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