blob: 9b38576fc51f85c6e20944432a81fd5fd46cc3be [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
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
Mathieu Chartierad2541a2013-10-25 10:05:23 -070017#include <ctime>
18
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "object.h"
20
21#include "array-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070022#include "art_field-inl.h"
23#include "art_field.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "class.h"
Ian Rogers04d7aa92013-03-16 14:29:17 -070026#include "class_linker-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080027#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080028#include "dex/dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/accounting/card_table-inl.h"
Andreas Gampe88dbad32018-06-26 19:54:12 -070030#include "gc/heap-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "handle_scope-inl.h"
Ian Rogers04d7aa92013-03-16 14:29:17 -070032#include "iftable-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "monitor.h"
34#include "object-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080035#include "object-refvisitor-inl.h"
Ian Rogers04d7aa92013-03-16 14:29:17 -070036#include "object_array-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "throwable.h"
39#include "well_known_classes.h"
40
41namespace art {
42namespace mirror {
43
tony.ys_liu7380c312015-01-16 19:16:45 +080044Atomic<uint32_t> Object::hash_code_seed(987654321U + std::time(nullptr));
45
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070046class CopyReferenceFieldsWithReadBarrierVisitor {
47 public:
Mathieu Chartier9d156d52016-10-06 17:44:26 -070048 explicit CopyReferenceFieldsWithReadBarrierVisitor(ObjPtr<Object> dest_obj)
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070049 : dest_obj_(dest_obj) {}
50
Mathieu Chartier9d156d52016-10-06 17:44:26 -070051 void operator()(ObjPtr<Object> obj, MemberOffset offset, bool /* is_static */) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070052 ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070053 // GetFieldObject() contains a RB.
Mathieu Chartier31e88222016-10-14 18:43:19 -070054 ObjPtr<Object> ref = obj->GetFieldObject<Object>(offset);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070055 // No WB here as a large object space does not have a card table
56 // coverage. Instead, cards will be marked separately.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070057 dest_obj_->SetFieldObjectWithoutWriteBarrier<false, false>(offset, ref);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070058 }
59
Mathieu Chartier9d156d52016-10-06 17:44:26 -070060 void operator()(ObjPtr<mirror::Class> klass, mirror::Reference* ref) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070061 ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070062 // Copy java.lang.ref.Reference.referent which isn't visited in
63 // Object::VisitReferences().
Fred Shih4ee7a662014-07-11 09:59:27 -070064 DCHECK(klass->IsTypeOfReferenceClass());
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070065 this->operator()(ref, mirror::Reference::ReferentOffset(), false);
66 }
67
Mathieu Chartierda7c6502015-07-23 16:01:26 -070068 // Unused since we don't copy class native roots.
69 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
70 const {}
71 void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
72
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070073 private:
Mathieu Chartier9d156d52016-10-06 17:44:26 -070074 ObjPtr<Object> const dest_obj_;
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070075};
76
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -070077Object* Object::CopyObject(ObjPtr<mirror::Object> dest,
Mathieu Chartier9d156d52016-10-06 17:44:26 -070078 ObjPtr<mirror::Object> src,
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -070079 size_t num_bytes) {
Mathieu Chartier03536982016-10-07 10:51:53 -070080 // Copy instance data. Don't assume memcpy copies by words (b/32012820).
81 {
82 const size_t offset = sizeof(Object);
Mathieu Chartier9d156d52016-10-06 17:44:26 -070083 uint8_t* src_bytes = reinterpret_cast<uint8_t*>(src.Ptr()) + offset;
84 uint8_t* dst_bytes = reinterpret_cast<uint8_t*>(dest.Ptr()) + offset;
Mathieu Chartier03536982016-10-07 10:51:53 -070085 num_bytes -= offset;
86 DCHECK_ALIGNED(src_bytes, sizeof(uintptr_t));
87 DCHECK_ALIGNED(dst_bytes, sizeof(uintptr_t));
88 // Use word sized copies to begin.
89 while (num_bytes >= sizeof(uintptr_t)) {
Orion Hodson88591fe2018-03-06 13:35:43 +000090 reinterpret_cast<Atomic<uintptr_t>*>(dst_bytes)->store(
91 reinterpret_cast<Atomic<uintptr_t>*>(src_bytes)->load(std::memory_order_relaxed),
92 std::memory_order_relaxed);
Mathieu Chartier03536982016-10-07 10:51:53 -070093 src_bytes += sizeof(uintptr_t);
94 dst_bytes += sizeof(uintptr_t);
95 num_bytes -= sizeof(uintptr_t);
96 }
97 // Copy possible 32 bit word.
98 if (sizeof(uintptr_t) != sizeof(uint32_t) && num_bytes >= sizeof(uint32_t)) {
Orion Hodson88591fe2018-03-06 13:35:43 +000099 reinterpret_cast<Atomic<uint32_t>*>(dst_bytes)->store(
100 reinterpret_cast<Atomic<uint32_t>*>(src_bytes)->load(std::memory_order_relaxed),
101 std::memory_order_relaxed);
Mathieu Chartier03536982016-10-07 10:51:53 -0700102 src_bytes += sizeof(uint32_t);
103 dst_bytes += sizeof(uint32_t);
104 num_bytes -= sizeof(uint32_t);
105 }
106 // Copy remaining bytes, avoid going past the end of num_bytes since there may be a redzone
107 // there.
108 while (num_bytes > 0) {
Orion Hodson88591fe2018-03-06 13:35:43 +0000109 reinterpret_cast<Atomic<uint8_t>*>(dst_bytes)->store(
110 reinterpret_cast<Atomic<uint8_t>*>(src_bytes)->load(std::memory_order_relaxed),
111 std::memory_order_relaxed);
Mathieu Chartier03536982016-10-07 10:51:53 -0700112 src_bytes += sizeof(uint8_t);
113 dst_bytes += sizeof(uint8_t);
114 num_bytes -= sizeof(uint8_t);
115 }
116 }
117
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800118 if (kUseReadBarrier) {
Mathieu Chartier03536982016-10-07 10:51:53 -0700119 // We need a RB here. After copying the whole object above, copy references fields one by one
120 // again with a RB to make sure there are no from space refs. TODO: Optimize this later?
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700121 CopyReferenceFieldsWithReadBarrierVisitor visitor(dest);
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700122 src->VisitReferences(visitor, visitor);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700123 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124 // Perform write barriers on copied object references.
Mathieu Chartier31e88222016-10-14 18:43:19 -0700125 ObjPtr<Class> c = src->GetClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 if (c->IsArrayClass()) {
127 if (!c->GetComponentType()->IsPrimitive()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800128 ObjectArray<Object>* array = dest->AsObjectArray<Object>();
Mathieu Chartier88ea61e2018-06-20 17:45:41 -0700129 WriteBarrier::ForArrayWrite(dest, 0, array->GetLength());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130 }
131 } else {
Mathieu Chartier88ea61e2018-06-20 17:45:41 -0700132 WriteBarrier::ForEveryFieldWrite(dest);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700134 return dest.Ptr();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700135}
136
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700137// An allocation pre-fence visitor that copies the object.
138class CopyObjectVisitor {
139 public:
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -0700140 CopyObjectVisitor(Handle<Object>* orig, size_t num_bytes)
141 : orig_(orig), num_bytes_(num_bytes) {}
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700142
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700143 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700144 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -0700145 Object::CopyObject(obj, orig_->Get(), num_bytes_);
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700146 }
147
148 private:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700149 Handle<Object>* const orig_;
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700150 const size_t num_bytes_;
151 DISALLOW_COPY_AND_ASSIGN(CopyObjectVisitor);
152};
153
Mathieu Chartier590fee92013-09-13 13:46:47 -0700154Object* Object::Clone(Thread* self) {
155 CHECK(!IsClass()) << "Can't clone classes.";
156 // Object::SizeOf gets the right size even if we're an array. Using c->AllocObject() here would
157 // be wrong.
158 gc::Heap* heap = Runtime::Current()->GetHeap();
159 size_t num_bytes = SizeOf();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700160 StackHandleScope<1> hs(self);
161 Handle<Object> this_object(hs.NewHandle(this));
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700162 ObjPtr<Object> copy;
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -0700163 CopyObjectVisitor visitor(&this_object, num_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700164 if (heap->IsMovableObject(this)) {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700165 copy = heap->AllocObject<true>(self, GetClass(), num_bytes, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700166 } else {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700167 copy = heap->AllocNonMovableObject<true>(self, GetClass(), num_bytes, visitor);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168 }
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -0700169 if (this_object->GetClass()->IsFinalizable()) {
170 heap->AddFinalizerReference(self, &copy);
171 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700172 return copy.Ptr();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173}
174
Ian Rogersbbcd30b2014-10-30 15:25:36 -0700175uint32_t Object::GenerateIdentityHashCode() {
Ian Rogersbbcd30b2014-10-30 15:25:36 -0700176 uint32_t expected_value, new_value;
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700177 do {
Orion Hodson88591fe2018-03-06 13:35:43 +0000178 expected_value = hash_code_seed.load(std::memory_order_relaxed);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700179 new_value = expected_value * 1103515245 + 12345;
Orion Hodson4557b382018-01-03 11:47:54 +0000180 } while (!hash_code_seed.CompareAndSetWeakRelaxed(expected_value, new_value) ||
tony.ys_liu7380c312015-01-16 19:16:45 +0800181 (expected_value & LockWord::kHashMask) == 0);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700182 return expected_value & LockWord::kHashMask;
183}
184
tony.ys_liu7380c312015-01-16 19:16:45 +0800185void Object::SetHashCodeSeed(uint32_t new_seed) {
Orion Hodson88591fe2018-03-06 13:35:43 +0000186 hash_code_seed.store(new_seed, std::memory_order_relaxed);
tony.ys_liu7380c312015-01-16 19:16:45 +0800187}
188
Mathieu Chartier31e88222016-10-14 18:43:19 -0700189int32_t Object::IdentityHashCode() {
190 ObjPtr<Object> current_this = this; // The this pointer may get invalidated by thread suspension.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700191 while (true) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700192 LockWord lw = current_this->GetLockWord(false);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700193 switch (lw.GetState()) {
194 case LockWord::kUnlocked: {
195 // Try to compare and swap in a new hash, if we succeed we will return the hash on the next
196 // loop iteration.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700197 LockWord hash_word = LockWord::FromHashCode(GenerateIdentityHashCode(), lw.GCState());
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700198 DCHECK_EQ(hash_word.GetState(), LockWord::kHashCode);
Mathieu Chartier8bb3c682018-06-18 12:53:10 -0700199 // Use a strong CAS to prevent spurious failures since these can make the boot image
200 // non-deterministic.
Mathieu Chartier42c2e502018-06-19 12:30:56 -0700201 if (current_this->CasLockWord(lw, hash_word, CASMode::kStrong, std::memory_order_relaxed)) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700202 return hash_word.GetHashCode();
203 }
204 break;
205 }
206 case LockWord::kThinLocked: {
Ian Rogers43c69cc2014-08-15 11:09:28 -0700207 // Inflate the thin lock to a monitor and stick the hash code inside of the monitor. May
208 // fail spuriously.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700209 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700210 StackHandleScope<1> hs(self);
211 Handle<mirror::Object> h_this(hs.NewHandle(current_this));
212 Monitor::InflateThinLocked(self, h_this, lw, GenerateIdentityHashCode());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700213 // A GC may have occurred when we switched to kBlocked.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700214 current_this = h_this.Get();
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700215 break;
216 }
217 case LockWord::kFatLocked: {
Roland Levillain51209232016-03-11 16:27:27 +0000218 // Already inflated, return the hash stored in the monitor.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700219 Monitor* monitor = lw.FatLockMonitor();
220 DCHECK(monitor != nullptr);
221 return monitor->GetHashCode();
222 }
223 case LockWord::kHashCode: {
224 return lw.GetHashCode();
225 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700226 default: {
227 LOG(FATAL) << "Invalid state during hashcode " << lw.GetState();
228 break;
229 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700230 }
231 }
Ian Rogers07140832014-09-30 15:43:59 -0700232 UNREACHABLE();
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700233}
234
Mathieu Chartiera058fdf2016-10-06 15:13:58 -0700235void Object::CheckFieldAssignmentImpl(MemberOffset field_offset, ObjPtr<Object> new_value) {
Mathieu Chartier31e88222016-10-14 18:43:19 -0700236 ObjPtr<Class> c = GetClass();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800237 Runtime* runtime = Runtime::Current();
238 if (runtime->GetClassLinker() == nullptr || !runtime->IsStarted() ||
239 !runtime->GetHeap()->IsObjectValidationEnabled() || !c->IsResolved()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 return;
241 }
Mathieu Chartier31e88222016-10-14 18:43:19 -0700242 for (ObjPtr<Class> cur = c; cur != nullptr; cur = cur->GetSuperClass()) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700243 for (ArtField& field : cur->GetIFields()) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700244 if (field.GetOffset().Int32Value() == field_offset.Int32Value()) {
245 CHECK_NE(field.GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700246 // TODO: resolve the field type for moving GC.
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000247 ObjPtr<mirror::Class> field_type =
Vladimir Marko208f6702017-12-08 12:00:50 +0000248 kMovingCollector ? field.LookupResolvedType() : field.ResolveType();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700249 if (field_type != nullptr) {
250 CHECK(field_type->IsAssignableFrom(new_value->GetClass()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800251 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700252 return;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800253 }
254 }
255 }
256 if (c->IsArrayClass()) {
257 // Bounds and assign-ability done in the array setter.
258 return;
259 }
260 if (IsClass()) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700261 for (ArtField& field : AsClass()->GetSFields()) {
262 if (field.GetOffset().Int32Value() == field_offset.Int32Value()) {
263 CHECK_NE(field.GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700264 // TODO: resolve the field type for moving GC.
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000265 ObjPtr<mirror::Class> field_type =
Vladimir Marko208f6702017-12-08 12:00:50 +0000266 kMovingCollector ? field.LookupResolvedType() : field.ResolveType();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700267 if (field_type != nullptr) {
268 CHECK(field_type->IsAssignableFrom(new_value->GetClass()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800269 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700270 return;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800271 }
272 }
273 }
274 LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this)
Roland Levillaind32ead22018-05-30 17:38:21 +0100275 << " of type " << c->PrettyDescriptor() << " at offset " << field_offset;
Ian Rogers08f1f502014-12-02 15:04:37 -0800276 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800277}
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800278
Hiroshi Yamauchieb2baaf2015-05-13 21:14:22 -0700279ArtField* Object::FindFieldByOffset(MemberOffset offset) {
280 return IsClass() ? ArtField::FindStaticFieldWithOffset(AsClass(), offset.Uint32Value())
281 : ArtField::FindInstanceFieldWithOffset(GetClass(), offset.Uint32Value());
282}
283
David Sehr709b0702016-10-13 09:12:37 -0700284std::string Object::PrettyTypeOf(ObjPtr<mirror::Object> obj) {
Mathieu Chartier99111282018-06-19 12:30:56 -0700285 return (obj == nullptr) ? "null" : obj->PrettyTypeOf();
David Sehr709b0702016-10-13 09:12:37 -0700286}
287
288std::string Object::PrettyTypeOf() {
Mathieu Chartierbc632f02017-04-20 13:31:39 -0700289 // From-space version is the same as the to-space version since the dex file never changes.
290 // Avoiding the read barrier here is important to prevent recursive AssertToSpaceInvariant
291 // issues.
292 ObjPtr<mirror::Class> klass = GetClass<kDefaultVerifyFlags, kWithoutReadBarrier>();
293 if (klass == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700294 return "(raw)";
295 }
296 std::string temp;
Mathieu Chartierbc632f02017-04-20 13:31:39 -0700297 std::string result(PrettyDescriptor(klass->GetDescriptor(&temp)));
298 if (klass->IsClassClass()) {
David Sehr709b0702016-10-13 09:12:37 -0700299 result += "<" + PrettyDescriptor(AsClass()->GetDescriptor(&temp)) + ">";
300 }
301 return result;
302}
303
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800304} // namespace mirror
305} // namespace art