blob: 9c634fa86114f86a8d38929264f436c7d361547a [file] [log] [blame]
Mathieu Chartierc56057e2014-05-04 13:18:58 -07001/*
2 * Copyright (C) 2014 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#ifndef ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_
18#define ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_
19
20#include "indirect_reference_table.h"
21
Mathieu Chartier8778c522016-10-04 19:06:30 -070022#include "base/dumpable.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "gc_root-inl.h"
Mathieu Chartier8778c522016-10-04 19:06:30 -070024#include "obj_ptr-inl.h"
Ian Rogersc0542af2014-09-03 16:16:56 -070025#include "runtime-inl.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070026#include "verify_object-inl.h"
27
28namespace art {
29namespace mirror {
30class Object;
31} // namespace mirror
32
33// Verifies that the indirect table lookup is valid.
34// Returns "false" if something looks bad.
35inline bool IndirectReferenceTable::GetChecked(IndirectRef iref) const {
36 if (UNLIKELY(iref == nullptr)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070037 LOG(WARNING) << "Attempt to look up nullptr " << kind_;
Mathieu Chartierc56057e2014-05-04 13:18:58 -070038 return false;
39 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070040 if (UNLIKELY(GetIndirectRefKind(iref) == kHandleScopeOrInvalid)) {
Andreas Gampef1e86302016-10-03 11:42:31 -070041 AbortIfNoCheckJNI(StringPrintf("JNI ERROR (app bug): invalid %s %p",
42 GetIndirectRefKindString(kind_),
43 iref));
Mathieu Chartierc56057e2014-05-04 13:18:58 -070044 return false;
45 }
Andreas Gampee03662b2016-10-13 17:12:56 -070046 const uint32_t top_index = segment_state_.top_index;
47 uint32_t idx = ExtractIndex(iref);
48 if (UNLIKELY(idx >= top_index)) {
Andreas Gampef1e86302016-10-03 11:42:31 -070049 std::string msg = StringPrintf(
50 "JNI ERROR (app bug): accessed stale %s %p (index %d in a table of size %d)",
51 GetIndirectRefKindString(kind_),
52 iref,
53 idx,
Andreas Gampee03662b2016-10-13 17:12:56 -070054 top_index);
Andreas Gampef1e86302016-10-03 11:42:31 -070055 AbortIfNoCheckJNI(msg);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070056 return false;
57 }
Mathieu Chartier4838d662014-09-25 15:27:43 -070058 if (UNLIKELY(table_[idx].GetReference()->IsNull())) {
Andreas Gampef1e86302016-10-03 11:42:31 -070059 AbortIfNoCheckJNI(StringPrintf("JNI ERROR (app bug): accessed deleted %s %p",
60 GetIndirectRefKindString(kind_),
61 iref));
Mathieu Chartierc56057e2014-05-04 13:18:58 -070062 return false;
63 }
64 if (UNLIKELY(!CheckEntry("use", iref, idx))) {
65 return false;
66 }
67 return true;
68}
69
70// Make sure that the entry at "idx" is correctly paired with "iref".
Andreas Gampee03662b2016-10-13 17:12:56 -070071inline bool IndirectReferenceTable::CheckEntry(const char* what,
72 IndirectRef iref,
73 uint32_t idx) const {
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -070074 IndirectRef checkRef = ToIndirectRef(idx);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070075 if (UNLIKELY(checkRef != iref)) {
Andreas Gampef1e86302016-10-03 11:42:31 -070076 std::string msg = StringPrintf(
77 "JNI ERROR (app bug): attempt to %s stale %s %p (should be %p)",
78 what,
79 GetIndirectRefKindString(kind_),
80 iref,
81 checkRef);
82 AbortIfNoCheckJNI(msg);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070083 return false;
84 }
85 return true;
86}
87
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -070088template<ReadBarrierOption kReadBarrierOption>
Mathieu Chartier8778c522016-10-04 19:06:30 -070089inline ObjPtr<mirror::Object> IndirectReferenceTable::Get(IndirectRef iref) const {
Mathieu Chartierc56057e2014-05-04 13:18:58 -070090 if (!GetChecked(iref)) {
Ian Rogersc0542af2014-09-03 16:16:56 -070091 return nullptr;
Mathieu Chartierc56057e2014-05-04 13:18:58 -070092 }
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070093 uint32_t idx = ExtractIndex(iref);
Mathieu Chartier8778c522016-10-04 19:06:30 -070094 ObjPtr<mirror::Object> obj = table_[idx].GetReference()->Read<kReadBarrierOption>();
Mathieu Chartier9d156d52016-10-06 17:44:26 -070095 VerifyObject(obj);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070096 return obj;
97}
98
Mathieu Chartier8778c522016-10-04 19:06:30 -070099inline void IndirectReferenceTable::Update(IndirectRef iref, ObjPtr<mirror::Object> obj) {
Jeff Hao39b6c242015-05-19 20:30:23 -0700100 if (!GetChecked(iref)) {
101 LOG(WARNING) << "IndirectReferenceTable Update failed to find reference " << iref;
102 return;
103 }
104 uint32_t idx = ExtractIndex(iref);
105 table_[idx].SetReference(obj);
106}
107
Mathieu Chartier8778c522016-10-04 19:06:30 -0700108inline void IrtEntry::Add(ObjPtr<mirror::Object> obj) {
109 ++serial_;
110 if (serial_ == kIRTPrevCount) {
111 serial_ = 0;
112 }
113 references_[serial_] = GcRoot<mirror::Object>(obj);
114}
115
116inline void IrtEntry::SetReference(ObjPtr<mirror::Object> obj) {
117 DCHECK_LT(serial_, kIRTPrevCount);
118 references_[serial_] = GcRoot<mirror::Object>(obj);
119}
120
Mathieu Chartierc56057e2014-05-04 13:18:58 -0700121} // namespace art
122
123#endif // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_