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 "class_linker.h"
21 #include "mirror/class-inl.h"
22 #include "mirror/method_handle_impl.h"
23 #include "mirror/method_type.h"
24 #include "mirror/string.h"
25 #include "mirror/throwable.h"
26 #include "reg_type.h"
27 #include "reg_type_cache.h"
28 
29 namespace art {
30 namespace verifier {
31 
GetFromId(uint16_t id)32 inline const art::verifier::RegType& RegTypeCache::GetFromId(uint16_t id) const {
33   DCHECK_LT(id, entries_.size());
34   const RegType* result = entries_[id];
35   DCHECK(result != nullptr);
36   return *result;
37 }
38 
FromCat1Const(int32_t value,bool precise)39 inline const ConstantType& RegTypeCache::FromCat1Const(int32_t value, bool precise) {
40   // We only expect 0 to be a precise constant.
41   DCHECK(value != 0 || precise);
42   if (precise && (value >= kMinSmallConstant) && (value <= kMaxSmallConstant)) {
43     return *small_precise_constants_[value - kMinSmallConstant];
44   }
45   return FromCat1NonSmallConstant(value, precise);
46 }
47 
Boolean()48 inline const BooleanType& RegTypeCache::Boolean() {
49   return *BooleanType::GetInstance();
50 }
Byte()51 inline const ByteType& RegTypeCache::Byte() {
52   return *ByteType::GetInstance();
53 }
Char()54 inline const CharType& RegTypeCache::Char() {
55   return *CharType::GetInstance();
56 }
Short()57 inline const ShortType& RegTypeCache::Short() {
58   return *ShortType::GetInstance();
59 }
Integer()60 inline const IntegerType& RegTypeCache::Integer() {
61   return *IntegerType::GetInstance();
62 }
Float()63 inline const FloatType& RegTypeCache::Float() {
64   return *FloatType::GetInstance();
65 }
LongLo()66 inline const LongLoType& RegTypeCache::LongLo() {
67   return *LongLoType::GetInstance();
68 }
LongHi()69 inline const LongHiType& RegTypeCache::LongHi() {
70   return *LongHiType::GetInstance();
71 }
DoubleLo()72 inline const DoubleLoType& RegTypeCache::DoubleLo() {
73   return *DoubleLoType::GetInstance();
74 }
DoubleHi()75 inline const DoubleHiType& RegTypeCache::DoubleHi() {
76   return *DoubleHiType::GetInstance();
77 }
Undefined()78 inline const UndefinedType& RegTypeCache::Undefined() {
79   return *UndefinedType::GetInstance();
80 }
Conflict()81 inline const ConflictType& RegTypeCache::Conflict() {
82   return *ConflictType::GetInstance();
83 }
Null()84 inline const NullType& RegTypeCache::Null() {
85   return *NullType::GetInstance();
86 }
87 
ByteConstant()88 inline const ImpreciseConstType& RegTypeCache::ByteConstant() {
89   const ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::min(), false);
90   DCHECK(result.IsImpreciseConstant());
91   return *down_cast<const ImpreciseConstType*>(&result);
92 }
93 
CharConstant()94 inline const ImpreciseConstType& RegTypeCache::CharConstant() {
95   int32_t jchar_max = static_cast<int32_t>(std::numeric_limits<jchar>::max());
96   const ConstantType& result =  FromCat1Const(jchar_max, false);
97   DCHECK(result.IsImpreciseConstant());
98   return *down_cast<const ImpreciseConstType*>(&result);
99 }
100 
ShortConstant()101 inline const ImpreciseConstType& RegTypeCache::ShortConstant() {
102   const ConstantType& result =  FromCat1Const(std::numeric_limits<jshort>::min(), false);
103   DCHECK(result.IsImpreciseConstant());
104   return *down_cast<const ImpreciseConstType*>(&result);
105 }
106 
IntConstant()107 inline const ImpreciseConstType& RegTypeCache::IntConstant() {
108   const ConstantType& result = FromCat1Const(std::numeric_limits<jint>::max(), false);
109   DCHECK(result.IsImpreciseConstant());
110   return *down_cast<const ImpreciseConstType*>(&result);
111 }
112 
PosByteConstant()113 inline const ImpreciseConstType& RegTypeCache::PosByteConstant() {
114   const ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::max(), false);
115   DCHECK(result.IsImpreciseConstant());
116   return *down_cast<const ImpreciseConstType*>(&result);
117 }
118 
PosShortConstant()119 inline const ImpreciseConstType& RegTypeCache::PosShortConstant() {
120   const ConstantType& result =  FromCat1Const(std::numeric_limits<jshort>::max(), false);
121   DCHECK(result.IsImpreciseConstant());
122   return *down_cast<const ImpreciseConstType*>(&result);
123 }
124 
JavaLangClass()125 inline const PreciseReferenceType& RegTypeCache::JavaLangClass() {
126   const RegType* result = &FromClass("Ljava/lang/Class;", mirror::Class::GetJavaLangClass(), true);
127   DCHECK(result->IsPreciseReference());
128   return *down_cast<const PreciseReferenceType*>(result);
129 }
130 
JavaLangString()131 inline const PreciseReferenceType& RegTypeCache::JavaLangString() {
132   // String is final and therefore always precise.
133   const RegType* result = &FromClass("Ljava/lang/String;", mirror::String::GetJavaLangString(),
134                                      true);
135   DCHECK(result->IsPreciseReference());
136   return *down_cast<const PreciseReferenceType*>(result);
137 }
138 
JavaLangInvokeMethodHandle()139 inline const PreciseReferenceType& RegTypeCache::JavaLangInvokeMethodHandle() {
140   const RegType* result = &FromClass("Ljava/lang/invoke/MethodHandle;",
141                                      mirror::MethodHandle::StaticClass(), true);
142   DCHECK(result->IsPreciseReference());
143   return *down_cast<const PreciseReferenceType*>(result);
144 }
145 
JavaLangInvokeMethodType()146 inline const PreciseReferenceType& RegTypeCache::JavaLangInvokeMethodType() {
147   const RegType* result = &FromClass("Ljava/lang/invoke/MethodType;",
148                                      mirror::MethodType::StaticClass(), true);
149   DCHECK(result->IsPreciseReference());
150   return *down_cast<const PreciseReferenceType*>(result);
151 }
152 
JavaLangThrowable(bool precise)153 inline const RegType&  RegTypeCache::JavaLangThrowable(bool precise) {
154   const RegType* result = &FromClass("Ljava/lang/Throwable;",
155                                      mirror::Throwable::GetJavaLangThrowable(), precise);
156   if (precise) {
157     DCHECK(result->IsPreciseReference());
158     return *down_cast<const PreciseReferenceType*>(result);
159   } else {
160     DCHECK(result->IsReference());
161     return *down_cast<const ReferenceType*>(result);
162   }
163 }
164 
JavaLangObject(bool precise)165 inline const RegType& RegTypeCache::JavaLangObject(bool precise) {
166   const RegType* result = &FromClass("Ljava/lang/Object;",
167                                      mirror::Class::GetJavaLangClass()->GetSuperClass(), precise);
168   if (precise) {
169     DCHECK(result->IsPreciseReference());
170     return *down_cast<const PreciseReferenceType*>(result);
171   } else {
172     DCHECK(result->IsReference());
173     return *down_cast<const ReferenceType*>(result);
174   }
175 }
176 
177 template <class RegTypeType>
AddEntry(RegTypeType * new_entry)178 inline RegTypeType& RegTypeCache::AddEntry(RegTypeType* new_entry) {
179   DCHECK(new_entry != nullptr);
180   entries_.push_back(new_entry);
181   if (new_entry->HasClass()) {
182     mirror::Class* klass = new_entry->GetClass();
183     DCHECK(!klass->IsPrimitive());
184     klass_entries_.push_back(std::make_pair(GcRoot<mirror::Class>(klass), new_entry));
185   }
186   return *new_entry;
187 }
188 
189 }  // namespace verifier
190 }  // namespace art
191 #endif  // ART_RUNTIME_VERIFIER_REG_TYPE_CACHE_INL_H_
192