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 | |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 45 | const VerifiedMethod* VerifiedMethod::Create(verifier::MethodVerifier* method_verifier, |
| 46 | bool compile) { |
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 (compile) { |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 52 | // TODO: move this out when DEX-to-DEX supports devirtualization. |
| 53 | if (method_verifier->HasVirtualOrInterfaceInvokes()) { |
| 54 | verified_method->GenerateDevirtMap(method_verifier); |
| 55 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 56 | |
| 57 | // Only need dequicken info for JIT so far. |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 58 | if (Runtime::Current()->UseJitCompilation() && |
| 59 | !verified_method->GenerateDequickenMap(method_verifier)) { |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 60 | return nullptr; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 61 | } |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if (method_verifier->HasCheckCasts()) { |
| 65 | verified_method->GenerateSafeCastSet(method_verifier); |
| 66 | } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 67 | |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 68 | return verified_method.release(); |
| 69 | } |
| 70 | |
| 71 | const MethodReference* VerifiedMethod::GetDevirtTarget(uint32_t dex_pc) const { |
| 72 | auto it = devirt_map_.find(dex_pc); |
| 73 | return (it != devirt_map_.end()) ? &it->second : nullptr; |
| 74 | } |
| 75 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 76 | const DexFileReference* VerifiedMethod::GetDequickenIndex(uint32_t dex_pc) const { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 77 | DCHECK(Runtime::Current()->UseJitCompilation()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 78 | auto it = dequicken_map_.find(dex_pc); |
| 79 | return (it != dequicken_map_.end()) ? &it->second : nullptr; |
| 80 | } |
| 81 | |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 82 | bool VerifiedMethod::IsSafeCast(uint32_t pc) const { |
| 83 | return std::binary_search(safe_cast_set_.begin(), safe_cast_set_.end(), pc); |
| 84 | } |
| 85 | |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 86 | bool VerifiedMethod::GenerateDequickenMap(verifier::MethodVerifier* method_verifier) { |
Mathieu Chartier | 36b58f5 | 2014-12-10 12:06:45 -0800 | [diff] [blame] | 87 | if (method_verifier->HasFailures()) { |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 88 | return false; |
Mathieu Chartier | 36b58f5 | 2014-12-10 12:06:45 -0800 | [diff] [blame] | 89 | } |
| 90 | const DexFile::CodeItem* code_item = method_verifier->CodeItem(); |
| 91 | const uint16_t* insns = code_item->insns_; |
| 92 | const Instruction* inst = Instruction::At(insns); |
| 93 | const Instruction* end = Instruction::At(insns + code_item->insns_size_in_code_units_); |
| 94 | for (; inst < end; inst = inst->Next()) { |
| 95 | const bool is_virtual_quick = inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK; |
| 96 | const bool is_range_quick = inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK; |
| 97 | if (is_virtual_quick || is_range_quick) { |
| 98 | uint32_t dex_pc = inst->GetDexPc(insns); |
| 99 | verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 100 | ArtMethod* method = |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 101 | method_verifier->GetQuickInvokedMethod(inst, line, is_range_quick, true); |
| 102 | if (method == nullptr) { |
| 103 | // It can be null if the line wasn't verified since it was unreachable. |
| 104 | return false; |
| 105 | } |
Mathieu Chartier | 36b58f5 | 2014-12-10 12:06:45 -0800 | [diff] [blame] | 106 | // The verifier must know what the type of the object was or else we would have gotten a |
| 107 | // failure. Put the dex method index in the dequicken map since we need this to get number of |
| 108 | // arguments in the compiler. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 109 | dequicken_map_.Put(dex_pc, DexFileReference(method->GetDexFile(), |
| 110 | method->GetDexMethodIndex())); |
| 111 | } else if (IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) { |
| 112 | uint32_t dex_pc = inst->GetDexPc(insns); |
| 113 | verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 114 | ArtField* field = method_verifier->GetQuickFieldAccess(inst, line); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 115 | if (field == nullptr) { |
| 116 | // It can be null if the line wasn't verified since it was unreachable. |
| 117 | return false; |
| 118 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 119 | // The verifier must know what the type of the field was or else we would have gotten a |
| 120 | // failure. Put the dex field index in the dequicken map since we need this for lowering |
| 121 | // in the compiler. |
| 122 | // TODO: Putting a field index in a method reference is gross. |
| 123 | dequicken_map_.Put(dex_pc, DexFileReference(field->GetDexFile(), field->GetDexFieldIndex())); |
Mathieu Chartier | 36b58f5 | 2014-12-10 12:06:45 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 126 | return true; |
Mathieu Chartier | 36b58f5 | 2014-12-10 12:06:45 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 129 | void VerifiedMethod::GenerateDevirtMap(verifier::MethodVerifier* method_verifier) { |
| 130 | // It is risky to rely on reg_types for sharpening in cases of soft |
| 131 | // verification, we might end up sharpening to a wrong implementation. Just abort. |
| 132 | if (method_verifier->HasFailures()) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | const DexFile::CodeItem* code_item = method_verifier->CodeItem(); |
| 137 | const uint16_t* insns = code_item->insns_; |
| 138 | const Instruction* inst = Instruction::At(insns); |
| 139 | const Instruction* end = Instruction::At(insns + code_item->insns_size_in_code_units_); |
| 140 | |
| 141 | for (; inst < end; inst = inst->Next()) { |
Mathieu Chartier | 36b58f5 | 2014-12-10 12:06:45 -0800 | [diff] [blame] | 142 | const bool is_virtual = inst->Opcode() == Instruction::INVOKE_VIRTUAL || |
| 143 | inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE; |
| 144 | const bool is_interface = inst->Opcode() == Instruction::INVOKE_INTERFACE || |
| 145 | inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE; |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 146 | |
| 147 | if (!is_interface && !is_virtual) { |
| 148 | continue; |
| 149 | } |
| 150 | // Get reg type for register holding the reference to the object that will be dispatched upon. |
| 151 | uint32_t dex_pc = inst->GetDexPc(insns); |
| 152 | verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc); |
Mathieu Chartier | 36b58f5 | 2014-12-10 12:06:45 -0800 | [diff] [blame] | 153 | const bool is_range = inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE || |
| 154 | inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 155 | const verifier::RegType& |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 156 | reg_type(line->GetRegisterType(method_verifier, |
| 157 | is_range ? inst->VRegC_3rc() : inst->VRegC_35c())); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 158 | |
| 159 | if (!reg_type.HasClass()) { |
| 160 | // We will compute devirtualization information only when we know the Class of the reg type. |
| 161 | continue; |
| 162 | } |
| 163 | mirror::Class* reg_class = reg_type.GetClass(); |
| 164 | if (reg_class->IsInterface()) { |
| 165 | // We can't devirtualize when the known type of the register is an interface. |
| 166 | continue; |
| 167 | } |
| 168 | if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) { |
| 169 | // We can't devirtualize abstract classes except on arrays of abstract classes. |
| 170 | continue; |
| 171 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 172 | auto* cl = Runtime::Current()->GetClassLinker(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 173 | PointerSize pointer_size = cl->GetImagePointerSize(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 174 | ArtMethod* abstract_method = method_verifier->GetDexCache()->GetResolvedMethod( |
| 175 | is_range ? inst->VRegB_3rc() : inst->VRegB_35c(), pointer_size); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 176 | if (abstract_method == nullptr) { |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 177 | // If the method is not found in the cache this means that it was never found |
| 178 | // by ResolveMethodAndCheckAccess() called when verifying invoke_*. |
| 179 | continue; |
| 180 | } |
| 181 | // Find the concrete method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 182 | ArtMethod* concrete_method = nullptr; |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 183 | if (is_interface) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 184 | concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface( |
| 185 | abstract_method, pointer_size); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 186 | } |
| 187 | if (is_virtual) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 188 | concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual( |
| 189 | abstract_method, pointer_size); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 190 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 191 | if (concrete_method == nullptr || !concrete_method->IsInvokable()) { |
| 192 | // In cases where concrete_method is not found, or is not invokable, continue to the next |
| 193 | // invoke. |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 194 | continue; |
| 195 | } |
| 196 | if (reg_type.IsPreciseReference() || concrete_method->IsFinal() || |
| 197 | concrete_method->GetDeclaringClass()->IsFinal()) { |
| 198 | // If we knew exactly the class being dispatched upon, or if the target method cannot be |
| 199 | // overridden record the target to be used in the compiler driver. |
Mathieu Chartier | 36b58f5 | 2014-12-10 12:06:45 -0800 | [diff] [blame] | 200 | devirt_map_.Put(dex_pc, concrete_method->ToMethodReference()); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void VerifiedMethod::GenerateSafeCastSet(verifier::MethodVerifier* method_verifier) { |
| 206 | /* |
| 207 | * Walks over the method code and adds any cast instructions in which |
| 208 | * the type cast is implicit to a set, which is used in the code generation |
| 209 | * to elide these casts. |
| 210 | */ |
| 211 | if (method_verifier->HasFailures()) { |
| 212 | return; |
| 213 | } |
| 214 | const DexFile::CodeItem* code_item = method_verifier->CodeItem(); |
| 215 | const Instruction* inst = Instruction::At(code_item->insns_); |
| 216 | const Instruction* end = Instruction::At(code_item->insns_ + |
| 217 | code_item->insns_size_in_code_units_); |
| 218 | |
| 219 | for (; inst < end; inst = inst->Next()) { |
| 220 | Instruction::Code code = inst->Opcode(); |
| 221 | if ((code == Instruction::CHECK_CAST) || (code == Instruction::APUT_OBJECT)) { |
| 222 | uint32_t dex_pc = inst->GetDexPc(code_item->insns_); |
Stephen Kyle | 40d3518 | 2014-10-03 13:47:56 +0100 | [diff] [blame] | 223 | if (!method_verifier->GetInstructionFlags(dex_pc).IsVisited()) { |
| 224 | // Do not attempt to quicken this instruction, it's unreachable anyway. |
| 225 | continue; |
| 226 | } |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 227 | const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc); |
| 228 | bool is_safe_cast = false; |
| 229 | if (code == Instruction::CHECK_CAST) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 230 | const verifier::RegType& reg_type(line->GetRegisterType(method_verifier, |
| 231 | inst->VRegA_21c())); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 232 | const verifier::RegType& cast_type = |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 233 | method_verifier->ResolveCheckedClass(inst->VRegB_21c()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 234 | is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type, method_verifier); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 235 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 236 | const verifier::RegType& array_type(line->GetRegisterType(method_verifier, |
| 237 | inst->VRegB_23x())); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 238 | // We only know its safe to assign to an array if the array type is precise. For example, |
| 239 | // an Object[] can have any type of object stored in it, but it may also be assigned a |
| 240 | // String[] in which case the stores need to be of Strings. |
| 241 | if (array_type.IsPreciseReference()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 242 | const verifier::RegType& value_type(line->GetRegisterType(method_verifier, |
| 243 | inst->VRegA_23x())); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 244 | const verifier::RegType& component_type = method_verifier->GetRegTypeCache() |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 245 | ->GetComponentType(array_type, method_verifier->GetClassLoader()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 246 | is_safe_cast = component_type.IsStrictlyAssignableFrom(value_type, method_verifier); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | if (is_safe_cast) { |
| 250 | // Verify ordering for push_back() to the sorted vector. |
| 251 | DCHECK(safe_cast_set_.empty() || safe_cast_set_.back() < dex_pc); |
| 252 | safe_cast_set_.push_back(dex_pc); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | } // namespace art |