blob: 130c10d3224cac7d27ade4bbaa14b0df1e1d55b3 [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
Mathieu Chartier8778c522016-10-04 19:06:30 -070019#include "base/dumpable-inl.h"
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -080020#include "base/systrace.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070021#include "jni_internal.h"
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -070022#include "nth_caller_visitor.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070023#include "reference_table.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070024#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "scoped_thread_state_change-inl.h"
Ian Rogers5a7a74a2011-09-26 16:32:29 -070026#include "thread.h"
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070027#include "utils.h"
Mathieu Chartier6dda8982014-03-06 11:11:48 -080028#include "verify_object-inl.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;
35
Andreas Gampef1e86302016-10-03 11:42:31 -070036const char* GetIndirectRefKindString(const IndirectRefKind& kind) {
37 switch (kind) {
38 case kHandleScopeOrInvalid:
39 return "HandleScopeOrInvalid";
40 case kLocal:
41 return "Local";
42 case kGlobal:
43 return "Global";
44 case kWeakGlobal:
45 return "WeakGlobal";
46 }
47 return "IndirectRefKind Error";
48}
49
Andreas Gampef1e86302016-10-03 11:42:31 -070050void IndirectReferenceTable::AbortIfNoCheckJNI(const std::string& msg) {
Elliott Hughesa2501992011-08-26 19:39:54 -070051 // If -Xcheck:jni is on, it'll give a more detailed error before aborting.
Ian Rogers68d8b422014-07-17 11:09:10 -070052 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
53 if (!vm->IsCheckJniEnabled()) {
Elliott Hughesa2501992011-08-26 19:39:54 -070054 // Otherwise, we want to abort rather than hand back a bad reference.
Andreas Gampef1e86302016-10-03 11:42:31 -070055 LOG(FATAL) << msg;
56 } else {
57 LOG(ERROR) << msg;
Elliott Hughesa2501992011-08-26 19:39:54 -070058 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -070059}
60
Andreas Gampea8e3b862016-10-17 20:12:52 -070061IndirectReferenceTable::IndirectReferenceTable(size_t max_count,
62 IndirectRefKind desired_kind,
Andreas Gampe3f5881f2015-04-08 10:26:16 -070063 bool abort_on_error)
Andreas Gampea8e3b862016-10-17 20:12:52 -070064 : kind_(desired_kind),
65 max_entries_(max_count) {
66 CHECK_NE(desired_kind, kHandleScopeOrInvalid);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070067
Mathieu Chartierc56057e2014-05-04 13:18:58 -070068 std::string error_str;
Andreas Gampea8e3b862016-10-17 20:12:52 -070069 const size_t table_bytes = max_count * sizeof(IrtEntry);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070070 table_mem_map_.reset(MemMap::MapAnonymous("indirect ref table", nullptr, table_bytes,
Vladimir Marko5c42c292015-02-25 12:02:49 +000071 PROT_READ | PROT_WRITE, false, false, &error_str));
Andreas Gampe3f5881f2015-04-08 10:26:16 -070072 if (abort_on_error) {
73 CHECK(table_mem_map_.get() != nullptr) << error_str;
74 CHECK_EQ(table_mem_map_->Size(), table_bytes);
75 CHECK(table_mem_map_->Begin() != nullptr);
76 } else if (table_mem_map_.get() == nullptr ||
77 table_mem_map_->Size() != table_bytes ||
78 table_mem_map_->Begin() == nullptr) {
79 table_mem_map_.reset();
80 LOG(ERROR) << error_str;
81 return;
82 }
Mathieu Chartier4838d662014-09-25 15:27:43 -070083 table_ = reinterpret_cast<IrtEntry*>(table_mem_map_->Begin());
Ian Rogersdc51b792011-09-22 20:41:37 -070084 segment_state_.all = IRT_FIRST_SEGMENT;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070085}
86
87IndirectReferenceTable::~IndirectReferenceTable() {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070088}
89
Andreas Gampe3f5881f2015-04-08 10:26:16 -070090bool IndirectReferenceTable::IsValid() const {
91 return table_mem_map_.get() != nullptr;
92}
93
Mathieu Chartier8778c522016-10-04 19:06:30 -070094IndirectRef IndirectReferenceTable::Add(uint32_t cookie, ObjPtr<mirror::Object> obj) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070095 IRTSegmentState prevState;
96 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -070097 size_t topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070098
Mathieu Chartier2cebb242015-04-21 16:50:40 -070099 CHECK(obj != nullptr);
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700100 VerifyObject(obj);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700101 DCHECK(table_ != nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700102 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700103
Mathieu Chartier4838d662014-09-25 15:27:43 -0700104 if (topIndex == max_entries_) {
Andreas Gampe90a32b12016-10-03 19:47:08 -0700105 LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow "
106 << "(max=" << max_entries_ << ")\n"
107 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700108 }
109
Elliott Hughes73e66f72012-05-09 09:34:45 -0700110 // We know there's enough room in the table. Now we just need to find
111 // the right spot. If there's a hole, find it and fill it; otherwise,
112 // add to the end of the list.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700113 IndirectRef result;
Ian Rogersdc51b792011-09-22 20:41:37 -0700114 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Mathieu Chartier4838d662014-09-25 15:27:43 -0700115 size_t index;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700116 if (numHoles > 0) {
117 DCHECK_GT(topIndex, 1U);
Elliott Hughes73e66f72012-05-09 09:34:45 -0700118 // Find the first hole; likely to be near the end of the list.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700119 IrtEntry* pScan = &table_[topIndex - 1];
120 DCHECK(!pScan->GetReference()->IsNull());
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700121 --pScan;
Mathieu Chartier4838d662014-09-25 15:27:43 -0700122 while (!pScan->GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700123 DCHECK_GE(pScan, table_ + prevState.parts.topIndex);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700124 --pScan;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700125 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700126 index = pScan - table_;
Ian Rogersdc51b792011-09-22 20:41:37 -0700127 segment_state_.parts.numHoles--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700128 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700129 // Add to the end.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700130 index = topIndex++;
Ian Rogersdc51b792011-09-22 20:41:37 -0700131 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700132 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700133 table_[index].Add(obj);
134 result = ToIndirectRef(index);
Ian Rogerscf7f1912014-10-22 22:06:39 -0700135 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700136 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex
137 << " holes=" << segment_state_.parts.numHoles;
138 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700139
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700140 DCHECK(result != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700141 return result;
142}
143
Elliott Hughes726079d2011-10-07 18:43:44 -0700144void IndirectReferenceTable::AssertEmpty() {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700145 for (size_t i = 0; i < Capacity(); ++i) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700146 if (!table_[i].GetReference()->IsNull()) {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700147 LOG(FATAL) << "Internal Error: non-empty local reference table\n"
148 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Mathieu Chartier8778c522016-10-04 19:06:30 -0700149 UNREACHABLE();
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700150 }
Elliott Hughes726079d2011-10-07 18:43:44 -0700151 }
152}
153
Elliott Hughes73e66f72012-05-09 09:34:45 -0700154// Removes an object. We extract the table offset bits from "iref"
155// and zap the corresponding entry, leaving a hole if it's not at the top.
156// If the entry is not between the current top index and the bottom index
157// specified by the cookie, we don't remove anything. This is the behavior
158// required by JNI's DeleteLocalRef function.
159// This method is not called when a local frame is popped; this is only used
160// for explicit single removals.
161// Returns "false" if nothing was removed.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700162bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
163 IRTSegmentState prevState;
164 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700165 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700166 int bottomIndex = prevState.parts.topIndex;
167
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700168 DCHECK(table_ != nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700169 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700170
Mathieu Chartierc263bf82015-04-29 09:57:48 -0700171 if (GetIndirectRefKind(iref) == kHandleScopeOrInvalid) {
172 auto* self = Thread::Current();
173 if (self->HandleScopeContains(reinterpret_cast<jobject>(iref))) {
174 auto* env = self->GetJniEnv();
175 DCHECK(env != nullptr);
176 if (env->check_jni) {
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -0700177 ScopedObjectAccess soa(self);
178 LOG(WARNING) << "Attempt to remove non-JNI local reference, dumping thread";
Mathieu Chartier2ada67b2015-07-30 11:41:04 -0700179 if (kDumpStackOnNonLocalReference) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700180 self->Dump(LOG_STREAM(WARNING));
Mathieu Chartier2ada67b2015-07-30 11:41:04 -0700181 }
Mathieu Chartierc263bf82015-04-29 09:57:48 -0700182 }
183 return true;
184 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700185 }
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800186 const int idx = ExtractIndex(iref);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700187 if (idx < bottomIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700188 // Wrong segment.
189 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
190 << " vs " << bottomIndex << "-" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700191 return false;
192 }
193 if (idx >= topIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700194 // Bad --- stale reference?
195 LOG(WARNING) << "Attempt to remove invalid index " << idx
196 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700197 return false;
198 }
199
Mathieu Chartier4838d662014-09-25 15:27:43 -0700200 if (idx == topIndex - 1) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700201 // Top-most entry. Scan up and consume holes.
202
Ian Rogers987560f2014-04-22 11:42:59 -0700203 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700204 return false;
205 }
206
Mathieu Chartier4838d662014-09-25 15:27:43 -0700207 *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700208 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700209 if (numHoles != 0) {
210 while (--topIndex > bottomIndex && numHoles != 0) {
Ian Rogerscf7f1912014-10-22 22:06:39 -0700211 if ((false)) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700212 LOG(INFO) << "+++ checking for hole at " << topIndex - 1
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700213 << " (cookie=" << cookie << ") val="
Mathieu Chartier4838d662014-09-25 15:27:43 -0700214 << table_[topIndex - 1].GetReference()->Read<kWithoutReadBarrier>();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700215 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700216 if (!table_[topIndex - 1].GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700217 break;
218 }
Ian Rogerscf7f1912014-10-22 22:06:39 -0700219 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700220 LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
221 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700222 numHoles--;
223 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700224 segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
225 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700226 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700227 segment_state_.parts.topIndex = topIndex-1;
Ian Rogerscf7f1912014-10-22 22:06:39 -0700228 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700229 LOG(INFO) << "+++ ate last entry " << topIndex - 1;
230 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700231 }
232 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700233 // Not the top-most entry. This creates a hole. We null out the entry to prevent somebody
234 // from deleting it twice and screwing up the hole count.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700235 if (table_[idx].GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700236 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
237 return false;
238 }
Ian Rogers987560f2014-04-22 11:42:59 -0700239 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700240 return false;
241 }
242
Mathieu Chartier4838d662014-09-25 15:27:43 -0700243 *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700244 segment_state_.parts.numHoles++;
Ian Rogerscf7f1912014-10-22 22:06:39 -0700245 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700246 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
247 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700248 }
249
250 return true;
251}
252
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800253void IndirectReferenceTable::Trim() {
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -0800254 ScopedTrace trace(__PRETTY_FUNCTION__);
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800255 const size_t top_index = Capacity();
256 auto* release_start = AlignUp(reinterpret_cast<uint8_t*>(&table_[top_index]), kPageSize);
257 uint8_t* release_end = table_mem_map_->End();
258 madvise(release_start, release_end - release_start, MADV_DONTNEED);
259}
260
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700261void IndirectReferenceTable::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
Mathieu Chartier4809d0a2015-04-07 10:39:04 -0700262 BufferedRootVisitor<kDefaultBufferedRootCount> root_visitor(visitor, root_info);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700263 for (auto ref : *this) {
Mathieu Chartier9086b652015-04-14 09:35:18 -0700264 if (!ref->IsNull()) {
265 root_visitor.VisitRoot(*ref);
266 DCHECK(!ref->IsNull());
267 }
Elliott Hughes410c0c82011-09-01 17:58:25 -0700268 }
269}
270
Elliott Hughes73e66f72012-05-09 09:34:45 -0700271void IndirectReferenceTable::Dump(std::ostream& os) const {
272 os << kind_ << " table dump:\n";
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700273 ReferenceTable::Table entries;
274 for (size_t i = 0; i < Capacity(); ++i) {
Mathieu Chartier8778c522016-10-04 19:06:30 -0700275 ObjPtr<mirror::Object> obj = table_[i].GetReference()->Read<kWithoutReadBarrier>();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700276 if (obj != nullptr) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700277 obj = table_[i].GetReference()->Read();
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700278 entries.push_back(GcRoot<mirror::Object>(obj));
Ian Rogers63818dc2012-09-26 12:23:04 -0700279 }
280 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700281 ReferenceTable::Dump(os, entries);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700282}
283
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700284} // namespace art