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