1 /*
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
22 #include "android-base/stringprintf.h"
23
24 #include "base/dumpable.h"
25 #include "gc_root-inl.h"
26 #include "obj_ptr-inl.h"
27 #include "verify_object.h"
28
29 namespace art {
30 namespace mirror {
31 class Object;
32 } // namespace mirror
33
34 // Verifies that the indirect table lookup is valid.
35 // Returns "false" if something looks bad.
GetChecked(IndirectRef iref)36 inline bool IndirectReferenceTable::GetChecked(IndirectRef iref) const {
37 if (UNLIKELY(iref == nullptr)) {
38 LOG(WARNING) << "Attempt to look up nullptr " << kind_;
39 return false;
40 }
41 if (UNLIKELY(GetIndirectRefKind(iref) == kHandleScopeOrInvalid)) {
42 AbortIfNoCheckJNI(android::base::StringPrintf("JNI ERROR (app bug): invalid %s %p",
43 GetIndirectRefKindString(kind_),
44 iref));
45 return false;
46 }
47 const uint32_t top_index = segment_state_.top_index;
48 uint32_t idx = ExtractIndex(iref);
49 if (UNLIKELY(idx >= top_index)) {
50 std::string msg = android::base::StringPrintf(
51 "JNI ERROR (app bug): accessed stale %s %p (index %d in a table of size %d)",
52 GetIndirectRefKindString(kind_),
53 iref,
54 idx,
55 top_index);
56 AbortIfNoCheckJNI(msg);
57 return false;
58 }
59 if (UNLIKELY(table_[idx].GetReference()->IsNull())) {
60 AbortIfNoCheckJNI(android::base::StringPrintf("JNI ERROR (app bug): accessed deleted %s %p",
61 GetIndirectRefKindString(kind_),
62 iref));
63 return false;
64 }
65 if (UNLIKELY(!CheckEntry("use", iref, idx))) {
66 return false;
67 }
68 return true;
69 }
70
71 // Make sure that the entry at "idx" is correctly paired with "iref".
CheckEntry(const char * what,IndirectRef iref,uint32_t idx)72 inline bool IndirectReferenceTable::CheckEntry(const char* what,
73 IndirectRef iref,
74 uint32_t idx) const {
75 IndirectRef checkRef = ToIndirectRef(idx);
76 if (UNLIKELY(checkRef != iref)) {
77 std::string msg = android::base::StringPrintf(
78 "JNI ERROR (app bug): attempt to %s stale %s %p (should be %p)",
79 what,
80 GetIndirectRefKindString(kind_),
81 iref,
82 checkRef);
83 AbortIfNoCheckJNI(msg);
84 return false;
85 }
86 return true;
87 }
88
89 template<ReadBarrierOption kReadBarrierOption>
Get(IndirectRef iref)90 inline ObjPtr<mirror::Object> IndirectReferenceTable::Get(IndirectRef iref) const {
91 if (!GetChecked(iref)) {
92 return nullptr;
93 }
94 uint32_t idx = ExtractIndex(iref);
95 ObjPtr<mirror::Object> obj = table_[idx].GetReference()->Read<kReadBarrierOption>();
96 VerifyObject(obj);
97 return obj;
98 }
99
Update(IndirectRef iref,ObjPtr<mirror::Object> obj)100 inline void IndirectReferenceTable::Update(IndirectRef iref, ObjPtr<mirror::Object> obj) {
101 if (!GetChecked(iref)) {
102 LOG(WARNING) << "IndirectReferenceTable Update failed to find reference " << iref;
103 return;
104 }
105 uint32_t idx = ExtractIndex(iref);
106 table_[idx].SetReference(obj);
107 }
108
Add(ObjPtr<mirror::Object> obj)109 inline void IrtEntry::Add(ObjPtr<mirror::Object> obj) {
110 ++serial_;
111 if (serial_ == kIRTPrevCount) {
112 serial_ = 0;
113 }
114 references_[serial_] = GcRoot<mirror::Object>(obj);
115 }
116
SetReference(ObjPtr<mirror::Object> obj)117 inline void IrtEntry::SetReference(ObjPtr<mirror::Object> obj) {
118 DCHECK_LT(serial_, kIRTPrevCount);
119 references_[serial_] = GcRoot<mirror::Object>(obj);
120 }
121
122 } // namespace art
123
124 #endif // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_
125