1 /*
2 * Copyright (C) 2012 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_VERIFIER_REG_TYPE_CACHE_INL_H_
18 #define ART_RUNTIME_VERIFIER_REG_TYPE_CACHE_INL_H_
19
20 #include "base/bit_vector-inl.h"
21 #include "class_root-inl.h"
22 #include "dex/dex_file.h"
23 #include "mirror/class-inl.h"
24 #include "mirror/method_handle_impl.h"
25 #include "mirror/method_type.h"
26 #include "mirror/string.h"
27 #include "mirror/throwable.h"
28 #include "reg_type.h"
29 #include "reg_type_cache.h"
30
31 namespace art HIDDEN {
32 namespace verifier {
33
GetFromId(uint16_t id)34 inline const art::verifier::RegType& RegTypeCache::GetFromId(uint16_t id) const {
35 DCHECK_LT(id, entries_.size());
36 const RegType* result = entries_[id];
37 DCHECK(result != nullptr);
38 return *result;
39 }
40
41 namespace detail {
42
43 struct RegKindToCacheId : RegTypeCache {
44 // Inherit fixed cache ids from `RegTypeCache` and add fake non-fixed cache ids so that
45 // we can use `FOR_EACH_CONCRETE_REG_TYPE` to check the fixed cache ids.
46 #define DEFINE_FAKE_CACHE_ID(name) \
47 static_assert(RegType::Kind::k##name >= kNumberOfRegKindCacheIds); \
48 static constexpr uint32_t k##name##CacheId = RegType::Kind::k##name;
49 DEFINE_FAKE_CACHE_ID(UnresolvedReference)
50 DEFINE_FAKE_CACHE_ID(UninitializedReference)
51 DEFINE_FAKE_CACHE_ID(UninitializedThisReference)
52 DEFINE_FAKE_CACHE_ID(UnresolvedUninitializedReference)
53 DEFINE_FAKE_CACHE_ID(UnresolvedUninitializedThisReference)
54 DEFINE_FAKE_CACHE_ID(UnresolvedMergedReference)
55 DEFINE_FAKE_CACHE_ID(Reference)
56 #undef DEFINE_FAKE_CACHE_ID
57
58 #define ASSERT_CACHE_ID_EQUALS_KIND(name) \
59 static_assert(k##name##CacheId == RegType::Kind::k##name);
60 FOR_EACH_CONCRETE_REG_TYPE(ASSERT_CACHE_ID_EQUALS_KIND);
61 #undef ASSERT_CACHE_ID_EQUALS_KIND
62
TranslateRegKindToCacheId63 static constexpr uint16_t Translate(RegType::Kind kind) {
64 DCHECK_LT(kind, kNumberOfRegKindCacheIds);
65 return kind;
66 }
67 };
68
69 } // namespace detail
70
IdForRegKind(RegType::Kind kind)71 constexpr uint16_t RegTypeCache::IdForRegKind(RegType::Kind kind) {
72 return detail::RegKindToCacheId::Translate(kind);
73 }
74
RegKindForId(uint16_t id)75 constexpr RegType::Kind RegTypeCache::RegKindForId(uint16_t id) {
76 DCHECK_LT(id, NumberOfRegKindCacheIds());
77 RegType::Kind kind = enum_cast<RegType::Kind>(id);
78 DCHECK_EQ(id, IdForRegKind(kind));
79 return kind;
80 }
81
GetFromRegKind(RegType::Kind kind)82 inline const RegType& RegTypeCache::GetFromRegKind(RegType::Kind kind) const {
83 return GetFromId(IdForRegKind(kind));
84 }
85
FromTypeIndex(dex::TypeIndex type_index)86 inline const RegType& RegTypeCache::FromTypeIndex(dex::TypeIndex type_index) {
87 DCHECK_LT(type_index.index_, dex_file_->NumTypeIds());
88 if (ids_for_type_index_[type_index.index_] != kNoIdForTypeIndex) {
89 return GetFromId(ids_for_type_index_[type_index.index_]);
90 }
91 return FromTypeIndexUncached(type_index);
92 }
93
Boolean()94 inline const BooleanType& RegTypeCache::Boolean() const {
95 return *down_cast<const BooleanType*>(entries_[kBooleanCacheId]);
96 }
97
Byte()98 inline const ByteType& RegTypeCache::Byte() const {
99 return *down_cast<const ByteType*>(entries_[kByteCacheId]);
100 }
101
Char()102 inline const CharType& RegTypeCache::Char() const {
103 return *down_cast<const CharType*>(entries_[kCharCacheId]);
104 }
105
Short()106 inline const ShortType& RegTypeCache::Short() const {
107 return *down_cast<const ShortType*>(entries_[kShortCacheId]);
108 }
109
Integer()110 inline const IntegerType& RegTypeCache::Integer() const {
111 return *down_cast<const IntegerType*>(entries_[kIntegerCacheId]);
112 }
113
Float()114 inline const FloatType& RegTypeCache::Float() const {
115 return *down_cast<const FloatType*>(entries_[kFloatCacheId]);
116 }
117
LongLo()118 inline const LongLoType& RegTypeCache::LongLo() const {
119 return *down_cast<const LongLoType*>(entries_[kLongLoCacheId]);
120 }
121
LongHi()122 inline const LongHiType& RegTypeCache::LongHi() const {
123 return *down_cast<const LongHiType*>(entries_[kLongHiCacheId]);
124 }
125
DoubleLo()126 inline const DoubleLoType& RegTypeCache::DoubleLo() const {
127 return *down_cast<const DoubleLoType*>(entries_[kDoubleLoCacheId]);
128 }
129
DoubleHi()130 inline const DoubleHiType& RegTypeCache::DoubleHi() const {
131 return *down_cast<const DoubleHiType*>(entries_[kDoubleHiCacheId]);
132 }
133
Undefined()134 inline const UndefinedType& RegTypeCache::Undefined() const {
135 return *down_cast<const UndefinedType*>(entries_[kUndefinedCacheId]);
136 }
137
Conflict()138 inline const ConflictType& RegTypeCache::Conflict() const {
139 return *down_cast<const ConflictType*>(entries_[kConflictCacheId]);
140 }
141
Null()142 inline const NullType& RegTypeCache::Null() const {
143 return *down_cast<const NullType*>(entries_[kNullCacheId]);
144 }
145
Zero()146 inline const ZeroType& RegTypeCache::Zero() const {
147 return *down_cast<const ZeroType*>(entries_[kZeroCacheId]);
148 }
149
BooleanConstant()150 inline const BooleanConstantType& RegTypeCache::BooleanConstant() const {
151 return *down_cast<const BooleanConstantType*>(entries_[kBooleanConstantCacheId]);
152 }
153
ByteConstant()154 inline const ByteConstantType& RegTypeCache::ByteConstant() const {
155 return *down_cast<const ByteConstantType*>(entries_[kByteConstantCacheId]);
156 }
157
CharConstant()158 inline const CharConstantType& RegTypeCache::CharConstant() const {
159 return *down_cast<const CharConstantType*>(entries_[kCharConstantCacheId]);
160 }
161
ShortConstant()162 inline const ShortConstantType& RegTypeCache::ShortConstant() const {
163 return *down_cast<const ShortConstantType*>(entries_[kShortConstantCacheId]);
164 }
165
IntegerConstant()166 inline const IntegerConstantType& RegTypeCache::IntegerConstant() const {
167 return *down_cast<const IntegerConstantType*>(entries_[kIntegerConstantCacheId]);
168 }
169
PositiveByteConstant()170 inline const PositiveByteConstantType& RegTypeCache::PositiveByteConstant() const {
171 return *down_cast<const PositiveByteConstantType*>(entries_[kPositiveByteConstantCacheId]);
172 }
173
PositiveShortConstant()174 inline const PositiveShortConstantType& RegTypeCache::PositiveShortConstant() const {
175 return *down_cast<const PositiveShortConstantType*>(entries_[kPositiveShortConstantCacheId]);
176 }
177
ConstantLo()178 inline const ConstantLoType& RegTypeCache::ConstantLo() const {
179 return *down_cast<const ConstantLoType*>(entries_[kConstantLoCacheId]);
180 }
181
ConstantHi()182 inline const ConstantHiType& RegTypeCache::ConstantHi() const {
183 return *down_cast<const ConstantHiType*>(entries_[kConstantHiCacheId]);
184 }
185
JavaLangClass()186 inline const ReferenceType& RegTypeCache::JavaLangClass() {
187 const RegType* result = &FromClass(GetClassRoot<mirror::Class>());
188 DCHECK(result->GetClass()->DescriptorEquals("Ljava/lang/Class;"));
189 DCHECK(result->IsReference());
190 return *down_cast<const ReferenceType*>(result);
191 }
192
JavaLangString()193 inline const ReferenceType& RegTypeCache::JavaLangString() {
194 const RegType* result = &FromClass(GetClassRoot<mirror::String>());
195 DCHECK(result->GetClass()->DescriptorEquals("Ljava/lang/String;"));
196 DCHECK(result->IsReference());
197 return *down_cast<const ReferenceType*>(result);
198 }
199
JavaLangInvokeMethodHandle()200 inline const ReferenceType& RegTypeCache::JavaLangInvokeMethodHandle() {
201 const RegType* result = &FromClass(GetClassRoot<mirror::MethodHandle>());
202 DCHECK(result->GetClass()->DescriptorEquals("Ljava/lang/invoke/MethodHandle;"));
203 DCHECK(result->IsReference());
204 return *down_cast<const ReferenceType*>(result);
205 }
206
JavaLangInvokeMethodType()207 inline const ReferenceType& RegTypeCache::JavaLangInvokeMethodType() {
208 const RegType* result = &FromClass(GetClassRoot<mirror::MethodType>());
209 DCHECK(result->GetClass()->DescriptorEquals("Ljava/lang/invoke/MethodType;"));
210 DCHECK(result->IsReference());
211 return *down_cast<const ReferenceType*>(result);
212 }
213
JavaLangThrowable()214 inline const ReferenceType& RegTypeCache::JavaLangThrowable() {
215 const RegType* result = &FromClass(GetClassRoot<mirror::Throwable>());
216 DCHECK(result->GetClass()->DescriptorEquals("Ljava/lang/Throwable;"));
217 DCHECK(result->IsReference());
218 return *down_cast<const ReferenceType*>(result);
219 }
220
JavaLangObject()221 inline const JavaLangObjectType& RegTypeCache::JavaLangObject() {
222 const RegType& result = GetFromId(kJavaLangObjectCacheId);
223 DCHECK_EQ(result.GetDescriptor(), "Ljava/lang/Object;");
224 DCHECK(result.IsJavaLangObject());
225 return down_cast<const JavaLangObjectType&>(result);
226 }
227
228 template <class RegTypeType>
AddEntry(RegTypeType * new_entry)229 inline RegTypeType& RegTypeCache::AddEntry(RegTypeType* new_entry) {
230 DCHECK(new_entry != nullptr);
231 entries_.push_back(new_entry);
232 if (new_entry->HasClass()) {
233 Handle<mirror::Class> klass = new_entry->GetClassHandle();
234 DCHECK(!klass->IsPrimitive());
235 klass_entries_.push_back(std::make_pair(klass, new_entry));
236 }
237 return *new_entry;
238 }
239
240 } // namespace verifier
241 } // namespace art
242 #endif // ART_RUNTIME_VERIFIER_REG_TYPE_CACHE_INL_H_
243