blob: 8ab4a9b47ea62a02a1a60094333900ab660fe86c [file] [log] [blame]
Elliott Hughes6c1a3942011-08-17 15:00:06 -07001/*
2 * Copyright (C) 2009 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 Chartierc56057e2014-05-04 13:18:58 -070017#include "indirect_reference_table-inl.h"
18
David Sehr1ce2b3b2018-04-05 11:02:03 -070019#include "base/mutator_locked_dumpable.h"
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -080020#include "base/systrace.h"
David Sehrc431b9d2018-03-02 12:01:51 -080021#include "base/utils.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010022#include "jni/java_vm_ext.h"
23#include "jni/jni_internal.h"
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -070024#include "nth_caller_visitor.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070025#include "reference_table.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070026#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070027#include "scoped_thread_state_change-inl.h"
Ian Rogers5a7a74a2011-09-26 16:32:29 -070028#include "thread.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070029
30#include <cstdlib>
31
32namespace art {
33
Mathieu Chartier2ada67b2015-07-30 11:41:04 -070034static constexpr bool kDumpStackOnNonLocalReference = false;
Andreas Gampee03662b2016-10-13 17:12:56 -070035static constexpr bool kDebugIRT = false;
Mathieu Chartier2ada67b2015-07-30 11:41:04 -070036
Andreas Gampe0ece10d2017-05-31 20:09:28 -070037// Maximum table size we allow.
38static constexpr size_t kMaxTableSizeInBytes = 128 * MB;
39
Andreas Gampef1e86302016-10-03 11:42:31 -070040const char* GetIndirectRefKindString(const IndirectRefKind& kind) {
41 switch (kind) {
42 case kHandleScopeOrInvalid:
43 return "HandleScopeOrInvalid";
44 case kLocal:
45 return "Local";
46 case kGlobal:
47 return "Global";
48 case kWeakGlobal:
49 return "WeakGlobal";
50 }
51 return "IndirectRefKind Error";
52}
53
Andreas Gampef1e86302016-10-03 11:42:31 -070054void IndirectReferenceTable::AbortIfNoCheckJNI(const std::string& msg) {
Elliott Hughesa2501992011-08-26 19:39:54 -070055 // If -Xcheck:jni is on, it'll give a more detailed error before aborting.
Ian Rogers68d8b422014-07-17 11:09:10 -070056 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
57 if (!vm->IsCheckJniEnabled()) {
Elliott Hughesa2501992011-08-26 19:39:54 -070058 // Otherwise, we want to abort rather than hand back a bad reference.
Andreas Gampef1e86302016-10-03 11:42:31 -070059 LOG(FATAL) << msg;
60 } else {
61 LOG(ERROR) << msg;
Elliott Hughesa2501992011-08-26 19:39:54 -070062 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -070063}
64
Andreas Gampea8e3b862016-10-17 20:12:52 -070065IndirectReferenceTable::IndirectReferenceTable(size_t max_count,
66 IndirectRefKind desired_kind,
Andreas Gampe9d7ef622016-10-24 19:35:19 -070067 ResizableCapacity resizable,
Richard Uhlerda0a69e2016-10-11 15:06:38 +010068 std::string* error_msg)
Andreas Gampee03662b2016-10-13 17:12:56 -070069 : segment_state_(kIRTFirstSegment),
70 kind_(desired_kind),
71 max_entries_(max_count),
Andreas Gampe9d7ef622016-10-24 19:35:19 -070072 current_num_holes_(0),
73 resizable_(resizable) {
Richard Uhlerda0a69e2016-10-11 15:06:38 +010074 CHECK(error_msg != nullptr);
Andreas Gampea8e3b862016-10-17 20:12:52 -070075 CHECK_NE(desired_kind, kHandleScopeOrInvalid);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070076
Andreas Gampe0ece10d2017-05-31 20:09:28 -070077 // Overflow and maximum check.
78 CHECK_LE(max_count, kMaxTableSizeInBytes / sizeof(IrtEntry));
79
Andreas Gampea8e3b862016-10-17 20:12:52 -070080 const size_t table_bytes = max_count * sizeof(IrtEntry);
Vladimir Markoc34bebf2018-08-16 16:12:49 +010081 table_mem_map_ = MemMap::MapAnonymous("indirect ref table",
82 /* addr */ nullptr,
83 table_bytes,
84 PROT_READ | PROT_WRITE,
85 /* low_4gb */ false,
Vladimir Markoc34bebf2018-08-16 16:12:49 +010086 error_msg);
87 if (!table_mem_map_.IsValid() && error_msg->empty()) {
Richard Uhlerda0a69e2016-10-11 15:06:38 +010088 *error_msg = "Unable to map memory for indirect ref table";
Andreas Gampe3f5881f2015-04-08 10:26:16 -070089 }
Richard Uhlerda0a69e2016-10-11 15:06:38 +010090
Vladimir Markoc34bebf2018-08-16 16:12:49 +010091 if (table_mem_map_.IsValid()) {
92 table_ = reinterpret_cast<IrtEntry*>(table_mem_map_.Begin());
Richard Uhlerda0a69e2016-10-11 15:06:38 +010093 } else {
94 table_ = nullptr;
95 }
Andreas Gampee03662b2016-10-13 17:12:56 -070096 segment_state_ = kIRTFirstSegment;
Andreas Gampe94a52022016-10-25 12:01:48 -070097 last_known_previous_state_ = kIRTFirstSegment;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070098}
99
100IndirectReferenceTable::~IndirectReferenceTable() {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700101}
102
Andreas Gampedc061d02016-10-24 13:19:37 -0700103void IndirectReferenceTable::ConstexprChecks() {
104 // Use this for some assertions. They can't be put into the header as C++ wants the class
105 // to be complete.
106
107 // Check kind.
108 static_assert((EncodeIndirectRefKind(kLocal) & (~kKindMask)) == 0, "Kind encoding error");
109 static_assert((EncodeIndirectRefKind(kGlobal) & (~kKindMask)) == 0, "Kind encoding error");
110 static_assert((EncodeIndirectRefKind(kWeakGlobal) & (~kKindMask)) == 0, "Kind encoding error");
111 static_assert(DecodeIndirectRefKind(EncodeIndirectRefKind(kLocal)) == kLocal,
112 "Kind encoding error");
113 static_assert(DecodeIndirectRefKind(EncodeIndirectRefKind(kGlobal)) == kGlobal,
114 "Kind encoding error");
115 static_assert(DecodeIndirectRefKind(EncodeIndirectRefKind(kWeakGlobal)) == kWeakGlobal,
116 "Kind encoding error");
117
118 // Check serial.
119 static_assert(DecodeSerial(EncodeSerial(0u)) == 0u, "Serial encoding error");
120 static_assert(DecodeSerial(EncodeSerial(1u)) == 1u, "Serial encoding error");
121 static_assert(DecodeSerial(EncodeSerial(2u)) == 2u, "Serial encoding error");
122 static_assert(DecodeSerial(EncodeSerial(3u)) == 3u, "Serial encoding error");
123
124 // Table index.
125 static_assert(DecodeIndex(EncodeIndex(0u)) == 0u, "Index encoding error");
126 static_assert(DecodeIndex(EncodeIndex(1u)) == 1u, "Index encoding error");
127 static_assert(DecodeIndex(EncodeIndex(2u)) == 2u, "Index encoding error");
128 static_assert(DecodeIndex(EncodeIndex(3u)) == 3u, "Index encoding error");
129}
130
Andreas Gampe3f5881f2015-04-08 10:26:16 -0700131bool IndirectReferenceTable::IsValid() const {
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100132 return table_mem_map_.IsValid();
Andreas Gampe3f5881f2015-04-08 10:26:16 -0700133}
134
Andreas Gampee03662b2016-10-13 17:12:56 -0700135// Holes:
136//
137// To keep the IRT compact, we want to fill "holes" created by non-stack-discipline Add & Remove
138// operation sequences. For simplicity and lower memory overhead, we do not use a free list or
139// similar. Instead, we scan for holes, with the expectation that we will find holes fast as they
140// are usually near the end of the table (see the header, TODO: verify this assumption). To avoid
141// scans when there are no holes, the number of known holes should be tracked.
142//
143// A previous implementation stored the top index and the number of holes as the segment state.
144// This constraints the maximum number of references to 16-bit. We want to relax this, as it
145// is easy to require more references (e.g., to list all classes in large applications). Thus,
146// the implicitly stack-stored state, the IRTSegmentState, is only the top index.
147//
148// Thus, hole count is a local property of the current segment, and needs to be recovered when
149// (or after) a frame is pushed or popped. To keep JNI transitions simple (and inlineable), we
150// cannot do work when the segment changes. Thus, Add and Remove need to ensure the current
151// hole count is correct.
152//
153// To be able to detect segment changes, we require an additional local field that can describe
154// the known segment. This is last_known_previous_state_. The requirement will become clear with
155// the following (some non-trivial) cases that have to be supported:
156//
157// 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference
158// 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference
159// 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove
160// reference
161// 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference
162// 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove
163// reference
164//
165// Storing the last known *previous* state (bottom index) allows conservatively detecting all the
166// segment changes above. The condition is simply that the last known state is greater than or
167// equal to the current previous state, and smaller than the current state (top index). The
168// condition is conservative as it adds O(1) overhead to operations on an empty segment.
169
170static size_t CountNullEntries(const IrtEntry* table, size_t from, size_t to) {
171 size_t count = 0;
172 for (size_t index = from; index != to; ++index) {
173 if (table[index].GetReference()->IsNull()) {
174 count++;
175 }
176 }
177 return count;
178}
179
180void IndirectReferenceTable::RecoverHoles(IRTSegmentState prev_state) {
181 if (last_known_previous_state_.top_index >= segment_state_.top_index ||
182 last_known_previous_state_.top_index < prev_state.top_index) {
183 const size_t top_index = segment_state_.top_index;
184 size_t count = CountNullEntries(table_, prev_state.top_index, top_index);
185
186 if (kDebugIRT) {
187 LOG(INFO) << "+++ Recovered holes: "
188 << " Current prev=" << prev_state.top_index
189 << " Current top_index=" << top_index
190 << " Old num_holes=" << current_num_holes_
191 << " New num_holes=" << count;
192 }
193
194 current_num_holes_ = count;
195 last_known_previous_state_ = prev_state;
196 } else if (kDebugIRT) {
197 LOG(INFO) << "No need to recover holes";
198 }
199}
200
201ALWAYS_INLINE
202static inline void CheckHoleCount(IrtEntry* table,
203 size_t exp_num_holes,
204 IRTSegmentState prev_state,
205 IRTSegmentState cur_state) {
206 if (kIsDebugBuild) {
207 size_t count = CountNullEntries(table, prev_state.top_index, cur_state.top_index);
208 CHECK_EQ(exp_num_holes, count) << "prevState=" << prev_state.top_index
209 << " topIndex=" << cur_state.top_index;
210 }
211}
212
213bool IndirectReferenceTable::Resize(size_t new_size, std::string* error_msg) {
214 CHECK_GT(new_size, max_entries_);
215
Andreas Gampe0ece10d2017-05-31 20:09:28 -0700216 constexpr size_t kMaxEntries = kMaxTableSizeInBytes / sizeof(IrtEntry);
217 if (new_size > kMaxEntries) {
218 *error_msg = android::base::StringPrintf("Requested size exceeds maximum: %zu", new_size);
219 return false;
220 }
221 // Note: the above check also ensures that there is no overflow below.
222
Andreas Gampee03662b2016-10-13 17:12:56 -0700223 const size_t table_bytes = new_size * sizeof(IrtEntry);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100224 MemMap new_map = MemMap::MapAnonymous("indirect ref table",
225 /* addr */ nullptr,
226 table_bytes,
227 PROT_READ | PROT_WRITE,
228 /* is_low_4gb */ false,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100229 error_msg);
230 if (!new_map.IsValid()) {
Andreas Gampee03662b2016-10-13 17:12:56 -0700231 return false;
232 }
233
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100234 memcpy(new_map.Begin(), table_mem_map_.Begin(), table_mem_map_.Size());
Andreas Gampee03662b2016-10-13 17:12:56 -0700235 table_mem_map_ = std::move(new_map);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100236 table_ = reinterpret_cast<IrtEntry*>(table_mem_map_.Begin());
Andreas Gampee03662b2016-10-13 17:12:56 -0700237 max_entries_ = new_size;
238
239 return true;
240}
241
242IndirectRef IndirectReferenceTable::Add(IRTSegmentState previous_state,
Andreas Gampe25651122017-09-25 14:50:23 -0700243 ObjPtr<mirror::Object> obj,
244 std::string* error_msg) {
Andreas Gampee03662b2016-10-13 17:12:56 -0700245 if (kDebugIRT) {
246 LOG(INFO) << "+++ Add: previous_state=" << previous_state.top_index
247 << " top_index=" << segment_state_.top_index
248 << " last_known_prev_top_index=" << last_known_previous_state_.top_index
249 << " holes=" << current_num_holes_;
250 }
251
252 size_t top_index = segment_state_.top_index;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700253
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700254 CHECK(obj != nullptr);
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700255 VerifyObject(obj);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700256 DCHECK(table_ != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700257
Andreas Gampee03662b2016-10-13 17:12:56 -0700258 if (top_index == max_entries_) {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700259 if (resizable_ == ResizableCapacity::kNo) {
Andreas Gampe25651122017-09-25 14:50:23 -0700260 std::ostringstream oss;
261 oss << "JNI ERROR (app bug): " << kind_ << " table overflow "
262 << "(max=" << max_entries_ << ")"
263 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
264 *error_msg = oss.str();
265 return nullptr;
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700266 }
267
268 // Try to double space.
Andreas Gampe0ece10d2017-05-31 20:09:28 -0700269 if (std::numeric_limits<size_t>::max() / 2 < max_entries_) {
Andreas Gampe25651122017-09-25 14:50:23 -0700270 std::ostringstream oss;
271 oss << "JNI ERROR (app bug): " << kind_ << " table overflow "
272 << "(max=" << max_entries_ << ")" << std::endl
273 << MutatorLockedDumpable<IndirectReferenceTable>(*this)
274 << " Resizing failed: exceeds size_t";
275 *error_msg = oss.str();
276 return nullptr;
Andreas Gampe0ece10d2017-05-31 20:09:28 -0700277 }
278
Andreas Gampe25651122017-09-25 14:50:23 -0700279 std::string inner_error_msg;
280 if (!Resize(max_entries_ * 2, &inner_error_msg)) {
281 std::ostringstream oss;
282 oss << "JNI ERROR (app bug): " << kind_ << " table overflow "
283 << "(max=" << max_entries_ << ")" << std::endl
284 << MutatorLockedDumpable<IndirectReferenceTable>(*this)
285 << " Resizing failed: " << inner_error_msg;
286 *error_msg = oss.str();
287 return nullptr;
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700288 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700289 }
290
Andreas Gampee03662b2016-10-13 17:12:56 -0700291 RecoverHoles(previous_state);
292 CheckHoleCount(table_, current_num_holes_, previous_state, segment_state_);
293
Elliott Hughes73e66f72012-05-09 09:34:45 -0700294 // We know there's enough room in the table. Now we just need to find
295 // the right spot. If there's a hole, find it and fill it; otherwise,
296 // add to the end of the list.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700297 IndirectRef result;
Mathieu Chartier4838d662014-09-25 15:27:43 -0700298 size_t index;
Andreas Gampee03662b2016-10-13 17:12:56 -0700299 if (current_num_holes_ > 0) {
300 DCHECK_GT(top_index, 1U);
Elliott Hughes73e66f72012-05-09 09:34:45 -0700301 // Find the first hole; likely to be near the end of the list.
Andreas Gampee03662b2016-10-13 17:12:56 -0700302 IrtEntry* p_scan = &table_[top_index - 1];
303 DCHECK(!p_scan->GetReference()->IsNull());
304 --p_scan;
305 while (!p_scan->GetReference()->IsNull()) {
306 DCHECK_GE(p_scan, table_ + previous_state.top_index);
307 --p_scan;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700308 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700309 index = p_scan - table_;
310 current_num_holes_--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700311 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700312 // Add to the end.
Andreas Gampee03662b2016-10-13 17:12:56 -0700313 index = top_index++;
314 segment_state_.top_index = top_index;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700315 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700316 table_[index].Add(obj);
317 result = ToIndirectRef(index);
Andreas Gampee03662b2016-10-13 17:12:56 -0700318 if (kDebugIRT) {
319 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.top_index
320 << " holes=" << current_num_holes_;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700321 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700322
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700323 DCHECK(result != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700324 return result;
325}
326
Elliott Hughes726079d2011-10-07 18:43:44 -0700327void IndirectReferenceTable::AssertEmpty() {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700328 for (size_t i = 0; i < Capacity(); ++i) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700329 if (!table_[i].GetReference()->IsNull()) {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700330 LOG(FATAL) << "Internal Error: non-empty local reference table\n"
331 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Mathieu Chartier8778c522016-10-04 19:06:30 -0700332 UNREACHABLE();
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700333 }
Elliott Hughes726079d2011-10-07 18:43:44 -0700334 }
335}
336
Elliott Hughes73e66f72012-05-09 09:34:45 -0700337// Removes an object. We extract the table offset bits from "iref"
338// and zap the corresponding entry, leaving a hole if it's not at the top.
339// If the entry is not between the current top index and the bottom index
340// specified by the cookie, we don't remove anything. This is the behavior
341// required by JNI's DeleteLocalRef function.
342// This method is not called when a local frame is popped; this is only used
343// for explicit single removals.
344// Returns "false" if nothing was removed.
Andreas Gampee03662b2016-10-13 17:12:56 -0700345bool IndirectReferenceTable::Remove(IRTSegmentState previous_state, IndirectRef iref) {
346 if (kDebugIRT) {
347 LOG(INFO) << "+++ Remove: previous_state=" << previous_state.top_index
348 << " top_index=" << segment_state_.top_index
349 << " last_known_prev_top_index=" << last_known_previous_state_.top_index
350 << " holes=" << current_num_holes_;
351 }
352
353 const uint32_t top_index = segment_state_.top_index;
354 const uint32_t bottom_index = previous_state.top_index;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700355
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700356 DCHECK(table_ != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700357
Mathieu Chartierc263bf82015-04-29 09:57:48 -0700358 if (GetIndirectRefKind(iref) == kHandleScopeOrInvalid) {
359 auto* self = Thread::Current();
360 if (self->HandleScopeContains(reinterpret_cast<jobject>(iref))) {
361 auto* env = self->GetJniEnv();
362 DCHECK(env != nullptr);
Ian Rogers55256cb2017-12-21 17:07:11 -0800363 if (env->IsCheckJniEnabled()) {
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -0700364 ScopedObjectAccess soa(self);
365 LOG(WARNING) << "Attempt to remove non-JNI local reference, dumping thread";
Mathieu Chartier2ada67b2015-07-30 11:41:04 -0700366 if (kDumpStackOnNonLocalReference) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700367 self->Dump(LOG_STREAM(WARNING));
Mathieu Chartier2ada67b2015-07-30 11:41:04 -0700368 }
Mathieu Chartierc263bf82015-04-29 09:57:48 -0700369 }
370 return true;
371 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700372 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700373 const uint32_t idx = ExtractIndex(iref);
374 if (idx < bottom_index) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700375 // Wrong segment.
376 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
Andreas Gampee03662b2016-10-13 17:12:56 -0700377 << " vs " << bottom_index << "-" << top_index << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700378 return false;
379 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700380 if (idx >= top_index) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700381 // Bad --- stale reference?
382 LOG(WARNING) << "Attempt to remove invalid index " << idx
Andreas Gampee03662b2016-10-13 17:12:56 -0700383 << " (bottom=" << bottom_index << " top=" << top_index << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700384 return false;
385 }
386
Andreas Gampee03662b2016-10-13 17:12:56 -0700387 RecoverHoles(previous_state);
388 CheckHoleCount(table_, current_num_holes_, previous_state, segment_state_);
389
390 if (idx == top_index - 1) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700391 // Top-most entry. Scan up and consume holes.
392
Ian Rogers987560f2014-04-22 11:42:59 -0700393 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700394 return false;
395 }
396
Mathieu Chartier4838d662014-09-25 15:27:43 -0700397 *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700398 if (current_num_holes_ != 0) {
399 uint32_t collapse_top_index = top_index;
400 while (--collapse_top_index > bottom_index && current_num_holes_ != 0) {
401 if (kDebugIRT) {
402 ScopedObjectAccess soa(Thread::Current());
403 LOG(INFO) << "+++ checking for hole at " << collapse_top_index - 1
404 << " (previous_state=" << bottom_index << ") val="
405 << table_[collapse_top_index - 1].GetReference()->Read<kWithoutReadBarrier>();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700406 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700407 if (!table_[collapse_top_index - 1].GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700408 break;
409 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700410 if (kDebugIRT) {
411 LOG(INFO) << "+++ ate hole at " << (collapse_top_index - 1);
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700412 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700413 current_num_holes_--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700414 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700415 segment_state_.top_index = collapse_top_index;
416
417 CheckHoleCount(table_, current_num_holes_, previous_state, segment_state_);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700418 } else {
Andreas Gampee03662b2016-10-13 17:12:56 -0700419 segment_state_.top_index = top_index - 1;
420 if (kDebugIRT) {
421 LOG(INFO) << "+++ ate last entry " << top_index - 1;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700422 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700423 }
424 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700425 // Not the top-most entry. This creates a hole. We null out the entry to prevent somebody
426 // from deleting it twice and screwing up the hole count.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700427 if (table_[idx].GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700428 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
429 return false;
430 }
Ian Rogers987560f2014-04-22 11:42:59 -0700431 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700432 return false;
433 }
434
Mathieu Chartier4838d662014-09-25 15:27:43 -0700435 *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700436 current_num_holes_++;
437 CheckHoleCount(table_, current_num_holes_, previous_state, segment_state_);
438 if (kDebugIRT) {
439 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << current_num_holes_;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700440 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700441 }
442
443 return true;
444}
445
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800446void IndirectReferenceTable::Trim() {
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -0800447 ScopedTrace trace(__PRETTY_FUNCTION__);
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800448 const size_t top_index = Capacity();
449 auto* release_start = AlignUp(reinterpret_cast<uint8_t*>(&table_[top_index]), kPageSize);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100450 uint8_t* release_end = table_mem_map_.End();
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800451 madvise(release_start, release_end - release_start, MADV_DONTNEED);
452}
453
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700454void IndirectReferenceTable::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
Mathieu Chartier4809d0a2015-04-07 10:39:04 -0700455 BufferedRootVisitor<kDefaultBufferedRootCount> root_visitor(visitor, root_info);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700456 for (auto ref : *this) {
Mathieu Chartier9086b652015-04-14 09:35:18 -0700457 if (!ref->IsNull()) {
458 root_visitor.VisitRoot(*ref);
459 DCHECK(!ref->IsNull());
460 }
Elliott Hughes410c0c82011-09-01 17:58:25 -0700461 }
462}
463
Elliott Hughes73e66f72012-05-09 09:34:45 -0700464void IndirectReferenceTable::Dump(std::ostream& os) const {
465 os << kind_ << " table dump:\n";
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700466 ReferenceTable::Table entries;
467 for (size_t i = 0; i < Capacity(); ++i) {
Mathieu Chartier8778c522016-10-04 19:06:30 -0700468 ObjPtr<mirror::Object> obj = table_[i].GetReference()->Read<kWithoutReadBarrier>();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700469 if (obj != nullptr) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700470 obj = table_[i].GetReference()->Read();
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700471 entries.push_back(GcRoot<mirror::Object>(obj));
Ian Rogers63818dc2012-09-26 12:23:04 -0700472 }
473 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700474 ReferenceTable::Dump(os, entries);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700475}
476
Andreas Gampee03662b2016-10-13 17:12:56 -0700477void IndirectReferenceTable::SetSegmentState(IRTSegmentState new_state) {
478 if (kDebugIRT) {
479 LOG(INFO) << "Setting segment state: "
480 << segment_state_.top_index
481 << " -> "
482 << new_state.top_index;
483 }
484 segment_state_ = new_state;
485}
486
Andreas Gampe88831082017-05-31 19:46:03 -0700487bool IndirectReferenceTable::EnsureFreeCapacity(size_t free_capacity, std::string* error_msg) {
488 size_t top_index = segment_state_.top_index;
489 if (top_index < max_entries_ && top_index + free_capacity <= max_entries_) {
490 return true;
491 }
492
493 // We're only gonna do a simple best-effort here, ensuring the asked-for capacity at the end.
494 if (resizable_ == ResizableCapacity::kNo) {
495 *error_msg = "Table is not resizable";
496 return false;
497 }
498
499 // Try to increase the table size.
500
501 // Would this overflow?
502 if (std::numeric_limits<size_t>::max() - free_capacity < top_index) {
503 *error_msg = "Cannot resize table, overflow.";
504 return false;
505 }
506
507 if (!Resize(top_index + free_capacity, error_msg)) {
508 LOG(WARNING) << "JNI ERROR: Unable to reserve space in EnsureFreeCapacity (" << free_capacity
509 << "): " << std::endl
510 << MutatorLockedDumpable<IndirectReferenceTable>(*this)
511 << " Resizing failed: " << *error_msg;
512 return false;
513 }
514 return true;
515}
516
Andreas Gampe1b35b462017-09-29 18:52:15 -0700517size_t IndirectReferenceTable::FreeCapacity() const {
Andreas Gampe88831082017-05-31 19:46:03 -0700518 return max_entries_ - segment_state_.top_index;
519}
520
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700521} // namespace art