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 <string>
     20 
     21 #include "UniquePtr.h"
     22 #include "class_linker-inl.h"
     23 #include "common_test.h"
     24 #include "dex_file.h"
     25 #include "entrypoints/entrypoint_utils.h"
     26 #include "gc/heap.h"
     27 #include "mirror/art_field-inl.h"
     28 #include "mirror/art_method.h"
     29 #include "mirror/art_method-inl.h"
     30 #include "mirror/class-inl.h"
     31 #include "mirror/dex_cache.h"
     32 #include "mirror/object-inl.h"
     33 #include "mirror/object_array-inl.h"
     34 #include "mirror/proxy.h"
     35 #include "mirror/stack_trace_element.h"
     36 #include "sirt_ref.h"
     37 
     38 namespace art {
     39 
     40 class ClassLinkerTest : public CommonTest {
     41  protected:
     42   void AssertNonExistentClass(const std::string& descriptor)
     43       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
     44     EXPECT_TRUE(class_linker_->FindSystemClass(descriptor.c_str()) == NULL);
     45     Thread* self = Thread::Current();
     46     EXPECT_TRUE(self->IsExceptionPending());
     47     mirror::Object* exception = self->GetException(NULL);
     48     self->ClearException();
     49     mirror::Class* exception_class = class_linker_->FindSystemClass("Ljava/lang/NoClassDefFoundError;");
     50     EXPECT_TRUE(exception->InstanceOf(exception_class));
     51   }
     52 
     53   void AssertPrimitiveClass(const std::string& descriptor)
     54       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
     55     AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(descriptor.c_str()));
     56   }
     57 
     58   void AssertPrimitiveClass(const std::string& descriptor, const mirror::Class* primitive)
     59       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
     60     ClassHelper primitive_ch(primitive);
     61     ASSERT_TRUE(primitive != NULL);
     62     ASSERT_TRUE(primitive->GetClass() != NULL);
     63     ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
     64     EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != NULL);
     65     ASSERT_STREQ(descriptor.c_str(), primitive_ch.GetDescriptor());
     66     EXPECT_TRUE(primitive->GetSuperClass() == NULL);
     67     EXPECT_FALSE(primitive->HasSuperClass());
     68     EXPECT_TRUE(primitive->GetClassLoader() == NULL);
     69     EXPECT_EQ(mirror::Class::kStatusInitialized, primitive->GetStatus());
     70     EXPECT_FALSE(primitive->IsErroneous());
     71     EXPECT_TRUE(primitive->IsLoaded());
     72     EXPECT_TRUE(primitive->IsResolved());
     73     EXPECT_TRUE(primitive->IsVerified());
     74     EXPECT_TRUE(primitive->IsInitialized());
     75     EXPECT_FALSE(primitive->IsArrayInstance());
     76     EXPECT_FALSE(primitive->IsArrayClass());
     77     EXPECT_TRUE(primitive->GetComponentType() == NULL);
     78     EXPECT_FALSE(primitive->IsInterface());
     79     EXPECT_TRUE(primitive->IsPublic());
     80     EXPECT_TRUE(primitive->IsFinal());
     81     EXPECT_TRUE(primitive->IsPrimitive());
     82     EXPECT_FALSE(primitive->IsSynthetic());
     83     EXPECT_EQ(0U, primitive->NumDirectMethods());
     84     EXPECT_EQ(0U, primitive->NumVirtualMethods());
     85     EXPECT_EQ(0U, primitive->NumInstanceFields());
     86     EXPECT_EQ(0U, primitive->NumStaticFields());
     87     EXPECT_EQ(0U, primitive_ch.NumDirectInterfaces());
     88     EXPECT_TRUE(primitive->GetVTable() == NULL);
     89     EXPECT_EQ(0, primitive->GetIfTableCount());
     90     EXPECT_TRUE(primitive->GetIfTable() == NULL);
     91     EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract, primitive->GetAccessFlags());
     92   }
     93 
     94   void AssertArrayClass(const std::string& array_descriptor,
     95                         const std::string& component_type,
     96                         mirror::ClassLoader* class_loader)
     97       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
     98     mirror::Class* array = class_linker_->FindClass(array_descriptor.c_str(), class_loader);
     99     ClassHelper array_component_ch(array->GetComponentType());
    100     EXPECT_STREQ(component_type.c_str(), array_component_ch.GetDescriptor());
    101     EXPECT_EQ(class_loader, array->GetClassLoader());
    102     EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract)));
    103     AssertArrayClass(array_descriptor, array);
    104   }
    105 
    106   void AssertArrayClass(const std::string& array_descriptor, mirror::Class* array)
    107       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
    108     ClassHelper kh(array);
    109     ASSERT_TRUE(array != NULL);
    110     ASSERT_TRUE(array->GetClass() != NULL);
    111     ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
    112     EXPECT_TRUE(array->GetClass()->GetSuperClass() != NULL);
    113     ASSERT_STREQ(array_descriptor.c_str(), kh.GetDescriptor());
    114     EXPECT_TRUE(array->GetSuperClass() != NULL);
    115     EXPECT_EQ(class_linker_->FindSystemClass("Ljava/lang/Object;"), array->GetSuperClass());
    116     EXPECT_TRUE(array->HasSuperClass());
    117     ASSERT_TRUE(array->GetComponentType() != NULL);
    118     kh.ChangeClass(array->GetComponentType());
    119     ASSERT_TRUE(kh.GetDescriptor() != NULL);
    120     EXPECT_EQ(mirror::Class::kStatusInitialized, array->GetStatus());
    121     EXPECT_FALSE(array->IsErroneous());
    122     EXPECT_TRUE(array->IsLoaded());
    123     EXPECT_TRUE(array->IsResolved());
    124     EXPECT_TRUE(array->IsVerified());
    125     EXPECT_TRUE(array->IsInitialized());
    126     EXPECT_FALSE(array->IsArrayInstance());
    127     EXPECT_TRUE(array->IsArrayClass());
    128     EXPECT_FALSE(array->IsInterface());
    129     EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
    130     EXPECT_TRUE(array->IsFinal());
    131     EXPECT_FALSE(array->IsPrimitive());
    132     EXPECT_FALSE(array->IsSynthetic());
    133     EXPECT_EQ(0U, array->NumDirectMethods());
    134     EXPECT_EQ(0U, array->NumVirtualMethods());
    135     EXPECT_EQ(0U, array->NumInstanceFields());
    136     EXPECT_EQ(0U, array->NumStaticFields());
    137     kh.ChangeClass(array);
    138     EXPECT_EQ(2U, kh.NumDirectInterfaces());
    139     EXPECT_TRUE(array->GetVTable() != NULL);
    140     EXPECT_EQ(2, array->GetIfTableCount());
    141     mirror::IfTable* iftable = array->GetIfTable();
    142     ASSERT_TRUE(iftable != NULL);
    143     kh.ChangeClass(kh.GetDirectInterface(0));
    144     EXPECT_STREQ(kh.GetDescriptor(), "Ljava/lang/Cloneable;");
    145     kh.ChangeClass(array);
    146     kh.ChangeClass(kh.GetDirectInterface(1));
    147     EXPECT_STREQ(kh.GetDescriptor(), "Ljava/io/Serializable;");
    148   }
    149 
    150   void AssertMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
    151     MethodHelper mh(method);
    152     EXPECT_TRUE(method != NULL);
    153     EXPECT_TRUE(method->GetClass() != NULL);
    154     EXPECT_TRUE(mh.GetName() != NULL);
    155     EXPECT_TRUE(mh.GetSignature() != NULL);
    156 
    157     EXPECT_TRUE(method->GetDexCacheStrings() != NULL);
    158     EXPECT_TRUE(method->GetDexCacheResolvedMethods() != NULL);
    159     EXPECT_TRUE(method->GetDexCacheResolvedTypes() != NULL);
    160     EXPECT_TRUE(method->GetDexCacheInitializedStaticStorage() != NULL);
    161     EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetStrings(),
    162               method->GetDexCacheStrings());
    163     EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods(),
    164               method->GetDexCacheResolvedMethods());
    165     EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedTypes(),
    166               method->GetDexCacheResolvedTypes());
    167     EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetInitializedStaticStorage(),
    168               method->GetDexCacheInitializedStaticStorage());
    169   }
    170 
    171   void AssertField(mirror::Class* klass, mirror::ArtField* field)
    172       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
    173     FieldHelper fh(field);
    174     EXPECT_TRUE(field != NULL);
    175     EXPECT_TRUE(field->GetClass() != NULL);
    176     EXPECT_EQ(klass, field->GetDeclaringClass());
    177     EXPECT_TRUE(fh.GetName() != NULL);
    178     EXPECT_TRUE(fh.GetType() != NULL);
    179   }
    180 
    181   void AssertClass(const std::string& descriptor, mirror::Class* klass)
    182       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
    183     ClassHelper kh(klass);
    184     EXPECT_STREQ(descriptor.c_str(), kh.GetDescriptor());
    185     if (descriptor == "Ljava/lang/Object;") {
    186       EXPECT_FALSE(klass->HasSuperClass());
    187     } else {
    188       EXPECT_TRUE(klass->HasSuperClass());
    189       EXPECT_TRUE(klass->GetSuperClass() != NULL);
    190     }
    191     EXPECT_TRUE(klass->GetClass() != NULL);
    192     EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
    193     EXPECT_TRUE(klass->GetDexCache() != NULL);
    194     EXPECT_TRUE(klass->IsLoaded());
    195     EXPECT_TRUE(klass->IsResolved());
    196     EXPECT_FALSE(klass->IsErroneous());
    197     EXPECT_FALSE(klass->IsArrayClass());
    198     EXPECT_TRUE(klass->GetComponentType() == NULL);
    199     EXPECT_TRUE(klass->IsInSamePackage(klass));
    200     EXPECT_TRUE(mirror::Class::IsInSamePackage(kh.GetDescriptor(), kh.GetDescriptor()));
    201     if (klass->IsInterface()) {
    202       EXPECT_TRUE(klass->IsAbstract());
    203       if (klass->NumDirectMethods() == 1) {
    204         MethodHelper mh(klass->GetDirectMethod(0));
    205         EXPECT_TRUE(mh.IsClassInitializer());
    206         EXPECT_TRUE(klass->GetDirectMethod(0)->IsDirect());
    207       } else {
    208         EXPECT_EQ(0U, klass->NumDirectMethods());
    209       }
    210     } else {
    211       if (!klass->IsSynthetic()) {
    212         EXPECT_NE(0U, klass->NumDirectMethods());
    213       }
    214     }
    215     EXPECT_EQ(klass->IsInterface(), klass->GetVTable() == NULL);
    216     const mirror::IfTable* iftable = klass->GetIfTable();
    217     for (int i = 0; i < klass->GetIfTableCount(); i++) {
    218       mirror::Class* interface = iftable->GetInterface(i);
    219       ASSERT_TRUE(interface != NULL);
    220       if (klass->IsInterface()) {
    221         EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
    222       } else {
    223         EXPECT_EQ(interface->NumVirtualMethods(), iftable->GetMethodArrayCount(i));
    224       }
    225     }
    226     if (klass->IsAbstract()) {
    227       EXPECT_FALSE(klass->IsFinal());
    228     } else {
    229       EXPECT_FALSE(klass->IsAnnotation());
    230     }
    231     if (klass->IsFinal()) {
    232       EXPECT_FALSE(klass->IsAbstract());
    233       EXPECT_FALSE(klass->IsAnnotation());
    234     }
    235     if (klass->IsAnnotation()) {
    236       EXPECT_FALSE(klass->IsFinal());
    237       EXPECT_TRUE(klass->IsAbstract());
    238     }
    239 
    240     EXPECT_FALSE(klass->IsPrimitive());
    241     EXPECT_TRUE(klass->CanAccess(klass));
    242 
    243     for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
    244       mirror::ArtMethod* method = klass->GetDirectMethod(i);
    245       AssertMethod(method);
    246       EXPECT_TRUE(method->IsDirect());
    247       EXPECT_EQ(klass, method->GetDeclaringClass());
    248     }
    249 
    250     for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
    251       mirror::ArtMethod* method = klass->GetVirtualMethod(i);
    252       AssertMethod(method);
    253       EXPECT_FALSE(method->IsDirect());
    254       EXPECT_TRUE(method->GetDeclaringClass()->IsAssignableFrom(klass));
    255     }
    256 
    257     for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
    258       mirror::ArtField* field = klass->GetInstanceField(i);
    259       AssertField(klass, field);
    260       EXPECT_FALSE(field->IsStatic());
    261     }
    262 
    263     for (size_t i = 0; i < klass->NumStaticFields(); i++) {
    264       mirror::ArtField* field = klass->GetStaticField(i);
    265       AssertField(klass, field);
    266       EXPECT_TRUE(field->IsStatic());
    267     }
    268 
    269     // Confirm that all instances fields are packed together at the start
    270     EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
    271     FieldHelper fh;
    272     for (size_t i = 0; i < klass->NumReferenceInstanceFields(); i++) {
    273       mirror::ArtField* field = klass->GetInstanceField(i);
    274       fh.ChangeField(field);
    275       ASSERT_TRUE(!fh.IsPrimitiveType());
    276       mirror::Class* field_type = fh.GetType();
    277       ASSERT_TRUE(field_type != NULL);
    278       ASSERT_TRUE(!field_type->IsPrimitive());
    279     }
    280     for (size_t i = klass->NumReferenceInstanceFields(); i < klass->NumInstanceFields(); i++) {
    281       mirror::ArtField* field = klass->GetInstanceField(i);
    282       fh.ChangeField(field);
    283       mirror::Class* field_type = fh.GetType();
    284       ASSERT_TRUE(field_type != NULL);
    285       if (!fh.IsPrimitiveType() || !field_type->IsPrimitive()) {
    286         // While Reference.referent is not primitive, the ClassLinker
    287         // treats it as such so that the garbage collector won't scan it.
    288         EXPECT_EQ(PrettyField(field), "java.lang.Object java.lang.ref.Reference.referent");
    289       }
    290     }
    291 
    292     size_t total_num_reference_instance_fields = 0;
    293     mirror::Class* k = klass;
    294     while (k != NULL) {
    295       total_num_reference_instance_fields += k->NumReferenceInstanceFields();
    296       k = k->GetSuperClass();
    297     }
    298     EXPECT_EQ(klass->GetReferenceInstanceOffsets() == 0,
    299               total_num_reference_instance_fields == 0);
    300   }
    301 
    302   void AssertDexFileClass(mirror::ClassLoader* class_loader, const std::string& descriptor)
    303       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
    304     ASSERT_TRUE(descriptor != NULL);
    305     mirror::Class* klass = class_linker_->FindSystemClass(descriptor.c_str());
    306     ASSERT_TRUE(klass != NULL);
    307     EXPECT_STREQ(descriptor.c_str(), ClassHelper(klass).GetDescriptor());
    308     EXPECT_EQ(class_loader, klass->GetClassLoader());
    309     if (klass->IsPrimitive()) {
    310       AssertPrimitiveClass(descriptor, klass);
    311     } else if (klass->IsArrayClass()) {
    312       AssertArrayClass(descriptor, klass);
    313     } else {
    314       AssertClass(descriptor, klass);
    315     }
    316   }
    317 
    318   void AssertDexFile(const DexFile* dex, mirror::ClassLoader* class_loader)
    319       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
    320     ASSERT_TRUE(dex != NULL);
    321 
    322     // Verify all the classes defined in this file
    323     for (size_t i = 0; i < dex->NumClassDefs(); i++) {
    324       const DexFile::ClassDef& class_def = dex->GetClassDef(i);
    325       const char* descriptor = dex->GetClassDescriptor(class_def);
    326       AssertDexFileClass(class_loader, descriptor);
    327     }
    328     // Verify all the types referenced by this file
    329     for (size_t i = 0; i < dex->NumTypeIds(); i++) {
    330       const DexFile::TypeId& type_id = dex->GetTypeId(i);
    331       const char* descriptor = dex->GetTypeDescriptor(type_id);
    332       AssertDexFileClass(class_loader, descriptor);
    333     }
    334     class_linker_->VisitRoots(TestRootVisitor, NULL, false, false);
    335     // Verify the dex cache has resolution methods in all resolved method slots
    336     mirror::DexCache* dex_cache = class_linker_->FindDexCache(*dex);
    337     mirror::ObjectArray<mirror::ArtMethod>* resolved_methods = dex_cache->GetResolvedMethods();
    338     for (size_t i = 0; i < static_cast<size_t>(resolved_methods->GetLength()); i++) {
    339       EXPECT_TRUE(resolved_methods->Get(i) != NULL);
    340     }
    341   }
    342 
    343   static void TestRootVisitor(const mirror::Object* root, void*) {
    344     EXPECT_TRUE(root != NULL);
    345   }
    346 };
    347 
    348 struct CheckOffset {
    349   size_t cpp_offset;
    350   const char* java_name;
    351   CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
    352 };
    353 
    354 template <typename T>
    355 struct CheckOffsets {
    356   CheckOffsets(bool is_static, const char* class_descriptor)
    357       : is_static(is_static), class_descriptor(class_descriptor) {}
    358   bool is_static;
    359   std::string class_descriptor;
    360   std::vector<CheckOffset> offsets;
    361 
    362   bool Check() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
    363     mirror::Class* klass = Runtime::Current()->GetClassLinker()->FindSystemClass(class_descriptor.c_str());
    364     CHECK(klass != NULL) << class_descriptor;
    365 
    366     bool error = false;
    367 
    368     if (!klass->IsClassClass() && !is_static) {
    369       size_t expected_size = is_static ? klass->GetClassSize(): klass->GetObjectSize();
    370       if (sizeof(T) != expected_size) {
    371         LOG(ERROR) << "Class size mismatch:"
    372            << " class=" << class_descriptor
    373            << " Java=" << expected_size
    374            << " C++=" << sizeof(T);
    375         error = true;
    376       }
    377     }
    378 
    379     size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
    380     if (offsets.size() != num_fields) {
    381       LOG(ERROR) << "Field count mismatch:"
    382          << " class=" << class_descriptor
    383          << " Java=" << num_fields
    384          << " C++=" << offsets.size();
    385       error = true;
    386     }
    387 
    388     FieldHelper fh;
    389     for (size_t i = 0; i < offsets.size(); i++) {
    390       mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
    391       fh.ChangeField(field);
    392       StringPiece field_name(fh.GetName());
    393       if (field_name != offsets[i].java_name) {
    394         error = true;
    395       }
    396     }
    397     if (error) {
    398       for (size_t i = 0; i < offsets.size(); i++) {
    399         CheckOffset& offset = offsets[i];
    400         mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
    401         fh.ChangeField(field);
    402         StringPiece field_name(fh.GetName());
    403         if (field_name != offsets[i].java_name) {
    404           LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
    405         }
    406         LOG(ERROR) << "Java field order:"
    407            << " i=" << i << " class=" << class_descriptor
    408            << " Java=" << field_name
    409            << " CheckOffsets=" << offset.java_name;
    410       }
    411     }
    412 
    413     for (size_t i = 0; i < offsets.size(); i++) {
    414       CheckOffset& offset = offsets[i];
    415       mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
    416       if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
    417         error = true;
    418       }
    419     }
    420     if (error) {
    421       for (size_t i = 0; i < offsets.size(); i++) {
    422         CheckOffset& offset = offsets[i];
    423         mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
    424         if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
    425           LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
    426         }
    427         LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
    428            << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
    429       }
    430     }
    431 
    432     return !error;
    433   };
    434 
    435  private:
    436   DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
    437 };
    438 
    439 // Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
    440 // is first since if it is failing, others are unlikely to succeed.
    441 
    442 struct ObjectOffsets : public CheckOffsets<mirror::Object> {
    443   ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
    444     // alphabetical references
    445     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Object, klass_),   "shadow$_klass_"));
    446 
    447     // alphabetical 32-bit
    448     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_"));
    449   };
    450 };
    451 
    452 struct ArtFieldOffsets : public CheckOffsets<mirror::ArtField> {
    453   ArtFieldOffsets() : CheckOffsets<mirror::ArtField>(false, "Ljava/lang/reflect/ArtField;") {
    454     // alphabetical references
    455     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, declaring_class_), "declaringClass"));
    456 
    457     // alphabetical 32-bit
    458     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, access_flags_),    "accessFlags"));
    459     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, field_dex_idx_),   "fieldDexIndex"));
    460     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, offset_),          "offset"));
    461   };
    462 };
    463 
    464 struct ArtMethodOffsets : public CheckOffsets<mirror::ArtMethod> {
    465   ArtMethodOffsets() : CheckOffsets<mirror::ArtMethod>(false, "Ljava/lang/reflect/ArtMethod;") {
    466     // alphabetical references
    467     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, declaring_class_),                      "declaringClass"));
    468     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_cache_initialized_static_storage_), "dexCacheInitializedStaticStorage"));
    469     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_cache_resolved_methods_),           "dexCacheResolvedMethods"));
    470     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_cache_resolved_types_),             "dexCacheResolvedTypes"));
    471     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_cache_strings_),                    "dexCacheStrings"));
    472 
    473     // alphabetical 32-bit
    474     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, access_flags_),                   "accessFlags"));
    475     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, code_item_offset_),               "codeItemOffset"));
    476     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, core_spill_mask_),                "coreSpillMask"));
    477     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, entry_point_from_compiled_code_), "entryPointFromCompiledCode"));
    478     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, entry_point_from_interpreter_),   "entryPointFromInterpreter"));
    479     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, fp_spill_mask_),                  "fpSpillMask"));
    480     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, frame_size_in_bytes_),            "frameSizeInBytes"));
    481     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, gc_map_),                         "gcMap"));
    482     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, mapping_table_),                  "mappingTable"));
    483     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, method_dex_index_),               "methodDexIndex"));
    484     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, method_index_),                   "methodIndex"));
    485     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, native_method_),                  "nativeMethod"));
    486     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, vmap_table_),                     "vmapTable"));
    487   };
    488 };
    489 
    490 struct ClassOffsets : public CheckOffsets<mirror::Class> {
    491   ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
    492     // alphabetical references
    493     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_),                  "classLoader"));
    494     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, component_type_),                "componentType"));
    495     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_),                     "dexCache"));
    496     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, direct_methods_),                "directMethods"));
    497     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, ifields_),                       "iFields"));
    498     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, iftable_),                       "ifTable"));
    499     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, name_),                          "name"));
    500     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, sfields_),                       "sFields"));
    501     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, super_class_),                   "superClass"));
    502     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, verify_error_class_),            "verifyErrorClass"));
    503     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_),               "virtualMethods"));
    504     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, vtable_),                        "vtable"));
    505 
    506     // alphabetical 32-bit
    507     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_),                  "accessFlags"));
    508     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, class_size_),                    "classSize"));
    509     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_),              "clinitThreadId"));
    510     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_),             "dexClassDefIndex"));
    511     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_),                  "dexTypeIndex"));
    512     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_), "numReferenceInstanceFields"));
    513     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_),   "numReferenceStaticFields"));
    514     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, object_size_),                   "objectSize"));
    515     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_),                "primitiveType"));
    516     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_),    "referenceInstanceOffsets"));
    517     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, reference_static_offsets_),      "referenceStaticOffsets"));
    518     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, status_),                        "status"));
    519   };
    520 };
    521 
    522 struct StringOffsets : public CheckOffsets<mirror::String> {
    523   StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
    524     // alphabetical references
    525     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, array_),     "value"));
    526 
    527     // alphabetical 32-bit
    528     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, count_),     "count"));
    529     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hashCode"));
    530     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, offset_),    "offset"));
    531   };
    532 };
    533 
    534 struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
    535   ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
    536     // alphabetical references
    537     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_),                 "cause"));
    538     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_),        "detailMessage"));
    539     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_state_),           "stackState"));
    540     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_),           "stackTrace"));
    541     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions"));
    542   };
    543 };
    544 
    545 struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
    546   StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(false, "Ljava/lang/StackTraceElement;") {
    547     // alphabetical references
    548     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass"));
    549     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_),       "fileName"));
    550     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_),     "methodName"));
    551     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_),     "lineNumber"));
    552   };
    553 };
    554 
    555 struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
    556   ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
    557     // alphabetical references
    558     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_),   "packages"));
    559     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_),     "parent"));
    560     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache"));
    561   };
    562 };
    563 
    564 struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
    565   ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
    566     // alphabetical references
    567     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h"));
    568   };
    569 };
    570 
    571 struct ClassClassOffsets : public CheckOffsets<mirror::ClassClass> {
    572   ClassClassOffsets() : CheckOffsets<mirror::ClassClass>(true, "Ljava/lang/Class;") {
    573     // alphabetical 64-bit
    574     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ClassClass, serialVersionUID_), "serialVersionUID"));
    575   };
    576 };
    577 
    578 struct StringClassOffsets : public CheckOffsets<mirror::StringClass> {
    579   StringClassOffsets() : CheckOffsets<mirror::StringClass>(true, "Ljava/lang/String;") {
    580     // alphabetical references
    581     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StringClass, ASCII_),                  "ASCII"));
    582     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StringClass, CASE_INSENSITIVE_ORDER_), "CASE_INSENSITIVE_ORDER"));
    583 
    584     // alphabetical 64-bit
    585     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StringClass, serialVersionUID_),       "serialVersionUID"));
    586 
    587     // alphabetical 32-bit
    588     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StringClass, REPLACEMENT_CHAR_),       "REPLACEMENT_CHAR"));
    589   };
    590 };
    591 
    592 struct ArtFieldClassOffsets : public CheckOffsets<mirror::ArtFieldClass> {
    593   ArtFieldClassOffsets() : CheckOffsets<mirror::ArtFieldClass>(true, "Ljava/lang/reflect/ArtField;") {
    594   };
    595 };
    596 
    597 struct ArtMethodClassOffsets : public CheckOffsets<mirror::ArtMethodClass> {
    598   ArtMethodClassOffsets() : CheckOffsets<mirror::ArtMethodClass>(true, "Ljava/lang/reflect/ArtMethod;") {
    599   };
    600 };
    601 
    602 struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
    603   DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
    604     // alphabetical references
    605     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_),                        "dex"));
    606     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, initialized_static_storage_), "initializedStaticStorage"));
    607     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, location_),                   "location"));
    608     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_),            "resolvedFields"));
    609     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_),           "resolvedMethods"));
    610     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_),             "resolvedTypes"));
    611     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_),                    "strings"));
    612     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_),                   "dexFile"));
    613   };
    614 };
    615 
    616 // C++ fields must exactly match the fields in the Java classes. If this fails,
    617 // reorder the fields in the C++ class. Managed class fields are ordered by
    618 // ClassLinker::LinkFields.
    619 TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
    620   ScopedObjectAccess soa(Thread::Current());
    621   EXPECT_TRUE(ObjectOffsets().Check());
    622   EXPECT_TRUE(ArtFieldOffsets().Check());
    623   EXPECT_TRUE(ArtMethodOffsets().Check());
    624   EXPECT_TRUE(ClassOffsets().Check());
    625   EXPECT_TRUE(StringOffsets().Check());
    626   EXPECT_TRUE(ThrowableOffsets().Check());
    627   EXPECT_TRUE(StackTraceElementOffsets().Check());
    628   EXPECT_TRUE(ClassLoaderOffsets().Check());
    629   EXPECT_TRUE(ProxyOffsets().Check());
    630   EXPECT_TRUE(DexCacheOffsets().Check());
    631 
    632   EXPECT_TRUE(ClassClassOffsets().Check());
    633   EXPECT_TRUE(StringClassOffsets().Check());
    634   EXPECT_TRUE(ArtFieldClassOffsets().Check());
    635   EXPECT_TRUE(ArtMethodClassOffsets().Check());
    636 }
    637 
    638 TEST_F(ClassLinkerTest, FindClassNonexistent) {
    639   ScopedObjectAccess soa(Thread::Current());
    640   AssertNonExistentClass("NoSuchClass;");
    641   AssertNonExistentClass("LNoSuchClass;");
    642 }
    643 
    644 TEST_F(ClassLinkerTest, FindClassNested) {
    645   ScopedObjectAccess soa(Thread::Current());
    646   SirtRef<mirror::ClassLoader> class_loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(LoadDex("Nested")));
    647 
    648   mirror::Class* outer = class_linker_->FindClass("LNested;", class_loader.get());
    649   ASSERT_TRUE(outer != NULL);
    650   EXPECT_EQ(0U, outer->NumVirtualMethods());
    651   EXPECT_EQ(1U, outer->NumDirectMethods());
    652 
    653   mirror::Class* inner = class_linker_->FindClass("LNested$Inner;", class_loader.get());
    654   ASSERT_TRUE(inner != NULL);
    655   EXPECT_EQ(0U, inner->NumVirtualMethods());
    656   EXPECT_EQ(1U, inner->NumDirectMethods());
    657 }
    658 
    659 TEST_F(ClassLinkerTest, FindClass_Primitives) {
    660   ScopedObjectAccess soa(Thread::Current());
    661   const std::string expected("BCDFIJSZV");
    662   for (int ch = 1; ch < 256; ++ch) {
    663     std::string descriptor;
    664     descriptor.push_back(ch);
    665     if (expected.find(ch) == std::string::npos) {
    666       AssertNonExistentClass(descriptor);
    667     } else {
    668       AssertPrimitiveClass(descriptor);
    669     }
    670   }
    671 }
    672 
    673 TEST_F(ClassLinkerTest, FindClass) {
    674   ScopedObjectAccess soa(Thread::Current());
    675   mirror::Class* JavaLangObject = class_linker_->FindSystemClass("Ljava/lang/Object;");
    676   ClassHelper kh(JavaLangObject);
    677   ASSERT_TRUE(JavaLangObject != NULL);
    678   ASSERT_TRUE(JavaLangObject->GetClass() != NULL);
    679   ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
    680   EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
    681   ASSERT_STREQ(kh.GetDescriptor(), "Ljava/lang/Object;");
    682   EXPECT_TRUE(JavaLangObject->GetSuperClass() == NULL);
    683   EXPECT_FALSE(JavaLangObject->HasSuperClass());
    684   EXPECT_TRUE(JavaLangObject->GetClassLoader() == NULL);
    685   EXPECT_EQ(mirror::Class::kStatusInitialized, JavaLangObject->GetStatus());
    686   EXPECT_FALSE(JavaLangObject->IsErroneous());
    687   EXPECT_TRUE(JavaLangObject->IsLoaded());
    688   EXPECT_TRUE(JavaLangObject->IsResolved());
    689   EXPECT_TRUE(JavaLangObject->IsVerified());
    690   EXPECT_TRUE(JavaLangObject->IsInitialized());
    691   EXPECT_FALSE(JavaLangObject->IsArrayInstance());
    692   EXPECT_FALSE(JavaLangObject->IsArrayClass());
    693   EXPECT_TRUE(JavaLangObject->GetComponentType() == NULL);
    694   EXPECT_FALSE(JavaLangObject->IsInterface());
    695   EXPECT_TRUE(JavaLangObject->IsPublic());
    696   EXPECT_FALSE(JavaLangObject->IsFinal());
    697   EXPECT_FALSE(JavaLangObject->IsPrimitive());
    698   EXPECT_FALSE(JavaLangObject->IsSynthetic());
    699   EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
    700   EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
    701   EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
    702   FieldHelper fh(JavaLangObject->GetInstanceField(0));
    703   EXPECT_STREQ(fh.GetName(), "shadow$_klass_");
    704   fh.ChangeField(JavaLangObject->GetInstanceField(1));
    705   EXPECT_STREQ(fh.GetName(), "shadow$_monitor_");
    706 
    707   EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
    708   EXPECT_EQ(0U, kh.NumDirectInterfaces());
    709 
    710   SirtRef<mirror::ClassLoader> class_loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass")));
    711   AssertNonExistentClass("LMyClass;");
    712   mirror::Class* MyClass = class_linker_->FindClass("LMyClass;", class_loader.get());
    713   kh.ChangeClass(MyClass);
    714   ASSERT_TRUE(MyClass != NULL);
    715   ASSERT_TRUE(MyClass->GetClass() != NULL);
    716   ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
    717   EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
    718   ASSERT_STREQ(kh.GetDescriptor(), "LMyClass;");
    719   EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
    720   EXPECT_TRUE(MyClass->HasSuperClass());
    721   EXPECT_EQ(class_loader.get(), MyClass->GetClassLoader());
    722   EXPECT_EQ(mirror::Class::kStatusResolved, MyClass->GetStatus());
    723   EXPECT_FALSE(MyClass->IsErroneous());
    724   EXPECT_TRUE(MyClass->IsLoaded());
    725   EXPECT_TRUE(MyClass->IsResolved());
    726   EXPECT_FALSE(MyClass->IsVerified());
    727   EXPECT_FALSE(MyClass->IsInitialized());
    728   EXPECT_FALSE(MyClass->IsArrayInstance());
    729   EXPECT_FALSE(MyClass->IsArrayClass());
    730   EXPECT_TRUE(MyClass->GetComponentType() == NULL);
    731   EXPECT_FALSE(MyClass->IsInterface());
    732   EXPECT_FALSE(MyClass->IsPublic());
    733   EXPECT_FALSE(MyClass->IsFinal());
    734   EXPECT_FALSE(MyClass->IsPrimitive());
    735   EXPECT_FALSE(MyClass->IsSynthetic());
    736   EXPECT_EQ(1U, MyClass->NumDirectMethods());
    737   EXPECT_EQ(0U, MyClass->NumVirtualMethods());
    738   EXPECT_EQ(0U, MyClass->NumInstanceFields());
    739   EXPECT_EQ(0U, MyClass->NumStaticFields());
    740   EXPECT_EQ(0U, kh.NumDirectInterfaces());
    741 
    742   EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
    743 
    744   // created by class_linker
    745   AssertArrayClass("[C", "C", NULL);
    746   AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", NULL);
    747   // synthesized on the fly
    748   AssertArrayClass("[[C", "[C", NULL);
    749   AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.get());
    750   // or not available at all
    751   AssertNonExistentClass("[[[[LNonExistentClass;");
    752 }
    753 
    754 TEST_F(ClassLinkerTest, LibCore) {
    755   ScopedObjectAccess soa(Thread::Current());
    756   AssertDexFile(java_lang_dex_file_, NULL);
    757 }
    758 
    759 // The first reference array element must be a multiple of 4 bytes from the
    760 // start of the object
    761 TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
    762   ScopedObjectAccess soa(Thread::Current());
    763   mirror::Class* array_class = class_linker_->FindSystemClass("[Ljava/lang/String;");
    764   mirror::ObjectArray<mirror::String>* array =
    765       mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0);
    766   uint32_t array_offset = reinterpret_cast<uint32_t>(array);
    767   uint32_t data_offset =
    768       array_offset + mirror::ObjectArray<mirror::String>::DataOffset(sizeof(mirror::String*)).Uint32Value();
    769   if (sizeof(mirror::String*) == sizeof(int32_t)) {
    770     EXPECT_TRUE(IsAligned<4>(data_offset));  // Check 4 byte alignment.
    771   } else {
    772     EXPECT_TRUE(IsAligned<8>(data_offset));  // Check 8 byte alignment.
    773   }
    774 }
    775 
    776 TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
    777   ScopedObjectAccess soa(Thread::Current());
    778   SirtRef<mirror::LongArray> long_array(soa.Self(), mirror::LongArray::Alloc(soa.Self(), 0));
    779   EXPECT_EQ(class_linker_->FindSystemClass("[J"), long_array->GetClass());
    780   uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
    781   EXPECT_TRUE(IsAligned<8>(data_offset));  // Longs require 8 byte alignment
    782 
    783   SirtRef<mirror::DoubleArray> double_array(soa.Self(), mirror::DoubleArray::Alloc(soa.Self(), 0));
    784   EXPECT_EQ(class_linker_->FindSystemClass("[D"), double_array->GetClass());
    785   data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
    786   EXPECT_TRUE(IsAligned<8>(data_offset));  // Doubles require 8 byte alignment
    787 
    788   SirtRef<mirror::IntArray> int_array(soa.Self(), mirror::IntArray::Alloc(soa.Self(), 0));
    789   EXPECT_EQ(class_linker_->FindSystemClass("[I"), int_array->GetClass());
    790   data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
    791   EXPECT_TRUE(IsAligned<4>(data_offset));  // Ints require 4 byte alignment
    792 
    793   SirtRef<mirror::CharArray> char_array(soa.Self(), mirror::CharArray::Alloc(soa.Self(), 0));
    794   EXPECT_EQ(class_linker_->FindSystemClass("[C"), char_array->GetClass());
    795   data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
    796   EXPECT_TRUE(IsAligned<2>(data_offset));  // Chars require 2 byte alignment
    797 
    798   SirtRef<mirror::ShortArray> short_array(soa.Self(), mirror::ShortArray::Alloc(soa.Self(), 0));
    799   EXPECT_EQ(class_linker_->FindSystemClass("[S"), short_array->GetClass());
    800   data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
    801   EXPECT_TRUE(IsAligned<2>(data_offset));  // Shorts require 2 byte alignment
    802 
    803   // Take it as given that bytes and booleans have byte alignment
    804 }
    805 
    806 TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
    807   // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
    808   // This lets UnboxPrimitive avoid searching for the field by name at runtime.
    809   ScopedObjectAccess soa(Thread::Current());
    810   mirror::Class* c;
    811   c = class_linker_->FindClass("Ljava/lang/Boolean;", NULL);
    812   FieldHelper fh(c->GetIFields()->Get(0));
    813   EXPECT_STREQ("value", fh.GetName());
    814   c = class_linker_->FindClass("Ljava/lang/Byte;", NULL);
    815   fh.ChangeField(c->GetIFields()->Get(0));
    816   EXPECT_STREQ("value", fh.GetName());
    817   c = class_linker_->FindClass("Ljava/lang/Character;", NULL);
    818   fh.ChangeField(c->GetIFields()->Get(0));
    819   EXPECT_STREQ("value", fh.GetName());
    820   c = class_linker_->FindClass("Ljava/lang/Double;", NULL);
    821   fh.ChangeField(c->GetIFields()->Get(0));
    822   EXPECT_STREQ("value", fh.GetName());
    823   c = class_linker_->FindClass("Ljava/lang/Float;", NULL);
    824   fh.ChangeField(c->GetIFields()->Get(0));
    825   EXPECT_STREQ("value", fh.GetName());
    826   c = class_linker_->FindClass("Ljava/lang/Integer;", NULL);
    827   fh.ChangeField(c->GetIFields()->Get(0));
    828   EXPECT_STREQ("value", fh.GetName());
    829   c = class_linker_->FindClass("Ljava/lang/Long;", NULL);
    830   fh.ChangeField(c->GetIFields()->Get(0));
    831   EXPECT_STREQ("value", fh.GetName());
    832   c = class_linker_->FindClass("Ljava/lang/Short;", NULL);
    833   fh.ChangeField(c->GetIFields()->Get(0));
    834   EXPECT_STREQ("value", fh.GetName());
    835 }
    836 
    837 TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
    838   ScopedObjectAccess soa(Thread::Current());
    839   SirtRef<mirror::ClassLoader> class_loader_1(soa.Self(), soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass")));
    840   SirtRef<mirror::ClassLoader> class_loader_2(soa.Self(), soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass")));
    841   mirror::Class* MyClass_1 = class_linker_->FindClass("LMyClass;", class_loader_1.get());
    842   mirror::Class* MyClass_2 = class_linker_->FindClass("LMyClass;", class_loader_2.get());
    843   EXPECT_TRUE(MyClass_1 != NULL);
    844   EXPECT_TRUE(MyClass_2 != NULL);
    845   EXPECT_NE(MyClass_1, MyClass_2);
    846 }
    847 
    848 TEST_F(ClassLinkerTest, StaticFields) {
    849   ScopedObjectAccess soa(Thread::Current());
    850   SirtRef<mirror::ClassLoader> class_loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(LoadDex("Statics")));
    851   mirror::Class* statics = class_linker_->FindClass("LStatics;", class_loader.get());
    852   class_linker_->EnsureInitialized(statics, true, true);
    853 
    854   // Static final primitives that are initialized by a compile-time constant
    855   // expression resolve to a copy of a constant value from the constant pool.
    856   // So <clinit> should be null.
    857   mirror::ArtMethod* clinit = statics->FindDirectMethod("<clinit>", "()V");
    858   EXPECT_TRUE(clinit == NULL);
    859 
    860   EXPECT_EQ(9U, statics->NumStaticFields());
    861 
    862   mirror::ArtField* s0 = statics->FindStaticField("s0", "Z");
    863   FieldHelper fh(s0);
    864   EXPECT_STREQ(ClassHelper(s0->GetClass()).GetDescriptor(), "Ljava/lang/reflect/ArtField;");
    865   EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimBoolean);
    866   EXPECT_EQ(true, s0->GetBoolean(statics));
    867   s0->SetBoolean(statics, false);
    868 
    869   mirror::ArtField* s1 = statics->FindStaticField("s1", "B");
    870   fh.ChangeField(s1);
    871   EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimByte);
    872   EXPECT_EQ(5, s1->GetByte(statics));
    873   s1->SetByte(statics, 6);
    874 
    875   mirror::ArtField* s2 = statics->FindStaticField("s2", "C");
    876   fh.ChangeField(s2);
    877   EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimChar);
    878   EXPECT_EQ('a', s2->GetChar(statics));
    879   s2->SetChar(statics, 'b');
    880 
    881   mirror::ArtField* s3 = statics->FindStaticField("s3", "S");
    882   fh.ChangeField(s3);
    883   EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimShort);
    884   EXPECT_EQ(-536, s3->GetShort(statics));
    885   s3->SetShort(statics, -535);
    886 
    887   mirror::ArtField* s4 = statics->FindStaticField("s4", "I");
    888   fh.ChangeField(s4);
    889   EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimInt);
    890   EXPECT_EQ(2000000000, s4->GetInt(statics));
    891   s4->SetInt(statics, 2000000001);
    892 
    893   mirror::ArtField* s5 = statics->FindStaticField("s5", "J");
    894   fh.ChangeField(s5);
    895   EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimLong);
    896   EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics));
    897   s5->SetLong(statics, 0x34567890abcdef12LL);
    898 
    899   mirror::ArtField* s6 = statics->FindStaticField("s6", "F");
    900   fh.ChangeField(s6);
    901   EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimFloat);
    902   EXPECT_EQ(0.5, s6->GetFloat(statics));
    903   s6->SetFloat(statics, 0.75);
    904 
    905   mirror::ArtField* s7 = statics->FindStaticField("s7", "D");
    906   fh.ChangeField(s7);
    907   EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimDouble);
    908   EXPECT_EQ(16777217, s7->GetDouble(statics));
    909   s7->SetDouble(statics, 16777219);
    910 
    911   mirror::ArtField* s8 = statics->FindStaticField("s8", "Ljava/lang/String;");
    912   fh.ChangeField(s8);
    913   EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimNot);
    914   EXPECT_TRUE(s8->GetObject(statics)->AsString()->Equals("android"));
    915   s8->SetObject(s8->GetDeclaringClass(), mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot"));
    916 
    917   // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
    918   // http://code.google.com/p/googletest/issues/detail?id=322
    919   EXPECT_FALSE(s0->GetBoolean(statics));
    920   EXPECT_EQ(6, s1->GetByte(statics));
    921   EXPECT_EQ('b', s2->GetChar(statics));
    922   EXPECT_EQ(-535, s3->GetShort(statics));
    923   EXPECT_EQ(2000000001, s4->GetInt(statics));
    924   EXPECT_EQ(0x34567890abcdef12LL, s5->GetLong(statics));
    925   EXPECT_EQ(0.75, s6->GetFloat(statics));
    926   EXPECT_EQ(16777219, s7->GetDouble(statics));
    927   EXPECT_TRUE(s8->GetObject(statics)->AsString()->Equals("robot"));
    928 }
    929 
    930 TEST_F(ClassLinkerTest, Interfaces) {
    931   ScopedObjectAccess soa(Thread::Current());
    932   SirtRef<mirror::ClassLoader> class_loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(LoadDex("Interfaces")));
    933   mirror::Class* I = class_linker_->FindClass("LInterfaces$I;", class_loader.get());
    934   mirror::Class* J = class_linker_->FindClass("LInterfaces$J;", class_loader.get());
    935   mirror::Class* K = class_linker_->FindClass("LInterfaces$K;", class_loader.get());
    936   mirror::Class* A = class_linker_->FindClass("LInterfaces$A;", class_loader.get());
    937   mirror::Class* B = class_linker_->FindClass("LInterfaces$B;", class_loader.get());
    938   EXPECT_TRUE(I->IsAssignableFrom(A));
    939   EXPECT_TRUE(J->IsAssignableFrom(A));
    940   EXPECT_TRUE(J->IsAssignableFrom(K));
    941   EXPECT_TRUE(K->IsAssignableFrom(B));
    942   EXPECT_TRUE(J->IsAssignableFrom(B));
    943 
    944   mirror::ArtMethod* Ii = I->FindVirtualMethod("i", "()V");
    945   mirror::ArtMethod* Jj1 = J->FindVirtualMethod("j1", "()V");
    946   mirror::ArtMethod* Jj2 = J->FindVirtualMethod("j2", "()V");
    947   mirror::ArtMethod* Kj1 = K->FindInterfaceMethod("j1", "()V");
    948   mirror::ArtMethod* Kj2 = K->FindInterfaceMethod("j2", "()V");
    949   mirror::ArtMethod* Kk = K->FindInterfaceMethod("k", "()V");
    950   mirror::ArtMethod* Ai = A->FindVirtualMethod("i", "()V");
    951   mirror::ArtMethod* Aj1 = A->FindVirtualMethod("j1", "()V");
    952   mirror::ArtMethod* Aj2 = A->FindVirtualMethod("j2", "()V");
    953   ASSERT_TRUE(Ii != NULL);
    954   ASSERT_TRUE(Jj1 != NULL);
    955   ASSERT_TRUE(Jj2 != NULL);
    956   ASSERT_TRUE(Kj1 != NULL);
    957   ASSERT_TRUE(Kj2 != NULL);
    958   ASSERT_TRUE(Kk != NULL);
    959   ASSERT_TRUE(Ai != NULL);
    960   ASSERT_TRUE(Aj1 != NULL);
    961   ASSERT_TRUE(Aj2 != NULL);
    962   EXPECT_NE(Ii, Ai);
    963   EXPECT_NE(Jj1, Aj1);
    964   EXPECT_NE(Jj2, Aj2);
    965   EXPECT_EQ(Kj1, Jj1);
    966   EXPECT_EQ(Kj2, Jj2);
    967   EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii));
    968   EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1));
    969   EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2));
    970   EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii));
    971   EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1));
    972   EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2));
    973 
    974   mirror::ArtField* Afoo = A->FindStaticField("foo", "Ljava/lang/String;");
    975   mirror::ArtField* Bfoo = B->FindStaticField("foo", "Ljava/lang/String;");
    976   mirror::ArtField* Jfoo = J->FindStaticField("foo", "Ljava/lang/String;");
    977   mirror::ArtField* Kfoo = K->FindStaticField("foo", "Ljava/lang/String;");
    978   ASSERT_TRUE(Afoo != NULL);
    979   EXPECT_EQ(Afoo, Bfoo);
    980   EXPECT_EQ(Afoo, Jfoo);
    981   EXPECT_EQ(Afoo, Kfoo);
    982 }
    983 
    984 TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
    985   // pretend we are trying to get the static storage for the StaticsFromCode class.
    986 
    987   // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
    988   // case 2, get the initialized storage from StaticsFromCode.getS0
    989 
    990   ScopedObjectAccess soa(Thread::Current());
    991   jobject jclass_loader = LoadDex("StaticsFromCode");
    992   SirtRef<mirror::ClassLoader> class_loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(jclass_loader));
    993   const DexFile* dex_file = Runtime::Current()->GetCompileTimeClassPath(jclass_loader)[0];
    994   CHECK(dex_file != NULL);
    995 
    996   mirror::Class* klass = class_linker_->FindClass("LStaticsFromCode;", class_loader.get());
    997   mirror::ArtMethod* clinit = klass->FindDirectMethod("<clinit>", "()V");
    998   mirror::ArtMethod* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;");
    999   const DexFile::StringId* string_id = dex_file->FindStringId("LStaticsFromCode;");
   1000   ASSERT_TRUE(string_id != NULL);
   1001   const DexFile::TypeId* type_id = dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id));
   1002   ASSERT_TRUE(type_id != NULL);
   1003   uint32_t type_idx = dex_file->GetIndexForTypeId(*type_id);
   1004   EXPECT_TRUE(clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx) == NULL);
   1005   mirror::StaticStorageBase* uninit = ResolveVerifyAndClinit(type_idx, clinit, Thread::Current(), true, false);
   1006   EXPECT_TRUE(uninit != NULL);
   1007   EXPECT_TRUE(clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx) == NULL);
   1008   mirror::StaticStorageBase* init = ResolveVerifyAndClinit(type_idx, getS0, Thread::Current(), true, false);
   1009   EXPECT_TRUE(init != NULL);
   1010   EXPECT_EQ(init, clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx));
   1011 }
   1012 
   1013 TEST_F(ClassLinkerTest, FinalizableBit) {
   1014   ScopedObjectAccess soa(Thread::Current());
   1015   mirror::Class* c;
   1016 
   1017   // Object has a finalize method, but we know it's empty.
   1018   c = class_linker_->FindSystemClass("Ljava/lang/Object;");
   1019   EXPECT_FALSE(c->IsFinalizable());
   1020 
   1021   // Enum has a finalize method to prevent its subclasses from implementing one.
   1022   c = class_linker_->FindSystemClass("Ljava/lang/Enum;");
   1023   EXPECT_FALSE(c->IsFinalizable());
   1024 
   1025   // RoundingMode is an enum.
   1026   c = class_linker_->FindSystemClass("Ljava/math/RoundingMode;");
   1027   EXPECT_FALSE(c->IsFinalizable());
   1028 
   1029   // RandomAccessFile extends Object and overrides finalize.
   1030   c = class_linker_->FindSystemClass("Ljava/io/RandomAccessFile;");
   1031   EXPECT_TRUE(c->IsFinalizable());
   1032 
   1033   // FileInputStream is finalizable and extends InputStream which isn't.
   1034   c = class_linker_->FindSystemClass("Ljava/io/InputStream;");
   1035   EXPECT_FALSE(c->IsFinalizable());
   1036   c = class_linker_->FindSystemClass("Ljava/io/FileInputStream;");
   1037   EXPECT_TRUE(c->IsFinalizable());
   1038 
   1039   // ScheduledThreadPoolExecutor doesn't have a finalize method but
   1040   // extends ThreadPoolExecutor which does.
   1041   c = class_linker_->FindSystemClass("Ljava/util/concurrent/ThreadPoolExecutor;");
   1042   EXPECT_TRUE(c->IsFinalizable());
   1043   c = class_linker_->FindSystemClass("Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
   1044   EXPECT_TRUE(c->IsFinalizable());
   1045 }
   1046 
   1047 TEST_F(ClassLinkerTest, ClassRootDescriptors) {
   1048   ScopedObjectAccess soa(Thread::Current());
   1049   ClassHelper kh;
   1050   for (int i = 0; i < ClassLinker::kClassRootsMax; i++) {
   1051     mirror::Class* klass = class_linker_->GetClassRoot(ClassLinker::ClassRoot(i));
   1052     kh.ChangeClass(klass);
   1053     EXPECT_TRUE(kh.GetDescriptor() != NULL);
   1054     EXPECT_STREQ(kh.GetDescriptor(),
   1055                  class_linker_->GetClassRootDescriptor(ClassLinker::ClassRoot(i))) << " i = " << i;
   1056   }
   1057 }
   1058 
   1059 }  // namespace art
   1060