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