blob: fdcafe8a1580eed2fecf3e78ff554446c9b04deb [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"
Andreas Gampe542451c2016-07-26 09:02:02 -070024#include "base/enums.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000025#include "base/logging.h"
26#include "base/stl_util.h"
27#include "dex_file.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000028#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080029#include "dex_instruction_utils.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000030#include "mirror/class-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000031#include "mirror/dex_cache-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000032#include "mirror/object-inl.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080033#include "utils.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000034#include "verifier/method_verifier-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070035#include "verifier/reg_type-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000036#include "verifier/register_line-inl.h"
37
38namespace art {
39
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000040VerifiedMethod::VerifiedMethod(uint32_t encountered_error_types, bool has_runtime_throw)
Andreas Gampe0760a812015-08-26 17:12:51 -070041 : encountered_error_types_(encountered_error_types),
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000042 has_runtime_throw_(has_runtime_throw) {
Andreas Gampe0760a812015-08-26 17:12:51 -070043}
44
Vladimir Markoc7f83202014-01-24 17:55:18 +000045const VerifiedMethod* VerifiedMethod::Create(verifier::MethodVerifier* method_verifier,
46 bool compile) {
Andreas Gampe0760a812015-08-26 17:12:51 -070047 std::unique_ptr<VerifiedMethod> verified_method(
48 new VerifiedMethod(method_verifier->GetEncounteredFailureTypes(),
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000049 method_verifier->HasInstructionThatWillThrow()));
Andreas Gampe0760a812015-08-26 17:12:51 -070050
Vladimir Markoc7f83202014-01-24 17:55:18 +000051 if (compile) {
Vladimir Markoc7f83202014-01-24 17:55:18 +000052 // TODO: move this out when DEX-to-DEX supports devirtualization.
53 if (method_verifier->HasVirtualOrInterfaceInvokes()) {
54 verified_method->GenerateDevirtMap(method_verifier);
55 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080056
57 // Only need dequicken info for JIT so far.
Calin Juravleffc87072016-04-20 14:22:09 +010058 if (Runtime::Current()->UseJitCompilation() &&
59 !verified_method->GenerateDequickenMap(method_verifier)) {
Mathieu Chartier091d2382015-03-06 10:59:06 -080060 return nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080061 }
Vladimir Markoc7f83202014-01-24 17:55:18 +000062 }
63
64 if (method_verifier->HasCheckCasts()) {
65 verified_method->GenerateSafeCastSet(method_verifier);
66 }
Jeff Hao848f70a2014-01-15 13:49:50 -080067
Vladimir Markoc7f83202014-01-24 17:55:18 +000068 return verified_method.release();
69}
70
71const 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 Chartiere5f13e52015-02-24 09:37:21 -080076const DexFileReference* VerifiedMethod::GetDequickenIndex(uint32_t dex_pc) const {
Calin Juravleffc87072016-04-20 14:22:09 +010077 DCHECK(Runtime::Current()->UseJitCompilation());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080078 auto it = dequicken_map_.find(dex_pc);
79 return (it != dequicken_map_.end()) ? &it->second : nullptr;
80}
81
Vladimir Markoc7f83202014-01-24 17:55:18 +000082bool VerifiedMethod::IsSafeCast(uint32_t pc) const {
83 return std::binary_search(safe_cast_set_.begin(), safe_cast_set_.end(), pc);
84}
85
Mathieu Chartier091d2382015-03-06 10:59:06 -080086bool VerifiedMethod::GenerateDequickenMap(verifier::MethodVerifier* method_verifier) {
Mathieu Chartier36b58f52014-12-10 12:06:45 -080087 if (method_verifier->HasFailures()) {
Mathieu Chartier091d2382015-03-06 10:59:06 -080088 return false;
Mathieu Chartier36b58f52014-12-10 12:06:45 -080089 }
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 Chartiere401d142015-04-22 13:56:20 -0700100 ArtMethod* method =
Mathieu Chartier091d2382015-03-06 10:59:06 -0800101 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 Chartier36b58f52014-12-10 12:06:45 -0800106 // 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 Chartiere5f13e52015-02-24 09:37:21 -0800109 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 Chartierc7853442015-03-27 14:35:38 -0700114 ArtField* field = method_verifier->GetQuickFieldAccess(inst, line);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800115 if (field == nullptr) {
116 // It can be null if the line wasn't verified since it was unreachable.
117 return false;
118 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800119 // 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 Chartier36b58f52014-12-10 12:06:45 -0800124 }
125 }
Mathieu Chartier091d2382015-03-06 10:59:06 -0800126 return true;
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800127}
128
Vladimir Markoc7f83202014-01-24 17:55:18 +0000129void 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 Chartier36b58f52014-12-10 12:06:45 -0800142 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 Markoc7f83202014-01-24 17:55:18 +0000146
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 Chartier36b58f52014-12-10 12:06:45 -0800153 const bool is_range = inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
154 inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE;
Ian Rogersd8f69b02014-09-10 21:43:52 +0000155 const verifier::RegType&
Ian Rogers7b078e82014-09-10 14:44:24 -0700156 reg_type(line->GetRegisterType(method_verifier,
157 is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Vladimir Markoc7f83202014-01-24 17:55:18 +0000158
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 Chartiere401d142015-04-22 13:56:20 -0700172 auto* cl = Runtime::Current()->GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700173 PointerSize pointer_size = cl->GetImagePointerSize();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700174 ArtMethod* abstract_method = method_verifier->GetDexCache()->GetResolvedMethod(
175 is_range ? inst->VRegB_3rc() : inst->VRegB_35c(), pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700176 if (abstract_method == nullptr) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000177 // 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 Chartiere401d142015-04-22 13:56:20 -0700182 ArtMethod* concrete_method = nullptr;
Vladimir Markoc7f83202014-01-24 17:55:18 +0000183 if (is_interface) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700184 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(
185 abstract_method, pointer_size);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000186 }
187 if (is_virtual) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700188 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(
189 abstract_method, pointer_size);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000190 }
Alex Light9139e002015-10-09 15:59:48 -0700191 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 Markoc7f83202014-01-24 17:55:18 +0000194 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 Chartier36b58f52014-12-10 12:06:45 -0800200 devirt_map_.Put(dex_pc, concrete_method->ToMethodReference());
Vladimir Markoc7f83202014-01-24 17:55:18 +0000201 }
202 }
203}
204
205void 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();
Nicolas Geoffrayd1665a02016-12-12 13:07:07 +0000221 if (code == Instruction::CHECK_CAST) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000222 uint32_t dex_pc = inst->GetDexPc(code_item->insns_);
Stephen Kyle40d35182014-10-03 13:47:56 +0100223 if (!method_verifier->GetInstructionFlags(dex_pc).IsVisited()) {
224 // Do not attempt to quicken this instruction, it's unreachable anyway.
225 continue;
226 }
Vladimir Markoc7f83202014-01-24 17:55:18 +0000227 const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
Nicolas Geoffrayd1665a02016-12-12 13:07:07 +0000228 const verifier::RegType& reg_type(line->GetRegisterType(method_verifier,
229 inst->VRegA_21c()));
230 const verifier::RegType& cast_type =
231 method_verifier->ResolveCheckedClass(dex::TypeIndex(inst->VRegB_21c()));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000232 // Pass null for the method verifier to not record the VerifierDeps dependency
233 // if the types are not assignable.
234 if (cast_type.IsStrictlyAssignableFrom(reg_type, /* method_verifier */ nullptr)) {
235 // The types are assignable, we record that dependency in the VerifierDeps so
236 // that if this changes after OTA, we will re-verify again.
237 // We check if reg_type has a class, as the verifier may have inferred it's
238 // 'null'.
239 if (reg_type.HasClass()) {
240 DCHECK(cast_type.HasClass());
241 verifier::VerifierDeps::MaybeRecordAssignability(method_verifier->GetDexFile(),
242 cast_type.GetClass(),
243 reg_type.GetClass(),
244 /* strict */ true,
245 /* assignable */ true);
246 }
Vladimir Markoc7f83202014-01-24 17:55:18 +0000247 // Verify ordering for push_back() to the sorted vector.
248 DCHECK(safe_cast_set_.empty() || safe_cast_set_.back() < dex_pc);
249 safe_cast_set_.push_back(dex_pc);
250 }
251 }
252 }
253}
254
255} // namespace art