blob: 4240e702b530bc17ed3d43d3fe90d7b9ed4c2055 [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"
30#include "gc/heap.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 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700124 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125 // Perform write barriers on copied object references.
Mathieu Chartier31e88222016-10-14 18:43:19 -0700126 ObjPtr<Class> c = src->GetClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127 if (c->IsArrayClass()) {
128 if (!c->GetComponentType()->IsPrimitive()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 ObjectArray<Object>* array = dest->AsObjectArray<Object>();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700130 heap->WriteBarrierArray(dest, 0, array->GetLength());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 }
132 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700133 heap->WriteBarrierEveryFieldOf(dest);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700135 return dest.Ptr();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700136}
137
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700138// An allocation pre-fence visitor that copies the object.
139class CopyObjectVisitor {
140 public:
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -0700141 CopyObjectVisitor(Handle<Object>* orig, size_t num_bytes)
142 : orig_(orig), num_bytes_(num_bytes) {}
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700143
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700144 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700145 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -0700146 Object::CopyObject(obj, orig_->Get(), num_bytes_);
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700147 }
148
149 private:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700150 Handle<Object>* const orig_;
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700151 const size_t num_bytes_;
152 DISALLOW_COPY_AND_ASSIGN(CopyObjectVisitor);
153};
154
Mathieu Chartier590fee92013-09-13 13:46:47 -0700155Object* Object::Clone(Thread* self) {
156 CHECK(!IsClass()) << "Can't clone classes.";
157 // Object::SizeOf gets the right size even if we're an array. Using c->AllocObject() here would
158 // be wrong.
159 gc::Heap* heap = Runtime::Current()->GetHeap();
160 size_t num_bytes = SizeOf();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700161 StackHandleScope<1> hs(self);
162 Handle<Object> this_object(hs.NewHandle(this));
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700163 ObjPtr<Object> copy;
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -0700164 CopyObjectVisitor visitor(&this_object, num_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700165 if (heap->IsMovableObject(this)) {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700166 copy = heap->AllocObject<true>(self, GetClass(), num_bytes, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700167 } else {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700168 copy = heap->AllocNonMovableObject<true>(self, GetClass(), num_bytes, visitor);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 }
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -0700170 if (this_object->GetClass()->IsFinalizable()) {
171 heap->AddFinalizerReference(self, &copy);
172 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700173 return copy.Ptr();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174}
175
Ian Rogersbbcd30b2014-10-30 15:25:36 -0700176uint32_t Object::GenerateIdentityHashCode() {
Ian Rogersbbcd30b2014-10-30 15:25:36 -0700177 uint32_t expected_value, new_value;
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700178 do {
Orion Hodson88591fe2018-03-06 13:35:43 +0000179 expected_value = hash_code_seed.load(std::memory_order_relaxed);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700180 new_value = expected_value * 1103515245 + 12345;
Orion Hodson4557b382018-01-03 11:47:54 +0000181 } while (!hash_code_seed.CompareAndSetWeakRelaxed(expected_value, new_value) ||
tony.ys_liu7380c312015-01-16 19:16:45 +0800182 (expected_value & LockWord::kHashMask) == 0);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700183 return expected_value & LockWord::kHashMask;
184}
185
tony.ys_liu7380c312015-01-16 19:16:45 +0800186void Object::SetHashCodeSeed(uint32_t new_seed) {
Orion Hodson88591fe2018-03-06 13:35:43 +0000187 hash_code_seed.store(new_seed, std::memory_order_relaxed);
tony.ys_liu7380c312015-01-16 19:16:45 +0800188}
189
Mathieu Chartier31e88222016-10-14 18:43:19 -0700190int32_t Object::IdentityHashCode() {
191 ObjPtr<Object> current_this = this; // The this pointer may get invalidated by thread suspension.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700192 while (true) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700193 LockWord lw = current_this->GetLockWord(false);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700194 switch (lw.GetState()) {
195 case LockWord::kUnlocked: {
196 // Try to compare and swap in a new hash, if we succeed we will return the hash on the next
197 // loop iteration.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700198 LockWord hash_word = LockWord::FromHashCode(GenerateIdentityHashCode(), lw.GCState());
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700199 DCHECK_EQ(hash_word.GetState(), LockWord::kHashCode);
Mathieu Chartier31e88222016-10-14 18:43:19 -0700200 if (current_this->CasLockWordWeakRelaxed(lw, hash_word)) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700201 return hash_word.GetHashCode();
202 }
203 break;
204 }
205 case LockWord::kThinLocked: {
Ian Rogers43c69cc2014-08-15 11:09:28 -0700206 // Inflate the thin lock to a monitor and stick the hash code inside of the monitor. May
207 // fail spuriously.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700208 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700209 StackHandleScope<1> hs(self);
210 Handle<mirror::Object> h_this(hs.NewHandle(current_this));
211 Monitor::InflateThinLocked(self, h_this, lw, GenerateIdentityHashCode());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700212 // A GC may have occurred when we switched to kBlocked.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700213 current_this = h_this.Get();
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700214 break;
215 }
216 case LockWord::kFatLocked: {
Roland Levillain51209232016-03-11 16:27:27 +0000217 // Already inflated, return the hash stored in the monitor.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700218 Monitor* monitor = lw.FatLockMonitor();
219 DCHECK(monitor != nullptr);
220 return monitor->GetHashCode();
221 }
222 case LockWord::kHashCode: {
223 return lw.GetHashCode();
224 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700225 default: {
226 LOG(FATAL) << "Invalid state during hashcode " << lw.GetState();
227 break;
228 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700229 }
230 }
Ian Rogers07140832014-09-30 15:43:59 -0700231 UNREACHABLE();
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700232}
233
Mathieu Chartiera058fdf2016-10-06 15:13:58 -0700234void Object::CheckFieldAssignmentImpl(MemberOffset field_offset, ObjPtr<Object> new_value) {
Mathieu Chartier31e88222016-10-14 18:43:19 -0700235 ObjPtr<Class> c = GetClass();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800236 Runtime* runtime = Runtime::Current();
237 if (runtime->GetClassLinker() == nullptr || !runtime->IsStarted() ||
238 !runtime->GetHeap()->IsObjectValidationEnabled() || !c->IsResolved()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239 return;
240 }
Mathieu Chartier31e88222016-10-14 18:43:19 -0700241 for (ObjPtr<Class> cur = c; cur != nullptr; cur = cur->GetSuperClass()) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700242 for (ArtField& field : cur->GetIFields()) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700243 if (field.GetOffset().Int32Value() == field_offset.Int32Value()) {
244 CHECK_NE(field.GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700245 // TODO: resolve the field type for moving GC.
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000246 ObjPtr<mirror::Class> field_type =
Vladimir Marko208f6702017-12-08 12:00:50 +0000247 kMovingCollector ? field.LookupResolvedType() : field.ResolveType();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700248 if (field_type != nullptr) {
249 CHECK(field_type->IsAssignableFrom(new_value->GetClass()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800250 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700251 return;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 }
253 }
254 }
255 if (c->IsArrayClass()) {
256 // Bounds and assign-ability done in the array setter.
257 return;
258 }
259 if (IsClass()) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700260 for (ArtField& field : AsClass()->GetSFields()) {
261 if (field.GetOffset().Int32Value() == field_offset.Int32Value()) {
262 CHECK_NE(field.GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700263 // TODO: resolve the field type for moving GC.
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000264 ObjPtr<mirror::Class> field_type =
Vladimir Marko208f6702017-12-08 12:00:50 +0000265 kMovingCollector ? field.LookupResolvedType() : field.ResolveType();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700266 if (field_type != nullptr) {
267 CHECK(field_type->IsAssignableFrom(new_value->GetClass()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800268 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700269 return;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800270 }
271 }
272 }
273 LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this)
Roland Levillaind32ead22018-05-30 17:38:21 +0100274 << " of type " << c->PrettyDescriptor() << " at offset " << field_offset;
Ian Rogers08f1f502014-12-02 15:04:37 -0800275 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800276}
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800277
Hiroshi Yamauchieb2baaf2015-05-13 21:14:22 -0700278ArtField* Object::FindFieldByOffset(MemberOffset offset) {
279 return IsClass() ? ArtField::FindStaticFieldWithOffset(AsClass(), offset.Uint32Value())
280 : ArtField::FindInstanceFieldWithOffset(GetClass(), offset.Uint32Value());
281}
282
David Sehr709b0702016-10-13 09:12:37 -0700283std::string Object::PrettyTypeOf(ObjPtr<mirror::Object> obj) {
284 if (obj == nullptr) {
285 return "null";
286 }
287 return obj->PrettyTypeOf();
288}
289
290std::string Object::PrettyTypeOf() {
Mathieu Chartierbc632f02017-04-20 13:31:39 -0700291 // From-space version is the same as the to-space version since the dex file never changes.
292 // Avoiding the read barrier here is important to prevent recursive AssertToSpaceInvariant
293 // issues.
294 ObjPtr<mirror::Class> klass = GetClass<kDefaultVerifyFlags, kWithoutReadBarrier>();
295 if (klass == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700296 return "(raw)";
297 }
298 std::string temp;
Mathieu Chartierbc632f02017-04-20 13:31:39 -0700299 std::string result(PrettyDescriptor(klass->GetDescriptor(&temp)));
300 if (klass->IsClassClass()) {
David Sehr709b0702016-10-13 09:12:37 -0700301 result += "<" + PrettyDescriptor(AsClass()->GetDescriptor(&temp)) + ">";
302 }
303 return result;
304}
305
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306} // namespace mirror
307} // namespace art