• 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_INL_H_
18 #define ART_RUNTIME_MIRROR_CLASS_INL_H_
19 
20 #include "class.h"
21 
22 #include "art_field-inl.h"
23 #include "art_method.h"
24 #include "art_method-inl.h"
25 #include "base/array_slice.h"
26 #include "base/length_prefixed_array.h"
27 #include "class_loader.h"
28 #include "common_throws.h"
29 #include "dex_cache.h"
30 #include "dex_file.h"
31 #include "gc/heap-inl.h"
32 #include "iftable.h"
33 #include "object_array-inl.h"
34 #include "read_barrier-inl.h"
35 #include "reference-inl.h"
36 #include "runtime.h"
37 #include "string.h"
38 #include "utils.h"
39 
40 namespace art {
41 namespace mirror {
42 
43 template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
GetObjectSize()44 inline uint32_t Class::GetObjectSize() {
45   // Note: Extra parentheses to avoid the comma being interpreted as macro parameter separator.
46   DCHECK((!IsVariableSize<kVerifyFlags, kReadBarrierOption>())) << " class=" << PrettyTypeOf(this);
47   return GetField32(ObjectSizeOffset());
48 }
49 
50 template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
GetSuperClass()51 inline Class* Class::GetSuperClass() {
52   // Can only get super class for loaded classes (hack for when runtime is
53   // initializing)
54   DCHECK(IsLoaded<kVerifyFlags>() ||
55          IsErroneous<kVerifyFlags>() ||
56          !Runtime::Current()->IsStarted()) << IsLoaded();
57   return GetFieldObject<Class, kVerifyFlags, kReadBarrierOption>(
58       OFFSET_OF_OBJECT_MEMBER(Class, super_class_));
59 }
60 
GetClassLoader()61 inline ClassLoader* Class::GetClassLoader() {
62   return GetFieldObject<ClassLoader>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_));
63 }
64 
65 template<VerifyObjectFlags kVerifyFlags>
GetDexCache()66 inline DexCache* Class::GetDexCache() {
67   return GetFieldObject<DexCache, kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_));
68 }
69 
GetCopiedMethodsStartOffset()70 inline uint32_t Class::GetCopiedMethodsStartOffset() {
71   return GetFieldShort(OFFSET_OF_OBJECT_MEMBER(Class, copied_methods_offset_));
72 }
73 
GetDirectMethodsStartOffset()74 inline uint32_t Class::GetDirectMethodsStartOffset() {
75   return 0;
76 }
77 
GetVirtualMethodsStartOffset()78 inline uint32_t Class::GetVirtualMethodsStartOffset() {
79   return GetFieldShort(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_offset_));
80 }
81 
82 template<VerifyObjectFlags kVerifyFlags>
GetDirectMethodsSlice(size_t pointer_size)83 inline ArraySlice<ArtMethod> Class::GetDirectMethodsSlice(size_t pointer_size) {
84   DCHECK(IsLoaded() || IsErroneous());
85   DCHECK(ValidPointerSize(pointer_size)) << pointer_size;
86   return GetDirectMethodsSliceUnchecked(pointer_size);
87 }
88 
GetDirectMethodsSliceUnchecked(size_t pointer_size)89 inline ArraySlice<ArtMethod> Class::GetDirectMethodsSliceUnchecked(size_t pointer_size) {
90   return ArraySlice<ArtMethod>(GetMethodsPtr(),
91                                GetDirectMethodsStartOffset(),
92                                GetVirtualMethodsStartOffset(),
93                                ArtMethod::Size(pointer_size),
94                                ArtMethod::Alignment(pointer_size));
95 }
96 
97 template<VerifyObjectFlags kVerifyFlags>
GetDeclaredMethodsSlice(size_t pointer_size)98 inline ArraySlice<ArtMethod> Class::GetDeclaredMethodsSlice(size_t pointer_size) {
99   DCHECK(IsLoaded() || IsErroneous());
100   DCHECK(ValidPointerSize(pointer_size)) << pointer_size;
101   return GetDeclaredMethodsSliceUnchecked(pointer_size);
102 }
103 
GetDeclaredMethodsSliceUnchecked(size_t pointer_size)104 inline ArraySlice<ArtMethod> Class::GetDeclaredMethodsSliceUnchecked(size_t pointer_size) {
105   return ArraySlice<ArtMethod>(GetMethodsPtr(),
106                                GetDirectMethodsStartOffset(),
107                                GetCopiedMethodsStartOffset(),
108                                ArtMethod::Size(pointer_size),
109                                ArtMethod::Alignment(pointer_size));
110 }
111 template<VerifyObjectFlags kVerifyFlags>
GetDeclaredVirtualMethodsSlice(size_t pointer_size)112 inline ArraySlice<ArtMethod> Class::GetDeclaredVirtualMethodsSlice(size_t pointer_size) {
113   DCHECK(IsLoaded() || IsErroneous());
114   DCHECK(ValidPointerSize(pointer_size)) << pointer_size;
115   return GetDeclaredVirtualMethodsSliceUnchecked(pointer_size);
116 }
117 
GetDeclaredVirtualMethodsSliceUnchecked(size_t pointer_size)118 inline ArraySlice<ArtMethod> Class::GetDeclaredVirtualMethodsSliceUnchecked(size_t pointer_size) {
119   return ArraySlice<ArtMethod>(GetMethodsPtr(),
120                                GetVirtualMethodsStartOffset(),
121                                GetCopiedMethodsStartOffset(),
122                                ArtMethod::Size(pointer_size),
123                                ArtMethod::Alignment(pointer_size));
124 }
125 
126 template<VerifyObjectFlags kVerifyFlags>
GetVirtualMethodsSlice(size_t pointer_size)127 inline ArraySlice<ArtMethod> Class::GetVirtualMethodsSlice(size_t pointer_size) {
128   DCHECK(IsLoaded() || IsErroneous());
129   DCHECK(ValidPointerSize(pointer_size)) << pointer_size;
130   return GetVirtualMethodsSliceUnchecked(pointer_size);
131 }
132 
GetVirtualMethodsSliceUnchecked(size_t pointer_size)133 inline ArraySlice<ArtMethod> Class::GetVirtualMethodsSliceUnchecked(size_t pointer_size) {
134   LengthPrefixedArray<ArtMethod>* methods = GetMethodsPtr();
135   return ArraySlice<ArtMethod>(methods,
136                                GetVirtualMethodsStartOffset(),
137                                NumMethods(),
138                                ArtMethod::Size(pointer_size),
139                                ArtMethod::Alignment(pointer_size));
140 }
141 
142 template<VerifyObjectFlags kVerifyFlags>
GetCopiedMethodsSlice(size_t pointer_size)143 inline ArraySlice<ArtMethod> Class::GetCopiedMethodsSlice(size_t pointer_size) {
144   DCHECK(IsLoaded() || IsErroneous());
145   DCHECK(ValidPointerSize(pointer_size)) << pointer_size;
146   return GetCopiedMethodsSliceUnchecked(pointer_size);
147 }
148 
GetCopiedMethodsSliceUnchecked(size_t pointer_size)149 inline ArraySlice<ArtMethod> Class::GetCopiedMethodsSliceUnchecked(size_t pointer_size) {
150   LengthPrefixedArray<ArtMethod>* methods = GetMethodsPtr();
151   return ArraySlice<ArtMethod>(methods,
152                                GetCopiedMethodsStartOffset(),
153                                NumMethods(),
154                                ArtMethod::Size(pointer_size),
155                                ArtMethod::Alignment(pointer_size));
156 }
157 
GetMethodsPtr()158 inline LengthPrefixedArray<ArtMethod>* Class::GetMethodsPtr() {
159   return reinterpret_cast<LengthPrefixedArray<ArtMethod>*>(
160       static_cast<uintptr_t>(GetField64(OFFSET_OF_OBJECT_MEMBER(Class, methods_))));
161 }
162 
163 template<VerifyObjectFlags kVerifyFlags>
GetMethodsSlice(size_t pointer_size)164 inline ArraySlice<ArtMethod> Class::GetMethodsSlice(size_t pointer_size) {
165   DCHECK(IsLoaded() || IsErroneous());
166   LengthPrefixedArray<ArtMethod>* methods = GetMethodsPtr();
167   return ArraySlice<ArtMethod>(methods,
168                                0,
169                                NumMethods(),
170                                ArtMethod::Size(pointer_size),
171                                ArtMethod::Alignment(pointer_size));
172 }
173 
174 
NumMethods()175 inline uint32_t Class::NumMethods() {
176   LengthPrefixedArray<ArtMethod>* methods = GetMethodsPtr();
177   return (methods == nullptr) ? 0 : methods->size();
178 }
179 
GetDirectMethodUnchecked(size_t i,size_t pointer_size)180 inline ArtMethod* Class::GetDirectMethodUnchecked(size_t i, size_t pointer_size) {
181   CheckPointerSize(pointer_size);
182   return &GetDirectMethodsSliceUnchecked(pointer_size).At(i);
183 }
184 
GetDirectMethod(size_t i,size_t pointer_size)185 inline ArtMethod* Class::GetDirectMethod(size_t i, size_t pointer_size) {
186   CheckPointerSize(pointer_size);
187   return &GetDirectMethodsSlice(pointer_size).At(i);
188 }
189 
SetMethodsPtr(LengthPrefixedArray<ArtMethod> * new_methods,uint32_t num_direct,uint32_t num_virtual)190 inline void Class::SetMethodsPtr(LengthPrefixedArray<ArtMethod>* new_methods,
191                                  uint32_t num_direct,
192                                  uint32_t num_virtual) {
193   DCHECK(GetMethodsPtr() == nullptr);
194   SetMethodsPtrUnchecked(new_methods, num_direct, num_virtual);
195 }
196 
197 
SetMethodsPtrUnchecked(LengthPrefixedArray<ArtMethod> * new_methods,uint32_t num_direct,uint32_t num_virtual)198 inline void Class::SetMethodsPtrUnchecked(LengthPrefixedArray<ArtMethod>* new_methods,
199                                           uint32_t num_direct,
200                                           uint32_t num_virtual) {
201   DCHECK_LE(num_direct + num_virtual, (new_methods == nullptr) ? 0 : new_methods->size());
202   SetMethodsPtrInternal(new_methods);
203   SetFieldShort<false>(OFFSET_OF_OBJECT_MEMBER(Class, copied_methods_offset_),
204                     dchecked_integral_cast<uint16_t>(num_direct + num_virtual));
205   SetFieldShort<false>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_offset_),
206                        dchecked_integral_cast<uint16_t>(num_direct));
207 }
208 
SetMethodsPtrInternal(LengthPrefixedArray<ArtMethod> * new_methods)209 inline void Class::SetMethodsPtrInternal(LengthPrefixedArray<ArtMethod>* new_methods) {
210   SetField64<false>(OFFSET_OF_OBJECT_MEMBER(Class, methods_),
211                     static_cast<uint64_t>(reinterpret_cast<uintptr_t>(new_methods)));
212 }
213 
214 template<VerifyObjectFlags kVerifyFlags>
GetVirtualMethod(size_t i,size_t pointer_size)215 inline ArtMethod* Class::GetVirtualMethod(size_t i, size_t pointer_size) {
216   CheckPointerSize(pointer_size);
217   DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>())
218       << PrettyClass(this) << " status=" << GetStatus();
219   return GetVirtualMethodUnchecked(i, pointer_size);
220 }
221 
GetVirtualMethodDuringLinking(size_t i,size_t pointer_size)222 inline ArtMethod* Class::GetVirtualMethodDuringLinking(size_t i, size_t pointer_size) {
223   CheckPointerSize(pointer_size);
224   DCHECK(IsLoaded() || IsErroneous());
225   return GetVirtualMethodUnchecked(i, pointer_size);
226 }
227 
GetVirtualMethodUnchecked(size_t i,size_t pointer_size)228 inline ArtMethod* Class::GetVirtualMethodUnchecked(size_t i, size_t pointer_size) {
229   CheckPointerSize(pointer_size);
230   return &GetVirtualMethodsSliceUnchecked(pointer_size).At(i);
231 }
232 
233 template<VerifyObjectFlags kVerifyFlags,
234          ReadBarrierOption kReadBarrierOption>
GetVTable()235 inline PointerArray* Class::GetVTable() {
236   DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>());
237   return GetFieldObject<PointerArray, kVerifyFlags, kReadBarrierOption>(
238       OFFSET_OF_OBJECT_MEMBER(Class, vtable_));
239 }
240 
GetVTableDuringLinking()241 inline PointerArray* Class::GetVTableDuringLinking() {
242   DCHECK(IsLoaded() || IsErroneous());
243   return GetFieldObject<PointerArray>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_));
244 }
245 
SetVTable(PointerArray * new_vtable)246 inline void Class::SetVTable(PointerArray* new_vtable) {
247   SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable);
248 }
249 
EmbeddedImTableEntryOffset(uint32_t i,size_t pointer_size)250 inline MemberOffset Class::EmbeddedImTableEntryOffset(uint32_t i, size_t pointer_size) {
251   DCHECK_LT(i, kImtSize);
252   return MemberOffset(
253       EmbeddedImTableOffset(pointer_size).Uint32Value() + i * ImTableEntrySize(pointer_size));
254 }
255 
256 template <VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
GetEmbeddedImTableEntry(uint32_t i,size_t pointer_size)257 inline ArtMethod* Class::GetEmbeddedImTableEntry(uint32_t i, size_t pointer_size) {
258   DCHECK((ShouldHaveEmbeddedImtAndVTable<kVerifyFlags, kReadBarrierOption>()));
259   return GetFieldPtrWithSize<ArtMethod*>(
260       EmbeddedImTableEntryOffset(i, pointer_size), pointer_size);
261 }
262 
263 template <VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
SetEmbeddedImTableEntry(uint32_t i,ArtMethod * method,size_t pointer_size)264 inline void Class::SetEmbeddedImTableEntry(uint32_t i, ArtMethod* method, size_t pointer_size) {
265   DCHECK((ShouldHaveEmbeddedImtAndVTable<kVerifyFlags, kReadBarrierOption>()));
266   SetFieldPtrWithSize<false>(EmbeddedImTableEntryOffset(i, pointer_size), method, pointer_size);
267 }
268 
HasVTable()269 inline bool Class::HasVTable() {
270   return GetVTable() != nullptr || ShouldHaveEmbeddedImtAndVTable();
271 }
272 
GetVTableLength()273 inline int32_t Class::GetVTableLength() {
274   if (ShouldHaveEmbeddedImtAndVTable()) {
275     return GetEmbeddedVTableLength();
276   }
277   return GetVTable() != nullptr ? GetVTable()->GetLength() : 0;
278 }
279 
GetVTableEntry(uint32_t i,size_t pointer_size)280 inline ArtMethod* Class::GetVTableEntry(uint32_t i, size_t pointer_size) {
281   if (ShouldHaveEmbeddedImtAndVTable()) {
282     return GetEmbeddedVTableEntry(i, pointer_size);
283   }
284   auto* vtable = GetVTable();
285   DCHECK(vtable != nullptr);
286   return vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size);
287 }
288 
GetEmbeddedVTableLength()289 inline int32_t Class::GetEmbeddedVTableLength() {
290   return GetField32(MemberOffset(EmbeddedVTableLengthOffset()));
291 }
292 
SetEmbeddedVTableLength(int32_t len)293 inline void Class::SetEmbeddedVTableLength(int32_t len) {
294   SetField32<false>(MemberOffset(EmbeddedVTableLengthOffset()), len);
295 }
296 
EmbeddedVTableEntryOffset(uint32_t i,size_t pointer_size)297 inline MemberOffset Class::EmbeddedVTableEntryOffset(uint32_t i, size_t pointer_size) {
298   return MemberOffset(
299       EmbeddedVTableOffset(pointer_size).Uint32Value() + i * VTableEntrySize(pointer_size));
300 }
301 
GetEmbeddedVTableEntry(uint32_t i,size_t pointer_size)302 inline ArtMethod* Class::GetEmbeddedVTableEntry(uint32_t i, size_t pointer_size) {
303   return GetFieldPtrWithSize<ArtMethod*>(EmbeddedVTableEntryOffset(i, pointer_size), pointer_size);
304 }
305 
SetEmbeddedVTableEntryUnchecked(uint32_t i,ArtMethod * method,size_t pointer_size)306 inline void Class::SetEmbeddedVTableEntryUnchecked(
307     uint32_t i, ArtMethod* method, size_t pointer_size) {
308   SetFieldPtrWithSize<false>(EmbeddedVTableEntryOffset(i, pointer_size), method, pointer_size);
309 }
310 
SetEmbeddedVTableEntry(uint32_t i,ArtMethod * method,size_t pointer_size)311 inline void Class::SetEmbeddedVTableEntry(uint32_t i, ArtMethod* method, size_t pointer_size) {
312   auto* vtable = GetVTableDuringLinking();
313   CHECK_EQ(method, vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size));
314   SetEmbeddedVTableEntryUnchecked(i, method, pointer_size);
315 }
316 
Implements(Class * klass)317 inline bool Class::Implements(Class* klass) {
318   DCHECK(klass != nullptr);
319   DCHECK(klass->IsInterface()) << PrettyClass(this);
320   // All interfaces implemented directly and by our superclass, and
321   // recursively all super-interfaces of those interfaces, are listed
322   // in iftable_, so we can just do a linear scan through that.
323   int32_t iftable_count = GetIfTableCount();
324   IfTable* iftable = GetIfTable();
325   for (int32_t i = 0; i < iftable_count; i++) {
326     if (iftable->GetInterface(i) == klass) {
327       return true;
328     }
329   }
330   return false;
331 }
332 
333 // Determine whether "this" is assignable from "src", where both of these
334 // are array classes.
335 //
336 // Consider an array class, e.g. Y[][], where Y is a subclass of X.
337 //   Y[][]            = Y[][] --> true (identity)
338 //   X[][]            = Y[][] --> true (element superclass)
339 //   Y                = Y[][] --> false
340 //   Y[]              = Y[][] --> false
341 //   Object           = Y[][] --> true (everything is an object)
342 //   Object[]         = Y[][] --> true
343 //   Object[][]       = Y[][] --> true
344 //   Object[][][]     = Y[][] --> false (too many []s)
345 //   Serializable     = Y[][] --> true (all arrays are Serializable)
346 //   Serializable[]   = Y[][] --> true
347 //   Serializable[][] = Y[][] --> false (unless Y is Serializable)
348 //
349 // Don't forget about primitive types.
350 //   Object[]         = int[] --> false
351 //
IsArrayAssignableFromArray(Class * src)352 inline bool Class::IsArrayAssignableFromArray(Class* src) {
353   DCHECK(IsArrayClass())  << PrettyClass(this);
354   DCHECK(src->IsArrayClass()) << PrettyClass(src);
355   return GetComponentType()->IsAssignableFrom(src->GetComponentType());
356 }
357 
IsAssignableFromArray(Class * src)358 inline bool Class::IsAssignableFromArray(Class* src) {
359   DCHECK(!IsInterface()) << PrettyClass(this);  // handled first in IsAssignableFrom
360   DCHECK(src->IsArrayClass()) << PrettyClass(src);
361   if (!IsArrayClass()) {
362     // If "this" is not also an array, it must be Object.
363     // src's super should be java_lang_Object, since it is an array.
364     Class* java_lang_Object = src->GetSuperClass();
365     DCHECK(java_lang_Object != nullptr) << PrettyClass(src);
366     DCHECK(java_lang_Object->GetSuperClass() == nullptr) << PrettyClass(src);
367     return this == java_lang_Object;
368   }
369   return IsArrayAssignableFromArray(src);
370 }
371 
372 template <bool throw_on_failure, bool use_referrers_cache>
ResolvedFieldAccessTest(Class * access_to,ArtField * field,uint32_t field_idx,DexCache * dex_cache)373 inline bool Class::ResolvedFieldAccessTest(Class* access_to, ArtField* field,
374                                            uint32_t field_idx, DexCache* dex_cache) {
375   DCHECK_EQ(use_referrers_cache, dex_cache == nullptr);
376   if (UNLIKELY(!this->CanAccess(access_to))) {
377     // The referrer class can't access the field's declaring class but may still be able
378     // to access the field if the FieldId specifies an accessible subclass of the declaring
379     // class rather than the declaring class itself.
380     DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache;
381     uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetFieldId(field_idx).class_idx_;
382     // The referenced class has already been resolved with the field, get it from the dex cache.
383     Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx);
384     DCHECK(dex_access_to != nullptr);
385     if (UNLIKELY(!this->CanAccess(dex_access_to))) {
386       if (throw_on_failure) {
387         ThrowIllegalAccessErrorClass(this, dex_access_to);
388       }
389       return false;
390     }
391   }
392   if (LIKELY(this->CanAccessMember(access_to, field->GetAccessFlags()))) {
393     return true;
394   }
395   if (throw_on_failure) {
396     ThrowIllegalAccessErrorField(this, field);
397   }
398   return false;
399 }
400 
401 template <bool throw_on_failure, bool use_referrers_cache, InvokeType throw_invoke_type>
ResolvedMethodAccessTest(Class * access_to,ArtMethod * method,uint32_t method_idx,DexCache * dex_cache)402 inline bool Class::ResolvedMethodAccessTest(Class* access_to, ArtMethod* method,
403                                             uint32_t method_idx, DexCache* dex_cache) {
404   static_assert(throw_on_failure || throw_invoke_type == kStatic, "Non-default throw invoke type");
405   DCHECK_EQ(use_referrers_cache, dex_cache == nullptr);
406   if (UNLIKELY(!this->CanAccess(access_to))) {
407     // The referrer class can't access the method's declaring class but may still be able
408     // to access the method if the MethodId specifies an accessible subclass of the declaring
409     // class rather than the declaring class itself.
410     DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache;
411     uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
412     // The referenced class has already been resolved with the method, get it from the dex cache.
413     Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx);
414     DCHECK(dex_access_to != nullptr);
415     if (UNLIKELY(!this->CanAccess(dex_access_to))) {
416       if (throw_on_failure) {
417         ThrowIllegalAccessErrorClassForMethodDispatch(this, dex_access_to,
418                                                       method, throw_invoke_type);
419       }
420       return false;
421     }
422   }
423   if (LIKELY(this->CanAccessMember(access_to, method->GetAccessFlags()))) {
424     return true;
425   }
426   if (throw_on_failure) {
427     ThrowIllegalAccessErrorMethod(this, method);
428   }
429   return false;
430 }
431 
CanAccessResolvedField(Class * access_to,ArtField * field,DexCache * dex_cache,uint32_t field_idx)432 inline bool Class::CanAccessResolvedField(Class* access_to, ArtField* field,
433                                           DexCache* dex_cache, uint32_t field_idx) {
434   return ResolvedFieldAccessTest<false, false>(access_to, field, field_idx, dex_cache);
435 }
436 
CheckResolvedFieldAccess(Class * access_to,ArtField * field,uint32_t field_idx)437 inline bool Class::CheckResolvedFieldAccess(Class* access_to, ArtField* field,
438                                             uint32_t field_idx) {
439   return ResolvedFieldAccessTest<true, true>(access_to, field, field_idx, nullptr);
440 }
441 
CanAccessResolvedMethod(Class * access_to,ArtMethod * method,DexCache * dex_cache,uint32_t method_idx)442 inline bool Class::CanAccessResolvedMethod(Class* access_to, ArtMethod* method,
443                                            DexCache* dex_cache, uint32_t method_idx) {
444   return ResolvedMethodAccessTest<false, false, kStatic>(access_to, method, method_idx, dex_cache);
445 }
446 
447 template <InvokeType throw_invoke_type>
CheckResolvedMethodAccess(Class * access_to,ArtMethod * method,uint32_t method_idx)448 inline bool Class::CheckResolvedMethodAccess(Class* access_to, ArtMethod* method,
449                                              uint32_t method_idx) {
450   return ResolvedMethodAccessTest<true, true, throw_invoke_type>(access_to, method, method_idx,
451                                                                  nullptr);
452 }
453 
IsSubClass(Class * klass)454 inline bool Class::IsSubClass(Class* klass) {
455   DCHECK(!IsInterface()) << PrettyClass(this);
456   DCHECK(!IsArrayClass()) << PrettyClass(this);
457   Class* current = this;
458   do {
459     if (current == klass) {
460       return true;
461     }
462     current = current->GetSuperClass();
463   } while (current != nullptr);
464   return false;
465 }
466 
FindVirtualMethodForInterface(ArtMethod * method,size_t pointer_size)467 inline ArtMethod* Class::FindVirtualMethodForInterface(ArtMethod* method, size_t pointer_size) {
468   Class* declaring_class = method->GetDeclaringClass();
469   DCHECK(declaring_class != nullptr) << PrettyClass(this);
470   DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
471   // TODO cache to improve lookup speed
472   const int32_t iftable_count = GetIfTableCount();
473   IfTable* iftable = GetIfTable();
474   for (int32_t i = 0; i < iftable_count; i++) {
475     if (iftable->GetInterface(i) == declaring_class) {
476       return iftable->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>(
477           method->GetMethodIndex(), pointer_size);
478     }
479   }
480   return nullptr;
481 }
482 
FindVirtualMethodForVirtual(ArtMethod * method,size_t pointer_size)483 inline ArtMethod* Class::FindVirtualMethodForVirtual(ArtMethod* method, size_t pointer_size) {
484   // Only miranda or default methods may come from interfaces and be used as a virtual.
485   DCHECK(!method->GetDeclaringClass()->IsInterface() || method->IsDefault() || method->IsMiranda());
486   // The argument method may from a super class.
487   // Use the index to a potentially overridden one for this instance's class.
488   return GetVTableEntry(method->GetMethodIndex(), pointer_size);
489 }
490 
FindVirtualMethodForSuper(ArtMethod * method,size_t pointer_size)491 inline ArtMethod* Class::FindVirtualMethodForSuper(ArtMethod* method, size_t pointer_size) {
492   DCHECK(!method->GetDeclaringClass()->IsInterface());
493   return GetSuperClass()->GetVTableEntry(method->GetMethodIndex(), pointer_size);
494 }
495 
FindVirtualMethodForVirtualOrInterface(ArtMethod * method,size_t pointer_size)496 inline ArtMethod* Class::FindVirtualMethodForVirtualOrInterface(ArtMethod* method,
497                                                                 size_t pointer_size) {
498   if (method->IsDirect()) {
499     return method;
500   }
501   if (method->GetDeclaringClass()->IsInterface() && !method->IsCopied()) {
502     return FindVirtualMethodForInterface(method, pointer_size);
503   }
504   return FindVirtualMethodForVirtual(method, pointer_size);
505 }
506 
507 template<VerifyObjectFlags kVerifyFlags,
508          ReadBarrierOption kReadBarrierOption>
GetIfTable()509 inline IfTable* Class::GetIfTable() {
510   return GetFieldObject<IfTable, kVerifyFlags, kReadBarrierOption>(
511       OFFSET_OF_OBJECT_MEMBER(Class, iftable_));
512 }
513 
GetIfTableCount()514 inline int32_t Class::GetIfTableCount() {
515   IfTable* iftable = GetIfTable();
516   if (iftable == nullptr) {
517     return 0;
518   }
519   return iftable->Count();
520 }
521 
SetIfTable(IfTable * new_iftable)522 inline void Class::SetIfTable(IfTable* new_iftable) {
523   SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable);
524 }
525 
GetIFieldsPtr()526 inline LengthPrefixedArray<ArtField>* Class::GetIFieldsPtr() {
527   DCHECK(IsLoaded() || IsErroneous()) << GetStatus();
528   return GetFieldPtr<LengthPrefixedArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_));
529 }
530 
531 template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
GetFirstReferenceInstanceFieldOffset()532 inline MemberOffset Class::GetFirstReferenceInstanceFieldOffset() {
533   Class* super_class = GetSuperClass<kVerifyFlags, kReadBarrierOption>();
534   return (super_class != nullptr)
535       ? MemberOffset(RoundUp(super_class->GetObjectSize(),
536                              sizeof(mirror::HeapReference<mirror::Object>)))
537       : ClassOffset();
538 }
539 
540 template <VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
GetFirstReferenceStaticFieldOffset(size_t pointer_size)541 inline MemberOffset Class::GetFirstReferenceStaticFieldOffset(size_t pointer_size) {
542   DCHECK(IsResolved());
543   uint32_t base = sizeof(mirror::Class);  // Static fields come after the class.
544   if (ShouldHaveEmbeddedImtAndVTable<kVerifyFlags, kReadBarrierOption>()) {
545     // Static fields come after the embedded tables.
546     base = mirror::Class::ComputeClassSize(
547         true, GetEmbeddedVTableLength(), 0, 0, 0, 0, 0, pointer_size);
548   }
549   return MemberOffset(base);
550 }
551 
GetFirstReferenceStaticFieldOffsetDuringLinking(size_t pointer_size)552 inline MemberOffset Class::GetFirstReferenceStaticFieldOffsetDuringLinking(size_t pointer_size) {
553   DCHECK(IsLoaded());
554   uint32_t base = sizeof(mirror::Class);  // Static fields come after the class.
555   if (ShouldHaveEmbeddedImtAndVTable()) {
556     // Static fields come after the embedded tables.
557     base = mirror::Class::ComputeClassSize(true, GetVTableDuringLinking()->GetLength(),
558                                            0, 0, 0, 0, 0, pointer_size);
559   }
560   return MemberOffset(base);
561 }
562 
SetIFieldsPtr(LengthPrefixedArray<ArtField> * new_ifields)563 inline void Class::SetIFieldsPtr(LengthPrefixedArray<ArtField>* new_ifields) {
564   DCHECK(GetIFieldsPtrUnchecked() == nullptr);
565   return SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields);
566 }
567 
SetIFieldsPtrUnchecked(LengthPrefixedArray<ArtField> * new_ifields)568 inline void Class::SetIFieldsPtrUnchecked(LengthPrefixedArray<ArtField>* new_ifields) {
569   SetFieldPtr<false, true, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields);
570 }
571 
GetSFieldsPtrUnchecked()572 inline LengthPrefixedArray<ArtField>* Class::GetSFieldsPtrUnchecked() {
573   return GetFieldPtr<LengthPrefixedArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_));
574 }
575 
GetIFieldsPtrUnchecked()576 inline LengthPrefixedArray<ArtField>* Class::GetIFieldsPtrUnchecked() {
577   return GetFieldPtr<LengthPrefixedArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_));
578 }
579 
GetSFieldsPtr()580 inline LengthPrefixedArray<ArtField>* Class::GetSFieldsPtr() {
581   DCHECK(IsLoaded() || IsErroneous()) << GetStatus();
582   return GetSFieldsPtrUnchecked();
583 }
584 
SetSFieldsPtr(LengthPrefixedArray<ArtField> * new_sfields)585 inline void Class::SetSFieldsPtr(LengthPrefixedArray<ArtField>* new_sfields) {
586   DCHECK((IsRetired() && new_sfields == nullptr) ||
587          GetFieldPtr<ArtField*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_)) == nullptr);
588   SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields);
589 }
590 
SetSFieldsPtrUnchecked(LengthPrefixedArray<ArtField> * new_sfields)591 inline void Class::SetSFieldsPtrUnchecked(LengthPrefixedArray<ArtField>* new_sfields) {
592   SetFieldPtr<false, true, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields);
593 }
594 
GetStaticField(uint32_t i)595 inline ArtField* Class::GetStaticField(uint32_t i) {
596   return &GetSFieldsPtr()->At(i);
597 }
598 
GetInstanceField(uint32_t i)599 inline ArtField* Class::GetInstanceField(uint32_t i) {
600   return &GetIFieldsPtr()->At(i);
601 }
602 
603 template<VerifyObjectFlags kVerifyFlags>
GetReferenceInstanceOffsets()604 inline uint32_t Class::GetReferenceInstanceOffsets() {
605   DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>());
606   return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_));
607 }
608 
SetClinitThreadId(pid_t new_clinit_thread_id)609 inline void Class::SetClinitThreadId(pid_t new_clinit_thread_id) {
610   if (Runtime::Current()->IsActiveTransaction()) {
611     SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id);
612   } else {
613     SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id);
614   }
615 }
616 
617 template<VerifyObjectFlags kVerifyFlags>
GetAccessFlags()618 inline uint32_t Class::GetAccessFlags() {
619   // Check class is loaded/retired or this is java.lang.String that has a
620   // circularity issue during loading the names of its members
621   DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() ||
622          IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
623          this == String::GetJavaLangString())
624       << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>()
625       << " IsRetired=" << IsRetired<kVerifyFlags>()
626       << " IsErroneous=" <<
627           IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>()
628       << " IsString=" << (this == String::GetJavaLangString())
629       << " status= " << GetStatus<kVerifyFlags>()
630       << " descriptor=" << PrettyDescriptor(this);
631   return GetField32<kVerifyFlags>(AccessFlagsOffset());
632 }
633 
GetName()634 inline String* Class::GetName() {
635   return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Class, name_));
636 }
637 
SetName(String * name)638 inline void Class::SetName(String* name) {
639   if (Runtime::Current()->IsActiveTransaction()) {
640     SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name);
641   } else {
642     SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name);
643   }
644 }
645 
646 template<VerifyObjectFlags kVerifyFlags>
GetPrimitiveType()647 inline Primitive::Type Class::GetPrimitiveType() {
648   static_assert(sizeof(Primitive::Type) == sizeof(int32_t),
649                 "art::Primitive::Type and int32_t have different sizes.");
650   int32_t v32 = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_));
651   Primitive::Type type = static_cast<Primitive::Type>(v32 & 0xFFFF);
652   DCHECK_EQ(static_cast<size_t>(v32 >> 16), Primitive::ComponentSizeShift(type));
653   return type;
654 }
655 
656 template<VerifyObjectFlags kVerifyFlags>
GetPrimitiveTypeSizeShift()657 inline size_t Class::GetPrimitiveTypeSizeShift() {
658   static_assert(sizeof(Primitive::Type) == sizeof(int32_t),
659                 "art::Primitive::Type and int32_t have different sizes.");
660   int32_t v32 = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_));
661   size_t size_shift = static_cast<Primitive::Type>(v32 >> 16);
662   DCHECK_EQ(size_shift, Primitive::ComponentSizeShift(static_cast<Primitive::Type>(v32 & 0xFFFF)));
663   return size_shift;
664 }
665 
CheckObjectAlloc()666 inline void Class::CheckObjectAlloc() {
667   DCHECK(!IsArrayClass())
668       << PrettyClass(this)
669       << "A array shouldn't be allocated through this "
670       << "as it requires a pre-fence visitor that sets the class size.";
671   DCHECK(!IsClassClass())
672       << PrettyClass(this)
673       << "A class object shouldn't be allocated through this "
674       << "as it requires a pre-fence visitor that sets the class size.";
675   DCHECK(!IsStringClass())
676       << PrettyClass(this)
677       << "A string shouldn't be allocated through this "
678       << "as it requires a pre-fence visitor that sets the class size.";
679   DCHECK(IsInstantiable()) << PrettyClass(this);
680   // TODO: decide whether we want this check. It currently fails during bootstrap.
681   // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
682   DCHECK_GE(this->object_size_, sizeof(Object));
683 }
684 
685 template<bool kIsInstrumented, bool kCheckAddFinalizer>
Alloc(Thread * self,gc::AllocatorType allocator_type)686 inline Object* Class::Alloc(Thread* self, gc::AllocatorType allocator_type) {
687   CheckObjectAlloc();
688   gc::Heap* heap = Runtime::Current()->GetHeap();
689   const bool add_finalizer = kCheckAddFinalizer && IsFinalizable();
690   if (!kCheckAddFinalizer) {
691     DCHECK(!IsFinalizable());
692   }
693   mirror::Object* obj =
694       heap->AllocObjectWithAllocator<kIsInstrumented, false>(self, this, this->object_size_,
695                                                              allocator_type, VoidFunctor());
696   if (add_finalizer && LIKELY(obj != nullptr)) {
697     heap->AddFinalizerReference(self, &obj);
698     if (UNLIKELY(self->IsExceptionPending())) {
699       // Failed to allocate finalizer reference, it means that the whole allocation failed.
700       obj = nullptr;
701     }
702   }
703   return obj;
704 }
705 
AllocObject(Thread * self)706 inline Object* Class::AllocObject(Thread* self) {
707   return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
708 }
709 
AllocNonMovableObject(Thread * self)710 inline Object* Class::AllocNonMovableObject(Thread* self) {
711   return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentNonMovingAllocator());
712 }
713 
ComputeClassSize(bool has_embedded_tables,uint32_t num_vtable_entries,uint32_t num_8bit_static_fields,uint32_t num_16bit_static_fields,uint32_t num_32bit_static_fields,uint32_t num_64bit_static_fields,uint32_t num_ref_static_fields,size_t pointer_size)714 inline uint32_t Class::ComputeClassSize(bool has_embedded_tables,
715                                         uint32_t num_vtable_entries,
716                                         uint32_t num_8bit_static_fields,
717                                         uint32_t num_16bit_static_fields,
718                                         uint32_t num_32bit_static_fields,
719                                         uint32_t num_64bit_static_fields,
720                                         uint32_t num_ref_static_fields,
721                                         size_t pointer_size) {
722   // Space used by java.lang.Class and its instance fields.
723   uint32_t size = sizeof(Class);
724   // Space used by embedded tables.
725   if (has_embedded_tables) {
726     const uint32_t embedded_imt_size = kImtSize * ImTableEntrySize(pointer_size);
727     const uint32_t embedded_vtable_size = num_vtable_entries * VTableEntrySize(pointer_size);
728     size = RoundUp(size + sizeof(uint32_t) /* embedded vtable len */, pointer_size) +
729         embedded_imt_size + embedded_vtable_size;
730   }
731 
732   // Space used by reference statics.
733   size += num_ref_static_fields * sizeof(HeapReference<Object>);
734   if (!IsAligned<8>(size) && num_64bit_static_fields > 0) {
735     uint32_t gap = 8 - (size & 0x7);
736     size += gap;  // will be padded
737     // Shuffle 4-byte fields forward.
738     while (gap >= sizeof(uint32_t) && num_32bit_static_fields != 0) {
739       --num_32bit_static_fields;
740       gap -= sizeof(uint32_t);
741     }
742     // Shuffle 2-byte fields forward.
743     while (gap >= sizeof(uint16_t) && num_16bit_static_fields != 0) {
744       --num_16bit_static_fields;
745       gap -= sizeof(uint16_t);
746     }
747     // Shuffle byte fields forward.
748     while (gap >= sizeof(uint8_t) && num_8bit_static_fields != 0) {
749       --num_8bit_static_fields;
750       gap -= sizeof(uint8_t);
751     }
752   }
753   // Guaranteed to be at least 4 byte aligned. No need for further alignments.
754   // Space used for primitive static fields.
755   size += num_8bit_static_fields * sizeof(uint8_t) + num_16bit_static_fields * sizeof(uint16_t) +
756       num_32bit_static_fields * sizeof(uint32_t) + num_64bit_static_fields * sizeof(uint64_t);
757   return size;
758 }
759 
760 template <bool kVisitNativeRoots,
761           VerifyObjectFlags kVerifyFlags,
762           ReadBarrierOption kReadBarrierOption,
763           typename Visitor>
VisitReferences(mirror::Class * klass,const Visitor & visitor)764 inline void Class::VisitReferences(mirror::Class* klass, const Visitor& visitor) {
765   VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass, visitor);
766   // Right after a class is allocated, but not yet loaded
767   // (kStatusNotReady, see ClassLinker::LoadClass()), GC may find it
768   // and scan it. IsTemp() may call Class::GetAccessFlags() but may
769   // fail in the DCHECK in Class::GetAccessFlags() because the class
770   // status is kStatusNotReady. To avoid it, rely on IsResolved()
771   // only. This is fine because a temp class never goes into the
772   // kStatusResolved state.
773   if (IsResolved<kVerifyFlags>()) {
774     // Temp classes don't ever populate imt/vtable or static fields and they are not even
775     // allocated with the right size for those. Also, unresolved classes don't have fields
776     // linked yet.
777     VisitStaticFieldsReferences<kVerifyFlags, kReadBarrierOption>(this, visitor);
778   }
779   if (kVisitNativeRoots) {
780     // Since this class is reachable, we must also visit the associated roots when we scan it.
781     VisitNativeRoots(visitor, Runtime::Current()->GetClassLinker()->GetImagePointerSize());
782   }
783 }
784 
785 template<ReadBarrierOption kReadBarrierOption>
IsReferenceClass()786 inline bool Class::IsReferenceClass() const {
787   return this == Reference::GetJavaLangRefReference<kReadBarrierOption>();
788 }
789 
790 template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
IsClassClass()791 inline bool Class::IsClassClass() {
792   Class* java_lang_Class = GetClass<kVerifyFlags, kReadBarrierOption>()->
793       template GetClass<kVerifyFlags, kReadBarrierOption>();
794   return this == java_lang_Class;
795 }
796 
GetDexFile()797 inline const DexFile& Class::GetDexFile() {
798   return *GetDexCache()->GetDexFile();
799 }
800 
DescriptorEquals(const char * match)801 inline bool Class::DescriptorEquals(const char* match) {
802   if (IsArrayClass()) {
803     return match[0] == '[' && GetComponentType()->DescriptorEquals(match + 1);
804   } else if (IsPrimitive()) {
805     return strcmp(Primitive::Descriptor(GetPrimitiveType()), match) == 0;
806   } else if (IsProxyClass()) {
807     return ProxyDescriptorEquals(match);
808   } else {
809     const DexFile& dex_file = GetDexFile();
810     const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
811     return strcmp(dex_file.GetTypeDescriptor(type_id), match) == 0;
812   }
813 }
814 
AssertInitializedOrInitializingInThread(Thread * self)815 inline void Class::AssertInitializedOrInitializingInThread(Thread* self) {
816   if (kIsDebugBuild && !IsInitialized()) {
817     CHECK(IsInitializing()) << PrettyClass(this) << " is not initializing: " << GetStatus();
818     CHECK_EQ(GetClinitThreadId(), self->GetTid()) << PrettyClass(this)
819                                                   << " is initializing in a different thread";
820   }
821 }
822 
GetInterfaces()823 inline ObjectArray<Class>* Class::GetInterfaces() {
824   CHECK(IsProxyClass());
825   // First static field.
826   auto* field = GetStaticField(0);
827   DCHECK_STREQ(field->GetName(), "interfaces");
828   MemberOffset field_offset = field->GetOffset();
829   return GetFieldObject<ObjectArray<Class>>(field_offset);
830 }
831 
GetThrows()832 inline ObjectArray<ObjectArray<Class>>* Class::GetThrows() {
833   CHECK(IsProxyClass());
834   // Second static field.
835   auto* field = GetStaticField(1);
836   DCHECK_STREQ(field->GetName(), "throws");
837   MemberOffset field_offset = field->GetOffset();
838   return GetFieldObject<ObjectArray<ObjectArray<Class>>>(field_offset);
839 }
840 
GetDisableIntrinsicFlagOffset()841 inline MemberOffset Class::GetDisableIntrinsicFlagOffset() {
842   CHECK(IsReferenceClass());
843   // First static field
844   auto* field = GetStaticField(0);
845   DCHECK_STREQ(field->GetName(), "disableIntrinsic");
846   return field->GetOffset();
847 }
848 
GetSlowPathFlagOffset()849 inline MemberOffset Class::GetSlowPathFlagOffset() {
850   CHECK(IsReferenceClass());
851   // Second static field
852   auto* field = GetStaticField(1);
853   DCHECK_STREQ(field->GetName(), "slowPathEnabled");
854   return field->GetOffset();
855 }
856 
GetSlowPathEnabled()857 inline bool Class::GetSlowPathEnabled() {
858   return GetFieldBoolean(GetSlowPathFlagOffset());
859 }
860 
SetSlowPath(bool enabled)861 inline void Class::SetSlowPath(bool enabled) {
862   SetFieldBoolean<false, false>(GetSlowPathFlagOffset(), enabled);
863 }
864 
operator()865 inline void Class::InitializeClassVisitor::operator()(
866     mirror::Object* obj, size_t usable_size) const {
867   DCHECK_LE(class_size_, usable_size);
868   // Avoid AsClass as object is not yet in live bitmap or allocation stack.
869   mirror::Class* klass = down_cast<mirror::Class*>(obj);
870   // DCHECK(klass->IsClass());
871   klass->SetClassSize(class_size_);
872   klass->SetPrimitiveType(Primitive::kPrimNot);  // Default to not being primitive.
873   klass->SetDexClassDefIndex(DexFile::kDexNoIndex16);  // Default to no valid class def index.
874   klass->SetDexTypeIndex(DexFile::kDexNoIndex16);  // Default to no valid type index.
875 }
876 
SetAccessFlags(uint32_t new_access_flags)877 inline void Class::SetAccessFlags(uint32_t new_access_flags) {
878   // Called inside a transaction when setting pre-verified flag during boot image compilation.
879   if (Runtime::Current()->IsActiveTransaction()) {
880     SetField32<true>(AccessFlagsOffset(), new_access_flags);
881   } else {
882     SetField32<false>(AccessFlagsOffset(), new_access_flags);
883   }
884 }
885 
SetClassFlags(uint32_t new_flags)886 inline void Class::SetClassFlags(uint32_t new_flags) {
887   if (Runtime::Current()->IsActiveTransaction()) {
888     SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_flags_), new_flags);
889   } else {
890     SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_flags_), new_flags);
891   }
892 }
893 
NumDirectInterfaces()894 inline uint32_t Class::NumDirectInterfaces() {
895   if (IsPrimitive()) {
896     return 0;
897   } else if (IsArrayClass()) {
898     return 2;
899   } else if (IsProxyClass()) {
900     mirror::ObjectArray<mirror::Class>* interfaces = GetInterfaces();
901     return interfaces != nullptr ? interfaces->GetLength() : 0;
902   } else {
903     const DexFile::TypeList* interfaces = GetInterfaceTypeList();
904     if (interfaces == nullptr) {
905       return 0;
906     } else {
907       return interfaces->Size();
908     }
909   }
910 }
911 
SetDexCacheStrings(GcRoot<String> * new_dex_cache_strings)912 inline void Class::SetDexCacheStrings(GcRoot<String>* new_dex_cache_strings) {
913   SetFieldPtr<false>(DexCacheStringsOffset(), new_dex_cache_strings);
914 }
915 
GetDexCacheStrings()916 inline GcRoot<String>* Class::GetDexCacheStrings() {
917   return GetFieldPtr<GcRoot<String>*>(DexCacheStringsOffset());
918 }
919 
920 template<class Visitor>
VisitNativeRoots(Visitor & visitor,size_t pointer_size)921 void mirror::Class::VisitNativeRoots(Visitor& visitor, size_t pointer_size) {
922   for (ArtField& field : GetSFieldsUnchecked()) {
923     // Visit roots first in case the declaring class gets moved.
924     field.VisitRoots(visitor);
925     if (kIsDebugBuild && IsResolved()) {
926       CHECK_EQ(field.GetDeclaringClass(), this) << GetStatus();
927     }
928   }
929   for (ArtField& field : GetIFieldsUnchecked()) {
930     // Visit roots first in case the declaring class gets moved.
931     field.VisitRoots(visitor);
932     if (kIsDebugBuild && IsResolved()) {
933       CHECK_EQ(field.GetDeclaringClass(), this) << GetStatus();
934     }
935   }
936   for (ArtMethod& method : GetMethods(pointer_size)) {
937     method.VisitRoots(visitor, pointer_size);
938   }
939 }
940 
GetDirectMethods(size_t pointer_size)941 inline IterationRange<StrideIterator<ArtMethod>> Class::GetDirectMethods(size_t pointer_size) {
942   CheckPointerSize(pointer_size);
943   return GetDirectMethodsSliceUnchecked(pointer_size).AsRange();
944 }
945 
GetDeclaredMethods(size_t pointer_size)946 inline IterationRange<StrideIterator<ArtMethod>> Class::GetDeclaredMethods(
947       size_t pointer_size) {
948   CheckPointerSize(pointer_size);
949   return GetDeclaredMethodsSliceUnchecked(pointer_size).AsRange();
950 }
951 
GetDeclaredVirtualMethods(size_t pointer_size)952 inline IterationRange<StrideIterator<ArtMethod>> Class::GetDeclaredVirtualMethods(
953       size_t pointer_size) {
954   CheckPointerSize(pointer_size);
955   return GetDeclaredVirtualMethodsSliceUnchecked(pointer_size).AsRange();
956 }
957 
GetVirtualMethods(size_t pointer_size)958 inline IterationRange<StrideIterator<ArtMethod>> Class::GetVirtualMethods(size_t pointer_size) {
959   CheckPointerSize(pointer_size);
960   return GetVirtualMethodsSliceUnchecked(pointer_size).AsRange();
961 }
962 
GetCopiedMethods(size_t pointer_size)963 inline IterationRange<StrideIterator<ArtMethod>> Class::GetCopiedMethods(size_t pointer_size) {
964   CheckPointerSize(pointer_size);
965   return GetCopiedMethodsSliceUnchecked(pointer_size).AsRange();
966 }
967 
968 
GetMethods(size_t pointer_size)969 inline IterationRange<StrideIterator<ArtMethod>> Class::GetMethods(size_t pointer_size) {
970   CheckPointerSize(pointer_size);
971   return MakeIterationRangeFromLengthPrefixedArray(GetMethodsPtr(),
972                                                    ArtMethod::Size(pointer_size),
973                                                    ArtMethod::Alignment(pointer_size));
974 }
975 
GetIFields()976 inline IterationRange<StrideIterator<ArtField>> Class::GetIFields() {
977   return MakeIterationRangeFromLengthPrefixedArray(GetIFieldsPtr());
978 }
979 
GetSFields()980 inline IterationRange<StrideIterator<ArtField>> Class::GetSFields() {
981   return MakeIterationRangeFromLengthPrefixedArray(GetSFieldsPtr());
982 }
983 
GetIFieldsUnchecked()984 inline IterationRange<StrideIterator<ArtField>> Class::GetIFieldsUnchecked() {
985   return MakeIterationRangeFromLengthPrefixedArray(GetIFieldsPtrUnchecked());
986 }
987 
GetSFieldsUnchecked()988 inline IterationRange<StrideIterator<ArtField>> Class::GetSFieldsUnchecked() {
989   return MakeIterationRangeFromLengthPrefixedArray(GetSFieldsPtrUnchecked());
990 }
991 
EmbeddedImTableOffset(size_t pointer_size)992 inline MemberOffset Class::EmbeddedImTableOffset(size_t pointer_size) {
993   CheckPointerSize(pointer_size);
994   // Round up since we want the embedded imt and vtable to be pointer size aligned in case 64 bits.
995   // Add 32 bits for embedded vtable length.
996   return MemberOffset(
997       RoundUp(EmbeddedVTableLengthOffset().Uint32Value() + sizeof(uint32_t), pointer_size));
998 }
999 
EmbeddedVTableOffset(size_t pointer_size)1000 inline MemberOffset Class::EmbeddedVTableOffset(size_t pointer_size) {
1001   CheckPointerSize(pointer_size);
1002   return MemberOffset(EmbeddedImTableOffset(pointer_size).Uint32Value() +
1003                       kImtSize * ImTableEntrySize(pointer_size));
1004 }
1005 
CheckPointerSize(size_t pointer_size)1006 inline void Class::CheckPointerSize(size_t pointer_size) {
1007   DCHECK(ValidPointerSize(pointer_size)) << pointer_size;
1008   DCHECK_EQ(pointer_size, Runtime::Current()->GetClassLinker()->GetImagePointerSize());
1009 }
1010 
1011 template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
GetComponentType()1012 inline Class* Class::GetComponentType() {
1013   return GetFieldObject<Class, kVerifyFlags, kReadBarrierOption>(ComponentTypeOffset());
1014 }
1015 
1016 template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
IsArrayClass()1017 inline bool Class::IsArrayClass() {
1018   return GetComponentType<kVerifyFlags, kReadBarrierOption>() != nullptr;
1019 }
1020 
IsAssignableFrom(Class * src)1021 inline bool Class::IsAssignableFrom(Class* src) {
1022   DCHECK(src != nullptr);
1023   if (this == src) {
1024     // Can always assign to things of the same type.
1025     return true;
1026   } else if (IsObjectClass()) {
1027     // Can assign any reference to java.lang.Object.
1028     return !src->IsPrimitive();
1029   } else if (IsInterface()) {
1030     return src->Implements(this);
1031   } else if (src->IsArrayClass()) {
1032     return IsAssignableFromArray(src);
1033   } else {
1034     return !src->IsInterface() && src->IsSubClass(this);
1035   }
1036 }
1037 
NumDirectMethods()1038 inline uint32_t Class::NumDirectMethods() {
1039   return GetVirtualMethodsStartOffset();
1040 }
1041 
NumDeclaredVirtualMethods()1042 inline uint32_t Class::NumDeclaredVirtualMethods() {
1043   return GetCopiedMethodsStartOffset() - GetVirtualMethodsStartOffset();
1044 }
1045 
NumVirtualMethods()1046 inline uint32_t Class::NumVirtualMethods() {
1047   return NumMethods() - GetVirtualMethodsStartOffset();
1048 }
1049 
NumInstanceFields()1050 inline uint32_t Class::NumInstanceFields() {
1051   LengthPrefixedArray<ArtField>* arr = GetIFieldsPtrUnchecked();
1052   return arr != nullptr ? arr->size() : 0u;
1053 }
1054 
NumStaticFields()1055 inline uint32_t Class::NumStaticFields() {
1056   LengthPrefixedArray<ArtField>* arr = GetSFieldsPtrUnchecked();
1057   return arr != nullptr ? arr->size() : 0u;
1058 }
1059 
1060 template <VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption, typename Visitor>
FixupNativePointers(mirror::Class * dest,size_t pointer_size,const Visitor & visitor)1061 inline void Class::FixupNativePointers(mirror::Class* dest,
1062                                        size_t pointer_size,
1063                                        const Visitor& visitor) {
1064   // Update the field arrays.
1065   LengthPrefixedArray<ArtField>* const sfields = GetSFieldsPtr();
1066   LengthPrefixedArray<ArtField>* const new_sfields = visitor(sfields);
1067   if (sfields != new_sfields) {
1068     dest->SetSFieldsPtrUnchecked(new_sfields);
1069   }
1070   LengthPrefixedArray<ArtField>* const ifields = GetIFieldsPtr();
1071   LengthPrefixedArray<ArtField>* const new_ifields = visitor(ifields);
1072   if (ifields != new_ifields) {
1073     dest->SetIFieldsPtrUnchecked(new_ifields);
1074   }
1075   // Update method array.
1076   LengthPrefixedArray<ArtMethod>* methods = GetMethodsPtr();
1077   LengthPrefixedArray<ArtMethod>* new_methods = visitor(methods);
1078   if (methods != new_methods) {
1079     dest->SetMethodsPtrInternal(new_methods);
1080   }
1081   // Update dex cache strings.
1082   GcRoot<mirror::String>* strings = GetDexCacheStrings();
1083   GcRoot<mirror::String>* new_strings = visitor(strings);
1084   if (strings != new_strings) {
1085     dest->SetDexCacheStrings(new_strings);
1086   }
1087   // Fix up embedded tables.
1088   if (!IsTemp() && ShouldHaveEmbeddedImtAndVTable<kVerifyNone, kReadBarrierOption>()) {
1089     for (int32_t i = 0, count = GetEmbeddedVTableLength(); i < count; ++i) {
1090       ArtMethod* method = GetEmbeddedVTableEntry(i, pointer_size);
1091       ArtMethod* new_method = visitor(method);
1092       if (method != new_method) {
1093         dest->SetEmbeddedVTableEntryUnchecked(i, new_method, pointer_size);
1094       }
1095     }
1096     for (size_t i = 0; i < mirror::Class::kImtSize; ++i) {
1097       ArtMethod* method = GetEmbeddedImTableEntry<kVerifyFlags, kReadBarrierOption>(i,
1098                                                                                     pointer_size);
1099       ArtMethod* new_method = visitor(method);
1100       if (method != new_method) {
1101         dest->SetEmbeddedImTableEntry<kVerifyFlags, kReadBarrierOption>(i,
1102                                                                         new_method,
1103                                                                         pointer_size);
1104       }
1105     }
1106   }
1107 }
1108 
1109 }  // namespace mirror
1110 }  // namespace art
1111 
1112 #endif  // ART_RUNTIME_MIRROR_CLASS_INL_H_
1113