blob: 3765d0af59ca08a33def1f17cd6c0f5a7b522d22 [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 Sehr9e734c72018-01-04 17:56:19 -080027#include "dex/dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
29#include "gc/heap.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070030#include "handle_scope-inl.h"
Ian Rogers04d7aa92013-03-16 14:29:17 -070031#include "iftable-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "monitor.h"
33#include "object-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080034#include "object-refvisitor-inl.h"
Ian Rogers04d7aa92013-03-16 14:29:17 -070035#include "object_array-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "throwable.h"
38#include "well_known_classes.h"
39
40namespace art {
41namespace mirror {
42
tony.ys_liu7380c312015-01-16 19:16:45 +080043Atomic<uint32_t> Object::hash_code_seed(987654321U + std::time(nullptr));
44
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070045class CopyReferenceFieldsWithReadBarrierVisitor {
46 public:
Mathieu Chartier9d156d52016-10-06 17:44:26 -070047 explicit CopyReferenceFieldsWithReadBarrierVisitor(ObjPtr<Object> dest_obj)
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070048 : dest_obj_(dest_obj) {}
49
Mathieu Chartier9d156d52016-10-06 17:44:26 -070050 void operator()(ObjPtr<Object> obj, MemberOffset offset, bool /* is_static */) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070051 ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070052 // GetFieldObject() contains a RB.
Mathieu Chartier31e88222016-10-14 18:43:19 -070053 ObjPtr<Object> ref = obj->GetFieldObject<Object>(offset);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070054 // No WB here as a large object space does not have a card table
55 // coverage. Instead, cards will be marked separately.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070056 dest_obj_->SetFieldObjectWithoutWriteBarrier<false, false>(offset, ref);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070057 }
58
Mathieu Chartier9d156d52016-10-06 17:44:26 -070059 void operator()(ObjPtr<mirror::Class> klass, mirror::Reference* ref) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070060 ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070061 // Copy java.lang.ref.Reference.referent which isn't visited in
62 // Object::VisitReferences().
Fred Shih4ee7a662014-07-11 09:59:27 -070063 DCHECK(klass->IsTypeOfReferenceClass());
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070064 this->operator()(ref, mirror::Reference::ReferentOffset(), false);
65 }
66
Mathieu Chartierda7c6502015-07-23 16:01:26 -070067 // Unused since we don't copy class native roots.
68 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
69 const {}
70 void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
71
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070072 private:
Mathieu Chartier9d156d52016-10-06 17:44:26 -070073 ObjPtr<Object> const dest_obj_;
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070074};
75
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -070076Object* Object::CopyObject(ObjPtr<mirror::Object> dest,
Mathieu Chartier9d156d52016-10-06 17:44:26 -070077 ObjPtr<mirror::Object> src,
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -070078 size_t num_bytes) {
Mathieu Chartier03536982016-10-07 10:51:53 -070079 // Copy instance data. Don't assume memcpy copies by words (b/32012820).
80 {
81 const size_t offset = sizeof(Object);
Mathieu Chartier9d156d52016-10-06 17:44:26 -070082 uint8_t* src_bytes = reinterpret_cast<uint8_t*>(src.Ptr()) + offset;
83 uint8_t* dst_bytes = reinterpret_cast<uint8_t*>(dest.Ptr()) + offset;
Mathieu Chartier03536982016-10-07 10:51:53 -070084 num_bytes -= offset;
85 DCHECK_ALIGNED(src_bytes, sizeof(uintptr_t));
86 DCHECK_ALIGNED(dst_bytes, sizeof(uintptr_t));
87 // Use word sized copies to begin.
88 while (num_bytes >= sizeof(uintptr_t)) {
Mathieu Chartierfec13d42016-10-07 12:59:33 -070089 reinterpret_cast<Atomic<uintptr_t>*>(dst_bytes)->StoreRelaxed(
90 reinterpret_cast<Atomic<uintptr_t>*>(src_bytes)->LoadRelaxed());
Mathieu Chartier03536982016-10-07 10:51:53 -070091 src_bytes += sizeof(uintptr_t);
92 dst_bytes += sizeof(uintptr_t);
93 num_bytes -= sizeof(uintptr_t);
94 }
95 // Copy possible 32 bit word.
96 if (sizeof(uintptr_t) != sizeof(uint32_t) && num_bytes >= sizeof(uint32_t)) {
Mathieu Chartierfec13d42016-10-07 12:59:33 -070097 reinterpret_cast<Atomic<uint32_t>*>(dst_bytes)->StoreRelaxed(
98 reinterpret_cast<Atomic<uint32_t>*>(src_bytes)->LoadRelaxed());
Mathieu Chartier03536982016-10-07 10:51:53 -070099 src_bytes += sizeof(uint32_t);
100 dst_bytes += sizeof(uint32_t);
101 num_bytes -= sizeof(uint32_t);
102 }
103 // Copy remaining bytes, avoid going past the end of num_bytes since there may be a redzone
104 // there.
105 while (num_bytes > 0) {
Mathieu Chartierfec13d42016-10-07 12:59:33 -0700106 reinterpret_cast<Atomic<uint8_t>*>(dst_bytes)->StoreRelaxed(
107 reinterpret_cast<Atomic<uint8_t>*>(src_bytes)->LoadRelaxed());
Mathieu Chartier03536982016-10-07 10:51:53 -0700108 src_bytes += sizeof(uint8_t);
109 dst_bytes += sizeof(uint8_t);
110 num_bytes -= sizeof(uint8_t);
111 }
112 }
113
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800114 if (kUseReadBarrier) {
Mathieu Chartier03536982016-10-07 10:51:53 -0700115 // We need a RB here. After copying the whole object above, copy references fields one by one
116 // again with a RB to make sure there are no from space refs. TODO: Optimize this later?
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700117 CopyReferenceFieldsWithReadBarrierVisitor visitor(dest);
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700118 src->VisitReferences(visitor, visitor);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700119 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700120 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121 // Perform write barriers on copied object references.
Mathieu Chartier31e88222016-10-14 18:43:19 -0700122 ObjPtr<Class> c = src->GetClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800123 if (c->IsArrayClass()) {
124 if (!c->GetComponentType()->IsPrimitive()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800125 ObjectArray<Object>* array = dest->AsObjectArray<Object>();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700126 heap->WriteBarrierArray(dest, 0, array->GetLength());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127 }
128 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700129 heap->WriteBarrierEveryFieldOf(dest);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700131 return dest.Ptr();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700132}
133
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700134// An allocation pre-fence visitor that copies the object.
135class CopyObjectVisitor {
136 public:
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -0700137 CopyObjectVisitor(Handle<Object>* orig, size_t num_bytes)
138 : orig_(orig), num_bytes_(num_bytes) {}
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700139
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700140 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700141 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -0700142 Object::CopyObject(obj, orig_->Get(), num_bytes_);
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700143 }
144
145 private:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700146 Handle<Object>* const orig_;
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700147 const size_t num_bytes_;
148 DISALLOW_COPY_AND_ASSIGN(CopyObjectVisitor);
149};
150
Mathieu Chartier590fee92013-09-13 13:46:47 -0700151Object* Object::Clone(Thread* self) {
152 CHECK(!IsClass()) << "Can't clone classes.";
153 // Object::SizeOf gets the right size even if we're an array. Using c->AllocObject() here would
154 // be wrong.
155 gc::Heap* heap = Runtime::Current()->GetHeap();
156 size_t num_bytes = SizeOf();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700157 StackHandleScope<1> hs(self);
158 Handle<Object> this_object(hs.NewHandle(this));
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700159 ObjPtr<Object> copy;
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -0700160 CopyObjectVisitor visitor(&this_object, num_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700161 if (heap->IsMovableObject(this)) {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700162 copy = heap->AllocObject<true>(self, GetClass(), num_bytes, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700163 } else {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700164 copy = heap->AllocNonMovableObject<true>(self, GetClass(), num_bytes, visitor);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 }
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -0700166 if (this_object->GetClass()->IsFinalizable()) {
167 heap->AddFinalizerReference(self, &copy);
168 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700169 return copy.Ptr();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170}
171
Ian Rogersbbcd30b2014-10-30 15:25:36 -0700172uint32_t Object::GenerateIdentityHashCode() {
Ian Rogersbbcd30b2014-10-30 15:25:36 -0700173 uint32_t expected_value, new_value;
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700174 do {
tony.ys_liu7380c312015-01-16 19:16:45 +0800175 expected_value = hash_code_seed.LoadRelaxed();
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700176 new_value = expected_value * 1103515245 + 12345;
Orion Hodson4557b382018-01-03 11:47:54 +0000177 } while (!hash_code_seed.CompareAndSetWeakRelaxed(expected_value, new_value) ||
tony.ys_liu7380c312015-01-16 19:16:45 +0800178 (expected_value & LockWord::kHashMask) == 0);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700179 return expected_value & LockWord::kHashMask;
180}
181
tony.ys_liu7380c312015-01-16 19:16:45 +0800182void Object::SetHashCodeSeed(uint32_t new_seed) {
183 hash_code_seed.StoreRelaxed(new_seed);
184}
185
Mathieu Chartier31e88222016-10-14 18:43:19 -0700186int32_t Object::IdentityHashCode() {
187 ObjPtr<Object> current_this = this; // The this pointer may get invalidated by thread suspension.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700188 while (true) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700189 LockWord lw = current_this->GetLockWord(false);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700190 switch (lw.GetState()) {
191 case LockWord::kUnlocked: {
192 // Try to compare and swap in a new hash, if we succeed we will return the hash on the next
193 // loop iteration.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700194 LockWord hash_word = LockWord::FromHashCode(GenerateIdentityHashCode(), lw.GCState());
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700195 DCHECK_EQ(hash_word.GetState(), LockWord::kHashCode);
Mathieu Chartier31e88222016-10-14 18:43:19 -0700196 if (current_this->CasLockWordWeakRelaxed(lw, hash_word)) {
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();
223 break;
224 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700225 }
226 }
Ian Rogers07140832014-09-30 15:43:59 -0700227 UNREACHABLE();
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700228}
229
Mathieu Chartiera058fdf2016-10-06 15:13:58 -0700230void Object::CheckFieldAssignmentImpl(MemberOffset field_offset, ObjPtr<Object> new_value) {
Mathieu Chartier31e88222016-10-14 18:43:19 -0700231 ObjPtr<Class> c = GetClass();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800232 Runtime* runtime = Runtime::Current();
233 if (runtime->GetClassLinker() == nullptr || !runtime->IsStarted() ||
234 !runtime->GetHeap()->IsObjectValidationEnabled() || !c->IsResolved()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 return;
236 }
Mathieu Chartier31e88222016-10-14 18:43:19 -0700237 for (ObjPtr<Class> cur = c; cur != nullptr; cur = cur->GetSuperClass()) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700238 for (ArtField& field : cur->GetIFields()) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700239 if (field.GetOffset().Int32Value() == field_offset.Int32Value()) {
240 CHECK_NE(field.GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700241 // TODO: resolve the field type for moving GC.
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000242 ObjPtr<mirror::Class> field_type =
Vladimir Marko208f6702017-12-08 12:00:50 +0000243 kMovingCollector ? field.LookupResolvedType() : field.ResolveType();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700244 if (field_type != nullptr) {
245 CHECK(field_type->IsAssignableFrom(new_value->GetClass()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800246 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700247 return;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800248 }
249 }
250 }
251 if (c->IsArrayClass()) {
252 // Bounds and assign-ability done in the array setter.
253 return;
254 }
255 if (IsClass()) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700256 for (ArtField& field : AsClass()->GetSFields()) {
257 if (field.GetOffset().Int32Value() == field_offset.Int32Value()) {
258 CHECK_NE(field.GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700259 // TODO: resolve the field type for moving GC.
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000260 ObjPtr<mirror::Class> field_type =
Vladimir Marko208f6702017-12-08 12:00:50 +0000261 kMovingCollector ? field.LookupResolvedType() : field.ResolveType();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700262 if (field_type != nullptr) {
263 CHECK(field_type->IsAssignableFrom(new_value->GetClass()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700265 return;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 }
267 }
268 }
269 LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this)
David Sehr709b0702016-10-13 09:12:37 -0700270 << " of type " << c->PrettyDescriptor() << " at offset " << field_offset;
Ian Rogers08f1f502014-12-02 15:04:37 -0800271 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800272}
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273
Hiroshi Yamauchieb2baaf2015-05-13 21:14:22 -0700274ArtField* Object::FindFieldByOffset(MemberOffset offset) {
275 return IsClass() ? ArtField::FindStaticFieldWithOffset(AsClass(), offset.Uint32Value())
276 : ArtField::FindInstanceFieldWithOffset(GetClass(), offset.Uint32Value());
277}
278
David Sehr709b0702016-10-13 09:12:37 -0700279std::string Object::PrettyTypeOf(ObjPtr<mirror::Object> obj) {
280 if (obj == nullptr) {
281 return "null";
282 }
283 return obj->PrettyTypeOf();
284}
285
286std::string Object::PrettyTypeOf() {
Mathieu Chartierbc632f02017-04-20 13:31:39 -0700287 // From-space version is the same as the to-space version since the dex file never changes.
288 // Avoiding the read barrier here is important to prevent recursive AssertToSpaceInvariant
289 // issues.
290 ObjPtr<mirror::Class> klass = GetClass<kDefaultVerifyFlags, kWithoutReadBarrier>();
291 if (klass == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700292 return "(raw)";
293 }
294 std::string temp;
Mathieu Chartierbc632f02017-04-20 13:31:39 -0700295 std::string result(PrettyDescriptor(klass->GetDescriptor(&temp)));
296 if (klass->IsClassClass()) {
David Sehr709b0702016-10-13 09:12:37 -0700297 result += "<" + PrettyDescriptor(AsClass()->GetDescriptor(&temp)) + ">";
298 }
299 return result;
300}
301
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800302} // namespace mirror
303} // namespace art