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.
IsValidReference(IndirectRef iref,std::string * error_msg)36 inline bool IndirectReferenceTable::IsValidReference(IndirectRef iref,
37                                                      /*out*/std::string* error_msg) const {
38   DCHECK(iref != nullptr);
39   DCHECK_EQ(GetIndirectRefKind(iref), kind_);
40   const uint32_t top_index = segment_state_.top_index;
41   uint32_t idx = ExtractIndex(iref);
42   if (UNLIKELY(idx >= top_index)) {
43     *error_msg = android::base::StringPrintf("deleted reference at index %u in a table of size %u",
44                                              idx,
45                                              top_index);
46     return false;
47   }
48   if (UNLIKELY(table_[idx].GetReference()->IsNull())) {
49     *error_msg = android::base::StringPrintf("deleted reference at index %u", idx);
50     return false;
51   }
52   uint32_t iref_serial = DecodeSerial(reinterpret_cast<uintptr_t>(iref));
53   uint32_t entry_serial = table_[idx].GetSerial();
54   if (UNLIKELY(iref_serial != entry_serial)) {
55     *error_msg = android::base::StringPrintf("stale reference with serial number %u v. current %u",
56                                              iref_serial,
57                                              entry_serial);
58     return false;
59   }
60   return true;
61 }
62 
63 // Make sure that the entry at "idx" is correctly paired with "iref".
CheckEntry(const char * what,IndirectRef iref,uint32_t idx)64 inline bool IndirectReferenceTable::CheckEntry(const char* what,
65                                                IndirectRef iref,
66                                                uint32_t idx) const {
67   IndirectRef checkRef = ToIndirectRef(idx);
68   if (UNLIKELY(checkRef != iref)) {
69     std::string msg = android::base::StringPrintf(
70         "JNI ERROR (app bug): attempt to %s stale %s %p (should be %p)",
71         what,
72         GetIndirectRefKindString(kind_),
73         iref,
74         checkRef);
75     AbortIfNoCheckJNI(msg);
76     return false;
77   }
78   return true;
79 }
80 
81 template<ReadBarrierOption kReadBarrierOption>
Get(IndirectRef iref)82 inline ObjPtr<mirror::Object> IndirectReferenceTable::Get(IndirectRef iref) const {
83   DCHECK_EQ(GetIndirectRefKind(iref), kind_);
84   uint32_t idx = ExtractIndex(iref);
85   DCHECK_LT(idx, segment_state_.top_index);
86   DCHECK_EQ(DecodeSerial(reinterpret_cast<uintptr_t>(iref)), table_[idx].GetSerial());
87   DCHECK(!table_[idx].GetReference()->IsNull());
88   ObjPtr<mirror::Object> obj = table_[idx].GetReference()->Read<kReadBarrierOption>();
89   VerifyObject(obj);
90   return obj;
91 }
92 
Update(IndirectRef iref,ObjPtr<mirror::Object> obj)93 inline void IndirectReferenceTable::Update(IndirectRef iref, ObjPtr<mirror::Object> obj) {
94   DCHECK_EQ(GetIndirectRefKind(iref), kind_);
95   uint32_t idx = ExtractIndex(iref);
96   DCHECK_LT(idx, segment_state_.top_index);
97   DCHECK_EQ(DecodeSerial(reinterpret_cast<uintptr_t>(iref)), table_[idx].GetSerial());
98   DCHECK(!table_[idx].GetReference()->IsNull());
99   table_[idx].SetReference(obj);
100 }
101 
Add(ObjPtr<mirror::Object> obj)102 inline void IrtEntry::Add(ObjPtr<mirror::Object> obj) {
103   ++serial_;
104   if (serial_ == kIRTPrevCount) {
105     serial_ = 0;
106   }
107   references_[serial_] = GcRoot<mirror::Object>(obj);
108 }
109 
SetReference(ObjPtr<mirror::Object> obj)110 inline void IrtEntry::SetReference(ObjPtr<mirror::Object> obj) {
111   DCHECK_LT(serial_, kIRTPrevCount);
112   references_[serial_] = GcRoot<mirror::Object>(obj);
113 }
114 
115 }  // namespace art
116 
117 #endif  // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_
118