blob: ede1c665774d1935e0453d192563ec05fa566c69 [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
Vladimir Marko4617d582019-03-28 13:48:31 +000060 void operator()(ObjPtr<mirror::Class> klass, ObjPtr<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:
Vladimir Marko0984e482019-03-27 16:41:41 +000074 const ObjPtr<Object> dest_obj_;
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070075};
76
Vladimir Markodfc0de72019-04-01 10:57:55 +010077ObjPtr<Object> Object::CopyObject(ObjPtr<mirror::Object> dest,
78 ObjPtr<mirror::Object> src,
79 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()) {
Vladimir Marko4617d582019-03-28 13:48:31 +0000128 ObjPtr<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 }
Vladimir Markodfc0de72019-04-01 10:57:55 +0100134 return dest;
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
Vladimir Marko3068d582019-05-28 16:39:29 +0100154ObjPtr<Object> Object::Clone(Handle<Object> h_this, Thread* self) {
155 CHECK(!h_this->IsClass()) << "Can't clone classes.";
Mathieu Chartier590fee92013-09-13 13:46:47 -0700156 // 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();
Vladimir Marko3068d582019-05-28 16:39:29 +0100159 size_t num_bytes = h_this->SizeOf();
160 CopyObjectVisitor visitor(&h_this, num_bytes);
161 ObjPtr<Object> copy = heap->IsMovableObject(h_this.Get())
162 ? heap->AllocObject(self, h_this->GetClass(), num_bytes, visitor)
163 : heap->AllocNonMovableObject(self, h_this->GetClass(), num_bytes, visitor);
164 if (h_this->GetClass()->IsFinalizable()) {
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -0700165 heap->AddFinalizerReference(self, &copy);
166 }
Vladimir Markodfc0de72019-04-01 10:57:55 +0100167 return copy;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168}
169
Ian Rogersbbcd30b2014-10-30 15:25:36 -0700170uint32_t Object::GenerateIdentityHashCode() {
Ian Rogersbbcd30b2014-10-30 15:25:36 -0700171 uint32_t expected_value, new_value;
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700172 do {
Orion Hodson88591fe2018-03-06 13:35:43 +0000173 expected_value = hash_code_seed.load(std::memory_order_relaxed);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700174 new_value = expected_value * 1103515245 + 12345;
Orion Hodson4557b382018-01-03 11:47:54 +0000175 } while (!hash_code_seed.CompareAndSetWeakRelaxed(expected_value, new_value) ||
tony.ys_liu7380c312015-01-16 19:16:45 +0800176 (expected_value & LockWord::kHashMask) == 0);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700177 return expected_value & LockWord::kHashMask;
178}
179
tony.ys_liu7380c312015-01-16 19:16:45 +0800180void Object::SetHashCodeSeed(uint32_t new_seed) {
Orion Hodson88591fe2018-03-06 13:35:43 +0000181 hash_code_seed.store(new_seed, std::memory_order_relaxed);
tony.ys_liu7380c312015-01-16 19:16:45 +0800182}
183
Mathieu Chartier31e88222016-10-14 18:43:19 -0700184int32_t Object::IdentityHashCode() {
185 ObjPtr<Object> current_this = this; // The this pointer may get invalidated by thread suspension.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700186 while (true) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700187 LockWord lw = current_this->GetLockWord(false);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700188 switch (lw.GetState()) {
189 case LockWord::kUnlocked: {
190 // Try to compare and swap in a new hash, if we succeed we will return the hash on the next
191 // loop iteration.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700192 LockWord hash_word = LockWord::FromHashCode(GenerateIdentityHashCode(), lw.GCState());
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700193 DCHECK_EQ(hash_word.GetState(), LockWord::kHashCode);
Mathieu Chartier8bb3c682018-06-18 12:53:10 -0700194 // Use a strong CAS to prevent spurious failures since these can make the boot image
195 // non-deterministic.
Mathieu Chartier42c2e502018-06-19 12:30:56 -0700196 if (current_this->CasLockWord(lw, hash_word, CASMode::kStrong, std::memory_order_relaxed)) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700197 return hash_word.GetHashCode();
198 }
199 break;
200 }
201 case LockWord::kThinLocked: {
Ian Rogers43c69cc2014-08-15 11:09:28 -0700202 // Inflate the thin lock to a monitor and stick the hash code inside of the monitor. May
203 // fail spuriously.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700204 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700205 StackHandleScope<1> hs(self);
206 Handle<mirror::Object> h_this(hs.NewHandle(current_this));
207 Monitor::InflateThinLocked(self, h_this, lw, GenerateIdentityHashCode());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700208 // A GC may have occurred when we switched to kBlocked.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700209 current_this = h_this.Get();
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700210 break;
211 }
212 case LockWord::kFatLocked: {
Roland Levillain51209232016-03-11 16:27:27 +0000213 // Already inflated, return the hash stored in the monitor.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700214 Monitor* monitor = lw.FatLockMonitor();
215 DCHECK(monitor != nullptr);
216 return monitor->GetHashCode();
217 }
218 case LockWord::kHashCode: {
219 return lw.GetHashCode();
220 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700221 default: {
222 LOG(FATAL) << "Invalid state during hashcode " << lw.GetState();
Elliott Hughesc1896c92018-11-29 11:33:18 -0800223 UNREACHABLE();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700224 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700225 }
226 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700227}
228
Mathieu Chartiera058fdf2016-10-06 15:13:58 -0700229void Object::CheckFieldAssignmentImpl(MemberOffset field_offset, ObjPtr<Object> new_value) {
Mathieu Chartier31e88222016-10-14 18:43:19 -0700230 ObjPtr<Class> c = GetClass();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800231 Runtime* runtime = Runtime::Current();
232 if (runtime->GetClassLinker() == nullptr || !runtime->IsStarted() ||
233 !runtime->GetHeap()->IsObjectValidationEnabled() || !c->IsResolved()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800234 return;
235 }
Mathieu Chartier31e88222016-10-14 18:43:19 -0700236 for (ObjPtr<Class> cur = c; cur != nullptr; cur = cur->GetSuperClass()) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700237 for (ArtField& field : cur->GetIFields()) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700238 if (field.GetOffset().Int32Value() == field_offset.Int32Value()) {
239 CHECK_NE(field.GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700240 // TODO: resolve the field type for moving GC.
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000241 ObjPtr<mirror::Class> field_type =
Vladimir Marko208f6702017-12-08 12:00:50 +0000242 kMovingCollector ? field.LookupResolvedType() : field.ResolveType();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700243 if (field_type != nullptr) {
244 CHECK(field_type->IsAssignableFrom(new_value->GetClass()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800245 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700246 return;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 }
248 }
249 }
250 if (c->IsArrayClass()) {
251 // Bounds and assign-ability done in the array setter.
252 return;
253 }
254 if (IsClass()) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700255 for (ArtField& field : AsClass()->GetSFields()) {
256 if (field.GetOffset().Int32Value() == field_offset.Int32Value()) {
257 CHECK_NE(field.GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700258 // TODO: resolve the field type for moving GC.
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000259 ObjPtr<mirror::Class> field_type =
Vladimir Marko208f6702017-12-08 12:00:50 +0000260 kMovingCollector ? field.LookupResolvedType() : field.ResolveType();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700261 if (field_type != nullptr) {
262 CHECK(field_type->IsAssignableFrom(new_value->GetClass()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800263 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700264 return;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265 }
266 }
267 }
268 LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this)
Roland Levillaind32ead22018-05-30 17:38:21 +0100269 << " of type " << c->PrettyDescriptor() << " at offset " << field_offset;
Ian Rogers08f1f502014-12-02 15:04:37 -0800270 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800271}
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800272
Hiroshi Yamauchieb2baaf2015-05-13 21:14:22 -0700273ArtField* Object::FindFieldByOffset(MemberOffset offset) {
274 return IsClass() ? ArtField::FindStaticFieldWithOffset(AsClass(), offset.Uint32Value())
275 : ArtField::FindInstanceFieldWithOffset(GetClass(), offset.Uint32Value());
276}
277
David Sehr709b0702016-10-13 09:12:37 -0700278std::string Object::PrettyTypeOf(ObjPtr<mirror::Object> obj) {
Mathieu Chartier99111282018-06-19 12:30:56 -0700279 return (obj == nullptr) ? "null" : obj->PrettyTypeOf();
David Sehr709b0702016-10-13 09:12:37 -0700280}
281
282std::string Object::PrettyTypeOf() {
Mathieu Chartierbc632f02017-04-20 13:31:39 -0700283 // From-space version is the same as the to-space version since the dex file never changes.
284 // Avoiding the read barrier here is important to prevent recursive AssertToSpaceInvariant
285 // issues.
286 ObjPtr<mirror::Class> klass = GetClass<kDefaultVerifyFlags, kWithoutReadBarrier>();
287 if (klass == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700288 return "(raw)";
289 }
290 std::string temp;
Mathieu Chartierbc632f02017-04-20 13:31:39 -0700291 std::string result(PrettyDescriptor(klass->GetDescriptor(&temp)));
292 if (klass->IsClassClass()) {
David Sehr709b0702016-10-13 09:12:37 -0700293 result += "<" + PrettyDescriptor(AsClass()->GetDescriptor(&temp)) + ">";
294 }
295 return result;
296}
297
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800298} // namespace mirror
299} // namespace art