blob: 756ac9606eb85c4471778694069c97407d689d55 [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;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070077
Mathieu Chartierc56057e2014-05-04 13:18:58 -070078 table_ = reinterpret_cast<mirror::Object**>(table_mem_map_->Begin());
79 CHECK(table_ != nullptr);
80 memset(table_, 0xd1, initial_bytes);
81
82 const size_t slot_bytes = maxCount * sizeof(IndirectRefSlot);
83 slot_mem_map_.reset(MemMap::MapAnonymous("indirect ref table slots", nullptr, slot_bytes,
84 PROT_READ | PROT_WRITE, false, &error_str));
85 CHECK(slot_mem_map_.get() != nullptr) << error_str;
86 slot_data_ = reinterpret_cast<IndirectRefSlot*>(slot_mem_map_->Begin());
87 CHECK(slot_data_ != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070088
Ian Rogersdc51b792011-09-22 20:41:37 -070089 segment_state_.all = IRT_FIRST_SEGMENT;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070090 alloc_entries_ = initialCount;
91 max_entries_ = maxCount;
92 kind_ = desiredKind;
93}
94
95IndirectReferenceTable::~IndirectReferenceTable() {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070096}
97
Mathieu Chartier423d2a32013-09-12 17:33:56 -070098IndirectRef IndirectReferenceTable::Add(uint32_t cookie, mirror::Object* obj) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070099 IRTSegmentState prevState;
100 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700101 size_t topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700102
Mathieu Chartiercd2cfff2013-12-19 15:46:55 -0800103 CHECK(obj != NULL);
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800104 VerifyObject(obj);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700105 DCHECK(table_ != NULL);
106 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -0700107 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700108
109 if (topIndex == alloc_entries_) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700110 // reached end of allocated space; did we hit buffer max?
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700111 if (topIndex == max_entries_) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700112 LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow "
113 << "(max=" << max_entries_ << ")\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700114 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700115 }
116
117 size_t newSize = alloc_entries_ * 2;
118 if (newSize > max_entries_) {
119 newSize = max_entries_;
120 }
121 DCHECK_GT(newSize, alloc_entries_);
122
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700123 alloc_entries_ = newSize;
124 }
125
Elliott Hughes73e66f72012-05-09 09:34:45 -0700126 // We know there's enough room in the table. Now we just need to find
127 // the right spot. If there's a hole, find it and fill it; otherwise,
128 // add to the end of the list.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700129 IndirectRef result;
Ian Rogersdc51b792011-09-22 20:41:37 -0700130 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700131 if (numHoles > 0) {
132 DCHECK_GT(topIndex, 1U);
Elliott Hughes73e66f72012-05-09 09:34:45 -0700133 // Find the first hole; likely to be near the end of the list.
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700134 mirror::Object** pScan = &table_[topIndex - 1];
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700135 DCHECK(*pScan != NULL);
136 while (*--pScan != NULL) {
137 DCHECK_GE(pScan, table_ + prevState.parts.topIndex);
138 }
139 UpdateSlotAdd(obj, pScan - table_);
140 result = ToIndirectRef(obj, pScan - table_);
141 *pScan = obj;
Ian Rogersdc51b792011-09-22 20:41:37 -0700142 segment_state_.parts.numHoles--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700143 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700144 // Add to the end.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700145 UpdateSlotAdd(obj, topIndex);
146 result = ToIndirectRef(obj, topIndex);
147 table_[topIndex++] = obj;
Ian Rogersdc51b792011-09-22 20:41:37 -0700148 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700149 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700150 if (false) {
151 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex
152 << " holes=" << segment_state_.parts.numHoles;
153 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700154
155 DCHECK(result != NULL);
156 return result;
157}
158
Elliott Hughes726079d2011-10-07 18:43:44 -0700159void IndirectReferenceTable::AssertEmpty() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700160 if (UNLIKELY(begin() != end())) {
161 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes73e66f72012-05-09 09:34:45 -0700162 LOG(FATAL) << "Internal Error: non-empty local reference table\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes726079d2011-10-07 18:43:44 -0700164 }
165}
166
Elliott Hughes73e66f72012-05-09 09:34:45 -0700167// Removes an object. We extract the table offset bits from "iref"
168// and zap the corresponding entry, leaving a hole if it's not at the top.
169// If the entry is not between the current top index and the bottom index
170// specified by the cookie, we don't remove anything. This is the behavior
171// required by JNI's DeleteLocalRef function.
172// This method is not called when a local frame is popped; this is only used
173// for explicit single removals.
174// Returns "false" if nothing was removed.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700175bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
176 IRTSegmentState prevState;
177 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700178 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700179 int bottomIndex = prevState.parts.topIndex;
180
181 DCHECK(table_ != NULL);
182 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -0700183 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700184
185 int idx = ExtractIndex(iref);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700186
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700187 if (GetIndirectRefKind(iref) == kHandleScopeOrInvalid &&
188 Thread::Current()->HandleScopeContains(reinterpret_cast<jobject>(iref))) {
189 LOG(WARNING) << "Attempt to remove local handle scope entry from IRT, ignoring";
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700190 return true;
191 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700192
193 if (idx < bottomIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700194 // Wrong segment.
195 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
196 << " vs " << bottomIndex << "-" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700197 return false;
198 }
199 if (idx >= topIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700200 // Bad --- stale reference?
201 LOG(WARNING) << "Attempt to remove invalid index " << idx
202 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700203 return false;
204 }
205
206 if (idx == topIndex-1) {
207 // Top-most entry. Scan up and consume holes.
208
Ian Rogers987560f2014-04-22 11:42:59 -0700209 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700210 return false;
211 }
212
213 table_[idx] = NULL;
Ian Rogersdc51b792011-09-22 20:41:37 -0700214 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700215 if (numHoles != 0) {
216 while (--topIndex > bottomIndex && numHoles != 0) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700217 if (false) {
218 LOG(INFO) << "+++ checking for hole at " << topIndex-1
219 << " (cookie=" << cookie << ") val=" << table_[topIndex - 1];
220 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700221 if (table_[topIndex-1] != NULL) {
222 break;
223 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700224 if (false) {
225 LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
226 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700227 numHoles--;
228 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700229 segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
230 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700231 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700232 segment_state_.parts.topIndex = topIndex-1;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700233 if (false) {
234 LOG(INFO) << "+++ ate last entry " << topIndex - 1;
235 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700236 }
237 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700238 // Not the top-most entry. This creates a hole. We NULL out the
239 // entry to prevent somebody from deleting it twice and screwing up
240 // the hole count.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700241 if (table_[idx] == NULL) {
242 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
243 return false;
244 }
Ian Rogers987560f2014-04-22 11:42:59 -0700245 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700246 return false;
247 }
248
249 table_[idx] = NULL;
Ian Rogersdc51b792011-09-22 20:41:37 -0700250 segment_state_.parts.numHoles++;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700251 if (false) {
252 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
253 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700254 }
255
256 return true;
257}
258
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800259void IndirectReferenceTable::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
260 RootType root_type) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700261 for (auto ref : *this) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800262 callback(ref, arg, tid, root_type);
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700263 DCHECK(*ref != nullptr);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700264 }
265}
266
Elliott Hughes73e66f72012-05-09 09:34:45 -0700267void IndirectReferenceTable::Dump(std::ostream& os) const {
268 os << kind_ << " table dump:\n";
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700269 ReferenceTable::Table entries;
270 for (size_t i = 0; i < Capacity(); ++i) {
271 mirror::Object** root = &table_[i];
272 mirror::Object* obj = *root;
273 if (UNLIKELY(obj == nullptr)) {
274 // Remove NULLs.
275 } else if (UNLIKELY(obj == kClearedJniWeakGlobal)) {
276 // ReferenceTable::Dump() will handle kClearedJniWeakGlobal
277 // while the read barrier won't.
278 entries.push_back(obj);
279 } else {
280 // We need a read barrier if weak globals. Since this is for
281 // debugging where performance isn't top priority, we
282 // unconditionally enable the read barrier, which is conservative.
283 obj = ReadBarrier::BarrierForWeakRoot<mirror::Object, kWithReadBarrier>(root);
284 entries.push_back(obj);
Ian Rogers63818dc2012-09-26 12:23:04 -0700285 }
286 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700287 ReferenceTable::Dump(os, entries);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700288}
289
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700290} // namespace art