blob: 17328c4a5b2bf087859e3b6942bb90f44eccb39e [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)) {
52 CHECK(method_verifier->HasFailures());
53 return nullptr; // Not a real failure, but a failure to encode.
54 }
55 if (kIsDebugBuild) {
56 VerifyGcMap(method_verifier, verified_method->dex_gc_map_);
57 }
58
59 // TODO: move this out when DEX-to-DEX supports devirtualization.
60 if (method_verifier->HasVirtualOrInterfaceInvokes()) {
61 verified_method->GenerateDevirtMap(method_verifier);
62 }
63 }
64
65 if (method_verifier->HasCheckCasts()) {
66 verified_method->GenerateSafeCastSet(method_verifier);
67 }
68 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
76bool VerifiedMethod::IsSafeCast(uint32_t pc) const {
77 return std::binary_search(safe_cast_set_.begin(), safe_cast_set_.end(), pc);
78}
79
80bool VerifiedMethod::GenerateGcMap(verifier::MethodVerifier* method_verifier) {
81 DCHECK(dex_gc_map_.empty());
82 size_t num_entries, ref_bitmap_bits, pc_bits;
83 ComputeGcMapSizes(method_verifier, &num_entries, &ref_bitmap_bits, &pc_bits);
84 // There's a single byte to encode the size of each bitmap.
85 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
86 // TODO: either a better GC map format or per method failures
87 method_verifier->Fail(verifier::VERIFY_ERROR_BAD_CLASS_HARD)
88 << "Cannot encode GC map for method with " << ref_bitmap_bits << " registers";
89 return false;
90 }
91 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
92 // There are 2 bytes to encode the number of entries.
93 if (num_entries >= 65536) {
94 // TODO: Either a better GC map format or per method failures.
95 method_verifier->Fail(verifier::VERIFY_ERROR_BAD_CLASS_HARD)
96 << "Cannot encode GC map for method with " << num_entries << " entries";
97 return false;
98 }
99 size_t pc_bytes;
100 verifier::RegisterMapFormat format;
101 if (pc_bits <= 8) {
102 format = verifier::kRegMapFormatCompact8;
103 pc_bytes = 1;
104 } else if (pc_bits <= 16) {
105 format = verifier::kRegMapFormatCompact16;
106 pc_bytes = 2;
107 } else {
108 // TODO: Either a better GC map format or per method failures.
109 method_verifier->Fail(verifier::VERIFY_ERROR_BAD_CLASS_HARD)
110 << "Cannot encode GC map for method with "
111 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
112 return false;
113 }
114 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
115 dex_gc_map_.reserve(table_size);
116 // Write table header.
117 dex_gc_map_.push_back(format | ((ref_bitmap_bytes & ~0xFF) >> 5));
118 dex_gc_map_.push_back(ref_bitmap_bytes & 0xFF);
119 dex_gc_map_.push_back(num_entries & 0xFF);
120 dex_gc_map_.push_back((num_entries >> 8) & 0xFF);
121 // Write table data.
122 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
123 for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) {
124 if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) {
125 dex_gc_map_.push_back(i & 0xFF);
126 if (pc_bytes == 2) {
127 dex_gc_map_.push_back((i >> 8) & 0xFF);
128 }
129 verifier::RegisterLine* line = method_verifier->GetRegLine(i);
Ian Rogers7b078e82014-09-10 14:44:24 -0700130 line->WriteReferenceBitMap(method_verifier, &dex_gc_map_, ref_bitmap_bytes);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000131 }
132 }
133 DCHECK_EQ(dex_gc_map_.size(), table_size);
134 return true;
135}
136
137void VerifiedMethod::VerifyGcMap(verifier::MethodVerifier* method_verifier,
138 const std::vector<uint8_t>& data) {
139 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
140 // that the table data is well formed and all references are marked (or not) in the bitmap.
141 verifier::DexPcToReferenceMap map(&data[0]);
142 DCHECK_EQ(data.size(), map.RawSize());
143 size_t map_index = 0;
144 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
145 for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) {
146 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
147 if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) {
148 DCHECK_LT(map_index, map.NumEntries());
149 DCHECK_EQ(map.GetDexPc(map_index), i);
150 DCHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
151 map_index++;
152 verifier::RegisterLine* line = method_verifier->GetRegLine(i);
153 for (size_t j = 0; j < code_item->registers_size_; j++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700154 if (line->GetRegisterType(method_verifier, j).IsNonZeroReferenceTypes()) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000155 DCHECK_LT(j / 8, map.RegWidth());
156 DCHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
157 } else if ((j / 8) < map.RegWidth()) {
158 DCHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
159 } else {
160 // If a register doesn't contain a reference then the bitmap may be shorter than the line.
161 }
162 }
163 } else {
164 DCHECK(reg_bitmap == NULL);
165 }
166 }
167}
168
169void VerifiedMethod::ComputeGcMapSizes(verifier::MethodVerifier* method_verifier,
170 size_t* gc_points, size_t* ref_bitmap_bits,
171 size_t* log2_max_gc_pc) {
172 size_t local_gc_points = 0;
173 size_t max_insn = 0;
174 size_t max_ref_reg = -1;
175 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
176 for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) {
177 if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) {
178 local_gc_points++;
179 max_insn = i;
180 verifier::RegisterLine* line = method_verifier->GetRegLine(i);
Ian Rogers7b078e82014-09-10 14:44:24 -0700181 max_ref_reg = line->GetMaxNonZeroReferenceReg(method_verifier, max_ref_reg);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000182 }
183 }
184 *gc_points = local_gc_points;
185 *ref_bitmap_bits = max_ref_reg + 1; // If max register is 0 we need 1 bit to encode (ie +1).
186 size_t i = 0;
187 while ((1U << i) <= max_insn) {
188 i++;
189 }
190 *log2_max_gc_pc = i;
191}
192
193void VerifiedMethod::GenerateDevirtMap(verifier::MethodVerifier* method_verifier) {
194 // It is risky to rely on reg_types for sharpening in cases of soft
195 // verification, we might end up sharpening to a wrong implementation. Just abort.
196 if (method_verifier->HasFailures()) {
197 return;
198 }
199
200 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
201 const uint16_t* insns = code_item->insns_;
202 const Instruction* inst = Instruction::At(insns);
203 const Instruction* end = Instruction::At(insns + code_item->insns_size_in_code_units_);
204
205 for (; inst < end; inst = inst->Next()) {
206 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
207 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
208 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
209 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
210
211 if (!is_interface && !is_virtual) {
212 continue;
213 }
214 // Get reg type for register holding the reference to the object that will be dispatched upon.
215 uint32_t dex_pc = inst->GetDexPc(insns);
216 verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
217 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
218 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogersd8f69b02014-09-10 21:43:52 +0000219 const verifier::RegType&
Ian Rogers7b078e82014-09-10 14:44:24 -0700220 reg_type(line->GetRegisterType(method_verifier,
221 is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Vladimir Markoc7f83202014-01-24 17:55:18 +0000222
223 if (!reg_type.HasClass()) {
224 // We will compute devirtualization information only when we know the Class of the reg type.
225 continue;
226 }
227 mirror::Class* reg_class = reg_type.GetClass();
228 if (reg_class->IsInterface()) {
229 // We can't devirtualize when the known type of the register is an interface.
230 continue;
231 }
232 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
233 // We can't devirtualize abstract classes except on arrays of abstract classes.
234 continue;
235 }
236 mirror::ArtMethod* abstract_method = method_verifier->GetDexCache()->GetResolvedMethod(
237 is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
238 if (abstract_method == NULL) {
239 // If the method is not found in the cache this means that it was never found
240 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
241 continue;
242 }
243 // Find the concrete method.
244 mirror::ArtMethod* concrete_method = NULL;
245 if (is_interface) {
246 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
247 }
248 if (is_virtual) {
249 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
250 }
251 if (concrete_method == NULL || concrete_method->IsAbstract()) {
252 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
253 continue;
254 }
255 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
256 concrete_method->GetDeclaringClass()->IsFinal()) {
257 // If we knew exactly the class being dispatched upon, or if the target method cannot be
258 // overridden record the target to be used in the compiler driver.
259 MethodReference concrete_ref(
260 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
261 concrete_method->GetDexMethodIndex());
262 devirt_map_.Put(dex_pc, concrete_ref);
263 }
264 }
265}
266
267void VerifiedMethod::GenerateSafeCastSet(verifier::MethodVerifier* method_verifier) {
268 /*
269 * Walks over the method code and adds any cast instructions in which
270 * the type cast is implicit to a set, which is used in the code generation
271 * to elide these casts.
272 */
273 if (method_verifier->HasFailures()) {
274 return;
275 }
276 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
277 const Instruction* inst = Instruction::At(code_item->insns_);
278 const Instruction* end = Instruction::At(code_item->insns_ +
279 code_item->insns_size_in_code_units_);
280
281 for (; inst < end; inst = inst->Next()) {
282 Instruction::Code code = inst->Opcode();
283 if ((code == Instruction::CHECK_CAST) || (code == Instruction::APUT_OBJECT)) {
284 uint32_t dex_pc = inst->GetDexPc(code_item->insns_);
Stephen Kyle40d35182014-10-03 13:47:56 +0100285 if (!method_verifier->GetInstructionFlags(dex_pc).IsVisited()) {
286 // Do not attempt to quicken this instruction, it's unreachable anyway.
287 continue;
288 }
Vladimir Markoc7f83202014-01-24 17:55:18 +0000289 const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
290 bool is_safe_cast = false;
291 if (code == Instruction::CHECK_CAST) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700292 const verifier::RegType& reg_type(line->GetRegisterType(method_verifier,
293 inst->VRegA_21c()));
Ian Rogersd8f69b02014-09-10 21:43:52 +0000294 const verifier::RegType& cast_type =
Vladimir Markoc7f83202014-01-24 17:55:18 +0000295 method_verifier->ResolveCheckedClass(inst->VRegB_21c());
296 is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type);
297 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -0700298 const verifier::RegType& array_type(line->GetRegisterType(method_verifier,
299 inst->VRegB_23x()));
Vladimir Markoc7f83202014-01-24 17:55:18 +0000300 // We only know its safe to assign to an array if the array type is precise. For example,
301 // an Object[] can have any type of object stored in it, but it may also be assigned a
302 // String[] in which case the stores need to be of Strings.
303 if (array_type.IsPreciseReference()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700304 const verifier::RegType& value_type(line->GetRegisterType(method_verifier,
305 inst->VRegA_23x()));
Ian Rogersd8f69b02014-09-10 21:43:52 +0000306 const verifier::RegType& component_type = method_verifier->GetRegTypeCache()
Vladimir Markoc7f83202014-01-24 17:55:18 +0000307 ->GetComponentType(array_type, method_verifier->GetClassLoader());
308 is_safe_cast = component_type.IsStrictlyAssignableFrom(value_type);
309 }
310 }
311 if (is_safe_cast) {
312 // Verify ordering for push_back() to the sorted vector.
313 DCHECK(safe_cast_set_.empty() || safe_cast_set_.back() < dex_pc);
314 safe_cast_set_.push_back(dex_pc);
315 }
316 }
317 }
318}
319
320} // namespace art