Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame^] | 18 | #include "jni_internal.h" |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 19 | #include "reference_table.h" |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame^] | 20 | #include "runtime.h" |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 21 | #include "utils.h" |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 22 | |
| 23 | #include <cstdlib> |
| 24 | |
| 25 | namespace art { |
| 26 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 27 | static void AbortMaybe() { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame^] | 28 | // 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 Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | IndirectReferenceTable::IndirectReferenceTable(size_t initialCount, |
| 36 | size_t maxCount, IndirectRefKind desiredKind) |
| 37 | { |
| 38 | CHECK_GT(initialCount, 0U); |
| 39 | CHECK_LE(initialCount, maxCount); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 40 | CHECK_NE(desiredKind, kSirtOrInvalid); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 41 | |
| 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 | |
| 57 | IndirectReferenceTable::~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 | */ |
| 68 | bool 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 | |
| 81 | IndirectRef 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 Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 87 | // TODO: stronger sanity check on the object (such as in heap) |
| 88 | DCHECK(IsAligned(reinterpret_cast<intptr_t>(obj), 8)); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 89 | 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 | */ |
| 161 | bool IndirectReferenceTable::GetChecked(IndirectRef iref) const { |
| 162 | if (iref == NULL) { |
| 163 | LOG(WARNING) << "Attempt to look up NULL " << kind_; |
| 164 | return false; |
| 165 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 166 | if (GetIndirectRefKind(iref) == kSirtOrInvalid) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 167 | 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 | |
| 194 | static 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 | |
| 203 | bool 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 | */ |
| 220 | bool 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 Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 233 | if (GetIndirectRefKind(iref) == kSirtOrInvalid /*&& gDvmJni.workAroundAppJniBugs*/) { // TODO |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 234 | 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 Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 275 | //LOG(INFO) << "+++ ate last entry " << topIndex-1; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 276 | } |
| 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 | |
| 299 | std::ostream& operator<<(std::ostream& os, IndirectRefKind rhs) { |
| 300 | switch (rhs) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 301 | case kSirtOrInvalid: |
| 302 | os << "stack indirect reference table or invalid reference"; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 303 | 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 | |
| 320 | void IndirectReferenceTable::Dump() const { |
| 321 | LOG(WARNING) << kind_ << " table dump:"; |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 322 | std::vector<const Object*> entries(table_, table_ + Capacity()); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 323 | ReferenceTable::Dump(entries); |
| 324 | } |
| 325 | |
| 326 | } // namespace art |