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_H_
18 #define ART_RUNTIME_MIRROR_OBJECT_H_
19 
20 #include "base/atomic.h"
21 #include "base/casts.h"
22 #include "base/enums.h"
23 #include "dex/primitive.h"
24 #include "obj_ptr.h"
25 #include "object_reference.h"
26 #include "offsets.h"
27 #include "read_barrier_config.h"
28 #include "read_barrier_option.h"
29 #include "runtime_globals.h"
30 #include "verify_object.h"
31 
32 namespace art {
33 
34 class ArtField;
35 class ArtMethod;
36 template <class T> class Handle;
37 class LockWord;
38 class Monitor;
39 struct ObjectOffsets;
40 class Thread;
41 class VoidFunctor;
42 
43 namespace mirror {
44 
45 class Array;
46 class Class;
47 class ClassLoader;
48 class DexCache;
49 class FinalizerReference;
50 template<class T> class ObjectArray;
51 template<class T> class PrimitiveArray;
52 typedef PrimitiveArray<uint8_t> BooleanArray;
53 typedef PrimitiveArray<int8_t> ByteArray;
54 typedef PrimitiveArray<uint16_t> CharArray;
55 typedef PrimitiveArray<double> DoubleArray;
56 typedef PrimitiveArray<float> FloatArray;
57 typedef PrimitiveArray<int32_t> IntArray;
58 typedef PrimitiveArray<int64_t> LongArray;
59 typedef PrimitiveArray<int16_t> ShortArray;
60 class Reference;
61 class String;
62 class Throwable;
63 
64 // Fields within mirror objects aren't accessed directly so that the appropriate amount of
65 // handshaking is done with GC (for example, read and write barriers). This macro is used to
66 // compute an offset for the Set/Get methods defined in Object that can safely access fields.
67 #define OFFSET_OF_OBJECT_MEMBER(type, field) \
68     MemberOffset(OFFSETOF_MEMBER(type, field))
69 
70 // Checks that we don't do field assignments which violate the typing system.
71 static constexpr bool kCheckFieldAssignments = false;
72 
73 // Size of Object.
74 static constexpr uint32_t kObjectHeaderSize = kUseBrooksReadBarrier ? 16 : 8;
75 
76 // C++ mirror of java.lang.Object
77 class MANAGED LOCKABLE Object {
78  public:
79   // The number of vtable entries in java.lang.Object.
80   static constexpr size_t kVTableLength = 11;
81 
82   // The size of the java.lang.Class representing a java.lang.Object.
83   static uint32_t ClassSize(PointerSize pointer_size);
84 
85   // Size of an instance of java.lang.Object.
InstanceSize()86   static constexpr uint32_t InstanceSize() {
87     return sizeof(Object);
88   }
89 
ClassOffset()90   static constexpr MemberOffset ClassOffset() {
91     return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
92   }
93 
94   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
95            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
96   ALWAYS_INLINE Class* GetClass() REQUIRES_SHARED(Locks::mutator_lock_);
97 
98   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
99   void SetClass(ObjPtr<Class> new_klass) REQUIRES_SHARED(Locks::mutator_lock_);
100 
101   // Get the read barrier state with a fake address dependency.
102   // '*fake_address_dependency' will be set to 0.
103   ALWAYS_INLINE uint32_t GetReadBarrierState(uintptr_t* fake_address_dependency)
104       REQUIRES_SHARED(Locks::mutator_lock_);
105   // This version does not offer any special mechanism to prevent load-load reordering.
106   ALWAYS_INLINE uint32_t GetReadBarrierState() REQUIRES_SHARED(Locks::mutator_lock_);
107   // Get the read barrier state with a load-acquire.
108   ALWAYS_INLINE uint32_t GetReadBarrierStateAcquire() REQUIRES_SHARED(Locks::mutator_lock_);
109 
110   ALWAYS_INLINE void SetReadBarrierState(uint32_t rb_state) REQUIRES_SHARED(Locks::mutator_lock_);
111 
112   template<std::memory_order kMemoryOrder = std::memory_order_relaxed>
113   ALWAYS_INLINE bool AtomicSetReadBarrierState(uint32_t expected_rb_state, uint32_t rb_state)
114       REQUIRES_SHARED(Locks::mutator_lock_);
115 
116   ALWAYS_INLINE uint32_t GetMarkBit() REQUIRES_SHARED(Locks::mutator_lock_);
117 
118   ALWAYS_INLINE bool AtomicSetMarkBit(uint32_t expected_mark_bit, uint32_t mark_bit)
119       REQUIRES_SHARED(Locks::mutator_lock_);
120 
121   // Assert that the read barrier state is in the default (white, i.e. non-gray) state.
122   ALWAYS_INLINE void AssertReadBarrierState() const REQUIRES_SHARED(Locks::mutator_lock_);
123 
124   // The verifier treats all interfaces as java.lang.Object and relies on runtime checks in
125   // invoke-interface to detect incompatible interface types.
126   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
127   bool VerifierInstanceOf(ObjPtr<Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
128   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
129   ALWAYS_INLINE bool InstanceOf(ObjPtr<Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
130 
131   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
132   size_t SizeOf() REQUIRES_SHARED(Locks::mutator_lock_);
133 
134   static ObjPtr<Object> Clone(Handle<Object> h_this, Thread* self)
135       REQUIRES_SHARED(Locks::mutator_lock_)
136       REQUIRES(!Roles::uninterruptible_);
137 
138   int32_t IdentityHashCode()
139       REQUIRES_SHARED(Locks::mutator_lock_)
140       REQUIRES(!Locks::thread_list_lock_,
141                !Locks::thread_suspend_count_lock_);
142 
MonitorOffset()143   static constexpr MemberOffset MonitorOffset() {
144     return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
145   }
146 
147   // As_volatile can be false if the mutators are suspended. This is an optimization since it
148   // avoids the barriers.
149   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
150   LockWord GetLockWord(bool as_volatile) REQUIRES_SHARED(Locks::mutator_lock_);
151   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
152   void SetLockWord(LockWord new_val, bool as_volatile) REQUIRES_SHARED(Locks::mutator_lock_);
153   bool CasLockWord(LockWord old_val, LockWord new_val, CASMode mode, std::memory_order memory_order)
154       REQUIRES_SHARED(Locks::mutator_lock_);
155   uint32_t GetLockOwnerThreadId();
156 
157   // Try to enter the monitor, returns non null if we succeeded.
158   ObjPtr<mirror::Object> MonitorTryEnter(Thread* self)
159       EXCLUSIVE_LOCK_FUNCTION()
160       REQUIRES(!Roles::uninterruptible_)
161       REQUIRES_SHARED(Locks::mutator_lock_);
162   ObjPtr<mirror::Object> MonitorEnter(Thread* self)
163       EXCLUSIVE_LOCK_FUNCTION()
164       REQUIRES(!Roles::uninterruptible_)
165       REQUIRES_SHARED(Locks::mutator_lock_);
166   bool MonitorExit(Thread* self)
167       REQUIRES(!Roles::uninterruptible_)
168       REQUIRES_SHARED(Locks::mutator_lock_)
169       UNLOCK_FUNCTION();
170   void Notify(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
171   void NotifyAll(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
172   void Wait(Thread* self, int64_t timeout, int32_t nanos) REQUIRES_SHARED(Locks::mutator_lock_);
173 
174   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
175   bool IsClass() REQUIRES_SHARED(Locks::mutator_lock_);
176   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
177   ObjPtr<Class> AsClass() REQUIRES_SHARED(Locks::mutator_lock_);
178 
179   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
180   bool IsObjectArray() REQUIRES_SHARED(Locks::mutator_lock_);
181   template<class T, VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
182   ObjPtr<ObjectArray<T>> AsObjectArray() REQUIRES_SHARED(Locks::mutator_lock_);
183 
184   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
185            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
186   bool IsClassLoader() REQUIRES_SHARED(Locks::mutator_lock_);
187   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
188            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
189   ObjPtr<ClassLoader> AsClassLoader() REQUIRES_SHARED(Locks::mutator_lock_);
190 
191   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
192            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
193   bool IsDexCache() REQUIRES_SHARED(Locks::mutator_lock_);
194   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
195            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
196   ObjPtr<DexCache> AsDexCache() REQUIRES_SHARED(Locks::mutator_lock_);
197 
198   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
199   bool IsArrayInstance() REQUIRES_SHARED(Locks::mutator_lock_);
200   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
201   ObjPtr<Array> AsArray() REQUIRES_SHARED(Locks::mutator_lock_);
202 
203   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
204   bool IsBooleanArray() REQUIRES_SHARED(Locks::mutator_lock_);
205   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
206   ObjPtr<BooleanArray> AsBooleanArray() REQUIRES_SHARED(Locks::mutator_lock_);
207 
208   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
209   bool IsByteArray() REQUIRES_SHARED(Locks::mutator_lock_);
210   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
211   ObjPtr<ByteArray> AsByteArray() REQUIRES_SHARED(Locks::mutator_lock_);
212 
213   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
214   bool IsCharArray() REQUIRES_SHARED(Locks::mutator_lock_);
215   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
216   ObjPtr<CharArray> AsCharArray() REQUIRES_SHARED(Locks::mutator_lock_);
217 
218   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
219   bool IsShortArray() REQUIRES_SHARED(Locks::mutator_lock_);
220   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
221   ObjPtr<ShortArray> AsShortArray() REQUIRES_SHARED(Locks::mutator_lock_);
222 
223   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
224   bool IsIntArray() REQUIRES_SHARED(Locks::mutator_lock_);
225   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
226   ObjPtr<IntArray> AsIntArray() REQUIRES_SHARED(Locks::mutator_lock_);
227   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
228   ObjPtr<IntArray> AsIntArrayUnchecked() REQUIRES_SHARED(Locks::mutator_lock_);
229 
230   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
231   bool IsLongArray() REQUIRES_SHARED(Locks::mutator_lock_);
232   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
233   ObjPtr<LongArray> AsLongArray() REQUIRES_SHARED(Locks::mutator_lock_);
234   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
235   ObjPtr<LongArray> AsLongArrayUnchecked() REQUIRES_SHARED(Locks::mutator_lock_);
236 
237   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
238   bool IsFloatArray() REQUIRES_SHARED(Locks::mutator_lock_);
239   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
240   ObjPtr<FloatArray> AsFloatArray() REQUIRES_SHARED(Locks::mutator_lock_);
241 
242   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
243   bool IsDoubleArray() REQUIRES_SHARED(Locks::mutator_lock_);
244   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
245   ObjPtr<DoubleArray> AsDoubleArray() REQUIRES_SHARED(Locks::mutator_lock_);
246 
247   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
248   bool IsString() REQUIRES_SHARED(Locks::mutator_lock_);
249 
250   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
251   ObjPtr<String> AsString() REQUIRES_SHARED(Locks::mutator_lock_);
252 
253   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
254   ObjPtr<Throwable> AsThrowable() REQUIRES_SHARED(Locks::mutator_lock_);
255 
256   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
257            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
258   bool IsReferenceInstance() REQUIRES_SHARED(Locks::mutator_lock_);
259   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
260            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
261   ObjPtr<Reference> AsReference() REQUIRES_SHARED(Locks::mutator_lock_);
262   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
263   bool IsWeakReferenceInstance() REQUIRES_SHARED(Locks::mutator_lock_);
264   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
265   bool IsSoftReferenceInstance() REQUIRES_SHARED(Locks::mutator_lock_);
266   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
267   bool IsFinalizerReferenceInstance() REQUIRES_SHARED(Locks::mutator_lock_);
268   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
269   ObjPtr<FinalizerReference> AsFinalizerReference() REQUIRES_SHARED(Locks::mutator_lock_);
270   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
271   bool IsPhantomReferenceInstance() REQUIRES_SHARED(Locks::mutator_lock_);
272 
273   // Accessor for Java type fields.
274   template<class T,
275            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
276            ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
277            bool kIsVolatile = false>
278   ALWAYS_INLINE T* GetFieldObject(MemberOffset field_offset)
279       REQUIRES_SHARED(Locks::mutator_lock_);
280 
281   template<class T,
282            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
283            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
284   ALWAYS_INLINE T* GetFieldObjectVolatile(MemberOffset field_offset)
285       REQUIRES_SHARED(Locks::mutator_lock_);
286 
287   template<bool kTransactionActive,
288            bool kCheckTransaction = true,
289            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
290            bool kIsVolatile = false>
291   ALWAYS_INLINE void SetFieldObjectWithoutWriteBarrier(MemberOffset field_offset,
292                                                        ObjPtr<Object> new_value)
293       REQUIRES_SHARED(Locks::mutator_lock_);
294 
295   template<bool kTransactionActive,
296            bool kCheckTransaction = true,
297            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
298            bool kIsVolatile = false>
299   ALWAYS_INLINE void SetFieldObject(MemberOffset field_offset, ObjPtr<Object> new_value)
300       REQUIRES_SHARED(Locks::mutator_lock_);
301 
302   template<bool kTransactionActive,
303            bool kCheckTransaction = true,
304            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
305   ALWAYS_INLINE void SetFieldObjectVolatile(MemberOffset field_offset, ObjPtr<Object> new_value)
306       REQUIRES_SHARED(Locks::mutator_lock_);
307 
308   template<bool kCheckTransaction = true,
309            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
310            bool kIsVolatile = false>
311   ALWAYS_INLINE void SetFieldObjectTransaction(MemberOffset field_offset, ObjPtr<Object> new_value)
312       REQUIRES_SHARED(Locks::mutator_lock_);
313 
314   template<bool kTransactionActive,
315            bool kCheckTransaction = true,
316            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
317   ALWAYS_INLINE bool CasFieldObject(MemberOffset field_offset,
318                                     ObjPtr<Object> old_value,
319                                     ObjPtr<Object> new_value,
320                                     CASMode mode,
321                                     std::memory_order memory_order)
322       REQUIRES_SHARED(Locks::mutator_lock_);
323   template<bool kTransactionActive,
324            bool kCheckTransaction = true,
325            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
326   ALWAYS_INLINE bool CasFieldObjectWithoutWriteBarrier(MemberOffset field_offset,
327                                                        ObjPtr<Object> old_value,
328                                                        ObjPtr<Object> new_value,
329                                                        CASMode mode,
330                                                        std::memory_order memory_order)
331       REQUIRES_SHARED(Locks::mutator_lock_);
332 
333   template<bool kTransactionActive,
334            bool kCheckTransaction = true,
335            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
336   ObjPtr<Object> CompareAndExchangeFieldObject(MemberOffset field_offset,
337                                                ObjPtr<Object> old_value,
338                                                ObjPtr<Object> new_value)
339       REQUIRES_SHARED(Locks::mutator_lock_);
340 
341   template<bool kTransactionActive,
342            bool kCheckTransaction = true,
343            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
344   ObjPtr<Object> ExchangeFieldObject(MemberOffset field_offset, ObjPtr<Object> new_value)
345       REQUIRES_SHARED(Locks::mutator_lock_);
346 
347   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
348   HeapReference<Object>* GetFieldObjectReferenceAddr(MemberOffset field_offset)
349       REQUIRES_SHARED(Locks::mutator_lock_);
350 
351   template<typename kType, bool kIsVolatile>
SetFieldPrimitive(MemberOffset field_offset,kType new_value)352   ALWAYS_INLINE void SetFieldPrimitive(MemberOffset field_offset, kType new_value)
353       REQUIRES_SHARED(Locks::mutator_lock_) {
354     uint8_t* raw_addr = reinterpret_cast<uint8_t*>(this) + field_offset.Int32Value();
355     kType* addr = reinterpret_cast<kType*>(raw_addr);
356     if (kIsVolatile) {
357       reinterpret_cast<Atomic<kType>*>(addr)->store(new_value, std::memory_order_seq_cst);
358     } else {
359       reinterpret_cast<Atomic<kType>*>(addr)->StoreJavaData(new_value);
360     }
361   }
362 
363   template<typename kType, bool kIsVolatile>
GetFieldPrimitive(MemberOffset field_offset)364   ALWAYS_INLINE kType GetFieldPrimitive(MemberOffset field_offset)
365       REQUIRES_SHARED(Locks::mutator_lock_) {
366     const uint8_t* raw_addr = reinterpret_cast<const uint8_t*>(this) + field_offset.Int32Value();
367     const kType* addr = reinterpret_cast<const kType*>(raw_addr);
368     if (kIsVolatile) {
369       return reinterpret_cast<const Atomic<kType>*>(addr)->load(std::memory_order_seq_cst);
370     } else {
371       return reinterpret_cast<const Atomic<kType>*>(addr)->LoadJavaData();
372     }
373   }
374 
375   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
GetFieldBoolean(MemberOffset field_offset)376   ALWAYS_INLINE uint8_t GetFieldBoolean(MemberOffset field_offset)
377       REQUIRES_SHARED(Locks::mutator_lock_) {
378     Verify<kVerifyFlags>();
379     return GetFieldPrimitive<uint8_t, kIsVolatile>(field_offset);
380   }
381 
382   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
383   ALWAYS_INLINE int8_t GetFieldByte(MemberOffset field_offset)
384       REQUIRES_SHARED(Locks::mutator_lock_);
385 
386   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
387   ALWAYS_INLINE uint8_t GetFieldBooleanVolatile(MemberOffset field_offset)
388       REQUIRES_SHARED(Locks::mutator_lock_);
389 
390   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
391   ALWAYS_INLINE int8_t GetFieldByteVolatile(MemberOffset field_offset)
392       REQUIRES_SHARED(Locks::mutator_lock_);
393 
394   template<bool kTransactionActive,
395            bool kCheckTransaction = true,
396            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
397            bool kIsVolatile = false>
398   ALWAYS_INLINE void SetFieldBoolean(MemberOffset field_offset, uint8_t new_value)
399       REQUIRES_SHARED(Locks::mutator_lock_);
400 
401   template<bool kTransactionActive,
402            bool kCheckTransaction = true,
403            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
404            bool kIsVolatile = false>
405   ALWAYS_INLINE void SetFieldByte(MemberOffset field_offset, int8_t new_value)
406       REQUIRES_SHARED(Locks::mutator_lock_);
407 
408   template<bool kTransactionActive,
409            bool kCheckTransaction = true,
410            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
411   ALWAYS_INLINE void SetFieldBooleanVolatile(MemberOffset field_offset, uint8_t new_value)
412       REQUIRES_SHARED(Locks::mutator_lock_);
413 
414   template<bool kTransactionActive,
415            bool kCheckTransaction = true,
416            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
417   ALWAYS_INLINE void SetFieldByteVolatile(MemberOffset field_offset, int8_t new_value)
418       REQUIRES_SHARED(Locks::mutator_lock_);
419 
420   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
421   ALWAYS_INLINE uint16_t GetFieldChar(MemberOffset field_offset)
422       REQUIRES_SHARED(Locks::mutator_lock_);
423 
424   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
425   ALWAYS_INLINE int16_t GetFieldShort(MemberOffset field_offset)
426       REQUIRES_SHARED(Locks::mutator_lock_);
427 
428   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
429   ALWAYS_INLINE uint16_t GetFieldCharVolatile(MemberOffset field_offset)
430       REQUIRES_SHARED(Locks::mutator_lock_);
431 
432   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
433   ALWAYS_INLINE int16_t GetFieldShortVolatile(MemberOffset field_offset)
434       REQUIRES_SHARED(Locks::mutator_lock_);
435 
436   template<bool kTransactionActive,
437            bool kCheckTransaction = true,
438            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
439            bool kIsVolatile = false>
440   ALWAYS_INLINE void SetFieldChar(MemberOffset field_offset, uint16_t new_value)
441       REQUIRES_SHARED(Locks::mutator_lock_);
442 
443   template<bool kTransactionActive,
444            bool kCheckTransaction = true,
445            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
446            bool kIsVolatile = false>
447   ALWAYS_INLINE void SetFieldShort(MemberOffset field_offset, int16_t new_value)
448       REQUIRES_SHARED(Locks::mutator_lock_);
449 
450   template<bool kTransactionActive,
451            bool kCheckTransaction = true,
452            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
453   ALWAYS_INLINE void SetFieldCharVolatile(MemberOffset field_offset, uint16_t new_value)
454       REQUIRES_SHARED(Locks::mutator_lock_);
455 
456   template<bool kTransactionActive,
457            bool kCheckTransaction = true,
458            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
459   ALWAYS_INLINE void SetFieldShortVolatile(MemberOffset field_offset, int16_t new_value)
460       REQUIRES_SHARED(Locks::mutator_lock_);
461 
462   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
GetField32(MemberOffset field_offset)463   ALWAYS_INLINE int32_t GetField32(MemberOffset field_offset)
464       REQUIRES_SHARED(Locks::mutator_lock_) {
465     Verify<kVerifyFlags>();
466     return GetFieldPrimitive<int32_t, kIsVolatile>(field_offset);
467   }
468 
469   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
GetField32Volatile(MemberOffset field_offset)470   ALWAYS_INLINE int32_t GetField32Volatile(MemberOffset field_offset)
471       REQUIRES_SHARED(Locks::mutator_lock_) {
472     return GetField32<kVerifyFlags, true>(field_offset);
473   }
474 
475   template<bool kTransactionActive,
476            bool kCheckTransaction = true,
477            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
478            bool kIsVolatile = false>
479   ALWAYS_INLINE void SetField32(MemberOffset field_offset, int32_t new_value)
480       REQUIRES_SHARED(Locks::mutator_lock_);
481 
482   template<bool kTransactionActive,
483            bool kCheckTransaction = true,
484            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
485   ALWAYS_INLINE void SetField32Volatile(MemberOffset field_offset, int32_t new_value)
486       REQUIRES_SHARED(Locks::mutator_lock_);
487 
488   template<bool kCheckTransaction = true,
489            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
490            bool kIsVolatile = false>
491   ALWAYS_INLINE void SetField32Transaction(MemberOffset field_offset, int32_t new_value)
492       REQUIRES_SHARED(Locks::mutator_lock_);
493 
494   template<bool kTransactionActive,
495            bool kCheckTransaction = true,
496            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
497   ALWAYS_INLINE bool CasField32(MemberOffset field_offset,
498                                 int32_t old_value,
499                                 int32_t new_value,
500                                 CASMode mode,
501                                 std::memory_order memory_order)
502       REQUIRES_SHARED(Locks::mutator_lock_);
503 
504   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
GetField64(MemberOffset field_offset)505   ALWAYS_INLINE int64_t GetField64(MemberOffset field_offset)
506       REQUIRES_SHARED(Locks::mutator_lock_) {
507     Verify<kVerifyFlags>();
508     return GetFieldPrimitive<int64_t, kIsVolatile>(field_offset);
509   }
510 
511   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
GetField64Volatile(MemberOffset field_offset)512   ALWAYS_INLINE int64_t GetField64Volatile(MemberOffset field_offset)
513       REQUIRES_SHARED(Locks::mutator_lock_) {
514     return GetField64<kVerifyFlags, true>(field_offset);
515   }
516 
517   template<bool kTransactionActive,
518            bool kCheckTransaction = true,
519            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
520            bool kIsVolatile = false>
521   ALWAYS_INLINE void SetField64(MemberOffset field_offset, int64_t new_value)
522       REQUIRES_SHARED(Locks::mutator_lock_);
523 
524   template<bool kTransactionActive,
525            bool kCheckTransaction = true,
526            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
527   ALWAYS_INLINE void SetField64Volatile(MemberOffset field_offset, int64_t new_value)
528       REQUIRES_SHARED(Locks::mutator_lock_);
529 
530   template<bool kCheckTransaction = true,
531            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
532            bool kIsVolatile = false>
533   ALWAYS_INLINE void SetField64Transaction(MemberOffset field_offset, int32_t new_value)
534       REQUIRES_SHARED(Locks::mutator_lock_);
535 
536   template<bool kTransactionActive,
537            bool kCheckTransaction = true,
538            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
539   bool CasFieldWeakSequentiallyConsistent64(MemberOffset field_offset,
540                                             int64_t old_value,
541                                             int64_t new_value)
542       REQUIRES_SHARED(Locks::mutator_lock_);
543 
544   template<bool kTransactionActive,
545            bool kCheckTransaction = true,
546            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
547   bool CasFieldStrongSequentiallyConsistent64(MemberOffset field_offset,
548                                               int64_t old_value,
549                                               int64_t new_value)
550       REQUIRES_SHARED(Locks::mutator_lock_);
551 
552   template<bool kTransactionActive,
553            bool kCheckTransaction = true,
554            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
555            typename T>
SetFieldPtr(MemberOffset field_offset,T new_value)556   void SetFieldPtr(MemberOffset field_offset, T new_value)
557       REQUIRES_SHARED(Locks::mutator_lock_) {
558     SetFieldPtrWithSize<kTransactionActive, kCheckTransaction, kVerifyFlags>(
559         field_offset, new_value, kRuntimePointerSize);
560   }
561   template<bool kTransactionActive,
562            bool kCheckTransaction = true,
563            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
564            typename T>
SetFieldPtr64(MemberOffset field_offset,T new_value)565   void SetFieldPtr64(MemberOffset field_offset, T new_value)
566       REQUIRES_SHARED(Locks::mutator_lock_) {
567     SetFieldPtrWithSize<kTransactionActive, kCheckTransaction, kVerifyFlags>(
568         field_offset, new_value, PointerSize::k64);
569   }
570 
571   template<bool kTransactionActive,
572            bool kCheckTransaction = true,
573            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
574            typename T>
SetFieldPtrWithSize(MemberOffset field_offset,T new_value,PointerSize pointer_size)575   ALWAYS_INLINE void SetFieldPtrWithSize(MemberOffset field_offset,
576                                          T new_value,
577                                          PointerSize pointer_size)
578       REQUIRES_SHARED(Locks::mutator_lock_) {
579     if (pointer_size == PointerSize::k32) {
580       SetField32<kTransactionActive, kCheckTransaction, kVerifyFlags>(
581           field_offset, reinterpret_cast32<int32_t>(new_value));
582     } else {
583       SetField64<kTransactionActive, kCheckTransaction, kVerifyFlags>(
584           field_offset, reinterpret_cast64<int64_t>(new_value));
585     }
586   }
587 
588   // Base class for accessors used to describe accesses performed by VarHandle methods.
589   template <typename T>
590   class Accessor {
591    public:
~Accessor()592     virtual ~Accessor() {
593       static_assert(std::is_arithmetic<T>::value, "unsupported type");
594     }
595     virtual void Access(T* field_address) = 0;
596   };
597 
598   // Getter method that exposes the raw address of a primitive value-type field to an Accessor
599   // instance. This are used by VarHandle accessor methods to read fields with a wider range of
600   // memory orderings than usually required.
601   template<typename T, VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
602   void GetPrimitiveFieldViaAccessor(MemberOffset field_offset, Accessor<T>* accessor)
603       REQUIRES_SHARED(Locks::mutator_lock_);
604 
605   // Update methods that expose the raw address of a primitive value-type to an Accessor instance
606   // that will attempt to update the field. These are used by VarHandle accessor methods to
607   // atomically update fields with a wider range of memory orderings than usually required.
608   template<bool kTransactionActive,
609            bool kCheckTransaction = true,
610            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
611   void UpdateFieldBooleanViaAccessor(MemberOffset field_offset, Accessor<uint8_t>* accessor)
612       REQUIRES_SHARED(Locks::mutator_lock_);
613   template<bool kTransactionActive,
614            bool kCheckTransaction = true,
615            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
616   void UpdateFieldByteViaAccessor(MemberOffset field_offset, Accessor<int8_t>* accessor)
617       REQUIRES_SHARED(Locks::mutator_lock_);
618   template<bool kTransactionActive,
619            bool kCheckTransaction = true,
620            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
621   void UpdateFieldCharViaAccessor(MemberOffset field_offset, Accessor<uint16_t>* accessor)
622       REQUIRES_SHARED(Locks::mutator_lock_);
623   template<bool kTransactionActive,
624            bool kCheckTransaction = true,
625            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
626   void UpdateFieldShortViaAccessor(MemberOffset field_offset, Accessor<int16_t>* accessor)
627       REQUIRES_SHARED(Locks::mutator_lock_);
628   template<bool kTransactionActive,
629            bool kCheckTransaction = true,
630            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
631   void UpdateField32ViaAccessor(MemberOffset field_offset, Accessor<int32_t>* accessor)
632       REQUIRES_SHARED(Locks::mutator_lock_);
633   template<bool kTransactionActive,
634            bool kCheckTransaction = true,
635            VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
636   void UpdateField64ViaAccessor(MemberOffset field_offset, Accessor<int64_t>* accessor)
637       REQUIRES_SHARED(Locks::mutator_lock_);
638 
639   // TODO fix thread safety analysis broken by the use of template. This should be
640   // REQUIRES_SHARED(Locks::mutator_lock_).
641   template <bool kVisitNativeRoots = true,
642             VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
643             ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
644             typename Visitor,
645             typename JavaLangRefVisitor = VoidFunctor>
646   void VisitReferences(const Visitor& visitor, const JavaLangRefVisitor& ref_visitor)
647       NO_THREAD_SAFETY_ANALYSIS;
648 
649   ArtField* FindFieldByOffset(MemberOffset offset) REQUIRES_SHARED(Locks::mutator_lock_);
650 
651   // Used by object_test.
652   static void SetHashCodeSeed(uint32_t new_seed);
653   // Generate an identity hash code. Public for object test.
654   static uint32_t GenerateIdentityHashCode();
655 
656   // Returns a human-readable form of the name of the *class* of the given object.
657   // So given an instance of java.lang.String, the output would
658   // be "java.lang.String". Given an array of int, the output would be "int[]".
659   // Given String.class, the output would be "java.lang.Class<java.lang.String>".
660   static std::string PrettyTypeOf(ObjPtr<mirror::Object> obj)
661       REQUIRES_SHARED(Locks::mutator_lock_);
662   std::string PrettyTypeOf()
663       REQUIRES_SHARED(Locks::mutator_lock_);
664 
665  protected:
666   // Accessors for non-Java type fields
667   template<class T, VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
GetFieldPtr(MemberOffset field_offset)668   T GetFieldPtr(MemberOffset field_offset)
669       REQUIRES_SHARED(Locks::mutator_lock_) {
670     return GetFieldPtrWithSize<T, kVerifyFlags, kIsVolatile>(field_offset, kRuntimePointerSize);
671   }
672   template<class T, VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
GetFieldPtr64(MemberOffset field_offset)673   T GetFieldPtr64(MemberOffset field_offset)
674       REQUIRES_SHARED(Locks::mutator_lock_) {
675     return GetFieldPtrWithSize<T, kVerifyFlags, kIsVolatile>(field_offset, PointerSize::k64);
676   }
677 
678   template<class T, VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
GetFieldPtrWithSize(MemberOffset field_offset,PointerSize pointer_size)679   ALWAYS_INLINE T GetFieldPtrWithSize(MemberOffset field_offset, PointerSize pointer_size)
680       REQUIRES_SHARED(Locks::mutator_lock_) {
681     if (pointer_size == PointerSize::k32) {
682       int32_t v = GetField32<kVerifyFlags, kIsVolatile>(field_offset);
683       return reinterpret_cast32<T>(v);
684     } else {
685       int64_t v = GetField64<kVerifyFlags, kIsVolatile>(field_offset);
686       return reinterpret_cast64<T>(v);
687     }
688   }
689 
690   // TODO: Fixme when anotatalysis works with visitors.
691   template<bool kIsStatic,
692           VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
693           ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
694           typename Visitor>
695   void VisitFieldsReferences(uint32_t ref_offsets, const Visitor& visitor) HOT_ATTR
696       NO_THREAD_SAFETY_ANALYSIS;
697   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
698            ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
699            typename Visitor>
700   void VisitInstanceFieldsReferences(ObjPtr<mirror::Class> klass, const Visitor& visitor) HOT_ATTR
701       REQUIRES_SHARED(Locks::mutator_lock_);
702   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
703            ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
704            typename Visitor>
705   void VisitStaticFieldsReferences(ObjPtr<mirror::Class> klass, const Visitor& visitor) HOT_ATTR
706       REQUIRES_SHARED(Locks::mutator_lock_);
707 
708  private:
709   // Get a field with acquire semantics.
710   template<typename kSize>
711   ALWAYS_INLINE kSize GetFieldAcquire(MemberOffset field_offset)
712       REQUIRES_SHARED(Locks::mutator_lock_);
713 
714   // Verify the type correctness of stores to fields.
715   // TODO: This can cause thread suspension and isn't moving GC safe.
716   void CheckFieldAssignmentImpl(MemberOffset field_offset, ObjPtr<Object> new_value)
717       REQUIRES_SHARED(Locks::mutator_lock_);
CheckFieldAssignment(MemberOffset field_offset,ObjPtr<Object> new_value)718   void CheckFieldAssignment(MemberOffset field_offset, ObjPtr<Object>new_value)
719       REQUIRES_SHARED(Locks::mutator_lock_) {
720     if (kCheckFieldAssignments) {
721       CheckFieldAssignmentImpl(field_offset, new_value);
722     }
723   }
724 
725   template<VerifyObjectFlags kVerifyFlags>
Verify()726   ALWAYS_INLINE void Verify() {
727     if (kVerifyFlags & kVerifyThis) {
728       VerifyObject(this);
729     }
730   }
731 
732   // Not ObjPtr since the values may be unaligned for logic in verification.cc.
733   template<VerifyObjectFlags kVerifyFlags, typename Reference>
VerifyRead(Reference value)734   ALWAYS_INLINE static void VerifyRead(Reference value) {
735     if (kVerifyFlags & kVerifyReads) {
736       VerifyObject(value);
737     }
738   }
739 
740   template<VerifyObjectFlags kVerifyFlags>
VerifyWrite(ObjPtr<mirror::Object> value)741   ALWAYS_INLINE static void VerifyWrite(ObjPtr<mirror::Object> value) {
742     if (kVerifyFlags & kVerifyWrites) {
743       VerifyObject(value);
744     }
745   }
746 
747   template<VerifyObjectFlags kVerifyFlags>
VerifyCAS(ObjPtr<mirror::Object> new_value,ObjPtr<mirror::Object> old_value)748   ALWAYS_INLINE void VerifyCAS(ObjPtr<mirror::Object> new_value, ObjPtr<mirror::Object> old_value) {
749     Verify<kVerifyFlags>();
750     VerifyRead<kVerifyFlags>(old_value);
751     VerifyWrite<kVerifyFlags>(new_value);
752   }
753 
754   // Verify transaction is active (if required).
755   template<bool kTransactionActive, bool kCheckTransaction>
756   ALWAYS_INLINE void VerifyTransaction();
757 
758   // A utility function that copies an object in a read barrier and write barrier-aware way.
759   // This is internally used by Clone() and Class::CopyOf(). If the object is finalizable,
760   // it is the callers job to call Heap::AddFinalizerReference.
761   static ObjPtr<Object> CopyObject(ObjPtr<mirror::Object> dest,
762                                    ObjPtr<mirror::Object> src,
763                                    size_t num_bytes)
764       REQUIRES_SHARED(Locks::mutator_lock_);
765 
766   template<VerifyObjectFlags kVerifyFlags, Primitive::Type kType>
767   bool IsSpecificPrimitiveArray() REQUIRES_SHARED(Locks::mutator_lock_);
768 
769   static Atomic<uint32_t> hash_code_seed;
770 
771   // The Class representing the type of the object.
772   HeapReference<Class> klass_;
773   // Monitor and hash code information.
774   uint32_t monitor_;
775 
776 #ifdef USE_BROOKS_READ_BARRIER
777   // Note names use a 'x' prefix and the x_rb_ptr_ is of type int
778   // instead of Object to go with the alphabetical/by-type field order
779   // on the Java side.
780   uint32_t x_rb_ptr_;      // For the Brooks pointer.
781   uint32_t x_xpadding_;    // For 8-byte alignment. TODO: get rid of this.
782 #endif
783 
784   friend class art::Monitor;
785   friend struct art::ObjectOffsets;  // for verifying offset information
786   friend class CopyObjectVisitor;  // for CopyObject().
787   friend class CopyClassVisitor;   // for CopyObject().
788   DISALLOW_ALLOCATION();
789   DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
790 };
791 
792 }  // namespace mirror
793 }  // namespace art
794 
795 #endif  // ART_RUNTIME_MIRROR_OBJECT_H_
796