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