blob: 6e5446fba2ea2b87c994c085310acf589e4ec310 [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 Rogerscdd1d2d2011-08-18 09:58:17 -070021#include "utils.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070022
23#include <cstdlib>
24
25namespace art {
26
Elliott Hughes6c1a3942011-08-17 15:00:06 -070027static void AbortMaybe() {
Elliott Hughesa2501992011-08-26 19:39:54 -070028 // If -Xcheck:jni is on, it'll give a more detailed error before aborting.
29 if (!Runtime::Current()->GetJavaVM()->check_jni) {
30 // Otherwise, we want to abort rather than hand back a bad reference.
31 LOG(FATAL) << "JNI ERROR (app bug): see above.";
32 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -070033}
34
35IndirectReferenceTable::IndirectReferenceTable(size_t initialCount,
36 size_t maxCount, IndirectRefKind desiredKind)
37{
38 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
42 table_ = reinterpret_cast<Object**>(malloc(initialCount * sizeof(Object*)));
43 CHECK(table_ != NULL);
44#ifndef NDEBUG
45 memset(table_, 0xd1, initialCount * sizeof(Object*));
46#endif
47
48 slot_data_ = reinterpret_cast<IndirectRefSlot*>(calloc(initialCount, sizeof(IndirectRefSlot)));
49 CHECK(slot_data_ != NULL);
50
51 segmentState.all = IRT_FIRST_SEGMENT;
52 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 {
69 Object* obj = table_[idx];
70 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
81IndirectRef IndirectReferenceTable::Add(uint32_t cookie, Object* obj) {
82 IRTSegmentState prevState;
83 prevState.all = cookie;
84 size_t topIndex = segmentState.parts.topIndex;
85
86 DCHECK(obj != NULL);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070087 // TODO: stronger sanity check on the object (such as in heap)
88 DCHECK(IsAligned(reinterpret_cast<intptr_t>(obj), 8));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070089 DCHECK(table_ != NULL);
90 DCHECK_LE(alloc_entries_, max_entries_);
91 DCHECK_GE(segmentState.parts.numHoles, prevState.parts.numHoles);
92
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
108 table_ = (Object**) realloc(table_, newSize * sizeof(Object*));
109 slot_data_ = (IndirectRefSlot*) realloc(slot_data_, newSize * sizeof(IndirectRefSlot));
110 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;
131 int numHoles = segmentState.parts.numHoles - prevState.parts.numHoles;
132 if (numHoles > 0) {
133 DCHECK_GT(topIndex, 1U);
134 /* find the first hole; likely to be near the end of the list */
135 Object** pScan = &table_[topIndex - 1];
136 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;
143 segmentState.parts.numHoles--;
144 } else {
145 /* add to the end */
146 UpdateSlotAdd(obj, topIndex);
147 result = ToIndirectRef(obj, topIndex);
148 table_[topIndex++] = obj;
149 segmentState.parts.topIndex = topIndex;
150 }
151
152 DCHECK(result != NULL);
153 return result;
154}
155
156/*
157 * Verify that the indirect table lookup is valid.
158 *
159 * Returns "false" if something looks bad.
160 */
161bool 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
172 int topIndex = segmentState.parts.topIndex;
173 int idx = ExtractIndex(iref);
174 if (idx >= topIndex) {
175 /* bad -- stale reference? */
176 LOG(ERROR) << "JNI ERROR (app bug): accessed stale " << kind_ << " " << iref << " (index " << idx << " in a table of size " << topIndex << ")";
177 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
194static int LinearScan(IndirectRef iref, int bottomIndex, int topIndex, Object** table) {
195 for (int i = bottomIndex; i < topIndex; ++i) {
196 if (table[i] == reinterpret_cast<Object*>(iref)) {
197 return i;
198 }
199 }
200 return -1;
201}
202
203bool IndirectReferenceTable::Contains(IndirectRef iref) const {
204 return LinearScan(iref, 0, segmentState.parts.topIndex, table_) != -1;
205}
206
207/*
208 * Remove "obj" from "pRef". We extract the table offset bits from "iref"
209 * and zap the corresponding entry, leaving a hole if it's not at the top.
210 *
211 * If the entry is not between the current top index and the bottom index
212 * specified by the cookie, we don't remove anything. This is the behavior
213 * required by JNI's DeleteLocalRef function.
214 *
215 * Note this is NOT called when a local frame is popped. This is only used
216 * for explicit single removals.
217 *
218 * Returns "false" if nothing was removed.
219 */
220bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
221 IRTSegmentState prevState;
222 prevState.all = cookie;
223 int topIndex = segmentState.parts.topIndex;
224 int bottomIndex = prevState.parts.topIndex;
225
226 DCHECK(table_ != NULL);
227 DCHECK_LE(alloc_entries_, max_entries_);
228 DCHECK_GE(segmentState.parts.numHoles, prevState.parts.numHoles);
229
230 int idx = ExtractIndex(iref);
231 bool workAroundAppJniBugs = false;
232
Ian Rogers408f79a2011-08-23 18:22:33 -0700233 if (GetIndirectRefKind(iref) == kSirtOrInvalid /*&& gDvmJni.workAroundAppJniBugs*/) { // TODO
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700234 idx = LinearScan(iref, bottomIndex, topIndex, table_);
235 workAroundAppJniBugs = true;
236 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) {
243 /* wrong segment */
244 LOG(INFO) << "Attempt to remove index outside index area (" << idx << " vs " << bottomIndex << "-" << topIndex << ")";
245 return false;
246 }
247 if (idx >= topIndex) {
248 /* bad -- stale reference? */
249 LOG(INFO) << "Attempt to remove invalid index " << idx << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
250 return false;
251 }
252
253 if (idx == topIndex-1) {
254 // Top-most entry. Scan up and consume holes.
255
256 if (workAroundAppJniBugs == false && !CheckEntry("remove", iref, idx)) {
257 return false;
258 }
259
260 table_[idx] = NULL;
261 int numHoles = segmentState.parts.numHoles - prevState.parts.numHoles;
262 if (numHoles != 0) {
263 while (--topIndex > bottomIndex && numHoles != 0) {
264 //LOG(INFO) << "+++ checking for hole at " << topIndex-1 << " (cookie=" << cookie << ") val=" << table_[topIndex-1];
265 if (table_[topIndex-1] != NULL) {
266 break;
267 }
268 //LOG(INFO) << "+++ ate hole at " << (topIndex-1);
269 numHoles--;
270 }
271 segmentState.parts.numHoles = numHoles + prevState.parts.numHoles;
272 segmentState.parts.topIndex = topIndex;
273 } else {
274 segmentState.parts.topIndex = topIndex-1;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700275 //LOG(INFO) << "+++ ate last entry " << topIndex-1;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700276 }
277 } else {
278 /*
279 * Not the top-most entry. This creates a hole. We NULL out the
280 * entry to prevent somebody from deleting it twice and screwing up
281 * the hole count.
282 */
283 if (table_[idx] == NULL) {
284 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
285 return false;
286 }
287 if (workAroundAppJniBugs == false && !CheckEntry("remove", iref, idx)) {
288 return false;
289 }
290
291 table_[idx] = NULL;
292 segmentState.parts.numHoles++;
293 //LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segmentState.parts.numHoles;
294 }
295
296 return true;
297}
298
299std::ostream& operator<<(std::ostream& os, IndirectRefKind rhs) {
300 switch (rhs) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700301 case kSirtOrInvalid:
302 os << "stack indirect reference table or invalid reference";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700303 break;
304 case kLocal:
305 os << "local reference";
306 break;
307 case kGlobal:
308 os << "global reference";
309 break;
310 case kWeakGlobal:
311 os << "weak global reference";
312 break;
313 default:
314 os << "IndirectRefKind[" << static_cast<int>(rhs) << "]";
315 break;
316 }
317 return os;
318}
319
320void IndirectReferenceTable::Dump() const {
321 LOG(WARNING) << kind_ << " table dump:";
Elliott Hughes75770752011-08-24 17:52:38 -0700322 std::vector<const Object*> entries(table_, table_ + Capacity());
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700323 ReferenceTable::Dump(entries);
324}
325
326} // namespace art