• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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_JNI_LOCAL_REFERENCE_TABLE_INL_H_
18 #define ART_RUNTIME_JNI_LOCAL_REFERENCE_TABLE_INL_H_
19 
20 #include "local_reference_table.h"
21 
22 #include "android-base/stringprintf.h"
23 
24 #include "base/casts.h"
25 #include "gc_root-inl.h"
26 #include "obj_ptr-inl.h"
27 #include "mirror/object_reference.h"
28 #include "verify_object.h"
29 
30 namespace art HIDDEN {
31 namespace jni {
32 
SetReference(ObjPtr<mirror::Object> ref)33 inline void LrtEntry::SetReference(ObjPtr<mirror::Object> ref) {
34   root_ = GcRoot<mirror::Object>(
35       mirror::CompressedReference<mirror::Object>::FromMirrorPtr(ref.Ptr()));
36   DCHECK(!IsFree());
37   DCHECK(!IsSerialNumber());
38 }
39 
GetReference()40 inline ObjPtr<mirror::Object> LrtEntry::GetReference() {
41   DCHECK(!IsFree());
42   DCHECK(!IsSerialNumber());
43   DCHECK(!IsNull());
44   // Local references do not need read barriers. They are marked during the thread root flip.
45   return root_.Read<kWithoutReadBarrier>();
46 }
47 
SetNextFree(uint32_t next_free)48 inline void LrtEntry::SetNextFree(uint32_t next_free) {
49   SetVRegValue(NextFreeField::Update(next_free, 1u << kFlagFree));
50   DCHECK(IsFree());
51   DCHECK(!IsSerialNumber());
52 }
53 
SetSerialNumber(uint32_t serial_number)54 inline void LrtEntry::SetSerialNumber(uint32_t serial_number) {
55   SetVRegValue(SerialNumberField::Update(serial_number, 1u << kFlagSerialNumber));
56   DCHECK(!IsFree());
57   DCHECK(IsSerialNumber());
58 }
59 
SetVRegValue(uint32_t value)60 inline void LrtEntry::SetVRegValue(uint32_t value) {
61   root_ = GcRoot<mirror::Object>(
62       mirror::CompressedReference<mirror::Object>::FromVRegValue(value));
63 }
64 
GetReferenceEntryIndex(IndirectRef iref)65 inline uint32_t LocalReferenceTable::GetReferenceEntryIndex(IndirectRef iref) const {
66   DCHECK_EQ(IndirectReferenceTable::GetIndirectRefKind(iref), kLocal);
67   LrtEntry* entry = ToLrtEntry(iref);
68 
69   if (LIKELY(small_table_ != nullptr)) {
70     DCHECK(tables_.empty());
71     if (!std::less<const LrtEntry*>()(entry, small_table_) &&
72         std::less<const LrtEntry*>()(entry, small_table_ + kSmallLrtEntries)) {
73       return dchecked_integral_cast<uint32_t>(entry - small_table_);
74     }
75   } else {
76     for (size_t i = 0, size = tables_.size(); i != size; ++i) {
77       LrtEntry* table = tables_[i];
78       size_t table_size = GetTableSize(i);
79       if (!std::less<const LrtEntry*>()(entry, table) &&
80           std::less<const LrtEntry*>()(entry, table + table_size)) {
81         return dchecked_integral_cast<size_t>(i != 0u ? table_size : 0u) +
82                dchecked_integral_cast<size_t>(entry - table);
83       }
84     }
85   }
86   return std::numeric_limits<uint32_t>::max();
87 }
88 
IsValidReference(IndirectRef iref,std::string * error_msg)89 inline bool LocalReferenceTable::IsValidReference(IndirectRef iref,
90                                                   /*out*/std::string* error_msg) const {
91   uint32_t entry_index = GetReferenceEntryIndex(iref);
92   if (UNLIKELY(entry_index == std::numeric_limits<uint32_t>::max())) {
93     *error_msg = android::base::StringPrintf("reference outside the table: %p", iref);
94     return false;
95   }
96   if (UNLIKELY(entry_index >= segment_state_.top_index)) {
97     *error_msg = android::base::StringPrintf("popped reference at index %u in a table of size %u",
98                                              entry_index,
99                                              segment_state_.top_index);
100     return false;
101   }
102   LrtEntry* entry = ToLrtEntry(iref);
103   LrtEntry* serial_number_entry = GetCheckJniSerialNumberEntry(entry);
104   if (serial_number_entry->IsSerialNumber()) {
105     // This reference was created with CheckJNI enabled.
106     uint32_t expected_serial_number = serial_number_entry->GetSerialNumber();
107     uint32_t serial_number = entry - serial_number_entry;
108     DCHECK_LT(serial_number, kCheckJniEntriesPerReference);
109     if (serial_number != expected_serial_number || serial_number == 0u) {
110       *error_msg = android::base::StringPrintf(
111           "reference at index %u with bad serial number %u v. %u (valid 1 - %u)",
112           entry_index,
113           serial_number,
114           expected_serial_number,
115           dchecked_integral_cast<uint32_t>(kCheckJniEntriesPerReference - 1u));
116       return false;
117     }
118   }
119   if (UNLIKELY(entry->IsFree())) {
120     *error_msg = android::base::StringPrintf("deleted reference at index %u", entry_index);
121     return false;
122   }
123   if (UNLIKELY(entry->IsNull())) {
124     // This should never really happen and may indicate memory coruption.
125     *error_msg = android::base::StringPrintf("null reference at index %u", entry_index);
126     return false;
127   }
128   return true;
129 }
130 
DCheckValidReference(IndirectRef iref)131 inline void LocalReferenceTable::DCheckValidReference(IndirectRef iref) const {
132   // If CheckJNI is performing the checks, we should not reach this point with an invalid
133   // reference with the exception of gtests that intercept the CheckJNI abort and proceed
134   // to decode the reference anyway and we do not want to abort again in this case.
135   if (kIsDebugBuild && !IsCheckJniEnabled()) {
136     std::string error_msg;
137     CHECK(IsValidReference(iref, &error_msg)) << error_msg;
138   }
139 }
140 
Get(IndirectRef iref)141 inline ObjPtr<mirror::Object> LocalReferenceTable::Get(IndirectRef iref) const {
142   DCheckValidReference(iref);
143   return ToLrtEntry(iref)->GetReference();
144 }
145 
Update(IndirectRef iref,ObjPtr<mirror::Object> obj)146 inline void LocalReferenceTable::Update(IndirectRef iref, ObjPtr<mirror::Object> obj) {
147   DCheckValidReference(iref);
148   ToLrtEntry(iref)->SetReference(obj);
149 }
150 
151 }  // namespace jni
152 }  // namespace art
153 
154 #endif  // ART_RUNTIME_JNI_LOCAL_REFERENCE_TABLE_INL_H_
155