blob: 55632bad55c9c442466898a8b32539511b4c4be6 [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
Elliott Hughesa2501992011-08-26 19:39:54 -070019#include "jni_internal.h"
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -070020#include "nth_caller_visitor.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070021#include "reference_table.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070022#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070023#include "scoped_thread_state_change.h"
Ian Rogers5a7a74a2011-09-26 16:32:29 -070024#include "thread.h"
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070025#include "utils.h"
Mathieu Chartier6dda8982014-03-06 11:11:48 -080026#include "verify_object-inl.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070027
28#include <cstdlib>
29
30namespace art {
31
Ian Rogers719d1a32014-03-06 12:13:39 -080032template<typename T>
33class MutatorLockedDumpable {
34 public:
35 explicit MutatorLockedDumpable(T& value)
36 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : value_(value) {
37 }
38
39 void Dump(std::ostream& os) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
40 value_.Dump(os);
41 }
42
43 private:
44 T& value_;
45
46 DISALLOW_COPY_AND_ASSIGN(MutatorLockedDumpable);
47};
48
49template<typename T>
50std::ostream& operator<<(std::ostream& os, const MutatorLockedDumpable<T>& rhs)
51// TODO: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) however annotalysis
52// currently fails for this.
53 NO_THREAD_SAFETY_ANALYSIS {
54 rhs.Dump(os);
55 return os;
56}
57
Mathieu Chartierc56057e2014-05-04 13:18:58 -070058void IndirectReferenceTable::AbortIfNoCheckJNI() {
Elliott Hughesa2501992011-08-26 19:39:54 -070059 // If -Xcheck:jni is on, it'll give a more detailed error before aborting.
Ian Rogers68d8b422014-07-17 11:09:10 -070060 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
61 if (!vm->IsCheckJniEnabled()) {
Elliott Hughesa2501992011-08-26 19:39:54 -070062 // Otherwise, we want to abort rather than hand back a bad reference.
63 LOG(FATAL) << "JNI ERROR (app bug): see above.";
64 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -070065}
66
67IndirectReferenceTable::IndirectReferenceTable(size_t initialCount,
Andreas Gampe3f5881f2015-04-08 10:26:16 -070068 size_t maxCount, IndirectRefKind desiredKind,
69 bool abort_on_error)
Mathieu Chartier4838d662014-09-25 15:27:43 -070070 : kind_(desiredKind),
71 max_entries_(maxCount) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070072 CHECK_GT(initialCount, 0U);
73 CHECK_LE(initialCount, maxCount);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070074 CHECK_NE(desiredKind, kHandleScopeOrInvalid);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070075
Mathieu Chartierc56057e2014-05-04 13:18:58 -070076 std::string error_str;
Mathieu Chartier4838d662014-09-25 15:27:43 -070077 const size_t table_bytes = maxCount * sizeof(IrtEntry);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070078 table_mem_map_.reset(MemMap::MapAnonymous("indirect ref table", nullptr, table_bytes,
Vladimir Marko5c42c292015-02-25 12:02:49 +000079 PROT_READ | PROT_WRITE, false, false, &error_str));
Andreas Gampe3f5881f2015-04-08 10:26:16 -070080 if (abort_on_error) {
81 CHECK(table_mem_map_.get() != nullptr) << error_str;
82 CHECK_EQ(table_mem_map_->Size(), table_bytes);
83 CHECK(table_mem_map_->Begin() != nullptr);
84 } else if (table_mem_map_.get() == nullptr ||
85 table_mem_map_->Size() != table_bytes ||
86 table_mem_map_->Begin() == nullptr) {
87 table_mem_map_.reset();
88 LOG(ERROR) << error_str;
89 return;
90 }
Mathieu Chartier4838d662014-09-25 15:27:43 -070091 table_ = reinterpret_cast<IrtEntry*>(table_mem_map_->Begin());
Ian Rogersdc51b792011-09-22 20:41:37 -070092 segment_state_.all = IRT_FIRST_SEGMENT;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070093}
94
95IndirectReferenceTable::~IndirectReferenceTable() {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070096}
97
Andreas Gampe3f5881f2015-04-08 10:26:16 -070098bool IndirectReferenceTable::IsValid() const {
99 return table_mem_map_.get() != nullptr;
100}
101
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700102IndirectRef IndirectReferenceTable::Add(uint32_t cookie, mirror::Object* obj) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700103 IRTSegmentState prevState;
104 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700105 size_t topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700106
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700107 CHECK(obj != nullptr);
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800108 VerifyObject(obj);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700109 DCHECK(table_ != nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700110 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700111
Mathieu Chartier4838d662014-09-25 15:27:43 -0700112 if (topIndex == max_entries_) {
113 LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow "
114 << "(max=" << max_entries_ << ")\n"
115 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700116 }
117
Elliott Hughes73e66f72012-05-09 09:34:45 -0700118 // We know there's enough room in the table. Now we just need to find
119 // the right spot. If there's a hole, find it and fill it; otherwise,
120 // add to the end of the list.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700121 IndirectRef result;
Ian Rogersdc51b792011-09-22 20:41:37 -0700122 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Mathieu Chartier4838d662014-09-25 15:27:43 -0700123 size_t index;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700124 if (numHoles > 0) {
125 DCHECK_GT(topIndex, 1U);
Elliott Hughes73e66f72012-05-09 09:34:45 -0700126 // Find the first hole; likely to be near the end of the list.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700127 IrtEntry* pScan = &table_[topIndex - 1];
128 DCHECK(!pScan->GetReference()->IsNull());
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700129 --pScan;
Mathieu Chartier4838d662014-09-25 15:27:43 -0700130 while (!pScan->GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700131 DCHECK_GE(pScan, table_ + prevState.parts.topIndex);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700132 --pScan;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700133 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700134 index = pScan - table_;
Ian Rogersdc51b792011-09-22 20:41:37 -0700135 segment_state_.parts.numHoles--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700136 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700137 // Add to the end.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700138 index = topIndex++;
Ian Rogersdc51b792011-09-22 20:41:37 -0700139 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700140 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700141 table_[index].Add(obj);
142 result = ToIndirectRef(index);
Ian Rogerscf7f1912014-10-22 22:06:39 -0700143 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700144 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex
145 << " holes=" << segment_state_.parts.numHoles;
146 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700147
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700148 DCHECK(result != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700149 return result;
150}
151
Elliott Hughes726079d2011-10-07 18:43:44 -0700152void IndirectReferenceTable::AssertEmpty() {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700153 for (size_t i = 0; i < Capacity(); ++i) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700154 if (!table_[i].GetReference()->IsNull()) {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700155 ScopedObjectAccess soa(Thread::Current());
156 LOG(FATAL) << "Internal Error: non-empty local reference table\n"
157 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
158 }
Elliott Hughes726079d2011-10-07 18:43:44 -0700159 }
160}
161
Elliott Hughes73e66f72012-05-09 09:34:45 -0700162// Removes an object. We extract the table offset bits from "iref"
163// and zap the corresponding entry, leaving a hole if it's not at the top.
164// If the entry is not between the current top index and the bottom index
165// specified by the cookie, we don't remove anything. This is the behavior
166// required by JNI's DeleteLocalRef function.
167// This method is not called when a local frame is popped; this is only used
168// for explicit single removals.
169// Returns "false" if nothing was removed.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700170bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
171 IRTSegmentState prevState;
172 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700173 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700174 int bottomIndex = prevState.parts.topIndex;
175
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700176 DCHECK(table_ != nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700177 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700178
Mathieu Chartierc263bf82015-04-29 09:57:48 -0700179 if (GetIndirectRefKind(iref) == kHandleScopeOrInvalid) {
180 auto* self = Thread::Current();
181 if (self->HandleScopeContains(reinterpret_cast<jobject>(iref))) {
182 auto* env = self->GetJniEnv();
183 DCHECK(env != nullptr);
184 if (env->check_jni) {
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -0700185 ScopedObjectAccess soa(self);
186 LOG(WARNING) << "Attempt to remove non-JNI local reference, dumping thread";
187 self->Dump(LOG(WARNING));
Mathieu Chartierc263bf82015-04-29 09:57:48 -0700188 }
189 return true;
190 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700191 }
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800192 const int idx = ExtractIndex(iref);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700193 if (idx < bottomIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700194 // Wrong segment.
195 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
196 << " vs " << bottomIndex << "-" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700197 return false;
198 }
199 if (idx >= topIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700200 // Bad --- stale reference?
201 LOG(WARNING) << "Attempt to remove invalid index " << idx
202 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700203 return false;
204 }
205
Mathieu Chartier4838d662014-09-25 15:27:43 -0700206 if (idx == topIndex - 1) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700207 // Top-most entry. Scan up and consume holes.
208
Ian Rogers987560f2014-04-22 11:42:59 -0700209 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700210 return false;
211 }
212
Mathieu Chartier4838d662014-09-25 15:27:43 -0700213 *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700214 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700215 if (numHoles != 0) {
216 while (--topIndex > bottomIndex && numHoles != 0) {
Ian Rogerscf7f1912014-10-22 22:06:39 -0700217 if ((false)) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700218 LOG(INFO) << "+++ checking for hole at " << topIndex - 1
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700219 << " (cookie=" << cookie << ") val="
Mathieu Chartier4838d662014-09-25 15:27:43 -0700220 << table_[topIndex - 1].GetReference()->Read<kWithoutReadBarrier>();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700221 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700222 if (!table_[topIndex - 1].GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700223 break;
224 }
Ian Rogerscf7f1912014-10-22 22:06:39 -0700225 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700226 LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
227 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700228 numHoles--;
229 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700230 segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
231 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700232 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700233 segment_state_.parts.topIndex = topIndex-1;
Ian Rogerscf7f1912014-10-22 22:06:39 -0700234 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700235 LOG(INFO) << "+++ ate last entry " << topIndex - 1;
236 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700237 }
238 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700239 // Not the top-most entry. This creates a hole. We null out the entry to prevent somebody
240 // from deleting it twice and screwing up the hole count.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700241 if (table_[idx].GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700242 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
243 return false;
244 }
Ian Rogers987560f2014-04-22 11:42:59 -0700245 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700246 return false;
247 }
248
Mathieu Chartier4838d662014-09-25 15:27:43 -0700249 *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700250 segment_state_.parts.numHoles++;
Ian Rogerscf7f1912014-10-22 22:06:39 -0700251 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700252 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
253 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700254 }
255
256 return true;
257}
258
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800259void IndirectReferenceTable::Trim() {
260 const size_t top_index = Capacity();
261 auto* release_start = AlignUp(reinterpret_cast<uint8_t*>(&table_[top_index]), kPageSize);
262 uint8_t* release_end = table_mem_map_->End();
263 madvise(release_start, release_end - release_start, MADV_DONTNEED);
264}
265
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700266void IndirectReferenceTable::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
Mathieu Chartier4809d0a2015-04-07 10:39:04 -0700267 BufferedRootVisitor<kDefaultBufferedRootCount> root_visitor(visitor, root_info);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700268 for (auto ref : *this) {
Mathieu Chartier9086b652015-04-14 09:35:18 -0700269 if (!ref->IsNull()) {
270 root_visitor.VisitRoot(*ref);
271 DCHECK(!ref->IsNull());
272 }
Elliott Hughes410c0c82011-09-01 17:58:25 -0700273 }
274}
275
Elliott Hughes73e66f72012-05-09 09:34:45 -0700276void IndirectReferenceTable::Dump(std::ostream& os) const {
277 os << kind_ << " table dump:\n";
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700278 ReferenceTable::Table entries;
279 for (size_t i = 0; i < Capacity(); ++i) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700280 mirror::Object* obj = table_[i].GetReference()->Read<kWithoutReadBarrier>();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700281 if (obj != nullptr) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700282 obj = table_[i].GetReference()->Read();
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700283 entries.push_back(GcRoot<mirror::Object>(obj));
Ian Rogers63818dc2012-09-26 12:23:04 -0700284 }
285 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700286 ReferenceTable::Dump(os, entries);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700287}
288
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700289} // namespace art