blob: 5b90ba92e0edfa03924460de9d641a22548a59dc [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);
Vladimir Markoc7f83202014-01-24 17:55:18 +000043 if (compile) {
44 /* Generate a register map. */
45 if (!verified_method->GenerateGcMap(method_verifier)) {
Vladimir Markoc7f83202014-01-24 17:55:18 +000046 return nullptr; // Not a real failure, but a failure to encode.
47 }
48 if (kIsDebugBuild) {
49 VerifyGcMap(method_verifier, verified_method->dex_gc_map_);
50 }
51
52 // 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.
Mathieu Chartier091d2382015-03-06 10:59:06 -080058 if (Runtime::Current()->UseJit() && !verified_method->GenerateDequickenMap(method_verifier)) {
59 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 }
66 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
84bool VerifiedMethod::GenerateGcMap(verifier::MethodVerifier* method_verifier) {
85 DCHECK(dex_gc_map_.empty());
86 size_t num_entries, ref_bitmap_bits, pc_bits;
87 ComputeGcMapSizes(method_verifier, &num_entries, &ref_bitmap_bits, &pc_bits);
88 // There's a single byte to encode the size of each bitmap.
Mathieu Chartier36b58f52014-12-10 12:06:45 -080089 if (ref_bitmap_bits >= kBitsPerByte * 8192 /* 13-bit size */) {
Andreas Gampe6c170c92014-12-17 14:35:46 -080090 LOG(WARNING) << "Cannot encode GC map for method with " << ref_bitmap_bits << " registers: "
91 << PrettyMethod(method_verifier->GetMethodReference().dex_method_index,
92 *method_verifier->GetMethodReference().dex_file);
Vladimir Markoc7f83202014-01-24 17:55:18 +000093 return false;
94 }
Mathieu Chartier36b58f52014-12-10 12:06:45 -080095 size_t ref_bitmap_bytes = RoundUp(ref_bitmap_bits, kBitsPerByte) / kBitsPerByte;
Vladimir Markoc7f83202014-01-24 17:55:18 +000096 // There are 2 bytes to encode the number of entries.
97 if (num_entries >= 65536) {
Andreas Gampe6c170c92014-12-17 14:35:46 -080098 LOG(WARNING) << "Cannot encode GC map for method with " << num_entries << " entries: "
99 << PrettyMethod(method_verifier->GetMethodReference().dex_method_index,
100 *method_verifier->GetMethodReference().dex_file);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000101 return false;
102 }
103 size_t pc_bytes;
104 verifier::RegisterMapFormat format;
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800105 if (pc_bits <= kBitsPerByte) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000106 format = verifier::kRegMapFormatCompact8;
107 pc_bytes = 1;
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800108 } else if (pc_bits <= kBitsPerByte * 2) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000109 format = verifier::kRegMapFormatCompact16;
110 pc_bytes = 2;
111 } else {
Andreas Gampe6c170c92014-12-17 14:35:46 -0800112 LOG(WARNING) << "Cannot encode GC map for method with "
113 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2): "
114 << PrettyMethod(method_verifier->GetMethodReference().dex_method_index,
115 *method_verifier->GetMethodReference().dex_file);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000116 return false;
117 }
118 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
119 dex_gc_map_.reserve(table_size);
120 // Write table header.
121 dex_gc_map_.push_back(format | ((ref_bitmap_bytes & ~0xFF) >> 5));
122 dex_gc_map_.push_back(ref_bitmap_bytes & 0xFF);
123 dex_gc_map_.push_back(num_entries & 0xFF);
124 dex_gc_map_.push_back((num_entries >> 8) & 0xFF);
125 // Write table data.
126 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
127 for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) {
128 if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) {
129 dex_gc_map_.push_back(i & 0xFF);
130 if (pc_bytes == 2) {
131 dex_gc_map_.push_back((i >> 8) & 0xFF);
132 }
133 verifier::RegisterLine* line = method_verifier->GetRegLine(i);
Ian Rogers7b078e82014-09-10 14:44:24 -0700134 line->WriteReferenceBitMap(method_verifier, &dex_gc_map_, ref_bitmap_bytes);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000135 }
136 }
137 DCHECK_EQ(dex_gc_map_.size(), table_size);
138 return true;
139}
140
141void VerifiedMethod::VerifyGcMap(verifier::MethodVerifier* method_verifier,
142 const std::vector<uint8_t>& data) {
143 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
144 // that the table data is well formed and all references are marked (or not) in the bitmap.
145 verifier::DexPcToReferenceMap map(&data[0]);
146 DCHECK_EQ(data.size(), map.RawSize());
147 size_t map_index = 0;
148 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
149 for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) {
150 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
151 if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) {
152 DCHECK_LT(map_index, map.NumEntries());
153 DCHECK_EQ(map.GetDexPc(map_index), i);
154 DCHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
155 map_index++;
156 verifier::RegisterLine* line = method_verifier->GetRegLine(i);
157 for (size_t j = 0; j < code_item->registers_size_; j++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700158 if (line->GetRegisterType(method_verifier, j).IsNonZeroReferenceTypes()) {
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800159 DCHECK_LT(j / kBitsPerByte, map.RegWidth());
160 DCHECK_EQ((reg_bitmap[j / kBitsPerByte] >> (j % kBitsPerByte)) & 1, 1);
161 } else if ((j / kBitsPerByte) < map.RegWidth()) {
162 DCHECK_EQ((reg_bitmap[j / kBitsPerByte] >> (j % kBitsPerByte)) & 1, 0);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000163 } else {
164 // If a register doesn't contain a reference then the bitmap may be shorter than the line.
165 }
166 }
167 } else {
Andreas Gampe6c170c92014-12-17 14:35:46 -0800168 DCHECK(i >= 65536 || reg_bitmap == NULL);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000169 }
170 }
171}
172
173void VerifiedMethod::ComputeGcMapSizes(verifier::MethodVerifier* method_verifier,
174 size_t* gc_points, size_t* ref_bitmap_bits,
175 size_t* log2_max_gc_pc) {
176 size_t local_gc_points = 0;
177 size_t max_insn = 0;
178 size_t max_ref_reg = -1;
179 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
180 for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) {
181 if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) {
182 local_gc_points++;
183 max_insn = i;
184 verifier::RegisterLine* line = method_verifier->GetRegLine(i);
Ian Rogers7b078e82014-09-10 14:44:24 -0700185 max_ref_reg = line->GetMaxNonZeroReferenceReg(method_verifier, max_ref_reg);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000186 }
187 }
188 *gc_points = local_gc_points;
189 *ref_bitmap_bits = max_ref_reg + 1; // If max register is 0 we need 1 bit to encode (ie +1).
190 size_t i = 0;
191 while ((1U << i) <= max_insn) {
192 i++;
193 }
194 *log2_max_gc_pc = i;
195}
196
Mathieu Chartier091d2382015-03-06 10:59:06 -0800197bool VerifiedMethod::GenerateDequickenMap(verifier::MethodVerifier* method_verifier) {
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800198 if (method_verifier->HasFailures()) {
Mathieu Chartier091d2382015-03-06 10:59:06 -0800199 return false;
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800200 }
201 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
202 const uint16_t* insns = code_item->insns_;
203 const Instruction* inst = Instruction::At(insns);
204 const Instruction* end = Instruction::At(insns + code_item->insns_size_in_code_units_);
205 for (; inst < end; inst = inst->Next()) {
206 const bool is_virtual_quick = inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK;
207 const bool is_range_quick = inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK;
208 if (is_virtual_quick || is_range_quick) {
209 uint32_t dex_pc = inst->GetDexPc(insns);
210 verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800211 mirror::ArtMethod* method =
Mathieu Chartier091d2382015-03-06 10:59:06 -0800212 method_verifier->GetQuickInvokedMethod(inst, line, is_range_quick, true);
213 if (method == nullptr) {
214 // It can be null if the line wasn't verified since it was unreachable.
215 return false;
216 }
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800217 // The verifier must know what the type of the object was or else we would have gotten a
218 // failure. Put the dex method index in the dequicken map since we need this to get number of
219 // arguments in the compiler.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800220 dequicken_map_.Put(dex_pc, DexFileReference(method->GetDexFile(),
221 method->GetDexMethodIndex()));
222 } else if (IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) {
223 uint32_t dex_pc = inst->GetDexPc(insns);
224 verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
225 mirror::ArtField* field = method_verifier->GetQuickFieldAccess(inst, line);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800226 if (field == nullptr) {
227 // It can be null if the line wasn't verified since it was unreachable.
228 return false;
229 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800230 // The verifier must know what the type of the field was or else we would have gotten a
231 // failure. Put the dex field index in the dequicken map since we need this for lowering
232 // in the compiler.
233 // TODO: Putting a field index in a method reference is gross.
234 dequicken_map_.Put(dex_pc, DexFileReference(field->GetDexFile(), field->GetDexFieldIndex()));
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800235 }
236 }
Mathieu Chartier091d2382015-03-06 10:59:06 -0800237 return true;
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800238}
239
Vladimir Markoc7f83202014-01-24 17:55:18 +0000240void VerifiedMethod::GenerateDevirtMap(verifier::MethodVerifier* method_verifier) {
241 // It is risky to rely on reg_types for sharpening in cases of soft
242 // verification, we might end up sharpening to a wrong implementation. Just abort.
243 if (method_verifier->HasFailures()) {
244 return;
245 }
246
247 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
248 const uint16_t* insns = code_item->insns_;
249 const Instruction* inst = Instruction::At(insns);
250 const Instruction* end = Instruction::At(insns + code_item->insns_size_in_code_units_);
251
252 for (; inst < end; inst = inst->Next()) {
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800253 const bool is_virtual = inst->Opcode() == Instruction::INVOKE_VIRTUAL ||
254 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE;
255 const bool is_interface = inst->Opcode() == Instruction::INVOKE_INTERFACE ||
256 inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE;
Vladimir Markoc7f83202014-01-24 17:55:18 +0000257
258 if (!is_interface && !is_virtual) {
259 continue;
260 }
261 // Get reg type for register holding the reference to the object that will be dispatched upon.
262 uint32_t dex_pc = inst->GetDexPc(insns);
263 verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800264 const bool is_range = inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
265 inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE;
Ian Rogersd8f69b02014-09-10 21:43:52 +0000266 const verifier::RegType&
Ian Rogers7b078e82014-09-10 14:44:24 -0700267 reg_type(line->GetRegisterType(method_verifier,
268 is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Vladimir Markoc7f83202014-01-24 17:55:18 +0000269
270 if (!reg_type.HasClass()) {
271 // We will compute devirtualization information only when we know the Class of the reg type.
272 continue;
273 }
274 mirror::Class* reg_class = reg_type.GetClass();
275 if (reg_class->IsInterface()) {
276 // We can't devirtualize when the known type of the register is an interface.
277 continue;
278 }
279 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
280 // We can't devirtualize abstract classes except on arrays of abstract classes.
281 continue;
282 }
283 mirror::ArtMethod* abstract_method = method_verifier->GetDexCache()->GetResolvedMethod(
284 is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
285 if (abstract_method == NULL) {
286 // If the method is not found in the cache this means that it was never found
287 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
288 continue;
289 }
290 // Find the concrete method.
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800291 mirror::ArtMethod* concrete_method = nullptr;
Vladimir Markoc7f83202014-01-24 17:55:18 +0000292 if (is_interface) {
293 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
294 }
295 if (is_virtual) {
296 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
297 }
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800298 if (concrete_method == nullptr || concrete_method->IsAbstract()) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000299 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
300 continue;
301 }
302 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
303 concrete_method->GetDeclaringClass()->IsFinal()) {
304 // If we knew exactly the class being dispatched upon, or if the target method cannot be
305 // overridden record the target to be used in the compiler driver.
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800306 devirt_map_.Put(dex_pc, concrete_method->ToMethodReference());
Vladimir Markoc7f83202014-01-24 17:55:18 +0000307 }
308 }
309}
310
311void VerifiedMethod::GenerateSafeCastSet(verifier::MethodVerifier* method_verifier) {
312 /*
313 * Walks over the method code and adds any cast instructions in which
314 * the type cast is implicit to a set, which is used in the code generation
315 * to elide these casts.
316 */
317 if (method_verifier->HasFailures()) {
318 return;
319 }
320 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
321 const Instruction* inst = Instruction::At(code_item->insns_);
322 const Instruction* end = Instruction::At(code_item->insns_ +
323 code_item->insns_size_in_code_units_);
324
325 for (; inst < end; inst = inst->Next()) {
326 Instruction::Code code = inst->Opcode();
327 if ((code == Instruction::CHECK_CAST) || (code == Instruction::APUT_OBJECT)) {
328 uint32_t dex_pc = inst->GetDexPc(code_item->insns_);
Stephen Kyle40d35182014-10-03 13:47:56 +0100329 if (!method_verifier->GetInstructionFlags(dex_pc).IsVisited()) {
330 // Do not attempt to quicken this instruction, it's unreachable anyway.
331 continue;
332 }
Vladimir Markoc7f83202014-01-24 17:55:18 +0000333 const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
334 bool is_safe_cast = false;
335 if (code == Instruction::CHECK_CAST) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700336 const verifier::RegType& reg_type(line->GetRegisterType(method_verifier,
337 inst->VRegA_21c()));
Ian Rogersd8f69b02014-09-10 21:43:52 +0000338 const verifier::RegType& cast_type =
Vladimir Markoc7f83202014-01-24 17:55:18 +0000339 method_verifier->ResolveCheckedClass(inst->VRegB_21c());
340 is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type);
341 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -0700342 const verifier::RegType& array_type(line->GetRegisterType(method_verifier,
343 inst->VRegB_23x()));
Vladimir Markoc7f83202014-01-24 17:55:18 +0000344 // We only know its safe to assign to an array if the array type is precise. For example,
345 // an Object[] can have any type of object stored in it, but it may also be assigned a
346 // String[] in which case the stores need to be of Strings.
347 if (array_type.IsPreciseReference()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700348 const verifier::RegType& value_type(line->GetRegisterType(method_verifier,
349 inst->VRegA_23x()));
Ian Rogersd8f69b02014-09-10 21:43:52 +0000350 const verifier::RegType& component_type = method_verifier->GetRegTypeCache()
Vladimir Markoc7f83202014-01-24 17:55:18 +0000351 ->GetComponentType(array_type, method_verifier->GetClassLoader());
352 is_safe_cast = component_type.IsStrictlyAssignableFrom(value_type);
353 }
354 }
355 if (is_safe_cast) {
356 // Verify ordering for push_back() to the sorted vector.
357 DCHECK(safe_cast_set_.empty() || safe_cast_set_.back() < dex_pc);
358 safe_cast_set_.push_back(dex_pc);
359 }
360 }
361 }
362}
363
364} // namespace art