blob: ebb71b218ef02515c0ddbfc8c3307c49a77e6afd [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "dex_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004
Elliott Hughes1f359b02011-07-17 14:27:17 -07005#include <iostream>
6
Brian Carlstrom1f870082011-08-23 16:02:11 -07007#include "class_linker.h"
jeffhaob4df5142011-09-19 20:25:32 -07008#include "dex_cache.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -07009#include "dex_file.h"
10#include "dex_instruction.h"
11#include "dex_instruction_visitor.h"
jeffhaobdb76512011-09-07 11:43:16 -070012#include "dex_verifier.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070013#include "logging.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070014#include "runtime.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070015#include "stringpiece.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
17namespace art {
18
jeffhaobdb76512011-09-07 11:43:16 -070019#define k_ kRegTypeUnknown
20#define kU kRegTypeUninit
21#define kX kRegTypeConflict
22#define k0 kRegTypeZero
23#define k1 kRegTypeOne
24#define kZ kRegTypeBoolean
25#define ky kRegTypeConstPosByte
26#define kY kRegTypeConstByte
27#define kh kRegTypeConstPosShort
28#define kH kRegTypeConstShort
29#define kc kRegTypeConstChar
30#define ki kRegTypeConstInteger
31#define kb kRegTypePosByte
32#define kB kRegTypeByte
33#define ks kRegTypePosShort
34#define kS kRegTypeShort
35#define kC kRegTypeChar
36#define kI kRegTypeInteger
37#define kF kRegTypeFloat
38#define kN kRegTypeConstLo
39#define kn kRegTypeConstHi
40#define kJ kRegTypeLongLo
41#define kj kRegTypeLongHi
42#define kD kRegTypeDoubleLo
43#define kd kRegTypeDoubleHi
44
45const char DexVerifier::merge_table_[kRegTypeMAX][kRegTypeMAX] =
46 {
47 /* chk: _ U X 0 1 Z y Y h H c i b B s S C I F N n J j D d */
48 { /*_*/ k_,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX },
49 { /*U*/ kX,kU,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX },
50 { /*X*/ kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX },
51 { /*0*/ kX,kX,kX,k0,kZ,kZ,ky,kY,kh,kH,kc,ki,kb,kB,ks,kS,kC,kI,kF,kX,kX,kX,kX,kX,kX },
52 { /*1*/ kX,kX,kX,kZ,k1,kZ,ky,kY,kh,kH,kc,ki,kb,kB,ks,kS,kC,kI,kF,kX,kX,kX,kX,kX,kX },
53 { /*Z*/ kX,kX,kX,kZ,kZ,kZ,ky,kY,kh,kH,kc,ki,kb,kB,ks,kS,kC,kI,kF,kX,kX,kX,kX,kX,kX },
54 { /*y*/ kX,kX,kX,ky,ky,ky,ky,kY,kh,kH,kc,ki,kb,kB,ks,kS,kC,kI,kF,kX,kX,kX,kX,kX,kX },
55 { /*Y*/ kX,kX,kX,kY,kY,kY,kY,kY,kh,kH,kc,ki,kB,kB,kS,kS,kI,kI,kF,kX,kX,kX,kX,kX,kX },
56 { /*h*/ kX,kX,kX,kh,kh,kh,kh,kh,kh,kH,kc,ki,ks,kS,ks,kS,kC,kI,kF,kX,kX,kX,kX,kX,kX },
57 { /*H*/ kX,kX,kX,kH,kH,kH,kH,kH,kH,kH,kc,ki,kS,kS,kS,kS,kI,kI,kF,kX,kX,kX,kX,kX,kX },
58 { /*c*/ kX,kX,kX,kc,kc,kc,kc,kc,kc,kc,kc,ki,kC,kI,kC,kI,kC,kI,kF,kX,kX,kX,kX,kX,kX },
59 { /*i*/ kX,kX,kX,ki,ki,ki,ki,ki,ki,ki,ki,ki,kI,kI,kI,kI,kI,kI,kF,kX,kX,kX,kX,kX,kX },
60 { /*b*/ kX,kX,kX,kb,kb,kb,kb,kB,ks,kS,kC,kI,kb,kB,ks,kS,kC,kI,kX,kX,kX,kX,kX,kX,kX },
61 { /*B*/ kX,kX,kX,kB,kB,kB,kB,kB,kS,kS,kI,kI,kB,kB,kS,kS,kI,kI,kX,kX,kX,kX,kX,kX,kX },
62 { /*s*/ kX,kX,kX,ks,ks,ks,ks,kS,ks,kS,kC,kI,ks,kS,ks,kS,kC,kI,kX,kX,kX,kX,kX,kX,kX },
63 { /*S*/ kX,kX,kX,kS,kS,kS,kS,kS,kS,kS,kI,kI,kS,kS,kS,kS,kI,kI,kX,kX,kX,kX,kX,kX,kX },
64 { /*C*/ kX,kX,kX,kC,kC,kC,kC,kI,kC,kI,kC,kI,kC,kI,kC,kI,kC,kI,kX,kX,kX,kX,kX,kX,kX },
65 { /*I*/ kX,kX,kX,kI,kI,kI,kI,kI,kI,kI,kI,kI,kI,kI,kI,kI,kI,kI,kX,kX,kX,kX,kX,kX,kX },
66 { /*F*/ kX,kX,kX,kF,kF,kF,kF,kF,kF,kF,kF,kF,kX,kX,kX,kX,kX,kX,kF,kX,kX,kX,kX,kX,kX },
67 { /*N*/ kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kN,kX,kJ,kX,kD,kX },
68 { /*n*/ kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kn,kX,kj,kX,kd },
69 { /*J*/ kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kJ,kX,kJ,kX,kX,kX },
70 { /*j*/ kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kj,kX,kj,kX,kX },
71 { /*D*/ kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kD,kX,kX,kX,kD,kX },
72 { /*d*/ kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kd,kX,kX,kX,kd },
73 };
74
75#undef k_
76#undef kU
77#undef kX
78#undef k0
79#undef k1
80#undef kZ
81#undef ky
82#undef kY
83#undef kh
84#undef kH
85#undef kc
86#undef ki
87#undef kb
88#undef kB
89#undef ks
90#undef kS
91#undef kC
92#undef kI
93#undef kF
94#undef kN
95#undef kn
96#undef kJ
97#undef kj
98#undef kD
99#undef kd
100
101bool DexVerifier::VerifyClass(Class* klass) {
102 if (klass->IsVerified()) {
103 return true;
104 }
105 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
106 Method* method = klass->GetDirectMethod(i);
107 if (!VerifyMethod(method)) {
jeffhao98eacac2011-09-14 16:11:53 -0700108 LOG(ERROR) << "Verifier rejected class "
109 << klass->GetDescriptor()->ToModifiedUtf8();
jeffhaobdb76512011-09-07 11:43:16 -0700110 return false;
111 }
112 }
113 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
114 Method* method = klass->GetVirtualMethod(i);
115 if (!VerifyMethod(method)) {
jeffhao98eacac2011-09-14 16:11:53 -0700116 LOG(ERROR) << "Verifier rejected class "
117 << klass->GetDescriptor()->ToModifiedUtf8();
jeffhaobdb76512011-09-07 11:43:16 -0700118 return false;
119 }
120 }
121 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700122}
123
jeffhaobdb76512011-09-07 11:43:16 -0700124bool DexVerifier::VerifyMethod(Method* method) {
125 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
126 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
127 const DexFile& dex_file = class_linker->FindDexFile(dex_cache);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700128 const DexFile::CodeItem* code_item =
jeffhaobdb76512011-09-07 11:43:16 -0700129 dex_file.GetCodeItem(method->GetCodeItemOffset());
130
131 /*
132 * Construct the verifier state container object.
133 */
134 VerifierData vdata(method, &dex_file, code_item);
135
136 /*
137 * If there aren't any instructions, make sure that's expected, then
138 * exit successfully.
139 */
140 if (code_item == NULL) {
141 if (!method->IsNative() && !method->IsAbstract()) {
142 LOG(ERROR) << "VFY: zero-length code in concrete non-native method";
143 return false;
144 }
145 return true;
146 }
147
148 /*
jeffhaod1f0fde2011-09-08 17:25:33 -0700149 * Sanity-check the register counts. ins + locals = registers, so make
jeffhaobdb76512011-09-07 11:43:16 -0700150 * sure that ins <= registers.
151 */
152 if (code_item->ins_size_ > code_item->registers_size_) {
153 LOG(ERROR) << "VFY: bad register counts (ins=" << code_item->ins_size_
154 << " regs=" << code_item->registers_size_;
155 return false;
156 }
157
158 /*
159 * Allocate and initialize an array to hold instruction data.
160 */
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700161 vdata.insn_flags_.reset(new InsnFlags[code_item->insns_size_]());
jeffhaobdb76512011-09-07 11:43:16 -0700162
163 /*
164 * Run through the instructions and see if the width checks out.
165 */
166 if (!ComputeWidthsAndCountOps(&vdata)) {
167 return false;
168 }
169
170 /*
171 * Flag instructions guarded by a "try" block and check exception handlers.
172 */
173 if (!ScanTryCatchBlocks(&vdata)) {
174 return false;
175 }
176
177 /*
178 * Perform static instruction verification.
179 */
180 if (!VerifyInstructions(&vdata)) {
181 return false;
182 }
183
184 /*
185 * Perform code flow analysis.
186 */
187 if (!VerifyCodeFlow(&vdata)) {
188 return false;
189 }
190
191 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700192}
193
jeffhaobdb76512011-09-07 11:43:16 -0700194bool DexVerifier::VerifyInstructions(VerifierData* vdata) {
195 const DexFile::CodeItem* code_item = vdata->code_item_;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700196 InsnFlags* insn_flags = vdata->insn_flags_.get();
jeffhaobdb76512011-09-07 11:43:16 -0700197 const byte* ptr = reinterpret_cast<const byte*>(code_item->insns_);
198 const Instruction* inst = Instruction::At(ptr);
199
200 /* Flag the start of the method as a branch target. */
201 InsnSetBranchTarget(insn_flags, 0);
202
203 uint32_t width = 0;
204 uint32_t insns_size = code_item->insns_size_;
205
206 while (width < insns_size) {
207 if (!VerifyInstruction(vdata, inst, width)) {
jeffhaod1f0fde2011-09-08 17:25:33 -0700208 LOG(ERROR) << "VFY: rejecting opcode 0x" << std::hex
jeffhaobdb76512011-09-07 11:43:16 -0700209 << (int) inst->Opcode() << " at 0x" << width << std::dec;
210 return false;
211 }
212
213 /* Flag instructions that are garbage collection points */
214 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() ||
215 inst->IsReturn()) {
216 InsnSetGcPoint(insn_flags, width);
217 }
218
219 width += inst->Size();
220 inst = inst->Next();
221 }
222 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700223}
224
jeffhaobdb76512011-09-07 11:43:16 -0700225bool DexVerifier::VerifyInstruction(VerifierData* vdata,
226 const Instruction* inst, uint32_t code_offset) {
227 const DexFile* dex_file = vdata->dex_file_;
228 const DexFile::CodeItem* code_item = vdata->code_item_;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700229 InsnFlags* insn_flags = vdata->insn_flags_.get();
jeffhaobdb76512011-09-07 11:43:16 -0700230 Instruction::DecodedInstruction dec_insn(inst);
231 bool result = true;
232
233 int argumentA = inst->GetVerifyTypeArgumentA();
234 int argumentB = inst->GetVerifyTypeArgumentB();
235 int argumentC = inst->GetVerifyTypeArgumentC();
236 int extra_flags = inst->GetVerifyExtraFlags();
237
238 switch (argumentA) {
239 case Instruction::kVerifyRegA:
240 result &= CheckRegisterIndex(code_item, dec_insn.vA_);
241 break;
242 case Instruction::kVerifyRegAWide:
243 result &= CheckWideRegisterIndex(code_item, dec_insn.vA_);
244 break;
245 }
246
247 switch (argumentB) {
248 case Instruction::kVerifyRegB:
249 result &= CheckRegisterIndex(code_item, dec_insn.vB_);
250 break;
251 case Instruction::kVerifyRegBField:
252 result &= CheckFieldIndex(dex_file, dec_insn.vB_);
253 break;
254 case Instruction::kVerifyRegBMethod:
255 result &= CheckMethodIndex(dex_file, dec_insn.vB_);
256 break;
257 case Instruction::kVerifyRegBNewInstance:
258 result &= CheckNewInstance(dex_file, dec_insn.vB_);
259 break;
260 case Instruction::kVerifyRegBString:
261 result &= CheckStringIndex(dex_file, dec_insn.vB_);
262 break;
263 case Instruction::kVerifyRegBType:
264 result &= CheckTypeIndex(dex_file, dec_insn.vB_);
265 break;
266 case Instruction::kVerifyRegBWide:
267 result &= CheckWideRegisterIndex(code_item, dec_insn.vB_);
268 break;
269 }
270
271 switch (argumentC) {
272 case Instruction::kVerifyRegC:
273 result &= CheckRegisterIndex(code_item, dec_insn.vC_);
274 break;
275 case Instruction::kVerifyRegCField:
276 result &= CheckFieldIndex(dex_file, dec_insn.vC_);
277 break;
278 case Instruction::kVerifyRegCNewArray:
279 result &= CheckNewArray(dex_file, dec_insn.vC_);
280 break;
281 case Instruction::kVerifyRegCType:
282 result &= CheckTypeIndex(dex_file, dec_insn.vC_);
283 break;
284 case Instruction::kVerifyRegCWide:
285 result &= CheckWideRegisterIndex(code_item, dec_insn.vC_);
286 break;
287 }
288
289 switch (extra_flags) {
290 case Instruction::kVerifyArrayData:
291 result &= CheckArrayData(code_item, code_offset);
292 break;
293 case Instruction::kVerifyBranchTarget:
294 result &= CheckBranchTarget(code_item, insn_flags, code_offset);
295 break;
296 case Instruction::kVerifySwitchTargets:
297 result &= CheckSwitchTargets(code_item, insn_flags, code_offset);
298 break;
299 case Instruction::kVerifyVarArg:
300 result &= CheckVarArgRegs(code_item, dec_insn.vA_, dec_insn.arg_);
301 break;
302 case Instruction::kVerifyVarArgRange:
303 result &= CheckVarArgRangeRegs(code_item, dec_insn.vA_, dec_insn.vC_);
304 break;
305 case Instruction::kVerifyError:
306 LOG(ERROR) << "VFY: unexpected opcode " << std::hex
307 << (int) dec_insn.opcode_ << std::dec;
308 result = false;
309 break;
310 }
311
312 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700313}
314
jeffhaobdb76512011-09-07 11:43:16 -0700315bool DexVerifier::VerifyCodeFlow(VerifierData* vdata) {
316 Method* method = vdata->method_;
317 const DexFile::CodeItem* code_item = vdata->code_item_;
318 uint16_t registers_size = code_item->registers_size_;
319 uint32_t insns_size = code_item->insns_size_;
320 RegisterTable reg_table;
jeffhaoba5ebb92011-08-25 17:24:37 -0700321
jeffhaobdb76512011-09-07 11:43:16 -0700322 if (registers_size * insns_size > 4*1024*1024) {
323 LOG(ERROR) << "VFY: warning: method is huge (regs=" << registers_size
324 << " insns_size=" << insns_size << ")";
325 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700326
jeffhaobdb76512011-09-07 11:43:16 -0700327 /* Create and initialize register lists. */
jeffhaoa0a764a2011-09-16 10:43:38 -0700328 if (!InitRegisterTable(vdata, &reg_table, kTrackRegsGcPoints)) {
jeffhaobdb76512011-09-07 11:43:16 -0700329 return false;
330 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700331
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700332 vdata->register_lines_ = reg_table.register_lines_.get();
jeffhaobdb76512011-09-07 11:43:16 -0700333
334 /* Allocate a map to hold the classes of uninitialized instances. */
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700335 vdata->uninit_map_.reset(CreateUninitInstanceMap(vdata));
jeffhaobdb76512011-09-07 11:43:16 -0700336
337 /* Initialize register types of method arguments. */
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700338 if (!SetTypesFromSignature(vdata, reg_table.register_lines_[0].reg_types_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -0700339 LOG(ERROR) << "VFY: bad signature '"
340 << method->GetSignature()->ToModifiedUtf8() << "' for "
341 << method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
342 << "." << method->GetName()->ToModifiedUtf8();
343 return false;
344 }
345
346 /* Perform code flow verification. */
347 if (!CodeFlowVerifyMethod(vdata, &reg_table)) {
348 return false;
349 }
350
jeffhaoa0a764a2011-09-16 10:43:38 -0700351 /* Generate a register map and add it to the method. */
352 UniquePtr<RegisterMap> map(GenerateRegisterMapV(vdata));
353 ByteArray* header = ByteArray::Alloc(sizeof(RegisterMapHeader));
354 ByteArray* data = ByteArray::Alloc(ComputeRegisterMapSize(map.get()));
jeffhaoe23d93c2011-09-15 14:48:43 -0700355
jeffhaoa0a764a2011-09-16 10:43:38 -0700356 memcpy(header->GetData(), map.get()->header_, sizeof(RegisterMapHeader));
357 memcpy(data->GetData(), map.get()->data_, ComputeRegisterMapSize(map.get()));
jeffhaoe23d93c2011-09-15 14:48:43 -0700358
jeffhaoa0a764a2011-09-16 10:43:38 -0700359 method->SetRegisterMapHeader(header);
360 method->SetRegisterMapData(data);
jeffhaoba5ebb92011-08-25 17:24:37 -0700361
jeffhaobdb76512011-09-07 11:43:16 -0700362 return true;
363}
364
365bool DexVerifier::ComputeWidthsAndCountOps(VerifierData* vdata) {
366 const uint16_t* insns = vdata->code_item_->insns_;
367 uint32_t insns_size = vdata->code_item_->insns_size_;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700368 InsnFlags* insn_flags = vdata->insn_flags_.get();
jeffhaobdb76512011-09-07 11:43:16 -0700369 const byte* ptr = reinterpret_cast<const byte*>(insns);
370 const Instruction* inst = Instruction::At(ptr);
371 size_t new_instance_count = 0;
372 size_t monitor_enter_count = 0;
373 size_t width = 0;
374
375 while (width < insns_size) {
376 Instruction::Code opcode = inst->Opcode();
377 if (opcode == Instruction::NEW_INSTANCE) {
378 new_instance_count++;
379 } else if (opcode == Instruction::MONITOR_ENTER) {
380 monitor_enter_count++;
381 }
382
383 insn_flags[width] |= inst->Size();
384 width += inst->Size();
385 inst = inst->Next();
386 }
387
388 if (width != insns_size) {
389 LOG(ERROR) << "VFY: code did not end where expected (" << width << " vs. "
390 << insns_size << ")";
391 return false;
392 }
393
394 vdata->new_instance_count_ = new_instance_count;
395 vdata->monitor_enter_count_ = monitor_enter_count;
396 return true;
397}
398
399bool DexVerifier::ScanTryCatchBlocks(VerifierData* vdata) {
400 const DexFile::CodeItem* code_item = vdata->code_item_;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700401 InsnFlags* insn_flags = vdata->insn_flags_.get();
jeffhaobdb76512011-09-07 11:43:16 -0700402 uint32_t insns_size = code_item->insns_size_;
403 uint32_t tries_size = code_item->tries_size_;
404
405 if (tries_size == 0) {
406 return true;
407 }
408
409 const DexFile::TryItem* tries = DexFile::dexGetTryItems(*code_item, 0);
410
411 for (uint32_t idx = 0; idx < tries_size; idx++) {
412 const DexFile::TryItem* try_item = &tries[idx];
413 uint32_t start = try_item->start_addr_;
414 uint32_t end = start + try_item->insn_count_;
415
416 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
417 LOG(ERROR) << "VFY: bad exception entry: startAddr=" << start
418 << " endAddr=" << end << " (size=" << insns_size << ")";
419 return false;
420 }
421
422 if (InsnGetWidth(insn_flags, start) == 0) {
423 LOG(ERROR) << "VFY: 'try' block starts inside an instruction ("
424 << start << ")";
425 return false;
426 }
427
428 uint32_t addr;
429 for (addr = start; addr < end; addr += InsnGetWidth(insn_flags, addr)) {
430 InsnSetInTry(insn_flags, addr);
431 }
432 }
433
434 /* Iterate over each of the handlers to verify target addresses. */
435 const byte* handlers_ptr = DexFile::dexGetCatchHandlerData(*code_item, 0);
436 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
437 for (uint32_t idx = 0; idx < handlers_size; idx++) {
438 DexFile::CatchHandlerIterator iterator(handlers_ptr);
439
440 for (; !iterator.HasNext(); iterator.Next()) {
441 uint32_t addr = iterator.Get().address_;
442 if (InsnGetWidth(insn_flags, addr) == 0) {
443 LOG(ERROR) << "VFY: exception handler starts at bad address ("
444 << addr << ")";
445 return false;
446 }
447
448 InsnSetBranchTarget(insn_flags, addr);
449 }
450
451 handlers_ptr = iterator.GetData();
452 }
453
454 return true;
455}
456
457bool DexVerifier::GetBranchOffset(const DexFile::CodeItem* code_item,
458 const InsnFlags insn_flags[], uint32_t cur_offset, int32_t* pOffset,
jeffhaoba5ebb92011-08-25 17:24:37 -0700459 bool* pConditional, bool* selfOkay) {
460 const uint16_t* insns = code_item->insns_ + cur_offset;
461
462 switch (*insns & 0xff) {
463 case Instruction::GOTO:
464 *pOffset = ((int16_t) *insns) >> 8;
465 *pConditional = false;
466 *selfOkay = false;
467 break;
468 case Instruction::GOTO_32:
469 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
470 *pConditional = false;
471 *selfOkay = true;
472 break;
473 case Instruction::GOTO_16:
474 *pOffset = (int16_t) insns[1];
475 *pConditional = false;
476 *selfOkay = false;
477 break;
478 case Instruction::IF_EQ:
479 case Instruction::IF_NE:
480 case Instruction::IF_LT:
481 case Instruction::IF_GE:
482 case Instruction::IF_GT:
483 case Instruction::IF_LE:
484 case Instruction::IF_EQZ:
485 case Instruction::IF_NEZ:
486 case Instruction::IF_LTZ:
487 case Instruction::IF_GEZ:
488 case Instruction::IF_GTZ:
489 case Instruction::IF_LEZ:
490 *pOffset = (int16_t) insns[1];
491 *pConditional = true;
492 *selfOkay = false;
493 break;
494 default:
495 return false;
496 break;
497 }
498
499 return true;
500}
501
jeffhaobdb76512011-09-07 11:43:16 -0700502bool DexVerifier::CheckArrayData(const DexFile::CodeItem* code_item,
jeffhaoba5ebb92011-08-25 17:24:37 -0700503 uint32_t cur_offset) {
504 const uint32_t insn_count = code_item->insns_size_;
505 const uint16_t* insns = code_item->insns_ + cur_offset;
506 const uint16_t* array_data;
507 int32_t array_data_offset;
508
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700509 DCHECK_LT(cur_offset, insn_count);
jeffhaoba5ebb92011-08-25 17:24:37 -0700510
511 /* make sure the start of the array data table is in range */
512 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
513 if ((int32_t) cur_offset + array_data_offset < 0 ||
514 cur_offset + array_data_offset + 2 >= insn_count)
515 {
516 LOG(ERROR) << "VFY: invalid array data start: at " << cur_offset
517 << ", data offset " << array_data_offset << ", count "
518 << insn_count;
519 return false;
520 }
521
522 /* offset to array data table is a relative branch-style offset */
523 array_data = insns + array_data_offset;
524
525 /* make sure the table is 32-bit aligned */
526 if ((((uint32_t) array_data) & 0x03) != 0) {
527 LOG(ERROR) << "VFY: unaligned array data table: at " << cur_offset
528 << ", data offset " << array_data_offset;
529 return false;
530 }
531
532 uint32_t value_width = array_data[1];
533 uint32_t value_count = *(uint32_t*) (&array_data[2]);
534 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
535
536 /* make sure the end of the switch is in range */
537 if (cur_offset + array_data_offset + table_size > insn_count) {
538 LOG(ERROR) << "VFY: invalid array data end: at " << cur_offset
539 << ", data offset " << array_data_offset << ", end "
540 << cur_offset + array_data_offset + table_size << ", count "
541 << insn_count;
542 return false;
543 }
544
545 return true;
546}
547
jeffhaobdb76512011-09-07 11:43:16 -0700548bool DexVerifier::CheckNewInstance(const DexFile* dex_file, uint32_t idx) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700549 if (idx >= dex_file->GetHeader().type_ids_size_) {
550 LOG(ERROR) << "VFY: bad type index " << idx << " (max "
551 << dex_file->GetHeader().type_ids_size_ << ")";
552 return false;
553 }
554
555 const char* descriptor = dex_file->dexStringByTypeIdx(idx);
556 if (descriptor[0] != 'L') {
557 LOG(ERROR) << "VFY: can't call new-instance on type '"
558 << descriptor << "'";
559 return false;
560 }
561
562 return true;
563}
564
jeffhaobdb76512011-09-07 11:43:16 -0700565bool DexVerifier::CheckNewArray(const DexFile* dex_file, uint32_t idx) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700566 if (idx >= dex_file->GetHeader().type_ids_size_) {
567 LOG(ERROR) << "VFY: bad type index " << idx << " (max "
568 << dex_file->GetHeader().type_ids_size_ << ")";
569 return false;
570 }
571
572 int bracket_count = 0;
573 const char* descriptor = dex_file->dexStringByTypeIdx(idx);
574 const char* cp = descriptor;
575 while (*cp++ == '[')
576 bracket_count++;
577
578 if (bracket_count == 0) {
579 /* The given class must be an array type. */
580 LOG(ERROR) << "VFY: can't new-array class '" << descriptor
581 << "' (not an array)";
582 return false;
583 } else if (bracket_count > 255) {
584 /* It is illegal to create an array of more than 255 dimensions. */
585 LOG(ERROR) << "VFY: can't new-array class '" << descriptor
586 << "' (exceeds limit)";
587 return false;
588 }
589
590 return true;
591}
592
jeffhaobdb76512011-09-07 11:43:16 -0700593bool DexVerifier::CheckTypeIndex(const DexFile* dex_file, uint32_t idx) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700594 if (idx >= dex_file->GetHeader().type_ids_size_) {
595 LOG(ERROR) << "VFY: bad type index " << idx << " (max "
596 << dex_file->GetHeader().type_ids_size_ << ")";
597 return false;
598 }
599 return true;
600}
601
jeffhaobdb76512011-09-07 11:43:16 -0700602bool DexVerifier::CheckFieldIndex(const DexFile* dex_file, uint32_t idx) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700603 if (idx >= dex_file->GetHeader().field_ids_size_) {
604 LOG(ERROR) << "VFY: bad field index " << idx << " (max "
605 << dex_file->GetHeader().field_ids_size_ << ")";
606 return false;
607 }
608 return true;
609}
610
jeffhaobdb76512011-09-07 11:43:16 -0700611bool DexVerifier::CheckMethodIndex(const DexFile* dex_file, uint32_t idx) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700612 if (idx >= dex_file->GetHeader().method_ids_size_) {
613 LOG(ERROR) << "VFY: bad method index " << idx << " (max "
614 << dex_file->GetHeader().method_ids_size_ << ")";
615 return false;
616 }
617 return true;
618}
619
jeffhaobdb76512011-09-07 11:43:16 -0700620bool DexVerifier::CheckStringIndex(const DexFile* dex_file, uint32_t idx) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700621 if (idx >= dex_file->GetHeader().string_ids_size_) {
622 LOG(ERROR) << "VFY: bad string index " << idx << " (max "
623 << dex_file->GetHeader().string_ids_size_ << ")";
624 return false;
625 }
626 return true;
627}
628
jeffhaobdb76512011-09-07 11:43:16 -0700629bool DexVerifier::CheckRegisterIndex(const DexFile::CodeItem* code_item,
630 uint32_t idx) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700631 if (idx >= code_item->registers_size_) {
632 LOG(ERROR) << "VFY: register index out of range (" << idx << " >= "
633 << code_item->registers_size_ << ")";
634 return false;
635 }
636 return true;
637}
638
jeffhaobdb76512011-09-07 11:43:16 -0700639bool DexVerifier::CheckWideRegisterIndex(const DexFile::CodeItem* code_item,
jeffhaoba5ebb92011-08-25 17:24:37 -0700640 uint32_t idx) {
641 if (idx + 1 >= code_item->registers_size_) {
642 LOG(ERROR) << "VFY: wide register index out of range (" << idx
643 << "+1 >= " << code_item->registers_size_ << ")";
644 return false;
645 }
646 return true;
647}
648
jeffhaobdb76512011-09-07 11:43:16 -0700649bool DexVerifier::CheckVarArgRegs(const DexFile::CodeItem* code_item,
650 uint32_t vA, uint32_t arg[]) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700651 uint16_t registers_size = code_item->registers_size_;
652 uint32_t idx;
653
654 if (vA > 5) {
655 LOG(ERROR) << "VFY: invalid arg count (" << vA << ") in non-range invoke)";
656 return false;
657 }
658
659 for (idx = 0; idx < vA; idx++) {
660 if (arg[idx] > registers_size) {
661 LOG(ERROR) << "VFY: invalid reg index (" << arg[idx]
662 << ") in non-range invoke (> " << registers_size << ")";
663 return false;
664 }
665 }
666
667 return true;
668}
669
jeffhaobdb76512011-09-07 11:43:16 -0700670bool DexVerifier::CheckVarArgRangeRegs(const DexFile::CodeItem* code_item,
jeffhaoba5ebb92011-08-25 17:24:37 -0700671 uint32_t vA, uint32_t vC) {
672 uint16_t registers_size = code_item->registers_size_;
673
674 /*
675 * vA/vC are unsigned 8-bit/16-bit quantities for /range instructions,
676 * so there's no risk of integer overflow when adding them here.
677 */
678 if (vA + vC > registers_size) {
679 LOG(ERROR) << "VFY: invalid reg index " << vA << "+" << vC
680 << " in range invoke (> " << registers_size << ")";
681 return false;
682 }
683
684 return true;
685}
686
jeffhaobdb76512011-09-07 11:43:16 -0700687bool DexVerifier::CheckSwitchTargets(const DexFile::CodeItem* code_item,
688 InsnFlags insn_flags[], uint32_t cur_offset) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700689 const uint32_t insn_count = code_item->insns_size_;
690 const uint16_t* insns = code_item->insns_ + cur_offset;
691 const uint16_t* switch_insns;
692 uint16_t expected_signature;
693 uint32_t switch_count, table_size;
694 int32_t switch_offset, keys_offset, targets_offset;
695 int32_t offset, abs_offset;
696 uint32_t targ;
697
698 /* make sure the start of the switch is in range */
699 switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
700 if ((int32_t) cur_offset + switch_offset < 0 ||
701 cur_offset + switch_offset + 2 >= insn_count) {
702 LOG(ERROR) << "VFY: invalid switch start: at " << cur_offset
703 << ", switch offset " << switch_offset << ", count "
704 << insn_count;
705 return false;
706 }
707
708 /* offset to switch table is a relative branch-style offset */
709 switch_insns = insns + switch_offset;
710
711 /* make sure the table is 32-bit aligned */
712 if ((((uint32_t) switch_insns) & 0x03) != 0) {
713 LOG(ERROR) << "VFY: unaligned switch table: at " << cur_offset
714 << ", switch offset " << switch_offset;
715 return false;
716 }
717
718 switch_count = switch_insns[1];
719
720 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
721 /* 0=sig, 1=count, 2/3=firstKey */
722 targets_offset = 4;
723 keys_offset = -1;
724 expected_signature = Instruction::kPackedSwitchSignature;
725 } else {
726 /* 0=sig, 1=count, 2..count*2 = keys */
727 keys_offset = 2;
728 targets_offset = 2 + 2 * switch_count;
729 expected_signature = Instruction::kSparseSwitchSignature;
730 }
731 table_size = targets_offset + switch_count * 2;
732
733 if (switch_insns[0] != expected_signature) {
jeffhaobdb76512011-09-07 11:43:16 -0700734 LOG(ERROR) << "VFY: wrong signature for switch table (0x" << std::hex
735 << switch_insns[0] << ", wanted 0x" << expected_signature << ")"
736 << std::dec;
jeffhaoba5ebb92011-08-25 17:24:37 -0700737 return false;
738 }
739
740 /* make sure the end of the switch is in range */
741 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
742 LOG(ERROR) << "VFY: invalid switch end: at " << cur_offset
743 << ", switch offset " << switch_offset << ", end "
744 << cur_offset + switch_offset + table_size << ", count "
745 << insn_count;
746 return false;
747 }
748
749 /* for a sparse switch, verify the keys are in ascending order */
750 if (keys_offset > 0 && switch_count > 1) {
751 int32_t last_key;
752
753 last_key = switch_insns[keys_offset] |
754 (switch_insns[keys_offset + 1] << 16);
755 for (targ = 1; targ < switch_count; targ++) {
756 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
757 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
758 if (key <= last_key) {
759 LOG(ERROR) << "VFY: invalid packed switch: last key=" << last_key
760 << ", this=" << key;
761 return false;
762 }
763
764 last_key = key;
765 }
766 }
767
768 /* verify each switch target */
769 for (targ = 0; targ < switch_count; targ++) {
770 offset = (int32_t) switch_insns[targets_offset + targ * 2] |
771 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
772 abs_offset = cur_offset + offset;
773
774 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count ||
775 !InsnIsOpcode(insn_flags, abs_offset)) {
776 LOG(ERROR) << "VFY: invalid switch target " << offset << " (-> "
jeffhaobdb76512011-09-07 11:43:16 -0700777 << std::hex << abs_offset << ") at " << cur_offset << std::dec
778 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700779 return false;
780 }
jeffhaobdb76512011-09-07 11:43:16 -0700781 InsnSetBranchTarget(insn_flags, abs_offset);
jeffhaoba5ebb92011-08-25 17:24:37 -0700782 }
783
784 return true;
785}
786
jeffhaobdb76512011-09-07 11:43:16 -0700787bool DexVerifier::CheckBranchTarget(const DexFile::CodeItem* code_item,
788 InsnFlags insn_flags[], uint32_t cur_offset) {
789 const uint32_t insn_count = code_item->insns_size_;
jeffhaoba5ebb92011-08-25 17:24:37 -0700790 int32_t offset, abs_offset;
791 bool isConditional, selfOkay;
792
793 if (!GetBranchOffset(code_item, insn_flags, cur_offset, &offset,
794 &isConditional, &selfOkay))
795 return false;
796
797 if (!selfOkay && offset == 0) {
jeffhaobdb76512011-09-07 11:43:16 -0700798 LOG(ERROR) << "VFY: branch offset of zero not allowed at" << std::hex
799 << cur_offset << std::dec;
jeffhaoba5ebb92011-08-25 17:24:37 -0700800 return false;
801 }
802
803 /*
jeffhaod1f0fde2011-09-08 17:25:33 -0700804 * Check for 32-bit overflow. This isn't strictly necessary if we can
jeffhaoba5ebb92011-08-25 17:24:37 -0700805 * depend on the VM to have identical "wrap-around" behavior, but
806 * it's unwise to depend on that.
807 */
808 if (((int64_t) cur_offset + (int64_t) offset) !=
jeffhaobdb76512011-09-07 11:43:16 -0700809 (int64_t) (cur_offset + offset)) {
810 LOG(ERROR) << "VFY: branch target overflow " << std::hex << cur_offset
811 << std::dec << " +" << offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700812 return false;
813 }
814 abs_offset = cur_offset + offset;
jeffhaobdb76512011-09-07 11:43:16 -0700815 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count ||
jeffhaoba5ebb92011-08-25 17:24:37 -0700816 !InsnIsOpcode(insn_flags, abs_offset))
817 {
818 LOG(ERROR) << "VFY: invalid branch target " << offset << " (-> "
jeffhaobdb76512011-09-07 11:43:16 -0700819 << std::hex << abs_offset << ") at " << cur_offset << std::dec;
jeffhaoba5ebb92011-08-25 17:24:37 -0700820 return false;
821 }
jeffhaobdb76512011-09-07 11:43:16 -0700822 InsnSetBranchTarget(insn_flags, abs_offset);
jeffhaoba5ebb92011-08-25 17:24:37 -0700823
824 return true;
825}
826
jeffhaobdb76512011-09-07 11:43:16 -0700827bool DexVerifier::InitRegisterTable(VerifierData* vdata,
828 RegisterTable* reg_table, RegisterTrackingMode track_regs_for) {
829 const DexFile::CodeItem* code_item = vdata->code_item_;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700830 InsnFlags* insn_flags = vdata->insn_flags_.get();
jeffhaobdb76512011-09-07 11:43:16 -0700831 uint16_t registers_size = code_item->registers_size_;
832 uint32_t insns_size = code_item->insns_size_;
833 uint32_t i;
834
835 /*
jeffhaod1f0fde2011-09-08 17:25:33 -0700836 * Every address gets a RegisterLine struct. This is wasteful, but
jeffhaobdb76512011-09-07 11:43:16 -0700837 * not so much that it's worth chasing through an extra level of
838 * indirection.
839 */
840 reg_table->insn_reg_count_plus_ = registers_size + kExtraRegs;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700841 reg_table->register_lines_.reset(new RegisterLine[insns_size]());
jeffhaobdb76512011-09-07 11:43:16 -0700842
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700843 DCHECK_GT(insns_size, 0U);
jeffhaobdb76512011-09-07 11:43:16 -0700844
845 bool track_monitors;
846 //if (gDvm.monitorVerification) {
847 //track_monitors = (vdata->monitor_enter_count_ != 0);
848 //} else {
849 track_monitors = false;
850 //}
851
852 /*
853 * Allocate entries in the sparse register line table.
854 *
855 * There is a RegisterLine associated with every address, but not
856 * every RegisterLine has non-NULL pointers to storage for its fields.
857 */
858 for (i = 0; i < insns_size; i++) {
859 bool interesting;
860
861 switch (track_regs_for) {
862 case kTrackRegsAll:
863 interesting = InsnIsOpcode(insn_flags, i);
864 break;
865 case kTrackRegsGcPoints:
866 interesting = InsnIsGcPoint(insn_flags, i) ||
867 InsnIsBranchTarget(insn_flags, i);
868 break;
869 case kTrackRegsBranches:
870 interesting = InsnIsBranchTarget(insn_flags, i);
871 break;
872 default:
873 return false;
874 }
875
876 if (interesting) {
877 reg_table->register_lines_[i].Alloc(reg_table->insn_reg_count_plus_,
878 track_monitors);
879 }
880 }
881
882 /*
883 * Allocate space for our "temporary" register lines.
884 */
885 reg_table->work_line_.Alloc(reg_table->insn_reg_count_plus_, track_monitors);
886 reg_table->saved_line_.Alloc(reg_table->insn_reg_count_plus_, track_monitors);
887
888 return true;
889}
890
891DexVerifier::UninitInstanceMap* DexVerifier::CreateUninitInstanceMap(
892 VerifierData* vdata) {
893 Method* method = vdata->method_;
894 const DexFile::CodeItem* code_item = vdata->code_item_;
895 size_t new_instance_count = vdata->new_instance_count_;
896
897 if (IsInitMethod(method)) {
898 new_instance_count++;
899 }
900
901 /*
902 * Allocate the header and map as a single unit.
903 *
904 * TODO: consider having a static instance so we can avoid allocations.
905 * I don't think the verifier is guaranteed to be single-threaded when
906 * running in the VM (rather than dexopt), so that must be taken into
907 * account.
908 */
909 UninitInstanceMap* uninit_map = new UninitInstanceMap(new_instance_count);
910
911 size_t idx = 0;
912 if (IsInitMethod(method)) {
913 uninit_map->map_[idx++].addr_ = kUninitThisArgAddr;
914 }
915
916 /*
917 * Run through and find the new-instance instructions.
918 */
919 uint32_t addr = 0;
920 uint32_t insns_size = code_item->insns_size_;
921 const byte* ptr = reinterpret_cast<const byte*>(code_item->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700922 const Instruction* inst = Instruction::At(ptr);
jeffhaobdb76512011-09-07 11:43:16 -0700923 while (addr < insns_size) {
924 Instruction::Code opcode = inst->Opcode();
925 if (opcode == Instruction::NEW_INSTANCE) {
926 uninit_map->map_[idx++].addr_ = addr;
927 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700928
jeffhaobdb76512011-09-07 11:43:16 -0700929 addr += inst->Size();
jeffhaoba5ebb92011-08-25 17:24:37 -0700930 inst = inst->Next();
931 }
932
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700933 CHECK_EQ(idx, new_instance_count);
jeffhaobdb76512011-09-07 11:43:16 -0700934 return uninit_map;
935}
936
937bool DexVerifier::IsInitMethod(const Method* method) {
938 return (method->GetName()->Equals("<init>"));
939}
940
941Class* DexVerifier::LookupClassByDescriptor(const Method* method,
942 const char* descriptor, VerifyError* failure) {
943 /*
jeffhaod1f0fde2011-09-08 17:25:33 -0700944 * The compiler occasionally puts references to nonexistent classes in
945 * signatures. For example, if you have a non-static inner class with no
946 * constructor, the compiler provides a private <init> for you.
947 * Constructing the class requires <init>(parent), but the outer class can't
948 * call that because the method is private. So the compiler generates a
949 * package-scope <init>(parent,bogus) method that just calls the regular
950 * <init> (the "bogus" part being necessary to distinguish the signature of
951 * the synthetic method). Treating the bogus class as an instance of
952 * java.lang.Object allows the verifier to process the class successfully.
jeffhaobdb76512011-09-07 11:43:16 -0700953 */
954 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
955 const ClassLoader* class_loader =
956 method->GetDeclaringClass()->GetClassLoader();
957 Class* klass = class_linker->FindClass(descriptor, class_loader);
958
959 if (klass == NULL) {
960 Thread::Current()->ClearException();
961 if (strchr(descriptor, '$') != NULL) {
962 LOG(INFO) << "VFY: unable to find class referenced in signature ("
963 << descriptor << ")";
964 } else {
965 LOG(ERROR) << "VFY: unable to find class referenced in signature ("
966 << descriptor << ")";
967 }
968
969 /* Check if the descriptor is an array. */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700970 if (descriptor[0] == '[' && descriptor[1] != '\0') {
jeffhaobdb76512011-09-07 11:43:16 -0700971 /*
972 * There should never be a problem loading primitive arrays.
973 */
974 if (descriptor[1] != 'L' && descriptor[1] != '[') {
975 LOG(ERROR) << "VFY: invalid char in signature in '" << descriptor
976 << "'";
977 *failure = VERIFY_ERROR_GENERIC;
978 }
979
980 /*
jeffhaod1f0fde2011-09-08 17:25:33 -0700981 * Try to continue with base array type. This will let us pass basic
982 * stuff (e.g. get array len) that wouldn't fly with an Object. This
983 * is NOT correct if the missing type is a primitive array, but we
984 * should never have a problem loading those. (I'm not convinced this
985 * is correct or even useful. Just use Object here?)
jeffhaobdb76512011-09-07 11:43:16 -0700986 */
987 klass = class_linker->FindClass("[Ljava/lang/Object;", class_loader);
988 } else if (descriptor[0] == 'L') {
989 /*
990 * We are looking at a non-array reference descriptor;
991 * try to continue with base reference type.
992 */
993 klass = class_linker->FindSystemClass("Ljava/lang/Object;");
994 } else {
995 /* We are looking at a primitive type. */
996 LOG(ERROR) << "VFY: invalid char in signature in '" << descriptor << "'";
997 *failure = VERIFY_ERROR_GENERIC;
998 }
999
1000 if (klass == NULL) {
1001 *failure = VERIFY_ERROR_GENERIC;
1002 }
1003 }
1004
1005 if (klass->IsPrimitive()) {
1006 LOG(ERROR) << "VFY: invalid use of primitive type '" << descriptor << "'";
1007 *failure = VERIFY_ERROR_GENERIC;
1008 klass = NULL;
1009 }
1010
1011 return klass;
1012}
1013
1014Class* DexVerifier::LookupSignatureClass(const Method* method, std::string sig,
1015 VerifyError* failure) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001016 DCHECK_EQ(sig[0], 'L');
jeffhaobdb76512011-09-07 11:43:16 -07001017 size_t end = sig.find(';');
1018
1019 if (end == std::string::npos) {
1020 LOG(ERROR) << "VFY: bad signature component '" << sig << "' (missing ';')";
1021 *failure = VERIFY_ERROR_GENERIC;
1022 return NULL;
1023 }
1024
1025 return LookupClassByDescriptor(method, sig.substr(0, end + 1).c_str(),
1026 failure);
1027}
1028
1029Class* DexVerifier::LookupSignatureArrayClass(const Method* method,
1030 std::string sig, VerifyError* failure) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001031 DCHECK_EQ(sig[0], '[');
jeffhaobdb76512011-09-07 11:43:16 -07001032 size_t end = 0;
1033
1034 while (sig[end] == '[')
1035 end++;
1036
1037 if (sig[end] == 'L') {
1038 end = sig.find(';');
1039 if (end == std::string::npos) {
1040 LOG(ERROR) << "VFY: bad signature component '" << sig
1041 << "' (missing ';')";
1042 *failure = VERIFY_ERROR_GENERIC;
1043 return NULL;
1044 }
1045 }
1046
1047 return LookupClassByDescriptor(method, sig.substr(0, end + 1).c_str(),
1048 failure);
1049}
1050
1051bool DexVerifier::SetTypesFromSignature(VerifierData* vdata, RegType* reg_types)
1052{
1053 Method* method = vdata->method_;
1054 const DexFile* dex_file = vdata->dex_file_;
1055 const DexFile::CodeItem* code_item = vdata->code_item_;
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001056 UninitInstanceMap* uninit_map = vdata->uninit_map_.get();
jeffhaobdb76512011-09-07 11:43:16 -07001057
1058 int arg_start = code_item->registers_size_ - code_item->ins_size_;
1059 int expected_args = code_item->ins_size_; /* long/double count as two */
1060 int actual_args = 0;
1061
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001062 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
jeffhaobdb76512011-09-07 11:43:16 -07001063
1064 /*
1065 * Include the "this" pointer.
1066 */
1067 if (!method->IsStatic()) {
1068 /*
1069 * If this is a constructor for a class other than java.lang.Object,
jeffhaod1f0fde2011-09-08 17:25:33 -07001070 * mark the first ("this") argument as uninitialized. This restricts
jeffhaobdb76512011-09-07 11:43:16 -07001071 * field access until the superclass constructor is called.
1072 */
1073 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1074 Class* klass_object = class_linker->FindSystemClass("Ljava/lang/Object;");
1075 if (IsInitMethod(method) && method->GetDeclaringClass() != klass_object) {
1076 int idx = SetUninitInstance(uninit_map, kUninitThisArgAddr,
1077 method->GetDeclaringClass());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001078 DCHECK_EQ(idx, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001079 reg_types[arg_start + actual_args] = RegTypeFromUninitIndex(idx);
1080 } else {
1081 reg_types[arg_start + actual_args] =
1082 RegTypeFromClass(method->GetDeclaringClass());
1083 }
1084 actual_args++;
1085 }
1086
1087 const DexFile::ProtoId& proto_id =
1088 dex_file->GetProtoId(method->GetProtoIdx());
1089 DexFile::ParameterIterator iterator(*dex_file, proto_id);
1090 VerifyError failure = VERIFY_ERROR_NONE;
1091
1092 for (; iterator.HasNext(); iterator.Next()) {
1093 const char* descriptor = iterator.GetDescriptor();
1094
1095 if (descriptor == NULL) {
1096 break;
1097 }
1098
1099 if (actual_args >= expected_args) {
1100 LOG(ERROR) << "VFY: expected " << expected_args << " args, found more ("
1101 << descriptor << ")";
1102 return false;
1103 }
1104
1105 switch (*descriptor) {
1106 case 'L':
1107 case '[':
1108 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001109 * We assume that reference arguments are initialized. The only way
1110 * it could be otherwise (assuming the caller was verified) is if
1111 * the current method is <init>, but in that case it's effectively
1112 * considered initialized the instant we reach here (in the sense
1113 * that we can return without doing anything or call virtual methods).
jeffhaobdb76512011-09-07 11:43:16 -07001114 */
1115 {
1116 Class* klass =
1117 LookupClassByDescriptor(method, descriptor, &failure);
1118 if (failure != VERIFY_ERROR_NONE)
1119 return false;
1120 reg_types[arg_start + actual_args] = RegTypeFromClass(klass);
1121 }
1122 actual_args++;
1123 break;
1124 case 'Z':
1125 reg_types[arg_start + actual_args] = kRegTypeBoolean;
1126 actual_args++;
1127 break;
1128 case 'C':
1129 reg_types[arg_start + actual_args] = kRegTypeChar;
1130 actual_args++;
1131 break;
1132 case 'B':
1133 reg_types[arg_start + actual_args] = kRegTypeByte;
1134 actual_args++;
1135 break;
1136 case 'I':
1137 reg_types[arg_start + actual_args] = kRegTypeInteger;
1138 actual_args++;
1139 break;
1140 case 'S':
1141 reg_types[arg_start + actual_args] = kRegTypeShort;
1142 actual_args++;
1143 break;
1144 case 'F':
1145 reg_types[arg_start + actual_args] = kRegTypeFloat;
1146 actual_args++;
1147 break;
1148 case 'D':
1149 reg_types[arg_start + actual_args] = kRegTypeDoubleLo;
1150 reg_types[arg_start + actual_args +1] = kRegTypeDoubleHi;
1151 actual_args += 2;
1152 break;
1153 case 'J':
1154 reg_types[arg_start + actual_args] = kRegTypeLongLo;
1155 reg_types[arg_start + actual_args +1] = kRegTypeLongHi;
1156 actual_args += 2;
1157 break;
1158 default:
1159 LOG(ERROR) << "VFY: unexpected signature type char '" << descriptor
1160 << "'";
1161 return false;
1162 }
1163 }
1164
1165 if (actual_args != expected_args) {
1166 LOG(ERROR) << "VFY: expected " << expected_args << " args, found "
1167 << actual_args;
jeffhaoba5ebb92011-08-25 17:24:37 -07001168 return false;
1169 }
1170
jeffhaobdb76512011-09-07 11:43:16 -07001171 const char* descriptor = dex_file->GetReturnTypeDescriptor(proto_id);
1172
1173 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001174 * Validate return type. We don't do the type lookup; just want to make
1175 * sure that it has the right format. Only major difference from the
jeffhaobdb76512011-09-07 11:43:16 -07001176 * method argument format is that 'V' is supported.
1177 */
1178 switch (*descriptor) {
1179 case 'I':
1180 case 'C':
1181 case 'S':
1182 case 'B':
1183 case 'Z':
1184 case 'V':
1185 case 'F':
1186 case 'D':
1187 case 'J':
1188 if (*(descriptor + 1) != '\0')
1189 return false;
1190 break;
1191 case '[':
1192 /* single/multi, object/primitive */
1193 while (*++descriptor == '[')
1194 ;
1195 if (*descriptor == 'L') {
1196 while (*++descriptor != ';' && *descriptor != '\0')
1197 ;
1198 if (*descriptor != ';')
1199 return false;
1200 } else {
1201 if (*(descriptor+1) != '\0')
1202 return false;
1203 }
1204 break;
1205 case 'L':
1206 /* could be more thorough here, but shouldn't be required */
1207 while (*++descriptor != ';' && *descriptor != '\0')
1208 ;
1209 if (*descriptor != ';')
1210 return false;
1211 break;
1212 default:
1213 return false;
1214 }
1215
jeffhaoba5ebb92011-08-25 17:24:37 -07001216 return true;
1217}
1218
jeffhaobdb76512011-09-07 11:43:16 -07001219int DexVerifier::SetUninitInstance(UninitInstanceMap* uninit_map, int addr,
1220 Class* klass) {
1221 int idx;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001222 DCHECK(klass != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07001223
jeffhaod1f0fde2011-09-08 17:25:33 -07001224 /* TODO: binary search when num_entries > 8 */
jeffhaobdb76512011-09-07 11:43:16 -07001225 for (idx = uninit_map->num_entries_ - 1; idx >= 0; idx--) {
1226 if (uninit_map->map_[idx].addr_ == addr) {
1227 if (uninit_map->map_[idx].klass_ != NULL &&
1228 uninit_map->map_[idx].klass_ != klass) {
1229 LOG(ERROR) << "VFY: addr " << addr << " already set to "
1230 << (int) uninit_map->map_[idx].klass_ << ", not setting to "
1231 << (int) klass;
1232 return -1; // already set to something else??
1233 }
1234 uninit_map->map_[idx].klass_ = klass;
1235 return idx;
1236 }
1237 }
1238
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001239 LOG(FATAL) << "VFY: addr " << addr << " not found in uninit map";
jeffhaobdb76512011-09-07 11:43:16 -07001240 return -1;
1241}
1242
1243bool DexVerifier::CodeFlowVerifyMethod(VerifierData* vdata,
1244 RegisterTable* reg_table) {
1245 const Method* method = vdata->method_;
1246 const DexFile::CodeItem* code_item = vdata->code_item_;
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001247 InsnFlags* insn_flags = vdata->insn_flags_.get();
jeffhaobdb76512011-09-07 11:43:16 -07001248 const uint16_t* insns = code_item->insns_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001249 uint32_t insns_size = code_item->insns_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001250 size_t insn_idx, start_guess;
jeffhaoba5ebb92011-08-25 17:24:37 -07001251
jeffhaobdb76512011-09-07 11:43:16 -07001252 /* Begin by marking the first instruction as "changed". */
1253 InsnSetChanged(insn_flags, 0, true);
jeffhaoba5ebb92011-08-25 17:24:37 -07001254
jeffhaobdb76512011-09-07 11:43:16 -07001255 start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001256
jeffhaobdb76512011-09-07 11:43:16 -07001257 /* Continue until no instructions are marked "changed". */
1258 while (true) {
1259 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001260 * Find the first marked one. Use "start_guess" as a way to find
jeffhaobdb76512011-09-07 11:43:16 -07001261 * one quickly.
1262 */
1263 for (insn_idx = start_guess; insn_idx < insns_size; insn_idx++) {
1264 if (InsnIsChanged(insn_flags, insn_idx))
1265 break;
1266 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001267
jeffhaobdb76512011-09-07 11:43:16 -07001268 if (insn_idx == insns_size) {
1269 if (start_guess != 0) {
1270 /* try again, starting from the top */
1271 start_guess = 0;
1272 continue;
1273 } else {
1274 /* all flags are clear */
1275 break;
1276 }
1277 }
1278
1279 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001280 * We carry the working set of registers from instruction to instruction.
1281 * If this address can be the target of a branch (or throw) instruction,
1282 * or if we're skipping around chasing "changed" flags, we need to load
1283 * the set of registers from the table.
jeffhaobdb76512011-09-07 11:43:16 -07001284 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001285 * Because we always prefer to continue on to the next instruction, we
1286 * should never have a situation where we have a stray "changed" flag set
1287 * on an instruction that isn't a branch target.
jeffhaobdb76512011-09-07 11:43:16 -07001288 */
1289 if (InsnIsBranchTarget(insn_flags, insn_idx)) {
1290 RegisterLine* work_line = &reg_table->work_line_;
1291 CopyLineFromTable(work_line, reg_table, insn_idx);
1292 } else {
1293#ifndef NDEBUG
1294 /*
1295 * Sanity check: retrieve the stored register line (assuming
1296 * a full table) and make sure it actually matches.
1297 */
1298 RegisterLine* register_line = GetRegisterLine(reg_table, insn_idx);
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001299 if (register_line->reg_types_.get() != NULL && CompareLineToTable(reg_table,
jeffhaobdb76512011-09-07 11:43:16 -07001300 insn_idx, &reg_table->work_line_) != 0) {
1301 Class* klass = method->GetDeclaringClass();
1302 LOG(ERROR) << "HUH? work_line diverged in "
1303 << klass->GetDescriptor()->ToModifiedUtf8() << "."
1304 << method->GetName()->ToModifiedUtf8() << " "
1305 << method->GetSignature()->ToModifiedUtf8();
1306 }
1307#endif
1308 }
1309
1310 if (!CodeFlowVerifyInstruction(vdata, reg_table, insn_idx, &start_guess)) {
1311 Class* klass = method->GetDeclaringClass();
1312 LOG(ERROR) << "VFY: failure to verify "
1313 << klass->GetDescriptor()->ToModifiedUtf8() << "."
1314 << method->GetName()->ToModifiedUtf8() << " "
1315 << method->GetSignature()->ToModifiedUtf8();
jeffhaoba5ebb92011-08-25 17:24:37 -07001316 return false;
1317 }
1318
jeffhaobdb76512011-09-07 11:43:16 -07001319 /* Clear "changed" and mark as visited. */
1320 InsnSetVisited(insn_flags, insn_idx, true);
1321 InsnSetChanged(insn_flags, insn_idx, false);
1322 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001323
jeffhaobdb76512011-09-07 11:43:16 -07001324 if (DEAD_CODE_SCAN && ((method->GetAccessFlags() & kAccWritable) == 0)) {
1325 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001326 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001327 * (besides the wasted space), but it indicates a flaw somewhere
1328 * down the line, possibly in the verifier.
1329 *
1330 * If we've substituted "always throw" instructions into the stream,
1331 * we are almost certainly going to have some dead code.
1332 */
1333 int dead_start = -1;
1334 for (insn_idx = 0; insn_idx < insns_size;
1335 insn_idx += InsnGetWidth(insn_flags, insn_idx)) {
1336 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001337 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001338 * may or may not be preceded by a padding NOP (for alignment).
1339 */
1340 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1341 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1342 insns[insn_idx] == Instruction::kArrayDataSignature ||
1343 (insns[insn_idx] == Instruction::NOP &&
1344 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1345 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1346 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
1347 InsnSetVisited(insn_flags, insn_idx, true);
1348 }
1349
1350 if (!InsnIsVisited(insn_flags, insn_idx)) {
1351 if (dead_start < 0)
1352 dead_start = insn_idx;
1353 } else if (dead_start >= 0) {
1354 Class* klass = method->GetDeclaringClass();
1355 LOG(INFO) << "VFY: dead code 0x" << std::hex << dead_start << "-"
1356 << insn_idx - 1 << std::dec << " in "
1357 << klass->GetDescriptor()->ToModifiedUtf8() << "."
1358 << method->GetName()->ToModifiedUtf8() << " "
1359 << method->GetSignature()->ToModifiedUtf8();
1360 dead_start = -1;
1361 }
1362 }
1363 if (dead_start >= 0) {
1364 Class* klass = method->GetDeclaringClass();
1365 LOG(INFO) << "VFY: dead code 0x" << std::hex << dead_start << "-"
1366 << insn_idx - 1 << std::dec << " in "
1367 << klass->GetDescriptor()->ToModifiedUtf8() << "."
1368 << method->GetName()->ToModifiedUtf8() << " "
1369 << method->GetSignature()->ToModifiedUtf8();
jeffhaoba5ebb92011-08-25 17:24:37 -07001370 }
1371 }
1372
jeffhaobdb76512011-09-07 11:43:16 -07001373 return true;
1374}
1375
1376bool DexVerifier::CodeFlowVerifyInstruction(VerifierData* vdata,
1377 RegisterTable* reg_table, uint32_t insn_idx, size_t* start_guess) {
1378 const Method* method = vdata->method_;
1379 Class* klass = method->GetDeclaringClass();
1380 const DexFile::CodeItem* code_item = vdata->code_item_;
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001381 InsnFlags* insn_flags = vdata->insn_flags_.get();
jeffhaobdb76512011-09-07 11:43:16 -07001382 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001383 UninitInstanceMap* uninit_map = vdata->uninit_map_.get();
jeffhaoe0cfb6f2011-09-22 16:42:56 -07001384 const ClassLoader* class_loader =
1385 method->GetDeclaringClass()->GetClassLoader();
jeffhaobdb76512011-09-07 11:43:16 -07001386 const uint16_t* insns = code_item->insns_ + insn_idx;
1387 uint32_t insns_size = code_item->insns_size_;
1388 uint32_t registers_size = code_item->registers_size_;
1389
1390#ifdef VERIFIER_STATS
1391 if (InsnIsVisited(insn_flags, insn_idx)) {
1392 gDvm.verifierStats.instrsReexamined++;
1393 } else {
1394 gDvm.verifierStats.instrsExamined++;
1395 }
1396#endif
1397
1398 /*
1399 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001400 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001401 * control to another statement:
1402 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001403 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001404 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001405 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001406 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001407 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001408 * throw an exception that is handled by an encompassing "try"
1409 * block.
1410 *
1411 * We can also return, in which case there is no successor instruction
1412 * from this point.
1413 *
1414 * The behavior can be determined from the OpcodeFlags.
1415 */
1416 RegisterLine* work_line = &reg_table->work_line_;
1417 const DexFile* dex_file = vdata->dex_file_;
1418 const byte* ptr = reinterpret_cast<const byte*>(insns);
1419 const Instruction* inst = Instruction::At(ptr);
1420 Instruction::DecodedInstruction dec_insn(inst);
1421 int opcode_flag = inst->Flag();
1422
1423 Class* res_class;
1424 int32_t branch_target = 0;
1425 RegType tmp_type;
1426 bool just_set_result = false;
1427 VerifyError failure = VERIFY_ERROR_NONE;
1428
1429 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001430 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001431 * can throw an exception, we will copy/merge this into the "catch"
1432 * address rather than work_line, because we don't want the result
1433 * from the "successful" code path (e.g. a check-cast that "improves"
1434 * a type) to be visible to the exception handler.
1435 */
1436 if ((opcode_flag & Instruction::kThrow) != 0 &&
1437 InsnIsInTry(insn_flags, insn_idx)) {
1438 CopyRegisterLine(&reg_table->saved_line_, work_line,
1439 reg_table->insn_reg_count_plus_);
1440 } else {
1441#ifndef NDEBUG
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001442 memset(reg_table->saved_line_.reg_types_.get(), 0xdd,
jeffhaobdb76512011-09-07 11:43:16 -07001443 reg_table->insn_reg_count_plus_ * sizeof(RegType));
1444#endif
1445 }
1446
1447 switch (dec_insn.opcode_) {
1448 case Instruction::NOP:
1449 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001450 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001451 * a signature that looks like a NOP; if we see one of these in
1452 * the course of executing code then we have a problem.
1453 */
1454 if (dec_insn.vA_ != 0) {
1455 LOG(ERROR) << "VFY: encountered data table in instruction stream";
1456 failure = VERIFY_ERROR_GENERIC;
1457 }
1458 break;
1459
1460 case Instruction::MOVE:
1461 case Instruction::MOVE_FROM16:
1462 case Instruction::MOVE_16:
1463 CopyRegister1(work_line, dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr,
1464 &failure);
1465 break;
1466 case Instruction::MOVE_WIDE:
1467 case Instruction::MOVE_WIDE_FROM16:
1468 case Instruction::MOVE_WIDE_16:
1469 CopyRegister2(work_line, dec_insn.vA_, dec_insn.vB_, &failure);
1470 break;
1471 case Instruction::MOVE_OBJECT:
1472 case Instruction::MOVE_OBJECT_FROM16:
1473 case Instruction::MOVE_OBJECT_16:
1474 CopyRegister1(work_line, dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef,
1475 &failure);
1476 break;
1477
1478 /*
1479 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001480 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001481 * might want to hold the result in an actual CPU register, so the
1482 * Dalvik spec requires that these only appear immediately after an
1483 * invoke or filled-new-array.
1484 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001485 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001486 * redundant with the reset done below, but it can make the debug info
1487 * easier to read in some cases.)
1488 */
1489 case Instruction::MOVE_RESULT:
1490 CopyResultRegister1(work_line, registers_size, dec_insn.vA_,
1491 kTypeCategory1nr, &failure);
1492 break;
1493 case Instruction::MOVE_RESULT_WIDE:
1494 CopyResultRegister2(work_line, registers_size, dec_insn.vA_, &failure);
1495 break;
1496 case Instruction::MOVE_RESULT_OBJECT:
1497 CopyResultRegister1(work_line, registers_size, dec_insn.vA_,
1498 kTypeCategoryRef, &failure);
1499 break;
1500
1501 case Instruction::MOVE_EXCEPTION:
1502 /*
1503 * This statement can only appear as the first instruction in an
1504 * exception handler (though not all exception handlers need to
jeffhaod1f0fde2011-09-08 17:25:33 -07001505 * have one of these). We verify that as part of extracting the
jeffhaobdb76512011-09-07 11:43:16 -07001506 * exception type from the catch block list.
1507 *
1508 * "res_class" will hold the closest common superclass of all
1509 * exceptions that can be handled here.
1510 */
1511 res_class = GetCaughtExceptionType(vdata, insn_idx, &failure);
1512 if (res_class == NULL) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001513 DCHECK(failure != VERIFY_ERROR_NONE);
jeffhaobdb76512011-09-07 11:43:16 -07001514 } else {
1515 SetRegisterType(work_line, dec_insn.vA_, RegTypeFromClass(res_class));
1516 }
1517 break;
1518
1519 case Instruction::RETURN_VOID:
1520 if (!CheckConstructorReturn(method, work_line, registers_size)) {
1521 failure = VERIFY_ERROR_GENERIC;
1522 } else if (GetMethodReturnType(dex_file, method) != kRegTypeUnknown) {
1523 LOG(ERROR) << "VFY: return-void not expected";
1524 failure = VERIFY_ERROR_GENERIC;
1525 }
1526 break;
1527 case Instruction::RETURN:
1528 if (!CheckConstructorReturn(method, work_line, registers_size)) {
1529 failure = VERIFY_ERROR_GENERIC;
1530 } else {
1531 /* check the method signature */
1532 RegType return_type = GetMethodReturnType(dex_file, method);
1533 CheckTypeCategory(return_type, kTypeCategory1nr, &failure);
1534 if (failure != VERIFY_ERROR_NONE)
1535 LOG(ERROR) << "VFY: return-1nr not expected";
1536
1537 /*
1538 * compiler may generate synthetic functions that write byte
1539 * values into boolean fields. Also, it may use integer values
1540 * for boolean, byte, short, and character return types.
1541 */
1542 RegType src_type = GetRegisterType(work_line, dec_insn.vA_);
1543 if ((return_type == kRegTypeBoolean && src_type == kRegTypeByte) ||
1544 ((return_type == kRegTypeBoolean || return_type == kRegTypeByte ||
1545 return_type == kRegTypeShort || return_type == kRegTypeChar) &&
1546 src_type == kRegTypeInteger))
1547 return_type = src_type;
1548
1549 /* check the register contents */
1550 VerifyRegisterType(work_line, dec_insn.vA_, return_type, &failure);
1551 if (failure != VERIFY_ERROR_NONE) {
1552 LOG(ERROR) << "VFY: return-1nr on invalid register v" << dec_insn.vA_;
1553 }
1554 }
1555 break;
1556 case Instruction::RETURN_WIDE:
1557 if (!CheckConstructorReturn(method, work_line, registers_size)) {
1558 failure = VERIFY_ERROR_GENERIC;
1559 } else {
1560 RegType return_type;
1561
1562 /* check the method signature */
1563 return_type = GetMethodReturnType(dex_file, method);
1564 CheckTypeCategory(return_type, kTypeCategory2, &failure);
1565 if (failure != VERIFY_ERROR_NONE)
1566 LOG(ERROR) << "VFY: return-wide not expected";
1567
1568 /* check the register contents */
1569 VerifyRegisterType(work_line, dec_insn.vA_, return_type, &failure);
1570 if (failure != VERIFY_ERROR_NONE) {
1571 LOG(ERROR) << "VFY: return-wide on invalid register pair v"
1572 << dec_insn.vA_;
1573 }
1574 }
1575 break;
1576 case Instruction::RETURN_OBJECT:
1577 if (!CheckConstructorReturn(method, work_line, registers_size)) {
1578 failure = VERIFY_ERROR_GENERIC;
1579 } else {
1580 RegType return_type = GetMethodReturnType(dex_file, method);
1581 CheckTypeCategory(return_type, kTypeCategoryRef, &failure);
1582 if (failure != VERIFY_ERROR_NONE) {
1583 LOG(ERROR) << "VFY: return-object not expected";
1584 break;
1585 }
1586
1587 /* return_type is the *expected* return type, not register value */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001588 DCHECK(return_type != kRegTypeZero);
1589 DCHECK(!RegTypeIsUninitReference(return_type));
jeffhaobdb76512011-09-07 11:43:16 -07001590
1591 /*
1592 * Verify that the reference in vAA is an instance of the type
jeffhaod1f0fde2011-09-08 17:25:33 -07001593 * in "return_type". The Zero type is allowed here. If the
jeffhaobdb76512011-09-07 11:43:16 -07001594 * method is declared to return an interface, then any
1595 * initialized reference is acceptable.
1596 *
1597 * Note GetClassFromRegister fails if the register holds an
1598 * uninitialized reference, so we do not allow them to be
1599 * returned.
1600 */
1601 Class* decl_class = RegTypeInitializedReferenceToClass(return_type);
1602 res_class = GetClassFromRegister(work_line, dec_insn.vA_, &failure);
1603 if (failure != VERIFY_ERROR_NONE)
1604 break;
1605 if (res_class != NULL) {
1606 if (!decl_class->IsInterface() &&
jeffhaobdb76512011-09-07 11:43:16 -07001607 !decl_class->IsAssignableFrom(res_class)) {
1608 LOG(ERROR) << "VFY: returning " << std::hex
1609 << res_class->GetDescriptor()->ToModifiedUtf8()
1610 << " (cl=0x" << (int) res_class->GetClassLoader()
1611 << "), declared "
1612 << decl_class->GetDescriptor()->ToModifiedUtf8()
1613 << " (cl=0x" << (int) decl_class->GetClassLoader()
1614 << ")" << std::dec;
1615 failure = VERIFY_ERROR_GENERIC;
1616 break;
1617 }
1618 }
1619 }
1620 break;
1621
1622 case Instruction::CONST_4:
1623 case Instruction::CONST_16:
1624 case Instruction::CONST:
1625 /* could be boolean, int, float, or a null reference */
1626 SetRegisterType(work_line, dec_insn.vA_,
1627 DetermineCat1Const((int32_t) dec_insn.vB_));
1628 break;
1629 case Instruction::CONST_HIGH16:
1630 /* could be boolean, int, float, or a null reference */
1631 SetRegisterType(work_line, dec_insn.vA_,
1632 DetermineCat1Const((int32_t) dec_insn.vB_ << 16));
1633 break;
1634 case Instruction::CONST_WIDE_16:
1635 case Instruction::CONST_WIDE_32:
1636 case Instruction::CONST_WIDE:
1637 case Instruction::CONST_WIDE_HIGH16:
1638 /* could be long or double; resolved upon use */
1639 SetRegisterType(work_line, dec_insn.vA_, kRegTypeConstLo);
1640 break;
1641 case Instruction::CONST_STRING:
1642 case Instruction::CONST_STRING_JUMBO:
1643 SetRegisterType(work_line, dec_insn.vA_, RegTypeFromClass(
1644 class_linker->FindSystemClass("Ljava/lang/String;")));
1645 break;
1646 case Instruction::CONST_CLASS:
1647 /* make sure we can resolve the class; access check is important */
jeffhao98eacac2011-09-14 16:11:53 -07001648 res_class = ResolveClassAndCheckAccess(dex_file, dec_insn.vB_, klass, &failure);
jeffhaobdb76512011-09-07 11:43:16 -07001649 if (res_class == NULL) {
1650 const char* bad_class_desc = dex_file->dexStringByTypeIdx(dec_insn.vB_);
1651 LOG(ERROR) << "VFY: unable to resolve const-class " << dec_insn.vB_
1652 << " (" << bad_class_desc << ") in "
1653 << klass->GetDescriptor()->ToModifiedUtf8();
jeffhaoe0cfb6f2011-09-22 16:42:56 -07001654 if (failure != VERIFY_ERROR_NONE) {
1655 break;
1656 }
jeffhaobdb76512011-09-07 11:43:16 -07001657 }
jeffhaoe0cfb6f2011-09-22 16:42:56 -07001658 SetRegisterType(work_line, dec_insn.vA_, RegTypeFromClass(
1659 class_linker->FindSystemClass("Ljava/lang/Class;")));
jeffhaobdb76512011-09-07 11:43:16 -07001660 break;
1661
1662 case Instruction::MONITOR_ENTER:
1663 HandleMonitorEnter(work_line, dec_insn.vA_, insn_idx, &failure);
1664 break;
1665 case Instruction::MONITOR_EXIT:
1666 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001667 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001668 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001669 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001670 * to the need to handle asynchronous exceptions, a now-deprecated
1671 * feature that Dalvik doesn't support.)
1672 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001673 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001674 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001675 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001676 * structured locking checks are working, the former would have
1677 * failed on the -enter instruction, and the latter is impossible.
1678 *
1679 * This is fortunate, because issue 3221411 prevents us from
1680 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001681 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001682 * some catch blocks (which will show up as "dead" code when
1683 * we skip them here); if we can't, then the code path could be
1684 * "live" so we still need to check it.
1685 */
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001686 if (work_line->monitor_entries_.get() != NULL)
jeffhaobdb76512011-09-07 11:43:16 -07001687 opcode_flag &= ~Instruction::kThrow;
1688 HandleMonitorExit(work_line, dec_insn.vA_, insn_idx, &failure);
1689 break;
1690
1691 case Instruction::CHECK_CAST:
1692 /*
1693 * If this instruction succeeds, we will promote register vA to
jeffhaod1f0fde2011-09-08 17:25:33 -07001694 * the type in vB. (This could be a demotion -- not expected, so
jeffhaobdb76512011-09-07 11:43:16 -07001695 * we don't try to address it.)
1696 *
1697 * If it fails, an exception is thrown, which we deal with later
1698 * by ignoring the update to dec_insn.vA_ when branching to a handler.
1699 */
jeffhao98eacac2011-09-14 16:11:53 -07001700 res_class = ResolveClassAndCheckAccess(dex_file, dec_insn.vB_, klass, &failure);
jeffhaobdb76512011-09-07 11:43:16 -07001701 if (res_class == NULL) {
1702 const char* bad_class_desc = dex_file->dexStringByTypeIdx(dec_insn.vB_);
1703 LOG(ERROR) << "VFY: unable to resolve check-cast " << dec_insn.vB_
1704 << " (" << bad_class_desc << ") in "
1705 << klass->GetDescriptor()->ToModifiedUtf8();
jeffhaoe0cfb6f2011-09-22 16:42:56 -07001706 if (failure != VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07001707 break;
1708 }
jeffhaoe0cfb6f2011-09-22 16:42:56 -07001709 /* if the class is unresolvable, treat it as an object */
1710 res_class = class_linker->FindClass("Ljava/lang/Object;", class_loader);
jeffhaobdb76512011-09-07 11:43:16 -07001711 }
jeffhaoe0cfb6f2011-09-22 16:42:56 -07001712 RegType orig_type;
1713
1714 orig_type = GetRegisterType(work_line, dec_insn.vA_);
1715 if (!RegTypeIsReference(orig_type)) {
1716 LOG(ERROR) << "VFY: check-cast on non-reference in v" << dec_insn.vA_;
1717 failure = VERIFY_ERROR_GENERIC;
1718 break;
1719 }
1720 SetRegisterType(work_line, dec_insn.vA_, RegTypeFromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07001721 break;
1722 case Instruction::INSTANCE_OF:
1723 /* make sure we're checking a reference type */
1724 tmp_type = GetRegisterType(work_line, dec_insn.vB_);
1725 if (!RegTypeIsReference(tmp_type)) {
1726 LOG(ERROR) << "VFY: vB not a reference (" << tmp_type << ")";
1727 failure = VERIFY_ERROR_GENERIC;
1728 break;
1729 }
1730
1731 /* make sure we can resolve the class; access check is important */
jeffhao98eacac2011-09-14 16:11:53 -07001732 res_class = ResolveClassAndCheckAccess(dex_file, dec_insn.vC_, klass, &failure);
jeffhaobdb76512011-09-07 11:43:16 -07001733 if (res_class == NULL) {
1734 const char* bad_class_desc = dex_file->dexStringByTypeIdx(dec_insn.vC_);
1735 LOG(ERROR) << "VFY: unable to resolve instanceof " << dec_insn.vC_
1736 << " (" << bad_class_desc << ") in "
1737 << klass->GetDescriptor()->ToModifiedUtf8();
jeffhaoe0cfb6f2011-09-22 16:42:56 -07001738 if (failure != VERIFY_ERROR_NONE) {
1739 break;
1740 }
jeffhaobdb76512011-09-07 11:43:16 -07001741 }
jeffhaoe0cfb6f2011-09-22 16:42:56 -07001742 /* result is boolean */
1743 SetRegisterType(work_line, dec_insn.vA_, kRegTypeBoolean);
jeffhaobdb76512011-09-07 11:43:16 -07001744 break;
1745
1746 case Instruction::ARRAY_LENGTH:
1747 res_class = GetClassFromRegister(work_line, dec_insn.vB_, &failure);
1748 if (failure != VERIFY_ERROR_NONE)
1749 break;
1750 if (res_class != NULL && !res_class->IsArrayClass()) {
1751 LOG(ERROR) << "VFY: array-length on non-array";
1752 failure = VERIFY_ERROR_GENERIC;
1753 break;
1754 }
1755 SetRegisterType(work_line, dec_insn.vA_, kRegTypeInteger);
1756 break;
1757
1758 case Instruction::NEW_INSTANCE:
jeffhaoe0cfb6f2011-09-22 16:42:56 -07001759 {
1760 res_class = ResolveClassAndCheckAccess(dex_file, dec_insn.vB_, klass, &failure);
1761 if (res_class == NULL) {
1762 const char* bad_class_desc = dex_file->dexStringByTypeIdx(dec_insn.vB_);
1763 LOG(ERROR) << "VFY: unable to resolve new-instance " << dec_insn.vB_
1764 << " (" << bad_class_desc << ") in "
1765 << klass->GetDescriptor()->ToModifiedUtf8();
1766 if (failure != VERIFY_ERROR_NONE) {
1767 break;
1768 }
1769 /* if the class is unresolvable, treat it as an object */
1770 res_class = class_linker->FindClass("Ljava/lang/Object;", class_loader);
1771 }
jeffhaobdb76512011-09-07 11:43:16 -07001772 RegType uninit_type;
1773
1774 /* can't create an instance of an interface or abstract class */
1775 if (res_class->IsAbstract() || res_class->IsInterface()) {
1776 LOG(ERROR) << "VFY: new-instance on interface or abstract class"
1777 << res_class->GetDescriptor()->ToModifiedUtf8();
1778 failure = VERIFY_ERROR_INSTANTIATION;
1779 break;
1780 }
1781
1782 /* add resolved class to uninit map if not already there */
1783 int uidx = SetUninitInstance(uninit_map, insn_idx, res_class);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001784 DCHECK_GE(uidx, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001785 uninit_type = RegTypeFromUninitIndex(uidx);
1786
1787 /*
1788 * Any registers holding previous allocations from this address
1789 * that have not yet been initialized must be marked invalid.
1790 */
1791 MarkUninitRefsAsInvalid(work_line, registers_size, uninit_map,
1792 uninit_type);
1793
1794 /* add the new uninitialized reference to the register ste */
1795 SetRegisterType(work_line, dec_insn.vA_, uninit_type);
jeffhaoe0cfb6f2011-09-22 16:42:56 -07001796 break;
jeffhaobdb76512011-09-07 11:43:16 -07001797 }
jeffhaobdb76512011-09-07 11:43:16 -07001798 case Instruction::NEW_ARRAY:
jeffhao98eacac2011-09-14 16:11:53 -07001799 res_class = ResolveClassAndCheckAccess(dex_file, dec_insn.vC_, klass, &failure);
jeffhaobdb76512011-09-07 11:43:16 -07001800 if (res_class == NULL) {
1801 const char* bad_class_desc = dex_file->dexStringByTypeIdx(dec_insn.vC_);
1802 LOG(ERROR) << "VFY: unable to resolve new-array " << dec_insn.vC_
1803 << " (" << bad_class_desc << ") in "
1804 << klass->GetDescriptor()->ToModifiedUtf8();
jeffhaoe0cfb6f2011-09-22 16:42:56 -07001805 if (failure != VERIFY_ERROR_NONE) {
1806 break;
1807 }
1808 /* if the class is unresolvable, treat it as an object array */
1809 res_class = class_linker->FindClass("[Ljava/lang/Object;", class_loader);
1810 }
1811 if (!res_class->IsArrayClass()) {
jeffhaobdb76512011-09-07 11:43:16 -07001812 LOG(ERROR) << "VFY: new-array on non-array class";
1813 failure = VERIFY_ERROR_GENERIC;
1814 } else {
1815 /* make sure "size" register is valid type */
1816 VerifyRegisterType(work_line, dec_insn.vB_, kRegTypeInteger, &failure);
1817 /* set register type to array class */
1818 SetRegisterType(work_line, dec_insn.vA_, RegTypeFromClass(res_class));
1819 }
1820 break;
1821 case Instruction::FILLED_NEW_ARRAY:
1822 case Instruction::FILLED_NEW_ARRAY_RANGE:
jeffhao98eacac2011-09-14 16:11:53 -07001823 res_class = ResolveClassAndCheckAccess(dex_file, dec_insn.vB_, klass, &failure);
jeffhaobdb76512011-09-07 11:43:16 -07001824 if (res_class == NULL) {
1825 const char* bad_class_desc = dex_file->dexStringByTypeIdx(dec_insn.vB_);
1826 LOG(ERROR) << "VFY: unable to resolve filled-array " << dec_insn.vB_
1827 << " (" << bad_class_desc << ") in "
1828 << klass->GetDescriptor()->ToModifiedUtf8();
jeffhaoe0cfb6f2011-09-22 16:42:56 -07001829 if (failure != VERIFY_ERROR_NONE) {
1830 break;
1831 }
1832 /* if the class is unresolvable, treat it as an object array */
1833 res_class = class_linker->FindClass("[Ljava/lang/Object;", class_loader);
1834 }
1835 if (!res_class->IsArrayClass()) {
jeffhaobdb76512011-09-07 11:43:16 -07001836 LOG(ERROR) << "VFY: filled-new-array on non-array class";
1837 failure = VERIFY_ERROR_GENERIC;
1838 } else {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07001839 bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
jeffhaobdb76512011-09-07 11:43:16 -07001840
1841 /* check the arguments to the instruction */
1842 VerifyFilledNewArrayRegs(method, work_line, &dec_insn, res_class,
1843 is_range, &failure);
1844 /* filled-array result goes into "result" register */
1845 SetResultRegisterType(work_line, registers_size,
1846 RegTypeFromClass(res_class));
1847 just_set_result = true;
1848 }
1849 break;
1850
1851 case Instruction::CMPL_FLOAT:
1852 case Instruction::CMPG_FLOAT:
1853 VerifyRegisterType(work_line, dec_insn.vB_, kRegTypeFloat, &failure);
1854 VerifyRegisterType(work_line, dec_insn.vC_, kRegTypeFloat, &failure);
1855 SetRegisterType(work_line, dec_insn.vA_, kRegTypeBoolean);
1856 break;
1857 case Instruction::CMPL_DOUBLE:
1858 case Instruction::CMPG_DOUBLE:
1859 VerifyRegisterType(work_line, dec_insn.vB_, kRegTypeDoubleLo, &failure);
1860 VerifyRegisterType(work_line, dec_insn.vC_, kRegTypeDoubleLo, &failure);
1861 SetRegisterType(work_line, dec_insn.vA_, kRegTypeBoolean);
1862 break;
1863 case Instruction::CMP_LONG:
1864 VerifyRegisterType(work_line, dec_insn.vB_, kRegTypeLongLo, &failure);
1865 VerifyRegisterType(work_line, dec_insn.vC_, kRegTypeLongLo, &failure);
1866 SetRegisterType(work_line, dec_insn.vA_, kRegTypeBoolean);
1867 break;
1868
1869 case Instruction::THROW:
1870 res_class = GetClassFromRegister(work_line, dec_insn.vA_, &failure);
1871 if (failure == VERIFY_ERROR_NONE && res_class != NULL) {
1872 Class* throwable_class =
1873 class_linker->FindSystemClass("Ljava/lang/Throwable;");
1874 if (!throwable_class->IsAssignableFrom(res_class)) {
1875 LOG(ERROR) << "VFY: thrown class "
1876 << res_class->GetDescriptor()->ToModifiedUtf8()
1877 << " not instanceof Throwable",
1878 failure = VERIFY_ERROR_GENERIC;
1879 }
1880 }
1881 break;
1882
1883 case Instruction::GOTO:
1884 case Instruction::GOTO_16:
1885 case Instruction::GOTO_32:
1886 /* no effect on or use of registers */
1887 break;
1888
1889 case Instruction::PACKED_SWITCH:
1890 case Instruction::SPARSE_SWITCH:
1891 /* verify that vAA is an integer, or can be converted to one */
1892 VerifyRegisterType(work_line, dec_insn.vA_, kRegTypeInteger, &failure);
1893 break;
1894
1895 case Instruction::FILL_ARRAY_DATA:
1896 {
1897 RegType value_type;
1898 const uint16_t *array_data;
1899 uint16_t elem_width;
1900
1901 /* Similar to the verification done for APUT */
1902 res_class = GetClassFromRegister(work_line, dec_insn.vA_, &failure);
1903 if (failure != VERIFY_ERROR_NONE)
1904 break;
1905
1906 /* res_class can be null if the reg type is Zero */
1907 if (res_class == NULL)
1908 break;
1909
1910 Class::PrimitiveType prim_type =
1911 res_class->GetComponentType()->GetPrimitiveType();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001912 if (!res_class->IsArrayClass() ||
jeffhaobdb76512011-09-07 11:43:16 -07001913 prim_type == Class::kPrimNot || prim_type == Class::kPrimVoid) {
1914 LOG(ERROR) << "VFY: invalid fill-array-data on " <<
1915 res_class->GetDescriptor()->ToModifiedUtf8();
1916 failure = VERIFY_ERROR_GENERIC;
1917 break;
1918 }
1919
1920 value_type = PrimitiveTypeToRegType(prim_type);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001921 DCHECK(value_type != kRegTypeUnknown);
jeffhaobdb76512011-09-07 11:43:16 -07001922
1923 /*
1924 * Now verify if the element width in the table matches the element
1925 * width declared in the array
1926 */
1927 array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1928 if (array_data[0] != Instruction::kArrayDataSignature) {
1929 LOG(ERROR) << "VFY: invalid magic for array-data";
1930 failure = VERIFY_ERROR_GENERIC;
1931 break;
1932 }
1933
1934 switch (prim_type) {
1935 case Class::kPrimBoolean:
1936 case Class::kPrimByte:
1937 elem_width = 1;
1938 break;
1939 case Class::kPrimChar:
1940 case Class::kPrimShort:
1941 elem_width = 2;
1942 break;
1943 case Class::kPrimFloat:
1944 case Class::kPrimInt:
1945 elem_width = 4;
1946 break;
1947 case Class::kPrimDouble:
1948 case Class::kPrimLong:
1949 elem_width = 8;
1950 break;
1951 default:
1952 elem_width = 0;
1953 break;
1954 }
1955
1956 /*
1957 * Since we don't compress the data in Dex, expect to see equal
1958 * width of data stored in the table and expected from the array
1959 * class.
1960 */
1961 if (array_data[1] != elem_width) {
1962 LOG(ERROR) << "VFY: array-data size mismatch (" << array_data[1]
1963 << " vs " << elem_width << ")";
1964 failure = VERIFY_ERROR_GENERIC;
1965 }
1966 }
1967 break;
1968
1969 case Instruction::IF_EQ:
1970 case Instruction::IF_NE:
1971 {
1972 RegType type1, type2;
1973
1974 type1 = GetRegisterType(work_line, dec_insn.vA_);
1975 type2 = GetRegisterType(work_line, dec_insn.vB_);
1976
1977 /* both references? */
1978 if (RegTypeIsReference(type1) && RegTypeIsReference(type2))
1979 break;
1980
1981 /* both category-1nr? */
1982 CheckTypeCategory(type1, kTypeCategory1nr, &failure);
1983 CheckTypeCategory(type2, kTypeCategory1nr, &failure);
1984 if (failure != VERIFY_ERROR_NONE) {
1985 LOG(ERROR) << "VFY: args to if-eq/if-ne must both be refs or cat1";
1986 break;
1987 }
1988 }
1989 break;
1990 case Instruction::IF_LT:
1991 case Instruction::IF_GE:
1992 case Instruction::IF_GT:
1993 case Instruction::IF_LE:
1994 tmp_type = GetRegisterType(work_line, dec_insn.vA_);
1995 CheckTypeCategory(tmp_type, kTypeCategory1nr, &failure);
1996 if (failure != VERIFY_ERROR_NONE) {
1997 LOG(ERROR) << "VFY: args to 'if' must be cat-1nr";
1998 break;
1999 }
2000 tmp_type = GetRegisterType(work_line, dec_insn.vB_);
2001 CheckTypeCategory(tmp_type, kTypeCategory1nr, &failure);
2002 if (failure != VERIFY_ERROR_NONE) {
2003 LOG(ERROR) << "VFY: args to 'if' must be cat-1nr";
2004 break;
2005 }
2006 break;
2007 case Instruction::IF_EQZ:
2008 case Instruction::IF_NEZ:
2009 tmp_type = GetRegisterType(work_line, dec_insn.vA_);
2010 if (RegTypeIsReference(tmp_type))
2011 break;
2012 CheckTypeCategory(tmp_type, kTypeCategory1nr, &failure);
2013 if (failure != VERIFY_ERROR_NONE)
2014 LOG(ERROR) << "VFY: expected cat-1 arg to if";
2015 break;
2016 case Instruction::IF_LTZ:
2017 case Instruction::IF_GEZ:
2018 case Instruction::IF_GTZ:
2019 case Instruction::IF_LEZ:
2020 tmp_type = GetRegisterType(work_line, dec_insn.vA_);
2021 CheckTypeCategory(tmp_type, kTypeCategory1nr, &failure);
2022 if (failure != VERIFY_ERROR_NONE)
2023 LOG(ERROR) << "VFY: expected cat-1 arg to if";
2024 break;
2025
2026 case Instruction::AGET:
2027 tmp_type = kRegTypeConstInteger;
2028 goto aget_1nr_common;
2029 case Instruction::AGET_BOOLEAN:
2030 tmp_type = kRegTypeBoolean;
2031 goto aget_1nr_common;
2032 case Instruction::AGET_BYTE:
2033 tmp_type = kRegTypeByte;
2034 goto aget_1nr_common;
2035 case Instruction::AGET_CHAR:
2036 tmp_type = kRegTypeChar;
2037 goto aget_1nr_common;
2038 case Instruction::AGET_SHORT:
2039 tmp_type = kRegTypeShort;
2040 goto aget_1nr_common;
2041aget_1nr_common:
2042 {
2043 RegType src_type, index_type;
2044
2045 index_type = GetRegisterType(work_line, dec_insn.vC_);
2046 CheckArrayIndexType(method, index_type, &failure);
2047 if (failure != VERIFY_ERROR_NONE)
2048 break;
2049
2050 res_class = GetClassFromRegister(work_line, dec_insn.vB_, &failure);
2051 if (failure != VERIFY_ERROR_NONE)
2052 break;
2053 if (res_class != NULL) {
2054 /* verify the class */
2055 Class::PrimitiveType prim_type =
2056 res_class->GetComponentType()->GetPrimitiveType();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002057 if (!res_class->IsArrayClass() || prim_type == Class::kPrimNot) {
jeffhaobdb76512011-09-07 11:43:16 -07002058 LOG(ERROR) << "VFY: invalid aget-1nr target "
2059 << res_class->GetDescriptor()->ToModifiedUtf8();
2060 failure = VERIFY_ERROR_GENERIC;
2061 break;
2062 }
2063
2064 /* make sure array type matches instruction */
2065 src_type = PrimitiveTypeToRegType(prim_type);
2066
2067 /* differentiate between float and int */
2068 if (src_type == kRegTypeFloat || src_type == kRegTypeInteger)
2069 tmp_type = src_type;
2070
2071 if (tmp_type != src_type) {
2072 LOG(ERROR) << "VFY: invalid aget-1nr, array type=" << src_type
2073 << " with inst type=" << tmp_type << " (on "
2074 << res_class->GetDescriptor()->ToModifiedUtf8() << ")";
2075 failure = VERIFY_ERROR_GENERIC;
2076 break;
2077 }
2078
2079 }
2080 SetRegisterType(work_line, dec_insn.vA_, tmp_type);
2081 }
2082 break;
2083
2084 case Instruction::AGET_WIDE:
2085 {
2086 RegType dst_type, index_type;
2087
2088 index_type = GetRegisterType(work_line, dec_insn.vC_);
2089 CheckArrayIndexType(method, index_type, &failure);
2090 if (failure != VERIFY_ERROR_NONE)
2091 break;
2092
2093 res_class = GetClassFromRegister(work_line, dec_insn.vB_, &failure);
2094 if (failure != VERIFY_ERROR_NONE)
2095 break;
2096 if (res_class != NULL) {
2097 /* verify the class */
2098 Class::PrimitiveType prim_type =
2099 res_class->GetComponentType()->GetPrimitiveType();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002100 if (!res_class->IsArrayClass() || prim_type == Class::kPrimNot) {
jeffhaobdb76512011-09-07 11:43:16 -07002101 LOG(ERROR) << "VFY: invalid aget-wide target "
2102 << res_class->GetDescriptor()->ToModifiedUtf8();
2103 failure = VERIFY_ERROR_GENERIC;
2104 break;
2105 }
2106
2107 /* try to refine "dst_type" */
2108 switch (prim_type) {
2109 case Class::kPrimLong:
2110 dst_type = kRegTypeLongLo;
2111 break;
2112 case Class::kPrimDouble:
2113 dst_type = kRegTypeDoubleLo;
2114 break;
2115 default:
2116 LOG(ERROR) << "VFY: invalid aget-wide on "
2117 << res_class->GetDescriptor()->ToModifiedUtf8();
2118 dst_type = kRegTypeUnknown;
2119 failure = VERIFY_ERROR_GENERIC;
2120 break;
2121 }
2122 } else {
2123 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002124 * Null array ref; this code path will fail at runtime. We
jeffhaobdb76512011-09-07 11:43:16 -07002125 * know this is either long or double, so label it const.
2126 */
2127 dst_type = kRegTypeConstLo;
2128 }
2129 SetRegisterType(work_line, dec_insn.vA_, dst_type);
2130 }
2131 break;
2132
2133 case Instruction::AGET_OBJECT:
2134 {
2135 RegType dst_type, index_type;
2136
2137 index_type = GetRegisterType(work_line, dec_insn.vC_);
2138 CheckArrayIndexType(method, index_type, &failure);
2139 if (failure != VERIFY_ERROR_NONE)
2140 break;
2141
2142 /* get the class of the array we're pulling an object from */
2143 res_class = GetClassFromRegister(work_line, dec_insn.vB_, &failure);
2144 if (failure != VERIFY_ERROR_NONE)
2145 break;
2146 if (res_class != NULL) {
2147 Class* element_class;
2148
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002149 DCHECK(res_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07002150 if (!res_class->IsArrayClass()) {
2151 LOG(ERROR) << "VFY: aget-object on non-array class";
2152 failure = VERIFY_ERROR_GENERIC;
2153 break;
2154 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002155 DCHECK(res_class->GetComponentType() != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07002156
2157 /*
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002158 * Find the element class.
jeffhaobdb76512011-09-07 11:43:16 -07002159 */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002160 element_class = res_class->GetComponentType();
2161 if (element_class->IsPrimitive()) {
jeffhaobdb76512011-09-07 11:43:16 -07002162 LOG(ERROR) << "VFY: aget-object on non-ref array class ("
2163 << res_class->GetDescriptor()->ToModifiedUtf8() << ")";
2164 failure = VERIFY_ERROR_GENERIC;
2165 break;
2166 }
2167
2168 dst_type = RegTypeFromClass(element_class);
2169 } else {
2170 /*
2171 * The array reference is NULL, so the current code path will
jeffhaod1f0fde2011-09-08 17:25:33 -07002172 * throw an exception. For proper merging with later code
jeffhaobdb76512011-09-07 11:43:16 -07002173 * paths, and correct handling of "if-eqz" tests on the
2174 * result of the array get, we want to treat this as a null
2175 * reference.
2176 */
2177 dst_type = kRegTypeZero;
2178 }
2179 SetRegisterType(work_line, dec_insn.vA_, dst_type);
2180 }
2181 break;
2182 case Instruction::APUT:
2183 tmp_type = kRegTypeInteger;
2184 goto aput_1nr_common;
2185 case Instruction::APUT_BOOLEAN:
2186 tmp_type = kRegTypeBoolean;
2187 goto aput_1nr_common;
2188 case Instruction::APUT_BYTE:
2189 tmp_type = kRegTypeByte;
2190 goto aput_1nr_common;
2191 case Instruction::APUT_CHAR:
2192 tmp_type = kRegTypeChar;
2193 goto aput_1nr_common;
2194 case Instruction::APUT_SHORT:
2195 tmp_type = kRegTypeShort;
2196 goto aput_1nr_common;
2197aput_1nr_common:
2198 {
2199 RegType src_type, dst_type, index_type;
2200
2201 index_type = GetRegisterType(work_line, dec_insn.vC_);
2202 CheckArrayIndexType(method, index_type, &failure);
2203 if (failure != VERIFY_ERROR_NONE)
2204 break;
2205
2206 res_class = GetClassFromRegister(work_line, dec_insn.vB_, &failure);
2207 if (failure != VERIFY_ERROR_NONE)
2208 break;
2209
2210 /* res_class can be null if the reg type is Zero */
2211 if (res_class == NULL)
2212 break;
2213
2214 Class::PrimitiveType prim_type =
2215 res_class->GetComponentType()->GetPrimitiveType();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002216 if (!res_class->IsArrayClass() || prim_type == Class::kPrimNot) {
jeffhaobdb76512011-09-07 11:43:16 -07002217 LOG(ERROR) << "VFY: invalid aput-1nr on "
2218 << res_class->GetDescriptor()->ToModifiedUtf8();
2219 failure = VERIFY_ERROR_GENERIC;
2220 break;
2221 }
2222
2223 /* verify that instruction matches array */
2224 dst_type = PrimitiveTypeToRegType(prim_type);
2225
2226 /* correct if float */
2227 if (dst_type == kRegTypeFloat)
2228 tmp_type = kRegTypeFloat;
2229
2230 /* make sure the source register has the correct type */
2231 src_type = GetRegisterType(work_line, dec_insn.vA_);
2232 if (!CanConvertTo1nr(src_type, tmp_type)) {
2233 LOG(ERROR) << "VFY: invalid reg type " << src_type
2234 << " on aput instr (need " << tmp_type << ")";
2235 failure = VERIFY_ERROR_GENERIC;
2236 break;
2237 }
2238
2239 VerifyRegisterType(work_line, dec_insn.vA_, dst_type, &failure);
2240
2241 if (failure != VERIFY_ERROR_NONE || dst_type == kRegTypeUnknown ||
2242 tmp_type != dst_type) {
2243 LOG(ERROR) << "VFY: invalid aput-1nr on "
2244 << res_class->GetDescriptor()->ToModifiedUtf8()
2245 << " (inst=" << tmp_type << " dst=" << dst_type << ")";
2246 failure = VERIFY_ERROR_GENERIC;
2247 break;
2248 }
2249 }
2250 break;
2251 case Instruction::APUT_WIDE:
2252 tmp_type = GetRegisterType(work_line, dec_insn.vC_);
2253 CheckArrayIndexType(method, tmp_type, &failure);
2254 if (failure != VERIFY_ERROR_NONE)
2255 break;
2256
2257 res_class = GetClassFromRegister(work_line, dec_insn.vB_, &failure);
2258 if (failure != VERIFY_ERROR_NONE)
2259 break;
2260 if (res_class != NULL) {
2261 Class::PrimitiveType prim_type =
2262 res_class->GetComponentType()->GetPrimitiveType();
2263 /* verify the class and try to refine "dst_type" */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002264 if (!res_class->IsArrayClass() || prim_type == Class::kPrimNot)
jeffhaobdb76512011-09-07 11:43:16 -07002265 {
2266 LOG(ERROR) << "VFY: invalid aput-wide on "
2267 << res_class->GetDescriptor()->ToModifiedUtf8();
2268 failure = VERIFY_ERROR_GENERIC;
2269 break;
2270 }
2271
2272 switch (prim_type) {
2273 case Class::kPrimLong:
2274 VerifyRegisterType(work_line, dec_insn.vA_, kRegTypeLongLo,
2275 &failure);
2276 break;
2277 case Class::kPrimDouble:
2278 VerifyRegisterType(work_line, dec_insn.vA_, kRegTypeDoubleLo,
2279 &failure);
2280 break;
2281 default:
2282 LOG(ERROR) << "VFY: invalid aput-wide on "
2283 << res_class->GetDescriptor()->ToModifiedUtf8();
2284 failure = VERIFY_ERROR_GENERIC;
2285 break;
2286 }
2287 }
2288 break;
2289 case Instruction::APUT_OBJECT:
2290 tmp_type = GetRegisterType(work_line, dec_insn.vC_);
2291 CheckArrayIndexType(method, tmp_type, &failure);
2292 if (failure != VERIFY_ERROR_NONE)
2293 break;
2294
2295 /* get the ref we're storing; Zero is okay, Uninit is not */
2296 res_class = GetClassFromRegister(work_line, dec_insn.vA_, &failure);
2297 if (failure != VERIFY_ERROR_NONE)
2298 break;
2299 if (res_class != NULL) {
2300 Class* array_class;
2301 Class* element_class;
2302
2303 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002304 * Get the array class. If the array ref is null, we won't
jeffhaobdb76512011-09-07 11:43:16 -07002305 * have type information (and we'll crash at runtime with a
2306 * null pointer exception).
2307 */
2308 array_class = GetClassFromRegister(work_line, dec_insn.vB_, &failure);
2309
2310 if (array_class != NULL) {
2311 /* see if the array holds a compatible type */
2312 if (!array_class->IsArrayClass()) {
2313 LOG(ERROR) << "VFY: invalid aput-object on "
2314 << array_class->GetDescriptor()->ToModifiedUtf8();
2315 failure = VERIFY_ERROR_GENERIC;
2316 break;
2317 }
2318
2319 /*
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002320 * Find the element class.
jeffhaobdb76512011-09-07 11:43:16 -07002321 *
2322 * All we want to check here is that the element type is a
jeffhaod1f0fde2011-09-08 17:25:33 -07002323 * reference class. We *don't* check instanceof here, because
jeffhaobdb76512011-09-07 11:43:16 -07002324 * you can still put a String into a String[] after the latter
2325 * has been cast to an Object[].
2326 */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002327 element_class = array_class->GetComponentType();
2328 if (element_class->IsPrimitive()) {
jeffhaobdb76512011-09-07 11:43:16 -07002329 LOG(ERROR) << "VFY: invalid aput-object of "
2330 << res_class->GetDescriptor()->ToModifiedUtf8()
2331 << " into "
2332 << array_class->GetDescriptor()->ToModifiedUtf8();
2333 failure = VERIFY_ERROR_GENERIC;
2334 break;
2335 }
2336 }
2337 }
2338 break;
2339
2340 case Instruction::IGET:
2341 tmp_type = kRegTypeInteger;
2342 goto iget_1nr_common;
2343 case Instruction::IGET_BOOLEAN:
2344 tmp_type = kRegTypeBoolean;
2345 goto iget_1nr_common;
2346 case Instruction::IGET_BYTE:
2347 tmp_type = kRegTypeByte;
2348 goto iget_1nr_common;
2349 case Instruction::IGET_CHAR:
2350 tmp_type = kRegTypeChar;
2351 goto iget_1nr_common;
2352 case Instruction::IGET_SHORT:
2353 tmp_type = kRegTypeShort;
2354 goto iget_1nr_common;
2355iget_1nr_common:
2356 {
2357 Field* inst_field;
2358 RegType obj_type, field_type;
2359
2360 obj_type = GetRegisterType(work_line, dec_insn.vB_);
2361 inst_field = GetInstField(vdata, obj_type, dec_insn.vC_, &failure);
2362 if (failure != VERIFY_ERROR_NONE)
2363 break;
2364
2365 /* make sure the field's type is compatible with expectation */
2366 field_type =
2367 PrimitiveTypeToRegType(inst_field->GetType()->GetPrimitiveType());
2368
2369 /* correct if float */
2370 if (field_type == kRegTypeFloat)
2371 tmp_type = kRegTypeFloat;
2372
2373 if (field_type == kRegTypeUnknown || tmp_type != field_type) {
2374 Class* inst_field_class = inst_field->GetDeclaringClass();
2375 LOG(ERROR) << "VFY: invalid iget-1nr of "
2376 << inst_field_class->GetDescriptor()->ToModifiedUtf8()
2377 << "." << inst_field->GetName()->ToModifiedUtf8()
2378 << " (inst=" << tmp_type << " field=" << field_type << ")";
2379 failure = VERIFY_ERROR_GENERIC;
2380 break;
2381 }
2382
2383 SetRegisterType(work_line, dec_insn.vA_, tmp_type);
2384 }
2385 break;
2386 case Instruction::IGET_WIDE:
2387 {
2388 RegType dst_type;
2389 Field* inst_field;
2390 RegType obj_type;
2391
2392 obj_type = GetRegisterType(work_line, dec_insn.vB_);
2393 inst_field = GetInstField(vdata, obj_type, dec_insn.vC_, &failure);
2394 Class* inst_field_class = inst_field->GetDeclaringClass();
2395 if (failure != VERIFY_ERROR_NONE)
2396 break;
2397 /* check the type, which should be prim */
2398 switch (inst_field->GetType()->GetPrimitiveType()) {
2399 case Class::kPrimDouble:
2400 dst_type = kRegTypeDoubleLo;
2401 break;
2402 case Class::kPrimLong:
2403 dst_type = kRegTypeLongLo;
2404 break;
2405 default:
2406 LOG(ERROR) << "VFY: invalid iget-wide of "
2407 << inst_field_class->GetDescriptor()->ToModifiedUtf8()
2408 << "." << inst_field->GetName()->ToModifiedUtf8();
2409 dst_type = kRegTypeUnknown;
2410 failure = VERIFY_ERROR_GENERIC;
2411 break;
2412 }
2413 if (failure == VERIFY_ERROR_NONE) {
2414 SetRegisterType(work_line, dec_insn.vA_, dst_type);
2415 }
2416 }
2417 break;
2418 case Instruction::IGET_OBJECT:
2419 {
2420 Class* field_class;
2421 Field* inst_field;
2422 RegType obj_type;
2423
2424 obj_type = GetRegisterType(work_line, dec_insn.vB_);
2425 inst_field = GetInstField(vdata, obj_type, dec_insn.vC_, &failure);
2426 if (failure != VERIFY_ERROR_NONE)
2427 break;
2428 field_class = inst_field->GetType();
2429 if (field_class == NULL) {
2430 /* class not found or primitive type */
2431 LOG(ERROR) << "VFY: unable to recover field class from "
2432 << inst_field->GetName()->ToModifiedUtf8();
2433 failure = VERIFY_ERROR_GENERIC;
2434 break;
2435 }
2436 if (failure == VERIFY_ERROR_NONE) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002437 DCHECK(!field_class->IsPrimitive()) << PrettyClass(field_class);
jeffhaobdb76512011-09-07 11:43:16 -07002438 SetRegisterType(work_line, dec_insn.vA_,
2439 RegTypeFromClass(field_class));
2440 }
2441 }
2442 break;
2443 case Instruction::IPUT:
2444 tmp_type = kRegTypeInteger;
2445 goto iput_1nr_common;
2446 case Instruction::IPUT_BOOLEAN:
2447 tmp_type = kRegTypeBoolean;
2448 goto iput_1nr_common;
2449 case Instruction::IPUT_BYTE:
2450 tmp_type = kRegTypeByte;
2451 goto iput_1nr_common;
2452 case Instruction::IPUT_CHAR:
2453 tmp_type = kRegTypeChar;
2454 goto iput_1nr_common;
2455 case Instruction::IPUT_SHORT:
2456 tmp_type = kRegTypeShort;
2457 goto iput_1nr_common;
2458iput_1nr_common:
2459 {
2460 RegType src_type, field_type, obj_type;
2461 Field* inst_field;
2462
2463 obj_type = GetRegisterType(work_line, dec_insn.vB_);
2464 inst_field = GetInstField(vdata, obj_type, dec_insn.vC_, &failure);
2465 if (failure != VERIFY_ERROR_NONE)
2466 break;
2467 CheckFinalFieldAccess(method, inst_field, &failure);
2468 if (failure != VERIFY_ERROR_NONE)
2469 break;
2470
2471 /* get type of field we're storing into */
2472 field_type =
2473 PrimitiveTypeToRegType(inst_field->GetType()->GetPrimitiveType());
2474 src_type = GetRegisterType(work_line, dec_insn.vA_);
2475
2476 /* correct if float */
2477 if (field_type == kRegTypeFloat)
2478 tmp_type = kRegTypeFloat;
2479
2480 /*
2481 * compiler can generate synthetic functions that write byte values
2482 * into boolean fields.
2483 */
2484 if (tmp_type == kRegTypeBoolean && src_type == kRegTypeByte)
2485 tmp_type = kRegTypeByte;
2486 if (field_type == kRegTypeBoolean && src_type == kRegTypeByte)
2487 field_type = kRegTypeByte;
2488
2489 /* make sure the source register has the correct type */
2490 if (!CanConvertTo1nr(src_type, tmp_type)) {
2491 LOG(ERROR) << "VFY: invalid reg type " << src_type
2492 << " on iput instr (need " << tmp_type << ")",
2493 failure = VERIFY_ERROR_GENERIC;
2494 break;
2495 }
2496
2497 VerifyRegisterType(work_line, dec_insn.vA_, field_type, &failure);
2498
2499 if (failure != VERIFY_ERROR_NONE || field_type == kRegTypeUnknown ||
2500 tmp_type != field_type) {
2501 Class* inst_field_class = inst_field->GetDeclaringClass();
2502 LOG(ERROR) << "VFY: invalid iput-1nr of "
2503 << inst_field_class->GetDescriptor()->ToModifiedUtf8()
2504 << "." << inst_field->GetName()->ToModifiedUtf8()
2505 << " (inst=" << tmp_type << " field=" << field_type << ")";
2506 failure = VERIFY_ERROR_GENERIC;
2507 break;
2508 }
2509 }
2510 break;
2511 case Instruction::IPUT_WIDE:
2512 Field* inst_field;
2513 RegType obj_type;
2514
2515 obj_type = GetRegisterType(work_line, dec_insn.vB_);
2516 inst_field = GetInstField(vdata, obj_type, dec_insn.vC_, &failure);
2517 if (failure != VERIFY_ERROR_NONE)
2518 break;
2519 CheckFinalFieldAccess(method, inst_field, &failure);
2520 if (failure != VERIFY_ERROR_NONE)
2521 break;
2522
2523 /* check the type, which should be prim */
2524 switch (inst_field->GetType()->GetPrimitiveType()) {
2525 case Class::kPrimDouble:
2526 VerifyRegisterType(work_line, dec_insn.vA_, kRegTypeDoubleLo,
2527 &failure);
2528 break;
2529 case Class::kPrimLong:
2530 VerifyRegisterType(work_line, dec_insn.vA_, kRegTypeLongLo, &failure);
2531 break;
2532 default:
2533 LOG(ERROR) << "VFY: invalid iput-wide of "
2534 << inst_field->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
2535 << "." << inst_field->GetName()->ToModifiedUtf8();
2536 failure = VERIFY_ERROR_GENERIC;
2537 break;
2538 }
2539 break;
2540 case Instruction::IPUT_OBJECT:
2541 {
2542 Class* field_class;
2543 Class* value_class;
2544 Field* inst_field;
2545 RegType obj_type, value_type;
2546
2547 obj_type = GetRegisterType(work_line, dec_insn.vB_);
2548 inst_field = GetInstField(vdata, obj_type, dec_insn.vC_, &failure);
2549 if (failure != VERIFY_ERROR_NONE)
2550 break;
2551 CheckFinalFieldAccess(method, inst_field, &failure);
2552 if (failure != VERIFY_ERROR_NONE)
2553 break;
2554
2555 field_class = inst_field->GetType();
2556 if (field_class == NULL) {
2557 LOG(ERROR) << "VFY: unable to recover field class from '"
2558 << inst_field->GetName()->ToModifiedUtf8() << "'";
2559 failure = VERIFY_ERROR_GENERIC;
2560 break;
2561 }
2562
2563 value_type = GetRegisterType(work_line, dec_insn.vA_);
2564 if (!RegTypeIsReference(value_type)) {
2565 LOG(ERROR) << "VFY: storing non-ref v" << dec_insn.vA_
2566 << " into ref field '"
2567 << inst_field->GetName()->ToModifiedUtf8() << "' ("
2568 << field_class->GetDescriptor()->ToModifiedUtf8() << ")";
2569 failure = VERIFY_ERROR_GENERIC;
2570 break;
2571 }
2572 if (value_type != kRegTypeZero) {
2573 value_class = RegTypeInitializedReferenceToClass(value_type);
2574 if (value_class == NULL) {
2575 LOG(ERROR) << "VFY: storing uninit ref v" << dec_insn.vA_
2576 << " into ref field";
2577 failure = VERIFY_ERROR_GENERIC;
2578 break;
2579 }
2580 /* allow if field is any interface or field is base class */
2581 if (!field_class->IsInterface() &&
2582 !field_class->IsAssignableFrom(value_class)) {
2583 Class* inst_field_class = inst_field->GetDeclaringClass();
2584 LOG(ERROR) << "VFY: storing type '"
2585 << value_class->GetDescriptor()->ToModifiedUtf8()
2586 << "' into field type '"
2587 << field_class->GetDescriptor()->ToModifiedUtf8()
2588 << "' ("
2589 << inst_field_class->GetDescriptor()->ToModifiedUtf8()
2590 << "." << inst_field->GetName()->ToModifiedUtf8() << ")";
2591 failure = VERIFY_ERROR_GENERIC;
2592 break;
2593 }
2594 }
2595 }
2596 break;
2597
2598 case Instruction::SGET:
2599 tmp_type = kRegTypeInteger;
2600 goto sget_1nr_common;
2601 case Instruction::SGET_BOOLEAN:
2602 tmp_type = kRegTypeBoolean;
2603 goto sget_1nr_common;
2604 case Instruction::SGET_BYTE:
2605 tmp_type = kRegTypeByte;
2606 goto sget_1nr_common;
2607 case Instruction::SGET_CHAR:
2608 tmp_type = kRegTypeChar;
2609 goto sget_1nr_common;
2610 case Instruction::SGET_SHORT:
2611 tmp_type = kRegTypeShort;
2612 goto sget_1nr_common;
2613sget_1nr_common:
2614 {
2615 Field* static_field;
2616 RegType field_type;
2617
2618 static_field = GetStaticField(vdata, dec_insn.vB_, &failure);
2619 if (failure != VERIFY_ERROR_NONE)
2620 break;
2621
2622 /*
2623 * Make sure the field's type is compatible with expectation.
2624 * We can get ourselves into trouble if we mix & match loads
2625 * and stores with different widths, so rather than just checking
2626 * "CanConvertTo1nr" we require that the field types have equal
2627 * widths.
2628 */
2629 field_type =
2630 PrimitiveTypeToRegType(static_field->GetType()->GetPrimitiveType());
2631
2632 /* correct if float */
2633 if (field_type == kRegTypeFloat)
2634 tmp_type = kRegTypeFloat;
2635
2636 if (tmp_type != field_type) {
2637 Class* static_field_class = static_field->GetDeclaringClass();
2638 LOG(ERROR) << "VFY: invalid sget-1nr of "
2639 << static_field_class->GetDescriptor()->ToModifiedUtf8()
2640 << "." << static_field->GetName()->ToModifiedUtf8()
2641 << " (inst=" << tmp_type << " actual=" << field_type
2642 << ")";
2643 failure = VERIFY_ERROR_GENERIC;
2644 break;
2645 }
2646
2647 SetRegisterType(work_line, dec_insn.vA_, tmp_type);
2648 }
2649 break;
2650 case Instruction::SGET_WIDE:
2651 {
2652 Field* static_field;
2653 RegType dst_type;
2654
2655 static_field = GetStaticField(vdata, dec_insn.vB_, &failure);
2656 Class* static_field_class = static_field->GetDeclaringClass();
2657 if (failure != VERIFY_ERROR_NONE)
2658 break;
2659 /* check the type, which should be prim */
2660 switch (static_field->GetType()->GetPrimitiveType()) {
2661 case Class::kPrimDouble:
2662 dst_type = kRegTypeDoubleLo;
2663 break;
2664 case Class::kPrimLong:
2665 dst_type = kRegTypeLongLo;
2666 break;
2667 default:
2668 LOG(ERROR) << "VFY: invalid sget-wide of "
2669 << static_field_class->GetDescriptor()->ToModifiedUtf8()
2670 << "." << static_field->GetName()->ToModifiedUtf8();
2671 dst_type = kRegTypeUnknown;
2672 failure = VERIFY_ERROR_GENERIC;
2673 break;
2674 }
2675 if (failure == VERIFY_ERROR_NONE) {
2676 SetRegisterType(work_line, dec_insn.vA_, dst_type);
2677 }
2678 }
2679 break;
2680 case Instruction::SGET_OBJECT:
2681 {
2682 Field* static_field;
2683 Class* field_class;
2684
2685 static_field = GetStaticField(vdata, dec_insn.vB_, &failure);
2686 if (failure != VERIFY_ERROR_NONE)
2687 break;
2688 field_class = static_field->GetType();
2689 if (field_class == NULL) {
2690 LOG(ERROR) << "VFY: unable to recover field class from '"
2691 << static_field->GetName()->ToModifiedUtf8() << "'";
2692 failure = VERIFY_ERROR_GENERIC;
2693 break;
2694 }
2695 if (field_class->IsPrimitive()) {
2696 LOG(ERROR) << "VFY: attempt to get prim field with sget-object";
2697 failure = VERIFY_ERROR_GENERIC;
2698 break;
2699 }
2700 SetRegisterType(work_line, dec_insn.vA_, RegTypeFromClass(field_class));
2701 }
2702 break;
2703 case Instruction::SPUT:
2704 tmp_type = kRegTypeInteger;
2705 goto sput_1nr_common;
2706 case Instruction::SPUT_BOOLEAN:
2707 tmp_type = kRegTypeBoolean;
2708 goto sput_1nr_common;
2709 case Instruction::SPUT_BYTE:
2710 tmp_type = kRegTypeByte;
2711 goto sput_1nr_common;
2712 case Instruction::SPUT_CHAR:
2713 tmp_type = kRegTypeChar;
2714 goto sput_1nr_common;
2715 case Instruction::SPUT_SHORT:
2716 tmp_type = kRegTypeShort;
2717 goto sput_1nr_common;
2718sput_1nr_common:
2719 {
2720 RegType src_type, field_type;
2721 Field* static_field;
2722
2723 static_field = GetStaticField(vdata, dec_insn.vB_, &failure);
2724 if (failure != VERIFY_ERROR_NONE)
2725 break;
2726 CheckFinalFieldAccess(method, static_field, &failure);
2727 if (failure != VERIFY_ERROR_NONE)
2728 break;
2729
2730 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002731 * Get type of field we're storing into. We know that the
jeffhaobdb76512011-09-07 11:43:16 -07002732 * contents of the register match the instruction, but we also
2733 * need to ensure that the instruction matches the field type.
2734 * Using e.g. sput-short to write into a 32-bit integer field
2735 * can lead to trouble if we do 16-bit writes.
2736 */
2737 field_type =
2738 PrimitiveTypeToRegType(static_field->GetType()->GetPrimitiveType());
2739 src_type = GetRegisterType(work_line, dec_insn.vA_);
2740
2741 /* correct if float */
2742 if (field_type == kRegTypeFloat)
2743 tmp_type = kRegTypeFloat;
2744
2745 /*
2746 * compiler can generate synthetic functions that write byte values
2747 * into boolean fields.
2748 */
2749 if (tmp_type == kRegTypeBoolean && src_type == kRegTypeByte)
2750 tmp_type = kRegTypeByte;
2751 if (field_type == kRegTypeBoolean && src_type == kRegTypeByte)
2752 field_type = kRegTypeByte;
2753
2754 /* make sure the source register has the correct type */
2755 if (!CanConvertTo1nr(src_type, tmp_type)) {
2756 LOG(ERROR) << "VFY: invalid reg type " << src_type
2757 << " on sput instr (need " << tmp_type << ")";
2758 failure = VERIFY_ERROR_GENERIC;
2759 break;
2760 }
2761
2762 VerifyRegisterType(work_line, dec_insn.vA_, field_type, &failure);
2763
2764 if (failure != VERIFY_ERROR_NONE || field_type == kRegTypeUnknown ||
2765 tmp_type != field_type) {
2766 Class* static_field_class = static_field->GetDeclaringClass();
2767 LOG(ERROR) << "VFY: invalid sput-1nr of "
2768 << static_field_class->GetDescriptor()->ToModifiedUtf8()
2769 << "." << static_field->GetName()->ToModifiedUtf8()
2770 << " (inst=" << tmp_type << " actual=" << field_type
2771 << ")";
2772 failure = VERIFY_ERROR_GENERIC;
2773 break;
2774 }
2775 }
2776 break;
2777 case Instruction::SPUT_WIDE:
2778 Field* static_field;
2779
2780 static_field = GetStaticField(vdata, dec_insn.vB_, &failure);
2781 if (failure != VERIFY_ERROR_NONE)
2782 break;
2783 CheckFinalFieldAccess(method, static_field, &failure);
2784 if (failure != VERIFY_ERROR_NONE)
2785 break;
2786
2787 /* check the type, which should be prim */
2788 switch (static_field->GetType()->GetPrimitiveType()) {
2789 case Class::kPrimDouble:
2790 VerifyRegisterType(work_line, dec_insn.vA_, kRegTypeDoubleLo,
2791 &failure);
2792 break;
2793 case Class::kPrimLong:
2794 VerifyRegisterType(work_line, dec_insn.vA_, kRegTypeLongLo, &failure);
2795 break;
2796 default:
2797 LOG(ERROR) << "VFY: invalid sput-wide of "
2798 << static_field->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
2799 << "." << static_field->GetName()->ToModifiedUtf8();
2800 failure = VERIFY_ERROR_GENERIC;
2801 break;
2802 }
2803 break;
2804 case Instruction::SPUT_OBJECT:
2805 {
2806 Class* field_class;
2807 Class* value_class;
2808 Field* static_field;
2809 RegType value_type;
2810
2811 static_field = GetStaticField(vdata, dec_insn.vB_, &failure);
2812 if (failure != VERIFY_ERROR_NONE)
2813 break;
2814 CheckFinalFieldAccess(method, static_field, &failure);
2815 if (failure != VERIFY_ERROR_NONE)
2816 break;
2817
2818 field_class = static_field->GetType();
2819 if (field_class == NULL) {
2820 LOG(ERROR) << "VFY: unable to recover field class from '"
2821 << static_field->GetName()->ToModifiedUtf8() << "'";
2822 failure = VERIFY_ERROR_GENERIC;
2823 break;
2824 }
2825
2826 value_type = GetRegisterType(work_line, dec_insn.vA_);
2827 if (!RegTypeIsReference(value_type)) {
2828 LOG(ERROR) << "VFY: storing non-ref v" << dec_insn.vA_
2829 << " into ref field '"
2830 << static_field->GetName()->ToModifiedUtf8() << "' ("
2831 << field_class->GetDescriptor()->ToModifiedUtf8() << ")",
2832 failure = VERIFY_ERROR_GENERIC;
2833 break;
2834 }
2835 if (value_type != kRegTypeZero) {
2836 value_class = RegTypeInitializedReferenceToClass(value_type);
2837 if (value_class == NULL) {
2838 LOG(ERROR) << "VFY: storing uninit ref v" << dec_insn.vA_
2839 << " into ref field";
2840 failure = VERIFY_ERROR_GENERIC;
2841 break;
2842 }
2843 /* allow if field is any interface or field is base class */
2844 if (!field_class->IsInterface() &&
2845 !field_class->IsAssignableFrom(value_class)) {
2846 Class* static_field_class = static_field->GetDeclaringClass();
2847 LOG(ERROR) << "VFY: storing type '"
2848 << value_class->GetDescriptor()->ToModifiedUtf8()
2849 << "' into field type '"
2850 << field_class->GetDescriptor()->ToModifiedUtf8()
2851 << "' ("
2852 << static_field_class->GetDescriptor()->ToModifiedUtf8()
2853 << "." << static_field->GetName()->ToModifiedUtf8()
2854 << ")",
2855 failure = VERIFY_ERROR_GENERIC;
2856 break;
2857 }
2858 }
2859 }
2860 break;
2861
2862 case Instruction::INVOKE_VIRTUAL:
2863 case Instruction::INVOKE_VIRTUAL_RANGE:
2864 case Instruction::INVOKE_SUPER:
2865 case Instruction::INVOKE_SUPER_RANGE:
2866 {
2867 Method* called_method;
2868 RegType return_type;
2869 bool is_range;
2870 bool is_super;
2871
2872 is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2873 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2874 is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2875 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2876
2877 called_method = VerifyInvocationArgs(vdata, work_line, registers_size,
2878 &dec_insn, METHOD_VIRTUAL, is_range, is_super, &failure);
2879 if (failure != VERIFY_ERROR_NONE)
2880 break;
2881 return_type = GetMethodReturnType(dex_file, called_method);
2882 SetResultRegisterType(work_line, registers_size, return_type);
2883 just_set_result = true;
2884 }
2885 break;
2886 case Instruction::INVOKE_DIRECT:
2887 case Instruction::INVOKE_DIRECT_RANGE:
2888 {
2889 RegType return_type;
2890 Method* called_method;
2891 bool is_range;
2892
2893 is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2894 called_method = VerifyInvocationArgs(vdata, work_line, registers_size,
2895 &dec_insn, METHOD_DIRECT, is_range, false, &failure);
2896 if (failure != VERIFY_ERROR_NONE)
2897 break;
2898
2899 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002900 * Some additional checks when calling <init>. We know from
jeffhaobdb76512011-09-07 11:43:16 -07002901 * the invocation arg check that the "this" argument is an
jeffhaod1f0fde2011-09-08 17:25:33 -07002902 * instance of called_method->klass. Now we further restrict
jeffhaobdb76512011-09-07 11:43:16 -07002903 * that to require that called_method->klass is the same as
2904 * this->klass or this->super, allowing the latter only if
2905 * the "this" argument is the same as the "this" argument to
2906 * this method (which implies that we're in <init> ourselves).
2907 */
2908 if (IsInitMethod(called_method)) {
2909 RegType this_type;
2910 this_type = GetInvocationThis(work_line, &dec_insn, &failure);
2911 if (failure != VERIFY_ERROR_NONE)
2912 break;
2913
2914 /* no null refs allowed (?) */
2915 if (this_type == kRegTypeZero) {
2916 LOG(ERROR) << "VFY: unable to initialize null ref";
2917 failure = VERIFY_ERROR_GENERIC;
2918 break;
2919 }
2920
2921 Class* this_class;
2922
2923 this_class = RegTypeReferenceToClass(this_type, uninit_map);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002924 DCHECK(this_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07002925
2926 /* must be in same class or in superclass */
2927 if (called_method->GetDeclaringClass() == this_class->GetSuperClass())
2928 {
2929 if (this_class != method->GetDeclaringClass()) {
2930 LOG(ERROR) << "VFY: invoke-direct <init> on super only "
2931 << "allowed for 'this' in <init>";
2932 failure = VERIFY_ERROR_GENERIC;
2933 break;
2934 }
2935 } else if (called_method->GetDeclaringClass() != this_class) {
2936 LOG(ERROR) << "VFY: invoke-direct <init> must be on current "
2937 << "class or super";
2938 failure = VERIFY_ERROR_GENERIC;
2939 break;
2940 }
2941
2942 /* arg must be an uninitialized reference */
2943 if (!RegTypeIsUninitReference(this_type)) {
2944 LOG(ERROR) << "VFY: can only initialize the uninitialized";
2945 failure = VERIFY_ERROR_GENERIC;
2946 break;
2947 }
2948
2949 /*
2950 * Replace the uninitialized reference with an initialized
jeffhaod1f0fde2011-09-08 17:25:33 -07002951 * one, and clear the entry in the uninit map. We need to
jeffhaobdb76512011-09-07 11:43:16 -07002952 * do this for all registers that have the same object
2953 * instance in them, not just the "this" register.
2954 */
2955 MarkRefsAsInitialized(work_line, registers_size, uninit_map,
2956 this_type, &failure);
2957 if (failure != VERIFY_ERROR_NONE)
2958 break;
2959 }
2960 return_type = GetMethodReturnType(dex_file, called_method);
2961 SetResultRegisterType(work_line, registers_size, return_type);
2962 just_set_result = true;
2963 }
2964 break;
2965 case Instruction::INVOKE_STATIC:
2966 case Instruction::INVOKE_STATIC_RANGE:
2967 {
2968 RegType return_type;
2969 Method* called_method;
2970 bool is_range;
2971
2972 is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2973 called_method = VerifyInvocationArgs(vdata, work_line, registers_size,
2974 &dec_insn, METHOD_STATIC, is_range, false, &failure);
2975 if (failure != VERIFY_ERROR_NONE)
2976 break;
2977
2978 return_type = GetMethodReturnType(dex_file, called_method);
2979 SetResultRegisterType(work_line, registers_size, return_type);
2980 just_set_result = true;
2981 }
2982 break;
2983 case Instruction::INVOKE_INTERFACE:
2984 case Instruction::INVOKE_INTERFACE_RANGE:
2985 {
2986 RegType /*this_type,*/ return_type;
2987 Method* abs_method;
2988 bool is_range;
2989
2990 is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2991 abs_method = VerifyInvocationArgs(vdata, work_line, registers_size,
2992 &dec_insn, METHOD_INTERFACE, is_range, false, &failure);
2993 if (failure != VERIFY_ERROR_NONE)
2994 break;
2995
2996#if 0 /* can't do this here, fails on dalvik test 052-verifier-fun */
2997 /*
2998 * Get the type of the "this" arg, which should always be an
jeffhaod1f0fde2011-09-08 17:25:33 -07002999 * interface class. Because we don't do a full merge on
jeffhaobdb76512011-09-07 11:43:16 -07003000 * interface classes, this might have reduced to Object.
3001 */
3002 this_type = GetInvocationThis(work_line, &dec_insn, &failure);
3003 if (failure != VERIFY_ERROR_NONE)
3004 break;
3005
3006 if (this_type == kRegTypeZero) {
3007 /* null pointer always passes (and always fails at runtime) */
3008 } else {
3009 Class* this_class;
3010
3011 this_class = RegTypeInitializedReferenceToClass(this_type);
3012 if (this_class == NULL) {
3013 LOG(ERROR) << "VFY: interface call on uninitialized";
3014 failure = VERIFY_ERROR_GENERIC;
3015 break;
3016 }
3017
3018 /*
3019 * Either "this_class" needs to be the interface class that
3020 * defined abs_method, or abs_method's class needs to be one
jeffhaod1f0fde2011-09-08 17:25:33 -07003021 * of the interfaces implemented by "this_class". (Or, if
jeffhaobdb76512011-09-07 11:43:16 -07003022 * we couldn't complete the merge, this will be Object.)
3023 */
3024 if (this_class != abs_method->GetDeclaringClass() &&
3025 this_class != class_linker->FindSystemClass("Ljava/lang/Object;") &&
3026 !this_class->Implements(abs_method->GetDeclaringClass())) {
3027 LOG(ERROR) << "VFY: unable to match abs_method '"
3028 << abs_method->GetName()->ToModifiedUtf8() << "' with "
3029 << this_class->GetDescriptor()->ToModifiedUtf8()
3030 << " interfaces";
3031 failure = VERIFY_ERROR_GENERIC;
3032 break;
3033 }
3034 }
3035#endif
3036
3037 /*
3038 * We don't have an object instance, so we can't find the
jeffhaod1f0fde2011-09-08 17:25:33 -07003039 * concrete method. However, all of the type information is
jeffhaobdb76512011-09-07 11:43:16 -07003040 * in the abstract method, so we're good.
3041 */
3042 return_type = GetMethodReturnType(dex_file, abs_method);
3043 SetResultRegisterType(work_line, registers_size, return_type);
3044 just_set_result = true;
3045 }
3046 break;
3047
3048 case Instruction::NEG_INT:
3049 case Instruction::NOT_INT:
3050 CheckUnop(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger,
3051 &failure);
3052 break;
3053 case Instruction::NEG_LONG:
3054 case Instruction::NOT_LONG:
3055 CheckUnop(work_line, &dec_insn, kRegTypeLongLo, kRegTypeLongLo, &failure);
3056 break;
3057 case Instruction::NEG_FLOAT:
3058 CheckUnop(work_line, &dec_insn, kRegTypeFloat, kRegTypeFloat, &failure);
3059 break;
3060 case Instruction::NEG_DOUBLE:
3061 CheckUnop(work_line, &dec_insn, kRegTypeDoubleLo, kRegTypeDoubleLo,
3062 &failure);
3063 break;
3064 case Instruction::INT_TO_LONG:
3065 CheckUnop(work_line, &dec_insn, kRegTypeLongLo, kRegTypeInteger,
3066 &failure);
3067 break;
3068 case Instruction::INT_TO_FLOAT:
3069 CheckUnop(work_line, &dec_insn, kRegTypeFloat, kRegTypeInteger, &failure);
3070 break;
3071 case Instruction::INT_TO_DOUBLE:
3072 CheckUnop(work_line, &dec_insn, kRegTypeDoubleLo, kRegTypeInteger,
3073 &failure);
3074 break;
3075 case Instruction::LONG_TO_INT:
3076 CheckUnop(work_line, &dec_insn, kRegTypeInteger, kRegTypeLongLo,
3077 &failure);
3078 break;
3079 case Instruction::LONG_TO_FLOAT:
3080 CheckUnop(work_line, &dec_insn, kRegTypeFloat, kRegTypeLongLo, &failure);
3081 break;
3082 case Instruction::LONG_TO_DOUBLE:
3083 CheckUnop(work_line, &dec_insn, kRegTypeDoubleLo, kRegTypeLongLo,
3084 &failure);
3085 break;
3086 case Instruction::FLOAT_TO_INT:
3087 CheckUnop(work_line, &dec_insn, kRegTypeInteger, kRegTypeFloat, &failure);
3088 break;
3089 case Instruction::FLOAT_TO_LONG:
3090 CheckUnop(work_line, &dec_insn, kRegTypeLongLo, kRegTypeFloat, &failure);
3091 break;
3092 case Instruction::FLOAT_TO_DOUBLE:
3093 CheckUnop(work_line, &dec_insn, kRegTypeDoubleLo, kRegTypeFloat,
3094 &failure);
3095 break;
3096 case Instruction::DOUBLE_TO_INT:
3097 CheckUnop(work_line, &dec_insn, kRegTypeInteger, kRegTypeDoubleLo,
3098 &failure);
3099 break;
3100 case Instruction::DOUBLE_TO_LONG:
3101 CheckUnop(work_line, &dec_insn, kRegTypeLongLo, kRegTypeDoubleLo,
3102 &failure);
3103 break;
3104 case Instruction::DOUBLE_TO_FLOAT:
3105 CheckUnop(work_line, &dec_insn, kRegTypeFloat, kRegTypeDoubleLo,
3106 &failure);
3107 break;
3108 case Instruction::INT_TO_BYTE:
3109 CheckUnop(work_line, &dec_insn, kRegTypeByte, kRegTypeInteger, &failure);
3110 break;
3111 case Instruction::INT_TO_CHAR:
3112 CheckUnop(work_line, &dec_insn, kRegTypeChar, kRegTypeInteger, &failure);
3113 break;
3114 case Instruction::INT_TO_SHORT:
3115 CheckUnop(work_line, &dec_insn, kRegTypeShort, kRegTypeInteger, &failure);
3116 break;
3117
3118 case Instruction::ADD_INT:
3119 case Instruction::SUB_INT:
3120 case Instruction::MUL_INT:
3121 case Instruction::REM_INT:
3122 case Instruction::DIV_INT:
3123 case Instruction::SHL_INT:
3124 case Instruction::SHR_INT:
3125 case Instruction::USHR_INT:
3126 CheckBinop(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger,
3127 kRegTypeInteger, false, &failure);
3128 break;
3129 case Instruction::AND_INT:
3130 case Instruction::OR_INT:
3131 case Instruction::XOR_INT:
3132 CheckBinop(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger,
3133 kRegTypeInteger, true, &failure);
3134 break;
3135 case Instruction::ADD_LONG:
3136 case Instruction::SUB_LONG:
3137 case Instruction::MUL_LONG:
3138 case Instruction::DIV_LONG:
3139 case Instruction::REM_LONG:
3140 case Instruction::AND_LONG:
3141 case Instruction::OR_LONG:
3142 case Instruction::XOR_LONG:
3143 CheckBinop(work_line, &dec_insn, kRegTypeLongLo, kRegTypeLongLo,
3144 kRegTypeLongLo, false, &failure);
3145 break;
3146 case Instruction::SHL_LONG:
3147 case Instruction::SHR_LONG:
3148 case Instruction::USHR_LONG:
3149 /* shift distance is Int, making these different from other binops */
3150 CheckBinop(work_line, &dec_insn, kRegTypeLongLo, kRegTypeLongLo,
3151 kRegTypeInteger, false, &failure);
3152 break;
3153 case Instruction::ADD_FLOAT:
3154 case Instruction::SUB_FLOAT:
3155 case Instruction::MUL_FLOAT:
3156 case Instruction::DIV_FLOAT:
3157 case Instruction::REM_FLOAT:
3158 CheckBinop(work_line, &dec_insn, kRegTypeFloat, kRegTypeFloat,
3159 kRegTypeFloat, false, &failure);
3160 break;
3161 case Instruction::ADD_DOUBLE:
3162 case Instruction::SUB_DOUBLE:
3163 case Instruction::MUL_DOUBLE:
3164 case Instruction::DIV_DOUBLE:
3165 case Instruction::REM_DOUBLE:
3166 CheckBinop(work_line, &dec_insn, kRegTypeDoubleLo, kRegTypeDoubleLo,
3167 kRegTypeDoubleLo, false, &failure);
3168 break;
3169 case Instruction::ADD_INT_2ADDR:
3170 case Instruction::SUB_INT_2ADDR:
3171 case Instruction::MUL_INT_2ADDR:
3172 case Instruction::REM_INT_2ADDR:
3173 case Instruction::SHL_INT_2ADDR:
3174 case Instruction::SHR_INT_2ADDR:
3175 case Instruction::USHR_INT_2ADDR:
3176 CheckBinop2addr(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger,
3177 kRegTypeInteger, false, &failure);
3178 break;
3179 case Instruction::AND_INT_2ADDR:
3180 case Instruction::OR_INT_2ADDR:
3181 case Instruction::XOR_INT_2ADDR:
3182 CheckBinop2addr(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger,
3183 kRegTypeInteger, true, &failure);
3184 break;
3185 case Instruction::DIV_INT_2ADDR:
3186 CheckBinop2addr(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger,
3187 kRegTypeInteger, false, &failure);
3188 break;
3189 case Instruction::ADD_LONG_2ADDR:
3190 case Instruction::SUB_LONG_2ADDR:
3191 case Instruction::MUL_LONG_2ADDR:
3192 case Instruction::DIV_LONG_2ADDR:
3193 case Instruction::REM_LONG_2ADDR:
3194 case Instruction::AND_LONG_2ADDR:
3195 case Instruction::OR_LONG_2ADDR:
3196 case Instruction::XOR_LONG_2ADDR:
3197 CheckBinop2addr(work_line, &dec_insn, kRegTypeLongLo, kRegTypeLongLo,
3198 kRegTypeLongLo, false, &failure);
3199 break;
3200 case Instruction::SHL_LONG_2ADDR:
3201 case Instruction::SHR_LONG_2ADDR:
3202 case Instruction::USHR_LONG_2ADDR:
3203 CheckBinop2addr(work_line, &dec_insn, kRegTypeLongLo, kRegTypeLongLo,
3204 kRegTypeInteger, false, &failure);
3205 break;
3206 case Instruction::ADD_FLOAT_2ADDR:
3207 case Instruction::SUB_FLOAT_2ADDR:
3208 case Instruction::MUL_FLOAT_2ADDR:
3209 case Instruction::DIV_FLOAT_2ADDR:
3210 case Instruction::REM_FLOAT_2ADDR:
3211 CheckBinop2addr(work_line, &dec_insn, kRegTypeFloat, kRegTypeFloat,
3212 kRegTypeFloat, false, &failure);
3213 break;
3214 case Instruction::ADD_DOUBLE_2ADDR:
3215 case Instruction::SUB_DOUBLE_2ADDR:
3216 case Instruction::MUL_DOUBLE_2ADDR:
3217 case Instruction::DIV_DOUBLE_2ADDR:
3218 case Instruction::REM_DOUBLE_2ADDR:
3219 CheckBinop2addr(work_line, &dec_insn, kRegTypeDoubleLo, kRegTypeDoubleLo,
3220 kRegTypeDoubleLo, false, &failure);
3221 break;
3222 case Instruction::ADD_INT_LIT16:
3223 case Instruction::RSUB_INT:
3224 case Instruction::MUL_INT_LIT16:
3225 case Instruction::DIV_INT_LIT16:
3226 case Instruction::REM_INT_LIT16:
3227 CheckLitop(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger, false,
3228 &failure);
3229 break;
3230 case Instruction::AND_INT_LIT16:
3231 case Instruction::OR_INT_LIT16:
3232 case Instruction::XOR_INT_LIT16:
3233 CheckLitop(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger, true,
3234 &failure);
3235 break;
3236 case Instruction::ADD_INT_LIT8:
3237 case Instruction::RSUB_INT_LIT8:
3238 case Instruction::MUL_INT_LIT8:
3239 case Instruction::DIV_INT_LIT8:
3240 case Instruction::REM_INT_LIT8:
3241 case Instruction::SHL_INT_LIT8:
3242 CheckLitop(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger, false,
3243 &failure);
3244 break;
3245 case Instruction::SHR_INT_LIT8:
3246 tmp_type = AdjustForRightShift(work_line, dec_insn.vB_, dec_insn.vC_,
jeffhaob4df5142011-09-19 20:25:32 -07003247 false);
jeffhaobdb76512011-09-07 11:43:16 -07003248 CheckLitop(work_line, &dec_insn, tmp_type, kRegTypeInteger, false,
3249 &failure);
3250 break;
3251 case Instruction::USHR_INT_LIT8:
3252 tmp_type = AdjustForRightShift(work_line, dec_insn.vB_, dec_insn.vC_,
jeffhaob4df5142011-09-19 20:25:32 -07003253 true);
jeffhaobdb76512011-09-07 11:43:16 -07003254 CheckLitop(work_line, &dec_insn, tmp_type, kRegTypeInteger, false,
3255 &failure);
3256 break;
3257 case Instruction::AND_INT_LIT8:
3258 case Instruction::OR_INT_LIT8:
3259 case Instruction::XOR_INT_LIT8:
3260 CheckLitop(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger, true,
3261 &failure);
3262 break;
3263
3264 /*
3265 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07003266 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07003267 * inserted in the course of verification, we can expect to see it here.
3268 */
jeffhaob4df5142011-09-19 20:25:32 -07003269 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07003270 break;
3271
3272 /*
3273 * Verifying "quickened" instructions is tricky, because we have
jeffhaod1f0fde2011-09-08 17:25:33 -07003274 * discarded the original field/method information. The byte offsets
jeffhaobdb76512011-09-07 11:43:16 -07003275 * and vtable indices only have meaning in the context of an object
3276 * instance.
3277 *
3278 * If a piece of code declares a local reference variable, assigns
3279 * null to it, and then issues a virtual method call on it, we
jeffhaod1f0fde2011-09-08 17:25:33 -07003280 * cannot evaluate the method call during verification. This situation
jeffhaobdb76512011-09-07 11:43:16 -07003281 * isn't hard to handle, since we know the call will always result in an
jeffhaod1f0fde2011-09-08 17:25:33 -07003282 * NPE, and the arguments and return value don't matter. Any code that
jeffhaobdb76512011-09-07 11:43:16 -07003283 * depends on the result of the method call is inaccessible, so the
3284 * fact that we can't fully verify anything that comes after the bad
3285 * call is not a problem.
3286 *
3287 * We must also consider the case of multiple code paths, only some of
jeffhaod1f0fde2011-09-08 17:25:33 -07003288 * which involve a null reference. We can completely verify the method
jeffhaobdb76512011-09-07 11:43:16 -07003289 * if we sidestep the results of executing with a null reference.
3290 * For example, if on the first pass through the code we try to do a
3291 * virtual method invocation through a null ref, we have to skip the
3292 * method checks and have the method return a "wildcard" type (which
jeffhaod1f0fde2011-09-08 17:25:33 -07003293 * merges with anything to become that other thing). The move-result
jeffhaobdb76512011-09-07 11:43:16 -07003294 * will tell us if it's a reference, single-word numeric, or double-word
jeffhaod1f0fde2011-09-08 17:25:33 -07003295 * value. We continue to perform the verification, and at the end of
jeffhaobdb76512011-09-07 11:43:16 -07003296 * the function any invocations that were never fully exercised are
3297 * marked as null-only.
3298 *
jeffhaod1f0fde2011-09-08 17:25:33 -07003299 * We would do something similar for the field accesses. The field's
jeffhaobdb76512011-09-07 11:43:16 -07003300 * type, once known, can be used to recover the width of short integers.
3301 * If the object reference was null, the field-get returns the "wildcard"
3302 * type, which is acceptable for any operation.
3303 */
3304 case Instruction::UNUSED_EE:
3305 case Instruction::UNUSED_EF:
3306 case Instruction::UNUSED_F2:
3307 case Instruction::UNUSED_F3:
3308 case Instruction::UNUSED_F4:
3309 case Instruction::UNUSED_F5:
3310 case Instruction::UNUSED_F6:
3311 case Instruction::UNUSED_F7:
3312 case Instruction::UNUSED_F8:
3313 case Instruction::UNUSED_F9:
3314 case Instruction::UNUSED_FA:
3315 case Instruction::UNUSED_FB:
3316 //case Instruction::EXECUTE_INLINE:
3317 //case Instruction::EXECUTE_INLINE_RANGE:
3318 //case Instruction::IGET_QUICK:
3319 //case Instruction::IGET_WIDE_QUICK:
3320 //case Instruction::IGET_OBJECT_QUICK:
3321 //case Instruction::IPUT_QUICK:
3322 //case Instruction::IPUT_WIDE_QUICK:
3323 //case Instruction::IPUT_OBJECT_QUICK:
3324 //case Instruction::INVOKE_VIRTUAL_QUICK:
3325 //case Instruction::INVOKE_VIRTUAL_QUICK_RANGE:
3326 //case Instruction::INVOKE_SUPER_QUICK:
3327 //case Instruction::INVOKE_SUPER_QUICK_RANGE:
3328 /* fall through to failure */
3329
3330 /*
3331 * These instructions are equivalent (from the verifier's point of view)
jeffhaod1f0fde2011-09-08 17:25:33 -07003332 * to the original form. The change was made for correctness rather
jeffhaobdb76512011-09-07 11:43:16 -07003333 * than improved performance (except for invoke-object-init, which
jeffhaod1f0fde2011-09-08 17:25:33 -07003334 * provides both). The substitution takes place after verification
jeffhaobdb76512011-09-07 11:43:16 -07003335 * completes, though, so we don't expect to see them here.
3336 */
3337 case Instruction::UNUSED_F0:
3338 case Instruction::UNUSED_F1:
3339 case Instruction::UNUSED_E3:
3340 case Instruction::UNUSED_E8:
3341 case Instruction::UNUSED_E7:
3342 case Instruction::UNUSED_E4:
3343 case Instruction::UNUSED_E9:
3344 case Instruction::UNUSED_FC:
3345 case Instruction::UNUSED_E5:
3346 case Instruction::UNUSED_EA:
3347 case Instruction::UNUSED_FD:
3348 case Instruction::UNUSED_E6:
3349 case Instruction::UNUSED_EB:
3350 case Instruction::UNUSED_FE:
3351 //case Instruction::INVOKE_OBJECT_INIT_RANGE:
3352 //case Instruction::RETURN_VOID_BARRIER:
3353 //case Instruction::IGET_VOLATILE:
3354 //case Instruction::IGET_WIDE_VOLATILE:
3355 //case Instruction::IGET_OBJECT_VOLATILE:
3356 //case Instruction::IPUT_VOLATILE:
3357 //case Instruction::IPUT_WIDE_VOLATILE:
3358 //case Instruction::IPUT_OBJECT_VOLATILE:
3359 //case Instruction::SGET_VOLATILE:
3360 //case Instruction::SGET_WIDE_VOLATILE:
3361 //case Instruction::SGET_OBJECT_VOLATILE:
3362 //case Instruction::SPUT_VOLATILE:
3363 //case Instruction::SPUT_WIDE_VOLATILE:
3364 //case Instruction::SPUT_OBJECT_VOLATILE:
3365 /* fall through to failure */
3366
3367 /* These should never appear during verification. */
3368 case Instruction::UNUSED_3E:
3369 case Instruction::UNUSED_3F:
3370 case Instruction::UNUSED_40:
3371 case Instruction::UNUSED_41:
3372 case Instruction::UNUSED_42:
3373 case Instruction::UNUSED_43:
3374 case Instruction::UNUSED_73:
3375 case Instruction::UNUSED_79:
3376 case Instruction::UNUSED_7A:
3377 case Instruction::UNUSED_EC:
3378 case Instruction::UNUSED_FF:
3379 //case Instruction::BREAKPOINT:
3380 //case Instruction::DISPATCH_FF:
3381 failure = VERIFY_ERROR_GENERIC;
3382 break;
3383
3384 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003385 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003386 * complain if an instruction is missing (which is desirable).
3387 */
3388 }
3389
3390 if (failure != VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07003391 if (failure == VERIFY_ERROR_GENERIC) {
3392 /* immediate failure, reject class */
3393 LOG(ERROR) << "VFY: rejecting opcode 0x" << std::hex
3394 << (int) dec_insn.opcode_ << " at 0x" << insn_idx << std::dec;
3395 return false;
3396 } else {
3397 /* replace opcode and continue on */
3398 LOG(ERROR) << "VFY: replacing opcode 0x" << std::hex
3399 << (int) dec_insn.opcode_ << " at 0x" << insn_idx << std::dec;
jeffhaob4df5142011-09-19 20:25:32 -07003400 if (!ReplaceFailingInstruction(code_item, insn_idx, failure)) {
jeffhaobdb76512011-09-07 11:43:16 -07003401 LOG(ERROR) << "VFY: rejecting opcode 0x" << std::hex
3402 << (int) dec_insn.opcode_ << " at 0x" << insn_idx
3403 << std::dec;
3404 return false;
3405 }
3406 /* IMPORTANT: method->insns may have been changed */
3407 insns = code_item->insns_ + insn_idx;
3408
3409 /* continue on as if we just handled a throw-verification-error */
3410 failure = VERIFY_ERROR_NONE;
3411 opcode_flag = Instruction::kThrow;
3412 }
3413 }
3414
3415 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003416 * If we didn't just set the result register, clear it out. This
jeffhaobdb76512011-09-07 11:43:16 -07003417 * ensures that you can only use "move-result" immediately after the
jeffhaod1f0fde2011-09-08 17:25:33 -07003418 * result is set. (We could check this statically, but it's not
jeffhaobdb76512011-09-07 11:43:16 -07003419 * expensive and it makes our debugging output cleaner.)
3420 */
3421 if (!just_set_result) {
3422 int reg = RESULT_REGISTER(registers_size);
3423 SetRegisterType(work_line, reg, kRegTypeUnknown);
3424 SetRegisterType(work_line, reg + 1, kRegTypeUnknown);
3425 }
3426
jeffhaoa0a764a2011-09-16 10:43:38 -07003427 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07003428 if ((opcode_flag & Instruction::kContinue) != 0) {
3429 size_t insn_width = InsnGetWidth(insn_flags, insn_idx);
3430 if (insn_idx + insn_width >= insns_size) {
3431 LOG(ERROR) << "VFY: execution can walk off end of code area (from 0x"
3432 << std::hex << insn_idx << std::dec << ")";
3433 return false;
3434 }
3435
3436 /*
3437 * The only way to get to a move-exception instruction is to get
jeffhaod1f0fde2011-09-08 17:25:33 -07003438 * thrown there. Make sure the next instruction isn't one.
jeffhaobdb76512011-09-07 11:43:16 -07003439 */
3440 if (!CheckMoveException(code_item->insns_, insn_idx + insn_width))
3441 return false;
3442
Elliott Hughes5fe594f2011-09-08 12:33:17 -07003443 if (GetRegisterLine(reg_table, insn_idx + insn_width)->reg_types_.get() != NULL) {
jeffhaobdb76512011-09-07 11:43:16 -07003444 /*
3445 * Merge registers into what we have for the next instruction,
3446 * and set the "changed" flag if needed.
3447 */
3448 if (!UpdateRegisters(insn_flags, reg_table, insn_idx + insn_width,
3449 work_line))
3450 return false;
3451 } else {
3452 /*
3453 * We're not recording register data for the next instruction,
jeffhaod1f0fde2011-09-08 17:25:33 -07003454 * so we don't know what the prior state was. We have to
jeffhaobdb76512011-09-07 11:43:16 -07003455 * assume that something has changed and re-evaluate it.
3456 */
3457 InsnSetChanged(insn_flags, insn_idx + insn_width, true);
3458 }
3459 }
3460
3461 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003462 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003463 *
3464 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003465 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003466 * somebody could get a reference field, check it for zero, and if the
3467 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003468 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003469 * that, and will reject the code.
3470 *
3471 * TODO: avoid re-fetching the branch target
3472 */
3473 if ((opcode_flag & Instruction::kBranch) != 0) {
3474 bool isConditional, selfOkay;
3475
3476 if (!GetBranchOffset(code_item, insn_flags, insn_idx, &branch_target,
3477 &isConditional, &selfOkay)) {
3478 /* should never happen after static verification */
3479 LOG(ERROR) << "VFY: bad branch at 0x" << std::hex << insn_idx << std::dec;
3480 return false;
3481 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003482 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
jeffhaobdb76512011-09-07 11:43:16 -07003483
3484 if (!CheckMoveException(code_item->insns_, insn_idx + branch_target))
3485 return false;
3486
3487 /* update branch target, set "changed" if appropriate */
3488 if (!UpdateRegisters(insn_flags, reg_table, insn_idx + branch_target,
3489 work_line))
3490 return false;
3491 }
3492
3493 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003494 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003495 *
3496 * We've already verified that the table is structurally sound, so we
3497 * just need to walk through and tag the targets.
3498 */
3499 if ((opcode_flag & Instruction::kSwitch) != 0) {
3500 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
3501 const uint16_t* switch_insns = insns + offset_to_switch;
3502 int switch_count = switch_insns[1];
3503 int offset_to_targets, targ;
3504
3505 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3506 /* 0 = sig, 1 = count, 2/3 = first key */
3507 offset_to_targets = 4;
3508 } else {
3509 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003510 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003511 offset_to_targets = 2 + 2 * switch_count;
3512 }
3513
3514 /* verify each switch target */
3515 for (targ = 0; targ < switch_count; targ++) {
3516 int offset;
3517 uint32_t abs_offset;
3518
3519 /* offsets are 32-bit, and only partly endian-swapped */
3520 offset = switch_insns[offset_to_targets + targ * 2] |
3521 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
3522 abs_offset = insn_idx + offset;
3523
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003524 DCHECK_LT(abs_offset, insns_size);
jeffhaobdb76512011-09-07 11:43:16 -07003525
3526 if (!CheckMoveException(code_item->insns_, abs_offset))
3527 return false;
3528
3529 if (!UpdateRegisters(insn_flags, reg_table, abs_offset, work_line))
3530 return false;
3531 }
3532 }
3533
3534 /*
3535 * Handle instructions that can throw and that are sitting in a
jeffhaod1f0fde2011-09-08 17:25:33 -07003536 * "try" block. (If they're not in a "try" block when they throw,
jeffhaobdb76512011-09-07 11:43:16 -07003537 * control transfers out of the method.)
3538 */
3539 if ((opcode_flag & Instruction::kThrow) != 0 &&
3540 InsnIsInTry(insn_flags, insn_idx)) {
3541 bool has_catch_all = false;
3542 DexFile::CatchHandlerIterator iterator = DexFile::dexFindCatchHandler(
3543 *code_item, insn_idx);
3544
3545 for (; !iterator.HasNext(); iterator.Next()) {
3546 if (iterator.Get().type_idx_ == DexFile::kDexNoIndex)
3547 has_catch_all = true;
3548
3549 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003550 * Merge registers into the "catch" block. We want to use the
3551 * "savedRegs" rather than "work_regs", because at runtime the
3552 * exception will be thrown before the instruction modifies any
3553 * registers.
jeffhaobdb76512011-09-07 11:43:16 -07003554 */
3555 if (!UpdateRegisters(insn_flags, reg_table, iterator.Get().address_,
3556 &reg_table->saved_line_))
3557 return false;
3558 }
3559
3560 /*
3561 * If the monitor stack depth is nonzero, there must be a "catch all"
jeffhaod1f0fde2011-09-08 17:25:33 -07003562 * handler for this instruction. This does apply to monitor-exit
jeffhaobdb76512011-09-07 11:43:16 -07003563 * because of async exception handling.
3564 */
3565 if (work_line->monitor_stack_top_ != 0 && !has_catch_all) {
3566 /*
3567 * The state in work_line reflects the post-execution state.
3568 * If the current instruction is a monitor-enter and the monitor
3569 * stack was empty, we don't need a catch-all (if it throws,
3570 * it will do so before grabbing the lock).
3571 */
3572 if (!(dec_insn.opcode_ == Instruction::MONITOR_ENTER &&
3573 work_line->monitor_stack_top_ == 1))
3574 {
3575 LOG(ERROR) << "VFY: no catch-all for instruction at 0x" << std::hex
3576 << insn_idx << std::dec;
3577 return false;
3578 }
3579 }
3580 }
3581
jeffhaod1f0fde2011-09-08 17:25:33 -07003582 /* If we're returning from the method, make sure monitor stack is empty. */
jeffhaobdb76512011-09-07 11:43:16 -07003583 if ((opcode_flag & Instruction::kReturn) != 0 &&
3584 work_line->monitor_stack_top_ != 0) {
3585 LOG(ERROR) << "VFY: return with stack depth="
3586 << work_line->monitor_stack_top_ << " at 0x" << std::hex
3587 << insn_idx << std::dec;
3588 return false;
3589 }
3590
3591 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003592 * Update start_guess. Advance to the next instruction of that's
3593 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003594 * neither of those exists we're in a return or throw; leave start_guess
3595 * alone and let the caller sort it out.
3596 */
3597 if ((opcode_flag & Instruction::kContinue) != 0) {
3598 *start_guess = insn_idx + InsnGetWidth(insn_flags, insn_idx);
3599 } else if ((opcode_flag & Instruction::kBranch) != 0) {
3600 /* we're still okay if branch_target is zero */
3601 *start_guess = insn_idx + branch_target;
3602 }
3603
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003604 DCHECK_LT(*start_guess, insns_size);
Brian Carlstrom03c99df2011-09-18 10:52:00 -07003605 DCHECK_NE(InsnGetWidth(insn_flags, *start_guess), 0);
jeffhaobdb76512011-09-07 11:43:16 -07003606
3607 return true;
3608}
3609
3610bool DexVerifier::ReplaceFailingInstruction(const DexFile::CodeItem* code_item,
jeffhaob4df5142011-09-19 20:25:32 -07003611 int insn_idx, VerifyError failure) {
jeffhaobdb76512011-09-07 11:43:16 -07003612 const uint16_t* insns = code_item->insns_ + insn_idx;
3613 const byte* ptr = reinterpret_cast<const byte*>(insns);
3614 const Instruction* inst = Instruction::At(ptr);
3615 Instruction::Code opcode = inst->Opcode();
3616 VerifyErrorRefType ref_type;
3617
3618 /*
3619 * Generate the new instruction out of the old.
3620 *
3621 * First, make sure this is an instruction we're expecting to stomp on.
3622 */
3623 switch (opcode) {
3624 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 bytes
3625 case Instruction::CHECK_CAST:
3626 case Instruction::INSTANCE_OF:
3627 case Instruction::NEW_INSTANCE:
3628 case Instruction::NEW_ARRAY:
3629 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 bytes
3630 case Instruction::FILLED_NEW_ARRAY_RANGE:
3631 ref_type = VERIFY_ERROR_REF_CLASS;
3632 break;
3633
3634 case Instruction::IGET: // insn[1] == field ref, 2 bytes
3635 case Instruction::IGET_BOOLEAN:
3636 case Instruction::IGET_BYTE:
3637 case Instruction::IGET_CHAR:
3638 case Instruction::IGET_SHORT:
3639 case Instruction::IGET_WIDE:
3640 case Instruction::IGET_OBJECT:
3641 case Instruction::IPUT:
3642 case Instruction::IPUT_BOOLEAN:
3643 case Instruction::IPUT_BYTE:
3644 case Instruction::IPUT_CHAR:
3645 case Instruction::IPUT_SHORT:
3646 case Instruction::IPUT_WIDE:
3647 case Instruction::IPUT_OBJECT:
3648 case Instruction::SGET:
3649 case Instruction::SGET_BOOLEAN:
3650 case Instruction::SGET_BYTE:
3651 case Instruction::SGET_CHAR:
3652 case Instruction::SGET_SHORT:
3653 case Instruction::SGET_WIDE:
3654 case Instruction::SGET_OBJECT:
3655 case Instruction::SPUT:
3656 case Instruction::SPUT_BOOLEAN:
3657 case Instruction::SPUT_BYTE:
3658 case Instruction::SPUT_CHAR:
3659 case Instruction::SPUT_SHORT:
3660 case Instruction::SPUT_WIDE:
3661 case Instruction::SPUT_OBJECT:
3662 ref_type = VERIFY_ERROR_REF_FIELD;
3663 break;
3664
3665 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 bytes
3666 case Instruction::INVOKE_VIRTUAL_RANGE:
3667 case Instruction::INVOKE_SUPER:
3668 case Instruction::INVOKE_SUPER_RANGE:
3669 case Instruction::INVOKE_DIRECT:
3670 case Instruction::INVOKE_DIRECT_RANGE:
3671 case Instruction::INVOKE_STATIC:
3672 case Instruction::INVOKE_STATIC_RANGE:
3673 case Instruction::INVOKE_INTERFACE:
3674 case Instruction::INVOKE_INTERFACE_RANGE:
3675 ref_type = VERIFY_ERROR_REF_METHOD;
3676 break;
3677
3678 default:
3679 /* could handle this in a generic way, but this is probably safer */
3680 LOG(ERROR) << "GLITCH: verifier asked to replace opcode 0x" << std::hex
3681 << (int) opcode << std::dec;
3682 return false;
3683 }
3684
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003685 DCHECK(inst->IsThrow());
jeffhaobdb76512011-09-07 11:43:16 -07003686
3687 /* write a NOP over the third code unit, if necessary */
jeffhaob4df5142011-09-19 20:25:32 -07003688 int width = inst->Size();
jeffhaobdb76512011-09-07 11:43:16 -07003689 switch (width) {
3690 case 2:
3691 /* nothing to do */
3692 break;
3693 case 3:
jeffhaob4df5142011-09-19 20:25:32 -07003694 UpdateCodeUnit(insns + 2, Instruction::NOP);
jeffhaobdb76512011-09-07 11:43:16 -07003695 break;
3696 default:
3697 /* whoops */
3698 LOG(FATAL) << "ERROR: stomped a " << width
3699 << "-unit instruction with a verifier error";
3700 }
3701
3702 /* encode the opcode, with the failure code in the high byte */
jeffhaob4df5142011-09-19 20:25:32 -07003703 DCHECK(width == 2 || width == 3);
3704 uint16_t new_val = Instruction::THROW_VERIFICATION_ERROR |
3705 (failure << 8) | (ref_type << (8 + kVerifyErrorRefTypeShift));
3706 UpdateCodeUnit(insns, new_val);
jeffhaobdb76512011-09-07 11:43:16 -07003707
3708 return true;
3709}
3710
jeffhaob4df5142011-09-19 20:25:32 -07003711void DexVerifier::UpdateCodeUnit(const uint16_t* ptr, uint16_t new_val) {
3712 *(uint16_t*) ptr = new_val;
3713}
3714
jeffhaobdb76512011-09-07 11:43:16 -07003715void DexVerifier::HandleMonitorEnter(RegisterLine* work_line, uint32_t reg_idx,
3716 uint32_t insn_idx, VerifyError* failure) {
3717 if (!RegTypeIsReference(GetRegisterType(work_line, reg_idx))) {
3718 LOG(ERROR) << "VFY: monitor-enter on non-object";
3719 *failure = VERIFY_ERROR_GENERIC;
3720 return;
3721 }
3722
Elliott Hughes5fe594f2011-09-08 12:33:17 -07003723 if (work_line->monitor_entries_.get() == NULL) {
jeffhaobdb76512011-09-07 11:43:16 -07003724 return;
3725 }
3726
3727 if (work_line->monitor_stack_top_ == kMaxMonitorStackDepth) {
3728 LOG(ERROR) << "VFY: monitor-enter stack overflow (" << kMaxMonitorStackDepth
3729 << ")";
3730 *failure = VERIFY_ERROR_GENERIC;
3731 return;
3732 }
3733
3734 /*
3735 * Push an entry on the stack, and set a bit in the register flags to
3736 * indicate that it's associated with this register.
3737 */
3738 work_line->monitor_entries_[reg_idx] |= 1 << work_line->monitor_stack_top_;
3739 work_line->monitor_stack_[work_line->monitor_stack_top_++] = insn_idx;
3740}
3741
jeffhaobdb76512011-09-07 11:43:16 -07003742void DexVerifier::HandleMonitorExit(RegisterLine* work_line, uint32_t reg_idx,
3743 uint32_t insn_idx, VerifyError* failure) {
3744 if (!RegTypeIsReference(GetRegisterType(work_line, reg_idx))) {
3745 LOG(ERROR) << "VFY: monitor-exit on non-object";
3746 *failure = VERIFY_ERROR_GENERIC;
3747 return;
3748 }
3749
Elliott Hughes5fe594f2011-09-08 12:33:17 -07003750 if (work_line->monitor_entries_.get() == NULL) {
jeffhaobdb76512011-09-07 11:43:16 -07003751 return;
3752 }
3753
3754 if (work_line->monitor_stack_top_ == 0) {
3755 LOG(ERROR) << "VFY: monitor-exit stack underflow";
3756 *failure = VERIFY_ERROR_GENERIC;
3757 return;
3758 }
3759
3760 /*
3761 * Confirm that the entry at the top of the stack is associated with
jeffhaod1f0fde2011-09-08 17:25:33 -07003762 * the register. Pop the top entry off.
jeffhaobdb76512011-09-07 11:43:16 -07003763 */
3764 work_line->monitor_stack_top_--;
3765#ifdef BUG_3215458_FIXED
3766 /*
3767 * TODO: This code can safely be enabled if know we are working on
3768 * a dex file of format version 036 or later. (That is, we'll need to
3769 * add a check for the version number.)
3770 */
3771 if ((work_line->monitor_entries_[reg_idx] &
3772 (1 << work_line->monitor_stack_top_)) == 0) {
3773 LOG(ERROR) << "VFY: monitor-exit bit " << work_line->monitor_stack_top_
3774 << " not set: addr=0x" << std::hex << insn_idx << std::dec
3775 << " (bits[" << reg_idx << "]=" << std::hex
3776 << work_line->monitor_entries_[reg_idx] << std::dec << ")";
3777 *failure = VERIFY_ERROR_GENERIC;
3778 return;
3779 }
3780#endif
3781 work_line->monitor_stack_[work_line->monitor_stack_top_] = 0;
3782
3783 /* Clear the bit from the register flags. */
3784 work_line->monitor_entries_[reg_idx] &= ~(1 << work_line->monitor_stack_top_);
3785}
3786
3787Field* DexVerifier::GetInstField(VerifierData* vdata, RegType obj_type,
3788 int field_idx, VerifyError* failure) {
3789 Method* method = vdata->method_;
3790 const DexFile* dex_file = vdata->dex_file_;
Elliott Hughes5fe594f2011-09-08 12:33:17 -07003791 UninitInstanceMap* uninit_map = vdata->uninit_map_.get();
jeffhaobdb76512011-09-07 11:43:16 -07003792 bool must_be_local = false;
3793
3794 if (!RegTypeIsReference(obj_type)) {
3795 LOG(ERROR) << "VFY: attempt to access field in non-reference type "
3796 << obj_type;
3797 *failure = VERIFY_ERROR_GENERIC;
jeffhaob4df5142011-09-19 20:25:32 -07003798 return NULL;
jeffhaobdb76512011-09-07 11:43:16 -07003799 }
3800
jeffhaob4df5142011-09-19 20:25:32 -07003801 Field* field = ResolveFieldAndCheckAccess(dex_file, field_idx,
3802 method->GetDeclaringClass(), failure, false);
jeffhaobdb76512011-09-07 11:43:16 -07003803 if (field == NULL) {
3804 LOG(ERROR) << "VFY: unable to resolve instance field " << field_idx;
3805 return field;
3806 }
3807
3808 if (obj_type == kRegTypeZero)
3809 return field;
3810
3811 /*
3812 * Access to fields in uninitialized objects is allowed if this is
3813 * the <init> method for the object and the field in question is
3814 * declared by this class.
3815 */
jeffhaob4df5142011-09-19 20:25:32 -07003816 Class* obj_class = RegTypeReferenceToClass(obj_type, uninit_map);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003817 DCHECK(obj_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07003818 if (RegTypeIsUninitReference(obj_type)) {
3819 if (!IsInitMethod(method) || method->GetDeclaringClass() != obj_class) {
3820 LOG(ERROR) << "VFY: attempt to access field via uninitialized ref";
3821 *failure = VERIFY_ERROR_GENERIC;
3822 return field;
3823 }
3824 must_be_local = true;
3825 }
3826
jeffhaobdb76512011-09-07 11:43:16 -07003827 if (!field->GetDeclaringClass()->IsAssignableFrom(obj_class)) {
3828 LOG(ERROR) << "VFY: invalid field access (field "
3829 << field->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
3830 << "." << field->GetName()->ToModifiedUtf8() << ", through "
3831 << obj_class->GetDescriptor()->ToModifiedUtf8() << " ref)";
3832 *failure = VERIFY_ERROR_NO_FIELD;
3833 return field;
3834 }
3835
3836 if (must_be_local) {
3837 bool found = false;
3838 /* for uninit ref, make sure it's defined by this class, not super */
3839 for (uint32_t i = 0; i < obj_class->NumInstanceFields(); i++) {
3840 found |= (field == obj_class->GetInstanceField(i));
3841 }
3842 if (!found) {
3843 LOG(ERROR) << "VFY: invalid constructor field access (field "
3844 << field->GetName()->ToModifiedUtf8() << " in "
3845 << obj_class->GetDescriptor()->ToModifiedUtf8() << ")";
3846 *failure = VERIFY_ERROR_GENERIC;
3847 return field;
3848 }
3849 }
3850
3851 return field;
3852}
3853
3854Field* DexVerifier::GetStaticField(VerifierData* vdata, int field_idx,
3855 VerifyError* failure) {
3856 Method* method = vdata->method_;
3857 const DexFile* dex_file = vdata->dex_file_;
jeffhaob4df5142011-09-19 20:25:32 -07003858 Field* field = ResolveFieldAndCheckAccess(dex_file, field_idx,
3859 method->GetDeclaringClass(), failure, true);
jeffhaobdb76512011-09-07 11:43:16 -07003860 if (field == NULL) {
jeffhaob4df5142011-09-19 20:25:32 -07003861 const DexFile::FieldId& field_id = dex_file->GetFieldId(field_idx);
3862 LOG(ERROR) << "VFY: unable to resolve static field " << field_idx << " ("
3863 << dex_file->GetFieldName(field_id) << ") in "
3864 << dex_file->GetFieldClassDescriptor(field_id);
3865 *failure = VERIFY_ERROR_NO_FIELD;
jeffhaobdb76512011-09-07 11:43:16 -07003866 }
3867
3868 return field;
3869}
3870
3871Class* DexVerifier::GetCaughtExceptionType(VerifierData* vdata, int insn_idx,
3872 VerifyError* failure) {
3873 const DexFile* dex_file = vdata->dex_file_;
3874 const DexFile::CodeItem* code_item = vdata->code_item_;
3875 Method* method = vdata->method_;
3876 Class* common_super = NULL;
3877 uint32_t handlers_size;
jeffhaoba5ebb92011-08-25 17:24:37 -07003878 const byte* handlers_ptr = DexFile::dexGetCatchHandlerData(*code_item, 0);
jeffhaobdb76512011-09-07 11:43:16 -07003879
3880 if (code_item->tries_size_ != 0) {
3881 handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3882 } else {
3883 handlers_size = 0;
3884 }
3885
3886 for (uint32_t i = 0; i < handlers_size; i++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07003887 DexFile::CatchHandlerIterator iterator(handlers_ptr);
3888
3889 for (; !iterator.HasNext(); iterator.Next()) {
jeffhaobdb76512011-09-07 11:43:16 -07003890 DexFile::CatchHandlerItem handler = iterator.Get();
3891 if (handler.address_ == (uint32_t) insn_idx) {
3892 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3893 Class* klass;
jeffhaoba5ebb92011-08-25 17:24:37 -07003894
jeffhaobdb76512011-09-07 11:43:16 -07003895 if (handler.type_idx_ == DexFile::kDexNoIndex) {
3896 klass = class_linker->FindSystemClass("Ljava/lang/Throwable;");
3897 } else {
jeffhao98eacac2011-09-14 16:11:53 -07003898 klass = ResolveClassAndCheckAccess(dex_file, handler.type_idx_,
3899 method->GetDeclaringClass(), failure);
jeffhaobdb76512011-09-07 11:43:16 -07003900 }
3901
3902 if (klass == NULL) {
3903 LOG(ERROR) << "VFY: unable to resolve exception class "
3904 << handler.type_idx_ << " ("
3905 << dex_file->dexStringByTypeIdx(handler.type_idx_) << ")";
jeffhaod1f0fde2011-09-08 17:25:33 -07003906 /* TODO: do we want to keep going? If we don't fail this we run
3907 * the risk of having a non-Throwable introduced at runtime.
3908 * However, that won't pass an instanceof test, so is essentially
3909 * harmless.
jeffhaobdb76512011-09-07 11:43:16 -07003910 */
3911 } else {
3912 if (common_super == NULL)
3913 common_super = klass;
3914 else
3915 common_super = FindCommonSuperclass(klass, common_super);
3916 }
3917 }
jeffhaoba5ebb92011-08-25 17:24:37 -07003918 }
3919
3920 handlers_ptr = iterator.GetData();
3921 }
3922
jeffhaobdb76512011-09-07 11:43:16 -07003923 if (common_super == NULL) {
3924 /* no catch blocks, or no catches with classes we can find */
3925 LOG(ERROR) << "VFY: unable to find exception handler at addr 0x" << std::hex
3926 << insn_idx << std::dec;
3927 *failure = VERIFY_ERROR_GENERIC;
3928 }
3929
3930 return common_super;
jeffhaoba5ebb92011-08-25 17:24:37 -07003931}
3932
jeffhaobdb76512011-09-07 11:43:16 -07003933DexVerifier::RegType DexVerifier::GetMethodReturnType(const DexFile* dex_file,
3934 const Method* method) {
3935 Class* klass = method->GetReturnType();
3936 if (klass->IsPrimitive())
3937 return PrimitiveTypeToRegType(klass->GetPrimitiveType());
3938 else
3939 return RegTypeFromClass(klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003940}
3941
jeffhaobdb76512011-09-07 11:43:16 -07003942Class* DexVerifier::GetClassFromRegister(const RegisterLine* register_line,
3943 uint32_t vsrc, VerifyError* failure) {
3944 /* get the element type of the array held in vsrc */
3945 RegType type = GetRegisterType(register_line, vsrc);
jeffhaoba5ebb92011-08-25 17:24:37 -07003946
jeffhaobdb76512011-09-07 11:43:16 -07003947 /* if "always zero", we allow it to fail at runtime */
3948 if (type == kRegTypeZero)
3949 return NULL;
3950
3951 if (!RegTypeIsReference(type)) {
3952 LOG(ERROR) << "VFY: tried to get class from non-ref register v" << vsrc
3953 << " (type=" << type << ")",
3954 *failure = VERIFY_ERROR_GENERIC;
3955 return NULL;
3956 }
3957 if (RegTypeIsUninitReference(type)) {
3958 LOG(ERROR) << "VFY: register " << vsrc << " holds uninitialized reference";
3959 *failure = VERIFY_ERROR_GENERIC;
3960 return NULL;
jeffhaoba5ebb92011-08-25 17:24:37 -07003961 }
3962
jeffhaobdb76512011-09-07 11:43:16 -07003963 return RegTypeInitializedReferenceToClass(type);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003964}
3965
jeffhaobdb76512011-09-07 11:43:16 -07003966DexVerifier::RegType DexVerifier::GetInvocationThis(
3967 const RegisterLine* register_line,
3968 const Instruction::DecodedInstruction* dec_insn, VerifyError* failure) {
3969 if (dec_insn->vA_ < 1) {
3970 LOG(ERROR) << "VFY: invoke lacks 'this'";
3971 *failure = VERIFY_ERROR_GENERIC;
3972 return kRegTypeUnknown;
jeffhaoba5ebb92011-08-25 17:24:37 -07003973 }
jeffhaobdb76512011-09-07 11:43:16 -07003974
3975 /* get the element type of the array held in vsrc */
3976 RegType this_type = GetRegisterType(register_line, dec_insn->vC_);
3977 if (!RegTypeIsReference(this_type)) {
3978 LOG(ERROR) << "VFY: tried to get class from non-ref register v"
3979 << dec_insn->vC_ << " (type=" << this_type << ")";
3980 *failure = VERIFY_ERROR_GENERIC;
3981 return kRegTypeUnknown;
3982 }
3983
3984 return this_type;
jeffhaoba5ebb92011-08-25 17:24:37 -07003985}
3986
jeffhaobdb76512011-09-07 11:43:16 -07003987void DexVerifier::SetRegisterType(RegisterLine* register_line, uint32_t vdst,
3988 RegType new_type) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07003989 RegType* insn_regs = register_line->reg_types_.get();
jeffhaoba5ebb92011-08-25 17:24:37 -07003990
jeffhaobdb76512011-09-07 11:43:16 -07003991 switch (new_type) {
3992 case kRegTypeUnknown:
3993 case kRegTypeBoolean:
3994 case kRegTypeOne:
3995 case kRegTypeConstByte:
3996 case kRegTypeConstPosByte:
3997 case kRegTypeConstShort:
3998 case kRegTypeConstPosShort:
3999 case kRegTypeConstChar:
4000 case kRegTypeConstInteger:
4001 case kRegTypeByte:
4002 case kRegTypePosByte:
4003 case kRegTypeShort:
4004 case kRegTypePosShort:
4005 case kRegTypeChar:
4006 case kRegTypeInteger:
4007 case kRegTypeFloat:
4008 case kRegTypeZero:
4009 case kRegTypeUninit:
4010 insn_regs[vdst] = new_type;
jeffhaoba5ebb92011-08-25 17:24:37 -07004011 break;
jeffhaobdb76512011-09-07 11:43:16 -07004012 case kRegTypeConstLo:
4013 case kRegTypeLongLo:
4014 case kRegTypeDoubleLo:
4015 insn_regs[vdst] = new_type;
4016 insn_regs[vdst + 1] = new_type + 1;
4017 break;
4018 case kRegTypeConstHi:
4019 case kRegTypeLongHi:
4020 case kRegTypeDoubleHi:
4021 /* should never set these explicitly */
4022 LOG(FATAL) << "BUG: explicit set of high register type";
4023 break;
4024
4025 default:
4026 /* can't switch for ref types, so we check explicitly */
4027 if (RegTypeIsReference(new_type)) {
4028 insn_regs[vdst] = new_type;
4029
4030 /*
4031 * In most circumstances we won't see a reference to a primitive
4032 * class here (e.g. "D"), since that would mean the object in the
jeffhaod1f0fde2011-09-08 17:25:33 -07004033 * register is actually a primitive type. It can happen as the
jeffhaobdb76512011-09-07 11:43:16 -07004034 * result of an assumed-successful check-cast instruction in
jeffhaod1f0fde2011-09-08 17:25:33 -07004035 * which the second argument refers to a primitive class. (In
jeffhaobdb76512011-09-07 11:43:16 -07004036 * practice, such an instruction will always throw an exception.)
4037 *
4038 * This is not an issue for instructions like const-class, where
4039 * the object in the register is a java.lang.Class instance.
4040 */
4041 break;
4042 }
4043 /* bad type - fall through */
4044
4045 case kRegTypeConflict: // should only be set during a merge
4046 LOG(FATAL) << "BUG: set register to unknown type " << new_type;
jeffhaoba5ebb92011-08-25 17:24:37 -07004047 break;
4048 }
4049
jeffhaobdb76512011-09-07 11:43:16 -07004050 /*
4051 * Clear the monitor entry bits for this register.
4052 */
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004053 if (register_line->monitor_entries_.get() != NULL)
jeffhaobdb76512011-09-07 11:43:16 -07004054 register_line->monitor_entries_[vdst] = 0;
4055}
4056
4057void DexVerifier::VerifyRegisterType(RegisterLine* register_line, uint32_t vsrc,
4058 RegType check_type, VerifyError* failure) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004059 const RegType* insn_regs = register_line->reg_types_.get();
jeffhaobdb76512011-09-07 11:43:16 -07004060 RegType src_type = insn_regs[vsrc];
4061
4062 switch (check_type) {
4063 case kRegTypeFloat:
4064 case kRegTypeBoolean:
4065 case kRegTypePosByte:
4066 case kRegTypeByte:
4067 case kRegTypePosShort:
4068 case kRegTypeShort:
4069 case kRegTypeChar:
4070 case kRegTypeInteger:
4071 if (!CanConvertTo1nr(src_type, check_type)) {
4072 LOG(ERROR) << "VFY: register1 v" << vsrc << " type " << src_type
4073 << ", wanted " << check_type;
4074 *failure = VERIFY_ERROR_GENERIC;
4075 }
4076 /* Update type if result is float */
4077 if (check_type == kRegTypeFloat) {
4078 SetRegisterType(register_line, vsrc, check_type);
4079 } else {
4080 /* Update const type to actual type after use */
4081 SetRegisterType(register_line, vsrc, ConstTypeToRegType(src_type));
4082 }
jeffhaoba5ebb92011-08-25 17:24:37 -07004083 break;
jeffhaobdb76512011-09-07 11:43:16 -07004084 case kRegTypeLongLo:
4085 case kRegTypeDoubleLo:
4086 if (insn_regs[vsrc + 1] != src_type + 1) {
4087 LOG(ERROR) << "VFY: register2 v" << vsrc << "-" << vsrc + 1
4088 << " values " << insn_regs[vsrc] << ","
4089 << insn_regs[vsrc + 1];
4090 *failure = VERIFY_ERROR_GENERIC;
4091 } else if (!CanConvertTo2(src_type, check_type)) {
4092 LOG(ERROR) << "VFY: register2 v" << vsrc << " type " << src_type
4093 << ", wanted " << check_type;
4094 *failure = VERIFY_ERROR_GENERIC;
4095 }
4096 /* Update type if source is from const */
4097 if (src_type == kRegTypeConstLo) {
4098 SetRegisterType(register_line, vsrc, check_type);
4099 }
jeffhaoba5ebb92011-08-25 17:24:37 -07004100 break;
jeffhaobdb76512011-09-07 11:43:16 -07004101 case kRegTypeConstLo:
4102 case kRegTypeConstHi:
4103 case kRegTypeLongHi:
4104 case kRegTypeDoubleHi:
4105 case kRegTypeZero:
4106 case kRegTypeOne:
4107 case kRegTypeUnknown:
4108 case kRegTypeConflict:
4109 /* should never be checking for these explicitly */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004110 DCHECK(false);
jeffhaobdb76512011-09-07 11:43:16 -07004111 *failure = VERIFY_ERROR_GENERIC;
4112 return;
4113 case kRegTypeUninit:
4114 default:
4115 /* make sure check_type is initialized reference */
4116 if (!RegTypeIsReference(check_type)) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004117 LOG(FATAL) << "VFY: unexpected check type " << check_type;
jeffhaobdb76512011-09-07 11:43:16 -07004118 *failure = VERIFY_ERROR_GENERIC;
4119 break;
4120 }
4121 if (RegTypeIsUninitReference(check_type)) {
4122 LOG(ERROR) << "VFY: uninitialized ref not expected as reg check";
4123 *failure = VERIFY_ERROR_GENERIC;
4124 break;
4125 }
4126 /* make sure src_type is initialized reference or always-NULL */
4127 if (!RegTypeIsReference(src_type)) {
4128 LOG(ERROR) << "VFY: register1 v" << vsrc << " type " << src_type
4129 << ", wanted ref";
4130 *failure = VERIFY_ERROR_GENERIC;
4131 break;
4132 }
4133 if (RegTypeIsUninitReference(src_type)) {
4134 LOG(ERROR) << "VFY: register1 v" << vsrc << " holds uninitialized ref";
4135 *failure = VERIFY_ERROR_GENERIC;
4136 break;
4137 }
4138 /* if the register isn't Zero, make sure it's an instance of check */
4139 if (src_type != kRegTypeZero) {
4140 Class* src_class = RegTypeInitializedReferenceToClass(src_type);
4141 Class* check_class = RegTypeInitializedReferenceToClass(check_type);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004142 DCHECK(src_class != NULL);
4143 DCHECK(check_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07004144
jeffhaoe0cfb6f2011-09-22 16:42:56 -07004145 if (check_class->IsInterface()) {
4146 /*
4147 * All objects implement all interfaces as far as the verifier is
4148 * concerned. The runtime has to sort it out. See coments above
4149 * FindCommonSuperclass.
4150 */
4151 } else {
4152 if (!check_class->IsAssignableFrom(src_class)) {
4153 LOG(ERROR) << "VFY: " << src_class->GetDescriptor()->ToModifiedUtf8()
4154 << " is not instance of "
4155 << check_class->GetDescriptor()->ToModifiedUtf8();
4156 *failure = VERIFY_ERROR_GENERIC;
4157 }
jeffhaobdb76512011-09-07 11:43:16 -07004158 }
4159 }
jeffhaoba5ebb92011-08-25 17:24:37 -07004160 break;
4161 }
jeffhaobdb76512011-09-07 11:43:16 -07004162}
jeffhaoba5ebb92011-08-25 17:24:37 -07004163
jeffhaobdb76512011-09-07 11:43:16 -07004164void DexVerifier::SetResultRegisterType(RegisterLine* register_line,
4165 const int insn_reg_count, RegType new_type) {
4166 SetRegisterType(register_line, RESULT_REGISTER(insn_reg_count), new_type);
4167}
4168
4169void DexVerifier::MarkRefsAsInitialized(RegisterLine* register_line,
4170 int insn_reg_count, UninitInstanceMap* uninit_map, RegType uninit_type,
4171 VerifyError* failure) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004172 RegType* insn_regs = register_line->reg_types_.get();
jeffhaobdb76512011-09-07 11:43:16 -07004173 Class* klass = GetUninitInstance(uninit_map,
4174 RegTypeToUninitIndex(uninit_type));
4175
4176 if (klass == NULL) {
4177 LOG(ERROR) << "VFY: unable to find type=" << std::hex << uninit_type
4178 << std::dec << " (idx=" << RegTypeToUninitIndex(uninit_type)
4179 << ")";
4180 *failure = VERIFY_ERROR_GENERIC;
4181 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07004182 }
4183
jeffhaobdb76512011-09-07 11:43:16 -07004184 RegType init_type = RegTypeFromClass(klass);
4185 int changed = 0;
4186 for (int i = 0; i < insn_reg_count; i++) {
4187 if (insn_regs[i] == uninit_type) {
4188 insn_regs[i] = init_type;
4189 changed++;
4190 }
4191 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004192 DCHECK_GT(changed, 0);
jeffhaobdb76512011-09-07 11:43:16 -07004193
4194 return;
4195}
4196
4197void DexVerifier::MarkUninitRefsAsInvalid(RegisterLine* register_line,
4198 int insn_reg_count, UninitInstanceMap* uninit_map, RegType uninit_type) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004199 RegType* insn_regs = register_line->reg_types_.get();
jeffhaobdb76512011-09-07 11:43:16 -07004200
4201 for (int i = 0; i < insn_reg_count; i++) {
4202 if (insn_regs[i] == uninit_type) {
4203 insn_regs[i] = kRegTypeConflict;
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004204 if (register_line->monitor_entries_.get() != NULL)
jeffhaobdb76512011-09-07 11:43:16 -07004205 register_line->monitor_entries_[i] = 0;
4206 }
4207 }
4208}
4209
4210void DexVerifier::CopyRegister1(RegisterLine* register_line, uint32_t vdst,
4211 uint32_t vsrc, TypeCategory cat, VerifyError* failure) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004212 DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07004213 RegType type = GetRegisterType(register_line, vsrc);
4214 CheckTypeCategory(type, cat, failure);
4215 if (*failure != VERIFY_ERROR_NONE) {
4216 LOG(ERROR) << "VFY: copy1 v" << vdst << "<-v" << vsrc << " type=" << type
4217 << " cat=" << (int) cat;
4218 } else {
4219 SetRegisterType(register_line, vdst, type);
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004220 if (cat == kTypeCategoryRef && register_line->monitor_entries_.get() != NULL) {
jeffhaobdb76512011-09-07 11:43:16 -07004221 register_line->monitor_entries_[vdst] =
4222 register_line->monitor_entries_[vsrc];
4223 }
4224 }
4225}
4226
4227void DexVerifier::CopyRegister2(RegisterLine* register_line, uint32_t vdst,
4228 uint32_t vsrc, VerifyError* failure) {
4229 RegType type_l = GetRegisterType(register_line, vsrc);
4230 RegType type_h = GetRegisterType(register_line, vsrc + 1);
4231
4232 CheckTypeCategory(type_l, kTypeCategory2, failure);
4233 CheckWidePair(type_l, type_h, failure);
4234 if (*failure != VERIFY_ERROR_NONE) {
4235 LOG(ERROR) << "VFY: copy2 v" << vdst << "<-v" << vsrc << " type=" << type_l
4236 << "/" << type_h;
4237 } else {
4238 SetRegisterType(register_line, vdst, type_l);
4239 }
4240}
4241
4242void DexVerifier::CopyResultRegister1(RegisterLine* register_line,
4243 const int insn_reg_count, uint32_t vdst, TypeCategory cat,
4244 VerifyError* failure) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004245 DCHECK_LT(vdst, static_cast<uint32_t>(insn_reg_count));
jeffhaobdb76512011-09-07 11:43:16 -07004246
4247 uint32_t vsrc = RESULT_REGISTER(insn_reg_count);
4248 RegType type = GetRegisterType(register_line, vsrc);
4249 CheckTypeCategory(type, cat, failure);
4250 if (*failure != VERIFY_ERROR_NONE) {
4251 LOG(ERROR) << "VFY: copyRes1 v" << vdst << "<-v" << vsrc << " cat="
4252 << (int) cat << " type=" << type;
4253 } else {
4254 SetRegisterType(register_line, vdst, type);
4255 SetRegisterType(register_line, vsrc, kRegTypeUnknown);
4256 }
4257}
4258
4259/*
jeffhaod1f0fde2011-09-08 17:25:33 -07004260 * Implement "move-result-wide". Copy the category-2 value from the result
jeffhaobdb76512011-09-07 11:43:16 -07004261 * register to another register, and reset the result register.
4262 */
4263void DexVerifier::CopyResultRegister2(RegisterLine* register_line,
4264 const int insn_reg_count, uint32_t vdst, VerifyError* failure) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004265 DCHECK_LT(vdst, static_cast<uint32_t>(insn_reg_count));
jeffhaobdb76512011-09-07 11:43:16 -07004266
4267 uint32_t vsrc = RESULT_REGISTER(insn_reg_count);
4268 RegType type_l = GetRegisterType(register_line, vsrc);
4269 RegType type_h = GetRegisterType(register_line, vsrc + 1);
4270 CheckTypeCategory(type_l, kTypeCategory2, failure);
4271 CheckWidePair(type_l, type_h, failure);
4272 if (*failure != VERIFY_ERROR_NONE) {
4273 LOG(ERROR) << "VFY: copyRes2 v" << vdst << "<-v" << vsrc << " type="
4274 << type_l << "/" << type_h;
4275 } else {
4276 SetRegisterType(register_line, vdst, type_l);
4277 SetRegisterType(register_line, vsrc, kRegTypeUnknown);
4278 SetRegisterType(register_line, vsrc + 1, kRegTypeUnknown);
4279 }
4280}
4281
4282int DexVerifier::GetClassDepth(Class* klass) {
4283 int depth = 0;
4284 while (klass->GetSuperClass() != NULL) {
4285 klass = klass->GetSuperClass();
4286 depth++;
4287 }
4288 return depth;
4289}
4290
4291Class* DexVerifier::DigForSuperclass(Class* c1, Class* c2) {
4292 int depth1, depth2;
4293
4294 depth1 = GetClassDepth(c1);
4295 depth2 = GetClassDepth(c2);
4296
4297 /* pull the deepest one up */
4298 if (depth1 > depth2) {
4299 while (depth1 > depth2) {
4300 c1 = c1->GetSuperClass();
4301 depth1--;
4302 }
4303 } else {
4304 while (depth2 > depth1) {
4305 c2 = c2->GetSuperClass();
4306 depth2--;
4307 }
jeffhaoba5ebb92011-08-25 17:24:37 -07004308 }
4309
jeffhaobdb76512011-09-07 11:43:16 -07004310 /* walk up in lock-step */
4311 while (c1 != c2) {
4312 c1 = c1->GetSuperClass();
4313 c2 = c2->GetSuperClass();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004314 DCHECK(c1 != NULL);
4315 DCHECK(c2 != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07004316 }
4317
4318 return c1;
4319}
4320
4321Class* DexVerifier::FindCommonArraySuperclass(Class* c1, Class* c2) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004322 DCHECK(c1->IsArrayClass());
4323 DCHECK(c2->IsArrayClass());
4324 Class* e1 = c1->GetComponentType();
4325 Class* e2 = c2->GetComponentType();
4326 if (e1->IsPrimitive() || e2->IsPrimitive()) {
4327 return c1->GetSuperClass(); // == java.lang.Object
jeffhaobdb76512011-09-07 11:43:16 -07004328 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004329 Class* common_elem = FindCommonSuperclass(c1->GetComponentType(), c2->GetComponentType());
4330 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
4331 const ClassLoader* class_loader = c1->GetClassLoader();
4332 std::string descriptor = "[" + common_elem->GetDescriptor()->ToModifiedUtf8();
4333 Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
4334 DCHECK(array_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07004335 return array_class;
4336}
4337
4338Class* DexVerifier::FindCommonSuperclass(Class* c1, Class* c2) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004339 DCHECK(!c1->IsPrimitive()) << PrettyClass(c1);
4340 DCHECK(!c2->IsPrimitive()) << PrettyClass(c2);
jeffhaobdb76512011-09-07 11:43:16 -07004341
4342 if (c1 == c2)
4343 return c1;
4344
jeffhao5dbddee2011-09-07 16:38:26 -07004345 if (c1->IsInterface() && c1->IsAssignableFrom(c2)) {
jeffhaobdb76512011-09-07 11:43:16 -07004346 return c1;
4347 }
jeffhao5dbddee2011-09-07 16:38:26 -07004348 if (c2->IsInterface() && c2->IsAssignableFrom(c1)) {
jeffhaobdb76512011-09-07 11:43:16 -07004349 return c2;
4350 }
4351 if (c1->IsArrayClass() && c2->IsArrayClass()) {
4352 return FindCommonArraySuperclass(c1, c2);
4353 }
4354
4355 return DigForSuperclass(c1, c2);
4356}
4357
jeffhao98eacac2011-09-14 16:11:53 -07004358Class* DexVerifier::ResolveClassAndCheckAccess(const DexFile* dex_file,
jeffhaob4df5142011-09-19 20:25:32 -07004359 uint32_t class_idx, const Class* referrer, VerifyError* failure) {
jeffhao98eacac2011-09-14 16:11:53 -07004360 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
4361 Class* res_class = class_linker->ResolveType(*dex_file, class_idx, referrer);
4362
4363 if (res_class == NULL) {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07004364 //*failure = VERIFY_ERROR_NO_CLASS;
4365 LOG(ERROR) << "VFY: can't find class with index 0x" << std::hex << class_idx << std::dec;
jeffhao98eacac2011-09-14 16:11:53 -07004366 return NULL;
4367 }
4368
jeffhaoe0cfb6f2011-09-22 16:42:56 -07004369#if 0
jeffhao98eacac2011-09-14 16:11:53 -07004370 /* Check if access is allowed. */
4371 if (!referrer->CanAccess(res_class)) {
4372 LOG(ERROR) << "VFY: illegal class access: "
4373 << referrer->GetDescriptor()->ToModifiedUtf8() << " -> "
4374 << res_class->GetDescriptor()->ToModifiedUtf8();
4375 *failure = VERIFY_ERROR_ACCESS_CLASS;
4376 return NULL;
4377 }
jeffhaoe0cfb6f2011-09-22 16:42:56 -07004378#endif
jeffhao98eacac2011-09-14 16:11:53 -07004379
4380 return res_class;
4381}
4382
jeffhaob4df5142011-09-19 20:25:32 -07004383Method* DexVerifier::ResolveMethodAndCheckAccess(const DexFile* dex_file,
4384 uint32_t method_idx, const Class* referrer, VerifyError* failure,
4385 bool is_direct) {
4386 DexCache* dex_cache = referrer->GetDexCache();
4387 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
4388
4389 if (res_method == NULL) {
4390 const DexFile::MethodId& method_id = dex_file->GetMethodId(method_idx);
4391 Class* klass = ResolveClassAndCheckAccess(dex_file, method_id.class_idx_, referrer, failure);
4392 if (klass == NULL) {
4393 DCHECK(*failure != VERIFY_ERROR_NONE);
4394 return NULL;
4395 }
4396
4397 const char* name = dex_file->dexStringById(method_id.name_idx_);
4398 std::string signature(dex_file->CreateMethodDescriptor(method_id.proto_idx_, NULL));
4399 if (is_direct) {
4400 res_method = klass->FindDirectMethod(name, signature);
4401 } else if (klass->IsInterface()) {
4402 res_method = klass->FindInterfaceMethod(name, signature);
4403 } else {
4404 res_method = klass->FindVirtualMethod(name, signature);
4405 }
4406 if (res_method != NULL) {
4407 dex_cache->SetResolvedMethod(method_idx, res_method);
4408 } else {
4409 LOG(ERROR) << "VFY: couldn't find method "
4410 << klass->GetDescriptor()->ToModifiedUtf8() << "." << name
4411 << " " << signature;
4412 *failure = VERIFY_ERROR_NO_METHOD;
4413 return NULL;
4414 }
4415 }
4416
jeffhaoe0cfb6f2011-09-22 16:42:56 -07004417#if 0
jeffhaob4df5142011-09-19 20:25:32 -07004418 /* Check if access is allowed. */
4419 if (!referrer->CanAccess(res_method->GetDeclaringClass())) {
4420 LOG(ERROR) << "VFY: illegal method access (call "
4421 << res_method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
jeffhaoe0cfb6f2011-09-22 16:42:56 -07004422 << "." << res_method->GetName()->ToModifiedUtf8() << " "
4423 << res_method->GetSignature()->ToModifiedUtf8() << " from "
jeffhaob4df5142011-09-19 20:25:32 -07004424 << referrer->GetDescriptor()->ToModifiedUtf8() << ")";
4425 *failure = VERIFY_ERROR_ACCESS_METHOD;
4426 return NULL;
4427 }
jeffhaoe0cfb6f2011-09-22 16:42:56 -07004428#endif
jeffhaob4df5142011-09-19 20:25:32 -07004429
4430 return res_method;
4431}
4432
4433Field* DexVerifier::ResolveFieldAndCheckAccess(const DexFile* dex_file,
4434 uint32_t field_idx, const Class* referrer, VerifyError* failure,
4435 bool is_static) {
4436 DexCache* dex_cache = referrer->GetDexCache();
4437 Field* res_field = dex_cache->GetResolvedField(field_idx);
4438
4439 if (res_field == NULL) {
4440 const DexFile::FieldId& field_id = dex_file->GetFieldId(field_idx);
4441 Class* klass = ResolveClassAndCheckAccess(dex_file, field_id.class_idx_, referrer, failure);
4442 if (klass == NULL) {
4443 DCHECK(*failure != VERIFY_ERROR_NONE);
4444 return NULL;
4445 }
4446
4447 Class* field_type = ResolveClassAndCheckAccess(dex_file, field_id.type_idx_, referrer, failure);
4448 if (field_type == NULL) {
4449 DCHECK(*failure != VERIFY_ERROR_NONE);
4450 return NULL;
4451 }
4452
4453 const char* name = dex_file->dexStringById(field_id.name_idx_);
4454 if (is_static) {
4455 res_field = klass->FindStaticField(name, field_type);
4456 } else {
4457 res_field = klass->FindInstanceField(name, field_type);
4458 }
4459 if (res_field != NULL) {
Elliott Hughes4a2b4172011-09-20 17:08:25 -07004460 dex_cache->SetResolvedField(field_idx, res_field);
jeffhaob4df5142011-09-19 20:25:32 -07004461 } else {
4462 LOG(ERROR) << "VFY: couldn't find field "
4463 << klass->GetDescriptor()->ToModifiedUtf8() << "." << name;
4464 *failure = VERIFY_ERROR_NO_FIELD;
4465 return NULL;
4466 }
4467 }
4468
4469 /* Check if access is allowed. */
4470 if (!referrer->CanAccess(res_field->GetDeclaringClass())) {
4471 LOG(ERROR) << "VFY: access denied from "
4472 << referrer->GetDescriptor()->ToModifiedUtf8() << " to field "
4473 << res_field->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
4474 << "." << res_field->GetName()->ToModifiedUtf8();
4475 *failure = VERIFY_ERROR_ACCESS_FIELD;
4476 return NULL;
4477 }
4478
4479 return res_field;
4480}
4481
jeffhaobdb76512011-09-07 11:43:16 -07004482DexVerifier::RegType DexVerifier::MergeTypes(RegType type1, RegType type2,
4483 bool* changed) {
4484 RegType result;
4485
jeffhao98eacac2011-09-14 16:11:53 -07004486 /* Check for trivial case so we don't have to hit memory. */
jeffhaobdb76512011-09-07 11:43:16 -07004487 if (type1 == type2)
4488 return type1;
4489
4490 /*
4491 * Use the table if we can, and reject any attempts to merge something
4492 * from the table with a reference type.
4493 *
4494 * Uninitialized references are composed of the enum ORed with an
jeffhaod1f0fde2011-09-08 17:25:33 -07004495 * index value. The uninitialized table entry at index zero *will*
4496 * show up as a simple kRegTypeUninit value. Since this cannot be
jeffhaobdb76512011-09-07 11:43:16 -07004497 * merged with anything but itself, the rules do the right thing.
4498 */
4499 if (type1 < kRegTypeMAX) {
4500 if (type2 < kRegTypeMAX) {
4501 result = merge_table_[type1][type2];
4502 } else {
4503 /* simple + reference == conflict, usually */
4504 if (type1 == kRegTypeZero)
4505 result = type2;
4506 else
4507 result = kRegTypeConflict;
4508 }
4509 } else {
4510 if (type2 < kRegTypeMAX) {
4511 /* reference + simple == conflict, usually */
4512 if (type2 == kRegTypeZero)
4513 result = type1;
4514 else
4515 result = kRegTypeConflict;
4516 } else {
4517 /* merging two references */
4518 if (RegTypeIsUninitReference(type1) ||
4519 RegTypeIsUninitReference(type2))
4520 {
4521 /* can't merge uninit with anything but self */
4522 result = kRegTypeConflict;
4523 } else {
4524 Class* klass1 = RegTypeInitializedReferenceToClass(type1);
4525 Class* klass2 = RegTypeInitializedReferenceToClass(type2);
4526 Class* merged_class = FindCommonSuperclass(klass1, klass2);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004527 DCHECK(merged_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07004528 result = RegTypeFromClass(merged_class);
4529 }
4530 }
4531 }
4532
4533 if (result != type1)
4534 *changed = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07004535 return result;
jeffhaobdb76512011-09-07 11:43:16 -07004536}
4537
4538DexVerifier::MonitorEntries DexVerifier::MergeMonitorEntries(
4539 MonitorEntries ents1, MonitorEntries ents2, bool* changed) {
4540 MonitorEntries result = ents1 & ents2;
4541 if (result != ents1)
4542 *changed = true;
4543 return result;
4544}
4545
4546bool DexVerifier::UpdateRegisters(InsnFlags* insn_flags,
4547 RegisterTable* reg_table, int next_insn, const RegisterLine* work_line) {
4548 const size_t insn_reg_count_plus = reg_table->insn_reg_count_plus_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004549 DCHECK(work_line != NULL);
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004550 const RegType* work_regs = work_line->reg_types_.get();
jeffhaobdb76512011-09-07 11:43:16 -07004551
4552 if (!InsnIsVisitedOrChanged(insn_flags, next_insn)) {
4553 /*
4554 * We haven't processed this instruction before, and we haven't
jeffhaod1f0fde2011-09-08 17:25:33 -07004555 * touched the registers here, so there's nothing to "merge". Copy
4556 * the registers over and mark it as changed. (This is the only
jeffhaobdb76512011-09-07 11:43:16 -07004557 * way a register can transition out of "unknown", so this is not
4558 * just an optimization.)
4559 */
4560 CopyLineToTable(reg_table, next_insn, work_line);
4561 InsnSetChanged(insn_flags, next_insn, true);
4562 } else {
4563 /* Merge registers, set Changed only if different */
4564 RegisterLine* target_line = GetRegisterLine(reg_table, next_insn);
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004565 RegType* target_regs = target_line->reg_types_.get();
4566 MonitorEntries* work_mon_ents = work_line->monitor_entries_.get();
4567 MonitorEntries* target_mon_ents = target_line->monitor_entries_.get();
jeffhaobdb76512011-09-07 11:43:16 -07004568 bool changed = false;
4569 unsigned int idx;
4570
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004571 DCHECK(target_regs != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07004572 if (target_mon_ents != NULL) {
4573 /* Monitor stacks must be identical. */
4574 if (target_line->monitor_stack_top_ != work_line->monitor_stack_top_) {
4575 LOG(ERROR) << "VFY: mismatched stack depth "
4576 << target_line->monitor_stack_top_ << " vs. "
4577 << work_line->monitor_stack_top_ << " at 0x"
4578 << std::hex << next_insn << std::dec;
4579 return false;
4580 }
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004581 if (memcmp(target_line->monitor_stack_.get(), work_line->monitor_stack_.get(),
jeffhaobdb76512011-09-07 11:43:16 -07004582 target_line->monitor_stack_top_ * sizeof(uint32_t)) != 0) {
4583 LOG(ERROR) << "VFY: mismatched monitor stacks at 0x" << std::hex
4584 << next_insn << std::dec;
4585 return false;
4586 }
4587 }
4588
4589 for (idx = 0; idx < insn_reg_count_plus; idx++) {
4590 target_regs[idx] = MergeTypes(target_regs[idx], work_regs[idx], &changed);
4591
4592 if (target_mon_ents != NULL) {
4593 target_mon_ents[idx] = MergeMonitorEntries(target_mon_ents[idx],
4594 work_mon_ents[idx], &changed);
4595 }
4596 }
4597
4598 if (changed) {
4599 InsnSetChanged(insn_flags, next_insn, true);
4600 }
4601 }
4602
4603 return true;
4604}
4605
4606bool DexVerifier::CanConvertTo1nr(RegType src_type, RegType check_type) {
4607 static const char conv_tab[kRegType1nrEND - kRegType1nrSTART + 1]
4608 [kRegType1nrEND - kRegType1nrSTART + 1] =
4609 {
4610 /* chk: 0 1 Z y Y h H c i b B s S C I F */
4611 { /*0*/ 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
4612 { /*1*/ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
4613 { /*Z*/ 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
4614 { /*y*/ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
4615 { /*Y*/ 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1 },
4616 { /*h*/ 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1 },
4617 { /*H*/ 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1 },
4618 { /*c*/ 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1 },
4619 { /*i*/ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1 },
4620 { /*b*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0 },
4621 { /*B*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0 },
4622 { /*s*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0 },
4623 { /*S*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0 },
4624 { /*C*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 },
4625 { /*I*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
4626 { /*F*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
4627 };
4628
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004629 DCHECK(check_type >= kRegType1nrSTART);
4630 DCHECK(check_type <= kRegType1nrEND);
jeffhaobdb76512011-09-07 11:43:16 -07004631
4632 if (src_type >= kRegType1nrSTART && src_type <= kRegType1nrEND)
4633 return (bool) conv_tab[src_type - kRegType1nrSTART]
4634 [check_type - kRegType1nrSTART];
4635
4636 return false;
4637}
4638
4639bool DexVerifier::CanConvertTo2(RegType src_type, RegType check_type) {
4640 return ((src_type == kRegTypeConstLo || src_type == check_type) &&
4641 (check_type == kRegTypeLongLo || check_type == kRegTypeDoubleLo));
4642}
4643
4644DexVerifier::RegType DexVerifier::PrimitiveTypeToRegType(
4645 Class::PrimitiveType prim_type) {
4646 switch (prim_type) {
4647 case Class::kPrimBoolean: return kRegTypeBoolean;
4648 case Class::kPrimByte: return kRegTypeByte;
4649 case Class::kPrimShort: return kRegTypeShort;
4650 case Class::kPrimChar: return kRegTypeChar;
4651 case Class::kPrimInt: return kRegTypeInteger;
4652 case Class::kPrimLong: return kRegTypeLongLo;
4653 case Class::kPrimFloat: return kRegTypeFloat;
4654 case Class::kPrimDouble: return kRegTypeDoubleLo;
4655 case Class::kPrimVoid:
4656 default: {
4657 return kRegTypeUnknown;
4658 }
4659 }
4660}
4661
4662DexVerifier::RegType DexVerifier::ConstTypeToRegType(RegType const_type) {
4663 switch (const_type) {
4664 case kRegTypeConstPosByte: return kRegTypePosByte;
4665 case kRegTypeConstByte: return kRegTypeByte;
4666 case kRegTypeConstPosShort: return kRegTypePosShort;
4667 case kRegTypeConstShort: return kRegTypeShort;
4668 case kRegTypeConstChar: return kRegTypeChar;
4669 case kRegTypeConstInteger: return kRegTypeInteger;
4670 default: {
4671 return const_type;
4672 }
4673 }
4674}
4675
4676char DexVerifier::DetermineCat1Const(int32_t value) {
4677 if (value < -32768)
4678 return kRegTypeConstInteger;
4679 else if (value < -128)
4680 return kRegTypeConstShort;
4681 else if (value < 0)
4682 return kRegTypeConstByte;
4683 else if (value == 0)
4684 return kRegTypeZero;
4685 else if (value == 1)
4686 return kRegTypeOne;
4687 else if (value < 128)
4688 return kRegTypeConstPosByte;
4689 else if (value < 32768)
4690 return kRegTypeConstPosShort;
4691 else if (value < 65536)
4692 return kRegTypeConstChar;
4693 else
4694 return kRegTypeConstInteger;
4695}
4696
4697void DexVerifier::CheckFinalFieldAccess(const Method* method,
4698 const Field* field, VerifyError* failure) {
4699 if (!field->IsFinal())
4700 return;
4701
4702 /* make sure we're in the same class */
4703 if (method->GetDeclaringClass() != field->GetDeclaringClass()) {
4704 LOG(ERROR) << "VFY: can't modify final field "
4705 << field->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
4706 << "." << field->GetName()->ToModifiedUtf8();
4707 *failure = VERIFY_ERROR_ACCESS_FIELD;
4708 return;
4709 }
4710}
4711
4712void DexVerifier::CheckArrayIndexType(const Method* method, RegType reg_type,
4713 VerifyError* failure) {
4714 if (*failure == VERIFY_ERROR_NONE) {
4715 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07004716 * The 1nr types are interchangeable at this level. We could
jeffhaobdb76512011-09-07 11:43:16 -07004717 * do something special if we can definitively identify it as a
4718 * float, but there's no real value in doing so.
4719 */
4720 CheckTypeCategory(reg_type, kTypeCategory1nr, failure);
4721 if (*failure != VERIFY_ERROR_NONE) {
4722 LOG(ERROR) << "Invalid reg type for array index (" << reg_type << ")";
4723 }
4724 }
4725}
4726
4727bool DexVerifier::CheckConstructorReturn(const Method* method,
4728 const RegisterLine* register_line, const int insn_reg_count) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004729 const RegType* insn_regs = register_line->reg_types_.get();
jeffhaobdb76512011-09-07 11:43:16 -07004730
4731 if (!IsInitMethod(method))
4732 return true;
4733
4734 RegType uninit_this = RegTypeFromUninitIndex(kUninitThisArgSlot);
4735
4736 for (int i = 0; i < insn_reg_count; i++) {
4737 if (insn_regs[i] == uninit_this) {
4738 LOG(ERROR) << "VFY: <init> returning without calling superclass init";
4739 return false;
4740 }
4741 }
4742 return true;
4743}
4744
4745bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
4746 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
4747 LOG(ERROR) << "VFY: invalid use of move-exception";
4748 return false;
4749 }
4750 return true;
4751}
4752
4753void DexVerifier::CheckTypeCategory(RegType type, TypeCategory cat,
4754 VerifyError* failure) {
4755 switch (cat) {
4756 case kTypeCategory1nr:
4757 switch (type) {
4758 case kRegTypeZero:
4759 case kRegTypeOne:
4760 case kRegTypeBoolean:
4761 case kRegTypeConstPosByte:
4762 case kRegTypeConstByte:
4763 case kRegTypeConstPosShort:
4764 case kRegTypeConstShort:
4765 case kRegTypeConstChar:
4766 case kRegTypeConstInteger:
4767 case kRegTypePosByte:
4768 case kRegTypeByte:
4769 case kRegTypePosShort:
4770 case kRegTypeShort:
4771 case kRegTypeChar:
4772 case kRegTypeInteger:
4773 case kRegTypeFloat:
4774 break;
4775 default:
4776 *failure = VERIFY_ERROR_GENERIC;
4777 break;
4778 }
4779 break;
4780 case kTypeCategory2:
4781 switch (type) {
4782 case kRegTypeConstLo:
4783 case kRegTypeLongLo:
4784 case kRegTypeDoubleLo:
4785 break;
4786 default:
4787 *failure = VERIFY_ERROR_GENERIC;
4788 break;
4789 }
4790 break;
4791 case kTypeCategoryRef:
4792 if (type != kRegTypeZero && !RegTypeIsReference(type))
4793 *failure = VERIFY_ERROR_GENERIC;
4794 break;
4795 default:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004796 DCHECK(false);
jeffhaobdb76512011-09-07 11:43:16 -07004797 *failure = VERIFY_ERROR_GENERIC;
4798 break;
4799 }
4800}
4801
4802void DexVerifier::CheckWidePair(RegType type_l, RegType type_h,
4803 VerifyError* failure) {
4804 if ((type_h != type_l + 1))
4805 *failure = VERIFY_ERROR_GENERIC;
4806}
4807
4808void DexVerifier::CheckUnop(RegisterLine* register_line,
4809 Instruction::DecodedInstruction* dec_insn, RegType dst_type,
4810 RegType src_type, VerifyError* failure) {
4811 VerifyRegisterType(register_line, dec_insn->vB_, src_type, failure);
4812 SetRegisterType(register_line, dec_insn->vA_, dst_type);
4813}
4814
4815bool DexVerifier::UpcastBooleanOp(RegisterLine* register_line, uint32_t reg1,
4816 uint32_t reg2) {
4817 RegType type1, type2;
4818
4819 type1 = GetRegisterType(register_line, reg1);
4820 type2 = GetRegisterType(register_line, reg2);
4821
4822 if ((type1 == kRegTypeBoolean || type1 == kRegTypeZero || type1 == kRegTypeOne) &&
4823 (type2 == kRegTypeBoolean || type2 == kRegTypeZero || type2 == kRegTypeOne)) {
4824 return true;
4825 }
4826 return false;
4827}
4828
4829void DexVerifier::CheckLitop(RegisterLine* register_line,
4830 Instruction::DecodedInstruction* dec_insn, RegType dst_type,
4831 RegType src_type, bool check_boolean_op, VerifyError* failure) {
4832 VerifyRegisterType(register_line, dec_insn->vB_, src_type, failure);
4833
4834 if ((*failure == VERIFY_ERROR_NONE) && check_boolean_op) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004835 DCHECK(dst_type == kRegTypeInteger);
jeffhaobdb76512011-09-07 11:43:16 -07004836
4837 /* check vB with the call, then check the constant manually */
4838 if (UpcastBooleanOp(register_line, dec_insn->vB_, dec_insn->vB_)
4839 && (dec_insn->vC_ == 0 || dec_insn->vC_ == 1)) {
4840 dst_type = kRegTypeBoolean;
4841 }
4842 }
4843
4844 SetRegisterType(register_line, dec_insn->vA_, dst_type);
4845}
4846
4847void DexVerifier::CheckBinop(RegisterLine* register_line,
4848 Instruction::DecodedInstruction* dec_insn, RegType dst_type,
4849 RegType src_type1, RegType src_type2, bool check_boolean_op,
4850 VerifyError* failure) {
4851 VerifyRegisterType(register_line, dec_insn->vB_, src_type1, failure);
4852 VerifyRegisterType(register_line, dec_insn->vC_, src_type2, failure);
4853
4854 if ((*failure == VERIFY_ERROR_NONE) && check_boolean_op) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004855 DCHECK(dst_type == kRegTypeInteger);
jeffhaobdb76512011-09-07 11:43:16 -07004856 if (UpcastBooleanOp(register_line, dec_insn->vB_, dec_insn->vC_))
4857 dst_type = kRegTypeBoolean;
4858 }
4859
4860 SetRegisterType(register_line, dec_insn->vA_, dst_type);
4861}
4862
4863void DexVerifier::CheckBinop2addr(RegisterLine* register_line,
4864 Instruction::DecodedInstruction* dec_insn, RegType dst_type,
4865 RegType src_type1, RegType src_type2, bool check_boolean_op,
4866 VerifyError* failure) {
4867 VerifyRegisterType(register_line, dec_insn->vA_, src_type1, failure);
4868 VerifyRegisterType(register_line, dec_insn->vB_, src_type2, failure);
4869
4870 if ((*failure == VERIFY_ERROR_NONE) && check_boolean_op) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004871 DCHECK(dst_type == kRegTypeInteger);
jeffhaobdb76512011-09-07 11:43:16 -07004872 if (UpcastBooleanOp(register_line, dec_insn->vA_, dec_insn->vB_))
4873 dst_type = kRegTypeBoolean;
4874 }
4875
4876 SetRegisterType(register_line, dec_insn->vA_, dst_type);
4877}
4878
4879DexVerifier::RegType DexVerifier::AdjustForRightShift(
4880 RegisterLine* register_line, int reg, unsigned int shift_count,
jeffhaob4df5142011-09-19 20:25:32 -07004881 bool is_unsigned_shift) {
jeffhaobdb76512011-09-07 11:43:16 -07004882 RegType src_type = GetRegisterType(register_line, reg);
4883 RegType new_type;
4884
4885 /* convert const derived types to their actual types */
4886 src_type = ConstTypeToRegType(src_type);
4887
4888 /* no-op */
4889 if (shift_count == 0)
4890 return src_type;
4891
4892 /* safe defaults */
4893 if (is_unsigned_shift)
4894 new_type = kRegTypeInteger;
4895 else
4896 new_type = src_type;
4897
4898 if (shift_count >= 32) {
4899 LOG(ERROR) << "Got unexpectedly large shift count " << shift_count;
4900 /* fail? */
4901 return new_type;
4902 }
4903
4904 switch (src_type) {
4905 case kRegTypeInteger: /* 32-bit signed value */
4906 if (is_unsigned_shift) {
4907 if (shift_count > 24)
4908 new_type = kRegTypePosByte;
4909 else if (shift_count >= 16)
4910 new_type = kRegTypeChar;
4911 } else {
4912 if (shift_count >= 24)
4913 new_type = kRegTypeByte;
4914 else if (shift_count >= 16)
4915 new_type = kRegTypeShort;
4916 }
4917 break;
4918 case kRegTypeShort: /* 16-bit signed value */
4919 if (is_unsigned_shift) {
4920 /* default (kRegTypeInteger) is correct */
4921 } else {
4922 if (shift_count >= 8)
4923 new_type = kRegTypeByte;
4924 }
4925 break;
4926 case kRegTypePosShort: /* 15-bit unsigned value */
4927 if (shift_count >= 8)
4928 new_type = kRegTypePosByte;
4929 break;
4930 case kRegTypeChar: /* 16-bit unsigned value */
4931 if (shift_count > 8)
4932 new_type = kRegTypePosByte;
4933 break;
4934 case kRegTypeByte: /* 8-bit signed value */
4935 /* defaults (u=kRegTypeInteger / s=src_type) are correct */
4936 break;
4937 case kRegTypePosByte: /* 7-bit unsigned value */
4938 /* always use new_type=src_type */
4939 new_type = src_type;
4940 break;
4941 case kRegTypeZero: /* 1-bit unsigned value */
4942 case kRegTypeOne:
4943 case kRegTypeBoolean:
4944 /* unnecessary? */
4945 new_type = kRegTypeZero;
4946 break;
4947 default:
4948 /* long, double, references; shouldn't be here! */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004949 DCHECK(false);
jeffhaobdb76512011-09-07 11:43:16 -07004950 break;
4951 }
4952
4953 return new_type;
4954}
4955
4956void DexVerifier::VerifyFilledNewArrayRegs(const Method* method,
4957 RegisterLine* register_line,
4958 const Instruction::DecodedInstruction* dec_insn, Class* res_class,
4959 bool is_range, VerifyError* failure) {
4960 uint32_t arg_count = dec_insn->vA_;
4961 RegType expected_type;
4962 Class::PrimitiveType elem_type;
4963 unsigned int ui;
4964
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004965 DCHECK(res_class->IsArrayClass()) << PrettyClass(res_class);
jeffhaobdb76512011-09-07 11:43:16 -07004966 elem_type = res_class->GetComponentType()->GetPrimitiveType();
4967 if (elem_type == Class::kPrimNot) {
4968 expected_type = RegTypeFromClass(res_class->GetComponentType());
4969 } else {
4970 expected_type = PrimitiveTypeToRegType(elem_type);
4971 }
4972
4973 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07004974 * Verify each register. If "arg_count" is bad, VerifyRegisterType()
4975 * will run off the end of the list and fail. It's legal, if silly,
jeffhaobdb76512011-09-07 11:43:16 -07004976 * for arg_count to be zero.
4977 */
4978 for (ui = 0; ui < arg_count; ui++) {
4979 uint32_t get_reg;
4980
4981 if (is_range)
4982 get_reg = dec_insn->vC_ + ui;
4983 else
4984 get_reg = dec_insn->arg_[ui];
4985
4986 VerifyRegisterType(register_line, get_reg, expected_type, failure);
4987 if (*failure != VERIFY_ERROR_NONE) {
4988 LOG(ERROR) << "VFY: filled-new-array arg " << ui << "(" << get_reg
4989 << ") not valid";
4990 return;
4991 }
4992 }
4993}
4994
4995bool DexVerifier::IsCorrectInvokeKind(MethodType method_type,
4996 Method* res_method) {
4997 switch (method_type) {
4998 case METHOD_DIRECT:
4999 return res_method->IsDirect();
5000 case METHOD_STATIC:
5001 return res_method->IsStatic();
5002 case METHOD_VIRTUAL:
5003 case METHOD_INTERFACE:
5004 return !res_method->IsDirect();
5005 default:
5006 return false;
5007 }
5008}
5009
5010Method* DexVerifier::VerifyInvocationArgs(VerifierData* vdata,
5011 RegisterLine* register_line, const int insn_reg_count,
5012 const Instruction::DecodedInstruction* dec_insn, MethodType method_type,
5013 bool is_range, bool is_super, VerifyError* failure) {
5014 Method* method = vdata->method_;
5015 const DexFile* dex_file = vdata->dex_file_;
5016 const DexFile::CodeItem* code_item = vdata->code_item_;
Elliott Hughes5fe594f2011-09-08 12:33:17 -07005017 UninitInstanceMap* uninit_map = vdata->uninit_map_.get();
jeffhaobdb76512011-09-07 11:43:16 -07005018
5019 Method* res_method;
5020 std::string sig;
5021 size_t sig_offset;
5022 int expected_args;
5023 int actual_args;
5024
5025 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07005026 * Resolve the method. This could be an abstract or concrete method
jeffhaobdb76512011-09-07 11:43:16 -07005027 * depending on what sort of call we're making.
5028 */
jeffhaob4df5142011-09-19 20:25:32 -07005029 res_method = ResolveMethodAndCheckAccess(dex_file, dec_insn->vB_, method->GetDeclaringClass(),
5030 failure, (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
jeffhaobdb76512011-09-07 11:43:16 -07005031
jeffhaobdb76512011-09-07 11:43:16 -07005032 if (res_method == NULL) {
jeffhao98eacac2011-09-14 16:11:53 -07005033 const DexFile::MethodId& method_id = dex_file->GetMethodId(dec_insn->vB_);
5034 const char* method_name = dex_file->GetMethodName(method_id);
5035 const char* method_proto = dex_file->GetMethodPrototype(method_id);
5036 const char* class_descriptor = dex_file->GetMethodClassDescriptor(method_id);
5037
5038 LOG(ERROR) << "VFY: unable to resolve method " << dec_insn->vB_ << ": "
5039 << class_descriptor << "." << method_name << " " << method_proto;
jeffhaobdb76512011-09-07 11:43:16 -07005040 *failure = VERIFY_ERROR_NO_METHOD;
5041 return NULL;
5042 }
jeffhaobdb76512011-09-07 11:43:16 -07005043
5044 /*
5045 * Only time you can explicitly call a method starting with '<' is when
jeffhaod1f0fde2011-09-08 17:25:33 -07005046 * making a "direct" invocation on "<init>". There are additional
jeffhaobdb76512011-09-07 11:43:16 -07005047 * restrictions but we don't enforce them here.
5048 */
5049 if (res_method->GetName()->Equals("<init>")) {
5050 if (method_type != METHOD_DIRECT || !IsInitMethod(res_method)) {
5051 LOG(ERROR) << "VFY: invalid call to "
jeffhao98eacac2011-09-14 16:11:53 -07005052 << res_method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
jeffhaobdb76512011-09-07 11:43:16 -07005053 << "." << res_method->GetName();
5054 goto bad_sig;
5055 }
5056 }
5057
5058 /*
5059 * See if the method type implied by the invoke instruction matches the
5060 * access flags for the target method.
5061 */
5062 if (!IsCorrectInvokeKind(method_type, res_method)) {
5063 LOG(ERROR) << "VFY: invoke type does not match method type of "
jeffhao98eacac2011-09-14 16:11:53 -07005064 << res_method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
jeffhaobdb76512011-09-07 11:43:16 -07005065 << "." << res_method->GetName()->ToModifiedUtf8();
5066
5067 *failure = VERIFY_ERROR_GENERIC;
5068 return NULL;
5069 }
5070
5071 /*
5072 * If we're using invoke-super(method), make sure that the executing
5073 * method's class' superclass has a vtable entry for the target method.
5074 */
5075 if (is_super) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005076 DCHECK(method_type == METHOD_VIRTUAL);
jeffhaobdb76512011-09-07 11:43:16 -07005077 Class* super = method->GetDeclaringClass()->GetSuperClass();
5078 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
5079 if (super == NULL) {
5080 LOG(ERROR) << "VFY: invalid invoke-super from "
5081 << method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
5082 << "." << method->GetName()->ToModifiedUtf8() << " to super -."
5083 << res_method->GetName()->ToModifiedUtf8()
5084 << " " << res_method->GetSignature()->ToModifiedUtf8();
5085 } else {
5086 LOG(ERROR) << "VFY: invalid invoke-super from "
5087 << method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
5088 << "." << method->GetName()->ToModifiedUtf8() << " to super "
5089 << super->GetDescriptor()->ToModifiedUtf8()
5090 << "." << res_method->GetName()->ToModifiedUtf8()
5091 << " " << res_method->GetSignature()->ToModifiedUtf8();
5092 }
5093 *failure = VERIFY_ERROR_NO_METHOD;
5094 return NULL;
5095 }
5096 }
5097
5098 /*
5099 * We use vAA as our expected arg count, rather than res_method->insSize,
jeffhaod1f0fde2011-09-08 17:25:33 -07005100 * because we need to match the call to the signature. Also, we might
jeffhaobdb76512011-09-07 11:43:16 -07005101 * might be calling through an abstract method definition (which doesn't
5102 * have register count values).
5103 */
5104 expected_args = dec_insn->vA_;
5105 actual_args = 0;
5106
5107 /* caught by static verifier */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005108 DCHECK(is_range || expected_args <= 5);
jeffhaobdb76512011-09-07 11:43:16 -07005109
5110 if (expected_args > code_item->outs_size_) {
5111 LOG(ERROR) << "VFY: invalid arg count (" << expected_args
5112 << ") exceeds outsSize (" << code_item->outs_size_ << ")";
5113 *failure = VERIFY_ERROR_GENERIC;
5114 return NULL;
5115 }
5116
5117 sig = res_method->GetSignature()->ToModifiedUtf8();
5118 if (sig[0] != '(') {
5119 LOG(ERROR) << "VFY: descriptor doesn't start with '(': " << sig;
5120 goto bad_sig;
5121 }
5122
5123 /*
5124 * Check the "this" argument, which must be an instance of the class
jeffhaod1f0fde2011-09-08 17:25:33 -07005125 * that declared the method. For an interface class, we don't do the
jeffhaobdb76512011-09-07 11:43:16 -07005126 * full interface merge, so we can't do a rigorous check here (which
5127 * is okay since we have to do it at runtime).
5128 */
5129 if (!res_method->IsStatic()) {
5130 Class* actual_this_ref;
5131 RegType actual_arg_type;
5132
5133 actual_arg_type = GetInvocationThis(register_line, dec_insn, failure);
5134 if (*failure != VERIFY_ERROR_NONE)
5135 return NULL;
5136
5137 if (RegTypeIsUninitReference(actual_arg_type) &&
5138 !res_method->GetName()->Equals("<init>")) {
5139 LOG(ERROR) << "VFY: 'this' arg must be initialized";
5140 *failure = VERIFY_ERROR_GENERIC;
5141 return NULL;
5142 }
5143 if (method_type != METHOD_INTERFACE && actual_arg_type != kRegTypeZero) {
5144 actual_this_ref = RegTypeReferenceToClass(actual_arg_type, uninit_map);
5145 if (!res_method->GetDeclaringClass()->IsAssignableFrom(actual_this_ref)) {
5146 LOG(ERROR) << "VFY: 'this' arg '"
5147 << actual_this_ref->GetDescriptor()->ToModifiedUtf8()
5148 << "' not instance of '"
5149 << res_method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
5150 << "'";
5151 *failure = VERIFY_ERROR_GENERIC;
5152 return NULL;
5153 }
5154 }
5155 actual_args++;
5156 }
5157
5158 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07005159 * Process the target method's signature. This signature may or may not
jeffhaobdb76512011-09-07 11:43:16 -07005160 * have been verified, so we can't assume it's properly formed.
5161 */
5162 for (sig_offset = 1; sig_offset < sig.size(); sig_offset++) {
jeffhaobdb76512011-09-07 11:43:16 -07005163 if (sig[sig_offset] == ')')
5164 break;
5165
5166 if (actual_args >= expected_args) {
5167 LOG(ERROR) << "VFY: expected " << expected_args << " args, found more ("
5168 << sig.substr(sig_offset) << ")";
5169 goto bad_sig;
5170 }
5171
5172 uint32_t get_reg;
5173 if (is_range)
5174 get_reg = dec_insn->vC_ + actual_args;
5175 else
5176 get_reg = dec_insn->arg_[actual_args];
5177
5178 switch (sig[sig_offset]) {
5179 case 'L':
5180 {
5181 Class* klass = LookupSignatureClass(method, sig.substr(sig_offset),
5182 failure);
5183 if (*failure != VERIFY_ERROR_NONE)
5184 goto bad_sig;
5185 VerifyRegisterType(register_line, get_reg, RegTypeFromClass(klass),
5186 failure);
5187 if (*failure != VERIFY_ERROR_NONE) {
5188 LOG(ERROR) << "VFY: bad arg " << actual_args << " (into "
5189 << klass->GetDescriptor()->ToModifiedUtf8() << ")";
5190 goto bad_sig;
5191 }
5192 sig_offset += sig.substr(sig_offset).find(';');
5193 }
5194 actual_args++;
5195 break;
5196 case '[':
5197 {
5198 Class* klass = LookupSignatureArrayClass(method,
5199 sig.substr(sig_offset), failure);
5200 if (*failure != VERIFY_ERROR_NONE)
5201 goto bad_sig;
5202 VerifyRegisterType(register_line, get_reg, RegTypeFromClass(klass),
5203 failure);
5204 if (*failure != VERIFY_ERROR_NONE) {
5205 LOG(ERROR) << "VFY: bad arg " << actual_args << " (into "
5206 << klass->GetDescriptor()->ToModifiedUtf8() << ")";
5207 goto bad_sig;
5208 }
5209 while (sig[sig_offset] == '[')
5210 sig_offset++;
5211 if (sig[sig_offset] == 'L')
5212 sig_offset += sig.substr(sig_offset).find(';');
5213 }
5214 actual_args++;
5215 break;
5216 case 'Z':
5217 VerifyRegisterType(register_line, get_reg, kRegTypeBoolean, failure);
5218 actual_args++;
5219 break;
5220 case 'C':
5221 VerifyRegisterType(register_line, get_reg, kRegTypeChar, failure);
5222 actual_args++;
5223 break;
5224 case 'B':
5225 VerifyRegisterType(register_line, get_reg, kRegTypeByte, failure);
5226 actual_args++;
5227 break;
5228 case 'I':
5229 VerifyRegisterType(register_line, get_reg, kRegTypeInteger, failure);
5230 actual_args++;
5231 break;
5232 case 'S':
5233 VerifyRegisterType(register_line, get_reg, kRegTypeShort, failure);
5234 actual_args++;
5235 break;
5236 case 'F':
5237 VerifyRegisterType(register_line, get_reg, kRegTypeFloat, failure);
5238 actual_args++;
5239 break;
5240 case 'D':
5241 VerifyRegisterType(register_line, get_reg, kRegTypeDoubleLo, failure);
5242 actual_args += 2;
5243 break;
5244 case 'J':
5245 VerifyRegisterType(register_line, get_reg, kRegTypeLongLo, failure);
5246 actual_args += 2;
5247 break;
5248 default:
5249 LOG(ERROR) << "VFY: invocation target: bad signature type char '"
5250 << sig << "'";
5251 goto bad_sig;
5252 }
5253 }
5254 if (sig[sig_offset] != ')') {
5255 LOG(ERROR) << "VFY: invocation target: bad signature '"
5256 << res_method->GetSignature()->ToModifiedUtf8() << "'";
5257 goto bad_sig;
5258 }
5259
5260 if (actual_args != expected_args) {
5261 LOG(ERROR) << "VFY: expected " << expected_args << " args, found "
5262 << actual_args;
5263 goto bad_sig;
5264 }
5265
5266 return res_method;
5267
5268bad_sig:
5269 if (res_method != NULL) {
5270 LOG(ERROR) << "VFY: rejecting call to "
5271 << res_method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
5272 << "." << res_method->GetName()->ToModifiedUtf8() << " "
5273 << res_method->GetSignature()->ToModifiedUtf8();
5274 }
5275
5276 if (*failure == VERIFY_ERROR_NONE)
5277 *failure = VERIFY_ERROR_GENERIC;
5278 return NULL;
5279}
jeffhaoba5ebb92011-08-25 17:24:37 -07005280
jeffhaod1f0fde2011-09-08 17:25:33 -07005281DexVerifier::RegisterMap* DexVerifier::GenerateRegisterMapV(VerifierData* vdata)
5282{
5283 const DexFile::CodeItem* code_item = vdata->code_item_;
5284 int i, bytes_for_addr, gc_point_count;
5285
5286 if (code_item->registers_size_ >= 2048) {
5287 LOG(ERROR) << "ERROR: register map can't handle "
5288 << code_item->registers_size_ << " registers";
5289 return NULL;
5290 }
5291 uint8_t reg_width = (code_item->registers_size_ + 7) / 8;
5292
5293 /*
5294 * Decide if we need 8 or 16 bits to hold the address. Strictly speaking
5295 * we only need 16 bits if we actually encode an address >= 256 -- if
5296 * the method has a section at the end without GC points (e.g. array
5297 * data) we don't need to count it. The situation is unusual, and
5298 * detecting it requires scanning the entire method, so we don't bother.
5299 */
5300 RegisterMapFormat format;
5301 if (code_item->insns_size_ < 256) {
5302 format = kRegMapFormatCompact8;
5303 bytes_for_addr = 1;
5304 } else {
5305 format = kRegMapFormatCompact16;
5306 bytes_for_addr = 2;
5307 }
5308
5309 /*
5310 * Count up the number of GC point instructions.
5311 *
5312 * NOTE: this does not automatically include the first instruction,
5313 * since we don't count method entry as a GC point.
5314 */
5315 gc_point_count = 0;
5316 for (i = 0; i < (int) code_item->insns_size_; i++) {
5317 if (InsnIsGcPoint(vdata->insn_flags_.get(), i))
5318 gc_point_count++;
5319 }
5320 if (gc_point_count >= 65536) {
5321 /* We could handle this, but in practice we don't get near this. */
5322 LOG(ERROR) << "ERROR: register map can't handle " << gc_point_count
5323 << " gc points in one method";
5324 return NULL;
5325 }
5326
5327 /* Calculate size of buffer to hold the map data. */
5328 uint32_t data_size = gc_point_count * (bytes_for_addr + reg_width);
5329
5330 RegisterMap* map = new RegisterMap(format, reg_width, gc_point_count,
jeffhaoa0a764a2011-09-16 10:43:38 -07005331 data_size);
jeffhaod1f0fde2011-09-08 17:25:33 -07005332
5333 /* Populate it. */
jeffhaoe23d93c2011-09-15 14:48:43 -07005334 uint8_t* map_data = map->data_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005335 for (i = 0; i < (int) vdata->code_item_->insns_size_; i++) {
5336 if (InsnIsGcPoint(vdata->insn_flags_.get(), i)) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005337 DCHECK(vdata->register_lines_[i].reg_types_.get() != NULL);
jeffhaod1f0fde2011-09-08 17:25:33 -07005338 if (format == kRegMapFormatCompact8) {
5339 *map_data++ = i;
5340 } else /*kRegMapFormatCompact16*/ {
5341 *map_data++ = i & 0xff;
5342 *map_data++ = i >> 8;
5343 }
5344 OutputTypeVector(vdata->register_lines_[i].reg_types_.get(),
5345 code_item->registers_size_, map_data);
5346 map_data += reg_width;
5347 }
5348 }
5349
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005350 DCHECK_EQ((uint32_t) map_data - (uint32_t) map->data_, data_size);
jeffhaod1f0fde2011-09-08 17:25:33 -07005351
5352 // TODO: Remove this check when it's really running...
5353#if 1
5354 if (!VerifyMap(vdata, map)) {
5355 LOG(ERROR) << "Map failed to verify";
5356 return NULL;
5357 }
5358#endif
5359
5360 /* Try to compress the map. */
5361 RegisterMap* compress_map = CompressMapDifferential(map);
5362 if (compress_map != NULL) {
5363 // TODO: Remove this check when it's really running...
5364#if 1
5365 /*
5366 * Expand the compressed map we just created, and compare it
5367 * to the original. Abort the VM if it doesn't match up.
5368 */
5369 UniquePtr<RegisterMap> uncompressed_map(UncompressMapDifferential(compress_map));
5370 if (uncompressed_map.get() == NULL) {
5371 LOG(ERROR) << "Map failed to uncompress - "
5372 << vdata->method_->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
5373 << "." << vdata->method_->GetName()->ToModifiedUtf8();
5374 delete map;
5375 delete compress_map;
5376 /* bad - compression is broken or we're out of memory */
5377 return NULL;
5378 } else {
5379 if (!CompareMaps(map, uncompressed_map.get())) {
5380 LOG(ERROR) << "Map comparison failed - "
5381 << vdata->method_->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
5382 << "." << vdata->method_->GetName()->ToModifiedUtf8();
5383 delete map;
5384 delete compress_map;
5385 /* bad - compression is broken */
5386 return NULL;
5387 }
5388 }
5389#endif
5390 delete map;
5391 map = compress_map;
5392 }
5393
5394 return map;
5395}
5396
jeffhaoa0a764a2011-09-16 10:43:38 -07005397DexVerifier::RegisterMap* DexVerifier::GetExpandedRegisterMapHelper(
5398 Method* method, RegisterMap* map) {
5399 RegisterMap* new_map;
5400
5401 if (map == NULL)
5402 return NULL;
5403
5404 /* TODO: sanity check to ensure this isn't called w/o external locking */
5405
5406 uint8_t format = map->header_->format_;
5407 switch (format) {
5408 case kRegMapFormatCompact8:
5409 case kRegMapFormatCompact16:
5410 /* already expanded */
5411 return map;
5412 case kRegMapFormatDifferential:
5413 new_map = UncompressMapDifferential(map);
5414 break;
5415 default:
5416 LOG(ERROR) << "Unknown format " << format
5417 << " in dvmGetExpandedRegisterMap";
5418 return NULL;
5419 }
5420
5421 if (new_map == NULL) {
5422 LOG(ERROR) << "Map failed to uncompress (fmt=" << format << ") "
5423 << method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
5424 << "." << method->GetName();
5425 return NULL;
5426 }
5427
5428 /* Update method, and free compressed map if it was sitting on the heap. */
5429 ByteArray* header = ByteArray::Alloc(sizeof(RegisterMapHeader));
5430 ByteArray* data = ByteArray::Alloc(ComputeRegisterMapSize(map));
5431
5432 memcpy(header->GetData(), map->header_, sizeof(RegisterMapHeader));
5433 memcpy(data->GetData(), map->data_, ComputeRegisterMapSize(map));
5434
5435 method->SetRegisterMapHeader(header);
5436 method->SetRegisterMapData(data);
5437
5438 delete map;
5439 return new_map;
5440}
5441
5442const uint8_t* DexVerifier::RegisterMapGetLine(const RegisterMap* map, int addr) {
5443 int addr_width, line_width;
5444 uint8_t format = map->header_->format_;
5445 uint16_t num_entries = map->header_->num_entries_;
5446
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005447 DCHECK_GT(num_entries, 0);
jeffhaoa0a764a2011-09-16 10:43:38 -07005448
5449 switch (format) {
5450 case kRegMapFormatNone:
5451 return NULL;
5452 case kRegMapFormatCompact8:
5453 addr_width = 1;
5454 break;
5455 case kRegMapFormatCompact16:
5456 addr_width = 2;
5457 break;
5458 default:
5459 LOG(ERROR) << "Unknown format " << format;
5460 return NULL;
5461 }
5462
5463 line_width = addr_width + map->header_->reg_width_;
5464
5465 /*
5466 * Find the appropriate entry. Many maps are very small, some are very large.
5467 */
5468 static const int kSearchThreshold = 8;
5469 const uint8_t* data = NULL;
5470 int line_addr;
5471
5472 if (num_entries < kSearchThreshold) {
5473 int i;
5474 data = map->data_;
5475 for (i = num_entries; i > 0; i--) {
5476 line_addr = data[0];
5477 if (addr_width > 1)
5478 line_addr |= data[1] << 8;
5479 if (line_addr == addr)
5480 return data + addr_width;
5481
5482 data += line_width;
5483 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005484 DCHECK_EQ(data, map->data_ + line_width * num_entries);
jeffhaoa0a764a2011-09-16 10:43:38 -07005485 } else {
5486 int hi, lo, mid;
5487
5488 lo = 0;
5489 hi = num_entries -1;
5490
5491 while (hi >= lo) {
5492 mid = (hi + lo) / 2;
5493 data = map->data_ + line_width * mid;
5494
5495 line_addr = data[0];
5496 if (addr_width > 1)
5497 line_addr |= data[1] << 8;
5498
5499 if (addr > line_addr) {
5500 lo = mid + 1;
5501 } else if (addr < line_addr) {
5502 hi = mid - 1;
5503 } else {
5504 return data + addr_width;
5505 }
5506 }
5507 }
5508
5509 return NULL;
5510}
5511
jeffhaod1f0fde2011-09-08 17:25:33 -07005512void DexVerifier::OutputTypeVector(const RegType* regs, int insn_reg_count,
5513 uint8_t* data) {
5514 uint8_t val = 0;
5515 int i;
5516
5517 for (i = 0; i < insn_reg_count; i++) {
5518 RegType type = *regs++;
5519 val >>= 1;
5520 if (IsReferenceType(type))
5521 val |= 0x80; /* set hi bit */
5522
5523 if ((i & 0x07) == 7)
5524 *data++ = val;
5525 }
5526 if ((i & 0x07) != 0) {
5527 /* Flush bits from last byte. */
5528 val >>= 8 - (i & 0x07);
5529 *data++ = val;
5530 }
5531}
5532
5533bool DexVerifier::VerifyMap(VerifierData* vdata, const RegisterMap* map) {
jeffhaoe23d93c2011-09-15 14:48:43 -07005534 const uint8_t* raw_map = map->data_;
5535 uint8_t format = map->header_->format_;
5536 const int num_entries = map->header_->num_entries_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005537 int ent;
5538
jeffhaoe23d93c2011-09-15 14:48:43 -07005539 if ((vdata->code_item_->registers_size_ + 7) / 8 != map->header_->reg_width_) {
jeffhaod1f0fde2011-09-08 17:25:33 -07005540 LOG(ERROR) << "GLITCH: registersSize=" << vdata->code_item_->registers_size_
jeffhaoe23d93c2011-09-15 14:48:43 -07005541 << ", reg_width=" << map->header_->reg_width_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005542 return false;
5543 }
5544
5545 for (ent = 0; ent < num_entries; ent++) {
5546 int addr;
5547
5548 switch (format) {
5549 case kRegMapFormatCompact8:
5550 addr = *raw_map++;
5551 break;
5552 case kRegMapFormatCompact16:
5553 addr = *raw_map++;
5554 addr |= (*raw_map++) << 8;
5555 break;
5556 default:
5557 LOG(FATAL) << "GLITCH: bad format (" << format << ")";
5558 return false;
5559 }
5560
5561 const RegType* regs = vdata->register_lines_[addr].reg_types_.get();
5562 if (regs == NULL) {
5563 LOG(ERROR) << "GLITCH: addr " << addr << " has no data";
5564 return false;
5565 }
5566
5567 uint8_t val = 0;
5568 int i;
5569
5570 for (i = 0; i < vdata->code_item_->registers_size_; i++) {
5571 bool bit_is_ref, reg_is_ref;
5572
5573 val >>= 1;
5574 if ((i & 0x07) == 0) {
5575 /* Load next byte of data. */
5576 val = *raw_map++;
5577 }
5578
5579 bit_is_ref = val & 0x01;
5580
5581 RegType type = regs[i];
5582 reg_is_ref = IsReferenceType(type);
5583
5584 if (bit_is_ref != reg_is_ref) {
5585 LOG(ERROR) << "GLITCH: addr " << addr << " reg " << i << ": bit="
5586 << bit_is_ref << " reg=" << reg_is_ref << "(" << type << ")";
5587 return false;
5588 }
5589 }
5590 /* Raw_map now points to the address field of the next entry. */
5591 }
5592
5593 return true;
5594}
5595
5596bool DexVerifier::CompareMaps(const RegisterMap* map1, const RegisterMap* map2)
5597{
5598 size_t size1, size2;
5599
5600 size1 = ComputeRegisterMapSize(map1);
5601 size2 = ComputeRegisterMapSize(map2);
5602 if (size1 != size2) {
5603 LOG(ERROR) << "CompareMaps: size mismatch (" << size1 << " vs " << size2
5604 << ")";
5605 return false;
5606 }
5607
jeffhaoe23d93c2011-09-15 14:48:43 -07005608 if (map1->header_->format_ != map2->header_->format_ ||
5609 map1->header_->reg_width_ != map2->header_->reg_width_ ||
jeffhaoa0a764a2011-09-16 10:43:38 -07005610 map1->header_->num_entries_ != map2->header_->num_entries_) {
jeffhaod1f0fde2011-09-08 17:25:33 -07005611 LOG(ERROR) << "CompareMaps: fields mismatch";
5612 }
jeffhaoe23d93c2011-09-15 14:48:43 -07005613 if (memcmp(map1->data_, map2->data_, size1) != 0) {
jeffhaod1f0fde2011-09-08 17:25:33 -07005614 LOG(ERROR) << "CompareMaps: data mismatch";
5615 return false;
5616 }
5617
5618 return true;
5619}
5620
5621size_t DexVerifier::ComputeRegisterMapSize(const RegisterMap* map) {
jeffhaoe23d93c2011-09-15 14:48:43 -07005622 uint8_t format = map->header_->format_;
5623 uint16_t num_entries = map->header_->num_entries_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005624
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005625 DCHECK(map != NULL);
jeffhaod1f0fde2011-09-08 17:25:33 -07005626
5627 switch (format) {
5628 case kRegMapFormatNone:
5629 return 1;
5630 case kRegMapFormatCompact8:
jeffhaoe23d93c2011-09-15 14:48:43 -07005631 return (1 + map->header_->reg_width_) * num_entries;
jeffhaod1f0fde2011-09-08 17:25:33 -07005632 case kRegMapFormatCompact16:
jeffhaoe23d93c2011-09-15 14:48:43 -07005633 return (2 + map->header_->reg_width_) * num_entries;
jeffhaod1f0fde2011-09-08 17:25:33 -07005634 case kRegMapFormatDifferential:
5635 {
5636 /* Decoded ULEB128 length. */
jeffhaoe23d93c2011-09-15 14:48:43 -07005637 const uint8_t* ptr = map->data_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005638 return DecodeUnsignedLeb128(&ptr);
5639 }
5640 default:
5641 LOG(FATAL) << "Bad register map format " << format;
5642 return 0;
5643 }
5644}
5645
5646int DexVerifier::ComputeBitDiff(const uint8_t* bits1, const uint8_t* bits2,
5647 int byte_width, int* first_bit_changed_ptr, int* num_bits_changed_ptr,
5648 uint8_t* leb_out_buf) {
5649 int num_bits_changed = 0;
5650 int first_bit_changed = -1;
5651 int leb_size = 0;
5652 int byte_num;
5653
5654 /*
5655 * Run through the vectors, first comparing them at the byte level. This
5656 * will yield a fairly quick result if nothing has changed between them.
5657 */
5658 for (byte_num = 0; byte_num < byte_width; byte_num++) {
5659 uint8_t byte1 = *bits1++;
5660 uint8_t byte2 = *bits2++;
5661 if (byte1 != byte2) {
5662 /* Walk through the byte, identifying the changed bits. */
5663 int bit_num;
5664 for (bit_num = 0; bit_num < 8; bit_num++) {
5665 if (((byte1 >> bit_num) & 0x01) != ((byte2 >> bit_num) & 0x01)) {
5666 int bit_offset = (byte_num << 3) + bit_num;
5667
5668 if (first_bit_changed < 0)
5669 first_bit_changed = bit_offset;
5670 num_bits_changed++;
5671
5672 if (leb_out_buf == NULL) {
5673 leb_size += UnsignedLeb128Size(bit_offset);
5674 } else {
5675 uint8_t* cur_buf = leb_out_buf;
5676 leb_out_buf = WriteUnsignedLeb128(leb_out_buf, bit_offset);
5677 leb_size += leb_out_buf - cur_buf;
5678 }
5679 }
5680 }
5681 }
5682 }
5683
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005684 if (num_bits_changed > 0) {
5685 DCHECK_GE(first_bit_changed, 0);
5686 }
jeffhaod1f0fde2011-09-08 17:25:33 -07005687
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005688 if (first_bit_changed_ptr != NULL) {
jeffhaod1f0fde2011-09-08 17:25:33 -07005689 *first_bit_changed_ptr = first_bit_changed;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005690 }
5691
5692 if (num_bits_changed_ptr != NULL) {
jeffhaod1f0fde2011-09-08 17:25:33 -07005693 *num_bits_changed_ptr = num_bits_changed;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005694 }
jeffhaod1f0fde2011-09-08 17:25:33 -07005695
5696 return leb_size;
5697}
5698
5699DexVerifier::RegisterMap* DexVerifier::CompressMapDifferential(
5700 const RegisterMap* map) {
5701 int orig_size = ComputeRegisterMapSize(map);
5702 uint8_t* tmp_ptr;
5703 int addr_width;
5704
jeffhaoe23d93c2011-09-15 14:48:43 -07005705 uint8_t format = map->header_->format_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005706 switch (format) {
5707 case kRegMapFormatCompact8:
5708 addr_width = 1;
5709 break;
5710 case kRegMapFormatCompact16:
5711 addr_width = 2;
5712 break;
5713 default:
5714 LOG(ERROR) << "ERROR: can't compress map with format=" << format;
5715 return NULL;
5716 }
5717
jeffhaoe23d93c2011-09-15 14:48:43 -07005718 int reg_width = map->header_->reg_width_;
5719 int num_entries = map->header_->num_entries_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005720
5721 if (num_entries <= 1) {
5722 return NULL;
5723 }
5724
5725 /*
5726 * We don't know how large the compressed data will be. It's possible
5727 * for it to expand and become larger than the original. The header
5728 * itself is variable-sized, so we generate everything into a temporary
5729 * buffer and then copy it to form-fitting storage once we know how big
5730 * it will be (and that it's smaller than the original).
5731 *
5732 * If we use a size that is equal to the size of the input map plus
5733 * a value longer than a single entry can possibly expand to, we need
5734 * only check for overflow at the end of each entry. The worst case
5735 * for a single line is (1 + <ULEB8 address> + <full copy of vector>).
5736 * Addresses are 16 bits, so that's (1 + 3 + reg_width).
5737 *
5738 * The initial address offset and bit vector will take up less than
5739 * or equal to the amount of space required when uncompressed -- large
5740 * initial offsets are rejected.
5741 */
5742 UniquePtr<uint8_t[]> tmp_buf(new uint8_t[orig_size + (1 + 3 + reg_width)]);
5743
5744 tmp_ptr = tmp_buf.get();
5745
jeffhaoe23d93c2011-09-15 14:48:43 -07005746 const uint8_t* map_data = map->data_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005747 const uint8_t* prev_bits;
5748 uint16_t addr, prev_addr;
5749
5750 addr = *map_data++;
5751 if (addr_width > 1)
5752 addr |= (*map_data++) << 8;
5753
5754 if (addr >= 128) {
5755 LOG(ERROR) << "Can't compress map with starting address >= 128";
5756 return NULL;
5757 }
5758
5759 /*
5760 * Start by writing the initial address and bit vector data. The high
5761 * bit of the initial address is used to indicate the required address
5762 * width (which the decoder can't otherwise determine without parsing
5763 * the compressed data).
5764 */
5765 *tmp_ptr++ = addr | (addr_width > 1 ? 0x80 : 0x00);
5766 memcpy(tmp_ptr, map_data, reg_width);
5767
5768 prev_bits = map_data;
5769 prev_addr = addr;
5770
5771 tmp_ptr += reg_width;
5772 map_data += reg_width;
5773
5774 /* Loop over all following entries. */
5775 for (int entry = 1; entry < num_entries; entry++) {
5776 int addr_diff;
5777 uint8_t key;
5778
5779 /* Pull out the address and figure out how to encode it. */
5780 addr = *map_data++;
5781 if (addr_width > 1)
5782 addr |= (*map_data++) << 8;
5783
5784 addr_diff = addr - prev_addr;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005785 DCHECK_GT(addr_diff, 0);
jeffhaod1f0fde2011-09-08 17:25:33 -07005786 if (addr_diff < 8) {
5787 /* Small difference, encode in 3 bits. */
5788 key = addr_diff -1; /* set 00000AAA */
5789 } else {
5790 /* Large difference, output escape code. */
5791 key = 0x07; /* escape code for AAA */
5792 }
5793
5794 int num_bits_changed, first_bit_changed, leb_size;
5795
5796 leb_size = ComputeBitDiff(prev_bits, map_data, reg_width,
5797 &first_bit_changed, &num_bits_changed, NULL);
5798
5799 if (num_bits_changed == 0) {
5800 /* set B to 1 and CCCC to zero to indicate no bits were changed */
5801 key |= 0x08;
5802 } else if (num_bits_changed == 1 && first_bit_changed < 16) {
5803 /* set B to 0 and CCCC to the index of the changed bit */
5804 key |= first_bit_changed << 4;
5805 } else if (num_bits_changed < 15 && leb_size < reg_width) {
5806 /* set B to 1 and CCCC to the number of bits */
5807 key |= 0x08 | (num_bits_changed << 4);
5808 } else {
5809 /* set B to 1 and CCCC to 0x0f so we store the entire vector */
5810 key |= 0x08 | 0xf0;
5811 }
5812
5813 /*
5814 * Encode output. Start with the key, follow with the address
5815 * diff (if it didn't fit in 3 bits), then the changed bit info.
5816 */
5817 *tmp_ptr++ = key;
5818 if ((key & 0x07) == 0x07)
5819 tmp_ptr = WriteUnsignedLeb128(tmp_ptr, addr_diff);
5820
5821 if ((key & 0x08) != 0) {
5822 int bit_count = key >> 4;
5823 if (bit_count == 0) {
5824 /* nothing changed, no additional output required */
5825 } else if (bit_count == 15) {
5826 /* full vector is most compact representation */
5827 memcpy(tmp_ptr, map_data, reg_width);
5828 tmp_ptr += reg_width;
5829 } else {
5830 /* write bit indices in ULEB128 format */
5831 (void) ComputeBitDiff(prev_bits, map_data, reg_width,
5832 NULL, NULL, tmp_ptr);
5833 tmp_ptr += leb_size;
5834 }
5835 } else {
5836 /* single-bit changed, value encoded in key byte */
5837 }
5838
5839 prev_bits = map_data;
5840 prev_addr = addr;
5841 map_data += reg_width;
5842
5843 /* See if we've run past the original size. */
5844 if (tmp_ptr - tmp_buf.get() >= orig_size) {
5845 return NULL;
5846 }
5847 }
5848
5849 /*
5850 * Create a RegisterMap with the contents.
5851 *
5852 * TODO: consider using a threshold other than merely ">=". We would
5853 * get poorer compression but potentially use less native heap space.
5854 */
5855 int new_data_size = tmp_ptr - tmp_buf.get();
5856 int new_map_size = new_data_size + UnsignedLeb128Size(new_data_size);
5857
5858 if (new_map_size >= orig_size) {
5859 return NULL;
5860 }
5861
5862 RegisterMap* new_map = new RegisterMap(kRegMapFormatDifferential, reg_width,
jeffhaoa0a764a2011-09-16 10:43:38 -07005863 num_entries, new_map_size);
jeffhaod1f0fde2011-09-08 17:25:33 -07005864
jeffhaoe23d93c2011-09-15 14:48:43 -07005865 tmp_ptr = new_map->data_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005866 tmp_ptr = WriteUnsignedLeb128(tmp_ptr, new_data_size);
5867 memcpy(tmp_ptr, tmp_buf.get(), new_data_size);
5868
5869 return new_map;
5870}
5871
5872DexVerifier::RegisterMap* DexVerifier::UncompressMapDifferential(
5873 const RegisterMap* map) {
jeffhaoe23d93c2011-09-15 14:48:43 -07005874 uint8_t format = map->header_->format_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005875 RegisterMapFormat new_format;
5876 int reg_width, num_entries, new_addr_width, new_data_size;
5877
5878 if (format != kRegMapFormatDifferential) {
5879 LOG(ERROR) << "Not differential (" << format << ")";
5880 return NULL;
5881 }
5882
jeffhaoe23d93c2011-09-15 14:48:43 -07005883 reg_width = map->header_->reg_width_;
5884 num_entries = map->header_->num_entries_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005885
5886 /* Get the data size; we can check this at the end. */
jeffhaoe23d93c2011-09-15 14:48:43 -07005887 const uint8_t* src_ptr = map->data_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005888 int expected_src_len = DecodeUnsignedLeb128(&src_ptr);
5889 const uint8_t* src_start = src_ptr;
5890
5891 /* Get the initial address and the 16-bit address flag. */
5892 int addr = *src_ptr & 0x7f;
5893 if ((*src_ptr & 0x80) == 0) {
5894 new_format = kRegMapFormatCompact8;
5895 new_addr_width = 1;
5896 } else {
5897 new_format = kRegMapFormatCompact16;
5898 new_addr_width = 2;
5899 }
5900 src_ptr++;
5901
5902 /* Now we know enough to allocate the new map. */
5903 new_data_size = (new_addr_width + reg_width) * num_entries;
5904 RegisterMap* new_map = new RegisterMap(new_format, reg_width, num_entries,
jeffhaoa0a764a2011-09-16 10:43:38 -07005905 new_data_size);
jeffhaod1f0fde2011-09-08 17:25:33 -07005906
5907 /* Write the start address and initial bits to the new map. */
jeffhaoe23d93c2011-09-15 14:48:43 -07005908 uint8_t* dst_ptr = new_map->data_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005909
5910 *dst_ptr++ = addr & 0xff;
5911 if (new_addr_width > 1)
5912 *dst_ptr++ = (uint8_t) (addr >> 8);
5913
5914 memcpy(dst_ptr, src_ptr, reg_width);
5915
5916 int prev_addr = addr;
5917 const uint8_t* prev_bits = dst_ptr; /* point at uncompressed data */
5918
5919 dst_ptr += reg_width;
5920 src_ptr += reg_width;
5921
5922 /* Walk through, uncompressing one line at a time. */
5923 int entry;
5924 for (entry = 1; entry < num_entries; entry++) {
5925 int addr_diff;
5926 uint8_t key;
5927
5928 key = *src_ptr++;
5929
5930 /* Get the address. */
5931 if ((key & 0x07) == 7) {
5932 /* Address diff follows in ULEB128. */
5933 addr_diff = DecodeUnsignedLeb128(&src_ptr);
5934 } else {
5935 addr_diff = (key & 0x07) +1;
5936 }
5937
5938 addr = prev_addr + addr_diff;
5939 *dst_ptr++ = addr & 0xff;
5940 if (new_addr_width > 1)
5941 *dst_ptr++ = (uint8_t) (addr >> 8);
5942
5943 /* Unpack the bits. */
5944 if ((key & 0x08) != 0) {
5945 int bit_count = (key >> 4);
5946 if (bit_count == 0) {
5947 /* No bits changed, just copy previous. */
5948 memcpy(dst_ptr, prev_bits, reg_width);
5949 } else if (bit_count == 15) {
5950 /* Full copy of bit vector is present; ignore prev_bits. */
5951 memcpy(dst_ptr, src_ptr, reg_width);
5952 src_ptr += reg_width;
5953 } else {
5954 /* Copy previous bits and modify listed indices. */
5955 memcpy(dst_ptr, prev_bits, reg_width);
5956 while (bit_count--) {
5957 int bit_index = DecodeUnsignedLeb128(&src_ptr);
5958 ToggleBit(dst_ptr, bit_index);
5959 }
5960 }
5961 } else {
5962 /* Copy previous bits and modify the specified one. */
5963 memcpy(dst_ptr, prev_bits, reg_width);
5964
5965 /* One bit, from 0-15 inclusive, was changed. */
5966 ToggleBit(dst_ptr, key >> 4);
5967 }
5968
5969 prev_addr = addr;
5970 prev_bits = dst_ptr;
5971 dst_ptr += reg_width;
5972 }
5973
jeffhaoe23d93c2011-09-15 14:48:43 -07005974 if (dst_ptr - new_map->data_ != new_data_size) {
5975 LOG(ERROR) << "ERROR: output " << dst_ptr - new_map->data_
jeffhaod1f0fde2011-09-08 17:25:33 -07005976 << " bytes, expected " << new_data_size;
5977 free(new_map);
5978 return NULL;
5979 }
5980
5981 if (src_ptr - src_start != expected_src_len) {
5982 LOG(ERROR) << "ERROR: consumed " << src_ptr - src_start
5983 << " bytes, expected " << expected_src_len;
5984 free(new_map);
5985 return NULL;
5986 }
5987
5988 return new_map;
5989}
5990
jeffhaoe0cfb6f2011-09-22 16:42:56 -07005991/* Dump the register types for the specifed address to the log file. */
5992void DexVerifier::DumpRegTypes(const VerifierData* vdata,
5993 const RegisterLine* register_line, int addr, const char* addr_name,
5994 const UninitInstanceMap* uninit_map) {
5995 const DexFile::CodeItem* code_item = vdata->code_item_;
5996 uint16_t reg_count = code_item->registers_size_;
5997 uint32_t insns_size = code_item->insns_size_;
5998 const InsnFlags* insn_flags = vdata->insn_flags_.get();
5999 const RegType* addr_regs = register_line->reg_types_.get();
6000 int full_reg_count = reg_count + kExtraRegs;
6001 bool branch_target = InsnIsBranchTarget(insn_flags, addr);
6002 int i;
6003
6004 CHECK(addr >= 0 && addr < (int) insns_size);
6005
6006 int reg_char_size = full_reg_count + (full_reg_count - 1) / 4 + 2 + 1;
6007 char reg_chars[reg_char_size + 1];
6008 memset(reg_chars, ' ', reg_char_size);
6009 reg_chars[0] = '[';
6010 if (reg_count == 0)
6011 reg_chars[1] = ']';
6012 else
6013 reg_chars[1 + (reg_count - 1) + (reg_count -1 ) / 4 + 1] = ']';
6014 reg_chars[reg_char_size] = '\0';
6015
6016 for (i = 0; i < reg_count + kExtraRegs; i++) {
6017 char tch;
6018
6019 switch (addr_regs[i]) {
6020 case kRegTypeUnknown: tch = '.'; break;
6021 case kRegTypeConflict: tch = 'X'; break;
6022 case kRegTypeZero: tch = '0'; break;
6023 case kRegTypeOne: tch = '1'; break;
6024 case kRegTypeBoolean: tch = 'Z'; break;
6025 case kRegTypeConstPosByte: tch = 'y'; break;
6026 case kRegTypeConstByte: tch = 'Y'; break;
6027 case kRegTypeConstPosShort: tch = 'h'; break;
6028 case kRegTypeConstShort: tch = 'H'; break;
6029 case kRegTypeConstChar: tch = 'c'; break;
6030 case kRegTypeConstInteger: tch = 'i'; break;
6031 case kRegTypePosByte: tch = 'b'; break;
6032 case kRegTypeByte: tch = 'B'; break;
6033 case kRegTypePosShort: tch = 's'; break;
6034 case kRegTypeShort: tch = 'S'; break;
6035 case kRegTypeChar: tch = 'C'; break;
6036 case kRegTypeInteger: tch = 'I'; break;
6037 case kRegTypeFloat: tch = 'F'; break;
6038 case kRegTypeConstLo: tch = 'N'; break;
6039 case kRegTypeConstHi: tch = 'n'; break;
6040 case kRegTypeLongLo: tch = 'J'; break;
6041 case kRegTypeLongHi: tch = 'j'; break;
6042 case kRegTypeDoubleLo: tch = 'D'; break;
6043 case kRegTypeDoubleHi: tch = 'd'; break;
6044 default:
6045 if (RegTypeIsReference(addr_regs[i])) {
6046 if (RegTypeIsUninitReference(addr_regs[i]))
6047 tch = 'U';
6048 else
6049 tch = 'L';
6050 } else {
6051 tch = '*';
6052 CHECK(false);
6053 }
6054 break;
6055 }
6056
6057 if (i < reg_count)
6058 reg_chars[1 + i + (i / 4)] = tch;
6059 else
6060 reg_chars[1 + i + (i / 4) + 2] = tch;
6061 }
6062
6063 if (addr == 0 && addr_name != NULL) {
6064 char start = branch_target ? '>' : ' ';
6065 LOG(INFO) << start << addr_name << " " << reg_chars << " mst="
6066 << register_line->monitor_stack_top_;
6067 } else {
6068 char start = branch_target ? '>' : ' ';
6069 LOG(INFO) << start << "0x" << std::hex << addr << std::dec << " "
6070 << reg_chars << " mst=" << register_line->monitor_stack_top_;
6071 }
6072
6073 for (i = 0; i < reg_count + kExtraRegs; i++) {
6074 if (RegTypeIsReference(addr_regs[i]) && addr_regs[i] != kRegTypeZero) {
6075 Class* klass = RegTypeReferenceToClass(addr_regs[i], uninit_map);
6076 if (i < reg_count) {
6077 const char* undef = RegTypeIsUninitReference(addr_regs[i]) ? "[U]" : "";
6078 LOG(INFO) << " " << i << ": 0x" << std::hex << addr_regs[i] << std::dec
6079 << " " << undef << klass->GetDescriptor()->ToModifiedUtf8();
6080 } else {
6081 const char* undef = RegTypeIsUninitReference(addr_regs[i]) ? "[U]" : "";
6082 LOG(INFO) << " RS: 0x" << std::hex << addr_regs[i] << std::dec
6083 << " " << undef << klass->GetDescriptor()->ToModifiedUtf8();
6084 }
6085 }
6086 }
6087}
6088
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07006089} // namespace art