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