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