blob: 82b53f6a8902ada59428649879a8014aaee6fd7d [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
17#include "indirect_reference_table.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070018#include "jni_internal.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070019#include "reference_table.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070020#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070021#include "scoped_thread_state_change.h"
Ian Rogers5a7a74a2011-09-26 16:32:29 -070022#include "thread.h"
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070023#include "utils.h"
Mathieu Chartier6dda8982014-03-06 11:11:48 -080024#include "verify_object-inl.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070025
26#include <cstdlib>
27
28namespace art {
29
Elliott Hughes6c1a3942011-08-17 15:00:06 -070030static void AbortMaybe() {
Elliott Hughesa2501992011-08-26 19:39:54 -070031 // If -Xcheck:jni is on, it'll give a more detailed error before aborting.
32 if (!Runtime::Current()->GetJavaVM()->check_jni) {
33 // Otherwise, we want to abort rather than hand back a bad reference.
34 LOG(FATAL) << "JNI ERROR (app bug): see above.";
35 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -070036}
37
38IndirectReferenceTable::IndirectReferenceTable(size_t initialCount,
Elliott Hughesba8eee12012-01-24 20:25:24 -080039 size_t maxCount, IndirectRefKind desiredKind) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070040 CHECK_GT(initialCount, 0U);
41 CHECK_LE(initialCount, maxCount);
Ian Rogers408f79a2011-08-23 18:22:33 -070042 CHECK_NE(desiredKind, kSirtOrInvalid);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070043
Mathieu Chartier423d2a32013-09-12 17:33:56 -070044 table_ = reinterpret_cast<mirror::Object**>(malloc(initialCount * sizeof(const mirror::Object*)));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070045 CHECK(table_ != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046 memset(table_, 0xd1, initialCount * sizeof(const mirror::Object*));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070047
48 slot_data_ = reinterpret_cast<IndirectRefSlot*>(calloc(initialCount, sizeof(IndirectRefSlot)));
49 CHECK(slot_data_ != NULL);
50
Ian Rogersdc51b792011-09-22 20:41:37 -070051 segment_state_.all = IRT_FIRST_SEGMENT;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070052 alloc_entries_ = initialCount;
53 max_entries_ = maxCount;
54 kind_ = desiredKind;
55}
56
57IndirectReferenceTable::~IndirectReferenceTable() {
58 free(table_);
59 free(slot_data_);
60 table_ = NULL;
61 slot_data_ = NULL;
62 alloc_entries_ = max_entries_ = -1;
63}
64
Elliott Hughes73e66f72012-05-09 09:34:45 -070065// Make sure that the entry at "idx" is correctly paired with "iref".
Elliott Hughes6c1a3942011-08-17 15:00:06 -070066bool IndirectReferenceTable::CheckEntry(const char* what, IndirectRef iref, int idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067 const mirror::Object* obj = table_[idx];
Elliott Hughes6c1a3942011-08-17 15:00:06 -070068 IndirectRef checkRef = ToIndirectRef(obj, idx);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070069 if (UNLIKELY(checkRef != iref)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070070 LOG(ERROR) << "JNI ERROR (app bug): attempt to " << what
71 << " stale " << kind_ << " " << iref
72 << " (should be " << checkRef << ")";
73 AbortMaybe();
74 return false;
75 }
76 return true;
77}
78
Mathieu Chartier423d2a32013-09-12 17:33:56 -070079IndirectRef IndirectReferenceTable::Add(uint32_t cookie, mirror::Object* obj) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070080 IRTSegmentState prevState;
81 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -070082 size_t topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070083
Mathieu Chartiercd2cfff2013-12-19 15:46:55 -080084 CHECK(obj != NULL);
Mathieu Chartier6dda8982014-03-06 11:11:48 -080085 VerifyObject(obj);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070086 DCHECK(table_ != NULL);
87 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -070088 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070089
90 if (topIndex == alloc_entries_) {
Elliott Hughes73e66f72012-05-09 09:34:45 -070091 // reached end of allocated space; did we hit buffer max?
Elliott Hughes6c1a3942011-08-17 15:00:06 -070092 if (topIndex == max_entries_) {
Elliott Hughes73e66f72012-05-09 09:34:45 -070093 LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow "
94 << "(max=" << max_entries_ << ")\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070095 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070096 }
97
98 size_t newSize = alloc_entries_ * 2;
99 if (newSize > max_entries_) {
100 newSize = max_entries_;
101 }
102 DCHECK_GT(newSize, alloc_entries_);
103
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700104 table_ = reinterpret_cast<mirror::Object**>(realloc(table_, newSize * sizeof(mirror::Object*)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700105 slot_data_ = reinterpret_cast<IndirectRefSlot*>(realloc(slot_data_,
106 newSize * sizeof(IndirectRefSlot)));
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700107 if (table_ == NULL || slot_data_ == NULL) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700108 LOG(FATAL) << "JNI ERROR (app bug): unable to expand "
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700109 << kind_ << " table (from "
110 << alloc_entries_ << " to " << newSize
Elliott Hughes73e66f72012-05-09 09:34:45 -0700111 << ", max=" << max_entries_ << ")\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700112 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700113 }
114
115 // Clear the newly-allocated slot_data_ elements.
116 memset(slot_data_ + alloc_entries_, 0, (newSize - alloc_entries_) * sizeof(IndirectRefSlot));
117
118 alloc_entries_ = newSize;
119 }
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;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700126 if (numHoles > 0) {
127 DCHECK_GT(topIndex, 1U);
Elliott Hughes73e66f72012-05-09 09:34:45 -0700128 // Find the first hole; likely to be near the end of the list.
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700129 mirror::Object** pScan = &table_[topIndex - 1];
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700130 DCHECK(*pScan != NULL);
131 while (*--pScan != NULL) {
132 DCHECK_GE(pScan, table_ + prevState.parts.topIndex);
133 }
134 UpdateSlotAdd(obj, pScan - table_);
135 result = ToIndirectRef(obj, pScan - table_);
136 *pScan = obj;
Ian Rogersdc51b792011-09-22 20:41:37 -0700137 segment_state_.parts.numHoles--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700138 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700139 // Add to the end.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700140 UpdateSlotAdd(obj, topIndex);
141 result = ToIndirectRef(obj, topIndex);
142 table_[topIndex++] = obj;
Ian Rogersdc51b792011-09-22 20:41:37 -0700143 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700144 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700145 if (false) {
146 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex
147 << " holes=" << segment_state_.parts.numHoles;
148 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700149
150 DCHECK(result != NULL);
151 return result;
152}
153
Elliott Hughes726079d2011-10-07 18:43:44 -0700154void IndirectReferenceTable::AssertEmpty() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700155 if (UNLIKELY(begin() != end())) {
156 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes73e66f72012-05-09 09:34:45 -0700157 LOG(FATAL) << "Internal Error: non-empty local reference table\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700158 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes726079d2011-10-07 18:43:44 -0700159 }
160}
161
Elliott Hughes73e66f72012-05-09 09:34:45 -0700162// Verifies that the indirect table lookup is valid.
163// Returns "false" if something looks bad.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700164bool IndirectReferenceTable::GetChecked(IndirectRef iref) const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700165 if (UNLIKELY(iref == NULL)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700166 LOG(WARNING) << "Attempt to look up NULL " << kind_;
167 return false;
168 }
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700169 if (UNLIKELY(GetIndirectRefKind(iref) == kSirtOrInvalid)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700170 LOG(ERROR) << "JNI ERROR (app bug): invalid " << kind_ << " " << iref;
171 AbortMaybe();
172 return false;
173 }
174
Ian Rogersdc51b792011-09-22 20:41:37 -0700175 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700176 int idx = ExtractIndex(iref);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700177 if (UNLIKELY(idx >= topIndex)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700178 LOG(ERROR) << "JNI ERROR (app bug): accessed stale " << kind_ << " "
179 << iref << " (index " << idx << " in a table of size " << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700180 AbortMaybe();
181 return false;
182 }
183
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700184 if (UNLIKELY(table_[idx] == NULL)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700185 LOG(ERROR) << "JNI ERROR (app bug): accessed deleted " << kind_ << " " << iref;
186 AbortMaybe();
187 return false;
188 }
189
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700190 if (UNLIKELY(!CheckEntry("use", iref, idx))) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700191 return false;
192 }
193
194 return true;
195}
196
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700197static int Find(mirror::Object* direct_pointer, int bottomIndex, int topIndex,
198 mirror::Object** table) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700199 for (int i = bottomIndex; i < topIndex; ++i) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700200 if (table[i] == direct_pointer) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700201 return i;
202 }
203 }
204 return -1;
205}
206
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207bool IndirectReferenceTable::ContainsDirectPointer(mirror::Object* direct_pointer) const {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700208 return Find(direct_pointer, 0, segment_state_.parts.topIndex, table_) != -1;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700209}
210
Elliott Hughes73e66f72012-05-09 09:34:45 -0700211// Removes an object. We extract the table offset bits from "iref"
212// and zap the corresponding entry, leaving a hole if it's not at the top.
213// If the entry is not between the current top index and the bottom index
214// specified by the cookie, we don't remove anything. This is the behavior
215// required by JNI's DeleteLocalRef function.
216// This method is not called when a local frame is popped; this is only used
217// for explicit single removals.
218// Returns "false" if nothing was removed.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700219bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
220 IRTSegmentState prevState;
221 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700222 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700223 int bottomIndex = prevState.parts.topIndex;
224
225 DCHECK(table_ != NULL);
226 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -0700227 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700228
229 int idx = ExtractIndex(iref);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700230
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700231 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700232 if (GetIndirectRefKind(iref) == kSirtOrInvalid &&
Ian Rogers0399dde2012-06-06 17:09:28 -0700233 Thread::Current()->SirtContains(reinterpret_cast<jobject>(iref))) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700234 LOG(WARNING) << "Attempt to remove local SIRT entry from IRT, ignoring";
235 return true;
236 }
Ian Rogers467c9692012-02-21 11:05:16 -0800237 if (GetIndirectRefKind(iref) == kSirtOrInvalid && vm->work_around_app_jni_bugs) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238 mirror::Object* direct_pointer = reinterpret_cast<mirror::Object*>(iref);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700239 idx = Find(direct_pointer, bottomIndex, topIndex, table_);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700240 if (idx == -1) {
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700241 LOG(WARNING) << "Trying to work around app JNI bugs, but didn't find " << iref << " in table!";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700242 return false;
243 }
244 }
245
246 if (idx < bottomIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700247 // Wrong segment.
248 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
249 << " vs " << bottomIndex << "-" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700250 return false;
251 }
252 if (idx >= topIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700253 // Bad --- stale reference?
254 LOG(WARNING) << "Attempt to remove invalid index " << idx
255 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700256 return false;
257 }
258
259 if (idx == topIndex-1) {
260 // Top-most entry. Scan up and consume holes.
261
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700262 if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700263 return false;
264 }
265
266 table_[idx] = NULL;
Ian Rogersdc51b792011-09-22 20:41:37 -0700267 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700268 if (numHoles != 0) {
269 while (--topIndex > bottomIndex && numHoles != 0) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700270 if (false) {
271 LOG(INFO) << "+++ checking for hole at " << topIndex-1
272 << " (cookie=" << cookie << ") val=" << table_[topIndex - 1];
273 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700274 if (table_[topIndex-1] != NULL) {
275 break;
276 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700277 if (false) {
278 LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
279 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700280 numHoles--;
281 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700282 segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
283 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700284 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700285 segment_state_.parts.topIndex = topIndex-1;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700286 if (false) {
287 LOG(INFO) << "+++ ate last entry " << topIndex - 1;
288 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700289 }
290 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700291 // Not the top-most entry. This creates a hole. We NULL out the
292 // entry to prevent somebody from deleting it twice and screwing up
293 // the hole count.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700294 if (table_[idx] == NULL) {
295 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
296 return false;
297 }
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700298 if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700299 return false;
300 }
301
302 table_[idx] = NULL;
Ian Rogersdc51b792011-09-22 20:41:37 -0700303 segment_state_.parts.numHoles++;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700304 if (false) {
305 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
306 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700307 }
308
309 return true;
310}
311
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800312void IndirectReferenceTable::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
313 RootType root_type) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700314 for (auto ref : *this) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800315 callback(ref, arg, tid, root_type);
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700316 DCHECK(*ref != nullptr);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700317 }
318}
319
Elliott Hughes73e66f72012-05-09 09:34:45 -0700320void IndirectReferenceTable::Dump(std::ostream& os) const {
321 os << kind_ << " table dump:\n";
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700322 ReferenceTable::Table entries(table_, table_ + Capacity());
Ian Rogers63818dc2012-09-26 12:23:04 -0700323 // Remove NULLs.
324 for (int i = entries.size() - 1; i >= 0; --i) {
325 if (entries[i] == NULL) {
326 entries.erase(entries.begin() + i);
327 }
328 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700329 ReferenceTable::Dump(os, entries);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700330}
331
332} // namespace art