Home | History | Annotate | Download | only in runtime
      1 /*
      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  */
     16 
     17 #include "class_linker.h"
     18 
     19 #include <memory>
     20 #include <string>
     21 #include <string_view>
     22 
     23 #include "android-base/strings.h"
     24 
     25 #include "art_field-inl.h"
     26 #include "art_method-inl.h"
     27 #include "base/enums.h"
     28 #include "class_linker-inl.h"
     29 #include "class_root.h"
     30 #include "common_runtime_test.h"
     31 #include "dex/dex_file_types.h"
     32 #include "dex/signature-inl.h"
     33 #include "dex/standard_dex_file.h"
     34 #include "entrypoints/entrypoint_utils-inl.h"
     35 #include "experimental_flags.h"
     36 #include "gc/heap.h"
     37 #include "handle_scope-inl.h"
     38 #include "mirror/array-alloc-inl.h"
     39 #include "mirror/accessible_object.h"
     40 #include "mirror/call_site.h"
     41 #include "mirror/class-inl.h"
     42 #include "mirror/class_ext.h"
     43 #include "mirror/dex_cache.h"
     44 #include "mirror/emulated_stack_frame.h"
     45 #include "mirror/executable.h"
     46 #include "mirror/field.h"
     47 #include "mirror/method_handle_impl.h"
     48 #include "mirror/method_handles_lookup.h"
     49 #include "mirror/method_type-inl.h"
     50 #include "mirror/object-inl.h"
     51 #include "mirror/object_array-alloc-inl.h"
     52 #include "mirror/object_array-inl.h"
     53 #include "mirror/proxy.h"
     54 #include "mirror/reference.h"
     55 #include "mirror/stack_trace_element.h"
     56 #include "mirror/string-inl.h"
     57 #include "mirror/var_handle.h"
     58 #include "scoped_thread_state_change-inl.h"
     59 #include "thread-current-inl.h"
     60 
     61 namespace art {
     62 
     63 class ClassLinkerTest : public CommonRuntimeTest {
     64  protected:
     65   void AssertNonExistentClass(const std::string& descriptor)
     66       REQUIRES_SHARED(Locks::mutator_lock_) {
     67     Thread* self = Thread::Current();
     68     EXPECT_TRUE(class_linker_->FindSystemClass(self, descriptor.c_str()) == nullptr);
     69     EXPECT_TRUE(self->IsExceptionPending());
     70     StackHandleScope<1> hs(self);
     71     Handle<mirror::Object> exception = hs.NewHandle<mirror::Object>(self->GetException());
     72     self->ClearException();
     73     ObjPtr<mirror::Class> exception_class =
     74         class_linker_->FindSystemClass(self, "Ljava/lang/NoClassDefFoundError;");
     75     EXPECT_TRUE(exception->InstanceOf(exception_class));
     76   }
     77 
     78   void AssertPrimitiveClass(const std::string& descriptor)
     79       REQUIRES_SHARED(Locks::mutator_lock_) {
     80     Thread* self = Thread::Current();
     81     AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(self, descriptor.c_str()));
     82   }
     83 
     84   void AssertPrimitiveClass(const std::string& descriptor, ObjPtr<mirror::Class> primitive)
     85       REQUIRES_SHARED(Locks::mutator_lock_) {
     86     ASSERT_TRUE(primitive != nullptr);
     87     ASSERT_TRUE(primitive->GetClass() != nullptr);
     88     ASSERT_OBJ_PTR_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
     89     EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != nullptr);
     90     std::string temp;
     91     ASSERT_STREQ(descriptor.c_str(), primitive->GetDescriptor(&temp));
     92     EXPECT_TRUE(primitive->GetSuperClass() == nullptr);
     93     EXPECT_FALSE(primitive->HasSuperClass());
     94     EXPECT_TRUE(primitive->GetClassLoader() == nullptr);
     95     EXPECT_EQ(ClassStatus::kInitialized, primitive->GetStatus());
     96     EXPECT_FALSE(primitive->IsErroneous());
     97     EXPECT_TRUE(primitive->IsLoaded());
     98     EXPECT_TRUE(primitive->IsResolved());
     99     EXPECT_FALSE(primitive->IsErroneousResolved());
    100     EXPECT_TRUE(primitive->IsVerified());
    101     EXPECT_TRUE(primitive->IsInitialized());
    102     EXPECT_FALSE(primitive->IsArrayInstance());
    103     EXPECT_FALSE(primitive->IsArrayClass());
    104     EXPECT_TRUE(primitive->GetComponentType() == nullptr);
    105     EXPECT_FALSE(primitive->IsInterface());
    106     EXPECT_TRUE(primitive->IsPublic());
    107     EXPECT_TRUE(primitive->IsFinal());
    108     EXPECT_TRUE(primitive->IsPrimitive());
    109     EXPECT_FALSE(primitive->IsSynthetic());
    110     EXPECT_EQ(0U, primitive->NumDirectMethods());
    111     EXPECT_EQ(0U, primitive->NumVirtualMethods());
    112     EXPECT_EQ(0U, primitive->NumInstanceFields());
    113     EXPECT_EQ(0U, primitive->NumStaticFields());
    114     EXPECT_EQ(0U, primitive->NumDirectInterfaces());
    115     EXPECT_FALSE(primitive->HasVTable());
    116     EXPECT_EQ(0, primitive->GetIfTableCount());
    117     EXPECT_TRUE(primitive->GetIfTable() != nullptr);
    118     EXPECT_EQ(primitive->GetIfTable()->Count(), 0u);
    119     EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract | kAccVerificationAttempted,
    120               primitive->GetAccessFlags());
    121   }
    122 
    123   void AssertObjectClass(ObjPtr<mirror::Class> JavaLangObject)
    124       REQUIRES_SHARED(Locks::mutator_lock_) {
    125     ASSERT_TRUE(JavaLangObject != nullptr);
    126     ASSERT_TRUE(JavaLangObject->GetClass() != nullptr);
    127     ASSERT_OBJ_PTR_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
    128     EXPECT_OBJ_PTR_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
    129     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);
    134     EXPECT_EQ(ClassStatus::kInitialized, JavaLangObject->GetStatus());
    135     EXPECT_FALSE(JavaLangObject->IsErroneous());
    136     EXPECT_TRUE(JavaLangObject->IsLoaded());
    137     EXPECT_TRUE(JavaLangObject->IsResolved());
    138     EXPECT_FALSE(JavaLangObject->IsErroneousResolved());
    139     EXPECT_TRUE(JavaLangObject->IsVerified());
    140     EXPECT_TRUE(JavaLangObject->IsInitialized());
    141     EXPECT_FALSE(JavaLangObject->IsArrayInstance());
    142     EXPECT_FALSE(JavaLangObject->IsArrayClass());
    143     EXPECT_TRUE(JavaLangObject->GetComponentType() == nullptr);
    144     EXPECT_FALSE(JavaLangObject->IsInterface());
    145     EXPECT_TRUE(JavaLangObject->IsPublic());
    146     EXPECT_FALSE(JavaLangObject->IsFinal());
    147     EXPECT_FALSE(JavaLangObject->IsPrimitive());
    148     EXPECT_FALSE(JavaLangObject->IsSynthetic());
    149     EXPECT_EQ(4U, JavaLangObject->NumDirectMethods());
    150     EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
    151     if (!kUseBrooksReadBarrier) {
    152       EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
    153     } else {
    154       EXPECT_EQ(4U, JavaLangObject->NumInstanceFields());
    155     }
    156     EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(),
    157                  "shadow$_klass_");
    158     EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(),
    159                  "shadow$_monitor_");
    160     if (kUseBrooksReadBarrier) {
    161       EXPECT_STREQ(JavaLangObject->GetInstanceField(2)->GetName(),
    162                    "shadow$_x_rb_ptr_");
    163       EXPECT_STREQ(JavaLangObject->GetInstanceField(3)->GetName(),
    164                    "shadow$_x_xpadding_");
    165     }
    166 
    167     EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
    168     EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces());
    169 
    170     PointerSize pointer_size = class_linker_->GetImagePointerSize();
    171     ArtMethod* unimplemented = runtime_->GetImtUnimplementedMethod();
    172     ImTable* imt = JavaLangObject->GetImt(pointer_size);
    173     ASSERT_NE(nullptr, imt);
    174     for (size_t i = 0; i < ImTable::kSize; ++i) {
    175       ASSERT_EQ(unimplemented, imt->Get(i, pointer_size));
    176     }
    177   }
    178 
    179   void AssertArrayClass(const std::string& array_descriptor,
    180                         const std::string& component_type,
    181                         ObjPtr<mirror::ClassLoader> class_loader)
    182       REQUIRES_SHARED(Locks::mutator_lock_) {
    183     Thread* self = Thread::Current();
    184     StackHandleScope<2> hs(self);
    185     Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
    186     Handle<mirror::Class> array(
    187         hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader)));
    188     std::string temp;
    189     EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp));
    190     EXPECT_OBJ_PTR_EQ(loader.Get(), array->GetClassLoader());
    191     EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract)));
    192     AssertArrayClass(array_descriptor, array);
    193   }
    194 
    195   void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array)
    196       REQUIRES_SHARED(Locks::mutator_lock_) {
    197     ASSERT_TRUE(array != nullptr);
    198     ASSERT_TRUE(array->GetClass() != nullptr);
    199     ASSERT_OBJ_PTR_EQ(array->GetClass(), array->GetClass()->GetClass());
    200     EXPECT_TRUE(array->GetClass()->GetSuperClass() != nullptr);
    201     std::string temp;
    202     ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp));
    203     EXPECT_TRUE(array->GetSuperClass() != nullptr);
    204     Thread* self = Thread::Current();
    205     EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"),
    206                       array->GetSuperClass());
    207     EXPECT_TRUE(array->HasSuperClass());
    208     ASSERT_TRUE(array->GetComponentType() != nullptr);
    209     ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U);
    210     EXPECT_EQ(ClassStatus::kInitialized, array->GetStatus());
    211     EXPECT_FALSE(array->IsErroneous());
    212     EXPECT_TRUE(array->IsLoaded());
    213     EXPECT_TRUE(array->IsResolved());
    214     EXPECT_FALSE(array->IsErroneousResolved());
    215     EXPECT_TRUE(array->IsVerified());
    216     EXPECT_TRUE(array->IsInitialized());
    217     EXPECT_FALSE(array->IsArrayInstance());
    218     EXPECT_TRUE(array->IsArrayClass());
    219     EXPECT_FALSE(array->IsInterface());
    220     EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
    221     EXPECT_TRUE(array->IsFinal());
    222     EXPECT_FALSE(array->IsPrimitive());
    223     EXPECT_FALSE(array->IsSynthetic());
    224     EXPECT_EQ(0U, array->NumDirectMethods());
    225     EXPECT_EQ(0U, array->NumVirtualMethods());
    226     EXPECT_EQ(0U, array->NumInstanceFields());
    227     EXPECT_EQ(0U, array->NumStaticFields());
    228     EXPECT_EQ(2U, array->NumDirectInterfaces());
    229     EXPECT_TRUE(array->ShouldHaveImt());
    230     EXPECT_TRUE(array->ShouldHaveEmbeddedVTable());
    231     EXPECT_EQ(2, array->GetIfTableCount());
    232     ASSERT_TRUE(array->GetIfTable() != nullptr);
    233     ObjPtr<mirror::Class> direct_interface0 =
    234         mirror::Class::GetDirectInterface(self, array.Get(), 0);
    235     EXPECT_TRUE(direct_interface0 != nullptr);
    236     EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;");
    237     ObjPtr<mirror::Class> direct_interface1 =
    238         mirror::Class::GetDirectInterface(self, array.Get(), 1);
    239     EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;");
    240     EXPECT_OBJ_PTR_EQ(class_linker_->FindArrayClass(self, array->GetComponentType()), array.Get());
    241 
    242     PointerSize pointer_size = class_linker_->GetImagePointerSize();
    243     ObjPtr<mirror::Class> JavaLangObject =
    244         class_linker_->FindSystemClass(self, "Ljava/lang/Object;");
    245     ImTable* JavaLangObject_imt = JavaLangObject->GetImt(pointer_size);
    246     // IMT of a array class should be shared with the IMT of the java.lag.Object
    247     ASSERT_EQ(JavaLangObject_imt, array->GetImt(pointer_size));
    248   }
    249 
    250   void AssertMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
    251     EXPECT_TRUE(method != nullptr);
    252     EXPECT_TRUE(method->GetDeclaringClass() != nullptr);
    253     EXPECT_TRUE(method->GetName() != nullptr);
    254     EXPECT_TRUE(method->GetSignature() != Signature::NoSignature());
    255   }
    256 
    257   void AssertField(ObjPtr<mirror::Class> klass, ArtField* field)
    258       REQUIRES_SHARED(Locks::mutator_lock_) {
    259     EXPECT_TRUE(field != nullptr);
    260     EXPECT_OBJ_PTR_EQ(klass, field->GetDeclaringClass());
    261     EXPECT_TRUE(field->GetName() != nullptr);
    262     EXPECT_TRUE(field->ResolveType() != nullptr);
    263   }
    264 
    265   void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass)
    266       REQUIRES_SHARED(Locks::mutator_lock_) {
    267     std::string temp;
    268     EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
    269     if (descriptor == "Ljava/lang/Object;") {
    270       EXPECT_FALSE(klass->HasSuperClass());
    271     } else {
    272       EXPECT_TRUE(klass->HasSuperClass());
    273       EXPECT_TRUE(klass->GetSuperClass() != nullptr);
    274     }
    275     EXPECT_TRUE(klass->GetClass() != nullptr);
    276     EXPECT_OBJ_PTR_EQ(klass->GetClass(), klass->GetClass()->GetClass());
    277     EXPECT_TRUE(klass->GetDexCache() != nullptr);
    278     EXPECT_TRUE(klass->IsLoaded());
    279     EXPECT_TRUE(klass->IsResolved());
    280     EXPECT_FALSE(klass->IsErroneousResolved());
    281     EXPECT_FALSE(klass->IsErroneous());
    282     EXPECT_FALSE(klass->IsArrayClass());
    283     EXPECT_TRUE(klass->GetComponentType() == nullptr);
    284     EXPECT_TRUE(klass->IsInSamePackage(klass.Get()));
    285     std::string temp2;
    286     EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp),
    287                                                klass->GetDescriptor(&temp2)));
    288     if (klass->IsInterface()) {
    289       EXPECT_TRUE(klass->IsAbstract());
    290       // Check that all direct methods are static (either <clinit> or a regular static method).
    291       for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) {
    292         EXPECT_TRUE(m.IsStatic());
    293         EXPECT_TRUE(m.IsDirect());
    294       }
    295     } else {
    296       if (!klass->IsSynthetic()) {
    297         EXPECT_NE(0U, klass->NumDirectMethods());
    298       }
    299     }
    300     EXPECT_EQ(klass->IsInterface(), !klass->HasVTable());
    301     ObjPtr<mirror::IfTable> iftable = klass->GetIfTable();
    302     for (int i = 0; i < klass->GetIfTableCount(); i++) {
    303       ObjPtr<mirror::Class> interface = iftable->GetInterface(i);
    304       ASSERT_TRUE(interface != nullptr);
    305       if (klass->IsInterface()) {
    306         EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
    307       } else {
    308         EXPECT_EQ(interface->NumDeclaredVirtualMethods(), iftable->GetMethodArrayCount(i));
    309       }
    310     }
    311     if (klass->IsAbstract()) {
    312       EXPECT_FALSE(klass->IsFinal());
    313     } else {
    314       EXPECT_FALSE(klass->IsAnnotation());
    315     }
    316     if (klass->IsFinal()) {
    317       EXPECT_FALSE(klass->IsAbstract());
    318       EXPECT_FALSE(klass->IsAnnotation());
    319     }
    320     if (klass->IsAnnotation()) {
    321       EXPECT_FALSE(klass->IsFinal());
    322       EXPECT_TRUE(klass->IsAbstract());
    323     }
    324 
    325     EXPECT_FALSE(klass->IsPrimitive());
    326     EXPECT_TRUE(klass->CanAccess(klass.Get()));
    327 
    328     for (ArtMethod& method : klass->GetDirectMethods(kRuntimePointerSize)) {
    329       AssertMethod(&method);
    330       EXPECT_TRUE(method.IsDirect());
    331       EXPECT_OBJ_PTR_EQ(klass.Get(), method.GetDeclaringClass());
    332     }
    333 
    334     for (ArtMethod& method : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
    335       AssertMethod(&method);
    336       EXPECT_FALSE(method.IsDirect());
    337       EXPECT_OBJ_PTR_EQ(klass.Get(), method.GetDeclaringClass());
    338     }
    339 
    340     for (ArtMethod& method : klass->GetCopiedMethods(kRuntimePointerSize)) {
    341       AssertMethod(&method);
    342       EXPECT_FALSE(method.IsDirect());
    343       EXPECT_TRUE(method.IsCopied());
    344       EXPECT_TRUE(method.GetDeclaringClass()->IsInterface())
    345           << "declaring class: " << method.GetDeclaringClass()->PrettyClass();
    346       EXPECT_TRUE(method.GetDeclaringClass()->IsAssignableFrom(klass.Get()))
    347           << "declaring class: " << method.GetDeclaringClass()->PrettyClass();
    348     }
    349 
    350     for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
    351       ArtField* field = klass->GetInstanceField(i);
    352       AssertField(klass.Get(), field);
    353       EXPECT_FALSE(field->IsStatic());
    354     }
    355 
    356     for (size_t i = 0; i < klass->NumStaticFields(); i++) {
    357       ArtField* field = klass->GetStaticField(i);
    358       AssertField(klass.Get(), field);
    359       EXPECT_TRUE(field->IsStatic());
    360     }
    361 
    362     // Confirm that all instances field offsets are packed together at the start.
    363     EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
    364     MemberOffset start_ref_offset = klass->GetFirstReferenceInstanceFieldOffset();
    365     MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
    366                                 klass->NumReferenceInstanceFields() *
    367                                     sizeof(mirror::HeapReference<mirror::Object>));
    368     MemberOffset current_ref_offset = start_ref_offset;
    369     for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
    370       ArtField* field = klass->GetInstanceField(i);
    371       ObjPtr<mirror::Class> field_type = field->ResolveType();
    372       ASSERT_TRUE(field_type != nullptr);
    373       if (!field->IsPrimitiveType()) {
    374         ASSERT_TRUE(!field_type->IsPrimitive());
    375         ASSERT_EQ(current_ref_offset.Uint32Value(), field->GetOffset().Uint32Value());
    376         if (current_ref_offset.Uint32Value() == end_ref_offset.Uint32Value()) {
    377           // While Reference.referent is not primitive, the ClassLinker
    378           // treats it as such so that the garbage collector won't scan it.
    379           EXPECT_EQ(field->PrettyField(), "java.lang.Object java.lang.ref.Reference.referent");
    380         } else {
    381           current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
    382                                             sizeof(mirror::HeapReference<mirror::Object>));
    383         }
    384       } else {
    385         if (field->GetOffset().Uint32Value() < end_ref_offset.Uint32Value()) {
    386           // Shuffled before references.
    387           ASSERT_LT(field->GetOffset().Uint32Value(), start_ref_offset.Uint32Value());
    388           CHECK(!IsAligned<4>(field->GetOffset().Uint32Value()));
    389         }
    390       }
    391     }
    392     ASSERT_EQ(end_ref_offset.Uint32Value(), current_ref_offset.Uint32Value());
    393 
    394     uint32_t total_num_reference_instance_fields = 0;
    395     ObjPtr<mirror::Class> k = klass.Get();
    396     while (k != nullptr) {
    397       total_num_reference_instance_fields += k->NumReferenceInstanceFields();
    398       k = k->GetSuperClass();
    399     }
    400     EXPECT_GE(total_num_reference_instance_fields, 1U);  // Should always have Object's class.
    401     if (klass->GetReferenceInstanceOffsets() != mirror::Class::kClassWalkSuper) {
    402       // The reference instance offsets have a bit set for each reference offset.
    403       // +1 for Object's class.
    404       EXPECT_EQ(static_cast<uint32_t>(POPCOUNT(klass->GetReferenceInstanceOffsets())) + 1,
    405                 total_num_reference_instance_fields);
    406     }
    407   }
    408 
    409   void AssertDexFileClass(ObjPtr<mirror::ClassLoader> class_loader, const std::string& descriptor)
    410       REQUIRES_SHARED(Locks::mutator_lock_) {
    411     ASSERT_FALSE(descriptor.empty());
    412     Thread* self = Thread::Current();
    413     StackHandleScope<1> hs(self);
    414     Handle<mirror::Class> klass(
    415         hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str())));
    416     ASSERT_TRUE(klass != nullptr);
    417     std::string temp;
    418     EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
    419     EXPECT_OBJ_PTR_EQ(class_loader, klass->GetClassLoader());
    420     if (klass->IsPrimitive()) {
    421       AssertPrimitiveClass(descriptor, klass.Get());
    422     } else if (klass->IsArrayClass()) {
    423       AssertArrayClass(descriptor, klass);
    424     } else {
    425       AssertClass(descriptor, klass);
    426     }
    427   }
    428 
    429   void AssertDexFile(const DexFile& dex, ObjPtr<mirror::ClassLoader> class_loader)
    430       REQUIRES_SHARED(Locks::mutator_lock_) {
    431     // Verify all the classes defined in this file
    432     for (size_t i = 0; i < dex.NumClassDefs(); i++) {
    433       const dex::ClassDef& class_def = dex.GetClassDef(i);
    434       const char* descriptor = dex.GetClassDescriptor(class_def);
    435       AssertDexFileClass(class_loader, descriptor);
    436     }
    437     // Verify all the types referenced by this file
    438     for (size_t i = 0; i < dex.NumTypeIds(); i++) {
    439       const dex::TypeId& type_id = dex.GetTypeId(dex::TypeIndex(i));
    440       const char* descriptor = dex.GetTypeDescriptor(type_id);
    441       AssertDexFileClass(class_loader, descriptor);
    442     }
    443     TestRootVisitor visitor;
    444     class_linker_->VisitRoots(&visitor, kVisitRootFlagAllRoots);
    445   }
    446 
    447   class TestRootVisitor : public SingleRootVisitor {
    448    public:
    449     void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) override {
    450       EXPECT_TRUE(root != nullptr);
    451     }
    452   };
    453 };
    454 
    455 class ClassLinkerMethodHandlesTest : public ClassLinkerTest {
    456  protected:
    457   void SetUpRuntimeOptions(RuntimeOptions* options) override {
    458     CommonRuntimeTest::SetUpRuntimeOptions(options);
    459   }
    460 };
    461 
    462 struct CheckOffset {
    463   size_t cpp_offset;
    464   const char* java_name;
    465   CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
    466 };
    467 
    468 template <typename T>
    469 struct CheckOffsets {
    470   CheckOffsets(bool is_static_in, const char* class_descriptor_in)
    471       : is_static(is_static_in), class_descriptor(class_descriptor_in) {}
    472   bool is_static;
    473   std::string class_descriptor;
    474   std::vector<CheckOffset> offsets;
    475 
    476   bool Check() REQUIRES_SHARED(Locks::mutator_lock_) {
    477     Thread* self = Thread::Current();
    478     ObjPtr<mirror::Class> klass =
    479         Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str());
    480     CHECK(klass != nullptr) << class_descriptor;
    481 
    482     bool error = false;
    483 
    484     // Classes have a different size due to padding field. Strings are variable length.
    485     if (!klass->IsClassClass() && !klass->IsStringClass() && !is_static) {
    486       // Currently only required for AccessibleObject since of the padding fields. The class linker
    487       // says AccessibleObject is 9 bytes but sizeof(AccessibleObject) is 12 bytes due to padding.
    488       // The RoundUp is to get around this case.
    489       static constexpr size_t kPackAlignment = 4;
    490       size_t expected_size = RoundUp(is_static ? klass->GetClassSize() : klass->GetObjectSize(),
    491           kPackAlignment);
    492       if (sizeof(T) != expected_size) {
    493         LOG(ERROR) << "Class size mismatch:"
    494            << " class=" << class_descriptor
    495            << " Java=" << expected_size
    496            << " C++=" << sizeof(T);
    497         error = true;
    498       }
    499     }
    500 
    501     size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
    502     if (offsets.size() != num_fields) {
    503       LOG(ERROR) << "Field count mismatch:"
    504          << " class=" << class_descriptor
    505          << " Java=" << num_fields
    506          << " C++=" << offsets.size();
    507       error = true;
    508     }
    509 
    510     for (size_t i = 0; i < offsets.size(); i++) {
    511       ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
    512       std::string_view field_name(field->GetName());
    513       if (field_name != offsets[i].java_name) {
    514         error = true;
    515       }
    516     }
    517     if (error) {
    518       for (size_t i = 0; i < offsets.size(); i++) {
    519         CheckOffset& offset = offsets[i];
    520         ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
    521         std::string_view field_name(field->GetName());
    522         if (field_name != offsets[i].java_name) {
    523           LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
    524         }
    525         LOG(ERROR) << "Java field order:"
    526            << " i=" << i << " class=" << class_descriptor
    527            << " Java=" << field_name
    528            << " CheckOffsets=" << offset.java_name;
    529       }
    530     }
    531 
    532     for (size_t i = 0; i < offsets.size(); i++) {
    533       CheckOffset& offset = offsets[i];
    534       ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
    535       if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
    536         error = true;
    537       }
    538     }
    539     if (error) {
    540       for (size_t i = 0; i < offsets.size(); i++) {
    541         CheckOffset& offset = offsets[i];
    542         ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
    543         if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
    544           LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
    545         }
    546         LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
    547            << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
    548       }
    549     }
    550 
    551     return !error;
    552   };
    553 
    554   void addOffset(size_t offset, const char* name) {
    555     offsets.push_back(CheckOffset(offset, name));
    556   }
    557 
    558  private:
    559   DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
    560 };
    561 
    562 // Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
    563 // is first since if it is failing, others are unlikely to succeed.
    564 
    565 struct ObjectOffsets : public CheckOffsets<mirror::Object> {
    566   ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
    567     addOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_");
    568     addOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_");
    569 #ifdef USE_BROOKS_READ_BARRIER
    570     addOffset(OFFSETOF_MEMBER(mirror::Object, x_rb_ptr_), "shadow$_x_rb_ptr_");
    571     addOffset(OFFSETOF_MEMBER(mirror::Object, x_xpadding_), "shadow$_x_xpadding_");
    572 #endif
    573   }
    574 };
    575 
    576 struct ClassOffsets : public CheckOffsets<mirror::Class> {
    577   ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
    578     addOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags");
    579     addOffset(OFFSETOF_MEMBER(mirror::Class, class_flags_), "classFlags");
    580     addOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader");
    581     addOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize");
    582     addOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId");
    583     addOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType");
    584     addOffset(OFFSETOF_MEMBER(mirror::Class, copied_methods_offset_), "copiedMethodsOffset");
    585     addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache");
    586     addOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex");
    587     addOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex");
    588     addOffset(OFFSETOF_MEMBER(mirror::Class, ext_data_), "extData");
    589     addOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields");
    590     addOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable");
    591     addOffset(OFFSETOF_MEMBER(mirror::Class, methods_), "methods");
    592     addOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name");
    593     addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_),
    594               "numReferenceInstanceFields");
    595     addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_),
    596               "numReferenceStaticFields");
    597     addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize");
    598     addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_alloc_fast_path_),
    599               "objectSizeAllocFastPath");
    600     addOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType");
    601     addOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_),
    602               "referenceInstanceOffsets");
    603     addOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields");
    604     addOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status");
    605     addOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass");
    606     addOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_offset_), "virtualMethodsOffset");
    607     addOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable");
    608   }
    609 };
    610 
    611 struct ClassExtOffsets : public CheckOffsets<mirror::ClassExt> {
    612   ClassExtOffsets() : CheckOffsets<mirror::ClassExt>(false, "Ldalvik/system/ClassExt;") {
    613     addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_dex_caches_), "obsoleteDexCaches");
    614     addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_methods_), "obsoleteMethods");
    615     addOffset(OFFSETOF_MEMBER(mirror::ClassExt, original_dex_file_), "originalDexFile");
    616     addOffset(OFFSETOF_MEMBER(mirror::ClassExt, pre_redefine_class_def_index_),
    617               "preRedefineClassDefIndex");
    618     addOffset(OFFSETOF_MEMBER(mirror::ClassExt, pre_redefine_dex_file_ptr_),
    619               "preRedefineDexFilePtr");
    620     addOffset(OFFSETOF_MEMBER(mirror::ClassExt, verify_error_), "verifyError");
    621   }
    622 };
    623 
    624 struct StringOffsets : public CheckOffsets<mirror::String> {
    625   StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
    626     addOffset(OFFSETOF_MEMBER(mirror::String, count_), "count");
    627     addOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hash");
    628   }
    629 };
    630 
    631 struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
    632   ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
    633     addOffset(OFFSETOF_MEMBER(mirror::Throwable, backtrace_), "backtrace");
    634     addOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause");
    635     addOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage");
    636     addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace");
    637     addOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions");
    638   }
    639 };
    640 
    641 struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
    642   StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(
    643       false, "Ljava/lang/StackTraceElement;") {
    644     addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass");
    645     addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName");
    646     addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber");
    647     addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName");
    648   }
    649 };
    650 
    651 struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
    652   ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
    653     addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, allocator_), "allocator");
    654     addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, class_table_), "classTable");
    655     addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages");
    656     addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent");
    657     addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache");
    658   }
    659 };
    660 
    661 struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
    662   ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
    663     addOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h");
    664   }
    665 };
    666 
    667 struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
    668   DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
    669     addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile");
    670     addOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location");
    671     addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_preresolved_strings_), "numPreResolvedStrings");
    672     addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_call_sites_), "numResolvedCallSites");
    673     addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_fields_), "numResolvedFields");
    674     addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_method_types_), "numResolvedMethodTypes");
    675     addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_methods_), "numResolvedMethods");
    676     addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_types_), "numResolvedTypes");
    677     addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_strings_), "numStrings");
    678     addOffset(OFFSETOF_MEMBER(mirror::DexCache, preresolved_strings_), "preResolvedStrings");
    679     addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_call_sites_), "resolvedCallSites");
    680     addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields");
    681     addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_method_types_), "resolvedMethodTypes");
    682     addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods");
    683     addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes");
    684     addOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings");
    685   }
    686 };
    687 
    688 struct ReferenceOffsets : public CheckOffsets<mirror::Reference> {
    689   ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") {
    690     addOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext");
    691     addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue");
    692     addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext");
    693     addOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent");
    694   }
    695 };
    696 
    697 struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> {
    698   FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>(
    699       false, "Ljava/lang/ref/FinalizerReference;") {
    700     addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next");
    701     addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev");
    702     addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie");
    703   }
    704 };
    705 
    706 struct AccessibleObjectOffsets : public CheckOffsets<mirror::AccessibleObject> {
    707   AccessibleObjectOffsets() : CheckOffsets<mirror::AccessibleObject>(
    708       false, "Ljava/lang/reflect/AccessibleObject;") {
    709     addOffset(mirror::AccessibleObject::FlagOffset().Uint32Value(), "override");
    710   }
    711 };
    712 
    713 struct FieldOffsets : public CheckOffsets<mirror::Field> {
    714   FieldOffsets() : CheckOffsets<mirror::Field>(false, "Ljava/lang/reflect/Field;") {
    715     addOffset(OFFSETOF_MEMBER(mirror::Field, access_flags_), "accessFlags");
    716     addOffset(OFFSETOF_MEMBER(mirror::Field, declaring_class_), "declaringClass");
    717     addOffset(OFFSETOF_MEMBER(mirror::Field, dex_field_index_), "dexFieldIndex");
    718     addOffset(OFFSETOF_MEMBER(mirror::Field, offset_), "offset");
    719     addOffset(OFFSETOF_MEMBER(mirror::Field, type_), "type");
    720   }
    721 };
    722 
    723 struct ExecutableOffsets : public CheckOffsets<mirror::Executable> {
    724   ExecutableOffsets() : CheckOffsets<mirror::Executable>(
    725       false, "Ljava/lang/reflect/Executable;") {
    726     addOffset(OFFSETOF_MEMBER(mirror::Executable, access_flags_), "accessFlags");
    727     addOffset(OFFSETOF_MEMBER(mirror::Executable, art_method_), "artMethod");
    728     addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_), "declaringClass");
    729     addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_of_overridden_method_),
    730               "declaringClassOfOverriddenMethod");
    731     addOffset(OFFSETOF_MEMBER(mirror::Executable, dex_method_index_), "dexMethodIndex");
    732     addOffset(OFFSETOF_MEMBER(mirror::Executable, has_real_parameter_data_),
    733               "hasRealParameterData");
    734     addOffset(OFFSETOF_MEMBER(mirror::Executable, parameters_), "parameters");
    735   }
    736 };
    737 
    738 struct MethodTypeOffsets : public CheckOffsets<mirror::MethodType> {
    739   MethodTypeOffsets() : CheckOffsets<mirror::MethodType>(
    740       false, "Ljava/lang/invoke/MethodType;") {
    741     addOffset(OFFSETOF_MEMBER(mirror::MethodType, form_), "form");
    742     addOffset(OFFSETOF_MEMBER(mirror::MethodType, method_descriptor_), "methodDescriptor");
    743     addOffset(OFFSETOF_MEMBER(mirror::MethodType, p_types_), "ptypes");
    744     addOffset(OFFSETOF_MEMBER(mirror::MethodType, r_type_), "rtype");
    745     addOffset(OFFSETOF_MEMBER(mirror::MethodType, wrap_alt_), "wrapAlt");
    746   }
    747 };
    748 
    749 struct MethodHandleOffsets : public CheckOffsets<mirror::MethodHandle> {
    750   MethodHandleOffsets() : CheckOffsets<mirror::MethodHandle>(
    751       false, "Ljava/lang/invoke/MethodHandle;") {
    752     addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, art_field_or_method_), "artFieldOrMethod");
    753     addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, cached_spread_invoker_),
    754               "cachedSpreadInvoker");
    755     addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, handle_kind_), "handleKind");
    756     addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, nominal_type_), "nominalType");
    757     addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, method_type_), "type");
    758   }
    759 };
    760 
    761 struct MethodHandleImplOffsets : public CheckOffsets<mirror::MethodHandleImpl> {
    762   MethodHandleImplOffsets() : CheckOffsets<mirror::MethodHandleImpl>(
    763       false, "Ljava/lang/invoke/MethodHandleImpl;") {
    764     addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, info_), "info");
    765   }
    766 };
    767 
    768 struct MethodHandlesLookupOffsets : public CheckOffsets<mirror::MethodHandlesLookup> {
    769   MethodHandlesLookupOffsets() : CheckOffsets<mirror::MethodHandlesLookup>(
    770       false, "Ljava/lang/invoke/MethodHandles$Lookup;") {
    771     addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, allowed_modes_), "allowedModes");
    772     addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, lookup_class_), "lookupClass");
    773   }
    774 };
    775 
    776 struct EmulatedStackFrameOffsets : public CheckOffsets<mirror::EmulatedStackFrame> {
    777   EmulatedStackFrameOffsets() : CheckOffsets<mirror::EmulatedStackFrame>(
    778       false, "Ldalvik/system/EmulatedStackFrame;") {
    779     addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, callsite_type_), "callsiteType");
    780     addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, references_), "references");
    781     addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, stack_frame_), "stackFrame");
    782     addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, type_), "type");
    783   }
    784 };
    785 
    786 struct CallSiteOffsets : public CheckOffsets<mirror::CallSite> {
    787   CallSiteOffsets() : CheckOffsets<mirror::CallSite>(
    788       false, "Ljava/lang/invoke/CallSite;") {
    789     addOffset(OFFSETOF_MEMBER(mirror::CallSite, target_), "target");
    790   }
    791 };
    792 
    793 struct VarHandleOffsets : public CheckOffsets<mirror::VarHandle> {
    794   VarHandleOffsets() : CheckOffsets<mirror::VarHandle>(
    795       false, "Ljava/lang/invoke/VarHandle;") {
    796     addOffset(OFFSETOF_MEMBER(mirror::VarHandle, access_modes_bit_mask_), "accessModesBitMask");
    797     addOffset(OFFSETOF_MEMBER(mirror::VarHandle, coordinate_type0_), "coordinateType0");
    798     addOffset(OFFSETOF_MEMBER(mirror::VarHandle, coordinate_type1_), "coordinateType1");
    799     addOffset(OFFSETOF_MEMBER(mirror::VarHandle, var_type_), "varType");
    800   }
    801 };
    802 
    803 struct FieldVarHandleOffsets : public CheckOffsets<mirror::FieldVarHandle> {
    804   FieldVarHandleOffsets() : CheckOffsets<mirror::FieldVarHandle>(
    805       false, "Ljava/lang/invoke/FieldVarHandle;") {
    806     addOffset(OFFSETOF_MEMBER(mirror::FieldVarHandle, art_field_), "artField");
    807   }
    808 };
    809 
    810 struct ArrayElementVarHandleOffsets : public CheckOffsets<mirror::ArrayElementVarHandle> {
    811   ArrayElementVarHandleOffsets() : CheckOffsets<mirror::ArrayElementVarHandle>(
    812       false, "Ljava/lang/invoke/ArrayElementVarHandle;") {
    813   }
    814 };
    815 
    816 struct ByteArrayViewVarHandleOffsets : public CheckOffsets<mirror::ByteArrayViewVarHandle> {
    817   ByteArrayViewVarHandleOffsets() : CheckOffsets<mirror::ByteArrayViewVarHandle>(
    818       false, "Ljava/lang/invoke/ByteArrayViewVarHandle;") {
    819     addOffset(OFFSETOF_MEMBER(mirror::ByteArrayViewVarHandle, native_byte_order_),
    820               "nativeByteOrder");
    821   }
    822 };
    823 
    824 struct ByteBufferViewVarHandleOffsets : public CheckOffsets<mirror::ByteBufferViewVarHandle> {
    825   ByteBufferViewVarHandleOffsets() : CheckOffsets<mirror::ByteBufferViewVarHandle>(
    826       false, "Ljava/lang/invoke/ByteBufferViewVarHandle;") {
    827     addOffset(OFFSETOF_MEMBER(mirror::ByteBufferViewVarHandle, native_byte_order_),
    828               "nativeByteOrder");
    829   }
    830 };
    831 
    832 // C++ fields must exactly match the fields in the Java classes. If this fails,
    833 // reorder the fields in the C++ class. Managed class fields are ordered by
    834 // ClassLinker::LinkFields.
    835 TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
    836   ScopedObjectAccess soa(Thread::Current());
    837   EXPECT_TRUE(ObjectOffsets().Check());
    838   EXPECT_TRUE(ClassOffsets().Check());
    839   EXPECT_TRUE(ClassExtOffsets().Check());
    840   EXPECT_TRUE(StringOffsets().Check());
    841   EXPECT_TRUE(ThrowableOffsets().Check());
    842   EXPECT_TRUE(StackTraceElementOffsets().Check());
    843   EXPECT_TRUE(ClassLoaderOffsets().Check());
    844   EXPECT_TRUE(ProxyOffsets().Check());
    845   EXPECT_TRUE(DexCacheOffsets().Check());
    846   EXPECT_TRUE(ReferenceOffsets().Check());
    847   EXPECT_TRUE(FinalizerReferenceOffsets().Check());
    848   EXPECT_TRUE(AccessibleObjectOffsets().Check());
    849   EXPECT_TRUE(FieldOffsets().Check());
    850   EXPECT_TRUE(ExecutableOffsets().Check());
    851   EXPECT_TRUE(MethodTypeOffsets().Check());
    852   EXPECT_TRUE(MethodHandleOffsets().Check());
    853   EXPECT_TRUE(MethodHandleImplOffsets().Check());
    854   EXPECT_TRUE(MethodHandlesLookupOffsets().Check());
    855   EXPECT_TRUE(EmulatedStackFrameOffsets().Check());
    856   EXPECT_TRUE(CallSiteOffsets().Check());
    857   EXPECT_TRUE(VarHandleOffsets().Check());
    858   EXPECT_TRUE(FieldVarHandleOffsets().Check());
    859   EXPECT_TRUE(ArrayElementVarHandleOffsets().Check());
    860   EXPECT_TRUE(ByteArrayViewVarHandleOffsets().Check());
    861   EXPECT_TRUE(ByteBufferViewVarHandleOffsets().Check());
    862 }
    863 
    864 TEST_F(ClassLinkerTest, FindClassNonexistent) {
    865   ScopedObjectAccess soa(Thread::Current());
    866   AssertNonExistentClass("NoSuchClass;");
    867   AssertNonExistentClass("LNoSuchClass;");
    868 }
    869 
    870 TEST_F(ClassLinkerTest, GetDexFiles) {
    871   ScopedObjectAccess soa(Thread::Current());
    872 
    873   jobject jclass_loader = LoadDex("Nested");
    874   std::vector<const DexFile*> dex_files(GetDexFiles(jclass_loader));
    875   ASSERT_EQ(dex_files.size(), 1U);
    876   EXPECT_TRUE(android::base::EndsWith(dex_files[0]->GetLocation(), "Nested.jar"));
    877 
    878   jobject jclass_loader2 = LoadDex("MultiDex");
    879   std::vector<const DexFile*> dex_files2(GetDexFiles(jclass_loader2));
    880   ASSERT_EQ(dex_files2.size(), 2U);
    881   EXPECT_TRUE(android::base::EndsWith(dex_files2[0]->GetLocation(), "MultiDex.jar"));
    882 }
    883 
    884 TEST_F(ClassLinkerTest, FindClassNested) {
    885   ScopedObjectAccess soa(Thread::Current());
    886   StackHandleScope<1> hs(soa.Self());
    887   Handle<mirror::ClassLoader> class_loader(
    888       hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Nested"))));
    889 
    890   ObjPtr<mirror::Class> outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader);
    891   ASSERT_TRUE(outer != nullptr);
    892   EXPECT_EQ(0U, outer->NumVirtualMethods());
    893   EXPECT_EQ(1U, outer->NumDirectMethods());
    894 
    895   ObjPtr<mirror::Class> inner =
    896       class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader);
    897   ASSERT_TRUE(inner != nullptr);
    898   EXPECT_EQ(0U, inner->NumVirtualMethods());
    899   EXPECT_EQ(1U, inner->NumDirectMethods());
    900 }
    901 
    902 TEST_F(ClassLinkerTest, FindClass_Primitives) {
    903   ScopedObjectAccess soa(Thread::Current());
    904   const std::string expected("BCDFIJSZV");
    905   for (int ch = 1; ch < 256; ++ch) {
    906     std::string descriptor;
    907     descriptor.push_back(ch);
    908     if (expected.find(ch) == std::string::npos) {
    909       AssertNonExistentClass(descriptor);
    910     } else {
    911       AssertPrimitiveClass(descriptor);
    912     }
    913   }
    914 }
    915 
    916 TEST_F(ClassLinkerTest, FindClass) {
    917   ScopedObjectAccess soa(Thread::Current());
    918   StackHandleScope<2> hs(soa.Self());
    919   Handle<mirror::Class> JavaLangObject = hs.NewHandle(
    920       class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
    921   AssertObjectClass(JavaLangObject.Get());
    922 
    923   Handle<mirror::ClassLoader> class_loader(
    924       hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
    925   AssertNonExistentClass("LMyClass;");
    926   ObjPtr<mirror::Class> MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
    927   ASSERT_TRUE(MyClass != nullptr);
    928   ASSERT_TRUE(MyClass->GetClass() != nullptr);
    929   ASSERT_OBJ_PTR_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
    930   EXPECT_OBJ_PTR_EQ(JavaLangObject.Get(), MyClass->GetClass()->GetSuperClass());
    931   std::string temp;
    932   ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;");
    933   EXPECT_OBJ_PTR_EQ(MyClass->GetSuperClass(), JavaLangObject.Get());
    934   EXPECT_TRUE(MyClass->HasSuperClass());
    935   EXPECT_OBJ_PTR_EQ(class_loader.Get(), MyClass->GetClassLoader());
    936   EXPECT_EQ(ClassStatus::kResolved, MyClass->GetStatus());
    937   EXPECT_FALSE(MyClass->IsErroneous());
    938   EXPECT_TRUE(MyClass->IsLoaded());
    939   EXPECT_TRUE(MyClass->IsResolved());
    940   EXPECT_FALSE(MyClass->IsErroneousResolved());
    941   EXPECT_FALSE(MyClass->IsVerified());
    942   EXPECT_FALSE(MyClass->IsInitialized());
    943   EXPECT_FALSE(MyClass->IsArrayInstance());
    944   EXPECT_FALSE(MyClass->IsArrayClass());
    945   EXPECT_TRUE(MyClass->GetComponentType() == nullptr);
    946   EXPECT_FALSE(MyClass->IsInterface());
    947   EXPECT_FALSE(MyClass->IsPublic());
    948   EXPECT_FALSE(MyClass->IsFinal());
    949   EXPECT_FALSE(MyClass->IsPrimitive());
    950   EXPECT_FALSE(MyClass->IsSynthetic());
    951   EXPECT_EQ(1U, MyClass->NumDirectMethods());
    952   EXPECT_EQ(0U, MyClass->NumVirtualMethods());
    953   EXPECT_EQ(0U, MyClass->NumInstanceFields());
    954   EXPECT_EQ(0U, MyClass->NumStaticFields());
    955   EXPECT_EQ(0U, MyClass->NumDirectInterfaces());
    956 
    957   EXPECT_OBJ_PTR_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
    958 
    959   // created by class_linker
    960   AssertArrayClass("[C", "C", nullptr);
    961   AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr);
    962   // synthesized on the fly
    963   AssertArrayClass("[[C", "[C", nullptr);
    964   AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get());
    965   // or not available at all
    966   AssertNonExistentClass("[[[[LNonExistentClass;");
    967 }
    968 
    969 TEST_F(ClassLinkerTest, LookupResolvedType) {
    970   ScopedObjectAccess soa(Thread::Current());
    971   StackHandleScope<1> hs(soa.Self());
    972   Handle<mirror::ClassLoader> class_loader(
    973       hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
    974   AssertNonExistentClass("LMyClass;");
    975   ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
    976   dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_;
    977   ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
    978   EXPECT_OBJ_PTR_EQ(
    979       class_linker_->LookupResolvedType(type_idx, dex_cache, class_loader.Get()),
    980       klass);
    981   // Zero out the resolved type and make sure LookupResolvedType still finds it.
    982   dex_cache->ClearResolvedType(type_idx);
    983   EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
    984   EXPECT_OBJ_PTR_EQ(
    985       class_linker_->LookupResolvedType(type_idx, dex_cache, class_loader.Get()),
    986       klass);
    987 }
    988 
    989 TEST_F(ClassLinkerTest, LookupResolvedTypeArray) {
    990   ScopedObjectAccess soa(Thread::Current());
    991   StackHandleScope<2> hs(soa.Self());
    992   Handle<mirror::ClassLoader> class_loader(
    993       hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("AllFields"))));
    994   // Get the AllFields class for the dex cache and dex file.
    995   ObjPtr<mirror::Class> all_fields_klass
    996       = class_linker_->FindClass(soa.Self(), "LAllFields;", class_loader);
    997   ASSERT_TRUE(all_fields_klass != nullptr);
    998   Handle<mirror::DexCache> dex_cache = hs.NewHandle(all_fields_klass->GetDexCache());
    999   const DexFile& dex_file = *dex_cache->GetDexFile();
   1000   // Get the index of the array class we want to test.
   1001   const dex::TypeId* array_id = dex_file.FindTypeId("[Ljava/lang/Object;");
   1002   ASSERT_TRUE(array_id != nullptr);
   1003   dex::TypeIndex array_idx = dex_file.GetIndexForTypeId(*array_id);
   1004   // Check that the array class wasn't resolved yet.
   1005   EXPECT_TRUE(
   1006       class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()) == nullptr);
   1007   // Resolve the array class we want to test.
   1008   ObjPtr<mirror::Class> array_klass
   1009       = class_linker_->FindClass(soa.Self(), "[Ljava/lang/Object;", class_loader);
   1010   ASSERT_TRUE(array_klass != nullptr);
   1011   // Test that LookupResolvedType() finds the array class.
   1012   EXPECT_OBJ_PTR_EQ(
   1013       class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()),
   1014       array_klass);
   1015   // Zero out the resolved type and make sure LookupResolvedType() still finds it.
   1016   dex_cache->ClearResolvedType(array_idx);
   1017   EXPECT_TRUE(dex_cache->GetResolvedType(array_idx) == nullptr);
   1018   EXPECT_OBJ_PTR_EQ(
   1019       class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()),
   1020       array_klass);
   1021 }
   1022 
   1023 TEST_F(ClassLinkerTest, LookupResolvedTypeErroneousInit) {
   1024   ScopedObjectAccess soa(Thread::Current());
   1025   StackHandleScope<3> hs(soa.Self());
   1026   Handle<mirror::ClassLoader> class_loader(
   1027       hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("ErroneousInit"))));
   1028   AssertNonExistentClass("LErroneousInit;");
   1029   Handle<mirror::Class> klass =
   1030       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LErroneousInit;", class_loader));
   1031   ASSERT_TRUE(klass != nullptr);
   1032   dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_;
   1033   Handle<mirror::DexCache> dex_cache = hs.NewHandle(klass->GetDexCache());
   1034   EXPECT_OBJ_PTR_EQ(
   1035       class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
   1036       klass.Get());
   1037   // Zero out the resolved type and make sure LookupResolvedType still finds it.
   1038   dex_cache->ClearResolvedType(type_idx);
   1039   EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
   1040   EXPECT_OBJ_PTR_EQ(
   1041       class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
   1042       klass.Get());
   1043   // Force initialization to turn the class erroneous.
   1044   bool initialized = class_linker_->EnsureInitialized(soa.Self(),
   1045                                                       klass,
   1046                                                       /* can_init_fields= */ true,
   1047                                                       /* can_init_parents= */ true);
   1048   EXPECT_FALSE(initialized);
   1049   EXPECT_TRUE(soa.Self()->IsExceptionPending());
   1050   soa.Self()->ClearException();
   1051   // Check that the LookupResolvedType() can still find the resolved type.
   1052   EXPECT_OBJ_PTR_EQ(
   1053       class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
   1054       klass.Get());
   1055   // Zero out the resolved type and make sure LookupResolvedType() still finds it.
   1056   dex_cache->ClearResolvedType(type_idx);
   1057   EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
   1058   EXPECT_OBJ_PTR_EQ(
   1059       class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
   1060       klass.Get());
   1061 }
   1062 
   1063 TEST_F(ClassLinkerTest, LibCore) {
   1064   ScopedObjectAccess soa(Thread::Current());
   1065   ASSERT_TRUE(java_lang_dex_file_ != nullptr);
   1066   AssertDexFile(*java_lang_dex_file_, nullptr);
   1067 }
   1068 
   1069 // The first reference array element must be a multiple of 4 bytes from the
   1070 // start of the object
   1071 TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
   1072   ScopedObjectAccess soa(Thread::Current());
   1073   ObjPtr<mirror::Class> array_class =
   1074       class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
   1075   ObjPtr<mirror::ObjectArray<mirror::String>> array =
   1076       mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0);
   1077   uintptr_t data_offset =
   1078       reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>),
   1079                                                     0));
   1080   if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) {
   1081     EXPECT_TRUE(IsAligned<4>(data_offset));  // Check 4 byte alignment.
   1082   } else {
   1083     EXPECT_TRUE(IsAligned<8>(data_offset));  // Check 8 byte alignment.
   1084   }
   1085 }
   1086 
   1087 TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
   1088   ScopedObjectAccess soa(Thread::Current());
   1089   StackHandleScope<5> hs(soa.Self());
   1090   Handle<mirror::LongArray> long_array(hs.NewHandle(mirror::LongArray::Alloc(soa.Self(), 0)));
   1091   EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass());
   1092   uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
   1093   EXPECT_TRUE(IsAligned<8>(data_offset));  // Longs require 8 byte alignment
   1094 
   1095   Handle<mirror::DoubleArray> double_array(hs.NewHandle(mirror::DoubleArray::Alloc(soa.Self(), 0)));
   1096   EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass());
   1097   data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
   1098   EXPECT_TRUE(IsAligned<8>(data_offset));  // Doubles require 8 byte alignment
   1099 
   1100   Handle<mirror::IntArray> int_array(hs.NewHandle(mirror::IntArray::Alloc(soa.Self(), 0)));
   1101   EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass());
   1102   data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
   1103   EXPECT_TRUE(IsAligned<4>(data_offset));  // Ints require 4 byte alignment
   1104 
   1105   Handle<mirror::CharArray> char_array(hs.NewHandle(mirror::CharArray::Alloc(soa.Self(), 0)));
   1106   EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass());
   1107   data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
   1108   EXPECT_TRUE(IsAligned<2>(data_offset));  // Chars require 2 byte alignment
   1109 
   1110   Handle<mirror::ShortArray> short_array(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0)));
   1111   EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass());
   1112   data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
   1113   EXPECT_TRUE(IsAligned<2>(data_offset));  // Shorts require 2 byte alignment
   1114 
   1115   // Take it as given that bytes and booleans have byte alignment
   1116 }
   1117 
   1118 TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
   1119   // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
   1120   // This lets UnboxPrimitive avoid searching for the field by name at runtime.
   1121   ScopedObjectAccess soa(Thread::Current());
   1122   ScopedNullHandle<mirror::ClassLoader> class_loader;
   1123   ObjPtr<mirror::Class> c;
   1124   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader);
   1125   EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
   1126   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader);
   1127   EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
   1128   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader);
   1129   EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
   1130   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader);
   1131   EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
   1132   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader);
   1133   EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
   1134   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", class_loader);
   1135   EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
   1136   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Long;", class_loader);
   1137   EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
   1138   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Short;", class_loader);
   1139   EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
   1140 }
   1141 
   1142 TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
   1143   ScopedObjectAccess soa(Thread::Current());
   1144   StackHandleScope<3> hs(soa.Self());
   1145   Handle<mirror::ClassLoader> class_loader_1(
   1146       hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
   1147   Handle<mirror::ClassLoader> class_loader_2(
   1148       hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
   1149   Handle<mirror::Class> MyClass_1 = hs.NewHandle(
   1150       class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1));
   1151   ObjPtr<mirror::Class> MyClass_2 =
   1152       class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_2);
   1153   EXPECT_TRUE(MyClass_1 != nullptr);
   1154   EXPECT_TRUE(MyClass_2 != nullptr);
   1155   EXPECT_OBJ_PTR_NE(MyClass_1.Get(), MyClass_2);
   1156 }
   1157 
   1158 TEST_F(ClassLinkerTest, StaticFields) {
   1159   ScopedObjectAccess soa(Thread::Current());
   1160   StackHandleScope<2> hs(soa.Self());
   1161   Handle<mirror::ClassLoader> class_loader(
   1162       hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
   1163   Handle<mirror::Class> statics(
   1164       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
   1165   class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
   1166 
   1167   // Static final primitives that are initialized by a compile-time constant
   1168   // expression resolve to a copy of a constant value from the constant pool.
   1169   // So <clinit> should be null.
   1170   ArtMethod* clinit = statics->FindClassMethod("<clinit>", "()V", kRuntimePointerSize);
   1171   EXPECT_TRUE(clinit == nullptr);
   1172 
   1173   EXPECT_EQ(9U, statics->NumStaticFields());
   1174 
   1175   ArtField* s0 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s0", "Z");
   1176   EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
   1177   EXPECT_EQ(true, s0->GetBoolean(statics.Get()));
   1178   s0->SetBoolean<false>(statics.Get(), false);
   1179 
   1180   ArtField* s1 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s1", "B");
   1181   EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
   1182   EXPECT_EQ(5, s1->GetByte(statics.Get()));
   1183   s1->SetByte<false>(statics.Get(), 6);
   1184 
   1185   ArtField* s2 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s2", "C");
   1186   EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
   1187   EXPECT_EQ('a', s2->GetChar(statics.Get()));
   1188   s2->SetChar<false>(statics.Get(), 'b');
   1189 
   1190   ArtField* s3 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s3", "S");
   1191   EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
   1192   EXPECT_EQ(-536, s3->GetShort(statics.Get()));
   1193   s3->SetShort<false>(statics.Get(), -535);
   1194 
   1195   ArtField* s4 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s4", "I");
   1196   EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
   1197   EXPECT_EQ(2000000000, s4->GetInt(statics.Get()));
   1198   s4->SetInt<false>(statics.Get(), 2000000001);
   1199 
   1200   ArtField* s5 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s5", "J");
   1201   EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
   1202   EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get()));
   1203   s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12));
   1204 
   1205   ArtField* s6 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s6", "F");
   1206   EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
   1207   EXPECT_DOUBLE_EQ(0.5, s6->GetFloat(statics.Get()));
   1208   s6->SetFloat<false>(statics.Get(), 0.75);
   1209 
   1210   ArtField* s7 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s7", "D");
   1211   EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
   1212   EXPECT_DOUBLE_EQ(16777217.0, s7->GetDouble(statics.Get()));
   1213   s7->SetDouble<false>(statics.Get(), 16777219);
   1214 
   1215   ArtField* s8 = mirror::Class::FindStaticField(
   1216       soa.Self(), statics.Get(), "s8", "Ljava/lang/String;");
   1217   EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
   1218   EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android"));
   1219   ObjPtr<mirror::String> str_value = mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot");
   1220   s8->SetObject<false>(s8->GetDeclaringClass(), str_value);
   1221 
   1222   // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
   1223   // http://code.google.com/p/googletest/issues/detail?id=322
   1224   EXPECT_FALSE(s0->GetBoolean(statics.Get()));
   1225   EXPECT_EQ(6, s1->GetByte(statics.Get()));
   1226   EXPECT_EQ('b', s2->GetChar(statics.Get()));
   1227   EXPECT_EQ(-535, s3->GetShort(statics.Get()));
   1228   EXPECT_EQ(2000000001, s4->GetInt(statics.Get()));
   1229   EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get()));
   1230   EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get()));
   1231   EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get()));
   1232   EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot"));
   1233 }
   1234 
   1235 TEST_F(ClassLinkerTest, Interfaces) {
   1236   ScopedObjectAccess soa(Thread::Current());
   1237   StackHandleScope<6> hs(soa.Self());
   1238   Handle<mirror::ClassLoader> class_loader(
   1239       hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Interfaces"))));
   1240   Handle<mirror::Class> I(
   1241       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
   1242   Handle<mirror::Class> J(
   1243       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
   1244   Handle<mirror::Class> K(
   1245       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader)));
   1246   Handle<mirror::Class> A(
   1247       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader)));
   1248   Handle<mirror::Class> B(
   1249       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader)));
   1250   EXPECT_TRUE(I->IsAssignableFrom(A.Get()));
   1251   EXPECT_TRUE(J->IsAssignableFrom(A.Get()));
   1252   EXPECT_TRUE(J->IsAssignableFrom(K.Get()));
   1253   EXPECT_TRUE(K->IsAssignableFrom(B.Get()));
   1254   EXPECT_TRUE(J->IsAssignableFrom(B.Get()));
   1255 
   1256   const std::string_view void_sig("()V");
   1257   ArtMethod* Ii = I->FindClassMethod("i", void_sig, kRuntimePointerSize);
   1258   ArtMethod* Jj1 = J->FindClassMethod("j1", void_sig, kRuntimePointerSize);
   1259   ArtMethod* Jj2 = J->FindClassMethod("j2", void_sig, kRuntimePointerSize);
   1260   ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig, kRuntimePointerSize);
   1261   ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig, kRuntimePointerSize);
   1262   ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig, kRuntimePointerSize);
   1263   ArtMethod* Ai = A->FindClassMethod("i", void_sig, kRuntimePointerSize);
   1264   ArtMethod* Aj1 = A->FindClassMethod("j1", void_sig, kRuntimePointerSize);
   1265   ArtMethod* Aj2 = A->FindClassMethod("j2", void_sig, kRuntimePointerSize);
   1266   ASSERT_TRUE(Ii != nullptr);
   1267   ASSERT_FALSE(Ii->IsDirect());
   1268   ASSERT_TRUE(Jj1 != nullptr);
   1269   ASSERT_FALSE(Jj1->IsDirect());
   1270   ASSERT_TRUE(Jj2 != nullptr);
   1271   ASSERT_FALSE(Jj2->IsDirect());
   1272   ASSERT_TRUE(Kj1 != nullptr);
   1273   ASSERT_TRUE(Kj2 != nullptr);
   1274   ASSERT_TRUE(Kk != nullptr);
   1275   ASSERT_TRUE(Ai != nullptr);
   1276   ASSERT_FALSE(Ai->IsDirect());
   1277   ASSERT_TRUE(Aj1 != nullptr);
   1278   ASSERT_FALSE(Aj1->IsDirect());
   1279   ASSERT_TRUE(Aj2 != nullptr);
   1280   ASSERT_FALSE(Aj2->IsDirect());
   1281   EXPECT_NE(Ii, Ai);
   1282   EXPECT_NE(Jj1, Aj1);
   1283   EXPECT_NE(Jj2, Aj2);
   1284   EXPECT_EQ(Kj1, Jj1);
   1285   EXPECT_EQ(Kj2, Jj2);
   1286   EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, kRuntimePointerSize));
   1287   EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, kRuntimePointerSize));
   1288   EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, kRuntimePointerSize));
   1289   EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii, kRuntimePointerSize));
   1290   EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1, kRuntimePointerSize));
   1291   EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2, kRuntimePointerSize));
   1292 
   1293   ArtField* Afoo =
   1294       mirror::Class::FindStaticField(soa.Self(), A.Get(), "foo", "Ljava/lang/String;");
   1295   ArtField* Bfoo =
   1296       mirror::Class::FindStaticField(soa.Self(), B.Get(), "foo", "Ljava/lang/String;");
   1297   ArtField* Jfoo =
   1298       mirror::Class::FindStaticField(soa.Self(), J.Get(), "foo", "Ljava/lang/String;");
   1299   ArtField* Kfoo =
   1300       mirror::Class::FindStaticField(soa.Self(), K.Get(), "foo", "Ljava/lang/String;");
   1301   ASSERT_TRUE(Afoo != nullptr);
   1302   EXPECT_EQ(Afoo, Bfoo);
   1303   EXPECT_EQ(Afoo, Jfoo);
   1304   EXPECT_EQ(Afoo, Kfoo);
   1305 }
   1306 
   1307 TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
   1308   // pretend we are trying to get the static storage for the StaticsFromCode class.
   1309 
   1310   // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
   1311   // case 2, get the initialized storage from StaticsFromCode.getS0
   1312 
   1313   ScopedObjectAccess soa(Thread::Current());
   1314   jobject jclass_loader = LoadDex("StaticsFromCode");
   1315   const DexFile* dex_file = GetFirstDexFile(jclass_loader);
   1316   StackHandleScope<1> hs(soa.Self());
   1317   Handle<mirror::ClassLoader> class_loader(
   1318       hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
   1319   ObjPtr<mirror::Class> klass =
   1320       class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader);
   1321   ArtMethod* clinit = klass->FindClassInitializer(kRuntimePointerSize);
   1322   ArtMethod* getS0 =
   1323       klass->FindClassMethod("getS0", "()Ljava/lang/Object;", kRuntimePointerSize);
   1324   ASSERT_TRUE(getS0 != nullptr);
   1325   ASSERT_TRUE(getS0->IsStatic());
   1326   const dex::TypeId* type_id = dex_file->FindTypeId("LStaticsFromCode;");
   1327   ASSERT_TRUE(type_id != nullptr);
   1328   dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id);
   1329   ObjPtr<mirror::Class> uninit = ResolveVerifyAndClinit(type_idx,
   1330                                                         clinit,
   1331                                                         soa.Self(),
   1332                                                         /* can_run_clinit= */ true,
   1333                                                         /* verify_access= */ false);
   1334   EXPECT_TRUE(uninit != nullptr);
   1335   EXPECT_FALSE(uninit->IsInitialized());
   1336   ObjPtr<mirror::Class> init = ResolveVerifyAndClinit(type_idx,
   1337                                                       getS0,
   1338                                                       soa.Self(),
   1339                                                       /* can_run_clinit= */ true,
   1340                                                       /* verify_access= */ false);
   1341   EXPECT_TRUE(init != nullptr);
   1342   EXPECT_TRUE(init->IsInitialized());
   1343 }
   1344 
   1345 TEST_F(ClassLinkerTest, ErroneousClass) {
   1346   ScopedObjectAccess soa(Thread::Current());
   1347   jobject jclass_loader = LoadMultiDex("ErroneousA", "ErroneousB");
   1348   StackHandleScope<1> hs(soa.Self());
   1349   Handle<mirror::ClassLoader> class_loader(
   1350       hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
   1351   hs.Self()->AssertNoPendingException();
   1352   const char* descriptor = "LErroneous;";
   1353   ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), descriptor, class_loader);
   1354   // Erronenous since we are extending final class.
   1355   hs.Self()->AssertPendingException();
   1356   EXPECT_TRUE(klass == nullptr);
   1357   klass = class_linker_->LookupClass(soa.Self(), descriptor, class_loader.Get());
   1358   EXPECT_FALSE(klass == nullptr);
   1359   EXPECT_TRUE(klass->IsErroneous());
   1360   EXPECT_TRUE(klass->GetIfTable() != nullptr);
   1361 }
   1362 
   1363 TEST_F(ClassLinkerTest, FinalizableBit) {
   1364   ScopedObjectAccess soa(Thread::Current());
   1365   ObjPtr<mirror::Class> c;
   1366 
   1367   // Object has a finalize method, but we know it's empty.
   1368   c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
   1369   EXPECT_FALSE(c->IsFinalizable());
   1370 
   1371   // Enum has a finalize method to prevent its subclasses from implementing one.
   1372   c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;");
   1373   EXPECT_FALSE(c->IsFinalizable());
   1374 
   1375   // RoundingMode is an enum.
   1376   c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;");
   1377   EXPECT_FALSE(c->IsFinalizable());
   1378 
   1379   // RandomAccessFile extends Object and overrides finalize.
   1380   c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;");
   1381   EXPECT_TRUE(c->IsFinalizable());
   1382 
   1383   // FileInputStream is finalizable and extends InputStream which isn't.
   1384   c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;");
   1385   EXPECT_FALSE(c->IsFinalizable());
   1386   c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;");
   1387   EXPECT_TRUE(c->IsFinalizable());
   1388 
   1389   // ScheduledThreadPoolExecutor doesn't have a finalize method but
   1390   // extends ThreadPoolExecutor which does.
   1391   c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;");
   1392   EXPECT_TRUE(c->IsFinalizable());
   1393   c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
   1394   EXPECT_TRUE(c->IsFinalizable());
   1395 }
   1396 
   1397 TEST_F(ClassLinkerTest, ClassRootDescriptors) {
   1398   ScopedObjectAccess soa(Thread::Current());
   1399   std::string temp;
   1400   for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) {
   1401     ObjPtr<mirror::Class> klass = GetClassRoot(ClassRoot(i), class_linker_);
   1402     EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U);
   1403     EXPECT_STREQ(klass->GetDescriptor(&temp), GetClassRootDescriptor(ClassRoot(i))) << " i = " << i;
   1404   }
   1405 }
   1406 
   1407 TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) {
   1408   ScopedObjectAccess soa(Thread::Current());
   1409   ScopedNullHandle<mirror::ClassLoader> class_loader;
   1410   ObjPtr<mirror::Class> c;
   1411 
   1412   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader);
   1413   ASSERT_TRUE(c != nullptr);
   1414   EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize(kRuntimePointerSize));
   1415 
   1416   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader);
   1417   ASSERT_TRUE(c != nullptr);
   1418   EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize(kRuntimePointerSize));
   1419 
   1420   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader);
   1421   ASSERT_TRUE(c != nullptr);
   1422   EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize(kRuntimePointerSize));
   1423 
   1424   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader);
   1425   ASSERT_TRUE(c != nullptr);
   1426   EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize(kRuntimePointerSize));
   1427 }
   1428 
   1429 static void CheckMethod(ArtMethod* method, bool verified)
   1430     REQUIRES_SHARED(Locks::mutator_lock_) {
   1431   if (!method->IsNative() && !method->IsAbstract()) {
   1432     EXPECT_EQ((method->GetAccessFlags() & kAccSkipAccessChecks) != 0U, verified)
   1433         << method->PrettyMethod(true);
   1434   }
   1435 }
   1436 
   1437 static void CheckVerificationAttempted(ObjPtr<mirror::Class> c, bool preverified)
   1438     REQUIRES_SHARED(Locks::mutator_lock_) {
   1439   EXPECT_EQ((c->GetAccessFlags() & kAccVerificationAttempted) != 0U, preverified)
   1440       << "Class " << mirror::Class::PrettyClass(c) << " not as expected";
   1441   for (auto& m : c->GetMethods(kRuntimePointerSize)) {
   1442     CheckMethod(&m, preverified);
   1443   }
   1444 }
   1445 
   1446 TEST_F(ClassLinkerTest, Preverified_InitializedBoot) {
   1447   ScopedObjectAccess soa(Thread::Current());
   1448 
   1449   ObjPtr<mirror::Class> JavaLangObject =
   1450       class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
   1451   ASSERT_TRUE(JavaLangObject != nullptr);
   1452   EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the "
   1453                                                   "core";
   1454   CheckVerificationAttempted(JavaLangObject, true);
   1455 }
   1456 
   1457 TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) {
   1458   ScopedObjectAccess soa(Thread::Current());
   1459 
   1460   StackHandleScope<1> hs(soa.Self());
   1461 
   1462   Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass(
   1463       soa.Self(), "Ljava/lang/SecurityManager;")));
   1464   EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the "
   1465                                                      "core";
   1466 
   1467   CheckVerificationAttempted(security_manager.Get(), false);
   1468 
   1469   class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true);
   1470   CheckVerificationAttempted(security_manager.Get(), true);
   1471 }
   1472 
   1473 TEST_F(ClassLinkerTest, Preverified_App) {
   1474   ScopedObjectAccess soa(Thread::Current());
   1475 
   1476   StackHandleScope<2> hs(soa.Self());
   1477   Handle<mirror::ClassLoader> class_loader(
   1478       hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
   1479   Handle<mirror::Class> statics(
   1480       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
   1481 
   1482   CheckVerificationAttempted(statics.Get(), false);
   1483 
   1484   class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
   1485   CheckVerificationAttempted(statics.Get(), true);
   1486 }
   1487 
   1488 TEST_F(ClassLinkerTest, IsBootStrapClassLoaded) {
   1489   ScopedObjectAccess soa(Thread::Current());
   1490 
   1491   StackHandleScope<3> hs(soa.Self());
   1492   Handle<mirror::ClassLoader> class_loader(
   1493       hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
   1494 
   1495   // java.lang.Object is a bootstrap class.
   1496   Handle<mirror::Class> jlo_class(
   1497       hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
   1498   ASSERT_TRUE(jlo_class != nullptr);
   1499   EXPECT_TRUE(jlo_class->IsBootStrapClassLoaded());
   1500 
   1501   // Statics is not a bootstrap class.
   1502   Handle<mirror::Class> statics(
   1503       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
   1504   ASSERT_TRUE(statics != nullptr);
   1505   EXPECT_FALSE(statics->IsBootStrapClassLoaded());
   1506 }
   1507 
   1508 // Regression test for b/26799552.
   1509 TEST_F(ClassLinkerTest, RegisterDexFileName) {
   1510   ScopedObjectAccess soa(Thread::Current());
   1511   StackHandleScope<2> hs(soa.Self());
   1512   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
   1513   MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr));
   1514   {
   1515     ReaderMutexLock mu(soa.Self(), *Locks::dex_lock_);
   1516     for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
   1517       dex_cache.Assign(soa.Self()->DecodeJObject(data.weak_root)->AsDexCache());
   1518       if (dex_cache != nullptr) {
   1519         break;
   1520       }
   1521     }
   1522     ASSERT_TRUE(dex_cache != nullptr);
   1523   }
   1524   // Make a copy of the dex cache and change the name.
   1525   dex_cache.Assign(dex_cache->Clone(soa.Self())->AsDexCache());
   1526   const uint16_t data[] = { 0x20AC, 0x20A1 };
   1527   Handle<mirror::String> location(hs.NewHandle(mirror::String::AllocFromUtf16(soa.Self(),
   1528                                                                               arraysize(data),
   1529                                                                               data)));
   1530   dex_cache->SetLocation(location.Get());
   1531   const DexFile* old_dex_file = dex_cache->GetDexFile();
   1532 
   1533   std::unique_ptr<DexFile> dex_file(new StandardDexFile(old_dex_file->Begin(),
   1534                                                         old_dex_file->Size(),
   1535                                                         location->ToModifiedUtf8(),
   1536                                                         0u,
   1537                                                         nullptr,
   1538                                                         nullptr));
   1539   {
   1540     WriterMutexLock mu(soa.Self(), *Locks::dex_lock_);
   1541     // Check that inserting with a UTF16 name works.
   1542     class_linker->RegisterDexFileLocked(*dex_file, dex_cache.Get(), /* class_loader= */ nullptr);
   1543   }
   1544 }
   1545 
   1546 TEST_F(ClassLinkerMethodHandlesTest, TestResolveMethodTypes) {
   1547   ScopedObjectAccess soa(Thread::Current());
   1548   StackHandleScope<7> hs(soa.Self());
   1549 
   1550   Handle<mirror::ClassLoader> class_loader(
   1551       hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MethodTypes"))));
   1552   Handle<mirror::Class> method_types(
   1553       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader)));
   1554   class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
   1555 
   1556   ArtMethod* method1 = method_types->FindClassMethod(
   1557       "method1",
   1558       "(Ljava/lang/String;)Ljava/lang/String;",
   1559       kRuntimePointerSize);
   1560   ASSERT_TRUE(method1 != nullptr);
   1561   ASSERT_FALSE(method1->IsDirect());
   1562 
   1563   const DexFile& dex_file = *(method1->GetDexFile());
   1564   Handle<mirror::DexCache> dex_cache = hs.NewHandle(
   1565       class_linker_->FindDexCache(soa.Self(), dex_file));
   1566 
   1567   const dex::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
   1568 
   1569   // This is the MethodType corresponding to the prototype of
   1570   // String MethodTypes# method1(String).
   1571   // Its RType = Ljava/lang/String;
   1572   // Its PTypes = { Ljava/lang/String; }
   1573   Handle<mirror::MethodType> method1_type = hs.NewHandle(
   1574       class_linker_->ResolveMethodType(soa.Self(), method1_id.proto_idx_, dex_cache, class_loader));
   1575 
   1576   // Assert that the method type was resolved successfully.
   1577   ASSERT_TRUE(method1_type != nullptr);
   1578 
   1579   // Assert that the return type and the method arguments are as we expect.
   1580   Handle<mirror::Class> string_class(hs.NewHandle(class_linker_->FindClass(soa.Self(),
   1581                                                                            "Ljava/lang/String;",
   1582                                                                            class_loader)));
   1583   ASSERT_OBJ_PTR_EQ(string_class.Get(), method1_type->GetRType());
   1584   ASSERT_OBJ_PTR_EQ(string_class.Get(), method1_type->GetPTypes()->Get(0));
   1585 
   1586   // Resolve the method type again and assert that we get back the same value.
   1587   Handle<mirror::MethodType> method1_type2 = hs.NewHandle(
   1588       class_linker_->ResolveMethodType(soa.Self(), method1_id.proto_idx_, dex_cache, class_loader));
   1589   ASSERT_OBJ_PTR_EQ(method1_type.Get(), method1_type2.Get());
   1590 
   1591   // Resolve the MethodType associated with a different method signature
   1592   // and assert it's different.
   1593   ArtMethod* method2 = method_types->FindClassMethod(
   1594       "method2",
   1595       "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
   1596       kRuntimePointerSize);
   1597   ASSERT_TRUE(method2 != nullptr);
   1598   ASSERT_FALSE(method2->IsDirect());
   1599   const dex::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
   1600   Handle<mirror::MethodType> method2_type = hs.NewHandle(
   1601       class_linker_->ResolveMethodType(soa.Self(), method2_id.proto_idx_, dex_cache, class_loader));
   1602   ASSERT_OBJ_PTR_NE(method1_type.Get(), method2_type.Get());
   1603 }
   1604 
   1605 // Verify that ClassLinker's CreateWellknownClassLoader works as expected
   1606 // by creating a chain of class loaders with various dex files.
   1607 TEST_F(ClassLinkerTest, CreateWellKnownClassLoader) {
   1608   // LoadDexIn*ClassLoader methods already assert that the parent loader is the expected one.
   1609   // No need to check again.
   1610   jobject class_loader_a = LoadDexInPathClassLoader("MyClass", nullptr);
   1611   jobject class_loader_b = LoadDexInDelegateLastClassLoader("Nested", class_loader_a);
   1612   jobject class_loader_c = LoadDexInPathClassLoader("MultiDex", class_loader_b);
   1613   LoadDexInDelegateLastClassLoader("Interfaces", class_loader_c);
   1614 }
   1615 
   1616 TEST_F(ClassLinkerTest, PrettyClass) {
   1617   ScopedObjectAccess soa(Thread::Current());
   1618   EXPECT_EQ("null", mirror::Class::PrettyClass(nullptr));
   1619   ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
   1620   ASSERT_TRUE(c != nullptr);
   1621   ObjPtr<mirror::Object> o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
   1622   EXPECT_EQ("java.lang.Class<java.lang.String[]>", mirror::Class::PrettyClass(o->GetClass()));
   1623 }
   1624 
   1625 TEST_F(ClassLinkerTest, PrettyClassAndClassLoader) {
   1626   ScopedObjectAccess soa(Thread::Current());
   1627   EXPECT_EQ("null", mirror::Class::PrettyClassAndClassLoader(nullptr));
   1628   ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
   1629   ASSERT_TRUE(c != nullptr);
   1630   ObjPtr<mirror::Object> o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
   1631   EXPECT_EQ("java.lang.Class<java.lang.String[],null>",
   1632             mirror::Class::PrettyClassAndClassLoader(o->GetClass()));
   1633 }
   1634 
   1635 TEST_F(ClassLinkerTest, PrettyField) {
   1636   ScopedObjectAccess soa(Thread::Current());
   1637   EXPECT_EQ("null", ArtField::PrettyField(nullptr));
   1638 
   1639   ObjPtr<mirror::Class> java_lang_String = class_linker_->FindSystemClass(soa.Self(),
   1640                                                                           "Ljava/lang/String;");
   1641 
   1642   ArtField* f;
   1643   f = java_lang_String->FindDeclaredInstanceField("count", "I");
   1644   EXPECT_EQ("int java.lang.String.count", f->PrettyField());
   1645   EXPECT_EQ("java.lang.String.count", f->PrettyField(false));
   1646 }
   1647 
   1648 TEST_F(ClassLinkerTest, JniShortName_JniLongName) {
   1649   ScopedObjectAccess soa(Thread::Current());
   1650   ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;");
   1651   ASSERT_TRUE(c != nullptr);
   1652   ArtMethod* m;
   1653 
   1654   m = c->FindClassMethod("charAt", "(I)C", kRuntimePointerSize);
   1655   ASSERT_TRUE(m != nullptr);
   1656   ASSERT_FALSE(m->IsDirect());
   1657   EXPECT_EQ("Java_java_lang_String_charAt", m->JniShortName());
   1658   EXPECT_EQ("Java_java_lang_String_charAt__I", m->JniLongName());
   1659 
   1660   m = c->FindClassMethod("indexOf", "(Ljava/lang/String;I)I", kRuntimePointerSize);
   1661   ASSERT_TRUE(m != nullptr);
   1662   ASSERT_FALSE(m->IsDirect());
   1663   EXPECT_EQ("Java_java_lang_String_indexOf", m->JniShortName());
   1664   EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", m->JniLongName());
   1665 
   1666   m = c->FindClassMethod("copyValueOf", "([CII)Ljava/lang/String;", kRuntimePointerSize);
   1667   ASSERT_TRUE(m != nullptr);
   1668   ASSERT_TRUE(m->IsStatic());
   1669   EXPECT_EQ("Java_java_lang_String_copyValueOf", m->JniShortName());
   1670   EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", m->JniLongName());
   1671 }
   1672 
   1673 class ClassLinkerClassLoaderTest : public ClassLinkerTest {
   1674  protected:
   1675   // Verifies that the class identified by the given descriptor is loaded with
   1676   // the expected_class_loader_obj when search from class_loader_to_search_obj.
   1677   // When expected_class_loader_obj is null the check will be done against BootClassLoader.
   1678   void VerifyClassResolution(const std::string& descriptor,
   1679                              jobject class_loader_to_search_obj,
   1680                              jobject expected_class_loader_obj,
   1681                              bool should_find = true) {
   1682     Thread* self = Thread::Current();
   1683     ScopedObjectAccess soa(self);
   1684     StackHandleScope<3> hs(self);
   1685     Handle<mirror::ClassLoader> class_loader_to_search(
   1686         hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_to_search_obj)));
   1687 
   1688     Handle<mirror::Class> klass = hs.NewHandle(
   1689         class_linker_->FindClass(soa.Self(), descriptor.c_str(), class_loader_to_search));
   1690 
   1691     if (!should_find) {
   1692       if (self->IsExceptionPending()) {
   1693         self->ClearException();
   1694       }
   1695       ASSERT_TRUE(klass == nullptr);
   1696     } else if (expected_class_loader_obj == nullptr) {
   1697       ASSERT_TRUE(ClassLinker::IsBootClassLoader(soa, klass->GetClassLoader()));
   1698     } else {
   1699       ASSERT_TRUE(klass != nullptr) << descriptor;
   1700       Handle<mirror::ClassLoader> expected_class_loader(
   1701           hs.NewHandle(soa.Decode<mirror::ClassLoader>(expected_class_loader_obj)));
   1702       ASSERT_OBJ_PTR_EQ(klass->GetClassLoader(), expected_class_loader.Get());
   1703     }
   1704   }
   1705 };
   1706 
   1707 TEST_F(ClassLinkerClassLoaderTest, CreatePathClassLoader) {
   1708   jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
   1709   VerifyClassResolution("LDefinedInA;", class_loader_a, class_loader_a);
   1710   VerifyClassResolution("Ljava/lang/String;", class_loader_a, nullptr);
   1711   VerifyClassResolution("LDefinedInB;", class_loader_a, nullptr, /*should_find=*/ false);
   1712 }
   1713 
   1714 TEST_F(ClassLinkerClassLoaderTest, CreateDelegateLastClassLoader) {
   1715   jobject class_loader_a = LoadDexInDelegateLastClassLoader("ForClassLoaderA", nullptr);
   1716   VerifyClassResolution("LDefinedInA;", class_loader_a, class_loader_a);
   1717   VerifyClassResolution("Ljava/lang/String;", class_loader_a, nullptr);
   1718   VerifyClassResolution("LDefinedInB;", class_loader_a, nullptr, /*should_find=*/ false);
   1719 }
   1720 
   1721 TEST_F(ClassLinkerClassLoaderTest, CreateClassLoaderChain) {
   1722   // The chain is
   1723   //    ClassLoaderA (PathClassLoader, defines: A, AB, AC, AD)
   1724   //       ^
   1725   //       |
   1726   //    ClassLoaderB (DelegateLastClassLoader, defines: B, AB, BC, BD)
   1727   //       ^
   1728   //       |
   1729   //    ClassLoaderC (PathClassLoader, defines: C, AC, BC, CD)
   1730   //       ^
   1731   //       |
   1732   //    ClassLoaderD (DelegateLastClassLoader, defines: D, AD, BD, CD)
   1733 
   1734   jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
   1735   jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a);
   1736   jobject class_loader_c = LoadDexInPathClassLoader("ForClassLoaderC", class_loader_b);
   1737   jobject class_loader_d = LoadDexInDelegateLastClassLoader("ForClassLoaderD", class_loader_c);
   1738 
   1739   // Verify exclusive classes (present in only one class loader).
   1740   VerifyClassResolution("LDefinedInD;", class_loader_d, class_loader_d);
   1741   VerifyClassResolution("LDefinedInC;", class_loader_d, class_loader_c);
   1742   VerifyClassResolution("LDefinedInB;", class_loader_d, class_loader_b);
   1743   VerifyClassResolution("LDefinedInA;", class_loader_d, class_loader_a);
   1744 
   1745   // Verify classes that are defined in multiple classloader.
   1746 
   1747   // Classes defined in B should be found in B even if they are defined in A or C because
   1748   // B is a DelegateLastClassLoader.
   1749   VerifyClassResolution("LDefinedInAB;", class_loader_d, class_loader_b);
   1750   VerifyClassResolution("LDefinedInABC;", class_loader_d, class_loader_b);
   1751   VerifyClassResolution("LDefinedInBC;", class_loader_d, class_loader_b);
   1752 
   1753   // Classes defined in D should be found in D even if they are defined in parent class loaders
   1754   // as well because D is a DelegateLastClassLoader.
   1755   VerifyClassResolution("LDefinedInAD;", class_loader_d, class_loader_d);
   1756   VerifyClassResolution("LDefinedInBD;", class_loader_d, class_loader_d);
   1757   VerifyClassResolution("LDefinedInCD;", class_loader_d, class_loader_d);
   1758 
   1759 
   1760   // Classes not defined in the DelegateLastClassLoaders (i.e. D or B) should be found
   1761   // in the top parent.
   1762   VerifyClassResolution("LDefinedInAC;", class_loader_d, class_loader_a);
   1763 
   1764   // Sanity check that we don't find an undefined class.
   1765   VerifyClassResolution("LNotDefined;", class_loader_d, nullptr, /*should_find=*/ false);
   1766 }
   1767 
   1768 }  // namespace art
   1769