blob: 93e9a51da1c6fa00bd34a82db3c8e6f3fbe0c256 [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"
26#include "dex_instruction.h"
27#include "dex_instruction-inl.h"
28#include "base/mutex.h"
29#include "base/mutex-inl.h"
30#include "mirror/art_method.h"
31#include "mirror/art_method-inl.h"
32#include "mirror/class.h"
33#include "mirror/class-inl.h"
34#include "mirror/dex_cache.h"
35#include "mirror/dex_cache-inl.h"
36#include "mirror/object.h"
37#include "mirror/object-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000038#include "verifier/dex_gc_map.h"
39#include "verifier/method_verifier.h"
40#include "verifier/method_verifier-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070041#include "verifier/reg_type-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000042#include "verifier/register_line-inl.h"
43
44namespace art {
45
46const VerifiedMethod* VerifiedMethod::Create(verifier::MethodVerifier* method_verifier,
47 bool compile) {
Ian Rogers700a4022014-05-19 16:49:03 -070048 std::unique_ptr<VerifiedMethod> verified_method(new VerifiedMethod);
Vladimir Markoc7f83202014-01-24 17:55:18 +000049 if (compile) {
50 /* Generate a register map. */
51 if (!verified_method->GenerateGcMap(method_verifier)) {
Vladimir Markoc7f83202014-01-24 17:55:18 +000052 return nullptr; // Not a real failure, but a failure to encode.
53 }
54 if (kIsDebugBuild) {
55 VerifyGcMap(method_verifier, verified_method->dex_gc_map_);
56 }
57
58 // TODO: move this out when DEX-to-DEX supports devirtualization.
59 if (method_verifier->HasVirtualOrInterfaceInvokes()) {
60 verified_method->GenerateDevirtMap(method_verifier);
61 }
62 }
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
75bool VerifiedMethod::IsSafeCast(uint32_t pc) const {
76 return std::binary_search(safe_cast_set_.begin(), safe_cast_set_.end(), pc);
77}
78
79bool VerifiedMethod::GenerateGcMap(verifier::MethodVerifier* method_verifier) {
80 DCHECK(dex_gc_map_.empty());
81 size_t num_entries, ref_bitmap_bits, pc_bits;
82 ComputeGcMapSizes(method_verifier, &num_entries, &ref_bitmap_bits, &pc_bits);
83 // There's a single byte to encode the size of each bitmap.
Mathieu Chartier36b58f52014-12-10 12:06:45 -080084 if (ref_bitmap_bits >= kBitsPerByte * 8192 /* 13-bit size */) {
Andreas Gampe6c170c92014-12-17 14:35:46 -080085 LOG(WARNING) << "Cannot encode GC map for method with " << ref_bitmap_bits << " registers: "
86 << PrettyMethod(method_verifier->GetMethodReference().dex_method_index,
87 *method_verifier->GetMethodReference().dex_file);
Vladimir Markoc7f83202014-01-24 17:55:18 +000088 return false;
89 }
Mathieu Chartier36b58f52014-12-10 12:06:45 -080090 size_t ref_bitmap_bytes = RoundUp(ref_bitmap_bits, kBitsPerByte) / kBitsPerByte;
Vladimir Markoc7f83202014-01-24 17:55:18 +000091 // There are 2 bytes to encode the number of entries.
92 if (num_entries >= 65536) {
Andreas Gampe6c170c92014-12-17 14:35:46 -080093 LOG(WARNING) << "Cannot encode GC map for method with " << num_entries << " entries: "
94 << PrettyMethod(method_verifier->GetMethodReference().dex_method_index,
95 *method_verifier->GetMethodReference().dex_file);
Vladimir Markoc7f83202014-01-24 17:55:18 +000096 return false;
97 }
98 size_t pc_bytes;
99 verifier::RegisterMapFormat format;
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800100 if (pc_bits <= kBitsPerByte) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000101 format = verifier::kRegMapFormatCompact8;
102 pc_bytes = 1;
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800103 } else if (pc_bits <= kBitsPerByte * 2) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000104 format = verifier::kRegMapFormatCompact16;
105 pc_bytes = 2;
106 } else {
Andreas Gampe6c170c92014-12-17 14:35:46 -0800107 LOG(WARNING) << "Cannot encode GC map for method with "
108 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2): "
109 << PrettyMethod(method_verifier->GetMethodReference().dex_method_index,
110 *method_verifier->GetMethodReference().dex_file);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000111 return false;
112 }
113 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
114 dex_gc_map_.reserve(table_size);
115 // Write table header.
116 dex_gc_map_.push_back(format | ((ref_bitmap_bytes & ~0xFF) >> 5));
117 dex_gc_map_.push_back(ref_bitmap_bytes & 0xFF);
118 dex_gc_map_.push_back(num_entries & 0xFF);
119 dex_gc_map_.push_back((num_entries >> 8) & 0xFF);
120 // Write table data.
121 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
122 for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) {
123 if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) {
124 dex_gc_map_.push_back(i & 0xFF);
125 if (pc_bytes == 2) {
126 dex_gc_map_.push_back((i >> 8) & 0xFF);
127 }
128 verifier::RegisterLine* line = method_verifier->GetRegLine(i);
Ian Rogers7b078e82014-09-10 14:44:24 -0700129 line->WriteReferenceBitMap(method_verifier, &dex_gc_map_, ref_bitmap_bytes);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000130 }
131 }
132 DCHECK_EQ(dex_gc_map_.size(), table_size);
133 return true;
134}
135
136void VerifiedMethod::VerifyGcMap(verifier::MethodVerifier* method_verifier,
137 const std::vector<uint8_t>& data) {
138 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
139 // that the table data is well formed and all references are marked (or not) in the bitmap.
140 verifier::DexPcToReferenceMap map(&data[0]);
141 DCHECK_EQ(data.size(), map.RawSize());
142 size_t map_index = 0;
143 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
144 for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) {
145 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
146 if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) {
147 DCHECK_LT(map_index, map.NumEntries());
148 DCHECK_EQ(map.GetDexPc(map_index), i);
149 DCHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
150 map_index++;
151 verifier::RegisterLine* line = method_verifier->GetRegLine(i);
152 for (size_t j = 0; j < code_item->registers_size_; j++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700153 if (line->GetRegisterType(method_verifier, j).IsNonZeroReferenceTypes()) {
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800154 DCHECK_LT(j / kBitsPerByte, map.RegWidth());
155 DCHECK_EQ((reg_bitmap[j / kBitsPerByte] >> (j % kBitsPerByte)) & 1, 1);
156 } else if ((j / kBitsPerByte) < map.RegWidth()) {
157 DCHECK_EQ((reg_bitmap[j / kBitsPerByte] >> (j % kBitsPerByte)) & 1, 0);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000158 } else {
159 // If a register doesn't contain a reference then the bitmap may be shorter than the line.
160 }
161 }
162 } else {
Andreas Gampe6c170c92014-12-17 14:35:46 -0800163 DCHECK(i >= 65536 || reg_bitmap == NULL);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000164 }
165 }
166}
167
168void VerifiedMethod::ComputeGcMapSizes(verifier::MethodVerifier* method_verifier,
169 size_t* gc_points, size_t* ref_bitmap_bits,
170 size_t* log2_max_gc_pc) {
171 size_t local_gc_points = 0;
172 size_t max_insn = 0;
173 size_t max_ref_reg = -1;
174 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
175 for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) {
176 if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) {
177 local_gc_points++;
178 max_insn = i;
179 verifier::RegisterLine* line = method_verifier->GetRegLine(i);
Ian Rogers7b078e82014-09-10 14:44:24 -0700180 max_ref_reg = line->GetMaxNonZeroReferenceReg(method_verifier, max_ref_reg);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000181 }
182 }
183 *gc_points = local_gc_points;
184 *ref_bitmap_bits = max_ref_reg + 1; // If max register is 0 we need 1 bit to encode (ie +1).
185 size_t i = 0;
186 while ((1U << i) <= max_insn) {
187 i++;
188 }
189 *log2_max_gc_pc = i;
190}
191
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800192void VerifiedMethod::GenerateDeQuickenMap(verifier::MethodVerifier* method_verifier) {
193 if (method_verifier->HasFailures()) {
194 return;
195 }
196 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
197 const uint16_t* insns = code_item->insns_;
198 const Instruction* inst = Instruction::At(insns);
199 const Instruction* end = Instruction::At(insns + code_item->insns_size_in_code_units_);
200 for (; inst < end; inst = inst->Next()) {
201 const bool is_virtual_quick = inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK;
202 const bool is_range_quick = inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK;
203 if (is_virtual_quick || is_range_quick) {
204 uint32_t dex_pc = inst->GetDexPc(insns);
205 verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
206 mirror::ArtMethod* method = method_verifier->GetQuickInvokedMethod(inst, line,
207 is_range_quick);
208 CHECK(method != nullptr);
209 // The verifier must know what the type of the object was or else we would have gotten a
210 // failure. Put the dex method index in the dequicken map since we need this to get number of
211 // arguments in the compiler.
212 dequicken_map_.Put(dex_pc, method->ToMethodReference());
213 }
214 }
215}
216
Vladimir Markoc7f83202014-01-24 17:55:18 +0000217void VerifiedMethod::GenerateDevirtMap(verifier::MethodVerifier* method_verifier) {
218 // It is risky to rely on reg_types for sharpening in cases of soft
219 // verification, we might end up sharpening to a wrong implementation. Just abort.
220 if (method_verifier->HasFailures()) {
221 return;
222 }
223
224 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
225 const uint16_t* insns = code_item->insns_;
226 const Instruction* inst = Instruction::At(insns);
227 const Instruction* end = Instruction::At(insns + code_item->insns_size_in_code_units_);
228
229 for (; inst < end; inst = inst->Next()) {
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800230 const bool is_virtual = inst->Opcode() == Instruction::INVOKE_VIRTUAL ||
231 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE;
232 const bool is_interface = inst->Opcode() == Instruction::INVOKE_INTERFACE ||
233 inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE;
Vladimir Markoc7f83202014-01-24 17:55:18 +0000234
235 if (!is_interface && !is_virtual) {
236 continue;
237 }
238 // Get reg type for register holding the reference to the object that will be dispatched upon.
239 uint32_t dex_pc = inst->GetDexPc(insns);
240 verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800241 const bool is_range = inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
242 inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE;
Ian Rogersd8f69b02014-09-10 21:43:52 +0000243 const verifier::RegType&
Ian Rogers7b078e82014-09-10 14:44:24 -0700244 reg_type(line->GetRegisterType(method_verifier,
245 is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Vladimir Markoc7f83202014-01-24 17:55:18 +0000246
247 if (!reg_type.HasClass()) {
248 // We will compute devirtualization information only when we know the Class of the reg type.
249 continue;
250 }
251 mirror::Class* reg_class = reg_type.GetClass();
252 if (reg_class->IsInterface()) {
253 // We can't devirtualize when the known type of the register is an interface.
254 continue;
255 }
256 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
257 // We can't devirtualize abstract classes except on arrays of abstract classes.
258 continue;
259 }
260 mirror::ArtMethod* abstract_method = method_verifier->GetDexCache()->GetResolvedMethod(
261 is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
262 if (abstract_method == NULL) {
263 // If the method is not found in the cache this means that it was never found
264 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
265 continue;
266 }
267 // Find the concrete method.
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800268 mirror::ArtMethod* concrete_method = nullptr;
Vladimir Markoc7f83202014-01-24 17:55:18 +0000269 if (is_interface) {
270 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
271 }
272 if (is_virtual) {
273 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
274 }
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800275 if (concrete_method == nullptr || concrete_method->IsAbstract()) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000276 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
277 continue;
278 }
279 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
280 concrete_method->GetDeclaringClass()->IsFinal()) {
281 // If we knew exactly the class being dispatched upon, or if the target method cannot be
282 // overridden record the target to be used in the compiler driver.
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800283 devirt_map_.Put(dex_pc, concrete_method->ToMethodReference());
Vladimir Markoc7f83202014-01-24 17:55:18 +0000284 }
285 }
286}
287
288void VerifiedMethod::GenerateSafeCastSet(verifier::MethodVerifier* method_verifier) {
289 /*
290 * Walks over the method code and adds any cast instructions in which
291 * the type cast is implicit to a set, which is used in the code generation
292 * to elide these casts.
293 */
294 if (method_verifier->HasFailures()) {
295 return;
296 }
297 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
298 const Instruction* inst = Instruction::At(code_item->insns_);
299 const Instruction* end = Instruction::At(code_item->insns_ +
300 code_item->insns_size_in_code_units_);
301
302 for (; inst < end; inst = inst->Next()) {
303 Instruction::Code code = inst->Opcode();
304 if ((code == Instruction::CHECK_CAST) || (code == Instruction::APUT_OBJECT)) {
305 uint32_t dex_pc = inst->GetDexPc(code_item->insns_);
Stephen Kyle40d35182014-10-03 13:47:56 +0100306 if (!method_verifier->GetInstructionFlags(dex_pc).IsVisited()) {
307 // Do not attempt to quicken this instruction, it's unreachable anyway.
308 continue;
309 }
Vladimir Markoc7f83202014-01-24 17:55:18 +0000310 const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
311 bool is_safe_cast = false;
312 if (code == Instruction::CHECK_CAST) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700313 const verifier::RegType& reg_type(line->GetRegisterType(method_verifier,
314 inst->VRegA_21c()));
Ian Rogersd8f69b02014-09-10 21:43:52 +0000315 const verifier::RegType& cast_type =
Vladimir Markoc7f83202014-01-24 17:55:18 +0000316 method_verifier->ResolveCheckedClass(inst->VRegB_21c());
317 is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type);
318 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -0700319 const verifier::RegType& array_type(line->GetRegisterType(method_verifier,
320 inst->VRegB_23x()));
Vladimir Markoc7f83202014-01-24 17:55:18 +0000321 // We only know its safe to assign to an array if the array type is precise. For example,
322 // an Object[] can have any type of object stored in it, but it may also be assigned a
323 // String[] in which case the stores need to be of Strings.
324 if (array_type.IsPreciseReference()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700325 const verifier::RegType& value_type(line->GetRegisterType(method_verifier,
326 inst->VRegA_23x()));
Ian Rogersd8f69b02014-09-10 21:43:52 +0000327 const verifier::RegType& component_type = method_verifier->GetRegTypeCache()
Vladimir Markoc7f83202014-01-24 17:55:18 +0000328 ->GetComponentType(array_type, method_verifier->GetClassLoader());
329 is_safe_cast = component_type.IsStrictlyAssignableFrom(value_type);
330 }
331 }
332 if (is_safe_cast) {
333 // Verify ordering for push_back() to the sorted vector.
334 DCHECK(safe_cast_set_.empty() || safe_cast_set_.back() < dex_pc);
335 safe_cast_set_.push_back(dex_pc);
336 }
337 }
338 }
339}
340
341} // namespace art