blob: ed3fb5fa0cba6329d1c46e53f17e273587743700 [file] [log] [blame]
Elliott Hughes6c1a3942011-08-17 15:00:06 -07001/*
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 Hughesa2501992011-08-26 19:39:54 -070018#include "jni_internal.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070019#include "reference_table.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070020#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070021#include "scoped_thread_state_change.h"
Ian Rogers5a7a74a2011-09-26 16:32:29 -070022#include "thread.h"
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070023#include "utils.h"
Mathieu Chartier6dda8982014-03-06 11:11:48 -080024#include "verify_object-inl.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070025
26#include <cstdlib>
27
28namespace art {
29
Ian Rogers719d1a32014-03-06 12:13:39 -080030template<typename T>
31class 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
47template<typename T>
48std::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 Hughes6c1a3942011-08-17 15:00:06 -070056static void AbortMaybe() {
Elliott Hughesa2501992011-08-26 19:39:54 -070057 // 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 Hughes6c1a3942011-08-17 15:00:06 -070062}
63
64IndirectReferenceTable::IndirectReferenceTable(size_t initialCount,
Elliott Hughesba8eee12012-01-24 20:25:24 -080065 size_t maxCount, IndirectRefKind desiredKind) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070066 CHECK_GT(initialCount, 0U);
67 CHECK_LE(initialCount, maxCount);
Ian Rogers408f79a2011-08-23 18:22:33 -070068 CHECK_NE(desiredKind, kSirtOrInvalid);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070069
Mathieu Chartier423d2a32013-09-12 17:33:56 -070070 table_ = reinterpret_cast<mirror::Object**>(malloc(initialCount * sizeof(const mirror::Object*)));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070071 CHECK(table_ != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072 memset(table_, 0xd1, initialCount * sizeof(const mirror::Object*));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070073
74 slot_data_ = reinterpret_cast<IndirectRefSlot*>(calloc(initialCount, sizeof(IndirectRefSlot)));
75 CHECK(slot_data_ != NULL);
76
Ian Rogersdc51b792011-09-22 20:41:37 -070077 segment_state_.all = IRT_FIRST_SEGMENT;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070078 alloc_entries_ = initialCount;
79 max_entries_ = maxCount;
80 kind_ = desiredKind;
81}
82
83IndirectReferenceTable::~IndirectReferenceTable() {
84 free(table_);
85 free(slot_data_);
86 table_ = NULL;
87 slot_data_ = NULL;
88 alloc_entries_ = max_entries_ = -1;
89}
90
Elliott Hughes73e66f72012-05-09 09:34:45 -070091// Make sure that the entry at "idx" is correctly paired with "iref".
Elliott Hughes6c1a3942011-08-17 15:00:06 -070092bool IndirectReferenceTable::CheckEntry(const char* what, IndirectRef iref, int idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 const mirror::Object* obj = table_[idx];
Elliott Hughes6c1a3942011-08-17 15:00:06 -070094 IndirectRef checkRef = ToIndirectRef(obj, idx);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070095 if (UNLIKELY(checkRef != iref)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070096 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 Chartier423d2a32013-09-12 17:33:56 -0700105IndirectRef IndirectReferenceTable::Add(uint32_t cookie, mirror::Object* obj) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700106 IRTSegmentState prevState;
107 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700108 size_t topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700109
Mathieu Chartiercd2cfff2013-12-19 15:46:55 -0800110 CHECK(obj != NULL);
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800111 VerifyObject(obj);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700112 DCHECK(table_ != NULL);
113 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -0700114 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700115
116 if (topIndex == alloc_entries_) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700117 // reached end of allocated space; did we hit buffer max?
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700118 if (topIndex == max_entries_) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700119 LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow "
120 << "(max=" << max_entries_ << ")\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700121 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700122 }
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 Chartier423d2a32013-09-12 17:33:56 -0700130 table_ = reinterpret_cast<mirror::Object**>(realloc(table_, newSize * sizeof(mirror::Object*)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700131 slot_data_ = reinterpret_cast<IndirectRefSlot*>(realloc(slot_data_,
132 newSize * sizeof(IndirectRefSlot)));
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700133 if (table_ == NULL || slot_data_ == NULL) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700134 LOG(FATAL) << "JNI ERROR (app bug): unable to expand "
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700135 << kind_ << " table (from "
136 << alloc_entries_ << " to " << newSize
Elliott Hughes73e66f72012-05-09 09:34:45 -0700137 << ", max=" << max_entries_ << ")\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700138 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700139 }
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 Hughes73e66f72012-05-09 09:34:45 -0700147 // 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 Hughes6c1a3942011-08-17 15:00:06 -0700150 IndirectRef result;
Ian Rogersdc51b792011-09-22 20:41:37 -0700151 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700152 if (numHoles > 0) {
153 DCHECK_GT(topIndex, 1U);
Elliott Hughes73e66f72012-05-09 09:34:45 -0700154 // Find the first hole; likely to be near the end of the list.
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700155 mirror::Object** pScan = &table_[topIndex - 1];
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700156 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 Rogersdc51b792011-09-22 20:41:37 -0700163 segment_state_.parts.numHoles--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700164 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700165 // Add to the end.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700166 UpdateSlotAdd(obj, topIndex);
167 result = ToIndirectRef(obj, topIndex);
168 table_[topIndex++] = obj;
Ian Rogersdc51b792011-09-22 20:41:37 -0700169 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700170 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700171 if (false) {
172 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex
173 << " holes=" << segment_state_.parts.numHoles;
174 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700175
176 DCHECK(result != NULL);
177 return result;
178}
179
Elliott Hughes726079d2011-10-07 18:43:44 -0700180void IndirectReferenceTable::AssertEmpty() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700181 if (UNLIKELY(begin() != end())) {
182 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes73e66f72012-05-09 09:34:45 -0700183 LOG(FATAL) << "Internal Error: non-empty local reference table\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700184 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes726079d2011-10-07 18:43:44 -0700185 }
186}
187
Elliott Hughes73e66f72012-05-09 09:34:45 -0700188// Verifies that the indirect table lookup is valid.
189// Returns "false" if something looks bad.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700190bool IndirectReferenceTable::GetChecked(IndirectRef iref) const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700191 if (UNLIKELY(iref == NULL)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700192 LOG(WARNING) << "Attempt to look up NULL " << kind_;
193 return false;
194 }
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700195 if (UNLIKELY(GetIndirectRefKind(iref) == kSirtOrInvalid)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700196 LOG(ERROR) << "JNI ERROR (app bug): invalid " << kind_ << " " << iref;
197 AbortMaybe();
198 return false;
199 }
200
Ian Rogersdc51b792011-09-22 20:41:37 -0700201 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700202 int idx = ExtractIndex(iref);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700203 if (UNLIKELY(idx >= topIndex)) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700204 LOG(ERROR) << "JNI ERROR (app bug): accessed stale " << kind_ << " "
205 << iref << " (index " << idx << " in a table of size " << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700206 AbortMaybe();
207 return false;
208 }
209
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700210 if (UNLIKELY(table_[idx] == NULL)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700211 LOG(ERROR) << "JNI ERROR (app bug): accessed deleted " << kind_ << " " << iref;
212 AbortMaybe();
213 return false;
214 }
215
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700216 if (UNLIKELY(!CheckEntry("use", iref, idx))) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700217 return false;
218 }
219
220 return true;
221}
222
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700223static int Find(mirror::Object* direct_pointer, int bottomIndex, int topIndex,
224 mirror::Object** table) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700225 for (int i = bottomIndex; i < topIndex; ++i) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700226 if (table[i] == direct_pointer) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700227 return i;
228 }
229 }
230 return -1;
231}
232
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233bool IndirectReferenceTable::ContainsDirectPointer(mirror::Object* direct_pointer) const {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700234 return Find(direct_pointer, 0, segment_state_.parts.topIndex, table_) != -1;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700235}
236
Elliott Hughes73e66f72012-05-09 09:34:45 -0700237// 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 Hughes6c1a3942011-08-17 15:00:06 -0700245bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
246 IRTSegmentState prevState;
247 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700248 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700249 int bottomIndex = prevState.parts.topIndex;
250
251 DCHECK(table_ != NULL);
252 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -0700253 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700254
255 int idx = ExtractIndex(iref);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700256
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700257 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700258 if (GetIndirectRefKind(iref) == kSirtOrInvalid &&
Ian Rogers0399dde2012-06-06 17:09:28 -0700259 Thread::Current()->SirtContains(reinterpret_cast<jobject>(iref))) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700260 LOG(WARNING) << "Attempt to remove local SIRT entry from IRT, ignoring";
261 return true;
262 }
Ian Rogers467c9692012-02-21 11:05:16 -0800263 if (GetIndirectRefKind(iref) == kSirtOrInvalid && vm->work_around_app_jni_bugs) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264 mirror::Object* direct_pointer = reinterpret_cast<mirror::Object*>(iref);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700265 idx = Find(direct_pointer, bottomIndex, topIndex, table_);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700266 if (idx == -1) {
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700267 LOG(WARNING) << "Trying to work around app JNI bugs, but didn't find " << iref << " in table!";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700268 return false;
269 }
270 }
271
272 if (idx < bottomIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700273 // Wrong segment.
274 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
275 << " vs " << bottomIndex << "-" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700276 return false;
277 }
278 if (idx >= topIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700279 // Bad --- stale reference?
280 LOG(WARNING) << "Attempt to remove invalid index " << idx
281 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700282 return false;
283 }
284
285 if (idx == topIndex-1) {
286 // Top-most entry. Scan up and consume holes.
287
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700288 if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700289 return false;
290 }
291
292 table_[idx] = NULL;
Ian Rogersdc51b792011-09-22 20:41:37 -0700293 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700294 if (numHoles != 0) {
295 while (--topIndex > bottomIndex && numHoles != 0) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700296 if (false) {
297 LOG(INFO) << "+++ checking for hole at " << topIndex-1
298 << " (cookie=" << cookie << ") val=" << table_[topIndex - 1];
299 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700300 if (table_[topIndex-1] != NULL) {
301 break;
302 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700303 if (false) {
304 LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
305 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700306 numHoles--;
307 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700308 segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
309 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700310 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700311 segment_state_.parts.topIndex = topIndex-1;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700312 if (false) {
313 LOG(INFO) << "+++ ate last entry " << topIndex - 1;
314 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700315 }
316 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700317 // 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 Hughes6c1a3942011-08-17 15:00:06 -0700320 if (table_[idx] == NULL) {
321 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
322 return false;
323 }
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700324 if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700325 return false;
326 }
327
328 table_[idx] = NULL;
Ian Rogersdc51b792011-09-22 20:41:37 -0700329 segment_state_.parts.numHoles++;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700330 if (false) {
331 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
332 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700333 }
334
335 return true;
336}
337
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800338void IndirectReferenceTable::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
339 RootType root_type) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700340 for (auto ref : *this) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800341 callback(ref, arg, tid, root_type);
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700342 DCHECK(*ref != nullptr);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700343 }
344}
345
Elliott Hughes73e66f72012-05-09 09:34:45 -0700346void IndirectReferenceTable::Dump(std::ostream& os) const {
347 os << kind_ << " table dump:\n";
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700348 ReferenceTable::Table entries(table_, table_ + Capacity());
Ian Rogers63818dc2012-09-26 12:23:04 -0700349 // 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 Hughes73e66f72012-05-09 09:34:45 -0700355 ReferenceTable::Dump(os, entries);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700356}
357
Mathieu Chartierc645f1d2014-03-06 18:11:53 -0800358mirror::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 Hughes6c1a3942011-08-17 15:00:06 -0700367} // namespace art