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 | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 27 | #include "gc/accounting/card_table-inl.h" |
| 28 | #include "gc/heap.h" |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 29 | #include "iftable-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | #include "monitor.h" |
| 31 | #include "object-inl.h" |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 32 | #include "object_array-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | #include "object_utils.h" |
| 34 | #include "runtime.h" |
| 35 | #include "sirt_ref.h" |
| 36 | #include "throwable.h" |
| 37 | #include "well_known_classes.h" |
| 38 | |
| 39 | namespace art { |
| 40 | namespace mirror { |
| 41 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 42 | static Object* CopyObject(Thread* self, mirror::Object* dest, mirror::Object* src, size_t num_bytes) |
| 43 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 44 | // Copy instance data. We assume memcpy copies by words. |
| 45 | // TODO: expose and use move32. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 46 | byte* src_bytes = reinterpret_cast<byte*>(src); |
| 47 | byte* dst_bytes = reinterpret_cast<byte*>(dest); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | size_t offset = sizeof(Object); |
| 49 | memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 50 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 51 | // Perform write barriers on copied object references. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 52 | Class* c = src->GetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 53 | if (c->IsArrayClass()) { |
| 54 | if (!c->GetComponentType()->IsPrimitive()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 55 | const ObjectArray<Object>* array = dest->AsObjectArray<Object>(); |
| 56 | heap->WriteBarrierArray(dest, 0, array->GetLength()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 57 | } |
| 58 | } else { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 59 | heap->WriteBarrierEveryFieldOf(dest); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 60 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 61 | if (c->IsFinalizable()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 62 | SirtRef<Object> sirt_dest(self, dest); |
| 63 | heap->AddFinalizerReference(self, dest); |
| 64 | return sirt_dest.get(); |
| 65 | } |
| 66 | return dest; |
| 67 | } |
| 68 | |
| 69 | Object* Object::Clone(Thread* self) { |
| 70 | CHECK(!IsClass()) << "Can't clone classes."; |
| 71 | // Object::SizeOf gets the right size even if we're an array. Using c->AllocObject() here would |
| 72 | // be wrong. |
| 73 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 74 | size_t num_bytes = SizeOf(); |
| 75 | SirtRef<Object> this_object(self, this); |
| 76 | Object* copy; |
| 77 | if (heap->IsMovableObject(this)) { |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 78 | copy = heap->AllocObject<true>(self, GetClass(), num_bytes); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 79 | } else { |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 80 | copy = heap->AllocNonMovableObject<true>(self, GetClass(), num_bytes); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 81 | } |
| 82 | if (LIKELY(copy != nullptr)) { |
| 83 | return CopyObject(self, copy, this_object.get(), num_bytes); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 84 | } |
Mathieu Chartier | 0732d59 | 2013-11-06 11:02:50 -0800 | [diff] [blame] | 85 | return copy; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 88 | int32_t Object::GenerateIdentityHashCode() { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 89 | static AtomicInteger seed(987654321 + std::time(nullptr)); |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 90 | int32_t expected_value, new_value; |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 91 | do { |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame^] | 92 | expected_value = static_cast<uint32_t>(seed.Load()); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 93 | new_value = expected_value * 1103515245 + 12345; |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 94 | } while ((expected_value & LockWord::kHashMask) == 0 || |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame^] | 95 | !seed.CompareAndSwap(expected_value, new_value)); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 96 | return expected_value & LockWord::kHashMask; |
| 97 | } |
| 98 | |
| 99 | int32_t Object::IdentityHashCode() const { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 100 | mirror::Object* current_this = const_cast<mirror::Object*>(this); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 101 | while (true) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 102 | LockWord lw = current_this->GetLockWord(); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 103 | switch (lw.GetState()) { |
| 104 | case LockWord::kUnlocked: { |
| 105 | // Try to compare and swap in a new hash, if we succeed we will return the hash on the next |
| 106 | // loop iteration. |
| 107 | LockWord hash_word(LockWord::FromHashCode(GenerateIdentityHashCode())); |
| 108 | DCHECK_EQ(hash_word.GetState(), LockWord::kHashCode); |
| 109 | if (const_cast<Object*>(this)->CasLockWord(lw, hash_word)) { |
| 110 | return hash_word.GetHashCode(); |
| 111 | } |
| 112 | break; |
| 113 | } |
| 114 | case LockWord::kThinLocked: { |
| 115 | // Inflate the thin lock to a monitor and stick the hash code inside of the monitor. |
| 116 | Thread* self = Thread::Current(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 117 | SirtRef<mirror::Object> sirt_this(self, current_this); |
| 118 | Monitor::InflateThinLocked(self, sirt_this, lw, GenerateIdentityHashCode()); |
| 119 | // A GC may have occurred when we switched to kBlocked. |
| 120 | current_this = sirt_this.get(); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 121 | break; |
| 122 | } |
| 123 | case LockWord::kFatLocked: { |
| 124 | // Already inflated, return the has stored in the monitor. |
| 125 | Monitor* monitor = lw.FatLockMonitor(); |
| 126 | DCHECK(monitor != nullptr); |
| 127 | return monitor->GetHashCode(); |
| 128 | } |
| 129 | case LockWord::kHashCode: { |
| 130 | return lw.GetHashCode(); |
| 131 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 132 | default: { |
| 133 | LOG(FATAL) << "Invalid state during hashcode " << lw.GetState(); |
| 134 | break; |
| 135 | } |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | LOG(FATAL) << "Unreachable"; |
| 139 | return 0; |
| 140 | } |
| 141 | |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 142 | void Object::CheckFieldAssignmentImpl(MemberOffset field_offset, const Object* new_value) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 143 | const Class* c = GetClass(); |
| 144 | if (Runtime::Current()->GetClassLinker() == NULL || |
| 145 | !Runtime::Current()->GetHeap()->IsObjectValidationEnabled() || |
| 146 | !c->IsResolved()) { |
| 147 | return; |
| 148 | } |
| 149 | for (const Class* cur = c; cur != NULL; cur = cur->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 150 | ObjectArray<ArtField>* fields = cur->GetIFields(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 151 | if (fields != NULL) { |
| 152 | size_t num_ref_ifields = cur->NumReferenceInstanceFields(); |
| 153 | for (size_t i = 0; i < num_ref_ifields; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 154 | ArtField* field = fields->Get(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 155 | if (field->GetOffset().Int32Value() == field_offset.Int32Value()) { |
| 156 | FieldHelper fh(field); |
| 157 | CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass())); |
| 158 | return; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | if (c->IsArrayClass()) { |
| 164 | // Bounds and assign-ability done in the array setter. |
| 165 | return; |
| 166 | } |
| 167 | if (IsClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 168 | ObjectArray<ArtField>* fields = AsClass()->GetSFields(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 169 | if (fields != NULL) { |
| 170 | size_t num_ref_sfields = AsClass()->NumReferenceStaticFields(); |
| 171 | for (size_t i = 0; i < num_ref_sfields; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 172 | ArtField* field = fields->Get(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 173 | if (field->GetOffset().Int32Value() == field_offset.Int32Value()) { |
| 174 | FieldHelper fh(field); |
| 175 | CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass())); |
| 176 | return; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this) |
| 182 | << " of type " << PrettyDescriptor(c) << " at offset " << field_offset; |
| 183 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 184 | |
| 185 | } // namespace mirror |
| 186 | } // namespace art |