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.h" 18 19 #include "android-base/stringprintf.h" 20 21 #include "art_field-inl.h" 22 #include "art_method-inl.h" 23 #include "base/logging.h" // For VLOG. 24 #include "base/utils.h" 25 #include "class-inl.h" 26 #include "class_ext.h" 27 #include "class_linker-inl.h" 28 #include "class_loader.h" 29 #include "class_root.h" 30 #include "dex/descriptors_names.h" 31 #include "dex/dex_file-inl.h" 32 #include "dex/dex_file_annotations.h" 33 #include "dex/signature-inl.h" 34 #include "dex_cache-inl.h" 35 #include "gc/accounting/card_table-inl.h" 36 #include "gc/heap-inl.h" 37 #include "handle_scope-inl.h" 38 #include "hidden_api.h" 39 #include "subtype_check.h" 40 #include "method.h" 41 #include "object-inl.h" 42 #include "object-refvisitor-inl.h" 43 #include "object_array-inl.h" 44 #include "object_lock.h" 45 #include "string-inl.h" 46 #include "runtime.h" 47 #include "thread.h" 48 #include "throwable.h" 49 #include "well_known_classes.h" 50 51 namespace art { 52 53 // TODO: move to own CC file? 54 constexpr size_t BitString::kBitSizeAtPosition[BitString::kCapacity]; 55 constexpr size_t BitString::kCapacity; 56 57 namespace mirror { 58 59 using android::base::StringPrintf; 60 61 ObjPtr<mirror::Class> Class::GetPrimitiveClass(ObjPtr<mirror::String> name) { 62 const char* expected_name = nullptr; 63 ClassRoot class_root = ClassRoot::kJavaLangObject; // Invalid. 64 if (name != nullptr && name->GetLength() >= 2) { 65 // Perfect hash for the expected values: from the second letters of the primitive types, 66 // only 'y' has the bit 0x10 set, so use it to change 'b' to 'B'. 67 char hash = name->CharAt(0) ^ ((name->CharAt(1) & 0x10) << 1); 68 switch (hash) { 69 case 'b': expected_name = "boolean"; class_root = ClassRoot::kPrimitiveBoolean; break; 70 case 'B': expected_name = "byte"; class_root = ClassRoot::kPrimitiveByte; break; 71 case 'c': expected_name = "char"; class_root = ClassRoot::kPrimitiveChar; break; 72 case 'd': expected_name = "double"; class_root = ClassRoot::kPrimitiveDouble; break; 73 case 'f': expected_name = "float"; class_root = ClassRoot::kPrimitiveFloat; break; 74 case 'i': expected_name = "int"; class_root = ClassRoot::kPrimitiveInt; break; 75 case 'l': expected_name = "long"; class_root = ClassRoot::kPrimitiveLong; break; 76 case 's': expected_name = "short"; class_root = ClassRoot::kPrimitiveShort; break; 77 case 'v': expected_name = "void"; class_root = ClassRoot::kPrimitiveVoid; break; 78 default: break; 79 } 80 } 81 if (expected_name != nullptr && name->Equals(expected_name)) { 82 ObjPtr<mirror::Class> klass = GetClassRoot(class_root); 83 DCHECK(klass != nullptr); 84 return klass; 85 } else { 86 Thread* self = Thread::Current(); 87 if (name == nullptr) { 88 // Note: ThrowNullPointerException() requires a message which we deliberately want to omit. 89 self->ThrowNewException("Ljava/lang/NullPointerException;", /* msg= */ nullptr); 90 } else { 91 self->ThrowNewException("Ljava/lang/ClassNotFoundException;", name->ToModifiedUtf8().c_str()); 92 } 93 return nullptr; 94 } 95 } 96 97 ObjPtr<ClassExt> Class::EnsureExtDataPresent(Thread* self) { 98 ObjPtr<ClassExt> existing(GetExtData()); 99 if (!existing.IsNull()) { 100 return existing; 101 } 102 StackHandleScope<3> hs(self); 103 // Handlerize 'this' since we are allocating here. 104 Handle<Class> h_this(hs.NewHandle(this)); 105 // Clear exception so we can allocate. 106 Handle<Throwable> throwable(hs.NewHandle(self->GetException())); 107 self->ClearException(); 108 // Allocate the ClassExt 109 Handle<ClassExt> new_ext(hs.NewHandle(ClassExt::Alloc(self))); 110 if (new_ext == nullptr) { 111 // OOM allocating the classExt. 112 // TODO Should we restore the suppressed exception? 113 self->AssertPendingOOMException(); 114 return nullptr; 115 } else { 116 MemberOffset ext_offset(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_)); 117 bool set; 118 // Set the ext_data_ field using CAS semantics. 119 if (Runtime::Current()->IsActiveTransaction()) { 120 set = h_this->CasFieldObject<true>(ext_offset, 121 nullptr, 122 new_ext.Get(), 123 CASMode::kStrong, 124 std::memory_order_seq_cst); 125 } else { 126 set = h_this->CasFieldObject<false>(ext_offset, 127 nullptr, 128 new_ext.Get(), 129 CASMode::kStrong, 130 std::memory_order_seq_cst); 131 } 132 ObjPtr<ClassExt> ret(set ? new_ext.Get() : h_this->GetExtData()); 133 DCHECK(!set || h_this->GetExtData() == new_ext.Get()); 134 CHECK(!ret.IsNull()); 135 // Restore the exception if there was one. 136 if (throwable != nullptr) { 137 self->SetException(throwable.Get()); 138 } 139 return ret; 140 } 141 } 142 143 void Class::SetStatus(Handle<Class> h_this, ClassStatus new_status, Thread* self) { 144 ClassStatus old_status = h_this->GetStatus(); 145 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 146 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized(); 147 if (LIKELY(class_linker_initialized)) { 148 if (UNLIKELY(new_status <= old_status && 149 new_status != ClassStatus::kErrorUnresolved && 150 new_status != ClassStatus::kErrorResolved && 151 new_status != ClassStatus::kRetired)) { 152 LOG(FATAL) << "Unexpected change back of class status for " << h_this->PrettyClass() 153 << " " << old_status << " -> " << new_status; 154 } 155 if (new_status >= ClassStatus::kResolved || old_status >= ClassStatus::kResolved) { 156 // When classes are being resolved the resolution code should hold the lock. 157 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId()) 158 << "Attempt to change status of class while not holding its lock: " 159 << h_this->PrettyClass() << " " << old_status << " -> " << new_status; 160 } 161 } 162 if (UNLIKELY(IsErroneous(new_status))) { 163 CHECK(!h_this->IsErroneous()) 164 << "Attempt to set as erroneous an already erroneous class " 165 << h_this->PrettyClass() 166 << " old_status: " << old_status << " new_status: " << new_status; 167 CHECK_EQ(new_status == ClassStatus::kErrorResolved, old_status >= ClassStatus::kResolved); 168 if (VLOG_IS_ON(class_linker)) { 169 LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous."; 170 if (self->IsExceptionPending()) { 171 LOG(ERROR) << "Exception: " << self->GetException()->Dump(); 172 } 173 } 174 175 ObjPtr<ClassExt> ext(h_this->EnsureExtDataPresent(self)); 176 if (!ext.IsNull()) { 177 self->AssertPendingException(); 178 ext->SetVerifyError(self->GetException()); 179 } else { 180 self->AssertPendingOOMException(); 181 } 182 self->AssertPendingException(); 183 } 184 185 if (kBitstringSubtypeCheckEnabled) { 186 // FIXME: This looks broken with respect to aborted transactions. 187 ObjPtr<mirror::Class> h_this_ptr = h_this.Get(); 188 SubtypeCheck<ObjPtr<mirror::Class>>::WriteStatus(h_this_ptr, new_status); 189 } else { 190 // The ClassStatus is always in the 4 most-significant bits of status_. 191 static_assert(sizeof(status_) == sizeof(uint32_t), "Size of status_ not equal to uint32"); 192 uint32_t new_status_value = static_cast<uint32_t>(new_status) << (32 - kClassStatusBitSize); 193 if (Runtime::Current()->IsActiveTransaction()) { 194 h_this->SetField32Volatile<true>(StatusOffset(), new_status_value); 195 } else { 196 h_this->SetField32Volatile<false>(StatusOffset(), new_status_value); 197 } 198 } 199 200 // Setting the object size alloc fast path needs to be after the status write so that if the 201 // alloc path sees a valid object size, we would know that it's initialized as long as it has a 202 // load-acquire/fake dependency. 203 if (new_status == ClassStatus::kInitialized && !h_this->IsVariableSize()) { 204 DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max()); 205 // Finalizable objects must always go slow path. 206 if (!h_this->IsFinalizable()) { 207 h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment)); 208 } 209 } 210 211 if (kIsDebugBuild && new_status >= ClassStatus::kInitialized) { 212 CHECK(h_this->WasVerificationAttempted()) << h_this->PrettyClassAndClassLoader(); 213 } 214 215 if (!class_linker_initialized) { 216 // When the class linker is being initialized its single threaded and by definition there can be 217 // no waiters. During initialization classes may appear temporary but won't be retired as their 218 // size was statically computed. 219 } else { 220 // Classes that are being resolved or initialized need to notify waiters that the class status 221 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass. 222 if (h_this->IsTemp()) { 223 // Class is a temporary one, ensure that waiters for resolution get notified of retirement 224 // so that they can grab the new version of the class from the class linker's table. 225 CHECK_LT(new_status, ClassStatus::kResolved) << h_this->PrettyDescriptor(); 226 if (new_status == ClassStatus::kRetired || new_status == ClassStatus::kErrorUnresolved) { 227 h_this->NotifyAll(self); 228 } 229 } else { 230 CHECK_NE(new_status, ClassStatus::kRetired); 231 if (old_status >= ClassStatus::kResolved || new_status >= ClassStatus::kResolved) { 232 h_this->NotifyAll(self); 233 } 234 } 235 } 236 } 237 238 void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) { 239 SetFieldObjectTransaction(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache); 240 } 241 242 void Class::SetClassSize(uint32_t new_class_size) { 243 if (kIsDebugBuild && new_class_size < GetClassSize()) { 244 DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail); 245 LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize(); 246 LOG(FATAL) << "class=" << PrettyTypeOf(); 247 } 248 SetField32Transaction(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size); 249 } 250 251 // Return the class' name. The exact format is bizarre, but it's the specified behavior for 252 // Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int" 253 // but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than 254 // slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness. 255 ObjPtr<String> Class::ComputeName(Handle<Class> h_this) { 256 ObjPtr<String> name = h_this->GetName(); 257 if (name != nullptr) { 258 return name; 259 } 260 std::string temp; 261 const char* descriptor = h_this->GetDescriptor(&temp); 262 Thread* self = Thread::Current(); 263 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) { 264 // The descriptor indicates that this is the class for 265 // a primitive type; special-case the return value. 266 const char* c_name = nullptr; 267 switch (descriptor[0]) { 268 case 'Z': c_name = "boolean"; break; 269 case 'B': c_name = "byte"; break; 270 case 'C': c_name = "char"; break; 271 case 'S': c_name = "short"; break; 272 case 'I': c_name = "int"; break; 273 case 'J': c_name = "long"; break; 274 case 'F': c_name = "float"; break; 275 case 'D': c_name = "double"; break; 276 case 'V': c_name = "void"; break; 277 default: 278 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]); 279 } 280 name = String::AllocFromModifiedUtf8(self, c_name); 281 } else { 282 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package 283 // components. 284 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str()); 285 } 286 h_this->SetName(name); 287 return name; 288 } 289 290 void Class::DumpClass(std::ostream& os, int flags) { 291 if ((flags & kDumpClassFullDetail) == 0) { 292 os << PrettyClass(); 293 if ((flags & kDumpClassClassLoader) != 0) { 294 os << ' ' << GetClassLoader(); 295 } 296 if ((flags & kDumpClassInitialized) != 0) { 297 os << ' ' << GetStatus(); 298 } 299 os << "\n"; 300 return; 301 } 302 303 Thread* const self = Thread::Current(); 304 StackHandleScope<2> hs(self); 305 Handle<Class> h_this(hs.NewHandle(this)); 306 Handle<Class> h_super(hs.NewHandle(GetSuperClass())); 307 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); 308 309 std::string temp; 310 os << "----- " << (IsInterface() ? "interface" : "class") << " " 311 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n", 312 os << " objectSize=" << SizeOf() << " " 313 << "(" << (h_super != nullptr ? h_super->SizeOf() : -1) << " from super)\n", 314 os << StringPrintf(" access=0x%04x.%04x\n", 315 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask); 316 if (h_super != nullptr) { 317 os << " super='" << h_super->PrettyClass() << "' (cl=" << h_super->GetClassLoader() 318 << ")\n"; 319 } 320 if (IsArrayClass()) { 321 os << " componentType=" << PrettyClass(GetComponentType()) << "\n"; 322 } 323 const size_t num_direct_interfaces = NumDirectInterfaces(); 324 if (num_direct_interfaces > 0) { 325 os << " interfaces (" << num_direct_interfaces << "):\n"; 326 for (size_t i = 0; i < num_direct_interfaces; ++i) { 327 ObjPtr<Class> interface = GetDirectInterface(self, h_this.Get(), i); 328 if (interface == nullptr) { 329 os << StringPrintf(" %2zd: nullptr!\n", i); 330 } else { 331 ObjPtr<ClassLoader> cl = interface->GetClassLoader(); 332 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr()); 333 } 334 } 335 } 336 if (!IsLoaded()) { 337 os << " class not yet loaded"; 338 } else { 339 // After this point, this may have moved due to GetDirectInterface. 340 os << " vtable (" << h_this->NumVirtualMethods() << " entries, " 341 << (h_super != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n"; 342 for (size_t i = 0; i < NumVirtualMethods(); ++i) { 343 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod( 344 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str()); 345 } 346 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n"; 347 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) { 348 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod( 349 h_this->GetDirectMethod(i, image_pointer_size)).c_str()); 350 } 351 if (h_this->NumStaticFields() > 0) { 352 os << " static fields (" << h_this->NumStaticFields() << " entries):\n"; 353 if (h_this->IsResolved()) { 354 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) { 355 os << StringPrintf(" %2zd: %s\n", i, 356 ArtField::PrettyField(h_this->GetStaticField(i)).c_str()); 357 } 358 } else { 359 os << " <not yet available>"; 360 } 361 } 362 if (h_this->NumInstanceFields() > 0) { 363 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n"; 364 if (h_this->IsResolved()) { 365 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) { 366 os << StringPrintf(" %2zd: %s\n", i, 367 ArtField::PrettyField(h_this->GetInstanceField(i)).c_str()); 368 } 369 } else { 370 os << " <not yet available>"; 371 } 372 } 373 } 374 } 375 376 void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) { 377 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) { 378 // Sanity check that the number of bits set in the reference offset bitmap 379 // agrees with the number of references 380 uint32_t count = 0; 381 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) { 382 count += c->NumReferenceInstanceFieldsDuringLinking(); 383 } 384 // +1 for the Class in Object. 385 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count); 386 } 387 // Not called within a transaction. 388 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), 389 new_reference_offsets); 390 } 391 392 bool Class::IsInSamePackage(std::string_view descriptor1, std::string_view descriptor2) { 393 size_t i = 0; 394 size_t min_length = std::min(descriptor1.size(), descriptor2.size()); 395 while (i < min_length && descriptor1[i] == descriptor2[i]) { 396 ++i; 397 } 398 if (descriptor1.find('/', i) != std::string_view::npos || 399 descriptor2.find('/', i) != std::string_view::npos) { 400 return false; 401 } else { 402 return true; 403 } 404 } 405 406 bool Class::IsInSamePackage(ObjPtr<Class> that) { 407 ObjPtr<Class> klass1 = this; 408 ObjPtr<Class> klass2 = that; 409 if (klass1 == klass2) { 410 return true; 411 } 412 // Class loaders must match. 413 if (klass1->GetClassLoader() != klass2->GetClassLoader()) { 414 return false; 415 } 416 // Arrays are in the same package when their element classes are. 417 while (klass1->IsArrayClass()) { 418 klass1 = klass1->GetComponentType(); 419 } 420 while (klass2->IsArrayClass()) { 421 klass2 = klass2->GetComponentType(); 422 } 423 // trivial check again for array types 424 if (klass1 == klass2) { 425 return true; 426 } 427 // Compare the package part of the descriptor string. 428 std::string temp1, temp2; 429 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2)); 430 } 431 432 bool Class::IsThrowableClass() { 433 return GetClassRoot<mirror::Throwable>()->IsAssignableFrom(this); 434 } 435 436 template <typename SignatureType> 437 static inline ArtMethod* FindInterfaceMethodWithSignature(ObjPtr<Class> klass, 438 std::string_view name, 439 const SignatureType& signature, 440 PointerSize pointer_size) 441 REQUIRES_SHARED(Locks::mutator_lock_) { 442 // If the current class is not an interface, skip the search of its declared methods; 443 // such lookup is used only to distinguish between IncompatibleClassChangeError and 444 // NoSuchMethodError and the caller has already tried to search methods in the class. 445 if (LIKELY(klass->IsInterface())) { 446 // Search declared methods, both direct and virtual. 447 // (This lookup is used also for invoke-static on interface classes.) 448 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) { 449 if (method.GetNameView() == name && method.GetSignature() == signature) { 450 return &method; 451 } 452 } 453 } 454 455 // TODO: If there is a unique maximally-specific non-abstract superinterface method, 456 // we should return it, otherwise an arbitrary one can be returned. 457 ObjPtr<IfTable> iftable = klass->GetIfTable(); 458 for (int32_t i = 0, iftable_count = iftable->Count(); i < iftable_count; ++i) { 459 ObjPtr<Class> iface = iftable->GetInterface(i); 460 for (ArtMethod& method : iface->GetVirtualMethodsSlice(pointer_size)) { 461 if (method.GetNameView() == name && method.GetSignature() == signature) { 462 return &method; 463 } 464 } 465 } 466 467 // Then search for public non-static methods in the java.lang.Object. 468 if (LIKELY(klass->IsInterface())) { 469 ObjPtr<Class> object_class = klass->GetSuperClass(); 470 DCHECK(object_class->IsObjectClass()); 471 for (ArtMethod& method : object_class->GetDeclaredMethodsSlice(pointer_size)) { 472 if (method.IsPublic() && !method.IsStatic() && 473 method.GetNameView() == name && method.GetSignature() == signature) { 474 return &method; 475 } 476 } 477 } 478 return nullptr; 479 } 480 481 ArtMethod* Class::FindInterfaceMethod(std::string_view name, 482 std::string_view signature, 483 PointerSize pointer_size) { 484 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size); 485 } 486 487 ArtMethod* Class::FindInterfaceMethod(std::string_view name, 488 const Signature& signature, 489 PointerSize pointer_size) { 490 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size); 491 } 492 493 ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache, 494 uint32_t dex_method_idx, 495 PointerSize pointer_size) { 496 // We always search by name and signature, ignoring the type index in the MethodId. 497 const DexFile& dex_file = *dex_cache->GetDexFile(); 498 const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); 499 std::string_view name = dex_file.StringViewByIdx(method_id.name_idx_); 500 const Signature signature = dex_file.GetMethodSignature(method_id); 501 return FindInterfaceMethod(name, signature, pointer_size); 502 } 503 504 static inline bool IsValidInheritanceCheck(ObjPtr<mirror::Class> klass, 505 ObjPtr<mirror::Class> declaring_class) 506 REQUIRES_SHARED(Locks::mutator_lock_) { 507 if (klass->IsArrayClass()) { 508 return declaring_class->IsObjectClass(); 509 } else if (klass->IsInterface()) { 510 return declaring_class->IsObjectClass() || declaring_class == klass; 511 } else { 512 return klass->IsSubClass(declaring_class); 513 } 514 } 515 516 static inline bool IsInheritedMethod(ObjPtr<mirror::Class> klass, 517 ObjPtr<mirror::Class> declaring_class, 518 ArtMethod& method) 519 REQUIRES_SHARED(Locks::mutator_lock_) { 520 DCHECK_EQ(declaring_class, method.GetDeclaringClass()); 521 DCHECK_NE(klass, declaring_class); 522 DCHECK(IsValidInheritanceCheck(klass, declaring_class)); 523 uint32_t access_flags = method.GetAccessFlags(); 524 if ((access_flags & (kAccPublic | kAccProtected)) != 0) { 525 return true; 526 } 527 if ((access_flags & kAccPrivate) != 0) { 528 return false; 529 } 530 for (; klass != declaring_class; klass = klass->GetSuperClass()) { 531 if (!klass->IsInSamePackage(declaring_class)) { 532 return false; 533 } 534 } 535 return true; 536 } 537 538 template <typename SignatureType> 539 static inline ArtMethod* FindClassMethodWithSignature(ObjPtr<Class> this_klass, 540 std::string_view name, 541 const SignatureType& signature, 542 PointerSize pointer_size) 543 REQUIRES_SHARED(Locks::mutator_lock_) { 544 // Search declared methods first. 545 for (ArtMethod& method : this_klass->GetDeclaredMethodsSlice(pointer_size)) { 546 ArtMethod* np_method = method.GetInterfaceMethodIfProxy(pointer_size); 547 if (np_method->GetName() == name && np_method->GetSignature() == signature) { 548 return &method; 549 } 550 } 551 552 // Then search the superclass chain. If we find an inherited method, return it. 553 // If we find a method that's not inherited because of access restrictions, 554 // try to find a method inherited from an interface in copied methods. 555 ObjPtr<Class> klass = this_klass->GetSuperClass(); 556 ArtMethod* uninherited_method = nullptr; 557 for (; klass != nullptr; klass = klass->GetSuperClass()) { 558 DCHECK(!klass->IsProxyClass()); 559 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) { 560 if (method.GetName() == name && method.GetSignature() == signature) { 561 if (IsInheritedMethod(this_klass, klass, method)) { 562 return &method; 563 } 564 uninherited_method = &method; 565 break; 566 } 567 } 568 if (uninherited_method != nullptr) { 569 break; 570 } 571 } 572 573 // Then search copied methods. 574 // If we found a method that's not inherited, stop the search in its declaring class. 575 ObjPtr<Class> end_klass = klass; 576 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr); 577 klass = this_klass; 578 if (UNLIKELY(klass->IsProxyClass())) { 579 DCHECK(klass->GetCopiedMethodsSlice(pointer_size).empty()); 580 klass = klass->GetSuperClass(); 581 } 582 for (; klass != end_klass; klass = klass->GetSuperClass()) { 583 DCHECK(!klass->IsProxyClass()); 584 for (ArtMethod& method : klass->GetCopiedMethodsSlice(pointer_size)) { 585 if (method.GetName() == name && method.GetSignature() == signature) { 586 return &method; // No further check needed, copied methods are inherited by definition. 587 } 588 } 589 } 590 return uninherited_method; // Return the `uninherited_method` if any. 591 } 592 593 594 ArtMethod* Class::FindClassMethod(std::string_view name, 595 std::string_view signature, 596 PointerSize pointer_size) { 597 return FindClassMethodWithSignature(this, name, signature, pointer_size); 598 } 599 600 ArtMethod* Class::FindClassMethod(std::string_view name, 601 const Signature& signature, 602 PointerSize pointer_size) { 603 return FindClassMethodWithSignature(this, name, signature, pointer_size); 604 } 605 606 ArtMethod* Class::FindClassMethod(ObjPtr<DexCache> dex_cache, 607 uint32_t dex_method_idx, 608 PointerSize pointer_size) { 609 // FIXME: Hijacking a proxy class by a custom class loader can break this assumption. 610 DCHECK(!IsProxyClass()); 611 612 // First try to find a declared method by dex_method_idx if we have a dex_cache match. 613 ObjPtr<DexCache> this_dex_cache = GetDexCache(); 614 if (this_dex_cache == dex_cache) { 615 // Lookup is always performed in the class referenced by the MethodId. 616 DCHECK_EQ(dex_type_idx_, GetDexFile().GetMethodId(dex_method_idx).class_idx_.index_); 617 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) { 618 if (method.GetDexMethodIndex() == dex_method_idx) { 619 return &method; 620 } 621 } 622 } 623 // If not found, we need to search by name and signature. 624 const DexFile& dex_file = *dex_cache->GetDexFile(); 625 const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); 626 const Signature signature = dex_file.GetMethodSignature(method_id); 627 std::string_view name; // Delay strlen() until actually needed. 628 // If we do not have a dex_cache match, try to find the declared method in this class now. 629 if (this_dex_cache != dex_cache && !GetDeclaredMethodsSlice(pointer_size).empty()) { 630 DCHECK(name.empty()); 631 // Avoid string comparisons by comparing the respective unicode lengths first. 632 uint32_t length, other_length; // UTF16 length. 633 name = dex_file.GetMethodName(method_id, &length); 634 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) { 635 DCHECK_NE(method.GetDexMethodIndex(), dex::kDexNoIndex); 636 const char* other_name = method.GetDexFile()->GetMethodName( 637 method.GetDexMethodIndex(), &other_length); 638 if (length == other_length && name == other_name && signature == method.GetSignature()) { 639 return &method; 640 } 641 } 642 } 643 644 // Then search the superclass chain. If we find an inherited method, return it. 645 // If we find a method that's not inherited because of access restrictions, 646 // try to find a method inherited from an interface in copied methods. 647 ArtMethod* uninherited_method = nullptr; 648 ObjPtr<Class> klass = GetSuperClass(); 649 for (; klass != nullptr; klass = klass->GetSuperClass()) { 650 ArtMethod* candidate_method = nullptr; 651 ArraySlice<ArtMethod> declared_methods = klass->GetDeclaredMethodsSlice(pointer_size); 652 if (klass->GetDexCache() == dex_cache) { 653 // Matching dex_cache. We cannot compare the `dex_method_idx` anymore because 654 // the type index differs, so compare the name index and proto index. 655 for (ArtMethod& method : declared_methods) { 656 const dex::MethodId& cmp_method_id = dex_file.GetMethodId(method.GetDexMethodIndex()); 657 if (cmp_method_id.name_idx_ == method_id.name_idx_ && 658 cmp_method_id.proto_idx_ == method_id.proto_idx_) { 659 candidate_method = &method; 660 break; 661 } 662 } 663 } else { 664 if (!declared_methods.empty() && name.empty()) { 665 name = dex_file.StringDataByIdx(method_id.name_idx_); 666 } 667 for (ArtMethod& method : declared_methods) { 668 if (method.GetName() == name && method.GetSignature() == signature) { 669 candidate_method = &method; 670 break; 671 } 672 } 673 } 674 if (candidate_method != nullptr) { 675 if (IsInheritedMethod(this, klass, *candidate_method)) { 676 return candidate_method; 677 } else { 678 uninherited_method = candidate_method; 679 break; 680 } 681 } 682 } 683 684 // Then search copied methods. 685 // If we found a method that's not inherited, stop the search in its declaring class. 686 ObjPtr<Class> end_klass = klass; 687 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr); 688 // After we have searched the declared methods of the super-class chain, 689 // search copied methods which can contain methods from interfaces. 690 for (klass = this; klass != end_klass; klass = klass->GetSuperClass()) { 691 ArraySlice<ArtMethod> copied_methods = klass->GetCopiedMethodsSlice(pointer_size); 692 if (!copied_methods.empty() && name.empty()) { 693 name = dex_file.StringDataByIdx(method_id.name_idx_); 694 } 695 for (ArtMethod& method : copied_methods) { 696 if (method.GetName() == name && method.GetSignature() == signature) { 697 return &method; // No further check needed, copied methods are inherited by definition. 698 } 699 } 700 } 701 return uninherited_method; // Return the `uninherited_method` if any. 702 } 703 704 ArtMethod* Class::FindConstructor(std::string_view signature, PointerSize pointer_size) { 705 // Internal helper, never called on proxy classes. We can skip GetInterfaceMethodIfProxy(). 706 DCHECK(!IsProxyClass()); 707 std::string_view name("<init>"); 708 for (ArtMethod& method : GetDirectMethodsSliceUnchecked(pointer_size)) { 709 if (method.GetName() == name && method.GetSignature() == signature) { 710 return &method; 711 } 712 } 713 return nullptr; 714 } 715 716 ArtMethod* Class::FindDeclaredDirectMethodByName(std::string_view name, PointerSize pointer_size) { 717 for (auto& method : GetDirectMethods(pointer_size)) { 718 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size); 719 if (name == np_method->GetName()) { 720 return &method; 721 } 722 } 723 return nullptr; 724 } 725 726 ArtMethod* Class::FindDeclaredVirtualMethodByName(std::string_view name, PointerSize pointer_size) { 727 for (auto& method : GetVirtualMethods(pointer_size)) { 728 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size); 729 if (name == np_method->GetName()) { 730 return &method; 731 } 732 } 733 return nullptr; 734 } 735 736 ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) { 737 DCHECK(method->GetDeclaringClass()->IsInterface()); 738 DCHECK(IsInterface()) << "Should only be called on a interface class"; 739 // Check if we have one defined on this interface first. This includes searching copied ones to 740 // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We 741 // don't do any indirect method checks here. 742 for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) { 743 if (method->HasSameNameAndSignature(&iface_method)) { 744 return &iface_method; 745 } 746 } 747 748 std::vector<ArtMethod*> abstract_methods; 749 // Search through the IFTable for a working version. We don't need to check for conflicts 750 // because if there was one it would appear in this classes virtual_methods_ above. 751 752 Thread* self = Thread::Current(); 753 StackHandleScope<2> hs(self); 754 MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable())); 755 MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr)); 756 size_t iftable_count = GetIfTableCount(); 757 // Find the method. We don't need to check for conflicts because they would have been in the 758 // copied virtuals of this interface. Order matters, traverse in reverse topological order; most 759 // subtypiest interfaces get visited first. 760 for (size_t k = iftable_count; k != 0;) { 761 k--; 762 DCHECK_LT(k, iftable->Count()); 763 iface.Assign(iftable->GetInterface(k)); 764 // Iterate through every declared method on this interface. Each direct method's name/signature 765 // is unique so the order of the inner loop doesn't matter. 766 for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) { 767 ArtMethod* current_method = &method_iter; 768 if (current_method->HasSameNameAndSignature(method)) { 769 if (current_method->IsDefault()) { 770 // Handle JLS soft errors, a default method from another superinterface tree can 771 // "override" an abstract method(s) from another superinterface tree(s). To do this, 772 // ignore any [default] method which are dominated by the abstract methods we've seen so 773 // far. Check if overridden by any in abstract_methods. We do not need to check for 774 // default_conflicts because we would hit those before we get to this loop. 775 bool overridden = false; 776 for (ArtMethod* possible_override : abstract_methods) { 777 DCHECK(possible_override->HasSameNameAndSignature(current_method)); 778 if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) { 779 overridden = true; 780 break; 781 } 782 } 783 if (!overridden) { 784 return current_method; 785 } 786 } else { 787 // Is not default. 788 // This might override another default method. Just stash it for now. 789 abstract_methods.push_back(current_method); 790 } 791 } 792 } 793 } 794 // If we reach here we either never found any declaration of the method (in which case 795 // 'abstract_methods' is empty or we found no non-overriden default methods in which case 796 // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one 797 // of these arbitrarily. 798 return abstract_methods.empty() ? nullptr : abstract_methods[0]; 799 } 800 801 ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) { 802 for (ArtMethod& method : GetDirectMethods(pointer_size)) { 803 if (method.IsClassInitializer()) { 804 DCHECK_STREQ(method.GetName(), "<clinit>"); 805 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V"); 806 return &method; 807 } 808 } 809 return nullptr; 810 } 811 812 // Custom binary search to avoid double comparisons from std::binary_search. 813 static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields, 814 std::string_view name, 815 std::string_view type) 816 REQUIRES_SHARED(Locks::mutator_lock_) { 817 if (fields == nullptr) { 818 return nullptr; 819 } 820 size_t low = 0; 821 size_t high = fields->size(); 822 ArtField* ret = nullptr; 823 while (low < high) { 824 size_t mid = (low + high) / 2; 825 ArtField& field = fields->At(mid); 826 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file 827 // verifier. There can be multiple fields with the same in the same class name due to proguard. 828 // Note: std::string_view::compare() uses lexicographical comparison and treats the `char` as 829 // unsigned; for modified-UTF-8 without embedded nulls this is consistent with the 830 // CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues() ordering. 831 int result = std::string_view(field.GetName()).compare(name); 832 if (result == 0) { 833 result = std::string_view(field.GetTypeDescriptor()).compare(type); 834 } 835 if (result < 0) { 836 low = mid + 1; 837 } else if (result > 0) { 838 high = mid; 839 } else { 840 ret = &field; 841 break; 842 } 843 } 844 if (kIsDebugBuild) { 845 ArtField* found = nullptr; 846 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) { 847 if (name == field.GetName() && type == field.GetTypeDescriptor()) { 848 found = &field; 849 break; 850 } 851 } 852 CHECK_EQ(found, ret) << "Found " << found->PrettyField() << " vs " << ret->PrettyField(); 853 } 854 return ret; 855 } 856 857 ArtField* Class::FindDeclaredInstanceField(std::string_view name, std::string_view type) { 858 // Binary search by name. Interfaces are not relevant because they can't contain instance fields. 859 return FindFieldByNameAndType(GetIFieldsPtr(), name, type); 860 } 861 862 ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) { 863 if (GetDexCache() == dex_cache) { 864 for (ArtField& field : GetIFields()) { 865 if (field.GetDexFieldIndex() == dex_field_idx) { 866 return &field; 867 } 868 } 869 } 870 return nullptr; 871 } 872 873 ArtField* Class::FindInstanceField(std::string_view name, std::string_view type) { 874 // Is the field in this class, or any of its superclasses? 875 // Interfaces are not relevant because they can't contain instance fields. 876 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) { 877 ArtField* f = c->FindDeclaredInstanceField(name, type); 878 if (f != nullptr) { 879 return f; 880 } 881 } 882 return nullptr; 883 } 884 885 ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) { 886 // Is the field in this class, or any of its superclasses? 887 // Interfaces are not relevant because they can't contain instance fields. 888 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) { 889 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx); 890 if (f != nullptr) { 891 return f; 892 } 893 } 894 return nullptr; 895 } 896 897 ArtField* Class::FindDeclaredStaticField(std::string_view name, std::string_view type) { 898 DCHECK(!type.empty()); 899 return FindFieldByNameAndType(GetSFieldsPtr(), name, type); 900 } 901 902 ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) { 903 if (dex_cache == GetDexCache()) { 904 for (ArtField& field : GetSFields()) { 905 if (field.GetDexFieldIndex() == dex_field_idx) { 906 return &field; 907 } 908 } 909 } 910 return nullptr; 911 } 912 913 ArtField* Class::FindStaticField(Thread* self, 914 ObjPtr<Class> klass, 915 std::string_view name, 916 std::string_view type) { 917 // Is the field in this class (or its interfaces), or any of its 918 // superclasses (or their interfaces)? 919 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) { 920 // Is the field in this class? 921 ArtField* f = k->FindDeclaredStaticField(name, type); 922 if (f != nullptr) { 923 return f; 924 } 925 // Is this field in any of this class' interfaces? 926 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) { 927 ObjPtr<Class> interface = GetDirectInterface(self, k, i); 928 DCHECK(interface != nullptr); 929 f = FindStaticField(self, interface, name, type); 930 if (f != nullptr) { 931 return f; 932 } 933 } 934 } 935 return nullptr; 936 } 937 938 ArtField* Class::FindStaticField(Thread* self, 939 ObjPtr<Class> klass, 940 ObjPtr<DexCache> dex_cache, 941 uint32_t dex_field_idx) { 942 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) { 943 // Is the field in this class? 944 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx); 945 if (f != nullptr) { 946 return f; 947 } 948 // Though GetDirectInterface() should not cause thread suspension when called 949 // from here, it takes a Handle as an argument, so we need to wrap `k`. 950 ScopedAssertNoThreadSuspension ants(__FUNCTION__); 951 // Is this field in any of this class' interfaces? 952 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) { 953 ObjPtr<Class> interface = GetDirectInterface(self, k, i); 954 DCHECK(interface != nullptr); 955 f = FindStaticField(self, interface, dex_cache, dex_field_idx); 956 if (f != nullptr) { 957 return f; 958 } 959 } 960 } 961 return nullptr; 962 } 963 964 ArtField* Class::FindField(Thread* self, 965 ObjPtr<Class> klass, 966 std::string_view name, 967 std::string_view type) { 968 // Find a field using the JLS field resolution order 969 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) { 970 // Is the field in this class? 971 ArtField* f = k->FindDeclaredInstanceField(name, type); 972 if (f != nullptr) { 973 return f; 974 } 975 f = k->FindDeclaredStaticField(name, type); 976 if (f != nullptr) { 977 return f; 978 } 979 // Is this field in any of this class' interfaces? 980 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) { 981 ObjPtr<Class> interface = GetDirectInterface(self, k, i); 982 DCHECK(interface != nullptr); 983 f = FindStaticField(self, interface, name, type); 984 if (f != nullptr) { 985 return f; 986 } 987 } 988 } 989 return nullptr; 990 } 991 992 void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) { 993 DCHECK(IsVerified()); 994 for (auto& m : GetMethods(pointer_size)) { 995 if (!m.IsNative() && m.IsInvokable()) { 996 m.SetSkipAccessChecks(); 997 } 998 } 999 } 1000 1001 const char* Class::GetDescriptor(std::string* storage) { 1002 size_t dim = 0u; 1003 ObjPtr<mirror::Class> klass = this; 1004 while (klass->IsArrayClass()) { 1005 ++dim; 1006 // No read barrier needed, we're reading a chain of constant references for comparison 1007 // with null. Then we follow up below with reading constant references to read constant 1008 // primitive data in both proxy and non-proxy paths. See ReadBarrierOption. 1009 klass = klass->GetComponentType<kDefaultVerifyFlags, kWithoutReadBarrier>(); 1010 } 1011 if (klass->IsProxyClass()) { 1012 // No read barrier needed, the `name` field is constant for proxy classes and 1013 // the contents of the String are also constant. See ReadBarrierOption. 1014 ObjPtr<mirror::String> name = klass->GetName<kVerifyNone, kWithoutReadBarrier>(); 1015 DCHECK(name != nullptr); 1016 *storage = DotToDescriptor(name->ToModifiedUtf8().c_str()); 1017 } else { 1018 const char* descriptor; 1019 if (klass->IsPrimitive()) { 1020 descriptor = Primitive::Descriptor(klass->GetPrimitiveType()); 1021 } else { 1022 const DexFile& dex_file = klass->GetDexFile(); 1023 const dex::TypeId& type_id = dex_file.GetTypeId(klass->GetDexTypeIndex()); 1024 descriptor = dex_file.GetTypeDescriptor(type_id); 1025 } 1026 if (dim == 0) { 1027 return descriptor; 1028 } 1029 *storage = descriptor; 1030 } 1031 storage->insert(0u, dim, '['); 1032 return storage->c_str(); 1033 } 1034 1035 const dex::ClassDef* Class::GetClassDef() { 1036 uint16_t class_def_idx = GetDexClassDefIndex(); 1037 if (class_def_idx == DexFile::kDexNoIndex16) { 1038 return nullptr; 1039 } 1040 return &GetDexFile().GetClassDef(class_def_idx); 1041 } 1042 1043 dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) { 1044 DCHECK(!IsPrimitive()); 1045 DCHECK(!IsArrayClass()); 1046 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_; 1047 } 1048 1049 ObjPtr<Class> Class::GetDirectInterface(Thread* self, ObjPtr<Class> klass, uint32_t idx) { 1050 DCHECK(klass != nullptr); 1051 DCHECK(!klass->IsPrimitive()); 1052 if (klass->IsArrayClass()) { 1053 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1054 // Use ClassLinker::LookupClass(); avoid poisoning ObjPtr<>s by ClassLinker::FindSystemClass(). 1055 ObjPtr<Class> interface; 1056 if (idx == 0) { 1057 interface = class_linker->LookupClass(self, "Ljava/lang/Cloneable;", nullptr); 1058 } else { 1059 DCHECK_EQ(1U, idx); 1060 interface = class_linker->LookupClass(self, "Ljava/io/Serializable;", nullptr); 1061 } 1062 DCHECK(interface != nullptr); 1063 return interface; 1064 } else if (klass->IsProxyClass()) { 1065 ObjPtr<ObjectArray<Class>> interfaces = klass->GetProxyInterfaces(); 1066 DCHECK(interfaces != nullptr); 1067 return interfaces->Get(idx); 1068 } else { 1069 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx); 1070 ObjPtr<Class> interface = Runtime::Current()->GetClassLinker()->LookupResolvedType( 1071 type_idx, klass->GetDexCache(), klass->GetClassLoader()); 1072 return interface; 1073 } 1074 } 1075 1076 ObjPtr<Class> Class::ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) { 1077 ObjPtr<Class> interface = GetDirectInterface(self, klass.Get(), idx); 1078 if (interface == nullptr) { 1079 DCHECK(!klass->IsArrayClass()); 1080 DCHECK(!klass->IsProxyClass()); 1081 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx); 1082 interface = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, klass.Get()); 1083 CHECK(interface != nullptr || self->IsExceptionPending()); 1084 } 1085 return interface; 1086 } 1087 1088 ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) { 1089 DCHECK(klass != nullptr); 1090 DCHECK(!klass->IsInterface()); 1091 DCHECK(!IsInterface()); 1092 ObjPtr<Class> common_super_class = this; 1093 while (!common_super_class->IsAssignableFrom(klass.Get())) { 1094 ObjPtr<Class> old_common = common_super_class; 1095 common_super_class = old_common->GetSuperClass(); 1096 DCHECK(common_super_class != nullptr) << old_common->PrettyClass(); 1097 } 1098 return common_super_class; 1099 } 1100 1101 const char* Class::GetSourceFile() { 1102 const DexFile& dex_file = GetDexFile(); 1103 const dex::ClassDef* dex_class_def = GetClassDef(); 1104 if (dex_class_def == nullptr) { 1105 // Generated classes have no class def. 1106 return nullptr; 1107 } 1108 return dex_file.GetSourceFile(*dex_class_def); 1109 } 1110 1111 std::string Class::GetLocation() { 1112 ObjPtr<DexCache> dex_cache = GetDexCache(); 1113 if (dex_cache != nullptr && !IsProxyClass()) { 1114 return dex_cache->GetLocation()->ToModifiedUtf8(); 1115 } 1116 // Arrays and proxies are generated and have no corresponding dex file location. 1117 return "generated class"; 1118 } 1119 1120 const dex::TypeList* Class::GetInterfaceTypeList() { 1121 const dex::ClassDef* class_def = GetClassDef(); 1122 if (class_def == nullptr) { 1123 return nullptr; 1124 } 1125 return GetDexFile().GetInterfacesList(*class_def); 1126 } 1127 1128 void Class::PopulateEmbeddedVTable(PointerSize pointer_size) { 1129 ObjPtr<PointerArray> table = GetVTableDuringLinking(); 1130 CHECK(table != nullptr) << PrettyClass(); 1131 const size_t table_length = table->GetLength(); 1132 SetEmbeddedVTableLength(table_length); 1133 for (size_t i = 0; i < table_length; i++) { 1134 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size); 1135 } 1136 // Keep java.lang.Object class's vtable around for since it's easier 1137 // to be reused by array classes during their linking. 1138 if (!IsObjectClass()) { 1139 SetVTable(nullptr); 1140 } 1141 } 1142 1143 class ReadBarrierOnNativeRootsVisitor { 1144 public: 1145 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED, 1146 MemberOffset offset ATTRIBUTE_UNUSED, 1147 bool is_static ATTRIBUTE_UNUSED) const {} 1148 1149 void VisitRootIfNonNull(CompressedReference<Object>* root) const 1150 REQUIRES_SHARED(Locks::mutator_lock_) { 1151 if (!root->IsNull()) { 1152 VisitRoot(root); 1153 } 1154 } 1155 1156 void VisitRoot(CompressedReference<Object>* root) const 1157 REQUIRES_SHARED(Locks::mutator_lock_) { 1158 ObjPtr<Object> old_ref = root->AsMirrorPtr(); 1159 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root); 1160 if (old_ref != new_ref) { 1161 // Update the field atomically. This may fail if mutator updates before us, but it's ok. 1162 auto* atomic_root = 1163 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root); 1164 atomic_root->CompareAndSetStrongSequentiallyConsistent( 1165 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()), 1166 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr())); 1167 } 1168 } 1169 }; 1170 1171 // The pre-fence visitor for Class::CopyOf(). 1172 class CopyClassVisitor { 1173 public: 1174 CopyClassVisitor(Thread* self, 1175 Handle<Class>* orig, 1176 size_t new_length, 1177 size_t copy_bytes, 1178 ImTable* imt, 1179 PointerSize pointer_size) 1180 : self_(self), orig_(orig), new_length_(new_length), 1181 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) { 1182 } 1183 1184 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const 1185 REQUIRES_SHARED(Locks::mutator_lock_) { 1186 StackHandleScope<1> hs(self_); 1187 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass())); 1188 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_); 1189 Class::SetStatus(h_new_class_obj, ClassStatus::kResolving, self_); 1190 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_); 1191 h_new_class_obj->SetImt(imt_, pointer_size_); 1192 h_new_class_obj->SetClassSize(new_length_); 1193 // Visit all of the references to make sure there is no from space references in the native 1194 // roots. 1195 h_new_class_obj->Object::VisitReferences(ReadBarrierOnNativeRootsVisitor(), VoidFunctor()); 1196 } 1197 1198 private: 1199 Thread* const self_; 1200 Handle<Class>* const orig_; 1201 const size_t new_length_; 1202 const size_t copy_bytes_; 1203 ImTable* imt_; 1204 const PointerSize pointer_size_; 1205 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor); 1206 }; 1207 1208 ObjPtr<Class> Class::CopyOf( 1209 Thread* self, int32_t new_length, ImTable* imt, PointerSize pointer_size) { 1210 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class))); 1211 // We may get copied by a compacting GC. 1212 StackHandleScope<1> hs(self); 1213 Handle<Class> h_this(hs.NewHandle(this)); 1214 Runtime* runtime = Runtime::Current(); 1215 gc::Heap* heap = runtime->GetHeap(); 1216 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf() 1217 // to skip copying the tail part that we will overwrite here. 1218 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size); 1219 ObjPtr<mirror::Class> java_lang_Class = GetClassRoot<mirror::Class>(runtime->GetClassLinker()); 1220 ObjPtr<Object> new_class = kMovingClasses ? 1221 heap->AllocObject<true>(self, java_lang_Class, new_length, visitor) : 1222 heap->AllocNonMovableObject<true>(self, java_lang_Class, new_length, visitor); 1223 if (UNLIKELY(new_class == nullptr)) { 1224 self->AssertPendingOOMException(); 1225 return nullptr; 1226 } 1227 return new_class->AsClass(); 1228 } 1229 1230 bool Class::ProxyDescriptorEquals(const char* match) { 1231 DCHECK(IsProxyClass()); 1232 std::string storage; 1233 const char* descriptor = GetDescriptor(&storage); 1234 DCHECK(descriptor == storage.c_str()); 1235 return storage == match; 1236 } 1237 1238 // TODO: Move this to java_lang_Class.cc? 1239 ArtMethod* Class::GetDeclaredConstructor( 1240 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) { 1241 for (auto& m : GetDirectMethods(pointer_size)) { 1242 // Skip <clinit> which is a static constructor, as well as non constructors. 1243 if (m.IsStatic() || !m.IsConstructor()) { 1244 continue; 1245 } 1246 // May cause thread suspension and exceptions. 1247 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) { 1248 return &m; 1249 } 1250 if (UNLIKELY(self->IsExceptionPending())) { 1251 return nullptr; 1252 } 1253 } 1254 return nullptr; 1255 } 1256 1257 uint32_t Class::Depth() { 1258 uint32_t depth = 0; 1259 for (ObjPtr<Class> cls = this; cls->GetSuperClass() != nullptr; cls = cls->GetSuperClass()) { 1260 depth++; 1261 } 1262 return depth; 1263 } 1264 1265 dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) { 1266 std::string temp; 1267 const dex::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp)); 1268 return (type_id == nullptr) ? dex::TypeIndex() : dex_file.GetIndexForTypeId(*type_id); 1269 } 1270 1271 ALWAYS_INLINE 1272 static bool IsMethodPreferredOver(ArtMethod* orig_method, 1273 bool orig_method_hidden, 1274 ArtMethod* new_method, 1275 bool new_method_hidden) { 1276 DCHECK(new_method != nullptr); 1277 1278 // Is this the first result? 1279 if (orig_method == nullptr) { 1280 return true; 1281 } 1282 1283 // Original method is hidden, the new one is not? 1284 if (orig_method_hidden && !new_method_hidden) { 1285 return true; 1286 } 1287 1288 // We iterate over virtual methods first and then over direct ones, 1289 // so we can never be in situation where `orig_method` is direct and 1290 // `new_method` is virtual. 1291 DCHECK(!orig_method->IsDirect() || new_method->IsDirect()); 1292 1293 // Original method is synthetic, the new one is not? 1294 if (orig_method->IsSynthetic() && !new_method->IsSynthetic()) { 1295 return true; 1296 } 1297 1298 return false; 1299 } 1300 1301 template <PointerSize kPointerSize, bool kTransactionActive> 1302 ObjPtr<Method> Class::GetDeclaredMethodInternal( 1303 Thread* self, 1304 ObjPtr<Class> klass, 1305 ObjPtr<String> name, 1306 ObjPtr<ObjectArray<Class>> args, 1307 const std::function<hiddenapi::AccessContext()>& fn_get_access_context) { 1308 // Covariant return types (or smali) permit the class to define 1309 // multiple methods with the same name and parameter types. 1310 // Prefer (in decreasing order of importance): 1311 // 1) non-hidden method over hidden 1312 // 2) virtual methods over direct 1313 // 3) non-synthetic methods over synthetic 1314 // We never return miranda methods that were synthesized by the runtime. 1315 StackHandleScope<3> hs(self); 1316 auto h_method_name = hs.NewHandle(name); 1317 if (UNLIKELY(h_method_name == nullptr)) { 1318 ThrowNullPointerException("name == null"); 1319 return nullptr; 1320 } 1321 auto h_args = hs.NewHandle(args); 1322 Handle<Class> h_klass = hs.NewHandle(klass); 1323 constexpr hiddenapi::AccessMethod access_method = hiddenapi::AccessMethod::kNone; 1324 ArtMethod* result = nullptr; 1325 bool result_hidden = false; 1326 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) { 1327 if (m.IsMiranda()) { 1328 continue; 1329 } 1330 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize); 1331 // May cause thread suspension. 1332 ObjPtr<String> np_name = np_method->ResolveNameString(); 1333 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) { 1334 if (UNLIKELY(self->IsExceptionPending())) { 1335 return nullptr; 1336 } 1337 continue; 1338 } 1339 bool m_hidden = hiddenapi::ShouldDenyAccessToMember(&m, fn_get_access_context, access_method); 1340 if (!m_hidden && !m.IsSynthetic()) { 1341 // Non-hidden, virtual, non-synthetic. Best possible result, exit early. 1342 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m); 1343 } else if (IsMethodPreferredOver(result, result_hidden, &m, m_hidden)) { 1344 // Remember as potential result. 1345 result = &m; 1346 result_hidden = m_hidden; 1347 } 1348 } 1349 1350 if ((result != nullptr) && !result_hidden) { 1351 // We have not found a non-hidden, virtual, non-synthetic method, but 1352 // if we have found a non-hidden, virtual, synthetic method, we cannot 1353 // do better than that later. 1354 DCHECK(!result->IsDirect()); 1355 DCHECK(result->IsSynthetic()); 1356 } else { 1357 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) { 1358 auto modifiers = m.GetAccessFlags(); 1359 if ((modifiers & kAccConstructor) != 0) { 1360 continue; 1361 } 1362 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize); 1363 // May cause thread suspension. 1364 ObjPtr<String> np_name = np_method->ResolveNameString(); 1365 if (np_name == nullptr) { 1366 self->AssertPendingException(); 1367 return nullptr; 1368 } 1369 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) { 1370 if (UNLIKELY(self->IsExceptionPending())) { 1371 return nullptr; 1372 } 1373 continue; 1374 } 1375 DCHECK(!m.IsMiranda()); // Direct methods cannot be miranda methods. 1376 bool m_hidden = hiddenapi::ShouldDenyAccessToMember(&m, fn_get_access_context, access_method); 1377 if (!m_hidden && !m.IsSynthetic()) { 1378 // Non-hidden, direct, non-synthetic. Any virtual result could only have been 1379 // hidden, therefore this is the best possible match. Exit now. 1380 DCHECK((result == nullptr) || result_hidden); 1381 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m); 1382 } else if (IsMethodPreferredOver(result, result_hidden, &m, m_hidden)) { 1383 // Remember as potential result. 1384 result = &m; 1385 result_hidden = m_hidden; 1386 } 1387 } 1388 } 1389 1390 return result != nullptr 1391 ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result) 1392 : nullptr; 1393 } 1394 1395 template 1396 ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>( 1397 Thread* self, 1398 ObjPtr<Class> klass, 1399 ObjPtr<String> name, 1400 ObjPtr<ObjectArray<Class>> args, 1401 const std::function<hiddenapi::AccessContext()>& fn_get_access_context); 1402 template 1403 ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>( 1404 Thread* self, 1405 ObjPtr<Class> klass, 1406 ObjPtr<String> name, 1407 ObjPtr<ObjectArray<Class>> args, 1408 const std::function<hiddenapi::AccessContext()>& fn_get_access_context); 1409 template 1410 ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, false>( 1411 Thread* self, 1412 ObjPtr<Class> klass, 1413 ObjPtr<String> name, 1414 ObjPtr<ObjectArray<Class>> args, 1415 const std::function<hiddenapi::AccessContext()>& fn_get_access_context); 1416 template 1417 ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>( 1418 Thread* self, 1419 ObjPtr<Class> klass, 1420 ObjPtr<String> name, 1421 ObjPtr<ObjectArray<Class>> args, 1422 const std::function<hiddenapi::AccessContext()>& fn_get_access_context); 1423 1424 template <PointerSize kPointerSize, bool kTransactionActive> 1425 ObjPtr<Constructor> Class::GetDeclaredConstructorInternal( 1426 Thread* self, 1427 ObjPtr<Class> klass, 1428 ObjPtr<ObjectArray<Class>> args) { 1429 StackHandleScope<1> hs(self); 1430 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize); 1431 return result != nullptr 1432 ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result) 1433 : nullptr; 1434 } 1435 1436 // Constructor::CreateFromArtMethod<kTransactionActive>(self, result) 1437 1438 template 1439 ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>( 1440 Thread* self, 1441 ObjPtr<Class> klass, 1442 ObjPtr<ObjectArray<Class>> args); 1443 template 1444 ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, true>( 1445 Thread* self, 1446 ObjPtr<Class> klass, 1447 ObjPtr<ObjectArray<Class>> args); 1448 template 1449 ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>( 1450 Thread* self, 1451 ObjPtr<Class> klass, 1452 ObjPtr<ObjectArray<Class>> args); 1453 template 1454 ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>( 1455 Thread* self, 1456 ObjPtr<Class> klass, 1457 ObjPtr<ObjectArray<Class>> args); 1458 1459 int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) { 1460 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) { 1461 return default_value; 1462 } 1463 uint32_t flags; 1464 if (!annotations::GetInnerClassFlags(h_this, &flags)) { 1465 return default_value; 1466 } 1467 return flags; 1468 } 1469 1470 void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) { 1471 if (Runtime::Current()->IsActiveTransaction()) { 1472 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size); 1473 } else { 1474 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size); 1475 } 1476 } 1477 1478 std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) { 1479 if (klass == nullptr) { 1480 return "null"; 1481 } 1482 return klass->PrettyDescriptor(); 1483 } 1484 1485 std::string Class::PrettyDescriptor() { 1486 std::string temp; 1487 return art::PrettyDescriptor(GetDescriptor(&temp)); 1488 } 1489 1490 std::string Class::PrettyClass(ObjPtr<mirror::Class> c) { 1491 if (c == nullptr) { 1492 return "null"; 1493 } 1494 return c->PrettyClass(); 1495 } 1496 1497 std::string Class::PrettyClass() { 1498 std::string result; 1499 result += "java.lang.Class<"; 1500 result += PrettyDescriptor(); 1501 result += ">"; 1502 return result; 1503 } 1504 1505 std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) { 1506 if (c == nullptr) { 1507 return "null"; 1508 } 1509 return c->PrettyClassAndClassLoader(); 1510 } 1511 1512 std::string Class::PrettyClassAndClassLoader() { 1513 std::string result; 1514 result += "java.lang.Class<"; 1515 result += PrettyDescriptor(); 1516 result += ","; 1517 result += mirror::Object::PrettyTypeOf(GetClassLoader()); 1518 // TODO: add an identifying hash value for the loader 1519 result += ">"; 1520 return result; 1521 } 1522 1523 template<VerifyObjectFlags kVerifyFlags> void Class::GetAccessFlagsDCheck() { 1524 // Check class is loaded/retired or this is java.lang.String that has a 1525 // circularity issue during loading the names of its members 1526 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() || 1527 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() || 1528 this == GetClassRoot<String>()) 1529 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>() 1530 << " IsRetired=" << IsRetired<kVerifyFlags>() 1531 << " IsErroneous=" << 1532 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() 1533 << " IsString=" << (this == GetClassRoot<String>()) 1534 << " status= " << GetStatus<kVerifyFlags>() 1535 << " descriptor=" << PrettyDescriptor(); 1536 } 1537 // Instantiate the common cases. 1538 template void Class::GetAccessFlagsDCheck<kVerifyNone>(); 1539 template void Class::GetAccessFlagsDCheck<kVerifyThis>(); 1540 template void Class::GetAccessFlagsDCheck<kVerifyReads>(); 1541 template void Class::GetAccessFlagsDCheck<kVerifyWrites>(); 1542 template void Class::GetAccessFlagsDCheck<kVerifyAll>(); 1543 1544 void Class::SetAccessFlagsDCheck(uint32_t new_access_flags) { 1545 uint32_t old_access_flags = GetField32<kVerifyNone>(AccessFlagsOffset()); 1546 // kAccVerificationAttempted is retained. 1547 CHECK((old_access_flags & kAccVerificationAttempted) == 0 || 1548 (new_access_flags & kAccVerificationAttempted) != 0); 1549 } 1550 1551 } // namespace mirror 1552 } // namespace art 1553