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 | |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 257 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 258 | if (GetIndirectRefKind(iref) == kSirtOrInvalid && |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 259 | Thread::Current()->SirtContains(reinterpret_cast<jobject>(iref))) { |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 260 | LOG(WARNING) << "Attempt to remove local SIRT entry from IRT, ignoring"; |
| 261 | return true; |
| 262 | } |
Ian Rogers | 467c969 | 2012-02-21 11:05:16 -0800 | [diff] [blame] | 263 | if (GetIndirectRefKind(iref) == kSirtOrInvalid && vm->work_around_app_jni_bugs) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 264 | mirror::Object* direct_pointer = reinterpret_cast<mirror::Object*>(iref); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 265 | idx = Find(direct_pointer, bottomIndex, topIndex, table_); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 266 | if (idx == -1) { |
Elliott Hughes | 9dcd45c | 2013-07-29 14:40:52 -0700 | [diff] [blame] | 267 | 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] | 268 | return false; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | if (idx < bottomIndex) { |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 273 | // Wrong segment. |
| 274 | LOG(WARNING) << "Attempt to remove index outside index area (" << idx |
| 275 | << " vs " << bottomIndex << "-" << topIndex << ")"; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 276 | return false; |
| 277 | } |
| 278 | if (idx >= topIndex) { |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 279 | // Bad --- stale reference? |
| 280 | LOG(WARNING) << "Attempt to remove invalid index " << idx |
| 281 | << " (bottom=" << bottomIndex << " top=" << topIndex << ")"; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 282 | return false; |
| 283 | } |
| 284 | |
| 285 | if (idx == topIndex-1) { |
| 286 | // Top-most entry. Scan up and consume holes. |
| 287 | |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 288 | if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 289 | return false; |
| 290 | } |
| 291 | |
| 292 | table_[idx] = NULL; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 293 | int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 294 | if (numHoles != 0) { |
| 295 | while (--topIndex > bottomIndex && numHoles != 0) { |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 296 | if (false) { |
| 297 | LOG(INFO) << "+++ checking for hole at " << topIndex-1 |
| 298 | << " (cookie=" << cookie << ") val=" << table_[topIndex - 1]; |
| 299 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 300 | if (table_[topIndex-1] != NULL) { |
| 301 | break; |
| 302 | } |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 303 | if (false) { |
| 304 | LOG(INFO) << "+++ ate hole at " << (topIndex - 1); |
| 305 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 306 | numHoles--; |
| 307 | } |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 308 | segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles; |
| 309 | segment_state_.parts.topIndex = topIndex; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 310 | } else { |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 311 | segment_state_.parts.topIndex = topIndex-1; |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 312 | if (false) { |
| 313 | LOG(INFO) << "+++ ate last entry " << topIndex - 1; |
| 314 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 315 | } |
| 316 | } else { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 317 | // Not the top-most entry. This creates a hole. We NULL out the |
| 318 | // entry to prevent somebody from deleting it twice and screwing up |
| 319 | // the hole count. |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 320 | if (table_[idx] == NULL) { |
| 321 | LOG(INFO) << "--- WEIRD: removing null entry " << idx; |
| 322 | return false; |
| 323 | } |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 324 | if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 325 | return false; |
| 326 | } |
| 327 | |
| 328 | table_[idx] = NULL; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 329 | segment_state_.parts.numHoles++; |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 330 | if (false) { |
| 331 | LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles; |
| 332 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | return true; |
| 336 | } |
| 337 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 338 | void IndirectReferenceTable::VisitRoots(RootCallback* callback, void* arg, uint32_t tid, |
| 339 | RootType root_type) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 340 | for (auto ref : *this) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 341 | callback(ref, arg, tid, root_type); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 342 | DCHECK(*ref != nullptr); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 346 | void IndirectReferenceTable::Dump(std::ostream& os) const { |
| 347 | os << kind_ << " table dump:\n"; |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 348 | ReferenceTable::Table entries(table_, table_ + Capacity()); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 349 | // Remove NULLs. |
| 350 | for (int i = entries.size() - 1; i >= 0; --i) { |
| 351 | if (entries[i] == NULL) { |
| 352 | entries.erase(entries.begin() + i); |
| 353 | } |
| 354 | } |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 355 | ReferenceTable::Dump(os, entries); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Mathieu Chartier | c645f1d | 2014-03-06 18:11:53 -0800 | [diff] [blame] | 358 | mirror::Object* IndirectReferenceTable::Get(IndirectRef iref) const { |
| 359 | if (!GetChecked(iref)) { |
| 360 | return kInvalidIndirectRefObject; |
| 361 | } |
| 362 | mirror::Object* obj = table_[ExtractIndex(iref)];; |
| 363 | VerifyObject(obj); |
| 364 | return obj; |
| 365 | } |
| 366 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 367 | } // namespace art |