blob: 26ddba23f29482ead3d3c6f242991f50120237ea [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
Mathieu Chartierc56057e2014-05-04 13:18:58 -070017#include "indirect_reference_table-inl.h"
18
Elliott Hughesa2501992011-08-26 19:39:54 -070019#include "jni_internal.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070020#include "reference_table.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070021#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070022#include "scoped_thread_state_change.h"
Ian Rogers5a7a74a2011-09-26 16:32:29 -070023#include "thread.h"
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070024#include "utils.h"
Mathieu Chartier6dda8982014-03-06 11:11:48 -080025#include "verify_object-inl.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070026
27#include <cstdlib>
28
29namespace art {
30
Ian Rogers719d1a32014-03-06 12:13:39 -080031template<typename T>
32class MutatorLockedDumpable {
33 public:
34 explicit MutatorLockedDumpable(T& value)
35 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : value_(value) {
36 }
37
38 void Dump(std::ostream& os) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
39 value_.Dump(os);
40 }
41
42 private:
43 T& value_;
44
45 DISALLOW_COPY_AND_ASSIGN(MutatorLockedDumpable);
46};
47
48template<typename T>
49std::ostream& operator<<(std::ostream& os, const MutatorLockedDumpable<T>& rhs)
50// TODO: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) however annotalysis
51// currently fails for this.
52 NO_THREAD_SAFETY_ANALYSIS {
53 rhs.Dump(os);
54 return os;
55}
56
Mathieu Chartierc56057e2014-05-04 13:18:58 -070057void IndirectReferenceTable::AbortIfNoCheckJNI() {
Elliott Hughesa2501992011-08-26 19:39:54 -070058 // If -Xcheck:jni is on, it'll give a more detailed error before aborting.
59 if (!Runtime::Current()->GetJavaVM()->check_jni) {
60 // Otherwise, we want to abort rather than hand back a bad reference.
61 LOG(FATAL) << "JNI ERROR (app bug): see above.";
62 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -070063}
64
65IndirectReferenceTable::IndirectReferenceTable(size_t initialCount,
Elliott Hughesba8eee12012-01-24 20:25:24 -080066 size_t maxCount, IndirectRefKind desiredKind) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070067 CHECK_GT(initialCount, 0U);
68 CHECK_LE(initialCount, maxCount);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070069 CHECK_NE(desiredKind, kHandleScopeOrInvalid);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070070
Mathieu Chartierc56057e2014-05-04 13:18:58 -070071 std::string error_str;
72 const size_t initial_bytes = initialCount * sizeof(const mirror::Object*);
73 const size_t table_bytes = maxCount * sizeof(const mirror::Object*);
74 table_mem_map_.reset(MemMap::MapAnonymous("indirect ref table", nullptr, table_bytes,
75 PROT_READ | PROT_WRITE, false, &error_str));
76 CHECK(table_mem_map_.get() != nullptr) << error_str;
Alex Lighta59dd802014-07-02 16:28:08 -070077 CHECK_EQ(table_mem_map_->Size(), table_bytes);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070078
Mathieu Chartierc56057e2014-05-04 13:18:58 -070079 table_ = reinterpret_cast<mirror::Object**>(table_mem_map_->Begin());
80 CHECK(table_ != nullptr);
81 memset(table_, 0xd1, initial_bytes);
82
83 const size_t slot_bytes = maxCount * sizeof(IndirectRefSlot);
84 slot_mem_map_.reset(MemMap::MapAnonymous("indirect ref table slots", nullptr, slot_bytes,
85 PROT_READ | PROT_WRITE, false, &error_str));
86 CHECK(slot_mem_map_.get() != nullptr) << error_str;
87 slot_data_ = reinterpret_cast<IndirectRefSlot*>(slot_mem_map_->Begin());
88 CHECK(slot_data_ != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070089
Ian Rogersdc51b792011-09-22 20:41:37 -070090 segment_state_.all = IRT_FIRST_SEGMENT;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070091 alloc_entries_ = initialCount;
92 max_entries_ = maxCount;
93 kind_ = desiredKind;
94}
95
96IndirectReferenceTable::~IndirectReferenceTable() {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070097}
98
Mathieu Chartier423d2a32013-09-12 17:33:56 -070099IndirectRef IndirectReferenceTable::Add(uint32_t cookie, mirror::Object* obj) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700100 IRTSegmentState prevState;
101 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700102 size_t topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700103
Mathieu Chartiercd2cfff2013-12-19 15:46:55 -0800104 CHECK(obj != NULL);
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800105 VerifyObject(obj);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700106 DCHECK(table_ != NULL);
107 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -0700108 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700109
110 if (topIndex == alloc_entries_) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700111 // reached end of allocated space; did we hit buffer max?
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700112 if (topIndex == max_entries_) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700113 LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow "
114 << "(max=" << max_entries_ << ")\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700115 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700116 }
117
118 size_t newSize = alloc_entries_ * 2;
119 if (newSize > max_entries_) {
120 newSize = max_entries_;
121 }
122 DCHECK_GT(newSize, alloc_entries_);
123
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700124 alloc_entries_ = newSize;
125 }
126
Elliott Hughes73e66f72012-05-09 09:34:45 -0700127 // 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.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700130 IndirectRef result;
Ian Rogersdc51b792011-09-22 20:41:37 -0700131 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700132 if (numHoles > 0) {
133 DCHECK_GT(topIndex, 1U);
Elliott Hughes73e66f72012-05-09 09:34:45 -0700134 // Find the first hole; likely to be near the end of the list.
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700135 mirror::Object** pScan = &table_[topIndex - 1];
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700136 DCHECK(*pScan != NULL);
137 while (*--pScan != NULL) {
138 DCHECK_GE(pScan, table_ + prevState.parts.topIndex);
139 }
140 UpdateSlotAdd(obj, pScan - table_);
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700141 result = ToIndirectRef(pScan - table_);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700142 *pScan = obj;
Ian Rogersdc51b792011-09-22 20:41:37 -0700143 segment_state_.parts.numHoles--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700144 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700145 // Add to the end.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700146 UpdateSlotAdd(obj, topIndex);
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700147 result = ToIndirectRef(topIndex);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700148 table_[topIndex++] = obj;
Ian Rogersdc51b792011-09-22 20:41:37 -0700149 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700150 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700151 if (false) {
152 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex
153 << " holes=" << segment_state_.parts.numHoles;
154 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700155
156 DCHECK(result != NULL);
157 return result;
158}
159
Elliott Hughes726079d2011-10-07 18:43:44 -0700160void IndirectReferenceTable::AssertEmpty() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700161 if (UNLIKELY(begin() != end())) {
162 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes73e66f72012-05-09 09:34:45 -0700163 LOG(FATAL) << "Internal Error: non-empty local reference table\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700164 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes726079d2011-10-07 18:43:44 -0700165 }
166}
167
Elliott Hughes73e66f72012-05-09 09:34:45 -0700168// Removes an object. We extract the table offset bits from "iref"
169// and zap the corresponding entry, leaving a hole if it's not at the top.
170// If the entry is not between the current top index and the bottom index
171// specified by the cookie, we don't remove anything. This is the behavior
172// required by JNI's DeleteLocalRef function.
173// This method is not called when a local frame is popped; this is only used
174// for explicit single removals.
175// Returns "false" if nothing was removed.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700176bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
177 IRTSegmentState prevState;
178 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700179 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700180 int bottomIndex = prevState.parts.topIndex;
181
182 DCHECK(table_ != NULL);
183 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -0700184 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700185
186 int idx = ExtractIndex(iref);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700187
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700188 if (GetIndirectRefKind(iref) == kHandleScopeOrInvalid &&
189 Thread::Current()->HandleScopeContains(reinterpret_cast<jobject>(iref))) {
190 LOG(WARNING) << "Attempt to remove local handle scope entry from IRT, ignoring";
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700191 return true;
192 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700193
194 if (idx < bottomIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700195 // Wrong segment.
196 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
197 << " vs " << bottomIndex << "-" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700198 return false;
199 }
200 if (idx >= topIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700201 // Bad --- stale reference?
202 LOG(WARNING) << "Attempt to remove invalid index " << idx
203 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700204 return false;
205 }
206
207 if (idx == topIndex-1) {
208 // Top-most entry. Scan up and consume holes.
209
Ian Rogers987560f2014-04-22 11:42:59 -0700210 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700211 return false;
212 }
213
214 table_[idx] = NULL;
Ian Rogersdc51b792011-09-22 20:41:37 -0700215 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700216 if (numHoles != 0) {
217 while (--topIndex > bottomIndex && numHoles != 0) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700218 if (false) {
219 LOG(INFO) << "+++ checking for hole at " << topIndex-1
220 << " (cookie=" << cookie << ") val=" << table_[topIndex - 1];
221 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700222 if (table_[topIndex-1] != NULL) {
223 break;
224 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700225 if (false) {
226 LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
227 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700228 numHoles--;
229 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700230 segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
231 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700232 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700233 segment_state_.parts.topIndex = topIndex-1;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700234 if (false) {
235 LOG(INFO) << "+++ ate last entry " << topIndex - 1;
236 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700237 }
238 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700239 // Not the top-most entry. This creates a hole. We NULL out the
240 // entry to prevent somebody from deleting it twice and screwing up
241 // the hole count.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700242 if (table_[idx] == NULL) {
243 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
244 return false;
245 }
Ian Rogers987560f2014-04-22 11:42:59 -0700246 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700247 return false;
248 }
249
250 table_[idx] = NULL;
Ian Rogersdc51b792011-09-22 20:41:37 -0700251 segment_state_.parts.numHoles++;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700252 if (false) {
253 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
254 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700255 }
256
257 return true;
258}
259
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800260void IndirectReferenceTable::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
261 RootType root_type) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700262 for (auto ref : *this) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800263 callback(ref, arg, tid, root_type);
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700264 DCHECK(*ref != nullptr);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700265 }
266}
267
Elliott Hughes73e66f72012-05-09 09:34:45 -0700268void IndirectReferenceTable::Dump(std::ostream& os) const {
269 os << kind_ << " table dump:\n";
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700270 ReferenceTable::Table entries;
271 for (size_t i = 0; i < Capacity(); ++i) {
272 mirror::Object** root = &table_[i];
273 mirror::Object* obj = *root;
274 if (UNLIKELY(obj == nullptr)) {
275 // Remove NULLs.
276 } else if (UNLIKELY(obj == kClearedJniWeakGlobal)) {
277 // ReferenceTable::Dump() will handle kClearedJniWeakGlobal
278 // while the read barrier won't.
279 entries.push_back(obj);
280 } else {
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700281 obj = ReadBarrier::BarrierForRoot<mirror::Object, kWithReadBarrier>(root);
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700282 entries.push_back(obj);
Ian Rogers63818dc2012-09-26 12:23:04 -0700283 }
284 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700285 ReferenceTable::Dump(os, entries);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700286}
287
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700288} // namespace art