blob: 6914f383df779735dd96105206641f6a43c05c6b [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:
46 explicit CopyReferenceFieldsWithReadBarrierVisitor(Object* dest_obj)
47 : dest_obj_(dest_obj) {}
48
49 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const
50 ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
51 // GetFieldObject() contains a RB.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070052 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
58 void operator()(mirror::Class* klass, mirror::Reference* ref) const
59 ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
60 // 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
66 private:
67 Object* const dest_obj_;
68};
69
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -070070Object* Object::CopyObject(Thread* self, mirror::Object* dest, mirror::Object* src,
71 size_t num_bytes) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072 // Copy instance data. We assume memcpy copies by words.
73 // TODO: expose and use move32.
Ian Rogers13735952014-10-08 12:43:28 -070074 uint8_t* src_bytes = reinterpret_cast<uint8_t*>(src);
75 uint8_t* dst_bytes = reinterpret_cast<uint8_t*>(dest);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 size_t offset = sizeof(Object);
77 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
Hiroshi Yamauchi79719282014-04-10 12:46:22 -070078 if (kUseBakerOrBrooksReadBarrier) {
79 // We need a RB here. After the memcpy that covers the whole
80 // object above, copy references fields one by one again with a
81 // RB. TODO: Optimize this later?
82 CopyReferenceFieldsWithReadBarrierVisitor visitor(dest);
83 src->VisitReferences<true>(visitor, visitor);
84 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070085 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086 // Perform write barriers on copied object references.
Mathieu Chartier590fee92013-09-13 13:46:47 -070087 Class* c = src->GetClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088 if (c->IsArrayClass()) {
89 if (!c->GetComponentType()->IsPrimitive()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080090 ObjectArray<Object>* array = dest->AsObjectArray<Object>();
Mathieu Chartier590fee92013-09-13 13:46:47 -070091 heap->WriteBarrierArray(dest, 0, array->GetLength());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 }
93 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -070094 heap->WriteBarrierEveryFieldOf(dest);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096 if (c->IsFinalizable()) {
Mathieu Chartier8668c3c2014-04-24 16:48:11 -070097 heap->AddFinalizerReference(self, &dest);
Mathieu Chartier590fee92013-09-13 13:46:47 -070098 }
99 return dest;
100}
101
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700102// An allocation pre-fence visitor that copies the object.
103class CopyObjectVisitor {
104 public:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700105 explicit CopyObjectVisitor(Thread* self, Handle<Object>* orig, size_t num_bytes)
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700106 : self_(self), orig_(orig), num_bytes_(num_bytes) {
107 }
108
109 void operator()(Object* obj, size_t usable_size) const
110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
111 UNUSED(usable_size);
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700112 Object::CopyObject(self_, obj, orig_->Get(), num_bytes_);
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700113 }
114
115 private:
116 Thread* const self_;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700117 Handle<Object>* const orig_;
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700118 const size_t num_bytes_;
119 DISALLOW_COPY_AND_ASSIGN(CopyObjectVisitor);
120};
121
Mathieu Chartier590fee92013-09-13 13:46:47 -0700122Object* Object::Clone(Thread* self) {
123 CHECK(!IsClass()) << "Can't clone classes.";
124 // Object::SizeOf gets the right size even if we're an array. Using c->AllocObject() here would
125 // be wrong.
126 gc::Heap* heap = Runtime::Current()->GetHeap();
127 size_t num_bytes = SizeOf();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700128 StackHandleScope<1> hs(self);
129 Handle<Object> this_object(hs.NewHandle(this));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700130 Object* copy;
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700131 CopyObjectVisitor visitor(self, &this_object, num_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700132 if (heap->IsMovableObject(this)) {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700133 copy = heap->AllocObject<true>(self, GetClass(), num_bytes, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700134 } else {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700135 copy = heap->AllocNonMovableObject<true>(self, GetClass(), num_bytes, visitor);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136 }
Mathieu Chartier0732d592013-11-06 11:02:50 -0800137 return copy;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138}
139
Ian Rogersbbcd30b2014-10-30 15:25:36 -0700140uint32_t Object::GenerateIdentityHashCode() {
Ian Rogersbbcd30b2014-10-30 15:25:36 -0700141 uint32_t expected_value, new_value;
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700142 do {
tony.ys_liu7380c312015-01-16 19:16:45 +0800143 expected_value = hash_code_seed.LoadRelaxed();
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700144 new_value = expected_value * 1103515245 + 12345;
tony.ys_liu7380c312015-01-16 19:16:45 +0800145 } while (!hash_code_seed.CompareExchangeWeakRelaxed(expected_value, new_value) ||
146 (expected_value & LockWord::kHashMask) == 0);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700147 return expected_value & LockWord::kHashMask;
148}
149
tony.ys_liu7380c312015-01-16 19:16:45 +0800150void Object::SetHashCodeSeed(uint32_t new_seed) {
151 hash_code_seed.StoreRelaxed(new_seed);
152}
153
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700154int32_t Object::IdentityHashCode() const {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700155 mirror::Object* current_this = const_cast<mirror::Object*>(this);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700156 while (true) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700157 LockWord lw = current_this->GetLockWord(false);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700158 switch (lw.GetState()) {
159 case LockWord::kUnlocked: {
160 // Try to compare and swap in a new hash, if we succeed we will return the hash on the next
161 // loop iteration.
162 LockWord hash_word(LockWord::FromHashCode(GenerateIdentityHashCode()));
163 DCHECK_EQ(hash_word.GetState(), LockWord::kHashCode);
Hans Boehmd8434432014-07-11 09:56:07 -0700164 if (const_cast<Object*>(this)->CasLockWordWeakRelaxed(lw, hash_word)) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700165 return hash_word.GetHashCode();
166 }
167 break;
168 }
169 case LockWord::kThinLocked: {
Ian Rogers43c69cc2014-08-15 11:09:28 -0700170 // Inflate the thin lock to a monitor and stick the hash code inside of the monitor. May
171 // fail spuriously.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700172 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700173 StackHandleScope<1> hs(self);
174 Handle<mirror::Object> h_this(hs.NewHandle(current_this));
175 Monitor::InflateThinLocked(self, h_this, lw, GenerateIdentityHashCode());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700176 // A GC may have occurred when we switched to kBlocked.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700177 current_this = h_this.Get();
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700178 break;
179 }
180 case LockWord::kFatLocked: {
181 // Already inflated, return the has stored in the monitor.
182 Monitor* monitor = lw.FatLockMonitor();
183 DCHECK(monitor != nullptr);
184 return monitor->GetHashCode();
185 }
186 case LockWord::kHashCode: {
187 return lw.GetHashCode();
188 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700189 default: {
190 LOG(FATAL) << "Invalid state during hashcode " << lw.GetState();
191 break;
192 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700193 }
194 }
Ian Rogers07140832014-09-30 15:43:59 -0700195 UNREACHABLE();
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700196}
197
Ian Rogersef7d42f2014-01-06 12:55:46 -0800198void Object::CheckFieldAssignmentImpl(MemberOffset field_offset, Object* new_value) {
199 Class* c = GetClass();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800200 Runtime* runtime = Runtime::Current();
201 if (runtime->GetClassLinker() == nullptr || !runtime->IsStarted() ||
202 !runtime->GetHeap()->IsObjectValidationEnabled() || !c->IsResolved()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203 return;
204 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800205 for (Class* cur = c; cur != NULL; cur = cur->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700206 ObjectArray<ArtField>* fields = cur->GetIFields();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207 if (fields != NULL) {
Vladimir Marko76649e82014-11-10 18:32:59 +0000208 size_t num_ifields = fields->GetLength();
209 for (size_t i = 0; i < num_ifields; ++i) {
Ian Rogers08f1f502014-12-02 15:04:37 -0800210 StackHandleScope<1> hs(Thread::Current());
211 Handle<Object> h_object(hs.NewHandle(new_value));
Brian Carlstromea46f952013-07-30 01:26:50 -0700212 ArtField* field = fields->Get(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 if (field->GetOffset().Int32Value() == field_offset.Int32Value()) {
Vladimir Marko76649e82014-11-10 18:32:59 +0000214 CHECK_NE(field->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Ian Rogers08f1f502014-12-02 15:04:37 -0800215 // TODO: resolve the field type for moving GC.
216 mirror::Class* field_type = field->GetType(!kMovingCollector);
217 if (field_type != nullptr) {
218 CHECK(field_type->IsAssignableFrom(new_value->GetClass()));
219 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 return;
221 }
222 }
223 }
224 }
225 if (c->IsArrayClass()) {
226 // Bounds and assign-ability done in the array setter.
227 return;
228 }
229 if (IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700230 ObjectArray<ArtField>* fields = AsClass()->GetSFields();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800231 if (fields != NULL) {
Vladimir Marko76649e82014-11-10 18:32:59 +0000232 size_t num_sfields = fields->GetLength();
233 for (size_t i = 0; i < num_sfields; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700234 ArtField* field = fields->Get(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 if (field->GetOffset().Int32Value() == field_offset.Int32Value()) {
Vladimir Marko76649e82014-11-10 18:32:59 +0000236 CHECK_NE(field->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Ian Rogers08f1f502014-12-02 15:04:37 -0800237 // TODO: resolve the field type for moving GC.
238 mirror::Class* field_type = field->GetType(!kMovingCollector);
239 if (field_type != nullptr) {
240 CHECK(field_type->IsAssignableFrom(new_value->GetClass()));
241 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800242 return;
243 }
244 }
245 }
246 }
247 LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this)
248 << " of type " << PrettyDescriptor(c) << " at offset " << field_offset;
Ian Rogers08f1f502014-12-02 15:04:37 -0800249 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800250}
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800251
252} // namespace mirror
253} // namespace art