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 | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 21 | #include "scoped_thread_state_change.h" |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 22 | #include "thread.h" |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 23 | #include "utils.h" |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 24 | |
| 25 | #include <cstdlib> |
| 26 | |
| 27 | namespace art { |
| 28 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 29 | static void AbortMaybe() { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 30 | // If -Xcheck:jni is on, it'll give a more detailed error before aborting. |
| 31 | if (!Runtime::Current()->GetJavaVM()->check_jni) { |
| 32 | // Otherwise, we want to abort rather than hand back a bad reference. |
| 33 | LOG(FATAL) << "JNI ERROR (app bug): see above."; |
| 34 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | IndirectReferenceTable::IndirectReferenceTable(size_t initialCount, |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 38 | size_t maxCount, IndirectRefKind desiredKind) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 39 | CHECK_GT(initialCount, 0U); |
| 40 | CHECK_LE(initialCount, maxCount); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 41 | CHECK_NE(desiredKind, kSirtOrInvalid); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 42 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 43 | table_ = reinterpret_cast<const mirror::Object**>(malloc(initialCount * sizeof(const mirror::Object*))); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 44 | CHECK(table_ != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 45 | memset(table_, 0xd1, initialCount * sizeof(const mirror::Object*)); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 46 | |
| 47 | slot_data_ = reinterpret_cast<IndirectRefSlot*>(calloc(initialCount, sizeof(IndirectRefSlot))); |
| 48 | CHECK(slot_data_ != NULL); |
| 49 | |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 50 | segment_state_.all = IRT_FIRST_SEGMENT; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 51 | alloc_entries_ = initialCount; |
| 52 | max_entries_ = maxCount; |
| 53 | kind_ = desiredKind; |
| 54 | } |
| 55 | |
| 56 | IndirectReferenceTable::~IndirectReferenceTable() { |
| 57 | free(table_); |
| 58 | free(slot_data_); |
| 59 | table_ = NULL; |
| 60 | slot_data_ = NULL; |
| 61 | alloc_entries_ = max_entries_ = -1; |
| 62 | } |
| 63 | |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 64 | // Make sure that the entry at "idx" is correctly paired with "iref". |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 65 | bool IndirectReferenceTable::CheckEntry(const char* what, IndirectRef iref, int idx) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 66 | const mirror::Object* obj = table_[idx]; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 67 | IndirectRef checkRef = ToIndirectRef(obj, idx); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 68 | if (UNLIKELY(checkRef != iref)) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 69 | LOG(ERROR) << "JNI ERROR (app bug): attempt to " << what |
| 70 | << " stale " << kind_ << " " << iref |
| 71 | << " (should be " << checkRef << ")"; |
| 72 | AbortMaybe(); |
| 73 | return false; |
| 74 | } |
| 75 | return true; |
| 76 | } |
| 77 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 78 | IndirectRef IndirectReferenceTable::Add(uint32_t cookie, const mirror::Object* obj) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 79 | IRTSegmentState prevState; |
| 80 | prevState.all = cookie; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 81 | size_t topIndex = segment_state_.parts.topIndex; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 82 | |
| 83 | DCHECK(obj != NULL); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 84 | // TODO: stronger sanity check on the object (such as in heap) |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 85 | DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(obj), 8); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 86 | DCHECK(table_ != NULL); |
| 87 | DCHECK_LE(alloc_entries_, max_entries_); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 88 | DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 89 | |
| 90 | if (topIndex == alloc_entries_) { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 91 | // reached end of allocated space; did we hit buffer max? |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 92 | if (topIndex == max_entries_) { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 93 | LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow " |
| 94 | << "(max=" << max_entries_ << ")\n" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 95 | << MutatorLockedDumpable<IndirectReferenceTable>(*this); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | size_t newSize = alloc_entries_ * 2; |
| 99 | if (newSize > max_entries_) { |
| 100 | newSize = max_entries_; |
| 101 | } |
| 102 | DCHECK_GT(newSize, alloc_entries_); |
| 103 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 104 | table_ = reinterpret_cast<const mirror::Object**>(realloc(table_, newSize * sizeof(const mirror::Object*))); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 105 | slot_data_ = reinterpret_cast<IndirectRefSlot*>(realloc(slot_data_, |
| 106 | newSize * sizeof(IndirectRefSlot))); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 107 | if (table_ == NULL || slot_data_ == NULL) { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 108 | LOG(FATAL) << "JNI ERROR (app bug): unable to expand " |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 109 | << kind_ << " table (from " |
| 110 | << alloc_entries_ << " to " << newSize |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 111 | << ", max=" << max_entries_ << ")\n" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 112 | << MutatorLockedDumpable<IndirectReferenceTable>(*this); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // Clear the newly-allocated slot_data_ elements. |
| 116 | memset(slot_data_ + alloc_entries_, 0, (newSize - alloc_entries_) * sizeof(IndirectRefSlot)); |
| 117 | |
| 118 | alloc_entries_ = newSize; |
| 119 | } |
| 120 | |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 121 | // We know there's enough room in the table. Now we just need to find |
| 122 | // the right spot. If there's a hole, find it and fill it; otherwise, |
| 123 | // add to the end of the list. |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 124 | IndirectRef result; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 125 | int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 126 | if (numHoles > 0) { |
| 127 | DCHECK_GT(topIndex, 1U); |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 128 | // Find the first hole; likely to be near the end of the list. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 129 | const mirror::Object** pScan = &table_[topIndex - 1]; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 130 | DCHECK(*pScan != NULL); |
| 131 | while (*--pScan != NULL) { |
| 132 | DCHECK_GE(pScan, table_ + prevState.parts.topIndex); |
| 133 | } |
| 134 | UpdateSlotAdd(obj, pScan - table_); |
| 135 | result = ToIndirectRef(obj, pScan - table_); |
| 136 | *pScan = obj; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 137 | segment_state_.parts.numHoles--; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 138 | } else { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 139 | // Add to the end. |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 140 | UpdateSlotAdd(obj, topIndex); |
| 141 | result = ToIndirectRef(obj, topIndex); |
| 142 | table_[topIndex++] = obj; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 143 | segment_state_.parts.topIndex = topIndex; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 144 | } |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 145 | if (false) { |
| 146 | LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex |
| 147 | << " holes=" << segment_state_.parts.numHoles; |
| 148 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 149 | |
| 150 | DCHECK(result != NULL); |
| 151 | return result; |
| 152 | } |
| 153 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 154 | void IndirectReferenceTable::AssertEmpty() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 155 | if (UNLIKELY(begin() != end())) { |
| 156 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 157 | LOG(FATAL) << "Internal Error: non-empty local reference table\n" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 158 | << MutatorLockedDumpable<IndirectReferenceTable>(*this); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 162 | // Verifies that the indirect table lookup is valid. |
| 163 | // Returns "false" if something looks bad. |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 164 | bool IndirectReferenceTable::GetChecked(IndirectRef iref) const { |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 165 | if (UNLIKELY(iref == NULL)) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 166 | LOG(WARNING) << "Attempt to look up NULL " << kind_; |
| 167 | return false; |
| 168 | } |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 169 | if (UNLIKELY(GetIndirectRefKind(iref) == kSirtOrInvalid)) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 170 | LOG(ERROR) << "JNI ERROR (app bug): invalid " << kind_ << " " << iref; |
| 171 | AbortMaybe(); |
| 172 | return false; |
| 173 | } |
| 174 | |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 175 | int topIndex = segment_state_.parts.topIndex; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 176 | int idx = ExtractIndex(iref); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 177 | if (UNLIKELY(idx >= topIndex)) { |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 178 | LOG(ERROR) << "JNI ERROR (app bug): accessed stale " << kind_ << " " |
| 179 | << iref << " (index " << idx << " in a table of size " << topIndex << ")"; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 180 | AbortMaybe(); |
| 181 | return false; |
| 182 | } |
| 183 | |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 184 | if (UNLIKELY(table_[idx] == NULL)) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 185 | LOG(ERROR) << "JNI ERROR (app bug): accessed deleted " << kind_ << " " << iref; |
| 186 | AbortMaybe(); |
| 187 | return false; |
| 188 | } |
| 189 | |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 190 | if (UNLIKELY(!CheckEntry("use", iref, idx))) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 191 | return false; |
| 192 | } |
| 193 | |
| 194 | return true; |
| 195 | } |
| 196 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 197 | static int Find(mirror::Object* direct_pointer, int bottomIndex, int topIndex, const mirror::Object** table) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 198 | for (int i = bottomIndex; i < topIndex; ++i) { |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 199 | if (table[i] == direct_pointer) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 200 | return i; |
| 201 | } |
| 202 | } |
| 203 | return -1; |
| 204 | } |
| 205 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 206 | bool IndirectReferenceTable::ContainsDirectPointer(mirror::Object* direct_pointer) const { |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 207 | return Find(direct_pointer, 0, segment_state_.parts.topIndex, table_) != -1; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 210 | // Removes an object. We extract the table offset bits from "iref" |
| 211 | // and zap the corresponding entry, leaving a hole if it's not at the top. |
| 212 | // If the entry is not between the current top index and the bottom index |
| 213 | // specified by the cookie, we don't remove anything. This is the behavior |
| 214 | // required by JNI's DeleteLocalRef function. |
| 215 | // This method is not called when a local frame is popped; this is only used |
| 216 | // for explicit single removals. |
| 217 | // Returns "false" if nothing was removed. |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 218 | bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) { |
| 219 | IRTSegmentState prevState; |
| 220 | prevState.all = cookie; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 221 | int topIndex = segment_state_.parts.topIndex; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 222 | int bottomIndex = prevState.parts.topIndex; |
| 223 | |
| 224 | DCHECK(table_ != NULL); |
| 225 | DCHECK_LE(alloc_entries_, max_entries_); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 226 | DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 227 | |
| 228 | int idx = ExtractIndex(iref); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 229 | |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 230 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 231 | if (GetIndirectRefKind(iref) == kSirtOrInvalid && |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 232 | Thread::Current()->SirtContains(reinterpret_cast<jobject>(iref))) { |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 233 | LOG(WARNING) << "Attempt to remove local SIRT entry from IRT, ignoring"; |
| 234 | return true; |
| 235 | } |
Ian Rogers | 467c969 | 2012-02-21 11:05:16 -0800 | [diff] [blame] | 236 | if (GetIndirectRefKind(iref) == kSirtOrInvalid && vm->work_around_app_jni_bugs) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 237 | mirror::Object* direct_pointer = reinterpret_cast<mirror::Object*>(iref); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 238 | idx = Find(direct_pointer, bottomIndex, topIndex, table_); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 239 | if (idx == -1) { |
Elliott Hughes | 9dcd45c | 2013-07-29 14:40:52 -0700 | [diff] [blame] | 240 | LOG(WARNING) << "Trying to work around app JNI bugs, but didn't find " << iref << " in table!"; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | if (idx < bottomIndex) { |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 246 | // Wrong segment. |
| 247 | LOG(WARNING) << "Attempt to remove index outside index area (" << idx |
| 248 | << " vs " << bottomIndex << "-" << topIndex << ")"; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 249 | return false; |
| 250 | } |
| 251 | if (idx >= topIndex) { |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 252 | // Bad --- stale reference? |
| 253 | LOG(WARNING) << "Attempt to remove invalid index " << idx |
| 254 | << " (bottom=" << bottomIndex << " top=" << topIndex << ")"; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 255 | return false; |
| 256 | } |
| 257 | |
| 258 | if (idx == topIndex-1) { |
| 259 | // Top-most entry. Scan up and consume holes. |
| 260 | |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 261 | if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 262 | return false; |
| 263 | } |
| 264 | |
| 265 | table_[idx] = NULL; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 266 | int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 267 | if (numHoles != 0) { |
| 268 | while (--topIndex > bottomIndex && numHoles != 0) { |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 269 | if (false) { |
| 270 | LOG(INFO) << "+++ checking for hole at " << topIndex-1 |
| 271 | << " (cookie=" << cookie << ") val=" << table_[topIndex - 1]; |
| 272 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 273 | if (table_[topIndex-1] != NULL) { |
| 274 | break; |
| 275 | } |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 276 | if (false) { |
| 277 | LOG(INFO) << "+++ ate hole at " << (topIndex - 1); |
| 278 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 279 | numHoles--; |
| 280 | } |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 281 | segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles; |
| 282 | segment_state_.parts.topIndex = topIndex; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 283 | } else { |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 284 | segment_state_.parts.topIndex = topIndex-1; |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 285 | if (false) { |
| 286 | LOG(INFO) << "+++ ate last entry " << topIndex - 1; |
| 287 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 288 | } |
| 289 | } else { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 290 | // Not the top-most entry. This creates a hole. We NULL out the |
| 291 | // entry to prevent somebody from deleting it twice and screwing up |
| 292 | // the hole count. |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 293 | if (table_[idx] == NULL) { |
| 294 | LOG(INFO) << "--- WEIRD: removing null entry " << idx; |
| 295 | return false; |
| 296 | } |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 297 | if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 298 | return false; |
| 299 | } |
| 300 | |
| 301 | table_[idx] = NULL; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 302 | segment_state_.parts.numHoles++; |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 303 | if (false) { |
| 304 | LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles; |
| 305 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | return true; |
| 309 | } |
| 310 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 311 | void IndirectReferenceTable::VisitRoots(RootVisitor* visitor, void* arg) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 312 | typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 313 | for (It it = begin(), end = this->end(); it != end; ++it) { |
| 314 | visitor(**it, arg); |
| 315 | } |
| 316 | } |
| 317 | |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 318 | void IndirectReferenceTable::Dump(std::ostream& os) const { |
| 319 | os << kind_ << " table dump:\n"; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 320 | std::vector<const mirror::Object*> entries(table_, table_ + Capacity()); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 321 | // Remove NULLs. |
| 322 | for (int i = entries.size() - 1; i >= 0; --i) { |
| 323 | if (entries[i] == NULL) { |
| 324 | entries.erase(entries.begin() + i); |
| 325 | } |
| 326 | } |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 327 | ReferenceTable::Dump(os, entries); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | } // namespace art |