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
17 #include <ctime>
18
19 #include "object.h"
20
21 #include "array-inl.h"
22 #include "art_field-inl.h"
23 #include "art_field.h"
24 #include "class-inl.h"
25 #include "class.h"
26 #include "class_linker-inl.h"
27 #include "dex/descriptors_names.h"
28 #include "dex/dex_file-inl.h"
29 #include "gc/accounting/card_table-inl.h"
30 #include "gc/heap-inl.h"
31 #include "handle_scope-inl.h"
32 #include "iftable-inl.h"
33 #include "monitor.h"
34 #include "object-inl.h"
35 #include "object-refvisitor-inl.h"
36 #include "object_array-inl.h"
37 #include "runtime.h"
38 #include "throwable.h"
39 #include "well_known_classes.h"
40
41 namespace art {
42 namespace mirror {
43
44 Atomic<uint32_t> Object::hash_code_seed(987654321U + std::time(nullptr));
45
46 class CopyReferenceFieldsWithReadBarrierVisitor {
47 public:
CopyReferenceFieldsWithReadBarrierVisitor(ObjPtr<Object> dest_obj)48 explicit CopyReferenceFieldsWithReadBarrierVisitor(ObjPtr<Object> dest_obj)
49 : dest_obj_(dest_obj) {}
50
operator ()(ObjPtr<Object> obj,MemberOffset offset,bool) const51 void operator()(ObjPtr<Object> obj, MemberOffset offset, bool /* is_static */) const
52 ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
53 // GetFieldObject() contains a RB.
54 ObjPtr<Object> ref = obj->GetFieldObject<Object>(offset);
55 // No WB here as a large object space does not have a card table
56 // coverage. Instead, cards will be marked separately.
57 dest_obj_->SetFieldObjectWithoutWriteBarrier<false, false>(offset, ref);
58 }
59
operator ()(ObjPtr<mirror::Class> klass,ObjPtr<mirror::Reference> ref) const60 void operator()(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref) const
61 ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
62 // Copy java.lang.ref.Reference.referent which isn't visited in
63 // Object::VisitReferences().
64 DCHECK(klass->IsTypeOfReferenceClass());
65 this->operator()(ref, mirror::Reference::ReferentOffset(), false);
66 }
67
68 // Unused since we don't copy class native roots.
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root ATTRIBUTE_UNUSED) const69 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
70 const {}
VisitRoot(mirror::CompressedReference<mirror::Object> * root ATTRIBUTE_UNUSED) const71 void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
72
73 private:
74 const ObjPtr<Object> dest_obj_;
75 };
76
CopyObject(ObjPtr<mirror::Object> dest,ObjPtr<mirror::Object> src,size_t num_bytes)77 ObjPtr<Object> Object::CopyObject(ObjPtr<mirror::Object> dest,
78 ObjPtr<mirror::Object> src,
79 size_t num_bytes) {
80 // Copy instance data. Don't assume memcpy copies by words (b/32012820).
81 {
82 const size_t offset = sizeof(Object);
83 uint8_t* src_bytes = reinterpret_cast<uint8_t*>(src.Ptr()) + offset;
84 uint8_t* dst_bytes = reinterpret_cast<uint8_t*>(dest.Ptr()) + offset;
85 num_bytes -= offset;
86 DCHECK_ALIGNED(src_bytes, sizeof(uintptr_t));
87 DCHECK_ALIGNED(dst_bytes, sizeof(uintptr_t));
88 // Use word sized copies to begin.
89 while (num_bytes >= sizeof(uintptr_t)) {
90 reinterpret_cast<Atomic<uintptr_t>*>(dst_bytes)->store(
91 reinterpret_cast<Atomic<uintptr_t>*>(src_bytes)->load(std::memory_order_relaxed),
92 std::memory_order_relaxed);
93 src_bytes += sizeof(uintptr_t);
94 dst_bytes += sizeof(uintptr_t);
95 num_bytes -= sizeof(uintptr_t);
96 }
97 // Copy possible 32 bit word.
98 if (sizeof(uintptr_t) != sizeof(uint32_t) && num_bytes >= sizeof(uint32_t)) {
99 reinterpret_cast<Atomic<uint32_t>*>(dst_bytes)->store(
100 reinterpret_cast<Atomic<uint32_t>*>(src_bytes)->load(std::memory_order_relaxed),
101 std::memory_order_relaxed);
102 src_bytes += sizeof(uint32_t);
103 dst_bytes += sizeof(uint32_t);
104 num_bytes -= sizeof(uint32_t);
105 }
106 // Copy remaining bytes, avoid going past the end of num_bytes since there may be a redzone
107 // there.
108 while (num_bytes > 0) {
109 reinterpret_cast<Atomic<uint8_t>*>(dst_bytes)->store(
110 reinterpret_cast<Atomic<uint8_t>*>(src_bytes)->load(std::memory_order_relaxed),
111 std::memory_order_relaxed);
112 src_bytes += sizeof(uint8_t);
113 dst_bytes += sizeof(uint8_t);
114 num_bytes -= sizeof(uint8_t);
115 }
116 }
117
118 if (kUseReadBarrier) {
119 // We need a RB here. After copying the whole object above, copy references fields one by one
120 // again with a RB to make sure there are no from space refs. TODO: Optimize this later?
121 CopyReferenceFieldsWithReadBarrierVisitor visitor(dest);
122 src->VisitReferences(visitor, visitor);
123 }
124 // Perform write barriers on copied object references.
125 ObjPtr<Class> c = src->GetClass();
126 if (c->IsArrayClass()) {
127 if (!c->GetComponentType()->IsPrimitive()) {
128 ObjPtr<ObjectArray<Object>> array = dest->AsObjectArray<Object>();
129 WriteBarrier::ForArrayWrite(dest, 0, array->GetLength());
130 }
131 } else {
132 WriteBarrier::ForEveryFieldWrite(dest);
133 }
134 return dest;
135 }
136
137 // An allocation pre-fence visitor that copies the object.
138 class CopyObjectVisitor {
139 public:
CopyObjectVisitor(Handle<Object> * orig,size_t num_bytes)140 CopyObjectVisitor(Handle<Object>* orig, size_t num_bytes)
141 : orig_(orig), num_bytes_(num_bytes) {}
142
operator ()(ObjPtr<Object> obj,size_t usable_size ATTRIBUTE_UNUSED) const143 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
144 REQUIRES_SHARED(Locks::mutator_lock_) {
145 Object::CopyObject(obj, orig_->Get(), num_bytes_);
146 }
147
148 private:
149 Handle<Object>* const orig_;
150 const size_t num_bytes_;
151 DISALLOW_COPY_AND_ASSIGN(CopyObjectVisitor);
152 };
153
Clone(Handle<Object> h_this,Thread * self)154 ObjPtr<Object> Object::Clone(Handle<Object> h_this, Thread* self) {
155 CHECK(!h_this->IsClass()) << "Can't clone classes.";
156 // Object::SizeOf gets the right size even if we're an array. Using c->AllocObject() here would
157 // be wrong.
158 gc::Heap* heap = Runtime::Current()->GetHeap();
159 size_t num_bytes = h_this->SizeOf();
160 CopyObjectVisitor visitor(&h_this, num_bytes);
161 ObjPtr<Object> copy = heap->IsMovableObject(h_this.Get())
162 ? heap->AllocObject(self, h_this->GetClass(), num_bytes, visitor)
163 : heap->AllocNonMovableObject(self, h_this->GetClass(), num_bytes, visitor);
164 if (h_this->GetClass()->IsFinalizable()) {
165 heap->AddFinalizerReference(self, ©);
166 }
167 return copy;
168 }
169
GenerateIdentityHashCode()170 uint32_t Object::GenerateIdentityHashCode() {
171 uint32_t expected_value, new_value;
172 do {
173 expected_value = hash_code_seed.load(std::memory_order_relaxed);
174 new_value = expected_value * 1103515245 + 12345;
175 } while (!hash_code_seed.CompareAndSetWeakRelaxed(expected_value, new_value) ||
176 (expected_value & LockWord::kHashMask) == 0);
177 return expected_value & LockWord::kHashMask;
178 }
179
SetHashCodeSeed(uint32_t new_seed)180 void Object::SetHashCodeSeed(uint32_t new_seed) {
181 hash_code_seed.store(new_seed, std::memory_order_relaxed);
182 }
183
IdentityHashCode()184 int32_t Object::IdentityHashCode() {
185 ObjPtr<Object> current_this = this; // The this pointer may get invalidated by thread suspension.
186 while (true) {
187 LockWord lw = current_this->GetLockWord(false);
188 switch (lw.GetState()) {
189 case LockWord::kUnlocked: {
190 // Try to compare and swap in a new hash, if we succeed we will return the hash on the next
191 // loop iteration.
192 LockWord hash_word = LockWord::FromHashCode(GenerateIdentityHashCode(), lw.GCState());
193 DCHECK_EQ(hash_word.GetState(), LockWord::kHashCode);
194 // Use a strong CAS to prevent spurious failures since these can make the boot image
195 // non-deterministic.
196 if (current_this->CasLockWord(lw, hash_word, CASMode::kStrong, std::memory_order_relaxed)) {
197 return hash_word.GetHashCode();
198 }
199 break;
200 }
201 case LockWord::kThinLocked: {
202 // Inflate the thin lock to a monitor and stick the hash code inside of the monitor. May
203 // fail spuriously.
204 Thread* self = Thread::Current();
205 StackHandleScope<1> hs(self);
206 Handle<mirror::Object> h_this(hs.NewHandle(current_this));
207 Monitor::InflateThinLocked(self, h_this, lw, GenerateIdentityHashCode());
208 // A GC may have occurred when we switched to kBlocked.
209 current_this = h_this.Get();
210 break;
211 }
212 case LockWord::kFatLocked: {
213 // Already inflated, return the hash stored in the monitor.
214 Monitor* monitor = lw.FatLockMonitor();
215 DCHECK(monitor != nullptr);
216 return monitor->GetHashCode();
217 }
218 case LockWord::kHashCode: {
219 return lw.GetHashCode();
220 }
221 default: {
222 LOG(FATAL) << "Invalid state during hashcode " << lw.GetState();
223 UNREACHABLE();
224 }
225 }
226 }
227 }
228
CheckFieldAssignmentImpl(MemberOffset field_offset,ObjPtr<Object> new_value)229 void Object::CheckFieldAssignmentImpl(MemberOffset field_offset, ObjPtr<Object> new_value) {
230 ObjPtr<Class> c = GetClass();
231 Runtime* runtime = Runtime::Current();
232 if (runtime->GetClassLinker() == nullptr || !runtime->IsStarted() ||
233 !runtime->GetHeap()->IsObjectValidationEnabled() || !c->IsResolved()) {
234 return;
235 }
236 for (ObjPtr<Class> cur = c; cur != nullptr; cur = cur->GetSuperClass()) {
237 for (ArtField& field : cur->GetIFields()) {
238 if (field.GetOffset().Int32Value() == field_offset.Int32Value()) {
239 CHECK_NE(field.GetTypeAsPrimitiveType(), Primitive::kPrimNot);
240 // TODO: resolve the field type for moving GC.
241 ObjPtr<mirror::Class> field_type =
242 kMovingCollector ? field.LookupResolvedType() : field.ResolveType();
243 if (field_type != nullptr) {
244 CHECK(field_type->IsAssignableFrom(new_value->GetClass()));
245 }
246 return;
247 }
248 }
249 }
250 if (c->IsArrayClass()) {
251 // Bounds and assign-ability done in the array setter.
252 return;
253 }
254 if (IsClass()) {
255 for (ArtField& field : AsClass()->GetSFields()) {
256 if (field.GetOffset().Int32Value() == field_offset.Int32Value()) {
257 CHECK_NE(field.GetTypeAsPrimitiveType(), Primitive::kPrimNot);
258 // TODO: resolve the field type for moving GC.
259 ObjPtr<mirror::Class> field_type =
260 kMovingCollector ? field.LookupResolvedType() : field.ResolveType();
261 if (field_type != nullptr) {
262 CHECK(field_type->IsAssignableFrom(new_value->GetClass()));
263 }
264 return;
265 }
266 }
267 }
268 LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this)
269 << " of type " << c->PrettyDescriptor() << " at offset " << field_offset;
270 UNREACHABLE();
271 }
272
FindFieldByOffset(MemberOffset offset)273 ArtField* Object::FindFieldByOffset(MemberOffset offset) {
274 return IsClass() ? ArtField::FindStaticFieldWithOffset(AsClass(), offset.Uint32Value())
275 : ArtField::FindInstanceFieldWithOffset(GetClass(), offset.Uint32Value());
276 }
277
PrettyTypeOf(ObjPtr<mirror::Object> obj)278 std::string Object::PrettyTypeOf(ObjPtr<mirror::Object> obj) {
279 return (obj == nullptr) ? "null" : obj->PrettyTypeOf();
280 }
281
PrettyTypeOf()282 std::string Object::PrettyTypeOf() {
283 // From-space version is the same as the to-space version since the dex file never changes.
284 // Avoiding the read barrier here is important to prevent recursive AssertToSpaceInvariant
285 // issues.
286 ObjPtr<mirror::Class> klass = GetClass<kDefaultVerifyFlags, kWithoutReadBarrier>();
287 if (klass == nullptr) {
288 return "(raw)";
289 }
290 std::string temp;
291 std::string result(PrettyDescriptor(klass->GetDescriptor(&temp)));
292 if (klass->IsClassClass()) {
293 result += "<" + PrettyDescriptor(AsClass()->GetDescriptor(&temp)) + ">";
294 }
295 return result;
296 }
297
298 } // namespace mirror
299 } // namespace art
300