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 "base/casts.h"
20 #include "class_linker-inl.h"
21 #include "dex_file-inl.h"
22 #include "mirror/class-inl.h"
23 #include "mirror/object-inl.h"
24 #include "reg_type-inl.h"
25 
26 namespace art {
27 namespace verifier {
28 
29 bool RegTypeCache::primitive_initialized_ = false;
30 uint16_t RegTypeCache::primitive_count_ = 0;
31 const PreciseConstType* RegTypeCache::small_precise_constants_[kMaxSmallConstant - kMinSmallConstant + 1];
32 
MatchingPrecisionForClass(const RegType * entry,bool precise)33 static bool MatchingPrecisionForClass(const RegType* entry, bool precise)
34     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
35   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   }
46 }
47 
FillPrimitiveAndSmallConstantTypes()48 void RegTypeCache::FillPrimitiveAndSmallConstantTypes() {
49   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());
61   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   }
66   DCHECK_EQ(entries_.size(), primitive_count_);
67 }
68 
FromDescriptor(mirror::ClassLoader * loader,const char * descriptor,bool precise)69 const RegType& RegTypeCache::FromDescriptor(mirror::ClassLoader* loader, const char* descriptor,
70                                             bool precise) {
71   DCHECK(RegTypeCache::primitive_initialized_);
72   if (descriptor[1] == '\0') {
73     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:
92         return Conflict();
93     }
94   } else if (descriptor[0] == 'L' || descriptor[0] == '[') {
95     return From(loader, descriptor, precise);
96   } else {
97     return Conflict();
98   }
99 }
100 
RegTypeFromPrimitiveType(Primitive::Type prim_type) const101 const RegType& RegTypeCache::RegTypeFromPrimitiveType(Primitive::Type prim_type) const {
102   DCHECK(RegTypeCache::primitive_initialized_);
103   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();
123   }
124 }
125 
MatchDescriptor(size_t idx,const StringPiece & descriptor,bool precise)126 bool RegTypeCache::MatchDescriptor(size_t idx, const StringPiece& descriptor, bool precise) {
127   const RegType* entry = entries_[idx];
128   if (descriptor != entry->descriptor_) {
129     return false;
130   }
131   if (entry->HasClass()) {
132     return MatchingPrecisionForClass(entry, precise);
133   }
134   // 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;
138 }
139 
ResolveClass(const char * descriptor,mirror::ClassLoader * loader)140 mirror::Class* RegTypeCache::ResolveClass(const char* descriptor, mirror::ClassLoader* loader) {
141   // Class was not found, must create new type.
142   // Try resolving class
143   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
144   Thread* self = Thread::Current();
145   StackHandleScope<1> hs(self);
146   Handle<mirror::ClassLoader> class_loader(hs.NewHandle(loader));
147   mirror::Class* klass = nullptr;
148   if (can_load_classes_) {
149     klass = class_linker->FindClass(self, descriptor, class_loader);
150   } else {
151     klass = class_linker->LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor),
152                                       loader);
153     if (klass != nullptr && !klass->IsLoaded()) {
154       // We found the class but without it being loaded its not safe for use.
155       klass = nullptr;
156     }
157   }
158   return klass;
159 }
160 
From(mirror::ClassLoader * loader,const char * descriptor,bool precise)161 const RegType& RegTypeCache::From(mirror::ClassLoader* loader, const char* descriptor,
162                                   bool precise) {
163   // 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);
166   for (size_t i = primitive_count_; i < entries_.size(); i++) {
167     if (MatchDescriptor(i, descriptor_sp, precise)) {
168       return *(entries_[i]);
169     }
170   }
171   // Class not found in the cache, will create a new type for that.
172   // Try resolving class.
173   mirror::Class* klass = ResolveClass(descriptor, loader);
174   if (klass != nullptr) {
175     // Class resolved, first look for the class in the list of entries
176     // Class was not found, must create new type.
177     // To pass the verification, the type should be imprecise,
178     // instantiable or an interface with the precise type set to false.
179     DCHECK(!precise || klass->IsInstantiable());
180     // Create a precise type if:
181     // 1- Class is final and NOT an interface. a precise interface is meaningless !!
182     // 2- Precise Flag passed as true.
183     RegType* entry;
184     // Create an imprecise type if we can't tell for a fact that it is precise.
185     if (klass->CannotBeAssignedFromOtherTypes() || precise) {
186       DCHECK(!(klass->IsAbstract()) || klass->IsArrayClass());
187       DCHECK(!klass->IsInterface());
188       entry = new PreciseReferenceType(klass, descriptor_sp.as_string(), entries_.size());
189     } else {
190       entry = new ReferenceType(klass, descriptor_sp.as_string(), entries_.size());
191     }
192     AddEntry(entry);
193     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.
197     if (can_load_classes_) {
198       DCHECK(Thread::Current()->IsExceptionPending());
199       Thread::Current()->ClearException();
200     } else {
201       DCHECK(!Thread::Current()->IsExceptionPending());
202     }
203     if (IsValidDescriptor(descriptor)) {
204       RegType* entry = new UnresolvedReferenceType(descriptor_sp.as_string(), entries_.size());
205       AddEntry(entry);
206       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 }
214 
FromClass(const char * descriptor,mirror::Class * klass,bool precise)215 const RegType& RegTypeCache::FromClass(const char* descriptor, mirror::Class* klass, bool precise) {
216   DCHECK(klass != nullptr);
217   if (klass->IsPrimitive()) {
218     // Note: precise isn't used for primitive classes. A char is assignable to an int. All
219     // primitive classes are final.
220     return RegTypeFromPrimitiveType(klass->GetPrimitiveType());
221   } else {
222     // Look for the reference in the list of entries to have.
223     for (size_t i = primitive_count_; i < entries_.size(); i++) {
224       const RegType* cur_entry = entries_[i];
225       if (cur_entry->klass_.Read() == klass && MatchingPrecisionForClass(cur_entry, precise)) {
226         return *cur_entry;
227       }
228     }
229     // No reference to the class was found, create new reference.
230     RegType* entry;
231     if (precise) {
232       entry = new PreciseReferenceType(klass, descriptor, entries_.size());
233     } else {
234       entry = new ReferenceType(klass, descriptor, entries_.size());
235     }
236     AddEntry(entry);
237     return *entry;
238   }
239 }
240 
RegTypeCache(bool can_load_classes)241 RegTypeCache::RegTypeCache(bool can_load_classes) : can_load_classes_(can_load_classes) {
242   if (kIsDebugBuild) {
243     Thread::Current()->AssertThreadSuspensionIsAllowable(gAborting == 0);
244   }
245   entries_.reserve(64);
246   FillPrimitiveAndSmallConstantTypes();
247 }
248 
~RegTypeCache()249 RegTypeCache::~RegTypeCache() {
250   CHECK_LE(primitive_count_, entries_.size());
251   // Delete only the non primitive types.
252   if (entries_.size() == kNumPrimitivesAndSmallConstants) {
253     // All entries are from the global pool, nothing to delete.
254     return;
255   }
256   std::vector<const RegType*>::iterator non_primitive_begin = entries_.begin();
257   std::advance(non_primitive_begin, kNumPrimitivesAndSmallConstants);
258   STLDeleteContainerPointers(non_primitive_begin, entries_.end());
259 }
260 
ShutDown()261 void 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();
272     FloatType::Destroy();
273     DoubleLoType::Destroy();
274     DoubleHiType::Destroy();
275     for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
276       const PreciseConstType* type = small_precise_constants_[value - kMinSmallConstant];
277       delete type;
278       small_precise_constants_[value - kMinSmallConstant] = nullptr;
279     }
280     RegTypeCache::primitive_initialized_ = false;
281     RegTypeCache::primitive_count_ = 0;
282   }
283 }
284 
285 template <class Type>
CreatePrimitiveTypeInstance(const std::string & descriptor)286 const Type* RegTypeCache::CreatePrimitiveTypeInstance(const std::string& descriptor) {
287   mirror::Class* klass = nullptr;
288   // Try loading the class from linker.
289   if (!descriptor.empty()) {
290     klass = art::Runtime::Current()->GetClassLinker()->FindSystemClass(Thread::Current(),
291                                                                        descriptor.c_str());
292     DCHECK(klass != nullptr);
293   }
294   const Type* entry = Type::CreateInstance(klass, descriptor, RegTypeCache::primitive_count_);
295   RegTypeCache::primitive_count_++;
296   return entry;
297 }
298 
CreatePrimitiveAndSmallConstantTypes()299 void RegTypeCache::CreatePrimitiveAndSmallConstantTypes() {
300   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");
312   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   }
317 }
318 
FromUnresolvedMerge(const RegType & left,const RegType & right)319 const RegType& RegTypeCache::FromUnresolvedMerge(const RegType& left, const RegType& right) {
320   BitVector types(1,                                    // Allocate at least a word.
321                   true,                                 // Is expandable.
322                   Allocator::GetMallocAllocator());     // TODO: Arenas in the verifier.
323   const RegType* left_resolved;
324   if (left.IsUnresolvedMergedReference()) {
325     const UnresolvedMergedType* left_merge = down_cast<const UnresolvedMergedType*>(&left);
326     types.Copy(&left_merge->GetUnresolvedTypes());
327     left_resolved = &left_merge->GetResolvedPart();
328   } else if (left.IsUnresolvedTypes()) {
329     types.SetBit(left.GetId());
330     left_resolved = &Zero();
331   } else {
332     left_resolved = &left;
333   }
334 
335   const RegType* right_resolved;
336   if (right.IsUnresolvedMergedReference()) {
337     const UnresolvedMergedType* right_merge = down_cast<const UnresolvedMergedType*>(&right);
338     types.Union(&right_merge->GetUnresolvedTypes());
339     right_resolved = &right_merge->GetResolvedPart();
340   } else if (right.IsUnresolvedTypes()) {
341     types.SetBit(right.GetId());
342     right_resolved = &Zero();
343   } else {
344     right_resolved = &right;
345   }
346 
347   // Merge the resolved parts. Left and right might be equal, so use SafeMerge.
348   const RegType& resolved_parts_merged = left_resolved->SafeMerge(*right_resolved, this);
349   // If we get a conflict here, the merge result is a conflict, not an unresolved merge type.
350   if (resolved_parts_merged.IsConflict()) {
351     return Conflict();
352   }
353 
354   // Check if entry already exists.
355   for (size_t i = primitive_count_; i < entries_.size(); i++) {
356     const RegType* cur_entry = entries_[i];
357     if (cur_entry->IsUnresolvedMergedReference()) {
358       const UnresolvedMergedType* cmp_type = down_cast<const UnresolvedMergedType*>(cur_entry);
359       const RegType& resolved_part = cmp_type->GetResolvedPart();
360       const BitVector& unresolved_part = cmp_type->GetUnresolvedTypes();
361       // Use SameBitsSet. "types" is expandable to allow merging in the components, but the
362       // BitVector in the final RegType will be made non-expandable.
363       if (&resolved_part == &resolved_parts_merged &&
364               types.SameBitsSet(&unresolved_part)) {
365         return *cur_entry;
366       }
367     }
368   }
369 
370   // Create entry.
371   RegType* entry = new UnresolvedMergedType(resolved_parts_merged,
372                                             types,
373                                             this,
374                                             entries_.size());
375   AddEntry(entry);
376   return *entry;
377 }
378 
FromUnresolvedSuperClass(const RegType & child)379 const RegType& RegTypeCache::FromUnresolvedSuperClass(const RegType& child) {
380   // Check if entry already exists.
381   for (size_t i = primitive_count_; i < entries_.size(); i++) {
382     const RegType* cur_entry = entries_[i];
383     if (cur_entry->IsUnresolvedSuperClass()) {
384       const UnresolvedSuperClass* tmp_entry =
385           down_cast<const UnresolvedSuperClass*>(cur_entry);
386       uint16_t unresolved_super_child_id =
387           tmp_entry->GetUnresolvedSuperClassChildId();
388       if (unresolved_super_child_id == child.GetId()) {
389         return *cur_entry;
390       }
391     }
392   }
393   RegType* entry = new UnresolvedSuperClass(child.GetId(), this, entries_.size());
394   AddEntry(entry);
395   return *entry;
396 }
397 
Uninitialized(const RegType & type,uint32_t allocation_pc)398 const UninitializedType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
399   UninitializedType* entry = nullptr;
400   const std::string& descriptor(type.GetDescriptor());
401   if (type.IsUnresolvedTypes()) {
402     for (size_t i = primitive_count_; i < entries_.size(); i++) {
403       const RegType* cur_entry = entries_[i];
404       if (cur_entry->IsUnresolvedAndUninitializedReference() &&
405           down_cast<const UnresolvedUninitializedRefType*>(cur_entry)->GetAllocationPc()
406               == allocation_pc &&
407           (cur_entry->GetDescriptor() == descriptor)) {
408         return *down_cast<const UnresolvedUninitializedRefType*>(cur_entry);
409       }
410     }
411     entry = new UnresolvedUninitializedRefType(descriptor, allocation_pc, entries_.size());
412   } else {
413     mirror::Class* klass = type.GetClass();
414     for (size_t i = primitive_count_; i < entries_.size(); i++) {
415       const RegType* cur_entry = entries_[i];
416       if (cur_entry->IsUninitializedReference() &&
417           down_cast<const UninitializedReferenceType*>(cur_entry)
418               ->GetAllocationPc() == allocation_pc &&
419           cur_entry->GetClass() == klass) {
420         return *down_cast<const UninitializedReferenceType*>(cur_entry);
421       }
422     }
423     entry = new UninitializedReferenceType(klass, descriptor, allocation_pc, entries_.size());
424   }
425   AddEntry(entry);
426   return *entry;
427 }
428 
FromUninitialized(const RegType & uninit_type)429 const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
430   RegType* entry;
431 
432   if (uninit_type.IsUnresolvedTypes()) {
433     const std::string& descriptor(uninit_type.GetDescriptor());
434     for (size_t i = primitive_count_; i < entries_.size(); i++) {
435       const RegType* cur_entry = entries_[i];
436       if (cur_entry->IsUnresolvedReference() &&
437           cur_entry->GetDescriptor() == descriptor) {
438         return *cur_entry;
439       }
440     }
441     entry = new UnresolvedReferenceType(descriptor, entries_.size());
442   } else {
443     mirror::Class* klass = uninit_type.GetClass();
444     if (uninit_type.IsUninitializedThisReference() && !klass->IsFinal()) {
445       // For uninitialized "this reference" look for reference types that are not precise.
446       for (size_t i = primitive_count_; i < entries_.size(); i++) {
447         const RegType* cur_entry = entries_[i];
448         if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
449           return *cur_entry;
450         }
451       }
452       entry = new ReferenceType(klass, "", entries_.size());
453     } else if (klass->IsInstantiable()) {
454       // We're uninitialized because of allocation, look or create a precise type as allocations
455       // may only create objects of that type.
456       for (size_t i = primitive_count_; i < entries_.size(); i++) {
457         const RegType* cur_entry = entries_[i];
458         if (cur_entry->IsPreciseReference() && cur_entry->GetClass() == klass) {
459           return *cur_entry;
460         }
461       }
462       entry = new PreciseReferenceType(klass, uninit_type.GetDescriptor(), entries_.size());
463     } else {
464       return Conflict();
465     }
466   }
467   AddEntry(entry);
468   return *entry;
469 }
470 
UninitializedThisArgument(const RegType & type)471 const UninitializedType& RegTypeCache::UninitializedThisArgument(const RegType& type) {
472   UninitializedType* entry;
473   const std::string& descriptor(type.GetDescriptor());
474   if (type.IsUnresolvedTypes()) {
475     for (size_t i = primitive_count_; i < entries_.size(); i++) {
476       const RegType* cur_entry = entries_[i];
477       if (cur_entry->IsUnresolvedAndUninitializedThisReference() &&
478           cur_entry->GetDescriptor() == descriptor) {
479         return *down_cast<const UninitializedType*>(cur_entry);
480       }
481     }
482     entry = new UnresolvedUninitializedThisRefType(descriptor, entries_.size());
483   } else {
484     mirror::Class* klass = type.GetClass();
485     for (size_t i = primitive_count_; i < entries_.size(); i++) {
486       const RegType* cur_entry = entries_[i];
487       if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
488         return *down_cast<const UninitializedType*>(cur_entry);
489       }
490     }
491     entry = new UninitializedThisReferenceType(klass, descriptor, entries_.size());
492   }
493   AddEntry(entry);
494   return *entry;
495 }
496 
FromCat1NonSmallConstant(int32_t value,bool precise)497 const ConstantType& RegTypeCache::FromCat1NonSmallConstant(int32_t value, bool precise) {
498   for (size_t i = primitive_count_; i < entries_.size(); i++) {
499     const RegType* cur_entry = entries_[i];
500     if (cur_entry->klass_.IsNull() && cur_entry->IsConstant() &&
501         cur_entry->IsPreciseConstant() == precise &&
502         (down_cast<const ConstantType*>(cur_entry))->ConstantValue() == value) {
503       return *down_cast<const ConstantType*>(cur_entry);
504     }
505   }
506   ConstantType* entry;
507   if (precise) {
508     entry = new PreciseConstType(value, entries_.size());
509   } else {
510     entry = new ImpreciseConstType(value, entries_.size());
511   }
512   AddEntry(entry);
513   return *entry;
514 }
515 
FromCat2ConstLo(int32_t value,bool precise)516 const ConstantType& RegTypeCache::FromCat2ConstLo(int32_t value, bool precise) {
517   for (size_t i = primitive_count_; i < entries_.size(); i++) {
518     const RegType* cur_entry = entries_[i];
519     if (cur_entry->IsConstantLo() && (cur_entry->IsPrecise() == precise) &&
520         (down_cast<const ConstantType*>(cur_entry))->ConstantValueLo() == value) {
521       return *down_cast<const ConstantType*>(cur_entry);
522     }
523   }
524   ConstantType* entry;
525   if (precise) {
526     entry = new PreciseConstLoType(value, entries_.size());
527   } else {
528     entry = new ImpreciseConstLoType(value, entries_.size());
529   }
530   AddEntry(entry);
531   return *entry;
532 }
533 
FromCat2ConstHi(int32_t value,bool precise)534 const ConstantType& RegTypeCache::FromCat2ConstHi(int32_t value, bool precise) {
535   for (size_t i = primitive_count_; i < entries_.size(); i++) {
536     const RegType* cur_entry = entries_[i];
537     if (cur_entry->IsConstantHi() && (cur_entry->IsPrecise() == precise) &&
538         (down_cast<const ConstantType*>(cur_entry))->ConstantValueHi() == value) {
539       return *down_cast<const ConstantType*>(cur_entry);
540     }
541   }
542   ConstantType* entry;
543   if (precise) {
544     entry = new PreciseConstHiType(value, entries_.size());
545   } else {
546     entry = new ImpreciseConstHiType(value, entries_.size());
547   }
548   AddEntry(entry);
549   return *entry;
550 }
551 
GetComponentType(const RegType & array,mirror::ClassLoader * loader)552 const RegType& RegTypeCache::GetComponentType(const RegType& array, mirror::ClassLoader* loader) {
553   if (!array.IsArrayTypes()) {
554     return Conflict();
555   } else if (array.IsUnresolvedTypes()) {
556     const std::string& descriptor(array.GetDescriptor());
557     const std::string component(descriptor.substr(1, descriptor.size() - 1));
558     return FromDescriptor(loader, component.c_str(), false);
559   } else {
560     mirror::Class* klass = array.GetClass()->GetComponentType();
561     std::string temp;
562     if (klass->IsErroneous()) {
563       // Arrays may have erroneous component types, use unresolved in that case.
564       // We assume that the primitive classes are not erroneous, so we know it is a
565       // reference type.
566       return FromDescriptor(loader, klass->GetDescriptor(&temp), false);
567     } else {
568       return FromClass(klass->GetDescriptor(&temp), klass,
569                        klass->CannotBeAssignedFromOtherTypes());
570     }
571   }
572 }
573 
Dump(std::ostream & os)574 void RegTypeCache::Dump(std::ostream& os) {
575   for (size_t i = 0; i < entries_.size(); i++) {
576     const RegType* cur_entry = entries_[i];
577     if (cur_entry != nullptr) {
578       os << i << ": " << cur_entry->Dump() << "\n";
579     }
580   }
581 }
582 
VisitStaticRoots(RootVisitor * visitor)583 void RegTypeCache::VisitStaticRoots(RootVisitor* visitor) {
584   // Visit the primitive types, this is required since if there are no active verifiers they wont
585   // be in the entries array, and therefore not visited as roots.
586   if (primitive_initialized_) {
587     RootInfo ri(kRootUnknown);
588     UndefinedType::GetInstance()->VisitRoots(visitor, ri);
589     ConflictType::GetInstance()->VisitRoots(visitor, ri);
590     BooleanType::GetInstance()->VisitRoots(visitor, ri);
591     ByteType::GetInstance()->VisitRoots(visitor, ri);
592     ShortType::GetInstance()->VisitRoots(visitor, ri);
593     CharType::GetInstance()->VisitRoots(visitor, ri);
594     IntegerType::GetInstance()->VisitRoots(visitor, ri);
595     LongLoType::GetInstance()->VisitRoots(visitor, ri);
596     LongHiType::GetInstance()->VisitRoots(visitor, ri);
597     FloatType::GetInstance()->VisitRoots(visitor, ri);
598     DoubleLoType::GetInstance()->VisitRoots(visitor, ri);
599     DoubleHiType::GetInstance()->VisitRoots(visitor, ri);
600     for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
601       small_precise_constants_[value - kMinSmallConstant]->VisitRoots(visitor, ri);
602     }
603   }
604 }
605 
VisitRoots(RootVisitor * visitor,const RootInfo & root_info)606 void RegTypeCache::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
607   // Exclude the static roots that are visited by VisitStaticRoots().
608   for (size_t i = primitive_count_; i < entries_.size(); ++i) {
609     entries_[i]->VisitRoots(visitor, root_info);
610   }
611 }
612 
AddEntry(RegType * new_entry)613 void RegTypeCache::AddEntry(RegType* new_entry) {
614   entries_.push_back(new_entry);
615 }
616 
617 }  // namespace verifier
618 }  // namespace art
619