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