Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1 | /* |
| 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 Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 17 | #include <ctime> |
| 18 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | #include "object.h" |
| 20 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 21 | #include "art_field.h" |
| 22 | #include "art_field-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 23 | #include "array-inl.h" |
| 24 | #include "class.h" |
| 25 | #include "class-inl.h" |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 26 | #include "class_linker-inl.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 27 | #include "field_helper.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 28 | #include "gc/accounting/card_table-inl.h" |
| 29 | #include "gc/heap.h" |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 30 | #include "iftable-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | #include "monitor.h" |
| 32 | #include "object-inl.h" |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 33 | #include "object_array-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | #include "runtime.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 35 | #include "handle_scope-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | #include "throwable.h" |
| 37 | #include "well_known_classes.h" |
| 38 | |
| 39 | namespace art { |
| 40 | namespace mirror { |
| 41 | |
Hiroshi Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 42 | class CopyReferenceFieldsWithReadBarrierVisitor { |
| 43 | public: |
| 44 | explicit CopyReferenceFieldsWithReadBarrierVisitor(Object* dest_obj) |
| 45 | : dest_obj_(dest_obj) {} |
| 46 | |
| 47 | void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const |
| 48 | ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 49 | // GetFieldObject() contains a RB. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 50 | Object* ref = obj->GetFieldObject<Object>(offset); |
Hiroshi Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 51 | // No WB here as a large object space does not have a card table |
| 52 | // coverage. Instead, cards will be marked separately. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 53 | dest_obj_->SetFieldObjectWithoutWriteBarrier<false, false>(offset, ref); |
Hiroshi Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void operator()(mirror::Class* klass, mirror::Reference* ref) const |
| 57 | ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 58 | // Copy java.lang.ref.Reference.referent which isn't visited in |
| 59 | // Object::VisitReferences(). |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 60 | DCHECK(klass->IsTypeOfReferenceClass()); |
Hiroshi Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 61 | this->operator()(ref, mirror::Reference::ReferentOffset(), false); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | Object* const dest_obj_; |
| 66 | }; |
| 67 | |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 68 | Object* Object::CopyObject(Thread* self, mirror::Object* dest, mirror::Object* src, |
| 69 | size_t num_bytes) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 70 | // Copy instance data. We assume memcpy copies by words. |
| 71 | // TODO: expose and use move32. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 72 | uint8_t* src_bytes = reinterpret_cast<uint8_t*>(src); |
| 73 | uint8_t* dst_bytes = reinterpret_cast<uint8_t*>(dest); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 74 | size_t offset = sizeof(Object); |
| 75 | memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset); |
Hiroshi Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 76 | if (kUseBakerOrBrooksReadBarrier) { |
| 77 | // We need a RB here. After the memcpy that covers the whole |
| 78 | // object above, copy references fields one by one again with a |
| 79 | // RB. TODO: Optimize this later? |
| 80 | CopyReferenceFieldsWithReadBarrierVisitor visitor(dest); |
| 81 | src->VisitReferences<true>(visitor, visitor); |
| 82 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 83 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 84 | // Perform write barriers on copied object references. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 85 | Class* c = src->GetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 86 | if (c->IsArrayClass()) { |
| 87 | if (!c->GetComponentType()->IsPrimitive()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 88 | ObjectArray<Object>* array = dest->AsObjectArray<Object>(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 89 | heap->WriteBarrierArray(dest, 0, array->GetLength()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 90 | } |
| 91 | } else { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 92 | heap->WriteBarrierEveryFieldOf(dest); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 93 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 94 | if (c->IsFinalizable()) { |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 95 | heap->AddFinalizerReference(self, &dest); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 96 | } |
| 97 | return dest; |
| 98 | } |
| 99 | |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 100 | // An allocation pre-fence visitor that copies the object. |
| 101 | class CopyObjectVisitor { |
| 102 | public: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 103 | explicit CopyObjectVisitor(Thread* self, Handle<Object>* orig, size_t num_bytes) |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 104 | : self_(self), orig_(orig), num_bytes_(num_bytes) { |
| 105 | } |
| 106 | |
| 107 | void operator()(Object* obj, size_t usable_size) const |
| 108 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 109 | UNUSED(usable_size); |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 110 | Object::CopyObject(self_, obj, orig_->Get(), num_bytes_); |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | private: |
| 114 | Thread* const self_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 115 | Handle<Object>* const orig_; |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 116 | const size_t num_bytes_; |
| 117 | DISALLOW_COPY_AND_ASSIGN(CopyObjectVisitor); |
| 118 | }; |
| 119 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 120 | Object* Object::Clone(Thread* self) { |
| 121 | CHECK(!IsClass()) << "Can't clone classes."; |
| 122 | // Object::SizeOf gets the right size even if we're an array. Using c->AllocObject() here would |
| 123 | // be wrong. |
| 124 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 125 | size_t num_bytes = SizeOf(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 126 | StackHandleScope<1> hs(self); |
| 127 | Handle<Object> this_object(hs.NewHandle(this)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 128 | Object* copy; |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 129 | CopyObjectVisitor visitor(self, &this_object, num_bytes); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 130 | if (heap->IsMovableObject(this)) { |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 131 | copy = heap->AllocObject<true>(self, GetClass(), num_bytes, visitor); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 132 | } else { |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 133 | copy = heap->AllocNonMovableObject<true>(self, GetClass(), num_bytes, visitor); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 134 | } |
Mathieu Chartier | 0732d59 | 2013-11-06 11:02:50 -0800 | [diff] [blame] | 135 | return copy; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 138 | int32_t Object::GenerateIdentityHashCode() { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 139 | static AtomicInteger seed(987654321 + std::time(nullptr)); |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 140 | int32_t expected_value, new_value; |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 141 | do { |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 142 | expected_value = static_cast<uint32_t>(seed.LoadRelaxed()); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 143 | new_value = expected_value * 1103515245 + 12345; |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 144 | } while ((expected_value & LockWord::kHashMask) == 0 || |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 145 | !seed.CompareExchangeWeakRelaxed(expected_value, new_value)); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 146 | return expected_value & LockWord::kHashMask; |
| 147 | } |
| 148 | |
| 149 | int32_t Object::IdentityHashCode() const { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 150 | mirror::Object* current_this = const_cast<mirror::Object*>(this); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 151 | while (true) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 152 | LockWord lw = current_this->GetLockWord(false); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 153 | switch (lw.GetState()) { |
| 154 | case LockWord::kUnlocked: { |
| 155 | // Try to compare and swap in a new hash, if we succeed we will return the hash on the next |
| 156 | // loop iteration. |
| 157 | LockWord hash_word(LockWord::FromHashCode(GenerateIdentityHashCode())); |
| 158 | DCHECK_EQ(hash_word.GetState(), LockWord::kHashCode); |
Hans Boehm | d843443 | 2014-07-11 09:56:07 -0700 | [diff] [blame] | 159 | if (const_cast<Object*>(this)->CasLockWordWeakRelaxed(lw, hash_word)) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 160 | return hash_word.GetHashCode(); |
| 161 | } |
| 162 | break; |
| 163 | } |
| 164 | case LockWord::kThinLocked: { |
Ian Rogers | 43c69cc | 2014-08-15 11:09:28 -0700 | [diff] [blame] | 165 | // Inflate the thin lock to a monitor and stick the hash code inside of the monitor. May |
| 166 | // fail spuriously. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 167 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 168 | StackHandleScope<1> hs(self); |
| 169 | Handle<mirror::Object> h_this(hs.NewHandle(current_this)); |
| 170 | Monitor::InflateThinLocked(self, h_this, lw, GenerateIdentityHashCode()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 171 | // A GC may have occurred when we switched to kBlocked. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 172 | current_this = h_this.Get(); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 173 | break; |
| 174 | } |
| 175 | case LockWord::kFatLocked: { |
| 176 | // Already inflated, return the has stored in the monitor. |
| 177 | Monitor* monitor = lw.FatLockMonitor(); |
| 178 | DCHECK(monitor != nullptr); |
| 179 | return monitor->GetHashCode(); |
| 180 | } |
| 181 | case LockWord::kHashCode: { |
| 182 | return lw.GetHashCode(); |
| 183 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 184 | default: { |
| 185 | LOG(FATAL) << "Invalid state during hashcode " << lw.GetState(); |
| 186 | break; |
| 187 | } |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 190 | UNREACHABLE(); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 193 | void Object::CheckFieldAssignmentImpl(MemberOffset field_offset, Object* new_value) { |
| 194 | Class* c = GetClass(); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 195 | Runtime* runtime = Runtime::Current(); |
| 196 | if (runtime->GetClassLinker() == nullptr || !runtime->IsStarted() || |
| 197 | !runtime->GetHeap()->IsObjectValidationEnabled() || !c->IsResolved()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 198 | return; |
| 199 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 200 | for (Class* cur = c; cur != NULL; cur = cur->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 201 | ObjectArray<ArtField>* fields = cur->GetIFields(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 202 | if (fields != NULL) { |
| 203 | size_t num_ref_ifields = cur->NumReferenceInstanceFields(); |
| 204 | for (size_t i = 0; i < num_ref_ifields; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 205 | ArtField* field = fields->Get(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 206 | if (field->GetOffset().Int32Value() == field_offset.Int32Value()) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 207 | StackHandleScope<1> hs(Thread::Current()); |
| 208 | FieldHelper fh(hs.NewHandle(field)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 209 | CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass())); |
| 210 | return; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | if (c->IsArrayClass()) { |
| 216 | // Bounds and assign-ability done in the array setter. |
| 217 | return; |
| 218 | } |
| 219 | if (IsClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 220 | ObjectArray<ArtField>* fields = AsClass()->GetSFields(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 221 | if (fields != NULL) { |
| 222 | size_t num_ref_sfields = AsClass()->NumReferenceStaticFields(); |
| 223 | for (size_t i = 0; i < num_ref_sfields; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 224 | ArtField* field = fields->Get(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 225 | if (field->GetOffset().Int32Value() == field_offset.Int32Value()) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 226 | StackHandleScope<1> hs(Thread::Current()); |
| 227 | FieldHelper fh(hs.NewHandle(field)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 228 | CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass())); |
| 229 | return; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this) |
| 235 | << " of type " << PrettyDescriptor(c) << " at offset " << field_offset; |
| 236 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 237 | |
| 238 | } // namespace mirror |
| 239 | } // namespace art |