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