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