xref: /aosp_15_r20/art/runtime/indirect_reference_table-inl.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include "indirect_reference_table.h"
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker #include "base/dumpable.h"
25*795d594fSAndroid Build Coastguard Worker #include "gc_root-inl.h"
26*795d594fSAndroid Build Coastguard Worker #include "obj_ptr-inl.h"
27*795d594fSAndroid Build Coastguard Worker #include "verify_object.h"
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
30*795d594fSAndroid Build Coastguard Worker namespace mirror {
31*795d594fSAndroid Build Coastguard Worker class Object;
32*795d594fSAndroid Build Coastguard Worker }  // namespace mirror
33*795d594fSAndroid Build Coastguard Worker 
34*795d594fSAndroid Build Coastguard Worker // Verifies that the indirect table lookup is valid.
35*795d594fSAndroid Build Coastguard Worker // Returns "false" if something looks bad.
IsValidReference(IndirectRef iref,std::string * error_msg)36*795d594fSAndroid Build Coastguard Worker inline bool IndirectReferenceTable::IsValidReference(IndirectRef iref,
37*795d594fSAndroid Build Coastguard Worker                                                      /*out*/std::string* error_msg) const {
38*795d594fSAndroid Build Coastguard Worker   DCHECK(iref != nullptr);
39*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(GetIndirectRefKind(iref), kind_);
40*795d594fSAndroid Build Coastguard Worker   const uint32_t top_index = top_index_;
41*795d594fSAndroid Build Coastguard Worker   uint32_t idx = ExtractIndex(iref);
42*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(idx >= top_index)) {
43*795d594fSAndroid Build Coastguard Worker     *error_msg = android::base::StringPrintf("deleted reference at index %u in a table of size %u",
44*795d594fSAndroid Build Coastguard Worker                                              idx,
45*795d594fSAndroid Build Coastguard Worker                                              top_index);
46*795d594fSAndroid Build Coastguard Worker     return false;
47*795d594fSAndroid Build Coastguard Worker   }
48*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(table_[idx].GetReference()->IsNull())) {
49*795d594fSAndroid Build Coastguard Worker     *error_msg = android::base::StringPrintf("deleted reference at index %u", idx);
50*795d594fSAndroid Build Coastguard Worker     return false;
51*795d594fSAndroid Build Coastguard Worker   }
52*795d594fSAndroid Build Coastguard Worker   uint32_t iref_serial = DecodeSerial(reinterpret_cast<uintptr_t>(iref));
53*795d594fSAndroid Build Coastguard Worker   uint32_t entry_serial = table_[idx].GetSerial();
54*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(iref_serial != entry_serial)) {
55*795d594fSAndroid Build Coastguard Worker     *error_msg = android::base::StringPrintf("stale reference with serial number %u v. current %u",
56*795d594fSAndroid Build Coastguard Worker                                              iref_serial,
57*795d594fSAndroid Build Coastguard Worker                                              entry_serial);
58*795d594fSAndroid Build Coastguard Worker     return false;
59*795d594fSAndroid Build Coastguard Worker   }
60*795d594fSAndroid Build Coastguard Worker   return true;
61*795d594fSAndroid Build Coastguard Worker }
62*795d594fSAndroid Build Coastguard Worker 
63*795d594fSAndroid Build Coastguard Worker // Make sure that the entry at "idx" is correctly paired with "iref".
CheckEntry(const char * what,IndirectRef iref,uint32_t idx)64*795d594fSAndroid Build Coastguard Worker inline bool IndirectReferenceTable::CheckEntry(const char* what,
65*795d594fSAndroid Build Coastguard Worker                                                IndirectRef iref,
66*795d594fSAndroid Build Coastguard Worker                                                uint32_t idx) const {
67*795d594fSAndroid Build Coastguard Worker   IndirectRef checkRef = ToIndirectRef(idx);
68*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(checkRef != iref)) {
69*795d594fSAndroid Build Coastguard Worker     std::string msg = android::base::StringPrintf(
70*795d594fSAndroid Build Coastguard Worker         "JNI ERROR (app bug): attempt to %s stale %s %p (should be %p)",
71*795d594fSAndroid Build Coastguard Worker         what,
72*795d594fSAndroid Build Coastguard Worker         GetIndirectRefKindString(kind_),
73*795d594fSAndroid Build Coastguard Worker         iref,
74*795d594fSAndroid Build Coastguard Worker         checkRef);
75*795d594fSAndroid Build Coastguard Worker     AbortIfNoCheckJNI(msg);
76*795d594fSAndroid Build Coastguard Worker     return false;
77*795d594fSAndroid Build Coastguard Worker   }
78*795d594fSAndroid Build Coastguard Worker   return true;
79*795d594fSAndroid Build Coastguard Worker }
80*795d594fSAndroid Build Coastguard Worker 
81*795d594fSAndroid Build Coastguard Worker template<ReadBarrierOption kReadBarrierOption>
Get(IndirectRef iref)82*795d594fSAndroid Build Coastguard Worker inline ObjPtr<mirror::Object> IndirectReferenceTable::Get(IndirectRef iref) const {
83*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(GetIndirectRefKind(iref), kind_);
84*795d594fSAndroid Build Coastguard Worker   uint32_t idx = ExtractIndex(iref);
85*795d594fSAndroid Build Coastguard Worker   DCHECK_LT(idx, top_index_);
86*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(DecodeSerial(reinterpret_cast<uintptr_t>(iref)), table_[idx].GetSerial());
87*795d594fSAndroid Build Coastguard Worker   DCHECK(!table_[idx].GetReference()->IsNull());
88*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Object> obj = table_[idx].GetReference()->Read<kReadBarrierOption>();
89*795d594fSAndroid Build Coastguard Worker   VerifyObject(obj);
90*795d594fSAndroid Build Coastguard Worker   return obj;
91*795d594fSAndroid Build Coastguard Worker }
92*795d594fSAndroid Build Coastguard Worker 
Update(IndirectRef iref,ObjPtr<mirror::Object> obj)93*795d594fSAndroid Build Coastguard Worker inline void IndirectReferenceTable::Update(IndirectRef iref, ObjPtr<mirror::Object> obj) {
94*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(GetIndirectRefKind(iref), kind_);
95*795d594fSAndroid Build Coastguard Worker   uint32_t idx = ExtractIndex(iref);
96*795d594fSAndroid Build Coastguard Worker   DCHECK_LT(idx, top_index_);
97*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(DecodeSerial(reinterpret_cast<uintptr_t>(iref)), table_[idx].GetSerial());
98*795d594fSAndroid Build Coastguard Worker   DCHECK(!table_[idx].GetReference()->IsNull());
99*795d594fSAndroid Build Coastguard Worker   table_[idx].SetReference(obj);
100*795d594fSAndroid Build Coastguard Worker }
101*795d594fSAndroid Build Coastguard Worker 
Add(ObjPtr<mirror::Object> obj)102*795d594fSAndroid Build Coastguard Worker inline void IrtEntry::Add(ObjPtr<mirror::Object> obj) {
103*795d594fSAndroid Build Coastguard Worker   ++serial_;
104*795d594fSAndroid Build Coastguard Worker   if (serial_ == kIRTMaxSerial) {
105*795d594fSAndroid Build Coastguard Worker     serial_ = 0;
106*795d594fSAndroid Build Coastguard Worker   }
107*795d594fSAndroid Build Coastguard Worker   reference_ = GcRoot<mirror::Object>(obj);
108*795d594fSAndroid Build Coastguard Worker }
109*795d594fSAndroid Build Coastguard Worker 
SetReference(ObjPtr<mirror::Object> obj)110*795d594fSAndroid Build Coastguard Worker inline void IrtEntry::SetReference(ObjPtr<mirror::Object> obj) {
111*795d594fSAndroid Build Coastguard Worker   DCHECK_LT(serial_, kIRTMaxSerial);
112*795d594fSAndroid Build Coastguard Worker   reference_ = GcRoot<mirror::Object>(obj);
113*795d594fSAndroid Build Coastguard Worker }
114*795d594fSAndroid Build Coastguard Worker 
115*795d594fSAndroid Build Coastguard Worker }  // namespace art
116*795d594fSAndroid Build Coastguard Worker 
117*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_
118