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-inl.h"
18
19 #include "android-base/stringprintf.h"
20
21 #include "base/arena_bit_vector.h"
22 #include "base/bit_vector-inl.h"
23 #include "base/casts.h"
24 #include "class_linker-inl.h"
25 #include "dex/descriptors_names.h"
26 #include "dex/dex_file-inl.h"
27 #include "method_verifier.h"
28 #include "mirror/class-inl.h"
29 #include "mirror/class.h"
30 #include "mirror/object-inl.h"
31 #include "mirror/object_array-inl.h"
32 #include "reg_type_cache-inl.h"
33 #include "scoped_thread_state_change-inl.h"
34
35 #include <limits>
36 #include <sstream>
37
38 namespace art {
39 namespace verifier {
40
41 using android::base::StringPrintf;
42
43 const UndefinedType* UndefinedType::instance_ = nullptr;
44 const ConflictType* ConflictType::instance_ = nullptr;
45 const BooleanType* BooleanType::instance_ = nullptr;
46 const ByteType* ByteType::instance_ = nullptr;
47 const ShortType* ShortType::instance_ = nullptr;
48 const CharType* CharType::instance_ = nullptr;
49 const FloatType* FloatType::instance_ = nullptr;
50 const LongLoType* LongLoType::instance_ = nullptr;
51 const LongHiType* LongHiType::instance_ = nullptr;
52 const DoubleLoType* DoubleLoType::instance_ = nullptr;
53 const DoubleHiType* DoubleHiType::instance_ = nullptr;
54 const IntegerType* IntegerType::instance_ = nullptr;
55 const NullType* NullType::instance_ = nullptr;
56
PrimitiveType(ObjPtr<mirror::Class> klass,const std::string_view & descriptor,uint16_t cache_id)57 PrimitiveType::PrimitiveType(ObjPtr<mirror::Class> klass,
58 const std::string_view& descriptor,
59 uint16_t cache_id)
60 : RegType(klass, descriptor, cache_id) {
61 CHECK(klass != nullptr);
62 CHECK(!descriptor.empty());
63 }
64
Cat1Type(ObjPtr<mirror::Class> klass,const std::string_view & descriptor,uint16_t cache_id)65 Cat1Type::Cat1Type(ObjPtr<mirror::Class> klass,
66 const std::string_view& descriptor,
67 uint16_t cache_id)
68 : PrimitiveType(klass, descriptor, cache_id) {
69 }
70
Cat2Type(ObjPtr<mirror::Class> klass,const std::string_view & descriptor,uint16_t cache_id)71 Cat2Type::Cat2Type(ObjPtr<mirror::Class> klass,
72 const std::string_view& descriptor,
73 uint16_t cache_id)
74 : PrimitiveType(klass, descriptor, cache_id) {
75 }
76
Dump() const77 std::string PreciseConstType::Dump() const {
78 std::stringstream result;
79 uint32_t val = ConstantValue();
80 if (val == 0) {
81 CHECK(IsPreciseConstant());
82 result << "Zero/null";
83 } else {
84 result << "Precise ";
85 if (IsConstantShort()) {
86 result << StringPrintf("Constant: %d", val);
87 } else {
88 result << StringPrintf("Constant: 0x%x", val);
89 }
90 }
91 return result.str();
92 }
93
Dump() const94 std::string BooleanType::Dump() const {
95 return "Boolean";
96 }
97
Dump() const98 std::string ConflictType::Dump() const {
99 return "Conflict";
100 }
101
Dump() const102 std::string ByteType::Dump() const {
103 return "Byte";
104 }
105
Dump() const106 std::string ShortType::Dump() const {
107 return "Short";
108 }
109
Dump() const110 std::string CharType::Dump() const {
111 return "Char";
112 }
113
Dump() const114 std::string FloatType::Dump() const {
115 return "Float";
116 }
117
Dump() const118 std::string LongLoType::Dump() const {
119 return "Long (Low Half)";
120 }
121
Dump() const122 std::string LongHiType::Dump() const {
123 return "Long (High Half)";
124 }
125
Dump() const126 std::string DoubleLoType::Dump() const {
127 return "Double (Low Half)";
128 }
129
Dump() const130 std::string DoubleHiType::Dump() const {
131 return "Double (High Half)";
132 }
133
Dump() const134 std::string IntegerType::Dump() const {
135 return "Integer";
136 }
137
CreateInstance(ObjPtr<mirror::Class> klass,const std::string_view & descriptor,uint16_t cache_id)138 const DoubleHiType* DoubleHiType::CreateInstance(ObjPtr<mirror::Class> klass,
139 const std::string_view& descriptor,
140 uint16_t cache_id) {
141 CHECK(instance_ == nullptr);
142 instance_ = new DoubleHiType(klass, descriptor, cache_id);
143 return instance_;
144 }
145
Destroy()146 void DoubleHiType::Destroy() {
147 if (instance_ != nullptr) {
148 delete instance_;
149 instance_ = nullptr;
150 }
151 }
152
CreateInstance(ObjPtr<mirror::Class> klass,const std::string_view & descriptor,uint16_t cache_id)153 const DoubleLoType* DoubleLoType::CreateInstance(ObjPtr<mirror::Class> klass,
154 const std::string_view& descriptor,
155 uint16_t cache_id) {
156 CHECK(instance_ == nullptr);
157 instance_ = new DoubleLoType(klass, descriptor, cache_id);
158 return instance_;
159 }
160
Destroy()161 void DoubleLoType::Destroy() {
162 if (instance_ != nullptr) {
163 delete instance_;
164 instance_ = nullptr;
165 }
166 }
167
CreateInstance(ObjPtr<mirror::Class> klass,const std::string_view & descriptor,uint16_t cache_id)168 const LongLoType* LongLoType::CreateInstance(ObjPtr<mirror::Class> klass,
169 const std::string_view& descriptor,
170 uint16_t cache_id) {
171 CHECK(instance_ == nullptr);
172 instance_ = new LongLoType(klass, descriptor, cache_id);
173 return instance_;
174 }
175
CreateInstance(ObjPtr<mirror::Class> klass,const std::string_view & descriptor,uint16_t cache_id)176 const LongHiType* LongHiType::CreateInstance(ObjPtr<mirror::Class> klass,
177 const std::string_view& descriptor,
178 uint16_t cache_id) {
179 CHECK(instance_ == nullptr);
180 instance_ = new LongHiType(klass, descriptor, cache_id);
181 return instance_;
182 }
183
Destroy()184 void LongHiType::Destroy() {
185 if (instance_ != nullptr) {
186 delete instance_;
187 instance_ = nullptr;
188 }
189 }
190
Destroy()191 void LongLoType::Destroy() {
192 if (instance_ != nullptr) {
193 delete instance_;
194 instance_ = nullptr;
195 }
196 }
197
CreateInstance(ObjPtr<mirror::Class> klass,const std::string_view & descriptor,uint16_t cache_id)198 const FloatType* FloatType::CreateInstance(ObjPtr<mirror::Class> klass,
199 const std::string_view& descriptor,
200 uint16_t cache_id) {
201 CHECK(instance_ == nullptr);
202 instance_ = new FloatType(klass, descriptor, cache_id);
203 return instance_;
204 }
205
Destroy()206 void FloatType::Destroy() {
207 if (instance_ != nullptr) {
208 delete instance_;
209 instance_ = nullptr;
210 }
211 }
212
CreateInstance(ObjPtr<mirror::Class> klass,const std::string_view & descriptor,uint16_t cache_id)213 const CharType* CharType::CreateInstance(ObjPtr<mirror::Class> klass,
214 const std::string_view& descriptor,
215 uint16_t cache_id) {
216 CHECK(instance_ == nullptr);
217 instance_ = new CharType(klass, descriptor, cache_id);
218 return instance_;
219 }
220
Destroy()221 void CharType::Destroy() {
222 if (instance_ != nullptr) {
223 delete instance_;
224 instance_ = nullptr;
225 }
226 }
227
CreateInstance(ObjPtr<mirror::Class> klass,const std::string_view & descriptor,uint16_t cache_id)228 const ShortType* ShortType::CreateInstance(ObjPtr<mirror::Class> klass,
229 const std::string_view& descriptor,
230 uint16_t cache_id) {
231 CHECK(instance_ == nullptr);
232 instance_ = new ShortType(klass, descriptor, cache_id);
233 return instance_;
234 }
235
Destroy()236 void ShortType::Destroy() {
237 if (instance_ != nullptr) {
238 delete instance_;
239 instance_ = nullptr;
240 }
241 }
242
CreateInstance(ObjPtr<mirror::Class> klass,const std::string_view & descriptor,uint16_t cache_id)243 const ByteType* ByteType::CreateInstance(ObjPtr<mirror::Class> klass,
244 const std::string_view& descriptor,
245 uint16_t cache_id) {
246 CHECK(instance_ == nullptr);
247 instance_ = new ByteType(klass, descriptor, cache_id);
248 return instance_;
249 }
250
Destroy()251 void ByteType::Destroy() {
252 if (instance_ != nullptr) {
253 delete instance_;
254 instance_ = nullptr;
255 }
256 }
257
CreateInstance(ObjPtr<mirror::Class> klass,const std::string_view & descriptor,uint16_t cache_id)258 const IntegerType* IntegerType::CreateInstance(ObjPtr<mirror::Class> klass,
259 const std::string_view& descriptor,
260 uint16_t cache_id) {
261 CHECK(instance_ == nullptr);
262 instance_ = new IntegerType(klass, descriptor, cache_id);
263 return instance_;
264 }
265
Destroy()266 void IntegerType::Destroy() {
267 if (instance_ != nullptr) {
268 delete instance_;
269 instance_ = nullptr;
270 }
271 }
272
CreateInstance(ObjPtr<mirror::Class> klass,const std::string_view & descriptor,uint16_t cache_id)273 const ConflictType* ConflictType::CreateInstance(ObjPtr<mirror::Class> klass,
274 const std::string_view& descriptor,
275 uint16_t cache_id) {
276 CHECK(instance_ == nullptr);
277 instance_ = new ConflictType(klass, descriptor, cache_id);
278 return instance_;
279 }
280
Destroy()281 void ConflictType::Destroy() {
282 if (instance_ != nullptr) {
283 delete instance_;
284 instance_ = nullptr;
285 }
286 }
287
CreateInstance(ObjPtr<mirror::Class> klass,const std::string_view & descriptor,uint16_t cache_id)288 const BooleanType* BooleanType::CreateInstance(ObjPtr<mirror::Class> klass,
289 const std::string_view& descriptor,
290 uint16_t cache_id) {
291 CHECK(BooleanType::instance_ == nullptr);
292 instance_ = new BooleanType(klass, descriptor, cache_id);
293 return BooleanType::instance_;
294 }
295
Destroy()296 void BooleanType::Destroy() {
297 if (BooleanType::instance_ != nullptr) {
298 delete instance_;
299 instance_ = nullptr;
300 }
301 }
302
Dump() const303 std::string UndefinedType::Dump() const REQUIRES_SHARED(Locks::mutator_lock_) {
304 return "Undefined";
305 }
306
CreateInstance(ObjPtr<mirror::Class> klass,const std::string_view & descriptor,uint16_t cache_id)307 const UndefinedType* UndefinedType::CreateInstance(ObjPtr<mirror::Class> klass,
308 const std::string_view& descriptor,
309 uint16_t cache_id) {
310 CHECK(instance_ == nullptr);
311 instance_ = new UndefinedType(klass, descriptor, cache_id);
312 return instance_;
313 }
314
Destroy()315 void UndefinedType::Destroy() {
316 if (instance_ != nullptr) {
317 delete instance_;
318 instance_ = nullptr;
319 }
320 }
321
PreciseReferenceType(ObjPtr<mirror::Class> klass,const std::string_view & descriptor,uint16_t cache_id)322 PreciseReferenceType::PreciseReferenceType(ObjPtr<mirror::Class> klass,
323 const std::string_view& descriptor,
324 uint16_t cache_id)
325 : RegType(klass, descriptor, cache_id) {
326 // Note: no check for IsInstantiable() here. We may produce this in case an InstantiationError
327 // would be thrown at runtime, but we need to continue verification and *not* create a
328 // hard failure or abort.
329 CheckConstructorInvariants(this);
330 }
331
Dump() const332 std::string UnresolvedMergedType::Dump() const {
333 std::stringstream result;
334 result << "UnresolvedMergedReferences(" << GetResolvedPart().Dump() << " | ";
335 const BitVector& types = GetUnresolvedTypes();
336
337 bool first = true;
338 for (uint32_t idx : types.Indexes()) {
339 if (!first) {
340 result << ", ";
341 } else {
342 first = false;
343 }
344 result << reg_type_cache_->GetFromId(idx).Dump();
345 }
346 result << ")";
347 return result.str();
348 }
349
Dump() const350 std::string UnresolvedSuperClass::Dump() const {
351 std::stringstream result;
352 uint16_t super_type_id = GetUnresolvedSuperClassChildId();
353 result << "UnresolvedSuperClass(" << reg_type_cache_->GetFromId(super_type_id).Dump() << ")";
354 return result.str();
355 }
356
Dump() const357 std::string UnresolvedReferenceType::Dump() const {
358 std::stringstream result;
359 result << "Unresolved Reference: " << PrettyDescriptor(std::string(GetDescriptor()).c_str());
360 return result.str();
361 }
362
Dump() const363 std::string UnresolvedUninitializedRefType::Dump() const {
364 std::stringstream result;
365 result << "Unresolved And Uninitialized Reference: "
366 << PrettyDescriptor(std::string(GetDescriptor()).c_str())
367 << " Allocation PC: " << GetAllocationPc();
368 return result.str();
369 }
370
Dump() const371 std::string UnresolvedUninitializedThisRefType::Dump() const {
372 std::stringstream result;
373 result << "Unresolved And Uninitialized This Reference: "
374 << PrettyDescriptor(std::string(GetDescriptor()).c_str());
375 return result.str();
376 }
377
Dump() const378 std::string ReferenceType::Dump() const {
379 std::stringstream result;
380 result << "Reference: " << mirror::Class::PrettyDescriptor(GetClass());
381 return result.str();
382 }
383
Dump() const384 std::string PreciseReferenceType::Dump() const {
385 std::stringstream result;
386 result << "Precise Reference: " << mirror::Class::PrettyDescriptor(GetClass());
387 return result.str();
388 }
389
Dump() const390 std::string UninitializedReferenceType::Dump() const {
391 std::stringstream result;
392 result << "Uninitialized Reference: " << mirror::Class::PrettyDescriptor(GetClass());
393 result << " Allocation PC: " << GetAllocationPc();
394 return result.str();
395 }
396
Dump() const397 std::string UninitializedThisReferenceType::Dump() const {
398 std::stringstream result;
399 result << "Uninitialized This Reference: " << mirror::Class::PrettyDescriptor(GetClass());
400 result << "Allocation PC: " << GetAllocationPc();
401 return result.str();
402 }
403
Dump() const404 std::string ImpreciseConstType::Dump() const {
405 std::stringstream result;
406 uint32_t val = ConstantValue();
407 if (val == 0) {
408 result << "Zero/null";
409 } else {
410 result << "Imprecise ";
411 if (IsConstantShort()) {
412 result << StringPrintf("Constant: %d", val);
413 } else {
414 result << StringPrintf("Constant: 0x%x", val);
415 }
416 }
417 return result.str();
418 }
Dump() const419 std::string PreciseConstLoType::Dump() const {
420 std::stringstream result;
421
422 int32_t val = ConstantValueLo();
423 result << "Precise ";
424 if (val >= std::numeric_limits<jshort>::min() &&
425 val <= std::numeric_limits<jshort>::max()) {
426 result << StringPrintf("Low-half Constant: %d", val);
427 } else {
428 result << StringPrintf("Low-half Constant: 0x%x", val);
429 }
430 return result.str();
431 }
432
Dump() const433 std::string ImpreciseConstLoType::Dump() const {
434 std::stringstream result;
435
436 int32_t val = ConstantValueLo();
437 result << "Imprecise ";
438 if (val >= std::numeric_limits<jshort>::min() &&
439 val <= std::numeric_limits<jshort>::max()) {
440 result << StringPrintf("Low-half Constant: %d", val);
441 } else {
442 result << StringPrintf("Low-half Constant: 0x%x", val);
443 }
444 return result.str();
445 }
446
Dump() const447 std::string PreciseConstHiType::Dump() const {
448 std::stringstream result;
449 int32_t val = ConstantValueHi();
450 result << "Precise ";
451 if (val >= std::numeric_limits<jshort>::min() &&
452 val <= std::numeric_limits<jshort>::max()) {
453 result << StringPrintf("High-half Constant: %d", val);
454 } else {
455 result << StringPrintf("High-half Constant: 0x%x", val);
456 }
457 return result.str();
458 }
459
Dump() const460 std::string ImpreciseConstHiType::Dump() const {
461 std::stringstream result;
462 int32_t val = ConstantValueHi();
463 result << "Imprecise ";
464 if (val >= std::numeric_limits<jshort>::min() &&
465 val <= std::numeric_limits<jshort>::max()) {
466 result << StringPrintf("High-half Constant: %d", val);
467 } else {
468 result << StringPrintf("High-half Constant: 0x%x", val);
469 }
470 return result.str();
471 }
472
HighHalf(RegTypeCache * cache) const473 const RegType& RegType::HighHalf(RegTypeCache* cache) const {
474 DCHECK(IsLowHalf());
475 if (IsLongLo()) {
476 return cache->LongHi();
477 } else if (IsDoubleLo()) {
478 return cache->DoubleHi();
479 } else {
480 DCHECK(IsImpreciseConstantLo());
481 const ConstantType* const_val = down_cast<const ConstantType*>(this);
482 return cache->FromCat2ConstHi(const_val->ConstantValue(), false);
483 }
484 }
485
GetPrimitiveType() const486 Primitive::Type RegType::GetPrimitiveType() const {
487 if (IsNonZeroReferenceTypes()) {
488 return Primitive::kPrimNot;
489 } else if (IsBooleanTypes()) {
490 return Primitive::kPrimBoolean;
491 } else if (IsByteTypes()) {
492 return Primitive::kPrimByte;
493 } else if (IsShortTypes()) {
494 return Primitive::kPrimShort;
495 } else if (IsCharTypes()) {
496 return Primitive::kPrimChar;
497 } else if (IsFloat()) {
498 return Primitive::kPrimFloat;
499 } else if (IsIntegralTypes()) {
500 return Primitive::kPrimInt;
501 } else if (IsDoubleLo()) {
502 return Primitive::kPrimDouble;
503 } else {
504 DCHECK(IsLongTypes());
505 return Primitive::kPrimLong;
506 }
507 }
508
IsUninitializedTypes() const509 bool UninitializedType::IsUninitializedTypes() const {
510 return true;
511 }
512
IsNonZeroReferenceTypes() const513 bool UninitializedType::IsNonZeroReferenceTypes() const {
514 return true;
515 }
516
IsNonZeroReferenceTypes() const517 bool UnresolvedType::IsNonZeroReferenceTypes() const {
518 return true;
519 }
520
GetSuperClass(RegTypeCache * cache) const521 const RegType& RegType::GetSuperClass(RegTypeCache* cache) const {
522 if (!IsUnresolvedTypes()) {
523 ObjPtr<mirror::Class> super_klass = GetClass()->GetSuperClass();
524 if (super_klass != nullptr) {
525 // A super class of a precise type isn't precise as a precise type indicates the register
526 // holds exactly that type.
527 std::string temp;
528 return cache->FromClass(super_klass->GetDescriptor(&temp), super_klass, false);
529 } else {
530 return cache->Zero();
531 }
532 } else {
533 if (!IsUnresolvedMergedReference() && !IsUnresolvedSuperClass() &&
534 GetDescriptor()[0] == '[') {
535 // Super class of all arrays is Object.
536 return cache->JavaLangObject(true);
537 } else {
538 return cache->FromUnresolvedSuperClass(*this);
539 }
540 }
541 }
542
IsJavaLangObject() const543 bool RegType::IsJavaLangObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
544 return IsReference() && GetClass()->IsObjectClass();
545 }
546
IsObjectArrayTypes() const547 bool RegType::IsObjectArrayTypes() const REQUIRES_SHARED(Locks::mutator_lock_) {
548 if (IsUnresolvedTypes()) {
549 DCHECK(!IsUnresolvedMergedReference());
550
551 if (IsUnresolvedSuperClass()) {
552 // Cannot be an array, as the superclass of arrays is java.lang.Object (which cannot be
553 // unresolved).
554 return false;
555 }
556
557 // Primitive arrays will always resolve.
558 DCHECK(descriptor_[1] == 'L' || descriptor_[1] == '[');
559 return descriptor_[0] == '[';
560 } else if (HasClass()) {
561 ObjPtr<mirror::Class> type = GetClass();
562 return type->IsArrayClass() && !type->GetComponentType()->IsPrimitive();
563 } else {
564 return false;
565 }
566 }
567
IsArrayTypes() const568 bool RegType::IsArrayTypes() const REQUIRES_SHARED(Locks::mutator_lock_) {
569 if (IsUnresolvedTypes()) {
570 DCHECK(!IsUnresolvedMergedReference());
571
572 if (IsUnresolvedSuperClass()) {
573 // Cannot be an array, as the superclass of arrays is java.lang.Object (which cannot be
574 // unresolved).
575 return false;
576 }
577 return descriptor_[0] == '[';
578 } else if (HasClass()) {
579 return GetClass()->IsArrayClass();
580 } else {
581 return false;
582 }
583 }
584
IsJavaLangObjectArray() const585 bool RegType::IsJavaLangObjectArray() const {
586 if (HasClass()) {
587 ObjPtr<mirror::Class> type = GetClass();
588 return type->IsArrayClass() && type->GetComponentType()->IsObjectClass();
589 }
590 return false;
591 }
592
IsInstantiableTypes() const593 bool RegType::IsInstantiableTypes() const {
594 return IsUnresolvedTypes() || (IsNonZeroReferenceTypes() && GetClass()->IsInstantiable());
595 }
596
SelectNonConstant(const RegType & a,const RegType & b)597 static const RegType& SelectNonConstant(const RegType& a, const RegType& b) {
598 return a.IsConstantTypes() ? b : a;
599 }
600
SelectNonConstant2(const RegType & a,const RegType & b)601 static const RegType& SelectNonConstant2(const RegType& a, const RegType& b) {
602 return a.IsConstantTypes() ? (b.IsZero() ? a : b) : a;
603 }
604
605
606 namespace {
607
608 ObjPtr<mirror::Class> ArrayClassJoin(ObjPtr<mirror::Class> s,
609 ObjPtr<mirror::Class> t,
610 ClassLinker* class_linker)
611 REQUIRES_SHARED(Locks::mutator_lock_);
612
613 ObjPtr<mirror::Class> InterfaceClassJoin(ObjPtr<mirror::Class> s, ObjPtr<mirror::Class> t)
614 REQUIRES_SHARED(Locks::mutator_lock_);
615
616 /*
617 * A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is
618 * S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of
619 * S and T such that there isn't a parent of both S and T that isn't also the parent of J (ie J
620 * is the deepest (lowest upper bound) parent of S and T).
621 *
622 * This operation applies for regular classes and arrays, however, for interface types there
623 * needn't be a partial ordering on the types. We could solve the problem of a lack of a partial
624 * order by introducing sets of types, however, the only operation permissible on an interface is
625 * invoke-interface. In the tradition of Java verifiers [1] we defer the verification of interface
626 * types until an invoke-interface call on the interface typed reference at runtime and allow
627 * the perversion of Object being assignable to an interface type (note, however, that we don't
628 * allow assignment of Object or Interface to any concrete class and are therefore type safe).
629 *
630 * Note: This may return null in case of internal errors, e.g., OOME when a new class would have
631 * to be created but there is no heap space. The exception will stay pending, and it is
632 * the job of the caller to handle it.
633 *
634 * [1] Java bytecode verification: algorithms and formalizations, Xavier Leroy
635 */
ClassJoin(ObjPtr<mirror::Class> s,ObjPtr<mirror::Class> t,ClassLinker * class_linker)636 ObjPtr<mirror::Class> ClassJoin(ObjPtr<mirror::Class> s,
637 ObjPtr<mirror::Class> t,
638 ClassLinker* class_linker)
639 REQUIRES_SHARED(Locks::mutator_lock_) {
640 DCHECK(!s->IsPrimitive()) << s->PrettyClass();
641 DCHECK(!t->IsPrimitive()) << t->PrettyClass();
642 if (s == t) {
643 return s;
644 } else if (s->IsAssignableFrom(t)) {
645 return s;
646 } else if (t->IsAssignableFrom(s)) {
647 return t;
648 } else if (s->IsArrayClass() && t->IsArrayClass()) {
649 return ArrayClassJoin(s, t, class_linker);
650 } else if (s->IsInterface() || t->IsInterface()) {
651 return InterfaceClassJoin(s, t);
652 } else {
653 size_t s_depth = s->Depth();
654 size_t t_depth = t->Depth();
655 // Get s and t to the same depth in the hierarchy
656 if (s_depth > t_depth) {
657 while (s_depth > t_depth) {
658 s = s->GetSuperClass();
659 s_depth--;
660 }
661 } else {
662 while (t_depth > s_depth) {
663 t = t->GetSuperClass();
664 t_depth--;
665 }
666 }
667 // Go up the hierarchy until we get to the common parent
668 while (s != t) {
669 s = s->GetSuperClass();
670 t = t->GetSuperClass();
671 }
672 return s;
673 }
674 }
675
ArrayClassJoin(ObjPtr<mirror::Class> s,ObjPtr<mirror::Class> t,ClassLinker * class_linker)676 ObjPtr<mirror::Class> ArrayClassJoin(ObjPtr<mirror::Class> s,
677 ObjPtr<mirror::Class> t,
678 ClassLinker* class_linker) {
679 ObjPtr<mirror::Class> s_ct = s->GetComponentType();
680 ObjPtr<mirror::Class> t_ct = t->GetComponentType();
681 if (s_ct->IsPrimitive() || t_ct->IsPrimitive()) {
682 // Given the types aren't the same, if either array is of primitive types then the only
683 // common parent is java.lang.Object
684 ObjPtr<mirror::Class> result = s->GetSuperClass(); // short-cut to java.lang.Object
685 DCHECK(result->IsObjectClass());
686 return result;
687 }
688 Thread* self = Thread::Current();
689 ObjPtr<mirror::Class> common_elem = ClassJoin(s_ct, t_ct, class_linker);
690 if (UNLIKELY(common_elem == nullptr)) {
691 self->AssertPendingException();
692 return nullptr;
693 }
694 // Note: The following lookup invalidates existing ObjPtr<>s.
695 ObjPtr<mirror::Class> array_class = class_linker->FindArrayClass(self, common_elem);
696 if (UNLIKELY(array_class == nullptr)) {
697 self->AssertPendingException();
698 return nullptr;
699 }
700 return array_class;
701 }
702
InterfaceClassJoin(ObjPtr<mirror::Class> s,ObjPtr<mirror::Class> t)703 ObjPtr<mirror::Class> InterfaceClassJoin(ObjPtr<mirror::Class> s, ObjPtr<mirror::Class> t) {
704 // This is expensive, as we do not have good data structures to do this even halfway
705 // efficiently.
706 //
707 // We're not following JVMS for interface verification (not everything is assignable to an
708 // interface, we trade this for IMT dispatch). We also don't have set types to make up for
709 // it. So we choose one arbitrary common ancestor interface by walking the interface tables
710 // backwards.
711 //
712 // For comparison, runtimes following the JVMS will punt all interface type checking to
713 // runtime.
714 ObjPtr<mirror::IfTable> s_if = s->GetIfTable();
715 int32_t s_if_count = s->GetIfTableCount();
716 ObjPtr<mirror::IfTable> t_if = t->GetIfTable();
717 int32_t t_if_count = t->GetIfTableCount();
718
719 // Note: we'll be using index == count to stand for the argument itself.
720 for (int32_t s_it = s_if_count; s_it >= 0; --s_it) {
721 ObjPtr<mirror::Class> s_cl = s_it == s_if_count ? s : s_if->GetInterface(s_it);
722 if (!s_cl->IsInterface()) {
723 continue;
724 }
725
726 for (int32_t t_it = t_if_count; t_it >= 0; --t_it) {
727 ObjPtr<mirror::Class> t_cl = t_it == t_if_count ? t : t_if->GetInterface(t_it);
728 if (!t_cl->IsInterface()) {
729 continue;
730 }
731
732 if (s_cl == t_cl) {
733 // Found something arbitrary in common.
734 return s_cl;
735 }
736 }
737 }
738
739 // Return java.lang.Object.
740 ObjPtr<mirror::Class> obj_class = s->IsInterface() ? s->GetSuperClass() : t->GetSuperClass();
741 DCHECK(obj_class->IsObjectClass());
742 return obj_class;
743 }
744
745 } // namespace
746
Merge(const RegType & incoming_type,RegTypeCache * reg_types,MethodVerifier * verifier) const747 const RegType& RegType::Merge(const RegType& incoming_type,
748 RegTypeCache* reg_types,
749 MethodVerifier* verifier) const {
750 DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller
751 // Perform pointer equality tests for undefined and conflict to avoid virtual method dispatch.
752 const UndefinedType& undefined = reg_types->Undefined();
753 const ConflictType& conflict = reg_types->Conflict();
754 DCHECK_EQ(this == &undefined, IsUndefined());
755 DCHECK_EQ(&incoming_type == &undefined, incoming_type.IsUndefined());
756 DCHECK_EQ(this == &conflict, IsConflict());
757 DCHECK_EQ(&incoming_type == &conflict, incoming_type.IsConflict());
758 if (this == &undefined || &incoming_type == &undefined) {
759 // There is a difference between undefined and conflict. Conflicts may be copied around, but
760 // not used. Undefined registers must not be copied. So any merge with undefined should return
761 // undefined.
762 return undefined;
763 } else if (this == &conflict || &incoming_type == &conflict) {
764 return conflict; // (Conflict MERGE *) or (* MERGE Conflict) => Conflict
765 } else if (IsConstant() && incoming_type.IsConstant()) {
766 const ConstantType& type1 = *down_cast<const ConstantType*>(this);
767 const ConstantType& type2 = *down_cast<const ConstantType*>(&incoming_type);
768 int32_t val1 = type1.ConstantValue();
769 int32_t val2 = type2.ConstantValue();
770 if (val1 >= 0 && val2 >= 0) {
771 // +ve1 MERGE +ve2 => MAX(+ve1, +ve2)
772 if (val1 >= val2) {
773 if (!type1.IsPreciseConstant()) {
774 return *this;
775 } else {
776 return reg_types->FromCat1Const(val1, false);
777 }
778 } else {
779 if (!type2.IsPreciseConstant()) {
780 return type2;
781 } else {
782 return reg_types->FromCat1Const(val2, false);
783 }
784 }
785 } else if (val1 < 0 && val2 < 0) {
786 // -ve1 MERGE -ve2 => MIN(-ve1, -ve2)
787 if (val1 <= val2) {
788 if (!type1.IsPreciseConstant()) {
789 return *this;
790 } else {
791 return reg_types->FromCat1Const(val1, false);
792 }
793 } else {
794 if (!type2.IsPreciseConstant()) {
795 return type2;
796 } else {
797 return reg_types->FromCat1Const(val2, false);
798 }
799 }
800 } else {
801 // Values are +ve and -ve, choose smallest signed type in which they both fit
802 if (type1.IsConstantByte()) {
803 if (type2.IsConstantByte()) {
804 return reg_types->ByteConstant();
805 } else if (type2.IsConstantShort()) {
806 return reg_types->ShortConstant();
807 } else {
808 return reg_types->IntConstant();
809 }
810 } else if (type1.IsConstantShort()) {
811 if (type2.IsConstantShort()) {
812 return reg_types->ShortConstant();
813 } else {
814 return reg_types->IntConstant();
815 }
816 } else {
817 return reg_types->IntConstant();
818 }
819 }
820 } else if (IsConstantLo() && incoming_type.IsConstantLo()) {
821 const ConstantType& type1 = *down_cast<const ConstantType*>(this);
822 const ConstantType& type2 = *down_cast<const ConstantType*>(&incoming_type);
823 int32_t val1 = type1.ConstantValueLo();
824 int32_t val2 = type2.ConstantValueLo();
825 return reg_types->FromCat2ConstLo(val1 | val2, false);
826 } else if (IsConstantHi() && incoming_type.IsConstantHi()) {
827 const ConstantType& type1 = *down_cast<const ConstantType*>(this);
828 const ConstantType& type2 = *down_cast<const ConstantType*>(&incoming_type);
829 int32_t val1 = type1.ConstantValueHi();
830 int32_t val2 = type2.ConstantValueHi();
831 return reg_types->FromCat2ConstHi(val1 | val2, false);
832 } else if (IsIntegralTypes() && incoming_type.IsIntegralTypes()) {
833 if (IsBooleanTypes() && incoming_type.IsBooleanTypes()) {
834 return reg_types->Boolean(); // boolean MERGE boolean => boolean
835 }
836 if (IsByteTypes() && incoming_type.IsByteTypes()) {
837 return reg_types->Byte(); // byte MERGE byte => byte
838 }
839 if (IsShortTypes() && incoming_type.IsShortTypes()) {
840 return reg_types->Short(); // short MERGE short => short
841 }
842 if (IsCharTypes() && incoming_type.IsCharTypes()) {
843 return reg_types->Char(); // char MERGE char => char
844 }
845 return reg_types->Integer(); // int MERGE * => int
846 } else if ((IsFloatTypes() && incoming_type.IsFloatTypes()) ||
847 (IsLongTypes() && incoming_type.IsLongTypes()) ||
848 (IsLongHighTypes() && incoming_type.IsLongHighTypes()) ||
849 (IsDoubleTypes() && incoming_type.IsDoubleTypes()) ||
850 (IsDoubleHighTypes() && incoming_type.IsDoubleHighTypes())) {
851 // check constant case was handled prior to entry
852 DCHECK(!IsConstant() || !incoming_type.IsConstant());
853 // float/long/double MERGE float/long/double_constant => float/long/double
854 return SelectNonConstant(*this, incoming_type);
855 } else if (IsReferenceTypes() && incoming_type.IsReferenceTypes()) {
856 if (IsUninitializedTypes() || incoming_type.IsUninitializedTypes()) {
857 // Something that is uninitialized hasn't had its constructor called. Unitialized types are
858 // special. They may only ever be merged with themselves (must be taken care of by the
859 // caller of Merge(), see the DCHECK on entry). So mark any other merge as conflicting here.
860 return conflict;
861 } else if (IsZeroOrNull() || incoming_type.IsZeroOrNull()) {
862 return SelectNonConstant2(*this, incoming_type); // 0 MERGE ref => ref
863 } else if (IsJavaLangObject() || incoming_type.IsJavaLangObject()) {
864 return reg_types->JavaLangObject(false); // Object MERGE ref => Object
865 } else if (IsUnresolvedTypes() || incoming_type.IsUnresolvedTypes()) {
866 // We know how to merge an unresolved type with itself, 0 or Object. In this case we
867 // have two sub-classes and don't know how to merge. Create a new string-based unresolved
868 // type that reflects our lack of knowledge and that allows the rest of the unresolved
869 // mechanics to continue.
870 return reg_types->FromUnresolvedMerge(*this, incoming_type, verifier);
871 } else { // Two reference types, compute Join
872 // Do not cache the classes as ClassJoin() can suspend and invalidate ObjPtr<>s.
873 DCHECK(GetClass() != nullptr && !GetClass()->IsPrimitive());
874 DCHECK(incoming_type.GetClass() != nullptr && !incoming_type.GetClass()->IsPrimitive());
875 ObjPtr<mirror::Class> join_class = ClassJoin(GetClass(),
876 incoming_type.GetClass(),
877 reg_types->GetClassLinker());
878 if (UNLIKELY(join_class == nullptr)) {
879 // Internal error joining the classes (e.g., OOME). Report an unresolved reference type.
880 // We cannot report an unresolved merge type, as that will attempt to merge the resolved
881 // components, leaving us in an infinite loop.
882 // We do not want to report the originating exception, as that would require a fast path
883 // out all the way to VerifyClass. Instead attempt to continue on without a detailed type.
884 Thread* self = Thread::Current();
885 self->AssertPendingException();
886 self->ClearException();
887
888 // When compiling on the host, we rather want to abort to ensure determinism for preopting.
889 // (In that case, it is likely a misconfiguration of dex2oat.)
890 if (!kIsTargetBuild && (verifier != nullptr && verifier->IsAotMode())) {
891 LOG(FATAL) << "Could not create class join of "
892 << GetClass()->PrettyClass()
893 << " & "
894 << incoming_type.GetClass()->PrettyClass();
895 UNREACHABLE();
896 }
897
898 return reg_types->MakeUnresolvedReference();
899 }
900
901 // Record the dependency that both `GetClass()` and `incoming_type.GetClass()`
902 // are assignable to `join_class`. The `verifier` is null during unit tests.
903 if (verifier != nullptr) {
904 VerifierDeps::MaybeRecordAssignability(verifier->GetDexFile(),
905 join_class,
906 GetClass(),
907 /* is_strict= */ true,
908 /* is_assignable= */ true);
909 VerifierDeps::MaybeRecordAssignability(verifier->GetDexFile(),
910 join_class,
911 incoming_type.GetClass(),
912 /* is_strict= */ true,
913 /* is_assignable= */ true);
914 }
915 if (GetClass() == join_class && !IsPreciseReference()) {
916 return *this;
917 } else if (incoming_type.GetClass() == join_class && !incoming_type.IsPreciseReference()) {
918 return incoming_type;
919 } else {
920 std::string temp;
921 const char* descriptor = join_class->GetDescriptor(&temp);
922 return reg_types->FromClass(descriptor, join_class, /* precise= */ false);
923 }
924 }
925 } else {
926 return conflict; // Unexpected types => Conflict
927 }
928 }
929
CheckInvariants() const930 void RegType::CheckInvariants() const {
931 if (IsConstant() || IsConstantLo() || IsConstantHi()) {
932 CHECK(descriptor_.empty()) << *this;
933 CHECK(klass_.IsNull()) << *this;
934 }
935 if (!klass_.IsNull()) {
936 CHECK(!descriptor_.empty()) << *this;
937 std::string temp;
938 CHECK_EQ(descriptor_, klass_.Read()->GetDescriptor(&temp)) << *this;
939 }
940 }
941
VisitRoots(RootVisitor * visitor,const RootInfo & root_info) const942 void RegType::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) const {
943 klass_.VisitRootIfNonNull(visitor, root_info);
944 }
945
CheckInvariants() const946 void UninitializedThisReferenceType::CheckInvariants() const {
947 CHECK_EQ(GetAllocationPc(), 0U) << *this;
948 }
949
CheckInvariants() const950 void UnresolvedUninitializedThisRefType::CheckInvariants() const {
951 CHECK_EQ(GetAllocationPc(), 0U) << *this;
952 CHECK(!descriptor_.empty()) << *this;
953 CHECK(klass_.IsNull()) << *this;
954 }
955
CheckInvariants() const956 void UnresolvedUninitializedRefType::CheckInvariants() const {
957 CHECK(!descriptor_.empty()) << *this;
958 CHECK(klass_.IsNull()) << *this;
959 }
960
UnresolvedMergedType(const RegType & resolved,const BitVector & unresolved,const RegTypeCache * reg_type_cache,uint16_t cache_id)961 UnresolvedMergedType::UnresolvedMergedType(const RegType& resolved,
962 const BitVector& unresolved,
963 const RegTypeCache* reg_type_cache,
964 uint16_t cache_id)
965 : UnresolvedType("", cache_id),
966 reg_type_cache_(reg_type_cache),
967 resolved_part_(resolved),
968 unresolved_types_(unresolved, false, unresolved.GetAllocator()) {
969 CheckConstructorInvariants(this);
970 }
CheckInvariants() const971 void UnresolvedMergedType::CheckInvariants() const {
972 CHECK(reg_type_cache_ != nullptr);
973
974 // Unresolved merged types: merged types should be defined.
975 CHECK(descriptor_.empty()) << *this;
976 CHECK(klass_.IsNull()) << *this;
977
978 CHECK(!resolved_part_.IsConflict());
979 CHECK(resolved_part_.IsReferenceTypes());
980 CHECK(!resolved_part_.IsUnresolvedTypes());
981
982 CHECK(resolved_part_.IsZero() ||
983 !(resolved_part_.IsArrayTypes() && !resolved_part_.IsObjectArrayTypes()));
984
985 CHECK_GT(unresolved_types_.NumSetBits(), 0U);
986 bool unresolved_is_array =
987 reg_type_cache_->GetFromId(unresolved_types_.GetHighestBitSet()).IsArrayTypes();
988 for (uint32_t idx : unresolved_types_.Indexes()) {
989 const RegType& t = reg_type_cache_->GetFromId(idx);
990 CHECK_EQ(unresolved_is_array, t.IsArrayTypes());
991 }
992
993 if (!resolved_part_.IsZero()) {
994 CHECK_EQ(resolved_part_.IsArrayTypes(), unresolved_is_array);
995 }
996 }
997
IsArrayTypes() const998 bool UnresolvedMergedType::IsArrayTypes() const {
999 // For a merge to be an array, both the resolved and the unresolved part need to be object
1000 // arrays.
1001 // (Note: we encode a missing resolved part [which doesn't need to be an array] as zero.)
1002
1003 if (!resolved_part_.IsZero() && !resolved_part_.IsArrayTypes()) {
1004 return false;
1005 }
1006
1007 // It is enough to check just one of the merged types. Otherwise the merge should have been
1008 // collapsed (checked in CheckInvariants on construction).
1009 uint32_t idx = unresolved_types_.GetHighestBitSet();
1010 const RegType& unresolved = reg_type_cache_->GetFromId(idx);
1011 return unresolved.IsArrayTypes();
1012 }
IsObjectArrayTypes() const1013 bool UnresolvedMergedType::IsObjectArrayTypes() const {
1014 // Same as IsArrayTypes, as primitive arrays are always resolved.
1015 return IsArrayTypes();
1016 }
1017
CheckInvariants() const1018 void UnresolvedReferenceType::CheckInvariants() const {
1019 CHECK(!descriptor_.empty()) << *this;
1020 CHECK(klass_.IsNull()) << *this;
1021 }
1022
CheckInvariants() const1023 void UnresolvedSuperClass::CheckInvariants() const {
1024 // Unresolved merged types: merged types should be defined.
1025 CHECK(descriptor_.empty()) << *this;
1026 CHECK(klass_.IsNull()) << *this;
1027 CHECK_NE(unresolved_child_id_, 0U) << *this;
1028 }
1029
operator <<(std::ostream & os,const RegType & rhs)1030 std::ostream& operator<<(std::ostream& os, const RegType& rhs) {
1031 os << rhs.Dump();
1032 return os;
1033 }
1034
CanAssignArray(const RegType & src,RegTypeCache & reg_types,Handle<mirror::ClassLoader> class_loader,MethodVerifier * verifier,bool * soft_error) const1035 bool RegType::CanAssignArray(const RegType& src,
1036 RegTypeCache& reg_types,
1037 Handle<mirror::ClassLoader> class_loader,
1038 MethodVerifier* verifier,
1039 bool* soft_error) const {
1040 if (!IsArrayTypes() || !src.IsArrayTypes()) {
1041 *soft_error = false;
1042 return false;
1043 }
1044
1045 if (IsUnresolvedMergedReference() || src.IsUnresolvedMergedReference()) {
1046 // An unresolved array type means that it's an array of some reference type. Reference arrays
1047 // can never be assigned to primitive-type arrays, and vice versa. So it is a soft error if
1048 // both arrays are reference arrays, otherwise a hard error.
1049 *soft_error = IsObjectArrayTypes() && src.IsObjectArrayTypes();
1050 return false;
1051 }
1052
1053 const RegType& cmp1 = reg_types.GetComponentType(*this, class_loader.Get());
1054 const RegType& cmp2 = reg_types.GetComponentType(src, class_loader.Get());
1055
1056 if (cmp1.IsAssignableFrom(cmp2, verifier)) {
1057 return true;
1058 }
1059 if (cmp1.IsUnresolvedTypes()) {
1060 if (cmp2.IsIntegralTypes() || cmp2.IsFloatTypes() || cmp2.IsArrayTypes()) {
1061 *soft_error = false;
1062 return false;
1063 }
1064 *soft_error = true;
1065 return false;
1066 }
1067 if (cmp2.IsUnresolvedTypes()) {
1068 if (cmp1.IsIntegralTypes() || cmp1.IsFloatTypes() || cmp1.IsArrayTypes()) {
1069 *soft_error = false;
1070 return false;
1071 }
1072 *soft_error = true;
1073 return false;
1074 }
1075 if (!cmp1.IsArrayTypes() || !cmp2.IsArrayTypes()) {
1076 *soft_error = false;
1077 return false;
1078 }
1079 return cmp1.CanAssignArray(cmp2, reg_types, class_loader, verifier, soft_error);
1080 }
1081
CreateInstance(ObjPtr<mirror::Class> klass,const std::string_view & descriptor,uint16_t cache_id)1082 const NullType* NullType::CreateInstance(ObjPtr<mirror::Class> klass,
1083 const std::string_view& descriptor,
1084 uint16_t cache_id) {
1085 CHECK(instance_ == nullptr);
1086 instance_ = new NullType(klass, descriptor, cache_id);
1087 return instance_;
1088 }
1089
Destroy()1090 void NullType::Destroy() {
1091 if (NullType::instance_ != nullptr) {
1092 delete instance_;
1093 instance_ = nullptr;
1094 }
1095 }
1096
1097
1098 } // namespace verifier
1099 } // namespace art
1100