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 "dex_file.h" 21 #include "gc_root.h" 22 #include "gc/allocator_type.h" 23 #include "invoke_type.h" 24 #include "modifiers.h" 25 #include "object.h" 26 #include "object_array.h" 27 #include "object_callbacks.h" 28 #include "primitive.h" 29 #include "read_barrier_option.h" 30 31 /* 32 * A magic value for refOffsets. Ignore the bits and walk the super 33 * chain when this is the value. 34 * [This is an unlikely "natural" value, since it would be 30 non-ref instance 35 * fields followed by 2 ref instance fields.] 36 */ 37 #define CLASS_WALK_SUPER 3U 38 #define CLASS_BITS_PER_WORD (sizeof(uint32_t) * 8) 39 #define CLASS_OFFSET_ALIGNMENT 4 40 #define CLASS_HIGH_BIT (1U << (CLASS_BITS_PER_WORD - 1)) 41 /* 42 * Given an offset, return the bit number which would encode that offset. 43 * Local use only. 44 */ 45 #define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \ 46 ((unsigned int)(byteOffset) / \ 47 CLASS_OFFSET_ALIGNMENT) 48 /* 49 * Is the given offset too large to be encoded? 50 */ 51 #define CLASS_CAN_ENCODE_OFFSET(byteOffset) \ 52 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD) 53 /* 54 * Return a single bit, encoding the offset. 55 * Undefined if the offset is too large, as defined above. 56 */ 57 #define CLASS_BIT_FROM_OFFSET(byteOffset) \ 58 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset)) 59 /* 60 * Return an offset, given a bit number as returned from CLZ. 61 */ 62 #define CLASS_OFFSET_FROM_CLZ(rshift) \ 63 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT)) 64 65 #ifndef IMT_SIZE 66 #error IMT_SIZE not defined 67 #endif 68 69 namespace art { 70 71 struct ClassOffsets; 72 class Signature; 73 class StringPiece; 74 template<size_t kNumReferences> class PACKED(4) StackHandleScope; 75 76 namespace mirror { 77 78 class ArtField; 79 class ArtMethod; 80 class ClassLoader; 81 class DexCache; 82 class IfTable; 83 84 // C++ mirror of java.lang.Class 85 class MANAGED Class FINAL : public Object { 86 public: 87 // Interface method table size. Increasing this value reduces the chance of two interface methods 88 // colliding in the interface method table but increases the size of classes that implement 89 // (non-marker) interfaces. 90 static constexpr size_t kImtSize = IMT_SIZE; 91 92 // imtable entry embedded in class object. 93 struct MANAGED ImTableEntry { 94 HeapReference<ArtMethod> method; 95 }; 96 97 // vtable entry embedded in class object. 98 struct MANAGED VTableEntry { 99 HeapReference<ArtMethod> method; 100 }; 101 102 // Class Status 103 // 104 // kStatusNotReady: If a Class cannot be found in the class table by 105 // FindClass, it allocates an new one with AllocClass in the 106 // kStatusNotReady and calls LoadClass. Note if it does find a 107 // class, it may not be kStatusResolved and it will try to push it 108 // forward toward kStatusResolved. 109 // 110 // kStatusIdx: LoadClass populates with Class with information from 111 // the DexFile, moving the status to kStatusIdx, indicating that the 112 // Class value in super_class_ has not been populated. The new Class 113 // can then be inserted into the classes table. 114 // 115 // kStatusLoaded: After taking a lock on Class, the ClassLinker will 116 // attempt to move a kStatusIdx class forward to kStatusLoaded by 117 // using ResolveClass to initialize the super_class_ and ensuring the 118 // interfaces are resolved. 119 // 120 // kStatusResolving: Class is just cloned with the right size from 121 // temporary class that's acting as a placeholder for linking. The old 122 // class will be retired. New class is set to this status first before 123 // moving on to being resolved. 124 // 125 // kStatusResolved: Still holding the lock on Class, the ClassLinker 126 // shows linking is complete and fields of the Class populated by making 127 // it kStatusResolved. Java allows circularities of the form where a super 128 // class has a field that is of the type of the sub class. We need to be able 129 // to fully resolve super classes while resolving types for fields. 130 // 131 // kStatusRetryVerificationAtRuntime: The verifier sets a class to 132 // this state if it encounters a soft failure at compile time. This 133 // often happens when there are unresolved classes in other dex 134 // files, and this status marks a class as needing to be verified 135 // again at runtime. 136 // 137 // TODO: Explain the other states 138 enum Status { 139 kStatusRetired = -2, 140 kStatusError = -1, 141 kStatusNotReady = 0, 142 kStatusIdx = 1, // Loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_. 143 kStatusLoaded = 2, // DEX idx values resolved. 144 kStatusResolving = 3, // Just cloned from temporary class object. 145 kStatusResolved = 4, // Part of linking. 146 kStatusVerifying = 5, // In the process of being verified. 147 kStatusRetryVerificationAtRuntime = 6, // Compile time verification failed, retry at runtime. 148 kStatusVerifyingAtRuntime = 7, // Retrying verification at runtime. 149 kStatusVerified = 8, // Logically part of linking; done pre-init. 150 kStatusInitializing = 9, // Class init in progress. 151 kStatusInitialized = 10, // Ready to go. 152 kStatusMax = 11, 153 }; 154 155 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 156 Status GetStatus() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 157 COMPILE_ASSERT(sizeof(Status) == sizeof(uint32_t), size_of_status_not_uint32); 158 return static_cast<Status>( 159 GetField32Volatile<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, status_))); 160 } 161 162 void SetStatus(Status new_status, Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 163 164 static MemberOffset StatusOffset() { 165 return OFFSET_OF_OBJECT_MEMBER(Class, status_); 166 } 167 168 // Returns true if the class has been retired. 169 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 170 bool IsRetired() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 171 return GetStatus<kVerifyFlags>() == kStatusRetired; 172 } 173 174 // Returns true if the class has failed to link. 175 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 176 bool IsErroneous() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 177 return GetStatus<kVerifyFlags>() == kStatusError; 178 } 179 180 // Returns true if the class has been loaded. 181 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 182 bool IsIdxLoaded() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 183 return GetStatus<kVerifyFlags>() >= kStatusIdx; 184 } 185 186 // Returns true if the class has been loaded. 187 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 188 bool IsLoaded() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 189 return GetStatus<kVerifyFlags>() >= kStatusLoaded; 190 } 191 192 // Returns true if the class has been linked. 193 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 194 bool IsResolved() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 195 return GetStatus<kVerifyFlags>() >= kStatusResolved; 196 } 197 198 // Returns true if the class was compile-time verified. 199 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 200 bool IsCompileTimeVerified() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 201 return GetStatus<kVerifyFlags>() >= kStatusRetryVerificationAtRuntime; 202 } 203 204 // Returns true if the class has been verified. 205 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 206 bool IsVerified() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 207 return GetStatus<kVerifyFlags>() >= kStatusVerified; 208 } 209 210 // Returns true if the class is initializing. 211 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 212 bool IsInitializing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 213 return GetStatus<kVerifyFlags>() >= kStatusInitializing; 214 } 215 216 // Returns true if the class is initialized. 217 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 218 bool IsInitialized() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 219 return GetStatus<kVerifyFlags>() == kStatusInitialized; 220 } 221 222 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 223 ALWAYS_INLINE uint32_t GetAccessFlags() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 224 225 void SetAccessFlags(uint32_t new_access_flags) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 226 227 // Returns true if the class is an interface. 228 ALWAYS_INLINE bool IsInterface() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 229 return (GetAccessFlags() & kAccInterface) != 0; 230 } 231 232 // Returns true if the class is declared public. 233 ALWAYS_INLINE bool IsPublic() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 234 return (GetAccessFlags() & kAccPublic) != 0; 235 } 236 237 // Returns true if the class is declared final. 238 ALWAYS_INLINE bool IsFinal() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 239 return (GetAccessFlags() & kAccFinal) != 0; 240 } 241 242 ALWAYS_INLINE bool IsFinalizable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 243 return (GetAccessFlags() & kAccClassIsFinalizable) != 0; 244 } 245 246 ALWAYS_INLINE void SetFinalizable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 247 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_)); 248 SetAccessFlags(flags | kAccClassIsFinalizable); 249 } 250 251 // Returns true if the class is abstract. 252 ALWAYS_INLINE bool IsAbstract() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 253 return (GetAccessFlags() & kAccAbstract) != 0; 254 } 255 256 // Returns true if the class is an annotation. 257 ALWAYS_INLINE bool IsAnnotation() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 258 return (GetAccessFlags() & kAccAnnotation) != 0; 259 } 260 261 // Returns true if the class is synthetic. 262 ALWAYS_INLINE bool IsSynthetic() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 263 return (GetAccessFlags() & kAccSynthetic) != 0; 264 } 265 266 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 267 bool IsTypeOfReferenceClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 268 return (GetAccessFlags<kVerifyFlags>() & kAccClassIsReference) != 0; 269 } 270 271 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 272 bool IsWeakReferenceClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 273 return (GetAccessFlags<kVerifyFlags>() & kAccClassIsWeakReference) != 0; 274 } 275 276 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 277 bool IsSoftReferenceClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 278 return (GetAccessFlags<kVerifyFlags>() & kAccReferenceFlagsMask) == kAccClassIsReference; 279 } 280 281 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 282 bool IsFinalizerReferenceClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 283 return (GetAccessFlags<kVerifyFlags>() & kAccClassIsFinalizerReference) != 0; 284 } 285 286 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 287 bool IsPhantomReferenceClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 288 return (GetAccessFlags<kVerifyFlags>() & kAccClassIsPhantomReference) != 0; 289 } 290 291 // Can references of this type be assigned to by things of another type? For non-array types 292 // this is a matter of whether sub-classes may exist - which they can't if the type is final. 293 // For array classes, where all the classes are final due to there being no sub-classes, an 294 // Object[] may be assigned to by a String[] but a String[] may not be assigned to by other 295 // types as the component is final. 296 bool CannotBeAssignedFromOtherTypes() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 297 if (!IsArrayClass()) { 298 return IsFinal(); 299 } else { 300 Class* component = GetComponentType(); 301 if (component->IsPrimitive()) { 302 return true; 303 } else { 304 return component->CannotBeAssignedFromOtherTypes(); 305 } 306 } 307 } 308 309 // Returns true if this class is the placeholder and should retire and 310 // be replaced with a class with the right size for embedded imt/vtable. 311 bool IsTemp() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 312 Status s = GetStatus(); 313 return s < Status::kStatusResolving && ShouldHaveEmbeddedImtAndVTable(); 314 } 315 316 String* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); // Returns the cached name. 317 void SetName(String* name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); // Sets the cached name. 318 // Computes the name, then sets the cached value. 319 static String* ComputeName(Handle<Class> h_this) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 320 321 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 322 bool IsProxyClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 323 // Read access flags without using getter as whether something is a proxy can be check in 324 // any loaded state 325 // TODO: switch to a check if the super class is java.lang.reflect.Proxy? 326 uint32_t access_flags = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_)); 327 return (access_flags & kAccClassIsProxy) != 0; 328 } 329 330 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 331 Primitive::Type GetPrimitiveType() ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 332 333 void SetPrimitiveType(Primitive::Type new_type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 334 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t)); 335 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type); 336 } 337 338 // Returns true if the class is a primitive type. 339 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 340 bool IsPrimitive() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 341 return GetPrimitiveType<kVerifyFlags>() != Primitive::kPrimNot; 342 } 343 344 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 345 bool IsPrimitiveBoolean() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 346 return GetPrimitiveType<kVerifyFlags>() == Primitive::kPrimBoolean; 347 } 348 349 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 350 bool IsPrimitiveByte() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 351 return GetPrimitiveType<kVerifyFlags>() == Primitive::kPrimByte; 352 } 353 354 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 355 bool IsPrimitiveChar() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 356 return GetPrimitiveType<kVerifyFlags>() == Primitive::kPrimChar; 357 } 358 359 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 360 bool IsPrimitiveShort() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 361 return GetPrimitiveType<kVerifyFlags>() == Primitive::kPrimShort; 362 } 363 364 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 365 bool IsPrimitiveInt() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 366 return GetPrimitiveType() == Primitive::kPrimInt; 367 } 368 369 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 370 bool IsPrimitiveLong() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 371 return GetPrimitiveType<kVerifyFlags>() == Primitive::kPrimLong; 372 } 373 374 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 375 bool IsPrimitiveFloat() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 376 return GetPrimitiveType<kVerifyFlags>() == Primitive::kPrimFloat; 377 } 378 379 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 380 bool IsPrimitiveDouble() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 381 return GetPrimitiveType<kVerifyFlags>() == Primitive::kPrimDouble; 382 } 383 384 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 385 bool IsPrimitiveVoid() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 386 return GetPrimitiveType<kVerifyFlags>() == Primitive::kPrimVoid; 387 } 388 389 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 390 bool IsPrimitiveArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 391 return IsArrayClass<kVerifyFlags>() && 392 GetComponentType<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>()-> 393 IsPrimitive(); 394 } 395 396 // Depth of class from java.lang.Object 397 uint32_t Depth() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 398 uint32_t depth = 0; 399 for (Class* klass = this; klass->GetSuperClass() != NULL; klass = klass->GetSuperClass()) { 400 depth++; 401 } 402 return depth; 403 } 404 405 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 406 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 407 bool IsArrayClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 408 return GetComponentType<kVerifyFlags, kReadBarrierOption>() != NULL; 409 } 410 411 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 412 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 413 bool IsClassClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 414 415 bool IsStringClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 416 417 bool IsThrowableClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 418 419 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 420 bool IsArtFieldClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 421 422 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 423 bool IsArtMethodClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 424 425 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 426 bool IsReferenceClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 427 428 static MemberOffset ComponentTypeOffset() { 429 return OFFSET_OF_OBJECT_MEMBER(Class, component_type_); 430 } 431 432 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 433 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 434 Class* GetComponentType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 435 return GetFieldObject<Class, kVerifyFlags, kReadBarrierOption>(ComponentTypeOffset()); 436 } 437 438 void SetComponentType(Class* new_component_type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 439 DCHECK(GetComponentType() == NULL); 440 DCHECK(new_component_type != NULL); 441 // Component type is invariant: use non-transactional mode without check. 442 SetFieldObject<false, false>(ComponentTypeOffset(), new_component_type); 443 } 444 445 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 446 size_t GetComponentSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 447 return Primitive::ComponentSize( 448 GetComponentType<kDefaultVerifyFlags, kReadBarrierOption>()->GetPrimitiveType()); 449 } 450 451 bool IsObjectClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 452 return !IsPrimitive() && GetSuperClass() == NULL; 453 } 454 bool IsInstantiable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 455 return (!IsPrimitive() && !IsInterface() && !IsAbstract()) || ((IsAbstract()) && IsArrayClass()); 456 } 457 458 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 459 bool IsObjectArrayClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 460 return GetComponentType<kVerifyFlags>() != nullptr && !GetComponentType<kVerifyFlags>()->IsPrimitive(); 461 } 462 463 // Creates a raw object instance but does not invoke the default constructor. 464 template<bool kIsInstrumented, bool kCheckAddFinalizer = true> 465 ALWAYS_INLINE Object* Alloc(Thread* self, gc::AllocatorType allocator_type) 466 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 467 468 Object* AllocObject(Thread* self) 469 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 470 Object* AllocNonMovableObject(Thread* self) 471 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 472 473 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 474 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 475 bool IsVariableSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 476 // Classes and arrays vary in size, and so the object_size_ field cannot 477 // be used to Get their instance size 478 return IsClassClass<kVerifyFlags, kReadBarrierOption>() || 479 IsArrayClass<kVerifyFlags, kReadBarrierOption>(); 480 } 481 482 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 483 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 484 uint32_t SizeOf() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 485 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_)); 486 } 487 488 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 489 uint32_t GetClassSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 490 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_)); 491 } 492 493 void SetClassSize(uint32_t new_class_size) 494 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 495 496 // Compute how many bytes would be used a class with the given elements. 497 static uint32_t ComputeClassSize(bool has_embedded_tables, 498 uint32_t num_vtable_entries, 499 uint32_t num_32bit_static_fields, 500 uint32_t num_64bit_static_fields, 501 uint32_t num_ref_static_fields); 502 503 // The size of java.lang.Class.class. 504 static uint32_t ClassClassSize() { 505 // The number of vtable entries in java.lang.Class. 506 uint32_t vtable_entries = Object::kVTableLength + 66; 507 return ComputeClassSize(true, vtable_entries, 0, 1, 0); 508 } 509 510 // The size of a java.lang.Class representing a primitive such as int.class. 511 static uint32_t PrimitiveClassSize() { 512 return ComputeClassSize(false, 0, 0, 0, 0); 513 } 514 515 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, 516 ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 517 uint32_t GetObjectSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 518 519 void SetObjectSize(uint32_t new_object_size) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 520 DCHECK(!IsVariableSize()); 521 // Not called within a transaction. 522 return SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size); 523 } 524 525 void SetObjectSizeWithoutChecks(uint32_t new_object_size) 526 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 527 // Not called within a transaction. 528 return SetField32<false, false, kVerifyNone>( 529 OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size); 530 } 531 532 // Returns true if this class is in the same packages as that class. 533 bool IsInSamePackage(Class* that) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 534 535 static bool IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2); 536 537 // Returns true if this class can access that class. 538 bool CanAccess(Class* that) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 539 return that->IsPublic() || this->IsInSamePackage(that); 540 } 541 542 // Can this class access a member in the provided class with the provided member access flags? 543 // Note that access to the class isn't checked in case the declaring class is protected and the 544 // method has been exposed by a public sub-class 545 bool CanAccessMember(Class* access_to, uint32_t member_flags) 546 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 547 // Classes can access all of their own members 548 if (this == access_to) { 549 return true; 550 } 551 // Public members are trivially accessible 552 if (member_flags & kAccPublic) { 553 return true; 554 } 555 // Private members are trivially not accessible 556 if (member_flags & kAccPrivate) { 557 return false; 558 } 559 // Check for protected access from a sub-class, which may or may not be in the same package. 560 if (member_flags & kAccProtected) { 561 if (!this->IsInterface() && this->IsSubClass(access_to)) { 562 return true; 563 } 564 } 565 // Allow protected access from other classes in the same package. 566 return this->IsInSamePackage(access_to); 567 } 568 569 // Can this class access a resolved field? 570 // Note that access to field's class is checked and this may require looking up the class 571 // referenced by the FieldId in the DexFile in case the declaring class is inaccessible. 572 bool CanAccessResolvedField(Class* access_to, ArtField* field, 573 DexCache* dex_cache, uint32_t field_idx) 574 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 575 bool CheckResolvedFieldAccess(Class* access_to, ArtField* field, 576 uint32_t field_idx) 577 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 578 579 // Can this class access a resolved method? 580 // Note that access to methods's class is checked and this may require looking up the class 581 // referenced by the MethodId in the DexFile in case the declaring class is inaccessible. 582 bool CanAccessResolvedMethod(Class* access_to, ArtMethod* resolved_method, 583 DexCache* dex_cache, uint32_t method_idx) 584 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 585 template <InvokeType throw_invoke_type> 586 bool CheckResolvedMethodAccess(Class* access_to, ArtMethod* resolved_method, 587 uint32_t method_idx) 588 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 589 590 bool IsSubClass(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 591 592 // Can src be assigned to this class? For example, String can be assigned to Object (by an 593 // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing 594 // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface 595 // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign 596 // to themselves. Classes for primitive types may not assign to each other. 597 ALWAYS_INLINE bool IsAssignableFrom(Class* src) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 598 DCHECK(src != NULL); 599 if (this == src) { 600 // Can always assign to things of the same type. 601 return true; 602 } else if (IsObjectClass()) { 603 // Can assign any reference to java.lang.Object. 604 return !src->IsPrimitive(); 605 } else if (IsInterface()) { 606 return src->Implements(this); 607 } else if (src->IsArrayClass()) { 608 return IsAssignableFromArray(src); 609 } else { 610 return !src->IsInterface() && src->IsSubClass(this); 611 } 612 } 613 614 ALWAYS_INLINE Class* GetSuperClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 615 616 void SetSuperClass(Class *new_super_class) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 617 // Super class is assigned once, except during class linker initialization. 618 Class* old_super_class = GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_)); 619 DCHECK(old_super_class == nullptr || old_super_class == new_super_class); 620 DCHECK(new_super_class != nullptr); 621 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class); 622 } 623 624 bool HasSuperClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 625 return GetSuperClass() != NULL; 626 } 627 628 static MemberOffset SuperClassOffset() { 629 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_)); 630 } 631 632 ClassLoader* GetClassLoader() ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 633 634 void SetClassLoader(ClassLoader* new_cl) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 635 636 static MemberOffset DexCacheOffset() { 637 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_)); 638 } 639 640 enum { 641 kDumpClassFullDetail = 1, 642 kDumpClassClassLoader = (1 << 1), 643 kDumpClassInitialized = (1 << 2), 644 }; 645 646 void DumpClass(std::ostream& os, int flags) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 647 648 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 649 DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 650 651 // Also updates the dex_cache_strings_ variable from new_dex_cache. 652 void SetDexCache(DexCache* new_dex_cache) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 653 654 ALWAYS_INLINE ObjectArray<ArtMethod>* GetDirectMethods() 655 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 656 657 void SetDirectMethods(ObjectArray<ArtMethod>* new_direct_methods) 658 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 659 660 ALWAYS_INLINE ArtMethod* GetDirectMethod(int32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 661 662 void SetDirectMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t 663 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 664 665 // Returns the number of static, private, and constructor methods. 666 uint32_t NumDirectMethods() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 667 668 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 669 ALWAYS_INLINE ObjectArray<ArtMethod>* GetVirtualMethods() 670 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 671 672 ALWAYS_INLINE void SetVirtualMethods(ObjectArray<ArtMethod>* new_virtual_methods) 673 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 674 675 // Returns the number of non-inherited virtual methods. 676 ALWAYS_INLINE uint32_t NumVirtualMethods() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 677 678 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 679 ArtMethod* GetVirtualMethod(uint32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 680 681 ArtMethod* GetVirtualMethodDuringLinking(uint32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 682 683 void SetVirtualMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t 684 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 685 686 ALWAYS_INLINE ObjectArray<ArtMethod>* GetVTable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 687 688 ALWAYS_INLINE ObjectArray<ArtMethod>* GetVTableDuringLinking() 689 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 690 691 void SetVTable(ObjectArray<ArtMethod>* new_vtable) 692 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 693 694 static MemberOffset VTableOffset() { 695 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_); 696 } 697 698 static MemberOffset EmbeddedImTableOffset() { 699 return MemberOffset(sizeof(Class)); 700 } 701 702 static MemberOffset EmbeddedVTableLengthOffset() { 703 return MemberOffset(sizeof(Class) + kImtSize * sizeof(mirror::Class::ImTableEntry)); 704 } 705 706 static MemberOffset EmbeddedVTableOffset() { 707 return MemberOffset(sizeof(Class) + kImtSize * sizeof(ImTableEntry) + sizeof(int32_t)); 708 } 709 710 bool ShouldHaveEmbeddedImtAndVTable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 711 return IsInstantiable(); 712 } 713 714 bool HasVTable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 715 716 ArtMethod* GetEmbeddedImTableEntry(uint32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 717 718 void SetEmbeddedImTableEntry(uint32_t i, ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 719 720 int32_t GetVTableLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 721 722 ArtMethod* GetVTableEntry(uint32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 723 724 int32_t GetEmbeddedVTableLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 725 726 void SetEmbeddedVTableLength(int32_t len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 727 728 ArtMethod* GetEmbeddedVTableEntry(uint32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 729 730 void SetEmbeddedVTableEntry(uint32_t i, ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 731 732 void PopulateEmbeddedImtAndVTable(StackHandleScope<kImtSize>* imt_handle_scope) 733 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 734 735 // Given a method implemented by this class but potentially from a super class, return the 736 // specific implementation method for this class. 737 ArtMethod* FindVirtualMethodForVirtual(ArtMethod* method) 738 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 739 740 // Given a method implemented by this class' super class, return the specific implementation 741 // method for this class. 742 ArtMethod* FindVirtualMethodForSuper(ArtMethod* method) 743 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 744 745 // Given a method implemented by this class, but potentially from a 746 // super class or interface, return the specific implementation 747 // method for this class. 748 ArtMethod* FindVirtualMethodForInterface(ArtMethod* method) 749 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE; 750 751 ArtMethod* FindVirtualMethodForVirtualOrInterface(ArtMethod* method) 752 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 753 754 ArtMethod* FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) 755 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 756 757 ArtMethod* FindInterfaceMethod(const StringPiece& name, const Signature& signature) 758 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 759 760 ArtMethod* FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) 761 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 762 763 ArtMethod* FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) 764 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 765 766 ArtMethod* FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) 767 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 768 769 ArtMethod* FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) 770 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 771 772 ArtMethod* FindDirectMethod(const StringPiece& name, const StringPiece& signature) 773 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 774 775 ArtMethod* FindDirectMethod(const StringPiece& name, const Signature& signature) 776 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 777 778 ArtMethod* FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) 779 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 780 781 ArtMethod* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) 782 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 783 784 ArtMethod* FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) 785 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 786 787 ArtMethod* FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) 788 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 789 790 ArtMethod* FindVirtualMethod(const StringPiece& name, const StringPiece& signature) 791 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 792 793 ArtMethod* FindVirtualMethod(const StringPiece& name, const Signature& signature) 794 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 795 796 ArtMethod* FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) 797 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 798 799 ArtMethod* FindClassInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 800 801 ALWAYS_INLINE int32_t GetIfTableCount() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 802 803 ALWAYS_INLINE IfTable* GetIfTable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 804 805 ALWAYS_INLINE void SetIfTable(IfTable* new_iftable) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 806 807 // Get instance fields of the class (See also GetSFields). 808 ObjectArray<ArtField>* GetIFields() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 809 810 void SetIFields(ObjectArray<ArtField>* new_ifields) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 811 812 uint32_t NumInstanceFields() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 813 814 ArtField* GetInstanceField(uint32_t i) // TODO: uint16_t 815 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 816 817 void SetInstanceField(uint32_t i, ArtField* f) // TODO: uint16_t 818 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 819 820 // Returns the number of instance fields containing reference types. 821 uint32_t NumReferenceInstanceFields() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 822 DCHECK(IsResolved() || IsErroneous()); 823 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_)); 824 } 825 826 uint32_t NumReferenceInstanceFieldsDuringLinking() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 827 DCHECK(IsLoaded() || IsErroneous()); 828 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_)); 829 } 830 831 void SetNumReferenceInstanceFields(uint32_t new_num) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 832 // Not called within a transaction. 833 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num); 834 } 835 836 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 837 uint32_t GetReferenceInstanceOffsets() ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 838 839 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets) 840 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 841 842 // Get the offset of the first reference instance field. Other reference instance fields follow. 843 MemberOffset GetFirstReferenceInstanceFieldOffset() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 844 845 // Returns the number of static fields containing reference types. 846 uint32_t NumReferenceStaticFields() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 847 DCHECK(IsResolved() || IsErroneous()); 848 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_)); 849 } 850 851 uint32_t NumReferenceStaticFieldsDuringLinking() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 852 DCHECK(IsLoaded() || IsErroneous() || IsRetired()); 853 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_)); 854 } 855 856 void SetNumReferenceStaticFields(uint32_t new_num) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 857 // Not called within a transaction. 858 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num); 859 } 860 861 // Get the offset of the first reference static field. Other reference static fields follow. 862 MemberOffset GetFirstReferenceStaticFieldOffset() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 863 864 // Get the offset of the first reference static field. Other reference static fields follow. 865 MemberOffset GetFirstReferenceStaticFieldOffsetDuringLinking() 866 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 867 868 // Gets the static fields of the class. 869 ObjectArray<ArtField>* GetSFields() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 870 871 void SetSFields(ObjectArray<ArtField>* new_sfields) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 872 873 uint32_t NumStaticFields() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 874 875 // TODO: uint16_t 876 ArtField* GetStaticField(uint32_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 877 878 // TODO: uint16_t 879 void SetStaticField(uint32_t i, ArtField* f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 880 881 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 882 uint32_t GetReferenceStaticOffsets() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 883 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_)); 884 } 885 886 void SetReferenceStaticOffsets(uint32_t new_reference_offsets) 887 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 888 889 // Find a static or instance field using the JLS resolution order 890 static ArtField* FindField(Thread* self, Handle<Class> klass, const StringPiece& name, 891 const StringPiece& type) 892 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 893 894 // Finds the given instance field in this class or a superclass. 895 ArtField* FindInstanceField(const StringPiece& name, const StringPiece& type) 896 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 897 898 // Finds the given instance field in this class or a superclass, only searches classes that 899 // have the same dex cache. 900 ArtField* FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) 901 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 902 903 ArtField* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) 904 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 905 906 ArtField* FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) 907 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 908 909 // Finds the given static field in this class or a superclass. 910 static ArtField* FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name, 911 const StringPiece& type) 912 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 913 914 // Finds the given static field in this class or superclass, only searches classes that 915 // have the same dex cache. 916 static ArtField* FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache, 917 uint32_t dex_field_idx) 918 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 919 920 ArtField* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) 921 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 922 923 ArtField* FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) 924 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 925 926 pid_t GetClinitThreadId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 927 DCHECK(IsIdxLoaded() || IsErroneous()); 928 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_)); 929 } 930 931 void SetClinitThreadId(pid_t new_clinit_thread_id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 932 933 Class* GetVerifyErrorClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 934 // DCHECK(IsErroneous()); 935 return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_)); 936 } 937 938 uint16_t GetDexClassDefIndex() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 939 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_class_def_idx_)); 940 } 941 942 void SetDexClassDefIndex(uint16_t class_def_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 943 // Not called within a transaction. 944 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_class_def_idx_), class_def_idx); 945 } 946 947 uint16_t GetDexTypeIndex() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 948 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_)); 949 } 950 951 void SetDexTypeIndex(uint16_t type_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 952 // Not called within a transaction. 953 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), type_idx); 954 } 955 956 static Class* GetJavaLangClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 957 DCHECK(!java_lang_Class_.IsNull()); 958 return java_lang_Class_.Read(); 959 } 960 961 // Can't call this SetClass or else gets called instead of Object::SetClass in places. 962 static void SetClassClass(Class* java_lang_Class) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 963 static void ResetClass(); 964 static void VisitRoots(RootCallback* callback, void* arg) 965 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 966 967 // When class is verified, set the kAccPreverified flag on each method. 968 void SetPreverifiedFlagOnAllMethods() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 969 970 template <bool kVisitClass, typename Visitor> 971 void VisitReferences(mirror::Class* klass, const Visitor& visitor) 972 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 973 974 // Visit references within the embedded tables of the class. 975 // TODO: remove NO_THREAD_SAFETY_ANALYSIS when annotalysis handles visitors better. 976 template<typename Visitor> 977 void VisitEmbeddedImtAndVTable(const Visitor& visitor) NO_THREAD_SAFETY_ANALYSIS; 978 979 // Get the descriptor of the class. In a few cases a std::string is required, rather than 980 // always create one the storage argument is populated and its internal c_str() returned. We do 981 // this to avoid memory allocation in the common case. 982 const char* GetDescriptor(std::string* storage) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 983 984 const char* GetArrayDescriptor(std::string* storage) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 985 986 bool DescriptorEquals(const char* match) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 987 988 989 const DexFile::ClassDef* GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 990 991 ALWAYS_INLINE uint32_t NumDirectInterfaces() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 992 993 uint16_t GetDirectInterfaceTypeIdx(uint32_t idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 994 995 static mirror::Class* GetDirectInterface(Thread* self, Handle<mirror::Class> klass, uint32_t idx) 996 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 997 998 const char* GetSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 999 1000 std::string GetLocation() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1001 1002 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1003 1004 const DexFile::TypeList* GetInterfaceTypeList() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1005 1006 // Asserts we are initialized or initializing in the given thread. 1007 void AssertInitializedOrInitializingInThread(Thread* self) 1008 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1009 1010 Class* CopyOf(Thread* self, int32_t new_length, StackHandleScope<kImtSize>* imt_handle_scope) 1011 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1012 1013 // For proxy class only. 1014 ObjectArray<Class>* GetInterfaces() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1015 1016 // For proxy class only. 1017 ObjectArray<ObjectArray<Class>>* GetThrows() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1018 1019 // For reference class only. 1020 MemberOffset GetDisableIntrinsicFlagOffset() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1021 MemberOffset GetSlowPathFlagOffset() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1022 bool GetSlowPathEnabled() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1023 void SetSlowPath(bool enabled) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1024 1025 ObjectArray<String>* GetDexCacheStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1026 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) 1027 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1028 static MemberOffset DexCacheStringsOffset() { 1029 return OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_strings_); 1030 } 1031 1032 // Used to initialize a class in the allocation code path to ensure it is guarded by a StoreStore 1033 // fence. 1034 class InitializeClassVisitor { 1035 public: 1036 explicit InitializeClassVisitor(uint32_t class_size) : class_size_(class_size) { 1037 } 1038 1039 void operator()(mirror::Object* obj, size_t usable_size) const 1040 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1041 1042 private: 1043 const uint32_t class_size_; 1044 1045 DISALLOW_COPY_AND_ASSIGN(InitializeClassVisitor); 1046 }; 1047 1048 // Returns true if the class loader is null, ie the class loader is the boot strap class loader. 1049 bool IsBootStrapClassLoaded() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1050 return GetClassLoader() == nullptr; 1051 } 1052 1053 private: 1054 void SetVerifyErrorClass(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1055 1056 template <bool throw_on_failure, bool use_referrers_cache> 1057 bool ResolvedFieldAccessTest(Class* access_to, ArtField* field, 1058 uint32_t field_idx, DexCache* dex_cache) 1059 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1060 template <bool throw_on_failure, bool use_referrers_cache, InvokeType throw_invoke_type> 1061 bool ResolvedMethodAccessTest(Class* access_to, ArtMethod* resolved_method, 1062 uint32_t method_idx, DexCache* dex_cache) 1063 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1064 1065 bool Implements(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1066 bool IsArrayAssignableFromArray(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1067 bool IsAssignableFromArray(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1068 1069 void CheckObjectAlloc() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1070 1071 // defining class loader, or NULL for the "bootstrap" system loader 1072 HeapReference<ClassLoader> class_loader_; 1073 1074 // For array classes, the component class object for instanceof/checkcast 1075 // (for String[][][], this will be String[][]). NULL for non-array classes. 1076 HeapReference<Class> component_type_; 1077 1078 // DexCache of resolved constant pool entries (will be NULL for classes generated by the 1079 // runtime such as arrays and primitive classes). 1080 HeapReference<DexCache> dex_cache_; 1081 1082 // Short cuts to dex_cache_ member for fast compiled code access. 1083 HeapReference<ObjectArray<String>> dex_cache_strings_; 1084 1085 // static, private, and <init> methods 1086 HeapReference<ObjectArray<ArtMethod>> direct_methods_; 1087 1088 // instance fields 1089 // 1090 // These describe the layout of the contents of an Object. 1091 // Note that only the fields directly declared by this class are 1092 // listed in ifields; fields declared by a superclass are listed in 1093 // the superclass's Class.ifields. 1094 // 1095 // All instance fields that refer to objects are guaranteed to be at 1096 // the beginning of the field list. num_reference_instance_fields_ 1097 // specifies the number of reference fields. 1098 HeapReference<ObjectArray<ArtField>> ifields_; 1099 1100 // The interface table (iftable_) contains pairs of a interface class and an array of the 1101 // interface methods. There is one pair per interface supported by this class. That means one 1102 // pair for each interface we support directly, indirectly via superclass, or indirectly via a 1103 // superinterface. This will be null if neither we nor our superclass implement any interfaces. 1104 // 1105 // Why we need this: given "class Foo implements Face", declare "Face faceObj = new Foo()". 1106 // Invoke faceObj.blah(), where "blah" is part of the Face interface. We can't easily use a 1107 // single vtable. 1108 // 1109 // For every interface a concrete class implements, we create an array of the concrete vtable_ 1110 // methods for the methods in the interface. 1111 HeapReference<IfTable> iftable_; 1112 1113 // Descriptor for the class such as "java.lang.Class" or "[C". Lazily initialized by ComputeName 1114 HeapReference<String> name_; 1115 1116 // Static fields 1117 HeapReference<ObjectArray<ArtField>> sfields_; 1118 1119 // The superclass, or NULL if this is java.lang.Object, an interface or primitive type. 1120 HeapReference<Class> super_class_; 1121 1122 // If class verify fails, we must return same error on subsequent tries. 1123 HeapReference<Class> verify_error_class_; 1124 1125 // Virtual methods defined in this class; invoked through vtable. 1126 HeapReference<ObjectArray<ArtMethod>> virtual_methods_; 1127 1128 // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is 1129 // copied in, and virtual methods from our class either replace those from the super or are 1130 // appended. For abstract classes, methods may be created in the vtable that aren't in 1131 // virtual_ methods_ for miranda methods. 1132 HeapReference<ObjectArray<ArtMethod>> vtable_; 1133 1134 // Access flags; low 16 bits are defined by VM spec. 1135 uint32_t access_flags_; 1136 1137 // Total size of the Class instance; used when allocating storage on gc heap. 1138 // See also object_size_. 1139 uint32_t class_size_; 1140 1141 // Tid used to check for recursive <clinit> invocation. 1142 pid_t clinit_thread_id_; 1143 1144 // ClassDef index in dex file, -1 if no class definition such as an array. 1145 // TODO: really 16bits 1146 int32_t dex_class_def_idx_; 1147 1148 // Type index in dex file. 1149 // TODO: really 16bits 1150 int32_t dex_type_idx_; 1151 1152 // Number of instance fields that are object refs. 1153 uint32_t num_reference_instance_fields_; 1154 1155 // Number of static fields that are object refs, 1156 uint32_t num_reference_static_fields_; 1157 1158 // Total object size; used when allocating storage on gc heap. 1159 // (For interfaces and abstract classes this will be zero.) 1160 // See also class_size_. 1161 uint32_t object_size_; 1162 1163 // Primitive type value, or Primitive::kPrimNot (0); set for generated primitive classes. 1164 Primitive::Type primitive_type_; 1165 1166 // Bitmap of offsets of ifields. 1167 uint32_t reference_instance_offsets_; 1168 1169 // Bitmap of offsets of sfields. 1170 uint32_t reference_static_offsets_; 1171 1172 // State of class initialization. 1173 Status status_; 1174 1175 // TODO: ? 1176 // initiating class loader list 1177 // NOTE: for classes with low serialNumber, these are unused, and the 1178 // values are kept in a table in gDvm. 1179 // InitiatingLoaderList initiating_loader_list_; 1180 1181 // The following data exist in real class objects. 1182 // Embedded Imtable, for class object that's not an interface, fixed size. 1183 // ImTableEntry embedded_imtable_[0]; 1184 // Embedded Vtable, for class object that's not an interface, variable size. 1185 // VTableEntry embedded_vtable_[0]; 1186 // Static fields, variable size. 1187 // uint32_t fields_[0]; 1188 1189 // java.lang.Class 1190 static GcRoot<Class> java_lang_Class_; 1191 1192 friend struct art::ClassOffsets; // for verifying offset information 1193 DISALLOW_IMPLICIT_CONSTRUCTORS(Class); 1194 }; 1195 1196 std::ostream& operator<<(std::ostream& os, const Class::Status& rhs); 1197 1198 } // namespace mirror 1199 } // namespace art 1200 1201 #endif // ART_RUNTIME_MIRROR_CLASS_H_ 1202