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