reg_type_cache.h revision d8f69b086baf6717ce949d1c4de90d73b91083b0
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_H_ 18#define ART_RUNTIME_VERIFIER_REG_TYPE_CACHE_H_ 19 20#include "base/casts.h" 21#include "base/macros.h" 22#include "base/stl_util.h" 23#include "object_callbacks.h" 24#include "reg_type.h" 25#include "runtime.h" 26 27#include <stdint.h> 28#include <vector> 29 30namespace art { 31namespace mirror { 32 class Class; 33 class ClassLoader; 34} // namespace mirror 35class StringPiece; 36 37namespace verifier { 38 39class RegType; 40 41class RegTypeCache { 42 public: 43 explicit RegTypeCache(bool can_load_classes); 44 ~RegTypeCache(); 45 static void Init() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 46 if (!RegTypeCache::primitive_initialized_) { 47 CHECK_EQ(RegTypeCache::primitive_count_, 0); 48 CreatePrimitiveAndSmallConstantTypes(); 49 CHECK_EQ(RegTypeCache::primitive_count_, kNumPrimitivesAndSmallConstants); 50 RegTypeCache::primitive_initialized_ = true; 51 } 52 } 53 static void ShutDown(); 54 const art::verifier::RegType& GetFromId(uint16_t id) const; 55 const RegType& From(mirror::ClassLoader* loader, const char* descriptor, bool precise) 56 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 57 const RegType& FromClass(const char* descriptor, mirror::Class* klass, bool precise) 58 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 59 const ConstantType& FromCat1Const(int32_t value, bool precise) 60 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 61 const ConstantType& FromCat2ConstLo(int32_t value, bool precise) 62 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 63 const ConstantType& FromCat2ConstHi(int32_t value, bool precise) 64 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 65 const RegType& FromDescriptor(mirror::ClassLoader* loader, const char* descriptor, bool precise) 66 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 67 const RegType& FromUnresolvedMerge(const RegType& left, const RegType& right) 68 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 69 const RegType& FromUnresolvedSuperClass(const RegType& child) 70 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 71 const RegType& JavaLangString() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 72 // String is final and therefore always precise. 73 return From(NULL, "Ljava/lang/String;", true); 74 } 75 const RegType& JavaLangThrowable(bool precise) 76 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 77 return From(NULL, "Ljava/lang/Throwable;", precise); 78 } 79 const ConstantType& Zero() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 80 return FromCat1Const(0, true); 81 } 82 const ConstantType& One() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 83 return FromCat1Const(1, true); 84 } 85 size_t GetCacheSize() { 86 return entries_.size(); 87 } 88 const RegType& Boolean() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 89 return *BooleanType::GetInstance(); 90 } 91 const RegType& Byte() { 92 return *ByteType::GetInstance(); 93 } 94 const RegType& Char() { 95 return *CharType::GetInstance(); 96 } 97 const RegType& Short() { 98 return *ShortType::GetInstance(); 99 } 100 const RegType& Integer() { 101 return *IntegerType::GetInstance(); 102 } 103 const RegType& Float() { 104 return *FloatType::GetInstance(); 105 } 106 const RegType& LongLo() { 107 return *LongLoType::GetInstance(); 108 } 109 const RegType& LongHi() { 110 return *LongHiType::GetInstance(); 111 } 112 const RegType& DoubleLo() { 113 return *DoubleLoType::GetInstance(); 114 } 115 const RegType& DoubleHi() { 116 return *DoubleHiType::GetInstance(); 117 } 118 const RegType& Undefined() { 119 return *UndefinedType::GetInstance(); 120 } 121 const RegType& Conflict() { 122 return *ConflictType::GetInstance(); 123 } 124 const RegType& JavaLangClass(bool precise) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 125 return From(NULL, "Ljava/lang/Class;", precise); 126 } 127 const RegType& JavaLangObject(bool precise) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 128 return From(NULL, "Ljava/lang/Object;", precise); 129 } 130 const UninitializedType& Uninitialized(const RegType& type, uint32_t allocation_pc) 131 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 132 // Create an uninitialized 'this' argument for the given type. 133 const UninitializedType& UninitializedThisArgument(const RegType& type) 134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 135 const RegType& FromUninitialized(const RegType& uninit_type) 136 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 137 const ImpreciseConstType& ByteConstant() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 138 const ImpreciseConstType& CharConstant() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 139 const ImpreciseConstType& ShortConstant() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 140 const ImpreciseConstType& IntConstant() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 141 const ImpreciseConstType& PosByteConstant() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 142 const ImpreciseConstType& PosShortConstant() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 143 const RegType& GetComponentType(const RegType& array, mirror::ClassLoader* loader) 144 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 145 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 146 const RegType& RegTypeFromPrimitiveType(Primitive::Type) const; 147 148 void VisitRoots(RootCallback* callback, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 149 150 private: 151 void FillPrimitiveAndSmallConstantTypes() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 152 mirror::Class* ResolveClass(const char* descriptor, mirror::ClassLoader* loader) 153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 154 bool MatchDescriptor(size_t idx, const StringPiece& descriptor, bool precise) 155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 156 const ConstantType& FromCat1NonSmallConstant(int32_t value, bool precise) 157 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 158 159 void AddEntry(RegType* new_entry); 160 161 template <class Type> 162 static Type* CreatePrimitiveTypeInstance(const std::string& descriptor) 163 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 164 static void CreatePrimitiveAndSmallConstantTypes() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 165 166 // The actual storage for the RegTypes. 167 std::vector<RegType*> entries_; 168 169 // A quick look up for popular small constants. 170 static constexpr int32_t kMinSmallConstant = -1; 171 static constexpr int32_t kMaxSmallConstant = 4; 172 static PreciseConstType* small_precise_constants_[kMaxSmallConstant - kMinSmallConstant + 1]; 173 174 static constexpr size_t kNumPrimitivesAndSmallConstants = 175 12 + (kMaxSmallConstant - kMinSmallConstant + 1); 176 177 // Have the well known global primitives been created? 178 static bool primitive_initialized_; 179 180 // Number of well known primitives that will be copied into a RegTypeCache upon construction. 181 static uint16_t primitive_count_; 182 183 // Whether or not we're allowed to load classes. 184 const bool can_load_classes_; 185 186 DISALLOW_COPY_AND_ASSIGN(RegTypeCache); 187}; 188 189} // namespace verifier 190} // namespace art 191 192#endif // ART_RUNTIME_VERIFIER_REG_TYPE_CACHE_H_ 193