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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/*
* Copyright (C) 2009 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 "indirect_reference_table-inl.h"
#include "base/bit_utils.h"
#include "base/globals.h"
#include "base/mutator_locked_dumpable.h"
#include "base/systrace.h"
#include "base/utils.h"
#include "indirect_reference_table.h"
#include "jni/java_vm_ext.h"
#include "jni/jni_internal.h"
#include "mirror/object-inl.h"
#include "nth_caller_visitor.h"
#include "object_callbacks.h"
#include "reference_table.h"
#include "runtime-inl.h"
#include "scoped_thread_state_change-inl.h"
#include "thread.h"
#include <cstdlib>
namespace art {
static constexpr bool kDebugIRT = false;
// Maximum table size we allow.
static constexpr size_t kMaxTableSizeInBytes = 128 * MB;
const char* GetIndirectRefKindString(IndirectRefKind kind) {
switch (kind) {
case kJniTransition:
return "JniTransition";
case kLocal:
return "Local";
case kGlobal:
return "Global";
case kWeakGlobal:
return "WeakGlobal";
}
return "IndirectRefKind Error";
}
void IndirectReferenceTable::AbortIfNoCheckJNI(const std::string& msg) {
// If -Xcheck:jni is on, it'll give a more detailed error before aborting.
JavaVMExt* vm = Runtime::Current()->GetJavaVM();
if (!vm->IsCheckJniEnabled()) {
// Otherwise, we want to abort rather than hand back a bad reference.
LOG(FATAL) << msg;
} else {
LOG(ERROR) << msg;
}
}
// Mmap an "indirect ref table region. Table_bytes is a multiple of a page size.
static inline MemMap NewIRTMap(size_t table_bytes, std::string* error_msg) {
MemMap result = MemMap::MapAnonymous("indirect ref table",
table_bytes,
PROT_READ | PROT_WRITE,
/*low_4gb=*/ false,
error_msg);
if (!result.IsValid() && error_msg->empty()) {
*error_msg = "Unable to map memory for indirect ref table";
}
return result;
}
IndirectReferenceTable::IndirectReferenceTable(IndirectRefKind kind)
: table_mem_map_(),
table_(nullptr),
kind_(kind),
top_index_(0u),
max_entries_(0u),
current_num_holes_(0) {
CHECK_NE(kind, kJniTransition);
CHECK_NE(kind, kLocal);
}
bool IndirectReferenceTable::Initialize(size_t max_count, std::string* error_msg) {
CHECK(error_msg != nullptr);
// Overflow and maximum check.
CHECK_LE(max_count, kMaxTableSizeInBytes / sizeof(IrtEntry));
const size_t table_bytes = RoundUp(max_count * sizeof(IrtEntry), kPageSize);
table_mem_map_ = NewIRTMap(table_bytes, error_msg);
if (!table_mem_map_.IsValid()) {
DCHECK(!error_msg->empty());
return false;
}
table_ = reinterpret_cast<IrtEntry*>(table_mem_map_.Begin());
// Take into account the actual length.
max_entries_ = table_bytes / sizeof(IrtEntry);
return true;
}
IndirectReferenceTable::~IndirectReferenceTable() {
}
void IndirectReferenceTable::ConstexprChecks() {
// Use this for some assertions. They can't be put into the header as C++ wants the class
// to be complete.
// Check kind.
static_assert((EncodeIndirectRefKind(kLocal) & (~kKindMask)) == 0, "Kind encoding error");
static_assert((EncodeIndirectRefKind(kGlobal) & (~kKindMask)) == 0, "Kind encoding error");
static_assert((EncodeIndirectRefKind(kWeakGlobal) & (~kKindMask)) == 0, "Kind encoding error");
static_assert(DecodeIndirectRefKind(EncodeIndirectRefKind(kLocal)) == kLocal,
"Kind encoding error");
static_assert(DecodeIndirectRefKind(EncodeIndirectRefKind(kGlobal)) == kGlobal,
"Kind encoding error");
static_assert(DecodeIndirectRefKind(EncodeIndirectRefKind(kWeakGlobal)) == kWeakGlobal,
"Kind encoding error");
// Check serial.
static_assert(DecodeSerial(EncodeSerial(0u)) == 0u, "Serial encoding error");
static_assert(DecodeSerial(EncodeSerial(1u)) == 1u, "Serial encoding error");
static_assert(DecodeSerial(EncodeSerial(2u)) == 2u, "Serial encoding error");
static_assert(DecodeSerial(EncodeSerial(3u)) == 3u, "Serial encoding error");
// Table index.
static_assert(DecodeIndex(EncodeIndex(0u)) == 0u, "Index encoding error");
static_assert(DecodeIndex(EncodeIndex(1u)) == 1u, "Index encoding error");
static_assert(DecodeIndex(EncodeIndex(2u)) == 2u, "Index encoding error");
static_assert(DecodeIndex(EncodeIndex(3u)) == 3u, "Index encoding error");
// Distinguishing between local and (weak) global references.
static_assert((GetGlobalOrWeakGlobalMask() & EncodeIndirectRefKind(kJniTransition)) == 0u);
static_assert((GetGlobalOrWeakGlobalMask() & EncodeIndirectRefKind(kLocal)) == 0u);
static_assert((GetGlobalOrWeakGlobalMask() & EncodeIndirectRefKind(kGlobal)) != 0u);
static_assert((GetGlobalOrWeakGlobalMask() & EncodeIndirectRefKind(kWeakGlobal)) != 0u);
}
// Holes:
//
// To keep the IRT compact, we want to fill "holes" created by non-stack-discipline Add & Remove
// operation sequences. For simplicity and lower memory overhead, we do not use a free list or
// similar. Instead, we scan for holes, with the expectation that we will find holes fast as they
// are usually near the end of the table (see the header, TODO: verify this assumption). To avoid
// scans when there are no holes, the number of known holes should be tracked.
static size_t CountNullEntries(const IrtEntry* table, size_t to) {
size_t count = 0;
for (size_t index = 0u; index != to; ++index) {
if (table[index].GetReference()->IsNull()) {
count++;
}
}
return count;
}
ALWAYS_INLINE
static inline void CheckHoleCount(IrtEntry* table,
size_t exp_num_holes,
size_t top_index) {
if (kIsDebugBuild) {
size_t count = CountNullEntries(table, top_index);
CHECK_EQ(exp_num_holes, count) << " topIndex=" << top_index;
}
}
IndirectRef IndirectReferenceTable::Add(ObjPtr<mirror::Object> obj, std::string* error_msg) {
if (kDebugIRT) {
LOG(INFO) << "+++ Add: top_index=" << top_index_
<< " holes=" << current_num_holes_;
}
CHECK(obj != nullptr);
VerifyObject(obj);
DCHECK(table_ != nullptr);
if (top_index_ == max_entries_) {
// TODO: Fill holes before reporting error.
std::ostringstream oss;
oss << "JNI ERROR (app bug): " << kind_ << " table overflow "
<< "(max=" << max_entries_ << ")"
<< MutatorLockedDumpable<IndirectReferenceTable>(*this);
*error_msg = oss.str();
return nullptr;
}
CheckHoleCount(table_, current_num_holes_, top_index_);
// We know there's enough room in the table. Now we just need to find
// the right spot. If there's a hole, find it and fill it; otherwise,
// add to the end of the list.
IndirectRef result;
size_t index;
if (current_num_holes_ > 0) {
DCHECK_GT(top_index_, 1U);
// Find the first hole; likely to be near the end of the list.
IrtEntry* p_scan = &table_[top_index_ - 1];
DCHECK(!p_scan->GetReference()->IsNull());
--p_scan;
while (!p_scan->GetReference()->IsNull()) {
DCHECK_GT(p_scan, table_);
--p_scan;
}
index = p_scan - table_;
current_num_holes_--;
} else {
// Add to the end.
index = top_index_;
++top_index_;
}
table_[index].Add(obj);
result = ToIndirectRef(index);
if (kDebugIRT) {
LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << top_index_
<< " holes=" << current_num_holes_;
}
DCHECK(result != nullptr);
return result;
}
// Removes an object. We extract the table offset bits from "iref"
// and zap the corresponding entry, leaving a hole if it's not at the top.
// Returns "false" if nothing was removed.
bool IndirectReferenceTable::Remove(IndirectRef iref) {
if (kDebugIRT) {
LOG(INFO) << "+++ Remove: top_index=" << top_index_
<< " holes=" << current_num_holes_;
}
// TODO: We should eagerly check the ref kind against the `kind_` instead of postponing until
// `CheckEntry()` below. Passing the wrong kind shall currently result in misleading warnings.
const uint32_t top_index = top_index_;
DCHECK(table_ != nullptr);
const uint32_t idx = ExtractIndex(iref);
if (idx >= top_index) {
// Bad --- stale reference?
LOG(WARNING) << "Attempt to remove invalid index " << idx
<< " (top=" << top_index << ")";
return false;
}
CheckHoleCount(table_, current_num_holes_, top_index_);
if (idx == top_index - 1) {
// Top-most entry. Scan up and consume holes.
if (!CheckEntry("remove", iref, idx)) {
return false;
}
*table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
if (current_num_holes_ != 0) {
uint32_t collapse_top_index = top_index;
while (--collapse_top_index > 0u && current_num_holes_ != 0) {
if (kDebugIRT) {
ScopedObjectAccess soa(Thread::Current());
LOG(INFO) << "+++ checking for hole at " << collapse_top_index - 1 << " val="
<< table_[collapse_top_index - 1].GetReference()->Read<kWithoutReadBarrier>();
}
if (!table_[collapse_top_index - 1].GetReference()->IsNull()) {
break;
}
if (kDebugIRT) {
LOG(INFO) << "+++ ate hole at " << (collapse_top_index - 1);
}
current_num_holes_--;
}
top_index_ = collapse_top_index;
CheckHoleCount(table_, current_num_holes_, top_index_);
} else {
top_index_ = top_index - 1;
if (kDebugIRT) {
LOG(INFO) << "+++ ate last entry " << top_index - 1;
}
}
} else {
// Not the top-most entry. This creates a hole. We null out the entry to prevent somebody
// from deleting it twice and screwing up the hole count.
if (table_[idx].GetReference()->IsNull()) {
LOG(INFO) << "--- WEIRD: removing null entry " << idx;
return false;
}
if (!CheckEntry("remove", iref, idx)) {
return false;
}
*table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
current_num_holes_++;
CheckHoleCount(table_, current_num_holes_, top_index_);
if (kDebugIRT) {
LOG(INFO) << "+++ left hole at " << idx << ", holes=" << current_num_holes_;
}
}
return true;
}
void IndirectReferenceTable::Trim() {
ScopedTrace trace(__PRETTY_FUNCTION__);
DCHECK(table_mem_map_.IsValid());
const size_t top_index = Capacity();
uint8_t* release_start = AlignUp(reinterpret_cast<uint8_t*>(&table_[top_index]), kPageSize);
uint8_t* release_end = static_cast<uint8_t*>(table_mem_map_.BaseEnd());
DCHECK_GE(reinterpret_cast<uintptr_t>(release_end), reinterpret_cast<uintptr_t>(release_start));
DCHECK_ALIGNED(release_end, kPageSize);
DCHECK_ALIGNED(release_end - release_start, kPageSize);
if (release_start != release_end) {
madvise(release_start, release_end - release_start, MADV_DONTNEED);
}
}
void IndirectReferenceTable::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
BufferedRootVisitor<kDefaultBufferedRootCount> root_visitor(visitor, root_info);
for (size_t i = 0, capacity = Capacity(); i != capacity; ++i) {
GcRoot<mirror::Object>* ref = table_[i].GetReference();
if (!ref->IsNull()) {
root_visitor.VisitRoot(*ref);
DCHECK(!ref->IsNull());
}
}
}
void IndirectReferenceTable::SweepJniWeakGlobals(IsMarkedVisitor* visitor) {
CHECK_EQ(kind_, kWeakGlobal);
MutexLock mu(Thread::Current(), *Locks::jni_weak_globals_lock_);
Runtime* const runtime = Runtime::Current();
for (size_t i = 0, capacity = Capacity(); i != capacity; ++i) {
GcRoot<mirror::Object>* entry = table_[i].GetReference();
// Need to skip null here to distinguish between null entries and cleared weak ref entries.
if (!entry->IsNull()) {
mirror::Object* obj = entry->Read<kWithoutReadBarrier>();
mirror::Object* new_obj = visitor->IsMarked(obj);
if (new_obj == nullptr) {
new_obj = runtime->GetClearedJniWeakGlobal();
}
*entry = GcRoot<mirror::Object>(new_obj);
}
}
}
void IndirectReferenceTable::Dump(std::ostream& os) const {
os << kind_ << " table dump:\n";
ReferenceTable::Table entries;
for (size_t i = 0; i < Capacity(); ++i) {
ObjPtr<mirror::Object> obj = table_[i].GetReference()->Read<kWithoutReadBarrier>();
if (obj != nullptr) {
obj = table_[i].GetReference()->Read();
entries.push_back(GcRoot<mirror::Object>(obj));
}
}
ReferenceTable::Dump(os, entries);
}
size_t IndirectReferenceTable::FreeCapacity() const {
return max_entries_ - top_index_;
}
} // namespace art
|