blob: eabc29adad86359198aa3a587d282ae5c7382319 [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
Brian Carlstromea46f952013-07-30 01:26:50 -070021#include "art_field.h"
22#include "art_field-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "array-inl.h"
24#include "class.h"
25#include "class-inl.h"
Ian Rogers04d7aa92013-03-16 14:29:17 -070026#include "class_linker-inl.h"
Elliott Hughes956af0f2014-12-11 14:34:28 -080027#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
29#include "gc/heap.h"
Ian Rogers04d7aa92013-03-16 14:29:17 -070030#include "iftable-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "monitor.h"
32#include "object-inl.h"
Ian Rogers04d7aa92013-03-16 14:29:17 -070033#include "object_array-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070035#include "handle_scope-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "throwable.h"
37#include "well_known_classes.h"
38
39namespace art {
40namespace mirror {
41
tony.ys_liu7380c312015-01-16 19:16:45 +080042Atomic<uint32_t> Object::hash_code_seed(987654321U + std::time(nullptr));
43
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070044class CopyReferenceFieldsWithReadBarrierVisitor {
45 public:
Mathieu Chartier9d156d52016-10-06 17:44:26 -070046 explicit CopyReferenceFieldsWithReadBarrierVisitor(ObjPtr<Object> dest_obj)
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070047 : dest_obj_(dest_obj) {}
48
Mathieu Chartier9d156d52016-10-06 17:44:26 -070049 void operator()(ObjPtr<Object> obj, MemberOffset offset, bool /* is_static */) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070050 ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070051 // GetFieldObject() contains a RB.
Mathieu Chartier31e88222016-10-14 18:43:19 -070052 ObjPtr<Object> ref = obj->GetFieldObject<Object>(offset);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070053 // No WB here as a large object space does not have a card table
54 // coverage. Instead, cards will be marked separately.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070055 dest_obj_->SetFieldObjectWithoutWriteBarrier<false, false>(offset, ref);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070056 }
57
Mathieu Chartier9d156d52016-10-06 17:44:26 -070058 void operator()(ObjPtr<mirror::Class> klass, mirror::Reference* ref) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070059 ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070060 // Copy java.lang.ref.Reference.referent which isn't visited in
61 // Object::VisitReferences().
Fred Shih4ee7a662014-07-11 09:59:27 -070062 DCHECK(klass->IsTypeOfReferenceClass());
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070063 this->operator()(ref, mirror::Reference::ReferentOffset(), false);
64 }
65
Mathieu Chartierda7c6502015-07-23 16:01:26 -070066 // Unused since we don't copy class native roots.
67 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
68 const {}
69 void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
70
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070071 private:
Mathieu Chartier9d156d52016-10-06 17:44:26 -070072 ObjPtr<Object> const dest_obj_;
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070073};
74
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -070075Object* Object::CopyObject(ObjPtr<mirror::Object> dest,
Mathieu Chartier9d156d52016-10-06 17:44:26 -070076 ObjPtr<mirror::Object> src,
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -070077 size_t num_bytes) {
Mathieu Chartier03536982016-10-07 10:51:53 -070078 // Copy instance data. Don't assume memcpy copies by words (b/32012820).
79 {
80 const size_t offset = sizeof(Object);
Mathieu Chartier9d156d52016-10-06 17:44:26 -070081 uint8_t* src_bytes = reinterpret_cast<uint8_t*>(src.Ptr()) + offset;
82 uint8_t* dst_bytes = reinterpret_cast<uint8_t*>(dest.Ptr()) + offset;
Mathieu Chartier03536982016-10-07 10:51:53 -070083 num_bytes -= offset;
84 DCHECK_ALIGNED(src_bytes, sizeof(uintptr_t));
85 DCHECK_ALIGNED(dst_bytes, sizeof(uintptr_t));
86 // Use word sized copies to begin.
87 while (num_bytes >= sizeof(uintptr_t)) {
Mathieu Chartierfec13d42016-10-07 12:59:33 -070088 reinterpret_cast<Atomic<uintptr_t>*>(dst_bytes)->StoreRelaxed(
89 reinterpret_cast<Atomic<uintptr_t>*>(src_bytes)->LoadRelaxed());
Mathieu Chartier03536982016-10-07 10:51:53 -070090 src_bytes += sizeof(uintptr_t);
91 dst_bytes += sizeof(uintptr_t);
92 num_bytes -= sizeof(uintptr_t);
93 }
94 // Copy possible 32 bit word.
95 if (sizeof(uintptr_t) != sizeof(uint32_t) && num_bytes >= sizeof(uint32_t)) {
Mathieu Chartierfec13d42016-10-07 12:59:33 -070096 reinterpret_cast<Atomic<uint32_t>*>(dst_bytes)->StoreRelaxed(
97 reinterpret_cast<Atomic<uint32_t>*>(src_bytes)->LoadRelaxed());
Mathieu Chartier03536982016-10-07 10:51:53 -070098 src_bytes += sizeof(uint32_t);
99 dst_bytes += sizeof(uint32_t);
100 num_bytes -= sizeof(uint32_t);
101 }
102 // Copy remaining bytes, avoid going past the end of num_bytes since there may be a redzone
103 // there.
104 while (num_bytes > 0) {
Mathieu Chartierfec13d42016-10-07 12:59:33 -0700105 reinterpret_cast<Atomic<uint8_t>*>(dst_bytes)->StoreRelaxed(
106 reinterpret_cast<Atomic<uint8_t>*>(src_bytes)->LoadRelaxed());
Mathieu Chartier03536982016-10-07 10:51:53 -0700107 src_bytes += sizeof(uint8_t);
108 dst_bytes += sizeof(uint8_t);
109 num_bytes -= sizeof(uint8_t);
110 }
111 }
112
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800113 if (kUseReadBarrier) {
Mathieu Chartier03536982016-10-07 10:51:53 -0700114 // We need a RB here. After copying the whole object above, copy references fields one by one
115 // again with a RB to make sure there are no from space refs. TODO: Optimize this later?
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700116 CopyReferenceFieldsWithReadBarrierVisitor visitor(dest);
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700117 src->VisitReferences(visitor, visitor);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -0700118 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700119 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120 // Perform write barriers on copied object references.
Mathieu Chartier31e88222016-10-14 18:43:19 -0700121 ObjPtr<Class> c = src->GetClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 if (c->IsArrayClass()) {
123 if (!c->GetComponentType()->IsPrimitive()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800124 ObjectArray<Object>* array = dest->AsObjectArray<Object>();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700125 heap->WriteBarrierArray(dest, 0, array->GetLength());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 }
127 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700128 heap->WriteBarrierEveryFieldOf(dest);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700130 return dest.Ptr();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700131}
132
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700133// An allocation pre-fence visitor that copies the object.
134class CopyObjectVisitor {
135 public:
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -0700136 CopyObjectVisitor(Handle<Object>* orig, size_t num_bytes)
137 : orig_(orig), num_bytes_(num_bytes) {}
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700138
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700139 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700140 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -0700141 Object::CopyObject(obj, orig_->Get(), num_bytes_);
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700142 }
143
144 private:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700145 Handle<Object>* const orig_;
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700146 const size_t num_bytes_;
147 DISALLOW_COPY_AND_ASSIGN(CopyObjectVisitor);
148};
149
Mathieu Chartier590fee92013-09-13 13:46:47 -0700150Object* Object::Clone(Thread* self) {
151 CHECK(!IsClass()) << "Can't clone classes.";
152 // Object::SizeOf gets the right size even if we're an array. Using c->AllocObject() here would
153 // be wrong.
154 gc::Heap* heap = Runtime::Current()->GetHeap();
155 size_t num_bytes = SizeOf();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700156 StackHandleScope<1> hs(self);
157 Handle<Object> this_object(hs.NewHandle(this));
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700158 ObjPtr<Object> copy;
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -0700159 CopyObjectVisitor visitor(&this_object, num_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700160 if (heap->IsMovableObject(this)) {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700161 copy = heap->AllocObject<true>(self, GetClass(), num_bytes, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700162 } else {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700163 copy = heap->AllocNonMovableObject<true>(self, GetClass(), num_bytes, visitor);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164 }
Mathieu Chartier51eaa7f2016-10-12 16:08:35 -0700165 if (this_object->GetClass()->IsFinalizable()) {
166 heap->AddFinalizerReference(self, &copy);
167 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700168 return copy.Ptr();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169}
170
Ian Rogersbbcd30b2014-10-30 15:25:36 -0700171uint32_t Object::GenerateIdentityHashCode() {
Ian Rogersbbcd30b2014-10-30 15:25:36 -0700172 uint32_t expected_value, new_value;
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700173 do {
tony.ys_liu7380c312015-01-16 19:16:45 +0800174 expected_value = hash_code_seed.LoadRelaxed();
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700175 new_value = expected_value * 1103515245 + 12345;
tony.ys_liu7380c312015-01-16 19:16:45 +0800176 } while (!hash_code_seed.CompareExchangeWeakRelaxed(expected_value, new_value) ||
177 (expected_value & LockWord::kHashMask) == 0);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700178 return expected_value & LockWord::kHashMask;
179}
180
tony.ys_liu7380c312015-01-16 19:16:45 +0800181void Object::SetHashCodeSeed(uint32_t new_seed) {
182 hash_code_seed.StoreRelaxed(new_seed);
183}
184
Mathieu Chartier31e88222016-10-14 18:43:19 -0700185int32_t Object::IdentityHashCode() {
186 ObjPtr<Object> current_this = this; // The this pointer may get invalidated by thread suspension.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700187 while (true) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700188 LockWord lw = current_this->GetLockWord(false);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700189 switch (lw.GetState()) {
190 case LockWord::kUnlocked: {
191 // Try to compare and swap in a new hash, if we succeed we will return the hash on the next
192 // loop iteration.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700193 LockWord hash_word = LockWord::FromHashCode(GenerateIdentityHashCode(), lw.GCState());
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700194 DCHECK_EQ(hash_word.GetState(), LockWord::kHashCode);
Mathieu Chartier31e88222016-10-14 18:43:19 -0700195 if (current_this->CasLockWordWeakRelaxed(lw, hash_word)) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700196 return hash_word.GetHashCode();
197 }
198 break;
199 }
200 case LockWord::kThinLocked: {
Ian Rogers43c69cc2014-08-15 11:09:28 -0700201 // Inflate the thin lock to a monitor and stick the hash code inside of the monitor. May
202 // fail spuriously.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700203 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700204 StackHandleScope<1> hs(self);
205 Handle<mirror::Object> h_this(hs.NewHandle(current_this));
206 Monitor::InflateThinLocked(self, h_this, lw, GenerateIdentityHashCode());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700207 // A GC may have occurred when we switched to kBlocked.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700208 current_this = h_this.Get();
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700209 break;
210 }
211 case LockWord::kFatLocked: {
Roland Levillain51209232016-03-11 16:27:27 +0000212 // Already inflated, return the hash stored in the monitor.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700213 Monitor* monitor = lw.FatLockMonitor();
214 DCHECK(monitor != nullptr);
215 return monitor->GetHashCode();
216 }
217 case LockWord::kHashCode: {
218 return lw.GetHashCode();
219 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700220 default: {
221 LOG(FATAL) << "Invalid state during hashcode " << lw.GetState();
222 break;
223 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700224 }
225 }
Ian Rogers07140832014-09-30 15:43:59 -0700226 UNREACHABLE();
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.
Mathieu Chartier3398c782016-09-30 10:27:43 -0700241 ObjPtr<mirror::Class> field_type = field.GetType<!kMovingCollector>();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700242 if (field_type != nullptr) {
243 CHECK(field_type->IsAssignableFrom(new_value->GetClass()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800244 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700245 return;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800246 }
247 }
248 }
249 if (c->IsArrayClass()) {
250 // Bounds and assign-ability done in the array setter.
251 return;
252 }
253 if (IsClass()) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700254 for (ArtField& field : AsClass()->GetSFields()) {
255 if (field.GetOffset().Int32Value() == field_offset.Int32Value()) {
256 CHECK_NE(field.GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700257 // TODO: resolve the field type for moving GC.
Mathieu Chartier3398c782016-09-30 10:27:43 -0700258 ObjPtr<mirror::Class> field_type = field.GetType<!kMovingCollector>();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700259 if (field_type != nullptr) {
260 CHECK(field_type->IsAssignableFrom(new_value->GetClass()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800261 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700262 return;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800263 }
264 }
265 }
266 LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this)
David Sehr709b0702016-10-13 09:12:37 -0700267 << " of type " << c->PrettyDescriptor() << " at offset " << field_offset;
Ian Rogers08f1f502014-12-02 15:04:37 -0800268 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800269}
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800270
Hiroshi Yamauchieb2baaf2015-05-13 21:14:22 -0700271ArtField* Object::FindFieldByOffset(MemberOffset offset) {
272 return IsClass() ? ArtField::FindStaticFieldWithOffset(AsClass(), offset.Uint32Value())
273 : ArtField::FindInstanceFieldWithOffset(GetClass(), offset.Uint32Value());
274}
275
David Sehr709b0702016-10-13 09:12:37 -0700276std::string Object::PrettyTypeOf(ObjPtr<mirror::Object> obj) {
277 if (obj == nullptr) {
278 return "null";
279 }
280 return obj->PrettyTypeOf();
281}
282
283std::string Object::PrettyTypeOf() {
Mathieu Chartierbc632f02017-04-20 13:31:39 -0700284 // From-space version is the same as the to-space version since the dex file never changes.
285 // Avoiding the read barrier here is important to prevent recursive AssertToSpaceInvariant
286 // issues.
287 ObjPtr<mirror::Class> klass = GetClass<kDefaultVerifyFlags, kWithoutReadBarrier>();
288 if (klass == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700289 return "(raw)";
290 }
291 std::string temp;
Mathieu Chartierbc632f02017-04-20 13:31:39 -0700292 std::string result(PrettyDescriptor(klass->GetDescriptor(&temp)));
293 if (klass->IsClassClass()) {
David Sehr709b0702016-10-13 09:12:37 -0700294 result += "<" + PrettyDescriptor(AsClass()->GetDescriptor(&temp)) + ">";
295 }
296 return result;
297}
298
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299} // namespace mirror
300} // namespace art