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