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