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