blob: c677601c0d0e630294d7af2d0368ffef338e3f81 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "class_linker.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070018
Ian Rogers700a4022014-05-19 16:49:03 -070019#include <memory>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070020#include <string>
Vladimir Marko72101082019-02-05 16:16:30 +000021#include <string_view>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070022
Andreas Gampe9186ced2016-12-12 14:28:21 -080023#include "android-base/strings.h"
24
Mathieu Chartierc7853442015-03-27 14:35:38 -070025#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070026#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070027#include "base/enums.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "class_linker-inl.h"
Vladimir Marko5868ada2020-05-12 11:50:34 +010029#include "class_root-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080030#include "common_runtime_test.h"
David Sehr9e734c72018-01-04 17:56:19 -080031#include "dex/dex_file_types.h"
Andreas Gampead1aa632019-01-02 10:30:54 -080032#include "dex/signature-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080033#include "dex/standard_dex_file.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070036#include "gc/heap.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070037#include "handle_scope-inl.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070038#include "mirror/array-alloc-inl.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070039#include "mirror/accessible_object.h"
Orion Hodsonc069a302017-01-18 09:23:12 +000040#include "mirror/call_site.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/class-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070042#include "mirror/class_ext.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043#include "mirror/dex_cache.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010044#include "mirror/emulated_stack_frame.h"
Neil Fuller16b21cd2016-08-12 09:37:02 +010045#include "mirror/executable.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070046#include "mirror/field.h"
Narayan Kamathafa48272016-08-03 12:46:58 +010047#include "mirror/method_handle_impl.h"
Orion Hodsonc069a302017-01-18 09:23:12 +000048#include "mirror/method_handles_lookup.h"
Vladimir Marko5aead702019-03-27 11:00:36 +000049#include "mirror/method_type-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050#include "mirror/object-inl.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070051#include "mirror/object_array-alloc-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052#include "mirror/object_array-inl.h"
53#include "mirror/proxy.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070054#include "mirror/reference.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055#include "mirror/stack_trace_element.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070056#include "mirror/string-inl.h"
Orion Hodson005ac512017-10-24 15:43:43 +010057#include "mirror/var_handle.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070058#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070059#include "thread-current-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070060
61namespace art {
62
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080063class ClassLinkerTest : public CommonRuntimeTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070064 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070065 void AssertNonExistentClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070066 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -070067 Thread* self = Thread::Current();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070068 EXPECT_TRUE(class_linker_->FindSystemClass(self, descriptor.c_str()) == nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070069 EXPECT_TRUE(self->IsExceptionPending());
Vladimir Markoe9987b02018-05-22 16:26:43 +010070 StackHandleScope<1> hs(self);
71 Handle<mirror::Object> exception = hs.NewHandle<mirror::Object>(self->GetException());
Elliott Hughes885c3bd2011-08-22 16:59:20 -070072 self->ClearException();
Vladimir Markoe9987b02018-05-22 16:26:43 +010073 ObjPtr<mirror::Class> exception_class =
Ian Rogers98379392014-02-24 16:53:16 -080074 class_linker_->FindSystemClass(self, "Ljava/lang/NoClassDefFoundError;");
Elliott Hughes885c3bd2011-08-22 16:59:20 -070075 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070076 }
77
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078 void AssertPrimitiveClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070079 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -080080 Thread* self = Thread::Current();
81 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(self, descriptor.c_str()));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070082 }
83
Vladimir Markoe9987b02018-05-22 16:26:43 +010084 void AssertPrimitiveClass(const std::string& descriptor, ObjPtr<mirror::Class> primitive)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070085 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070086 ASSERT_TRUE(primitive != nullptr);
87 ASSERT_TRUE(primitive->GetClass() != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +010088 ASSERT_OBJ_PTR_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070089 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -070090 std::string temp;
91 ASSERT_STREQ(descriptor.c_str(), primitive->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070092 EXPECT_TRUE(primitive->GetSuperClass() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070093 EXPECT_FALSE(primitive->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070094 EXPECT_TRUE(primitive->GetClassLoader() == nullptr);
Vladimir Markobf121912019-06-04 13:49:05 +010095 EXPECT_EQ(ClassStatus::kVisiblyInitialized, primitive->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070096 EXPECT_FALSE(primitive->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070097 EXPECT_TRUE(primitive->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070098 EXPECT_TRUE(primitive->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +000099 EXPECT_FALSE(primitive->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700100 EXPECT_TRUE(primitive->IsVerified());
101 EXPECT_TRUE(primitive->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700102 EXPECT_FALSE(primitive->IsArrayInstance());
103 EXPECT_FALSE(primitive->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700104 EXPECT_TRUE(primitive->GetComponentType() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700105 EXPECT_FALSE(primitive->IsInterface());
106 EXPECT_TRUE(primitive->IsPublic());
107 EXPECT_TRUE(primitive->IsFinal());
108 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700109 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700110 EXPECT_EQ(0U, primitive->NumDirectMethods());
111 EXPECT_EQ(0U, primitive->NumVirtualMethods());
112 EXPECT_EQ(0U, primitive->NumInstanceFields());
113 EXPECT_EQ(0U, primitive->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700114 EXPECT_EQ(0U, primitive->NumDirectInterfaces());
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700115 EXPECT_FALSE(primitive->HasVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700116 EXPECT_EQ(0, primitive->GetIfTableCount());
Mathieu Chartier6beced42016-11-15 15:51:31 -0800117 EXPECT_TRUE(primitive->GetIfTable() != nullptr);
118 EXPECT_EQ(primitive->GetIfTable()->Count(), 0u);
Andreas Gampe5b20b352018-10-11 19:03:20 -0700119 EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract | kAccVerificationAttempted,
120 primitive->GetAccessFlags());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700121 }
122
Vladimir Markoe9987b02018-05-22 16:26:43 +0100123 void AssertObjectClass(ObjPtr<mirror::Class> JavaLangObject)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700124 REQUIRES_SHARED(Locks::mutator_lock_) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000125 ASSERT_TRUE(JavaLangObject != nullptr);
126 ASSERT_TRUE(JavaLangObject->GetClass() != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100127 ASSERT_OBJ_PTR_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100128 EXPECT_OBJ_PTR_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000129 std::string temp;
130 ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;");
131 EXPECT_TRUE(JavaLangObject->GetSuperClass() == nullptr);
132 EXPECT_FALSE(JavaLangObject->HasSuperClass());
133 EXPECT_TRUE(JavaLangObject->GetClassLoader() == nullptr);
Vladimir Markobf121912019-06-04 13:49:05 +0100134 class_linker_->MakeInitializedClassesVisiblyInitialized(Thread::Current(), /*wait=*/ true);
135 EXPECT_EQ(ClassStatus::kVisiblyInitialized, JavaLangObject->GetStatus());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000136 EXPECT_FALSE(JavaLangObject->IsErroneous());
137 EXPECT_TRUE(JavaLangObject->IsLoaded());
138 EXPECT_TRUE(JavaLangObject->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000139 EXPECT_FALSE(JavaLangObject->IsErroneousResolved());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000140 EXPECT_TRUE(JavaLangObject->IsVerified());
141 EXPECT_TRUE(JavaLangObject->IsInitialized());
142 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
143 EXPECT_FALSE(JavaLangObject->IsArrayClass());
144 EXPECT_TRUE(JavaLangObject->GetComponentType() == nullptr);
145 EXPECT_FALSE(JavaLangObject->IsInterface());
146 EXPECT_TRUE(JavaLangObject->IsPublic());
147 EXPECT_FALSE(JavaLangObject->IsFinal());
148 EXPECT_FALSE(JavaLangObject->IsPrimitive());
149 EXPECT_FALSE(JavaLangObject->IsSynthetic());
Nicolas Geoffray6e311ac2017-02-27 11:46:03 +0000150 EXPECT_EQ(4U, JavaLangObject->NumDirectMethods());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000151 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
152 if (!kUseBrooksReadBarrier) {
153 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
154 } else {
155 EXPECT_EQ(4U, JavaLangObject->NumInstanceFields());
156 }
157 EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(),
158 "shadow$_klass_");
159 EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(),
160 "shadow$_monitor_");
161 if (kUseBrooksReadBarrier) {
162 EXPECT_STREQ(JavaLangObject->GetInstanceField(2)->GetName(),
163 "shadow$_x_rb_ptr_");
164 EXPECT_STREQ(JavaLangObject->GetInstanceField(3)->GetName(),
165 "shadow$_x_xpadding_");
166 }
167
168 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
169 EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces());
170
Andreas Gampe542451c2016-07-26 09:02:02 -0700171 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000172 ArtMethod* unimplemented = runtime_->GetImtUnimplementedMethod();
173 ImTable* imt = JavaLangObject->GetImt(pointer_size);
174 ASSERT_NE(nullptr, imt);
175 for (size_t i = 0; i < ImTable::kSize; ++i) {
176 ASSERT_EQ(unimplemented, imt->Get(i, pointer_size));
177 }
178 }
179
Brian Carlstromaded5f72011-10-07 17:15:04 -0700180 void AssertArrayClass(const std::string& array_descriptor,
181 const std::string& component_type,
Vladimir Markoe9987b02018-05-22 16:26:43 +0100182 ObjPtr<mirror::ClassLoader> class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700183 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800184 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700185 StackHandleScope<2> hs(self);
186 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
187 Handle<mirror::Class> array(
188 hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader)));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700189 std::string temp;
190 EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp));
Vladimir Markoe9987b02018-05-22 16:26:43 +0100191 EXPECT_OBJ_PTR_EQ(loader.Get(), array->GetClassLoader());
Elliott Hughes00626c22013-06-14 15:04:14 -0700192 EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract)));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700193 AssertArrayClass(array_descriptor, array);
194 }
195
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700196 void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700197 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800198 ASSERT_TRUE(array != nullptr);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700199 ASSERT_TRUE(array->GetClass() != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100200 ASSERT_OBJ_PTR_EQ(array->GetClass(), array->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700201 EXPECT_TRUE(array->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700202 std::string temp;
203 ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700204 EXPECT_TRUE(array->GetSuperClass() != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800205 Thread* self = Thread::Current();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100206 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"),
207 array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700208 EXPECT_TRUE(array->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700209 ASSERT_TRUE(array->GetComponentType() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700210 ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U);
Vladimir Markobf121912019-06-04 13:49:05 +0100211 EXPECT_EQ(ClassStatus::kVisiblyInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700212 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700213 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700214 EXPECT_TRUE(array->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000215 EXPECT_FALSE(array->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700216 EXPECT_TRUE(array->IsVerified());
217 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700218 EXPECT_FALSE(array->IsArrayInstance());
219 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700220 EXPECT_FALSE(array->IsInterface());
221 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
222 EXPECT_TRUE(array->IsFinal());
223 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700224 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700225 EXPECT_EQ(0U, array->NumDirectMethods());
226 EXPECT_EQ(0U, array->NumVirtualMethods());
227 EXPECT_EQ(0U, array->NumInstanceFields());
228 EXPECT_EQ(0U, array->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700229 EXPECT_EQ(2U, array->NumDirectInterfaces());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000230 EXPECT_TRUE(array->ShouldHaveImt());
231 EXPECT_TRUE(array->ShouldHaveEmbeddedVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700232 EXPECT_EQ(2, array->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700233 ASSERT_TRUE(array->GetIfTable() != nullptr);
Vladimir Marko19a4d372016-12-08 14:41:46 +0000234 ObjPtr<mirror::Class> direct_interface0 =
235 mirror::Class::GetDirectInterface(self, array.Get(), 0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700236 EXPECT_TRUE(direct_interface0 != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700237 EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;");
Vladimir Marko19a4d372016-12-08 14:41:46 +0000238 ObjPtr<mirror::Class> direct_interface1 =
239 mirror::Class::GetDirectInterface(self, array.Get(), 1);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700240 EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;");
Vladimir Markobcf17522018-06-01 13:14:32 +0100241 EXPECT_OBJ_PTR_EQ(class_linker_->FindArrayClass(self, array->GetComponentType()), array.Get());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000242
Andreas Gampe542451c2016-07-26 09:02:02 -0700243 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100244 ObjPtr<mirror::Class> JavaLangObject =
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000245 class_linker_->FindSystemClass(self, "Ljava/lang/Object;");
246 ImTable* JavaLangObject_imt = JavaLangObject->GetImt(pointer_size);
247 // IMT of a array class should be shared with the IMT of the java.lag.Object
248 ASSERT_EQ(JavaLangObject_imt, array->GetImt(pointer_size));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700249 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700250
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700251 void AssertMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700252 EXPECT_TRUE(method != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700253 EXPECT_TRUE(method->GetDeclaringClass() != nullptr);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700254 EXPECT_TRUE(method->GetName() != nullptr);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700255 EXPECT_TRUE(method->GetSignature() != Signature::NoSignature());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700256 }
257
Mathieu Chartier3398c782016-09-30 10:27:43 -0700258 void AssertField(ObjPtr<mirror::Class> klass, ArtField* field)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700260 EXPECT_TRUE(field != nullptr);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700261 EXPECT_OBJ_PTR_EQ(klass, field->GetDeclaringClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700262 EXPECT_TRUE(field->GetName() != nullptr);
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000263 EXPECT_TRUE(field->ResolveType() != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700264 }
265
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700266 void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700267 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700268 std::string temp;
269 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800270 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700271 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700272 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700273 EXPECT_TRUE(klass->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700274 EXPECT_TRUE(klass->GetSuperClass() != nullptr);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700275 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700276 EXPECT_TRUE(klass->GetClass() != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100277 EXPECT_OBJ_PTR_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700278 EXPECT_TRUE(klass->GetDexCache() != nullptr);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700279 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700280 EXPECT_TRUE(klass->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000281 EXPECT_FALSE(klass->IsErroneousResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700282 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 EXPECT_FALSE(klass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700284 EXPECT_TRUE(klass->GetComponentType() == nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700285 EXPECT_TRUE(klass->IsInSamePackage(klass.Get()));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700286 std::string temp2;
287 EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp),
288 klass->GetDescriptor(&temp2)));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700289 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700290 EXPECT_TRUE(klass->IsAbstract());
Alex Light42d511d2016-02-03 13:45:41 -0800291 // Check that all direct methods are static (either <clinit> or a regular static method).
Andreas Gampe542451c2016-07-26 09:02:02 -0700292 for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) {
Alex Light42d511d2016-02-03 13:45:41 -0800293 EXPECT_TRUE(m.IsStatic());
294 EXPECT_TRUE(m.IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700295 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700296 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700297 if (!klass->IsSynthetic()) {
298 EXPECT_NE(0U, klass->NumDirectMethods());
299 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700300 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700301 EXPECT_EQ(klass->IsInterface(), !klass->HasVTable());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100302 ObjPtr<mirror::IfTable> iftable = klass->GetIfTable();
Brian Carlstrom86927212011-09-15 11:31:11 -0700303 for (int i = 0; i < klass->GetIfTableCount(); i++) {
Vladimir Markoe9987b02018-05-22 16:26:43 +0100304 ObjPtr<mirror::Class> interface = iftable->GetInterface(i);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700305 ASSERT_TRUE(interface != nullptr);
Brian Carlstrom86927212011-09-15 11:31:11 -0700306 if (klass->IsInterface()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700307 EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700308 } else {
Alex Light705ad492015-09-21 11:36:30 -0700309 EXPECT_EQ(interface->NumDeclaredVirtualMethods(), iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700310 }
311 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700312 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700313 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700314 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700315 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700316 }
317 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700318 EXPECT_FALSE(klass->IsAbstract());
319 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700320 }
321 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700322 EXPECT_FALSE(klass->IsFinal());
323 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700324 }
325
326 EXPECT_FALSE(klass->IsPrimitive());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700327 EXPECT_TRUE(klass->CanAccess(klass.Get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700328
Andreas Gampe542451c2016-07-26 09:02:02 -0700329 for (ArtMethod& method : klass->GetDirectMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700330 AssertMethod(&method);
331 EXPECT_TRUE(method.IsDirect());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100332 EXPECT_OBJ_PTR_EQ(klass.Get(), method.GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700333 }
334
Andreas Gampe542451c2016-07-26 09:02:02 -0700335 for (ArtMethod& method : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700336 AssertMethod(&method);
337 EXPECT_FALSE(method.IsDirect());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100338 EXPECT_OBJ_PTR_EQ(klass.Get(), method.GetDeclaringClass());
Alex Lighte64300b2015-12-15 15:02:47 -0800339 }
340
Andreas Gampe542451c2016-07-26 09:02:02 -0700341 for (ArtMethod& method : klass->GetCopiedMethods(kRuntimePointerSize)) {
Alex Lighte64300b2015-12-15 15:02:47 -0800342 AssertMethod(&method);
343 EXPECT_FALSE(method.IsDirect());
Alex Light36121492016-02-22 13:43:29 -0800344 EXPECT_TRUE(method.IsCopied());
Alex Lighte64300b2015-12-15 15:02:47 -0800345 EXPECT_TRUE(method.GetDeclaringClass()->IsInterface())
David Sehr709b0702016-10-13 09:12:37 -0700346 << "declaring class: " << method.GetDeclaringClass()->PrettyClass();
Alex Lighte64300b2015-12-15 15:02:47 -0800347 EXPECT_TRUE(method.GetDeclaringClass()->IsAssignableFrom(klass.Get()))
David Sehr709b0702016-10-13 09:12:37 -0700348 << "declaring class: " << method.GetDeclaringClass()->PrettyClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700349 }
350
351 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700352 ArtField* field = klass->GetInstanceField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700353 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700354 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700355 }
356
357 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700358 ArtField* field = klass->GetStaticField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700359 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700360 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700361 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700362
Vladimir Marko76649e82014-11-10 18:32:59 +0000363 // Confirm that all instances field offsets are packed together at the start.
Brian Carlstromae3ac012011-07-27 01:30:28 -0700364 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Vladimir Marko76649e82014-11-10 18:32:59 +0000365 MemberOffset start_ref_offset = klass->GetFirstReferenceInstanceFieldOffset();
366 MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
367 klass->NumReferenceInstanceFields() *
368 sizeof(mirror::HeapReference<mirror::Object>));
369 MemberOffset current_ref_offset = start_ref_offset;
370 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700371 ArtField* field = klass->GetInstanceField(i);
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000372 ObjPtr<mirror::Class> field_type = field->ResolveType();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700373 ASSERT_TRUE(field_type != nullptr);
Vladimir Marko76649e82014-11-10 18:32:59 +0000374 if (!field->IsPrimitiveType()) {
375 ASSERT_TRUE(!field_type->IsPrimitive());
376 ASSERT_EQ(current_ref_offset.Uint32Value(), field->GetOffset().Uint32Value());
377 if (current_ref_offset.Uint32Value() == end_ref_offset.Uint32Value()) {
378 // While Reference.referent is not primitive, the ClassLinker
379 // treats it as such so that the garbage collector won't scan it.
David Sehr709b0702016-10-13 09:12:37 -0700380 EXPECT_EQ(field->PrettyField(), "java.lang.Object java.lang.ref.Reference.referent");
Vladimir Marko76649e82014-11-10 18:32:59 +0000381 } else {
382 current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
383 sizeof(mirror::HeapReference<mirror::Object>));
384 }
385 } else {
386 if (field->GetOffset().Uint32Value() < end_ref_offset.Uint32Value()) {
387 // Shuffled before references.
388 ASSERT_LT(field->GetOffset().Uint32Value(), start_ref_offset.Uint32Value());
389 CHECK(!IsAligned<4>(field->GetOffset().Uint32Value()));
390 }
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700391 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700392 }
Vladimir Marko76649e82014-11-10 18:32:59 +0000393 ASSERT_EQ(end_ref_offset.Uint32Value(), current_ref_offset.Uint32Value());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700394
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700395 uint32_t total_num_reference_instance_fields = 0;
Vladimir Markoe9987b02018-05-22 16:26:43 +0100396 ObjPtr<mirror::Class> k = klass.Get();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700397 while (k != nullptr) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700398 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
399 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700400 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700401 EXPECT_GE(total_num_reference_instance_fields, 1U); // Should always have Object's class.
402 if (klass->GetReferenceInstanceOffsets() != mirror::Class::kClassWalkSuper) {
403 // The reference instance offsets have a bit set for each reference offset.
404 // +1 for Object's class.
405 EXPECT_EQ(static_cast<uint32_t>(POPCOUNT(klass->GetReferenceInstanceOffsets())) + 1,
406 total_num_reference_instance_fields);
407 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700408 }
409
Vladimir Markoe9987b02018-05-22 16:26:43 +0100410 void AssertDexFileClass(ObjPtr<mirror::ClassLoader> class_loader, const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700411 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markoe5125562019-02-06 17:38:26 +0000412 ASSERT_FALSE(descriptor.empty());
Ian Rogers98379392014-02-24 16:53:16 -0800413 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700414 StackHandleScope<1> hs(self);
415 Handle<mirror::Class> klass(
416 hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800417 ASSERT_TRUE(klass != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700418 std::string temp;
Vladimir Markoe9987b02018-05-22 16:26:43 +0100419 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
420 EXPECT_OBJ_PTR_EQ(class_loader, klass->GetClassLoader());
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700421 if (klass->IsPrimitive()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700422 AssertPrimitiveClass(descriptor, klass.Get());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700423 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700424 AssertArrayClass(descriptor, klass);
425 } else {
426 AssertClass(descriptor, klass);
427 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700428 }
429
Vladimir Markoe9987b02018-05-22 16:26:43 +0100430 void AssertDexFile(const DexFile& dex, ObjPtr<mirror::ClassLoader> class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700431 REQUIRES_SHARED(Locks::mutator_lock_) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700432 // Verify all the classes defined in this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800433 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800434 const dex::ClassDef& class_def = dex.GetClassDef(i);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800435 const char* descriptor = dex.GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700436 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700437 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700438 // Verify all the types referenced by this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800439 for (size_t i = 0; i < dex.NumTypeIds(); i++) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800440 const dex::TypeId& type_id = dex.GetTypeId(dex::TypeIndex(i));
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800441 const char* descriptor = dex.GetTypeDescriptor(type_id);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700442 AssertDexFileClass(class_loader, descriptor);
443 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700444 TestRootVisitor visitor;
445 class_linker_->VisitRoots(&visitor, kVisitRootFlagAllRoots);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700446 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700447
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700448 class TestRootVisitor : public SingleRootVisitor {
449 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100450 void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) override {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700451 EXPECT_TRUE(root != nullptr);
452 }
453 };
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700454};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700455
Narayan Kamath25352fc2016-08-03 12:46:58 +0100456class ClassLinkerMethodHandlesTest : public ClassLinkerTest {
457 protected:
Roland Levillainf73caca2018-08-24 17:19:07 +0100458 void SetUpRuntimeOptions(RuntimeOptions* options) override {
Narayan Kamath25352fc2016-08-03 12:46:58 +0100459 CommonRuntimeTest::SetUpRuntimeOptions(options);
Narayan Kamath25352fc2016-08-03 12:46:58 +0100460 }
461};
462
Brian Carlstrom693267a2011-09-06 09:25:34 -0700463struct CheckOffset {
464 size_t cpp_offset;
465 const char* java_name;
466 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
467};
468
Elliott Hughes80609252011-09-23 17:24:51 -0700469template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700470struct CheckOffsets {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800471 CheckOffsets(bool is_static_in, const char* class_descriptor_in)
472 : is_static(is_static_in), class_descriptor(class_descriptor_in) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700473 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700474 std::string class_descriptor;
475 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700476
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700477 bool Check() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -0800478 Thread* self = Thread::Current();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100479 ObjPtr<mirror::Class> klass =
Ian Rogers98379392014-02-24 16:53:16 -0800480 Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700481 CHECK(klass != nullptr) << class_descriptor;
Alex Lightf1b18fa2019-11-11 14:22:00 -0800482 CHECK(klass->IsMirrored()) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700483
484 bool error = false;
485
Mathieu Chartiere401d142015-04-22 13:56:20 -0700486 // Classes have a different size due to padding field. Strings are variable length.
487 if (!klass->IsClassClass() && !klass->IsStringClass() && !is_static) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700488 // Currently only required for AccessibleObject since of the padding fields. The class linker
489 // says AccessibleObject is 9 bytes but sizeof(AccessibleObject) is 12 bytes due to padding.
490 // The RoundUp is to get around this case.
491 static constexpr size_t kPackAlignment = 4;
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100492 size_t expected_size = RoundUp(is_static ? klass->GetClassSize() : klass->GetObjectSize(),
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700493 kPackAlignment);
Elliott Hughes80609252011-09-23 17:24:51 -0700494 if (sizeof(T) != expected_size) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800495 LOG(ERROR) << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700496 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700497 << " Java=" << expected_size
498 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700499 error = true;
500 }
501 }
502
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700503 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700504 if (offsets.size() != num_fields) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800505 LOG(ERROR) << "Field count mismatch:"
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700506 << " class=" << class_descriptor
507 << " Java=" << num_fields
508 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700509 error = true;
510 }
511
Brian Carlstromdbc05252011-09-09 01:59:59 -0700512 for (size_t i = 0; i < offsets.size(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700513 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Vladimir Marko72101082019-02-05 16:16:30 +0000514 std::string_view field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800515 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700516 error = true;
517 }
518 }
519 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700520 for (size_t i = 0; i < offsets.size(); i++) {
521 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700522 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Vladimir Marko72101082019-02-05 16:16:30 +0000523 std::string_view field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800524 if (field_name != offsets[i].java_name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800525 LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700526 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800527 LOG(ERROR) << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700528 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800529 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700530 << " CheckOffsets=" << offset.java_name;
531 }
532 }
533
Brian Carlstromdbc05252011-09-09 01:59:59 -0700534 for (size_t i = 0; i < offsets.size(); i++) {
535 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700536 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700537 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
538 error = true;
539 }
540 }
541 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700542 for (size_t i = 0; i < offsets.size(); i++) {
543 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700544 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700545 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800546 LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700547 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800548 LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700549 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
550 }
551 }
552
553 return !error;
554 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700555
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700556 void addOffset(size_t offset, const char* name) {
557 offsets.push_back(CheckOffset(offset, name));
558 }
559
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700560 private:
561 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700562};
563
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700564// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
565// is first since if it is failing, others are unlikely to succeed.
566
Brian Carlstromea46f952013-07-30 01:26:50 -0700567struct ObjectOffsets : public CheckOffsets<mirror::Object> {
568 ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700569 addOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_");
570 addOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_");
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700571#ifdef USE_BROOKS_READ_BARRIER
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700572 addOffset(OFFSETOF_MEMBER(mirror::Object, x_rb_ptr_), "shadow$_x_rb_ptr_");
573 addOffset(OFFSETOF_MEMBER(mirror::Object, x_xpadding_), "shadow$_x_xpadding_");
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800574#endif
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800575 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700576};
577
Brian Carlstromea46f952013-07-30 01:26:50 -0700578struct ClassOffsets : public CheckOffsets<mirror::Class> {
579 ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700580 addOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags");
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700581 addOffset(OFFSETOF_MEMBER(mirror::Class, class_flags_), "classFlags");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700582 addOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader");
583 addOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize");
584 addOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId");
585 addOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType");
Alex Lighte64300b2015-12-15 15:02:47 -0800586 addOffset(OFFSETOF_MEMBER(mirror::Class, copied_methods_offset_), "copiedMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700587 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700588 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex");
589 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex");
Alex Lightd6251582016-10-31 11:12:30 -0700590 addOffset(OFFSETOF_MEMBER(mirror::Class, ext_data_), "extData");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700591 addOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields");
592 addOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable");
Alex Lighte64300b2015-12-15 15:02:47 -0800593 addOffset(OFFSETOF_MEMBER(mirror::Class, methods_), "methods");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700594 addOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700595 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_),
596 "numReferenceInstanceFields");
597 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_),
598 "numReferenceStaticFields");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700599 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize");
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700600 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_alloc_fast_path_),
601 "objectSizeAllocFastPath");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700602 addOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType");
603 addOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_),
604 "referenceInstanceOffsets");
605 addOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields");
606 addOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status");
607 addOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass");
Alex Lighte64300b2015-12-15 15:02:47 -0800608 addOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_offset_), "virtualMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700609 addOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800610 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700611};
612
Alex Lightd6251582016-10-31 11:12:30 -0700613struct ClassExtOffsets : public CheckOffsets<mirror::ClassExt> {
614 ClassExtOffsets() : CheckOffsets<mirror::ClassExt>(false, "Ldalvik/system/ClassExt;") {
Alex Light79d6c802019-06-27 15:50:11 +0000615 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, instance_jfield_ids_), "instanceJfieldIDs");
616 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, jmethod_ids_), "jmethodIDs");
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000617 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_class_), "obsoleteClass");
Alex Lighta01de592016-11-15 10:43:06 -0800618 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_dex_caches_), "obsoleteDexCaches");
619 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_methods_), "obsoleteMethods");
Alex Light2f814aa2017-03-24 15:21:34 +0000620 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, original_dex_file_), "originalDexFile");
David Brazdil1a658632018-12-01 17:54:26 +0000621 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, pre_redefine_class_def_index_),
622 "preRedefineClassDefIndex");
623 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, pre_redefine_dex_file_ptr_),
624 "preRedefineDexFilePtr");
Alex Light79d6c802019-06-27 15:50:11 +0000625 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, static_jfield_ids_), "staticJfieldIDs");
Alex Lightd6251582016-10-31 11:12:30 -0700626 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, verify_error_), "verifyError");
627 }
628};
629
Brian Carlstromea46f952013-07-30 01:26:50 -0700630struct StringOffsets : public CheckOffsets<mirror::String> {
631 StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700632 addOffset(OFFSETOF_MEMBER(mirror::String, count_), "count");
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000633 addOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hash");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800634 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700635};
636
Brian Carlstromea46f952013-07-30 01:26:50 -0700637struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
638 ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000639 addOffset(OFFSETOF_MEMBER(mirror::Throwable, backtrace_), "backtrace");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700640 addOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause");
641 addOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700642 addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace");
643 addOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800644 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700645};
646
Brian Carlstromea46f952013-07-30 01:26:50 -0700647struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700648 StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(
649 false, "Ljava/lang/StackTraceElement;") {
650 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass");
651 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName");
652 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber");
653 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800654 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700655};
656
Brian Carlstromea46f952013-07-30 01:26:50 -0700657struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
658 ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700659 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, allocator_), "allocator");
Mathieu Chartier6b069532015-08-05 15:08:12 -0700660 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, class_table_), "classTable");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700661 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages");
662 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent");
663 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800664 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700665};
666
Brian Carlstromea46f952013-07-30 01:26:50 -0700667struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
668 ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700669 addOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800670 }
Jesse Wilson95caa792011-10-12 18:14:17 -0400671};
672
Brian Carlstromea46f952013-07-30 01:26:50 -0700673struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
674 DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
Andreas Gampe8a1a0f72020-03-03 16:07:45 -0800675 addOffset(OFFSETOF_MEMBER(mirror::DexCache, class_loader_), "classLoader");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700676 addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile");
677 addOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location");
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700678 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_preresolved_strings_), "numPreResolvedStrings");
Orion Hodsonc069a302017-01-18 09:23:12 +0000679 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_call_sites_), "numResolvedCallSites");
Vladimir Marko05792b92015-08-03 11:56:49 +0100680 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_fields_), "numResolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100681 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_method_types_), "numResolvedMethodTypes");
Vladimir Marko05792b92015-08-03 11:56:49 +0100682 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_methods_), "numResolvedMethods");
683 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_types_), "numResolvedTypes");
684 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_strings_), "numStrings");
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700685 addOffset(OFFSETOF_MEMBER(mirror::DexCache, preresolved_strings_), "preResolvedStrings");
Orion Hodsonc069a302017-01-18 09:23:12 +0000686 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_call_sites_), "resolvedCallSites");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700687 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100688 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_method_types_), "resolvedMethodTypes");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700689 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods");
690 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes");
691 addOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800692 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700693};
694
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700695struct ReferenceOffsets : public CheckOffsets<mirror::Reference> {
696 ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700697 addOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext");
698 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue");
699 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext");
700 addOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800701 }
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700702};
703
704struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700705 FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>(
706 false, "Ljava/lang/ref/FinalizerReference;") {
707 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next");
708 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev");
709 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800710 }
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700711};
712
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700713struct AccessibleObjectOffsets : public CheckOffsets<mirror::AccessibleObject> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700714 AccessibleObjectOffsets() : CheckOffsets<mirror::AccessibleObject>(
715 false, "Ljava/lang/reflect/AccessibleObject;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000716 addOffset(mirror::AccessibleObject::FlagOffset().Uint32Value(), "override");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800717 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700718};
719
720struct FieldOffsets : public CheckOffsets<mirror::Field> {
721 FieldOffsets() : CheckOffsets<mirror::Field>(false, "Ljava/lang/reflect/Field;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700722 addOffset(OFFSETOF_MEMBER(mirror::Field, access_flags_), "accessFlags");
Alex Light5e251ea2020-01-09 10:55:49 -0800723 addOffset(OFFSETOF_MEMBER(mirror::Field, art_field_index_), "artFieldIndex");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700724 addOffset(OFFSETOF_MEMBER(mirror::Field, declaring_class_), "declaringClass");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700725 addOffset(OFFSETOF_MEMBER(mirror::Field, offset_), "offset");
726 addOffset(OFFSETOF_MEMBER(mirror::Field, type_), "type");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800727 }
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700728};
729
Neil Fuller16b21cd2016-08-12 09:37:02 +0100730struct ExecutableOffsets : public CheckOffsets<mirror::Executable> {
731 ExecutableOffsets() : CheckOffsets<mirror::Executable>(
732 false, "Ljava/lang/reflect/Executable;") {
Neil Fuller0e844392016-09-08 13:43:31 +0100733 addOffset(OFFSETOF_MEMBER(mirror::Executable, access_flags_), "accessFlags");
734 addOffset(OFFSETOF_MEMBER(mirror::Executable, art_method_), "artMethod");
735 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_), "declaringClass");
736 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_of_overridden_method_),
737 "declaringClassOfOverriddenMethod");
738 addOffset(OFFSETOF_MEMBER(mirror::Executable, dex_method_index_), "dexMethodIndex");
Neil Fuller60458a02016-09-01 15:32:44 +0100739 addOffset(OFFSETOF_MEMBER(mirror::Executable, has_real_parameter_data_),
740 "hasRealParameterData");
741 addOffset(OFFSETOF_MEMBER(mirror::Executable, parameters_), "parameters");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800742 }
Neil Fuller16b21cd2016-08-12 09:37:02 +0100743};
744
Narayan Kamathafa48272016-08-03 12:46:58 +0100745struct MethodTypeOffsets : public CheckOffsets<mirror::MethodType> {
746 MethodTypeOffsets() : CheckOffsets<mirror::MethodType>(
747 false, "Ljava/lang/invoke/MethodType;") {
748 addOffset(OFFSETOF_MEMBER(mirror::MethodType, form_), "form");
749 addOffset(OFFSETOF_MEMBER(mirror::MethodType, method_descriptor_), "methodDescriptor");
750 addOffset(OFFSETOF_MEMBER(mirror::MethodType, p_types_), "ptypes");
751 addOffset(OFFSETOF_MEMBER(mirror::MethodType, r_type_), "rtype");
752 addOffset(OFFSETOF_MEMBER(mirror::MethodType, wrap_alt_), "wrapAlt");
753 }
754};
755
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000756struct MethodHandleOffsets : public CheckOffsets<mirror::MethodHandle> {
757 MethodHandleOffsets() : CheckOffsets<mirror::MethodHandle>(
758 false, "Ljava/lang/invoke/MethodHandle;") {
759 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, art_field_or_method_), "artFieldOrMethod");
760 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, cached_spread_invoker_),
761 "cachedSpreadInvoker");
762 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, handle_kind_), "handleKind");
763 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, nominal_type_), "nominalType");
764 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, method_type_), "type");
765 }
766};
767
Narayan Kamathafa48272016-08-03 12:46:58 +0100768struct MethodHandleImplOffsets : public CheckOffsets<mirror::MethodHandleImpl> {
769 MethodHandleImplOffsets() : CheckOffsets<mirror::MethodHandleImpl>(
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000770 false, "Ljava/lang/invoke/MethodHandleImpl;") {
771 addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, info_), "info");
Narayan Kamathafa48272016-08-03 12:46:58 +0100772 }
773};
774
Orion Hodsonc069a302017-01-18 09:23:12 +0000775struct MethodHandlesLookupOffsets : public CheckOffsets<mirror::MethodHandlesLookup> {
776 MethodHandlesLookupOffsets() : CheckOffsets<mirror::MethodHandlesLookup>(
777 false, "Ljava/lang/invoke/MethodHandles$Lookup;") {
778 addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, allowed_modes_), "allowedModes");
779 addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, lookup_class_), "lookupClass");
780 }
781};
782
Narayan Kamath000e1882016-10-24 17:14:25 +0100783struct EmulatedStackFrameOffsets : public CheckOffsets<mirror::EmulatedStackFrame> {
784 EmulatedStackFrameOffsets() : CheckOffsets<mirror::EmulatedStackFrame>(
785 false, "Ldalvik/system/EmulatedStackFrame;") {
Narayan Kamathb79bbd82017-01-16 17:48:28 +0000786 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, callsite_type_), "callsiteType");
Narayan Kamath000e1882016-10-24 17:14:25 +0100787 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, references_), "references");
788 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, stack_frame_), "stackFrame");
789 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, type_), "type");
790 }
791};
792
Orion Hodsonc069a302017-01-18 09:23:12 +0000793struct CallSiteOffsets : public CheckOffsets<mirror::CallSite> {
794 CallSiteOffsets() : CheckOffsets<mirror::CallSite>(
795 false, "Ljava/lang/invoke/CallSite;") {
796 addOffset(OFFSETOF_MEMBER(mirror::CallSite, target_), "target");
797 }
798};
799
Orion Hodson005ac512017-10-24 15:43:43 +0100800struct VarHandleOffsets : public CheckOffsets<mirror::VarHandle> {
801 VarHandleOffsets() : CheckOffsets<mirror::VarHandle>(
802 false, "Ljava/lang/invoke/VarHandle;") {
803 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, access_modes_bit_mask_), "accessModesBitMask");
804 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, coordinate_type0_), "coordinateType0");
805 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, coordinate_type1_), "coordinateType1");
806 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, var_type_), "varType");
807 }
808};
809
810struct FieldVarHandleOffsets : public CheckOffsets<mirror::FieldVarHandle> {
811 FieldVarHandleOffsets() : CheckOffsets<mirror::FieldVarHandle>(
812 false, "Ljava/lang/invoke/FieldVarHandle;") {
813 addOffset(OFFSETOF_MEMBER(mirror::FieldVarHandle, art_field_), "artField");
814 }
815};
816
Orion Hodsonffad6682018-01-02 10:18:58 +0000817struct ArrayElementVarHandleOffsets : public CheckOffsets<mirror::ArrayElementVarHandle> {
818 ArrayElementVarHandleOffsets() : CheckOffsets<mirror::ArrayElementVarHandle>(
819 false, "Ljava/lang/invoke/ArrayElementVarHandle;") {
820 }
821};
822
Orion Hodson005ac512017-10-24 15:43:43 +0100823struct ByteArrayViewVarHandleOffsets : public CheckOffsets<mirror::ByteArrayViewVarHandle> {
824 ByteArrayViewVarHandleOffsets() : CheckOffsets<mirror::ByteArrayViewVarHandle>(
825 false, "Ljava/lang/invoke/ByteArrayViewVarHandle;") {
826 addOffset(OFFSETOF_MEMBER(mirror::ByteArrayViewVarHandle, native_byte_order_),
827 "nativeByteOrder");
828 }
829};
830
831struct ByteBufferViewVarHandleOffsets : public CheckOffsets<mirror::ByteBufferViewVarHandle> {
832 ByteBufferViewVarHandleOffsets() : CheckOffsets<mirror::ByteBufferViewVarHandle>(
833 false, "Ljava/lang/invoke/ByteBufferViewVarHandle;") {
834 addOffset(OFFSETOF_MEMBER(mirror::ByteBufferViewVarHandle, native_byte_order_),
835 "nativeByteOrder");
836 }
837};
838
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400839// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400840// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700841// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400842TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700843 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700844 EXPECT_TRUE(ObjectOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700845 EXPECT_TRUE(ClassOffsets().Check());
Alex Lightd6251582016-10-31 11:12:30 -0700846 EXPECT_TRUE(ClassExtOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700847 EXPECT_TRUE(StringOffsets().Check());
848 EXPECT_TRUE(ThrowableOffsets().Check());
849 EXPECT_TRUE(StackTraceElementOffsets().Check());
850 EXPECT_TRUE(ClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400851 EXPECT_TRUE(ProxyOffsets().Check());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700852 EXPECT_TRUE(DexCacheOffsets().Check());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700853 EXPECT_TRUE(ReferenceOffsets().Check());
854 EXPECT_TRUE(FinalizerReferenceOffsets().Check());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700855 EXPECT_TRUE(AccessibleObjectOffsets().Check());
856 EXPECT_TRUE(FieldOffsets().Check());
Neil Fuller16b21cd2016-08-12 09:37:02 +0100857 EXPECT_TRUE(ExecutableOffsets().Check());
Narayan Kamathafa48272016-08-03 12:46:58 +0100858 EXPECT_TRUE(MethodTypeOffsets().Check());
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000859 EXPECT_TRUE(MethodHandleOffsets().Check());
Narayan Kamathafa48272016-08-03 12:46:58 +0100860 EXPECT_TRUE(MethodHandleImplOffsets().Check());
Orion Hodsonc069a302017-01-18 09:23:12 +0000861 EXPECT_TRUE(MethodHandlesLookupOffsets().Check());
Narayan Kamath000e1882016-10-24 17:14:25 +0100862 EXPECT_TRUE(EmulatedStackFrameOffsets().Check());
Orion Hodsonc069a302017-01-18 09:23:12 +0000863 EXPECT_TRUE(CallSiteOffsets().Check());
Orion Hodson005ac512017-10-24 15:43:43 +0100864 EXPECT_TRUE(VarHandleOffsets().Check());
865 EXPECT_TRUE(FieldVarHandleOffsets().Check());
Orion Hodsonffad6682018-01-02 10:18:58 +0000866 EXPECT_TRUE(ArrayElementVarHandleOffsets().Check());
Orion Hodson005ac512017-10-24 15:43:43 +0100867 EXPECT_TRUE(ByteArrayViewVarHandleOffsets().Check());
868 EXPECT_TRUE(ByteBufferViewVarHandleOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700869}
870
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700871TEST_F(ClassLinkerTest, FindClassNonexistent) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700872 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700873 AssertNonExistentClass("NoSuchClass;");
874 AssertNonExistentClass("LNoSuchClass;");
875}
876
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700877TEST_F(ClassLinkerTest, GetDexFiles) {
878 ScopedObjectAccess soa(Thread::Current());
879
880 jobject jclass_loader = LoadDex("Nested");
881 std::vector<const DexFile*> dex_files(GetDexFiles(jclass_loader));
882 ASSERT_EQ(dex_files.size(), 1U);
Andreas Gampe9186ced2016-12-12 14:28:21 -0800883 EXPECT_TRUE(android::base::EndsWith(dex_files[0]->GetLocation(), "Nested.jar"));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700884
885 jobject jclass_loader2 = LoadDex("MultiDex");
886 std::vector<const DexFile*> dex_files2(GetDexFiles(jclass_loader2));
887 ASSERT_EQ(dex_files2.size(), 2U);
Andreas Gampe9186ced2016-12-12 14:28:21 -0800888 EXPECT_TRUE(android::base::EndsWith(dex_files2[0]->GetLocation(), "MultiDex.jar"));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700889}
890
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700891TEST_F(ClassLinkerTest, FindClassNested) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700892 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700893 StackHandleScope<1> hs(soa.Self());
894 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700895 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Nested"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700896
Vladimir Markoe9987b02018-05-22 16:26:43 +0100897 ObjPtr<mirror::Class> outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700898 ASSERT_TRUE(outer != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700899 EXPECT_EQ(0U, outer->NumVirtualMethods());
900 EXPECT_EQ(1U, outer->NumDirectMethods());
901
Vladimir Markoe9987b02018-05-22 16:26:43 +0100902 ObjPtr<mirror::Class> inner =
903 class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700904 ASSERT_TRUE(inner != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700905 EXPECT_EQ(0U, inner->NumVirtualMethods());
906 EXPECT_EQ(1U, inner->NumDirectMethods());
907}
908
909TEST_F(ClassLinkerTest, FindClass_Primitives) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700910 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700911 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800912 for (int ch = 1; ch < 256; ++ch) {
913 std::string descriptor;
914 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700915 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700916 AssertNonExistentClass(descriptor);
917 } else {
918 AssertPrimitiveClass(descriptor);
919 }
920 }
921}
922
923TEST_F(ClassLinkerTest, FindClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700924 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100925 StackHandleScope<2> hs(soa.Self());
926 Handle<mirror::Class> JavaLangObject = hs.NewHandle(
927 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
928 AssertObjectClass(JavaLangObject.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700929
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700930 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700931 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700932 AssertNonExistentClass("LMyClass;");
Vladimir Markoe9987b02018-05-22 16:26:43 +0100933 ObjPtr<mirror::Class> MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700934 ASSERT_TRUE(MyClass != nullptr);
935 ASSERT_TRUE(MyClass->GetClass() != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100936 ASSERT_OBJ_PTR_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100937 EXPECT_OBJ_PTR_EQ(JavaLangObject.Get(), MyClass->GetClass()->GetSuperClass());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000938 std::string temp;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700939 ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;");
Vladimir Markoe9987b02018-05-22 16:26:43 +0100940 EXPECT_OBJ_PTR_EQ(MyClass->GetSuperClass(), JavaLangObject.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700941 EXPECT_TRUE(MyClass->HasSuperClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100942 EXPECT_OBJ_PTR_EQ(class_loader.Get(), MyClass->GetClassLoader());
Vladimir Marko2c64a832018-01-04 11:31:56 +0000943 EXPECT_EQ(ClassStatus::kResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700944 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700945 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700946 EXPECT_TRUE(MyClass->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000947 EXPECT_FALSE(MyClass->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700948 EXPECT_FALSE(MyClass->IsVerified());
949 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700950 EXPECT_FALSE(MyClass->IsArrayInstance());
951 EXPECT_FALSE(MyClass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700952 EXPECT_TRUE(MyClass->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700953 EXPECT_FALSE(MyClass->IsInterface());
954 EXPECT_FALSE(MyClass->IsPublic());
955 EXPECT_FALSE(MyClass->IsFinal());
956 EXPECT_FALSE(MyClass->IsPrimitive());
957 EXPECT_FALSE(MyClass->IsSynthetic());
958 EXPECT_EQ(1U, MyClass->NumDirectMethods());
959 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
960 EXPECT_EQ(0U, MyClass->NumInstanceFields());
961 EXPECT_EQ(0U, MyClass->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700962 EXPECT_EQ(0U, MyClass->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700963
Vladimir Markodfc0de72019-04-01 10:57:55 +0100964 EXPECT_OBJ_PTR_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700965
966 // created by class_linker
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700967 AssertArrayClass("[C", "C", nullptr);
968 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700969 // synthesized on the fly
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700970 AssertArrayClass("[[C", "[C", nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700971 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700972 // or not available at all
973 AssertNonExistentClass("[[[[LNonExistentClass;");
974}
975
Mathieu Chartierb8901302016-09-30 10:27:43 -0700976TEST_F(ClassLinkerTest, LookupResolvedType) {
977 ScopedObjectAccess soa(Thread::Current());
978 StackHandleScope<1> hs(soa.Self());
979 Handle<mirror::ClassLoader> class_loader(
980 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
981 AssertNonExistentClass("LMyClass;");
982 ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800983 dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_;
Mathieu Chartierb8901302016-09-30 10:27:43 -0700984 ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
Mathieu Chartierb8901302016-09-30 10:27:43 -0700985 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000986 class_linker_->LookupResolvedType(type_idx, dex_cache, class_loader.Get()),
Mathieu Chartierb8901302016-09-30 10:27:43 -0700987 klass);
988 // Zero out the resolved type and make sure LookupResolvedType still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000989 dex_cache->ClearResolvedType(type_idx);
Mathieu Chartierb8901302016-09-30 10:27:43 -0700990 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
991 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000992 class_linker_->LookupResolvedType(type_idx, dex_cache, class_loader.Get()),
Mathieu Chartierb8901302016-09-30 10:27:43 -0700993 klass);
994}
995
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000996TEST_F(ClassLinkerTest, LookupResolvedTypeArray) {
997 ScopedObjectAccess soa(Thread::Current());
998 StackHandleScope<2> hs(soa.Self());
999 Handle<mirror::ClassLoader> class_loader(
1000 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("AllFields"))));
1001 // Get the AllFields class for the dex cache and dex file.
1002 ObjPtr<mirror::Class> all_fields_klass
1003 = class_linker_->FindClass(soa.Self(), "LAllFields;", class_loader);
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001004 ASSERT_TRUE(all_fields_klass != nullptr);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001005 Handle<mirror::DexCache> dex_cache = hs.NewHandle(all_fields_klass->GetDexCache());
1006 const DexFile& dex_file = *dex_cache->GetDexFile();
1007 // Get the index of the array class we want to test.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001008 const dex::TypeId* array_id = dex_file.FindTypeId("[Ljava/lang/Object;");
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001009 ASSERT_TRUE(array_id != nullptr);
1010 dex::TypeIndex array_idx = dex_file.GetIndexForTypeId(*array_id);
1011 // Check that the array class wasn't resolved yet.
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001012 EXPECT_TRUE(
1013 class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()) == nullptr);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001014 // Resolve the array class we want to test.
1015 ObjPtr<mirror::Class> array_klass
1016 = class_linker_->FindClass(soa.Self(), "[Ljava/lang/Object;", class_loader);
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001017 ASSERT_TRUE(array_klass != nullptr);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001018 // Test that LookupResolvedType() finds the array class.
1019 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001020 class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001021 array_klass);
1022 // Zero out the resolved type and make sure LookupResolvedType() still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001023 dex_cache->ClearResolvedType(array_idx);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001024 EXPECT_TRUE(dex_cache->GetResolvedType(array_idx) == nullptr);
1025 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001026 class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001027 array_klass);
1028}
1029
Vladimir Marko72ab6842017-01-20 19:32:50 +00001030TEST_F(ClassLinkerTest, LookupResolvedTypeErroneousInit) {
1031 ScopedObjectAccess soa(Thread::Current());
1032 StackHandleScope<3> hs(soa.Self());
1033 Handle<mirror::ClassLoader> class_loader(
1034 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("ErroneousInit"))));
1035 AssertNonExistentClass("LErroneousInit;");
1036 Handle<mirror::Class> klass =
1037 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LErroneousInit;", class_loader));
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001038 ASSERT_TRUE(klass != nullptr);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001039 dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_;
1040 Handle<mirror::DexCache> dex_cache = hs.NewHandle(klass->GetDexCache());
Vladimir Marko72ab6842017-01-20 19:32:50 +00001041 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001042 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001043 klass.Get());
1044 // Zero out the resolved type and make sure LookupResolvedType still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001045 dex_cache->ClearResolvedType(type_idx);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001046 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
1047 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001048 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001049 klass.Get());
1050 // Force initialization to turn the class erroneous.
1051 bool initialized = class_linker_->EnsureInitialized(soa.Self(),
1052 klass,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001053 /* can_init_fields= */ true,
1054 /* can_init_parents= */ true);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001055 EXPECT_FALSE(initialized);
1056 EXPECT_TRUE(soa.Self()->IsExceptionPending());
1057 soa.Self()->ClearException();
1058 // Check that the LookupResolvedType() can still find the resolved type.
1059 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001060 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001061 klass.Get());
1062 // Zero out the resolved type and make sure LookupResolvedType() still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001063 dex_cache->ClearResolvedType(type_idx);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001064 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
1065 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001066 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001067 klass.Get());
1068}
1069
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001070TEST_F(ClassLinkerTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001071 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001072 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
1073 AssertDexFile(*java_lang_dex_file_, nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001074}
1075
Ian Rogersa15e67d2012-02-28 13:51:55 -08001076// The first reference array element must be a multiple of 4 bytes from the
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001077// start of the object
1078TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001079 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001080 ObjPtr<mirror::Class> array_class =
1081 class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
1082 ObjPtr<mirror::ObjectArray<mirror::String>> array =
Brian Carlstromea46f952013-07-30 01:26:50 -07001083 mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001084 uintptr_t data_offset =
1085 reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>),
1086 0));
1087 if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001088 EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment.
1089 } else {
1090 EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment.
1091 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001092}
1093
1094TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001095 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001096 StackHandleScope<5> hs(soa.Self());
1097 Handle<mirror::LongArray> long_array(hs.NewHandle(mirror::LongArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001098 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001099 uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
1100 EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment
1101
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001102 Handle<mirror::DoubleArray> double_array(hs.NewHandle(mirror::DoubleArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001103 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001104 data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
1105 EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment
1106
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001107 Handle<mirror::IntArray> int_array(hs.NewHandle(mirror::IntArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001108 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001109 data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
1110 EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment
1111
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001112 Handle<mirror::CharArray> char_array(hs.NewHandle(mirror::CharArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001113 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001114 data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
1115 EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment
1116
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001117 Handle<mirror::ShortArray> short_array(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001118 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001119 data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
1120 EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment
1121
1122 // Take it as given that bytes and booleans have byte alignment
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001123}
1124
Elliott Hughes33203b52011-09-20 19:42:01 -07001125TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
1126 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
1127 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001128 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -08001129 ScopedNullHandle<mirror::ClassLoader> class_loader;
Vladimir Markoe9987b02018-05-22 16:26:43 +01001130 ObjPtr<mirror::Class> c;
Ian Rogers98379392014-02-24 16:53:16 -08001131 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001132 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001133 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001134 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001135 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001136 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001137 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001138 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001139 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001140 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001141 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001142 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001143 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Long;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001144 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001145 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Short;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001146 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -07001147}
1148
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001149TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001150 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001151 StackHandleScope<3> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001152 Handle<mirror::ClassLoader> class_loader_1(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001153 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001154 Handle<mirror::ClassLoader> class_loader_2(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001155 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001156 Handle<mirror::Class> MyClass_1 = hs.NewHandle(
1157 class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1));
1158 ObjPtr<mirror::Class> MyClass_2 =
1159 class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_2);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001160 EXPECT_TRUE(MyClass_1 != nullptr);
1161 EXPECT_TRUE(MyClass_2 != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001162 EXPECT_OBJ_PTR_NE(MyClass_1.Get(), MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -04001163}
1164
Jesse Wilson7833bd22011-08-09 18:31:44 -04001165TEST_F(ClassLinkerTest, StaticFields) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001166 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001167 StackHandleScope<2> hs(soa.Self());
1168 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001169 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001170 Handle<mirror::Class> statics(
1171 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Ian Rogers7b078e82014-09-10 14:44:24 -07001172 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001173
jeffhaoabcfde32011-09-29 15:05:18 -07001174 // Static final primitives that are initialized by a compile-time constant
1175 // expression resolve to a copy of a constant value from the constant pool.
1176 // So <clinit> should be null.
Vladimir Markoba118822017-06-12 15:41:56 +01001177 ArtMethod* clinit = statics->FindClassMethod("<clinit>", "()V", kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001178 EXPECT_TRUE(clinit == nullptr);
jeffhaoabcfde32011-09-29 15:05:18 -07001179
1180 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -04001181
Vladimir Marko19a4d372016-12-08 14:41:46 +00001182 ArtField* s0 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s0", "Z");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001183 EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001184 EXPECT_EQ(true, s0->GetBoolean(statics.Get()));
1185 s0->SetBoolean<false>(statics.Get(), false);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001186
Vladimir Marko19a4d372016-12-08 14:41:46 +00001187 ArtField* s1 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s1", "B");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001188 EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001189 EXPECT_EQ(5, s1->GetByte(statics.Get()));
1190 s1->SetByte<false>(statics.Get(), 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001191
Vladimir Marko19a4d372016-12-08 14:41:46 +00001192 ArtField* s2 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s2", "C");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001193 EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001194 EXPECT_EQ('a', s2->GetChar(statics.Get()));
1195 s2->SetChar<false>(statics.Get(), 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -04001196
Vladimir Marko19a4d372016-12-08 14:41:46 +00001197 ArtField* s3 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s3", "S");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001198 EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001199 EXPECT_EQ(-536, s3->GetShort(statics.Get()));
1200 s3->SetShort<false>(statics.Get(), -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001201
Vladimir Marko19a4d372016-12-08 14:41:46 +00001202 ArtField* s4 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s4", "I");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001203 EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001204 EXPECT_EQ(2000000000, s4->GetInt(statics.Get()));
1205 s4->SetInt<false>(statics.Get(), 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001206
Vladimir Marko19a4d372016-12-08 14:41:46 +00001207 ArtField* s5 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s5", "J");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001208 EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001209 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get()));
1210 s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001211
Vladimir Marko19a4d372016-12-08 14:41:46 +00001212 ArtField* s6 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s6", "F");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001213 EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -07001214 EXPECT_DOUBLE_EQ(0.5, s6->GetFloat(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001215 s6->SetFloat<false>(statics.Get(), 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001216
Vladimir Marko19a4d372016-12-08 14:41:46 +00001217 ArtField* s7 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s7", "D");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001218 EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -07001219 EXPECT_DOUBLE_EQ(16777217.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001220 s7->SetDouble<false>(statics.Get(), 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001221
Vladimir Marko19a4d372016-12-08 14:41:46 +00001222 ArtField* s8 = mirror::Class::FindStaticField(
1223 soa.Self(), statics.Get(), "s8", "Ljava/lang/String;");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001224 EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001225 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android"));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001226 ObjPtr<mirror::String> str_value = mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot");
Mathieu Chartier3398c782016-09-30 10:27:43 -07001227 s8->SetObject<false>(s8->GetDeclaringClass(), str_value);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001228
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001229 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
1230 // http://code.google.com/p/googletest/issues/detail?id=322
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001231 EXPECT_FALSE(s0->GetBoolean(statics.Get()));
1232 EXPECT_EQ(6, s1->GetByte(statics.Get()));
1233 EXPECT_EQ('b', s2->GetChar(statics.Get()));
1234 EXPECT_EQ(-535, s3->GetShort(statics.Get()));
1235 EXPECT_EQ(2000000001, s4->GetInt(statics.Get()));
1236 EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get()));
Ian Rogers647b1a82014-10-10 11:02:11 -07001237 EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get()));
1238 EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001239 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001240}
1241
Brian Carlstrom30b94452011-08-25 21:35:26 -07001242TEST_F(ClassLinkerTest, Interfaces) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001243 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001244 StackHandleScope<6> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001245 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001246 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Interfaces"))));
Mathieu Chartierf8322842014-05-16 10:59:25 -07001247 Handle<mirror::Class> I(
1248 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
1249 Handle<mirror::Class> J(
1250 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
1251 Handle<mirror::Class> K(
1252 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader)));
1253 Handle<mirror::Class> A(
1254 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader)));
1255 Handle<mirror::Class> B(
1256 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader)));
1257 EXPECT_TRUE(I->IsAssignableFrom(A.Get()));
1258 EXPECT_TRUE(J->IsAssignableFrom(A.Get()));
1259 EXPECT_TRUE(J->IsAssignableFrom(K.Get()));
1260 EXPECT_TRUE(K->IsAssignableFrom(B.Get()));
1261 EXPECT_TRUE(J->IsAssignableFrom(B.Get()));
Brian Carlstrom30b94452011-08-25 21:35:26 -07001262
Vladimir Markoeb37ba52019-02-05 14:10:38 +00001263 const std::string_view void_sig("()V");
Vladimir Markoba118822017-06-12 15:41:56 +01001264 ArtMethod* Ii = I->FindClassMethod("i", void_sig, kRuntimePointerSize);
1265 ArtMethod* Jj1 = J->FindClassMethod("j1", void_sig, kRuntimePointerSize);
1266 ArtMethod* Jj2 = J->FindClassMethod("j2", void_sig, kRuntimePointerSize);
Andreas Gampe542451c2016-07-26 09:02:02 -07001267 ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig, kRuntimePointerSize);
1268 ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig, kRuntimePointerSize);
1269 ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig, kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001270 ArtMethod* Ai = A->FindClassMethod("i", void_sig, kRuntimePointerSize);
1271 ArtMethod* Aj1 = A->FindClassMethod("j1", void_sig, kRuntimePointerSize);
1272 ArtMethod* Aj2 = A->FindClassMethod("j2", void_sig, kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001273 ASSERT_TRUE(Ii != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001274 ASSERT_FALSE(Ii->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001275 ASSERT_TRUE(Jj1 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001276 ASSERT_FALSE(Jj1->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001277 ASSERT_TRUE(Jj2 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001278 ASSERT_FALSE(Jj2->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001279 ASSERT_TRUE(Kj1 != nullptr);
1280 ASSERT_TRUE(Kj2 != nullptr);
1281 ASSERT_TRUE(Kk != nullptr);
1282 ASSERT_TRUE(Ai != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001283 ASSERT_FALSE(Ai->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001284 ASSERT_TRUE(Aj1 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001285 ASSERT_FALSE(Aj1->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001286 ASSERT_TRUE(Aj2 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001287 ASSERT_FALSE(Aj2->IsDirect());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001288 EXPECT_NE(Ii, Ai);
1289 EXPECT_NE(Jj1, Aj1);
1290 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -07001291 EXPECT_EQ(Kj1, Jj1);
1292 EXPECT_EQ(Kj2, Jj2);
Andreas Gampe542451c2016-07-26 09:02:02 -07001293 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, kRuntimePointerSize));
1294 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, kRuntimePointerSize));
1295 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, kRuntimePointerSize));
1296 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii, kRuntimePointerSize));
1297 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1, kRuntimePointerSize));
1298 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2, kRuntimePointerSize));
Ian Rogersd24e2642012-06-06 21:21:43 -07001299
Vladimir Marko19a4d372016-12-08 14:41:46 +00001300 ArtField* Afoo =
1301 mirror::Class::FindStaticField(soa.Self(), A.Get(), "foo", "Ljava/lang/String;");
1302 ArtField* Bfoo =
1303 mirror::Class::FindStaticField(soa.Self(), B.Get(), "foo", "Ljava/lang/String;");
1304 ArtField* Jfoo =
1305 mirror::Class::FindStaticField(soa.Self(), J.Get(), "foo", "Ljava/lang/String;");
1306 ArtField* Kfoo =
1307 mirror::Class::FindStaticField(soa.Self(), K.Get(), "foo", "Ljava/lang/String;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001308 ASSERT_TRUE(Afoo != nullptr);
Ian Rogersd24e2642012-06-06 21:21:43 -07001309 EXPECT_EQ(Afoo, Bfoo);
1310 EXPECT_EQ(Afoo, Jfoo);
1311 EXPECT_EQ(Afoo, Kfoo);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001312}
1313
Elliott Hughesf3778f62012-01-26 14:14:35 -08001314TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -07001315 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001316
jeffhaoabcfde32011-09-29 15:05:18 -07001317 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
1318 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001319
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001320 ScopedObjectAccess soa(Thread::Current());
1321 jobject jclass_loader = LoadDex("StaticsFromCode");
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07001322 const DexFile* dex_file = GetFirstDexFile(jclass_loader);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001323 StackHandleScope<1> hs(soa.Self());
1324 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001325 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001326 ObjPtr<mirror::Class> klass =
1327 class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader);
Andreas Gampe542451c2016-07-26 09:02:02 -07001328 ArtMethod* clinit = klass->FindClassInitializer(kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001329 ArtMethod* getS0 =
1330 klass->FindClassMethod("getS0", "()Ljava/lang/Object;", kRuntimePointerSize);
1331 ASSERT_TRUE(getS0 != nullptr);
1332 ASSERT_TRUE(getS0->IsStatic());
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001333 const dex::TypeId* type_id = dex_file->FindTypeId("LStaticsFromCode;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001334 ASSERT_TRUE(type_id != nullptr);
Andreas Gampea5b09a62016-11-17 15:21:22 -08001335 dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id);
Vladimir Marko28e012a2017-12-07 11:22:59 +00001336 ObjPtr<mirror::Class> uninit = ResolveVerifyAndClinit(type_idx,
1337 clinit,
1338 soa.Self(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001339 /* can_run_clinit= */ true,
1340 /* verify_access= */ false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001341 EXPECT_TRUE(uninit != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001342 EXPECT_FALSE(uninit->IsInitialized());
Vladimir Marko28e012a2017-12-07 11:22:59 +00001343 ObjPtr<mirror::Class> init = ResolveVerifyAndClinit(type_idx,
1344 getS0,
1345 soa.Self(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001346 /* can_run_clinit= */ true,
1347 /* verify_access= */ false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001348 EXPECT_TRUE(init != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001349 EXPECT_TRUE(init->IsInitialized());
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001350}
1351
Mathieu Chartier1e4841e2016-12-15 14:21:04 -08001352TEST_F(ClassLinkerTest, ErroneousClass) {
1353 ScopedObjectAccess soa(Thread::Current());
1354 jobject jclass_loader = LoadMultiDex("ErroneousA", "ErroneousB");
1355 StackHandleScope<1> hs(soa.Self());
1356 Handle<mirror::ClassLoader> class_loader(
1357 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
1358 hs.Self()->AssertNoPendingException();
1359 const char* descriptor = "LErroneous;";
1360 ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), descriptor, class_loader);
1361 // Erronenous since we are extending final class.
1362 hs.Self()->AssertPendingException();
1363 EXPECT_TRUE(klass == nullptr);
1364 klass = class_linker_->LookupClass(soa.Self(), descriptor, class_loader.Get());
1365 EXPECT_FALSE(klass == nullptr);
1366 EXPECT_TRUE(klass->IsErroneous());
1367 EXPECT_TRUE(klass->GetIfTable() != nullptr);
1368}
1369
Elliott Hughes20cde902011-10-04 17:37:27 -07001370TEST_F(ClassLinkerTest, FinalizableBit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001371 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001372 ObjPtr<mirror::Class> c;
Elliott Hughes20cde902011-10-04 17:37:27 -07001373
1374 // Object has a finalize method, but we know it's empty.
Ian Rogers98379392014-02-24 16:53:16 -08001375 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001376 EXPECT_FALSE(c->IsFinalizable());
1377
1378 // Enum has a finalize method to prevent its subclasses from implementing one.
Ian Rogers98379392014-02-24 16:53:16 -08001379 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001380 EXPECT_FALSE(c->IsFinalizable());
1381
1382 // RoundingMode is an enum.
Ian Rogers98379392014-02-24 16:53:16 -08001383 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001384 EXPECT_FALSE(c->IsFinalizable());
1385
1386 // RandomAccessFile extends Object and overrides finalize.
Ian Rogers98379392014-02-24 16:53:16 -08001387 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001388 EXPECT_TRUE(c->IsFinalizable());
1389
1390 // FileInputStream is finalizable and extends InputStream which isn't.
Ian Rogers98379392014-02-24 16:53:16 -08001391 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001392 EXPECT_FALSE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001393 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001394 EXPECT_TRUE(c->IsFinalizable());
1395
1396 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1397 // extends ThreadPoolExecutor which does.
Ian Rogers98379392014-02-24 16:53:16 -08001398 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001399 EXPECT_TRUE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001400 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001401 EXPECT_TRUE(c->IsFinalizable());
1402}
1403
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001404TEST_F(ClassLinkerTest, ClassRootDescriptors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001405 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001406 std::string temp;
Vladimir Markob4eb1b12018-05-24 11:09:38 +01001407 for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) {
1408 ObjPtr<mirror::Class> klass = GetClassRoot(ClassRoot(i), class_linker_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07001409 EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U);
Vladimir Markob4eb1b12018-05-24 11:09:38 +01001410 EXPECT_STREQ(klass->GetDescriptor(&temp), GetClassRootDescriptor(ClassRoot(i))) << " i = " << i;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001411 }
1412}
1413
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001414TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) {
1415 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -08001416 ScopedNullHandle<mirror::ClassLoader> class_loader;
Vladimir Markoe9987b02018-05-22 16:26:43 +01001417 ObjPtr<mirror::Class> c;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001418
1419 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001420 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001421 EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001422
1423 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001424 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001425 EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001426
1427 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001428 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001429 EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001430
1431 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001432 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001433 EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001434}
1435
Mathieu Chartiere401d142015-04-22 13:56:20 -07001436static void CheckMethod(ArtMethod* method, bool verified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001437 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe48498592014-09-10 19:48:05 -07001438 if (!method->IsNative() && !method->IsAbstract()) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001439 EXPECT_EQ((method->GetAccessFlags() & kAccSkipAccessChecks) != 0U, verified)
David Sehr709b0702016-10-13 09:12:37 -07001440 << method->PrettyMethod(true);
Andreas Gampe48498592014-09-10 19:48:05 -07001441 }
1442}
1443
Vladimir Markoe9987b02018-05-22 16:26:43 +01001444static void CheckVerificationAttempted(ObjPtr<mirror::Class> c, bool preverified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001445 REQUIRES_SHARED(Locks::mutator_lock_) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001446 EXPECT_EQ((c->GetAccessFlags() & kAccVerificationAttempted) != 0U, preverified)
David Sehr709b0702016-10-13 09:12:37 -07001447 << "Class " << mirror::Class::PrettyClass(c) << " not as expected";
Andreas Gampe542451c2016-07-26 09:02:02 -07001448 for (auto& m : c->GetMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001449 CheckMethod(&m, preverified);
Andreas Gampe48498592014-09-10 19:48:05 -07001450 }
1451}
1452
1453TEST_F(ClassLinkerTest, Preverified_InitializedBoot) {
1454 ScopedObjectAccess soa(Thread::Current());
1455
Vladimir Markoe9987b02018-05-22 16:26:43 +01001456 ObjPtr<mirror::Class> JavaLangObject =
1457 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001458 ASSERT_TRUE(JavaLangObject != nullptr);
Andreas Gampe48498592014-09-10 19:48:05 -07001459 EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the "
1460 "core";
Igor Murashkindf707e42016-02-02 16:56:50 -08001461 CheckVerificationAttempted(JavaLangObject, true);
Andreas Gampe48498592014-09-10 19:48:05 -07001462}
1463
1464TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) {
1465 ScopedObjectAccess soa(Thread::Current());
1466
1467 StackHandleScope<1> hs(soa.Self());
1468
1469 Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass(
1470 soa.Self(), "Ljava/lang/SecurityManager;")));
1471 EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the "
1472 "core";
1473
Igor Murashkindf707e42016-02-02 16:56:50 -08001474 CheckVerificationAttempted(security_manager.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001475
Ian Rogers7b078e82014-09-10 14:44:24 -07001476 class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001477 CheckVerificationAttempted(security_manager.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001478}
1479
1480TEST_F(ClassLinkerTest, Preverified_App) {
1481 ScopedObjectAccess soa(Thread::Current());
1482
1483 StackHandleScope<2> hs(soa.Self());
1484 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001485 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Andreas Gampe48498592014-09-10 19:48:05 -07001486 Handle<mirror::Class> statics(
1487 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1488
Igor Murashkindf707e42016-02-02 16:56:50 -08001489 CheckVerificationAttempted(statics.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001490
Ian Rogers7b078e82014-09-10 14:44:24 -07001491 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001492 CheckVerificationAttempted(statics.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001493}
1494
Sebastien Hertz6963e442014-11-26 22:11:27 +01001495TEST_F(ClassLinkerTest, IsBootStrapClassLoaded) {
1496 ScopedObjectAccess soa(Thread::Current());
1497
1498 StackHandleScope<3> hs(soa.Self());
1499 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001500 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Sebastien Hertz6963e442014-11-26 22:11:27 +01001501
1502 // java.lang.Object is a bootstrap class.
1503 Handle<mirror::Class> jlo_class(
1504 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001505 ASSERT_TRUE(jlo_class != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001506 EXPECT_TRUE(jlo_class->IsBootStrapClassLoaded());
Sebastien Hertz6963e442014-11-26 22:11:27 +01001507
1508 // Statics is not a bootstrap class.
1509 Handle<mirror::Class> statics(
1510 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001511 ASSERT_TRUE(statics != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001512 EXPECT_FALSE(statics->IsBootStrapClassLoaded());
Sebastien Hertz6963e442014-11-26 22:11:27 +01001513}
1514
Mathieu Chartier76172162016-01-26 14:54:06 -08001515// Regression test for b/26799552.
1516TEST_F(ClassLinkerTest, RegisterDexFileName) {
1517 ScopedObjectAccess soa(Thread::Current());
1518 StackHandleScope<2> hs(soa.Self());
1519 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1520 MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr));
1521 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001522 ReaderMutexLock mu(soa.Self(), *Locks::dex_lock_);
Mathieu Chartier76172162016-01-26 14:54:06 -08001523 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001524 dex_cache.Assign(soa.Self()->DecodeJObject(data.weak_root)->AsDexCache());
Andreas Gampefa4333d2017-02-14 11:10:34 -08001525 if (dex_cache != nullptr) {
Mathieu Chartier76172162016-01-26 14:54:06 -08001526 break;
1527 }
1528 }
Andreas Gampefa4333d2017-02-14 11:10:34 -08001529 ASSERT_TRUE(dex_cache != nullptr);
Mathieu Chartier76172162016-01-26 14:54:06 -08001530 }
Mathieu Chartier76172162016-01-26 14:54:06 -08001531 const uint16_t data[] = { 0x20AC, 0x20A1 };
1532 Handle<mirror::String> location(hs.NewHandle(mirror::String::AllocFromUtf16(soa.Self(),
1533 arraysize(data),
1534 data)));
Mathieu Chartier76172162016-01-26 14:54:06 -08001535 const DexFile* old_dex_file = dex_cache->GetDexFile();
1536
David Sehr0b426772018-07-03 23:03:42 +00001537 std::unique_ptr<DexFile> dex_file(new StandardDexFile(old_dex_file->Begin(),
1538 old_dex_file->Size(),
1539 location->ToModifiedUtf8(),
1540 0u,
1541 nullptr,
1542 nullptr));
David Srbecky86d6cd52020-12-02 18:13:10 +00001543 // Make a copy of the dex cache with changed name.
1544 LinearAlloc* alloc = Runtime::Current()->GetLinearAlloc();
1545 dex_cache.Assign(class_linker->AllocAndInitializeDexCache(Thread::Current(), *dex_file, alloc));
1546 DCHECK_EQ(dex_cache->GetLocation()->CompareTo(location.Get()), 0);
Mathieu Chartier76172162016-01-26 14:54:06 -08001547 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001548 WriterMutexLock mu(soa.Self(), *Locks::dex_lock_);
Mathieu Chartier76172162016-01-26 14:54:06 -08001549 // Check that inserting with a UTF16 name works.
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001550 class_linker->RegisterDexFileLocked(*dex_file, dex_cache.Get(), /* class_loader= */ nullptr);
Mathieu Chartier76172162016-01-26 14:54:06 -08001551 }
1552}
1553
Narayan Kamath25352fc2016-08-03 12:46:58 +01001554TEST_F(ClassLinkerMethodHandlesTest, TestResolveMethodTypes) {
1555 ScopedObjectAccess soa(Thread::Current());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001556 StackHandleScope<7> hs(soa.Self());
1557
1558 Handle<mirror::ClassLoader> class_loader(
1559 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MethodTypes"))));
1560 Handle<mirror::Class> method_types(
1561 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader)));
1562 class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
1563
Vladimir Markoba118822017-06-12 15:41:56 +01001564 ArtMethod* method1 = method_types->FindClassMethod(
1565 "method1",
1566 "(Ljava/lang/String;)Ljava/lang/String;",
1567 kRuntimePointerSize);
1568 ASSERT_TRUE(method1 != nullptr);
1569 ASSERT_FALSE(method1->IsDirect());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001570
1571 const DexFile& dex_file = *(method1->GetDexFile());
1572 Handle<mirror::DexCache> dex_cache = hs.NewHandle(
Orion Hodsone7732be2017-10-11 14:35:20 +01001573 class_linker_->FindDexCache(soa.Self(), dex_file));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001574
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001575 const dex::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001576
1577 // This is the MethodType corresponding to the prototype of
1578 // String MethodTypes# method1(String).
1579 // Its RType = Ljava/lang/String;
Narayan Kamath42b3dd02016-10-03 11:33:01 +01001580 // Its PTypes = { Ljava/lang/String; }
Narayan Kamath25352fc2016-08-03 12:46:58 +01001581 Handle<mirror::MethodType> method1_type = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001582 class_linker_->ResolveMethodType(soa.Self(), method1_id.proto_idx_, dex_cache, class_loader));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001583
1584 // Assert that the method type was resolved successfully.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001585 ASSERT_TRUE(method1_type != nullptr);
Narayan Kamath25352fc2016-08-03 12:46:58 +01001586
1587 // Assert that the return type and the method arguments are as we expect.
Orion Hodsone7732be2017-10-11 14:35:20 +01001588 Handle<mirror::Class> string_class(hs.NewHandle(class_linker_->FindClass(soa.Self(),
1589 "Ljava/lang/String;",
1590 class_loader)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001591 ASSERT_OBJ_PTR_EQ(string_class.Get(), method1_type->GetRType());
1592 ASSERT_OBJ_PTR_EQ(string_class.Get(), method1_type->GetPTypes()->Get(0));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001593
1594 // Resolve the method type again and assert that we get back the same value.
1595 Handle<mirror::MethodType> method1_type2 = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001596 class_linker_->ResolveMethodType(soa.Self(), method1_id.proto_idx_, dex_cache, class_loader));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001597 ASSERT_OBJ_PTR_EQ(method1_type.Get(), method1_type2.Get());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001598
1599 // Resolve the MethodType associated with a different method signature
1600 // and assert it's different.
Vladimir Markoba118822017-06-12 15:41:56 +01001601 ArtMethod* method2 = method_types->FindClassMethod(
Narayan Kamath25352fc2016-08-03 12:46:58 +01001602 "method2",
1603 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
1604 kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001605 ASSERT_TRUE(method2 != nullptr);
1606 ASSERT_FALSE(method2->IsDirect());
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001607 const dex::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001608 Handle<mirror::MethodType> method2_type = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001609 class_linker_->ResolveMethodType(soa.Self(), method2_id.proto_idx_, dex_cache, class_loader));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001610 ASSERT_OBJ_PTR_NE(method1_type.Get(), method2_type.Get());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001611}
1612
Calin Juravle7865ac72017-06-28 11:03:12 -07001613// Verify that ClassLinker's CreateWellknownClassLoader works as expected
1614// by creating a chain of class loaders with various dex files.
1615TEST_F(ClassLinkerTest, CreateWellKnownClassLoader) {
1616 // LoadDexIn*ClassLoader methods already assert that the parent loader is the expected one.
1617 // No need to check again.
1618 jobject class_loader_a = LoadDexInPathClassLoader("MyClass", nullptr);
1619 jobject class_loader_b = LoadDexInDelegateLastClassLoader("Nested", class_loader_a);
1620 jobject class_loader_c = LoadDexInPathClassLoader("MultiDex", class_loader_b);
1621 LoadDexInDelegateLastClassLoader("Interfaces", class_loader_c);
1622}
1623
David Sehrc431b9d2018-03-02 12:01:51 -08001624TEST_F(ClassLinkerTest, PrettyClass) {
1625 ScopedObjectAccess soa(Thread::Current());
1626 EXPECT_EQ("null", mirror::Class::PrettyClass(nullptr));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001627 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001628 ASSERT_TRUE(c != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001629 ObjPtr<mirror::Object> o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehrc431b9d2018-03-02 12:01:51 -08001630 EXPECT_EQ("java.lang.Class<java.lang.String[]>", mirror::Class::PrettyClass(o->GetClass()));
1631}
1632
1633TEST_F(ClassLinkerTest, PrettyClassAndClassLoader) {
1634 ScopedObjectAccess soa(Thread::Current());
1635 EXPECT_EQ("null", mirror::Class::PrettyClassAndClassLoader(nullptr));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001636 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001637 ASSERT_TRUE(c != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001638 ObjPtr<mirror::Object> o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehrc431b9d2018-03-02 12:01:51 -08001639 EXPECT_EQ("java.lang.Class<java.lang.String[],null>",
1640 mirror::Class::PrettyClassAndClassLoader(o->GetClass()));
1641}
1642
1643TEST_F(ClassLinkerTest, PrettyField) {
1644 ScopedObjectAccess soa(Thread::Current());
1645 EXPECT_EQ("null", ArtField::PrettyField(nullptr));
1646
Vladimir Markoe9987b02018-05-22 16:26:43 +01001647 ObjPtr<mirror::Class> java_lang_String = class_linker_->FindSystemClass(soa.Self(),
1648 "Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001649
1650 ArtField* f;
1651 f = java_lang_String->FindDeclaredInstanceField("count", "I");
1652 EXPECT_EQ("int java.lang.String.count", f->PrettyField());
1653 EXPECT_EQ("java.lang.String.count", f->PrettyField(false));
1654}
1655
1656TEST_F(ClassLinkerTest, JniShortName_JniLongName) {
1657 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001658 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001659 ASSERT_TRUE(c != nullptr);
1660 ArtMethod* m;
1661
1662 m = c->FindClassMethod("charAt", "(I)C", kRuntimePointerSize);
1663 ASSERT_TRUE(m != nullptr);
1664 ASSERT_FALSE(m->IsDirect());
1665 EXPECT_EQ("Java_java_lang_String_charAt", m->JniShortName());
1666 EXPECT_EQ("Java_java_lang_String_charAt__I", m->JniLongName());
1667
1668 m = c->FindClassMethod("indexOf", "(Ljava/lang/String;I)I", kRuntimePointerSize);
1669 ASSERT_TRUE(m != nullptr);
1670 ASSERT_FALSE(m->IsDirect());
1671 EXPECT_EQ("Java_java_lang_String_indexOf", m->JniShortName());
1672 EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", m->JniLongName());
1673
1674 m = c->FindClassMethod("copyValueOf", "([CII)Ljava/lang/String;", kRuntimePointerSize);
1675 ASSERT_TRUE(m != nullptr);
1676 ASSERT_TRUE(m->IsStatic());
1677 EXPECT_EQ("Java_java_lang_String_copyValueOf", m->JniShortName());
1678 EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", m->JniLongName());
1679}
1680
Calin Juravlecdd49122017-07-05 20:09:53 -07001681class ClassLinkerClassLoaderTest : public ClassLinkerTest {
1682 protected:
1683 // Verifies that the class identified by the given descriptor is loaded with
1684 // the expected_class_loader_obj when search from class_loader_to_search_obj.
1685 // When expected_class_loader_obj is null the check will be done against BootClassLoader.
1686 void VerifyClassResolution(const std::string& descriptor,
1687 jobject class_loader_to_search_obj,
1688 jobject expected_class_loader_obj,
1689 bool should_find = true) {
1690 Thread* self = Thread::Current();
1691 ScopedObjectAccess soa(self);
1692 StackHandleScope<3> hs(self);
1693 Handle<mirror::ClassLoader> class_loader_to_search(
1694 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_to_search_obj)));
1695
1696 Handle<mirror::Class> klass = hs.NewHandle(
1697 class_linker_->FindClass(soa.Self(), descriptor.c_str(), class_loader_to_search));
1698
1699 if (!should_find) {
1700 if (self->IsExceptionPending()) {
1701 self->ClearException();
1702 }
1703 ASSERT_TRUE(klass == nullptr);
1704 } else if (expected_class_loader_obj == nullptr) {
1705 ASSERT_TRUE(ClassLinker::IsBootClassLoader(soa, klass->GetClassLoader()));
1706 } else {
1707 ASSERT_TRUE(klass != nullptr) << descriptor;
1708 Handle<mirror::ClassLoader> expected_class_loader(
1709 hs.NewHandle(soa.Decode<mirror::ClassLoader>(expected_class_loader_obj)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001710 ASSERT_OBJ_PTR_EQ(klass->GetClassLoader(), expected_class_loader.Get());
Calin Juravlecdd49122017-07-05 20:09:53 -07001711 }
1712 }
1713};
1714
1715TEST_F(ClassLinkerClassLoaderTest, CreatePathClassLoader) {
1716 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
1717 VerifyClassResolution("LDefinedInA;", class_loader_a, class_loader_a);
1718 VerifyClassResolution("Ljava/lang/String;", class_loader_a, nullptr);
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001719 VerifyClassResolution("LDefinedInB;", class_loader_a, nullptr, /*should_find=*/ false);
Calin Juravlecdd49122017-07-05 20:09:53 -07001720}
1721
1722TEST_F(ClassLinkerClassLoaderTest, CreateDelegateLastClassLoader) {
1723 jobject class_loader_a = LoadDexInDelegateLastClassLoader("ForClassLoaderA", nullptr);
1724 VerifyClassResolution("LDefinedInA;", class_loader_a, class_loader_a);
1725 VerifyClassResolution("Ljava/lang/String;", class_loader_a, nullptr);
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001726 VerifyClassResolution("LDefinedInB;", class_loader_a, nullptr, /*should_find=*/ false);
Calin Juravlecdd49122017-07-05 20:09:53 -07001727}
1728
1729TEST_F(ClassLinkerClassLoaderTest, CreateClassLoaderChain) {
1730 // The chain is
1731 // ClassLoaderA (PathClassLoader, defines: A, AB, AC, AD)
1732 // ^
1733 // |
1734 // ClassLoaderB (DelegateLastClassLoader, defines: B, AB, BC, BD)
1735 // ^
1736 // |
1737 // ClassLoaderC (PathClassLoader, defines: C, AC, BC, CD)
1738 // ^
1739 // |
Calin Juravlee64ed9e2017-07-06 22:01:00 -07001740 // ClassLoaderD (DelegateLastClassLoader, defines: D, AD, BD, CD)
Calin Juravlecdd49122017-07-05 20:09:53 -07001741
1742 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
1743 jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a);
1744 jobject class_loader_c = LoadDexInPathClassLoader("ForClassLoaderC", class_loader_b);
1745 jobject class_loader_d = LoadDexInDelegateLastClassLoader("ForClassLoaderD", class_loader_c);
1746
1747 // Verify exclusive classes (present in only one class loader).
1748 VerifyClassResolution("LDefinedInD;", class_loader_d, class_loader_d);
1749 VerifyClassResolution("LDefinedInC;", class_loader_d, class_loader_c);
1750 VerifyClassResolution("LDefinedInB;", class_loader_d, class_loader_b);
1751 VerifyClassResolution("LDefinedInA;", class_loader_d, class_loader_a);
1752
1753 // Verify classes that are defined in multiple classloader.
1754
1755 // Classes defined in B should be found in B even if they are defined in A or C because
1756 // B is a DelegateLastClassLoader.
1757 VerifyClassResolution("LDefinedInAB;", class_loader_d, class_loader_b);
1758 VerifyClassResolution("LDefinedInABC;", class_loader_d, class_loader_b);
1759 VerifyClassResolution("LDefinedInBC;", class_loader_d, class_loader_b);
1760
1761 // Classes defined in D should be found in D even if they are defined in parent class loaders
1762 // as well because D is a DelegateLastClassLoader.
1763 VerifyClassResolution("LDefinedInAD;", class_loader_d, class_loader_d);
1764 VerifyClassResolution("LDefinedInBD;", class_loader_d, class_loader_d);
1765 VerifyClassResolution("LDefinedInCD;", class_loader_d, class_loader_d);
1766
1767
1768 // Classes not defined in the DelegateLastClassLoaders (i.e. D or B) should be found
1769 // in the top parent.
1770 VerifyClassResolution("LDefinedInAC;", class_loader_d, class_loader_a);
1771
Ian Pedowitz2d536432020-07-22 14:33:00 -07001772 // Check that we don't find an undefined class.
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001773 VerifyClassResolution("LNotDefined;", class_loader_d, nullptr, /*should_find=*/ false);
Calin Juravlecdd49122017-07-05 20:09:53 -07001774}
1775
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001776} // namespace art