blob: 6eeb94a3503ecbdfa52c8860e1f0766cebb7ab64 [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);
44#ifndef NDEBUG
Elliott Hughescf4c6c42011-09-01 15:16:42 -070045 memset(table_, 0xd1, initialCount * sizeof(const Object*));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070046#endif
47
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
65/*
66 * Make sure that the entry at "idx" is correctly paired with "iref".
67 */
68bool IndirectReferenceTable::CheckEntry(const char* what, IndirectRef iref, int idx) const {
Elliott Hughescf4c6c42011-09-01 15:16:42 -070069 const Object* obj = table_[idx];
Elliott Hughes6c1a3942011-08-17 15:00:06 -070070 IndirectRef checkRef = ToIndirectRef(obj, idx);
71 if (checkRef != iref) {
72 LOG(ERROR) << "JNI ERROR (app bug): attempt to " << what
73 << " stale " << kind_ << " " << iref
74 << " (should be " << checkRef << ")";
75 AbortMaybe();
76 return false;
77 }
78 return true;
79}
80
Elliott Hughescf4c6c42011-09-01 15:16:42 -070081IndirectRef IndirectReferenceTable::Add(uint32_t cookie, const Object* obj) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070082 IRTSegmentState prevState;
83 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -070084 size_t topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070085
86 DCHECK(obj != NULL);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070087 // TODO: stronger sanity check on the object (such as in heap)
Elliott Hughesd07986f2011-12-06 18:27:45 -080088 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(obj), 8);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070089 DCHECK(table_ != NULL);
90 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -070091 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070092
93 if (topIndex == alloc_entries_) {
94 /* reached end of allocated space; did we hit buffer max? */
95 if (topIndex == max_entries_) {
96 LOG(ERROR) << "JNI ERROR (app bug): " << kind_ << " table overflow "
97 << "(max=" << max_entries_ << ")";
98 Dump();
99 LOG(FATAL); // TODO: operator<< for IndirectReferenceTable
100 }
101
102 size_t newSize = alloc_entries_ * 2;
103 if (newSize > max_entries_) {
104 newSize = max_entries_;
105 }
106 DCHECK_GT(newSize, alloc_entries_);
107
Elliott Hughesba8eee12012-01-24 20:25:24 -0800108 table_ = reinterpret_cast<const Object**>(realloc(table_, newSize * sizeof(const Object*)));
109 slot_data_ = reinterpret_cast<IndirectRefSlot*>(realloc(slot_data_, newSize * sizeof(IndirectRefSlot)));
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700110 if (table_ == NULL || slot_data_ == NULL) {
111 LOG(ERROR) << "JNI ERROR (app bug): unable to expand "
112 << kind_ << " table (from "
113 << alloc_entries_ << " to " << newSize
114 << ", max=" << max_entries_ << ")";
115 Dump();
116 LOG(FATAL); // TODO: operator<< for IndirectReferenceTable
117 }
118
119 // Clear the newly-allocated slot_data_ elements.
120 memset(slot_data_ + alloc_entries_, 0, (newSize - alloc_entries_) * sizeof(IndirectRefSlot));
121
122 alloc_entries_ = newSize;
123 }
124
125 /*
126 * We know there's enough room in the table. Now we just need to find
127 * the right spot. If there's a hole, find it and fill it; otherwise,
128 * add to the end of the list.
129 */
130 IndirectRef result;
Ian Rogersdc51b792011-09-22 20:41:37 -0700131 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700132 if (numHoles > 0) {
133 DCHECK_GT(topIndex, 1U);
134 /* find the first hole; likely to be near the end of the list */
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700135 const Object** pScan = &table_[topIndex - 1];
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700136 DCHECK(*pScan != NULL);
137 while (*--pScan != NULL) {
138 DCHECK_GE(pScan, table_ + prevState.parts.topIndex);
139 }
140 UpdateSlotAdd(obj, pScan - table_);
141 result = ToIndirectRef(obj, pScan - table_);
142 *pScan = obj;
Ian Rogersdc51b792011-09-22 20:41:37 -0700143 segment_state_.parts.numHoles--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700144 } else {
145 /* add to the end */
146 UpdateSlotAdd(obj, topIndex);
147 result = ToIndirectRef(obj, topIndex);
148 table_[topIndex++] = obj;
Ian Rogersdc51b792011-09-22 20:41:37 -0700149 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700150 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700151 if (false) {
152 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex
153 << " holes=" << segment_state_.parts.numHoles;
154 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700155
156 DCHECK(result != NULL);
157 return result;
158}
159
Elliott Hughes726079d2011-10-07 18:43:44 -0700160void IndirectReferenceTable::AssertEmpty() {
161 if (begin() != end()) {
162 Dump();
163 LOG(FATAL) << "Internal Error: non-empty local reference table";
164 }
165}
166
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700167/*
168 * Verify that the indirect table lookup is valid.
169 *
170 * Returns "false" if something looks bad.
171 */
172bool IndirectReferenceTable::GetChecked(IndirectRef iref) const {
173 if (iref == NULL) {
174 LOG(WARNING) << "Attempt to look up NULL " << kind_;
175 return false;
176 }
Ian Rogers408f79a2011-08-23 18:22:33 -0700177 if (GetIndirectRefKind(iref) == kSirtOrInvalid) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700178 LOG(ERROR) << "JNI ERROR (app bug): invalid " << kind_ << " " << iref;
179 AbortMaybe();
180 return false;
181 }
182
Ian Rogersdc51b792011-09-22 20:41:37 -0700183 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700184 int idx = ExtractIndex(iref);
185 if (idx >= topIndex) {
186 /* bad -- stale reference? */
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700187 LOG(ERROR) << "JNI ERROR (app bug): accessed stale " << kind_ << " "
188 << iref << " (index " << idx << " in a table of size " << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700189 AbortMaybe();
190 return false;
191 }
192
193 if (table_[idx] == NULL) {
194 LOG(ERROR) << "JNI ERROR (app bug): accessed deleted " << kind_ << " " << iref;
195 AbortMaybe();
196 return false;
197 }
198
199 if (!CheckEntry("use", iref, idx)) {
200 return false;
201 }
202
203 return true;
204}
205
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700206static int Find(Object* direct_pointer, int bottomIndex, int topIndex, const Object** table) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700207 for (int i = bottomIndex; i < topIndex; ++i) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700208 if (table[i] == direct_pointer) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700209 return i;
210 }
211 }
212 return -1;
213}
214
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700215bool IndirectReferenceTable::ContainsDirectPointer(Object* direct_pointer) const {
216 return Find(direct_pointer, 0, segment_state_.parts.topIndex, table_) != -1;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700217}
218
219/*
220 * Remove "obj" from "pRef". We extract the table offset bits from "iref"
221 * and zap the corresponding entry, leaving a hole if it's not at the top.
222 *
223 * If the entry is not between the current top index and the bottom index
224 * specified by the cookie, we don't remove anything. This is the behavior
225 * required by JNI's DeleteLocalRef function.
226 *
227 * Note this is NOT called when a local frame is popped. This is only used
228 * for explicit single removals.
229 *
230 * Returns "false" if nothing was removed.
231 */
232bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
233 IRTSegmentState prevState;
234 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700235 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700236 int bottomIndex = prevState.parts.topIndex;
237
238 DCHECK(table_ != NULL);
239 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -0700240 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700241
242 int idx = ExtractIndex(iref);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700243
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700244 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700245 if (GetIndirectRefKind(iref) == kSirtOrInvalid &&
246 Thread::Current()->SirtContains(reinterpret_cast<jobject>(iref))) {
247 LOG(WARNING) << "Attempt to remove local SIRT entry from IRT, ignoring";
248 return true;
249 }
Ian Rogers467c9692012-02-21 11:05:16 -0800250 if (GetIndirectRefKind(iref) == kSirtOrInvalid && vm->work_around_app_jni_bugs) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700251 Object* direct_pointer = reinterpret_cast<Object*>(iref);
252 idx = Find(direct_pointer, bottomIndex, topIndex, table_);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700253 if (idx == -1) {
254 LOG(WARNING) << "trying to work around app JNI bugs, but didn't find " << iref << " in table!";
255 return false;
256 }
257 }
258
259 if (idx < bottomIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700260 // Wrong segment.
261 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
262 << " vs " << bottomIndex << "-" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700263 return false;
264 }
265 if (idx >= topIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700266 // Bad --- stale reference?
267 LOG(WARNING) << "Attempt to remove invalid index " << idx
268 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700269 return false;
270 }
271
272 if (idx == topIndex-1) {
273 // Top-most entry. Scan up and consume holes.
274
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700275 if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700276 return false;
277 }
278
279 table_[idx] = NULL;
Ian Rogersdc51b792011-09-22 20:41:37 -0700280 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700281 if (numHoles != 0) {
282 while (--topIndex > bottomIndex && numHoles != 0) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700283 if (false) {
284 LOG(INFO) << "+++ checking for hole at " << topIndex-1
285 << " (cookie=" << cookie << ") val=" << table_[topIndex - 1];
286 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700287 if (table_[topIndex-1] != NULL) {
288 break;
289 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700290 if (false) {
291 LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
292 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700293 numHoles--;
294 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700295 segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
296 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700297 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700298 segment_state_.parts.topIndex = topIndex-1;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700299 if (false) {
300 LOG(INFO) << "+++ ate last entry " << topIndex - 1;
301 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700302 }
303 } else {
304 /*
305 * Not the top-most entry. This creates a hole. We NULL out the
306 * entry to prevent somebody from deleting it twice and screwing up
307 * the hole count.
308 */
309 if (table_[idx] == NULL) {
310 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
311 return false;
312 }
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700313 if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700314 return false;
315 }
316
317 table_[idx] = NULL;
Ian Rogersdc51b792011-09-22 20:41:37 -0700318 segment_state_.parts.numHoles++;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700319 if (false) {
320 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
321 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700322 }
323
324 return true;
325}
326
Elliott Hughes410c0c82011-09-01 17:58:25 -0700327void IndirectReferenceTable::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
328 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
329 for (It it = begin(), end = this->end(); it != end; ++it) {
330 visitor(**it, arg);
331 }
332}
333
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700334std::ostream& operator<<(std::ostream& os, IndirectRefKind rhs) {
335 switch (rhs) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700336 case kSirtOrInvalid:
337 os << "stack indirect reference table or invalid reference";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700338 break;
339 case kLocal:
340 os << "local reference";
341 break;
342 case kGlobal:
343 os << "global reference";
344 break;
345 case kWeakGlobal:
346 os << "weak global reference";
347 break;
348 default:
349 os << "IndirectRefKind[" << static_cast<int>(rhs) << "]";
350 break;
351 }
352 return os;
353}
354
355void IndirectReferenceTable::Dump() const {
356 LOG(WARNING) << kind_ << " table dump:";
Elliott Hughes75770752011-08-24 17:52:38 -0700357 std::vector<const Object*> entries(table_, table_ + Capacity());
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700358 ReferenceTable::Dump(entries);
359}
360
361} // namespace art