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