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 
22 #include "art_field-inl.h"
23 #include "art_method-inl.h"
24 #include "class_linker-inl.h"
25 #include "common_runtime_test.h"
26 #include "dex_file.h"
27 #include "entrypoints/entrypoint_utils-inl.h"
28 #include "gc/heap.h"
29 #include "mirror/abstract_method.h"
30 #include "mirror/accessible_object.h"
31 #include "mirror/class-inl.h"
32 #include "mirror/dex_cache.h"
33 #include "mirror/field.h"
34 #include "mirror/object-inl.h"
35 #include "mirror/object_array-inl.h"
36 #include "mirror/proxy.h"
37 #include "mirror/reference.h"
38 #include "mirror/stack_trace_element.h"
39 #include "mirror/string-inl.h"
40 #include "handle_scope-inl.h"
41 #include "scoped_thread_state_change.h"
42 #include "thread-inl.h"
43 
44 namespace art {
45 
46 class ClassLinkerTest : public CommonRuntimeTest {
47  protected:
AssertNonExistentClass(const std::string & descriptor)48   void AssertNonExistentClass(const std::string& descriptor)
49       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
50     Thread* self = Thread::Current();
51     EXPECT_TRUE(class_linker_->FindSystemClass(self, descriptor.c_str()) == nullptr);
52     EXPECT_TRUE(self->IsExceptionPending());
53     mirror::Object* exception = self->GetException();
54     self->ClearException();
55     mirror::Class* exception_class =
56         class_linker_->FindSystemClass(self, "Ljava/lang/NoClassDefFoundError;");
57     EXPECT_TRUE(exception->InstanceOf(exception_class));
58   }
59 
AssertPrimitiveClass(const std::string & descriptor)60   void AssertPrimitiveClass(const std::string& descriptor)
61       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
62     Thread* self = Thread::Current();
63     AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(self, descriptor.c_str()));
64   }
65 
AssertPrimitiveClass(const std::string & descriptor,mirror::Class * primitive)66   void AssertPrimitiveClass(const std::string& descriptor, mirror::Class* primitive)
67       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
68     ASSERT_TRUE(primitive != nullptr);
69     ASSERT_TRUE(primitive->GetClass() != nullptr);
70     ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
71     EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != nullptr);
72     std::string temp;
73     ASSERT_STREQ(descriptor.c_str(), primitive->GetDescriptor(&temp));
74     EXPECT_TRUE(primitive->GetSuperClass() == nullptr);
75     EXPECT_FALSE(primitive->HasSuperClass());
76     EXPECT_TRUE(primitive->GetClassLoader() == nullptr);
77     EXPECT_EQ(mirror::Class::kStatusInitialized, primitive->GetStatus());
78     EXPECT_FALSE(primitive->IsErroneous());
79     EXPECT_TRUE(primitive->IsLoaded());
80     EXPECT_TRUE(primitive->IsResolved());
81     EXPECT_TRUE(primitive->IsVerified());
82     EXPECT_TRUE(primitive->IsInitialized());
83     EXPECT_FALSE(primitive->IsArrayInstance());
84     EXPECT_FALSE(primitive->IsArrayClass());
85     EXPECT_TRUE(primitive->GetComponentType() == nullptr);
86     EXPECT_FALSE(primitive->IsInterface());
87     EXPECT_TRUE(primitive->IsPublic());
88     EXPECT_TRUE(primitive->IsFinal());
89     EXPECT_TRUE(primitive->IsPrimitive());
90     EXPECT_FALSE(primitive->IsSynthetic());
91     EXPECT_EQ(0U, primitive->NumDirectMethods());
92     EXPECT_EQ(0U, primitive->NumVirtualMethods());
93     EXPECT_EQ(0U, primitive->NumInstanceFields());
94     EXPECT_EQ(0U, primitive->NumStaticFields());
95     EXPECT_EQ(0U, primitive->NumDirectInterfaces());
96     EXPECT_FALSE(primitive->HasVTable());
97     EXPECT_EQ(0, primitive->GetIfTableCount());
98     EXPECT_TRUE(primitive->GetIfTable() == nullptr);
99     EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract, primitive->GetAccessFlags());
100   }
101 
AssertArrayClass(const std::string & array_descriptor,const std::string & component_type,mirror::ClassLoader * class_loader)102   void AssertArrayClass(const std::string& array_descriptor,
103                         const std::string& component_type,
104                         mirror::ClassLoader* class_loader)
105       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
106     Thread* self = Thread::Current();
107     StackHandleScope<2> hs(self);
108     Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
109     Handle<mirror::Class> array(
110         hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader)));
111     std::string temp;
112     EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp));
113     EXPECT_EQ(class_loader, array->GetClassLoader());
114     EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract)));
115     AssertArrayClass(array_descriptor, array);
116   }
117 
AssertArrayClass(const std::string & array_descriptor,Handle<mirror::Class> array)118   void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array)
119       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
120     ASSERT_TRUE(array.Get() != nullptr);
121     ASSERT_TRUE(array->GetClass() != nullptr);
122     ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
123     EXPECT_TRUE(array->GetClass()->GetSuperClass() != nullptr);
124     std::string temp;
125     ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp));
126     EXPECT_TRUE(array->GetSuperClass() != nullptr);
127     Thread* self = Thread::Current();
128     EXPECT_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"), array->GetSuperClass());
129     EXPECT_TRUE(array->HasSuperClass());
130     ASSERT_TRUE(array->GetComponentType() != nullptr);
131     ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U);
132     EXPECT_EQ(mirror::Class::kStatusInitialized, array->GetStatus());
133     EXPECT_FALSE(array->IsErroneous());
134     EXPECT_TRUE(array->IsLoaded());
135     EXPECT_TRUE(array->IsResolved());
136     EXPECT_TRUE(array->IsVerified());
137     EXPECT_TRUE(array->IsInitialized());
138     EXPECT_FALSE(array->IsArrayInstance());
139     EXPECT_TRUE(array->IsArrayClass());
140     EXPECT_FALSE(array->IsInterface());
141     EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
142     EXPECT_TRUE(array->IsFinal());
143     EXPECT_FALSE(array->IsPrimitive());
144     EXPECT_FALSE(array->IsSynthetic());
145     EXPECT_EQ(0U, array->NumDirectMethods());
146     EXPECT_EQ(0U, array->NumVirtualMethods());
147     EXPECT_EQ(0U, array->NumInstanceFields());
148     EXPECT_EQ(0U, array->NumStaticFields());
149     EXPECT_EQ(2U, array->NumDirectInterfaces());
150     EXPECT_TRUE(array->ShouldHaveEmbeddedImtAndVTable());
151     EXPECT_EQ(2, array->GetIfTableCount());
152     ASSERT_TRUE(array->GetIfTable() != nullptr);
153     mirror::Class* direct_interface0 = mirror::Class::GetDirectInterface(self, array, 0);
154     EXPECT_TRUE(direct_interface0 != nullptr);
155     EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;");
156     mirror::Class* direct_interface1 = mirror::Class::GetDirectInterface(self, array, 1);
157     EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;");
158     mirror::Class* array_ptr = array->GetComponentType();
159     EXPECT_EQ(class_linker_->FindArrayClass(self, &array_ptr), array.Get());
160   }
161 
AssertMethod(ArtMethod * method)162   void AssertMethod(ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
163     EXPECT_TRUE(method != nullptr);
164     EXPECT_TRUE(method->GetDeclaringClass() != nullptr);
165     EXPECT_TRUE(method->GetName() != nullptr);
166     EXPECT_TRUE(method->GetSignature() != Signature::NoSignature());
167 
168     EXPECT_TRUE(method->HasDexCacheResolvedMethods());
169     EXPECT_TRUE(method->HasDexCacheResolvedTypes());
170     EXPECT_TRUE(method->HasSameDexCacheResolvedMethods(
171         method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods()));
172     EXPECT_TRUE(method->HasSameDexCacheResolvedTypes(
173         method->GetDeclaringClass()->GetDexCache()->GetResolvedTypes()));
174   }
175 
AssertField(mirror::Class * klass,ArtField * field)176   void AssertField(mirror::Class* klass, ArtField* field)
177       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
178     EXPECT_TRUE(field != nullptr);
179     EXPECT_EQ(klass, field->GetDeclaringClass());
180     EXPECT_TRUE(field->GetName() != nullptr);
181     EXPECT_TRUE(field->GetType<true>() != nullptr);
182   }
183 
AssertClass(const std::string & descriptor,Handle<mirror::Class> klass)184   void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass)
185       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
186     std::string temp;
187     EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
188     if (descriptor == "Ljava/lang/Object;") {
189       EXPECT_FALSE(klass->HasSuperClass());
190     } else {
191       EXPECT_TRUE(klass->HasSuperClass());
192       EXPECT_TRUE(klass->GetSuperClass() != nullptr);
193     }
194     EXPECT_TRUE(klass->GetClass() != nullptr);
195     EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
196     EXPECT_TRUE(klass->GetDexCache() != nullptr);
197     EXPECT_TRUE(klass->IsLoaded());
198     EXPECT_TRUE(klass->IsResolved());
199     EXPECT_FALSE(klass->IsErroneous());
200     EXPECT_FALSE(klass->IsArrayClass());
201     EXPECT_TRUE(klass->GetComponentType() == nullptr);
202     EXPECT_TRUE(klass->IsInSamePackage(klass.Get()));
203     EXPECT_TRUE(klass->GetDexCacheStrings() != nullptr);
204     EXPECT_EQ(klass->GetDexCacheStrings(), klass->GetDexCache()->GetStrings());
205     std::string temp2;
206     EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp),
207                                                klass->GetDescriptor(&temp2)));
208     if (klass->IsInterface()) {
209       EXPECT_TRUE(klass->IsAbstract());
210       if (klass->NumDirectMethods() == 1) {
211         EXPECT_TRUE(klass->GetDirectMethod(0, sizeof(void*))->IsClassInitializer());
212         EXPECT_TRUE(klass->GetDirectMethod(0, sizeof(void*))->IsDirect());
213       } else {
214         EXPECT_EQ(0U, klass->NumDirectMethods());
215       }
216     } else {
217       if (!klass->IsSynthetic()) {
218         EXPECT_NE(0U, klass->NumDirectMethods());
219       }
220     }
221     EXPECT_EQ(klass->IsInterface(), !klass->HasVTable());
222     mirror::IfTable* iftable = klass->GetIfTable();
223     for (int i = 0; i < klass->GetIfTableCount(); i++) {
224       mirror::Class* interface = iftable->GetInterface(i);
225       ASSERT_TRUE(interface != nullptr);
226       if (klass->IsInterface()) {
227         EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
228       } else {
229         EXPECT_EQ(interface->NumVirtualMethods(), iftable->GetMethodArrayCount(i));
230       }
231     }
232     if (klass->IsAbstract()) {
233       EXPECT_FALSE(klass->IsFinal());
234     } else {
235       EXPECT_FALSE(klass->IsAnnotation());
236     }
237     if (klass->IsFinal()) {
238       EXPECT_FALSE(klass->IsAbstract());
239       EXPECT_FALSE(klass->IsAnnotation());
240     }
241     if (klass->IsAnnotation()) {
242       EXPECT_FALSE(klass->IsFinal());
243       EXPECT_TRUE(klass->IsAbstract());
244     }
245 
246     EXPECT_FALSE(klass->IsPrimitive());
247     EXPECT_TRUE(klass->CanAccess(klass.Get()));
248 
249     for (ArtMethod& method : klass->GetDirectMethods(sizeof(void*))) {
250       AssertMethod(&method);
251       EXPECT_TRUE(method.IsDirect());
252       EXPECT_EQ(klass.Get(), method.GetDeclaringClass());
253     }
254 
255     for (ArtMethod& method : klass->GetVirtualMethods(sizeof(void*))) {
256       AssertMethod(&method);
257       EXPECT_FALSE(method.IsDirect());
258       EXPECT_TRUE(method.GetDeclaringClass()->IsAssignableFrom(klass.Get()));
259     }
260 
261     for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
262       ArtField* field = klass->GetInstanceField(i);
263       AssertField(klass.Get(), field);
264       EXPECT_FALSE(field->IsStatic());
265     }
266 
267     for (size_t i = 0; i < klass->NumStaticFields(); i++) {
268       ArtField* field = klass->GetStaticField(i);
269       AssertField(klass.Get(), field);
270       EXPECT_TRUE(field->IsStatic());
271     }
272 
273     // Confirm that all instances field offsets are packed together at the start.
274     EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
275     MemberOffset start_ref_offset = klass->GetFirstReferenceInstanceFieldOffset();
276     MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
277                                 klass->NumReferenceInstanceFields() *
278                                     sizeof(mirror::HeapReference<mirror::Object>));
279     MemberOffset current_ref_offset = start_ref_offset;
280     for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
281       ArtField* field = klass->GetInstanceField(i);
282       mirror::Class* field_type = field->GetType<true>();
283       ASSERT_TRUE(field_type != nullptr);
284       if (!field->IsPrimitiveType()) {
285         ASSERT_TRUE(!field_type->IsPrimitive());
286         ASSERT_EQ(current_ref_offset.Uint32Value(), field->GetOffset().Uint32Value());
287         if (current_ref_offset.Uint32Value() == end_ref_offset.Uint32Value()) {
288           // While Reference.referent is not primitive, the ClassLinker
289           // treats it as such so that the garbage collector won't scan it.
290           EXPECT_EQ(PrettyField(field),
291                     "java.lang.Object java.lang.ref.Reference.referent");
292         } else {
293           current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
294                                             sizeof(mirror::HeapReference<mirror::Object>));
295         }
296       } else {
297         if (field->GetOffset().Uint32Value() < end_ref_offset.Uint32Value()) {
298           // Shuffled before references.
299           ASSERT_LT(field->GetOffset().Uint32Value(), start_ref_offset.Uint32Value());
300           CHECK(!IsAligned<4>(field->GetOffset().Uint32Value()));
301         }
302       }
303     }
304     ASSERT_EQ(end_ref_offset.Uint32Value(), current_ref_offset.Uint32Value());
305 
306     uint32_t total_num_reference_instance_fields = 0;
307     mirror::Class* k = klass.Get();
308     while (k != nullptr) {
309       total_num_reference_instance_fields += k->NumReferenceInstanceFields();
310       k = k->GetSuperClass();
311     }
312     EXPECT_GE(total_num_reference_instance_fields, 1U);  // Should always have Object's class.
313     if (klass->GetReferenceInstanceOffsets() != mirror::Class::kClassWalkSuper) {
314       // The reference instance offsets have a bit set for each reference offset.
315       // +1 for Object's class.
316       EXPECT_EQ(static_cast<uint32_t>(POPCOUNT(klass->GetReferenceInstanceOffsets())) + 1,
317                 total_num_reference_instance_fields);
318     }
319   }
320 
AssertDexFileClass(mirror::ClassLoader * class_loader,const std::string & descriptor)321   void AssertDexFileClass(mirror::ClassLoader* class_loader, const std::string& descriptor)
322       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
323     ASSERT_TRUE(descriptor != nullptr);
324     Thread* self = Thread::Current();
325     StackHandleScope<1> hs(self);
326     Handle<mirror::Class> klass(
327         hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str())));
328     ASSERT_TRUE(klass.Get() != nullptr);
329     std::string temp;
330     EXPECT_STREQ(descriptor.c_str(), klass.Get()->GetDescriptor(&temp));
331     EXPECT_EQ(class_loader, klass->GetClassLoader());
332     if (klass->IsPrimitive()) {
333       AssertPrimitiveClass(descriptor, klass.Get());
334     } else if (klass->IsArrayClass()) {
335       AssertArrayClass(descriptor, klass);
336     } else {
337       AssertClass(descriptor, klass);
338     }
339   }
340 
AssertDexFile(const DexFile & dex,mirror::ClassLoader * class_loader)341   void AssertDexFile(const DexFile& dex, mirror::ClassLoader* class_loader)
342       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
343     // Verify all the classes defined in this file
344     for (size_t i = 0; i < dex.NumClassDefs(); i++) {
345       const DexFile::ClassDef& class_def = dex.GetClassDef(i);
346       const char* descriptor = dex.GetClassDescriptor(class_def);
347       AssertDexFileClass(class_loader, descriptor);
348     }
349     // Verify all the types referenced by this file
350     for (size_t i = 0; i < dex.NumTypeIds(); i++) {
351       const DexFile::TypeId& type_id = dex.GetTypeId(i);
352       const char* descriptor = dex.GetTypeDescriptor(type_id);
353       AssertDexFileClass(class_loader, descriptor);
354     }
355     TestRootVisitor visitor;
356     class_linker_->VisitRoots(&visitor, kVisitRootFlagAllRoots);
357     // Verify the dex cache has resolution methods in all resolved method slots
358     mirror::DexCache* dex_cache = class_linker_->FindDexCache(dex);
359     auto* resolved_methods = dex_cache->GetResolvedMethods();
360     for (size_t i = 0; i < static_cast<size_t>(resolved_methods->GetLength()); i++) {
361       EXPECT_TRUE(resolved_methods->GetElementPtrSize<ArtMethod*>(i, sizeof(void*)) != nullptr)
362           << dex.GetLocation() << " i=" << i;
363     }
364   }
365 
366   class TestRootVisitor : public SingleRootVisitor {
367    public:
VisitRoot(mirror::Object * root,const RootInfo & info ATTRIBUTE_UNUSED)368     void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) OVERRIDE {
369       EXPECT_TRUE(root != nullptr);
370     }
371   };
372 };
373 
374 struct CheckOffset {
375   size_t cpp_offset;
376   const char* java_name;
CheckOffsetart::CheckOffset377   CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
378 };
379 
380 template <typename T>
381 struct CheckOffsets {
CheckOffsetsart::CheckOffsets382   CheckOffsets(bool is_static_in, const char* class_descriptor_in)
383       : is_static(is_static_in), class_descriptor(class_descriptor_in) {}
384   bool is_static;
385   std::string class_descriptor;
386   std::vector<CheckOffset> offsets;
387 
Checkart::CheckOffsets388   bool Check() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
389     Thread* self = Thread::Current();
390     mirror::Class* klass =
391         Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str());
392     CHECK(klass != nullptr) << class_descriptor;
393 
394     bool error = false;
395 
396     // Classes have a different size due to padding field. Strings are variable length.
397     if (!klass->IsClassClass() && !klass->IsStringClass() && !is_static) {
398       // Currently only required for AccessibleObject since of the padding fields. The class linker
399       // says AccessibleObject is 9 bytes but sizeof(AccessibleObject) is 12 bytes due to padding.
400       // The RoundUp is to get around this case.
401       static constexpr size_t kPackAlignment = 4;
402       size_t expected_size = RoundUp(is_static ? klass->GetClassSize(): klass->GetObjectSize(),
403           kPackAlignment);
404       if (sizeof(T) != expected_size) {
405         LOG(ERROR) << "Class size mismatch:"
406            << " class=" << class_descriptor
407            << " Java=" << expected_size
408            << " C++=" << sizeof(T);
409         error = true;
410       }
411     }
412 
413     size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
414     if (offsets.size() != num_fields) {
415       LOG(ERROR) << "Field count mismatch:"
416          << " class=" << class_descriptor
417          << " Java=" << num_fields
418          << " C++=" << offsets.size();
419       error = true;
420     }
421 
422     for (size_t i = 0; i < offsets.size(); i++) {
423       ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
424       StringPiece field_name(field->GetName());
425       if (field_name != offsets[i].java_name) {
426         error = true;
427       }
428     }
429     if (error) {
430       for (size_t i = 0; i < offsets.size(); i++) {
431         CheckOffset& offset = offsets[i];
432         ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
433         StringPiece field_name(field->GetName());
434         if (field_name != offsets[i].java_name) {
435           LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
436         }
437         LOG(ERROR) << "Java field order:"
438            << " i=" << i << " class=" << class_descriptor
439            << " Java=" << field_name
440            << " CheckOffsets=" << offset.java_name;
441       }
442     }
443 
444     for (size_t i = 0; i < offsets.size(); i++) {
445       CheckOffset& offset = offsets[i];
446       ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
447       if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
448         error = true;
449       }
450     }
451     if (error) {
452       for (size_t i = 0; i < offsets.size(); i++) {
453         CheckOffset& offset = offsets[i];
454         ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
455         if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
456           LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
457         }
458         LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
459            << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
460       }
461     }
462 
463     return !error;
464   };
465 
addOffsetart::CheckOffsets466   void addOffset(size_t offset, const char* name) {
467     offsets.push_back(CheckOffset(offset, name));
468   }
469 
470  private:
471   DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
472 };
473 
474 // Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
475 // is first since if it is failing, others are unlikely to succeed.
476 
477 struct ObjectOffsets : public CheckOffsets<mirror::Object> {
ObjectOffsetsart::ObjectOffsets478   ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
479     addOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_");
480     addOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_");
481 #ifdef USE_BROOKS_READ_BARRIER
482     addOffset(OFFSETOF_MEMBER(mirror::Object, x_rb_ptr_), "shadow$_x_rb_ptr_");
483     addOffset(OFFSETOF_MEMBER(mirror::Object, x_xpadding_), "shadow$_x_xpadding_");
484 #endif
485   };
486 };
487 
488 struct ClassOffsets : public CheckOffsets<mirror::Class> {
ClassOffsetsart::ClassOffsets489   ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
490     addOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags");
491     addOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader");
492     addOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize");
493     addOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId");
494     addOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType");
495     addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache");
496     addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_strings_), "dexCacheStrings");
497     addOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex");
498     addOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex");
499     addOffset(OFFSETOF_MEMBER(mirror::Class, direct_methods_), "directMethods");
500     addOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields");
501     addOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable");
502     addOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name");
503     addOffset(OFFSETOF_MEMBER(mirror::Class, num_direct_methods_), "numDirectMethods");
504     addOffset(OFFSETOF_MEMBER(mirror::Class, num_instance_fields_), "numInstanceFields");
505     addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_),
506               "numReferenceInstanceFields");
507     addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_),
508               "numReferenceStaticFields");
509     addOffset(OFFSETOF_MEMBER(mirror::Class, num_static_fields_), "numStaticFields");
510     addOffset(OFFSETOF_MEMBER(mirror::Class, num_virtual_methods_), "numVirtualMethods");
511     addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize");
512     addOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType");
513     addOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_),
514               "referenceInstanceOffsets");
515     addOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields");
516     addOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status");
517     addOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass");
518     addOffset(OFFSETOF_MEMBER(mirror::Class, verify_error_class_), "verifyErrorClass");
519     addOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_), "virtualMethods");
520     addOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable");
521   };
522 };
523 
524 struct StringOffsets : public CheckOffsets<mirror::String> {
StringOffsetsart::StringOffsets525   StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
526     addOffset(OFFSETOF_MEMBER(mirror::String, count_), "count");
527     addOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hashCode");
528   };
529 };
530 
531 struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
ThrowableOffsetsart::ThrowableOffsets532   ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
533     addOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause");
534     addOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage");
535     addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_state_), "stackState");
536     addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace");
537     addOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions");
538   };
539 };
540 
541 struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
StackTraceElementOffsetsart::StackTraceElementOffsets542   StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(
543       false, "Ljava/lang/StackTraceElement;") {
544     addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass");
545     addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName");
546     addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber");
547     addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName");
548   };
549 };
550 
551 struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
ClassLoaderOffsetsart::ClassLoaderOffsets552   ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
553     addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages");
554     addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent");
555     addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache");
556   };
557 };
558 
559 struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
ProxyOffsetsart::ProxyOffsets560   ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
561     addOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h");
562   };
563 };
564 
565 struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
DexCacheOffsetsart::DexCacheOffsets566   DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
567     addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_), "dex");
568     addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile");
569     addOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location");
570     addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields");
571     addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods");
572     addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes");
573     addOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings");
574   };
575 };
576 
577 struct ReferenceOffsets : public CheckOffsets<mirror::Reference> {
ReferenceOffsetsart::ReferenceOffsets578   ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") {
579     addOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext");
580     addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue");
581     addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext");
582     addOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent");
583   };
584 };
585 
586 struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> {
FinalizerReferenceOffsetsart::FinalizerReferenceOffsets587   FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>(
588       false, "Ljava/lang/ref/FinalizerReference;") {
589     addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next");
590     addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev");
591     addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie");
592   };
593 };
594 
595 struct AccessibleObjectOffsets : public CheckOffsets<mirror::AccessibleObject> {
AccessibleObjectOffsetsart::AccessibleObjectOffsets596   AccessibleObjectOffsets() : CheckOffsets<mirror::AccessibleObject>(
597       false, "Ljava/lang/reflect/AccessibleObject;") {
598     addOffset(mirror::AccessibleObject::FlagOffset().Uint32Value(), "flag");
599   };
600 };
601 
602 struct FieldOffsets : public CheckOffsets<mirror::Field> {
FieldOffsetsart::FieldOffsets603   FieldOffsets() : CheckOffsets<mirror::Field>(false, "Ljava/lang/reflect/Field;") {
604     addOffset(OFFSETOF_MEMBER(mirror::Field, access_flags_), "accessFlags");
605     addOffset(OFFSETOF_MEMBER(mirror::Field, declaring_class_), "declaringClass");
606     addOffset(OFFSETOF_MEMBER(mirror::Field, dex_field_index_), "dexFieldIndex");
607     addOffset(OFFSETOF_MEMBER(mirror::Field, offset_), "offset");
608     addOffset(OFFSETOF_MEMBER(mirror::Field, type_), "type");
609   };
610 };
611 
612 struct AbstractMethodOffsets : public CheckOffsets<mirror::AbstractMethod> {
AbstractMethodOffsetsart::AbstractMethodOffsets613   AbstractMethodOffsets() : CheckOffsets<mirror::AbstractMethod>(
614       false, "Ljava/lang/reflect/AbstractMethod;") {
615     addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, access_flags_), "accessFlags");
616     addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, art_method_), "artMethod");
617     addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, declaring_class_), "declaringClass");
618     addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, declaring_class_of_overridden_method_),
619               "declaringClassOfOverriddenMethod");
620     addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, dex_method_index_), "dexMethodIndex");
621   };
622 };
623 
624 // C++ fields must exactly match the fields in the Java classes. If this fails,
625 // reorder the fields in the C++ class. Managed class fields are ordered by
626 // ClassLinker::LinkFields.
TEST_F(ClassLinkerTest,ValidateFieldOrderOfJavaCppUnionClasses)627 TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
628   ScopedObjectAccess soa(Thread::Current());
629   EXPECT_TRUE(ObjectOffsets().Check());
630   EXPECT_TRUE(ClassOffsets().Check());
631   EXPECT_TRUE(StringOffsets().Check());
632   EXPECT_TRUE(ThrowableOffsets().Check());
633   EXPECT_TRUE(StackTraceElementOffsets().Check());
634   EXPECT_TRUE(ClassLoaderOffsets().Check());
635   EXPECT_TRUE(ProxyOffsets().Check());
636   EXPECT_TRUE(DexCacheOffsets().Check());
637   EXPECT_TRUE(ReferenceOffsets().Check());
638   EXPECT_TRUE(FinalizerReferenceOffsets().Check());
639   EXPECT_TRUE(AccessibleObjectOffsets().Check());
640   EXPECT_TRUE(FieldOffsets().Check());
641   EXPECT_TRUE(AbstractMethodOffsets().Check());
642 }
643 
TEST_F(ClassLinkerTest,FindClassNonexistent)644 TEST_F(ClassLinkerTest, FindClassNonexistent) {
645   ScopedObjectAccess soa(Thread::Current());
646   AssertNonExistentClass("NoSuchClass;");
647   AssertNonExistentClass("LNoSuchClass;");
648 }
649 
TEST_F(ClassLinkerTest,GetDexFiles)650 TEST_F(ClassLinkerTest, GetDexFiles) {
651   ScopedObjectAccess soa(Thread::Current());
652 
653   jobject jclass_loader = LoadDex("Nested");
654   std::vector<const DexFile*> dex_files(GetDexFiles(jclass_loader));
655   ASSERT_EQ(dex_files.size(), 1U);
656   EXPECT_TRUE(EndsWith(dex_files[0]->GetLocation(), "Nested.jar"));
657 
658   jobject jclass_loader2 = LoadDex("MultiDex");
659   std::vector<const DexFile*> dex_files2(GetDexFiles(jclass_loader2));
660   ASSERT_EQ(dex_files2.size(), 2U);
661   EXPECT_TRUE(EndsWith(dex_files2[0]->GetLocation(), "MultiDex.jar"));
662 }
663 
TEST_F(ClassLinkerTest,FindClassNested)664 TEST_F(ClassLinkerTest, FindClassNested) {
665   ScopedObjectAccess soa(Thread::Current());
666   StackHandleScope<1> hs(soa.Self());
667   Handle<mirror::ClassLoader> class_loader(
668       hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Nested"))));
669 
670   mirror::Class* outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader);
671   ASSERT_TRUE(outer != nullptr);
672   EXPECT_EQ(0U, outer->NumVirtualMethods());
673   EXPECT_EQ(1U, outer->NumDirectMethods());
674 
675   mirror::Class* inner = class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader);
676   ASSERT_TRUE(inner != nullptr);
677   EXPECT_EQ(0U, inner->NumVirtualMethods());
678   EXPECT_EQ(1U, inner->NumDirectMethods());
679 }
680 
TEST_F(ClassLinkerTest,FindClass_Primitives)681 TEST_F(ClassLinkerTest, FindClass_Primitives) {
682   ScopedObjectAccess soa(Thread::Current());
683   const std::string expected("BCDFIJSZV");
684   for (int ch = 1; ch < 256; ++ch) {
685     std::string descriptor;
686     descriptor.push_back(ch);
687     if (expected.find(ch) == std::string::npos) {
688       AssertNonExistentClass(descriptor);
689     } else {
690       AssertPrimitiveClass(descriptor);
691     }
692   }
693 }
694 
TEST_F(ClassLinkerTest,FindClass)695 TEST_F(ClassLinkerTest, FindClass) {
696   ScopedObjectAccess soa(Thread::Current());
697   mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
698   ASSERT_TRUE(JavaLangObject != nullptr);
699   ASSERT_TRUE(JavaLangObject->GetClass() != nullptr);
700   ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
701   EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
702   std::string temp;
703   ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;");
704   EXPECT_TRUE(JavaLangObject->GetSuperClass() == nullptr);
705   EXPECT_FALSE(JavaLangObject->HasSuperClass());
706   EXPECT_TRUE(JavaLangObject->GetClassLoader() == nullptr);
707   EXPECT_EQ(mirror::Class::kStatusInitialized, JavaLangObject->GetStatus());
708   EXPECT_FALSE(JavaLangObject->IsErroneous());
709   EXPECT_TRUE(JavaLangObject->IsLoaded());
710   EXPECT_TRUE(JavaLangObject->IsResolved());
711   EXPECT_TRUE(JavaLangObject->IsVerified());
712   EXPECT_TRUE(JavaLangObject->IsInitialized());
713   EXPECT_FALSE(JavaLangObject->IsArrayInstance());
714   EXPECT_FALSE(JavaLangObject->IsArrayClass());
715   EXPECT_TRUE(JavaLangObject->GetComponentType() == nullptr);
716   EXPECT_FALSE(JavaLangObject->IsInterface());
717   EXPECT_TRUE(JavaLangObject->IsPublic());
718   EXPECT_FALSE(JavaLangObject->IsFinal());
719   EXPECT_FALSE(JavaLangObject->IsPrimitive());
720   EXPECT_FALSE(JavaLangObject->IsSynthetic());
721   EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
722   EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
723   if (!kUseBrooksReadBarrier) {
724     EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
725   } else {
726     EXPECT_EQ(4U, JavaLangObject->NumInstanceFields());
727   }
728   EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(), "shadow$_klass_");
729   EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(), "shadow$_monitor_");
730   if (kUseBrooksReadBarrier) {
731     EXPECT_STREQ(JavaLangObject->GetInstanceField(2)->GetName(), "shadow$_x_rb_ptr_");
732     EXPECT_STREQ(JavaLangObject->GetInstanceField(3)->GetName(), "shadow$_x_xpadding_");
733   }
734 
735   EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
736   EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces());
737 
738   StackHandleScope<1> hs(soa.Self());
739   Handle<mirror::ClassLoader> class_loader(
740       hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
741   AssertNonExistentClass("LMyClass;");
742   mirror::Class* MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
743   ASSERT_TRUE(MyClass != nullptr);
744   ASSERT_TRUE(MyClass->GetClass() != nullptr);
745   ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
746   EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
747   ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;");
748   EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
749   EXPECT_TRUE(MyClass->HasSuperClass());
750   EXPECT_EQ(class_loader.Get(), MyClass->GetClassLoader());
751   EXPECT_EQ(mirror::Class::kStatusResolved, MyClass->GetStatus());
752   EXPECT_FALSE(MyClass->IsErroneous());
753   EXPECT_TRUE(MyClass->IsLoaded());
754   EXPECT_TRUE(MyClass->IsResolved());
755   EXPECT_FALSE(MyClass->IsVerified());
756   EXPECT_FALSE(MyClass->IsInitialized());
757   EXPECT_FALSE(MyClass->IsArrayInstance());
758   EXPECT_FALSE(MyClass->IsArrayClass());
759   EXPECT_TRUE(MyClass->GetComponentType() == nullptr);
760   EXPECT_FALSE(MyClass->IsInterface());
761   EXPECT_FALSE(MyClass->IsPublic());
762   EXPECT_FALSE(MyClass->IsFinal());
763   EXPECT_FALSE(MyClass->IsPrimitive());
764   EXPECT_FALSE(MyClass->IsSynthetic());
765   EXPECT_EQ(1U, MyClass->NumDirectMethods());
766   EXPECT_EQ(0U, MyClass->NumVirtualMethods());
767   EXPECT_EQ(0U, MyClass->NumInstanceFields());
768   EXPECT_EQ(0U, MyClass->NumStaticFields());
769   EXPECT_EQ(0U, MyClass->NumDirectInterfaces());
770 
771   EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
772 
773   // created by class_linker
774   AssertArrayClass("[C", "C", nullptr);
775   AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr);
776   // synthesized on the fly
777   AssertArrayClass("[[C", "[C", nullptr);
778   AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get());
779   // or not available at all
780   AssertNonExistentClass("[[[[LNonExistentClass;");
781 }
782 
TEST_F(ClassLinkerTest,LibCore)783 TEST_F(ClassLinkerTest, LibCore) {
784   ScopedObjectAccess soa(Thread::Current());
785   ASSERT_TRUE(java_lang_dex_file_ != nullptr);
786   AssertDexFile(*java_lang_dex_file_, nullptr);
787 }
788 
789 // The first reference array element must be a multiple of 4 bytes from the
790 // start of the object
TEST_F(ClassLinkerTest,ValidateObjectArrayElementsOffset)791 TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
792   ScopedObjectAccess soa(Thread::Current());
793   mirror::Class* array_class = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
794   mirror::ObjectArray<mirror::String>* array =
795       mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0);
796   uintptr_t data_offset =
797       reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>),
798                                                     0));
799   if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) {
800     EXPECT_TRUE(IsAligned<4>(data_offset));  // Check 4 byte alignment.
801   } else {
802     EXPECT_TRUE(IsAligned<8>(data_offset));  // Check 8 byte alignment.
803   }
804 }
805 
TEST_F(ClassLinkerTest,ValidatePrimitiveArrayElementsOffset)806 TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
807   ScopedObjectAccess soa(Thread::Current());
808   StackHandleScope<5> hs(soa.Self());
809   Handle<mirror::LongArray> long_array(hs.NewHandle(mirror::LongArray::Alloc(soa.Self(), 0)));
810   EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass());
811   uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
812   EXPECT_TRUE(IsAligned<8>(data_offset));  // Longs require 8 byte alignment
813 
814   Handle<mirror::DoubleArray> double_array(hs.NewHandle(mirror::DoubleArray::Alloc(soa.Self(), 0)));
815   EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass());
816   data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
817   EXPECT_TRUE(IsAligned<8>(data_offset));  // Doubles require 8 byte alignment
818 
819   Handle<mirror::IntArray> int_array(hs.NewHandle(mirror::IntArray::Alloc(soa.Self(), 0)));
820   EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass());
821   data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
822   EXPECT_TRUE(IsAligned<4>(data_offset));  // Ints require 4 byte alignment
823 
824   Handle<mirror::CharArray> char_array(hs.NewHandle(mirror::CharArray::Alloc(soa.Self(), 0)));
825   EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass());
826   data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
827   EXPECT_TRUE(IsAligned<2>(data_offset));  // Chars require 2 byte alignment
828 
829   Handle<mirror::ShortArray> short_array(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0)));
830   EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass());
831   data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
832   EXPECT_TRUE(IsAligned<2>(data_offset));  // Shorts require 2 byte alignment
833 
834   // Take it as given that bytes and booleans have byte alignment
835 }
836 
TEST_F(ClassLinkerTest,ValidateBoxedTypes)837 TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
838   // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
839   // This lets UnboxPrimitive avoid searching for the field by name at runtime.
840   ScopedObjectAccess soa(Thread::Current());
841   NullHandle<mirror::ClassLoader> class_loader;
842   mirror::Class* c;
843   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader);
844   EXPECT_STREQ("value", c->GetIFields()[0].GetName());
845   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader);
846   EXPECT_STREQ("value", c->GetIFields()[0].GetName());
847   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader);
848   EXPECT_STREQ("value", c->GetIFields()[0].GetName());
849   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader);
850   EXPECT_STREQ("value", c->GetIFields()[0].GetName());
851   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader);
852   EXPECT_STREQ("value", c->GetIFields()[0].GetName());
853   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", class_loader);
854   EXPECT_STREQ("value", c->GetIFields()[0].GetName());
855   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Long;", class_loader);
856   EXPECT_STREQ("value", c->GetIFields()[0].GetName());
857   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Short;", class_loader);
858   EXPECT_STREQ("value", c->GetIFields()[0].GetName());
859 }
860 
TEST_F(ClassLinkerTest,TwoClassLoadersOneClass)861 TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
862   ScopedObjectAccess soa(Thread::Current());
863   StackHandleScope<2> hs(soa.Self());
864   Handle<mirror::ClassLoader> class_loader_1(
865       hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
866   Handle<mirror::ClassLoader> class_loader_2(
867       hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
868   mirror::Class* MyClass_1 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1);
869   mirror::Class* MyClass_2 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_2);
870   EXPECT_TRUE(MyClass_1 != nullptr);
871   EXPECT_TRUE(MyClass_2 != nullptr);
872   EXPECT_NE(MyClass_1, MyClass_2);
873 }
874 
TEST_F(ClassLinkerTest,StaticFields)875 TEST_F(ClassLinkerTest, StaticFields) {
876   ScopedObjectAccess soa(Thread::Current());
877   StackHandleScope<2> hs(soa.Self());
878   Handle<mirror::ClassLoader> class_loader(
879       hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics"))));
880   Handle<mirror::Class> statics(
881       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
882   class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
883 
884   // Static final primitives that are initialized by a compile-time constant
885   // expression resolve to a copy of a constant value from the constant pool.
886   // So <clinit> should be null.
887   ArtMethod* clinit = statics->FindDirectMethod("<clinit>", "()V", sizeof(void*));
888   EXPECT_TRUE(clinit == nullptr);
889 
890   EXPECT_EQ(9U, statics->NumStaticFields());
891 
892   ArtField* s0 = mirror::Class::FindStaticField(soa.Self(), statics, "s0", "Z");
893   EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
894   EXPECT_EQ(true, s0->GetBoolean(statics.Get()));
895   s0->SetBoolean<false>(statics.Get(), false);
896 
897   ArtField* s1 = mirror::Class::FindStaticField(soa.Self(), statics, "s1", "B");
898   EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
899   EXPECT_EQ(5, s1->GetByte(statics.Get()));
900   s1->SetByte<false>(statics.Get(), 6);
901 
902   ArtField* s2 = mirror::Class::FindStaticField(soa.Self(), statics, "s2", "C");
903   EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
904   EXPECT_EQ('a', s2->GetChar(statics.Get()));
905   s2->SetChar<false>(statics.Get(), 'b');
906 
907   ArtField* s3 = mirror::Class::FindStaticField(soa.Self(), statics, "s3", "S");
908   EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
909   EXPECT_EQ(-536, s3->GetShort(statics.Get()));
910   s3->SetShort<false>(statics.Get(), -535);
911 
912   ArtField* s4 = mirror::Class::FindStaticField(soa.Self(), statics, "s4", "I");
913   EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
914   EXPECT_EQ(2000000000, s4->GetInt(statics.Get()));
915   s4->SetInt<false>(statics.Get(), 2000000001);
916 
917   ArtField* s5 = mirror::Class::FindStaticField(soa.Self(), statics, "s5", "J");
918   EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
919   EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get()));
920   s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12));
921 
922   ArtField* s6 = mirror::Class::FindStaticField(soa.Self(), statics, "s6", "F");
923   EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
924   EXPECT_DOUBLE_EQ(0.5, s6->GetFloat(statics.Get()));
925   s6->SetFloat<false>(statics.Get(), 0.75);
926 
927   ArtField* s7 = mirror::Class::FindStaticField(soa.Self(), statics, "s7", "D");
928   EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
929   EXPECT_DOUBLE_EQ(16777217.0, s7->GetDouble(statics.Get()));
930   s7->SetDouble<false>(statics.Get(), 16777219);
931 
932   ArtField* s8 = mirror::Class::FindStaticField(soa.Self(), statics, "s8",
933                                                         "Ljava/lang/String;");
934   EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
935   EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android"));
936   s8->SetObject<false>(s8->GetDeclaringClass(),
937                        mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot"));
938 
939   // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
940   // http://code.google.com/p/googletest/issues/detail?id=322
941   EXPECT_FALSE(s0->GetBoolean(statics.Get()));
942   EXPECT_EQ(6, s1->GetByte(statics.Get()));
943   EXPECT_EQ('b', s2->GetChar(statics.Get()));
944   EXPECT_EQ(-535, s3->GetShort(statics.Get()));
945   EXPECT_EQ(2000000001, s4->GetInt(statics.Get()));
946   EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get()));
947   EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get()));
948   EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get()));
949   EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot"));
950 }
951 
TEST_F(ClassLinkerTest,Interfaces)952 TEST_F(ClassLinkerTest, Interfaces) {
953   ScopedObjectAccess soa(Thread::Current());
954   StackHandleScope<6> hs(soa.Self());
955   Handle<mirror::ClassLoader> class_loader(
956       hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Interfaces"))));
957   Handle<mirror::Class> I(
958       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
959   Handle<mirror::Class> J(
960       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
961   Handle<mirror::Class> K(
962       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader)));
963   Handle<mirror::Class> A(
964       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader)));
965   Handle<mirror::Class> B(
966       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader)));
967   EXPECT_TRUE(I->IsAssignableFrom(A.Get()));
968   EXPECT_TRUE(J->IsAssignableFrom(A.Get()));
969   EXPECT_TRUE(J->IsAssignableFrom(K.Get()));
970   EXPECT_TRUE(K->IsAssignableFrom(B.Get()));
971   EXPECT_TRUE(J->IsAssignableFrom(B.Get()));
972 
973   const Signature void_sig = I->GetDexCache()->GetDexFile()->CreateSignature("()V");
974   ArtMethod* Ii = I->FindVirtualMethod("i", void_sig, sizeof(void*));
975   ArtMethod* Jj1 = J->FindVirtualMethod("j1", void_sig, sizeof(void*));
976   ArtMethod* Jj2 = J->FindVirtualMethod("j2", void_sig, sizeof(void*));
977   ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig, sizeof(void*));
978   ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig, sizeof(void*));
979   ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig, sizeof(void*));
980   ArtMethod* Ai = A->FindVirtualMethod("i", void_sig, sizeof(void*));
981   ArtMethod* Aj1 = A->FindVirtualMethod("j1", void_sig, sizeof(void*));
982   ArtMethod* Aj2 = A->FindVirtualMethod("j2", void_sig, sizeof(void*));
983   ASSERT_TRUE(Ii != nullptr);
984   ASSERT_TRUE(Jj1 != nullptr);
985   ASSERT_TRUE(Jj2 != nullptr);
986   ASSERT_TRUE(Kj1 != nullptr);
987   ASSERT_TRUE(Kj2 != nullptr);
988   ASSERT_TRUE(Kk != nullptr);
989   ASSERT_TRUE(Ai != nullptr);
990   ASSERT_TRUE(Aj1 != nullptr);
991   ASSERT_TRUE(Aj2 != nullptr);
992   EXPECT_NE(Ii, Ai);
993   EXPECT_NE(Jj1, Aj1);
994   EXPECT_NE(Jj2, Aj2);
995   EXPECT_EQ(Kj1, Jj1);
996   EXPECT_EQ(Kj2, Jj2);
997   EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, sizeof(void*)));
998   EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, sizeof(void*)));
999   EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, sizeof(void*)));
1000   EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii, sizeof(void*)));
1001   EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1, sizeof(void*)));
1002   EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2, sizeof(void*)));
1003 
1004   ArtField* Afoo = mirror::Class::FindStaticField(soa.Self(), A, "foo", "Ljava/lang/String;");
1005   ArtField* Bfoo = mirror::Class::FindStaticField(soa.Self(), B, "foo", "Ljava/lang/String;");
1006   ArtField* Jfoo = mirror::Class::FindStaticField(soa.Self(), J, "foo", "Ljava/lang/String;");
1007   ArtField* Kfoo = mirror::Class::FindStaticField(soa.Self(), K, "foo", "Ljava/lang/String;");
1008   ASSERT_TRUE(Afoo != nullptr);
1009   EXPECT_EQ(Afoo, Bfoo);
1010   EXPECT_EQ(Afoo, Jfoo);
1011   EXPECT_EQ(Afoo, Kfoo);
1012 }
1013 
TEST_F(ClassLinkerTest,ResolveVerifyAndClinit)1014 TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
1015   // pretend we are trying to get the static storage for the StaticsFromCode class.
1016 
1017   // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
1018   // case 2, get the initialized storage from StaticsFromCode.getS0
1019 
1020   ScopedObjectAccess soa(Thread::Current());
1021   jobject jclass_loader = LoadDex("StaticsFromCode");
1022   const DexFile* dex_file = GetFirstDexFile(jclass_loader);
1023   StackHandleScope<1> hs(soa.Self());
1024   Handle<mirror::ClassLoader> class_loader(
1025       hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
1026   mirror::Class* klass = class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader);
1027   ArtMethod* clinit = klass->FindClassInitializer(sizeof(void*));
1028   ArtMethod* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;", sizeof(void*));
1029   const DexFile::StringId* string_id = dex_file->FindStringId("LStaticsFromCode;");
1030   ASSERT_TRUE(string_id != nullptr);
1031   const DexFile::TypeId* type_id = dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id));
1032   ASSERT_TRUE(type_id != nullptr);
1033   uint32_t type_idx = dex_file->GetIndexForTypeId(*type_id);
1034   mirror::Class* uninit = ResolveVerifyAndClinit(type_idx, clinit, soa.Self(), true, false);
1035   EXPECT_TRUE(uninit != nullptr);
1036   EXPECT_FALSE(uninit->IsInitialized());
1037   mirror::Class* init = ResolveVerifyAndClinit(type_idx, getS0, soa.Self(), true, false);
1038   EXPECT_TRUE(init != nullptr);
1039   EXPECT_TRUE(init->IsInitialized());
1040 }
1041 
TEST_F(ClassLinkerTest,FinalizableBit)1042 TEST_F(ClassLinkerTest, FinalizableBit) {
1043   ScopedObjectAccess soa(Thread::Current());
1044   mirror::Class* c;
1045 
1046   // Object has a finalize method, but we know it's empty.
1047   c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
1048   EXPECT_FALSE(c->IsFinalizable());
1049 
1050   // Enum has a finalize method to prevent its subclasses from implementing one.
1051   c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;");
1052   EXPECT_FALSE(c->IsFinalizable());
1053 
1054   // RoundingMode is an enum.
1055   c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;");
1056   EXPECT_FALSE(c->IsFinalizable());
1057 
1058   // RandomAccessFile extends Object and overrides finalize.
1059   c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;");
1060   EXPECT_TRUE(c->IsFinalizable());
1061 
1062   // FileInputStream is finalizable and extends InputStream which isn't.
1063   c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;");
1064   EXPECT_FALSE(c->IsFinalizable());
1065   c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;");
1066   EXPECT_TRUE(c->IsFinalizable());
1067 
1068   // ScheduledThreadPoolExecutor doesn't have a finalize method but
1069   // extends ThreadPoolExecutor which does.
1070   c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;");
1071   EXPECT_TRUE(c->IsFinalizable());
1072   c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
1073   EXPECT_TRUE(c->IsFinalizable());
1074 }
1075 
TEST_F(ClassLinkerTest,ClassRootDescriptors)1076 TEST_F(ClassLinkerTest, ClassRootDescriptors) {
1077   ScopedObjectAccess soa(Thread::Current());
1078   std::string temp;
1079   for (int i = 0; i < ClassLinker::kClassRootsMax; i++) {
1080     mirror::Class* klass = class_linker_->GetClassRoot(ClassLinker::ClassRoot(i));
1081     EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U);
1082     EXPECT_STREQ(klass->GetDescriptor(&temp),
1083                  class_linker_->GetClassRootDescriptor(ClassLinker::ClassRoot(i))) << " i = " << i;
1084   }
1085 }
1086 
TEST_F(ClassLinkerTest,ValidatePredefinedClassSizes)1087 TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) {
1088   ScopedObjectAccess soa(Thread::Current());
1089   NullHandle<mirror::ClassLoader> class_loader;
1090   mirror::Class* c;
1091 
1092   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader);
1093   ASSERT_TRUE(c != nullptr);
1094   EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize(sizeof(void*)));
1095 
1096   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader);
1097   ASSERT_TRUE(c != nullptr);
1098   EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize(sizeof(void*)));
1099 
1100   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader);
1101   ASSERT_TRUE(c != nullptr);
1102   EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize(sizeof(void*)));
1103 
1104   c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader);
1105   ASSERT_TRUE(c != nullptr);
1106   EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize(sizeof(void*)));
1107 }
1108 
CheckMethod(ArtMethod * method,bool verified)1109 static void CheckMethod(ArtMethod* method, bool verified)
1110     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1111   if (!method->IsNative() && !method->IsAbstract()) {
1112     EXPECT_EQ((method->GetAccessFlags() & kAccPreverified) != 0U, verified)
1113         << PrettyMethod(method, true);
1114   }
1115 }
1116 
CheckPreverified(mirror::Class * c,bool preverified)1117 static void CheckPreverified(mirror::Class* c, bool preverified)
1118     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1119   EXPECT_EQ((c->GetAccessFlags() & kAccPreverified) != 0U, preverified)
1120       << "Class " << PrettyClass(c) << " not as expected";
1121   for (auto& m : c->GetDirectMethods(sizeof(void*))) {
1122     CheckMethod(&m, preverified);
1123   }
1124   for (auto& m : c->GetVirtualMethods(sizeof(void*))) {
1125     CheckMethod(&m, preverified);
1126   }
1127 }
1128 
TEST_F(ClassLinkerTest,Preverified_InitializedBoot)1129 TEST_F(ClassLinkerTest, Preverified_InitializedBoot) {
1130   ScopedObjectAccess soa(Thread::Current());
1131 
1132   mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
1133   ASSERT_TRUE(JavaLangObject != nullptr);
1134   EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the "
1135                                                   "core";
1136   CheckPreverified(JavaLangObject, true);
1137 }
1138 
TEST_F(ClassLinkerTest,Preverified_UninitializedBoot)1139 TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) {
1140   ScopedObjectAccess soa(Thread::Current());
1141 
1142   StackHandleScope<1> hs(soa.Self());
1143 
1144   Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass(
1145       soa.Self(), "Ljava/lang/SecurityManager;")));
1146   EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the "
1147                                                      "core";
1148 
1149   CheckPreverified(security_manager.Get(), false);
1150 
1151   class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true);
1152   CheckPreverified(security_manager.Get(), true);
1153 }
1154 
TEST_F(ClassLinkerTest,Preverified_App)1155 TEST_F(ClassLinkerTest, Preverified_App) {
1156   ScopedObjectAccess soa(Thread::Current());
1157 
1158   StackHandleScope<2> hs(soa.Self());
1159   Handle<mirror::ClassLoader> class_loader(
1160       hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics"))));
1161   Handle<mirror::Class> statics(
1162       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1163 
1164   CheckPreverified(statics.Get(), false);
1165 
1166   class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
1167   CheckPreverified(statics.Get(), true);
1168 }
1169 
TEST_F(ClassLinkerTest,IsBootStrapClassLoaded)1170 TEST_F(ClassLinkerTest, IsBootStrapClassLoaded) {
1171   ScopedObjectAccess soa(Thread::Current());
1172 
1173   StackHandleScope<3> hs(soa.Self());
1174   Handle<mirror::ClassLoader> class_loader(
1175       hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics"))));
1176 
1177   // java.lang.Object is a bootstrap class.
1178   Handle<mirror::Class> jlo_class(
1179       hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
1180   ASSERT_TRUE(jlo_class.Get() != nullptr);
1181   EXPECT_TRUE(jlo_class.Get()->IsBootStrapClassLoaded());
1182 
1183   // Statics is not a bootstrap class.
1184   Handle<mirror::Class> statics(
1185       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1186   ASSERT_TRUE(statics.Get() != nullptr);
1187   EXPECT_FALSE(statics.Get()->IsBootStrapClassLoaded());
1188 }
1189 
1190 }  // namespace art
1191