blob: 1ba2291adfd6782e50a06d163650390f95738531 [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.
Ian Rogers68d8b422014-07-17 11:09:10 -070059 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
60 if (!vm->IsCheckJniEnabled()) {
Elliott Hughesa2501992011-08-26 19:39:54 -070061 // Otherwise, we want to abort rather than hand back a bad reference.
62 LOG(FATAL) << "JNI ERROR (app bug): see above.";
63 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -070064}
65
66IndirectReferenceTable::IndirectReferenceTable(size_t initialCount,
Elliott Hughesba8eee12012-01-24 20:25:24 -080067 size_t maxCount, IndirectRefKind desiredKind) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070068 CHECK_GT(initialCount, 0U);
69 CHECK_LE(initialCount, maxCount);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070070 CHECK_NE(desiredKind, kHandleScopeOrInvalid);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070071
Mathieu Chartierc56057e2014-05-04 13:18:58 -070072 std::string error_str;
73 const size_t initial_bytes = initialCount * sizeof(const mirror::Object*);
74 const size_t table_bytes = maxCount * sizeof(const mirror::Object*);
75 table_mem_map_.reset(MemMap::MapAnonymous("indirect ref table", nullptr, table_bytes,
76 PROT_READ | PROT_WRITE, false, &error_str));
77 CHECK(table_mem_map_.get() != nullptr) << error_str;
Alex Lighta59dd802014-07-02 16:28:08 -070078 CHECK_EQ(table_mem_map_->Size(), table_bytes);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070079
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070080 table_ = reinterpret_cast<GcRoot<mirror::Object>*>(table_mem_map_->Begin());
Mathieu Chartierc56057e2014-05-04 13:18:58 -070081 CHECK(table_ != nullptr);
82 memset(table_, 0xd1, initial_bytes);
83
84 const size_t slot_bytes = maxCount * sizeof(IndirectRefSlot);
85 slot_mem_map_.reset(MemMap::MapAnonymous("indirect ref table slots", nullptr, slot_bytes,
86 PROT_READ | PROT_WRITE, false, &error_str));
87 CHECK(slot_mem_map_.get() != nullptr) << error_str;
88 slot_data_ = reinterpret_cast<IndirectRefSlot*>(slot_mem_map_->Begin());
89 CHECK(slot_data_ != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070090
Ian Rogersdc51b792011-09-22 20:41:37 -070091 segment_state_.all = IRT_FIRST_SEGMENT;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070092 alloc_entries_ = initialCount;
93 max_entries_ = maxCount;
94 kind_ = desiredKind;
95}
96
97IndirectReferenceTable::~IndirectReferenceTable() {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070098}
99
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700100IndirectRef IndirectReferenceTable::Add(uint32_t cookie, mirror::Object* obj) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700101 IRTSegmentState prevState;
102 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700103 size_t topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700104
Mathieu Chartiercd2cfff2013-12-19 15:46:55 -0800105 CHECK(obj != NULL);
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800106 VerifyObject(obj);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700107 DCHECK(table_ != NULL);
108 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -0700109 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700110
111 if (topIndex == alloc_entries_) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700112 // reached end of allocated space; did we hit buffer max?
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700113 if (topIndex == max_entries_) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700114 LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow "
115 << "(max=" << max_entries_ << ")\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700117 }
118
119 size_t newSize = alloc_entries_ * 2;
120 if (newSize > max_entries_) {
121 newSize = max_entries_;
122 }
123 DCHECK_GT(newSize, alloc_entries_);
124
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700125 alloc_entries_ = newSize;
126 }
127
Elliott Hughes73e66f72012-05-09 09:34:45 -0700128 // We know there's enough room in the table. Now we just need to find
129 // the right spot. If there's a hole, find it and fill it; otherwise,
130 // add to the end of the list.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700131 IndirectRef result;
Ian Rogersdc51b792011-09-22 20:41:37 -0700132 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700133 if (numHoles > 0) {
134 DCHECK_GT(topIndex, 1U);
Elliott Hughes73e66f72012-05-09 09:34:45 -0700135 // Find the first hole; likely to be near the end of the list.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700136 GcRoot<mirror::Object>* pScan = &table_[topIndex - 1];
137 DCHECK(!pScan->IsNull());
138 --pScan;
139 while (!pScan->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700140 DCHECK_GE(pScan, table_ + prevState.parts.topIndex);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700141 --pScan;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700142 }
143 UpdateSlotAdd(obj, pScan - table_);
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700144 result = ToIndirectRef(pScan - table_);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700145 *pScan = GcRoot<mirror::Object>(obj);
Ian Rogersdc51b792011-09-22 20:41:37 -0700146 segment_state_.parts.numHoles--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700147 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700148 // Add to the end.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700149 UpdateSlotAdd(obj, topIndex);
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700150 result = ToIndirectRef(topIndex);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700151 table_[topIndex++] = GcRoot<mirror::Object>(obj);
Ian Rogersdc51b792011-09-22 20:41:37 -0700152 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700153 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700154 if (false) {
155 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex
156 << " holes=" << segment_state_.parts.numHoles;
157 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700158
159 DCHECK(result != NULL);
160 return result;
161}
162
Elliott Hughes726079d2011-10-07 18:43:44 -0700163void IndirectReferenceTable::AssertEmpty() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700164 if (UNLIKELY(begin() != end())) {
165 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes73e66f72012-05-09 09:34:45 -0700166 LOG(FATAL) << "Internal Error: non-empty local reference table\n"
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700167 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Elliott Hughes726079d2011-10-07 18:43:44 -0700168 }
169}
170
Elliott Hughes73e66f72012-05-09 09:34:45 -0700171// Removes an object. We extract the table offset bits from "iref"
172// and zap the corresponding entry, leaving a hole if it's not at the top.
173// If the entry is not between the current top index and the bottom index
174// specified by the cookie, we don't remove anything. This is the behavior
175// required by JNI's DeleteLocalRef function.
176// This method is not called when a local frame is popped; this is only used
177// for explicit single removals.
178// Returns "false" if nothing was removed.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700179bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
180 IRTSegmentState prevState;
181 prevState.all = cookie;
Ian Rogersdc51b792011-09-22 20:41:37 -0700182 int topIndex = segment_state_.parts.topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700183 int bottomIndex = prevState.parts.topIndex;
184
185 DCHECK(table_ != NULL);
186 DCHECK_LE(alloc_entries_, max_entries_);
Ian Rogersdc51b792011-09-22 20:41:37 -0700187 DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700188
189 int idx = ExtractIndex(iref);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700190
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700191 if (GetIndirectRefKind(iref) == kHandleScopeOrInvalid &&
192 Thread::Current()->HandleScopeContains(reinterpret_cast<jobject>(iref))) {
193 LOG(WARNING) << "Attempt to remove local handle scope entry from IRT, ignoring";
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700194 return true;
195 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700196
197 if (idx < bottomIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700198 // Wrong segment.
199 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
200 << " vs " << bottomIndex << "-" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700201 return false;
202 }
203 if (idx >= topIndex) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700204 // Bad --- stale reference?
205 LOG(WARNING) << "Attempt to remove invalid index " << idx
206 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700207 return false;
208 }
209
210 if (idx == topIndex-1) {
211 // Top-most entry. Scan up and consume holes.
212
Ian Rogers987560f2014-04-22 11:42:59 -0700213 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700214 return false;
215 }
216
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700217 table_[idx] = GcRoot<mirror::Object>(nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700218 int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700219 if (numHoles != 0) {
220 while (--topIndex > bottomIndex && numHoles != 0) {
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700221 if (false) {
222 LOG(INFO) << "+++ checking for hole at " << topIndex-1
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700223 << " (cookie=" << cookie << ") val="
224 << table_[topIndex - 1].Read<kWithoutReadBarrier>();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700225 }
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700226 if (!table_[topIndex-1].IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700227 break;
228 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700229 if (false) {
230 LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
231 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700232 numHoles--;
233 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700234 segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
235 segment_state_.parts.topIndex = topIndex;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700236 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700237 segment_state_.parts.topIndex = topIndex-1;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700238 if (false) {
239 LOG(INFO) << "+++ ate last entry " << topIndex - 1;
240 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700241 }
242 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700243 // Not the top-most entry. This creates a hole. We NULL out the
244 // entry to prevent somebody from deleting it twice and screwing up
245 // the hole count.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700246 if (table_[idx].IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700247 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
248 return false;
249 }
Ian Rogers987560f2014-04-22 11:42:59 -0700250 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700251 return false;
252 }
253
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700254 table_[idx] = GcRoot<mirror::Object>(nullptr);
Ian Rogersdc51b792011-09-22 20:41:37 -0700255 segment_state_.parts.numHoles++;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700256 if (false) {
257 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
258 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700259 }
260
261 return true;
262}
263
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800264void IndirectReferenceTable::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
265 RootType root_type) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700266 for (auto ref : *this) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800267 callback(ref, arg, tid, root_type);
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700268 DCHECK(*ref != nullptr);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700269 }
270}
271
Elliott Hughes73e66f72012-05-09 09:34:45 -0700272void IndirectReferenceTable::Dump(std::ostream& os) const {
273 os << kind_ << " table dump:\n";
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700274 ReferenceTable::Table entries;
275 for (size_t i = 0; i < Capacity(); ++i) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700276 mirror::Object* obj = table_[i].Read<kWithoutReadBarrier>();
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700277 if (UNLIKELY(obj == nullptr)) {
278 // Remove NULLs.
279 } else if (UNLIKELY(obj == kClearedJniWeakGlobal)) {
280 // ReferenceTable::Dump() will handle kClearedJniWeakGlobal
281 // while the read barrier won't.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700282 entries.push_back(GcRoot<mirror::Object>(obj));
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700283 } else {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700284 obj = table_[i].Read();
285 entries.push_back(GcRoot<mirror::Object>(obj));
Ian Rogers63818dc2012-09-26 12:23:04 -0700286 }
287 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700288 ReferenceTable::Dump(os, entries);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700289}
290
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700291} // namespace art