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