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