blob: 7eba5152002d0233a3ed15c502bf17f0ba3b9fd8 [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
23#include "base/logging.h"
24#include "base/stl_util.h"
25#include "dex_file.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000026#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080027#include "dex_instruction_utils.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000028#include "mirror/art_method-inl.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/dex_gc_map.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
40const VerifiedMethod* VerifiedMethod::Create(verifier::MethodVerifier* method_verifier,
41 bool compile) {
Ian Rogers700a4022014-05-19 16:49:03 -070042 std::unique_ptr<VerifiedMethod> verified_method(new VerifiedMethod);
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010043 verified_method->has_verification_failures_ = method_verifier->HasFailures();
Vladimir Markoc7f83202014-01-24 17:55:18 +000044 if (compile) {
45 /* Generate a register map. */
46 if (!verified_method->GenerateGcMap(method_verifier)) {
Vladimir Markoc7f83202014-01-24 17:55:18 +000047 return nullptr; // Not a real failure, but a failure to encode.
48 }
49 if (kIsDebugBuild) {
50 VerifyGcMap(method_verifier, verified_method->dex_gc_map_);
51 }
52
53 // TODO: move this out when DEX-to-DEX supports devirtualization.
54 if (method_verifier->HasVirtualOrInterfaceInvokes()) {
55 verified_method->GenerateDevirtMap(method_verifier);
56 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080057
58 // Only need dequicken info for JIT so far.
Mathieu Chartier091d2382015-03-06 10:59:06 -080059 if (Runtime::Current()->UseJit() && !verified_method->GenerateDequickenMap(method_verifier)) {
60 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 }
67 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 {
76 DCHECK(Runtime::Current()->UseJit());
77 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
85bool VerifiedMethod::GenerateGcMap(verifier::MethodVerifier* method_verifier) {
86 DCHECK(dex_gc_map_.empty());
87 size_t num_entries, ref_bitmap_bits, pc_bits;
88 ComputeGcMapSizes(method_verifier, &num_entries, &ref_bitmap_bits, &pc_bits);
89 // There's a single byte to encode the size of each bitmap.
Mathieu Chartier36b58f52014-12-10 12:06:45 -080090 if (ref_bitmap_bits >= kBitsPerByte * 8192 /* 13-bit size */) {
Andreas Gampe6c170c92014-12-17 14:35:46 -080091 LOG(WARNING) << "Cannot encode GC map for method with " << ref_bitmap_bits << " registers: "
92 << PrettyMethod(method_verifier->GetMethodReference().dex_method_index,
93 *method_verifier->GetMethodReference().dex_file);
Vladimir Markoc7f83202014-01-24 17:55:18 +000094 return false;
95 }
Mathieu Chartier36b58f52014-12-10 12:06:45 -080096 size_t ref_bitmap_bytes = RoundUp(ref_bitmap_bits, kBitsPerByte) / kBitsPerByte;
Vladimir Markoc7f83202014-01-24 17:55:18 +000097 // There are 2 bytes to encode the number of entries.
98 if (num_entries >= 65536) {
Andreas Gampe6c170c92014-12-17 14:35:46 -080099 LOG(WARNING) << "Cannot encode GC map for method with " << num_entries << " entries: "
100 << PrettyMethod(method_verifier->GetMethodReference().dex_method_index,
101 *method_verifier->GetMethodReference().dex_file);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000102 return false;
103 }
104 size_t pc_bytes;
105 verifier::RegisterMapFormat format;
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800106 if (pc_bits <= kBitsPerByte) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000107 format = verifier::kRegMapFormatCompact8;
108 pc_bytes = 1;
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800109 } else if (pc_bits <= kBitsPerByte * 2) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000110 format = verifier::kRegMapFormatCompact16;
111 pc_bytes = 2;
112 } else {
Andreas Gampe6c170c92014-12-17 14:35:46 -0800113 LOG(WARNING) << "Cannot encode GC map for method with "
114 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2): "
115 << PrettyMethod(method_verifier->GetMethodReference().dex_method_index,
116 *method_verifier->GetMethodReference().dex_file);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000117 return false;
118 }
119 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
120 dex_gc_map_.reserve(table_size);
121 // Write table header.
122 dex_gc_map_.push_back(format | ((ref_bitmap_bytes & ~0xFF) >> 5));
123 dex_gc_map_.push_back(ref_bitmap_bytes & 0xFF);
124 dex_gc_map_.push_back(num_entries & 0xFF);
125 dex_gc_map_.push_back((num_entries >> 8) & 0xFF);
126 // Write table data.
127 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
128 for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) {
129 if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) {
130 dex_gc_map_.push_back(i & 0xFF);
131 if (pc_bytes == 2) {
132 dex_gc_map_.push_back((i >> 8) & 0xFF);
133 }
134 verifier::RegisterLine* line = method_verifier->GetRegLine(i);
Ian Rogers7b078e82014-09-10 14:44:24 -0700135 line->WriteReferenceBitMap(method_verifier, &dex_gc_map_, ref_bitmap_bytes);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000136 }
137 }
138 DCHECK_EQ(dex_gc_map_.size(), table_size);
139 return true;
140}
141
142void VerifiedMethod::VerifyGcMap(verifier::MethodVerifier* method_verifier,
143 const std::vector<uint8_t>& data) {
144 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
145 // that the table data is well formed and all references are marked (or not) in the bitmap.
146 verifier::DexPcToReferenceMap map(&data[0]);
147 DCHECK_EQ(data.size(), map.RawSize());
148 size_t map_index = 0;
149 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
150 for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) {
151 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
152 if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) {
153 DCHECK_LT(map_index, map.NumEntries());
154 DCHECK_EQ(map.GetDexPc(map_index), i);
155 DCHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
156 map_index++;
157 verifier::RegisterLine* line = method_verifier->GetRegLine(i);
158 for (size_t j = 0; j < code_item->registers_size_; j++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700159 if (line->GetRegisterType(method_verifier, j).IsNonZeroReferenceTypes()) {
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800160 DCHECK_LT(j / kBitsPerByte, map.RegWidth());
161 DCHECK_EQ((reg_bitmap[j / kBitsPerByte] >> (j % kBitsPerByte)) & 1, 1);
162 } else if ((j / kBitsPerByte) < map.RegWidth()) {
163 DCHECK_EQ((reg_bitmap[j / kBitsPerByte] >> (j % kBitsPerByte)) & 1, 0);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000164 } else {
165 // If a register doesn't contain a reference then the bitmap may be shorter than the line.
166 }
167 }
168 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700169 DCHECK(i >= 65536 || reg_bitmap == nullptr);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000170 }
171 }
172}
173
174void VerifiedMethod::ComputeGcMapSizes(verifier::MethodVerifier* method_verifier,
175 size_t* gc_points, size_t* ref_bitmap_bits,
176 size_t* log2_max_gc_pc) {
177 size_t local_gc_points = 0;
178 size_t max_insn = 0;
179 size_t max_ref_reg = -1;
180 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
181 for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) {
182 if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) {
183 local_gc_points++;
184 max_insn = i;
185 verifier::RegisterLine* line = method_verifier->GetRegLine(i);
Ian Rogers7b078e82014-09-10 14:44:24 -0700186 max_ref_reg = line->GetMaxNonZeroReferenceReg(method_verifier, max_ref_reg);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000187 }
188 }
189 *gc_points = local_gc_points;
190 *ref_bitmap_bits = max_ref_reg + 1; // If max register is 0 we need 1 bit to encode (ie +1).
191 size_t i = 0;
192 while ((1U << i) <= max_insn) {
193 i++;
194 }
195 *log2_max_gc_pc = i;
196}
197
Mathieu Chartier091d2382015-03-06 10:59:06 -0800198bool VerifiedMethod::GenerateDequickenMap(verifier::MethodVerifier* method_verifier) {
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800199 if (method_verifier->HasFailures()) {
Mathieu Chartier091d2382015-03-06 10:59:06 -0800200 return false;
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800201 }
202 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
203 const uint16_t* insns = code_item->insns_;
204 const Instruction* inst = Instruction::At(insns);
205 const Instruction* end = Instruction::At(insns + code_item->insns_size_in_code_units_);
206 for (; inst < end; inst = inst->Next()) {
207 const bool is_virtual_quick = inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK;
208 const bool is_range_quick = inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK;
209 if (is_virtual_quick || is_range_quick) {
210 uint32_t dex_pc = inst->GetDexPc(insns);
211 verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800212 mirror::ArtMethod* method =
Mathieu Chartier091d2382015-03-06 10:59:06 -0800213 method_verifier->GetQuickInvokedMethod(inst, line, is_range_quick, true);
214 if (method == nullptr) {
215 // It can be null if the line wasn't verified since it was unreachable.
216 return false;
217 }
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800218 // The verifier must know what the type of the object was or else we would have gotten a
219 // failure. Put the dex method index in the dequicken map since we need this to get number of
220 // arguments in the compiler.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800221 dequicken_map_.Put(dex_pc, DexFileReference(method->GetDexFile(),
222 method->GetDexMethodIndex()));
223 } else if (IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) {
224 uint32_t dex_pc = inst->GetDexPc(insns);
225 verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700226 ArtField* field = method_verifier->GetQuickFieldAccess(inst, line);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800227 if (field == nullptr) {
228 // It can be null if the line wasn't verified since it was unreachable.
229 return false;
230 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800231 // The verifier must know what the type of the field was or else we would have gotten a
232 // failure. Put the dex field index in the dequicken map since we need this for lowering
233 // in the compiler.
234 // TODO: Putting a field index in a method reference is gross.
235 dequicken_map_.Put(dex_pc, DexFileReference(field->GetDexFile(), field->GetDexFieldIndex()));
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800236 }
237 }
Mathieu Chartier091d2382015-03-06 10:59:06 -0800238 return true;
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800239}
240
Vladimir Markoc7f83202014-01-24 17:55:18 +0000241void VerifiedMethod::GenerateDevirtMap(verifier::MethodVerifier* method_verifier) {
242 // It is risky to rely on reg_types for sharpening in cases of soft
243 // verification, we might end up sharpening to a wrong implementation. Just abort.
244 if (method_verifier->HasFailures()) {
245 return;
246 }
247
248 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
249 const uint16_t* insns = code_item->insns_;
250 const Instruction* inst = Instruction::At(insns);
251 const Instruction* end = Instruction::At(insns + code_item->insns_size_in_code_units_);
252
253 for (; inst < end; inst = inst->Next()) {
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800254 const bool is_virtual = inst->Opcode() == Instruction::INVOKE_VIRTUAL ||
255 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE;
256 const bool is_interface = inst->Opcode() == Instruction::INVOKE_INTERFACE ||
257 inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE;
Vladimir Markoc7f83202014-01-24 17:55:18 +0000258
259 if (!is_interface && !is_virtual) {
260 continue;
261 }
262 // Get reg type for register holding the reference to the object that will be dispatched upon.
263 uint32_t dex_pc = inst->GetDexPc(insns);
264 verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800265 const bool is_range = inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
266 inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE;
Ian Rogersd8f69b02014-09-10 21:43:52 +0000267 const verifier::RegType&
Ian Rogers7b078e82014-09-10 14:44:24 -0700268 reg_type(line->GetRegisterType(method_verifier,
269 is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Vladimir Markoc7f83202014-01-24 17:55:18 +0000270
271 if (!reg_type.HasClass()) {
272 // We will compute devirtualization information only when we know the Class of the reg type.
273 continue;
274 }
275 mirror::Class* reg_class = reg_type.GetClass();
276 if (reg_class->IsInterface()) {
277 // We can't devirtualize when the known type of the register is an interface.
278 continue;
279 }
280 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
281 // We can't devirtualize abstract classes except on arrays of abstract classes.
282 continue;
283 }
284 mirror::ArtMethod* abstract_method = method_verifier->GetDexCache()->GetResolvedMethod(
285 is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700286 if (abstract_method == nullptr) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000287 // If the method is not found in the cache this means that it was never found
288 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
289 continue;
290 }
291 // Find the concrete method.
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800292 mirror::ArtMethod* concrete_method = nullptr;
Vladimir Markoc7f83202014-01-24 17:55:18 +0000293 if (is_interface) {
294 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
295 }
296 if (is_virtual) {
297 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
298 }
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800299 if (concrete_method == nullptr || concrete_method->IsAbstract()) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000300 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
301 continue;
302 }
303 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
304 concrete_method->GetDeclaringClass()->IsFinal()) {
305 // If we knew exactly the class being dispatched upon, or if the target method cannot be
306 // overridden record the target to be used in the compiler driver.
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800307 devirt_map_.Put(dex_pc, concrete_method->ToMethodReference());
Vladimir Markoc7f83202014-01-24 17:55:18 +0000308 }
309 }
310}
311
312void VerifiedMethod::GenerateSafeCastSet(verifier::MethodVerifier* method_verifier) {
313 /*
314 * Walks over the method code and adds any cast instructions in which
315 * the type cast is implicit to a set, which is used in the code generation
316 * to elide these casts.
317 */
318 if (method_verifier->HasFailures()) {
319 return;
320 }
321 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
322 const Instruction* inst = Instruction::At(code_item->insns_);
323 const Instruction* end = Instruction::At(code_item->insns_ +
324 code_item->insns_size_in_code_units_);
325
326 for (; inst < end; inst = inst->Next()) {
327 Instruction::Code code = inst->Opcode();
328 if ((code == Instruction::CHECK_CAST) || (code == Instruction::APUT_OBJECT)) {
329 uint32_t dex_pc = inst->GetDexPc(code_item->insns_);
Stephen Kyle40d35182014-10-03 13:47:56 +0100330 if (!method_verifier->GetInstructionFlags(dex_pc).IsVisited()) {
331 // Do not attempt to quicken this instruction, it's unreachable anyway.
332 continue;
333 }
Vladimir Markoc7f83202014-01-24 17:55:18 +0000334 const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
335 bool is_safe_cast = false;
336 if (code == Instruction::CHECK_CAST) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700337 const verifier::RegType& reg_type(line->GetRegisterType(method_verifier,
338 inst->VRegA_21c()));
Ian Rogersd8f69b02014-09-10 21:43:52 +0000339 const verifier::RegType& cast_type =
Vladimir Markoc7f83202014-01-24 17:55:18 +0000340 method_verifier->ResolveCheckedClass(inst->VRegB_21c());
341 is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type);
342 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -0700343 const verifier::RegType& array_type(line->GetRegisterType(method_verifier,
344 inst->VRegB_23x()));
Vladimir Markoc7f83202014-01-24 17:55:18 +0000345 // We only know its safe to assign to an array if the array type is precise. For example,
346 // an Object[] can have any type of object stored in it, but it may also be assigned a
347 // String[] in which case the stores need to be of Strings.
348 if (array_type.IsPreciseReference()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700349 const verifier::RegType& value_type(line->GetRegisterType(method_verifier,
350 inst->VRegA_23x()));
Ian Rogersd8f69b02014-09-10 21:43:52 +0000351 const verifier::RegType& component_type = method_verifier->GetRegTypeCache()
Vladimir Markoc7f83202014-01-24 17:55:18 +0000352 ->GetComponentType(array_type, method_verifier->GetClassLoader());
353 is_safe_cast = component_type.IsStrictlyAssignableFrom(value_type);
354 }
355 }
356 if (is_safe_cast) {
357 // Verify ordering for push_back() to the sorted vector.
358 DCHECK(safe_cast_set_.empty() || safe_cast_set_.back() < dex_pc);
359 safe_cast_set_.push_back(dex_pc);
360 }
361 }
362 }
363}
364
365} // namespace art