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 | |
| 21 | #include "array-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 22 | #include "art_field-inl.h" |
| 23 | #include "art_field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "class-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 25 | #include "class.h" |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 26 | #include "class_linker-inl.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 27 | #include "dex/dex_file-inl.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" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 30 | #include "handle_scope-inl.h" |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 31 | #include "iftable-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | #include "monitor.h" |
| 33 | #include "object-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 34 | #include "object-refvisitor-inl.h" |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 35 | #include "object_array-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | #include "runtime.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 37 | #include "throwable.h" |
| 38 | #include "well_known_classes.h" |
| 39 | |
| 40 | namespace art { |
| 41 | namespace mirror { |
| 42 | |
tony.ys_liu | 7380c31 | 2015-01-16 19:16:45 +0800 | [diff] [blame] | 43 | Atomic<uint32_t> Object::hash_code_seed(987654321U + std::time(nullptr)); |
| 44 | |
Hiroshi Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 45 | class CopyReferenceFieldsWithReadBarrierVisitor { |
| 46 | public: |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 47 | explicit CopyReferenceFieldsWithReadBarrierVisitor(ObjPtr<Object> dest_obj) |
Hiroshi Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 48 | : dest_obj_(dest_obj) {} |
| 49 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 50 | void operator()(ObjPtr<Object> obj, MemberOffset offset, bool /* is_static */) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 51 | ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 52 | // GetFieldObject() contains a RB. |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 53 | ObjPtr<Object> ref = obj->GetFieldObject<Object>(offset); |
Hiroshi Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 54 | // No WB here as a large object space does not have a card table |
| 55 | // coverage. Instead, cards will be marked separately. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 56 | dest_obj_->SetFieldObjectWithoutWriteBarrier<false, false>(offset, ref); |
Hiroshi Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 59 | void operator()(ObjPtr<mirror::Class> klass, mirror::Reference* ref) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 60 | ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 61 | // Copy java.lang.ref.Reference.referent which isn't visited in |
| 62 | // Object::VisitReferences(). |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 63 | DCHECK(klass->IsTypeOfReferenceClass()); |
Hiroshi Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 64 | this->operator()(ref, mirror::Reference::ReferentOffset(), false); |
| 65 | } |
| 66 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 67 | // 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 Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 72 | private: |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 73 | ObjPtr<Object> const dest_obj_; |
Hiroshi Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Mathieu Chartier | 51eaa7f | 2016-10-12 16:08:35 -0700 | [diff] [blame] | 76 | Object* Object::CopyObject(ObjPtr<mirror::Object> dest, |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 77 | ObjPtr<mirror::Object> src, |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 78 | size_t num_bytes) { |
Mathieu Chartier | 0353698 | 2016-10-07 10:51:53 -0700 | [diff] [blame] | 79 | // Copy instance data. Don't assume memcpy copies by words (b/32012820). |
| 80 | { |
| 81 | const size_t offset = sizeof(Object); |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 82 | uint8_t* src_bytes = reinterpret_cast<uint8_t*>(src.Ptr()) + offset; |
| 83 | uint8_t* dst_bytes = reinterpret_cast<uint8_t*>(dest.Ptr()) + offset; |
Mathieu Chartier | 0353698 | 2016-10-07 10:51:53 -0700 | [diff] [blame] | 84 | 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 Chartier | fec13d4 | 2016-10-07 12:59:33 -0700 | [diff] [blame] | 89 | reinterpret_cast<Atomic<uintptr_t>*>(dst_bytes)->StoreRelaxed( |
| 90 | reinterpret_cast<Atomic<uintptr_t>*>(src_bytes)->LoadRelaxed()); |
Mathieu Chartier | 0353698 | 2016-10-07 10:51:53 -0700 | [diff] [blame] | 91 | 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 Chartier | fec13d4 | 2016-10-07 12:59:33 -0700 | [diff] [blame] | 97 | reinterpret_cast<Atomic<uint32_t>*>(dst_bytes)->StoreRelaxed( |
| 98 | reinterpret_cast<Atomic<uint32_t>*>(src_bytes)->LoadRelaxed()); |
Mathieu Chartier | 0353698 | 2016-10-07 10:51:53 -0700 | [diff] [blame] | 99 | 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 Chartier | fec13d4 | 2016-10-07 12:59:33 -0700 | [diff] [blame] | 106 | reinterpret_cast<Atomic<uint8_t>*>(dst_bytes)->StoreRelaxed( |
| 107 | reinterpret_cast<Atomic<uint8_t>*>(src_bytes)->LoadRelaxed()); |
Mathieu Chartier | 0353698 | 2016-10-07 10:51:53 -0700 | [diff] [blame] | 108 | src_bytes += sizeof(uint8_t); |
| 109 | dst_bytes += sizeof(uint8_t); |
| 110 | num_bytes -= sizeof(uint8_t); |
| 111 | } |
| 112 | } |
| 113 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 114 | if (kUseReadBarrier) { |
Mathieu Chartier | 0353698 | 2016-10-07 10:51:53 -0700 | [diff] [blame] | 115 | // 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 Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 117 | CopyReferenceFieldsWithReadBarrierVisitor visitor(dest); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 118 | src->VisitReferences(visitor, visitor); |
Hiroshi Yamauchi | 7971928 | 2014-04-10 12:46:22 -0700 | [diff] [blame] | 119 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 120 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 121 | // Perform write barriers on copied object references. |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 122 | ObjPtr<Class> c = src->GetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 123 | if (c->IsArrayClass()) { |
| 124 | if (!c->GetComponentType()->IsPrimitive()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 125 | ObjectArray<Object>* array = dest->AsObjectArray<Object>(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 126 | heap->WriteBarrierArray(dest, 0, array->GetLength()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 127 | } |
| 128 | } else { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 129 | heap->WriteBarrierEveryFieldOf(dest); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 130 | } |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 131 | return dest.Ptr(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 134 | // An allocation pre-fence visitor that copies the object. |
| 135 | class CopyObjectVisitor { |
| 136 | public: |
Mathieu Chartier | 51eaa7f | 2016-10-12 16:08:35 -0700 | [diff] [blame] | 137 | CopyObjectVisitor(Handle<Object>* orig, size_t num_bytes) |
| 138 | : orig_(orig), num_bytes_(num_bytes) {} |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 139 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 140 | void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 141 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 51eaa7f | 2016-10-12 16:08:35 -0700 | [diff] [blame] | 142 | Object::CopyObject(obj, orig_->Get(), num_bytes_); |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | private: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 146 | Handle<Object>* const orig_; |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 147 | const size_t num_bytes_; |
| 148 | DISALLOW_COPY_AND_ASSIGN(CopyObjectVisitor); |
| 149 | }; |
| 150 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 151 | Object* 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 Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 157 | StackHandleScope<1> hs(self); |
| 158 | Handle<Object> this_object(hs.NewHandle(this)); |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 159 | ObjPtr<Object> copy; |
Mathieu Chartier | 51eaa7f | 2016-10-12 16:08:35 -0700 | [diff] [blame] | 160 | CopyObjectVisitor visitor(&this_object, num_bytes); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 161 | if (heap->IsMovableObject(this)) { |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 162 | copy = heap->AllocObject<true>(self, GetClass(), num_bytes, visitor); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 163 | } else { |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 164 | copy = heap->AllocNonMovableObject<true>(self, GetClass(), num_bytes, visitor); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 165 | } |
Mathieu Chartier | 51eaa7f | 2016-10-12 16:08:35 -0700 | [diff] [blame] | 166 | if (this_object->GetClass()->IsFinalizable()) { |
| 167 | heap->AddFinalizerReference(self, ©); |
| 168 | } |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 169 | return copy.Ptr(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Ian Rogers | bbcd30b | 2014-10-30 15:25:36 -0700 | [diff] [blame] | 172 | uint32_t Object::GenerateIdentityHashCode() { |
Ian Rogers | bbcd30b | 2014-10-30 15:25:36 -0700 | [diff] [blame] | 173 | uint32_t expected_value, new_value; |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 174 | do { |
tony.ys_liu | 7380c31 | 2015-01-16 19:16:45 +0800 | [diff] [blame] | 175 | expected_value = hash_code_seed.LoadRelaxed(); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 176 | new_value = expected_value * 1103515245 + 12345; |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 177 | } while (!hash_code_seed.CompareAndSetWeakRelaxed(expected_value, new_value) || |
tony.ys_liu | 7380c31 | 2015-01-16 19:16:45 +0800 | [diff] [blame] | 178 | (expected_value & LockWord::kHashMask) == 0); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 179 | return expected_value & LockWord::kHashMask; |
| 180 | } |
| 181 | |
tony.ys_liu | 7380c31 | 2015-01-16 19:16:45 +0800 | [diff] [blame] | 182 | void Object::SetHashCodeSeed(uint32_t new_seed) { |
| 183 | hash_code_seed.StoreRelaxed(new_seed); |
| 184 | } |
| 185 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 186 | int32_t Object::IdentityHashCode() { |
| 187 | ObjPtr<Object> current_this = this; // The this pointer may get invalidated by thread suspension. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 188 | while (true) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 189 | LockWord lw = current_this->GetLockWord(false); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 190 | 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 Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 194 | LockWord hash_word = LockWord::FromHashCode(GenerateIdentityHashCode(), lw.GCState()); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 195 | DCHECK_EQ(hash_word.GetState(), LockWord::kHashCode); |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 196 | if (current_this->CasLockWordWeakRelaxed(lw, hash_word)) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 197 | return hash_word.GetHashCode(); |
| 198 | } |
| 199 | break; |
| 200 | } |
| 201 | case LockWord::kThinLocked: { |
Ian Rogers | 43c69cc | 2014-08-15 11:09:28 -0700 | [diff] [blame] | 202 | // Inflate the thin lock to a monitor and stick the hash code inside of the monitor. May |
| 203 | // fail spuriously. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 204 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 205 | StackHandleScope<1> hs(self); |
| 206 | Handle<mirror::Object> h_this(hs.NewHandle(current_this)); |
| 207 | Monitor::InflateThinLocked(self, h_this, lw, GenerateIdentityHashCode()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 208 | // A GC may have occurred when we switched to kBlocked. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 209 | current_this = h_this.Get(); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 210 | break; |
| 211 | } |
| 212 | case LockWord::kFatLocked: { |
Roland Levillain | 5120923 | 2016-03-11 16:27:27 +0000 | [diff] [blame] | 213 | // Already inflated, return the hash stored in the monitor. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 214 | Monitor* monitor = lw.FatLockMonitor(); |
| 215 | DCHECK(monitor != nullptr); |
| 216 | return monitor->GetHashCode(); |
| 217 | } |
| 218 | case LockWord::kHashCode: { |
| 219 | return lw.GetHashCode(); |
| 220 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 221 | default: { |
| 222 | LOG(FATAL) << "Invalid state during hashcode " << lw.GetState(); |
| 223 | break; |
| 224 | } |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 227 | UNREACHABLE(); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Mathieu Chartier | a058fdf | 2016-10-06 15:13:58 -0700 | [diff] [blame] | 230 | void Object::CheckFieldAssignmentImpl(MemberOffset field_offset, ObjPtr<Object> new_value) { |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 231 | ObjPtr<Class> c = GetClass(); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 232 | Runtime* runtime = Runtime::Current(); |
| 233 | if (runtime->GetClassLinker() == nullptr || !runtime->IsStarted() || |
| 234 | !runtime->GetHeap()->IsObjectValidationEnabled() || !c->IsResolved()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 235 | return; |
| 236 | } |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 237 | for (ObjPtr<Class> cur = c; cur != nullptr; cur = cur->GetSuperClass()) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 238 | for (ArtField& field : cur->GetIFields()) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 239 | if (field.GetOffset().Int32Value() == field_offset.Int32Value()) { |
| 240 | CHECK_NE(field.GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 241 | // TODO: resolve the field type for moving GC. |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 242 | ObjPtr<mirror::Class> field_type = |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 243 | kMovingCollector ? field.LookupResolvedType() : field.ResolveType(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 244 | if (field_type != nullptr) { |
| 245 | CHECK(field_type->IsAssignableFrom(new_value->GetClass())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 246 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 247 | return; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |
| 251 | if (c->IsArrayClass()) { |
| 252 | // Bounds and assign-ability done in the array setter. |
| 253 | return; |
| 254 | } |
| 255 | if (IsClass()) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 256 | for (ArtField& field : AsClass()->GetSFields()) { |
| 257 | if (field.GetOffset().Int32Value() == field_offset.Int32Value()) { |
| 258 | CHECK_NE(field.GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 259 | // TODO: resolve the field type for moving GC. |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 260 | ObjPtr<mirror::Class> field_type = |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 261 | kMovingCollector ? field.LookupResolvedType() : field.ResolveType(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 262 | if (field_type != nullptr) { |
| 263 | CHECK(field_type->IsAssignableFrom(new_value->GetClass())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 264 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 265 | return; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | } |
| 269 | LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 270 | << " of type " << c->PrettyDescriptor() << " at offset " << field_offset; |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 271 | UNREACHABLE(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 272 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 273 | |
Hiroshi Yamauchi | eb2baaf | 2015-05-13 21:14:22 -0700 | [diff] [blame] | 274 | ArtField* Object::FindFieldByOffset(MemberOffset offset) { |
| 275 | return IsClass() ? ArtField::FindStaticFieldWithOffset(AsClass(), offset.Uint32Value()) |
| 276 | : ArtField::FindInstanceFieldWithOffset(GetClass(), offset.Uint32Value()); |
| 277 | } |
| 278 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 279 | std::string Object::PrettyTypeOf(ObjPtr<mirror::Object> obj) { |
| 280 | if (obj == nullptr) { |
| 281 | return "null"; |
| 282 | } |
| 283 | return obj->PrettyTypeOf(); |
| 284 | } |
| 285 | |
| 286 | std::string Object::PrettyTypeOf() { |
Mathieu Chartier | bc632f0 | 2017-04-20 13:31:39 -0700 | [diff] [blame] | 287 | // 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 Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 292 | return "(raw)"; |
| 293 | } |
| 294 | std::string temp; |
Mathieu Chartier | bc632f0 | 2017-04-20 13:31:39 -0700 | [diff] [blame] | 295 | std::string result(PrettyDescriptor(klass->GetDescriptor(&temp))); |
| 296 | if (klass->IsClassClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 297 | result += "<" + PrettyDescriptor(AsClass()->GetDescriptor(&temp)) + ">"; |
| 298 | } |
| 299 | return result; |
| 300 | } |
| 301 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 302 | } // namespace mirror |
| 303 | } // namespace art |