indirect_reference_table.cc revision 726079d3e2e50854cd6ca4c393f4529a796dba58
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" 18#include "jni_internal.h" 19#include "reference_table.h" 20#include "runtime.h" 21#include "thread.h" 22#include "utils.h" 23 24#include <cstdlib> 25 26namespace art { 27 28static void AbortMaybe() { 29 // 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 } 34} 35 36IndirectReferenceTable::IndirectReferenceTable(size_t initialCount, 37 size_t maxCount, IndirectRefKind desiredKind) 38{ 39 CHECK_GT(initialCount, 0U); 40 CHECK_LE(initialCount, maxCount); 41 CHECK_NE(desiredKind, kSirtOrInvalid); 42 43 table_ = reinterpret_cast<const Object**>(malloc(initialCount * sizeof(const Object*))); 44 CHECK(table_ != NULL); 45#ifndef NDEBUG 46 memset(table_, 0xd1, initialCount * sizeof(const Object*)); 47#endif 48 49 slot_data_ = reinterpret_cast<IndirectRefSlot*>(calloc(initialCount, sizeof(IndirectRefSlot))); 50 CHECK(slot_data_ != NULL); 51 52 segment_state_.all = IRT_FIRST_SEGMENT; 53 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 { 70 const Object* obj = table_[idx]; 71 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 82IndirectRef IndirectReferenceTable::Add(uint32_t cookie, const Object* obj) { 83 IRTSegmentState prevState; 84 prevState.all = cookie; 85 size_t topIndex = segment_state_.parts.topIndex; 86 87 DCHECK(obj != NULL); 88 // TODO: stronger sanity check on the object (such as in heap) 89 DCHECK(IsAligned(reinterpret_cast<intptr_t>(obj), 8)) << reinterpret_cast<const void*>(obj); 90 DCHECK(table_ != NULL); 91 DCHECK_LE(alloc_entries_, max_entries_); 92 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles); 93 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 109 table_ = (const Object**) realloc(table_, newSize * sizeof(const Object*)); 110 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; 132 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles; 133 if (numHoles > 0) { 134 DCHECK_GT(topIndex, 1U); 135 /* find the first hole; likely to be near the end of the list */ 136 const Object** pScan = &table_[topIndex - 1]; 137 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; 144 segment_state_.parts.numHoles--; 145 } else { 146 /* add to the end */ 147 UpdateSlotAdd(obj, topIndex); 148 result = ToIndirectRef(obj, topIndex); 149 table_[topIndex++] = obj; 150 segment_state_.parts.topIndex = topIndex; 151 } 152 if (false) { 153 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex 154 << " holes=" << segment_state_.parts.numHoles; 155 } 156 157 DCHECK(result != NULL); 158 return result; 159} 160 161void IndirectReferenceTable::AssertEmpty() { 162 if (begin() != end()) { 163 Dump(); 164 LOG(FATAL) << "Internal Error: non-empty local reference table"; 165 } 166} 167 168/* 169 * Verify that the indirect table lookup is valid. 170 * 171 * Returns "false" if something looks bad. 172 */ 173bool IndirectReferenceTable::GetChecked(IndirectRef iref) const { 174 if (iref == NULL) { 175 LOG(WARNING) << "Attempt to look up NULL " << kind_; 176 return false; 177 } 178 if (GetIndirectRefKind(iref) == kSirtOrInvalid) { 179 LOG(ERROR) << "JNI ERROR (app bug): invalid " << kind_ << " " << iref; 180 AbortMaybe(); 181 return false; 182 } 183 184 int topIndex = segment_state_.parts.topIndex; 185 int idx = ExtractIndex(iref); 186 if (idx >= topIndex) { 187 /* bad -- stale reference? */ 188 LOG(ERROR) << "JNI ERROR (app bug): accessed stale " << kind_ << " " 189 << iref << " (index " << idx << " in a table of size " << topIndex << ")"; 190 AbortMaybe(); 191 return false; 192 } 193 194 if (table_[idx] == NULL) { 195 LOG(ERROR) << "JNI ERROR (app bug): accessed deleted " << kind_ << " " << iref; 196 AbortMaybe(); 197 return false; 198 } 199 200 if (!CheckEntry("use", iref, idx)) { 201 return false; 202 } 203 204 return true; 205} 206 207static int LinearScan(IndirectRef iref, int bottomIndex, int topIndex, const Object** table) { 208 for (int i = bottomIndex; i < topIndex; ++i) { 209 if (table[i] == reinterpret_cast<const Object*>(iref)) { 210 return i; 211 } 212 } 213 return -1; 214} 215 216bool IndirectReferenceTable::Contains(IndirectRef iref) const { 217 return LinearScan(iref, 0, segment_state_.parts.topIndex, table_) != -1; 218} 219 220/* 221 * Remove "obj" from "pRef". We extract the table offset bits from "iref" 222 * and zap the corresponding entry, leaving a hole if it's not at the top. 223 * 224 * If the entry is not between the current top index and the bottom index 225 * specified by the cookie, we don't remove anything. This is the behavior 226 * required by JNI's DeleteLocalRef function. 227 * 228 * Note this is NOT called when a local frame is popped. This is only used 229 * for explicit single removals. 230 * 231 * Returns "false" if nothing was removed. 232 */ 233bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) { 234 IRTSegmentState prevState; 235 prevState.all = cookie; 236 int topIndex = segment_state_.parts.topIndex; 237 int bottomIndex = prevState.parts.topIndex; 238 239 DCHECK(table_ != NULL); 240 DCHECK_LE(alloc_entries_, max_entries_); 241 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles); 242 243 int idx = ExtractIndex(iref); 244 245 JavaVMExt* vm = Runtime::Current()->GetJavaVM(); 246 if (GetIndirectRefKind(iref) == kSirtOrInvalid && 247 Thread::Current()->SirtContains(reinterpret_cast<jobject>(iref))) { 248 LOG(WARNING) << "Attempt to remove local SIRT entry from IRT, ignoring"; 249 return true; 250 } 251 if (GetIndirectRefKind(iref) == kSirtOrInvalid || vm->work_around_app_jni_bugs) { 252 idx = LinearScan(iref, bottomIndex, topIndex, table_); 253 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) { 260 // Wrong segment. 261 LOG(WARNING) << "Attempt to remove index outside index area (" << idx 262 << " vs " << bottomIndex << "-" << topIndex << ")"; 263 return false; 264 } 265 if (idx >= topIndex) { 266 // Bad --- stale reference? 267 LOG(WARNING) << "Attempt to remove invalid index " << idx 268 << " (bottom=" << bottomIndex << " top=" << topIndex << ")"; 269 return false; 270 } 271 272 if (idx == topIndex-1) { 273 // Top-most entry. Scan up and consume holes. 274 275 if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) { 276 return false; 277 } 278 279 table_[idx] = NULL; 280 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles; 281 if (numHoles != 0) { 282 while (--topIndex > bottomIndex && numHoles != 0) { 283 if (false) { 284 LOG(INFO) << "+++ checking for hole at " << topIndex-1 285 << " (cookie=" << cookie << ") val=" << table_[topIndex - 1]; 286 } 287 if (table_[topIndex-1] != NULL) { 288 break; 289 } 290 if (false) { 291 LOG(INFO) << "+++ ate hole at " << (topIndex - 1); 292 } 293 numHoles--; 294 } 295 segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles; 296 segment_state_.parts.topIndex = topIndex; 297 } else { 298 segment_state_.parts.topIndex = topIndex-1; 299 if (false) { 300 LOG(INFO) << "+++ ate last entry " << topIndex - 1; 301 } 302 } 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 } 313 if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) { 314 return false; 315 } 316 317 table_[idx] = NULL; 318 segment_state_.parts.numHoles++; 319 if (false) { 320 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles; 321 } 322 } 323 324 return true; 325} 326 327void 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 334std::ostream& operator<<(std::ostream& os, IndirectRefKind rhs) { 335 switch (rhs) { 336 case kSirtOrInvalid: 337 os << "stack indirect reference table or invalid reference"; 338 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:"; 357 std::vector<const Object*> entries(table_, table_ + Capacity()); 358 ReferenceTable::Dump(entries); 359} 360 361} // namespace art 362