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