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/string.h"
23 #include "mirror/throwable.h"
24 #include "reg_type.h"
25 #include "reg_type_cache.h"
26 
27 namespace art {
28 namespace verifier {
29 
GetFromId(uint16_t id)30 inline const art::verifier::RegType& RegTypeCache::GetFromId(uint16_t id) const {
31   DCHECK_LT(id, entries_.size());
32   const RegType* result = entries_[id];
33   DCHECK(result != nullptr);
34   return *result;
35 }
36 
FromCat1Const(int32_t value,bool precise)37 inline const ConstantType& RegTypeCache::FromCat1Const(int32_t value, bool precise) {
38   // We only expect 0 to be a precise constant.
39   DCHECK(value != 0 || precise);
40   if (precise && (value >= kMinSmallConstant) && (value <= kMaxSmallConstant)) {
41     return *small_precise_constants_[value - kMinSmallConstant];
42   }
43   return FromCat1NonSmallConstant(value, precise);
44 }
45 
ByteConstant()46 inline const ImpreciseConstType& RegTypeCache::ByteConstant() {
47   const ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::min(), false);
48   DCHECK(result.IsImpreciseConstant());
49   return *down_cast<const ImpreciseConstType*>(&result);
50 }
51 
CharConstant()52 inline const ImpreciseConstType& RegTypeCache::CharConstant() {
53   int32_t jchar_max = static_cast<int32_t>(std::numeric_limits<jchar>::max());
54   const ConstantType& result =  FromCat1Const(jchar_max, false);
55   DCHECK(result.IsImpreciseConstant());
56   return *down_cast<const ImpreciseConstType*>(&result);
57 }
58 
ShortConstant()59 inline const ImpreciseConstType& RegTypeCache::ShortConstant() {
60   const ConstantType& result =  FromCat1Const(std::numeric_limits<jshort>::min(), false);
61   DCHECK(result.IsImpreciseConstant());
62   return *down_cast<const ImpreciseConstType*>(&result);
63 }
64 
IntConstant()65 inline const ImpreciseConstType& RegTypeCache::IntConstant() {
66   const ConstantType& result = FromCat1Const(std::numeric_limits<jint>::max(), false);
67   DCHECK(result.IsImpreciseConstant());
68   return *down_cast<const ImpreciseConstType*>(&result);
69 }
70 
PosByteConstant()71 inline const ImpreciseConstType& RegTypeCache::PosByteConstant() {
72   const ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::max(), false);
73   DCHECK(result.IsImpreciseConstant());
74   return *down_cast<const ImpreciseConstType*>(&result);
75 }
76 
PosShortConstant()77 inline const ImpreciseConstType& RegTypeCache::PosShortConstant() {
78   const ConstantType& result =  FromCat1Const(std::numeric_limits<jshort>::max(), false);
79   DCHECK(result.IsImpreciseConstant());
80   return *down_cast<const ImpreciseConstType*>(&result);
81 }
82 
JavaLangClass()83 inline const PreciseReferenceType& RegTypeCache::JavaLangClass() {
84   const RegType* result = &FromClass("Ljava/lang/Class;", mirror::Class::GetJavaLangClass(), true);
85   DCHECK(result->IsPreciseReference());
86   return *down_cast<const PreciseReferenceType*>(result);
87 }
88 
JavaLangString()89 inline const PreciseReferenceType& RegTypeCache::JavaLangString() {
90   // String is final and therefore always precise.
91   const RegType* result = &FromClass("Ljava/lang/String;", mirror::String::GetJavaLangString(),
92                                      true);
93   DCHECK(result->IsPreciseReference());
94   return *down_cast<const PreciseReferenceType*>(result);
95 }
96 
JavaLangThrowable(bool precise)97 inline const RegType&  RegTypeCache::JavaLangThrowable(bool precise) {
98   const RegType* result = &FromClass("Ljava/lang/Throwable;",
99                                      mirror::Throwable::GetJavaLangThrowable(), precise);
100   if (precise) {
101     DCHECK(result->IsPreciseReference());
102     return *down_cast<const PreciseReferenceType*>(result);
103   } else {
104     DCHECK(result->IsReference());
105     return *down_cast<const ReferenceType*>(result);
106   }
107 }
108 
JavaLangObject(bool precise)109 inline const RegType& RegTypeCache::JavaLangObject(bool precise) {
110   const RegType* result = &FromClass("Ljava/lang/Object;",
111                                      mirror::Class::GetJavaLangClass()->GetSuperClass(), precise);
112   if (precise) {
113     DCHECK(result->IsPreciseReference());
114     return *down_cast<const PreciseReferenceType*>(result);
115   } else {
116     DCHECK(result->IsReference());
117     return *down_cast<const ReferenceType*>(result);
118   }
119 }
120 
121 template <class RegTypeType>
AddEntry(RegTypeType * new_entry)122 inline RegTypeType& RegTypeCache::AddEntry(RegTypeType* new_entry) {
123   DCHECK(new_entry != nullptr);
124   entries_.push_back(new_entry);
125   if (new_entry->HasClass()) {
126     mirror::Class* klass = new_entry->GetClass();
127     DCHECK(!klass->IsPrimitive());
128     klass_entries_.push_back(std::make_pair(GcRoot<mirror::Class>(klass), new_entry));
129   }
130   return *new_entry;
131 }
132 
133 }  // namespace verifier
134 }  // namespace art
135 #endif  // ART_RUNTIME_VERIFIER_REG_TYPE_CACHE_INL_H_
136