1 /*
2  * Copyright (C) 2014 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 "verified_method.h"
18 
19 #include <algorithm>
20 #include <memory>
21 
22 #include <android-base/logging.h>
23 
24 #include "dex/code_item_accessors-inl.h"
25 #include "dex/dex_file.h"
26 #include "dex/dex_instruction-inl.h"
27 #include "runtime.h"
28 #include "verifier/method_verifier-inl.h"
29 #include "verifier/reg_type-inl.h"
30 #include "verifier/register_line-inl.h"
31 #include "verifier/verifier_deps.h"
32 
33 namespace art {
34 
VerifiedMethod(uint32_t encountered_error_types,bool has_runtime_throw)35 VerifiedMethod::VerifiedMethod(uint32_t encountered_error_types, bool has_runtime_throw)
36     : encountered_error_types_(encountered_error_types),
37       has_runtime_throw_(has_runtime_throw) {
38 }
39 
Create(verifier::MethodVerifier * method_verifier)40 const VerifiedMethod* VerifiedMethod::Create(verifier::MethodVerifier* method_verifier) {
41   DCHECK(Runtime::Current()->IsAotCompiler());
42   std::unique_ptr<VerifiedMethod> verified_method(
43       new VerifiedMethod(method_verifier->GetEncounteredFailureTypes(),
44                          method_verifier->HasInstructionThatWillThrow()));
45 
46   if (method_verifier->HasCheckCasts()) {
47     verified_method->GenerateSafeCastSet(method_verifier);
48   }
49 
50   return verified_method.release();
51 }
52 
IsSafeCast(uint32_t pc) const53 bool VerifiedMethod::IsSafeCast(uint32_t pc) const {
54   if (safe_cast_set_ == nullptr) {
55     return false;
56   }
57   return std::binary_search(safe_cast_set_->begin(), safe_cast_set_->end(), pc);
58 }
59 
GenerateSafeCastSet(verifier::MethodVerifier * method_verifier)60 void VerifiedMethod::GenerateSafeCastSet(verifier::MethodVerifier* method_verifier) {
61   /*
62    * Walks over the method code and adds any cast instructions in which
63    * the type cast is implicit to a set, which is used in the code generation
64    * to elide these casts.
65    */
66   if (method_verifier->HasFailures()) {
67     return;
68   }
69   for (const DexInstructionPcPair& pair : method_verifier->CodeItem()) {
70     const Instruction& inst = pair.Inst();
71     const Instruction::Code code = inst.Opcode();
72     if (code == Instruction::CHECK_CAST) {
73       const uint32_t dex_pc = pair.DexPc();
74       if (!method_verifier->GetInstructionFlags(dex_pc).IsVisited()) {
75         // Do not attempt to quicken this instruction, it's unreachable anyway.
76         continue;
77       }
78       const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
79       DCHECK(line != nullptr) << "Did not have line for dex pc 0x" << std::hex << dex_pc;
80       const verifier::RegType& reg_type(line->GetRegisterType(method_verifier,
81                                                               inst.VRegA_21c()));
82       const verifier::RegType& cast_type =
83           method_verifier->ResolveCheckedClass(dex::TypeIndex(inst.VRegB_21c()));
84       // Pass null for the method verifier to not record the VerifierDeps dependency
85       // if the types are not assignable.
86       if (cast_type.IsStrictlyAssignableFrom(reg_type, /* verifier= */ nullptr)) {
87         // The types are assignable, we record that dependency in the VerifierDeps so
88         // that if this changes after OTA, we will re-verify again.
89         // We check if reg_type has a class, as the verifier may have inferred it's
90         // 'null'.
91         if (reg_type.HasClass()) {
92           DCHECK(cast_type.HasClass());
93           verifier::VerifierDeps::MaybeRecordAssignability(method_verifier->GetDexFile(),
94                                                            cast_type.GetClass(),
95                                                            reg_type.GetClass(),
96                                                            /* is_strict= */ true,
97                                                            /* is_assignable= */ true);
98         }
99         if (safe_cast_set_ == nullptr) {
100           safe_cast_set_.reset(new SafeCastSet());
101         }
102         // Verify ordering for push_back() to the sorted vector.
103         DCHECK(safe_cast_set_->empty() || safe_cast_set_->back() < dex_pc);
104         safe_cast_set_->push_back(dex_pc);
105       }
106     }
107   }
108   DCHECK(safe_cast_set_ == nullptr || !safe_cast_set_->empty());
109 }
110 
111 }  // namespace art
112