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 #ifndef ART_RUNTIME_MIRROR_CLASS_H_ 18 #define ART_RUNTIME_MIRROR_CLASS_H_ 19 20 #include <string_view> 21 22 #include "base/bit_utils.h" 23 #include "base/casts.h" 24 #include "class_flags.h" 25 #include "class_status.h" 26 #include "dex/dex_file_types.h" 27 #include "dex/modifiers.h" 28 #include "dex/primitive.h" 29 #include "object.h" 30 #include "object_array.h" 31 #include "read_barrier_option.h" 32 33 namespace art HIDDEN { 34 35 namespace dex { 36 struct ClassDef; 37 class TypeList; 38 } // namespace dex 39 40 namespace gc { 41 enum AllocatorType : char; 42 } // namespace gc 43 44 namespace hiddenapi { 45 class AccessContext; 46 } // namespace hiddenapi 47 48 namespace linker { 49 class ImageWriter; 50 } // namespace linker 51 52 template<typename T> class ArraySlice; 53 class ArtField; 54 class ArtMethod; 55 struct ClassOffsets; 56 class DexFile; 57 template<class T> class Handle; 58 class ImTable; 59 enum InvokeType : uint32_t; 60 template <typename Iter> class IterationRange; 61 template<typename T> class LengthPrefixedArray; 62 enum class PointerSize : uint32_t; 63 class Signature; 64 template<typename T> class StrideIterator; 65 template<size_t kNumReferences> class PACKED(4) StackHandleScope; 66 class Thread; 67 class DexCacheVisitor; 68 class RuntimeImageHelper; 69 70 namespace mirror { 71 72 class ClassExt; 73 class ClassLoader; 74 class Constructor; 75 class DexCache; 76 class Field; 77 class IfTable; 78 class Method; 79 template <typename T> struct alignas(8) DexCachePair; 80 81 // C++ mirror of java.lang.Class 82 class EXPORT MANAGED Class final : public Object { 83 public: 84 MIRROR_CLASS("Ljava/lang/Class;"); 85 86 // 'reference_instance_offsets_' may contain up to 31 reference offsets. If 87 // more bits are required, then we set the most-significant bit and store the 88 // number of 32-bit bitmap entries required in the remaining bits. All the 89 // required bitmap entries after stored after static fields (at the end of the class). 90 static constexpr uint32_t kVisitReferencesSlowpathShift = 31; 91 static constexpr uint32_t kVisitReferencesSlowpathMask = 1u << kVisitReferencesSlowpathShift; 92 93 // Shift primitive type by kPrimitiveTypeSizeShiftShift to get the component type size shift 94 // Used for computing array size as follows: 95 // array_bytes = header_size + (elements << (primitive_type >> kPrimitiveTypeSizeShiftShift)) 96 static constexpr uint32_t kPrimitiveTypeSizeShiftShift = 16; 97 static constexpr uint32_t kPrimitiveTypeMask = (1u << kPrimitiveTypeSizeShiftShift) - 1; 98 99 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 100 bool kWithSynchronizationBarrier = true> GetStatus()101 ClassStatus GetStatus() REQUIRES_SHARED(Locks::mutator_lock_) { 102 // Reading the field without barrier is used exclusively for IsVisiblyInitialized(). 103 int32_t field_value = kWithSynchronizationBarrier 104 ? GetField32Volatile<kVerifyFlags>(StatusOffset()) 105 : GetField32<kVerifyFlags>(StatusOffset()); 106 // Avoid including "subtype_check_bits_and_status.h" to get the field. 107 // The ClassStatus is always in the 4 most-significant bits of status_. 108 return enum_cast<ClassStatus>(static_cast<uint32_t>(field_value) >> (32 - 4)); 109 } 110 111 // This is static because 'this' may be moved by GC. 112 static void SetStatus(Handle<Class> h_this, ClassStatus new_status, Thread* self) 113 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 114 115 // Used for structural redefinition to directly set the class-status while 116 // holding a strong mutator-lock. 117 void SetStatusLocked(ClassStatus new_status) REQUIRES(Locks::mutator_lock_); 118 119 void SetStatusForPrimitiveOrArray(ClassStatus new_status) REQUIRES_SHARED(Locks::mutator_lock_); 120 StatusOffset()121 static constexpr MemberOffset StatusOffset() { 122 return MemberOffset(OFFSET_OF_OBJECT_MEMBER(Class, status_)); 123 } 124 125 // Returns true if the class has been retired. 126 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsRetired()127 bool IsRetired() REQUIRES_SHARED(Locks::mutator_lock_) { 128 return GetStatus<kVerifyFlags>() == ClassStatus::kRetired; 129 } 130 131 // Returns true if the class has failed to link. 132 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsErroneousUnresolved()133 bool IsErroneousUnresolved() REQUIRES_SHARED(Locks::mutator_lock_) { 134 return GetStatus<kVerifyFlags>() == ClassStatus::kErrorUnresolved; 135 } 136 137 // Returns true if the class has failed to initialize. 138 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsErroneousResolved()139 bool IsErroneousResolved() REQUIRES_SHARED(Locks::mutator_lock_) { 140 return GetStatus<kVerifyFlags>() == ClassStatus::kErrorResolved; 141 } 142 143 // Returns true if the class status indicets that the class has failed to link or initialize. IsErroneous(ClassStatus status)144 static bool IsErroneous(ClassStatus status) { 145 return status == ClassStatus::kErrorUnresolved || status == ClassStatus::kErrorResolved; 146 } 147 148 // Returns true if the class has failed to link or initialize. 149 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsErroneous()150 bool IsErroneous() REQUIRES_SHARED(Locks::mutator_lock_) { 151 return IsErroneous(GetStatus<kVerifyFlags>()); 152 } 153 154 // Returns true if the class has been loaded. 155 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsIdxLoaded()156 bool IsIdxLoaded() REQUIRES_SHARED(Locks::mutator_lock_) { 157 return GetStatus<kVerifyFlags>() >= ClassStatus::kIdx; 158 } 159 160 // Returns true if the class has been loaded. 161 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsLoaded()162 bool IsLoaded() REQUIRES_SHARED(Locks::mutator_lock_) { 163 return GetStatus<kVerifyFlags>() >= ClassStatus::kLoaded; 164 } 165 166 // Returns true if the class has been linked. 167 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsResolved()168 bool IsResolved() REQUIRES_SHARED(Locks::mutator_lock_) { 169 ClassStatus status = GetStatus<kVerifyFlags>(); 170 return status >= ClassStatus::kResolved || status == ClassStatus::kErrorResolved; 171 } 172 173 // Returns true if the class should be verified at runtime. 174 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> ShouldVerifyAtRuntime()175 bool ShouldVerifyAtRuntime() REQUIRES_SHARED(Locks::mutator_lock_) { 176 return GetStatus<kVerifyFlags>() == ClassStatus::kRetryVerificationAtRuntime; 177 } 178 179 // Returns true if the class has been verified at compile time, but should be 180 // executed with access checks. 181 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsVerifiedNeedsAccessChecks()182 bool IsVerifiedNeedsAccessChecks() REQUIRES_SHARED(Locks::mutator_lock_) { 183 return GetStatus<kVerifyFlags>() == ClassStatus::kVerifiedNeedsAccessChecks; 184 } 185 186 // Returns true if the class has been verified. 187 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsVerified()188 bool IsVerified() REQUIRES_SHARED(Locks::mutator_lock_) { 189 return GetStatus<kVerifyFlags>() >= ClassStatus::kVerified; 190 } 191 192 // Returns true if the class is initializing. 193 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsInitializing()194 bool IsInitializing() REQUIRES_SHARED(Locks::mutator_lock_) { 195 return GetStatus<kVerifyFlags>() >= ClassStatus::kInitializing; 196 } 197 198 // Returns true if the class is initialized. 199 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsInitialized()200 bool IsInitialized() REQUIRES_SHARED(Locks::mutator_lock_) { 201 return GetStatus<kVerifyFlags>() >= ClassStatus::kInitialized; 202 } 203 204 // Returns true if the class is visibly initialized. 205 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsVisiblyInitialized()206 bool IsVisiblyInitialized() REQUIRES_SHARED(Locks::mutator_lock_) { 207 // Note: Avoiding the synchronization barrier for the visibly initialized check. 208 ClassStatus status = GetStatus<kVerifyFlags, /*kWithSynchronizationBarrier=*/ false>(); 209 210 // We need to prevent the compiler from reordering loads that depend 211 // on the class being visibly initialized before the status load above. 212 asm volatile ("" : : : "memory"); 213 214 return status == ClassStatus::kVisiblyInitialized; 215 } 216 217 // Returns true if this class is ever accessed through a C++ mirror. 218 bool IsMirrored() REQUIRES_SHARED(Locks::mutator_lock_); 219 220 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> GetAccessFlags()221 ALWAYS_INLINE uint32_t GetAccessFlags() REQUIRES_SHARED(Locks::mutator_lock_) { 222 if (kIsDebugBuild) { 223 GetAccessFlagsDCheck<kVerifyFlags>(); 224 } 225 return GetField32<kVerifyFlags>(AccessFlagsOffset()); 226 } 227 AccessFlagsOffset()228 static constexpr MemberOffset AccessFlagsOffset() { 229 return OFFSET_OF_OBJECT_MEMBER(Class, access_flags_); 230 } 231 232 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> GetClassFlags()233 ALWAYS_INLINE uint32_t GetClassFlags() REQUIRES_SHARED(Locks::mutator_lock_) { 234 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, class_flags_)); 235 } 236 void SetClassFlags(uint32_t new_flags) REQUIRES_SHARED(Locks::mutator_lock_); 237 238 // Set access flags during linking, these cannot be rolled back by a Transaction. 239 void SetAccessFlagsDuringLinking(uint32_t new_access_flags) REQUIRES_SHARED(Locks::mutator_lock_); 240 241 // Set access flags, recording the change if running inside a Transaction. 242 void SetAccessFlags(uint32_t new_access_flags) REQUIRES_SHARED(Locks::mutator_lock_); 243 SetInBootImageAndNotInPreloadedClasses()244 void SetInBootImageAndNotInPreloadedClasses() REQUIRES_SHARED(Locks::mutator_lock_) { 245 uint32_t flags = GetAccessFlags(); 246 SetAccessFlags(flags | kAccInBootImageAndNotInPreloadedClasses); 247 } 248 IsInBootImageAndNotInPreloadedClasses()249 ALWAYS_INLINE bool IsInBootImageAndNotInPreloadedClasses() REQUIRES_SHARED(Locks::mutator_lock_) { 250 return (GetAccessFlags() & kAccInBootImageAndNotInPreloadedClasses) != 0; 251 } 252 253 // Returns true if the class is an enum. IsEnum()254 ALWAYS_INLINE bool IsEnum() REQUIRES_SHARED(Locks::mutator_lock_) { 255 return (GetAccessFlags() & kAccEnum) != 0; 256 } 257 258 // Returns true if the class is an interface. 259 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsInterface()260 ALWAYS_INLINE bool IsInterface() REQUIRES_SHARED(Locks::mutator_lock_) { 261 return (GetAccessFlags<kVerifyFlags>() & kAccInterface) != 0; 262 } 263 264 // Returns true if the class is declared public. IsPublic()265 ALWAYS_INLINE bool IsPublic() REQUIRES_SHARED(Locks::mutator_lock_) { 266 return (GetAccessFlags() & kAccPublic) != 0; 267 } 268 269 // Returns true if the class is declared final. IsFinal()270 ALWAYS_INLINE bool IsFinal() REQUIRES_SHARED(Locks::mutator_lock_) { 271 return (GetAccessFlags() & kAccFinal) != 0; 272 } 273 IsFinalizable()274 ALWAYS_INLINE bool IsFinalizable() REQUIRES_SHARED(Locks::mutator_lock_) { 275 return (GetAccessFlags() & kAccClassIsFinalizable) != 0; 276 } 277 ShouldSkipHiddenApiChecks()278 ALWAYS_INLINE bool ShouldSkipHiddenApiChecks() REQUIRES_SHARED(Locks::mutator_lock_) { 279 return (GetAccessFlags() & kAccSkipHiddenapiChecks) != 0; 280 } 281 SetSkipHiddenApiChecks()282 ALWAYS_INLINE void SetSkipHiddenApiChecks() REQUIRES_SHARED(Locks::mutator_lock_) { 283 uint32_t flags = GetAccessFlags(); 284 SetAccessFlags(flags | kAccSkipHiddenapiChecks); 285 } 286 287 ALWAYS_INLINE void SetRecursivelyInitialized() REQUIRES_SHARED(Locks::mutator_lock_); 288 289 ALWAYS_INLINE void SetHasDefaultMethods() REQUIRES_SHARED(Locks::mutator_lock_); 290 SetFinalizable()291 ALWAYS_INLINE void SetFinalizable() REQUIRES_SHARED(Locks::mutator_lock_) { 292 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_)); 293 SetAccessFlagsDuringLinking(flags | kAccClassIsFinalizable); 294 } 295 296 ALWAYS_INLINE void ClearFinalizable() REQUIRES_SHARED(Locks::mutator_lock_); 297 298 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsStringClass()299 ALWAYS_INLINE bool IsStringClass() REQUIRES_SHARED(Locks::mutator_lock_) { 300 return (GetClassFlags<kVerifyFlags>() & kClassFlagString) != 0; 301 } 302 SetStringClass()303 ALWAYS_INLINE void SetStringClass() REQUIRES_SHARED(Locks::mutator_lock_) { 304 SetClassFlags(kClassFlagString | kClassFlagNoReferenceFields); 305 } 306 307 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsClassLoaderClass()308 ALWAYS_INLINE bool IsClassLoaderClass() REQUIRES_SHARED(Locks::mutator_lock_) { 309 return GetClassFlags<kVerifyFlags>() == kClassFlagClassLoader; 310 } 311 SetClassLoaderClass()312 ALWAYS_INLINE void SetClassLoaderClass() REQUIRES_SHARED(Locks::mutator_lock_) { 313 SetClassFlags(kClassFlagClassLoader); 314 } 315 316 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsDexCacheClass()317 ALWAYS_INLINE bool IsDexCacheClass() REQUIRES_SHARED(Locks::mutator_lock_) { 318 return (GetClassFlags<kVerifyFlags>() & kClassFlagDexCache) != 0; 319 } 320 SetDexCacheClass()321 ALWAYS_INLINE void SetDexCacheClass() REQUIRES_SHARED(Locks::mutator_lock_) { 322 SetClassFlags(GetClassFlags() | kClassFlagDexCache); 323 } 324 325 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsRecordClass()326 ALWAYS_INLINE bool IsRecordClass() REQUIRES_SHARED(Locks::mutator_lock_) { 327 return (GetClassFlags<kVerifyFlags>() & kClassFlagRecord) != 0; 328 } 329 SetRecordClass()330 ALWAYS_INLINE void SetRecordClass() REQUIRES_SHARED(Locks::mutator_lock_) { 331 SetClassFlags(GetClassFlags() | kClassFlagRecord); 332 } 333 334 // Returns true if the class is abstract. 335 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsAbstract()336 ALWAYS_INLINE bool IsAbstract() REQUIRES_SHARED(Locks::mutator_lock_) { 337 return (GetAccessFlags<kVerifyFlags>() & kAccAbstract) != 0; 338 } 339 340 // Returns true if the class is an annotation. IsAnnotation()341 ALWAYS_INLINE bool IsAnnotation() REQUIRES_SHARED(Locks::mutator_lock_) { 342 return (GetAccessFlags() & kAccAnnotation) != 0; 343 } 344 345 // Returns true if the class is synthetic. IsSynthetic()346 ALWAYS_INLINE bool IsSynthetic() REQUIRES_SHARED(Locks::mutator_lock_) { 347 return (GetAccessFlags() & kAccSynthetic) != 0; 348 } 349 IsObsoleteObject()350 bool IsObsoleteObject() REQUIRES_SHARED(Locks::mutator_lock_) { 351 return (GetAccessFlags() & kAccObsoleteObject) != 0; 352 } 353 SetObsoleteObject()354 void SetObsoleteObject() REQUIRES_SHARED(Locks::mutator_lock_) { 355 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_)); 356 if ((flags & kAccObsoleteObject) == 0) { 357 SetAccessFlags(flags | kAccObsoleteObject); 358 } 359 } 360 361 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsTypeOfReferenceClass()362 bool IsTypeOfReferenceClass() REQUIRES_SHARED(Locks::mutator_lock_) { 363 return (GetClassFlags<kVerifyFlags>() & kClassFlagReference) != 0; 364 } 365 366 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsWeakReferenceClass()367 bool IsWeakReferenceClass() REQUIRES_SHARED(Locks::mutator_lock_) { 368 return GetClassFlags<kVerifyFlags>() == kClassFlagWeakReference; 369 } 370 371 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsSoftReferenceClass()372 bool IsSoftReferenceClass() REQUIRES_SHARED(Locks::mutator_lock_) { 373 return GetClassFlags<kVerifyFlags>() == kClassFlagSoftReference; 374 } 375 376 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsFinalizerReferenceClass()377 bool IsFinalizerReferenceClass() REQUIRES_SHARED(Locks::mutator_lock_) { 378 return GetClassFlags<kVerifyFlags>() == kClassFlagFinalizerReference; 379 } 380 381 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsPhantomReferenceClass()382 bool IsPhantomReferenceClass() REQUIRES_SHARED(Locks::mutator_lock_) { 383 return GetClassFlags<kVerifyFlags>() == kClassFlagPhantomReference; 384 } 385 386 // Can references of this type be assigned to by things of another type? For non-array types 387 // this is a matter of whether sub-classes may exist - which they can't if the type is final. 388 // For array classes, where all the classes are final due to there being no sub-classes, an 389 // Object[] may be assigned to by a String[] but a String[] may not be assigned to by other 390 // types as the component is final. 391 bool CannotBeAssignedFromOtherTypes() REQUIRES_SHARED(Locks::mutator_lock_); 392 393 // Returns true if this class is the placeholder and should retire and 394 // be replaced with a class with the right size for embedded imt/vtable. 395 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsTemp()396 bool IsTemp() REQUIRES_SHARED(Locks::mutator_lock_) { 397 ClassStatus s = GetStatus<kVerifyFlags>(); 398 return s < ClassStatus::kResolving && 399 s != ClassStatus::kErrorResolved && 400 ShouldHaveEmbeddedVTable<kVerifyFlags>(); 401 } 402 403 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 404 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 405 ObjPtr<String> GetName() REQUIRES_SHARED(Locks::mutator_lock_); // Returns the cached name. 406 void SetName(ObjPtr<String> name) REQUIRES_SHARED(Locks::mutator_lock_); // Sets the cached name. 407 // Computes the name, then sets the cached value. 408 static ObjPtr<String> ComputeName(Handle<Class> h_this) REQUIRES_SHARED(Locks::mutator_lock_) 409 REQUIRES(!Roles::uninterruptible_); 410 411 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsProxyClass()412 bool IsProxyClass() REQUIRES_SHARED(Locks::mutator_lock_) { 413 // Read access flags without using getter as whether something is a proxy can be check in 414 // any loaded state 415 // TODO: switch to a check if the super class is java.lang.reflect.Proxy? 416 uint32_t access_flags = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_)); 417 return (access_flags & kAccClassIsProxy) != 0; 418 } 419 PrimitiveTypeOffset()420 static constexpr MemberOffset PrimitiveTypeOffset() { 421 return OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_); 422 } 423 424 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 425 Primitive::Type GetPrimitiveType() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_); 426 SetPrimitiveType(Primitive::Type new_type)427 void SetPrimitiveType(Primitive::Type new_type) REQUIRES_SHARED(Locks::mutator_lock_) { 428 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t)); 429 uint32_t v32 = static_cast<uint32_t>(new_type); 430 DCHECK_EQ(v32 & kPrimitiveTypeMask, v32) << "upper 16 bits aren't zero"; 431 // Store the component size shift in the upper 16 bits. 432 v32 |= Primitive::ComponentSizeShift(new_type) << kPrimitiveTypeSizeShiftShift; 433 SetField32</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>( 434 OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), v32); 435 } 436 437 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 438 size_t GetPrimitiveTypeSizeShift() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_); 439 440 // Returns true if the class is a primitive type. 441 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsPrimitive()442 bool IsPrimitive() REQUIRES_SHARED(Locks::mutator_lock_) { 443 return GetPrimitiveType<kVerifyFlags>() != Primitive::kPrimNot; 444 } 445 446 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsPrimitiveBoolean()447 bool IsPrimitiveBoolean() REQUIRES_SHARED(Locks::mutator_lock_) { 448 return GetPrimitiveType<kVerifyFlags>() == Primitive::kPrimBoolean; 449 } 450 451 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsPrimitiveByte()452 bool IsPrimitiveByte() REQUIRES_SHARED(Locks::mutator_lock_) { 453 return GetPrimitiveType<kVerifyFlags>() == Primitive::kPrimByte; 454 } 455 456 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsPrimitiveChar()457 bool IsPrimitiveChar() REQUIRES_SHARED(Locks::mutator_lock_) { 458 return GetPrimitiveType<kVerifyFlags>() == Primitive::kPrimChar; 459 } 460 461 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsPrimitiveShort()462 bool IsPrimitiveShort() REQUIRES_SHARED(Locks::mutator_lock_) { 463 return GetPrimitiveType<kVerifyFlags>() == Primitive::kPrimShort; 464 } 465 466 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsPrimitiveInt()467 bool IsPrimitiveInt() REQUIRES_SHARED(Locks::mutator_lock_) { 468 return GetPrimitiveType() == Primitive::kPrimInt; 469 } 470 471 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsPrimitiveLong()472 bool IsPrimitiveLong() REQUIRES_SHARED(Locks::mutator_lock_) { 473 return GetPrimitiveType<kVerifyFlags>() == Primitive::kPrimLong; 474 } 475 476 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsPrimitiveFloat()477 bool IsPrimitiveFloat() REQUIRES_SHARED(Locks::mutator_lock_) { 478 return GetPrimitiveType<kVerifyFlags>() == Primitive::kPrimFloat; 479 } 480 481 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsPrimitiveDouble()482 bool IsPrimitiveDouble() REQUIRES_SHARED(Locks::mutator_lock_) { 483 return GetPrimitiveType<kVerifyFlags>() == Primitive::kPrimDouble; 484 } 485 486 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> IsPrimitiveVoid()487 bool IsPrimitiveVoid() REQUIRES_SHARED(Locks::mutator_lock_) { 488 return GetPrimitiveType<kVerifyFlags>() == Primitive::kPrimVoid; 489 } 490 491 // Depth of class from java.lang.Object 492 uint32_t Depth() REQUIRES_SHARED(Locks::mutator_lock_); 493 494 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 495 bool IsArrayClass() REQUIRES_SHARED(Locks::mutator_lock_); 496 497 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 498 bool IsClassClass() REQUIRES_SHARED(Locks::mutator_lock_); 499 500 bool IsThrowableClass() REQUIRES_SHARED(Locks::mutator_lock_); 501 ComponentTypeOffset()502 static constexpr MemberOffset ComponentTypeOffset() { 503 return OFFSET_OF_OBJECT_MEMBER(Class, component_type_); 504 } 505 506 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 507 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 508 ObjPtr<Class> GetComponentType() REQUIRES_SHARED(Locks::mutator_lock_); 509 510 void SetComponentType(ObjPtr<Class> new_component_type) REQUIRES_SHARED(Locks::mutator_lock_); 511 512 size_t GetComponentSize() REQUIRES_SHARED(Locks::mutator_lock_); 513 514 template<ReadBarrierOption kReadBarrierOption = kWithoutReadBarrier> 515 size_t GetComponentSizeShift() REQUIRES_SHARED(Locks::mutator_lock_); 516 517 bool IsObjectClass() REQUIRES_SHARED(Locks::mutator_lock_); 518 519 bool IsInstantiableNonArray() REQUIRES_SHARED(Locks::mutator_lock_); 520 521 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 522 bool IsInstantiable() REQUIRES_SHARED(Locks::mutator_lock_); 523 524 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 525 ReadBarrierOption kReadBarrierOption = kWithoutReadBarrier> 526 ALWAYS_INLINE bool IsObjectArrayClass() REQUIRES_SHARED(Locks::mutator_lock_); 527 528 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 529 bool IsPrimitiveArray() REQUIRES_SHARED(Locks::mutator_lock_); 530 531 // Enum used to control whether we try to add a finalizer-reference for object alloc or not. 532 enum class AddFinalizer { 533 // Don't create a finalizer reference regardless of what the class-flags say. 534 kNoAddFinalizer, 535 // Use the class-flags to figure out if we should make a finalizer reference. 536 kUseClassTag, 537 }; 538 539 // Creates a raw object instance but does not invoke the default constructor. 540 // kCheckAddFinalizer controls whether we use a DCHECK to check that we create a 541 // finalizer-reference if needed. This should only be disabled when doing structural class 542 // redefinition. 543 template <bool kIsInstrumented = true, 544 AddFinalizer kAddFinalizer = AddFinalizer::kUseClassTag, 545 bool kCheckAddFinalizer = true> 546 ALWAYS_INLINE ObjPtr<Object> Alloc(Thread* self, gc::AllocatorType allocator_type) 547 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 548 549 ObjPtr<Object> AllocObject(Thread* self) 550 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 551 ObjPtr<Object> AllocNonMovableObject(Thread* self) 552 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 553 554 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 555 ALWAYS_INLINE bool IsVariableSize() REQUIRES_SHARED(Locks::mutator_lock_); 556 557 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> SizeOf()558 uint32_t SizeOf() REQUIRES_SHARED(Locks::mutator_lock_) { 559 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_)); 560 } 561 562 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> GetClassSize()563 uint32_t GetClassSize() REQUIRES_SHARED(Locks::mutator_lock_) { 564 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_)); 565 } 566 567 void SetClassSize(uint32_t new_class_size) 568 REQUIRES_SHARED(Locks::mutator_lock_); 569 570 // Adjust class-size during linking in case an overflow bitmap for reference 571 // offsets is required. 572 static size_t AdjustClassSizeForReferenceOffsetBitmapDuringLinking(ObjPtr<Class> klass, 573 size_t class_size) 574 REQUIRES_SHARED(Locks::mutator_lock_); 575 576 // Compute how many bytes would be used a class with the given elements. 577 static uint32_t ComputeClassSize(bool has_embedded_vtable, 578 uint32_t num_vtable_entries, 579 uint32_t num_8bit_static_fields, 580 uint32_t num_16bit_static_fields, 581 uint32_t num_32bit_static_fields, 582 uint32_t num_64bit_static_fields, 583 uint32_t num_ref_static_fields, 584 uint32_t num_ref_bitmap_entries, 585 PointerSize pointer_size); 586 587 // The size of java.lang.Class.class. ClassClassSize(PointerSize pointer_size)588 static uint32_t ClassClassSize(PointerSize pointer_size) { 589 // The number of vtable entries in java.lang.Class. 590 uint32_t vtable_entries = Object::kVTableLength + 83; 591 return ComputeClassSize(true, vtable_entries, 0, 0, 4, 1, 0, 0, pointer_size); 592 } 593 594 // The size of a java.lang.Class representing a primitive such as int.class. PrimitiveClassSize(PointerSize pointer_size)595 static uint32_t PrimitiveClassSize(PointerSize pointer_size) { 596 return ComputeClassSize(false, 0, 0, 0, 0, 0, 0, 0, pointer_size); 597 } 598 599 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 600 uint32_t GetObjectSize() REQUIRES_SHARED(Locks::mutator_lock_); ObjectSizeOffset()601 static constexpr MemberOffset ObjectSizeOffset() { 602 return OFFSET_OF_OBJECT_MEMBER(Class, object_size_); 603 } ObjectSizeAllocFastPathOffset()604 static constexpr MemberOffset ObjectSizeAllocFastPathOffset() { 605 return OFFSET_OF_OBJECT_MEMBER(Class, object_size_alloc_fast_path_); 606 } ClinitThreadIdOffset()607 static constexpr MemberOffset ClinitThreadIdOffset() { 608 return OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_); 609 } 610 611 ALWAYS_INLINE void SetObjectSize(uint32_t new_object_size) REQUIRES_SHARED(Locks::mutator_lock_); 612 613 void SetObjectSizeAllocFastPath(uint32_t new_object_size) REQUIRES_SHARED(Locks::mutator_lock_); 614 615 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 616 uint32_t GetObjectSizeAllocFastPath() REQUIRES_SHARED(Locks::mutator_lock_); 617 SetObjectSizeWithoutChecks(uint32_t new_object_size)618 void SetObjectSizeWithoutChecks(uint32_t new_object_size) 619 REQUIRES_SHARED(Locks::mutator_lock_) { 620 // Not called within a transaction. 621 return SetField32<false, false, kVerifyNone>( 622 OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size); 623 } 624 625 // Returns true if this class is in the same packages as that class. 626 bool IsInSamePackage(ObjPtr<Class> that) REQUIRES_SHARED(Locks::mutator_lock_); 627 628 static bool IsInSamePackage(std::string_view descriptor1, std::string_view descriptor2); 629 630 // Returns true if a class member should be discoverable with reflection given 631 // the criteria. Some reflection calls only return public members 632 // (public_only == true), some members should be hidden from non-boot class path 633 // callers (hiddenapi_context). 634 template<typename T> 635 ALWAYS_INLINE static bool IsDiscoverable(bool public_only, 636 const hiddenapi::AccessContext& access_context, 637 T* member) 638 REQUIRES_SHARED(Locks::mutator_lock_); 639 640 // Returns true if this class can access that class. 641 bool CanAccess(ObjPtr<Class> that) REQUIRES_SHARED(Locks::mutator_lock_); 642 643 // Can this class access a member in the provided class with the provided member access flags? 644 // Note that access to the class isn't checked in case the declaring class is protected and the 645 // method has been exposed by a public sub-class 646 bool CanAccessMember(ObjPtr<Class> access_to, uint32_t member_flags) 647 REQUIRES_SHARED(Locks::mutator_lock_); 648 649 // Can this class access a resolved field? 650 // Note that access to field's class is checked and this may require looking up the class 651 // referenced by the FieldId in the DexFile in case the declaring class is inaccessible. 652 bool CanAccessResolvedField(ObjPtr<Class> access_to, 653 ArtField* field, 654 ObjPtr<DexCache> dex_cache, 655 uint32_t field_idx) 656 REQUIRES_SHARED(Locks::mutator_lock_); 657 bool CheckResolvedFieldAccess(ObjPtr<Class> access_to, 658 ArtField* field, 659 ObjPtr<DexCache> dex_cache, 660 uint32_t field_idx) 661 REQUIRES_SHARED(Locks::mutator_lock_); 662 663 bool IsSubClass(ObjPtr<Class> klass) REQUIRES_SHARED(Locks::mutator_lock_); 664 665 // Can src be assigned to this class? For example, String can be assigned to Object (by an 666 // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing 667 // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface 668 // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign 669 // to themselves. Classes for primitive types may not assign to each other. 670 ALWAYS_INLINE bool IsAssignableFrom(ObjPtr<Class> src) REQUIRES_SHARED(Locks::mutator_lock_); 671 672 // Check if this class implements a given interface. 673 bool Implements(ObjPtr<Class> klass) REQUIRES_SHARED(Locks::mutator_lock_); 674 675 // Checks if 'klass' is a redefined version of this. 676 bool IsObsoleteVersionOf(ObjPtr<Class> klass) REQUIRES_SHARED(Locks::mutator_lock_); 677 678 ObjPtr<Class> GetObsoleteClass() REQUIRES_SHARED(Locks::mutator_lock_); 679 680 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 681 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 682 ALWAYS_INLINE ObjPtr<Class> GetSuperClass() REQUIRES_SHARED(Locks::mutator_lock_); 683 684 // Get first common super class. It will never return null. 685 // `This` and `klass` must be classes. 686 ObjPtr<Class> GetCommonSuperClass(Handle<Class> klass) REQUIRES_SHARED(Locks::mutator_lock_); 687 688 void SetSuperClass(ObjPtr<Class> new_super_class) REQUIRES_SHARED(Locks::mutator_lock_); 689 690 bool HasSuperClass() REQUIRES_SHARED(Locks::mutator_lock_); 691 SuperClassOffset()692 static constexpr MemberOffset SuperClassOffset() { 693 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_)); 694 } 695 696 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 697 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 698 ObjPtr<ClassLoader> GetClassLoader() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_); 699 700 void SetClassLoader(ObjPtr<ClassLoader> new_cl) REQUIRES_SHARED(Locks::mutator_lock_); 701 DexCacheOffset()702 static constexpr MemberOffset DexCacheOffset() { 703 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_)); 704 } 705 IfTableOffset()706 static constexpr MemberOffset IfTableOffset() { 707 return MemberOffset(OFFSETOF_MEMBER(Class, iftable_)); 708 } 709 710 enum { 711 kDumpClassFullDetail = 1, 712 kDumpClassClassLoader = (1 << 1), 713 kDumpClassInitialized = (1 << 2), 714 }; 715 716 void DumpClass(std::ostream& os, int flags) REQUIRES_SHARED(Locks::mutator_lock_); 717 718 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 719 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 720 ObjPtr<DexCache> GetDexCache() REQUIRES_SHARED(Locks::mutator_lock_); 721 722 // Also updates the dex_cache_strings_ variable from new_dex_cache. 723 void SetDexCache(ObjPtr<DexCache> new_dex_cache) REQUIRES_SHARED(Locks::mutator_lock_); 724 725 ALWAYS_INLINE ArraySlice<ArtMethod> GetDirectMethods(PointerSize pointer_size) 726 REQUIRES_SHARED(Locks::mutator_lock_); 727 728 ALWAYS_INLINE LengthPrefixedArray<ArtMethod>* GetMethodsPtr() 729 REQUIRES_SHARED(Locks::mutator_lock_); 730 MethodsOffset()731 static constexpr MemberOffset MethodsOffset() { 732 return MemberOffset(OFFSETOF_MEMBER(Class, methods_)); 733 } 734 735 ALWAYS_INLINE ArraySlice<ArtMethod> GetMethods(PointerSize pointer_size) 736 REQUIRES_SHARED(Locks::mutator_lock_); 737 738 void SetMethodsPtr(LengthPrefixedArray<ArtMethod>* new_methods, 739 uint32_t num_direct, 740 uint32_t num_virtual) 741 REQUIRES_SHARED(Locks::mutator_lock_); 742 // Used by image writer. 743 void SetMethodsPtrUnchecked(LengthPrefixedArray<ArtMethod>* new_methods, 744 uint32_t num_direct, 745 uint32_t num_virtual) 746 REQUIRES_SHARED(Locks::mutator_lock_); 747 748 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 749 ALWAYS_INLINE ArraySlice<ArtMethod> GetDirectMethodsSlice(PointerSize pointer_size) 750 REQUIRES_SHARED(Locks::mutator_lock_); 751 752 ALWAYS_INLINE ArtMethod* GetDirectMethod(size_t i, PointerSize pointer_size) 753 REQUIRES_SHARED(Locks::mutator_lock_); 754 755 // Use only when we are allocating populating the method arrays. 756 ALWAYS_INLINE ArtMethod* GetDirectMethodUnchecked(size_t i, PointerSize pointer_size) 757 REQUIRES_SHARED(Locks::mutator_lock_); 758 ALWAYS_INLINE ArtMethod* GetVirtualMethodUnchecked(size_t i, PointerSize pointer_size) 759 REQUIRES_SHARED(Locks::mutator_lock_); 760 761 // Returns the number of static, private, and constructor methods. 762 ALWAYS_INLINE uint32_t NumDirectMethods() REQUIRES_SHARED(Locks::mutator_lock_); 763 764 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 765 ALWAYS_INLINE ArraySlice<ArtMethod> GetMethodsSlice(PointerSize pointer_size) 766 REQUIRES_SHARED(Locks::mutator_lock_); 767 768 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 769 ALWAYS_INLINE ArraySlice<ArtMethod> GetDeclaredMethodsSlice(PointerSize pointer_size) 770 REQUIRES_SHARED(Locks::mutator_lock_); 771 772 ALWAYS_INLINE ArraySlice<ArtMethod> GetDeclaredMethods( 773 PointerSize pointer_size) 774 REQUIRES_SHARED(Locks::mutator_lock_); 775 776 template <PointerSize kPointerSize> 777 static ObjPtr<Method> GetDeclaredMethodInternal( 778 Thread* self, 779 ObjPtr<Class> klass, 780 ObjPtr<String> name, 781 ObjPtr<ObjectArray<Class>> args, 782 const std::function<hiddenapi::AccessContext()>& fn_get_access_context) 783 REQUIRES_SHARED(Locks::mutator_lock_); 784 785 template <PointerSize kPointerSize> 786 static ObjPtr<Constructor> GetDeclaredConstructorInternal(Thread* self, 787 ObjPtr<Class> klass, 788 ObjPtr<ObjectArray<Class>> args) 789 REQUIRES_SHARED(Locks::mutator_lock_); 790 791 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 792 ALWAYS_INLINE ArraySlice<ArtMethod> GetDeclaredVirtualMethodsSlice(PointerSize pointer_size) 793 REQUIRES_SHARED(Locks::mutator_lock_); 794 795 ALWAYS_INLINE ArraySlice<ArtMethod> GetDeclaredVirtualMethods( 796 PointerSize pointer_size) 797 REQUIRES_SHARED(Locks::mutator_lock_); 798 799 // The index in the methods_ array where the first copied method is. 800 ALWAYS_INLINE uint32_t GetCopiedMethodsStartOffset() REQUIRES_SHARED(Locks::mutator_lock_); 801 802 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 803 ALWAYS_INLINE ArraySlice<ArtMethod> GetCopiedMethodsSlice(PointerSize pointer_size) 804 REQUIRES_SHARED(Locks::mutator_lock_); 805 806 ALWAYS_INLINE ArraySlice<ArtMethod> GetCopiedMethods(PointerSize pointer_size) 807 REQUIRES_SHARED(Locks::mutator_lock_); 808 809 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 810 ALWAYS_INLINE ArraySlice<ArtMethod> GetVirtualMethodsSlice(PointerSize pointer_size) 811 REQUIRES_SHARED(Locks::mutator_lock_); 812 813 ALWAYS_INLINE ArraySlice<ArtMethod> GetVirtualMethods( 814 PointerSize pointer_size) 815 REQUIRES_SHARED(Locks::mutator_lock_); 816 817 // Returns the number of non-inherited virtual methods (sum of declared and copied methods). 818 ALWAYS_INLINE uint32_t NumVirtualMethods() REQUIRES_SHARED(Locks::mutator_lock_); 819 820 // Returns the number of copied virtual methods. 821 ALWAYS_INLINE uint32_t NumCopiedVirtualMethods() REQUIRES_SHARED(Locks::mutator_lock_); 822 823 // Returns the number of declared virtual methods. 824 ALWAYS_INLINE uint32_t NumDeclaredVirtualMethods() REQUIRES_SHARED(Locks::mutator_lock_); 825 826 ALWAYS_INLINE uint32_t NumMethods() REQUIRES_SHARED(Locks::mutator_lock_); 827 static ALWAYS_INLINE uint32_t NumMethods(LengthPrefixedArray<ArtMethod>* methods) 828 REQUIRES_SHARED(Locks::mutator_lock_); 829 830 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 831 ArtMethod* GetVirtualMethod(size_t i, PointerSize pointer_size) 832 REQUIRES_SHARED(Locks::mutator_lock_); 833 834 ArtMethod* GetVirtualMethodDuringLinking(size_t i, PointerSize pointer_size) 835 REQUIRES_SHARED(Locks::mutator_lock_); 836 837 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 838 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 839 ALWAYS_INLINE ObjPtr<PointerArray> GetVTable() REQUIRES_SHARED(Locks::mutator_lock_); 840 841 ALWAYS_INLINE ObjPtr<PointerArray> GetVTableDuringLinking() REQUIRES_SHARED(Locks::mutator_lock_); 842 843 void SetVTable(ObjPtr<PointerArray> new_vtable) REQUIRES_SHARED(Locks::mutator_lock_); 844 VTableOffset()845 static constexpr MemberOffset VTableOffset() { 846 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_); 847 } 848 EmbeddedVTableLengthOffset()849 static constexpr MemberOffset EmbeddedVTableLengthOffset() { 850 return MemberOffset(sizeof(Class)); 851 } 852 ImtPtrOffset(PointerSize pointer_size)853 static constexpr MemberOffset ImtPtrOffset(PointerSize pointer_size) { 854 return MemberOffset( 855 RoundUp(EmbeddedVTableLengthOffset().Uint32Value() + sizeof(uint32_t), 856 static_cast<size_t>(pointer_size))); 857 } 858 EmbeddedVTableOffset(PointerSize pointer_size)859 static constexpr MemberOffset EmbeddedVTableOffset(PointerSize pointer_size) { 860 return MemberOffset( 861 ImtPtrOffset(pointer_size).Uint32Value() + static_cast<size_t>(pointer_size)); 862 } 863 864 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 865 bool ShouldHaveImt() REQUIRES_SHARED(Locks::mutator_lock_); 866 867 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 868 bool ShouldHaveEmbeddedVTable() REQUIRES_SHARED(Locks::mutator_lock_); 869 870 bool HasVTable() REQUIRES_SHARED(Locks::mutator_lock_); 871 872 static MemberOffset EmbeddedVTableEntryOffset(uint32_t i, PointerSize pointer_size); 873 874 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 875 int32_t GetVTableLength() REQUIRES_SHARED(Locks::mutator_lock_); 876 877 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 878 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 879 ArtMethod* GetVTableEntry(uint32_t i, PointerSize pointer_size) 880 REQUIRES_SHARED(Locks::mutator_lock_); 881 882 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 883 int32_t GetEmbeddedVTableLength() REQUIRES_SHARED(Locks::mutator_lock_); 884 885 void SetEmbeddedVTableLength(int32_t len) REQUIRES_SHARED(Locks::mutator_lock_); 886 887 ImTable* GetImt(PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_); 888 889 void SetImt(ImTable* imt, PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_); 890 891 ImTable* FindSuperImt(PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_); 892 893 ArtMethod* GetEmbeddedVTableEntry(uint32_t i, PointerSize pointer_size) 894 REQUIRES_SHARED(Locks::mutator_lock_); 895 896 void SetEmbeddedVTableEntry(uint32_t i, ArtMethod* method, PointerSize pointer_size) 897 REQUIRES_SHARED(Locks::mutator_lock_); 898 899 inline void SetEmbeddedVTableEntryUnchecked(uint32_t i, 900 ArtMethod* method, 901 PointerSize pointer_size) 902 REQUIRES_SHARED(Locks::mutator_lock_); 903 904 void PopulateEmbeddedVTable(PointerSize pointer_size) 905 REQUIRES_SHARED(Locks::mutator_lock_); 906 907 template <VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 908 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 909 void VerifyOverflowReferenceBitmap() REQUIRES_SHARED(Locks::mutator_lock_); 910 // If the bitmap in `reference_instance_offsets_` was found to be insufficient 911 // in CreateReferenceInstanceOffsets(), then populate the overflow bitmap, 912 // which is at the end of class object. 913 void PopulateReferenceOffsetBitmap() REQUIRES_SHARED(Locks::mutator_lock_); 914 915 // Given a method implemented by this class but potentially from a super class, return the 916 // specific implementation method for this class. 917 ArtMethod* FindVirtualMethodForVirtual(ArtMethod* method, PointerSize pointer_size) 918 REQUIRES_SHARED(Locks::mutator_lock_); 919 920 // Given a method implemented by this class' super class, return the specific implementation 921 // method for this class. 922 ArtMethod* FindVirtualMethodForSuper(ArtMethod* method, PointerSize pointer_size) 923 REQUIRES_SHARED(Locks::mutator_lock_); 924 925 // Given a method from some implementor of this interface, return the specific implementation 926 // method for this class. 927 ArtMethod* FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) 928 REQUIRES_SHARED(Locks::mutator_lock_); 929 930 // Given a method implemented by this class, but potentially from a 931 // super class or interface, return the specific implementation 932 // method for this class. 933 ArtMethod* FindVirtualMethodForInterface(ArtMethod* method, PointerSize pointer_size) 934 REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE; 935 936 ArtMethod* FindVirtualMethodForVirtualOrInterface(ArtMethod* method, PointerSize pointer_size) 937 REQUIRES_SHARED(Locks::mutator_lock_); 938 939 // Find a method with the given name and signature in an interface class. 940 // 941 // Search for the method declared in the class, then search for a method declared in any 942 // superinterface, then search the superclass java.lang.Object (implicitly declared methods 943 // in an interface without superinterfaces, see JLS 9.2, can be inherited, see JLS 9.4.1). 944 // TODO: Implement search for a unique maximally-specific non-abstract superinterface method. 945 946 ArtMethod* FindInterfaceMethod(std::string_view name, 947 std::string_view signature, 948 PointerSize pointer_size) 949 REQUIRES_SHARED(Locks::mutator_lock_); 950 951 ArtMethod* FindInterfaceMethod(std::string_view name, 952 const Signature& signature, 953 PointerSize pointer_size) 954 REQUIRES_SHARED(Locks::mutator_lock_); 955 956 ArtMethod* FindInterfaceMethod(ObjPtr<DexCache> dex_cache, 957 uint32_t dex_method_idx, 958 PointerSize pointer_size) 959 REQUIRES_SHARED(Locks::mutator_lock_); 960 961 // Return the first public SDK method from the list of interfaces implemented by 962 // this class. 963 ArtMethod* FindAccessibleInterfaceMethod(ArtMethod* implementation_method, 964 PointerSize pointer_size) 965 REQUIRES_SHARED(Locks::mutator_lock_); 966 967 // Find a method with the given name and signature in a non-interface class. 968 // 969 // Search for the method in the class, following the JLS rules which conflict with the RI 970 // in some cases. The JLS says that inherited methods are searched (JLS 15.12.2.1) and 971 // these can come from a superclass or a superinterface (JLS 8.4.8). We perform the 972 // following search: 973 // 1. Search the methods declared directly in the class. If we find a method with the 974 // given name and signature, return that method. 975 // 2. Search the methods declared in superclasses until we find a method with the given 976 // signature or complete the search in java.lang.Object. If we find a method with the 977 // given name and signature, check if it's been inherited by the class where we're 978 // performing the lookup (qualifying type). If it's inherited, return it. Otherwise, 979 // just remember the method and its declaring class and proceed to step 3. 980 // 3. Search "copied" methods (containing methods inherited from interfaces) in the class 981 // and its superclass chain. If we found a method in step 2 (which was not inherited, 982 // otherwise we would not be performing step 3), end the search when we reach its 983 // declaring class, otherwise search the entire superclass chain. If we find a method 984 // with the given name and signature, return that method. 985 // 4. Return the method found in step 2 if any (not inherited), or null. 986 // 987 // It's the responsibility of the caller to throw exceptions if the returned method (or null) 988 // does not satisfy the request. Special consideration should be given to the case where this 989 // function returns a method that's not inherited (found in step 2, returned in step 4). 990 991 ArtMethod* FindClassMethod(std::string_view name, 992 std::string_view signature, 993 PointerSize pointer_size) 994 REQUIRES_SHARED(Locks::mutator_lock_); 995 996 ArtMethod* FindClassMethod(std::string_view name, 997 const Signature& signature, 998 PointerSize pointer_size) 999 REQUIRES_SHARED(Locks::mutator_lock_); 1000 1001 ArtMethod* FindClassMethod(ObjPtr<DexCache> dex_cache, 1002 uint32_t dex_method_idx, 1003 PointerSize pointer_size) 1004 REQUIRES_SHARED(Locks::mutator_lock_); 1005 1006 ArtMethod* FindConstructor(std::string_view signature, PointerSize pointer_size) 1007 REQUIRES_SHARED(Locks::mutator_lock_); 1008 1009 ArtMethod* FindDeclaredVirtualMethodByName(std::string_view name, PointerSize pointer_size) 1010 REQUIRES_SHARED(Locks::mutator_lock_); 1011 1012 ArtMethod* FindDeclaredDirectMethodByName(std::string_view name, PointerSize pointer_size) 1013 REQUIRES_SHARED(Locks::mutator_lock_); 1014 1015 ArtMethod* FindClassInitializer(PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_); 1016 HasDefaultMethods()1017 bool HasDefaultMethods() REQUIRES_SHARED(Locks::mutator_lock_) { 1018 return (GetAccessFlags() & kAccHasDefaultMethod) != 0; 1019 } 1020 HasBeenRecursivelyInitialized()1021 bool HasBeenRecursivelyInitialized() REQUIRES_SHARED(Locks::mutator_lock_) { 1022 return (GetAccessFlags() & kAccRecursivelyInitialized) != 0; 1023 } 1024 1025 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 1026 ALWAYS_INLINE int32_t GetIfTableCount() REQUIRES_SHARED(Locks::mutator_lock_); 1027 1028 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 1029 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 1030 ALWAYS_INLINE ObjPtr<IfTable> GetIfTable() REQUIRES_SHARED(Locks::mutator_lock_); 1031 1032 ALWAYS_INLINE void SetIfTable(ObjPtr<IfTable> new_iftable) 1033 REQUIRES_SHARED(Locks::mutator_lock_); 1034 1035 // Get instance fields of the class (See also GetSFields). 1036 LengthPrefixedArray<ArtField>* GetIFieldsPtr() REQUIRES_SHARED(Locks::mutator_lock_); 1037 1038 ALWAYS_INLINE IterationRange<StrideIterator<ArtField>> GetIFields() 1039 REQUIRES_SHARED(Locks::mutator_lock_); 1040 1041 void SetIFieldsPtr(LengthPrefixedArray<ArtField>* new_ifields) 1042 REQUIRES_SHARED(Locks::mutator_lock_); 1043 1044 // Unchecked edition has no verification flags. 1045 void SetIFieldsPtrUnchecked(LengthPrefixedArray<ArtField>* new_sfields) 1046 REQUIRES_SHARED(Locks::mutator_lock_); 1047 1048 uint32_t NumInstanceFields() REQUIRES_SHARED(Locks::mutator_lock_); 1049 ArtField* GetInstanceField(uint32_t i) REQUIRES_SHARED(Locks::mutator_lock_); 1050 1051 // Returns the number of instance fields containing reference types. Does not count fields in any 1052 // super classes. 1053 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> NumReferenceInstanceFields()1054 uint32_t NumReferenceInstanceFields() REQUIRES_SHARED(Locks::mutator_lock_) { 1055 DCHECK(IsResolved<kVerifyFlags>()); 1056 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_)); 1057 } 1058 NumReferenceInstanceFieldsDuringLinking()1059 uint32_t NumReferenceInstanceFieldsDuringLinking() REQUIRES_SHARED(Locks::mutator_lock_) { 1060 DCHECK(IsLoaded() || IsErroneous()); 1061 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_)); 1062 } 1063 SetNumReferenceInstanceFields(uint32_t new_num)1064 void SetNumReferenceInstanceFields(uint32_t new_num) REQUIRES_SHARED(Locks::mutator_lock_) { 1065 // Not called within a transaction. 1066 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num); 1067 } 1068 1069 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 1070 uint32_t GetReferenceInstanceOffsets() ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_); 1071 1072 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets) 1073 REQUIRES_SHARED(Locks::mutator_lock_); 1074 1075 // Get the offset of the first reference instance field. Other reference instance fields follow. 1076 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 1077 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 1078 MemberOffset GetFirstReferenceInstanceFieldOffset() 1079 REQUIRES_SHARED(Locks::mutator_lock_); 1080 1081 // Returns the number of static fields containing reference types. 1082 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> NumReferenceStaticFields()1083 uint32_t NumReferenceStaticFields() REQUIRES_SHARED(Locks::mutator_lock_) { 1084 DCHECK(IsResolved<kVerifyFlags>()); 1085 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_)); 1086 } 1087 NumReferenceStaticFieldsDuringLinking()1088 uint32_t NumReferenceStaticFieldsDuringLinking() REQUIRES_SHARED(Locks::mutator_lock_) { 1089 DCHECK(IsLoaded() || IsErroneous() || IsRetired()); 1090 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_)); 1091 } 1092 SetNumReferenceStaticFields(uint32_t new_num)1093 void SetNumReferenceStaticFields(uint32_t new_num) REQUIRES_SHARED(Locks::mutator_lock_) { 1094 // Not called within a transaction. 1095 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num); 1096 } 1097 1098 // Get the offset of the first reference static field. Other reference static fields follow. 1099 template <VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 1100 MemberOffset GetFirstReferenceStaticFieldOffset(PointerSize pointer_size) 1101 REQUIRES_SHARED(Locks::mutator_lock_); 1102 1103 // Get the offset of the first reference static field. Other reference static fields follow. 1104 MemberOffset GetFirstReferenceStaticFieldOffsetDuringLinking(PointerSize pointer_size) 1105 REQUIRES_SHARED(Locks::mutator_lock_); 1106 1107 // Gets the static fields of the class. 1108 LengthPrefixedArray<ArtField>* GetSFieldsPtr() REQUIRES_SHARED(Locks::mutator_lock_); 1109 ALWAYS_INLINE IterationRange<StrideIterator<ArtField>> GetSFields() 1110 REQUIRES_SHARED(Locks::mutator_lock_); 1111 1112 void SetSFieldsPtr(LengthPrefixedArray<ArtField>* new_sfields) 1113 REQUIRES_SHARED(Locks::mutator_lock_); 1114 1115 // Unchecked edition has no verification flags. 1116 void SetSFieldsPtrUnchecked(LengthPrefixedArray<ArtField>* new_sfields) 1117 REQUIRES_SHARED(Locks::mutator_lock_); 1118 1119 uint32_t NumStaticFields() REQUIRES_SHARED(Locks::mutator_lock_); 1120 1121 // TODO: uint16_t 1122 ArtField* GetStaticField(uint32_t i) REQUIRES_SHARED(Locks::mutator_lock_); 1123 1124 // Find a static or instance field using the JLS resolution order 1125 ArtField* FindField(ObjPtr<mirror::DexCache> dex_cache, uint32_t field_idx) 1126 REQUIRES_SHARED(Locks::mutator_lock_); 1127 1128 // Finds the given instance field in this class or a superclass. 1129 ArtField* FindInstanceField(std::string_view name, std::string_view type) 1130 REQUIRES_SHARED(Locks::mutator_lock_); 1131 1132 // Finds the given instance field in this class or a superclass, only searches classes that 1133 // have the same dex cache. 1134 ArtField* FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) 1135 REQUIRES_SHARED(Locks::mutator_lock_); 1136 1137 ArtField* FindDeclaredInstanceField(std::string_view name, std::string_view type) 1138 REQUIRES_SHARED(Locks::mutator_lock_); 1139 1140 ArtField* FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) 1141 REQUIRES_SHARED(Locks::mutator_lock_); 1142 1143 // Finds the given static field in this class or a superclass. 1144 ArtField* FindStaticField(std::string_view name, std::string_view type) 1145 REQUIRES_SHARED(Locks::mutator_lock_); 1146 1147 // Finds the given static field in this class or superclass, only searches classes that 1148 // have the same dex cache. 1149 ArtField* FindStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) 1150 REQUIRES_SHARED(Locks::mutator_lock_); 1151 1152 ArtField* FindDeclaredStaticField(std::string_view name, std::string_view type) 1153 REQUIRES_SHARED(Locks::mutator_lock_); 1154 1155 ArtField* FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) 1156 REQUIRES_SHARED(Locks::mutator_lock_); 1157 1158 ObjPtr<mirror::ObjectArray<mirror::Field>> GetDeclaredFields(Thread* self, 1159 bool public_only, 1160 bool force_resolve) 1161 REQUIRES_SHARED(Locks::mutator_lock_); 1162 1163 GetClinitThreadId()1164 pid_t GetClinitThreadId() REQUIRES_SHARED(Locks::mutator_lock_) { 1165 DCHECK(IsIdxLoaded() || IsErroneous()) << PrettyClass(); 1166 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_)); 1167 } 1168 1169 void SetClinitThreadId(pid_t new_clinit_thread_id) REQUIRES_SHARED(Locks::mutator_lock_); 1170 1171 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 1172 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 1173 ObjPtr<ClassExt> GetExtData() REQUIRES_SHARED(Locks::mutator_lock_); 1174 1175 // Returns the ExtData for this class, allocating one if necessary. This should be the only way 1176 // to force ext_data_ to be set. No functions are available for changing an already set ext_data_ 1177 // since doing so is not allowed. 1178 static ObjPtr<ClassExt> EnsureExtDataPresent(Handle<Class> h_this, Thread* self) 1179 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 1180 GetDexClassDefIndex()1181 uint16_t GetDexClassDefIndex() REQUIRES_SHARED(Locks::mutator_lock_) { 1182 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_class_def_idx_)); 1183 } 1184 SetDexClassDefIndex(uint16_t class_def_idx)1185 void SetDexClassDefIndex(uint16_t class_def_idx) REQUIRES_SHARED(Locks::mutator_lock_) { 1186 SetField32</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>( 1187 OFFSET_OF_OBJECT_MEMBER(Class, dex_class_def_idx_), class_def_idx); 1188 } 1189 GetDexTypeIndex()1190 dex::TypeIndex GetDexTypeIndex() REQUIRES_SHARED(Locks::mutator_lock_) { 1191 return dex::TypeIndex( 1192 static_cast<uint16_t>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_)))); 1193 } 1194 SetDexTypeIndex(dex::TypeIndex type_idx)1195 void SetDexTypeIndex(dex::TypeIndex type_idx) REQUIRES_SHARED(Locks::mutator_lock_) { 1196 SetField32</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>( 1197 OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), type_idx.index_); 1198 } 1199 1200 dex::TypeIndex FindTypeIndexInOtherDexFile(const DexFile& dex_file) 1201 REQUIRES_SHARED(Locks::mutator_lock_); 1202 1203 // Visit native roots visits roots which are keyed off the native pointers such as ArtFields and 1204 // ArtMethods. 1205 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier, 1206 bool kVisitProxyMethod = true, 1207 class Visitor> 1208 void VisitNativeRoots(Visitor& visitor, PointerSize pointer_size) 1209 REQUIRES_SHARED(Locks::mutator_lock_); 1210 1211 // Visit obsolete dex caches possibly stored in ext_data_ 1212 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 1213 void VisitObsoleteDexCaches(DexCacheVisitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_); 1214 1215 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier, class Visitor> 1216 void VisitObsoleteClass(Visitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_); 1217 1218 // Visit ArtMethods directly owned by this class. 1219 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier, class Visitor> 1220 void VisitMethods(Visitor visitor, PointerSize pointer_size) 1221 REQUIRES_SHARED(Locks::mutator_lock_); 1222 1223 // Visit ArtFields directly owned by this class. 1224 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier, class Visitor> 1225 void VisitFields(Visitor visitor) REQUIRES_SHARED(Locks::mutator_lock_); 1226 1227 // Get one of the primitive classes. 1228 static ObjPtr<mirror::Class> GetPrimitiveClass(ObjPtr<mirror::String> name) 1229 REQUIRES_SHARED(Locks::mutator_lock_); 1230 1231 // Clear the kAccMustCountLocks flag on each method, for class redefinition. 1232 void ClearMustCountLocksFlagOnAllMethods(PointerSize pointer_size) 1233 REQUIRES_SHARED(Locks::mutator_lock_); 1234 // Clear the kAccCompileDontBother flag on each method, for class redefinition. 1235 void ClearDontCompileFlagOnAllMethods(PointerSize pointer_size) 1236 REQUIRES_SHARED(Locks::mutator_lock_); 1237 1238 // Clear the kAccSkipAccessChecks flag on each method, for class redefinition. 1239 void ClearSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) 1240 REQUIRES_SHARED(Locks::mutator_lock_); 1241 // When class is verified, set the kAccSkipAccessChecks flag on each method. 1242 void SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) 1243 REQUIRES_SHARED(Locks::mutator_lock_); 1244 1245 // Get the descriptor of the class as `std::string_view`. The class must be directly 1246 // backed by a `DexFile` - it must not be primitive, array or proxy class. 1247 std::string_view GetDescriptorView() REQUIRES_SHARED(Locks::mutator_lock_); 1248 1249 // Get the descriptor of the class. In a few cases a std::string is required, rather than 1250 // always create one the storage argument is populated and its internal c_str() returned. We do 1251 // this to avoid memory allocation in the common case. 1252 const char* GetDescriptor(std::string* storage) REQUIRES_SHARED(Locks::mutator_lock_); 1253 1254 bool DescriptorEquals(ObjPtr<mirror::Class> match) REQUIRES_SHARED(Locks::mutator_lock_); 1255 bool DescriptorEquals(std::string_view match) REQUIRES_SHARED(Locks::mutator_lock_); 1256 1257 uint32_t DescriptorHash() REQUIRES_SHARED(Locks::mutator_lock_); 1258 1259 const dex::ClassDef* GetClassDef() REQUIRES_SHARED(Locks::mutator_lock_); 1260 1261 ALWAYS_INLINE uint32_t NumDirectInterfaces() REQUIRES_SHARED(Locks::mutator_lock_); 1262 1263 dex::TypeIndex GetDirectInterfaceTypeIdx(uint32_t idx) REQUIRES_SHARED(Locks::mutator_lock_); 1264 1265 // Get the direct interface at index `idx` if resolved, otherwise return null. 1266 // If the caller expects the interface to be resolved, for example for a resolved `klass`, 1267 // that assumption should be checked by `DCHECK(result != nullptr)`. 1268 ObjPtr<Class> GetDirectInterface(uint32_t idx) REQUIRES_SHARED(Locks::mutator_lock_); 1269 1270 // Resolve and get the direct interface of the `klass` at index `idx`. 1271 // Returns null with a pending exception if the resolution fails. 1272 static ObjPtr<Class> ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) 1273 REQUIRES_SHARED(Locks::mutator_lock_); 1274 1275 const char* GetSourceFile() REQUIRES_SHARED(Locks::mutator_lock_); 1276 1277 std::string GetLocation() REQUIRES_SHARED(Locks::mutator_lock_); 1278 1279 const DexFile& GetDexFile() REQUIRES_SHARED(Locks::mutator_lock_); 1280 1281 const dex::TypeList* GetInterfaceTypeList() REQUIRES_SHARED(Locks::mutator_lock_); 1282 1283 // Asserts we are initialized or initializing in the given thread. 1284 void AssertInitializedOrInitializingInThread(Thread* self) 1285 REQUIRES_SHARED(Locks::mutator_lock_); 1286 1287 static ObjPtr<Class> CopyOf(Handle<Class> h_this, 1288 Thread* self, 1289 int32_t new_length, 1290 ImTable* imt, 1291 PointerSize pointer_size) 1292 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 1293 1294 // For proxy class only. 1295 ObjPtr<ObjectArray<Class>> GetProxyInterfaces() REQUIRES_SHARED(Locks::mutator_lock_); 1296 1297 // For proxy class only. 1298 ObjPtr<ObjectArray<ObjectArray<Class>>> GetProxyThrows() REQUIRES_SHARED(Locks::mutator_lock_); 1299 1300 // May cause thread suspension due to EqualParameters. 1301 ArtMethod* GetDeclaredConstructor(Thread* self, 1302 Handle<ObjectArray<Class>> args, 1303 PointerSize pointer_size) 1304 REQUIRES_SHARED(Locks::mutator_lock_); 1305 1306 static int32_t GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) 1307 REQUIRES_SHARED(Locks::mutator_lock_); 1308 1309 // Used to initialize a class in the allocation code path to ensure it is guarded by a StoreStore 1310 // fence. 1311 class InitializeClassVisitor { 1312 public: InitializeClassVisitor(uint32_t class_size)1313 explicit InitializeClassVisitor(uint32_t class_size) : class_size_(class_size) { 1314 } 1315 1316 void operator()(ObjPtr<Object> obj, size_t usable_size) const 1317 REQUIRES_SHARED(Locks::mutator_lock_); 1318 1319 private: 1320 const uint32_t class_size_; 1321 1322 DISALLOW_COPY_AND_ASSIGN(InitializeClassVisitor); 1323 }; 1324 1325 // Returns true if the class loader is null, ie the class loader is the boot strap class loader. 1326 bool IsBootStrapClassLoaded() REQUIRES_SHARED(Locks::mutator_lock_); 1327 ImTableEntrySize(PointerSize pointer_size)1328 static size_t ImTableEntrySize(PointerSize pointer_size) { 1329 return static_cast<size_t>(pointer_size); 1330 } 1331 VTableEntrySize(PointerSize pointer_size)1332 static size_t VTableEntrySize(PointerSize pointer_size) { 1333 return static_cast<size_t>(pointer_size); 1334 } 1335 1336 ALWAYS_INLINE ArraySlice<ArtMethod> GetDirectMethodsSliceUnchecked(PointerSize pointer_size) 1337 REQUIRES_SHARED(Locks::mutator_lock_); 1338 1339 ALWAYS_INLINE ArraySlice<ArtMethod> GetVirtualMethodsSliceUnchecked(PointerSize pointer_size) 1340 REQUIRES_SHARED(Locks::mutator_lock_); 1341 1342 ALWAYS_INLINE ArraySlice<ArtMethod> GetDeclaredMethodsSliceUnchecked(PointerSize pointer_size) 1343 REQUIRES_SHARED(Locks::mutator_lock_); 1344 1345 ALWAYS_INLINE ArraySlice<ArtMethod> GetDeclaredVirtualMethodsSliceUnchecked( 1346 PointerSize pointer_size) 1347 REQUIRES_SHARED(Locks::mutator_lock_); 1348 1349 ALWAYS_INLINE ArraySlice<ArtMethod> GetCopiedMethodsSliceUnchecked(PointerSize pointer_size) 1350 REQUIRES_SHARED(Locks::mutator_lock_); 1351 1352 static std::string PrettyDescriptor(ObjPtr<mirror::Class> klass) 1353 REQUIRES_SHARED(Locks::mutator_lock_); 1354 std::string PrettyDescriptor() 1355 REQUIRES_SHARED(Locks::mutator_lock_); 1356 // Returns a human-readable form of the name of the given class. 1357 // Given String.class, the output would be "java.lang.Class<java.lang.String>". 1358 static std::string PrettyClass(ObjPtr<mirror::Class> c) 1359 REQUIRES_SHARED(Locks::mutator_lock_); 1360 std::string PrettyClass() 1361 REQUIRES_SHARED(Locks::mutator_lock_); 1362 // Returns a human-readable form of the name of the given class with its class loader. 1363 static std::string PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) 1364 REQUIRES_SHARED(Locks::mutator_lock_); 1365 std::string PrettyClassAndClassLoader() 1366 REQUIRES_SHARED(Locks::mutator_lock_); 1367 1368 // Fix up all of the native pointers in the class by running them through the visitor. Only sets 1369 // the corresponding entry in dest if visitor(obj) != obj to prevent dirty memory. Dest should be 1370 // initialized to a copy of *this to prevent issues. Does not visit the ArtMethod and ArtField 1371 // roots. 1372 template <VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, typename Visitor> 1373 void FixupNativePointers(Class* dest, PointerSize pointer_size, const Visitor& visitor) 1374 REQUIRES_SHARED(Locks::mutator_lock_); 1375 1376 // Get or create the various jni id arrays in a lock-less thread safe manner. 1377 static bool EnsureMethodIds(Handle<Class> h_this) 1378 REQUIRES_SHARED(Locks::mutator_lock_); 1379 ObjPtr<Object> GetMethodIds() REQUIRES_SHARED(Locks::mutator_lock_); 1380 static bool EnsureStaticFieldIds(Handle<Class> h_this) 1381 REQUIRES_SHARED(Locks::mutator_lock_); 1382 ObjPtr<Object> GetStaticFieldIds() REQUIRES_SHARED(Locks::mutator_lock_); 1383 static bool EnsureInstanceFieldIds(Handle<Class> h_this) 1384 REQUIRES_SHARED(Locks::mutator_lock_); 1385 ObjPtr<Object> GetInstanceFieldIds() REQUIRES_SHARED(Locks::mutator_lock_); 1386 1387 // Calculate the index in the ifields_, methods_ or sfields_ arrays a method is located at. This 1388 // is to be used with the above Get{,OrCreate}...Ids functions. 1389 size_t GetStaticFieldIdOffset(ArtField* field) 1390 REQUIRES_SHARED(Locks::mutator_lock_); 1391 size_t GetInstanceFieldIdOffset(ArtField* field) 1392 REQUIRES_SHARED(Locks::mutator_lock_); 1393 size_t GetMethodIdOffset(ArtMethod* method, PointerSize pointer_size) 1394 REQUIRES_SHARED(Locks::mutator_lock_); 1395 1396 // Returns whether the class should be visible to an app. 1397 // Notorious example is java.lang.ClassValue, which was added in Android U and proguarding tools 1398 // used that as justification to remove computeValue method implementation. Such an app running 1399 // on U+ will fail with AbstractMethodError as computeValue is not implemented. 1400 // See b/259501764. 1401 bool CheckIsVisibleWithTargetSdk(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_); 1402 1403 private: 1404 template <typename T, VerifyObjectFlags kVerifyFlags, typename Visitor> 1405 void FixupNativePointer( 1406 Class* dest, PointerSize pointer_size, const Visitor& visitor, MemberOffset member_offset) 1407 REQUIRES_SHARED(Locks::mutator_lock_); 1408 1409 ALWAYS_INLINE static ArraySlice<ArtMethod> GetMethodsSliceRangeUnchecked( 1410 LengthPrefixedArray<ArtMethod>* methods, 1411 PointerSize pointer_size, 1412 uint32_t start_offset, 1413 uint32_t end_offset) 1414 REQUIRES_SHARED(Locks::mutator_lock_); 1415 1416 template <bool throw_on_failure> 1417 bool ResolvedFieldAccessTest(ObjPtr<Class> access_to, 1418 ArtField* field, 1419 ObjPtr<DexCache> dex_cache, 1420 uint32_t field_idx) 1421 REQUIRES_SHARED(Locks::mutator_lock_); 1422 1423 bool IsArrayAssignableFromArray(ObjPtr<Class> klass) REQUIRES_SHARED(Locks::mutator_lock_); 1424 bool IsAssignableFromArray(ObjPtr<Class> klass) REQUIRES_SHARED(Locks::mutator_lock_); 1425 1426 void CheckObjectAlloc() REQUIRES_SHARED(Locks::mutator_lock_); 1427 1428 // Unchecked editions is for root visiting. 1429 LengthPrefixedArray<ArtField>* GetSFieldsPtrUnchecked() REQUIRES_SHARED(Locks::mutator_lock_); 1430 IterationRange<StrideIterator<ArtField>> GetSFieldsUnchecked() 1431 REQUIRES_SHARED(Locks::mutator_lock_); 1432 LengthPrefixedArray<ArtField>* GetIFieldsPtrUnchecked() REQUIRES_SHARED(Locks::mutator_lock_); 1433 IterationRange<StrideIterator<ArtField>> GetIFieldsUnchecked() 1434 REQUIRES_SHARED(Locks::mutator_lock_); 1435 1436 // The index in the methods_ array where the first declared virtual method is. 1437 ALWAYS_INLINE uint32_t GetVirtualMethodsStartOffset() REQUIRES_SHARED(Locks::mutator_lock_); 1438 1439 // The index in the methods_ array where the first direct method is. 1440 ALWAYS_INLINE uint32_t GetDirectMethodsStartOffset() REQUIRES_SHARED(Locks::mutator_lock_); 1441 1442 bool ProxyDescriptorEquals(ObjPtr<mirror::Class> match) REQUIRES_SHARED(Locks::mutator_lock_); 1443 bool ProxyDescriptorEquals(std::string_view match) REQUIRES_SHARED(Locks::mutator_lock_); 1444 static uint32_t UpdateHashForProxyClass(uint32_t hash, ObjPtr<mirror::Class> proxy_class) 1445 REQUIRES_SHARED(Locks::mutator_lock_); 1446 1447 template<VerifyObjectFlags kVerifyFlags> 1448 void GetAccessFlagsDCheck() REQUIRES_SHARED(Locks::mutator_lock_); 1449 1450 // Check that the pointer size matches the one in the class linker. 1451 ALWAYS_INLINE static void CheckPointerSize(PointerSize pointer_size); 1452 1453 template <VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, typename Visitor> 1454 void VisitStaticFieldsReferences(const Visitor& visitor) HOT_ATTR; 1455 1456 template <bool kVisitNativeRoots, 1457 VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 1458 ReadBarrierOption kReadBarrierOption = kWithReadBarrier, 1459 typename Visitor> 1460 void VisitReferences(ObjPtr<Class> klass, const Visitor& visitor) 1461 REQUIRES_SHARED(Locks::mutator_lock_); 1462 1463 // Helper to set the status without any validity cheks. 1464 void SetStatusInternal(ClassStatus new_status) 1465 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 1466 1467 // 'Class' Object Fields 1468 // Order governed by java field ordering. See art::ClassLinker::LinkFieldsHelper::LinkFields. 1469 1470 // Defining class loader, or null for the "bootstrap" system loader. 1471 HeapReference<ClassLoader> class_loader_; 1472 1473 // For array classes, the component class object for instanceof/checkcast 1474 // (for String[][][], this will be String[][]). null for non-array classes. 1475 HeapReference<Class> component_type_; 1476 1477 // DexCache of resolved constant pool entries (will be null for classes generated by the 1478 // runtime such as arrays and primitive classes). 1479 HeapReference<DexCache> dex_cache_; 1480 1481 // Extraneous class data that is not always needed. This field is allocated lazily and may 1482 // only be set with 'this' locked. This is synchronized on 'this'. 1483 // TODO(allight) We should probably synchronize it on something external or handle allocation in 1484 // some other (safe) way to prevent possible deadlocks. 1485 HeapReference<ClassExt> ext_data_; 1486 1487 // The interface table (iftable_) contains pairs of a interface class and an array of the 1488 // interface methods. There is one pair per interface supported by this class. That means one 1489 // pair for each interface we support directly, indirectly via superclass, or indirectly via a 1490 // superinterface. This will be null if neither we nor our superclass implement any interfaces. 1491 // 1492 // Why we need this: given "class Foo implements Face", declare "Face faceObj = new Foo()". 1493 // Invoke faceObj.blah(), where "blah" is part of the Face interface. We can't easily use a 1494 // single vtable. 1495 // 1496 // For every interface a concrete class implements, we create an array of the concrete vtable_ 1497 // methods for the methods in the interface. 1498 HeapReference<IfTable> iftable_; 1499 1500 // Descriptor for the class such as "java.lang.Class" or "[C". Lazily initialized by ComputeName 1501 HeapReference<String> name_; 1502 1503 // The superclass, or null if this is java.lang.Object or a primitive type. 1504 // 1505 // Note that interfaces have java.lang.Object as their 1506 // superclass. This doesn't match the expectations in JNI 1507 // GetSuperClass or java.lang.Class.getSuperClass() which need to 1508 // check for interfaces and return null. 1509 HeapReference<Class> super_class_; 1510 1511 // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is 1512 // copied in, and virtual methods from our class either replace those from the super or are 1513 // appended. For abstract classes, methods may be created in the vtable that aren't in 1514 // virtual_ methods_ for miranda methods. 1515 HeapReference<PointerArray> vtable_; 1516 1517 // instance fields 1518 // 1519 // These describe the layout of the contents of an Object. 1520 // Note that only the fields directly declared by this class are 1521 // listed in ifields; fields declared by a superclass are listed in 1522 // the superclass's Class.ifields. 1523 // 1524 // ArtFields are allocated as a length prefixed ArtField array, and not an array of pointers to 1525 // ArtFields. 1526 uint64_t ifields_; 1527 1528 // Pointer to an ArtMethod length-prefixed array. All the methods where this class is the place 1529 // where they are logically defined. This includes all private, static, final and virtual methods 1530 // as well as inherited default methods and miranda methods. 1531 // 1532 // The slice methods_ [0, virtual_methods_offset_) are the direct (static, private, init) methods 1533 // declared by this class. 1534 // 1535 // The slice methods_ [virtual_methods_offset_, copied_methods_offset_) are the virtual methods 1536 // declared by this class. 1537 // 1538 // The slice methods_ [copied_methods_offset_, |methods_|) are the methods that are copied from 1539 // interfaces such as miranda or default methods. These are copied for resolution purposes as this 1540 // class is where they are (logically) declared as far as the virtual dispatch is concerned. 1541 // 1542 // Note that this field is used by the native debugger as the unique identifier for the type. 1543 uint64_t methods_; 1544 1545 // Static fields length-prefixed array. 1546 uint64_t sfields_; 1547 1548 // Access flags; low 16 bits are defined by VM spec. 1549 uint32_t access_flags_; 1550 1551 // Class flags to help speed up visiting object references. 1552 uint32_t class_flags_; 1553 1554 // Total size of the Class instance; used when allocating storage on gc heap. 1555 // See also object_size_. 1556 uint32_t class_size_; 1557 1558 // Tid used to check for recursive <clinit> invocation. 1559 pid_t clinit_thread_id_; 1560 static_assert(sizeof(pid_t) == sizeof(int32_t), "java.lang.Class.clinitThreadId size check"); 1561 1562 // ClassDef index in dex file, -1 if no class definition such as an array. 1563 // TODO: really 16bits 1564 int32_t dex_class_def_idx_; 1565 1566 // Type index in dex file. 1567 // TODO: really 16bits 1568 int32_t dex_type_idx_; 1569 1570 // Number of instance fields that are object refs. Does not count object refs 1571 // in any super classes. 1572 uint32_t num_reference_instance_fields_; 1573 1574 // Number of static fields that are object refs, 1575 uint32_t num_reference_static_fields_; 1576 1577 // Total object size; used when allocating storage on gc heap. 1578 // (For interfaces and abstract classes this will be zero.) 1579 // See also class_size_. 1580 uint32_t object_size_; 1581 1582 // Aligned object size for allocation fast path. The value is max uint32_t if the object is 1583 // uninitialized or finalizable. Not currently used for variable sized objects. 1584 uint32_t object_size_alloc_fast_path_; 1585 1586 // The lower 16 bits contains a Primitive::Type value. The upper 16 1587 // bits contains the size shift of the primitive type. 1588 uint32_t primitive_type_; 1589 1590 // Bitmap of offsets of ifields. 1591 uint32_t reference_instance_offsets_; 1592 1593 // See the real definition in subtype_check_bits_and_status.h 1594 // typeof(status_) is actually SubtypeCheckBitsAndStatus. 1595 uint32_t status_; 1596 1597 // The offset of the first virtual method that is copied from an interface. This includes miranda, 1598 // default, and default-conflict methods. Having a hard limit of ((2 << 16) - 1) for methods 1599 // defined on a single class is well established in Java so we will use only uint16_t's here. 1600 uint16_t copied_methods_offset_; 1601 1602 // The offset of the first declared virtual methods in the methods_ array. 1603 uint16_t virtual_methods_offset_; 1604 1605 // The following data exist in real class objects. 1606 // Embedded Vtable length, for class object that's instantiable, fixed size. 1607 // uint32_t vtable_length_; 1608 // Embedded Imtable pointer, for class object that's not an interface, fixed size. 1609 // ImTableEntry embedded_imtable_; 1610 // Embedded Vtable, for class object that's not an interface, variable size. 1611 // VTableEntry embedded_vtable_[0]; 1612 // Static fields, variable size. 1613 // uint32_t fields_[0]; 1614 // Embedded bitmap of offsets of ifields, for classes that need more than 31 1615 // reference-offset bits. 'reference_instance_offsets_' stores the number of 1616 // 32-bit entries that hold the entire bitmap. We compute the offset of first 1617 // entry by subtracting this number from class_size_. 1618 // uint32_t reference_bitmap_[0]; 1619 1620 ART_FRIEND_TEST(DexCacheTest, TestResolvedFieldAccess); // For ResolvedFieldAccessTest 1621 friend struct art::ClassOffsets; // for verifying offset information 1622 friend class Object; // For VisitReferences 1623 friend class linker::ImageWriter; // For SetStatusInternal 1624 friend class art::RuntimeImageHelper; // For SetStatusInternal 1625 DISALLOW_IMPLICIT_CONSTRUCTORS(Class); 1626 }; 1627 1628 } // namespace mirror 1629 } // namespace art 1630 1631 #endif // ART_RUNTIME_MIRROR_CLASS_H_ 1632