• 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_OBJECT_INL_H_
18 #define ART_RUNTIME_MIRROR_OBJECT_INL_H_
19 
20 #include "object.h"
21 
22 #include "array-inl.h"
23 #include "art_field.h"
24 #include "art_method.h"
25 #include "base/atomic.h"
26 #include "class-inl.h"
27 #include "class_flags.h"
28 #include "class_linker.h"
29 #include "dex_cache.h"
30 #include "heap_poisoning.h"
31 #include "lock_word-inl.h"
32 #include "monitor.h"
33 #include "obj_ptr-inl.h"
34 #include "object-readbarrier-inl.h"
35 #include "object_array-inl.h"
36 #include "object_reference-inl.h"
37 #include "read_barrier-inl.h"
38 #include "reference.h"
39 #include "runtime.h"
40 #include "string.h"
41 #include "throwable.h"
42 #include "write_barrier-inl.h"
43 
44 namespace art HIDDEN {
45 namespace mirror {
46 
ClassSize(PointerSize pointer_size)47 inline uint32_t Object::ClassSize(PointerSize pointer_size) {
48   uint32_t vtable_entries = kVTableLength;
49   return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, pointer_size);
50 }
51 
52 template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
GetClass()53 inline Class* Object::GetClass() {
54   return GetFieldObject<Class, kVerifyFlags, kReadBarrierOption>(ClassOffset());
55 }
56 
57 template<VerifyObjectFlags kVerifyFlags>
SetClass(ObjPtr<Class> new_klass)58 inline void Object::SetClass(ObjPtr<Class> new_klass) {
59   // new_klass may be null prior to class linker initialization.
60   // We don't mark the card as this occurs as part of object allocation. Not all objects have
61   // backing cards, such as large objects.
62   // We use non transactional version since we can't undo this write. We also disable checking as
63   // we may run in transaction mode here.
64   SetFieldObjectWithoutWriteBarrier<false, false, RemoveThisFlags(kVerifyFlags)>(ClassOffset(),
65                                                                                  new_klass);
66 }
67 
68 template<VerifyObjectFlags kVerifyFlags>
SetLockWord(LockWord new_val,bool as_volatile)69 inline void Object::SetLockWord(LockWord new_val, bool as_volatile) {
70   // Force use of non-transactional mode and do not check.
71   if (as_volatile) {
72     SetField32Volatile<false, false, kVerifyFlags>(MonitorOffset(), new_val.GetValue());
73   } else {
74     SetField32<false, false, kVerifyFlags>(MonitorOffset(), new_val.GetValue());
75   }
76 }
77 
GetLockOwnerThreadId()78 inline uint32_t Object::GetLockOwnerThreadId() {
79   return Monitor::GetLockOwnerThreadId(this);
80 }
81 
MonitorEnter(Thread * self)82 inline ObjPtr<mirror::Object> Object::MonitorEnter(Thread* self) {
83   return Monitor::MonitorEnter(self, this, /*trylock=*/false);
84 }
85 
MonitorTryEnter(Thread * self)86 inline ObjPtr<mirror::Object> Object::MonitorTryEnter(Thread* self) {
87   return Monitor::MonitorEnter(self, this, /*trylock=*/true);
88 }
89 
MonitorExit(Thread * self)90 inline bool Object::MonitorExit(Thread* self) {
91   return Monitor::MonitorExit(self, this);
92 }
93 
Notify(Thread * self)94 inline void Object::Notify(Thread* self) {
95   Monitor::Notify(self, this);
96 }
97 
NotifyAll(Thread * self)98 inline void Object::NotifyAll(Thread* self) {
99   Monitor::NotifyAll(self, this);
100 }
101 
Wait(Thread * self,int64_t ms,int32_t ns)102 inline void Object::Wait(Thread* self, int64_t ms, int32_t ns) {
103   Monitor::Wait(self, this, ms, ns, true, ThreadState::kTimedWaiting);
104 }
105 
GetMarkBit()106 inline uint32_t Object::GetMarkBit() {
107   CHECK(gUseReadBarrier);
108   return GetLockWord(false).MarkBitState();
109 }
110 
SetReadBarrierState(uint32_t rb_state)111 inline void Object::SetReadBarrierState(uint32_t rb_state) {
112   CHECK(kUseBakerReadBarrier);
113   DCHECK(ReadBarrier::IsValidReadBarrierState(rb_state)) << rb_state;
114   LockWord lw = GetLockWord(false);
115   lw.SetReadBarrierState(rb_state);
116   SetLockWord(lw, false);
117 }
118 
AssertReadBarrierState()119 inline void Object::AssertReadBarrierState() const {
120   CHECK(kUseBakerReadBarrier);
121   Object* obj = const_cast<Object*>(this);
122   DCHECK_EQ(obj->GetReadBarrierState(), ReadBarrier::NonGrayState())
123       << "Bad Baker pointer: obj=" << obj << " rb_state" << obj->GetReadBarrierState();
124 }
125 
126 template<VerifyObjectFlags kVerifyFlags>
VerifierInstanceOf(ObjPtr<Class> klass)127 inline bool Object::VerifierInstanceOf(ObjPtr<Class> klass) {
128   DCHECK(klass != nullptr);
129   DCHECK(GetClass<kVerifyFlags>() != nullptr);
130   return klass->IsInterface() || InstanceOf(klass);
131 }
132 
133 template<VerifyObjectFlags kVerifyFlags>
InstanceOf(ObjPtr<Class> klass)134 inline bool Object::InstanceOf(ObjPtr<Class> klass) {
135   DCHECK(klass != nullptr);
136   DCHECK(GetClass<kVerifyNone>() != nullptr) << "this=" << this;
137   return klass->IsAssignableFrom(GetClass<kVerifyFlags>());
138 }
139 
140 template<VerifyObjectFlags kVerifyFlags>
IsClass()141 inline bool Object::IsClass() {
142   // OK to look at from-space copies since java.lang.Class.class is non-moveable
143   // (even when running without boot image, see ClassLinker::InitWithoutImage())
144   // and we're reading constant references for comparison only. See ReadBarrierOption.
145   ObjPtr<Class> klass = GetClass<kVerifyFlags, kWithoutReadBarrier>();
146   ObjPtr<Class> java_lang_Class = klass->GetClass<kVerifyFlags, kWithoutReadBarrier>();
147   return klass == java_lang_Class;
148 }
149 
150 template<VerifyObjectFlags kVerifyFlags>
AsClass()151 inline ObjPtr<Class> Object::AsClass() {
152   DCHECK((IsClass<kVerifyFlags>()));
153   return ObjPtr<Class>::DownCast(this);
154 }
155 
156 template<VerifyObjectFlags kVerifyFlags>
IsObjectArray()157 inline bool Object::IsObjectArray() {
158   // We do not need a read barrier here as the primitive type is constant,
159   // both from-space and to-space component type classes shall yield the same result.
160   constexpr VerifyObjectFlags kNewFlags = RemoveThisFlags(kVerifyFlags);
161   return IsArrayInstance<kVerifyFlags>() &&
162       !GetClass<kNewFlags, kWithoutReadBarrier>()->
163           template GetComponentType<kNewFlags, kWithoutReadBarrier>()->IsPrimitive();
164 }
165 
166 template<class T, VerifyObjectFlags kVerifyFlags>
AsObjectArray()167 inline ObjPtr<ObjectArray<T>> Object::AsObjectArray() {
168   DCHECK((IsObjectArray<kVerifyFlags>()));
169   return ObjPtr<ObjectArray<T>>::DownCast(this);
170 }
171 
172 template<VerifyObjectFlags kVerifyFlags>
IsArrayInstance()173 inline bool Object::IsArrayInstance() {
174   // We do not need a read barrier here, both from-space and to-space version of the class
175   // shall return the same result from IsArrayClass().
176   return GetClass<kVerifyFlags, kWithoutReadBarrier>()->template IsArrayClass<kVerifyFlags>();
177 }
178 
179 template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
IsReferenceInstance()180 inline bool Object::IsReferenceInstance() {
181   return GetClass<kVerifyFlags, kReadBarrierOption>()->IsTypeOfReferenceClass();
182 }
183 
184 template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
AsReference()185 inline ObjPtr<Reference> Object::AsReference() {
186   DCHECK((IsReferenceInstance<kVerifyFlags, kReadBarrierOption>()));
187   return ObjPtr<Reference>::DownCast(this);
188 }
189 
190 template<VerifyObjectFlags kVerifyFlags>
AsArray()191 inline ObjPtr<Array> Object::AsArray() {
192   DCHECK((IsArrayInstance<kVerifyFlags>()));
193   return ObjPtr<Array>::DownCast(this);
194 }
195 
196 template<VerifyObjectFlags kVerifyFlags, Primitive::Type kType>
IsSpecificPrimitiveArray()197 ALWAYS_INLINE bool Object::IsSpecificPrimitiveArray() {
198   // We do not need a read barrier here as the primitive type is constant, both from-space
199   // and to-space component type classes shall yield the same result. See ReadBarrierOption.
200   const ObjPtr<Class> klass = GetClass<kVerifyFlags, kWithoutReadBarrier>();
201   constexpr VerifyObjectFlags kNewFlags = RemoveThisFlags(kVerifyFlags);
202   const ObjPtr<Class> component_type = klass->GetComponentType<kNewFlags, kWithoutReadBarrier>();
203   return component_type != nullptr &&
204          component_type->GetPrimitiveType<kNewFlags>() == kType;
205 }
206 
207 template<VerifyObjectFlags kVerifyFlags>
IsBooleanArray()208 inline bool Object::IsBooleanArray() {
209   return IsSpecificPrimitiveArray<kVerifyFlags, Primitive::kPrimBoolean>();
210 }
211 
212 template<VerifyObjectFlags kVerifyFlags>
AsBooleanArray()213 inline ObjPtr<BooleanArray> Object::AsBooleanArray() {
214   DCHECK(IsBooleanArray<kVerifyFlags>());
215   return ObjPtr<BooleanArray>::DownCast(this);
216 }
217 
218 template<VerifyObjectFlags kVerifyFlags>
IsByteArray()219 inline bool Object::IsByteArray() {
220   return IsSpecificPrimitiveArray<kVerifyFlags, Primitive::kPrimByte>();
221 }
222 
223 template<VerifyObjectFlags kVerifyFlags>
AsByteArray()224 inline ObjPtr<ByteArray> Object::AsByteArray() {
225   DCHECK(IsByteArray<kVerifyFlags>());
226   return ObjPtr<ByteArray>::DownCast(this);
227 }
228 
229 template<VerifyObjectFlags kVerifyFlags>
IsCharArray()230 inline bool Object::IsCharArray() {
231   return IsSpecificPrimitiveArray<kVerifyFlags, Primitive::kPrimChar>();
232 }
233 
234 template<VerifyObjectFlags kVerifyFlags>
AsCharArray()235 inline ObjPtr<CharArray> Object::AsCharArray() {
236   DCHECK(IsCharArray<kVerifyFlags>());
237   return ObjPtr<CharArray>::DownCast(this);
238 }
239 
240 template<VerifyObjectFlags kVerifyFlags>
IsShortArray()241 inline bool Object::IsShortArray() {
242   return IsSpecificPrimitiveArray<kVerifyFlags, Primitive::kPrimShort>();
243 }
244 
245 template<VerifyObjectFlags kVerifyFlags>
AsShortArray()246 inline ObjPtr<ShortArray> Object::AsShortArray() {
247   DCHECK(IsShortArray<kVerifyFlags>());
248   return ObjPtr<ShortArray>::DownCast(this);
249 }
250 
251 template<VerifyObjectFlags kVerifyFlags>
IsIntArray()252 inline bool Object::IsIntArray() {
253   return IsSpecificPrimitiveArray<kVerifyFlags, Primitive::kPrimInt>();
254 }
255 
256 template<VerifyObjectFlags kVerifyFlags>
AsIntArrayUnchecked()257 inline ObjPtr<IntArray> Object::AsIntArrayUnchecked() {
258   return ObjPtr<IntArray>::DownCast(this);
259 }
260 template<VerifyObjectFlags kVerifyFlags>
AsIntArray()261 inline ObjPtr<IntArray> Object::AsIntArray() {
262   DCHECK((IsIntArray<kVerifyFlags>()));
263   return AsIntArrayUnchecked<kVerifyFlags>();
264 }
265 
266 template<VerifyObjectFlags kVerifyFlags>
IsLongArray()267 inline bool Object::IsLongArray() {
268   return IsSpecificPrimitiveArray<kVerifyFlags, Primitive::kPrimLong>();
269 }
270 
271 template<VerifyObjectFlags kVerifyFlags>
AsLongArrayUnchecked()272 inline ObjPtr<LongArray> Object::AsLongArrayUnchecked() {
273   return ObjPtr<LongArray>::DownCast(this);
274 }
275 template<VerifyObjectFlags kVerifyFlags>
AsLongArray()276 inline ObjPtr<LongArray> Object::AsLongArray() {
277   DCHECK((IsLongArray<kVerifyFlags>()));
278   return AsLongArrayUnchecked<kVerifyFlags>();
279 }
280 
281 template<VerifyObjectFlags kVerifyFlags>
IsFloatArray()282 inline bool Object::IsFloatArray() {
283   return IsSpecificPrimitiveArray<kVerifyFlags, Primitive::kPrimFloat>();
284 }
285 
286 template<VerifyObjectFlags kVerifyFlags>
AsFloatArray()287 inline ObjPtr<FloatArray> Object::AsFloatArray() {
288   DCHECK(IsFloatArray<kVerifyFlags>());
289   return ObjPtr<FloatArray>::DownCast(this);
290 }
291 
292 template<VerifyObjectFlags kVerifyFlags>
IsDoubleArray()293 inline bool Object::IsDoubleArray() {
294   return IsSpecificPrimitiveArray<kVerifyFlags, Primitive::kPrimDouble>();
295 }
296 
297 template<VerifyObjectFlags kVerifyFlags>
AsDoubleArray()298 inline ObjPtr<DoubleArray> Object::AsDoubleArray() {
299   DCHECK(IsDoubleArray<kVerifyFlags>());
300   return ObjPtr<DoubleArray>::DownCast(this);
301 }
302 
303 template<VerifyObjectFlags kVerifyFlags>
IsString()304 inline bool Object::IsString() {
305   // No read barrier is needed for reading a constant primitive field through
306   // constant reference field. See ReadBarrierOption.
307   return GetClass<kVerifyFlags, kWithoutReadBarrier>()->IsStringClass();
308 }
309 
310 template<VerifyObjectFlags kVerifyFlags>
AsString()311 inline ObjPtr<String> Object::AsString() {
312   DCHECK((IsString<kVerifyFlags>()));
313   return ObjPtr<String>::DownCast(this);
314 }
315 
316 template<VerifyObjectFlags kVerifyFlags>
AsThrowable()317 inline ObjPtr<Throwable> Object::AsThrowable() {
318   DCHECK(GetClass<kVerifyFlags>()->IsThrowableClass());
319   return ObjPtr<Throwable>::DownCast(this);
320 }
321 
322 template<VerifyObjectFlags kVerifyFlags>
IsWeakReferenceInstance()323 inline bool Object::IsWeakReferenceInstance() {
324   return GetClass<kVerifyFlags>()->IsWeakReferenceClass();
325 }
326 
327 template<VerifyObjectFlags kVerifyFlags>
IsSoftReferenceInstance()328 inline bool Object::IsSoftReferenceInstance() {
329   return GetClass<kVerifyFlags>()->IsSoftReferenceClass();
330 }
331 
332 template<VerifyObjectFlags kVerifyFlags>
IsFinalizerReferenceInstance()333 inline bool Object::IsFinalizerReferenceInstance() {
334   return GetClass<kVerifyFlags>()->IsFinalizerReferenceClass();
335 }
336 
337 template<VerifyObjectFlags kVerifyFlags>
AsFinalizerReference()338 inline ObjPtr<FinalizerReference> Object::AsFinalizerReference() {
339   DCHECK(IsFinalizerReferenceInstance<kVerifyFlags>());
340   return ObjPtr<FinalizerReference>::DownCast(this);
341 }
342 
343 template<VerifyObjectFlags kVerifyFlags>
IsPhantomReferenceInstance()344 inline bool Object::IsPhantomReferenceInstance() {
345   return GetClass<kVerifyFlags>()->IsPhantomReferenceClass();
346 }
347 
348 template<VerifyObjectFlags kVerifyFlags>
SizeOf()349 inline size_t Object::SizeOf() {
350   // Read barrier is never required for SizeOf since objects sizes are constant. Reading from-space
351   // values is OK because of that.
352   size_t result;
353   constexpr VerifyObjectFlags kNewFlags = RemoveThisFlags(kVerifyFlags);
354   if (IsArrayInstance<kVerifyFlags>()) {
355     result = AsArray<kNewFlags>()->template SizeOf<kNewFlags>();
356   } else if (IsClass<kNewFlags>()) {
357     result = AsClass<kNewFlags>()->template SizeOf<kNewFlags>();
358   } else if (IsString<kNewFlags>()) {
359     result = AsString<kNewFlags>()->template SizeOf<kNewFlags>();
360   } else {
361     result = GetClass<kNewFlags, kWithoutReadBarrier>()->template GetObjectSize<kNewFlags>();
362   }
363   DCHECK_GE(result, sizeof(Object)) << " class="
364       // Note: Class::PrettyClass() is reading constant reference fields to get to constant
365       // primitive fields and safely avoids read barriers, so it is safe to call on a Class
366       // reference read without read barrier from a constant reference field.
367       // See ReadBarrierOption. And, for correctness, we actually have to avoid the read
368       // barrier here if Object::SizeOf() is called on a from-space reference.
369       << GetClass<kNewFlags, kWithoutReadBarrier>()->PrettyClass();
370   return result;
371 }
372 
373 template<VerifyObjectFlags kVerifyFlags, bool kIsVolatile>
GetFieldByte(MemberOffset field_offset)374 inline int8_t Object::GetFieldByte(MemberOffset field_offset) {
375   Verify<kVerifyFlags>();
376   return GetFieldPrimitive<int8_t, kIsVolatile>(field_offset);
377 }
378 
379 template<VerifyObjectFlags kVerifyFlags>
GetFieldBooleanVolatile(MemberOffset field_offset)380 inline uint8_t Object::GetFieldBooleanVolatile(MemberOffset field_offset) {
381   return GetFieldBoolean<kVerifyFlags, true>(field_offset);
382 }
383 
384 template<VerifyObjectFlags kVerifyFlags>
GetFieldByteVolatile(MemberOffset field_offset)385 inline int8_t Object::GetFieldByteVolatile(MemberOffset field_offset) {
386   return GetFieldByte<kVerifyFlags, true>(field_offset);
387 }
388 
389 template<bool kTransactionActive,
390          bool kCheckTransaction,
391          VerifyObjectFlags kVerifyFlags,
392          bool kIsVolatile>
SetFieldBoolean(MemberOffset field_offset,uint8_t new_value)393 inline void Object::SetFieldBoolean(MemberOffset field_offset, uint8_t new_value) {
394   VerifyTransaction<kTransactionActive, kCheckTransaction>();
395   if (kTransactionActive) {
396     Runtime::Current()->GetClassLinker()->RecordWriteFieldBoolean(
397         this, field_offset, GetFieldBoolean<kVerifyFlags, kIsVolatile>(field_offset), kIsVolatile);
398   }
399   Verify<kVerifyFlags>();
400   SetFieldPrimitive<uint8_t, kIsVolatile>(field_offset, new_value);
401 }
402 
403 template<bool kTransactionActive,
404          bool kCheckTransaction,
405          VerifyObjectFlags kVerifyFlags,
406          bool kIsVolatile>
SetFieldByte(MemberOffset field_offset,int8_t new_value)407 inline void Object::SetFieldByte(MemberOffset field_offset, int8_t new_value) {
408   VerifyTransaction<kTransactionActive, kCheckTransaction>();
409   if (kTransactionActive) {
410     Runtime::Current()->GetClassLinker()->RecordWriteFieldByte(
411         this, field_offset, GetFieldByte<kVerifyFlags, kIsVolatile>(field_offset), kIsVolatile);
412   }
413   Verify<kVerifyFlags>();
414   SetFieldPrimitive<int8_t, kIsVolatile>(field_offset, new_value);
415 }
416 
417 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
SetFieldBooleanVolatile(MemberOffset field_offset,uint8_t new_value)418 inline void Object::SetFieldBooleanVolatile(MemberOffset field_offset, uint8_t new_value) {
419   return SetFieldBoolean<kTransactionActive, kCheckTransaction, kVerifyFlags, true>(
420       field_offset, new_value);
421 }
422 
423 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
SetFieldByteVolatile(MemberOffset field_offset,int8_t new_value)424 inline void Object::SetFieldByteVolatile(MemberOffset field_offset, int8_t new_value) {
425   return SetFieldByte<kTransactionActive, kCheckTransaction, kVerifyFlags, true>(
426       field_offset, new_value);
427 }
428 
429 template<VerifyObjectFlags kVerifyFlags, bool kIsVolatile>
GetFieldChar(MemberOffset field_offset)430 inline uint16_t Object::GetFieldChar(MemberOffset field_offset) {
431   Verify<kVerifyFlags>();
432   return GetFieldPrimitive<uint16_t, kIsVolatile>(field_offset);
433 }
434 
435 template<VerifyObjectFlags kVerifyFlags, bool kIsVolatile>
GetFieldShort(MemberOffset field_offset)436 inline int16_t Object::GetFieldShort(MemberOffset field_offset) {
437   Verify<kVerifyFlags>();
438   return GetFieldPrimitive<int16_t, kIsVolatile>(field_offset);
439 }
440 
441 template<VerifyObjectFlags kVerifyFlags>
GetFieldCharVolatile(MemberOffset field_offset)442 inline uint16_t Object::GetFieldCharVolatile(MemberOffset field_offset) {
443   return GetFieldChar<kVerifyFlags, true>(field_offset);
444 }
445 
446 template<VerifyObjectFlags kVerifyFlags>
GetFieldShortVolatile(MemberOffset field_offset)447 inline int16_t Object::GetFieldShortVolatile(MemberOffset field_offset) {
448   return GetFieldShort<kVerifyFlags, true>(field_offset);
449 }
450 
451 template<bool kTransactionActive,
452          bool kCheckTransaction,
453          VerifyObjectFlags kVerifyFlags,
454          bool kIsVolatile>
SetFieldChar(MemberOffset field_offset,uint16_t new_value)455 inline void Object::SetFieldChar(MemberOffset field_offset, uint16_t new_value) {
456   VerifyTransaction<kTransactionActive, kCheckTransaction>();
457   if (kTransactionActive) {
458     Runtime::Current()->GetClassLinker()->RecordWriteFieldChar(
459         this, field_offset, GetFieldChar<kVerifyFlags, kIsVolatile>(field_offset), kIsVolatile);
460   }
461   Verify<kVerifyFlags>();
462   SetFieldPrimitive<uint16_t, kIsVolatile>(field_offset, new_value);
463 }
464 
465 template<bool kTransactionActive,
466          bool kCheckTransaction,
467          VerifyObjectFlags kVerifyFlags,
468          bool kIsVolatile>
SetFieldShort(MemberOffset field_offset,int16_t new_value)469 inline void Object::SetFieldShort(MemberOffset field_offset, int16_t new_value) {
470   VerifyTransaction<kTransactionActive, kCheckTransaction>();
471   if (kTransactionActive) {
472     Runtime::Current()->GetClassLinker()->RecordWriteFieldChar(
473         this, field_offset, GetFieldShort<kVerifyFlags, kIsVolatile>(field_offset), kIsVolatile);
474   }
475   Verify<kVerifyFlags>();
476   SetFieldPrimitive<int16_t, kIsVolatile>(field_offset, new_value);
477 }
478 
479 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
SetFieldCharVolatile(MemberOffset field_offset,uint16_t new_value)480 inline void Object::SetFieldCharVolatile(MemberOffset field_offset, uint16_t new_value) {
481   return SetFieldChar<kTransactionActive, kCheckTransaction, kVerifyFlags, true>(
482       field_offset, new_value);
483 }
484 
485 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
SetFieldShortVolatile(MemberOffset field_offset,int16_t new_value)486 inline void Object::SetFieldShortVolatile(MemberOffset field_offset, int16_t new_value) {
487   return SetFieldShort<kTransactionActive, kCheckTransaction, kVerifyFlags, true>(
488       field_offset, new_value);
489 }
490 
491 template<bool kTransactionActive,
492          bool kCheckTransaction,
493          VerifyObjectFlags kVerifyFlags,
494          bool kIsVolatile>
SetField32(MemberOffset field_offset,int32_t new_value)495 inline void Object::SetField32(MemberOffset field_offset, int32_t new_value) {
496   VerifyTransaction<kTransactionActive, kCheckTransaction>();
497   if (kTransactionActive) {
498     Runtime::Current()->GetClassLinker()->RecordWriteField32(
499         this, field_offset, GetField32<kVerifyFlags, kIsVolatile>(field_offset), kIsVolatile);
500   }
501   Verify<kVerifyFlags>();
502   SetFieldPrimitive<int32_t, kIsVolatile>(field_offset, new_value);
503 }
504 
505 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
SetField32Volatile(MemberOffset field_offset,int32_t new_value)506 inline void Object::SetField32Volatile(MemberOffset field_offset, int32_t new_value) {
507   SetField32<kTransactionActive, kCheckTransaction, kVerifyFlags, true>(field_offset, new_value);
508 }
509 
510 template<bool kCheckTransaction, VerifyObjectFlags kVerifyFlags, bool kIsVolatile>
SetField32Transaction(MemberOffset field_offset,int32_t new_value)511 inline void Object::SetField32Transaction(MemberOffset field_offset, int32_t new_value) {
512   if (Runtime::Current()->IsActiveTransaction()) {
513     SetField32<true, kCheckTransaction, kVerifyFlags, kIsVolatile>(field_offset, new_value);
514   } else {
515     SetField32<false, kCheckTransaction, kVerifyFlags, kIsVolatile>(field_offset, new_value);
516   }
517 }
518 
519 template<bool kTransactionActive,
520          bool kCheckTransaction,
521          VerifyObjectFlags kVerifyFlags,
522          bool kIsVolatile>
SetField64(MemberOffset field_offset,int64_t new_value)523 inline void Object::SetField64(MemberOffset field_offset, int64_t new_value) {
524   VerifyTransaction<kTransactionActive, kCheckTransaction>();
525   if (kTransactionActive) {
526     Runtime::Current()->GetClassLinker()->RecordWriteField64(
527         this, field_offset, GetField64<kVerifyFlags, kIsVolatile>(field_offset), kIsVolatile);
528   }
529   Verify<kVerifyFlags>();
530   SetFieldPrimitive<int64_t, kIsVolatile>(field_offset, new_value);
531 }
532 
533 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
SetField64Volatile(MemberOffset field_offset,int64_t new_value)534 inline void Object::SetField64Volatile(MemberOffset field_offset, int64_t new_value) {
535   return SetField64<kTransactionActive, kCheckTransaction, kVerifyFlags, true>(field_offset,
536                                                                                new_value);
537 }
538 
539 template<bool kCheckTransaction, VerifyObjectFlags kVerifyFlags, bool kIsVolatile>
SetField64Transaction(MemberOffset field_offset,int32_t new_value)540 inline void Object::SetField64Transaction(MemberOffset field_offset, int32_t new_value) {
541   if (Runtime::Current()->IsActiveTransaction()) {
542     SetField64<true, kCheckTransaction, kVerifyFlags, kIsVolatile>(field_offset, new_value);
543   } else {
544     SetField64<false, kCheckTransaction, kVerifyFlags, kIsVolatile>(field_offset, new_value);
545   }
546 }
547 
548 template<typename kSize>
GetFieldAcquire(MemberOffset field_offset)549 inline kSize Object::GetFieldAcquire(MemberOffset field_offset) {
550   const uint8_t* raw_addr = reinterpret_cast<const uint8_t*>(this) + field_offset.Int32Value();
551   const kSize* addr = reinterpret_cast<const kSize*>(raw_addr);
552   return reinterpret_cast<const Atomic<kSize>*>(addr)->load(std::memory_order_acquire);
553 }
554 
555 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
CasFieldWeakSequentiallyConsistent64(MemberOffset field_offset,int64_t old_value,int64_t new_value)556 inline bool Object::CasFieldWeakSequentiallyConsistent64(MemberOffset field_offset,
557                                                          int64_t old_value,
558                                                          int64_t new_value) {
559   VerifyTransaction<kTransactionActive, kCheckTransaction>();
560   Verify<kVerifyFlags>();
561   uint8_t* raw_addr = reinterpret_cast<uint8_t*>(this) + field_offset.Int32Value();
562   Atomic<int64_t>* atomic_addr = reinterpret_cast<Atomic<int64_t>*>(raw_addr);
563   bool success = atomic_addr->CompareAndSetWeakSequentiallyConsistent(old_value, new_value);
564   if (kTransactionActive && success) {
565     Runtime::Current()->GetClassLinker()->RecordWriteField64(
566         this, field_offset, old_value, /*is_volatile=*/ true);
567   }
568   return success;
569 }
570 
571 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
CasFieldStrongSequentiallyConsistent64(MemberOffset field_offset,int64_t old_value,int64_t new_value)572 inline bool Object::CasFieldStrongSequentiallyConsistent64(MemberOffset field_offset,
573                                                            int64_t old_value,
574                                                            int64_t new_value) {
575   VerifyTransaction<kTransactionActive, kCheckTransaction>();
576   Verify<kVerifyFlags>();
577   uint8_t* raw_addr = reinterpret_cast<uint8_t*>(this) + field_offset.Int32Value();
578   Atomic<int64_t>* atomic_addr = reinterpret_cast<Atomic<int64_t>*>(raw_addr);
579   bool success = atomic_addr->CompareAndSetStrongSequentiallyConsistent(old_value, new_value);
580   if (kTransactionActive && success) {
581     Runtime::Current()->GetClassLinker()->RecordWriteField64(
582         this, field_offset, old_value, /*is_volatile=*/ true);
583   }
584   return success;
585 }
586 
587 /*
588  * Returns a pointer to an object representing what the field points to, not an
589  * object representing the field.
590  */
591 template<class T,
592          VerifyObjectFlags kVerifyFlags,
593          ReadBarrierOption kReadBarrierOption,
594          bool kIsVolatile>
GetFieldObject(MemberOffset field_offset)595 inline T* Object::GetFieldObject(MemberOffset field_offset) {
596   Verify<kVerifyFlags>();
597   uint8_t* raw_addr = reinterpret_cast<uint8_t*>(this) + field_offset.Int32Value();
598   HeapReference<T>* objref_addr = reinterpret_cast<HeapReference<T>*>(raw_addr);
599   T* result = ReadBarrier::Barrier<T, kIsVolatile, kReadBarrierOption>(
600       this,
601       field_offset,
602       objref_addr);
603   VerifyRead<kVerifyFlags>(result);
604   return result;
605 }
606 
607 template<class T, VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
GetFieldObjectVolatile(MemberOffset field_offset)608 inline T* Object::GetFieldObjectVolatile(MemberOffset field_offset) {
609   return GetFieldObject<T, kVerifyFlags, kReadBarrierOption, true>(field_offset);
610 }
611 
612 template<bool kTransactionActive,
613          bool kCheckTransaction,
614          VerifyObjectFlags kVerifyFlags,
615          bool kIsVolatile>
SetFieldObjectWithoutWriteBarrier(MemberOffset field_offset,ObjPtr<Object> new_value)616 inline void Object::SetFieldObjectWithoutWriteBarrier(MemberOffset field_offset,
617                                                       ObjPtr<Object> new_value) {
618   VerifyTransaction<kTransactionActive, kCheckTransaction>();
619   if (kTransactionActive) {
620     ObjPtr<Object> old_value =
621         GetFieldObject<Object, kVerifyFlags, kWithReadBarrier, kIsVolatile>(field_offset);
622     Runtime::Current()->GetClassLinker()->RecordWriteFieldReference(
623         this, field_offset, old_value, kIsVolatile);
624   }
625   Verify<kVerifyFlags>();
626   VerifyWrite<kVerifyFlags>(new_value);
627   uint8_t* raw_addr = reinterpret_cast<uint8_t*>(this) + field_offset.Int32Value();
628   HeapReference<Object>* objref_addr = reinterpret_cast<HeapReference<Object>*>(raw_addr);
629   objref_addr->Assign<kIsVolatile>(new_value.Ptr());
630 }
631 
632 template<bool kTransactionActive,
633          bool kCheckTransaction,
634          VerifyObjectFlags kVerifyFlags,
635          bool kIsVolatile>
SetFieldObject(MemberOffset field_offset,ObjPtr<Object> new_value)636 inline void Object::SetFieldObject(MemberOffset field_offset, ObjPtr<Object> new_value) {
637   SetFieldObjectWithoutWriteBarrier<kTransactionActive, kCheckTransaction, kVerifyFlags,
638       kIsVolatile>(field_offset, new_value);
639   if (new_value != nullptr) {
640     WriteBarrier::ForFieldWrite<WriteBarrier::kWithoutNullCheck>(this, field_offset, new_value);
641     // TODO: Check field assignment could theoretically cause thread suspension, TODO: fix this.
642     CheckFieldAssignment(field_offset, new_value);
643   }
644 }
645 
646 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
SetFieldObjectVolatile(MemberOffset field_offset,ObjPtr<Object> new_value)647 inline void Object::SetFieldObjectVolatile(MemberOffset field_offset, ObjPtr<Object> new_value) {
648   SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags, true>(field_offset,
649                                                                             new_value);
650 }
651 
652 template<bool kCheckTransaction, VerifyObjectFlags kVerifyFlags, bool kIsVolatile>
SetFieldObjectTransaction(MemberOffset field_offset,ObjPtr<Object> new_value)653 inline void Object::SetFieldObjectTransaction(MemberOffset field_offset, ObjPtr<Object> new_value) {
654   if (Runtime::Current()->IsActiveTransaction()) {
655     SetFieldObject<true, kCheckTransaction, kVerifyFlags, kIsVolatile>(field_offset, new_value);
656   } else {
657     SetFieldObject<false, kCheckTransaction, kVerifyFlags, kIsVolatile>(field_offset, new_value);
658   }
659 }
660 
661 template <VerifyObjectFlags kVerifyFlags>
GetFieldObjectReferenceAddr(MemberOffset field_offset)662 inline HeapReference<Object>* Object::GetFieldObjectReferenceAddr(MemberOffset field_offset) {
663   Verify<kVerifyFlags>();
664   return reinterpret_cast<HeapReference<Object>*>(reinterpret_cast<uint8_t*>(this) +
665       field_offset.Int32Value());
666 }
667 
668 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
CasFieldObjectWithoutWriteBarrier(MemberOffset field_offset,ObjPtr<Object> old_value,ObjPtr<Object> new_value,CASMode mode,std::memory_order memory_order)669 inline bool Object::CasFieldObjectWithoutWriteBarrier(MemberOffset field_offset,
670                                                       ObjPtr<Object> old_value,
671                                                       ObjPtr<Object> new_value,
672                                                       CASMode mode,
673                                                       std::memory_order memory_order) {
674   VerifyTransaction<kTransactionActive, kCheckTransaction>();
675   VerifyCAS<kVerifyFlags>(new_value, old_value);
676   uint32_t old_ref(PtrCompression<kPoisonHeapReferences, Object>::Compress(old_value));
677   uint32_t new_ref(PtrCompression<kPoisonHeapReferences, Object>::Compress(new_value));
678   uint8_t* raw_addr = reinterpret_cast<uint8_t*>(this) + field_offset.Int32Value();
679   Atomic<uint32_t>* atomic_addr = reinterpret_cast<Atomic<uint32_t>*>(raw_addr);
680   bool success = atomic_addr->CompareAndSet(old_ref, new_ref, mode, memory_order);
681   if (kTransactionActive && success) {
682     Runtime::Current()->GetClassLinker()->RecordWriteFieldReference(
683         this, field_offset, old_value, /*is_volatile=*/ true);
684   }
685   return success;
686 }
687 
688 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
CasFieldObject(MemberOffset field_offset,ObjPtr<Object> old_value,ObjPtr<Object> new_value,CASMode mode,std::memory_order memory_order)689 inline bool Object::CasFieldObject(MemberOffset field_offset,
690                                    ObjPtr<Object> old_value,
691                                    ObjPtr<Object> new_value,
692                                    CASMode mode,
693                                    std::memory_order memory_order) {
694   bool success = CasFieldObjectWithoutWriteBarrier<
695       kTransactionActive, kCheckTransaction, kVerifyFlags>(field_offset,
696                                                            old_value,
697                                                            new_value,
698                                                            mode,
699                                                            memory_order);
700   if (success) {
701     WriteBarrier::ForFieldWrite(this, field_offset, new_value);
702   }
703   return success;
704 }
705 
706 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
CompareAndExchangeFieldObject(MemberOffset field_offset,ObjPtr<Object> old_value,ObjPtr<Object> new_value)707 inline ObjPtr<Object> Object::CompareAndExchangeFieldObject(MemberOffset field_offset,
708                                                             ObjPtr<Object> old_value,
709                                                             ObjPtr<Object> new_value) {
710   VerifyTransaction<kTransactionActive, kCheckTransaction>();
711   VerifyCAS<kVerifyFlags>(new_value, old_value);
712   uint32_t old_ref(PtrCompression<kPoisonHeapReferences, Object>::Compress(old_value));
713   uint32_t new_ref(PtrCompression<kPoisonHeapReferences, Object>::Compress(new_value));
714   uint8_t* raw_addr = reinterpret_cast<uint8_t*>(this) + field_offset.Int32Value();
715   Atomic<uint32_t>* atomic_addr = reinterpret_cast<Atomic<uint32_t>*>(raw_addr);
716   bool success = atomic_addr->compare_exchange_strong(old_ref, new_ref, std::memory_order_seq_cst);
717   ObjPtr<Object> witness_value(PtrCompression<kPoisonHeapReferences, Object>::Decompress(old_ref));
718   if (kIsDebugBuild) {
719     // Ensure caller has done read barrier on the reference field so it's in the to-space.
720     ReadBarrier::AssertToSpaceInvariant(witness_value.Ptr());
721   }
722   if (success) {
723     if (kTransactionActive) {
724       Runtime::Current()->GetClassLinker()->RecordWriteFieldReference(
725           this, field_offset, witness_value, /*is_volatile=*/ true);
726     }
727     WriteBarrier::ForFieldWrite(this, field_offset, new_value);
728   }
729   VerifyRead<kVerifyFlags>(witness_value);
730   return witness_value;
731 }
732 
733 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
ExchangeFieldObject(MemberOffset field_offset,ObjPtr<Object> new_value)734 inline ObjPtr<Object> Object::ExchangeFieldObject(MemberOffset field_offset,
735                                                   ObjPtr<Object> new_value) {
736   VerifyTransaction<kTransactionActive, kCheckTransaction>();
737   VerifyCAS<kVerifyFlags>(new_value, /*old_value=*/ nullptr);
738 
739   uint32_t new_ref(PtrCompression<kPoisonHeapReferences, Object>::Compress(new_value));
740   uint8_t* raw_addr = reinterpret_cast<uint8_t*>(this) + field_offset.Int32Value();
741   Atomic<uint32_t>* atomic_addr = reinterpret_cast<Atomic<uint32_t>*>(raw_addr);
742   uint32_t old_ref = atomic_addr->exchange(new_ref, std::memory_order_seq_cst);
743   ObjPtr<Object> old_value(PtrCompression<kPoisonHeapReferences, Object>::Decompress(old_ref));
744   if (kIsDebugBuild) {
745     // Ensure caller has done read barrier on the reference field so it's in the to-space.
746     ReadBarrier::AssertToSpaceInvariant(old_value.Ptr());
747   }
748   if (kTransactionActive) {
749     Runtime::Current()->GetClassLinker()->RecordWriteFieldReference(
750         this, field_offset, old_value, /*is_volatile=*/ true);
751   }
752   WriteBarrier::ForFieldWrite(this, field_offset, new_value);
753   VerifyRead<kVerifyFlags>(old_value);
754   return old_value;
755 }
756 
757 template<typename T, VerifyObjectFlags kVerifyFlags>
GetPrimitiveFieldViaAccessor(MemberOffset field_offset,Accessor<T> * accessor)758 inline void Object::GetPrimitiveFieldViaAccessor(MemberOffset field_offset, Accessor<T>* accessor) {
759   Verify<kVerifyFlags>();
760   uint8_t* raw_addr = reinterpret_cast<uint8_t*>(this) + field_offset.Int32Value();
761   T* addr = reinterpret_cast<T*>(raw_addr);
762   accessor->Access(addr);
763 }
764 
765 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
UpdateFieldBooleanViaAccessor(MemberOffset field_offset,Accessor<uint8_t> * accessor)766 inline void Object::UpdateFieldBooleanViaAccessor(MemberOffset field_offset,
767                                                   Accessor<uint8_t>* accessor) {
768   VerifyTransaction<kTransactionActive, kCheckTransaction>();
769   if (kTransactionActive) {
770     static const bool kIsVolatile = true;
771     uint8_t old_value = GetFieldBoolean<kVerifyFlags, kIsVolatile>(field_offset);
772     Runtime::Current()->GetClassLinker()->RecordWriteFieldBoolean(
773         this, field_offset, old_value, kIsVolatile);
774   }
775   Verify<kVerifyFlags>();
776   uint8_t* raw_addr = reinterpret_cast<uint8_t*>(this) + field_offset.Int32Value();
777   uint8_t* addr = raw_addr;
778   accessor->Access(addr);
779 }
780 
781 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
UpdateFieldByteViaAccessor(MemberOffset field_offset,Accessor<int8_t> * accessor)782 inline void Object::UpdateFieldByteViaAccessor(MemberOffset field_offset,
783                                                Accessor<int8_t>* accessor) {
784   VerifyTransaction<kTransactionActive, kCheckTransaction>();
785   if (kTransactionActive) {
786     static const bool kIsVolatile = true;
787     int8_t old_value = GetFieldByte<kVerifyFlags, kIsVolatile>(field_offset);
788     Runtime::Current()->GetClassLinker()->RecordWriteFieldByte(
789         this, field_offset, old_value, kIsVolatile);
790   }
791   Verify<kVerifyFlags>();
792   uint8_t* raw_addr = reinterpret_cast<uint8_t*>(this) + field_offset.Int32Value();
793   int8_t* addr = reinterpret_cast<int8_t*>(raw_addr);
794   accessor->Access(addr);
795 }
796 
797 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
UpdateFieldCharViaAccessor(MemberOffset field_offset,Accessor<uint16_t> * accessor)798 inline void Object::UpdateFieldCharViaAccessor(MemberOffset field_offset,
799                                                Accessor<uint16_t>* accessor) {
800   VerifyTransaction<kTransactionActive, kCheckTransaction>();
801   if (kTransactionActive) {
802     static const bool kIsVolatile = true;
803     uint16_t old_value = GetFieldChar<kVerifyFlags, kIsVolatile>(field_offset);
804     Runtime::Current()->GetClassLinker()->RecordWriteFieldChar(
805         this, field_offset, old_value, kIsVolatile);
806   }
807   Verify<kVerifyFlags>();
808   uint8_t* raw_addr = reinterpret_cast<uint8_t*>(this) + field_offset.Int32Value();
809   uint16_t* addr = reinterpret_cast<uint16_t*>(raw_addr);
810   accessor->Access(addr);
811 }
812 
813 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
UpdateFieldShortViaAccessor(MemberOffset field_offset,Accessor<int16_t> * accessor)814 inline void Object::UpdateFieldShortViaAccessor(MemberOffset field_offset,
815                                                 Accessor<int16_t>* accessor) {
816   VerifyTransaction<kTransactionActive, kCheckTransaction>();
817   if (kTransactionActive) {
818     static const bool kIsVolatile = true;
819     int16_t old_value = GetFieldShort<kVerifyFlags, kIsVolatile>(field_offset);
820     Runtime::Current()->GetClassLinker()->RecordWriteFieldShort(
821         this, field_offset, old_value, kIsVolatile);
822   }
823   Verify<kVerifyFlags>();
824   uint8_t* raw_addr = reinterpret_cast<uint8_t*>(this) + field_offset.Int32Value();
825   int16_t* addr = reinterpret_cast<int16_t*>(raw_addr);
826   accessor->Access(addr);
827 }
828 
829 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
UpdateField32ViaAccessor(MemberOffset field_offset,Accessor<int32_t> * accessor)830 inline void Object::UpdateField32ViaAccessor(MemberOffset field_offset,
831                                              Accessor<int32_t>* accessor) {
832   VerifyTransaction<kTransactionActive, kCheckTransaction>();
833   if (kTransactionActive) {
834     static const bool kIsVolatile = true;
835     int32_t old_value = GetField32<kVerifyFlags, kIsVolatile>(field_offset);
836     Runtime::Current()->GetClassLinker()->RecordWriteField32(
837         this, field_offset, old_value, kIsVolatile);
838   }
839   Verify<kVerifyFlags>();
840   uint8_t* raw_addr = reinterpret_cast<uint8_t*>(this) + field_offset.Int32Value();
841   int32_t* addr = reinterpret_cast<int32_t*>(raw_addr);
842   accessor->Access(addr);
843 }
844 
845 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
UpdateField64ViaAccessor(MemberOffset field_offset,Accessor<int64_t> * accessor)846 inline void Object::UpdateField64ViaAccessor(MemberOffset field_offset,
847                                              Accessor<int64_t>* accessor) {
848   VerifyTransaction<kTransactionActive, kCheckTransaction>();
849   if (kTransactionActive) {
850     static const bool kIsVolatile = true;
851     int64_t old_value = GetField64<kVerifyFlags, kIsVolatile>(field_offset);
852     Runtime::Current()->GetClassLinker()->RecordWriteField64(
853         this, field_offset, old_value, kIsVolatile);
854   }
855   Verify<kVerifyFlags>();
856   uint8_t* raw_addr = reinterpret_cast<uint8_t*>(this) + field_offset.Int32Value();
857   int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
858   accessor->Access(addr);
859 }
860 
861 template<bool kIsStatic,
862          VerifyObjectFlags kVerifyFlags,
863          ReadBarrierOption kReadBarrierOption,
864          typename Visitor>
VisitFieldsReferences(uint32_t ref_offsets,const Visitor & visitor)865 inline void Object::VisitFieldsReferences(uint32_t ref_offsets, const Visitor& visitor) {
866   if (!kIsStatic && (ref_offsets != mirror::Class::kClassWalkSuper)) {
867     // Instance fields and not the slow-path.
868     uint32_t field_offset = mirror::kObjectHeaderSize;
869     while (ref_offsets != 0) {
870       if ((ref_offsets & 1) != 0) {
871         visitor(this, MemberOffset(field_offset), kIsStatic);
872       }
873       ref_offsets >>= 1;
874       field_offset += sizeof(mirror::HeapReference<mirror::Object>);
875     }
876   } else {
877     // There is no reference offset bitmap. In the non-static case, walk up the class
878     // inheritance hierarchy and find reference offsets the hard way. In the static case, just
879     // consider this class.
880     for (ObjPtr<Class> klass = kIsStatic
881             ? ObjPtr<Class>::DownCast(this)
882             : GetClass<kVerifyFlags, kReadBarrierOption>();
883         klass != nullptr;
884         klass = kIsStatic ? nullptr : klass->GetSuperClass<kVerifyFlags, kReadBarrierOption>()) {
885       const size_t num_reference_fields =
886           kIsStatic ? klass->NumReferenceStaticFields() : klass->NumReferenceInstanceFields();
887       if (num_reference_fields == 0u) {
888         continue;
889       }
890       // Presumably GC can happen when we are cross compiling, it should not cause performance
891       // problems to do pointer size logic.
892       MemberOffset field_offset = kIsStatic
893           ? klass->GetFirstReferenceStaticFieldOffset<kVerifyFlags>(
894               Runtime::Current()->GetClassLinker()->GetImagePointerSize())
895           : klass->GetFirstReferenceInstanceFieldOffset<kVerifyFlags, kReadBarrierOption>();
896       for (size_t i = 0u; i < num_reference_fields; ++i) {
897         // TODO: Do a simpler check?
898         if (field_offset.Uint32Value() != ClassOffset().Uint32Value()) {
899           visitor(this, field_offset, kIsStatic);
900         }
901         field_offset = MemberOffset(field_offset.Uint32Value() +
902                                     sizeof(mirror::HeapReference<mirror::Object>));
903       }
904     }
905   }
906 }
907 
908 template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption, typename Visitor>
VisitInstanceFieldsReferences(ObjPtr<Class> klass,const Visitor & visitor)909 inline void Object::VisitInstanceFieldsReferences(ObjPtr<Class> klass, const Visitor& visitor) {
910   VisitFieldsReferences<false, kVerifyFlags, kReadBarrierOption>(
911       klass->GetReferenceInstanceOffsets<kVerifyFlags>(), visitor);
912 }
913 
914 template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption, typename Visitor>
VisitStaticFieldsReferences(ObjPtr<Class> klass,const Visitor & visitor)915 inline void Object::VisitStaticFieldsReferences(ObjPtr<Class> klass, const Visitor& visitor) {
916   DCHECK(!klass->IsTemp<kVerifyFlags>());
917   klass->VisitFieldsReferences<true, kVerifyFlags, kReadBarrierOption>(0, visitor);
918 }
919 
920 template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
IsClassLoader()921 inline bool Object::IsClassLoader() {
922   return GetClass<kVerifyFlags, kReadBarrierOption>()->template IsClassLoaderClass<kVerifyFlags>();
923 }
924 
925 template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
AsClassLoader()926 inline ObjPtr<ClassLoader> Object::AsClassLoader() {
927   DCHECK((IsClassLoader<kVerifyFlags, kReadBarrierOption>()));
928   return ObjPtr<ClassLoader>::DownCast(this);
929 }
930 
931 template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
IsDexCache()932 inline bool Object::IsDexCache() {
933   return GetClass<kVerifyFlags, kReadBarrierOption>()->template IsDexCacheClass<kVerifyFlags>();
934 }
935 
936 template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
AsDexCache()937 inline ObjPtr<mirror::DexCache> Object::AsDexCache() {
938   DCHECK((IsDexCache<kVerifyFlags, kReadBarrierOption>()));
939   return ObjPtr<DexCache>::DownCast(this);
940 }
941 
942 template<bool kTransactionActive, bool kCheckTransaction>
VerifyTransaction()943 inline void Object::VerifyTransaction() {
944   if (kCheckTransaction) {
945     DCHECK_EQ(kTransactionActive, Runtime::Current()->IsActiveTransaction());
946   }
947 }
948 
949 }  // namespace mirror
950 }  // namespace art
951 
952 #endif  // ART_RUNTIME_MIRROR_OBJECT_INL_H_
953