blob: bace014713e886039485677943966412e3b2508f [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.
Calin Juravlee5de54c2016-04-20 14:22:09 +010057 if (Runtime::Current()->UseJitCompilation() &&
58 !verified_method->GenerateDequickenMap(method_verifier)) {
Mathieu Chartier091d2382015-03-06 10:59:06 -080059 return nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080060 }
Vladimir Markoc7f83202014-01-24 17:55:18 +000061 }
62
63 if (method_verifier->HasCheckCasts()) {
64 verified_method->GenerateSafeCastSet(method_verifier);
65 }
Jeff Hao848f70a2014-01-15 13:49:50 -080066
Vladimir Markoc7f83202014-01-24 17:55:18 +000067 return verified_method.release();
68}
69
70const 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 Chartiere5f13e52015-02-24 09:37:21 -080075const DexFileReference* VerifiedMethod::GetDequickenIndex(uint32_t dex_pc) const {
Calin Juravlee5de54c2016-04-20 14:22:09 +010076 DCHECK(Runtime::Current()->UseJitCompilation());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080077 auto it = dequicken_map_.find(dex_pc);
78 return (it != dequicken_map_.end()) ? &it->second : nullptr;
79}
80
Vladimir Markoc7f83202014-01-24 17:55:18 +000081bool VerifiedMethod::IsSafeCast(uint32_t pc) const {
82 return std::binary_search(safe_cast_set_.begin(), safe_cast_set_.end(), pc);
83}
84
Mathieu Chartier091d2382015-03-06 10:59:06 -080085bool VerifiedMethod::GenerateDequickenMap(verifier::MethodVerifier* method_verifier) {
Mathieu Chartier36b58f52014-12-10 12:06:45 -080086 if (method_verifier->HasFailures()) {
Mathieu Chartier091d2382015-03-06 10:59:06 -080087 return false;
Mathieu Chartier36b58f52014-12-10 12:06:45 -080088 }
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 Chartiere401d142015-04-22 13:56:20 -070099 ArtMethod* method =
Mathieu Chartier091d2382015-03-06 10:59:06 -0800100 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 Chartier36b58f52014-12-10 12:06:45 -0800105 // 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 Chartiere5f13e52015-02-24 09:37:21 -0800108 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 Chartierc7853442015-03-27 14:35:38 -0700113 ArtField* field = method_verifier->GetQuickFieldAccess(inst, line);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800114 if (field == nullptr) {
115 // It can be null if the line wasn't verified since it was unreachable.
116 return false;
117 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800118 // 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 Chartier36b58f52014-12-10 12:06:45 -0800123 }
124 }
Mathieu Chartier091d2382015-03-06 10:59:06 -0800125 return true;
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800126}
127
Vladimir Markoc7f83202014-01-24 17:55:18 +0000128void 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 Chartier36b58f52014-12-10 12:06:45 -0800141 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 Markoc7f83202014-01-24 17:55:18 +0000145
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 Chartier36b58f52014-12-10 12:06:45 -0800152 const bool is_range = inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
153 inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE;
Ian Rogersd8f69b02014-09-10 21:43:52 +0000154 const verifier::RegType&
Ian Rogers7b078e82014-09-10 14:44:24 -0700155 reg_type(line->GetRegisterType(method_verifier,
156 is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Vladimir Markoc7f83202014-01-24 17:55:18 +0000157
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 Chartiere401d142015-04-22 13:56:20 -0700171 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 Chartier2cebb242015-04-21 16:50:40 -0700175 if (abstract_method == nullptr) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000176 // 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 Chartiere401d142015-04-22 13:56:20 -0700181 ArtMethod* concrete_method = nullptr;
Vladimir Markoc7f83202014-01-24 17:55:18 +0000182 if (is_interface) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700183 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(
184 abstract_method, pointer_size);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000185 }
186 if (is_virtual) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700187 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(
188 abstract_method, pointer_size);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000189 }
Alex Light9139e002015-10-09 15:59:48 -0700190 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 Markoc7f83202014-01-24 17:55:18 +0000193 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 Chartier36b58f52014-12-10 12:06:45 -0800199 devirt_map_.Put(dex_pc, concrete_method->ToMethodReference());
Vladimir Markoc7f83202014-01-24 17:55:18 +0000200 }
201 }
202}
203
204void 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 Kyle40d35182014-10-03 13:47:56 +0100222 if (!method_verifier->GetInstructionFlags(dex_pc).IsVisited()) {
223 // Do not attempt to quicken this instruction, it's unreachable anyway.
224 continue;
225 }
Vladimir Markoc7f83202014-01-24 17:55:18 +0000226 const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
227 bool is_safe_cast = false;
228 if (code == Instruction::CHECK_CAST) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700229 const verifier::RegType& reg_type(line->GetRegisterType(method_verifier,
230 inst->VRegA_21c()));
Ian Rogersd8f69b02014-09-10 21:43:52 +0000231 const verifier::RegType& cast_type =
Vladimir Markoc7f83202014-01-24 17:55:18 +0000232 method_verifier->ResolveCheckedClass(inst->VRegB_21c());
233 is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type);
234 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -0700235 const verifier::RegType& array_type(line->GetRegisterType(method_verifier,
236 inst->VRegB_23x()));
Vladimir Markoc7f83202014-01-24 17:55:18 +0000237 // 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 Rogers7b078e82014-09-10 14:44:24 -0700241 const verifier::RegType& value_type(line->GetRegisterType(method_verifier,
242 inst->VRegA_23x()));
Ian Rogersd8f69b02014-09-10 21:43:52 +0000243 const verifier::RegType& component_type = method_verifier->GetRegTypeCache()
Vladimir Markoc7f83202014-01-24 17:55:18 +0000244 ->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