Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 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> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 20 | #include <memory> |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 23 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 24 | #include "base/enums.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 25 | #include "base/logging.h" |
| 26 | #include "base/stl_util.h" |
| 27 | #include "dex_file.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 28 | #include "dex_instruction-inl.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 29 | #include "dex_instruction_utils.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 30 | #include "mirror/class-inl.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 31 | #include "mirror/dex_cache-inl.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 32 | #include "mirror/object-inl.h" |
Andreas Gampe | 0b9203e | 2015-01-22 20:39:27 -0800 | [diff] [blame] | 33 | #include "utils.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 34 | #include "verifier/method_verifier-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 35 | #include "verifier/reg_type-inl.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 36 | #include "verifier/register_line-inl.h" |
| 37 | |
| 38 | namespace art { |
| 39 | |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 40 | VerifiedMethod::VerifiedMethod(uint32_t encountered_error_types, bool has_runtime_throw) |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 41 | : encountered_error_types_(encountered_error_types), |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 42 | has_runtime_throw_(has_runtime_throw) { |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Nicolas Geoffray | c51c7ca | 2016-11-25 15:46:48 +0000 | [diff] [blame] | 45 | const VerifiedMethod* VerifiedMethod::Create(verifier::MethodVerifier* method_verifier) { |
| 46 | DCHECK(Runtime::Current()->IsAotCompiler()); |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 47 | std::unique_ptr<VerifiedMethod> verified_method( |
| 48 | new VerifiedMethod(method_verifier->GetEncounteredFailureTypes(), |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 49 | method_verifier->HasInstructionThatWillThrow())); |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 50 | |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 51 | if (method_verifier->HasCheckCasts()) { |
| 52 | verified_method->GenerateSafeCastSet(method_verifier); |
| 53 | } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 54 | |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 55 | return verified_method.release(); |
| 56 | } |
| 57 | |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 58 | bool VerifiedMethod::IsSafeCast(uint32_t pc) const { |
| 59 | return std::binary_search(safe_cast_set_.begin(), safe_cast_set_.end(), pc); |
| 60 | } |
| 61 | |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 62 | void VerifiedMethod::GenerateSafeCastSet(verifier::MethodVerifier* method_verifier) { |
| 63 | /* |
| 64 | * Walks over the method code and adds any cast instructions in which |
| 65 | * the type cast is implicit to a set, which is used in the code generation |
| 66 | * to elide these casts. |
| 67 | */ |
| 68 | if (method_verifier->HasFailures()) { |
| 69 | return; |
| 70 | } |
| 71 | const DexFile::CodeItem* code_item = method_verifier->CodeItem(); |
| 72 | const Instruction* inst = Instruction::At(code_item->insns_); |
| 73 | const Instruction* end = Instruction::At(code_item->insns_ + |
| 74 | code_item->insns_size_in_code_units_); |
| 75 | |
| 76 | for (; inst < end; inst = inst->Next()) { |
| 77 | Instruction::Code code = inst->Opcode(); |
Nicolas Geoffray | d1665a0 | 2016-12-12 13:07:07 +0000 | [diff] [blame] | 78 | if (code == Instruction::CHECK_CAST) { |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 79 | uint32_t dex_pc = inst->GetDexPc(code_item->insns_); |
Stephen Kyle | 40d3518 | 2014-10-03 13:47:56 +0100 | [diff] [blame] | 80 | if (!method_verifier->GetInstructionFlags(dex_pc).IsVisited()) { |
| 81 | // Do not attempt to quicken this instruction, it's unreachable anyway. |
| 82 | continue; |
| 83 | } |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 84 | const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc); |
Nicolas Geoffray | d1665a0 | 2016-12-12 13:07:07 +0000 | [diff] [blame] | 85 | const verifier::RegType& reg_type(line->GetRegisterType(method_verifier, |
| 86 | inst->VRegA_21c())); |
| 87 | const verifier::RegType& cast_type = |
| 88 | method_verifier->ResolveCheckedClass(dex::TypeIndex(inst->VRegB_21c())); |
Nicolas Geoffray | 119e846 | 2016-12-21 10:29:43 +0000 | [diff] [blame] | 89 | // Pass null for the method verifier to not record the VerifierDeps dependency |
| 90 | // if the types are not assignable. |
| 91 | if (cast_type.IsStrictlyAssignableFrom(reg_type, /* method_verifier */ nullptr)) { |
| 92 | // The types are assignable, we record that dependency in the VerifierDeps so |
| 93 | // that if this changes after OTA, we will re-verify again. |
| 94 | // We check if reg_type has a class, as the verifier may have inferred it's |
| 95 | // 'null'. |
| 96 | if (reg_type.HasClass()) { |
| 97 | DCHECK(cast_type.HasClass()); |
| 98 | verifier::VerifierDeps::MaybeRecordAssignability(method_verifier->GetDexFile(), |
| 99 | cast_type.GetClass(), |
| 100 | reg_type.GetClass(), |
| 101 | /* strict */ true, |
| 102 | /* assignable */ true); |
| 103 | } |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 104 | // Verify ordering for push_back() to the sorted vector. |
| 105 | DCHECK(safe_cast_set_.empty() || safe_cast_set_.back() < dex_pc); |
| 106 | safe_cast_set_.push_back(dex_pc); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | } // namespace art |