blob: e78b362fd0723ba4fc0073a0024e84d7d2711526 [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,
37 size_t maxCount, IndirectRefKind desiredKind)
38{
39 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
Elliott Hughescf4c6c42011-09-01 15:16:42 -070043 table_ = reinterpret_cast<const Object**>(malloc(initialCount * sizeof(const Object*)));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070044 CHECK(table_ != NULL);
45#ifndef NDEBUG
Elliott Hughescf4c6c42011-09-01 15:16:42 -070046 memset(table_, 0xd1, initialCount * sizeof(const Object*));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070047#endif
48
49 slot_data_ = reinterpret_cast<IndirectRefSlot*>(calloc(initialCount, sizeof(IndirectRefSlot)));
50 CHECK(slot_data_ != NULL);
51
Ian Rogersdc51b792011-09-22 20:41:37 -070052 segment_state_.all = IRT_FIRST_SEGMENT;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070053 alloc_entries_ = initialCount;
54 max_entries_ = maxCount;
55 kind_ = desiredKind;
56}
57
58IndirectReferenceTable::~IndirectReferenceTable() {
59 free(table_);
60 free(slot_data_);
61 table_ = NULL;
62 slot_data_ = NULL;
63 alloc_entries_ = max_entries_ = -1;
64}
65
66/*
67 * Make sure that the entry at "idx" is correctly paired with "iref".
68 */
69bool IndirectReferenceTable::CheckEntry(const char* what, IndirectRef iref, int idx) const {
Elliott Hughescf4c6c42011-09-01 15:16:42 -070070 const Object* obj = table_[idx];
Elliott Hughes6c1a3942011-08-17 15:00:06 -070071 IndirectRef checkRef = ToIndirectRef(obj, idx);
72 if (checkRef != iref) {
73 LOG(ERROR) << "JNI ERROR (app bug): attempt to " << what
74 << " stale " << kind_ << " " << iref
75 << " (should be " << checkRef << ")";
76 AbortMaybe();
77 return false;
78 }
79 return true;
80}
81
Elliott Hughescf4c6c42011-09-01 15:16:42 -070082IndirectRef IndirectReferenceTable::Add(uint32_t cookie, const Object* obj) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070083 IRTSegmentState prevState;
84 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -070085 size_t topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070086
87 DCHECK(obj != NULL);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070088 // TODO: stronger sanity check on the object (such as in heap)
Brian Carlstrom395520e2011-09-25 19:35:00 -070089 DCHECK(IsAligned(reinterpret_cast<intptr_t>(obj), 8)) << reinterpret_cast<const void*>(obj);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070090 DCHECK(table_ != NULL);
91 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -070092 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070093
94 if (topIndex == alloc_entries_) {
95 /* reached end of allocated space; did we hit buffer max? */
96 if (topIndex == max_entries_) {
97 LOG(ERROR) << "JNI ERROR (app bug): " << kind_ << " table overflow "
98 << "(max=" << max_entries_ << ")";
99 Dump();
100 LOG(FATAL); // TODO: operator<< for IndirectReferenceTable
101 }
102
103 size_t newSize = alloc_entries_ * 2;
104 if (newSize > max_entries_) {
105 newSize = max_entries_;
106 }
107 DCHECK_GT(newSize, alloc_entries_);
108
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700109 table_ = (const Object**) realloc(table_, newSize * sizeof(const Object*));
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700110 slot_data_ = (IndirectRefSlot*) realloc(slot_data_, newSize * sizeof(IndirectRefSlot));
111 if (table_ == NULL || slot_data_ == NULL) {
112 LOG(ERROR) << "JNI ERROR (app bug): unable to expand "
113 << kind_ << " table (from "
114 << alloc_entries_ << " to " << newSize
115 << ", max=" << max_entries_ << ")";
116 Dump();
117 LOG(FATAL); // TODO: operator<< for IndirectReferenceTable
118 }
119
120 // Clear the newly-allocated slot_data_ elements.
121 memset(slot_data_ + alloc_entries_, 0, (newSize - alloc_entries_) * sizeof(IndirectRefSlot));
122
123 alloc_entries_ = newSize;
124 }
125
126 /*
127 * We know there's enough room in the table. Now we just need to find
128 * the right spot. If there's a hole, find it and fill it; otherwise,
129 * add to the end of the list.
130 */
131 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);
135 /* find the first hole; likely to be near the end of the list */
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700136 const Object** pScan = &table_[topIndex - 1];
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700137 DCHECK(*pScan != NULL);
138 while (*--pScan != NULL) {
139 DCHECK_GE(pScan, table_ + prevState.parts.topIndex);
140 }
141 UpdateSlotAdd(obj, pScan - table_);
142 result = ToIndirectRef(obj, pScan - table_);
143 *pScan = obj;
Ian Rogersdc51b792011-09-22 20:41:37 -0700144 segment_state_.parts.numHoles--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700145 } else {
146 /* add to the end */
147 UpdateSlotAdd(obj, topIndex);
148 result = ToIndirectRef(obj, topIndex);
149 table_[topIndex++] = obj;
Ian Rogersdc51b792011-09-22 20:41:37 -0700150 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700151 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700152 if (false) {
153 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex
154 << " holes=" << segment_state_.parts.numHoles;
155 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700156
157 DCHECK(result != NULL);
158 return result;
159}
160
161/*
162 * Verify that the indirect table lookup is valid.
163 *
164 * Returns "false" if something looks bad.
165 */
166bool IndirectReferenceTable::GetChecked(IndirectRef iref) const {
167 if (iref == NULL) {
168 LOG(WARNING) << "Attempt to look up NULL " << kind_;
169 return false;
170 }
Ian Rogers408f79a2011-08-23 18:22:33 -0700171 if (GetIndirectRefKind(iref) == kSirtOrInvalid) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700172 LOG(ERROR) << "JNI ERROR (app bug): invalid " << kind_ << " " << iref;
173 AbortMaybe();
174 return false;
175 }
176
Ian Rogersdc51b792011-09-22 20:41:37 -0700177 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700178 int idx = ExtractIndex(iref);
179 if (idx >= topIndex) {
180 /* bad -- stale reference? */
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700181 LOG(ERROR) << "JNI ERROR (app bug): accessed stale " << kind_ << " "
182 << iref << " (index " << idx << " in a table of size " << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700183 AbortMaybe();
184 return false;
185 }
186
187 if (table_[idx] == NULL) {
188 LOG(ERROR) << "JNI ERROR (app bug): accessed deleted " << kind_ << " " << iref;
189 AbortMaybe();
190 return false;
191 }
192
193 if (!CheckEntry("use", iref, idx)) {
194 return false;
195 }
196
197 return true;
198}
199
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700200static int LinearScan(IndirectRef iref, int bottomIndex, int topIndex, const Object** table) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700201 for (int i = bottomIndex; i < topIndex; ++i) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700202 if (table[i] == reinterpret_cast<const Object*>(iref)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700203 return i;
204 }
205 }
206 return -1;
207}
208
209bool IndirectReferenceTable::Contains(IndirectRef iref) const {
Ian Rogersdc51b792011-09-22 20:41:37 -0700210 return LinearScan(iref, 0, segment_state_.parts.topIndex, table_) != -1;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700211}
212
213/*
214 * Remove "obj" from "pRef". We extract the table offset bits from "iref"
215 * and zap the corresponding entry, leaving a hole if it's not at the top.
216 *
217 * If the entry is not between the current top index and the bottom index
218 * specified by the cookie, we don't remove anything. This is the behavior
219 * required by JNI's DeleteLocalRef function.
220 *
221 * Note this is NOT called when a local frame is popped. This is only used
222 * for explicit single removals.
223 *
224 * Returns "false" if nothing was removed.
225 */
226bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
227 IRTSegmentState prevState;
228 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700229 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700230 int bottomIndex = prevState.parts.topIndex;
231
232 DCHECK(table_ != NULL);
233 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -0700234 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700235
236 int idx = ExtractIndex(iref);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700237
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700238 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700239 if (GetIndirectRefKind(iref) == kSirtOrInvalid &&
240 Thread::Current()->SirtContains(reinterpret_cast<jobject>(iref))) {
241 LOG(WARNING) << "Attempt to remove local SIRT entry from IRT, ignoring";
242 return true;
243 }
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700244 if (GetIndirectRefKind(iref) == kSirtOrInvalid || vm->work_around_app_jni_bugs) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700245 idx = LinearScan(iref, bottomIndex, topIndex, table_);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700246 if (idx == -1) {
247 LOG(WARNING) << "trying to work around app JNI bugs, but didn't find " << iref << " in table!";
248 return false;
249 }
250 }
251
252 if (idx < bottomIndex) {
253 /* wrong segment */
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700254 LOG(INFO) << "Attempt to remove index outside index area (" << idx
255 << " vs " << bottomIndex << "-" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700256 return false;
257 }
258 if (idx >= topIndex) {
259 /* bad -- stale reference? */
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700260 LOG(INFO) << "Attempt to remove invalid index " << idx
261 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700262 return false;
263 }
264
265 if (idx == topIndex-1) {
266 // Top-most entry. Scan up and consume holes.
267
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700268 if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700269 return false;
270 }
271
272 table_[idx] = NULL;
Ian Rogersdc51b792011-09-22 20:41:37 -0700273 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700274 if (numHoles != 0) {
275 while (--topIndex > bottomIndex && numHoles != 0) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700276 if (false) {
277 LOG(INFO) << "+++ checking for hole at " << topIndex-1
278 << " (cookie=" << cookie << ") val=" << table_[topIndex - 1];
279 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700280 if (table_[topIndex-1] != NULL) {
281 break;
282 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700283 if (false) {
284 LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
285 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700286 numHoles--;
287 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700288 segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
289 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700290 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700291 segment_state_.parts.topIndex = topIndex-1;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700292 if (false) {
293 LOG(INFO) << "+++ ate last entry " << topIndex - 1;
294 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700295 }
296 } else {
297 /*
298 * Not the top-most entry. This creates a hole. We NULL out the
299 * entry to prevent somebody from deleting it twice and screwing up
300 * the hole count.
301 */
302 if (table_[idx] == NULL) {
303 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
304 return false;
305 }
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700306 if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700307 return false;
308 }
309
310 table_[idx] = NULL;
Ian Rogersdc51b792011-09-22 20:41:37 -0700311 segment_state_.parts.numHoles++;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700312 if (false) {
313 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
314 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700315 }
316
317 return true;
318}
319
Elliott Hughes410c0c82011-09-01 17:58:25 -0700320void IndirectReferenceTable::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
321 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
322 for (It it = begin(), end = this->end(); it != end; ++it) {
323 visitor(**it, arg);
324 }
325}
326
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700327std::ostream& operator<<(std::ostream& os, IndirectRefKind rhs) {
328 switch (rhs) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700329 case kSirtOrInvalid:
330 os << "stack indirect reference table or invalid reference";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700331 break;
332 case kLocal:
333 os << "local reference";
334 break;
335 case kGlobal:
336 os << "global reference";
337 break;
338 case kWeakGlobal:
339 os << "weak global reference";
340 break;
341 default:
342 os << "IndirectRefKind[" << static_cast<int>(rhs) << "]";
343 break;
344 }
345 return os;
346}
347
348void IndirectReferenceTable::Dump() const {
349 LOG(WARNING) << kind_ << " table dump:";
Elliott Hughes75770752011-08-24 17:52:38 -0700350 std::vector<const Object*> entries(table_, table_ + Capacity());
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700351 ReferenceTable::Dump(entries);
352}
353
354} // namespace art