blob: 8e49492cf40916d11d4e18cbc9b9a5d20e80a6a2 [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
Ian Rogers719d1a32014-03-06 12:13:39 -080035template<typename T>
36class MutatorLockedDumpable {
37 public:
38 explicit MutatorLockedDumpable(T& value)
Mathieu Chartier90443472015-07-16 20:32:27 -070039 SHARED_REQUIRES(Locks::mutator_lock_) : value_(value) {
Ian Rogers719d1a32014-03-06 12:13:39 -080040 }
41
Mathieu Chartier90443472015-07-16 20:32:27 -070042 void Dump(std::ostream& os) const SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers719d1a32014-03-06 12:13:39 -080043 value_.Dump(os);
44 }
45
46 private:
47 T& value_;
48
49 DISALLOW_COPY_AND_ASSIGN(MutatorLockedDumpable);
50};
51
52template<typename T>
53std::ostream& operator<<(std::ostream& os, const MutatorLockedDumpable<T>& rhs)
Mathieu Chartier90443472015-07-16 20:32:27 -070054// TODO: should be SHARED_REQUIRES(Locks::mutator_lock_) however annotalysis
Ian Rogers719d1a32014-03-06 12:13:39 -080055// currently fails for this.
56 NO_THREAD_SAFETY_ANALYSIS {
57 rhs.Dump(os);
58 return os;
59}
60
Mathieu Chartierc56057e2014-05-04 13:18:58 -070061void IndirectReferenceTable::AbortIfNoCheckJNI() {
Elliott Hughesa2501992011-08-26 19:39:54 -070062 // If -Xcheck:jni is on, it'll give a more detailed error before aborting.
Ian Rogers68d8b422014-07-17 11:09:10 -070063 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
64 if (!vm->IsCheckJniEnabled()) {
Elliott Hughesa2501992011-08-26 19:39:54 -070065 // Otherwise, we want to abort rather than hand back a bad reference.
66 LOG(FATAL) << "JNI ERROR (app bug): see above.";
67 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -070068}
69
70IndirectReferenceTable::IndirectReferenceTable(size_t initialCount,
Andreas Gampe3f5881f2015-04-08 10:26:16 -070071 size_t maxCount, IndirectRefKind desiredKind,
72 bool abort_on_error)
Mathieu Chartier4838d662014-09-25 15:27:43 -070073 : kind_(desiredKind),
74 max_entries_(maxCount) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070075 CHECK_GT(initialCount, 0U);
76 CHECK_LE(initialCount, maxCount);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070077 CHECK_NE(desiredKind, kHandleScopeOrInvalid);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070078
Mathieu Chartierc56057e2014-05-04 13:18:58 -070079 std::string error_str;
Mathieu Chartier4838d662014-09-25 15:27:43 -070080 const size_t table_bytes = maxCount * sizeof(IrtEntry);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070081 table_mem_map_.reset(MemMap::MapAnonymous("indirect ref table", nullptr, table_bytes,
Vladimir Marko5c42c292015-02-25 12:02:49 +000082 PROT_READ | PROT_WRITE, false, false, &error_str));
Andreas Gampe3f5881f2015-04-08 10:26:16 -070083 if (abort_on_error) {
84 CHECK(table_mem_map_.get() != nullptr) << error_str;
85 CHECK_EQ(table_mem_map_->Size(), table_bytes);
86 CHECK(table_mem_map_->Begin() != nullptr);
87 } else if (table_mem_map_.get() == nullptr ||
88 table_mem_map_->Size() != table_bytes ||
89 table_mem_map_->Begin() == nullptr) {
90 table_mem_map_.reset();
91 LOG(ERROR) << error_str;
92 return;
93 }
Mathieu Chartier4838d662014-09-25 15:27:43 -070094 table_ = reinterpret_cast<IrtEntry*>(table_mem_map_->Begin());
Ian Rogersdc51b792011-09-22 20:41:37 -070095 segment_state_.all = IRT_FIRST_SEGMENT;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070096}
97
98IndirectReferenceTable::~IndirectReferenceTable() {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070099}
100
Andreas Gampe3f5881f2015-04-08 10:26:16 -0700101bool IndirectReferenceTable::IsValid() const {
102 return table_mem_map_.get() != nullptr;
103}
104
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700105IndirectRef IndirectReferenceTable::Add(uint32_t cookie, mirror::Object* obj) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700106 IRTSegmentState prevState;
107 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700108 size_t topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700109
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700110 CHECK(obj != nullptr);
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800111 VerifyObject(obj);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700112 DCHECK(table_ != nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700113 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700114
Mathieu Chartier4838d662014-09-25 15:27:43 -0700115 if (topIndex == max_entries_) {
116 LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow "
117 << "(max=" << max_entries_ << ")\n"
118 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700119 }
120
Elliott Hughes73e66f72012-05-09 09:34:45 -0700121 // We know there's enough room in the table. Now we just need to find
122 // the right spot. If there's a hole, find it and fill it; otherwise,
123 // add to the end of the list.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700124 IndirectRef result;
Ian Rogersdc51b792011-09-22 20:41:37 -0700125 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Mathieu Chartier4838d662014-09-25 15:27:43 -0700126 size_t index;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700127 if (numHoles > 0) {
128 DCHECK_GT(topIndex, 1U);
Elliott Hughes73e66f72012-05-09 09:34:45 -0700129 // Find the first hole; likely to be near the end of the list.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700130 IrtEntry* pScan = &table_[topIndex - 1];
131 DCHECK(!pScan->GetReference()->IsNull());
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700132 --pScan;
Mathieu Chartier4838d662014-09-25 15:27:43 -0700133 while (!pScan->GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700134 DCHECK_GE(pScan, table_ + prevState.parts.topIndex);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700135 --pScan;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700136 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700137 index = pScan - table_;
Ian Rogersdc51b792011-09-22 20:41:37 -0700138 segment_state_.parts.numHoles--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700139 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700140 // Add to the end.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700141 index = topIndex++;
Ian Rogersdc51b792011-09-22 20:41:37 -0700142 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700143 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700144 table_[index].Add(obj);
145 result = ToIndirectRef(index);
Ian Rogerscf7f1912014-10-22 22:06:39 -0700146 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700147 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex
148 << " holes=" << segment_state_.parts.numHoles;
149 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700150
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700151 DCHECK(result != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700152 return result;
153}
154
Elliott Hughes726079d2011-10-07 18:43:44 -0700155void IndirectReferenceTable::AssertEmpty() {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700156 for (size_t i = 0; i < Capacity(); ++i) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700157 if (!table_[i].GetReference()->IsNull()) {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700158 ScopedObjectAccess soa(Thread::Current());
159 LOG(FATAL) << "Internal Error: non-empty local reference table\n"
160 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
161 }
Elliott Hughes726079d2011-10-07 18:43:44 -0700162 }
163}
164
Elliott Hughes73e66f72012-05-09 09:34:45 -0700165// Removes an object. We extract the table offset bits from "iref"
166// and zap the corresponding entry, leaving a hole if it's not at the top.
167// If the entry is not between the current top index and the bottom index
168// specified by the cookie, we don't remove anything. This is the behavior
169// required by JNI's DeleteLocalRef function.
170// This method is not called when a local frame is popped; this is only used
171// for explicit single removals.
172// Returns "false" if nothing was removed.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700173bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
174 IRTSegmentState prevState;
175 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700176 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700177 int bottomIndex = prevState.parts.topIndex;
178
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700179 DCHECK(table_ != nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700180 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700181
Mathieu Chartierc263bf82015-04-29 09:57:48 -0700182 if (GetIndirectRefKind(iref) == kHandleScopeOrInvalid) {
183 auto* self = Thread::Current();
184 if (self->HandleScopeContains(reinterpret_cast<jobject>(iref))) {
185 auto* env = self->GetJniEnv();
186 DCHECK(env != nullptr);
187 if (env->check_jni) {
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -0700188 ScopedObjectAccess soa(self);
189 LOG(WARNING) << "Attempt to remove non-JNI local reference, dumping thread";
Mathieu Chartier2ada67b2015-07-30 11:41:04 -0700190 if (kDumpStackOnNonLocalReference) {
191 self->Dump(LOG(WARNING));
192 }
Mathieu Chartierc263bf82015-04-29 09:57:48 -0700193 }
194 return true;
195 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700196 }
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800197 const int idx = ExtractIndex(iref);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700198 if (idx < bottomIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700199 // Wrong segment.
200 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
201 << " vs " << bottomIndex << "-" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700202 return false;
203 }
204 if (idx >= topIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700205 // Bad --- stale reference?
206 LOG(WARNING) << "Attempt to remove invalid index " << idx
207 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700208 return false;
209 }
210
Mathieu Chartier4838d662014-09-25 15:27:43 -0700211 if (idx == topIndex - 1) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700212 // Top-most entry. Scan up and consume holes.
213
Ian Rogers987560f2014-04-22 11:42:59 -0700214 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700215 return false;
216 }
217
Mathieu Chartier4838d662014-09-25 15:27:43 -0700218 *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700219 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700220 if (numHoles != 0) {
221 while (--topIndex > bottomIndex && numHoles != 0) {
Ian Rogerscf7f1912014-10-22 22:06:39 -0700222 if ((false)) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700223 LOG(INFO) << "+++ checking for hole at " << topIndex - 1
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700224 << " (cookie=" << cookie << ") val="
Mathieu Chartier4838d662014-09-25 15:27:43 -0700225 << table_[topIndex - 1].GetReference()->Read<kWithoutReadBarrier>();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700226 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700227 if (!table_[topIndex - 1].GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700228 break;
229 }
Ian Rogerscf7f1912014-10-22 22:06:39 -0700230 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700231 LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
232 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700233 numHoles--;
234 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700235 segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
236 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700237 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700238 segment_state_.parts.topIndex = topIndex-1;
Ian Rogerscf7f1912014-10-22 22:06:39 -0700239 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700240 LOG(INFO) << "+++ ate last entry " << topIndex - 1;
241 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700242 }
243 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700244 // Not the top-most entry. This creates a hole. We null out the entry to prevent somebody
245 // from deleting it twice and screwing up the hole count.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700246 if (table_[idx].GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700247 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
248 return false;
249 }
Ian Rogers987560f2014-04-22 11:42:59 -0700250 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700251 return false;
252 }
253
Mathieu Chartier4838d662014-09-25 15:27:43 -0700254 *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700255 segment_state_.parts.numHoles++;
Ian Rogerscf7f1912014-10-22 22:06:39 -0700256 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700257 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
258 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700259 }
260
261 return true;
262}
263
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800264void IndirectReferenceTable::Trim() {
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -0800265 ScopedTrace trace(__PRETTY_FUNCTION__);
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800266 const size_t top_index = Capacity();
267 auto* release_start = AlignUp(reinterpret_cast<uint8_t*>(&table_[top_index]), kPageSize);
268 uint8_t* release_end = table_mem_map_->End();
269 madvise(release_start, release_end - release_start, MADV_DONTNEED);
270}
271
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700272void IndirectReferenceTable::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
Mathieu Chartier4809d0a2015-04-07 10:39:04 -0700273 BufferedRootVisitor<kDefaultBufferedRootCount> root_visitor(visitor, root_info);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700274 for (auto ref : *this) {
Mathieu Chartier9086b652015-04-14 09:35:18 -0700275 if (!ref->IsNull()) {
276 root_visitor.VisitRoot(*ref);
277 DCHECK(!ref->IsNull());
278 }
Elliott Hughes410c0c82011-09-01 17:58:25 -0700279 }
280}
281
Elliott Hughes73e66f72012-05-09 09:34:45 -0700282void IndirectReferenceTable::Dump(std::ostream& os) const {
283 os << kind_ << " table dump:\n";
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700284 ReferenceTable::Table entries;
285 for (size_t i = 0; i < Capacity(); ++i) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700286 mirror::Object* obj = table_[i].GetReference()->Read<kWithoutReadBarrier>();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700287 if (obj != nullptr) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700288 obj = table_[i].GetReference()->Read();
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700289 entries.push_back(GcRoot<mirror::Object>(obj));
Ian Rogers63818dc2012-09-26 12:23:04 -0700290 }
291 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700292 ReferenceTable::Dump(os, entries);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700293}
294
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700295} // namespace art