blob: 5c0253c29e9c4302e43947999650ef0d8ae36e5a [file] [log] [blame]
Vladimir Markoc7f83202014-01-24 17:55:18 +00001/*
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 Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Vladimir Markoc7f83202014-01-24 17:55:18 +000021#include <vector>
22
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "art_method-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000024#include "base/logging.h"
25#include "base/stl_util.h"
26#include "dex_file.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000027#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080028#include "dex_instruction_utils.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000029#include "mirror/class-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000030#include "mirror/dex_cache-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000031#include "mirror/object-inl.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080032#include "utils.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000033#include "verifier/method_verifier-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070034#include "verifier/reg_type-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000035#include "verifier/register_line-inl.h"
36
37namespace art {
38
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000039VerifiedMethod::VerifiedMethod(uint32_t encountered_error_types, bool has_runtime_throw)
Andreas Gampe0760a812015-08-26 17:12:51 -070040 : encountered_error_types_(encountered_error_types),
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000041 has_runtime_throw_(has_runtime_throw) {
Andreas Gampe0760a812015-08-26 17:12:51 -070042}
43
Vladimir Markoc7f83202014-01-24 17:55:18 +000044const VerifiedMethod* VerifiedMethod::Create(verifier::MethodVerifier* method_verifier,
45 bool compile) {
Andreas Gampe0760a812015-08-26 17:12:51 -070046 std::unique_ptr<VerifiedMethod> verified_method(
47 new VerifiedMethod(method_verifier->GetEncounteredFailureTypes(),
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000048 method_verifier->HasInstructionThatWillThrow()));
Andreas Gampe0760a812015-08-26 17:12:51 -070049
Vladimir Markoc7f83202014-01-24 17:55:18 +000050 if (compile) {
Vladimir Markoc7f83202014-01-24 17:55:18 +000051 // TODO: move this out when DEX-to-DEX supports devirtualization.
52 if (method_verifier->HasVirtualOrInterfaceInvokes()) {
53 verified_method->GenerateDevirtMap(method_verifier);
54 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080055
56 // Only need dequicken info for JIT so far.
Mathieu Chartier091d2382015-03-06 10:59:06 -080057 if (Runtime::Current()->UseJit() && !verified_method->GenerateDequickenMap(method_verifier)) {
58 return nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080059 }
Vladimir Markoc7f83202014-01-24 17:55:18 +000060 }
61
62 if (method_verifier->HasCheckCasts()) {
63 verified_method->GenerateSafeCastSet(method_verifier);
64 }
Jeff Hao848f70a2014-01-15 13:49:50 -080065
Vladimir Markoc7f83202014-01-24 17:55:18 +000066 return verified_method.release();
67}
68
69const MethodReference* VerifiedMethod::GetDevirtTarget(uint32_t dex_pc) const {
70 auto it = devirt_map_.find(dex_pc);
71 return (it != devirt_map_.end()) ? &it->second : nullptr;
72}
73
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080074const DexFileReference* VerifiedMethod::GetDequickenIndex(uint32_t dex_pc) const {
75 DCHECK(Runtime::Current()->UseJit());
76 auto it = dequicken_map_.find(dex_pc);
77 return (it != dequicken_map_.end()) ? &it->second : nullptr;
78}
79
Vladimir Markoc7f83202014-01-24 17:55:18 +000080bool VerifiedMethod::IsSafeCast(uint32_t pc) const {
81 return std::binary_search(safe_cast_set_.begin(), safe_cast_set_.end(), pc);
82}
83
Mathieu Chartier091d2382015-03-06 10:59:06 -080084bool VerifiedMethod::GenerateDequickenMap(verifier::MethodVerifier* method_verifier) {
Mathieu Chartier36b58f52014-12-10 12:06:45 -080085 if (method_verifier->HasFailures()) {
Mathieu Chartier091d2382015-03-06 10:59:06 -080086 return false;
Mathieu Chartier36b58f52014-12-10 12:06:45 -080087 }
88 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
89 const uint16_t* insns = code_item->insns_;
90 const Instruction* inst = Instruction::At(insns);
91 const Instruction* end = Instruction::At(insns + code_item->insns_size_in_code_units_);
92 for (; inst < end; inst = inst->Next()) {
93 const bool is_virtual_quick = inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK;
94 const bool is_range_quick = inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK;
95 if (is_virtual_quick || is_range_quick) {
96 uint32_t dex_pc = inst->GetDexPc(insns);
97 verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
Mathieu Chartiere401d142015-04-22 13:56:20 -070098 ArtMethod* method =
Mathieu Chartier091d2382015-03-06 10:59:06 -080099 method_verifier->GetQuickInvokedMethod(inst, line, is_range_quick, true);
100 if (method == nullptr) {
101 // It can be null if the line wasn't verified since it was unreachable.
102 return false;
103 }
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800104 // The verifier must know what the type of the object was or else we would have gotten a
105 // failure. Put the dex method index in the dequicken map since we need this to get number of
106 // arguments in the compiler.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800107 dequicken_map_.Put(dex_pc, DexFileReference(method->GetDexFile(),
108 method->GetDexMethodIndex()));
109 } else if (IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) {
110 uint32_t dex_pc = inst->GetDexPc(insns);
111 verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700112 ArtField* field = method_verifier->GetQuickFieldAccess(inst, line);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800113 if (field == nullptr) {
114 // It can be null if the line wasn't verified since it was unreachable.
115 return false;
116 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800117 // The verifier must know what the type of the field was or else we would have gotten a
118 // failure. Put the dex field index in the dequicken map since we need this for lowering
119 // in the compiler.
120 // TODO: Putting a field index in a method reference is gross.
121 dequicken_map_.Put(dex_pc, DexFileReference(field->GetDexFile(), field->GetDexFieldIndex()));
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800122 }
123 }
Mathieu Chartier091d2382015-03-06 10:59:06 -0800124 return true;
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800125}
126
Vladimir Markoc7f83202014-01-24 17:55:18 +0000127void VerifiedMethod::GenerateDevirtMap(verifier::MethodVerifier* method_verifier) {
128 // It is risky to rely on reg_types for sharpening in cases of soft
129 // verification, we might end up sharpening to a wrong implementation. Just abort.
130 if (method_verifier->HasFailures()) {
131 return;
132 }
133
134 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
135 const uint16_t* insns = code_item->insns_;
136 const Instruction* inst = Instruction::At(insns);
137 const Instruction* end = Instruction::At(insns + code_item->insns_size_in_code_units_);
138
139 for (; inst < end; inst = inst->Next()) {
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800140 const bool is_virtual = inst->Opcode() == Instruction::INVOKE_VIRTUAL ||
141 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE;
142 const bool is_interface = inst->Opcode() == Instruction::INVOKE_INTERFACE ||
143 inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE;
Vladimir Markoc7f83202014-01-24 17:55:18 +0000144
145 if (!is_interface && !is_virtual) {
146 continue;
147 }
148 // Get reg type for register holding the reference to the object that will be dispatched upon.
149 uint32_t dex_pc = inst->GetDexPc(insns);
150 verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800151 const bool is_range = inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
152 inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE;
Ian Rogersd8f69b02014-09-10 21:43:52 +0000153 const verifier::RegType&
Ian Rogers7b078e82014-09-10 14:44:24 -0700154 reg_type(line->GetRegisterType(method_verifier,
155 is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Vladimir Markoc7f83202014-01-24 17:55:18 +0000156
157 if (!reg_type.HasClass()) {
158 // We will compute devirtualization information only when we know the Class of the reg type.
159 continue;
160 }
161 mirror::Class* reg_class = reg_type.GetClass();
162 if (reg_class->IsInterface()) {
163 // We can't devirtualize when the known type of the register is an interface.
164 continue;
165 }
166 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
167 // We can't devirtualize abstract classes except on arrays of abstract classes.
168 continue;
169 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700170 auto* cl = Runtime::Current()->GetClassLinker();
171 size_t pointer_size = cl->GetImagePointerSize();
172 ArtMethod* abstract_method = method_verifier->GetDexCache()->GetResolvedMethod(
173 is_range ? inst->VRegB_3rc() : inst->VRegB_35c(), pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700174 if (abstract_method == nullptr) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000175 // If the method is not found in the cache this means that it was never found
176 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
177 continue;
178 }
179 // Find the concrete method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700180 ArtMethod* concrete_method = nullptr;
Vladimir Markoc7f83202014-01-24 17:55:18 +0000181 if (is_interface) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700182 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(
183 abstract_method, pointer_size);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000184 }
185 if (is_virtual) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700186 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(
187 abstract_method, pointer_size);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000188 }
Alex Light9139e002015-10-09 15:59:48 -0700189 if (concrete_method == nullptr || !concrete_method->IsInvokable()) {
190 // In cases where concrete_method is not found, or is not invokable, continue to the next
191 // invoke.
Vladimir Markoc7f83202014-01-24 17:55:18 +0000192 continue;
193 }
194 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
195 concrete_method->GetDeclaringClass()->IsFinal()) {
196 // If we knew exactly the class being dispatched upon, or if the target method cannot be
197 // overridden record the target to be used in the compiler driver.
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800198 devirt_map_.Put(dex_pc, concrete_method->ToMethodReference());
Vladimir Markoc7f83202014-01-24 17:55:18 +0000199 }
200 }
201}
202
203void VerifiedMethod::GenerateSafeCastSet(verifier::MethodVerifier* method_verifier) {
204 /*
205 * Walks over the method code and adds any cast instructions in which
206 * the type cast is implicit to a set, which is used in the code generation
207 * to elide these casts.
208 */
209 if (method_verifier->HasFailures()) {
210 return;
211 }
212 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
213 const Instruction* inst = Instruction::At(code_item->insns_);
214 const Instruction* end = Instruction::At(code_item->insns_ +
215 code_item->insns_size_in_code_units_);
216
217 for (; inst < end; inst = inst->Next()) {
218 Instruction::Code code = inst->Opcode();
219 if ((code == Instruction::CHECK_CAST) || (code == Instruction::APUT_OBJECT)) {
220 uint32_t dex_pc = inst->GetDexPc(code_item->insns_);
Stephen Kyle40d35182014-10-03 13:47:56 +0100221 if (!method_verifier->GetInstructionFlags(dex_pc).IsVisited()) {
222 // Do not attempt to quicken this instruction, it's unreachable anyway.
223 continue;
224 }
Vladimir Markoc7f83202014-01-24 17:55:18 +0000225 const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
226 bool is_safe_cast = false;
227 if (code == Instruction::CHECK_CAST) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700228 const verifier::RegType& reg_type(line->GetRegisterType(method_verifier,
229 inst->VRegA_21c()));
Ian Rogersd8f69b02014-09-10 21:43:52 +0000230 const verifier::RegType& cast_type =
Vladimir Markoc7f83202014-01-24 17:55:18 +0000231 method_verifier->ResolveCheckedClass(inst->VRegB_21c());
232 is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type);
233 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -0700234 const verifier::RegType& array_type(line->GetRegisterType(method_verifier,
235 inst->VRegB_23x()));
Vladimir Markoc7f83202014-01-24 17:55:18 +0000236 // We only know its safe to assign to an array if the array type is precise. For example,
237 // an Object[] can have any type of object stored in it, but it may also be assigned a
238 // String[] in which case the stores need to be of Strings.
239 if (array_type.IsPreciseReference()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700240 const verifier::RegType& value_type(line->GetRegisterType(method_verifier,
241 inst->VRegA_23x()));
Ian Rogersd8f69b02014-09-10 21:43:52 +0000242 const verifier::RegType& component_type = method_verifier->GetRegTypeCache()
Vladimir Markoc7f83202014-01-24 17:55:18 +0000243 ->GetComponentType(array_type, method_verifier->GetClassLoader());
244 is_safe_cast = component_type.IsStrictlyAssignableFrom(value_type);
245 }
246 }
247 if (is_safe_cast) {
248 // Verify ordering for push_back() to the sorted vector.
249 DCHECK(safe_cast_set_.empty() || safe_cast_set_.back() < dex_pc);
250 safe_cast_set_.push_back(dex_pc);
251 }
252 }
253 }
254}
255
256} // namespace art