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