blob: f334a0553c4d87e6a41cc63a1db2a92833ec2143 [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 Chartierdabdc0f2016-03-04 14:58:03 -080019#include "base/systrace.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070020#include "jni_internal.h"
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -070021#include "nth_caller_visitor.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070022#include "reference_table.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070023#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070024#include "scoped_thread_state_change.h"
Ian Rogers5a7a74a2011-09-26 16:32:29 -070025#include "thread.h"
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070026#include "utils.h"
Mathieu Chartier6dda8982014-03-06 11:11:48 -080027#include "verify_object-inl.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070028
29#include <cstdlib>
30
31namespace art {
32
Mathieu Chartier2ada67b2015-07-30 11:41:04 -070033static constexpr bool kDumpStackOnNonLocalReference = false;
34
Andreas Gampe15b7c902016-10-03 11:42:31 -070035const char* GetIndirectRefKindString(const IndirectRefKind& kind) {
36 switch (kind) {
37 case kHandleScopeOrInvalid:
38 return "HandleScopeOrInvalid";
39 case kLocal:
40 return "Local";
41 case kGlobal:
42 return "Global";
43 case kWeakGlobal:
44 return "WeakGlobal";
45 }
46 return "IndirectRefKind Error";
47}
48
Ian Rogers719d1a32014-03-06 12:13:39 -080049template<typename T>
50class MutatorLockedDumpable {
51 public:
52 explicit MutatorLockedDumpable(T& value)
Mathieu Chartier90443472015-07-16 20:32:27 -070053 SHARED_REQUIRES(Locks::mutator_lock_) : value_(value) {
Ian Rogers719d1a32014-03-06 12:13:39 -080054 }
55
Mathieu Chartier90443472015-07-16 20:32:27 -070056 void Dump(std::ostream& os) const SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers719d1a32014-03-06 12:13:39 -080057 value_.Dump(os);
58 }
59
60 private:
61 T& value_;
62
63 DISALLOW_COPY_AND_ASSIGN(MutatorLockedDumpable);
64};
65
66template<typename T>
67std::ostream& operator<<(std::ostream& os, const MutatorLockedDumpable<T>& rhs)
Mathieu Chartier90443472015-07-16 20:32:27 -070068// TODO: should be SHARED_REQUIRES(Locks::mutator_lock_) however annotalysis
Ian Rogers719d1a32014-03-06 12:13:39 -080069// currently fails for this.
70 NO_THREAD_SAFETY_ANALYSIS {
71 rhs.Dump(os);
72 return os;
73}
74
Andreas Gampe15b7c902016-10-03 11:42:31 -070075void IndirectReferenceTable::AbortIfNoCheckJNI(const std::string& msg) {
Elliott Hughesa2501992011-08-26 19:39:54 -070076 // If -Xcheck:jni is on, it'll give a more detailed error before aborting.
Ian Rogers68d8b422014-07-17 11:09:10 -070077 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
78 if (!vm->IsCheckJniEnabled()) {
Elliott Hughesa2501992011-08-26 19:39:54 -070079 // Otherwise, we want to abort rather than hand back a bad reference.
Andreas Gampe15b7c902016-10-03 11:42:31 -070080 LOG(FATAL) << msg;
81 } else {
82 LOG(ERROR) << msg;
Elliott Hughesa2501992011-08-26 19:39:54 -070083 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -070084}
85
86IndirectReferenceTable::IndirectReferenceTable(size_t initialCount,
Andreas Gampe3f5881f2015-04-08 10:26:16 -070087 size_t maxCount, IndirectRefKind desiredKind,
88 bool abort_on_error)
Mathieu Chartier4838d662014-09-25 15:27:43 -070089 : kind_(desiredKind),
90 max_entries_(maxCount) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070091 CHECK_GT(initialCount, 0U);
92 CHECK_LE(initialCount, maxCount);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070093 CHECK_NE(desiredKind, kHandleScopeOrInvalid);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070094
Mathieu Chartierc56057e2014-05-04 13:18:58 -070095 std::string error_str;
Mathieu Chartier4838d662014-09-25 15:27:43 -070096 const size_t table_bytes = maxCount * sizeof(IrtEntry);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070097 table_mem_map_.reset(MemMap::MapAnonymous("indirect ref table", nullptr, table_bytes,
Vladimir Marko5c42c292015-02-25 12:02:49 +000098 PROT_READ | PROT_WRITE, false, false, &error_str));
Andreas Gampe3f5881f2015-04-08 10:26:16 -070099 if (abort_on_error) {
100 CHECK(table_mem_map_.get() != nullptr) << error_str;
101 CHECK_EQ(table_mem_map_->Size(), table_bytes);
102 CHECK(table_mem_map_->Begin() != nullptr);
103 } else if (table_mem_map_.get() == nullptr ||
104 table_mem_map_->Size() != table_bytes ||
105 table_mem_map_->Begin() == nullptr) {
106 table_mem_map_.reset();
107 LOG(ERROR) << error_str;
108 return;
109 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700110 table_ = reinterpret_cast<IrtEntry*>(table_mem_map_->Begin());
Ian Rogersdc51b792011-09-22 20:41:37 -0700111 segment_state_.all = IRT_FIRST_SEGMENT;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700112}
113
114IndirectReferenceTable::~IndirectReferenceTable() {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700115}
116
Andreas Gampe3f5881f2015-04-08 10:26:16 -0700117bool IndirectReferenceTable::IsValid() const {
118 return table_mem_map_.get() != nullptr;
119}
120
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700121IndirectRef IndirectReferenceTable::Add(uint32_t cookie, mirror::Object* obj) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700122 IRTSegmentState prevState;
123 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700124 size_t topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700125
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700126 CHECK(obj != nullptr);
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800127 VerifyObject(obj);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700128 DCHECK(table_ != nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700129 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700130
Mathieu Chartier4838d662014-09-25 15:27:43 -0700131 if (topIndex == max_entries_) {
Andreas Gampe15b7c902016-10-03 11:42:31 -0700132 std::ostringstream oss;
133 oss << "JNI ERROR (app bug): " << kind_ << " table overflow "
134 << "(max=" << max_entries_ << ")\n"
135 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
136 if (VLOG_IS_ON(jni)) {
137 LOG(FATAL) << oss.str();
138 } else {
139 LOG_FATAL_THIS_THREAD_ONLY(oss.str());
140 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700141 }
142
Elliott Hughes73e66f72012-05-09 09:34:45 -0700143 // We know there's enough room in the table. Now we just need to find
144 // the right spot. If there's a hole, find it and fill it; otherwise,
145 // add to the end of the list.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700146 IndirectRef result;
Ian Rogersdc51b792011-09-22 20:41:37 -0700147 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Mathieu Chartier4838d662014-09-25 15:27:43 -0700148 size_t index;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700149 if (numHoles > 0) {
150 DCHECK_GT(topIndex, 1U);
Elliott Hughes73e66f72012-05-09 09:34:45 -0700151 // Find the first hole; likely to be near the end of the list.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700152 IrtEntry* pScan = &table_[topIndex - 1];
153 DCHECK(!pScan->GetReference()->IsNull());
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700154 --pScan;
Mathieu Chartier4838d662014-09-25 15:27:43 -0700155 while (!pScan->GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700156 DCHECK_GE(pScan, table_ + prevState.parts.topIndex);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700157 --pScan;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700158 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700159 index = pScan - table_;
Ian Rogersdc51b792011-09-22 20:41:37 -0700160 segment_state_.parts.numHoles--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700161 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700162 // Add to the end.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700163 index = topIndex++;
Ian Rogersdc51b792011-09-22 20:41:37 -0700164 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700165 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700166 table_[index].Add(obj);
167 result = ToIndirectRef(index);
Ian Rogerscf7f1912014-10-22 22:06:39 -0700168 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700169 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex
170 << " holes=" << segment_state_.parts.numHoles;
171 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700172
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700173 DCHECK(result != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700174 return result;
175}
176
Elliott Hughes726079d2011-10-07 18:43:44 -0700177void IndirectReferenceTable::AssertEmpty() {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700178 for (size_t i = 0; i < Capacity(); ++i) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700179 if (!table_[i].GetReference()->IsNull()) {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700180 ScopedObjectAccess soa(Thread::Current());
181 LOG(FATAL) << "Internal Error: non-empty local reference table\n"
182 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
183 }
Elliott Hughes726079d2011-10-07 18:43:44 -0700184 }
185}
186
Elliott Hughes73e66f72012-05-09 09:34:45 -0700187// Removes an object. We extract the table offset bits from "iref"
188// and zap the corresponding entry, leaving a hole if it's not at the top.
189// If the entry is not between the current top index and the bottom index
190// specified by the cookie, we don't remove anything. This is the behavior
191// required by JNI's DeleteLocalRef function.
192// This method is not called when a local frame is popped; this is only used
193// for explicit single removals.
194// Returns "false" if nothing was removed.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700195bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
196 IRTSegmentState prevState;
197 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700198 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700199 int bottomIndex = prevState.parts.topIndex;
200
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700201 DCHECK(table_ != nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700202 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700203
Mathieu Chartierc263bf82015-04-29 09:57:48 -0700204 if (GetIndirectRefKind(iref) == kHandleScopeOrInvalid) {
205 auto* self = Thread::Current();
206 if (self->HandleScopeContains(reinterpret_cast<jobject>(iref))) {
207 auto* env = self->GetJniEnv();
208 DCHECK(env != nullptr);
209 if (env->check_jni) {
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -0700210 ScopedObjectAccess soa(self);
211 LOG(WARNING) << "Attempt to remove non-JNI local reference, dumping thread";
Mathieu Chartier2ada67b2015-07-30 11:41:04 -0700212 if (kDumpStackOnNonLocalReference) {
213 self->Dump(LOG(WARNING));
214 }
Mathieu Chartierc263bf82015-04-29 09:57:48 -0700215 }
216 return true;
217 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700218 }
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800219 const int idx = ExtractIndex(iref);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700220 if (idx < bottomIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700221 // Wrong segment.
222 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
223 << " vs " << bottomIndex << "-" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700224 return false;
225 }
226 if (idx >= topIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700227 // Bad --- stale reference?
228 LOG(WARNING) << "Attempt to remove invalid index " << idx
229 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700230 return false;
231 }
232
Mathieu Chartier4838d662014-09-25 15:27:43 -0700233 if (idx == topIndex - 1) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700234 // Top-most entry. Scan up and consume holes.
235
Ian Rogers987560f2014-04-22 11:42:59 -0700236 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700237 return false;
238 }
239
Mathieu Chartier4838d662014-09-25 15:27:43 -0700240 *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700241 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700242 if (numHoles != 0) {
243 while (--topIndex > bottomIndex && numHoles != 0) {
Ian Rogerscf7f1912014-10-22 22:06:39 -0700244 if ((false)) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700245 LOG(INFO) << "+++ checking for hole at " << topIndex - 1
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700246 << " (cookie=" << cookie << ") val="
Mathieu Chartier4838d662014-09-25 15:27:43 -0700247 << table_[topIndex - 1].GetReference()->Read<kWithoutReadBarrier>();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700248 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700249 if (!table_[topIndex - 1].GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700250 break;
251 }
Ian Rogerscf7f1912014-10-22 22:06:39 -0700252 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700253 LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
254 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700255 numHoles--;
256 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700257 segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
258 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700259 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700260 segment_state_.parts.topIndex = topIndex-1;
Ian Rogerscf7f1912014-10-22 22:06:39 -0700261 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700262 LOG(INFO) << "+++ ate last entry " << topIndex - 1;
263 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700264 }
265 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700266 // Not the top-most entry. This creates a hole. We null out the entry to prevent somebody
267 // from deleting it twice and screwing up the hole count.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700268 if (table_[idx].GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700269 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
270 return false;
271 }
Ian Rogers987560f2014-04-22 11:42:59 -0700272 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700273 return false;
274 }
275
Mathieu Chartier4838d662014-09-25 15:27:43 -0700276 *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700277 segment_state_.parts.numHoles++;
Ian Rogerscf7f1912014-10-22 22:06:39 -0700278 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700279 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
280 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700281 }
282
283 return true;
284}
285
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800286void IndirectReferenceTable::Trim() {
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -0800287 ScopedTrace trace(__PRETTY_FUNCTION__);
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800288 const size_t top_index = Capacity();
289 auto* release_start = AlignUp(reinterpret_cast<uint8_t*>(&table_[top_index]), kPageSize);
290 uint8_t* release_end = table_mem_map_->End();
291 madvise(release_start, release_end - release_start, MADV_DONTNEED);
292}
293
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700294void IndirectReferenceTable::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
Mathieu Chartier4809d0a2015-04-07 10:39:04 -0700295 BufferedRootVisitor<kDefaultBufferedRootCount> root_visitor(visitor, root_info);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700296 for (auto ref : *this) {
Mathieu Chartier9086b652015-04-14 09:35:18 -0700297 if (!ref->IsNull()) {
298 root_visitor.VisitRoot(*ref);
299 DCHECK(!ref->IsNull());
300 }
Elliott Hughes410c0c82011-09-01 17:58:25 -0700301 }
302}
303
Elliott Hughes73e66f72012-05-09 09:34:45 -0700304void IndirectReferenceTable::Dump(std::ostream& os) const {
305 os << kind_ << " table dump:\n";
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700306 ReferenceTable::Table entries;
307 for (size_t i = 0; i < Capacity(); ++i) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700308 mirror::Object* obj = table_[i].GetReference()->Read<kWithoutReadBarrier>();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700309 if (obj != nullptr) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700310 obj = table_[i].GetReference()->Read();
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700311 entries.push_back(GcRoot<mirror::Object>(obj));
Ian Rogers63818dc2012-09-26 12:23:04 -0700312 }
313 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700314 ReferenceTable::Dump(os, entries);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700315}
316
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700317} // namespace art