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 #include "reg_type_cache-inl.h"
18
19 #include <type_traits>
20
21 #include "base/aborting.h"
22 #include "base/arena_bit_vector.h"
23 #include "base/bit_vector-inl.h"
24 #include "base/casts.h"
25 #include "base/scoped_arena_allocator.h"
26 #include "base/stl_util.h"
27 #include "class_linker-inl.h"
28 #include "dex/descriptors_names.h"
29 #include "dex/dex_file-inl.h"
30 #include "mirror/class-inl.h"
31 #include "mirror/object-inl.h"
32 #include "reg_type-inl.h"
33
34 namespace art {
35 namespace verifier {
36
37 bool RegTypeCache::primitive_initialized_ = false;
38 uint16_t RegTypeCache::primitive_count_ = 0;
39 const PreciseConstType* RegTypeCache::small_precise_constants_[kMaxSmallConstant -
40 kMinSmallConstant + 1];
41
42 namespace {
43
44 ClassLinker* gInitClassLinker = nullptr;
45
46 } // namespace
47
MatchingPrecisionForClass(const RegType * entry,bool precise)48 ALWAYS_INLINE static inline bool MatchingPrecisionForClass(const RegType* entry, bool precise)
49 REQUIRES_SHARED(Locks::mutator_lock_) {
50 if (entry->IsPreciseReference() == precise) {
51 // We were or weren't looking for a precise reference and we found what we need.
52 return true;
53 } else {
54 if (!precise && entry->GetClass()->CannotBeAssignedFromOtherTypes()) {
55 // We weren't looking for a precise reference, as we're looking up based on a descriptor, but
56 // we found a matching entry based on the descriptor. Return the precise entry in that case.
57 return true;
58 }
59 return false;
60 }
61 }
62
FillPrimitiveAndSmallConstantTypes()63 void RegTypeCache::FillPrimitiveAndSmallConstantTypes() {
64 // Note: this must have the same order as CreatePrimitiveAndSmallConstantTypes.
65 entries_.push_back(UndefinedType::GetInstance());
66 entries_.push_back(ConflictType::GetInstance());
67 entries_.push_back(NullType::GetInstance());
68 entries_.push_back(BooleanType::GetInstance());
69 entries_.push_back(ByteType::GetInstance());
70 entries_.push_back(ShortType::GetInstance());
71 entries_.push_back(CharType::GetInstance());
72 entries_.push_back(IntegerType::GetInstance());
73 entries_.push_back(LongLoType::GetInstance());
74 entries_.push_back(LongHiType::GetInstance());
75 entries_.push_back(FloatType::GetInstance());
76 entries_.push_back(DoubleLoType::GetInstance());
77 entries_.push_back(DoubleHiType::GetInstance());
78 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
79 int32_t i = value - kMinSmallConstant;
80 DCHECK_EQ(entries_.size(), small_precise_constants_[i]->GetId());
81 entries_.push_back(small_precise_constants_[i]);
82 }
83 DCHECK_EQ(entries_.size(), primitive_count_);
84 }
85
FromDescriptor(ObjPtr<mirror::ClassLoader> loader,const char * descriptor,bool precise)86 const RegType& RegTypeCache::FromDescriptor(ObjPtr<mirror::ClassLoader> loader,
87 const char* descriptor,
88 bool precise) {
89 DCHECK(RegTypeCache::primitive_initialized_);
90 if (descriptor[1] == '\0') {
91 switch (descriptor[0]) {
92 case 'Z':
93 return Boolean();
94 case 'B':
95 return Byte();
96 case 'S':
97 return Short();
98 case 'C':
99 return Char();
100 case 'I':
101 return Integer();
102 case 'J':
103 return LongLo();
104 case 'F':
105 return Float();
106 case 'D':
107 return DoubleLo();
108 case 'V': // For void types, conflict types.
109 default:
110 return Conflict();
111 }
112 } else if (descriptor[0] == 'L' || descriptor[0] == '[') {
113 return From(loader, descriptor, precise);
114 } else {
115 return Conflict();
116 }
117 }
118
RegTypeFromPrimitiveType(Primitive::Type prim_type) const119 const RegType& RegTypeCache::RegTypeFromPrimitiveType(Primitive::Type prim_type) const {
120 DCHECK(RegTypeCache::primitive_initialized_);
121 switch (prim_type) {
122 case Primitive::kPrimBoolean:
123 return *BooleanType::GetInstance();
124 case Primitive::kPrimByte:
125 return *ByteType::GetInstance();
126 case Primitive::kPrimShort:
127 return *ShortType::GetInstance();
128 case Primitive::kPrimChar:
129 return *CharType::GetInstance();
130 case Primitive::kPrimInt:
131 return *IntegerType::GetInstance();
132 case Primitive::kPrimLong:
133 return *LongLoType::GetInstance();
134 case Primitive::kPrimFloat:
135 return *FloatType::GetInstance();
136 case Primitive::kPrimDouble:
137 return *DoubleLoType::GetInstance();
138 case Primitive::kPrimVoid:
139 default:
140 return *ConflictType::GetInstance();
141 }
142 }
143
MatchDescriptor(size_t idx,const std::string_view & descriptor,bool precise)144 bool RegTypeCache::MatchDescriptor(size_t idx, const std::string_view& descriptor, bool precise) {
145 const RegType* entry = entries_[idx];
146 if (descriptor != entry->descriptor_) {
147 return false;
148 }
149 if (entry->HasClass()) {
150 return MatchingPrecisionForClass(entry, precise);
151 }
152 // There is no notion of precise unresolved references, the precise information is just dropped
153 // on the floor.
154 DCHECK(entry->IsUnresolvedReference());
155 return true;
156 }
157
ResolveClass(const char * descriptor,ObjPtr<mirror::ClassLoader> loader)158 ObjPtr<mirror::Class> RegTypeCache::ResolveClass(const char* descriptor,
159 ObjPtr<mirror::ClassLoader> loader) {
160 // Class was not found, must create new type.
161 // Try resolving class
162 Thread* self = Thread::Current();
163 StackHandleScope<1> hs(self);
164 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(loader));
165 ObjPtr<mirror::Class> klass = nullptr;
166 if (can_load_classes_) {
167 klass = class_linker_->FindClass(self, descriptor, class_loader);
168 } else {
169 klass = class_linker_->LookupClass(self, descriptor, loader);
170 if (klass != nullptr && !klass->IsResolved()) {
171 // We found the class but without it being loaded its not safe for use.
172 klass = nullptr;
173 }
174 }
175 return klass;
176 }
177
AddString(const std::string_view & str)178 std::string_view RegTypeCache::AddString(const std::string_view& str) {
179 char* ptr = allocator_.AllocArray<char>(str.length());
180 memcpy(ptr, str.data(), str.length());
181 return std::string_view(ptr, str.length());
182 }
183
From(ObjPtr<mirror::ClassLoader> loader,const char * descriptor,bool precise)184 const RegType& RegTypeCache::From(ObjPtr<mirror::ClassLoader> loader,
185 const char* descriptor,
186 bool precise) {
187 std::string_view sv_descriptor(descriptor);
188 // Try looking up the class in the cache first. We use a std::string_view to avoid
189 // repeated strlen operations on the descriptor.
190 for (size_t i = primitive_count_; i < entries_.size(); i++) {
191 if (MatchDescriptor(i, sv_descriptor, precise)) {
192 return *(entries_[i]);
193 }
194 }
195 // Class not found in the cache, will create a new type for that.
196 // Try resolving class.
197 ObjPtr<mirror::Class> klass = ResolveClass(descriptor, loader);
198 if (klass != nullptr) {
199 // Class resolved, first look for the class in the list of entries
200 // Class was not found, must create new type.
201 // To pass the verification, the type should be imprecise,
202 // instantiable or an interface with the precise type set to false.
203 DCHECK(!precise || klass->IsInstantiable());
204 // Create a precise type if:
205 // 1- Class is final and NOT an interface. a precise interface is meaningless !!
206 // 2- Precise Flag passed as true.
207 RegType* entry;
208 // Create an imprecise type if we can't tell for a fact that it is precise.
209 if (klass->CannotBeAssignedFromOtherTypes() || precise) {
210 DCHECK(!(klass->IsAbstract()) || klass->IsArrayClass());
211 DCHECK(!klass->IsInterface());
212 entry =
213 new (&allocator_) PreciseReferenceType(klass, AddString(sv_descriptor), entries_.size());
214 } else {
215 entry = new (&allocator_) ReferenceType(klass, AddString(sv_descriptor), entries_.size());
216 }
217 return AddEntry(entry);
218 } else { // Class not resolved.
219 // We tried loading the class and failed, this might get an exception raised
220 // so we want to clear it before we go on.
221 if (can_load_classes_) {
222 DCHECK(Thread::Current()->IsExceptionPending());
223 Thread::Current()->ClearException();
224 } else {
225 DCHECK(!Thread::Current()->IsExceptionPending());
226 }
227 if (IsValidDescriptor(descriptor)) {
228 return AddEntry(
229 new (&allocator_) UnresolvedReferenceType(AddString(sv_descriptor), entries_.size()));
230 } else {
231 // The descriptor is broken return the unknown type as there's nothing sensible that
232 // could be done at runtime
233 return Conflict();
234 }
235 }
236 }
237
MakeUnresolvedReference()238 const RegType& RegTypeCache::MakeUnresolvedReference() {
239 // The descriptor is intentionally invalid so nothing else will match this type.
240 return AddEntry(new (&allocator_) UnresolvedReferenceType(AddString("a"), entries_.size()));
241 }
242
FindClass(ObjPtr<mirror::Class> klass,bool precise) const243 const RegType* RegTypeCache::FindClass(ObjPtr<mirror::Class> klass, bool precise) const {
244 DCHECK(klass != nullptr);
245 if (klass->IsPrimitive()) {
246 // Note: precise isn't used for primitive classes. A char is assignable to an int. All
247 // primitive classes are final.
248 return &RegTypeFromPrimitiveType(klass->GetPrimitiveType());
249 }
250 for (auto& pair : klass_entries_) {
251 const ObjPtr<mirror::Class> reg_klass = pair.first.Read();
252 if (reg_klass == klass) {
253 const RegType* reg_type = pair.second;
254 if (MatchingPrecisionForClass(reg_type, precise)) {
255 return reg_type;
256 }
257 }
258 }
259 return nullptr;
260 }
261
InsertClass(const std::string_view & descriptor,ObjPtr<mirror::Class> klass,bool precise)262 const RegType* RegTypeCache::InsertClass(const std::string_view& descriptor,
263 ObjPtr<mirror::Class> klass,
264 bool precise) {
265 // No reference to the class was found, create new reference.
266 DCHECK(FindClass(klass, precise) == nullptr);
267 RegType* const reg_type = precise
268 ? static_cast<RegType*>(
269 new (&allocator_) PreciseReferenceType(klass, descriptor, entries_.size()))
270 : new (&allocator_) ReferenceType(klass, descriptor, entries_.size());
271 return &AddEntry(reg_type);
272 }
273
FromClass(const char * descriptor,ObjPtr<mirror::Class> klass,bool precise)274 const RegType& RegTypeCache::FromClass(const char* descriptor,
275 ObjPtr<mirror::Class> klass,
276 bool precise) {
277 DCHECK(klass != nullptr);
278 const RegType* reg_type = FindClass(klass, precise);
279 if (reg_type == nullptr) {
280 reg_type = InsertClass(AddString(std::string_view(descriptor)), klass, precise);
281 }
282 return *reg_type;
283 }
284
RegTypeCache(ClassLinker * class_linker,bool can_load_classes,ScopedArenaAllocator & allocator,bool can_suspend)285 RegTypeCache::RegTypeCache(ClassLinker* class_linker,
286 bool can_load_classes,
287 ScopedArenaAllocator& allocator,
288 bool can_suspend)
289 : entries_(allocator.Adapter(kArenaAllocVerifier)),
290 klass_entries_(allocator.Adapter(kArenaAllocVerifier)),
291 allocator_(allocator),
292 class_linker_(class_linker),
293 can_load_classes_(can_load_classes) {
294 DCHECK_EQ(class_linker, gInitClassLinker);
295 DCHECK(can_suspend || !can_load_classes) << "Cannot load classes if suspension is disabled!";
296 if (kIsDebugBuild && can_suspend) {
297 Thread::Current()->AssertThreadSuspensionIsAllowable(gAborting == 0);
298 }
299 // The klass_entries_ array does not have primitives or small constants.
300 static constexpr size_t kNumReserveEntries = 32;
301 klass_entries_.reserve(kNumReserveEntries);
302 // We want to have room for additional entries after inserting primitives and small
303 // constants.
304 entries_.reserve(kNumReserveEntries + kNumPrimitivesAndSmallConstants);
305 FillPrimitiveAndSmallConstantTypes();
306 }
307
~RegTypeCache()308 RegTypeCache::~RegTypeCache() {
309 DCHECK_LE(primitive_count_, entries_.size());
310 }
311
ShutDown()312 void RegTypeCache::ShutDown() {
313 if (RegTypeCache::primitive_initialized_) {
314 UndefinedType::Destroy();
315 ConflictType::Destroy();
316 BooleanType::Destroy();
317 ByteType::Destroy();
318 ShortType::Destroy();
319 CharType::Destroy();
320 IntegerType::Destroy();
321 LongLoType::Destroy();
322 LongHiType::Destroy();
323 FloatType::Destroy();
324 DoubleLoType::Destroy();
325 DoubleHiType::Destroy();
326 NullType::Destroy();
327 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
328 const PreciseConstType* type = small_precise_constants_[value - kMinSmallConstant];
329 delete type;
330 small_precise_constants_[value - kMinSmallConstant] = nullptr;
331 }
332 RegTypeCache::primitive_initialized_ = false;
333 RegTypeCache::primitive_count_ = 0;
334 }
335 }
336
337 // Helper for create_primitive_type_instance lambda.
338 namespace {
339 template <typename T>
340 struct TypeHelper {
341 using type = T;
342 static_assert(std::is_convertible<T*, RegType*>::value, "T must be a RegType");
343
344 const char* descriptor;
345
TypeHelperart::verifier::__anon9fe3c3390211::TypeHelper346 explicit TypeHelper(const char* d) : descriptor(d) {}
347 };
348 } // namespace
349
CreatePrimitiveAndSmallConstantTypes(ClassLinker * class_linker)350 void RegTypeCache::CreatePrimitiveAndSmallConstantTypes(ClassLinker* class_linker) {
351 gInitClassLinker = class_linker;
352
353 // Note: this must have the same order as FillPrimitiveAndSmallConstantTypes.
354
355 // It is acceptable to pass on the const char* in type to CreateInstance, as all calls below are
356 // with compile-time constants that will have global lifetime. Use of the lambda ensures this
357 // code cannot leak to other users.
358 auto create_primitive_type_instance = [&](auto type) REQUIRES_SHARED(Locks::mutator_lock_) {
359 using Type = typename decltype(type)::type;
360 ObjPtr<mirror::Class> klass = nullptr;
361 // Try loading the class from linker.
362 DCHECK(type.descriptor != nullptr);
363 if (strlen(type.descriptor) > 0) {
364 klass = class_linker->FindSystemClass(Thread::Current(), type.descriptor);
365 DCHECK(klass != nullptr);
366 }
367 const Type* entry = Type::CreateInstance(klass,
368 type.descriptor,
369 RegTypeCache::primitive_count_);
370 RegTypeCache::primitive_count_++;
371 return entry;
372 };
373 create_primitive_type_instance(TypeHelper<UndefinedType>(""));
374 create_primitive_type_instance(TypeHelper<ConflictType>(""));
375 create_primitive_type_instance(TypeHelper<NullType>(""));
376 create_primitive_type_instance(TypeHelper<BooleanType>("Z"));
377 create_primitive_type_instance(TypeHelper<ByteType>("B"));
378 create_primitive_type_instance(TypeHelper<ShortType>("S"));
379 create_primitive_type_instance(TypeHelper<CharType>("C"));
380 create_primitive_type_instance(TypeHelper<IntegerType>("I"));
381 create_primitive_type_instance(TypeHelper<LongLoType>("J"));
382 create_primitive_type_instance(TypeHelper<LongHiType>("J"));
383 create_primitive_type_instance(TypeHelper<FloatType>("F"));
384 create_primitive_type_instance(TypeHelper<DoubleLoType>("D"));
385 create_primitive_type_instance(TypeHelper<DoubleHiType>("D"));
386
387 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
388 PreciseConstType* type = new PreciseConstType(value, primitive_count_);
389 small_precise_constants_[value - kMinSmallConstant] = type;
390 primitive_count_++;
391 }
392 }
393
FromUnresolvedMerge(const RegType & left,const RegType & right,MethodVerifier * verifier)394 const RegType& RegTypeCache::FromUnresolvedMerge(const RegType& left,
395 const RegType& right,
396 MethodVerifier* verifier) {
397 ArenaBitVector types(&allocator_,
398 kDefaultArenaBitVectorBytes * kBitsPerByte, // Allocate at least 8 bytes.
399 true); // Is expandable.
400 const RegType* left_resolved;
401 bool left_unresolved_is_array;
402 if (left.IsUnresolvedMergedReference()) {
403 const UnresolvedMergedType& left_merge = *down_cast<const UnresolvedMergedType*>(&left);
404
405 types.Copy(&left_merge.GetUnresolvedTypes());
406 left_resolved = &left_merge.GetResolvedPart();
407 left_unresolved_is_array = left.IsArrayTypes();
408 } else if (left.IsUnresolvedTypes()) {
409 types.ClearAllBits();
410 types.SetBit(left.GetId());
411 left_resolved = &Zero();
412 left_unresolved_is_array = left.IsArrayTypes();
413 } else {
414 types.ClearAllBits();
415 left_resolved = &left;
416 left_unresolved_is_array = false;
417 }
418
419 const RegType* right_resolved;
420 bool right_unresolved_is_array;
421 if (right.IsUnresolvedMergedReference()) {
422 const UnresolvedMergedType& right_merge = *down_cast<const UnresolvedMergedType*>(&right);
423
424 types.Union(&right_merge.GetUnresolvedTypes());
425 right_resolved = &right_merge.GetResolvedPart();
426 right_unresolved_is_array = right.IsArrayTypes();
427 } else if (right.IsUnresolvedTypes()) {
428 types.SetBit(right.GetId());
429 right_resolved = &Zero();
430 right_unresolved_is_array = right.IsArrayTypes();
431 } else {
432 right_resolved = &right;
433 right_unresolved_is_array = false;
434 }
435
436 // Merge the resolved parts. Left and right might be equal, so use SafeMerge.
437 const RegType& resolved_parts_merged = left_resolved->SafeMerge(*right_resolved, this, verifier);
438 // If we get a conflict here, the merge result is a conflict, not an unresolved merge type.
439 if (resolved_parts_merged.IsConflict()) {
440 return Conflict();
441 }
442 if (resolved_parts_merged.IsJavaLangObject()) {
443 return resolved_parts_merged;
444 }
445
446 bool resolved_merged_is_array = resolved_parts_merged.IsArrayTypes();
447 if (left_unresolved_is_array || right_unresolved_is_array || resolved_merged_is_array) {
448 // Arrays involved, see if we need to merge to Object.
449
450 // Is the resolved part a primitive array?
451 if (resolved_merged_is_array && !resolved_parts_merged.IsObjectArrayTypes()) {
452 return JavaLangObject(/* precise= */ false);
453 }
454
455 // Is any part not an array (but exists)?
456 if ((!left_unresolved_is_array && left_resolved != &left) ||
457 (!right_unresolved_is_array && right_resolved != &right) ||
458 !resolved_merged_is_array) {
459 return JavaLangObject(/* precise= */ false);
460 }
461 }
462
463 // Check if entry already exists.
464 for (size_t i = primitive_count_; i < entries_.size(); i++) {
465 const RegType* cur_entry = entries_[i];
466 if (cur_entry->IsUnresolvedMergedReference()) {
467 const UnresolvedMergedType* cmp_type = down_cast<const UnresolvedMergedType*>(cur_entry);
468 const RegType& resolved_part = cmp_type->GetResolvedPart();
469 const BitVector& unresolved_part = cmp_type->GetUnresolvedTypes();
470 // Use SameBitsSet. "types" is expandable to allow merging in the components, but the
471 // BitVector in the final RegType will be made non-expandable.
472 if (&resolved_part == &resolved_parts_merged && types.SameBitsSet(&unresolved_part)) {
473 return *cur_entry;
474 }
475 }
476 }
477 return AddEntry(new (&allocator_) UnresolvedMergedType(resolved_parts_merged,
478 types,
479 this,
480 entries_.size()));
481 }
482
FromUnresolvedSuperClass(const RegType & child)483 const RegType& RegTypeCache::FromUnresolvedSuperClass(const RegType& child) {
484 // Check if entry already exists.
485 for (size_t i = primitive_count_; i < entries_.size(); i++) {
486 const RegType* cur_entry = entries_[i];
487 if (cur_entry->IsUnresolvedSuperClass()) {
488 const UnresolvedSuperClass* tmp_entry =
489 down_cast<const UnresolvedSuperClass*>(cur_entry);
490 uint16_t unresolved_super_child_id =
491 tmp_entry->GetUnresolvedSuperClassChildId();
492 if (unresolved_super_child_id == child.GetId()) {
493 return *cur_entry;
494 }
495 }
496 }
497 return AddEntry(new (&allocator_) UnresolvedSuperClass(child.GetId(), this, entries_.size()));
498 }
499
Uninitialized(const RegType & type,uint32_t allocation_pc)500 const UninitializedType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
501 UninitializedType* entry = nullptr;
502 const std::string_view& descriptor(type.GetDescriptor());
503 if (type.IsUnresolvedTypes()) {
504 for (size_t i = primitive_count_; i < entries_.size(); i++) {
505 const RegType* cur_entry = entries_[i];
506 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
507 down_cast<const UnresolvedUninitializedRefType*>(cur_entry)->GetAllocationPc()
508 == allocation_pc &&
509 (cur_entry->GetDescriptor() == descriptor)) {
510 return *down_cast<const UnresolvedUninitializedRefType*>(cur_entry);
511 }
512 }
513 entry = new (&allocator_) UnresolvedUninitializedRefType(descriptor,
514 allocation_pc,
515 entries_.size());
516 } else {
517 ObjPtr<mirror::Class> klass = type.GetClass();
518 for (size_t i = primitive_count_; i < entries_.size(); i++) {
519 const RegType* cur_entry = entries_[i];
520 if (cur_entry->IsUninitializedReference() &&
521 down_cast<const UninitializedReferenceType*>(cur_entry)
522 ->GetAllocationPc() == allocation_pc &&
523 cur_entry->GetClass() == klass) {
524 return *down_cast<const UninitializedReferenceType*>(cur_entry);
525 }
526 }
527 entry = new (&allocator_) UninitializedReferenceType(klass,
528 descriptor,
529 allocation_pc,
530 entries_.size());
531 }
532 return AddEntry(entry);
533 }
534
FromUninitialized(const RegType & uninit_type)535 const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
536 RegType* entry;
537
538 if (uninit_type.IsUnresolvedTypes()) {
539 const std::string_view& descriptor(uninit_type.GetDescriptor());
540 for (size_t i = primitive_count_; i < entries_.size(); i++) {
541 const RegType* cur_entry = entries_[i];
542 if (cur_entry->IsUnresolvedReference() &&
543 cur_entry->GetDescriptor() == descriptor) {
544 return *cur_entry;
545 }
546 }
547 entry = new (&allocator_) UnresolvedReferenceType(descriptor, entries_.size());
548 } else {
549 ObjPtr<mirror::Class> klass = uninit_type.GetClass();
550 if (uninit_type.IsUninitializedThisReference() && !klass->IsFinal()) {
551 // For uninitialized "this reference" look for reference types that are not precise.
552 for (size_t i = primitive_count_; i < entries_.size(); i++) {
553 const RegType* cur_entry = entries_[i];
554 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
555 return *cur_entry;
556 }
557 }
558 entry = new (&allocator_) ReferenceType(klass, "", entries_.size());
559 } else if (!klass->IsPrimitive()) {
560 // We're uninitialized because of allocation, look or create a precise type as allocations
561 // may only create objects of that type.
562 // Note: we do not check whether the given klass is actually instantiable (besides being
563 // primitive), that is, we allow interfaces and abstract classes here. The reasoning is
564 // twofold:
565 // 1) The "new-instance" instruction to generate the uninitialized type will already
566 // queue an instantiation error. This is a soft error that must be thrown at runtime,
567 // and could potentially change if the class is resolved differently at runtime.
568 // 2) Checking whether the klass is instantiable and using conflict may produce a hard
569 // error when the value is used, which leads to a VerifyError, which is not the
570 // correct semantics.
571 for (size_t i = primitive_count_; i < entries_.size(); i++) {
572 const RegType* cur_entry = entries_[i];
573 if (cur_entry->IsPreciseReference() && cur_entry->GetClass() == klass) {
574 return *cur_entry;
575 }
576 }
577 entry = new (&allocator_) PreciseReferenceType(klass,
578 uninit_type.GetDescriptor(),
579 entries_.size());
580 } else {
581 return Conflict();
582 }
583 }
584 return AddEntry(entry);
585 }
586
UninitializedThisArgument(const RegType & type)587 const UninitializedType& RegTypeCache::UninitializedThisArgument(const RegType& type) {
588 UninitializedType* entry;
589 const std::string_view& descriptor(type.GetDescriptor());
590 if (type.IsUnresolvedTypes()) {
591 for (size_t i = primitive_count_; i < entries_.size(); i++) {
592 const RegType* cur_entry = entries_[i];
593 if (cur_entry->IsUnresolvedAndUninitializedThisReference() &&
594 cur_entry->GetDescriptor() == descriptor) {
595 return *down_cast<const UninitializedType*>(cur_entry);
596 }
597 }
598 entry = new (&allocator_) UnresolvedUninitializedThisRefType(descriptor, entries_.size());
599 } else {
600 ObjPtr<mirror::Class> klass = type.GetClass();
601 for (size_t i = primitive_count_; i < entries_.size(); i++) {
602 const RegType* cur_entry = entries_[i];
603 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
604 return *down_cast<const UninitializedType*>(cur_entry);
605 }
606 }
607 entry = new (&allocator_) UninitializedThisReferenceType(klass, descriptor, entries_.size());
608 }
609 return AddEntry(entry);
610 }
611
FromCat1NonSmallConstant(int32_t value,bool precise)612 const ConstantType& RegTypeCache::FromCat1NonSmallConstant(int32_t value, bool precise) {
613 for (size_t i = primitive_count_; i < entries_.size(); i++) {
614 const RegType* cur_entry = entries_[i];
615 if (cur_entry->klass_.IsNull() && cur_entry->IsConstant() &&
616 cur_entry->IsPreciseConstant() == precise &&
617 (down_cast<const ConstantType*>(cur_entry))->ConstantValue() == value) {
618 return *down_cast<const ConstantType*>(cur_entry);
619 }
620 }
621 ConstantType* entry;
622 if (precise) {
623 entry = new (&allocator_) PreciseConstType(value, entries_.size());
624 } else {
625 entry = new (&allocator_) ImpreciseConstType(value, entries_.size());
626 }
627 return AddEntry(entry);
628 }
629
FromCat2ConstLo(int32_t value,bool precise)630 const ConstantType& RegTypeCache::FromCat2ConstLo(int32_t value, bool precise) {
631 for (size_t i = primitive_count_; i < entries_.size(); i++) {
632 const RegType* cur_entry = entries_[i];
633 if (cur_entry->IsConstantLo() && (cur_entry->IsPrecise() == precise) &&
634 (down_cast<const ConstantType*>(cur_entry))->ConstantValueLo() == value) {
635 return *down_cast<const ConstantType*>(cur_entry);
636 }
637 }
638 ConstantType* entry;
639 if (precise) {
640 entry = new (&allocator_) PreciseConstLoType(value, entries_.size());
641 } else {
642 entry = new (&allocator_) ImpreciseConstLoType(value, entries_.size());
643 }
644 return AddEntry(entry);
645 }
646
FromCat2ConstHi(int32_t value,bool precise)647 const ConstantType& RegTypeCache::FromCat2ConstHi(int32_t value, bool precise) {
648 for (size_t i = primitive_count_; i < entries_.size(); i++) {
649 const RegType* cur_entry = entries_[i];
650 if (cur_entry->IsConstantHi() && (cur_entry->IsPrecise() == precise) &&
651 (down_cast<const ConstantType*>(cur_entry))->ConstantValueHi() == value) {
652 return *down_cast<const ConstantType*>(cur_entry);
653 }
654 }
655 ConstantType* entry;
656 if (precise) {
657 entry = new (&allocator_) PreciseConstHiType(value, entries_.size());
658 } else {
659 entry = new (&allocator_) ImpreciseConstHiType(value, entries_.size());
660 }
661 return AddEntry(entry);
662 }
663
GetComponentType(const RegType & array,ObjPtr<mirror::ClassLoader> loader)664 const RegType& RegTypeCache::GetComponentType(const RegType& array,
665 ObjPtr<mirror::ClassLoader> loader) {
666 if (!array.IsArrayTypes()) {
667 return Conflict();
668 } else if (array.IsUnresolvedTypes()) {
669 DCHECK(!array.IsUnresolvedMergedReference()); // Caller must make sure not to ask for this.
670 const std::string descriptor(array.GetDescriptor());
671 return FromDescriptor(loader, descriptor.c_str() + 1, false);
672 } else {
673 ObjPtr<mirror::Class> klass = array.GetClass()->GetComponentType();
674 std::string temp;
675 const char* descriptor = klass->GetDescriptor(&temp);
676 if (klass->IsErroneous()) {
677 // Arrays may have erroneous component types, use unresolved in that case.
678 // We assume that the primitive classes are not erroneous, so we know it is a
679 // reference type.
680 return FromDescriptor(loader, descriptor, false);
681 } else {
682 return FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
683 }
684 }
685 }
686
Dump(std::ostream & os)687 void RegTypeCache::Dump(std::ostream& os) {
688 for (size_t i = 0; i < entries_.size(); i++) {
689 const RegType* cur_entry = entries_[i];
690 if (cur_entry != nullptr) {
691 os << i << ": " << cur_entry->Dump() << "\n";
692 }
693 }
694 }
695
VisitStaticRoots(RootVisitor * visitor)696 void RegTypeCache::VisitStaticRoots(RootVisitor* visitor) {
697 // Visit the primitive types, this is required since if there are no active verifiers they wont
698 // be in the entries array, and therefore not visited as roots.
699 if (primitive_initialized_) {
700 RootInfo ri(kRootUnknown);
701 UndefinedType::GetInstance()->VisitRoots(visitor, ri);
702 ConflictType::GetInstance()->VisitRoots(visitor, ri);
703 BooleanType::GetInstance()->VisitRoots(visitor, ri);
704 ByteType::GetInstance()->VisitRoots(visitor, ri);
705 ShortType::GetInstance()->VisitRoots(visitor, ri);
706 CharType::GetInstance()->VisitRoots(visitor, ri);
707 IntegerType::GetInstance()->VisitRoots(visitor, ri);
708 LongLoType::GetInstance()->VisitRoots(visitor, ri);
709 LongHiType::GetInstance()->VisitRoots(visitor, ri);
710 FloatType::GetInstance()->VisitRoots(visitor, ri);
711 DoubleLoType::GetInstance()->VisitRoots(visitor, ri);
712 DoubleHiType::GetInstance()->VisitRoots(visitor, ri);
713 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
714 small_precise_constants_[value - kMinSmallConstant]->VisitRoots(visitor, ri);
715 }
716 }
717 }
718
VisitRoots(RootVisitor * visitor,const RootInfo & root_info)719 void RegTypeCache::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
720 // Exclude the static roots that are visited by VisitStaticRoots().
721 for (size_t i = primitive_count_; i < entries_.size(); ++i) {
722 entries_[i]->VisitRoots(visitor, root_info);
723 }
724 for (auto& pair : klass_entries_) {
725 GcRoot<mirror::Class>& root = pair.first;
726 root.VisitRoot(visitor, root_info);
727 }
728 }
729
730 } // namespace verifier
731 } // namespace art
732