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