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