blob: 2278408a52e20a2d97f91360084b27979a2cf3dd [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,
Elliott Hughesba8eee12012-01-24 20:25:24 -080067 size_t maxCount, IndirectRefKind desiredKind) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070068 CHECK_GT(initialCount, 0U);
69 CHECK_LE(initialCount, maxCount);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070070 CHECK_NE(desiredKind, kHandleScopeOrInvalid);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070071
Mathieu Chartierc56057e2014-05-04 13:18:58 -070072 std::string error_str;
73 const size_t initial_bytes = initialCount * sizeof(const mirror::Object*);
74 const size_t table_bytes = maxCount * sizeof(const mirror::Object*);
75 table_mem_map_.reset(MemMap::MapAnonymous("indirect ref table", nullptr, table_bytes,
76 PROT_READ | PROT_WRITE, false, &error_str));
77 CHECK(table_mem_map_.get() != nullptr) << error_str;
Alex Lighta59dd802014-07-02 16:28:08 -070078 CHECK_EQ(table_mem_map_->Size(), table_bytes);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070079
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070080 table_ = reinterpret_cast<GcRoot<mirror::Object>*>(table_mem_map_->Begin());
Mathieu Chartierc56057e2014-05-04 13:18:58 -070081 CHECK(table_ != nullptr);
82 memset(table_, 0xd1, initial_bytes);
83
84 const size_t slot_bytes = maxCount * sizeof(IndirectRefSlot);
85 slot_mem_map_.reset(MemMap::MapAnonymous("indirect ref table slots", nullptr, slot_bytes,
86 PROT_READ | PROT_WRITE, false, &error_str));
87 CHECK(slot_mem_map_.get() != nullptr) << error_str;
88 slot_data_ = reinterpret_cast<IndirectRefSlot*>(slot_mem_map_->Begin());
89 CHECK(slot_data_ != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070090
Ian Rogersdc51b792011-09-22 20:41:37 -070091 segment_state_.all = IRT_FIRST_SEGMENT;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070092 alloc_entries_ = initialCount;
93 max_entries_ = maxCount;
94 kind_ = desiredKind;
95}
96
97IndirectReferenceTable::~IndirectReferenceTable() {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070098}
99
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700100IndirectRef IndirectReferenceTable::Add(uint32_t cookie, mirror::Object* obj) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700101 IRTSegmentState prevState;
102 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700103 size_t topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700104
Mathieu Chartiercd2cfff2013-12-19 15:46:55 -0800105 CHECK(obj != NULL);
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800106 VerifyObject(obj);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700107 DCHECK(table_ != NULL);
108 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -0700109 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700110
111 if (topIndex == alloc_entries_) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700112 // reached end of allocated space; did we hit buffer max?
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700113 if (topIndex == max_entries_) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700114 LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow "
115 << "(max=" << max_entries_ << ")\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700117 }
118
119 size_t newSize = alloc_entries_ * 2;
120 if (newSize > max_entries_) {
121 newSize = max_entries_;
122 }
123 DCHECK_GT(newSize, alloc_entries_);
124
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700125 alloc_entries_ = newSize;
126 }
127
Elliott Hughes73e66f72012-05-09 09:34:45 -0700128 // We know there's enough room in the table. Now we just need to find
129 // the right spot. If there's a hole, find it and fill it; otherwise,
130 // add to the end of the list.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700131 IndirectRef result;
Ian Rogersdc51b792011-09-22 20:41:37 -0700132 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700133 if (numHoles > 0) {
134 DCHECK_GT(topIndex, 1U);
Elliott Hughes73e66f72012-05-09 09:34:45 -0700135 // Find the first hole; likely to be near the end of the list.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700136 GcRoot<mirror::Object>* pScan = &table_[topIndex - 1];
137 DCHECK(!pScan->IsNull());
138 --pScan;
139 while (!pScan->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700140 DCHECK_GE(pScan, table_ + prevState.parts.topIndex);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700141 --pScan;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700142 }
143 UpdateSlotAdd(obj, pScan - table_);
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700144 result = ToIndirectRef(pScan - table_);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700145 *pScan = GcRoot<mirror::Object>(obj);
Ian Rogersdc51b792011-09-22 20:41:37 -0700146 segment_state_.parts.numHoles--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700147 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700148 // Add to the end.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700149 UpdateSlotAdd(obj, topIndex);
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700150 result = ToIndirectRef(topIndex);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700151 table_[topIndex++] = GcRoot<mirror::Object>(obj);
Ian Rogersdc51b792011-09-22 20:41:37 -0700152 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700153 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700154 if (false) {
155 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex
156 << " holes=" << segment_state_.parts.numHoles;
157 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700158
159 DCHECK(result != NULL);
160 return result;
161}
162
Elliott Hughes726079d2011-10-07 18:43:44 -0700163void IndirectReferenceTable::AssertEmpty() {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700164 for (size_t i = 0; i < Capacity(); ++i) {
165 if (!table_[i].IsNull()) {
166 ScopedObjectAccess soa(Thread::Current());
167 LOG(FATAL) << "Internal Error: non-empty local reference table\n"
168 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
169 }
Elliott Hughes726079d2011-10-07 18:43:44 -0700170 }
171}
172
Elliott Hughes73e66f72012-05-09 09:34:45 -0700173// Removes an object. We extract the table offset bits from "iref"
174// and zap the corresponding entry, leaving a hole if it's not at the top.
175// If the entry is not between the current top index and the bottom index
176// specified by the cookie, we don't remove anything. This is the behavior
177// required by JNI's DeleteLocalRef function.
178// This method is not called when a local frame is popped; this is only used
179// for explicit single removals.
180// Returns "false" if nothing was removed.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700181bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
182 IRTSegmentState prevState;
183 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700184 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700185 int bottomIndex = prevState.parts.topIndex;
186
187 DCHECK(table_ != NULL);
188 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -0700189 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700190
191 int idx = ExtractIndex(iref);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700192
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700193 if (GetIndirectRefKind(iref) == kHandleScopeOrInvalid &&
194 Thread::Current()->HandleScopeContains(reinterpret_cast<jobject>(iref))) {
195 LOG(WARNING) << "Attempt to remove local handle scope entry from IRT, ignoring";
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700196 return true;
197 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700198
199 if (idx < bottomIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700200 // Wrong segment.
201 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
202 << " vs " << bottomIndex << "-" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700203 return false;
204 }
205 if (idx >= topIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700206 // Bad --- stale reference?
207 LOG(WARNING) << "Attempt to remove invalid index " << idx
208 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700209 return false;
210 }
211
212 if (idx == topIndex-1) {
213 // Top-most entry. Scan up and consume holes.
214
Ian Rogers987560f2014-04-22 11:42:59 -0700215 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700216 return false;
217 }
218
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700219 table_[idx] = GcRoot<mirror::Object>(nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700220 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700221 if (numHoles != 0) {
222 while (--topIndex > bottomIndex && numHoles != 0) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700223 if (false) {
224 LOG(INFO) << "+++ checking for hole at " << topIndex-1
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700225 << " (cookie=" << cookie << ") val="
226 << table_[topIndex - 1].Read<kWithoutReadBarrier>();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700227 }
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700228 if (!table_[topIndex-1].IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700229 break;
230 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700231 if (false) {
232 LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
233 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700234 numHoles--;
235 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700236 segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
237 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700238 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700239 segment_state_.parts.topIndex = topIndex-1;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700240 if (false) {
241 LOG(INFO) << "+++ ate last entry " << topIndex - 1;
242 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700243 }
244 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700245 // Not the top-most entry. This creates a hole. We NULL out the
246 // entry to prevent somebody from deleting it twice and screwing up
247 // the hole count.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700248 if (table_[idx].IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700249 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
250 return false;
251 }
Ian Rogers987560f2014-04-22 11:42:59 -0700252 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700253 return false;
254 }
255
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700256 table_[idx] = GcRoot<mirror::Object>(nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700257 segment_state_.parts.numHoles++;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700258 if (false) {
259 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
260 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700261 }
262
263 return true;
264}
265
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800266void IndirectReferenceTable::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
267 RootType root_type) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700268 for (auto ref : *this) {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700269 if (*ref == nullptr) {
270 // Need to skip null entries to make it possible to do the
271 // non-null check after the call back.
272 continue;
273 }
Mathieu Chartier815873e2014-02-13 18:02:13 -0800274 callback(ref, arg, tid, root_type);
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700275 DCHECK(*ref != nullptr);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700276 }
277}
278
Elliott Hughes73e66f72012-05-09 09:34:45 -0700279void IndirectReferenceTable::Dump(std::ostream& os) const {
280 os << kind_ << " table dump:\n";
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700281 ReferenceTable::Table entries;
282 for (size_t i = 0; i < Capacity(); ++i) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700283 mirror::Object* obj = table_[i].Read<kWithoutReadBarrier>();
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700284 if (UNLIKELY(obj == nullptr)) {
285 // Remove NULLs.
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700286 } else {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700287 obj = table_[i].Read();
288 entries.push_back(GcRoot<mirror::Object>(obj));
Ian Rogers63818dc2012-09-26 12:23:04 -0700289 }
290 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700291 ReferenceTable::Dump(os, entries);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700292}
293
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700294} // namespace art