Home | History | Annotate | Download | only in dex
      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 
     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 
     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 
     51 bool VerifiedMethod::IsSafeCast(uint32_t pc) const {
     52   if (safe_cast_set_ == nullptr) {
     53     return false;
     54   }
     55   return std::binary_search(safe_cast_set_->begin(), safe_cast_set_->end(), pc);
     56 }
     57 
     58 void VerifiedMethod::GenerateSafeCastSet(verifier::MethodVerifier* method_verifier) {
     59   /*
     60    * Walks over the method code and adds any cast instructions in which
     61    * the type cast is implicit to a set, which is used in the code generation
     62    * to elide these casts.
     63    */
     64   if (method_verifier->HasFailures()) {
     65     return;
     66   }
     67   const DexFile::CodeItem* code_item = method_verifier->CodeItem();
     68   const Instruction* inst = Instruction::At(code_item->insns_);
     69   const Instruction* end = Instruction::At(code_item->insns_ +
     70                                            code_item->insns_size_in_code_units_);
     71 
     72   for (; inst < end; inst = inst->Next()) {
     73     Instruction::Code code = inst->Opcode();
     74     if (code == Instruction::CHECK_CAST) {
     75       uint32_t dex_pc = inst->GetDexPc(code_item->insns_);
     76       if (!method_verifier->GetInstructionFlags(dex_pc).IsVisited()) {
     77         // Do not attempt to quicken this instruction, it's unreachable anyway.
     78         continue;
     79       }
     80       const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
     81       const verifier::RegType& reg_type(line->GetRegisterType(method_verifier,
     82                                                               inst->VRegA_21c()));
     83       const verifier::RegType& cast_type =
     84           method_verifier->ResolveCheckedClass(dex::TypeIndex(inst->VRegB_21c()));
     85       // Pass null for the method verifier to not record the VerifierDeps dependency
     86       // if the types are not assignable.
     87       if (cast_type.IsStrictlyAssignableFrom(reg_type, /* method_verifier */ nullptr)) {
     88         // The types are assignable, we record that dependency in the VerifierDeps so
     89         // that if this changes after OTA, we will re-verify again.
     90         // We check if reg_type has a class, as the verifier may have inferred it's
     91         // 'null'.
     92         if (reg_type.HasClass()) {
     93           DCHECK(cast_type.HasClass());
     94           verifier::VerifierDeps::MaybeRecordAssignability(method_verifier->GetDexFile(),
     95                                                            cast_type.GetClass(),
     96                                                            reg_type.GetClass(),
     97                                                            /* strict */ true,
     98                                                            /* assignable */ true);
     99         }
    100         if (safe_cast_set_ == nullptr) {
    101           safe_cast_set_.reset(new SafeCastSet());
    102         }
    103         // Verify ordering for push_back() to the sorted vector.
    104         DCHECK(safe_cast_set_->empty() || safe_cast_set_->back() < dex_pc);
    105         safe_cast_set_->push_back(dex_pc);
    106       }
    107     }
    108   }
    109   DCHECK(safe_cast_set_ == nullptr || !safe_cast_set_->empty());
    110 }
    111 
    112 }  // namespace art
    113