blob: 9b2b82e477eb99129c2d43a155be71ae4cfe7364 [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
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070079 table_ = reinterpret_cast<GcRoot<mirror::Object>*>(table_mem_map_->Begin());
Mathieu Chartierc56057e2014-05-04 13:18:58 -070080 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.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700135 GcRoot<mirror::Object>* pScan = &table_[topIndex - 1];
136 DCHECK(!pScan->IsNull());
137 --pScan;
138 while (!pScan->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700139 DCHECK_GE(pScan, table_ + prevState.parts.topIndex);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700140 --pScan;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700141 }
142 UpdateSlotAdd(obj, pScan - table_);
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700143 result = ToIndirectRef(pScan - table_);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700144 *pScan = GcRoot<mirror::Object>(obj);
Ian Rogersdc51b792011-09-22 20:41:37 -0700145 segment_state_.parts.numHoles--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700146 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700147 // Add to the end.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700148 UpdateSlotAdd(obj, topIndex);
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700149 result = ToIndirectRef(topIndex);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700150 table_[topIndex++] = GcRoot<mirror::Object>(obj);
Ian Rogersdc51b792011-09-22 20:41:37 -0700151 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700152 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700153 if (false) {
154 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex
155 << " holes=" << segment_state_.parts.numHoles;
156 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700157
158 DCHECK(result != NULL);
159 return result;
160}
161
Elliott Hughes726079d2011-10-07 18:43:44 -0700162void IndirectReferenceTable::AssertEmpty() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163 if (UNLIKELY(begin() != end())) {
164 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes73e66f72012-05-09 09:34:45 -0700165 LOG(FATAL) << "Internal Error: non-empty local reference table\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700166 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes726079d2011-10-07 18:43:44 -0700167 }
168}
169
Elliott Hughes73e66f72012-05-09 09:34:45 -0700170// Removes an object. We extract the table offset bits from "iref"
171// and zap the corresponding entry, leaving a hole if it's not at the top.
172// If the entry is not between the current top index and the bottom index
173// specified by the cookie, we don't remove anything. This is the behavior
174// required by JNI's DeleteLocalRef function.
175// This method is not called when a local frame is popped; this is only used
176// for explicit single removals.
177// Returns "false" if nothing was removed.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700178bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
179 IRTSegmentState prevState;
180 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700181 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700182 int bottomIndex = prevState.parts.topIndex;
183
184 DCHECK(table_ != NULL);
185 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -0700186 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700187
188 int idx = ExtractIndex(iref);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700189
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700190 if (GetIndirectRefKind(iref) == kHandleScopeOrInvalid &&
191 Thread::Current()->HandleScopeContains(reinterpret_cast<jobject>(iref))) {
192 LOG(WARNING) << "Attempt to remove local handle scope entry from IRT, ignoring";
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700193 return true;
194 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700195
196 if (idx < bottomIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700197 // Wrong segment.
198 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
199 << " vs " << bottomIndex << "-" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700200 return false;
201 }
202 if (idx >= topIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700203 // Bad --- stale reference?
204 LOG(WARNING) << "Attempt to remove invalid index " << idx
205 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700206 return false;
207 }
208
209 if (idx == topIndex-1) {
210 // Top-most entry. Scan up and consume holes.
211
Ian Rogers987560f2014-04-22 11:42:59 -0700212 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700213 return false;
214 }
215
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700216 table_[idx] = GcRoot<mirror::Object>(nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700217 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700218 if (numHoles != 0) {
219 while (--topIndex > bottomIndex && numHoles != 0) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700220 if (false) {
221 LOG(INFO) << "+++ checking for hole at " << topIndex-1
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700222 << " (cookie=" << cookie << ") val="
223 << table_[topIndex - 1].Read<kWithoutReadBarrier>();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700224 }
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700225 if (!table_[topIndex-1].IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700226 break;
227 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700228 if (false) {
229 LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
230 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700231 numHoles--;
232 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700233 segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
234 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700235 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700236 segment_state_.parts.topIndex = topIndex-1;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700237 if (false) {
238 LOG(INFO) << "+++ ate last entry " << topIndex - 1;
239 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700240 }
241 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700242 // Not the top-most entry. This creates a hole. We NULL out the
243 // entry to prevent somebody from deleting it twice and screwing up
244 // the hole count.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700245 if (table_[idx].IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700246 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
247 return false;
248 }
Ian Rogers987560f2014-04-22 11:42:59 -0700249 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700250 return false;
251 }
252
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700253 table_[idx] = GcRoot<mirror::Object>(nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700254 segment_state_.parts.numHoles++;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700255 if (false) {
256 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
257 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700258 }
259
260 return true;
261}
262
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800263void IndirectReferenceTable::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
264 RootType root_type) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700265 for (auto ref : *this) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800266 callback(ref, arg, tid, root_type);
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700267 DCHECK(*ref != nullptr);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700268 }
269}
270
Elliott Hughes73e66f72012-05-09 09:34:45 -0700271void IndirectReferenceTable::Dump(std::ostream& os) const {
272 os << kind_ << " table dump:\n";
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700273 ReferenceTable::Table entries;
274 for (size_t i = 0; i < Capacity(); ++i) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700275 mirror::Object* obj = table_[i].Read<kWithoutReadBarrier>();
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700276 if (UNLIKELY(obj == nullptr)) {
277 // Remove NULLs.
278 } else if (UNLIKELY(obj == kClearedJniWeakGlobal)) {
279 // ReferenceTable::Dump() will handle kClearedJniWeakGlobal
280 // while the read barrier won't.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700281 entries.push_back(GcRoot<mirror::Object>(obj));
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700282 } else {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700283 obj = table_[i].Read();
284 entries.push_back(GcRoot<mirror::Object>(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