blob: 1a3f1074e68d5ffa172060c3235c295049785d08 [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"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070020#include "reference_table.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070021#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070022#include "scoped_thread_state_change.h"
Ian Rogers5a7a74a2011-09-26 16:32:29 -070023#include "thread.h"
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070024#include "utils.h"
Mathieu Chartier6dda8982014-03-06 11:11:48 -080025#include "verify_object-inl.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070026
27#include <cstdlib>
28
29namespace art {
30
Ian Rogers719d1a32014-03-06 12:13:39 -080031template<typename T>
32class MutatorLockedDumpable {
33 public:
34 explicit MutatorLockedDumpable(T& value)
35 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : value_(value) {
36 }
37
38 void Dump(std::ostream& os) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
39 value_.Dump(os);
40 }
41
42 private:
43 T& value_;
44
45 DISALLOW_COPY_AND_ASSIGN(MutatorLockedDumpable);
46};
47
48template<typename T>
49std::ostream& operator<<(std::ostream& os, const MutatorLockedDumpable<T>& rhs)
50// TODO: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) however annotalysis
51// currently fails for this.
52 NO_THREAD_SAFETY_ANALYSIS {
53 rhs.Dump(os);
54 return os;
55}
56
Mathieu Chartierc56057e2014-05-04 13:18:58 -070057void IndirectReferenceTable::AbortIfNoCheckJNI() {
Elliott Hughesa2501992011-08-26 19:39:54 -070058 // If -Xcheck:jni is on, it'll give a more detailed error before aborting.
Ian Rogers68d8b422014-07-17 11:09:10 -070059 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
60 if (!vm->IsCheckJniEnabled()) {
Elliott Hughesa2501992011-08-26 19:39:54 -070061 // Otherwise, we want to abort rather than hand back a bad reference.
62 LOG(FATAL) << "JNI ERROR (app bug): see above.";
63 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -070064}
65
66IndirectReferenceTable::IndirectReferenceTable(size_t initialCount,
Mathieu Chartier4838d662014-09-25 15:27:43 -070067 size_t maxCount, IndirectRefKind desiredKind)
68 : kind_(desiredKind),
69 max_entries_(maxCount) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070070 CHECK_GT(initialCount, 0U);
71 CHECK_LE(initialCount, maxCount);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070072 CHECK_NE(desiredKind, kHandleScopeOrInvalid);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070073
Mathieu Chartierc56057e2014-05-04 13:18:58 -070074 std::string error_str;
Mathieu Chartier4838d662014-09-25 15:27:43 -070075 const size_t table_bytes = maxCount * sizeof(IrtEntry);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070076 table_mem_map_.reset(MemMap::MapAnonymous("indirect ref table", nullptr, table_bytes,
Vladimir Marko5c42c292015-02-25 12:02:49 +000077 PROT_READ | PROT_WRITE, false, false, &error_str));
Mathieu Chartierc56057e2014-05-04 13:18:58 -070078 CHECK(table_mem_map_.get() != nullptr) << error_str;
Alex Lighta59dd802014-07-02 16:28:08 -070079 CHECK_EQ(table_mem_map_->Size(), table_bytes);
Mathieu Chartier4838d662014-09-25 15:27:43 -070080 table_ = reinterpret_cast<IrtEntry*>(table_mem_map_->Begin());
Mathieu Chartierc56057e2014-05-04 13:18:58 -070081 CHECK(table_ != nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -070082 segment_state_.all = IRT_FIRST_SEGMENT;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070083}
84
85IndirectReferenceTable::~IndirectReferenceTable() {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070086}
87
Mathieu Chartier423d2a32013-09-12 17:33:56 -070088IndirectRef IndirectReferenceTable::Add(uint32_t cookie, mirror::Object* obj) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070089 IRTSegmentState prevState;
90 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -070091 size_t topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070092
Mathieu Chartiercd2cfff2013-12-19 15:46:55 -080093 CHECK(obj != NULL);
Mathieu Chartier6dda8982014-03-06 11:11:48 -080094 VerifyObject(obj);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070095 DCHECK(table_ != NULL);
Ian Rogersdc51b792011-09-22 20:41:37 -070096 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070097
Mathieu Chartier4838d662014-09-25 15:27:43 -070098 if (topIndex == max_entries_) {
99 LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow "
100 << "(max=" << max_entries_ << ")\n"
101 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700102 }
103
Elliott Hughes73e66f72012-05-09 09:34:45 -0700104 // We know there's enough room in the table. Now we just need to find
105 // the right spot. If there's a hole, find it and fill it; otherwise,
106 // add to the end of the list.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700107 IndirectRef result;
Ian Rogersdc51b792011-09-22 20:41:37 -0700108 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Mathieu Chartier4838d662014-09-25 15:27:43 -0700109 size_t index;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700110 if (numHoles > 0) {
111 DCHECK_GT(topIndex, 1U);
Elliott Hughes73e66f72012-05-09 09:34:45 -0700112 // Find the first hole; likely to be near the end of the list.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700113 IrtEntry* pScan = &table_[topIndex - 1];
114 DCHECK(!pScan->GetReference()->IsNull());
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700115 --pScan;
Mathieu Chartier4838d662014-09-25 15:27:43 -0700116 while (!pScan->GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700117 DCHECK_GE(pScan, table_ + prevState.parts.topIndex);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700118 --pScan;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700119 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700120 index = pScan - table_;
Ian Rogersdc51b792011-09-22 20:41:37 -0700121 segment_state_.parts.numHoles--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700122 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700123 // Add to the end.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700124 index = topIndex++;
Ian Rogersdc51b792011-09-22 20:41:37 -0700125 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700126 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700127 table_[index].Add(obj);
128 result = ToIndirectRef(index);
Ian Rogerscf7f1912014-10-22 22:06:39 -0700129 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700130 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex
131 << " holes=" << segment_state_.parts.numHoles;
132 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700133
134 DCHECK(result != NULL);
135 return result;
136}
137
Elliott Hughes726079d2011-10-07 18:43:44 -0700138void IndirectReferenceTable::AssertEmpty() {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700139 for (size_t i = 0; i < Capacity(); ++i) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700140 if (!table_[i].GetReference()->IsNull()) {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700141 ScopedObjectAccess soa(Thread::Current());
142 LOG(FATAL) << "Internal Error: non-empty local reference table\n"
143 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
144 }
Elliott Hughes726079d2011-10-07 18:43:44 -0700145 }
146}
147
Elliott Hughes73e66f72012-05-09 09:34:45 -0700148// Removes an object. We extract the table offset bits from "iref"
149// and zap the corresponding entry, leaving a hole if it's not at the top.
150// If the entry is not between the current top index and the bottom index
151// specified by the cookie, we don't remove anything. This is the behavior
152// required by JNI's DeleteLocalRef function.
153// This method is not called when a local frame is popped; this is only used
154// for explicit single removals.
155// Returns "false" if nothing was removed.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700156bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
157 IRTSegmentState prevState;
158 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700159 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700160 int bottomIndex = prevState.parts.topIndex;
161
162 DCHECK(table_ != NULL);
Ian Rogersdc51b792011-09-22 20:41:37 -0700163 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700164
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700165 if (GetIndirectRefKind(iref) == kHandleScopeOrInvalid &&
166 Thread::Current()->HandleScopeContains(reinterpret_cast<jobject>(iref))) {
167 LOG(WARNING) << "Attempt to remove local handle scope entry from IRT, ignoring";
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700168 return true;
169 }
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800170 const int idx = ExtractIndex(iref);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700171 if (idx < bottomIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700172 // Wrong segment.
173 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
174 << " vs " << bottomIndex << "-" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700175 return false;
176 }
177 if (idx >= topIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700178 // Bad --- stale reference?
179 LOG(WARNING) << "Attempt to remove invalid index " << idx
180 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700181 return false;
182 }
183
Mathieu Chartier4838d662014-09-25 15:27:43 -0700184 if (idx == topIndex - 1) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700185 // Top-most entry. Scan up and consume holes.
186
Ian Rogers987560f2014-04-22 11:42:59 -0700187 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700188 return false;
189 }
190
Mathieu Chartier4838d662014-09-25 15:27:43 -0700191 *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700192 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700193 if (numHoles != 0) {
194 while (--topIndex > bottomIndex && numHoles != 0) {
Ian Rogerscf7f1912014-10-22 22:06:39 -0700195 if ((false)) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700196 LOG(INFO) << "+++ checking for hole at " << topIndex - 1
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700197 << " (cookie=" << cookie << ") val="
Mathieu Chartier4838d662014-09-25 15:27:43 -0700198 << table_[topIndex - 1].GetReference()->Read<kWithoutReadBarrier>();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700199 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700200 if (!table_[topIndex - 1].GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700201 break;
202 }
Ian Rogerscf7f1912014-10-22 22:06:39 -0700203 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700204 LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
205 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700206 numHoles--;
207 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700208 segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
209 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700210 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700211 segment_state_.parts.topIndex = topIndex-1;
Ian Rogerscf7f1912014-10-22 22:06:39 -0700212 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700213 LOG(INFO) << "+++ ate last entry " << topIndex - 1;
214 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700215 }
216 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700217 // Not the top-most entry. This creates a hole. We NULL out the
218 // entry to prevent somebody from deleting it twice and screwing up
219 // the hole count.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700220 if (table_[idx].GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700221 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
222 return false;
223 }
Ian Rogers987560f2014-04-22 11:42:59 -0700224 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700225 return false;
226 }
227
Mathieu Chartier4838d662014-09-25 15:27:43 -0700228 *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700229 segment_state_.parts.numHoles++;
Ian Rogerscf7f1912014-10-22 22:06:39 -0700230 if ((false)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700231 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
232 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700233 }
234
235 return true;
236}
237
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800238void IndirectReferenceTable::Trim() {
239 const size_t top_index = Capacity();
240 auto* release_start = AlignUp(reinterpret_cast<uint8_t*>(&table_[top_index]), kPageSize);
241 uint8_t* release_end = table_mem_map_->End();
242 madvise(release_start, release_end - release_start, MADV_DONTNEED);
243}
244
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800245void IndirectReferenceTable::VisitRoots(RootCallback* callback, void* arg,
246 const RootInfo& root_info) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700247 for (auto ref : *this) {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700248 if (*ref == nullptr) {
249 // Need to skip null entries to make it possible to do the
250 // non-null check after the call back.
251 continue;
252 }
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800253 callback(ref, arg, root_info);
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700254 DCHECK(*ref != nullptr);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700255 }
256}
257
Elliott Hughes73e66f72012-05-09 09:34:45 -0700258void IndirectReferenceTable::Dump(std::ostream& os) const {
259 os << kind_ << " table dump:\n";
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700260 ReferenceTable::Table entries;
261 for (size_t i = 0; i < Capacity(); ++i) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700262 mirror::Object* obj = table_[i].GetReference()->Read<kWithoutReadBarrier>();
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700263 if (UNLIKELY(obj == nullptr)) {
264 // Remove NULLs.
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700265 } else {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700266 obj = table_[i].GetReference()->Read();
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700267 entries.push_back(GcRoot<mirror::Object>(obj));
Ian Rogers63818dc2012-09-26 12:23:04 -0700268 }
269 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700270 ReferenceTable::Dump(os, entries);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700271}
272
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700273} // namespace art