blob: 8af4d7eaab0403c9ff02f44409fa68863fe5133c [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"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070024
25#include <cstdlib>
26
27namespace art {
28
Elliott Hughes6c1a3942011-08-17 15:00:06 -070029static void AbortMaybe() {
Elliott Hughesa2501992011-08-26 19:39:54 -070030 // If -Xcheck:jni is on, it'll give a more detailed error before aborting.
31 if (!Runtime::Current()->GetJavaVM()->check_jni) {
32 // Otherwise, we want to abort rather than hand back a bad reference.
33 LOG(FATAL) << "JNI ERROR (app bug): see above.";
34 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -070035}
36
37IndirectReferenceTable::IndirectReferenceTable(size_t initialCount,
Elliott Hughesba8eee12012-01-24 20:25:24 -080038 size_t maxCount, IndirectRefKind desiredKind) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070039 CHECK_GT(initialCount, 0U);
40 CHECK_LE(initialCount, maxCount);
Ian Rogers408f79a2011-08-23 18:22:33 -070041 CHECK_NE(desiredKind, kSirtOrInvalid);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070042
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043 table_ = reinterpret_cast<const mirror::Object**>(malloc(initialCount * sizeof(const mirror::Object*)));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070044 CHECK(table_ != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045 memset(table_, 0xd1, initialCount * sizeof(const mirror::Object*));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070046
47 slot_data_ = reinterpret_cast<IndirectRefSlot*>(calloc(initialCount, sizeof(IndirectRefSlot)));
48 CHECK(slot_data_ != NULL);
49
Ian Rogersdc51b792011-09-22 20:41:37 -070050 segment_state_.all = IRT_FIRST_SEGMENT;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070051 alloc_entries_ = initialCount;
52 max_entries_ = maxCount;
53 kind_ = desiredKind;
54}
55
56IndirectReferenceTable::~IndirectReferenceTable() {
57 free(table_);
58 free(slot_data_);
59 table_ = NULL;
60 slot_data_ = NULL;
61 alloc_entries_ = max_entries_ = -1;
62}
63
Elliott Hughes73e66f72012-05-09 09:34:45 -070064// Make sure that the entry at "idx" is correctly paired with "iref".
Elliott Hughes6c1a3942011-08-17 15:00:06 -070065bool IndirectReferenceTable::CheckEntry(const char* what, IndirectRef iref, int idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 const mirror::Object* obj = table_[idx];
Elliott Hughes6c1a3942011-08-17 15:00:06 -070067 IndirectRef checkRef = ToIndirectRef(obj, idx);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070068 if (UNLIKELY(checkRef != iref)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070069 LOG(ERROR) << "JNI ERROR (app bug): attempt to " << what
70 << " stale " << kind_ << " " << iref
71 << " (should be " << checkRef << ")";
72 AbortMaybe();
73 return false;
74 }
75 return true;
76}
77
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078IndirectRef IndirectReferenceTable::Add(uint32_t cookie, const mirror::Object* obj) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070079 IRTSegmentState prevState;
80 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -070081 size_t topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070082
83 DCHECK(obj != NULL);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070084 // TODO: stronger sanity check on the object (such as in heap)
Elliott Hughesd07986f2011-12-06 18:27:45 -080085 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(obj), 8);
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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 table_ = reinterpret_cast<const mirror::Object**>(realloc(table_, newSize * sizeof(const 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.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 const 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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197static int Find(mirror::Object* direct_pointer, int bottomIndex, int topIndex, const mirror::Object** table) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700198 for (int i = bottomIndex; i < topIndex; ++i) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700199 if (table[i] == direct_pointer) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700200 return i;
201 }
202 }
203 return -1;
204}
205
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800206bool IndirectReferenceTable::ContainsDirectPointer(mirror::Object* direct_pointer) const {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700207 return Find(direct_pointer, 0, segment_state_.parts.topIndex, table_) != -1;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700208}
209
Elliott Hughes73e66f72012-05-09 09:34:45 -0700210// Removes an object. We extract the table offset bits from "iref"
211// and zap the corresponding entry, leaving a hole if it's not at the top.
212// If the entry is not between the current top index and the bottom index
213// specified by the cookie, we don't remove anything. This is the behavior
214// required by JNI's DeleteLocalRef function.
215// This method is not called when a local frame is popped; this is only used
216// for explicit single removals.
217// Returns "false" if nothing was removed.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700218bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
219 IRTSegmentState prevState;
220 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700221 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700222 int bottomIndex = prevState.parts.topIndex;
223
224 DCHECK(table_ != NULL);
225 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -0700226 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700227
228 int idx = ExtractIndex(iref);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700229
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700230 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700231 if (GetIndirectRefKind(iref) == kSirtOrInvalid &&
Ian Rogers0399dde2012-06-06 17:09:28 -0700232 Thread::Current()->SirtContains(reinterpret_cast<jobject>(iref))) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700233 LOG(WARNING) << "Attempt to remove local SIRT entry from IRT, ignoring";
234 return true;
235 }
Ian Rogers467c9692012-02-21 11:05:16 -0800236 if (GetIndirectRefKind(iref) == kSirtOrInvalid && vm->work_around_app_jni_bugs) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237 mirror::Object* direct_pointer = reinterpret_cast<mirror::Object*>(iref);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700238 idx = Find(direct_pointer, bottomIndex, topIndex, table_);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700239 if (idx == -1) {
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700240 LOG(WARNING) << "Trying to work around app JNI bugs, but didn't find " << iref << " in table!";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700241 return false;
242 }
243 }
244
245 if (idx < bottomIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700246 // Wrong segment.
247 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
248 << " vs " << bottomIndex << "-" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700249 return false;
250 }
251 if (idx >= topIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700252 // Bad --- stale reference?
253 LOG(WARNING) << "Attempt to remove invalid index " << idx
254 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700255 return false;
256 }
257
258 if (idx == topIndex-1) {
259 // Top-most entry. Scan up and consume holes.
260
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700261 if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700262 return false;
263 }
264
265 table_[idx] = NULL;
Ian Rogersdc51b792011-09-22 20:41:37 -0700266 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700267 if (numHoles != 0) {
268 while (--topIndex > bottomIndex && numHoles != 0) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700269 if (false) {
270 LOG(INFO) << "+++ checking for hole at " << topIndex-1
271 << " (cookie=" << cookie << ") val=" << table_[topIndex - 1];
272 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700273 if (table_[topIndex-1] != NULL) {
274 break;
275 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700276 if (false) {
277 LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
278 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700279 numHoles--;
280 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700281 segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
282 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700283 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700284 segment_state_.parts.topIndex = topIndex-1;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700285 if (false) {
286 LOG(INFO) << "+++ ate last entry " << topIndex - 1;
287 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700288 }
289 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700290 // Not the top-most entry. This creates a hole. We NULL out the
291 // entry to prevent somebody from deleting it twice and screwing up
292 // the hole count.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700293 if (table_[idx] == NULL) {
294 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
295 return false;
296 }
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700297 if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700298 return false;
299 }
300
301 table_[idx] = NULL;
Ian Rogersdc51b792011-09-22 20:41:37 -0700302 segment_state_.parts.numHoles++;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700303 if (false) {
304 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
305 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700306 }
307
308 return true;
309}
310
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800311void IndirectReferenceTable::VisitRoots(RootVisitor* visitor, void* arg) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700312 for (auto ref : *this) {
313 visitor(*ref, arg);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700314 }
315}
316
Elliott Hughes73e66f72012-05-09 09:34:45 -0700317void IndirectReferenceTable::Dump(std::ostream& os) const {
318 os << kind_ << " table dump:\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800319 std::vector<const mirror::Object*> entries(table_, table_ + Capacity());
Ian Rogers63818dc2012-09-26 12:23:04 -0700320 // Remove NULLs.
321 for (int i = entries.size() - 1; i >= 0; --i) {
322 if (entries[i] == NULL) {
323 entries.erase(entries.begin() + i);
324 }
325 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700326 ReferenceTable::Dump(os, entries);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700327}
328
329} // namespace art