Home | History | Annotate | Download | only in verifier
      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 
     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 
     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 
     46 inline const BooleanType& RegTypeCache::Boolean() {
     47   return *BooleanType::GetInstance();
     48 }
     49 inline const ByteType& RegTypeCache::Byte() {
     50   return *ByteType::GetInstance();
     51 }
     52 inline const CharType& RegTypeCache::Char() {
     53   return *CharType::GetInstance();
     54 }
     55 inline const ShortType& RegTypeCache::Short() {
     56   return *ShortType::GetInstance();
     57 }
     58 inline const IntegerType& RegTypeCache::Integer() {
     59   return *IntegerType::GetInstance();
     60 }
     61 inline const FloatType& RegTypeCache::Float() {
     62   return *FloatType::GetInstance();
     63 }
     64 inline const LongLoType& RegTypeCache::LongLo() {
     65   return *LongLoType::GetInstance();
     66 }
     67 inline const LongHiType& RegTypeCache::LongHi() {
     68   return *LongHiType::GetInstance();
     69 }
     70 inline const DoubleLoType& RegTypeCache::DoubleLo() {
     71   return *DoubleLoType::GetInstance();
     72 }
     73 inline const DoubleHiType& RegTypeCache::DoubleHi() {
     74   return *DoubleHiType::GetInstance();
     75 }
     76 inline const UndefinedType& RegTypeCache::Undefined() {
     77   return *UndefinedType::GetInstance();
     78 }
     79 inline const ConflictType& RegTypeCache::Conflict() {
     80   return *ConflictType::GetInstance();
     81 }
     82 
     83 inline const ImpreciseConstType& RegTypeCache::ByteConstant() {
     84   const ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::min(), false);
     85   DCHECK(result.IsImpreciseConstant());
     86   return *down_cast<const ImpreciseConstType*>(&result);
     87 }
     88 
     89 inline const ImpreciseConstType& RegTypeCache::CharConstant() {
     90   int32_t jchar_max = static_cast<int32_t>(std::numeric_limits<jchar>::max());
     91   const ConstantType& result =  FromCat1Const(jchar_max, false);
     92   DCHECK(result.IsImpreciseConstant());
     93   return *down_cast<const ImpreciseConstType*>(&result);
     94 }
     95 
     96 inline const ImpreciseConstType& RegTypeCache::ShortConstant() {
     97   const ConstantType& result =  FromCat1Const(std::numeric_limits<jshort>::min(), false);
     98   DCHECK(result.IsImpreciseConstant());
     99   return *down_cast<const ImpreciseConstType*>(&result);
    100 }
    101 
    102 inline const ImpreciseConstType& RegTypeCache::IntConstant() {
    103   const ConstantType& result = FromCat1Const(std::numeric_limits<jint>::max(), false);
    104   DCHECK(result.IsImpreciseConstant());
    105   return *down_cast<const ImpreciseConstType*>(&result);
    106 }
    107 
    108 inline const ImpreciseConstType& RegTypeCache::PosByteConstant() {
    109   const ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::max(), false);
    110   DCHECK(result.IsImpreciseConstant());
    111   return *down_cast<const ImpreciseConstType*>(&result);
    112 }
    113 
    114 inline const ImpreciseConstType& RegTypeCache::PosShortConstant() {
    115   const ConstantType& result =  FromCat1Const(std::numeric_limits<jshort>::max(), false);
    116   DCHECK(result.IsImpreciseConstant());
    117   return *down_cast<const ImpreciseConstType*>(&result);
    118 }
    119 
    120 inline const PreciseReferenceType& RegTypeCache::JavaLangClass() {
    121   const RegType* result = &FromClass("Ljava/lang/Class;", mirror::Class::GetJavaLangClass(), true);
    122   DCHECK(result->IsPreciseReference());
    123   return *down_cast<const PreciseReferenceType*>(result);
    124 }
    125 
    126 inline const PreciseReferenceType& RegTypeCache::JavaLangString() {
    127   // String is final and therefore always precise.
    128   const RegType* result = &FromClass("Ljava/lang/String;", mirror::String::GetJavaLangString(),
    129                                      true);
    130   DCHECK(result->IsPreciseReference());
    131   return *down_cast<const PreciseReferenceType*>(result);
    132 }
    133 
    134 inline const RegType&  RegTypeCache::JavaLangThrowable(bool precise) {
    135   const RegType* result = &FromClass("Ljava/lang/Throwable;",
    136                                      mirror::Throwable::GetJavaLangThrowable(), precise);
    137   if (precise) {
    138     DCHECK(result->IsPreciseReference());
    139     return *down_cast<const PreciseReferenceType*>(result);
    140   } else {
    141     DCHECK(result->IsReference());
    142     return *down_cast<const ReferenceType*>(result);
    143   }
    144 }
    145 
    146 inline const RegType& RegTypeCache::JavaLangObject(bool precise) {
    147   const RegType* result = &FromClass("Ljava/lang/Object;",
    148                                      mirror::Class::GetJavaLangClass()->GetSuperClass(), precise);
    149   if (precise) {
    150     DCHECK(result->IsPreciseReference());
    151     return *down_cast<const PreciseReferenceType*>(result);
    152   } else {
    153     DCHECK(result->IsReference());
    154     return *down_cast<const ReferenceType*>(result);
    155   }
    156 }
    157 
    158 template <class RegTypeType>
    159 inline RegTypeType& RegTypeCache::AddEntry(RegTypeType* new_entry) {
    160   DCHECK(new_entry != nullptr);
    161   entries_.push_back(new_entry);
    162   if (new_entry->HasClass()) {
    163     mirror::Class* klass = new_entry->GetClass();
    164     DCHECK(!klass->IsPrimitive());
    165     klass_entries_.push_back(std::make_pair(GcRoot<mirror::Class>(klass), new_entry));
    166   }
    167   return *new_entry;
    168 }
    169 
    170 }  // namespace verifier
    171 }  // namespace art
    172 #endif  // ART_RUNTIME_VERIFIER_REG_TYPE_CACHE_INL_H_
    173