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 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 42 | Object* Object::Clone(Thread* self) { |
Mathieu Chartier | 0732d59 | 2013-11-06 11:02:50 -0800 | [diff] [blame^] | 43 | mirror::Class* c = GetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 44 | DCHECK(!c->IsClassClass()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 45 | // Object::SizeOf gets the right size even if we're an array. |
| 46 | // Using c->AllocObject() here would be wrong. |
| 47 | size_t num_bytes = SizeOf(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 48 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 0732d59 | 2013-11-06 11:02:50 -0800 | [diff] [blame^] | 49 | SirtRef<mirror::Object> sirt_this(self, this); |
| 50 | Object* copy = heap->AllocObject(self, c, num_bytes); |
| 51 | if (UNLIKELY(copy == nullptr)) { |
| 52 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 53 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 54 | // Copy instance data. We assume memcpy copies by words. |
| 55 | // TODO: expose and use move32. |
Mathieu Chartier | 0732d59 | 2013-11-06 11:02:50 -0800 | [diff] [blame^] | 56 | byte* src_bytes = reinterpret_cast<byte*>(sirt_this.get()); |
| 57 | byte* dst_bytes = reinterpret_cast<byte*>(copy); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 58 | size_t offset = sizeof(Object); |
| 59 | memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 60 | // Perform write barriers on copied object references. |
Mathieu Chartier | 0732d59 | 2013-11-06 11:02:50 -0800 | [diff] [blame^] | 61 | c = copy->GetClass(); // Re-read Class in case it moved. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 62 | if (c->IsArrayClass()) { |
| 63 | if (!c->GetComponentType()->IsPrimitive()) { |
| 64 | const ObjectArray<Object>* array = copy->AsObjectArray<Object>(); |
Mathieu Chartier | 0732d59 | 2013-11-06 11:02:50 -0800 | [diff] [blame^] | 65 | heap->WriteBarrierArray(copy, 0, array->GetLength()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 66 | } |
| 67 | } else { |
Mathieu Chartier | 0732d59 | 2013-11-06 11:02:50 -0800 | [diff] [blame^] | 68 | heap->WriteBarrierEveryFieldOf(copy); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 69 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 70 | if (c->IsFinalizable()) { |
Mathieu Chartier | 0732d59 | 2013-11-06 11:02:50 -0800 | [diff] [blame^] | 71 | SirtRef<mirror::Object> sirt_copy(self, copy); |
| 72 | heap->AddFinalizerReference(self, copy); |
| 73 | return sirt_copy.get(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 74 | } |
Mathieu Chartier | 0732d59 | 2013-11-06 11:02:50 -0800 | [diff] [blame^] | 75 | return copy; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 78 | int32_t Object::GenerateIdentityHashCode() { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 79 | static AtomicInteger seed(987654321 + std::time(nullptr)); |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 80 | int32_t expected_value, new_value; |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 81 | do { |
| 82 | expected_value = static_cast<uint32_t>(seed.load()); |
| 83 | new_value = expected_value * 1103515245 + 12345; |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 84 | } while ((expected_value & LockWord::kHashMask) == 0 || |
| 85 | !seed.compare_and_swap(expected_value, new_value)); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 86 | return expected_value & LockWord::kHashMask; |
| 87 | } |
| 88 | |
| 89 | int32_t Object::IdentityHashCode() const { |
| 90 | while (true) { |
| 91 | LockWord lw = GetLockWord(); |
| 92 | switch (lw.GetState()) { |
| 93 | case LockWord::kUnlocked: { |
| 94 | // Try to compare and swap in a new hash, if we succeed we will return the hash on the next |
| 95 | // loop iteration. |
| 96 | LockWord hash_word(LockWord::FromHashCode(GenerateIdentityHashCode())); |
| 97 | DCHECK_EQ(hash_word.GetState(), LockWord::kHashCode); |
| 98 | if (const_cast<Object*>(this)->CasLockWord(lw, hash_word)) { |
| 99 | return hash_word.GetHashCode(); |
| 100 | } |
| 101 | break; |
| 102 | } |
| 103 | case LockWord::kThinLocked: { |
| 104 | // Inflate the thin lock to a monitor and stick the hash code inside of the monitor. |
| 105 | Thread* self = Thread::Current(); |
| 106 | Monitor::InflateThinLocked(self, const_cast<Object*>(this), lw, GenerateIdentityHashCode()); |
| 107 | break; |
| 108 | } |
| 109 | case LockWord::kFatLocked: { |
| 110 | // Already inflated, return the has stored in the monitor. |
| 111 | Monitor* monitor = lw.FatLockMonitor(); |
| 112 | DCHECK(monitor != nullptr); |
| 113 | return monitor->GetHashCode(); |
| 114 | } |
| 115 | case LockWord::kHashCode: { |
| 116 | return lw.GetHashCode(); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | LOG(FATAL) << "Unreachable"; |
| 121 | return 0; |
| 122 | } |
| 123 | |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 124 | void Object::CheckFieldAssignmentImpl(MemberOffset field_offset, const Object* new_value) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 125 | const Class* c = GetClass(); |
| 126 | if (Runtime::Current()->GetClassLinker() == NULL || |
| 127 | !Runtime::Current()->GetHeap()->IsObjectValidationEnabled() || |
| 128 | !c->IsResolved()) { |
| 129 | return; |
| 130 | } |
| 131 | for (const Class* cur = c; cur != NULL; cur = cur->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 132 | ObjectArray<ArtField>* fields = cur->GetIFields(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 133 | if (fields != NULL) { |
| 134 | size_t num_ref_ifields = cur->NumReferenceInstanceFields(); |
| 135 | for (size_t i = 0; i < num_ref_ifields; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 136 | ArtField* field = fields->Get(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 137 | if (field->GetOffset().Int32Value() == field_offset.Int32Value()) { |
| 138 | FieldHelper fh(field); |
| 139 | CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass())); |
| 140 | return; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | if (c->IsArrayClass()) { |
| 146 | // Bounds and assign-ability done in the array setter. |
| 147 | return; |
| 148 | } |
| 149 | if (IsClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 150 | ObjectArray<ArtField>* fields = AsClass()->GetSFields(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 151 | if (fields != NULL) { |
| 152 | size_t num_ref_sfields = AsClass()->NumReferenceStaticFields(); |
| 153 | for (size_t i = 0; i < num_ref_sfields; ++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 | LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this) |
| 164 | << " of type " << PrettyDescriptor(c) << " at offset " << field_offset; |
| 165 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 166 | |
| 167 | } // namespace mirror |
| 168 | } // namespace art |