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 | |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 62 | bool VerifiedMethod::GenerateDequickenMap(verifier::MethodVerifier* method_verifier) { |
Mathieu Chartier | 36b58f5 | 2014-12-10 12:06:45 -0800 | [diff] [blame] | 63 | if (method_verifier->HasFailures()) { |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 64 | return false; |
Mathieu Chartier | 36b58f5 | 2014-12-10 12:06:45 -0800 | [diff] [blame] | 65 | } |
| 66 | const DexFile::CodeItem* code_item = method_verifier->CodeItem(); |
| 67 | const uint16_t* insns = code_item->insns_; |
| 68 | const Instruction* inst = Instruction::At(insns); |
| 69 | const Instruction* end = Instruction::At(insns + code_item->insns_size_in_code_units_); |
| 70 | for (; inst < end; inst = inst->Next()) { |
| 71 | const bool is_virtual_quick = inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK; |
| 72 | const bool is_range_quick = inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK; |
| 73 | if (is_virtual_quick || is_range_quick) { |
| 74 | uint32_t dex_pc = inst->GetDexPc(insns); |
| 75 | verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 76 | ArtMethod* method = |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 77 | method_verifier->GetQuickInvokedMethod(inst, line, is_range_quick, true); |
| 78 | if (method == nullptr) { |
| 79 | // It can be null if the line wasn't verified since it was unreachable. |
| 80 | return false; |
| 81 | } |
Mathieu Chartier | 36b58f5 | 2014-12-10 12:06:45 -0800 | [diff] [blame] | 82 | // The verifier must know what the type of the object was or else we would have gotten a |
| 83 | // failure. Put the dex method index in the dequicken map since we need this to get number of |
| 84 | // arguments in the compiler. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 85 | dequicken_map_.Put(dex_pc, DexFileReference(method->GetDexFile(), |
| 86 | method->GetDexMethodIndex())); |
| 87 | } else if (IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) { |
| 88 | uint32_t dex_pc = inst->GetDexPc(insns); |
| 89 | verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 90 | ArtField* field = method_verifier->GetQuickFieldAccess(inst, line); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 91 | if (field == nullptr) { |
| 92 | // It can be null if the line wasn't verified since it was unreachable. |
| 93 | return false; |
| 94 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 95 | // The verifier must know what the type of the field was or else we would have gotten a |
| 96 | // failure. Put the dex field index in the dequicken map since we need this for lowering |
| 97 | // in the compiler. |
| 98 | // TODO: Putting a field index in a method reference is gross. |
| 99 | dequicken_map_.Put(dex_pc, DexFileReference(field->GetDexFile(), field->GetDexFieldIndex())); |
Mathieu Chartier | 36b58f5 | 2014-12-10 12:06:45 -0800 | [diff] [blame] | 100 | } |
| 101 | } |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 102 | return true; |
Mathieu Chartier | 36b58f5 | 2014-12-10 12:06:45 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 105 | void VerifiedMethod::GenerateSafeCastSet(verifier::MethodVerifier* method_verifier) { |
| 106 | /* |
| 107 | * Walks over the method code and adds any cast instructions in which |
| 108 | * the type cast is implicit to a set, which is used in the code generation |
| 109 | * to elide these casts. |
| 110 | */ |
| 111 | if (method_verifier->HasFailures()) { |
| 112 | return; |
| 113 | } |
| 114 | const DexFile::CodeItem* code_item = method_verifier->CodeItem(); |
| 115 | const Instruction* inst = Instruction::At(code_item->insns_); |
| 116 | const Instruction* end = Instruction::At(code_item->insns_ + |
| 117 | code_item->insns_size_in_code_units_); |
| 118 | |
| 119 | for (; inst < end; inst = inst->Next()) { |
| 120 | Instruction::Code code = inst->Opcode(); |
Nicolas Geoffray | d1665a0 | 2016-12-12 13:07:07 +0000 | [diff] [blame] | 121 | if (code == Instruction::CHECK_CAST) { |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 122 | uint32_t dex_pc = inst->GetDexPc(code_item->insns_); |
Stephen Kyle | 40d3518 | 2014-10-03 13:47:56 +0100 | [diff] [blame] | 123 | if (!method_verifier->GetInstructionFlags(dex_pc).IsVisited()) { |
| 124 | // Do not attempt to quicken this instruction, it's unreachable anyway. |
| 125 | continue; |
| 126 | } |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 127 | const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc); |
Nicolas Geoffray | d1665a0 | 2016-12-12 13:07:07 +0000 | [diff] [blame] | 128 | const verifier::RegType& reg_type(line->GetRegisterType(method_verifier, |
| 129 | inst->VRegA_21c())); |
| 130 | const verifier::RegType& cast_type = |
| 131 | method_verifier->ResolveCheckedClass(dex::TypeIndex(inst->VRegB_21c())); |
Nicolas Geoffray | 119e846 | 2016-12-21 10:29:43 +0000 | [diff] [blame] | 132 | // Pass null for the method verifier to not record the VerifierDeps dependency |
| 133 | // if the types are not assignable. |
| 134 | if (cast_type.IsStrictlyAssignableFrom(reg_type, /* method_verifier */ nullptr)) { |
| 135 | // The types are assignable, we record that dependency in the VerifierDeps so |
| 136 | // that if this changes after OTA, we will re-verify again. |
| 137 | // We check if reg_type has a class, as the verifier may have inferred it's |
| 138 | // 'null'. |
| 139 | if (reg_type.HasClass()) { |
| 140 | DCHECK(cast_type.HasClass()); |
| 141 | verifier::VerifierDeps::MaybeRecordAssignability(method_verifier->GetDexFile(), |
| 142 | cast_type.GetClass(), |
| 143 | reg_type.GetClass(), |
| 144 | /* strict */ true, |
| 145 | /* assignable */ true); |
| 146 | } |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 147 | // Verify ordering for push_back() to the sorted vector. |
| 148 | DCHECK(safe_cast_set_.empty() || safe_cast_set_.back() < dex_pc); |
| 149 | safe_cast_set_.push_back(dex_pc); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | } // namespace art |