blob: b371d7e3919351649282a05527de5d33f9c0f0e0 [file] [log] [blame]
Ian Rogers776ac1f2012-04-13 23:36:36 -07001/*
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
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080017#include "reg_type_cache-inl.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070018
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080019#include "base/casts.h"
Ian Rogers98379392014-02-24 16:53:16 -080020#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "dex_file-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/class-inl.h"
23#include "mirror/object-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070024#include "reg_type-inl.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070025
26namespace art {
27namespace verifier {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -070028
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080029bool RegTypeCache::primitive_initialized_ = false;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080030uint16_t RegTypeCache::primitive_count_ = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -070031const PreciseConstType* RegTypeCache::small_precise_constants_[kMaxSmallConstant - kMinSmallConstant + 1];
Ian Rogers776ac1f2012-04-13 23:36:36 -070032
Ian Rogers7b078e82014-09-10 14:44:24 -070033static bool MatchingPrecisionForClass(const RegType* entry, bool precise)
Ian Rogersb4903572012-10-11 11:52:56 -070034 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers04f94f42013-06-10 15:09:26 -070035 if (entry->IsPreciseReference() == precise) {
36 // We were or weren't looking for a precise reference and we found what we need.
37 return true;
38 } else {
39 if (!precise && entry->GetClass()->CannotBeAssignedFromOtherTypes()) {
40 // We weren't looking for a precise reference, as we're looking up based on a descriptor, but
41 // we found a matching entry based on the descriptor. Return the precise entry in that case.
42 return true;
43 }
44 return false;
45 }
Ian Rogersb4903572012-10-11 11:52:56 -070046}
47
Ian Rogers41c65c12013-09-05 16:57:17 -070048void RegTypeCache::FillPrimitiveAndSmallConstantTypes() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080049 entries_.push_back(UndefinedType::GetInstance());
50 entries_.push_back(ConflictType::GetInstance());
51 entries_.push_back(BooleanType::GetInstance());
52 entries_.push_back(ByteType::GetInstance());
53 entries_.push_back(ShortType::GetInstance());
54 entries_.push_back(CharType::GetInstance());
55 entries_.push_back(IntegerType::GetInstance());
56 entries_.push_back(LongLoType::GetInstance());
57 entries_.push_back(LongHiType::GetInstance());
58 entries_.push_back(FloatType::GetInstance());
59 entries_.push_back(DoubleLoType::GetInstance());
60 entries_.push_back(DoubleHiType::GetInstance());
Ian Rogers41c65c12013-09-05 16:57:17 -070061 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
62 int32_t i = value - kMinSmallConstant;
63 DCHECK_EQ(entries_.size(), small_precise_constants_[i]->GetId());
64 entries_.push_back(small_precise_constants_[i]);
65 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080066 DCHECK_EQ(entries_.size(), primitive_count_);
67}
68
Ian Rogersd8f69b02014-09-10 21:43:52 +000069const RegType& RegTypeCache::FromDescriptor(mirror::ClassLoader* loader, const char* descriptor,
70 bool precise) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -070071 DCHECK(RegTypeCache::primitive_initialized_);
72 if (descriptor[1] == '\0') {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080073 switch (descriptor[0]) {
74 case 'Z':
75 return Boolean();
76 case 'B':
77 return Byte();
78 case 'S':
79 return Short();
80 case 'C':
81 return Char();
82 case 'I':
83 return Integer();
84 case 'J':
85 return LongLo();
86 case 'F':
87 return Float();
88 case 'D':
89 return DoubleLo();
90 case 'V': // For void types, conflict types.
91 default:
Ian Rogersad0b3a32012-04-16 14:50:24 -070092 return Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -070093 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080094 } else if (descriptor[0] == 'L' || descriptor[0] == '[') {
95 return From(loader, descriptor, precise);
96 } else {
97 return Conflict();
98 }
Andreas Gampec8ccf682014-09-29 20:07:43 -070099}
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800100
Ian Rogersd8f69b02014-09-10 21:43:52 +0000101const RegType& RegTypeCache::RegTypeFromPrimitiveType(Primitive::Type prim_type) const {
Ian Rogers7b078e82014-09-10 14:44:24 -0700102 DCHECK(RegTypeCache::primitive_initialized_);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800103 switch (prim_type) {
104 case Primitive::kPrimBoolean:
105 return *BooleanType::GetInstance();
106 case Primitive::kPrimByte:
107 return *ByteType::GetInstance();
108 case Primitive::kPrimShort:
109 return *ShortType::GetInstance();
110 case Primitive::kPrimChar:
111 return *CharType::GetInstance();
112 case Primitive::kPrimInt:
113 return *IntegerType::GetInstance();
114 case Primitive::kPrimLong:
115 return *LongLoType::GetInstance();
116 case Primitive::kPrimFloat:
117 return *FloatType::GetInstance();
118 case Primitive::kPrimDouble:
119 return *DoubleLoType::GetInstance();
120 case Primitive::kPrimVoid:
121 default:
122 return *ConflictType::GetInstance();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700123 }
124}
125
Ian Rogers1ff3c982014-08-12 02:30:58 -0700126bool RegTypeCache::MatchDescriptor(size_t idx, const StringPiece& descriptor, bool precise) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700127 const RegType* entry = entries_[idx];
Ian Rogers1ff3c982014-08-12 02:30:58 -0700128 if (descriptor != entry->descriptor_) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700129 return false;
130 }
Ian Rogers04f94f42013-06-10 15:09:26 -0700131 if (entry->HasClass()) {
132 return MatchingPrecisionForClass(entry, precise);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800133 }
Ian Rogers04f94f42013-06-10 15:09:26 -0700134 // There is no notion of precise unresolved references, the precise information is just dropped
135 // on the floor.
136 DCHECK(entry->IsUnresolvedReference());
137 return true;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800138}
139
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700140mirror::Class* RegTypeCache::ResolveClass(const char* descriptor, mirror::ClassLoader* loader) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800141 // Class was not found, must create new type.
142 // Try resolving class
143 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -0800144 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700145 StackHandleScope<1> hs(self);
146 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(loader));
Ian Rogers7b078e82014-09-10 14:44:24 -0700147 mirror::Class* klass = nullptr;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800148 if (can_load_classes_) {
Ian Rogers98379392014-02-24 16:53:16 -0800149 klass = class_linker->FindClass(self, descriptor, class_loader);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800150 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800151 klass = class_linker->LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor),
152 loader);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700153 if (klass != nullptr && !klass->IsLoaded()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800154 // We found the class but without it being loaded its not safe for use.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700155 klass = nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800156 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800157 }
158 return klass;
159}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700160
Ian Rogersd8f69b02014-09-10 21:43:52 +0000161const RegType& RegTypeCache::From(mirror::ClassLoader* loader, const char* descriptor,
162 bool precise) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700163 // Try looking up the class in the cache first. We use a StringPiece to avoid continual strlen
164 // operations on the descriptor.
165 StringPiece descriptor_sp(descriptor);
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700166 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700167 if (MatchDescriptor(i, descriptor_sp, precise)) {
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700168 return *(entries_[i]);
169 }
170 }
171 // Class not found in the cache, will create a new type for that.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800172 // Try resolving class.
173 mirror::Class* klass = ResolveClass(descriptor, loader);
Ian Rogers7b078e82014-09-10 14:44:24 -0700174 if (klass != nullptr) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800175 // Class resolved, first look for the class in the list of entries
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800176 // Class was not found, must create new type.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700177 // To pass the verification, the type should be imprecise,
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700178 // instantiable or an interface with the precise type set to false.
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700179 DCHECK(!precise || klass->IsInstantiable());
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700180 // Create a precise type if:
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700181 // 1- Class is final and NOT an interface. a precise interface is meaningless !!
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700182 // 2- Precise Flag passed as true.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800183 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700184 // Create an imprecise type if we can't tell for a fact that it is precise.
Ian Rogers04f94f42013-06-10 15:09:26 -0700185 if (klass->CannotBeAssignedFromOtherTypes() || precise) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700186 DCHECK(!(klass->IsAbstract()) || klass->IsArrayClass());
187 DCHECK(!klass->IsInterface());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700188 entry = new PreciseReferenceType(klass, descriptor_sp.as_string(), entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800189 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700190 entry = new ReferenceType(klass, descriptor_sp.as_string(), entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800191 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700192 AddEntry(entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800193 return *entry;
194 } else { // Class not resolved.
195 // We tried loading the class and failed, this might get an exception raised
196 // so we want to clear it before we go on.
Andreas Gampe63981562014-04-17 12:28:43 -0700197 if (can_load_classes_) {
198 DCHECK(Thread::Current()->IsExceptionPending());
199 Thread::Current()->ClearException();
200 } else {
201 DCHECK(!Thread::Current()->IsExceptionPending());
202 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700203 if (IsValidDescriptor(descriptor)) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700204 RegType* entry = new UnresolvedReferenceType(descriptor_sp.as_string(), entries_.size());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700205 AddEntry(entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800206 return *entry;
207 } else {
208 // The descriptor is broken return the unknown type as there's nothing sensible that
209 // could be done at runtime
210 return Conflict();
211 }
212 }
213}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700214
Ian Rogersd8f69b02014-09-10 21:43:52 +0000215const RegType& RegTypeCache::FromClass(const char* descriptor, mirror::Class* klass, bool precise) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700216 DCHECK(klass != nullptr);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700217 if (klass->IsPrimitive()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700218 // Note: precise isn't used for primitive classes. A char is assignable to an int. All
219 // primitive classes are final.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800220 return RegTypeFromPrimitiveType(klass->GetPrimitiveType());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700221 } else {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800222 // Look for the reference in the list of entries to have.
223 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700224 const RegType* cur_entry = entries_[i];
Ian Rogers54410912014-09-10 15:33:05 -0700225 if (cur_entry->klass_.Read() == klass && MatchingPrecisionForClass(cur_entry, precise)) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700226 return *cur_entry;
227 }
228 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800229 // No reference to the class was found, create new reference.
230 RegType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800231 if (precise) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700232 entry = new PreciseReferenceType(klass, descriptor, entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800233 } else {
Ian Rogers637c65b2013-05-31 11:46:00 -0700234 entry = new ReferenceType(klass, descriptor, entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800235 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700236 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700237 return *entry;
238 }
239}
240
Mathieu Chartier12d625f2015-03-13 11:33:37 -0700241RegTypeCache::RegTypeCache(bool can_load_classes) : can_load_classes_(can_load_classes) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700242 if (kIsDebugBuild) {
243 Thread::Current()->AssertThreadSuspensionIsAllowable(gAborting == 0);
Andreas Gampe63981562014-04-17 12:28:43 -0700244 }
245 entries_.reserve(64);
246 FillPrimitiveAndSmallConstantTypes();
247}
248
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800249RegTypeCache::~RegTypeCache() {
250 CHECK_LE(primitive_count_, entries_.size());
251 // Delete only the non primitive types.
Ian Rogers41c65c12013-09-05 16:57:17 -0700252 if (entries_.size() == kNumPrimitivesAndSmallConstants) {
253 // All entries are from the global pool, nothing to delete.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800254 return;
255 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700256 std::vector<const RegType*>::iterator non_primitive_begin = entries_.begin();
Ian Rogers41c65c12013-09-05 16:57:17 -0700257 std::advance(non_primitive_begin, kNumPrimitivesAndSmallConstants);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800258 STLDeleteContainerPointers(non_primitive_begin, entries_.end());
259}
260
261void RegTypeCache::ShutDown() {
262 if (RegTypeCache::primitive_initialized_) {
263 UndefinedType::Destroy();
264 ConflictType::Destroy();
265 BooleanType::Destroy();
266 ByteType::Destroy();
267 ShortType::Destroy();
268 CharType::Destroy();
269 IntegerType::Destroy();
270 LongLoType::Destroy();
271 LongHiType::Destroy();
Ian Rogers25ae7eb2013-04-18 16:06:05 -0700272 FloatType::Destroy();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800273 DoubleLoType::Destroy();
274 DoubleHiType::Destroy();
Ian Rogersdfe78a62013-11-14 09:20:55 -0800275 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700276 const PreciseConstType* type = small_precise_constants_[value - kMinSmallConstant];
Ian Rogers41c65c12013-09-05 16:57:17 -0700277 delete type;
Ian Rogersdfe78a62013-11-14 09:20:55 -0800278 small_precise_constants_[value - kMinSmallConstant] = nullptr;
Ian Rogers41c65c12013-09-05 16:57:17 -0700279 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800280 RegTypeCache::primitive_initialized_ = false;
281 RegTypeCache::primitive_count_ = 0;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800282 }
283}
284
Ian Rogers41c65c12013-09-05 16:57:17 -0700285template <class Type>
Ian Rogers7b078e82014-09-10 14:44:24 -0700286const Type* RegTypeCache::CreatePrimitiveTypeInstance(const std::string& descriptor) {
287 mirror::Class* klass = nullptr;
Ian Rogers41c65c12013-09-05 16:57:17 -0700288 // Try loading the class from linker.
289 if (!descriptor.empty()) {
Ian Rogers98379392014-02-24 16:53:16 -0800290 klass = art::Runtime::Current()->GetClassLinker()->FindSystemClass(Thread::Current(),
291 descriptor.c_str());
Logan Chien10f0ca22014-09-23 23:01:47 +0800292 DCHECK(klass != nullptr);
Ian Rogers41c65c12013-09-05 16:57:17 -0700293 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700294 const Type* entry = Type::CreateInstance(klass, descriptor, RegTypeCache::primitive_count_);
Ian Rogers41c65c12013-09-05 16:57:17 -0700295 RegTypeCache::primitive_count_++;
296 return entry;
297}
298
299void RegTypeCache::CreatePrimitiveAndSmallConstantTypes() {
Ian Rogers33e95662013-05-20 20:29:14 -0700300 CreatePrimitiveTypeInstance<UndefinedType>("");
301 CreatePrimitiveTypeInstance<ConflictType>("");
302 CreatePrimitiveTypeInstance<BooleanType>("Z");
303 CreatePrimitiveTypeInstance<ByteType>("B");
304 CreatePrimitiveTypeInstance<ShortType>("S");
305 CreatePrimitiveTypeInstance<CharType>("C");
306 CreatePrimitiveTypeInstance<IntegerType>("I");
307 CreatePrimitiveTypeInstance<LongLoType>("J");
308 CreatePrimitiveTypeInstance<LongHiType>("J");
309 CreatePrimitiveTypeInstance<FloatType>("F");
310 CreatePrimitiveTypeInstance<DoubleLoType>("D");
311 CreatePrimitiveTypeInstance<DoubleHiType>("D");
Ian Rogers41c65c12013-09-05 16:57:17 -0700312 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
313 PreciseConstType* type = new PreciseConstType(value, primitive_count_);
314 small_precise_constants_[value - kMinSmallConstant] = type;
315 primitive_count_++;
316 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800317}
318
Ian Rogersd8f69b02014-09-10 21:43:52 +0000319const RegType& RegTypeCache::FromUnresolvedMerge(const RegType& left, const RegType& right) {
Ian Rogers529781d2012-07-23 17:24:29 -0700320 std::set<uint16_t> types;
321 if (left.IsUnresolvedMergedReference()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +0000322 RegType& non_const(const_cast<RegType&>(left));
323 types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700324 } else {
325 types.insert(left.GetId());
326 }
327 if (right.IsUnresolvedMergedReference()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +0000328 RegType& non_const(const_cast<RegType&>(right));
329 std::set<uint16_t> right_types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700330 types.insert(right_types.begin(), right_types.end());
331 } else {
332 types.insert(right.GetId());
333 }
334 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800335 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700336 const RegType* cur_entry = entries_[i];
Ian Rogers529781d2012-07-23 17:24:29 -0700337 if (cur_entry->IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800338 std::set<uint16_t> cur_entry_types =
Ian Rogers7b078e82014-09-10 14:44:24 -0700339 (down_cast<const UnresolvedMergedType*>(cur_entry))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700340 if (cur_entry_types == types) {
341 return *cur_entry;
342 }
343 }
344 }
345 // Create entry.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800346 RegType* entry = new UnresolvedMergedType(left.GetId(), right.GetId(), this, entries_.size());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700347 AddEntry(entry);
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700348 if (kIsDebugBuild) {
349 UnresolvedMergedType* tmp_entry = down_cast<UnresolvedMergedType*>(entry);
350 std::set<uint16_t> check_types = tmp_entry->GetMergedTypes();
351 CHECK(check_types == types);
352 }
Ian Rogers529781d2012-07-23 17:24:29 -0700353 return *entry;
354}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700355
Ian Rogersd8f69b02014-09-10 21:43:52 +0000356const RegType& RegTypeCache::FromUnresolvedSuperClass(const RegType& child) {
Ian Rogers529781d2012-07-23 17:24:29 -0700357 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800358 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700359 const RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800360 if (cur_entry->IsUnresolvedSuperClass()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700361 const UnresolvedSuperClass* tmp_entry =
362 down_cast<const UnresolvedSuperClass*>(cur_entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800363 uint16_t unresolved_super_child_id =
364 tmp_entry->GetUnresolvedSuperClassChildId();
365 if (unresolved_super_child_id == child.GetId()) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700366 return *cur_entry;
367 }
368 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800369 }
370 RegType* entry = new UnresolvedSuperClass(child.GetId(), this, entries_.size());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700371 AddEntry(entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800372 return *entry;
373}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700374
Ian Rogersd8f69b02014-09-10 21:43:52 +0000375const UninitializedType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700376 UninitializedType* entry = nullptr;
Ian Rogers637c65b2013-05-31 11:46:00 -0700377 const std::string& descriptor(type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800378 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800379 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700380 const RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800381 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
Ian Rogers7b078e82014-09-10 14:44:24 -0700382 down_cast<const UnresolvedUninitializedRefType*>(cur_entry)->GetAllocationPc()
383 == allocation_pc &&
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800384 (cur_entry->GetDescriptor() == descriptor)) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700385 return *down_cast<const UnresolvedUninitializedRefType*>(cur_entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800386 }
387 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700388 entry = new UnresolvedUninitializedRefType(descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700389 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800390 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800391 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700392 const RegType* cur_entry = entries_[i];
Ian Rogers776ac1f2012-04-13 23:36:36 -0700393 if (cur_entry->IsUninitializedReference() &&
Ian Rogers7b078e82014-09-10 14:44:24 -0700394 down_cast<const UninitializedReferenceType*>(cur_entry)
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800395 ->GetAllocationPc() == allocation_pc &&
Ian Rogers776ac1f2012-04-13 23:36:36 -0700396 cur_entry->GetClass() == klass) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700397 return *down_cast<const UninitializedReferenceType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700398 }
399 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700400 entry = new UninitializedReferenceType(klass, descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700401 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700402 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700403 return *entry;
404}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700405
Ian Rogersd8f69b02014-09-10 21:43:52 +0000406const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700407 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700408
Ian Rogers776ac1f2012-04-13 23:36:36 -0700409 if (uninit_type.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700410 const std::string& descriptor(uninit_type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800411 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700412 const RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800413 if (cur_entry->IsUnresolvedReference() &&
414 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700415 return *cur_entry;
416 }
417 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700418 entry = new UnresolvedReferenceType(descriptor, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700419 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800420 mirror::Class* klass = uninit_type.GetClass();
Brian Carlstromdf629502013-07-17 22:39:56 -0700421 if (uninit_type.IsUninitializedThisReference() && !klass->IsFinal()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700422 // For uninitialized "this reference" look for reference types that are not precise.
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700423 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700424 const RegType* cur_entry = entries_[i];
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700425 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
426 return *cur_entry;
427 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700428 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700429 entry = new ReferenceType(klass, "", entries_.size());
Ian Rogers62342ec2013-06-11 10:26:37 -0700430 } else if (klass->IsInstantiable()) {
431 // We're uninitialized because of allocation, look or create a precise type as allocations
432 // may only create objects of that type.
433 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700434 const RegType* cur_entry = entries_[i];
Ian Rogers62342ec2013-06-11 10:26:37 -0700435 if (cur_entry->IsPreciseReference() && cur_entry->GetClass() == klass) {
436 return *cur_entry;
Ian Rogers04f94f42013-06-10 15:09:26 -0700437 }
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700438 }
Ian Rogers62342ec2013-06-11 10:26:37 -0700439 entry = new PreciseReferenceType(klass, uninit_type.GetDescriptor(), entries_.size());
440 } else {
441 return Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700442 }
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700443 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700444 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700445 return *entry;
446}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700447
Ian Rogersd8f69b02014-09-10 21:43:52 +0000448const UninitializedType& RegTypeCache::UninitializedThisArgument(const RegType& type) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700449 UninitializedType* entry;
Ian Rogers637c65b2013-05-31 11:46:00 -0700450 const std::string& descriptor(type.GetDescriptor());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700451 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800452 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700453 const RegType* cur_entry = entries_[i];
Ian Rogersad0b3a32012-04-16 14:50:24 -0700454 if (cur_entry->IsUnresolvedAndUninitializedThisReference() &&
455 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700456 return *down_cast<const UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700457 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700458 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700459 entry = new UnresolvedUninitializedThisRefType(descriptor, entries_.size());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700460 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800461 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800462 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700463 const RegType* cur_entry = entries_[i];
Ian Rogers62342ec2013-06-11 10:26:37 -0700464 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700465 return *down_cast<const UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700466 }
467 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700468 entry = new UninitializedThisReferenceType(klass, descriptor, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700469 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700470 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700471 return *entry;
472}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700473
Ian Rogersd8f69b02014-09-10 21:43:52 +0000474const ConstantType& RegTypeCache::FromCat1NonSmallConstant(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800475 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700476 const RegType* cur_entry = entries_[i];
Ian Rogers54410912014-09-10 15:33:05 -0700477 if (cur_entry->klass_.IsNull() && cur_entry->IsConstant() &&
Ian Rogers637c65b2013-05-31 11:46:00 -0700478 cur_entry->IsPreciseConstant() == precise &&
Ian Rogers7b078e82014-09-10 14:44:24 -0700479 (down_cast<const ConstantType*>(cur_entry))->ConstantValue() == value) {
480 return *down_cast<const ConstantType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700481 }
482 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700483 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800484 if (precise) {
485 entry = new PreciseConstType(value, entries_.size());
486 } else {
487 entry = new ImpreciseConstType(value, entries_.size());
488 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700489 AddEntry(entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800490 return *entry;
491}
492
Ian Rogersd8f69b02014-09-10 21:43:52 +0000493const ConstantType& RegTypeCache::FromCat2ConstLo(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800494 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700495 const RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800496 if (cur_entry->IsConstantLo() && (cur_entry->IsPrecise() == precise) &&
Ian Rogers7b078e82014-09-10 14:44:24 -0700497 (down_cast<const ConstantType*>(cur_entry))->ConstantValueLo() == value) {
498 return *down_cast<const ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800499 }
500 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700501 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800502 if (precise) {
503 entry = new PreciseConstLoType(value, entries_.size());
504 } else {
505 entry = new ImpreciseConstLoType(value, entries_.size());
506 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700507 AddEntry(entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800508 return *entry;
509}
510
Ian Rogersd8f69b02014-09-10 21:43:52 +0000511const ConstantType& RegTypeCache::FromCat2ConstHi(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800512 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700513 const RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800514 if (cur_entry->IsConstantHi() && (cur_entry->IsPrecise() == precise) &&
Ian Rogers7b078e82014-09-10 14:44:24 -0700515 (down_cast<const ConstantType*>(cur_entry))->ConstantValueHi() == value) {
516 return *down_cast<const ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800517 }
518 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700519 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800520 if (precise) {
521 entry = new PreciseConstHiType(value, entries_.size());
522 } else {
523 entry = new ImpreciseConstHiType(value, entries_.size());
524 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700525 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700526 return *entry;
527}
528
Ian Rogersd8f69b02014-09-10 21:43:52 +0000529const RegType& RegTypeCache::GetComponentType(const RegType& array, mirror::ClassLoader* loader) {
Ian Rogersa9a82542013-10-04 11:17:26 -0700530 if (!array.IsArrayTypes()) {
531 return Conflict();
532 } else if (array.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700533 const std::string& descriptor(array.GetDescriptor());
534 const std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogersb4903572012-10-11 11:52:56 -0700535 return FromDescriptor(loader, component.c_str(), false);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700536 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800537 mirror::Class* klass = array.GetClass()->GetComponentType();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700538 std::string temp;
Andreas Gampeaa910d52014-07-30 18:59:05 -0700539 if (klass->IsErroneous()) {
540 // Arrays may have erroneous component types, use unresolved in that case.
541 // We assume that the primitive classes are not erroneous, so we know it is a
542 // reference type.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700543 return FromDescriptor(loader, klass->GetDescriptor(&temp), false);
Andreas Gampeaa910d52014-07-30 18:59:05 -0700544 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700545 return FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampeaa910d52014-07-30 18:59:05 -0700546 klass->CannotBeAssignedFromOtherTypes());
547 }
Ian Rogersb4903572012-10-11 11:52:56 -0700548 }
549}
550
551void RegTypeCache::Dump(std::ostream& os) {
Ian Rogersb4903572012-10-11 11:52:56 -0700552 for (size_t i = 0; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700553 const RegType* cur_entry = entries_[i];
554 if (cur_entry != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800555 os << i << ": " << cur_entry->Dump() << "\n";
Ian Rogersb4903572012-10-11 11:52:56 -0700556 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700557 }
558}
559
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700560void RegTypeCache::VisitStaticRoots(RootVisitor* visitor) {
Mathieu Chartier7c438b12014-09-12 17:01:24 -0700561 // Visit the primitive types, this is required since if there are no active verifiers they wont
562 // be in the entries array, and therefore not visited as roots.
563 if (primitive_initialized_) {
Mathieu Chartier12d625f2015-03-13 11:33:37 -0700564 RootInfo ri(kRootUnknown);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700565 UndefinedType::GetInstance()->VisitRoots(visitor, ri);
566 ConflictType::GetInstance()->VisitRoots(visitor, ri);
567 BooleanType::GetInstance()->VisitRoots(visitor, ri);
568 ByteType::GetInstance()->VisitRoots(visitor, ri);
569 ShortType::GetInstance()->VisitRoots(visitor, ri);
570 CharType::GetInstance()->VisitRoots(visitor, ri);
571 IntegerType::GetInstance()->VisitRoots(visitor, ri);
572 LongLoType::GetInstance()->VisitRoots(visitor, ri);
573 LongHiType::GetInstance()->VisitRoots(visitor, ri);
574 FloatType::GetInstance()->VisitRoots(visitor, ri);
575 DoubleLoType::GetInstance()->VisitRoots(visitor, ri);
576 DoubleHiType::GetInstance()->VisitRoots(visitor, ri);
Mathieu Chartier7c438b12014-09-12 17:01:24 -0700577 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700578 small_precise_constants_[value - kMinSmallConstant]->VisitRoots(visitor, ri);
Mathieu Chartier7c438b12014-09-12 17:01:24 -0700579 }
580 }
581}
582
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700583void RegTypeCache::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
Hiroshi Yamauchi3e781622015-02-25 12:52:34 -0800584 // Exclude the static roots that are visited by VisitStaticRoots().
585 for (size_t i = primitive_count_; i < entries_.size(); ++i) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700586 entries_[i]->VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800587 }
588}
589
Andreas Gampeaa910d52014-07-30 18:59:05 -0700590void RegTypeCache::AddEntry(RegType* new_entry) {
591 entries_.push_back(new_entry);
592}
593
Elliott Hughesa21039c2012-06-21 12:09:25 -0700594} // namespace verifier
595} // namespace art