blob: 69f171ce862cc8d1d61e7736b7c33c155dc0a018 [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 Rogers5a7a74a2011-09-26 16:32:29 -070021#include "thread.h"
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070022#include "utils.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070023
24#include <cstdlib>
25
26namespace art {
27
Elliott Hughes6c1a3942011-08-17 15:00:06 -070028static void AbortMaybe() {
Elliott Hughesa2501992011-08-26 19:39:54 -070029 // If -Xcheck:jni is on, it'll give a more detailed error before aborting.
30 if (!Runtime::Current()->GetJavaVM()->check_jni) {
31 // Otherwise, we want to abort rather than hand back a bad reference.
32 LOG(FATAL) << "JNI ERROR (app bug): see above.";
33 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -070034}
35
36IndirectReferenceTable::IndirectReferenceTable(size_t initialCount,
Elliott Hughesba8eee12012-01-24 20:25:24 -080037 size_t maxCount, IndirectRefKind desiredKind) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070038 CHECK_GT(initialCount, 0U);
39 CHECK_LE(initialCount, maxCount);
Ian Rogers408f79a2011-08-23 18:22:33 -070040 CHECK_NE(desiredKind, kSirtOrInvalid);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070041
Elliott Hughescf4c6c42011-09-01 15:16:42 -070042 table_ = reinterpret_cast<const Object**>(malloc(initialCount * sizeof(const Object*)));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070043 CHECK(table_ != NULL);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070044 memset(table_, 0xd1, initialCount * sizeof(const Object*));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070045
46 slot_data_ = reinterpret_cast<IndirectRefSlot*>(calloc(initialCount, sizeof(IndirectRefSlot)));
47 CHECK(slot_data_ != NULL);
48
Ian Rogersdc51b792011-09-22 20:41:37 -070049 segment_state_.all = IRT_FIRST_SEGMENT;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070050 alloc_entries_ = initialCount;
51 max_entries_ = maxCount;
52 kind_ = desiredKind;
53}
54
55IndirectReferenceTable::~IndirectReferenceTable() {
56 free(table_);
57 free(slot_data_);
58 table_ = NULL;
59 slot_data_ = NULL;
60 alloc_entries_ = max_entries_ = -1;
61}
62
Elliott Hughes73e66f72012-05-09 09:34:45 -070063// Make sure that the entry at "idx" is correctly paired with "iref".
Elliott Hughes6c1a3942011-08-17 15:00:06 -070064bool IndirectReferenceTable::CheckEntry(const char* what, IndirectRef iref, int idx) const {
Elliott Hughescf4c6c42011-09-01 15:16:42 -070065 const Object* obj = table_[idx];
Elliott Hughes6c1a3942011-08-17 15:00:06 -070066 IndirectRef checkRef = ToIndirectRef(obj, idx);
67 if (checkRef != iref) {
68 LOG(ERROR) << "JNI ERROR (app bug): attempt to " << what
69 << " stale " << kind_ << " " << iref
70 << " (should be " << checkRef << ")";
71 AbortMaybe();
72 return false;
73 }
74 return true;
75}
76
Elliott Hughescf4c6c42011-09-01 15:16:42 -070077IndirectRef IndirectReferenceTable::Add(uint32_t cookie, const Object* obj) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070078 IRTSegmentState prevState;
79 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -070080 size_t topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070081
82 DCHECK(obj != NULL);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070083 // TODO: stronger sanity check on the object (such as in heap)
Elliott Hughesd07986f2011-12-06 18:27:45 -080084 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(obj), 8);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070085 DCHECK(table_ != NULL);
86 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -070087 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070088
89 if (topIndex == alloc_entries_) {
Elliott Hughes73e66f72012-05-09 09:34:45 -070090 // reached end of allocated space; did we hit buffer max?
Elliott Hughes6c1a3942011-08-17 15:00:06 -070091 if (topIndex == max_entries_) {
Elliott Hughes73e66f72012-05-09 09:34:45 -070092 LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow "
93 << "(max=" << max_entries_ << ")\n"
94 << Dumpable<IndirectReferenceTable>(*this);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070095 }
96
97 size_t newSize = alloc_entries_ * 2;
98 if (newSize > max_entries_) {
99 newSize = max_entries_;
100 }
101 DCHECK_GT(newSize, alloc_entries_);
102
Elliott Hughesba8eee12012-01-24 20:25:24 -0800103 table_ = reinterpret_cast<const Object**>(realloc(table_, newSize * sizeof(const Object*)));
104 slot_data_ = reinterpret_cast<IndirectRefSlot*>(realloc(slot_data_, newSize * sizeof(IndirectRefSlot)));
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700105 if (table_ == NULL || slot_data_ == NULL) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700106 LOG(FATAL) << "JNI ERROR (app bug): unable to expand "
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700107 << kind_ << " table (from "
108 << alloc_entries_ << " to " << newSize
Elliott Hughes73e66f72012-05-09 09:34:45 -0700109 << ", max=" << max_entries_ << ")\n"
110 << Dumpable<IndirectReferenceTable>(*this);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700111 }
112
113 // Clear the newly-allocated slot_data_ elements.
114 memset(slot_data_ + alloc_entries_, 0, (newSize - alloc_entries_) * sizeof(IndirectRefSlot));
115
116 alloc_entries_ = newSize;
117 }
118
Elliott Hughes73e66f72012-05-09 09:34:45 -0700119 // We know there's enough room in the table. Now we just need to find
120 // the right spot. If there's a hole, find it and fill it; otherwise,
121 // add to the end of the list.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700122 IndirectRef result;
Ian Rogersdc51b792011-09-22 20:41:37 -0700123 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700124 if (numHoles > 0) {
125 DCHECK_GT(topIndex, 1U);
Elliott Hughes73e66f72012-05-09 09:34:45 -0700126 // Find the first hole; likely to be near the end of the list.
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700127 const Object** pScan = &table_[topIndex - 1];
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700128 DCHECK(*pScan != NULL);
129 while (*--pScan != NULL) {
130 DCHECK_GE(pScan, table_ + prevState.parts.topIndex);
131 }
132 UpdateSlotAdd(obj, pScan - table_);
133 result = ToIndirectRef(obj, pScan - table_);
134 *pScan = obj;
Ian Rogersdc51b792011-09-22 20:41:37 -0700135 segment_state_.parts.numHoles--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700136 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700137 // Add to the end.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700138 UpdateSlotAdd(obj, topIndex);
139 result = ToIndirectRef(obj, topIndex);
140 table_[topIndex++] = obj;
Ian Rogersdc51b792011-09-22 20:41:37 -0700141 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700142 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700143 if (false) {
144 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex
145 << " holes=" << segment_state_.parts.numHoles;
146 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700147
148 DCHECK(result != NULL);
149 return result;
150}
151
Elliott Hughes726079d2011-10-07 18:43:44 -0700152void IndirectReferenceTable::AssertEmpty() {
153 if (begin() != end()) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700154 LOG(FATAL) << "Internal Error: non-empty local reference table\n"
155 << Dumpable<IndirectReferenceTable>(*this);
Elliott Hughes726079d2011-10-07 18:43:44 -0700156 }
157}
158
Elliott Hughes73e66f72012-05-09 09:34:45 -0700159// Verifies that the indirect table lookup is valid.
160// Returns "false" if something looks bad.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700161bool IndirectReferenceTable::GetChecked(IndirectRef iref) const {
162 if (iref == NULL) {
163 LOG(WARNING) << "Attempt to look up NULL " << kind_;
164 return false;
165 }
Ian Rogers408f79a2011-08-23 18:22:33 -0700166 if (GetIndirectRefKind(iref) == kSirtOrInvalid) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700167 LOG(ERROR) << "JNI ERROR (app bug): invalid " << kind_ << " " << iref;
168 AbortMaybe();
169 return false;
170 }
171
Ian Rogersdc51b792011-09-22 20:41:37 -0700172 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700173 int idx = ExtractIndex(iref);
174 if (idx >= topIndex) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700175 LOG(ERROR) << "JNI ERROR (app bug): accessed stale " << kind_ << " "
176 << iref << " (index " << idx << " in a table of size " << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700177 AbortMaybe();
178 return false;
179 }
180
181 if (table_[idx] == NULL) {
182 LOG(ERROR) << "JNI ERROR (app bug): accessed deleted " << kind_ << " " << iref;
183 AbortMaybe();
184 return false;
185 }
186
187 if (!CheckEntry("use", iref, idx)) {
188 return false;
189 }
190
191 return true;
192}
193
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700194static int Find(Object* direct_pointer, int bottomIndex, int topIndex, const Object** table) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700195 for (int i = bottomIndex; i < topIndex; ++i) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700196 if (table[i] == direct_pointer) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700197 return i;
198 }
199 }
200 return -1;
201}
202
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700203bool IndirectReferenceTable::ContainsDirectPointer(Object* direct_pointer) const {
204 return Find(direct_pointer, 0, segment_state_.parts.topIndex, table_) != -1;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700205}
206
Elliott Hughes73e66f72012-05-09 09:34:45 -0700207// Removes an object. We extract the table offset bits from "iref"
208// and zap the corresponding entry, leaving a hole if it's not at the top.
209// If the entry is not between the current top index and the bottom index
210// specified by the cookie, we don't remove anything. This is the behavior
211// required by JNI's DeleteLocalRef function.
212// This method is not called when a local frame is popped; this is only used
213// for explicit single removals.
214// Returns "false" if nothing was removed.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700215bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
216 IRTSegmentState prevState;
217 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700218 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700219 int bottomIndex = prevState.parts.topIndex;
220
221 DCHECK(table_ != NULL);
222 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -0700223 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700224
225 int idx = ExtractIndex(iref);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700226
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700227 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700228 if (GetIndirectRefKind(iref) == kSirtOrInvalid &&
TDYa12728f1a142012-03-15 21:51:52 -0700229 Thread::Current()->StackReferencesContain(reinterpret_cast<jobject>(iref))) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700230 LOG(WARNING) << "Attempt to remove local SIRT entry from IRT, ignoring";
231 return true;
232 }
Ian Rogers467c9692012-02-21 11:05:16 -0800233 if (GetIndirectRefKind(iref) == kSirtOrInvalid && vm->work_around_app_jni_bugs) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700234 Object* direct_pointer = reinterpret_cast<Object*>(iref);
235 idx = Find(direct_pointer, bottomIndex, topIndex, table_);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700236 if (idx == -1) {
237 LOG(WARNING) << "trying to work around app JNI bugs, but didn't find " << iref << " in table!";
238 return false;
239 }
240 }
241
242 if (idx < bottomIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700243 // Wrong segment.
244 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
245 << " vs " << bottomIndex << "-" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700246 return false;
247 }
248 if (idx >= topIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700249 // Bad --- stale reference?
250 LOG(WARNING) << "Attempt to remove invalid index " << idx
251 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700252 return false;
253 }
254
255 if (idx == topIndex-1) {
256 // Top-most entry. Scan up and consume holes.
257
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700258 if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700259 return false;
260 }
261
262 table_[idx] = NULL;
Ian Rogersdc51b792011-09-22 20:41:37 -0700263 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700264 if (numHoles != 0) {
265 while (--topIndex > bottomIndex && numHoles != 0) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700266 if (false) {
267 LOG(INFO) << "+++ checking for hole at " << topIndex-1
268 << " (cookie=" << cookie << ") val=" << table_[topIndex - 1];
269 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700270 if (table_[topIndex-1] != NULL) {
271 break;
272 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700273 if (false) {
274 LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
275 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700276 numHoles--;
277 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700278 segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
279 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700280 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700281 segment_state_.parts.topIndex = topIndex-1;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700282 if (false) {
283 LOG(INFO) << "+++ ate last entry " << topIndex - 1;
284 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700285 }
286 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700287 // Not the top-most entry. This creates a hole. We NULL out the
288 // entry to prevent somebody from deleting it twice and screwing up
289 // the hole count.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700290 if (table_[idx] == NULL) {
291 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
292 return false;
293 }
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700294 if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700295 return false;
296 }
297
298 table_[idx] = NULL;
Ian Rogersdc51b792011-09-22 20:41:37 -0700299 segment_state_.parts.numHoles++;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700300 if (false) {
301 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
302 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700303 }
304
305 return true;
306}
307
Elliott Hughes410c0c82011-09-01 17:58:25 -0700308void IndirectReferenceTable::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
309 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
310 for (It it = begin(), end = this->end(); it != end; ++it) {
311 visitor(**it, arg);
312 }
313}
314
Elliott Hughes73e66f72012-05-09 09:34:45 -0700315void IndirectReferenceTable::Dump(std::ostream& os) const {
316 os << kind_ << " table dump:\n";
Elliott Hughes75770752011-08-24 17:52:38 -0700317 std::vector<const Object*> entries(table_, table_ + Capacity());
Elliott Hughes73e66f72012-05-09 09:34:45 -0700318 ReferenceTable::Dump(os, entries);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700319}
320
321} // namespace art