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