blob: b30ce539aaf2f73cb548ff9a905c86ffd4929755 [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"
18#include "reference_table.h"
19
20#include <cstdlib>
21
22namespace art {
23
24// TODO: implement this for art. (only needed for non-CheckJNI operation.)
25static void AbortMaybe() {
26 // If CheckJNI is on, it'll give a more detailed error before aborting.
27 // Otherwise, we want to abort rather than hand back a bad reference.
28// if (!gDvmJni.useCheckJni) {
29// LOG(FATAL) << "bye!";
30// }
31}
32
33IndirectReferenceTable::IndirectReferenceTable(size_t initialCount,
34 size_t maxCount, IndirectRefKind desiredKind)
35{
36 CHECK_GT(initialCount, 0U);
37 CHECK_LE(initialCount, maxCount);
38 CHECK_NE(desiredKind, kInvalid);
39
40 table_ = reinterpret_cast<Object**>(malloc(initialCount * sizeof(Object*)));
41 CHECK(table_ != NULL);
42#ifndef NDEBUG
43 memset(table_, 0xd1, initialCount * sizeof(Object*));
44#endif
45
46 slot_data_ = reinterpret_cast<IndirectRefSlot*>(calloc(initialCount, sizeof(IndirectRefSlot)));
47 CHECK(slot_data_ != NULL);
48
49 segmentState.all = IRT_FIRST_SEGMENT;
50 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
63/*
64 * Make sure that the entry at "idx" is correctly paired with "iref".
65 */
66bool IndirectReferenceTable::CheckEntry(const char* what, IndirectRef iref, int idx) const {
67 Object* obj = table_[idx];
68 IndirectRef checkRef = ToIndirectRef(obj, idx);
69 if (checkRef != iref) {
70 LOG(ERROR) << "JNI ERROR (app bug): attempt to " << what
71 << " stale " << kind_ << " " << iref
72 << " (should be " << checkRef << ")";
73 AbortMaybe();
74 return false;
75 }
76 return true;
77}
78
79IndirectRef IndirectReferenceTable::Add(uint32_t cookie, Object* obj) {
80 IRTSegmentState prevState;
81 prevState.all = cookie;
82 size_t topIndex = segmentState.parts.topIndex;
83
84 DCHECK(obj != NULL);
85 //DCHECK(dvmIsHeapAddress(obj));
86 DCHECK(table_ != NULL);
87 DCHECK_LE(alloc_entries_, max_entries_);
88 DCHECK_GE(segmentState.parts.numHoles, prevState.parts.numHoles);
89
90 if (topIndex == alloc_entries_) {
91 /* reached end of allocated space; did we hit buffer max? */
92 if (topIndex == max_entries_) {
93 LOG(ERROR) << "JNI ERROR (app bug): " << kind_ << " table overflow "
94 << "(max=" << max_entries_ << ")";
95 Dump();
96 LOG(FATAL); // TODO: operator<< for IndirectReferenceTable
97 }
98
99 size_t newSize = alloc_entries_ * 2;
100 if (newSize > max_entries_) {
101 newSize = max_entries_;
102 }
103 DCHECK_GT(newSize, alloc_entries_);
104
105 table_ = (Object**) realloc(table_, newSize * sizeof(Object*));
106 slot_data_ = (IndirectRefSlot*) realloc(slot_data_, newSize * sizeof(IndirectRefSlot));
107 if (table_ == NULL || slot_data_ == NULL) {
108 LOG(ERROR) << "JNI ERROR (app bug): unable to expand "
109 << kind_ << " table (from "
110 << alloc_entries_ << " to " << newSize
111 << ", max=" << max_entries_ << ")";
112 Dump();
113 LOG(FATAL); // TODO: operator<< for IndirectReferenceTable
114 }
115
116 // Clear the newly-allocated slot_data_ elements.
117 memset(slot_data_ + alloc_entries_, 0, (newSize - alloc_entries_) * sizeof(IndirectRefSlot));
118
119 alloc_entries_ = newSize;
120 }
121
122 /*
123 * We know there's enough room in the table. Now we just need to find
124 * the right spot. If there's a hole, find it and fill it; otherwise,
125 * add to the end of the list.
126 */
127 IndirectRef result;
128 int numHoles = segmentState.parts.numHoles - prevState.parts.numHoles;
129 if (numHoles > 0) {
130 DCHECK_GT(topIndex, 1U);
131 /* find the first hole; likely to be near the end of the list */
132 Object** pScan = &table_[topIndex - 1];
133 DCHECK(*pScan != NULL);
134 while (*--pScan != NULL) {
135 DCHECK_GE(pScan, table_ + prevState.parts.topIndex);
136 }
137 UpdateSlotAdd(obj, pScan - table_);
138 result = ToIndirectRef(obj, pScan - table_);
139 *pScan = obj;
140 segmentState.parts.numHoles--;
141 } else {
142 /* add to the end */
143 UpdateSlotAdd(obj, topIndex);
144 result = ToIndirectRef(obj, topIndex);
145 table_[topIndex++] = obj;
146 segmentState.parts.topIndex = topIndex;
147 }
148
149 DCHECK(result != NULL);
150 return result;
151}
152
153/*
154 * Verify that the indirect table lookup is valid.
155 *
156 * Returns "false" if something looks bad.
157 */
158bool IndirectReferenceTable::GetChecked(IndirectRef iref) const {
159 if (iref == NULL) {
160 LOG(WARNING) << "Attempt to look up NULL " << kind_;
161 return false;
162 }
163 if (GetIndirectRefKind(iref) == kInvalid) {
164 LOG(ERROR) << "JNI ERROR (app bug): invalid " << kind_ << " " << iref;
165 AbortMaybe();
166 return false;
167 }
168
169 int topIndex = segmentState.parts.topIndex;
170 int idx = ExtractIndex(iref);
171 if (idx >= topIndex) {
172 /* bad -- stale reference? */
173 LOG(ERROR) << "JNI ERROR (app bug): accessed stale " << kind_ << " " << iref << " (index " << idx << " in a table of size " << topIndex << ")";
174 AbortMaybe();
175 return false;
176 }
177
178 if (table_[idx] == NULL) {
179 LOG(ERROR) << "JNI ERROR (app bug): accessed deleted " << kind_ << " " << iref;
180 AbortMaybe();
181 return false;
182 }
183
184 if (!CheckEntry("use", iref, idx)) {
185 return false;
186 }
187
188 return true;
189}
190
191static int LinearScan(IndirectRef iref, int bottomIndex, int topIndex, Object** table) {
192 for (int i = bottomIndex; i < topIndex; ++i) {
193 if (table[i] == reinterpret_cast<Object*>(iref)) {
194 return i;
195 }
196 }
197 return -1;
198}
199
200bool IndirectReferenceTable::Contains(IndirectRef iref) const {
201 return LinearScan(iref, 0, segmentState.parts.topIndex, table_) != -1;
202}
203
204/*
205 * Remove "obj" from "pRef". We extract the table offset bits from "iref"
206 * and zap the corresponding entry, leaving a hole if it's not at the top.
207 *
208 * If the entry is not between the current top index and the bottom index
209 * specified by the cookie, we don't remove anything. This is the behavior
210 * required by JNI's DeleteLocalRef function.
211 *
212 * Note this is NOT called when a local frame is popped. This is only used
213 * for explicit single removals.
214 *
215 * Returns "false" if nothing was removed.
216 */
217bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
218 IRTSegmentState prevState;
219 prevState.all = cookie;
220 int topIndex = segmentState.parts.topIndex;
221 int bottomIndex = prevState.parts.topIndex;
222
223 DCHECK(table_ != NULL);
224 DCHECK_LE(alloc_entries_, max_entries_);
225 DCHECK_GE(segmentState.parts.numHoles, prevState.parts.numHoles);
226
227 int idx = ExtractIndex(iref);
228 bool workAroundAppJniBugs = false;
229
230 if (GetIndirectRefKind(iref) == kInvalid /*&& gDvmJni.workAroundAppJniBugs*/) { // TODO
231 idx = LinearScan(iref, bottomIndex, topIndex, table_);
232 workAroundAppJniBugs = true;
233 if (idx == -1) {
234 LOG(WARNING) << "trying to work around app JNI bugs, but didn't find " << iref << " in table!";
235 return false;
236 }
237 }
238
239 if (idx < bottomIndex) {
240 /* wrong segment */
241 LOG(INFO) << "Attempt to remove index outside index area (" << idx << " vs " << bottomIndex << "-" << topIndex << ")";
242 return false;
243 }
244 if (idx >= topIndex) {
245 /* bad -- stale reference? */
246 LOG(INFO) << "Attempt to remove invalid index " << idx << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
247 return false;
248 }
249
250 if (idx == topIndex-1) {
251 // Top-most entry. Scan up and consume holes.
252
253 if (workAroundAppJniBugs == false && !CheckEntry("remove", iref, idx)) {
254 return false;
255 }
256
257 table_[idx] = NULL;
258 int numHoles = segmentState.parts.numHoles - prevState.parts.numHoles;
259 if (numHoles != 0) {
260 while (--topIndex > bottomIndex && numHoles != 0) {
261 //LOG(INFO) << "+++ checking for hole at " << topIndex-1 << " (cookie=" << cookie << ") val=" << table_[topIndex-1];
262 if (table_[topIndex-1] != NULL) {
263 break;
264 }
265 //LOG(INFO) << "+++ ate hole at " << (topIndex-1);
266 numHoles--;
267 }
268 segmentState.parts.numHoles = numHoles + prevState.parts.numHoles;
269 segmentState.parts.topIndex = topIndex;
270 } else {
271 segmentState.parts.topIndex = topIndex-1;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700272 //LOG(INFO) << "+++ ate last entry " << topIndex-1;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700273 }
274 } else {
275 /*
276 * Not the top-most entry. This creates a hole. We NULL out the
277 * entry to prevent somebody from deleting it twice and screwing up
278 * the hole count.
279 */
280 if (table_[idx] == NULL) {
281 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
282 return false;
283 }
284 if (workAroundAppJniBugs == false && !CheckEntry("remove", iref, idx)) {
285 return false;
286 }
287
288 table_[idx] = NULL;
289 segmentState.parts.numHoles++;
290 //LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segmentState.parts.numHoles;
291 }
292
293 return true;
294}
295
296std::ostream& operator<<(std::ostream& os, IndirectRefKind rhs) {
297 switch (rhs) {
298 case kInvalid:
299 os << "invalid reference";
300 break;
301 case kLocal:
302 os << "local reference";
303 break;
304 case kGlobal:
305 os << "global reference";
306 break;
307 case kWeakGlobal:
308 os << "weak global reference";
309 break;
310 default:
311 os << "IndirectRefKind[" << static_cast<int>(rhs) << "]";
312 break;
313 }
314 return os;
315}
316
317void IndirectReferenceTable::Dump() const {
318 LOG(WARNING) << kind_ << " table dump:";
319 std::vector<Object*> entries(table_, table_ + Capacity());
320 ReferenceTable::Dump(entries);
321}
322
323} // namespace art