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 "globals.h"
21 #include "object_reference.h"
22 #include "offsets.h"
23 #include "verify_object.h"
24 
25 namespace art {
26 
27 class ArtField;
28 class ArtMethod;
29 class ImageWriter;
30 class LockWord;
31 class Monitor;
32 struct ObjectOffsets;
33 class Thread;
34 class VoidFunctor;
35 
36 namespace mirror {
37 
38 class Array;
39 class Class;
40 class FinalizerReference;
41 template<class T> class ObjectArray;
42 template<class T> class PrimitiveArray;
43 typedef PrimitiveArray<uint8_t> BooleanArray;
44 typedef PrimitiveArray<int8_t> ByteArray;
45 typedef PrimitiveArray<uint16_t> CharArray;
46 typedef PrimitiveArray<double> DoubleArray;
47 typedef PrimitiveArray<float> FloatArray;
48 typedef PrimitiveArray<int32_t> IntArray;
49 typedef PrimitiveArray<int64_t> LongArray;
50 typedef PrimitiveArray<int16_t> ShortArray;
51 class Reference;
52 class String;
53 class Throwable;
54 
55 // Fields within mirror objects aren't accessed directly so that the appropriate amount of
56 // handshaking is done with GC (for example, read and write barriers). This macro is used to
57 // compute an offset for the Set/Get methods defined in Object that can safely access fields.
58 #define OFFSET_OF_OBJECT_MEMBER(type, field) \
59     MemberOffset(OFFSETOF_MEMBER(type, field))
60 
61 // Checks that we don't do field assignments which violate the typing system.
62 static constexpr bool kCheckFieldAssignments = false;
63 
64 // Size of Object.
65 static constexpr uint32_t kObjectHeaderSize = kUseBrooksReadBarrier ? 16 : 8;
66 
67 // C++ mirror of java.lang.Object
68 class MANAGED LOCKABLE Object {
69  public:
70   // The number of vtable entries in java.lang.Object.
71   static constexpr size_t kVTableLength = 11;
72 
73   // The size of the java.lang.Class representing a java.lang.Object.
74   static uint32_t ClassSize(size_t pointer_size);
75 
76   // Size of an instance of java.lang.Object.
InstanceSize()77   static constexpr uint32_t InstanceSize() {
78     return sizeof(Object);
79   }
80 
ClassOffset()81   static MemberOffset ClassOffset() {
82     return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
83   }
84 
85   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
86            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
87   ALWAYS_INLINE Class* GetClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
88 
89   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
90   void SetClass(Class* new_klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
91 
92   Object* GetReadBarrierPointer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
93 #ifndef USE_BAKER_OR_BROOKS_READ_BARRIER
94   NO_RETURN
95 #endif
96   void SetReadBarrierPointer(Object* rb_ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
97 #ifndef USE_BAKER_OR_BROOKS_READ_BARRIER
98   NO_RETURN
99 #endif
100   bool AtomicSetReadBarrierPointer(Object* expected_rb_ptr, Object* rb_ptr)
101       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
102   void AssertReadBarrierPointer() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
103 
104   // The verifier treats all interfaces as java.lang.Object and relies on runtime checks in
105   // invoke-interface to detect incompatible interface types.
106   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
107   bool VerifierInstanceOf(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
108   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
109   ALWAYS_INLINE bool InstanceOf(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
110 
111   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
112            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
113   size_t SizeOf() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
114 
115   Object* Clone(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
116 
117   int32_t IdentityHashCode() const
118       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
119       LOCKS_EXCLUDED(Locks::thread_list_lock_,
120                      Locks::thread_suspend_count_lock_);
121 
MonitorOffset()122   static MemberOffset MonitorOffset() {
123     return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
124   }
125 
126   // As_volatile can be false if the mutators are suspended. This is an optimization since it
127   // avoids the barriers.
128   LockWord GetLockWord(bool as_volatile) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
129   void SetLockWord(LockWord new_val, bool as_volatile) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
130   bool CasLockWordWeakSequentiallyConsistent(LockWord old_val, LockWord new_val)
131       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
132   bool CasLockWordWeakRelaxed(LockWord old_val, LockWord new_val)
133       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
134   uint32_t GetLockOwnerThreadId();
135 
136   mirror::Object* MonitorEnter(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
137       EXCLUSIVE_LOCK_FUNCTION();
138   bool MonitorExit(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
139       UNLOCK_FUNCTION();
140   void Notify(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
141   void NotifyAll(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
142   void Wait(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
143   void Wait(Thread* self, int64_t timeout, int32_t nanos) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
144 
145   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
146            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
147   bool IsClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
148   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
149            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
150   Class* AsClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
151 
152   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
153   bool IsObjectArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
154   template<class T, VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
155   ObjectArray<T>* AsObjectArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
156 
157   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
158            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
159   bool IsArrayInstance() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
160   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
161            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
162   Array* AsArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
163 
164   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
165   BooleanArray* AsBooleanArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
166   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
167   ByteArray* AsByteArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
168   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
169   ByteArray* AsByteSizedArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
170 
171   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
172   CharArray* AsCharArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
173   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
174   ShortArray* AsShortArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
175   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
176   ShortArray* AsShortSizedArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
177 
178   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
179   bool IsIntArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
180   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
181   IntArray* AsIntArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
182 
183   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
184   bool IsLongArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
185   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
186   LongArray* AsLongArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
187 
188   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
189   bool IsFloatArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
190   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
191   FloatArray* AsFloatArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
192 
193   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
194   bool IsDoubleArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
195   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
196   DoubleArray* AsDoubleArray() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
197 
198   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
199            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
200   bool IsString() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
201 
202   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
203            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
204   String* AsString() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
205 
206   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
207   Throwable* AsThrowable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
208 
209   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
210   bool IsReferenceInstance() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
211   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
212   Reference* AsReference() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
213   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
214   bool IsWeakReferenceInstance() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
215   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
216   bool IsSoftReferenceInstance() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
217   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
218   bool IsFinalizerReferenceInstance() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
219   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
220   FinalizerReference* AsFinalizerReference() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
221   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
222   bool IsPhantomReferenceInstance() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
223 
224   // Accessor for Java type fields.
225   template<class T, VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
226       ReadBarrierOption kReadBarrierOption = kWithReadBarrier, bool kIsVolatile = false>
227   ALWAYS_INLINE T* GetFieldObject(MemberOffset field_offset)
228       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
229 
230   template<class T, VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
231       ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
232   ALWAYS_INLINE T* GetFieldObjectVolatile(MemberOffset field_offset)
233       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
234 
235   template<bool kTransactionActive, bool kCheckTransaction = true,
236       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
237   ALWAYS_INLINE void SetFieldObjectWithoutWriteBarrier(MemberOffset field_offset, Object* new_value)
238       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
239 
240   template<bool kTransactionActive, bool kCheckTransaction = true,
241       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
242   ALWAYS_INLINE void SetFieldObject(MemberOffset field_offset, Object* new_value)
243       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
244 
245   template<bool kTransactionActive, bool kCheckTransaction = true,
246       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
247   ALWAYS_INLINE void SetFieldObjectVolatile(MemberOffset field_offset, Object* new_value)
248       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
249 
250   template<bool kTransactionActive, bool kCheckTransaction = true,
251       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
252   bool CasFieldWeakSequentiallyConsistentObject(MemberOffset field_offset, Object* old_value,
253                                                 Object* new_value)
254       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
255   template<bool kTransactionActive, bool kCheckTransaction = true,
256       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
257   bool CasFieldWeakSequentiallyConsistentObjectWithoutWriteBarrier(MemberOffset field_offset,
258                                                                    Object* old_value,
259                                                                    Object* new_value)
260       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
261 
262   template<bool kTransactionActive, bool kCheckTransaction = true,
263       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
264   bool CasFieldStrongSequentiallyConsistentObject(MemberOffset field_offset, Object* old_value,
265                                                   Object* new_value)
266       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
267   template<bool kTransactionActive, bool kCheckTransaction = true,
268       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
269   bool CasFieldStrongSequentiallyConsistentObjectWithoutWriteBarrier(MemberOffset field_offset,
270                                                                      Object* old_value,
271                                                                      Object* new_value)
272       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
273 
274   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
275   HeapReference<Object>* GetFieldObjectReferenceAddr(MemberOffset field_offset);
276 
277   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
278   ALWAYS_INLINE uint8_t GetFieldBoolean(MemberOffset field_offset)
279       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
280 
281   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
282   ALWAYS_INLINE int8_t GetFieldByte(MemberOffset field_offset)
283       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
284 
285   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
286   ALWAYS_INLINE uint8_t GetFieldBooleanVolatile(MemberOffset field_offset)
287       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
288 
289   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
290   ALWAYS_INLINE int8_t GetFieldByteVolatile(MemberOffset field_offset)
291       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
292 
293   template<bool kTransactionActive, bool kCheckTransaction = true,
294       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
295   ALWAYS_INLINE void SetFieldBoolean(MemberOffset field_offset, uint8_t new_value)
296       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
297 
298   template<bool kTransactionActive, bool kCheckTransaction = true,
299       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
300   ALWAYS_INLINE void SetFieldByte(MemberOffset field_offset, int8_t new_value)
301       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
302 
303   template<bool kTransactionActive, bool kCheckTransaction = true,
304       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
305   ALWAYS_INLINE void SetFieldBooleanVolatile(MemberOffset field_offset, uint8_t new_value)
306       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
307 
308   template<bool kTransactionActive, bool kCheckTransaction = true,
309       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
310   ALWAYS_INLINE void SetFieldByteVolatile(MemberOffset field_offset, int8_t new_value)
311       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
312 
313   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
314   ALWAYS_INLINE uint16_t GetFieldChar(MemberOffset field_offset)
315       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
316 
317   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
318   ALWAYS_INLINE int16_t GetFieldShort(MemberOffset field_offset)
319       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
320 
321   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
322   ALWAYS_INLINE uint16_t GetFieldCharVolatile(MemberOffset field_offset)
323       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
324 
325   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
326   ALWAYS_INLINE int16_t GetFieldShortVolatile(MemberOffset field_offset)
327       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
328 
329   template<bool kTransactionActive, bool kCheckTransaction = true,
330       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
331   ALWAYS_INLINE void SetFieldChar(MemberOffset field_offset, uint16_t new_value)
332       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
333 
334   template<bool kTransactionActive, bool kCheckTransaction = true,
335       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
336   ALWAYS_INLINE void SetFieldShort(MemberOffset field_offset, int16_t new_value)
337       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
338 
339   template<bool kTransactionActive, bool kCheckTransaction = true,
340       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
341   ALWAYS_INLINE void SetFieldCharVolatile(MemberOffset field_offset, uint16_t new_value)
342       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
343 
344   template<bool kTransactionActive, bool kCheckTransaction = true,
345       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
346   ALWAYS_INLINE void SetFieldShortVolatile(MemberOffset field_offset, int16_t new_value)
347       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
348 
349   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
350   ALWAYS_INLINE int32_t GetField32(MemberOffset field_offset)
351       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
352 
353   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
354   ALWAYS_INLINE int32_t GetField32Volatile(MemberOffset field_offset)
355       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
356 
357   template<bool kTransactionActive, bool kCheckTransaction = true,
358       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
359   ALWAYS_INLINE void SetField32(MemberOffset field_offset, int32_t new_value)
360       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
361 
362   template<bool kTransactionActive, bool kCheckTransaction = true,
363       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
364   ALWAYS_INLINE void SetField32Volatile(MemberOffset field_offset, int32_t new_value)
365       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
366 
367   template<bool kTransactionActive, bool kCheckTransaction = true,
368       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
369   ALWAYS_INLINE bool CasFieldWeakSequentiallyConsistent32(MemberOffset field_offset,
370                                                           int32_t old_value, int32_t new_value)
371       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
372 
373   template<bool kTransactionActive, bool kCheckTransaction = true,
374       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
375   bool CasFieldWeakRelaxed32(MemberOffset field_offset, int32_t old_value,
376                              int32_t new_value) ALWAYS_INLINE
377       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
378 
379   template<bool kTransactionActive, bool kCheckTransaction = true,
380       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
381   bool CasFieldStrongSequentiallyConsistent32(MemberOffset field_offset, int32_t old_value,
382                                               int32_t new_value) ALWAYS_INLINE
383       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
384 
385   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
386   ALWAYS_INLINE int64_t GetField64(MemberOffset field_offset)
387       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
388 
389   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
390   ALWAYS_INLINE int64_t GetField64Volatile(MemberOffset field_offset)
391       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
392 
393   template<bool kTransactionActive, bool kCheckTransaction = true,
394       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
395   ALWAYS_INLINE void SetField64(MemberOffset field_offset, int64_t new_value)
396       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
397 
398   template<bool kTransactionActive, bool kCheckTransaction = true,
399       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
400   ALWAYS_INLINE void SetField64Volatile(MemberOffset field_offset, int64_t new_value)
401       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
402 
403   template<bool kTransactionActive, bool kCheckTransaction = true,
404       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
405   bool CasFieldWeakSequentiallyConsistent64(MemberOffset field_offset, int64_t old_value,
406                                             int64_t new_value)
407       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
408 
409   template<bool kTransactionActive, bool kCheckTransaction = true,
410       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
411   bool CasFieldStrongSequentiallyConsistent64(MemberOffset field_offset, int64_t old_value,
412                                               int64_t new_value)
413       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
414 
415   template<bool kTransactionActive, bool kCheckTransaction = true,
416       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, typename T>
SetFieldPtr(MemberOffset field_offset,T new_value)417   void SetFieldPtr(MemberOffset field_offset, T new_value)
418       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
419     SetFieldPtrWithSize<kTransactionActive, kCheckTransaction, kVerifyFlags>(
420         field_offset, new_value, sizeof(void*));
421   }
422 
423   template<bool kTransactionActive, bool kCheckTransaction = true,
424       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, typename T>
SetFieldPtrWithSize(MemberOffset field_offset,T new_value,size_t pointer_size)425   ALWAYS_INLINE void SetFieldPtrWithSize(MemberOffset field_offset, T new_value,
426                                          size_t pointer_size)
427       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
428     DCHECK(pointer_size == 4 || pointer_size == 8) << pointer_size;
429     if (pointer_size == 4) {
430       intptr_t ptr  = reinterpret_cast<intptr_t>(new_value);
431       DCHECK_EQ(static_cast<int32_t>(ptr), ptr);  // Check that we dont lose any non 0 bits.
432       SetField32<kTransactionActive, kCheckTransaction, kVerifyFlags>(
433           field_offset, static_cast<int32_t>(ptr));
434     } else {
435       SetField64<kTransactionActive, kCheckTransaction, kVerifyFlags>(
436           field_offset, static_cast<int64_t>(reinterpret_cast<uintptr_t>(new_value)));
437     }
438   }
439   // TODO fix thread safety analysis broken by the use of template. This should be
440   // SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
441   template <const bool kVisitClass, VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
442       typename Visitor, typename JavaLangRefVisitor = VoidFunctor>
443   void VisitReferences(const Visitor& visitor, const JavaLangRefVisitor& ref_visitor)
444       NO_THREAD_SAFETY_ANALYSIS;
445 
446   ArtField* FindFieldByOffset(MemberOffset offset) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
447 
448   // Used by object_test.
449   static void SetHashCodeSeed(uint32_t new_seed);
450   // Generate an identity hash code. Public for object test.
451   static uint32_t GenerateIdentityHashCode();
452 
453  protected:
454   // Accessors for non-Java type fields
455   template<class T, VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
GetFieldPtr(MemberOffset field_offset)456   T GetFieldPtr(MemberOffset field_offset)
457       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
458     return GetFieldPtrWithSize<T, kVerifyFlags, kIsVolatile>(field_offset, sizeof(void*));
459   }
460 
461   template<class T, VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags, bool kIsVolatile = false>
GetFieldPtrWithSize(MemberOffset field_offset,size_t pointer_size)462   ALWAYS_INLINE T GetFieldPtrWithSize(MemberOffset field_offset, size_t pointer_size)
463       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
464     DCHECK(pointer_size == 4 || pointer_size == 8) << pointer_size;
465     if (pointer_size == 4) {
466       return reinterpret_cast<T>(GetField32<kVerifyFlags, kIsVolatile>(field_offset));
467     } else {
468       int64_t v = GetField64<kVerifyFlags, kIsVolatile>(field_offset);
469       // Check that we dont lose any non 0 bits.
470       DCHECK_EQ(static_cast<int64_t>(static_cast<uintptr_t>(v)), v);
471       return reinterpret_cast<T>(static_cast<uintptr_t>(v));
472     }
473   }
474 
475   // TODO: Fixme when anotatalysis works with visitors.
476   template<bool kVisitClass, bool kIsStatic, typename Visitor>
477   void VisitFieldsReferences(uint32_t ref_offsets, const Visitor& visitor) HOT_ATTR
478       NO_THREAD_SAFETY_ANALYSIS;
479   template<bool kVisitClass, typename Visitor>
480   void VisitInstanceFieldsReferences(mirror::Class* klass, const Visitor& visitor) HOT_ATTR
481       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
482   template<bool kVisitClass, typename Visitor>
483   void VisitStaticFieldsReferences(mirror::Class* klass, const Visitor& visitor) HOT_ATTR
484       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
485 
486  private:
487   template<typename kSize, bool kIsVolatile>
488   ALWAYS_INLINE void SetField(MemberOffset field_offset, kSize new_value)
489       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
490   template<typename kSize, bool kIsVolatile>
491   ALWAYS_INLINE kSize GetField(MemberOffset field_offset)
492       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
493 
494   // Verify the type correctness of stores to fields.
495   // TODO: This can cause thread suspension and isn't moving GC safe.
496   void CheckFieldAssignmentImpl(MemberOffset field_offset, Object* new_value)
497       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
CheckFieldAssignment(MemberOffset field_offset,Object * new_value)498   void CheckFieldAssignment(MemberOffset field_offset, Object* new_value)
499       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
500     if (kCheckFieldAssignments) {
501       CheckFieldAssignmentImpl(field_offset, new_value);
502     }
503   }
504 
505   // A utility function that copies an object in a read barrier and
506   // write barrier-aware way. This is internally used by Clone() and
507   // Class::CopyOf().
508   static Object* CopyObject(Thread* self, mirror::Object* dest, mirror::Object* src,
509                             size_t num_bytes)
510       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
511 
512   static Atomic<uint32_t> hash_code_seed;
513 
514   // The Class representing the type of the object.
515   HeapReference<Class> klass_;
516   // Monitor and hash code information.
517   uint32_t monitor_;
518 
519 #ifdef USE_BROOKS_READ_BARRIER
520   // Note names use a 'x' prefix and the x_rb_ptr_ is of type int
521   // instead of Object to go with the alphabetical/by-type field order
522   // on the Java side.
523   uint32_t x_rb_ptr_;      // For the Brooks pointer.
524   uint32_t x_xpadding_;    // For 8-byte alignment. TODO: get rid of this.
525 #endif
526 
527   friend class art::ImageWriter;
528   friend class art::Monitor;
529   friend struct art::ObjectOffsets;  // for verifying offset information
530   friend class CopyObjectVisitor;  // for CopyObject().
531   friend class CopyClassVisitor;   // for CopyObject().
532   DISALLOW_ALLOCATION();
533   DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
534 };
535 
536 }  // namespace mirror
537 }  // namespace art
538 
539 #endif  // ART_RUNTIME_MIRROR_OBJECT_H_
540