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 | |
| 22 | #include "base/logging.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 23 | #include "dex_file.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 24 | #include "dex_instruction-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 25 | #include "runtime.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 26 | #include "verifier/method_verifier-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 27 | #include "verifier/reg_type-inl.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 28 | #include "verifier/register_line-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 29 | #include "verifier/verifier_deps.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 33 | VerifiedMethod::VerifiedMethod(uint32_t encountered_error_types, bool has_runtime_throw) |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 34 | : encountered_error_types_(encountered_error_types), |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 35 | has_runtime_throw_(has_runtime_throw) { |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Nicolas Geoffray | c51c7ca | 2016-11-25 15:46:48 +0000 | [diff] [blame] | 38 | const VerifiedMethod* VerifiedMethod::Create(verifier::MethodVerifier* method_verifier) { |
| 39 | DCHECK(Runtime::Current()->IsAotCompiler()); |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 40 | std::unique_ptr<VerifiedMethod> verified_method( |
| 41 | new VerifiedMethod(method_verifier->GetEncounteredFailureTypes(), |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 42 | method_verifier->HasInstructionThatWillThrow())); |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 43 | |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 44 | if (method_verifier->HasCheckCasts()) { |
| 45 | verified_method->GenerateSafeCastSet(method_verifier); |
| 46 | } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 47 | |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 48 | return verified_method.release(); |
| 49 | } |
| 50 | |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 51 | bool VerifiedMethod::IsSafeCast(uint32_t pc) const { |
Andreas Gampe | 26699c6 | 2017-05-12 08:19:28 -0700 | [diff] [blame] | 52 | if (safe_cast_set_ == nullptr) { |
| 53 | return false; |
| 54 | } |
| 55 | return std::binary_search(safe_cast_set_->begin(), safe_cast_set_->end(), pc); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 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 | } |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 67 | IterationRange<DexInstructionIterator> instructions = method_verifier->CodeItem()->Instructions(); |
| 68 | for (auto it = instructions.begin(); it != instructions.end(); ++it) { |
| 69 | const Instruction& inst = *it; |
| 70 | const Instruction::Code code = inst.Opcode(); |
Nicolas Geoffray | d1665a0 | 2016-12-12 13:07:07 +0000 | [diff] [blame] | 71 | if (code == Instruction::CHECK_CAST) { |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 72 | const uint32_t dex_pc = it.GetDexPC(instructions.begin()); |
Stephen Kyle | 40d3518 | 2014-10-03 13:47:56 +0100 | [diff] [blame] | 73 | if (!method_verifier->GetInstructionFlags(dex_pc).IsVisited()) { |
| 74 | // Do not attempt to quicken this instruction, it's unreachable anyway. |
| 75 | continue; |
| 76 | } |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 77 | const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc); |
Nicolas Geoffray | d1665a0 | 2016-12-12 13:07:07 +0000 | [diff] [blame] | 78 | const verifier::RegType& reg_type(line->GetRegisterType(method_verifier, |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 79 | inst.VRegA_21c())); |
Nicolas Geoffray | d1665a0 | 2016-12-12 13:07:07 +0000 | [diff] [blame] | 80 | const verifier::RegType& cast_type = |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 81 | method_verifier->ResolveCheckedClass(dex::TypeIndex(inst.VRegB_21c())); |
Nicolas Geoffray | 119e846 | 2016-12-21 10:29:43 +0000 | [diff] [blame] | 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 | } |
Andreas Gampe | 26699c6 | 2017-05-12 08:19:28 -0700 | [diff] [blame] | 97 | if (safe_cast_set_ == nullptr) { |
| 98 | safe_cast_set_.reset(new SafeCastSet()); |
| 99 | } |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 100 | // Verify ordering for push_back() to the sorted vector. |
Andreas Gampe | 26699c6 | 2017-05-12 08:19:28 -0700 | [diff] [blame] | 101 | DCHECK(safe_cast_set_->empty() || safe_cast_set_->back() < dex_pc); |
| 102 | safe_cast_set_->push_back(dex_pc); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |
Andreas Gampe | 26699c6 | 2017-05-12 08:19:28 -0700 | [diff] [blame] | 106 | DCHECK(safe_cast_set_ == nullptr || !safe_cast_set_->empty()); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | } // namespace art |