blob: fd100170a99243e3e2d01cb481dbb6357088a1c5 [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();
jeffhaobdb76512011-09-07 11:43:16 -07001384 const uint16_t* insns = code_item->insns_ + insn_idx;
1385 uint32_t insns_size = code_item->insns_size_;
1386 uint32_t registers_size = code_item->registers_size_;
1387
1388#ifdef VERIFIER_STATS
1389 if (InsnIsVisited(insn_flags, insn_idx)) {
1390 gDvm.verifierStats.instrsReexamined++;
1391 } else {
1392 gDvm.verifierStats.instrsExamined++;
1393 }
1394#endif
1395
1396 /*
1397 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001398 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001399 * control to another statement:
1400 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001401 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001402 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001403 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001404 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001405 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001406 * throw an exception that is handled by an encompassing "try"
1407 * block.
1408 *
1409 * We can also return, in which case there is no successor instruction
1410 * from this point.
1411 *
1412 * The behavior can be determined from the OpcodeFlags.
1413 */
1414 RegisterLine* work_line = &reg_table->work_line_;
1415 const DexFile* dex_file = vdata->dex_file_;
1416 const byte* ptr = reinterpret_cast<const byte*>(insns);
1417 const Instruction* inst = Instruction::At(ptr);
1418 Instruction::DecodedInstruction dec_insn(inst);
1419 int opcode_flag = inst->Flag();
1420
1421 Class* res_class;
1422 int32_t branch_target = 0;
1423 RegType tmp_type;
1424 bool just_set_result = false;
1425 VerifyError failure = VERIFY_ERROR_NONE;
1426
1427 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001428 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001429 * can throw an exception, we will copy/merge this into the "catch"
1430 * address rather than work_line, because we don't want the result
1431 * from the "successful" code path (e.g. a check-cast that "improves"
1432 * a type) to be visible to the exception handler.
1433 */
1434 if ((opcode_flag & Instruction::kThrow) != 0 &&
1435 InsnIsInTry(insn_flags, insn_idx)) {
1436 CopyRegisterLine(&reg_table->saved_line_, work_line,
1437 reg_table->insn_reg_count_plus_);
1438 } else {
1439#ifndef NDEBUG
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001440 memset(reg_table->saved_line_.reg_types_.get(), 0xdd,
jeffhaobdb76512011-09-07 11:43:16 -07001441 reg_table->insn_reg_count_plus_ * sizeof(RegType));
1442#endif
1443 }
1444
1445 switch (dec_insn.opcode_) {
1446 case Instruction::NOP:
1447 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001448 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001449 * a signature that looks like a NOP; if we see one of these in
1450 * the course of executing code then we have a problem.
1451 */
1452 if (dec_insn.vA_ != 0) {
1453 LOG(ERROR) << "VFY: encountered data table in instruction stream";
1454 failure = VERIFY_ERROR_GENERIC;
1455 }
1456 break;
1457
1458 case Instruction::MOVE:
1459 case Instruction::MOVE_FROM16:
1460 case Instruction::MOVE_16:
1461 CopyRegister1(work_line, dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr,
1462 &failure);
1463 break;
1464 case Instruction::MOVE_WIDE:
1465 case Instruction::MOVE_WIDE_FROM16:
1466 case Instruction::MOVE_WIDE_16:
1467 CopyRegister2(work_line, dec_insn.vA_, dec_insn.vB_, &failure);
1468 break;
1469 case Instruction::MOVE_OBJECT:
1470 case Instruction::MOVE_OBJECT_FROM16:
1471 case Instruction::MOVE_OBJECT_16:
1472 CopyRegister1(work_line, dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef,
1473 &failure);
1474 break;
1475
1476 /*
1477 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001478 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001479 * might want to hold the result in an actual CPU register, so the
1480 * Dalvik spec requires that these only appear immediately after an
1481 * invoke or filled-new-array.
1482 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001483 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001484 * redundant with the reset done below, but it can make the debug info
1485 * easier to read in some cases.)
1486 */
1487 case Instruction::MOVE_RESULT:
1488 CopyResultRegister1(work_line, registers_size, dec_insn.vA_,
1489 kTypeCategory1nr, &failure);
1490 break;
1491 case Instruction::MOVE_RESULT_WIDE:
1492 CopyResultRegister2(work_line, registers_size, dec_insn.vA_, &failure);
1493 break;
1494 case Instruction::MOVE_RESULT_OBJECT:
1495 CopyResultRegister1(work_line, registers_size, dec_insn.vA_,
1496 kTypeCategoryRef, &failure);
1497 break;
1498
1499 case Instruction::MOVE_EXCEPTION:
1500 /*
1501 * This statement can only appear as the first instruction in an
1502 * exception handler (though not all exception handlers need to
jeffhaod1f0fde2011-09-08 17:25:33 -07001503 * have one of these). We verify that as part of extracting the
jeffhaobdb76512011-09-07 11:43:16 -07001504 * exception type from the catch block list.
1505 *
1506 * "res_class" will hold the closest common superclass of all
1507 * exceptions that can be handled here.
1508 */
1509 res_class = GetCaughtExceptionType(vdata, insn_idx, &failure);
1510 if (res_class == NULL) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001511 DCHECK(failure != VERIFY_ERROR_NONE);
jeffhaobdb76512011-09-07 11:43:16 -07001512 } else {
1513 SetRegisterType(work_line, dec_insn.vA_, RegTypeFromClass(res_class));
1514 }
1515 break;
1516
1517 case Instruction::RETURN_VOID:
1518 if (!CheckConstructorReturn(method, work_line, registers_size)) {
1519 failure = VERIFY_ERROR_GENERIC;
1520 } else if (GetMethodReturnType(dex_file, method) != kRegTypeUnknown) {
1521 LOG(ERROR) << "VFY: return-void not expected";
1522 failure = VERIFY_ERROR_GENERIC;
1523 }
1524 break;
1525 case Instruction::RETURN:
1526 if (!CheckConstructorReturn(method, work_line, registers_size)) {
1527 failure = VERIFY_ERROR_GENERIC;
1528 } else {
1529 /* check the method signature */
1530 RegType return_type = GetMethodReturnType(dex_file, method);
1531 CheckTypeCategory(return_type, kTypeCategory1nr, &failure);
1532 if (failure != VERIFY_ERROR_NONE)
1533 LOG(ERROR) << "VFY: return-1nr not expected";
1534
1535 /*
1536 * compiler may generate synthetic functions that write byte
1537 * values into boolean fields. Also, it may use integer values
1538 * for boolean, byte, short, and character return types.
1539 */
1540 RegType src_type = GetRegisterType(work_line, dec_insn.vA_);
1541 if ((return_type == kRegTypeBoolean && src_type == kRegTypeByte) ||
1542 ((return_type == kRegTypeBoolean || return_type == kRegTypeByte ||
1543 return_type == kRegTypeShort || return_type == kRegTypeChar) &&
1544 src_type == kRegTypeInteger))
1545 return_type = src_type;
1546
1547 /* check the register contents */
1548 VerifyRegisterType(work_line, dec_insn.vA_, return_type, &failure);
1549 if (failure != VERIFY_ERROR_NONE) {
1550 LOG(ERROR) << "VFY: return-1nr on invalid register v" << dec_insn.vA_;
1551 }
1552 }
1553 break;
1554 case Instruction::RETURN_WIDE:
1555 if (!CheckConstructorReturn(method, work_line, registers_size)) {
1556 failure = VERIFY_ERROR_GENERIC;
1557 } else {
1558 RegType return_type;
1559
1560 /* check the method signature */
1561 return_type = GetMethodReturnType(dex_file, method);
1562 CheckTypeCategory(return_type, kTypeCategory2, &failure);
1563 if (failure != VERIFY_ERROR_NONE)
1564 LOG(ERROR) << "VFY: return-wide not expected";
1565
1566 /* check the register contents */
1567 VerifyRegisterType(work_line, dec_insn.vA_, return_type, &failure);
1568 if (failure != VERIFY_ERROR_NONE) {
1569 LOG(ERROR) << "VFY: return-wide on invalid register pair v"
1570 << dec_insn.vA_;
1571 }
1572 }
1573 break;
1574 case Instruction::RETURN_OBJECT:
1575 if (!CheckConstructorReturn(method, work_line, registers_size)) {
1576 failure = VERIFY_ERROR_GENERIC;
1577 } else {
1578 RegType return_type = GetMethodReturnType(dex_file, method);
1579 CheckTypeCategory(return_type, kTypeCategoryRef, &failure);
1580 if (failure != VERIFY_ERROR_NONE) {
1581 LOG(ERROR) << "VFY: return-object not expected";
1582 break;
1583 }
1584
1585 /* return_type is the *expected* return type, not register value */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001586 DCHECK(return_type != kRegTypeZero);
1587 DCHECK(!RegTypeIsUninitReference(return_type));
jeffhaobdb76512011-09-07 11:43:16 -07001588
1589 /*
1590 * Verify that the reference in vAA is an instance of the type
jeffhaod1f0fde2011-09-08 17:25:33 -07001591 * in "return_type". The Zero type is allowed here. If the
jeffhaobdb76512011-09-07 11:43:16 -07001592 * method is declared to return an interface, then any
1593 * initialized reference is acceptable.
1594 *
1595 * Note GetClassFromRegister fails if the register holds an
1596 * uninitialized reference, so we do not allow them to be
1597 * returned.
1598 */
1599 Class* decl_class = RegTypeInitializedReferenceToClass(return_type);
1600 res_class = GetClassFromRegister(work_line, dec_insn.vA_, &failure);
1601 if (failure != VERIFY_ERROR_NONE)
1602 break;
1603 if (res_class != NULL) {
1604 if (!decl_class->IsInterface() &&
jeffhaobdb76512011-09-07 11:43:16 -07001605 !decl_class->IsAssignableFrom(res_class)) {
1606 LOG(ERROR) << "VFY: returning " << std::hex
1607 << res_class->GetDescriptor()->ToModifiedUtf8()
1608 << " (cl=0x" << (int) res_class->GetClassLoader()
1609 << "), declared "
1610 << decl_class->GetDescriptor()->ToModifiedUtf8()
1611 << " (cl=0x" << (int) decl_class->GetClassLoader()
1612 << ")" << std::dec;
1613 failure = VERIFY_ERROR_GENERIC;
1614 break;
1615 }
1616 }
1617 }
1618 break;
1619
1620 case Instruction::CONST_4:
1621 case Instruction::CONST_16:
1622 case Instruction::CONST:
1623 /* could be boolean, int, float, or a null reference */
1624 SetRegisterType(work_line, dec_insn.vA_,
1625 DetermineCat1Const((int32_t) dec_insn.vB_));
1626 break;
1627 case Instruction::CONST_HIGH16:
1628 /* could be boolean, int, float, or a null reference */
1629 SetRegisterType(work_line, dec_insn.vA_,
1630 DetermineCat1Const((int32_t) dec_insn.vB_ << 16));
1631 break;
1632 case Instruction::CONST_WIDE_16:
1633 case Instruction::CONST_WIDE_32:
1634 case Instruction::CONST_WIDE:
1635 case Instruction::CONST_WIDE_HIGH16:
1636 /* could be long or double; resolved upon use */
1637 SetRegisterType(work_line, dec_insn.vA_, kRegTypeConstLo);
1638 break;
1639 case Instruction::CONST_STRING:
1640 case Instruction::CONST_STRING_JUMBO:
1641 SetRegisterType(work_line, dec_insn.vA_, RegTypeFromClass(
1642 class_linker->FindSystemClass("Ljava/lang/String;")));
1643 break;
1644 case Instruction::CONST_CLASS:
1645 /* make sure we can resolve the class; access check is important */
jeffhao98eacac2011-09-14 16:11:53 -07001646 res_class = ResolveClassAndCheckAccess(dex_file, dec_insn.vB_, klass, &failure);
jeffhaobdb76512011-09-07 11:43:16 -07001647 if (res_class == NULL) {
1648 const char* bad_class_desc = dex_file->dexStringByTypeIdx(dec_insn.vB_);
1649 LOG(ERROR) << "VFY: unable to resolve const-class " << dec_insn.vB_
1650 << " (" << bad_class_desc << ") in "
1651 << klass->GetDescriptor()->ToModifiedUtf8();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001652 DCHECK(failure != VERIFY_ERROR_GENERIC);
jeffhaobdb76512011-09-07 11:43:16 -07001653 } else {
1654 SetRegisterType(work_line, dec_insn.vA_, RegTypeFromClass(
1655 class_linker->FindSystemClass("Ljava/lang/Class;")));
1656 }
1657 break;
1658
1659 case Instruction::MONITOR_ENTER:
1660 HandleMonitorEnter(work_line, dec_insn.vA_, insn_idx, &failure);
1661 break;
1662 case Instruction::MONITOR_EXIT:
1663 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001664 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001665 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001666 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001667 * to the need to handle asynchronous exceptions, a now-deprecated
1668 * feature that Dalvik doesn't support.)
1669 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001670 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001671 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001672 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001673 * structured locking checks are working, the former would have
1674 * failed on the -enter instruction, and the latter is impossible.
1675 *
1676 * This is fortunate, because issue 3221411 prevents us from
1677 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001678 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001679 * some catch blocks (which will show up as "dead" code when
1680 * we skip them here); if we can't, then the code path could be
1681 * "live" so we still need to check it.
1682 */
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001683 if (work_line->monitor_entries_.get() != NULL)
jeffhaobdb76512011-09-07 11:43:16 -07001684 opcode_flag &= ~Instruction::kThrow;
1685 HandleMonitorExit(work_line, dec_insn.vA_, insn_idx, &failure);
1686 break;
1687
1688 case Instruction::CHECK_CAST:
1689 /*
1690 * If this instruction succeeds, we will promote register vA to
jeffhaod1f0fde2011-09-08 17:25:33 -07001691 * the type in vB. (This could be a demotion -- not expected, so
jeffhaobdb76512011-09-07 11:43:16 -07001692 * we don't try to address it.)
1693 *
1694 * If it fails, an exception is thrown, which we deal with later
1695 * by ignoring the update to dec_insn.vA_ when branching to a handler.
1696 */
jeffhao98eacac2011-09-14 16:11:53 -07001697 res_class = ResolveClassAndCheckAccess(dex_file, dec_insn.vB_, klass, &failure);
jeffhaobdb76512011-09-07 11:43:16 -07001698 if (res_class == NULL) {
1699 const char* bad_class_desc = dex_file->dexStringByTypeIdx(dec_insn.vB_);
1700 LOG(ERROR) << "VFY: unable to resolve check-cast " << dec_insn.vB_
1701 << " (" << bad_class_desc << ") in "
1702 << klass->GetDescriptor()->ToModifiedUtf8();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001703 DCHECK(failure != VERIFY_ERROR_GENERIC);
jeffhaobdb76512011-09-07 11:43:16 -07001704 } else {
1705 RegType orig_type;
1706
1707 orig_type = GetRegisterType(work_line, dec_insn.vA_);
1708 if (!RegTypeIsReference(orig_type)) {
1709 LOG(ERROR) << "VFY: check-cast on non-reference in v" << dec_insn.vA_;
1710 failure = VERIFY_ERROR_GENERIC;
1711 break;
1712 }
1713 SetRegisterType(work_line, dec_insn.vA_, RegTypeFromClass(res_class));
1714 }
1715 break;
1716 case Instruction::INSTANCE_OF:
1717 /* make sure we're checking a reference type */
1718 tmp_type = GetRegisterType(work_line, dec_insn.vB_);
1719 if (!RegTypeIsReference(tmp_type)) {
1720 LOG(ERROR) << "VFY: vB not a reference (" << tmp_type << ")";
1721 failure = VERIFY_ERROR_GENERIC;
1722 break;
1723 }
1724
1725 /* make sure we can resolve the class; access check is important */
jeffhao98eacac2011-09-14 16:11:53 -07001726 res_class = ResolveClassAndCheckAccess(dex_file, dec_insn.vC_, klass, &failure);
jeffhaobdb76512011-09-07 11:43:16 -07001727 if (res_class == NULL) {
1728 const char* bad_class_desc = dex_file->dexStringByTypeIdx(dec_insn.vC_);
1729 LOG(ERROR) << "VFY: unable to resolve instanceof " << dec_insn.vC_
1730 << " (" << bad_class_desc << ") in "
1731 << klass->GetDescriptor()->ToModifiedUtf8();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001732 DCHECK(failure != VERIFY_ERROR_GENERIC);
jeffhaobdb76512011-09-07 11:43:16 -07001733 } else {
1734 /* result is boolean */
1735 SetRegisterType(work_line, dec_insn.vA_, kRegTypeBoolean);
1736 }
1737 break;
1738
1739 case Instruction::ARRAY_LENGTH:
1740 res_class = GetClassFromRegister(work_line, dec_insn.vB_, &failure);
1741 if (failure != VERIFY_ERROR_NONE)
1742 break;
1743 if (res_class != NULL && !res_class->IsArrayClass()) {
1744 LOG(ERROR) << "VFY: array-length on non-array";
1745 failure = VERIFY_ERROR_GENERIC;
1746 break;
1747 }
1748 SetRegisterType(work_line, dec_insn.vA_, kRegTypeInteger);
1749 break;
1750
1751 case Instruction::NEW_INSTANCE:
jeffhao98eacac2011-09-14 16:11:53 -07001752 res_class = ResolveClassAndCheckAccess(dex_file, dec_insn.vB_, klass, &failure);
jeffhaobdb76512011-09-07 11:43:16 -07001753 if (res_class == NULL) {
1754 const char* bad_class_desc = dex_file->dexStringByTypeIdx(dec_insn.vB_);
1755 LOG(ERROR) << "VFY: unable to resolve new-instance " << dec_insn.vB_
1756 << " (" << bad_class_desc << ") in "
1757 << klass->GetDescriptor()->ToModifiedUtf8();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001758 DCHECK(failure != VERIFY_ERROR_GENERIC);
jeffhaobdb76512011-09-07 11:43:16 -07001759 } else {
1760 RegType uninit_type;
1761
1762 /* can't create an instance of an interface or abstract class */
1763 if (res_class->IsAbstract() || res_class->IsInterface()) {
1764 LOG(ERROR) << "VFY: new-instance on interface or abstract class"
1765 << res_class->GetDescriptor()->ToModifiedUtf8();
1766 failure = VERIFY_ERROR_INSTANTIATION;
1767 break;
1768 }
1769
1770 /* add resolved class to uninit map if not already there */
1771 int uidx = SetUninitInstance(uninit_map, insn_idx, res_class);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001772 DCHECK_GE(uidx, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001773 uninit_type = RegTypeFromUninitIndex(uidx);
1774
1775 /*
1776 * Any registers holding previous allocations from this address
1777 * that have not yet been initialized must be marked invalid.
1778 */
1779 MarkUninitRefsAsInvalid(work_line, registers_size, uninit_map,
1780 uninit_type);
1781
1782 /* add the new uninitialized reference to the register ste */
1783 SetRegisterType(work_line, dec_insn.vA_, uninit_type);
1784 }
1785 break;
1786 case Instruction::NEW_ARRAY:
jeffhao98eacac2011-09-14 16:11:53 -07001787 res_class = ResolveClassAndCheckAccess(dex_file, dec_insn.vC_, klass, &failure);
jeffhaobdb76512011-09-07 11:43:16 -07001788 if (res_class == NULL) {
1789 const char* bad_class_desc = dex_file->dexStringByTypeIdx(dec_insn.vC_);
1790 LOG(ERROR) << "VFY: unable to resolve new-array " << dec_insn.vC_
1791 << " (" << bad_class_desc << ") in "
1792 << klass->GetDescriptor()->ToModifiedUtf8();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001793 DCHECK(failure != VERIFY_ERROR_GENERIC);
jeffhaobdb76512011-09-07 11:43:16 -07001794 } else if (!res_class->IsArrayClass()) {
1795 LOG(ERROR) << "VFY: new-array on non-array class";
1796 failure = VERIFY_ERROR_GENERIC;
1797 } else {
1798 /* make sure "size" register is valid type */
1799 VerifyRegisterType(work_line, dec_insn.vB_, kRegTypeInteger, &failure);
1800 /* set register type to array class */
1801 SetRegisterType(work_line, dec_insn.vA_, RegTypeFromClass(res_class));
1802 }
1803 break;
1804 case Instruction::FILLED_NEW_ARRAY:
1805 case Instruction::FILLED_NEW_ARRAY_RANGE:
jeffhao98eacac2011-09-14 16:11:53 -07001806 res_class = ResolveClassAndCheckAccess(dex_file, dec_insn.vB_, klass, &failure);
jeffhaobdb76512011-09-07 11:43:16 -07001807 if (res_class == NULL) {
1808 const char* bad_class_desc = dex_file->dexStringByTypeIdx(dec_insn.vB_);
1809 LOG(ERROR) << "VFY: unable to resolve filled-array " << dec_insn.vB_
1810 << " (" << bad_class_desc << ") in "
1811 << klass->GetDescriptor()->ToModifiedUtf8();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001812 DCHECK(failure != VERIFY_ERROR_GENERIC);
jeffhaobdb76512011-09-07 11:43:16 -07001813 } else if (!res_class->IsArrayClass()) {
1814 LOG(ERROR) << "VFY: filled-new-array on non-array class";
1815 failure = VERIFY_ERROR_GENERIC;
1816 } else {
1817 bool is_range = (dec_insn.opcode_ ==
1818 Instruction::FILLED_NEW_ARRAY_RANGE);
1819
1820 /* check the arguments to the instruction */
1821 VerifyFilledNewArrayRegs(method, work_line, &dec_insn, res_class,
1822 is_range, &failure);
1823 /* filled-array result goes into "result" register */
1824 SetResultRegisterType(work_line, registers_size,
1825 RegTypeFromClass(res_class));
1826 just_set_result = true;
1827 }
1828 break;
1829
1830 case Instruction::CMPL_FLOAT:
1831 case Instruction::CMPG_FLOAT:
1832 VerifyRegisterType(work_line, dec_insn.vB_, kRegTypeFloat, &failure);
1833 VerifyRegisterType(work_line, dec_insn.vC_, kRegTypeFloat, &failure);
1834 SetRegisterType(work_line, dec_insn.vA_, kRegTypeBoolean);
1835 break;
1836 case Instruction::CMPL_DOUBLE:
1837 case Instruction::CMPG_DOUBLE:
1838 VerifyRegisterType(work_line, dec_insn.vB_, kRegTypeDoubleLo, &failure);
1839 VerifyRegisterType(work_line, dec_insn.vC_, kRegTypeDoubleLo, &failure);
1840 SetRegisterType(work_line, dec_insn.vA_, kRegTypeBoolean);
1841 break;
1842 case Instruction::CMP_LONG:
1843 VerifyRegisterType(work_line, dec_insn.vB_, kRegTypeLongLo, &failure);
1844 VerifyRegisterType(work_line, dec_insn.vC_, kRegTypeLongLo, &failure);
1845 SetRegisterType(work_line, dec_insn.vA_, kRegTypeBoolean);
1846 break;
1847
1848 case Instruction::THROW:
1849 res_class = GetClassFromRegister(work_line, dec_insn.vA_, &failure);
1850 if (failure == VERIFY_ERROR_NONE && res_class != NULL) {
1851 Class* throwable_class =
1852 class_linker->FindSystemClass("Ljava/lang/Throwable;");
1853 if (!throwable_class->IsAssignableFrom(res_class)) {
1854 LOG(ERROR) << "VFY: thrown class "
1855 << res_class->GetDescriptor()->ToModifiedUtf8()
1856 << " not instanceof Throwable",
1857 failure = VERIFY_ERROR_GENERIC;
1858 }
1859 }
1860 break;
1861
1862 case Instruction::GOTO:
1863 case Instruction::GOTO_16:
1864 case Instruction::GOTO_32:
1865 /* no effect on or use of registers */
1866 break;
1867
1868 case Instruction::PACKED_SWITCH:
1869 case Instruction::SPARSE_SWITCH:
1870 /* verify that vAA is an integer, or can be converted to one */
1871 VerifyRegisterType(work_line, dec_insn.vA_, kRegTypeInteger, &failure);
1872 break;
1873
1874 case Instruction::FILL_ARRAY_DATA:
1875 {
1876 RegType value_type;
1877 const uint16_t *array_data;
1878 uint16_t elem_width;
1879
1880 /* Similar to the verification done for APUT */
1881 res_class = GetClassFromRegister(work_line, dec_insn.vA_, &failure);
1882 if (failure != VERIFY_ERROR_NONE)
1883 break;
1884
1885 /* res_class can be null if the reg type is Zero */
1886 if (res_class == NULL)
1887 break;
1888
1889 Class::PrimitiveType prim_type =
1890 res_class->GetComponentType()->GetPrimitiveType();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001891 if (!res_class->IsArrayClass() ||
jeffhaobdb76512011-09-07 11:43:16 -07001892 prim_type == Class::kPrimNot || prim_type == Class::kPrimVoid) {
1893 LOG(ERROR) << "VFY: invalid fill-array-data on " <<
1894 res_class->GetDescriptor()->ToModifiedUtf8();
1895 failure = VERIFY_ERROR_GENERIC;
1896 break;
1897 }
1898
1899 value_type = PrimitiveTypeToRegType(prim_type);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001900 DCHECK(value_type != kRegTypeUnknown);
jeffhaobdb76512011-09-07 11:43:16 -07001901
1902 /*
1903 * Now verify if the element width in the table matches the element
1904 * width declared in the array
1905 */
1906 array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1907 if (array_data[0] != Instruction::kArrayDataSignature) {
1908 LOG(ERROR) << "VFY: invalid magic for array-data";
1909 failure = VERIFY_ERROR_GENERIC;
1910 break;
1911 }
1912
1913 switch (prim_type) {
1914 case Class::kPrimBoolean:
1915 case Class::kPrimByte:
1916 elem_width = 1;
1917 break;
1918 case Class::kPrimChar:
1919 case Class::kPrimShort:
1920 elem_width = 2;
1921 break;
1922 case Class::kPrimFloat:
1923 case Class::kPrimInt:
1924 elem_width = 4;
1925 break;
1926 case Class::kPrimDouble:
1927 case Class::kPrimLong:
1928 elem_width = 8;
1929 break;
1930 default:
1931 elem_width = 0;
1932 break;
1933 }
1934
1935 /*
1936 * Since we don't compress the data in Dex, expect to see equal
1937 * width of data stored in the table and expected from the array
1938 * class.
1939 */
1940 if (array_data[1] != elem_width) {
1941 LOG(ERROR) << "VFY: array-data size mismatch (" << array_data[1]
1942 << " vs " << elem_width << ")";
1943 failure = VERIFY_ERROR_GENERIC;
1944 }
1945 }
1946 break;
1947
1948 case Instruction::IF_EQ:
1949 case Instruction::IF_NE:
1950 {
1951 RegType type1, type2;
1952
1953 type1 = GetRegisterType(work_line, dec_insn.vA_);
1954 type2 = GetRegisterType(work_line, dec_insn.vB_);
1955
1956 /* both references? */
1957 if (RegTypeIsReference(type1) && RegTypeIsReference(type2))
1958 break;
1959
1960 /* both category-1nr? */
1961 CheckTypeCategory(type1, kTypeCategory1nr, &failure);
1962 CheckTypeCategory(type2, kTypeCategory1nr, &failure);
1963 if (failure != VERIFY_ERROR_NONE) {
1964 LOG(ERROR) << "VFY: args to if-eq/if-ne must both be refs or cat1";
1965 break;
1966 }
1967 }
1968 break;
1969 case Instruction::IF_LT:
1970 case Instruction::IF_GE:
1971 case Instruction::IF_GT:
1972 case Instruction::IF_LE:
1973 tmp_type = GetRegisterType(work_line, dec_insn.vA_);
1974 CheckTypeCategory(tmp_type, kTypeCategory1nr, &failure);
1975 if (failure != VERIFY_ERROR_NONE) {
1976 LOG(ERROR) << "VFY: args to 'if' must be cat-1nr";
1977 break;
1978 }
1979 tmp_type = GetRegisterType(work_line, dec_insn.vB_);
1980 CheckTypeCategory(tmp_type, kTypeCategory1nr, &failure);
1981 if (failure != VERIFY_ERROR_NONE) {
1982 LOG(ERROR) << "VFY: args to 'if' must be cat-1nr";
1983 break;
1984 }
1985 break;
1986 case Instruction::IF_EQZ:
1987 case Instruction::IF_NEZ:
1988 tmp_type = GetRegisterType(work_line, dec_insn.vA_);
1989 if (RegTypeIsReference(tmp_type))
1990 break;
1991 CheckTypeCategory(tmp_type, kTypeCategory1nr, &failure);
1992 if (failure != VERIFY_ERROR_NONE)
1993 LOG(ERROR) << "VFY: expected cat-1 arg to if";
1994 break;
1995 case Instruction::IF_LTZ:
1996 case Instruction::IF_GEZ:
1997 case Instruction::IF_GTZ:
1998 case Instruction::IF_LEZ:
1999 tmp_type = GetRegisterType(work_line, dec_insn.vA_);
2000 CheckTypeCategory(tmp_type, kTypeCategory1nr, &failure);
2001 if (failure != VERIFY_ERROR_NONE)
2002 LOG(ERROR) << "VFY: expected cat-1 arg to if";
2003 break;
2004
2005 case Instruction::AGET:
2006 tmp_type = kRegTypeConstInteger;
2007 goto aget_1nr_common;
2008 case Instruction::AGET_BOOLEAN:
2009 tmp_type = kRegTypeBoolean;
2010 goto aget_1nr_common;
2011 case Instruction::AGET_BYTE:
2012 tmp_type = kRegTypeByte;
2013 goto aget_1nr_common;
2014 case Instruction::AGET_CHAR:
2015 tmp_type = kRegTypeChar;
2016 goto aget_1nr_common;
2017 case Instruction::AGET_SHORT:
2018 tmp_type = kRegTypeShort;
2019 goto aget_1nr_common;
2020aget_1nr_common:
2021 {
2022 RegType src_type, index_type;
2023
2024 index_type = GetRegisterType(work_line, dec_insn.vC_);
2025 CheckArrayIndexType(method, index_type, &failure);
2026 if (failure != VERIFY_ERROR_NONE)
2027 break;
2028
2029 res_class = GetClassFromRegister(work_line, dec_insn.vB_, &failure);
2030 if (failure != VERIFY_ERROR_NONE)
2031 break;
2032 if (res_class != NULL) {
2033 /* verify the class */
2034 Class::PrimitiveType prim_type =
2035 res_class->GetComponentType()->GetPrimitiveType();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002036 if (!res_class->IsArrayClass() || prim_type == Class::kPrimNot) {
jeffhaobdb76512011-09-07 11:43:16 -07002037 LOG(ERROR) << "VFY: invalid aget-1nr target "
2038 << res_class->GetDescriptor()->ToModifiedUtf8();
2039 failure = VERIFY_ERROR_GENERIC;
2040 break;
2041 }
2042
2043 /* make sure array type matches instruction */
2044 src_type = PrimitiveTypeToRegType(prim_type);
2045
2046 /* differentiate between float and int */
2047 if (src_type == kRegTypeFloat || src_type == kRegTypeInteger)
2048 tmp_type = src_type;
2049
2050 if (tmp_type != src_type) {
2051 LOG(ERROR) << "VFY: invalid aget-1nr, array type=" << src_type
2052 << " with inst type=" << tmp_type << " (on "
2053 << res_class->GetDescriptor()->ToModifiedUtf8() << ")";
2054 failure = VERIFY_ERROR_GENERIC;
2055 break;
2056 }
2057
2058 }
2059 SetRegisterType(work_line, dec_insn.vA_, tmp_type);
2060 }
2061 break;
2062
2063 case Instruction::AGET_WIDE:
2064 {
2065 RegType dst_type, index_type;
2066
2067 index_type = GetRegisterType(work_line, dec_insn.vC_);
2068 CheckArrayIndexType(method, index_type, &failure);
2069 if (failure != VERIFY_ERROR_NONE)
2070 break;
2071
2072 res_class = GetClassFromRegister(work_line, dec_insn.vB_, &failure);
2073 if (failure != VERIFY_ERROR_NONE)
2074 break;
2075 if (res_class != NULL) {
2076 /* verify the class */
2077 Class::PrimitiveType prim_type =
2078 res_class->GetComponentType()->GetPrimitiveType();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002079 if (!res_class->IsArrayClass() || prim_type == Class::kPrimNot) {
jeffhaobdb76512011-09-07 11:43:16 -07002080 LOG(ERROR) << "VFY: invalid aget-wide target "
2081 << res_class->GetDescriptor()->ToModifiedUtf8();
2082 failure = VERIFY_ERROR_GENERIC;
2083 break;
2084 }
2085
2086 /* try to refine "dst_type" */
2087 switch (prim_type) {
2088 case Class::kPrimLong:
2089 dst_type = kRegTypeLongLo;
2090 break;
2091 case Class::kPrimDouble:
2092 dst_type = kRegTypeDoubleLo;
2093 break;
2094 default:
2095 LOG(ERROR) << "VFY: invalid aget-wide on "
2096 << res_class->GetDescriptor()->ToModifiedUtf8();
2097 dst_type = kRegTypeUnknown;
2098 failure = VERIFY_ERROR_GENERIC;
2099 break;
2100 }
2101 } else {
2102 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002103 * Null array ref; this code path will fail at runtime. We
jeffhaobdb76512011-09-07 11:43:16 -07002104 * know this is either long or double, so label it const.
2105 */
2106 dst_type = kRegTypeConstLo;
2107 }
2108 SetRegisterType(work_line, dec_insn.vA_, dst_type);
2109 }
2110 break;
2111
2112 case Instruction::AGET_OBJECT:
2113 {
2114 RegType dst_type, index_type;
2115
2116 index_type = GetRegisterType(work_line, dec_insn.vC_);
2117 CheckArrayIndexType(method, index_type, &failure);
2118 if (failure != VERIFY_ERROR_NONE)
2119 break;
2120
2121 /* get the class of the array we're pulling an object from */
2122 res_class = GetClassFromRegister(work_line, dec_insn.vB_, &failure);
2123 if (failure != VERIFY_ERROR_NONE)
2124 break;
2125 if (res_class != NULL) {
2126 Class* element_class;
2127
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002128 DCHECK(res_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07002129 if (!res_class->IsArrayClass()) {
2130 LOG(ERROR) << "VFY: aget-object on non-array class";
2131 failure = VERIFY_ERROR_GENERIC;
2132 break;
2133 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002134 DCHECK(res_class->GetComponentType() != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07002135
2136 /*
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002137 * Find the element class.
jeffhaobdb76512011-09-07 11:43:16 -07002138 */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002139 element_class = res_class->GetComponentType();
2140 if (element_class->IsPrimitive()) {
jeffhaobdb76512011-09-07 11:43:16 -07002141 LOG(ERROR) << "VFY: aget-object on non-ref array class ("
2142 << res_class->GetDescriptor()->ToModifiedUtf8() << ")";
2143 failure = VERIFY_ERROR_GENERIC;
2144 break;
2145 }
2146
2147 dst_type = RegTypeFromClass(element_class);
2148 } else {
2149 /*
2150 * The array reference is NULL, so the current code path will
jeffhaod1f0fde2011-09-08 17:25:33 -07002151 * throw an exception. For proper merging with later code
jeffhaobdb76512011-09-07 11:43:16 -07002152 * paths, and correct handling of "if-eqz" tests on the
2153 * result of the array get, we want to treat this as a null
2154 * reference.
2155 */
2156 dst_type = kRegTypeZero;
2157 }
2158 SetRegisterType(work_line, dec_insn.vA_, dst_type);
2159 }
2160 break;
2161 case Instruction::APUT:
2162 tmp_type = kRegTypeInteger;
2163 goto aput_1nr_common;
2164 case Instruction::APUT_BOOLEAN:
2165 tmp_type = kRegTypeBoolean;
2166 goto aput_1nr_common;
2167 case Instruction::APUT_BYTE:
2168 tmp_type = kRegTypeByte;
2169 goto aput_1nr_common;
2170 case Instruction::APUT_CHAR:
2171 tmp_type = kRegTypeChar;
2172 goto aput_1nr_common;
2173 case Instruction::APUT_SHORT:
2174 tmp_type = kRegTypeShort;
2175 goto aput_1nr_common;
2176aput_1nr_common:
2177 {
2178 RegType src_type, dst_type, index_type;
2179
2180 index_type = GetRegisterType(work_line, dec_insn.vC_);
2181 CheckArrayIndexType(method, index_type, &failure);
2182 if (failure != VERIFY_ERROR_NONE)
2183 break;
2184
2185 res_class = GetClassFromRegister(work_line, dec_insn.vB_, &failure);
2186 if (failure != VERIFY_ERROR_NONE)
2187 break;
2188
2189 /* res_class can be null if the reg type is Zero */
2190 if (res_class == NULL)
2191 break;
2192
2193 Class::PrimitiveType prim_type =
2194 res_class->GetComponentType()->GetPrimitiveType();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002195 if (!res_class->IsArrayClass() || prim_type == Class::kPrimNot) {
jeffhaobdb76512011-09-07 11:43:16 -07002196 LOG(ERROR) << "VFY: invalid aput-1nr on "
2197 << res_class->GetDescriptor()->ToModifiedUtf8();
2198 failure = VERIFY_ERROR_GENERIC;
2199 break;
2200 }
2201
2202 /* verify that instruction matches array */
2203 dst_type = PrimitiveTypeToRegType(prim_type);
2204
2205 /* correct if float */
2206 if (dst_type == kRegTypeFloat)
2207 tmp_type = kRegTypeFloat;
2208
2209 /* make sure the source register has the correct type */
2210 src_type = GetRegisterType(work_line, dec_insn.vA_);
2211 if (!CanConvertTo1nr(src_type, tmp_type)) {
2212 LOG(ERROR) << "VFY: invalid reg type " << src_type
2213 << " on aput instr (need " << tmp_type << ")";
2214 failure = VERIFY_ERROR_GENERIC;
2215 break;
2216 }
2217
2218 VerifyRegisterType(work_line, dec_insn.vA_, dst_type, &failure);
2219
2220 if (failure != VERIFY_ERROR_NONE || dst_type == kRegTypeUnknown ||
2221 tmp_type != dst_type) {
2222 LOG(ERROR) << "VFY: invalid aput-1nr on "
2223 << res_class->GetDescriptor()->ToModifiedUtf8()
2224 << " (inst=" << tmp_type << " dst=" << dst_type << ")";
2225 failure = VERIFY_ERROR_GENERIC;
2226 break;
2227 }
2228 }
2229 break;
2230 case Instruction::APUT_WIDE:
2231 tmp_type = GetRegisterType(work_line, dec_insn.vC_);
2232 CheckArrayIndexType(method, tmp_type, &failure);
2233 if (failure != VERIFY_ERROR_NONE)
2234 break;
2235
2236 res_class = GetClassFromRegister(work_line, dec_insn.vB_, &failure);
2237 if (failure != VERIFY_ERROR_NONE)
2238 break;
2239 if (res_class != NULL) {
2240 Class::PrimitiveType prim_type =
2241 res_class->GetComponentType()->GetPrimitiveType();
2242 /* verify the class and try to refine "dst_type" */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002243 if (!res_class->IsArrayClass() || prim_type == Class::kPrimNot)
jeffhaobdb76512011-09-07 11:43:16 -07002244 {
2245 LOG(ERROR) << "VFY: invalid aput-wide on "
2246 << res_class->GetDescriptor()->ToModifiedUtf8();
2247 failure = VERIFY_ERROR_GENERIC;
2248 break;
2249 }
2250
2251 switch (prim_type) {
2252 case Class::kPrimLong:
2253 VerifyRegisterType(work_line, dec_insn.vA_, kRegTypeLongLo,
2254 &failure);
2255 break;
2256 case Class::kPrimDouble:
2257 VerifyRegisterType(work_line, dec_insn.vA_, kRegTypeDoubleLo,
2258 &failure);
2259 break;
2260 default:
2261 LOG(ERROR) << "VFY: invalid aput-wide on "
2262 << res_class->GetDescriptor()->ToModifiedUtf8();
2263 failure = VERIFY_ERROR_GENERIC;
2264 break;
2265 }
2266 }
2267 break;
2268 case Instruction::APUT_OBJECT:
2269 tmp_type = GetRegisterType(work_line, dec_insn.vC_);
2270 CheckArrayIndexType(method, tmp_type, &failure);
2271 if (failure != VERIFY_ERROR_NONE)
2272 break;
2273
2274 /* get the ref we're storing; Zero is okay, Uninit is not */
2275 res_class = GetClassFromRegister(work_line, dec_insn.vA_, &failure);
2276 if (failure != VERIFY_ERROR_NONE)
2277 break;
2278 if (res_class != NULL) {
2279 Class* array_class;
2280 Class* element_class;
2281
2282 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002283 * Get the array class. If the array ref is null, we won't
jeffhaobdb76512011-09-07 11:43:16 -07002284 * have type information (and we'll crash at runtime with a
2285 * null pointer exception).
2286 */
2287 array_class = GetClassFromRegister(work_line, dec_insn.vB_, &failure);
2288
2289 if (array_class != NULL) {
2290 /* see if the array holds a compatible type */
2291 if (!array_class->IsArrayClass()) {
2292 LOG(ERROR) << "VFY: invalid aput-object on "
2293 << array_class->GetDescriptor()->ToModifiedUtf8();
2294 failure = VERIFY_ERROR_GENERIC;
2295 break;
2296 }
2297
2298 /*
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002299 * Find the element class.
jeffhaobdb76512011-09-07 11:43:16 -07002300 *
2301 * All we want to check here is that the element type is a
jeffhaod1f0fde2011-09-08 17:25:33 -07002302 * reference class. We *don't* check instanceof here, because
jeffhaobdb76512011-09-07 11:43:16 -07002303 * you can still put a String into a String[] after the latter
2304 * has been cast to an Object[].
2305 */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002306 element_class = array_class->GetComponentType();
2307 if (element_class->IsPrimitive()) {
jeffhaobdb76512011-09-07 11:43:16 -07002308 LOG(ERROR) << "VFY: invalid aput-object of "
2309 << res_class->GetDescriptor()->ToModifiedUtf8()
2310 << " into "
2311 << array_class->GetDescriptor()->ToModifiedUtf8();
2312 failure = VERIFY_ERROR_GENERIC;
2313 break;
2314 }
2315 }
2316 }
2317 break;
2318
2319 case Instruction::IGET:
2320 tmp_type = kRegTypeInteger;
2321 goto iget_1nr_common;
2322 case Instruction::IGET_BOOLEAN:
2323 tmp_type = kRegTypeBoolean;
2324 goto iget_1nr_common;
2325 case Instruction::IGET_BYTE:
2326 tmp_type = kRegTypeByte;
2327 goto iget_1nr_common;
2328 case Instruction::IGET_CHAR:
2329 tmp_type = kRegTypeChar;
2330 goto iget_1nr_common;
2331 case Instruction::IGET_SHORT:
2332 tmp_type = kRegTypeShort;
2333 goto iget_1nr_common;
2334iget_1nr_common:
2335 {
2336 Field* inst_field;
2337 RegType obj_type, field_type;
2338
2339 obj_type = GetRegisterType(work_line, dec_insn.vB_);
2340 inst_field = GetInstField(vdata, obj_type, dec_insn.vC_, &failure);
2341 if (failure != VERIFY_ERROR_NONE)
2342 break;
2343
2344 /* make sure the field's type is compatible with expectation */
2345 field_type =
2346 PrimitiveTypeToRegType(inst_field->GetType()->GetPrimitiveType());
2347
2348 /* correct if float */
2349 if (field_type == kRegTypeFloat)
2350 tmp_type = kRegTypeFloat;
2351
2352 if (field_type == kRegTypeUnknown || tmp_type != field_type) {
2353 Class* inst_field_class = inst_field->GetDeclaringClass();
2354 LOG(ERROR) << "VFY: invalid iget-1nr of "
2355 << inst_field_class->GetDescriptor()->ToModifiedUtf8()
2356 << "." << inst_field->GetName()->ToModifiedUtf8()
2357 << " (inst=" << tmp_type << " field=" << field_type << ")";
2358 failure = VERIFY_ERROR_GENERIC;
2359 break;
2360 }
2361
2362 SetRegisterType(work_line, dec_insn.vA_, tmp_type);
2363 }
2364 break;
2365 case Instruction::IGET_WIDE:
2366 {
2367 RegType dst_type;
2368 Field* inst_field;
2369 RegType obj_type;
2370
2371 obj_type = GetRegisterType(work_line, dec_insn.vB_);
2372 inst_field = GetInstField(vdata, obj_type, dec_insn.vC_, &failure);
2373 Class* inst_field_class = inst_field->GetDeclaringClass();
2374 if (failure != VERIFY_ERROR_NONE)
2375 break;
2376 /* check the type, which should be prim */
2377 switch (inst_field->GetType()->GetPrimitiveType()) {
2378 case Class::kPrimDouble:
2379 dst_type = kRegTypeDoubleLo;
2380 break;
2381 case Class::kPrimLong:
2382 dst_type = kRegTypeLongLo;
2383 break;
2384 default:
2385 LOG(ERROR) << "VFY: invalid iget-wide of "
2386 << inst_field_class->GetDescriptor()->ToModifiedUtf8()
2387 << "." << inst_field->GetName()->ToModifiedUtf8();
2388 dst_type = kRegTypeUnknown;
2389 failure = VERIFY_ERROR_GENERIC;
2390 break;
2391 }
2392 if (failure == VERIFY_ERROR_NONE) {
2393 SetRegisterType(work_line, dec_insn.vA_, dst_type);
2394 }
2395 }
2396 break;
2397 case Instruction::IGET_OBJECT:
2398 {
2399 Class* field_class;
2400 Field* inst_field;
2401 RegType obj_type;
2402
2403 obj_type = GetRegisterType(work_line, dec_insn.vB_);
2404 inst_field = GetInstField(vdata, obj_type, dec_insn.vC_, &failure);
2405 if (failure != VERIFY_ERROR_NONE)
2406 break;
2407 field_class = inst_field->GetType();
2408 if (field_class == NULL) {
2409 /* class not found or primitive type */
2410 LOG(ERROR) << "VFY: unable to recover field class from "
2411 << inst_field->GetName()->ToModifiedUtf8();
2412 failure = VERIFY_ERROR_GENERIC;
2413 break;
2414 }
2415 if (failure == VERIFY_ERROR_NONE) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002416 DCHECK(!field_class->IsPrimitive()) << PrettyClass(field_class);
jeffhaobdb76512011-09-07 11:43:16 -07002417 SetRegisterType(work_line, dec_insn.vA_,
2418 RegTypeFromClass(field_class));
2419 }
2420 }
2421 break;
2422 case Instruction::IPUT:
2423 tmp_type = kRegTypeInteger;
2424 goto iput_1nr_common;
2425 case Instruction::IPUT_BOOLEAN:
2426 tmp_type = kRegTypeBoolean;
2427 goto iput_1nr_common;
2428 case Instruction::IPUT_BYTE:
2429 tmp_type = kRegTypeByte;
2430 goto iput_1nr_common;
2431 case Instruction::IPUT_CHAR:
2432 tmp_type = kRegTypeChar;
2433 goto iput_1nr_common;
2434 case Instruction::IPUT_SHORT:
2435 tmp_type = kRegTypeShort;
2436 goto iput_1nr_common;
2437iput_1nr_common:
2438 {
2439 RegType src_type, field_type, obj_type;
2440 Field* inst_field;
2441
2442 obj_type = GetRegisterType(work_line, dec_insn.vB_);
2443 inst_field = GetInstField(vdata, obj_type, dec_insn.vC_, &failure);
2444 if (failure != VERIFY_ERROR_NONE)
2445 break;
2446 CheckFinalFieldAccess(method, inst_field, &failure);
2447 if (failure != VERIFY_ERROR_NONE)
2448 break;
2449
2450 /* get type of field we're storing into */
2451 field_type =
2452 PrimitiveTypeToRegType(inst_field->GetType()->GetPrimitiveType());
2453 src_type = GetRegisterType(work_line, dec_insn.vA_);
2454
2455 /* correct if float */
2456 if (field_type == kRegTypeFloat)
2457 tmp_type = kRegTypeFloat;
2458
2459 /*
2460 * compiler can generate synthetic functions that write byte values
2461 * into boolean fields.
2462 */
2463 if (tmp_type == kRegTypeBoolean && src_type == kRegTypeByte)
2464 tmp_type = kRegTypeByte;
2465 if (field_type == kRegTypeBoolean && src_type == kRegTypeByte)
2466 field_type = kRegTypeByte;
2467
2468 /* make sure the source register has the correct type */
2469 if (!CanConvertTo1nr(src_type, tmp_type)) {
2470 LOG(ERROR) << "VFY: invalid reg type " << src_type
2471 << " on iput instr (need " << tmp_type << ")",
2472 failure = VERIFY_ERROR_GENERIC;
2473 break;
2474 }
2475
2476 VerifyRegisterType(work_line, dec_insn.vA_, field_type, &failure);
2477
2478 if (failure != VERIFY_ERROR_NONE || field_type == kRegTypeUnknown ||
2479 tmp_type != field_type) {
2480 Class* inst_field_class = inst_field->GetDeclaringClass();
2481 LOG(ERROR) << "VFY: invalid iput-1nr of "
2482 << inst_field_class->GetDescriptor()->ToModifiedUtf8()
2483 << "." << inst_field->GetName()->ToModifiedUtf8()
2484 << " (inst=" << tmp_type << " field=" << field_type << ")";
2485 failure = VERIFY_ERROR_GENERIC;
2486 break;
2487 }
2488 }
2489 break;
2490 case Instruction::IPUT_WIDE:
2491 Field* inst_field;
2492 RegType obj_type;
2493
2494 obj_type = GetRegisterType(work_line, dec_insn.vB_);
2495 inst_field = GetInstField(vdata, obj_type, dec_insn.vC_, &failure);
2496 if (failure != VERIFY_ERROR_NONE)
2497 break;
2498 CheckFinalFieldAccess(method, inst_field, &failure);
2499 if (failure != VERIFY_ERROR_NONE)
2500 break;
2501
2502 /* check the type, which should be prim */
2503 switch (inst_field->GetType()->GetPrimitiveType()) {
2504 case Class::kPrimDouble:
2505 VerifyRegisterType(work_line, dec_insn.vA_, kRegTypeDoubleLo,
2506 &failure);
2507 break;
2508 case Class::kPrimLong:
2509 VerifyRegisterType(work_line, dec_insn.vA_, kRegTypeLongLo, &failure);
2510 break;
2511 default:
2512 LOG(ERROR) << "VFY: invalid iput-wide of "
2513 << inst_field->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
2514 << "." << inst_field->GetName()->ToModifiedUtf8();
2515 failure = VERIFY_ERROR_GENERIC;
2516 break;
2517 }
2518 break;
2519 case Instruction::IPUT_OBJECT:
2520 {
2521 Class* field_class;
2522 Class* value_class;
2523 Field* inst_field;
2524 RegType obj_type, value_type;
2525
2526 obj_type = GetRegisterType(work_line, dec_insn.vB_);
2527 inst_field = GetInstField(vdata, obj_type, dec_insn.vC_, &failure);
2528 if (failure != VERIFY_ERROR_NONE)
2529 break;
2530 CheckFinalFieldAccess(method, inst_field, &failure);
2531 if (failure != VERIFY_ERROR_NONE)
2532 break;
2533
2534 field_class = inst_field->GetType();
2535 if (field_class == NULL) {
2536 LOG(ERROR) << "VFY: unable to recover field class from '"
2537 << inst_field->GetName()->ToModifiedUtf8() << "'";
2538 failure = VERIFY_ERROR_GENERIC;
2539 break;
2540 }
2541
2542 value_type = GetRegisterType(work_line, dec_insn.vA_);
2543 if (!RegTypeIsReference(value_type)) {
2544 LOG(ERROR) << "VFY: storing non-ref v" << dec_insn.vA_
2545 << " into ref field '"
2546 << inst_field->GetName()->ToModifiedUtf8() << "' ("
2547 << field_class->GetDescriptor()->ToModifiedUtf8() << ")";
2548 failure = VERIFY_ERROR_GENERIC;
2549 break;
2550 }
2551 if (value_type != kRegTypeZero) {
2552 value_class = RegTypeInitializedReferenceToClass(value_type);
2553 if (value_class == NULL) {
2554 LOG(ERROR) << "VFY: storing uninit ref v" << dec_insn.vA_
2555 << " into ref field";
2556 failure = VERIFY_ERROR_GENERIC;
2557 break;
2558 }
2559 /* allow if field is any interface or field is base class */
2560 if (!field_class->IsInterface() &&
2561 !field_class->IsAssignableFrom(value_class)) {
2562 Class* inst_field_class = inst_field->GetDeclaringClass();
2563 LOG(ERROR) << "VFY: storing type '"
2564 << value_class->GetDescriptor()->ToModifiedUtf8()
2565 << "' into field type '"
2566 << field_class->GetDescriptor()->ToModifiedUtf8()
2567 << "' ("
2568 << inst_field_class->GetDescriptor()->ToModifiedUtf8()
2569 << "." << inst_field->GetName()->ToModifiedUtf8() << ")";
2570 failure = VERIFY_ERROR_GENERIC;
2571 break;
2572 }
2573 }
2574 }
2575 break;
2576
2577 case Instruction::SGET:
2578 tmp_type = kRegTypeInteger;
2579 goto sget_1nr_common;
2580 case Instruction::SGET_BOOLEAN:
2581 tmp_type = kRegTypeBoolean;
2582 goto sget_1nr_common;
2583 case Instruction::SGET_BYTE:
2584 tmp_type = kRegTypeByte;
2585 goto sget_1nr_common;
2586 case Instruction::SGET_CHAR:
2587 tmp_type = kRegTypeChar;
2588 goto sget_1nr_common;
2589 case Instruction::SGET_SHORT:
2590 tmp_type = kRegTypeShort;
2591 goto sget_1nr_common;
2592sget_1nr_common:
2593 {
2594 Field* static_field;
2595 RegType field_type;
2596
2597 static_field = GetStaticField(vdata, dec_insn.vB_, &failure);
2598 if (failure != VERIFY_ERROR_NONE)
2599 break;
2600
2601 /*
2602 * Make sure the field's type is compatible with expectation.
2603 * We can get ourselves into trouble if we mix & match loads
2604 * and stores with different widths, so rather than just checking
2605 * "CanConvertTo1nr" we require that the field types have equal
2606 * widths.
2607 */
2608 field_type =
2609 PrimitiveTypeToRegType(static_field->GetType()->GetPrimitiveType());
2610
2611 /* correct if float */
2612 if (field_type == kRegTypeFloat)
2613 tmp_type = kRegTypeFloat;
2614
2615 if (tmp_type != field_type) {
2616 Class* static_field_class = static_field->GetDeclaringClass();
2617 LOG(ERROR) << "VFY: invalid sget-1nr of "
2618 << static_field_class->GetDescriptor()->ToModifiedUtf8()
2619 << "." << static_field->GetName()->ToModifiedUtf8()
2620 << " (inst=" << tmp_type << " actual=" << field_type
2621 << ")";
2622 failure = VERIFY_ERROR_GENERIC;
2623 break;
2624 }
2625
2626 SetRegisterType(work_line, dec_insn.vA_, tmp_type);
2627 }
2628 break;
2629 case Instruction::SGET_WIDE:
2630 {
2631 Field* static_field;
2632 RegType dst_type;
2633
2634 static_field = GetStaticField(vdata, dec_insn.vB_, &failure);
2635 Class* static_field_class = static_field->GetDeclaringClass();
2636 if (failure != VERIFY_ERROR_NONE)
2637 break;
2638 /* check the type, which should be prim */
2639 switch (static_field->GetType()->GetPrimitiveType()) {
2640 case Class::kPrimDouble:
2641 dst_type = kRegTypeDoubleLo;
2642 break;
2643 case Class::kPrimLong:
2644 dst_type = kRegTypeLongLo;
2645 break;
2646 default:
2647 LOG(ERROR) << "VFY: invalid sget-wide of "
2648 << static_field_class->GetDescriptor()->ToModifiedUtf8()
2649 << "." << static_field->GetName()->ToModifiedUtf8();
2650 dst_type = kRegTypeUnknown;
2651 failure = VERIFY_ERROR_GENERIC;
2652 break;
2653 }
2654 if (failure == VERIFY_ERROR_NONE) {
2655 SetRegisterType(work_line, dec_insn.vA_, dst_type);
2656 }
2657 }
2658 break;
2659 case Instruction::SGET_OBJECT:
2660 {
2661 Field* static_field;
2662 Class* field_class;
2663
2664 static_field = GetStaticField(vdata, dec_insn.vB_, &failure);
2665 if (failure != VERIFY_ERROR_NONE)
2666 break;
2667 field_class = static_field->GetType();
2668 if (field_class == NULL) {
2669 LOG(ERROR) << "VFY: unable to recover field class from '"
2670 << static_field->GetName()->ToModifiedUtf8() << "'";
2671 failure = VERIFY_ERROR_GENERIC;
2672 break;
2673 }
2674 if (field_class->IsPrimitive()) {
2675 LOG(ERROR) << "VFY: attempt to get prim field with sget-object";
2676 failure = VERIFY_ERROR_GENERIC;
2677 break;
2678 }
2679 SetRegisterType(work_line, dec_insn.vA_, RegTypeFromClass(field_class));
2680 }
2681 break;
2682 case Instruction::SPUT:
2683 tmp_type = kRegTypeInteger;
2684 goto sput_1nr_common;
2685 case Instruction::SPUT_BOOLEAN:
2686 tmp_type = kRegTypeBoolean;
2687 goto sput_1nr_common;
2688 case Instruction::SPUT_BYTE:
2689 tmp_type = kRegTypeByte;
2690 goto sput_1nr_common;
2691 case Instruction::SPUT_CHAR:
2692 tmp_type = kRegTypeChar;
2693 goto sput_1nr_common;
2694 case Instruction::SPUT_SHORT:
2695 tmp_type = kRegTypeShort;
2696 goto sput_1nr_common;
2697sput_1nr_common:
2698 {
2699 RegType src_type, field_type;
2700 Field* static_field;
2701
2702 static_field = GetStaticField(vdata, dec_insn.vB_, &failure);
2703 if (failure != VERIFY_ERROR_NONE)
2704 break;
2705 CheckFinalFieldAccess(method, static_field, &failure);
2706 if (failure != VERIFY_ERROR_NONE)
2707 break;
2708
2709 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002710 * Get type of field we're storing into. We know that the
jeffhaobdb76512011-09-07 11:43:16 -07002711 * contents of the register match the instruction, but we also
2712 * need to ensure that the instruction matches the field type.
2713 * Using e.g. sput-short to write into a 32-bit integer field
2714 * can lead to trouble if we do 16-bit writes.
2715 */
2716 field_type =
2717 PrimitiveTypeToRegType(static_field->GetType()->GetPrimitiveType());
2718 src_type = GetRegisterType(work_line, dec_insn.vA_);
2719
2720 /* correct if float */
2721 if (field_type == kRegTypeFloat)
2722 tmp_type = kRegTypeFloat;
2723
2724 /*
2725 * compiler can generate synthetic functions that write byte values
2726 * into boolean fields.
2727 */
2728 if (tmp_type == kRegTypeBoolean && src_type == kRegTypeByte)
2729 tmp_type = kRegTypeByte;
2730 if (field_type == kRegTypeBoolean && src_type == kRegTypeByte)
2731 field_type = kRegTypeByte;
2732
2733 /* make sure the source register has the correct type */
2734 if (!CanConvertTo1nr(src_type, tmp_type)) {
2735 LOG(ERROR) << "VFY: invalid reg type " << src_type
2736 << " on sput instr (need " << tmp_type << ")";
2737 failure = VERIFY_ERROR_GENERIC;
2738 break;
2739 }
2740
2741 VerifyRegisterType(work_line, dec_insn.vA_, field_type, &failure);
2742
2743 if (failure != VERIFY_ERROR_NONE || field_type == kRegTypeUnknown ||
2744 tmp_type != field_type) {
2745 Class* static_field_class = static_field->GetDeclaringClass();
2746 LOG(ERROR) << "VFY: invalid sput-1nr of "
2747 << static_field_class->GetDescriptor()->ToModifiedUtf8()
2748 << "." << static_field->GetName()->ToModifiedUtf8()
2749 << " (inst=" << tmp_type << " actual=" << field_type
2750 << ")";
2751 failure = VERIFY_ERROR_GENERIC;
2752 break;
2753 }
2754 }
2755 break;
2756 case Instruction::SPUT_WIDE:
2757 Field* static_field;
2758
2759 static_field = GetStaticField(vdata, dec_insn.vB_, &failure);
2760 if (failure != VERIFY_ERROR_NONE)
2761 break;
2762 CheckFinalFieldAccess(method, static_field, &failure);
2763 if (failure != VERIFY_ERROR_NONE)
2764 break;
2765
2766 /* check the type, which should be prim */
2767 switch (static_field->GetType()->GetPrimitiveType()) {
2768 case Class::kPrimDouble:
2769 VerifyRegisterType(work_line, dec_insn.vA_, kRegTypeDoubleLo,
2770 &failure);
2771 break;
2772 case Class::kPrimLong:
2773 VerifyRegisterType(work_line, dec_insn.vA_, kRegTypeLongLo, &failure);
2774 break;
2775 default:
2776 LOG(ERROR) << "VFY: invalid sput-wide of "
2777 << static_field->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
2778 << "." << static_field->GetName()->ToModifiedUtf8();
2779 failure = VERIFY_ERROR_GENERIC;
2780 break;
2781 }
2782 break;
2783 case Instruction::SPUT_OBJECT:
2784 {
2785 Class* field_class;
2786 Class* value_class;
2787 Field* static_field;
2788 RegType value_type;
2789
2790 static_field = GetStaticField(vdata, dec_insn.vB_, &failure);
2791 if (failure != VERIFY_ERROR_NONE)
2792 break;
2793 CheckFinalFieldAccess(method, static_field, &failure);
2794 if (failure != VERIFY_ERROR_NONE)
2795 break;
2796
2797 field_class = static_field->GetType();
2798 if (field_class == NULL) {
2799 LOG(ERROR) << "VFY: unable to recover field class from '"
2800 << static_field->GetName()->ToModifiedUtf8() << "'";
2801 failure = VERIFY_ERROR_GENERIC;
2802 break;
2803 }
2804
2805 value_type = GetRegisterType(work_line, dec_insn.vA_);
2806 if (!RegTypeIsReference(value_type)) {
2807 LOG(ERROR) << "VFY: storing non-ref v" << dec_insn.vA_
2808 << " into ref field '"
2809 << static_field->GetName()->ToModifiedUtf8() << "' ("
2810 << field_class->GetDescriptor()->ToModifiedUtf8() << ")",
2811 failure = VERIFY_ERROR_GENERIC;
2812 break;
2813 }
2814 if (value_type != kRegTypeZero) {
2815 value_class = RegTypeInitializedReferenceToClass(value_type);
2816 if (value_class == NULL) {
2817 LOG(ERROR) << "VFY: storing uninit ref v" << dec_insn.vA_
2818 << " into ref field";
2819 failure = VERIFY_ERROR_GENERIC;
2820 break;
2821 }
2822 /* allow if field is any interface or field is base class */
2823 if (!field_class->IsInterface() &&
2824 !field_class->IsAssignableFrom(value_class)) {
2825 Class* static_field_class = static_field->GetDeclaringClass();
2826 LOG(ERROR) << "VFY: storing type '"
2827 << value_class->GetDescriptor()->ToModifiedUtf8()
2828 << "' into field type '"
2829 << field_class->GetDescriptor()->ToModifiedUtf8()
2830 << "' ("
2831 << static_field_class->GetDescriptor()->ToModifiedUtf8()
2832 << "." << static_field->GetName()->ToModifiedUtf8()
2833 << ")",
2834 failure = VERIFY_ERROR_GENERIC;
2835 break;
2836 }
2837 }
2838 }
2839 break;
2840
2841 case Instruction::INVOKE_VIRTUAL:
2842 case Instruction::INVOKE_VIRTUAL_RANGE:
2843 case Instruction::INVOKE_SUPER:
2844 case Instruction::INVOKE_SUPER_RANGE:
2845 {
2846 Method* called_method;
2847 RegType return_type;
2848 bool is_range;
2849 bool is_super;
2850
2851 is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2852 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2853 is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2854 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2855
2856 called_method = VerifyInvocationArgs(vdata, work_line, registers_size,
2857 &dec_insn, METHOD_VIRTUAL, is_range, is_super, &failure);
2858 if (failure != VERIFY_ERROR_NONE)
2859 break;
2860 return_type = GetMethodReturnType(dex_file, called_method);
2861 SetResultRegisterType(work_line, registers_size, return_type);
2862 just_set_result = true;
2863 }
2864 break;
2865 case Instruction::INVOKE_DIRECT:
2866 case Instruction::INVOKE_DIRECT_RANGE:
2867 {
2868 RegType return_type;
2869 Method* called_method;
2870 bool is_range;
2871
2872 is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2873 called_method = VerifyInvocationArgs(vdata, work_line, registers_size,
2874 &dec_insn, METHOD_DIRECT, is_range, false, &failure);
2875 if (failure != VERIFY_ERROR_NONE)
2876 break;
2877
2878 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002879 * Some additional checks when calling <init>. We know from
jeffhaobdb76512011-09-07 11:43:16 -07002880 * the invocation arg check that the "this" argument is an
jeffhaod1f0fde2011-09-08 17:25:33 -07002881 * instance of called_method->klass. Now we further restrict
jeffhaobdb76512011-09-07 11:43:16 -07002882 * that to require that called_method->klass is the same as
2883 * this->klass or this->super, allowing the latter only if
2884 * the "this" argument is the same as the "this" argument to
2885 * this method (which implies that we're in <init> ourselves).
2886 */
2887 if (IsInitMethod(called_method)) {
2888 RegType this_type;
2889 this_type = GetInvocationThis(work_line, &dec_insn, &failure);
2890 if (failure != VERIFY_ERROR_NONE)
2891 break;
2892
2893 /* no null refs allowed (?) */
2894 if (this_type == kRegTypeZero) {
2895 LOG(ERROR) << "VFY: unable to initialize null ref";
2896 failure = VERIFY_ERROR_GENERIC;
2897 break;
2898 }
2899
2900 Class* this_class;
2901
2902 this_class = RegTypeReferenceToClass(this_type, uninit_map);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002903 DCHECK(this_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07002904
2905 /* must be in same class or in superclass */
2906 if (called_method->GetDeclaringClass() == this_class->GetSuperClass())
2907 {
2908 if (this_class != method->GetDeclaringClass()) {
2909 LOG(ERROR) << "VFY: invoke-direct <init> on super only "
2910 << "allowed for 'this' in <init>";
2911 failure = VERIFY_ERROR_GENERIC;
2912 break;
2913 }
2914 } else if (called_method->GetDeclaringClass() != this_class) {
2915 LOG(ERROR) << "VFY: invoke-direct <init> must be on current "
2916 << "class or super";
2917 failure = VERIFY_ERROR_GENERIC;
2918 break;
2919 }
2920
2921 /* arg must be an uninitialized reference */
2922 if (!RegTypeIsUninitReference(this_type)) {
2923 LOG(ERROR) << "VFY: can only initialize the uninitialized";
2924 failure = VERIFY_ERROR_GENERIC;
2925 break;
2926 }
2927
2928 /*
2929 * Replace the uninitialized reference with an initialized
jeffhaod1f0fde2011-09-08 17:25:33 -07002930 * one, and clear the entry in the uninit map. We need to
jeffhaobdb76512011-09-07 11:43:16 -07002931 * do this for all registers that have the same object
2932 * instance in them, not just the "this" register.
2933 */
2934 MarkRefsAsInitialized(work_line, registers_size, uninit_map,
2935 this_type, &failure);
2936 if (failure != VERIFY_ERROR_NONE)
2937 break;
2938 }
2939 return_type = GetMethodReturnType(dex_file, called_method);
2940 SetResultRegisterType(work_line, registers_size, return_type);
2941 just_set_result = true;
2942 }
2943 break;
2944 case Instruction::INVOKE_STATIC:
2945 case Instruction::INVOKE_STATIC_RANGE:
2946 {
2947 RegType return_type;
2948 Method* called_method;
2949 bool is_range;
2950
2951 is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2952 called_method = VerifyInvocationArgs(vdata, work_line, registers_size,
2953 &dec_insn, METHOD_STATIC, is_range, false, &failure);
2954 if (failure != VERIFY_ERROR_NONE)
2955 break;
2956
2957 return_type = GetMethodReturnType(dex_file, called_method);
2958 SetResultRegisterType(work_line, registers_size, return_type);
2959 just_set_result = true;
2960 }
2961 break;
2962 case Instruction::INVOKE_INTERFACE:
2963 case Instruction::INVOKE_INTERFACE_RANGE:
2964 {
2965 RegType /*this_type,*/ return_type;
2966 Method* abs_method;
2967 bool is_range;
2968
2969 is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2970 abs_method = VerifyInvocationArgs(vdata, work_line, registers_size,
2971 &dec_insn, METHOD_INTERFACE, is_range, false, &failure);
2972 if (failure != VERIFY_ERROR_NONE)
2973 break;
2974
2975#if 0 /* can't do this here, fails on dalvik test 052-verifier-fun */
2976 /*
2977 * Get the type of the "this" arg, which should always be an
jeffhaod1f0fde2011-09-08 17:25:33 -07002978 * interface class. Because we don't do a full merge on
jeffhaobdb76512011-09-07 11:43:16 -07002979 * interface classes, this might have reduced to Object.
2980 */
2981 this_type = GetInvocationThis(work_line, &dec_insn, &failure);
2982 if (failure != VERIFY_ERROR_NONE)
2983 break;
2984
2985 if (this_type == kRegTypeZero) {
2986 /* null pointer always passes (and always fails at runtime) */
2987 } else {
2988 Class* this_class;
2989
2990 this_class = RegTypeInitializedReferenceToClass(this_type);
2991 if (this_class == NULL) {
2992 LOG(ERROR) << "VFY: interface call on uninitialized";
2993 failure = VERIFY_ERROR_GENERIC;
2994 break;
2995 }
2996
2997 /*
2998 * Either "this_class" needs to be the interface class that
2999 * defined abs_method, or abs_method's class needs to be one
jeffhaod1f0fde2011-09-08 17:25:33 -07003000 * of the interfaces implemented by "this_class". (Or, if
jeffhaobdb76512011-09-07 11:43:16 -07003001 * we couldn't complete the merge, this will be Object.)
3002 */
3003 if (this_class != abs_method->GetDeclaringClass() &&
3004 this_class != class_linker->FindSystemClass("Ljava/lang/Object;") &&
3005 !this_class->Implements(abs_method->GetDeclaringClass())) {
3006 LOG(ERROR) << "VFY: unable to match abs_method '"
3007 << abs_method->GetName()->ToModifiedUtf8() << "' with "
3008 << this_class->GetDescriptor()->ToModifiedUtf8()
3009 << " interfaces";
3010 failure = VERIFY_ERROR_GENERIC;
3011 break;
3012 }
3013 }
3014#endif
3015
3016 /*
3017 * We don't have an object instance, so we can't find the
jeffhaod1f0fde2011-09-08 17:25:33 -07003018 * concrete method. However, all of the type information is
jeffhaobdb76512011-09-07 11:43:16 -07003019 * in the abstract method, so we're good.
3020 */
3021 return_type = GetMethodReturnType(dex_file, abs_method);
3022 SetResultRegisterType(work_line, registers_size, return_type);
3023 just_set_result = true;
3024 }
3025 break;
3026
3027 case Instruction::NEG_INT:
3028 case Instruction::NOT_INT:
3029 CheckUnop(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger,
3030 &failure);
3031 break;
3032 case Instruction::NEG_LONG:
3033 case Instruction::NOT_LONG:
3034 CheckUnop(work_line, &dec_insn, kRegTypeLongLo, kRegTypeLongLo, &failure);
3035 break;
3036 case Instruction::NEG_FLOAT:
3037 CheckUnop(work_line, &dec_insn, kRegTypeFloat, kRegTypeFloat, &failure);
3038 break;
3039 case Instruction::NEG_DOUBLE:
3040 CheckUnop(work_line, &dec_insn, kRegTypeDoubleLo, kRegTypeDoubleLo,
3041 &failure);
3042 break;
3043 case Instruction::INT_TO_LONG:
3044 CheckUnop(work_line, &dec_insn, kRegTypeLongLo, kRegTypeInteger,
3045 &failure);
3046 break;
3047 case Instruction::INT_TO_FLOAT:
3048 CheckUnop(work_line, &dec_insn, kRegTypeFloat, kRegTypeInteger, &failure);
3049 break;
3050 case Instruction::INT_TO_DOUBLE:
3051 CheckUnop(work_line, &dec_insn, kRegTypeDoubleLo, kRegTypeInteger,
3052 &failure);
3053 break;
3054 case Instruction::LONG_TO_INT:
3055 CheckUnop(work_line, &dec_insn, kRegTypeInteger, kRegTypeLongLo,
3056 &failure);
3057 break;
3058 case Instruction::LONG_TO_FLOAT:
3059 CheckUnop(work_line, &dec_insn, kRegTypeFloat, kRegTypeLongLo, &failure);
3060 break;
3061 case Instruction::LONG_TO_DOUBLE:
3062 CheckUnop(work_line, &dec_insn, kRegTypeDoubleLo, kRegTypeLongLo,
3063 &failure);
3064 break;
3065 case Instruction::FLOAT_TO_INT:
3066 CheckUnop(work_line, &dec_insn, kRegTypeInteger, kRegTypeFloat, &failure);
3067 break;
3068 case Instruction::FLOAT_TO_LONG:
3069 CheckUnop(work_line, &dec_insn, kRegTypeLongLo, kRegTypeFloat, &failure);
3070 break;
3071 case Instruction::FLOAT_TO_DOUBLE:
3072 CheckUnop(work_line, &dec_insn, kRegTypeDoubleLo, kRegTypeFloat,
3073 &failure);
3074 break;
3075 case Instruction::DOUBLE_TO_INT:
3076 CheckUnop(work_line, &dec_insn, kRegTypeInteger, kRegTypeDoubleLo,
3077 &failure);
3078 break;
3079 case Instruction::DOUBLE_TO_LONG:
3080 CheckUnop(work_line, &dec_insn, kRegTypeLongLo, kRegTypeDoubleLo,
3081 &failure);
3082 break;
3083 case Instruction::DOUBLE_TO_FLOAT:
3084 CheckUnop(work_line, &dec_insn, kRegTypeFloat, kRegTypeDoubleLo,
3085 &failure);
3086 break;
3087 case Instruction::INT_TO_BYTE:
3088 CheckUnop(work_line, &dec_insn, kRegTypeByte, kRegTypeInteger, &failure);
3089 break;
3090 case Instruction::INT_TO_CHAR:
3091 CheckUnop(work_line, &dec_insn, kRegTypeChar, kRegTypeInteger, &failure);
3092 break;
3093 case Instruction::INT_TO_SHORT:
3094 CheckUnop(work_line, &dec_insn, kRegTypeShort, kRegTypeInteger, &failure);
3095 break;
3096
3097 case Instruction::ADD_INT:
3098 case Instruction::SUB_INT:
3099 case Instruction::MUL_INT:
3100 case Instruction::REM_INT:
3101 case Instruction::DIV_INT:
3102 case Instruction::SHL_INT:
3103 case Instruction::SHR_INT:
3104 case Instruction::USHR_INT:
3105 CheckBinop(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger,
3106 kRegTypeInteger, false, &failure);
3107 break;
3108 case Instruction::AND_INT:
3109 case Instruction::OR_INT:
3110 case Instruction::XOR_INT:
3111 CheckBinop(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger,
3112 kRegTypeInteger, true, &failure);
3113 break;
3114 case Instruction::ADD_LONG:
3115 case Instruction::SUB_LONG:
3116 case Instruction::MUL_LONG:
3117 case Instruction::DIV_LONG:
3118 case Instruction::REM_LONG:
3119 case Instruction::AND_LONG:
3120 case Instruction::OR_LONG:
3121 case Instruction::XOR_LONG:
3122 CheckBinop(work_line, &dec_insn, kRegTypeLongLo, kRegTypeLongLo,
3123 kRegTypeLongLo, false, &failure);
3124 break;
3125 case Instruction::SHL_LONG:
3126 case Instruction::SHR_LONG:
3127 case Instruction::USHR_LONG:
3128 /* shift distance is Int, making these different from other binops */
3129 CheckBinop(work_line, &dec_insn, kRegTypeLongLo, kRegTypeLongLo,
3130 kRegTypeInteger, false, &failure);
3131 break;
3132 case Instruction::ADD_FLOAT:
3133 case Instruction::SUB_FLOAT:
3134 case Instruction::MUL_FLOAT:
3135 case Instruction::DIV_FLOAT:
3136 case Instruction::REM_FLOAT:
3137 CheckBinop(work_line, &dec_insn, kRegTypeFloat, kRegTypeFloat,
3138 kRegTypeFloat, false, &failure);
3139 break;
3140 case Instruction::ADD_DOUBLE:
3141 case Instruction::SUB_DOUBLE:
3142 case Instruction::MUL_DOUBLE:
3143 case Instruction::DIV_DOUBLE:
3144 case Instruction::REM_DOUBLE:
3145 CheckBinop(work_line, &dec_insn, kRegTypeDoubleLo, kRegTypeDoubleLo,
3146 kRegTypeDoubleLo, false, &failure);
3147 break;
3148 case Instruction::ADD_INT_2ADDR:
3149 case Instruction::SUB_INT_2ADDR:
3150 case Instruction::MUL_INT_2ADDR:
3151 case Instruction::REM_INT_2ADDR:
3152 case Instruction::SHL_INT_2ADDR:
3153 case Instruction::SHR_INT_2ADDR:
3154 case Instruction::USHR_INT_2ADDR:
3155 CheckBinop2addr(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger,
3156 kRegTypeInteger, false, &failure);
3157 break;
3158 case Instruction::AND_INT_2ADDR:
3159 case Instruction::OR_INT_2ADDR:
3160 case Instruction::XOR_INT_2ADDR:
3161 CheckBinop2addr(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger,
3162 kRegTypeInteger, true, &failure);
3163 break;
3164 case Instruction::DIV_INT_2ADDR:
3165 CheckBinop2addr(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger,
3166 kRegTypeInteger, false, &failure);
3167 break;
3168 case Instruction::ADD_LONG_2ADDR:
3169 case Instruction::SUB_LONG_2ADDR:
3170 case Instruction::MUL_LONG_2ADDR:
3171 case Instruction::DIV_LONG_2ADDR:
3172 case Instruction::REM_LONG_2ADDR:
3173 case Instruction::AND_LONG_2ADDR:
3174 case Instruction::OR_LONG_2ADDR:
3175 case Instruction::XOR_LONG_2ADDR:
3176 CheckBinop2addr(work_line, &dec_insn, kRegTypeLongLo, kRegTypeLongLo,
3177 kRegTypeLongLo, false, &failure);
3178 break;
3179 case Instruction::SHL_LONG_2ADDR:
3180 case Instruction::SHR_LONG_2ADDR:
3181 case Instruction::USHR_LONG_2ADDR:
3182 CheckBinop2addr(work_line, &dec_insn, kRegTypeLongLo, kRegTypeLongLo,
3183 kRegTypeInteger, false, &failure);
3184 break;
3185 case Instruction::ADD_FLOAT_2ADDR:
3186 case Instruction::SUB_FLOAT_2ADDR:
3187 case Instruction::MUL_FLOAT_2ADDR:
3188 case Instruction::DIV_FLOAT_2ADDR:
3189 case Instruction::REM_FLOAT_2ADDR:
3190 CheckBinop2addr(work_line, &dec_insn, kRegTypeFloat, kRegTypeFloat,
3191 kRegTypeFloat, false, &failure);
3192 break;
3193 case Instruction::ADD_DOUBLE_2ADDR:
3194 case Instruction::SUB_DOUBLE_2ADDR:
3195 case Instruction::MUL_DOUBLE_2ADDR:
3196 case Instruction::DIV_DOUBLE_2ADDR:
3197 case Instruction::REM_DOUBLE_2ADDR:
3198 CheckBinop2addr(work_line, &dec_insn, kRegTypeDoubleLo, kRegTypeDoubleLo,
3199 kRegTypeDoubleLo, false, &failure);
3200 break;
3201 case Instruction::ADD_INT_LIT16:
3202 case Instruction::RSUB_INT:
3203 case Instruction::MUL_INT_LIT16:
3204 case Instruction::DIV_INT_LIT16:
3205 case Instruction::REM_INT_LIT16:
3206 CheckLitop(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger, false,
3207 &failure);
3208 break;
3209 case Instruction::AND_INT_LIT16:
3210 case Instruction::OR_INT_LIT16:
3211 case Instruction::XOR_INT_LIT16:
3212 CheckLitop(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger, true,
3213 &failure);
3214 break;
3215 case Instruction::ADD_INT_LIT8:
3216 case Instruction::RSUB_INT_LIT8:
3217 case Instruction::MUL_INT_LIT8:
3218 case Instruction::DIV_INT_LIT8:
3219 case Instruction::REM_INT_LIT8:
3220 case Instruction::SHL_INT_LIT8:
3221 CheckLitop(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger, false,
3222 &failure);
3223 break;
3224 case Instruction::SHR_INT_LIT8:
3225 tmp_type = AdjustForRightShift(work_line, dec_insn.vB_, dec_insn.vC_,
jeffhaob4df5142011-09-19 20:25:32 -07003226 false);
jeffhaobdb76512011-09-07 11:43:16 -07003227 CheckLitop(work_line, &dec_insn, tmp_type, kRegTypeInteger, false,
3228 &failure);
3229 break;
3230 case Instruction::USHR_INT_LIT8:
3231 tmp_type = AdjustForRightShift(work_line, dec_insn.vB_, dec_insn.vC_,
jeffhaob4df5142011-09-19 20:25:32 -07003232 true);
jeffhaobdb76512011-09-07 11:43:16 -07003233 CheckLitop(work_line, &dec_insn, tmp_type, kRegTypeInteger, false,
3234 &failure);
3235 break;
3236 case Instruction::AND_INT_LIT8:
3237 case Instruction::OR_INT_LIT8:
3238 case Instruction::XOR_INT_LIT8:
3239 CheckLitop(work_line, &dec_insn, kRegTypeInteger, kRegTypeInteger, true,
3240 &failure);
3241 break;
3242
3243 /*
3244 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07003245 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07003246 * inserted in the course of verification, we can expect to see it here.
3247 */
jeffhaob4df5142011-09-19 20:25:32 -07003248 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07003249 break;
3250
3251 /*
3252 * Verifying "quickened" instructions is tricky, because we have
jeffhaod1f0fde2011-09-08 17:25:33 -07003253 * discarded the original field/method information. The byte offsets
jeffhaobdb76512011-09-07 11:43:16 -07003254 * and vtable indices only have meaning in the context of an object
3255 * instance.
3256 *
3257 * If a piece of code declares a local reference variable, assigns
3258 * null to it, and then issues a virtual method call on it, we
jeffhaod1f0fde2011-09-08 17:25:33 -07003259 * cannot evaluate the method call during verification. This situation
jeffhaobdb76512011-09-07 11:43:16 -07003260 * isn't hard to handle, since we know the call will always result in an
jeffhaod1f0fde2011-09-08 17:25:33 -07003261 * NPE, and the arguments and return value don't matter. Any code that
jeffhaobdb76512011-09-07 11:43:16 -07003262 * depends on the result of the method call is inaccessible, so the
3263 * fact that we can't fully verify anything that comes after the bad
3264 * call is not a problem.
3265 *
3266 * We must also consider the case of multiple code paths, only some of
jeffhaod1f0fde2011-09-08 17:25:33 -07003267 * which involve a null reference. We can completely verify the method
jeffhaobdb76512011-09-07 11:43:16 -07003268 * if we sidestep the results of executing with a null reference.
3269 * For example, if on the first pass through the code we try to do a
3270 * virtual method invocation through a null ref, we have to skip the
3271 * method checks and have the method return a "wildcard" type (which
jeffhaod1f0fde2011-09-08 17:25:33 -07003272 * merges with anything to become that other thing). The move-result
jeffhaobdb76512011-09-07 11:43:16 -07003273 * will tell us if it's a reference, single-word numeric, or double-word
jeffhaod1f0fde2011-09-08 17:25:33 -07003274 * value. We continue to perform the verification, and at the end of
jeffhaobdb76512011-09-07 11:43:16 -07003275 * the function any invocations that were never fully exercised are
3276 * marked as null-only.
3277 *
jeffhaod1f0fde2011-09-08 17:25:33 -07003278 * We would do something similar for the field accesses. The field's
jeffhaobdb76512011-09-07 11:43:16 -07003279 * type, once known, can be used to recover the width of short integers.
3280 * If the object reference was null, the field-get returns the "wildcard"
3281 * type, which is acceptable for any operation.
3282 */
3283 case Instruction::UNUSED_EE:
3284 case Instruction::UNUSED_EF:
3285 case Instruction::UNUSED_F2:
3286 case Instruction::UNUSED_F3:
3287 case Instruction::UNUSED_F4:
3288 case Instruction::UNUSED_F5:
3289 case Instruction::UNUSED_F6:
3290 case Instruction::UNUSED_F7:
3291 case Instruction::UNUSED_F8:
3292 case Instruction::UNUSED_F9:
3293 case Instruction::UNUSED_FA:
3294 case Instruction::UNUSED_FB:
3295 //case Instruction::EXECUTE_INLINE:
3296 //case Instruction::EXECUTE_INLINE_RANGE:
3297 //case Instruction::IGET_QUICK:
3298 //case Instruction::IGET_WIDE_QUICK:
3299 //case Instruction::IGET_OBJECT_QUICK:
3300 //case Instruction::IPUT_QUICK:
3301 //case Instruction::IPUT_WIDE_QUICK:
3302 //case Instruction::IPUT_OBJECT_QUICK:
3303 //case Instruction::INVOKE_VIRTUAL_QUICK:
3304 //case Instruction::INVOKE_VIRTUAL_QUICK_RANGE:
3305 //case Instruction::INVOKE_SUPER_QUICK:
3306 //case Instruction::INVOKE_SUPER_QUICK_RANGE:
3307 /* fall through to failure */
3308
3309 /*
3310 * These instructions are equivalent (from the verifier's point of view)
jeffhaod1f0fde2011-09-08 17:25:33 -07003311 * to the original form. The change was made for correctness rather
jeffhaobdb76512011-09-07 11:43:16 -07003312 * than improved performance (except for invoke-object-init, which
jeffhaod1f0fde2011-09-08 17:25:33 -07003313 * provides both). The substitution takes place after verification
jeffhaobdb76512011-09-07 11:43:16 -07003314 * completes, though, so we don't expect to see them here.
3315 */
3316 case Instruction::UNUSED_F0:
3317 case Instruction::UNUSED_F1:
3318 case Instruction::UNUSED_E3:
3319 case Instruction::UNUSED_E8:
3320 case Instruction::UNUSED_E7:
3321 case Instruction::UNUSED_E4:
3322 case Instruction::UNUSED_E9:
3323 case Instruction::UNUSED_FC:
3324 case Instruction::UNUSED_E5:
3325 case Instruction::UNUSED_EA:
3326 case Instruction::UNUSED_FD:
3327 case Instruction::UNUSED_E6:
3328 case Instruction::UNUSED_EB:
3329 case Instruction::UNUSED_FE:
3330 //case Instruction::INVOKE_OBJECT_INIT_RANGE:
3331 //case Instruction::RETURN_VOID_BARRIER:
3332 //case Instruction::IGET_VOLATILE:
3333 //case Instruction::IGET_WIDE_VOLATILE:
3334 //case Instruction::IGET_OBJECT_VOLATILE:
3335 //case Instruction::IPUT_VOLATILE:
3336 //case Instruction::IPUT_WIDE_VOLATILE:
3337 //case Instruction::IPUT_OBJECT_VOLATILE:
3338 //case Instruction::SGET_VOLATILE:
3339 //case Instruction::SGET_WIDE_VOLATILE:
3340 //case Instruction::SGET_OBJECT_VOLATILE:
3341 //case Instruction::SPUT_VOLATILE:
3342 //case Instruction::SPUT_WIDE_VOLATILE:
3343 //case Instruction::SPUT_OBJECT_VOLATILE:
3344 /* fall through to failure */
3345
3346 /* These should never appear during verification. */
3347 case Instruction::UNUSED_3E:
3348 case Instruction::UNUSED_3F:
3349 case Instruction::UNUSED_40:
3350 case Instruction::UNUSED_41:
3351 case Instruction::UNUSED_42:
3352 case Instruction::UNUSED_43:
3353 case Instruction::UNUSED_73:
3354 case Instruction::UNUSED_79:
3355 case Instruction::UNUSED_7A:
3356 case Instruction::UNUSED_EC:
3357 case Instruction::UNUSED_FF:
3358 //case Instruction::BREAKPOINT:
3359 //case Instruction::DISPATCH_FF:
3360 failure = VERIFY_ERROR_GENERIC;
3361 break;
3362
3363 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003364 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003365 * complain if an instruction is missing (which is desirable).
3366 */
3367 }
3368
3369 if (failure != VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07003370 if (failure == VERIFY_ERROR_GENERIC) {
3371 /* immediate failure, reject class */
3372 LOG(ERROR) << "VFY: rejecting opcode 0x" << std::hex
3373 << (int) dec_insn.opcode_ << " at 0x" << insn_idx << std::dec;
3374 return false;
3375 } else {
3376 /* replace opcode and continue on */
3377 LOG(ERROR) << "VFY: replacing opcode 0x" << std::hex
3378 << (int) dec_insn.opcode_ << " at 0x" << insn_idx << std::dec;
jeffhaob4df5142011-09-19 20:25:32 -07003379 if (!ReplaceFailingInstruction(code_item, insn_idx, failure)) {
jeffhaobdb76512011-09-07 11:43:16 -07003380 LOG(ERROR) << "VFY: rejecting opcode 0x" << std::hex
3381 << (int) dec_insn.opcode_ << " at 0x" << insn_idx
3382 << std::dec;
3383 return false;
3384 }
3385 /* IMPORTANT: method->insns may have been changed */
3386 insns = code_item->insns_ + insn_idx;
3387
3388 /* continue on as if we just handled a throw-verification-error */
3389 failure = VERIFY_ERROR_NONE;
3390 opcode_flag = Instruction::kThrow;
3391 }
3392 }
3393
3394 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003395 * If we didn't just set the result register, clear it out. This
jeffhaobdb76512011-09-07 11:43:16 -07003396 * ensures that you can only use "move-result" immediately after the
jeffhaod1f0fde2011-09-08 17:25:33 -07003397 * result is set. (We could check this statically, but it's not
jeffhaobdb76512011-09-07 11:43:16 -07003398 * expensive and it makes our debugging output cleaner.)
3399 */
3400 if (!just_set_result) {
3401 int reg = RESULT_REGISTER(registers_size);
3402 SetRegisterType(work_line, reg, kRegTypeUnknown);
3403 SetRegisterType(work_line, reg + 1, kRegTypeUnknown);
3404 }
3405
jeffhaoa0a764a2011-09-16 10:43:38 -07003406 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07003407 if ((opcode_flag & Instruction::kContinue) != 0) {
3408 size_t insn_width = InsnGetWidth(insn_flags, insn_idx);
3409 if (insn_idx + insn_width >= insns_size) {
3410 LOG(ERROR) << "VFY: execution can walk off end of code area (from 0x"
3411 << std::hex << insn_idx << std::dec << ")";
3412 return false;
3413 }
3414
3415 /*
3416 * The only way to get to a move-exception instruction is to get
jeffhaod1f0fde2011-09-08 17:25:33 -07003417 * thrown there. Make sure the next instruction isn't one.
jeffhaobdb76512011-09-07 11:43:16 -07003418 */
3419 if (!CheckMoveException(code_item->insns_, insn_idx + insn_width))
3420 return false;
3421
Elliott Hughes5fe594f2011-09-08 12:33:17 -07003422 if (GetRegisterLine(reg_table, insn_idx + insn_width)->reg_types_.get() != NULL) {
jeffhaobdb76512011-09-07 11:43:16 -07003423 /*
3424 * Merge registers into what we have for the next instruction,
3425 * and set the "changed" flag if needed.
3426 */
3427 if (!UpdateRegisters(insn_flags, reg_table, insn_idx + insn_width,
3428 work_line))
3429 return false;
3430 } else {
3431 /*
3432 * We're not recording register data for the next instruction,
jeffhaod1f0fde2011-09-08 17:25:33 -07003433 * so we don't know what the prior state was. We have to
jeffhaobdb76512011-09-07 11:43:16 -07003434 * assume that something has changed and re-evaluate it.
3435 */
3436 InsnSetChanged(insn_flags, insn_idx + insn_width, true);
3437 }
3438 }
3439
3440 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003441 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003442 *
3443 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003444 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003445 * somebody could get a reference field, check it for zero, and if the
3446 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003447 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003448 * that, and will reject the code.
3449 *
3450 * TODO: avoid re-fetching the branch target
3451 */
3452 if ((opcode_flag & Instruction::kBranch) != 0) {
3453 bool isConditional, selfOkay;
3454
3455 if (!GetBranchOffset(code_item, insn_flags, insn_idx, &branch_target,
3456 &isConditional, &selfOkay)) {
3457 /* should never happen after static verification */
3458 LOG(ERROR) << "VFY: bad branch at 0x" << std::hex << insn_idx << std::dec;
3459 return false;
3460 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003461 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
jeffhaobdb76512011-09-07 11:43:16 -07003462
3463 if (!CheckMoveException(code_item->insns_, insn_idx + branch_target))
3464 return false;
3465
3466 /* update branch target, set "changed" if appropriate */
3467 if (!UpdateRegisters(insn_flags, reg_table, insn_idx + branch_target,
3468 work_line))
3469 return false;
3470 }
3471
3472 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003473 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003474 *
3475 * We've already verified that the table is structurally sound, so we
3476 * just need to walk through and tag the targets.
3477 */
3478 if ((opcode_flag & Instruction::kSwitch) != 0) {
3479 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
3480 const uint16_t* switch_insns = insns + offset_to_switch;
3481 int switch_count = switch_insns[1];
3482 int offset_to_targets, targ;
3483
3484 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3485 /* 0 = sig, 1 = count, 2/3 = first key */
3486 offset_to_targets = 4;
3487 } else {
3488 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003489 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003490 offset_to_targets = 2 + 2 * switch_count;
3491 }
3492
3493 /* verify each switch target */
3494 for (targ = 0; targ < switch_count; targ++) {
3495 int offset;
3496 uint32_t abs_offset;
3497
3498 /* offsets are 32-bit, and only partly endian-swapped */
3499 offset = switch_insns[offset_to_targets + targ * 2] |
3500 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
3501 abs_offset = insn_idx + offset;
3502
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003503 DCHECK_LT(abs_offset, insns_size);
jeffhaobdb76512011-09-07 11:43:16 -07003504
3505 if (!CheckMoveException(code_item->insns_, abs_offset))
3506 return false;
3507
3508 if (!UpdateRegisters(insn_flags, reg_table, abs_offset, work_line))
3509 return false;
3510 }
3511 }
3512
3513 /*
3514 * Handle instructions that can throw and that are sitting in a
jeffhaod1f0fde2011-09-08 17:25:33 -07003515 * "try" block. (If they're not in a "try" block when they throw,
jeffhaobdb76512011-09-07 11:43:16 -07003516 * control transfers out of the method.)
3517 */
3518 if ((opcode_flag & Instruction::kThrow) != 0 &&
3519 InsnIsInTry(insn_flags, insn_idx)) {
3520 bool has_catch_all = false;
3521 DexFile::CatchHandlerIterator iterator = DexFile::dexFindCatchHandler(
3522 *code_item, insn_idx);
3523
3524 for (; !iterator.HasNext(); iterator.Next()) {
3525 if (iterator.Get().type_idx_ == DexFile::kDexNoIndex)
3526 has_catch_all = true;
3527
3528 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003529 * Merge registers into the "catch" block. We want to use the
3530 * "savedRegs" rather than "work_regs", because at runtime the
3531 * exception will be thrown before the instruction modifies any
3532 * registers.
jeffhaobdb76512011-09-07 11:43:16 -07003533 */
3534 if (!UpdateRegisters(insn_flags, reg_table, iterator.Get().address_,
3535 &reg_table->saved_line_))
3536 return false;
3537 }
3538
3539 /*
3540 * If the monitor stack depth is nonzero, there must be a "catch all"
jeffhaod1f0fde2011-09-08 17:25:33 -07003541 * handler for this instruction. This does apply to monitor-exit
jeffhaobdb76512011-09-07 11:43:16 -07003542 * because of async exception handling.
3543 */
3544 if (work_line->monitor_stack_top_ != 0 && !has_catch_all) {
3545 /*
3546 * The state in work_line reflects the post-execution state.
3547 * If the current instruction is a monitor-enter and the monitor
3548 * stack was empty, we don't need a catch-all (if it throws,
3549 * it will do so before grabbing the lock).
3550 */
3551 if (!(dec_insn.opcode_ == Instruction::MONITOR_ENTER &&
3552 work_line->monitor_stack_top_ == 1))
3553 {
3554 LOG(ERROR) << "VFY: no catch-all for instruction at 0x" << std::hex
3555 << insn_idx << std::dec;
3556 return false;
3557 }
3558 }
3559 }
3560
jeffhaod1f0fde2011-09-08 17:25:33 -07003561 /* If we're returning from the method, make sure monitor stack is empty. */
jeffhaobdb76512011-09-07 11:43:16 -07003562 if ((opcode_flag & Instruction::kReturn) != 0 &&
3563 work_line->monitor_stack_top_ != 0) {
3564 LOG(ERROR) << "VFY: return with stack depth="
3565 << work_line->monitor_stack_top_ << " at 0x" << std::hex
3566 << insn_idx << std::dec;
3567 return false;
3568 }
3569
3570 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003571 * Update start_guess. Advance to the next instruction of that's
3572 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003573 * neither of those exists we're in a return or throw; leave start_guess
3574 * alone and let the caller sort it out.
3575 */
3576 if ((opcode_flag & Instruction::kContinue) != 0) {
3577 *start_guess = insn_idx + InsnGetWidth(insn_flags, insn_idx);
3578 } else if ((opcode_flag & Instruction::kBranch) != 0) {
3579 /* we're still okay if branch_target is zero */
3580 *start_guess = insn_idx + branch_target;
3581 }
3582
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003583 DCHECK_LT(*start_guess, insns_size);
Brian Carlstrom03c99df2011-09-18 10:52:00 -07003584 DCHECK_NE(InsnGetWidth(insn_flags, *start_guess), 0);
jeffhaobdb76512011-09-07 11:43:16 -07003585
3586 return true;
3587}
3588
3589bool DexVerifier::ReplaceFailingInstruction(const DexFile::CodeItem* code_item,
jeffhaob4df5142011-09-19 20:25:32 -07003590 int insn_idx, VerifyError failure) {
jeffhaobdb76512011-09-07 11:43:16 -07003591 const uint16_t* insns = code_item->insns_ + insn_idx;
3592 const byte* ptr = reinterpret_cast<const byte*>(insns);
3593 const Instruction* inst = Instruction::At(ptr);
3594 Instruction::Code opcode = inst->Opcode();
3595 VerifyErrorRefType ref_type;
3596
3597 /*
3598 * Generate the new instruction out of the old.
3599 *
3600 * First, make sure this is an instruction we're expecting to stomp on.
3601 */
3602 switch (opcode) {
3603 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 bytes
3604 case Instruction::CHECK_CAST:
3605 case Instruction::INSTANCE_OF:
3606 case Instruction::NEW_INSTANCE:
3607 case Instruction::NEW_ARRAY:
3608 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 bytes
3609 case Instruction::FILLED_NEW_ARRAY_RANGE:
3610 ref_type = VERIFY_ERROR_REF_CLASS;
3611 break;
3612
3613 case Instruction::IGET: // insn[1] == field ref, 2 bytes
3614 case Instruction::IGET_BOOLEAN:
3615 case Instruction::IGET_BYTE:
3616 case Instruction::IGET_CHAR:
3617 case Instruction::IGET_SHORT:
3618 case Instruction::IGET_WIDE:
3619 case Instruction::IGET_OBJECT:
3620 case Instruction::IPUT:
3621 case Instruction::IPUT_BOOLEAN:
3622 case Instruction::IPUT_BYTE:
3623 case Instruction::IPUT_CHAR:
3624 case Instruction::IPUT_SHORT:
3625 case Instruction::IPUT_WIDE:
3626 case Instruction::IPUT_OBJECT:
3627 case Instruction::SGET:
3628 case Instruction::SGET_BOOLEAN:
3629 case Instruction::SGET_BYTE:
3630 case Instruction::SGET_CHAR:
3631 case Instruction::SGET_SHORT:
3632 case Instruction::SGET_WIDE:
3633 case Instruction::SGET_OBJECT:
3634 case Instruction::SPUT:
3635 case Instruction::SPUT_BOOLEAN:
3636 case Instruction::SPUT_BYTE:
3637 case Instruction::SPUT_CHAR:
3638 case Instruction::SPUT_SHORT:
3639 case Instruction::SPUT_WIDE:
3640 case Instruction::SPUT_OBJECT:
3641 ref_type = VERIFY_ERROR_REF_FIELD;
3642 break;
3643
3644 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 bytes
3645 case Instruction::INVOKE_VIRTUAL_RANGE:
3646 case Instruction::INVOKE_SUPER:
3647 case Instruction::INVOKE_SUPER_RANGE:
3648 case Instruction::INVOKE_DIRECT:
3649 case Instruction::INVOKE_DIRECT_RANGE:
3650 case Instruction::INVOKE_STATIC:
3651 case Instruction::INVOKE_STATIC_RANGE:
3652 case Instruction::INVOKE_INTERFACE:
3653 case Instruction::INVOKE_INTERFACE_RANGE:
3654 ref_type = VERIFY_ERROR_REF_METHOD;
3655 break;
3656
3657 default:
3658 /* could handle this in a generic way, but this is probably safer */
3659 LOG(ERROR) << "GLITCH: verifier asked to replace opcode 0x" << std::hex
3660 << (int) opcode << std::dec;
3661 return false;
3662 }
3663
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003664 DCHECK(inst->IsThrow());
jeffhaobdb76512011-09-07 11:43:16 -07003665
3666 /* write a NOP over the third code unit, if necessary */
jeffhaob4df5142011-09-19 20:25:32 -07003667 int width = inst->Size();
jeffhaobdb76512011-09-07 11:43:16 -07003668 switch (width) {
3669 case 2:
3670 /* nothing to do */
3671 break;
3672 case 3:
jeffhaob4df5142011-09-19 20:25:32 -07003673 UpdateCodeUnit(insns + 2, Instruction::NOP);
jeffhaobdb76512011-09-07 11:43:16 -07003674 break;
3675 default:
3676 /* whoops */
3677 LOG(FATAL) << "ERROR: stomped a " << width
3678 << "-unit instruction with a verifier error";
3679 }
3680
3681 /* encode the opcode, with the failure code in the high byte */
jeffhaob4df5142011-09-19 20:25:32 -07003682 DCHECK(width == 2 || width == 3);
3683 uint16_t new_val = Instruction::THROW_VERIFICATION_ERROR |
3684 (failure << 8) | (ref_type << (8 + kVerifyErrorRefTypeShift));
3685 UpdateCodeUnit(insns, new_val);
jeffhaobdb76512011-09-07 11:43:16 -07003686
3687 return true;
3688}
3689
jeffhaob4df5142011-09-19 20:25:32 -07003690void DexVerifier::UpdateCodeUnit(const uint16_t* ptr, uint16_t new_val) {
3691 *(uint16_t*) ptr = new_val;
3692}
3693
jeffhaobdb76512011-09-07 11:43:16 -07003694void DexVerifier::HandleMonitorEnter(RegisterLine* work_line, uint32_t reg_idx,
3695 uint32_t insn_idx, VerifyError* failure) {
3696 if (!RegTypeIsReference(GetRegisterType(work_line, reg_idx))) {
3697 LOG(ERROR) << "VFY: monitor-enter on non-object";
3698 *failure = VERIFY_ERROR_GENERIC;
3699 return;
3700 }
3701
Elliott Hughes5fe594f2011-09-08 12:33:17 -07003702 if (work_line->monitor_entries_.get() == NULL) {
jeffhaobdb76512011-09-07 11:43:16 -07003703 return;
3704 }
3705
3706 if (work_line->monitor_stack_top_ == kMaxMonitorStackDepth) {
3707 LOG(ERROR) << "VFY: monitor-enter stack overflow (" << kMaxMonitorStackDepth
3708 << ")";
3709 *failure = VERIFY_ERROR_GENERIC;
3710 return;
3711 }
3712
3713 /*
3714 * Push an entry on the stack, and set a bit in the register flags to
3715 * indicate that it's associated with this register.
3716 */
3717 work_line->monitor_entries_[reg_idx] |= 1 << work_line->monitor_stack_top_;
3718 work_line->monitor_stack_[work_line->monitor_stack_top_++] = insn_idx;
3719}
3720
jeffhaobdb76512011-09-07 11:43:16 -07003721void DexVerifier::HandleMonitorExit(RegisterLine* work_line, uint32_t reg_idx,
3722 uint32_t insn_idx, VerifyError* failure) {
3723 if (!RegTypeIsReference(GetRegisterType(work_line, reg_idx))) {
3724 LOG(ERROR) << "VFY: monitor-exit on non-object";
3725 *failure = VERIFY_ERROR_GENERIC;
3726 return;
3727 }
3728
Elliott Hughes5fe594f2011-09-08 12:33:17 -07003729 if (work_line->monitor_entries_.get() == NULL) {
jeffhaobdb76512011-09-07 11:43:16 -07003730 return;
3731 }
3732
3733 if (work_line->monitor_stack_top_ == 0) {
3734 LOG(ERROR) << "VFY: monitor-exit stack underflow";
3735 *failure = VERIFY_ERROR_GENERIC;
3736 return;
3737 }
3738
3739 /*
3740 * Confirm that the entry at the top of the stack is associated with
jeffhaod1f0fde2011-09-08 17:25:33 -07003741 * the register. Pop the top entry off.
jeffhaobdb76512011-09-07 11:43:16 -07003742 */
3743 work_line->monitor_stack_top_--;
3744#ifdef BUG_3215458_FIXED
3745 /*
3746 * TODO: This code can safely be enabled if know we are working on
3747 * a dex file of format version 036 or later. (That is, we'll need to
3748 * add a check for the version number.)
3749 */
3750 if ((work_line->monitor_entries_[reg_idx] &
3751 (1 << work_line->monitor_stack_top_)) == 0) {
3752 LOG(ERROR) << "VFY: monitor-exit bit " << work_line->monitor_stack_top_
3753 << " not set: addr=0x" << std::hex << insn_idx << std::dec
3754 << " (bits[" << reg_idx << "]=" << std::hex
3755 << work_line->monitor_entries_[reg_idx] << std::dec << ")";
3756 *failure = VERIFY_ERROR_GENERIC;
3757 return;
3758 }
3759#endif
3760 work_line->monitor_stack_[work_line->monitor_stack_top_] = 0;
3761
3762 /* Clear the bit from the register flags. */
3763 work_line->monitor_entries_[reg_idx] &= ~(1 << work_line->monitor_stack_top_);
3764}
3765
3766Field* DexVerifier::GetInstField(VerifierData* vdata, RegType obj_type,
3767 int field_idx, VerifyError* failure) {
3768 Method* method = vdata->method_;
3769 const DexFile* dex_file = vdata->dex_file_;
Elliott Hughes5fe594f2011-09-08 12:33:17 -07003770 UninitInstanceMap* uninit_map = vdata->uninit_map_.get();
jeffhaobdb76512011-09-07 11:43:16 -07003771 bool must_be_local = false;
3772
3773 if (!RegTypeIsReference(obj_type)) {
3774 LOG(ERROR) << "VFY: attempt to access field in non-reference type "
3775 << obj_type;
3776 *failure = VERIFY_ERROR_GENERIC;
jeffhaob4df5142011-09-19 20:25:32 -07003777 return NULL;
jeffhaobdb76512011-09-07 11:43:16 -07003778 }
3779
jeffhaob4df5142011-09-19 20:25:32 -07003780 Field* field = ResolveFieldAndCheckAccess(dex_file, field_idx,
3781 method->GetDeclaringClass(), failure, false);
jeffhaobdb76512011-09-07 11:43:16 -07003782 if (field == NULL) {
3783 LOG(ERROR) << "VFY: unable to resolve instance field " << field_idx;
3784 return field;
3785 }
3786
3787 if (obj_type == kRegTypeZero)
3788 return field;
3789
3790 /*
3791 * Access to fields in uninitialized objects is allowed if this is
3792 * the <init> method for the object and the field in question is
3793 * declared by this class.
3794 */
jeffhaob4df5142011-09-19 20:25:32 -07003795 Class* obj_class = RegTypeReferenceToClass(obj_type, uninit_map);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003796 DCHECK(obj_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07003797 if (RegTypeIsUninitReference(obj_type)) {
3798 if (!IsInitMethod(method) || method->GetDeclaringClass() != obj_class) {
3799 LOG(ERROR) << "VFY: attempt to access field via uninitialized ref";
3800 *failure = VERIFY_ERROR_GENERIC;
3801 return field;
3802 }
3803 must_be_local = true;
3804 }
3805
jeffhaobdb76512011-09-07 11:43:16 -07003806 if (!field->GetDeclaringClass()->IsAssignableFrom(obj_class)) {
3807 LOG(ERROR) << "VFY: invalid field access (field "
3808 << field->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
3809 << "." << field->GetName()->ToModifiedUtf8() << ", through "
3810 << obj_class->GetDescriptor()->ToModifiedUtf8() << " ref)";
3811 *failure = VERIFY_ERROR_NO_FIELD;
3812 return field;
3813 }
3814
3815 if (must_be_local) {
3816 bool found = false;
3817 /* for uninit ref, make sure it's defined by this class, not super */
3818 for (uint32_t i = 0; i < obj_class->NumInstanceFields(); i++) {
3819 found |= (field == obj_class->GetInstanceField(i));
3820 }
3821 if (!found) {
3822 LOG(ERROR) << "VFY: invalid constructor field access (field "
3823 << field->GetName()->ToModifiedUtf8() << " in "
3824 << obj_class->GetDescriptor()->ToModifiedUtf8() << ")";
3825 *failure = VERIFY_ERROR_GENERIC;
3826 return field;
3827 }
3828 }
3829
3830 return field;
3831}
3832
3833Field* DexVerifier::GetStaticField(VerifierData* vdata, int field_idx,
3834 VerifyError* failure) {
3835 Method* method = vdata->method_;
3836 const DexFile* dex_file = vdata->dex_file_;
jeffhaob4df5142011-09-19 20:25:32 -07003837 Field* field = ResolveFieldAndCheckAccess(dex_file, field_idx,
3838 method->GetDeclaringClass(), failure, true);
jeffhaobdb76512011-09-07 11:43:16 -07003839 if (field == NULL) {
jeffhaob4df5142011-09-19 20:25:32 -07003840 const DexFile::FieldId& field_id = dex_file->GetFieldId(field_idx);
3841 LOG(ERROR) << "VFY: unable to resolve static field " << field_idx << " ("
3842 << dex_file->GetFieldName(field_id) << ") in "
3843 << dex_file->GetFieldClassDescriptor(field_id);
3844 *failure = VERIFY_ERROR_NO_FIELD;
jeffhaobdb76512011-09-07 11:43:16 -07003845 }
3846
3847 return field;
3848}
3849
3850Class* DexVerifier::GetCaughtExceptionType(VerifierData* vdata, int insn_idx,
3851 VerifyError* failure) {
3852 const DexFile* dex_file = vdata->dex_file_;
3853 const DexFile::CodeItem* code_item = vdata->code_item_;
3854 Method* method = vdata->method_;
3855 Class* common_super = NULL;
3856 uint32_t handlers_size;
jeffhaoba5ebb92011-08-25 17:24:37 -07003857 const byte* handlers_ptr = DexFile::dexGetCatchHandlerData(*code_item, 0);
jeffhaobdb76512011-09-07 11:43:16 -07003858
3859 if (code_item->tries_size_ != 0) {
3860 handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3861 } else {
3862 handlers_size = 0;
3863 }
3864
3865 for (uint32_t i = 0; i < handlers_size; i++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07003866 DexFile::CatchHandlerIterator iterator(handlers_ptr);
3867
3868 for (; !iterator.HasNext(); iterator.Next()) {
jeffhaobdb76512011-09-07 11:43:16 -07003869 DexFile::CatchHandlerItem handler = iterator.Get();
3870 if (handler.address_ == (uint32_t) insn_idx) {
3871 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3872 Class* klass;
jeffhaoba5ebb92011-08-25 17:24:37 -07003873
jeffhaobdb76512011-09-07 11:43:16 -07003874 if (handler.type_idx_ == DexFile::kDexNoIndex) {
3875 klass = class_linker->FindSystemClass("Ljava/lang/Throwable;");
3876 } else {
jeffhao98eacac2011-09-14 16:11:53 -07003877 klass = ResolveClassAndCheckAccess(dex_file, handler.type_idx_,
3878 method->GetDeclaringClass(), failure);
jeffhaobdb76512011-09-07 11:43:16 -07003879 }
3880
3881 if (klass == NULL) {
3882 LOG(ERROR) << "VFY: unable to resolve exception class "
3883 << handler.type_idx_ << " ("
3884 << dex_file->dexStringByTypeIdx(handler.type_idx_) << ")";
jeffhaod1f0fde2011-09-08 17:25:33 -07003885 /* TODO: do we want to keep going? If we don't fail this we run
3886 * the risk of having a non-Throwable introduced at runtime.
3887 * However, that won't pass an instanceof test, so is essentially
3888 * harmless.
jeffhaobdb76512011-09-07 11:43:16 -07003889 */
3890 } else {
3891 if (common_super == NULL)
3892 common_super = klass;
3893 else
3894 common_super = FindCommonSuperclass(klass, common_super);
3895 }
3896 }
jeffhaoba5ebb92011-08-25 17:24:37 -07003897 }
3898
3899 handlers_ptr = iterator.GetData();
3900 }
3901
jeffhaobdb76512011-09-07 11:43:16 -07003902 if (common_super == NULL) {
3903 /* no catch blocks, or no catches with classes we can find */
3904 LOG(ERROR) << "VFY: unable to find exception handler at addr 0x" << std::hex
3905 << insn_idx << std::dec;
3906 *failure = VERIFY_ERROR_GENERIC;
3907 }
3908
3909 return common_super;
jeffhaoba5ebb92011-08-25 17:24:37 -07003910}
3911
jeffhaobdb76512011-09-07 11:43:16 -07003912DexVerifier::RegType DexVerifier::GetMethodReturnType(const DexFile* dex_file,
3913 const Method* method) {
3914 Class* klass = method->GetReturnType();
3915 if (klass->IsPrimitive())
3916 return PrimitiveTypeToRegType(klass->GetPrimitiveType());
3917 else
3918 return RegTypeFromClass(klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003919}
3920
jeffhaobdb76512011-09-07 11:43:16 -07003921Class* DexVerifier::GetClassFromRegister(const RegisterLine* register_line,
3922 uint32_t vsrc, VerifyError* failure) {
3923 /* get the element type of the array held in vsrc */
3924 RegType type = GetRegisterType(register_line, vsrc);
jeffhaoba5ebb92011-08-25 17:24:37 -07003925
jeffhaobdb76512011-09-07 11:43:16 -07003926 /* if "always zero", we allow it to fail at runtime */
3927 if (type == kRegTypeZero)
3928 return NULL;
3929
3930 if (!RegTypeIsReference(type)) {
3931 LOG(ERROR) << "VFY: tried to get class from non-ref register v" << vsrc
3932 << " (type=" << type << ")",
3933 *failure = VERIFY_ERROR_GENERIC;
3934 return NULL;
3935 }
3936 if (RegTypeIsUninitReference(type)) {
3937 LOG(ERROR) << "VFY: register " << vsrc << " holds uninitialized reference";
3938 *failure = VERIFY_ERROR_GENERIC;
3939 return NULL;
jeffhaoba5ebb92011-08-25 17:24:37 -07003940 }
3941
jeffhaobdb76512011-09-07 11:43:16 -07003942 return RegTypeInitializedReferenceToClass(type);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003943}
3944
jeffhaobdb76512011-09-07 11:43:16 -07003945DexVerifier::RegType DexVerifier::GetInvocationThis(
3946 const RegisterLine* register_line,
3947 const Instruction::DecodedInstruction* dec_insn, VerifyError* failure) {
3948 if (dec_insn->vA_ < 1) {
3949 LOG(ERROR) << "VFY: invoke lacks 'this'";
3950 *failure = VERIFY_ERROR_GENERIC;
3951 return kRegTypeUnknown;
jeffhaoba5ebb92011-08-25 17:24:37 -07003952 }
jeffhaobdb76512011-09-07 11:43:16 -07003953
3954 /* get the element type of the array held in vsrc */
3955 RegType this_type = GetRegisterType(register_line, dec_insn->vC_);
3956 if (!RegTypeIsReference(this_type)) {
3957 LOG(ERROR) << "VFY: tried to get class from non-ref register v"
3958 << dec_insn->vC_ << " (type=" << this_type << ")";
3959 *failure = VERIFY_ERROR_GENERIC;
3960 return kRegTypeUnknown;
3961 }
3962
3963 return this_type;
jeffhaoba5ebb92011-08-25 17:24:37 -07003964}
3965
jeffhaobdb76512011-09-07 11:43:16 -07003966void DexVerifier::SetRegisterType(RegisterLine* register_line, uint32_t vdst,
3967 RegType new_type) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07003968 RegType* insn_regs = register_line->reg_types_.get();
jeffhaoba5ebb92011-08-25 17:24:37 -07003969
jeffhaobdb76512011-09-07 11:43:16 -07003970 switch (new_type) {
3971 case kRegTypeUnknown:
3972 case kRegTypeBoolean:
3973 case kRegTypeOne:
3974 case kRegTypeConstByte:
3975 case kRegTypeConstPosByte:
3976 case kRegTypeConstShort:
3977 case kRegTypeConstPosShort:
3978 case kRegTypeConstChar:
3979 case kRegTypeConstInteger:
3980 case kRegTypeByte:
3981 case kRegTypePosByte:
3982 case kRegTypeShort:
3983 case kRegTypePosShort:
3984 case kRegTypeChar:
3985 case kRegTypeInteger:
3986 case kRegTypeFloat:
3987 case kRegTypeZero:
3988 case kRegTypeUninit:
3989 insn_regs[vdst] = new_type;
jeffhaoba5ebb92011-08-25 17:24:37 -07003990 break;
jeffhaobdb76512011-09-07 11:43:16 -07003991 case kRegTypeConstLo:
3992 case kRegTypeLongLo:
3993 case kRegTypeDoubleLo:
3994 insn_regs[vdst] = new_type;
3995 insn_regs[vdst + 1] = new_type + 1;
3996 break;
3997 case kRegTypeConstHi:
3998 case kRegTypeLongHi:
3999 case kRegTypeDoubleHi:
4000 /* should never set these explicitly */
4001 LOG(FATAL) << "BUG: explicit set of high register type";
4002 break;
4003
4004 default:
4005 /* can't switch for ref types, so we check explicitly */
4006 if (RegTypeIsReference(new_type)) {
4007 insn_regs[vdst] = new_type;
4008
4009 /*
4010 * In most circumstances we won't see a reference to a primitive
4011 * class here (e.g. "D"), since that would mean the object in the
jeffhaod1f0fde2011-09-08 17:25:33 -07004012 * register is actually a primitive type. It can happen as the
jeffhaobdb76512011-09-07 11:43:16 -07004013 * result of an assumed-successful check-cast instruction in
jeffhaod1f0fde2011-09-08 17:25:33 -07004014 * which the second argument refers to a primitive class. (In
jeffhaobdb76512011-09-07 11:43:16 -07004015 * practice, such an instruction will always throw an exception.)
4016 *
4017 * This is not an issue for instructions like const-class, where
4018 * the object in the register is a java.lang.Class instance.
4019 */
4020 break;
4021 }
4022 /* bad type - fall through */
4023
4024 case kRegTypeConflict: // should only be set during a merge
4025 LOG(FATAL) << "BUG: set register to unknown type " << new_type;
jeffhaoba5ebb92011-08-25 17:24:37 -07004026 break;
4027 }
4028
jeffhaobdb76512011-09-07 11:43:16 -07004029 /*
4030 * Clear the monitor entry bits for this register.
4031 */
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004032 if (register_line->monitor_entries_.get() != NULL)
jeffhaobdb76512011-09-07 11:43:16 -07004033 register_line->monitor_entries_[vdst] = 0;
4034}
4035
4036void DexVerifier::VerifyRegisterType(RegisterLine* register_line, uint32_t vsrc,
4037 RegType check_type, VerifyError* failure) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004038 const RegType* insn_regs = register_line->reg_types_.get();
jeffhaobdb76512011-09-07 11:43:16 -07004039 RegType src_type = insn_regs[vsrc];
4040
4041 switch (check_type) {
4042 case kRegTypeFloat:
4043 case kRegTypeBoolean:
4044 case kRegTypePosByte:
4045 case kRegTypeByte:
4046 case kRegTypePosShort:
4047 case kRegTypeShort:
4048 case kRegTypeChar:
4049 case kRegTypeInteger:
4050 if (!CanConvertTo1nr(src_type, check_type)) {
4051 LOG(ERROR) << "VFY: register1 v" << vsrc << " type " << src_type
4052 << ", wanted " << check_type;
4053 *failure = VERIFY_ERROR_GENERIC;
4054 }
4055 /* Update type if result is float */
4056 if (check_type == kRegTypeFloat) {
4057 SetRegisterType(register_line, vsrc, check_type);
4058 } else {
4059 /* Update const type to actual type after use */
4060 SetRegisterType(register_line, vsrc, ConstTypeToRegType(src_type));
4061 }
jeffhaoba5ebb92011-08-25 17:24:37 -07004062 break;
jeffhaobdb76512011-09-07 11:43:16 -07004063 case kRegTypeLongLo:
4064 case kRegTypeDoubleLo:
4065 if (insn_regs[vsrc + 1] != src_type + 1) {
4066 LOG(ERROR) << "VFY: register2 v" << vsrc << "-" << vsrc + 1
4067 << " values " << insn_regs[vsrc] << ","
4068 << insn_regs[vsrc + 1];
4069 *failure = VERIFY_ERROR_GENERIC;
4070 } else if (!CanConvertTo2(src_type, check_type)) {
4071 LOG(ERROR) << "VFY: register2 v" << vsrc << " type " << src_type
4072 << ", wanted " << check_type;
4073 *failure = VERIFY_ERROR_GENERIC;
4074 }
4075 /* Update type if source is from const */
4076 if (src_type == kRegTypeConstLo) {
4077 SetRegisterType(register_line, vsrc, check_type);
4078 }
jeffhaoba5ebb92011-08-25 17:24:37 -07004079 break;
jeffhaobdb76512011-09-07 11:43:16 -07004080 case kRegTypeConstLo:
4081 case kRegTypeConstHi:
4082 case kRegTypeLongHi:
4083 case kRegTypeDoubleHi:
4084 case kRegTypeZero:
4085 case kRegTypeOne:
4086 case kRegTypeUnknown:
4087 case kRegTypeConflict:
4088 /* should never be checking for these explicitly */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004089 DCHECK(false);
jeffhaobdb76512011-09-07 11:43:16 -07004090 *failure = VERIFY_ERROR_GENERIC;
4091 return;
4092 case kRegTypeUninit:
4093 default:
4094 /* make sure check_type is initialized reference */
4095 if (!RegTypeIsReference(check_type)) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004096 LOG(FATAL) << "VFY: unexpected check type " << check_type;
jeffhaobdb76512011-09-07 11:43:16 -07004097 *failure = VERIFY_ERROR_GENERIC;
4098 break;
4099 }
4100 if (RegTypeIsUninitReference(check_type)) {
4101 LOG(ERROR) << "VFY: uninitialized ref not expected as reg check";
4102 *failure = VERIFY_ERROR_GENERIC;
4103 break;
4104 }
4105 /* make sure src_type is initialized reference or always-NULL */
4106 if (!RegTypeIsReference(src_type)) {
4107 LOG(ERROR) << "VFY: register1 v" << vsrc << " type " << src_type
4108 << ", wanted ref";
4109 *failure = VERIFY_ERROR_GENERIC;
4110 break;
4111 }
4112 if (RegTypeIsUninitReference(src_type)) {
4113 LOG(ERROR) << "VFY: register1 v" << vsrc << " holds uninitialized ref";
4114 *failure = VERIFY_ERROR_GENERIC;
4115 break;
4116 }
4117 /* if the register isn't Zero, make sure it's an instance of check */
4118 if (src_type != kRegTypeZero) {
4119 Class* src_class = RegTypeInitializedReferenceToClass(src_type);
4120 Class* check_class = RegTypeInitializedReferenceToClass(check_type);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004121 DCHECK(src_class != NULL);
4122 DCHECK(check_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07004123
4124 if (!check_class->IsAssignableFrom(src_class)) {
4125 LOG(ERROR) << "VFY: " << src_class->GetDescriptor()->ToModifiedUtf8()
4126 << " is not instance of "
4127 << check_class->GetDescriptor()->ToModifiedUtf8();
4128 *failure = VERIFY_ERROR_GENERIC;
4129 }
4130 }
jeffhaoba5ebb92011-08-25 17:24:37 -07004131 break;
4132 }
jeffhaobdb76512011-09-07 11:43:16 -07004133}
jeffhaoba5ebb92011-08-25 17:24:37 -07004134
jeffhaobdb76512011-09-07 11:43:16 -07004135void DexVerifier::SetResultRegisterType(RegisterLine* register_line,
4136 const int insn_reg_count, RegType new_type) {
4137 SetRegisterType(register_line, RESULT_REGISTER(insn_reg_count), new_type);
4138}
4139
4140void DexVerifier::MarkRefsAsInitialized(RegisterLine* register_line,
4141 int insn_reg_count, UninitInstanceMap* uninit_map, RegType uninit_type,
4142 VerifyError* failure) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004143 RegType* insn_regs = register_line->reg_types_.get();
jeffhaobdb76512011-09-07 11:43:16 -07004144 Class* klass = GetUninitInstance(uninit_map,
4145 RegTypeToUninitIndex(uninit_type));
4146
4147 if (klass == NULL) {
4148 LOG(ERROR) << "VFY: unable to find type=" << std::hex << uninit_type
4149 << std::dec << " (idx=" << RegTypeToUninitIndex(uninit_type)
4150 << ")";
4151 *failure = VERIFY_ERROR_GENERIC;
4152 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07004153 }
4154
jeffhaobdb76512011-09-07 11:43:16 -07004155 RegType init_type = RegTypeFromClass(klass);
4156 int changed = 0;
4157 for (int i = 0; i < insn_reg_count; i++) {
4158 if (insn_regs[i] == uninit_type) {
4159 insn_regs[i] = init_type;
4160 changed++;
4161 }
4162 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004163 DCHECK_GT(changed, 0);
jeffhaobdb76512011-09-07 11:43:16 -07004164
4165 return;
4166}
4167
4168void DexVerifier::MarkUninitRefsAsInvalid(RegisterLine* register_line,
4169 int insn_reg_count, UninitInstanceMap* uninit_map, RegType uninit_type) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004170 RegType* insn_regs = register_line->reg_types_.get();
jeffhaobdb76512011-09-07 11:43:16 -07004171
4172 for (int i = 0; i < insn_reg_count; i++) {
4173 if (insn_regs[i] == uninit_type) {
4174 insn_regs[i] = kRegTypeConflict;
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004175 if (register_line->monitor_entries_.get() != NULL)
jeffhaobdb76512011-09-07 11:43:16 -07004176 register_line->monitor_entries_[i] = 0;
4177 }
4178 }
4179}
4180
4181void DexVerifier::CopyRegister1(RegisterLine* register_line, uint32_t vdst,
4182 uint32_t vsrc, TypeCategory cat, VerifyError* failure) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004183 DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07004184 RegType type = GetRegisterType(register_line, vsrc);
4185 CheckTypeCategory(type, cat, failure);
4186 if (*failure != VERIFY_ERROR_NONE) {
4187 LOG(ERROR) << "VFY: copy1 v" << vdst << "<-v" << vsrc << " type=" << type
4188 << " cat=" << (int) cat;
4189 } else {
4190 SetRegisterType(register_line, vdst, type);
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004191 if (cat == kTypeCategoryRef && register_line->monitor_entries_.get() != NULL) {
jeffhaobdb76512011-09-07 11:43:16 -07004192 register_line->monitor_entries_[vdst] =
4193 register_line->monitor_entries_[vsrc];
4194 }
4195 }
4196}
4197
4198void DexVerifier::CopyRegister2(RegisterLine* register_line, uint32_t vdst,
4199 uint32_t vsrc, VerifyError* failure) {
4200 RegType type_l = GetRegisterType(register_line, vsrc);
4201 RegType type_h = GetRegisterType(register_line, vsrc + 1);
4202
4203 CheckTypeCategory(type_l, kTypeCategory2, failure);
4204 CheckWidePair(type_l, type_h, failure);
4205 if (*failure != VERIFY_ERROR_NONE) {
4206 LOG(ERROR) << "VFY: copy2 v" << vdst << "<-v" << vsrc << " type=" << type_l
4207 << "/" << type_h;
4208 } else {
4209 SetRegisterType(register_line, vdst, type_l);
4210 }
4211}
4212
4213void DexVerifier::CopyResultRegister1(RegisterLine* register_line,
4214 const int insn_reg_count, uint32_t vdst, TypeCategory cat,
4215 VerifyError* failure) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004216 DCHECK_LT(vdst, static_cast<uint32_t>(insn_reg_count));
jeffhaobdb76512011-09-07 11:43:16 -07004217
4218 uint32_t vsrc = RESULT_REGISTER(insn_reg_count);
4219 RegType type = GetRegisterType(register_line, vsrc);
4220 CheckTypeCategory(type, cat, failure);
4221 if (*failure != VERIFY_ERROR_NONE) {
4222 LOG(ERROR) << "VFY: copyRes1 v" << vdst << "<-v" << vsrc << " cat="
4223 << (int) cat << " type=" << type;
4224 } else {
4225 SetRegisterType(register_line, vdst, type);
4226 SetRegisterType(register_line, vsrc, kRegTypeUnknown);
4227 }
4228}
4229
4230/*
jeffhaod1f0fde2011-09-08 17:25:33 -07004231 * Implement "move-result-wide". Copy the category-2 value from the result
jeffhaobdb76512011-09-07 11:43:16 -07004232 * register to another register, and reset the result register.
4233 */
4234void DexVerifier::CopyResultRegister2(RegisterLine* register_line,
4235 const int insn_reg_count, uint32_t vdst, VerifyError* failure) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004236 DCHECK_LT(vdst, static_cast<uint32_t>(insn_reg_count));
jeffhaobdb76512011-09-07 11:43:16 -07004237
4238 uint32_t vsrc = RESULT_REGISTER(insn_reg_count);
4239 RegType type_l = GetRegisterType(register_line, vsrc);
4240 RegType type_h = GetRegisterType(register_line, vsrc + 1);
4241 CheckTypeCategory(type_l, kTypeCategory2, failure);
4242 CheckWidePair(type_l, type_h, failure);
4243 if (*failure != VERIFY_ERROR_NONE) {
4244 LOG(ERROR) << "VFY: copyRes2 v" << vdst << "<-v" << vsrc << " type="
4245 << type_l << "/" << type_h;
4246 } else {
4247 SetRegisterType(register_line, vdst, type_l);
4248 SetRegisterType(register_line, vsrc, kRegTypeUnknown);
4249 SetRegisterType(register_line, vsrc + 1, kRegTypeUnknown);
4250 }
4251}
4252
4253int DexVerifier::GetClassDepth(Class* klass) {
4254 int depth = 0;
4255 while (klass->GetSuperClass() != NULL) {
4256 klass = klass->GetSuperClass();
4257 depth++;
4258 }
4259 return depth;
4260}
4261
4262Class* DexVerifier::DigForSuperclass(Class* c1, Class* c2) {
4263 int depth1, depth2;
4264
4265 depth1 = GetClassDepth(c1);
4266 depth2 = GetClassDepth(c2);
4267
4268 /* pull the deepest one up */
4269 if (depth1 > depth2) {
4270 while (depth1 > depth2) {
4271 c1 = c1->GetSuperClass();
4272 depth1--;
4273 }
4274 } else {
4275 while (depth2 > depth1) {
4276 c2 = c2->GetSuperClass();
4277 depth2--;
4278 }
jeffhaoba5ebb92011-08-25 17:24:37 -07004279 }
4280
jeffhaobdb76512011-09-07 11:43:16 -07004281 /* walk up in lock-step */
4282 while (c1 != c2) {
4283 c1 = c1->GetSuperClass();
4284 c2 = c2->GetSuperClass();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004285 DCHECK(c1 != NULL);
4286 DCHECK(c2 != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07004287 }
4288
4289 return c1;
4290}
4291
4292Class* DexVerifier::FindCommonArraySuperclass(Class* c1, Class* c2) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004293 DCHECK(c1->IsArrayClass());
4294 DCHECK(c2->IsArrayClass());
4295 Class* e1 = c1->GetComponentType();
4296 Class* e2 = c2->GetComponentType();
4297 if (e1->IsPrimitive() || e2->IsPrimitive()) {
4298 return c1->GetSuperClass(); // == java.lang.Object
jeffhaobdb76512011-09-07 11:43:16 -07004299 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004300 Class* common_elem = FindCommonSuperclass(c1->GetComponentType(), c2->GetComponentType());
4301 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
4302 const ClassLoader* class_loader = c1->GetClassLoader();
4303 std::string descriptor = "[" + common_elem->GetDescriptor()->ToModifiedUtf8();
4304 Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
4305 DCHECK(array_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07004306 return array_class;
4307}
4308
4309Class* DexVerifier::FindCommonSuperclass(Class* c1, Class* c2) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004310 DCHECK(!c1->IsPrimitive()) << PrettyClass(c1);
4311 DCHECK(!c2->IsPrimitive()) << PrettyClass(c2);
jeffhaobdb76512011-09-07 11:43:16 -07004312
4313 if (c1 == c2)
4314 return c1;
4315
jeffhao5dbddee2011-09-07 16:38:26 -07004316 if (c1->IsInterface() && c1->IsAssignableFrom(c2)) {
jeffhaobdb76512011-09-07 11:43:16 -07004317 return c1;
4318 }
jeffhao5dbddee2011-09-07 16:38:26 -07004319 if (c2->IsInterface() && c2->IsAssignableFrom(c1)) {
jeffhaobdb76512011-09-07 11:43:16 -07004320 return c2;
4321 }
4322 if (c1->IsArrayClass() && c2->IsArrayClass()) {
4323 return FindCommonArraySuperclass(c1, c2);
4324 }
4325
4326 return DigForSuperclass(c1, c2);
4327}
4328
jeffhao98eacac2011-09-14 16:11:53 -07004329Class* DexVerifier::ResolveClassAndCheckAccess(const DexFile* dex_file,
jeffhaob4df5142011-09-19 20:25:32 -07004330 uint32_t class_idx, const Class* referrer, VerifyError* failure) {
jeffhao98eacac2011-09-14 16:11:53 -07004331 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
4332 Class* res_class = class_linker->ResolveType(*dex_file, class_idx, referrer);
4333
4334 if (res_class == NULL) {
4335 *failure = VERIFY_ERROR_NO_CLASS;
4336 return NULL;
4337 }
4338
4339 /* Check if access is allowed. */
4340 if (!referrer->CanAccess(res_class)) {
4341 LOG(ERROR) << "VFY: illegal class access: "
4342 << referrer->GetDescriptor()->ToModifiedUtf8() << " -> "
4343 << res_class->GetDescriptor()->ToModifiedUtf8();
4344 *failure = VERIFY_ERROR_ACCESS_CLASS;
4345 return NULL;
4346 }
4347
4348 return res_class;
4349}
4350
jeffhaob4df5142011-09-19 20:25:32 -07004351Method* DexVerifier::ResolveMethodAndCheckAccess(const DexFile* dex_file,
4352 uint32_t method_idx, const Class* referrer, VerifyError* failure,
4353 bool is_direct) {
4354 DexCache* dex_cache = referrer->GetDexCache();
4355 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
4356
4357 if (res_method == NULL) {
4358 const DexFile::MethodId& method_id = dex_file->GetMethodId(method_idx);
4359 Class* klass = ResolveClassAndCheckAccess(dex_file, method_id.class_idx_, referrer, failure);
4360 if (klass == NULL) {
4361 DCHECK(*failure != VERIFY_ERROR_NONE);
4362 return NULL;
4363 }
4364
4365 const char* name = dex_file->dexStringById(method_id.name_idx_);
4366 std::string signature(dex_file->CreateMethodDescriptor(method_id.proto_idx_, NULL));
4367 if (is_direct) {
4368 res_method = klass->FindDirectMethod(name, signature);
4369 } else if (klass->IsInterface()) {
4370 res_method = klass->FindInterfaceMethod(name, signature);
4371 } else {
4372 res_method = klass->FindVirtualMethod(name, signature);
4373 }
4374 if (res_method != NULL) {
4375 dex_cache->SetResolvedMethod(method_idx, res_method);
4376 } else {
4377 LOG(ERROR) << "VFY: couldn't find method "
4378 << klass->GetDescriptor()->ToModifiedUtf8() << "." << name
4379 << " " << signature;
4380 *failure = VERIFY_ERROR_NO_METHOD;
4381 return NULL;
4382 }
4383 }
4384
4385 /* Check if access is allowed. */
4386 if (!referrer->CanAccess(res_method->GetDeclaringClass())) {
4387 LOG(ERROR) << "VFY: illegal method access (call "
4388 << res_method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
4389 << "." << res_method->GetName() << " "
4390 << res_method->GetSignature() << " from "
4391 << referrer->GetDescriptor()->ToModifiedUtf8() << ")";
4392 *failure = VERIFY_ERROR_ACCESS_METHOD;
4393 return NULL;
4394 }
4395
4396 return res_method;
4397}
4398
4399Field* DexVerifier::ResolveFieldAndCheckAccess(const DexFile* dex_file,
4400 uint32_t field_idx, const Class* referrer, VerifyError* failure,
4401 bool is_static) {
4402 DexCache* dex_cache = referrer->GetDexCache();
4403 Field* res_field = dex_cache->GetResolvedField(field_idx);
4404
4405 if (res_field == NULL) {
4406 const DexFile::FieldId& field_id = dex_file->GetFieldId(field_idx);
4407 Class* klass = ResolveClassAndCheckAccess(dex_file, field_id.class_idx_, referrer, failure);
4408 if (klass == NULL) {
4409 DCHECK(*failure != VERIFY_ERROR_NONE);
4410 return NULL;
4411 }
4412
4413 Class* field_type = ResolveClassAndCheckAccess(dex_file, field_id.type_idx_, referrer, failure);
4414 if (field_type == NULL) {
4415 DCHECK(*failure != VERIFY_ERROR_NONE);
4416 return NULL;
4417 }
4418
4419 const char* name = dex_file->dexStringById(field_id.name_idx_);
4420 if (is_static) {
4421 res_field = klass->FindStaticField(name, field_type);
4422 } else {
4423 res_field = klass->FindInstanceField(name, field_type);
4424 }
4425 if (res_field != NULL) {
Elliott Hughes4a2b4172011-09-20 17:08:25 -07004426 dex_cache->SetResolvedField(field_idx, res_field);
jeffhaob4df5142011-09-19 20:25:32 -07004427 } else {
4428 LOG(ERROR) << "VFY: couldn't find field "
4429 << klass->GetDescriptor()->ToModifiedUtf8() << "." << name;
4430 *failure = VERIFY_ERROR_NO_FIELD;
4431 return NULL;
4432 }
4433 }
4434
4435 /* Check if access is allowed. */
4436 if (!referrer->CanAccess(res_field->GetDeclaringClass())) {
4437 LOG(ERROR) << "VFY: access denied from "
4438 << referrer->GetDescriptor()->ToModifiedUtf8() << " to field "
4439 << res_field->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
4440 << "." << res_field->GetName()->ToModifiedUtf8();
4441 *failure = VERIFY_ERROR_ACCESS_FIELD;
4442 return NULL;
4443 }
4444
4445 return res_field;
4446}
4447
jeffhaobdb76512011-09-07 11:43:16 -07004448DexVerifier::RegType DexVerifier::MergeTypes(RegType type1, RegType type2,
4449 bool* changed) {
4450 RegType result;
4451
jeffhao98eacac2011-09-14 16:11:53 -07004452 /* Check for trivial case so we don't have to hit memory. */
jeffhaobdb76512011-09-07 11:43:16 -07004453 if (type1 == type2)
4454 return type1;
4455
4456 /*
4457 * Use the table if we can, and reject any attempts to merge something
4458 * from the table with a reference type.
4459 *
4460 * Uninitialized references are composed of the enum ORed with an
jeffhaod1f0fde2011-09-08 17:25:33 -07004461 * index value. The uninitialized table entry at index zero *will*
4462 * show up as a simple kRegTypeUninit value. Since this cannot be
jeffhaobdb76512011-09-07 11:43:16 -07004463 * merged with anything but itself, the rules do the right thing.
4464 */
4465 if (type1 < kRegTypeMAX) {
4466 if (type2 < kRegTypeMAX) {
4467 result = merge_table_[type1][type2];
4468 } else {
4469 /* simple + reference == conflict, usually */
4470 if (type1 == kRegTypeZero)
4471 result = type2;
4472 else
4473 result = kRegTypeConflict;
4474 }
4475 } else {
4476 if (type2 < kRegTypeMAX) {
4477 /* reference + simple == conflict, usually */
4478 if (type2 == kRegTypeZero)
4479 result = type1;
4480 else
4481 result = kRegTypeConflict;
4482 } else {
4483 /* merging two references */
4484 if (RegTypeIsUninitReference(type1) ||
4485 RegTypeIsUninitReference(type2))
4486 {
4487 /* can't merge uninit with anything but self */
4488 result = kRegTypeConflict;
4489 } else {
4490 Class* klass1 = RegTypeInitializedReferenceToClass(type1);
4491 Class* klass2 = RegTypeInitializedReferenceToClass(type2);
4492 Class* merged_class = FindCommonSuperclass(klass1, klass2);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004493 DCHECK(merged_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07004494 result = RegTypeFromClass(merged_class);
4495 }
4496 }
4497 }
4498
4499 if (result != type1)
4500 *changed = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07004501 return result;
jeffhaobdb76512011-09-07 11:43:16 -07004502}
4503
4504DexVerifier::MonitorEntries DexVerifier::MergeMonitorEntries(
4505 MonitorEntries ents1, MonitorEntries ents2, bool* changed) {
4506 MonitorEntries result = ents1 & ents2;
4507 if (result != ents1)
4508 *changed = true;
4509 return result;
4510}
4511
4512bool DexVerifier::UpdateRegisters(InsnFlags* insn_flags,
4513 RegisterTable* reg_table, int next_insn, const RegisterLine* work_line) {
4514 const size_t insn_reg_count_plus = reg_table->insn_reg_count_plus_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004515 DCHECK(work_line != NULL);
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004516 const RegType* work_regs = work_line->reg_types_.get();
jeffhaobdb76512011-09-07 11:43:16 -07004517
4518 if (!InsnIsVisitedOrChanged(insn_flags, next_insn)) {
4519 /*
4520 * We haven't processed this instruction before, and we haven't
jeffhaod1f0fde2011-09-08 17:25:33 -07004521 * touched the registers here, so there's nothing to "merge". Copy
4522 * the registers over and mark it as changed. (This is the only
jeffhaobdb76512011-09-07 11:43:16 -07004523 * way a register can transition out of "unknown", so this is not
4524 * just an optimization.)
4525 */
4526 CopyLineToTable(reg_table, next_insn, work_line);
4527 InsnSetChanged(insn_flags, next_insn, true);
4528 } else {
4529 /* Merge registers, set Changed only if different */
4530 RegisterLine* target_line = GetRegisterLine(reg_table, next_insn);
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004531 RegType* target_regs = target_line->reg_types_.get();
4532 MonitorEntries* work_mon_ents = work_line->monitor_entries_.get();
4533 MonitorEntries* target_mon_ents = target_line->monitor_entries_.get();
jeffhaobdb76512011-09-07 11:43:16 -07004534 bool changed = false;
4535 unsigned int idx;
4536
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004537 DCHECK(target_regs != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07004538 if (target_mon_ents != NULL) {
4539 /* Monitor stacks must be identical. */
4540 if (target_line->monitor_stack_top_ != work_line->monitor_stack_top_) {
4541 LOG(ERROR) << "VFY: mismatched stack depth "
4542 << target_line->monitor_stack_top_ << " vs. "
4543 << work_line->monitor_stack_top_ << " at 0x"
4544 << std::hex << next_insn << std::dec;
4545 return false;
4546 }
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004547 if (memcmp(target_line->monitor_stack_.get(), work_line->monitor_stack_.get(),
jeffhaobdb76512011-09-07 11:43:16 -07004548 target_line->monitor_stack_top_ * sizeof(uint32_t)) != 0) {
4549 LOG(ERROR) << "VFY: mismatched monitor stacks at 0x" << std::hex
4550 << next_insn << std::dec;
4551 return false;
4552 }
4553 }
4554
4555 for (idx = 0; idx < insn_reg_count_plus; idx++) {
4556 target_regs[idx] = MergeTypes(target_regs[idx], work_regs[idx], &changed);
4557
4558 if (target_mon_ents != NULL) {
4559 target_mon_ents[idx] = MergeMonitorEntries(target_mon_ents[idx],
4560 work_mon_ents[idx], &changed);
4561 }
4562 }
4563
4564 if (changed) {
4565 InsnSetChanged(insn_flags, next_insn, true);
4566 }
4567 }
4568
4569 return true;
4570}
4571
4572bool DexVerifier::CanConvertTo1nr(RegType src_type, RegType check_type) {
4573 static const char conv_tab[kRegType1nrEND - kRegType1nrSTART + 1]
4574 [kRegType1nrEND - kRegType1nrSTART + 1] =
4575 {
4576 /* chk: 0 1 Z y Y h H c i b B s S C I F */
4577 { /*0*/ 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
4578 { /*1*/ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
4579 { /*Z*/ 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
4580 { /*y*/ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
4581 { /*Y*/ 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1 },
4582 { /*h*/ 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1 },
4583 { /*H*/ 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1 },
4584 { /*c*/ 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1 },
4585 { /*i*/ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1 },
4586 { /*b*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0 },
4587 { /*B*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0 },
4588 { /*s*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0 },
4589 { /*S*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0 },
4590 { /*C*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 },
4591 { /*I*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
4592 { /*F*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
4593 };
4594
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004595 DCHECK(check_type >= kRegType1nrSTART);
4596 DCHECK(check_type <= kRegType1nrEND);
jeffhaobdb76512011-09-07 11:43:16 -07004597
4598 if (src_type >= kRegType1nrSTART && src_type <= kRegType1nrEND)
4599 return (bool) conv_tab[src_type - kRegType1nrSTART]
4600 [check_type - kRegType1nrSTART];
4601
4602 return false;
4603}
4604
4605bool DexVerifier::CanConvertTo2(RegType src_type, RegType check_type) {
4606 return ((src_type == kRegTypeConstLo || src_type == check_type) &&
4607 (check_type == kRegTypeLongLo || check_type == kRegTypeDoubleLo));
4608}
4609
4610DexVerifier::RegType DexVerifier::PrimitiveTypeToRegType(
4611 Class::PrimitiveType prim_type) {
4612 switch (prim_type) {
4613 case Class::kPrimBoolean: return kRegTypeBoolean;
4614 case Class::kPrimByte: return kRegTypeByte;
4615 case Class::kPrimShort: return kRegTypeShort;
4616 case Class::kPrimChar: return kRegTypeChar;
4617 case Class::kPrimInt: return kRegTypeInteger;
4618 case Class::kPrimLong: return kRegTypeLongLo;
4619 case Class::kPrimFloat: return kRegTypeFloat;
4620 case Class::kPrimDouble: return kRegTypeDoubleLo;
4621 case Class::kPrimVoid:
4622 default: {
4623 return kRegTypeUnknown;
4624 }
4625 }
4626}
4627
4628DexVerifier::RegType DexVerifier::ConstTypeToRegType(RegType const_type) {
4629 switch (const_type) {
4630 case kRegTypeConstPosByte: return kRegTypePosByte;
4631 case kRegTypeConstByte: return kRegTypeByte;
4632 case kRegTypeConstPosShort: return kRegTypePosShort;
4633 case kRegTypeConstShort: return kRegTypeShort;
4634 case kRegTypeConstChar: return kRegTypeChar;
4635 case kRegTypeConstInteger: return kRegTypeInteger;
4636 default: {
4637 return const_type;
4638 }
4639 }
4640}
4641
4642char DexVerifier::DetermineCat1Const(int32_t value) {
4643 if (value < -32768)
4644 return kRegTypeConstInteger;
4645 else if (value < -128)
4646 return kRegTypeConstShort;
4647 else if (value < 0)
4648 return kRegTypeConstByte;
4649 else if (value == 0)
4650 return kRegTypeZero;
4651 else if (value == 1)
4652 return kRegTypeOne;
4653 else if (value < 128)
4654 return kRegTypeConstPosByte;
4655 else if (value < 32768)
4656 return kRegTypeConstPosShort;
4657 else if (value < 65536)
4658 return kRegTypeConstChar;
4659 else
4660 return kRegTypeConstInteger;
4661}
4662
4663void DexVerifier::CheckFinalFieldAccess(const Method* method,
4664 const Field* field, VerifyError* failure) {
4665 if (!field->IsFinal())
4666 return;
4667
4668 /* make sure we're in the same class */
4669 if (method->GetDeclaringClass() != field->GetDeclaringClass()) {
4670 LOG(ERROR) << "VFY: can't modify final field "
4671 << field->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
4672 << "." << field->GetName()->ToModifiedUtf8();
4673 *failure = VERIFY_ERROR_ACCESS_FIELD;
4674 return;
4675 }
4676}
4677
4678void DexVerifier::CheckArrayIndexType(const Method* method, RegType reg_type,
4679 VerifyError* failure) {
4680 if (*failure == VERIFY_ERROR_NONE) {
4681 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07004682 * The 1nr types are interchangeable at this level. We could
jeffhaobdb76512011-09-07 11:43:16 -07004683 * do something special if we can definitively identify it as a
4684 * float, but there's no real value in doing so.
4685 */
4686 CheckTypeCategory(reg_type, kTypeCategory1nr, failure);
4687 if (*failure != VERIFY_ERROR_NONE) {
4688 LOG(ERROR) << "Invalid reg type for array index (" << reg_type << ")";
4689 }
4690 }
4691}
4692
4693bool DexVerifier::CheckConstructorReturn(const Method* method,
4694 const RegisterLine* register_line, const int insn_reg_count) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004695 const RegType* insn_regs = register_line->reg_types_.get();
jeffhaobdb76512011-09-07 11:43:16 -07004696
4697 if (!IsInitMethod(method))
4698 return true;
4699
4700 RegType uninit_this = RegTypeFromUninitIndex(kUninitThisArgSlot);
4701
4702 for (int i = 0; i < insn_reg_count; i++) {
4703 if (insn_regs[i] == uninit_this) {
4704 LOG(ERROR) << "VFY: <init> returning without calling superclass init";
4705 return false;
4706 }
4707 }
4708 return true;
4709}
4710
4711bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
4712 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
4713 LOG(ERROR) << "VFY: invalid use of move-exception";
4714 return false;
4715 }
4716 return true;
4717}
4718
4719void DexVerifier::CheckTypeCategory(RegType type, TypeCategory cat,
4720 VerifyError* failure) {
4721 switch (cat) {
4722 case kTypeCategory1nr:
4723 switch (type) {
4724 case kRegTypeZero:
4725 case kRegTypeOne:
4726 case kRegTypeBoolean:
4727 case kRegTypeConstPosByte:
4728 case kRegTypeConstByte:
4729 case kRegTypeConstPosShort:
4730 case kRegTypeConstShort:
4731 case kRegTypeConstChar:
4732 case kRegTypeConstInteger:
4733 case kRegTypePosByte:
4734 case kRegTypeByte:
4735 case kRegTypePosShort:
4736 case kRegTypeShort:
4737 case kRegTypeChar:
4738 case kRegTypeInteger:
4739 case kRegTypeFloat:
4740 break;
4741 default:
4742 *failure = VERIFY_ERROR_GENERIC;
4743 break;
4744 }
4745 break;
4746 case kTypeCategory2:
4747 switch (type) {
4748 case kRegTypeConstLo:
4749 case kRegTypeLongLo:
4750 case kRegTypeDoubleLo:
4751 break;
4752 default:
4753 *failure = VERIFY_ERROR_GENERIC;
4754 break;
4755 }
4756 break;
4757 case kTypeCategoryRef:
4758 if (type != kRegTypeZero && !RegTypeIsReference(type))
4759 *failure = VERIFY_ERROR_GENERIC;
4760 break;
4761 default:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004762 DCHECK(false);
jeffhaobdb76512011-09-07 11:43:16 -07004763 *failure = VERIFY_ERROR_GENERIC;
4764 break;
4765 }
4766}
4767
4768void DexVerifier::CheckWidePair(RegType type_l, RegType type_h,
4769 VerifyError* failure) {
4770 if ((type_h != type_l + 1))
4771 *failure = VERIFY_ERROR_GENERIC;
4772}
4773
4774void DexVerifier::CheckUnop(RegisterLine* register_line,
4775 Instruction::DecodedInstruction* dec_insn, RegType dst_type,
4776 RegType src_type, VerifyError* failure) {
4777 VerifyRegisterType(register_line, dec_insn->vB_, src_type, failure);
4778 SetRegisterType(register_line, dec_insn->vA_, dst_type);
4779}
4780
4781bool DexVerifier::UpcastBooleanOp(RegisterLine* register_line, uint32_t reg1,
4782 uint32_t reg2) {
4783 RegType type1, type2;
4784
4785 type1 = GetRegisterType(register_line, reg1);
4786 type2 = GetRegisterType(register_line, reg2);
4787
4788 if ((type1 == kRegTypeBoolean || type1 == kRegTypeZero || type1 == kRegTypeOne) &&
4789 (type2 == kRegTypeBoolean || type2 == kRegTypeZero || type2 == kRegTypeOne)) {
4790 return true;
4791 }
4792 return false;
4793}
4794
4795void DexVerifier::CheckLitop(RegisterLine* register_line,
4796 Instruction::DecodedInstruction* dec_insn, RegType dst_type,
4797 RegType src_type, bool check_boolean_op, VerifyError* failure) {
4798 VerifyRegisterType(register_line, dec_insn->vB_, src_type, failure);
4799
4800 if ((*failure == VERIFY_ERROR_NONE) && check_boolean_op) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004801 DCHECK(dst_type == kRegTypeInteger);
jeffhaobdb76512011-09-07 11:43:16 -07004802
4803 /* check vB with the call, then check the constant manually */
4804 if (UpcastBooleanOp(register_line, dec_insn->vB_, dec_insn->vB_)
4805 && (dec_insn->vC_ == 0 || dec_insn->vC_ == 1)) {
4806 dst_type = kRegTypeBoolean;
4807 }
4808 }
4809
4810 SetRegisterType(register_line, dec_insn->vA_, dst_type);
4811}
4812
4813void DexVerifier::CheckBinop(RegisterLine* register_line,
4814 Instruction::DecodedInstruction* dec_insn, RegType dst_type,
4815 RegType src_type1, RegType src_type2, bool check_boolean_op,
4816 VerifyError* failure) {
4817 VerifyRegisterType(register_line, dec_insn->vB_, src_type1, failure);
4818 VerifyRegisterType(register_line, dec_insn->vC_, src_type2, failure);
4819
4820 if ((*failure == VERIFY_ERROR_NONE) && check_boolean_op) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004821 DCHECK(dst_type == kRegTypeInteger);
jeffhaobdb76512011-09-07 11:43:16 -07004822 if (UpcastBooleanOp(register_line, dec_insn->vB_, dec_insn->vC_))
4823 dst_type = kRegTypeBoolean;
4824 }
4825
4826 SetRegisterType(register_line, dec_insn->vA_, dst_type);
4827}
4828
4829void DexVerifier::CheckBinop2addr(RegisterLine* register_line,
4830 Instruction::DecodedInstruction* dec_insn, RegType dst_type,
4831 RegType src_type1, RegType src_type2, bool check_boolean_op,
4832 VerifyError* failure) {
4833 VerifyRegisterType(register_line, dec_insn->vA_, src_type1, failure);
4834 VerifyRegisterType(register_line, dec_insn->vB_, src_type2, failure);
4835
4836 if ((*failure == VERIFY_ERROR_NONE) && check_boolean_op) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004837 DCHECK(dst_type == kRegTypeInteger);
jeffhaobdb76512011-09-07 11:43:16 -07004838 if (UpcastBooleanOp(register_line, dec_insn->vA_, dec_insn->vB_))
4839 dst_type = kRegTypeBoolean;
4840 }
4841
4842 SetRegisterType(register_line, dec_insn->vA_, dst_type);
4843}
4844
4845DexVerifier::RegType DexVerifier::AdjustForRightShift(
4846 RegisterLine* register_line, int reg, unsigned int shift_count,
jeffhaob4df5142011-09-19 20:25:32 -07004847 bool is_unsigned_shift) {
jeffhaobdb76512011-09-07 11:43:16 -07004848 RegType src_type = GetRegisterType(register_line, reg);
4849 RegType new_type;
4850
4851 /* convert const derived types to their actual types */
4852 src_type = ConstTypeToRegType(src_type);
4853
4854 /* no-op */
4855 if (shift_count == 0)
4856 return src_type;
4857
4858 /* safe defaults */
4859 if (is_unsigned_shift)
4860 new_type = kRegTypeInteger;
4861 else
4862 new_type = src_type;
4863
4864 if (shift_count >= 32) {
4865 LOG(ERROR) << "Got unexpectedly large shift count " << shift_count;
4866 /* fail? */
4867 return new_type;
4868 }
4869
4870 switch (src_type) {
4871 case kRegTypeInteger: /* 32-bit signed value */
4872 if (is_unsigned_shift) {
4873 if (shift_count > 24)
4874 new_type = kRegTypePosByte;
4875 else if (shift_count >= 16)
4876 new_type = kRegTypeChar;
4877 } else {
4878 if (shift_count >= 24)
4879 new_type = kRegTypeByte;
4880 else if (shift_count >= 16)
4881 new_type = kRegTypeShort;
4882 }
4883 break;
4884 case kRegTypeShort: /* 16-bit signed value */
4885 if (is_unsigned_shift) {
4886 /* default (kRegTypeInteger) is correct */
4887 } else {
4888 if (shift_count >= 8)
4889 new_type = kRegTypeByte;
4890 }
4891 break;
4892 case kRegTypePosShort: /* 15-bit unsigned value */
4893 if (shift_count >= 8)
4894 new_type = kRegTypePosByte;
4895 break;
4896 case kRegTypeChar: /* 16-bit unsigned value */
4897 if (shift_count > 8)
4898 new_type = kRegTypePosByte;
4899 break;
4900 case kRegTypeByte: /* 8-bit signed value */
4901 /* defaults (u=kRegTypeInteger / s=src_type) are correct */
4902 break;
4903 case kRegTypePosByte: /* 7-bit unsigned value */
4904 /* always use new_type=src_type */
4905 new_type = src_type;
4906 break;
4907 case kRegTypeZero: /* 1-bit unsigned value */
4908 case kRegTypeOne:
4909 case kRegTypeBoolean:
4910 /* unnecessary? */
4911 new_type = kRegTypeZero;
4912 break;
4913 default:
4914 /* long, double, references; shouldn't be here! */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004915 DCHECK(false);
jeffhaobdb76512011-09-07 11:43:16 -07004916 break;
4917 }
4918
4919 return new_type;
4920}
4921
4922void DexVerifier::VerifyFilledNewArrayRegs(const Method* method,
4923 RegisterLine* register_line,
4924 const Instruction::DecodedInstruction* dec_insn, Class* res_class,
4925 bool is_range, VerifyError* failure) {
4926 uint32_t arg_count = dec_insn->vA_;
4927 RegType expected_type;
4928 Class::PrimitiveType elem_type;
4929 unsigned int ui;
4930
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07004931 DCHECK(res_class->IsArrayClass()) << PrettyClass(res_class);
jeffhaobdb76512011-09-07 11:43:16 -07004932 elem_type = res_class->GetComponentType()->GetPrimitiveType();
4933 if (elem_type == Class::kPrimNot) {
4934 expected_type = RegTypeFromClass(res_class->GetComponentType());
4935 } else {
4936 expected_type = PrimitiveTypeToRegType(elem_type);
4937 }
4938
4939 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07004940 * Verify each register. If "arg_count" is bad, VerifyRegisterType()
4941 * will run off the end of the list and fail. It's legal, if silly,
jeffhaobdb76512011-09-07 11:43:16 -07004942 * for arg_count to be zero.
4943 */
4944 for (ui = 0; ui < arg_count; ui++) {
4945 uint32_t get_reg;
4946
4947 if (is_range)
4948 get_reg = dec_insn->vC_ + ui;
4949 else
4950 get_reg = dec_insn->arg_[ui];
4951
4952 VerifyRegisterType(register_line, get_reg, expected_type, failure);
4953 if (*failure != VERIFY_ERROR_NONE) {
4954 LOG(ERROR) << "VFY: filled-new-array arg " << ui << "(" << get_reg
4955 << ") not valid";
4956 return;
4957 }
4958 }
4959}
4960
4961bool DexVerifier::IsCorrectInvokeKind(MethodType method_type,
4962 Method* res_method) {
4963 switch (method_type) {
4964 case METHOD_DIRECT:
4965 return res_method->IsDirect();
4966 case METHOD_STATIC:
4967 return res_method->IsStatic();
4968 case METHOD_VIRTUAL:
4969 case METHOD_INTERFACE:
4970 return !res_method->IsDirect();
4971 default:
4972 return false;
4973 }
4974}
4975
4976Method* DexVerifier::VerifyInvocationArgs(VerifierData* vdata,
4977 RegisterLine* register_line, const int insn_reg_count,
4978 const Instruction::DecodedInstruction* dec_insn, MethodType method_type,
4979 bool is_range, bool is_super, VerifyError* failure) {
4980 Method* method = vdata->method_;
4981 const DexFile* dex_file = vdata->dex_file_;
4982 const DexFile::CodeItem* code_item = vdata->code_item_;
Elliott Hughes5fe594f2011-09-08 12:33:17 -07004983 UninitInstanceMap* uninit_map = vdata->uninit_map_.get();
jeffhaobdb76512011-09-07 11:43:16 -07004984
4985 Method* res_method;
4986 std::string sig;
4987 size_t sig_offset;
4988 int expected_args;
4989 int actual_args;
4990
4991 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07004992 * Resolve the method. This could be an abstract or concrete method
jeffhaobdb76512011-09-07 11:43:16 -07004993 * depending on what sort of call we're making.
4994 */
jeffhaob4df5142011-09-19 20:25:32 -07004995 res_method = ResolveMethodAndCheckAccess(dex_file, dec_insn->vB_, method->GetDeclaringClass(),
4996 failure, (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
jeffhaobdb76512011-09-07 11:43:16 -07004997
jeffhaobdb76512011-09-07 11:43:16 -07004998 if (res_method == NULL) {
jeffhao98eacac2011-09-14 16:11:53 -07004999 const DexFile::MethodId& method_id = dex_file->GetMethodId(dec_insn->vB_);
5000 const char* method_name = dex_file->GetMethodName(method_id);
5001 const char* method_proto = dex_file->GetMethodPrototype(method_id);
5002 const char* class_descriptor = dex_file->GetMethodClassDescriptor(method_id);
5003
5004 LOG(ERROR) << "VFY: unable to resolve method " << dec_insn->vB_ << ": "
5005 << class_descriptor << "." << method_name << " " << method_proto;
jeffhaobdb76512011-09-07 11:43:16 -07005006 *failure = VERIFY_ERROR_NO_METHOD;
5007 return NULL;
5008 }
jeffhaobdb76512011-09-07 11:43:16 -07005009
5010 /*
5011 * Only time you can explicitly call a method starting with '<' is when
jeffhaod1f0fde2011-09-08 17:25:33 -07005012 * making a "direct" invocation on "<init>". There are additional
jeffhaobdb76512011-09-07 11:43:16 -07005013 * restrictions but we don't enforce them here.
5014 */
5015 if (res_method->GetName()->Equals("<init>")) {
5016 if (method_type != METHOD_DIRECT || !IsInitMethod(res_method)) {
5017 LOG(ERROR) << "VFY: invalid call to "
jeffhao98eacac2011-09-14 16:11:53 -07005018 << res_method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
jeffhaobdb76512011-09-07 11:43:16 -07005019 << "." << res_method->GetName();
5020 goto bad_sig;
5021 }
5022 }
5023
5024 /*
5025 * See if the method type implied by the invoke instruction matches the
5026 * access flags for the target method.
5027 */
5028 if (!IsCorrectInvokeKind(method_type, res_method)) {
5029 LOG(ERROR) << "VFY: invoke type does not match method type of "
jeffhao98eacac2011-09-14 16:11:53 -07005030 << res_method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
jeffhaobdb76512011-09-07 11:43:16 -07005031 << "." << res_method->GetName()->ToModifiedUtf8();
5032
5033 *failure = VERIFY_ERROR_GENERIC;
5034 return NULL;
5035 }
5036
5037 /*
5038 * If we're using invoke-super(method), make sure that the executing
5039 * method's class' superclass has a vtable entry for the target method.
5040 */
5041 if (is_super) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005042 DCHECK(method_type == METHOD_VIRTUAL);
jeffhaobdb76512011-09-07 11:43:16 -07005043 Class* super = method->GetDeclaringClass()->GetSuperClass();
5044 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
5045 if (super == NULL) {
5046 LOG(ERROR) << "VFY: invalid invoke-super from "
5047 << method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
5048 << "." << method->GetName()->ToModifiedUtf8() << " to super -."
5049 << res_method->GetName()->ToModifiedUtf8()
5050 << " " << res_method->GetSignature()->ToModifiedUtf8();
5051 } else {
5052 LOG(ERROR) << "VFY: invalid invoke-super from "
5053 << method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
5054 << "." << method->GetName()->ToModifiedUtf8() << " to super "
5055 << super->GetDescriptor()->ToModifiedUtf8()
5056 << "." << res_method->GetName()->ToModifiedUtf8()
5057 << " " << res_method->GetSignature()->ToModifiedUtf8();
5058 }
5059 *failure = VERIFY_ERROR_NO_METHOD;
5060 return NULL;
5061 }
5062 }
5063
5064 /*
5065 * We use vAA as our expected arg count, rather than res_method->insSize,
jeffhaod1f0fde2011-09-08 17:25:33 -07005066 * because we need to match the call to the signature. Also, we might
jeffhaobdb76512011-09-07 11:43:16 -07005067 * might be calling through an abstract method definition (which doesn't
5068 * have register count values).
5069 */
5070 expected_args = dec_insn->vA_;
5071 actual_args = 0;
5072
5073 /* caught by static verifier */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005074 DCHECK(is_range || expected_args <= 5);
jeffhaobdb76512011-09-07 11:43:16 -07005075
5076 if (expected_args > code_item->outs_size_) {
5077 LOG(ERROR) << "VFY: invalid arg count (" << expected_args
5078 << ") exceeds outsSize (" << code_item->outs_size_ << ")";
5079 *failure = VERIFY_ERROR_GENERIC;
5080 return NULL;
5081 }
5082
5083 sig = res_method->GetSignature()->ToModifiedUtf8();
5084 if (sig[0] != '(') {
5085 LOG(ERROR) << "VFY: descriptor doesn't start with '(': " << sig;
5086 goto bad_sig;
5087 }
5088
5089 /*
5090 * Check the "this" argument, which must be an instance of the class
jeffhaod1f0fde2011-09-08 17:25:33 -07005091 * that declared the method. For an interface class, we don't do the
jeffhaobdb76512011-09-07 11:43:16 -07005092 * full interface merge, so we can't do a rigorous check here (which
5093 * is okay since we have to do it at runtime).
5094 */
5095 if (!res_method->IsStatic()) {
5096 Class* actual_this_ref;
5097 RegType actual_arg_type;
5098
5099 actual_arg_type = GetInvocationThis(register_line, dec_insn, failure);
5100 if (*failure != VERIFY_ERROR_NONE)
5101 return NULL;
5102
5103 if (RegTypeIsUninitReference(actual_arg_type) &&
5104 !res_method->GetName()->Equals("<init>")) {
5105 LOG(ERROR) << "VFY: 'this' arg must be initialized";
5106 *failure = VERIFY_ERROR_GENERIC;
5107 return NULL;
5108 }
5109 if (method_type != METHOD_INTERFACE && actual_arg_type != kRegTypeZero) {
5110 actual_this_ref = RegTypeReferenceToClass(actual_arg_type, uninit_map);
5111 if (!res_method->GetDeclaringClass()->IsAssignableFrom(actual_this_ref)) {
5112 LOG(ERROR) << "VFY: 'this' arg '"
5113 << actual_this_ref->GetDescriptor()->ToModifiedUtf8()
5114 << "' not instance of '"
5115 << res_method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
5116 << "'";
5117 *failure = VERIFY_ERROR_GENERIC;
5118 return NULL;
5119 }
5120 }
5121 actual_args++;
5122 }
5123
5124 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07005125 * Process the target method's signature. This signature may or may not
jeffhaobdb76512011-09-07 11:43:16 -07005126 * have been verified, so we can't assume it's properly formed.
5127 */
5128 for (sig_offset = 1; sig_offset < sig.size(); sig_offset++) {
jeffhaobdb76512011-09-07 11:43:16 -07005129 if (sig[sig_offset] == ')')
5130 break;
5131
5132 if (actual_args >= expected_args) {
5133 LOG(ERROR) << "VFY: expected " << expected_args << " args, found more ("
5134 << sig.substr(sig_offset) << ")";
5135 goto bad_sig;
5136 }
5137
5138 uint32_t get_reg;
5139 if (is_range)
5140 get_reg = dec_insn->vC_ + actual_args;
5141 else
5142 get_reg = dec_insn->arg_[actual_args];
5143
5144 switch (sig[sig_offset]) {
5145 case 'L':
5146 {
5147 Class* klass = LookupSignatureClass(method, sig.substr(sig_offset),
5148 failure);
5149 if (*failure != VERIFY_ERROR_NONE)
5150 goto bad_sig;
5151 VerifyRegisterType(register_line, get_reg, RegTypeFromClass(klass),
5152 failure);
5153 if (*failure != VERIFY_ERROR_NONE) {
5154 LOG(ERROR) << "VFY: bad arg " << actual_args << " (into "
5155 << klass->GetDescriptor()->ToModifiedUtf8() << ")";
5156 goto bad_sig;
5157 }
5158 sig_offset += sig.substr(sig_offset).find(';');
5159 }
5160 actual_args++;
5161 break;
5162 case '[':
5163 {
5164 Class* klass = LookupSignatureArrayClass(method,
5165 sig.substr(sig_offset), failure);
5166 if (*failure != VERIFY_ERROR_NONE)
5167 goto bad_sig;
5168 VerifyRegisterType(register_line, get_reg, RegTypeFromClass(klass),
5169 failure);
5170 if (*failure != VERIFY_ERROR_NONE) {
5171 LOG(ERROR) << "VFY: bad arg " << actual_args << " (into "
5172 << klass->GetDescriptor()->ToModifiedUtf8() << ")";
5173 goto bad_sig;
5174 }
5175 while (sig[sig_offset] == '[')
5176 sig_offset++;
5177 if (sig[sig_offset] == 'L')
5178 sig_offset += sig.substr(sig_offset).find(';');
5179 }
5180 actual_args++;
5181 break;
5182 case 'Z':
5183 VerifyRegisterType(register_line, get_reg, kRegTypeBoolean, failure);
5184 actual_args++;
5185 break;
5186 case 'C':
5187 VerifyRegisterType(register_line, get_reg, kRegTypeChar, failure);
5188 actual_args++;
5189 break;
5190 case 'B':
5191 VerifyRegisterType(register_line, get_reg, kRegTypeByte, failure);
5192 actual_args++;
5193 break;
5194 case 'I':
5195 VerifyRegisterType(register_line, get_reg, kRegTypeInteger, failure);
5196 actual_args++;
5197 break;
5198 case 'S':
5199 VerifyRegisterType(register_line, get_reg, kRegTypeShort, failure);
5200 actual_args++;
5201 break;
5202 case 'F':
5203 VerifyRegisterType(register_line, get_reg, kRegTypeFloat, failure);
5204 actual_args++;
5205 break;
5206 case 'D':
5207 VerifyRegisterType(register_line, get_reg, kRegTypeDoubleLo, failure);
5208 actual_args += 2;
5209 break;
5210 case 'J':
5211 VerifyRegisterType(register_line, get_reg, kRegTypeLongLo, failure);
5212 actual_args += 2;
5213 break;
5214 default:
5215 LOG(ERROR) << "VFY: invocation target: bad signature type char '"
5216 << sig << "'";
5217 goto bad_sig;
5218 }
5219 }
5220 if (sig[sig_offset] != ')') {
5221 LOG(ERROR) << "VFY: invocation target: bad signature '"
5222 << res_method->GetSignature()->ToModifiedUtf8() << "'";
5223 goto bad_sig;
5224 }
5225
5226 if (actual_args != expected_args) {
5227 LOG(ERROR) << "VFY: expected " << expected_args << " args, found "
5228 << actual_args;
5229 goto bad_sig;
5230 }
5231
5232 return res_method;
5233
5234bad_sig:
5235 if (res_method != NULL) {
5236 LOG(ERROR) << "VFY: rejecting call to "
5237 << res_method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
5238 << "." << res_method->GetName()->ToModifiedUtf8() << " "
5239 << res_method->GetSignature()->ToModifiedUtf8();
5240 }
5241
5242 if (*failure == VERIFY_ERROR_NONE)
5243 *failure = VERIFY_ERROR_GENERIC;
5244 return NULL;
5245}
jeffhaoba5ebb92011-08-25 17:24:37 -07005246
jeffhaod1f0fde2011-09-08 17:25:33 -07005247DexVerifier::RegisterMap* DexVerifier::GenerateRegisterMapV(VerifierData* vdata)
5248{
5249 const DexFile::CodeItem* code_item = vdata->code_item_;
5250 int i, bytes_for_addr, gc_point_count;
5251
5252 if (code_item->registers_size_ >= 2048) {
5253 LOG(ERROR) << "ERROR: register map can't handle "
5254 << code_item->registers_size_ << " registers";
5255 return NULL;
5256 }
5257 uint8_t reg_width = (code_item->registers_size_ + 7) / 8;
5258
5259 /*
5260 * Decide if we need 8 or 16 bits to hold the address. Strictly speaking
5261 * we only need 16 bits if we actually encode an address >= 256 -- if
5262 * the method has a section at the end without GC points (e.g. array
5263 * data) we don't need to count it. The situation is unusual, and
5264 * detecting it requires scanning the entire method, so we don't bother.
5265 */
5266 RegisterMapFormat format;
5267 if (code_item->insns_size_ < 256) {
5268 format = kRegMapFormatCompact8;
5269 bytes_for_addr = 1;
5270 } else {
5271 format = kRegMapFormatCompact16;
5272 bytes_for_addr = 2;
5273 }
5274
5275 /*
5276 * Count up the number of GC point instructions.
5277 *
5278 * NOTE: this does not automatically include the first instruction,
5279 * since we don't count method entry as a GC point.
5280 */
5281 gc_point_count = 0;
5282 for (i = 0; i < (int) code_item->insns_size_; i++) {
5283 if (InsnIsGcPoint(vdata->insn_flags_.get(), i))
5284 gc_point_count++;
5285 }
5286 if (gc_point_count >= 65536) {
5287 /* We could handle this, but in practice we don't get near this. */
5288 LOG(ERROR) << "ERROR: register map can't handle " << gc_point_count
5289 << " gc points in one method";
5290 return NULL;
5291 }
5292
5293 /* Calculate size of buffer to hold the map data. */
5294 uint32_t data_size = gc_point_count * (bytes_for_addr + reg_width);
5295
5296 RegisterMap* map = new RegisterMap(format, reg_width, gc_point_count,
jeffhaoa0a764a2011-09-16 10:43:38 -07005297 data_size);
jeffhaod1f0fde2011-09-08 17:25:33 -07005298
5299 /* Populate it. */
jeffhaoe23d93c2011-09-15 14:48:43 -07005300 uint8_t* map_data = map->data_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005301 for (i = 0; i < (int) vdata->code_item_->insns_size_; i++) {
5302 if (InsnIsGcPoint(vdata->insn_flags_.get(), i)) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005303 DCHECK(vdata->register_lines_[i].reg_types_.get() != NULL);
jeffhaod1f0fde2011-09-08 17:25:33 -07005304 if (format == kRegMapFormatCompact8) {
5305 *map_data++ = i;
5306 } else /*kRegMapFormatCompact16*/ {
5307 *map_data++ = i & 0xff;
5308 *map_data++ = i >> 8;
5309 }
5310 OutputTypeVector(vdata->register_lines_[i].reg_types_.get(),
5311 code_item->registers_size_, map_data);
5312 map_data += reg_width;
5313 }
5314 }
5315
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005316 DCHECK_EQ((uint32_t) map_data - (uint32_t) map->data_, data_size);
jeffhaod1f0fde2011-09-08 17:25:33 -07005317
5318 // TODO: Remove this check when it's really running...
5319#if 1
5320 if (!VerifyMap(vdata, map)) {
5321 LOG(ERROR) << "Map failed to verify";
5322 return NULL;
5323 }
5324#endif
5325
5326 /* Try to compress the map. */
5327 RegisterMap* compress_map = CompressMapDifferential(map);
5328 if (compress_map != NULL) {
5329 // TODO: Remove this check when it's really running...
5330#if 1
5331 /*
5332 * Expand the compressed map we just created, and compare it
5333 * to the original. Abort the VM if it doesn't match up.
5334 */
5335 UniquePtr<RegisterMap> uncompressed_map(UncompressMapDifferential(compress_map));
5336 if (uncompressed_map.get() == NULL) {
5337 LOG(ERROR) << "Map failed to uncompress - "
5338 << vdata->method_->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
5339 << "." << vdata->method_->GetName()->ToModifiedUtf8();
5340 delete map;
5341 delete compress_map;
5342 /* bad - compression is broken or we're out of memory */
5343 return NULL;
5344 } else {
5345 if (!CompareMaps(map, uncompressed_map.get())) {
5346 LOG(ERROR) << "Map comparison failed - "
5347 << vdata->method_->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
5348 << "." << vdata->method_->GetName()->ToModifiedUtf8();
5349 delete map;
5350 delete compress_map;
5351 /* bad - compression is broken */
5352 return NULL;
5353 }
5354 }
5355#endif
5356 delete map;
5357 map = compress_map;
5358 }
5359
5360 return map;
5361}
5362
jeffhaoa0a764a2011-09-16 10:43:38 -07005363DexVerifier::RegisterMap* DexVerifier::GetExpandedRegisterMapHelper(
5364 Method* method, RegisterMap* map) {
5365 RegisterMap* new_map;
5366
5367 if (map == NULL)
5368 return NULL;
5369
5370 /* TODO: sanity check to ensure this isn't called w/o external locking */
5371
5372 uint8_t format = map->header_->format_;
5373 switch (format) {
5374 case kRegMapFormatCompact8:
5375 case kRegMapFormatCompact16:
5376 /* already expanded */
5377 return map;
5378 case kRegMapFormatDifferential:
5379 new_map = UncompressMapDifferential(map);
5380 break;
5381 default:
5382 LOG(ERROR) << "Unknown format " << format
5383 << " in dvmGetExpandedRegisterMap";
5384 return NULL;
5385 }
5386
5387 if (new_map == NULL) {
5388 LOG(ERROR) << "Map failed to uncompress (fmt=" << format << ") "
5389 << method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8()
5390 << "." << method->GetName();
5391 return NULL;
5392 }
5393
5394 /* Update method, and free compressed map if it was sitting on the heap. */
5395 ByteArray* header = ByteArray::Alloc(sizeof(RegisterMapHeader));
5396 ByteArray* data = ByteArray::Alloc(ComputeRegisterMapSize(map));
5397
5398 memcpy(header->GetData(), map->header_, sizeof(RegisterMapHeader));
5399 memcpy(data->GetData(), map->data_, ComputeRegisterMapSize(map));
5400
5401 method->SetRegisterMapHeader(header);
5402 method->SetRegisterMapData(data);
5403
5404 delete map;
5405 return new_map;
5406}
5407
5408const uint8_t* DexVerifier::RegisterMapGetLine(const RegisterMap* map, int addr) {
5409 int addr_width, line_width;
5410 uint8_t format = map->header_->format_;
5411 uint16_t num_entries = map->header_->num_entries_;
5412
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005413 DCHECK_GT(num_entries, 0);
jeffhaoa0a764a2011-09-16 10:43:38 -07005414
5415 switch (format) {
5416 case kRegMapFormatNone:
5417 return NULL;
5418 case kRegMapFormatCompact8:
5419 addr_width = 1;
5420 break;
5421 case kRegMapFormatCompact16:
5422 addr_width = 2;
5423 break;
5424 default:
5425 LOG(ERROR) << "Unknown format " << format;
5426 return NULL;
5427 }
5428
5429 line_width = addr_width + map->header_->reg_width_;
5430
5431 /*
5432 * Find the appropriate entry. Many maps are very small, some are very large.
5433 */
5434 static const int kSearchThreshold = 8;
5435 const uint8_t* data = NULL;
5436 int line_addr;
5437
5438 if (num_entries < kSearchThreshold) {
5439 int i;
5440 data = map->data_;
5441 for (i = num_entries; i > 0; i--) {
5442 line_addr = data[0];
5443 if (addr_width > 1)
5444 line_addr |= data[1] << 8;
5445 if (line_addr == addr)
5446 return data + addr_width;
5447
5448 data += line_width;
5449 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005450 DCHECK_EQ(data, map->data_ + line_width * num_entries);
jeffhaoa0a764a2011-09-16 10:43:38 -07005451 } else {
5452 int hi, lo, mid;
5453
5454 lo = 0;
5455 hi = num_entries -1;
5456
5457 while (hi >= lo) {
5458 mid = (hi + lo) / 2;
5459 data = map->data_ + line_width * mid;
5460
5461 line_addr = data[0];
5462 if (addr_width > 1)
5463 line_addr |= data[1] << 8;
5464
5465 if (addr > line_addr) {
5466 lo = mid + 1;
5467 } else if (addr < line_addr) {
5468 hi = mid - 1;
5469 } else {
5470 return data + addr_width;
5471 }
5472 }
5473 }
5474
5475 return NULL;
5476}
5477
jeffhaod1f0fde2011-09-08 17:25:33 -07005478void DexVerifier::OutputTypeVector(const RegType* regs, int insn_reg_count,
5479 uint8_t* data) {
5480 uint8_t val = 0;
5481 int i;
5482
5483 for (i = 0; i < insn_reg_count; i++) {
5484 RegType type = *regs++;
5485 val >>= 1;
5486 if (IsReferenceType(type))
5487 val |= 0x80; /* set hi bit */
5488
5489 if ((i & 0x07) == 7)
5490 *data++ = val;
5491 }
5492 if ((i & 0x07) != 0) {
5493 /* Flush bits from last byte. */
5494 val >>= 8 - (i & 0x07);
5495 *data++ = val;
5496 }
5497}
5498
5499bool DexVerifier::VerifyMap(VerifierData* vdata, const RegisterMap* map) {
jeffhaoe23d93c2011-09-15 14:48:43 -07005500 const uint8_t* raw_map = map->data_;
5501 uint8_t format = map->header_->format_;
5502 const int num_entries = map->header_->num_entries_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005503 int ent;
5504
jeffhaoe23d93c2011-09-15 14:48:43 -07005505 if ((vdata->code_item_->registers_size_ + 7) / 8 != map->header_->reg_width_) {
jeffhaod1f0fde2011-09-08 17:25:33 -07005506 LOG(ERROR) << "GLITCH: registersSize=" << vdata->code_item_->registers_size_
jeffhaoe23d93c2011-09-15 14:48:43 -07005507 << ", reg_width=" << map->header_->reg_width_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005508 return false;
5509 }
5510
5511 for (ent = 0; ent < num_entries; ent++) {
5512 int addr;
5513
5514 switch (format) {
5515 case kRegMapFormatCompact8:
5516 addr = *raw_map++;
5517 break;
5518 case kRegMapFormatCompact16:
5519 addr = *raw_map++;
5520 addr |= (*raw_map++) << 8;
5521 break;
5522 default:
5523 LOG(FATAL) << "GLITCH: bad format (" << format << ")";
5524 return false;
5525 }
5526
5527 const RegType* regs = vdata->register_lines_[addr].reg_types_.get();
5528 if (regs == NULL) {
5529 LOG(ERROR) << "GLITCH: addr " << addr << " has no data";
5530 return false;
5531 }
5532
5533 uint8_t val = 0;
5534 int i;
5535
5536 for (i = 0; i < vdata->code_item_->registers_size_; i++) {
5537 bool bit_is_ref, reg_is_ref;
5538
5539 val >>= 1;
5540 if ((i & 0x07) == 0) {
5541 /* Load next byte of data. */
5542 val = *raw_map++;
5543 }
5544
5545 bit_is_ref = val & 0x01;
5546
5547 RegType type = regs[i];
5548 reg_is_ref = IsReferenceType(type);
5549
5550 if (bit_is_ref != reg_is_ref) {
5551 LOG(ERROR) << "GLITCH: addr " << addr << " reg " << i << ": bit="
5552 << bit_is_ref << " reg=" << reg_is_ref << "(" << type << ")";
5553 return false;
5554 }
5555 }
5556 /* Raw_map now points to the address field of the next entry. */
5557 }
5558
5559 return true;
5560}
5561
5562bool DexVerifier::CompareMaps(const RegisterMap* map1, const RegisterMap* map2)
5563{
5564 size_t size1, size2;
5565
5566 size1 = ComputeRegisterMapSize(map1);
5567 size2 = ComputeRegisterMapSize(map2);
5568 if (size1 != size2) {
5569 LOG(ERROR) << "CompareMaps: size mismatch (" << size1 << " vs " << size2
5570 << ")";
5571 return false;
5572 }
5573
jeffhaoe23d93c2011-09-15 14:48:43 -07005574 if (map1->header_->format_ != map2->header_->format_ ||
5575 map1->header_->reg_width_ != map2->header_->reg_width_ ||
jeffhaoa0a764a2011-09-16 10:43:38 -07005576 map1->header_->num_entries_ != map2->header_->num_entries_) {
jeffhaod1f0fde2011-09-08 17:25:33 -07005577 LOG(ERROR) << "CompareMaps: fields mismatch";
5578 }
jeffhaoe23d93c2011-09-15 14:48:43 -07005579 if (memcmp(map1->data_, map2->data_, size1) != 0) {
jeffhaod1f0fde2011-09-08 17:25:33 -07005580 LOG(ERROR) << "CompareMaps: data mismatch";
5581 return false;
5582 }
5583
5584 return true;
5585}
5586
5587size_t DexVerifier::ComputeRegisterMapSize(const RegisterMap* map) {
jeffhaoe23d93c2011-09-15 14:48:43 -07005588 uint8_t format = map->header_->format_;
5589 uint16_t num_entries = map->header_->num_entries_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005590
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005591 DCHECK(map != NULL);
jeffhaod1f0fde2011-09-08 17:25:33 -07005592
5593 switch (format) {
5594 case kRegMapFormatNone:
5595 return 1;
5596 case kRegMapFormatCompact8:
jeffhaoe23d93c2011-09-15 14:48:43 -07005597 return (1 + map->header_->reg_width_) * num_entries;
jeffhaod1f0fde2011-09-08 17:25:33 -07005598 case kRegMapFormatCompact16:
jeffhaoe23d93c2011-09-15 14:48:43 -07005599 return (2 + map->header_->reg_width_) * num_entries;
jeffhaod1f0fde2011-09-08 17:25:33 -07005600 case kRegMapFormatDifferential:
5601 {
5602 /* Decoded ULEB128 length. */
jeffhaoe23d93c2011-09-15 14:48:43 -07005603 const uint8_t* ptr = map->data_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005604 return DecodeUnsignedLeb128(&ptr);
5605 }
5606 default:
5607 LOG(FATAL) << "Bad register map format " << format;
5608 return 0;
5609 }
5610}
5611
5612int DexVerifier::ComputeBitDiff(const uint8_t* bits1, const uint8_t* bits2,
5613 int byte_width, int* first_bit_changed_ptr, int* num_bits_changed_ptr,
5614 uint8_t* leb_out_buf) {
5615 int num_bits_changed = 0;
5616 int first_bit_changed = -1;
5617 int leb_size = 0;
5618 int byte_num;
5619
5620 /*
5621 * Run through the vectors, first comparing them at the byte level. This
5622 * will yield a fairly quick result if nothing has changed between them.
5623 */
5624 for (byte_num = 0; byte_num < byte_width; byte_num++) {
5625 uint8_t byte1 = *bits1++;
5626 uint8_t byte2 = *bits2++;
5627 if (byte1 != byte2) {
5628 /* Walk through the byte, identifying the changed bits. */
5629 int bit_num;
5630 for (bit_num = 0; bit_num < 8; bit_num++) {
5631 if (((byte1 >> bit_num) & 0x01) != ((byte2 >> bit_num) & 0x01)) {
5632 int bit_offset = (byte_num << 3) + bit_num;
5633
5634 if (first_bit_changed < 0)
5635 first_bit_changed = bit_offset;
5636 num_bits_changed++;
5637
5638 if (leb_out_buf == NULL) {
5639 leb_size += UnsignedLeb128Size(bit_offset);
5640 } else {
5641 uint8_t* cur_buf = leb_out_buf;
5642 leb_out_buf = WriteUnsignedLeb128(leb_out_buf, bit_offset);
5643 leb_size += leb_out_buf - cur_buf;
5644 }
5645 }
5646 }
5647 }
5648 }
5649
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005650 if (num_bits_changed > 0) {
5651 DCHECK_GE(first_bit_changed, 0);
5652 }
jeffhaod1f0fde2011-09-08 17:25:33 -07005653
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005654 if (first_bit_changed_ptr != NULL) {
jeffhaod1f0fde2011-09-08 17:25:33 -07005655 *first_bit_changed_ptr = first_bit_changed;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005656 }
5657
5658 if (num_bits_changed_ptr != NULL) {
jeffhaod1f0fde2011-09-08 17:25:33 -07005659 *num_bits_changed_ptr = num_bits_changed;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005660 }
jeffhaod1f0fde2011-09-08 17:25:33 -07005661
5662 return leb_size;
5663}
5664
5665DexVerifier::RegisterMap* DexVerifier::CompressMapDifferential(
5666 const RegisterMap* map) {
5667 int orig_size = ComputeRegisterMapSize(map);
5668 uint8_t* tmp_ptr;
5669 int addr_width;
5670
jeffhaoe23d93c2011-09-15 14:48:43 -07005671 uint8_t format = map->header_->format_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005672 switch (format) {
5673 case kRegMapFormatCompact8:
5674 addr_width = 1;
5675 break;
5676 case kRegMapFormatCompact16:
5677 addr_width = 2;
5678 break;
5679 default:
5680 LOG(ERROR) << "ERROR: can't compress map with format=" << format;
5681 return NULL;
5682 }
5683
jeffhaoe23d93c2011-09-15 14:48:43 -07005684 int reg_width = map->header_->reg_width_;
5685 int num_entries = map->header_->num_entries_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005686
5687 if (num_entries <= 1) {
5688 return NULL;
5689 }
5690
5691 /*
5692 * We don't know how large the compressed data will be. It's possible
5693 * for it to expand and become larger than the original. The header
5694 * itself is variable-sized, so we generate everything into a temporary
5695 * buffer and then copy it to form-fitting storage once we know how big
5696 * it will be (and that it's smaller than the original).
5697 *
5698 * If we use a size that is equal to the size of the input map plus
5699 * a value longer than a single entry can possibly expand to, we need
5700 * only check for overflow at the end of each entry. The worst case
5701 * for a single line is (1 + <ULEB8 address> + <full copy of vector>).
5702 * Addresses are 16 bits, so that's (1 + 3 + reg_width).
5703 *
5704 * The initial address offset and bit vector will take up less than
5705 * or equal to the amount of space required when uncompressed -- large
5706 * initial offsets are rejected.
5707 */
5708 UniquePtr<uint8_t[]> tmp_buf(new uint8_t[orig_size + (1 + 3 + reg_width)]);
5709
5710 tmp_ptr = tmp_buf.get();
5711
jeffhaoe23d93c2011-09-15 14:48:43 -07005712 const uint8_t* map_data = map->data_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005713 const uint8_t* prev_bits;
5714 uint16_t addr, prev_addr;
5715
5716 addr = *map_data++;
5717 if (addr_width > 1)
5718 addr |= (*map_data++) << 8;
5719
5720 if (addr >= 128) {
5721 LOG(ERROR) << "Can't compress map with starting address >= 128";
5722 return NULL;
5723 }
5724
5725 /*
5726 * Start by writing the initial address and bit vector data. The high
5727 * bit of the initial address is used to indicate the required address
5728 * width (which the decoder can't otherwise determine without parsing
5729 * the compressed data).
5730 */
5731 *tmp_ptr++ = addr | (addr_width > 1 ? 0x80 : 0x00);
5732 memcpy(tmp_ptr, map_data, reg_width);
5733
5734 prev_bits = map_data;
5735 prev_addr = addr;
5736
5737 tmp_ptr += reg_width;
5738 map_data += reg_width;
5739
5740 /* Loop over all following entries. */
5741 for (int entry = 1; entry < num_entries; entry++) {
5742 int addr_diff;
5743 uint8_t key;
5744
5745 /* Pull out the address and figure out how to encode it. */
5746 addr = *map_data++;
5747 if (addr_width > 1)
5748 addr |= (*map_data++) << 8;
5749
5750 addr_diff = addr - prev_addr;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07005751 DCHECK_GT(addr_diff, 0);
jeffhaod1f0fde2011-09-08 17:25:33 -07005752 if (addr_diff < 8) {
5753 /* Small difference, encode in 3 bits. */
5754 key = addr_diff -1; /* set 00000AAA */
5755 } else {
5756 /* Large difference, output escape code. */
5757 key = 0x07; /* escape code for AAA */
5758 }
5759
5760 int num_bits_changed, first_bit_changed, leb_size;
5761
5762 leb_size = ComputeBitDiff(prev_bits, map_data, reg_width,
5763 &first_bit_changed, &num_bits_changed, NULL);
5764
5765 if (num_bits_changed == 0) {
5766 /* set B to 1 and CCCC to zero to indicate no bits were changed */
5767 key |= 0x08;
5768 } else if (num_bits_changed == 1 && first_bit_changed < 16) {
5769 /* set B to 0 and CCCC to the index of the changed bit */
5770 key |= first_bit_changed << 4;
5771 } else if (num_bits_changed < 15 && leb_size < reg_width) {
5772 /* set B to 1 and CCCC to the number of bits */
5773 key |= 0x08 | (num_bits_changed << 4);
5774 } else {
5775 /* set B to 1 and CCCC to 0x0f so we store the entire vector */
5776 key |= 0x08 | 0xf0;
5777 }
5778
5779 /*
5780 * Encode output. Start with the key, follow with the address
5781 * diff (if it didn't fit in 3 bits), then the changed bit info.
5782 */
5783 *tmp_ptr++ = key;
5784 if ((key & 0x07) == 0x07)
5785 tmp_ptr = WriteUnsignedLeb128(tmp_ptr, addr_diff);
5786
5787 if ((key & 0x08) != 0) {
5788 int bit_count = key >> 4;
5789 if (bit_count == 0) {
5790 /* nothing changed, no additional output required */
5791 } else if (bit_count == 15) {
5792 /* full vector is most compact representation */
5793 memcpy(tmp_ptr, map_data, reg_width);
5794 tmp_ptr += reg_width;
5795 } else {
5796 /* write bit indices in ULEB128 format */
5797 (void) ComputeBitDiff(prev_bits, map_data, reg_width,
5798 NULL, NULL, tmp_ptr);
5799 tmp_ptr += leb_size;
5800 }
5801 } else {
5802 /* single-bit changed, value encoded in key byte */
5803 }
5804
5805 prev_bits = map_data;
5806 prev_addr = addr;
5807 map_data += reg_width;
5808
5809 /* See if we've run past the original size. */
5810 if (tmp_ptr - tmp_buf.get() >= orig_size) {
5811 return NULL;
5812 }
5813 }
5814
5815 /*
5816 * Create a RegisterMap with the contents.
5817 *
5818 * TODO: consider using a threshold other than merely ">=". We would
5819 * get poorer compression but potentially use less native heap space.
5820 */
5821 int new_data_size = tmp_ptr - tmp_buf.get();
5822 int new_map_size = new_data_size + UnsignedLeb128Size(new_data_size);
5823
5824 if (new_map_size >= orig_size) {
5825 return NULL;
5826 }
5827
5828 RegisterMap* new_map = new RegisterMap(kRegMapFormatDifferential, reg_width,
jeffhaoa0a764a2011-09-16 10:43:38 -07005829 num_entries, new_map_size);
jeffhaod1f0fde2011-09-08 17:25:33 -07005830
jeffhaoe23d93c2011-09-15 14:48:43 -07005831 tmp_ptr = new_map->data_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005832 tmp_ptr = WriteUnsignedLeb128(tmp_ptr, new_data_size);
5833 memcpy(tmp_ptr, tmp_buf.get(), new_data_size);
5834
5835 return new_map;
5836}
5837
5838DexVerifier::RegisterMap* DexVerifier::UncompressMapDifferential(
5839 const RegisterMap* map) {
jeffhaoe23d93c2011-09-15 14:48:43 -07005840 uint8_t format = map->header_->format_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005841 RegisterMapFormat new_format;
5842 int reg_width, num_entries, new_addr_width, new_data_size;
5843
5844 if (format != kRegMapFormatDifferential) {
5845 LOG(ERROR) << "Not differential (" << format << ")";
5846 return NULL;
5847 }
5848
jeffhaoe23d93c2011-09-15 14:48:43 -07005849 reg_width = map->header_->reg_width_;
5850 num_entries = map->header_->num_entries_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005851
5852 /* Get the data size; we can check this at the end. */
jeffhaoe23d93c2011-09-15 14:48:43 -07005853 const uint8_t* src_ptr = map->data_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005854 int expected_src_len = DecodeUnsignedLeb128(&src_ptr);
5855 const uint8_t* src_start = src_ptr;
5856
5857 /* Get the initial address and the 16-bit address flag. */
5858 int addr = *src_ptr & 0x7f;
5859 if ((*src_ptr & 0x80) == 0) {
5860 new_format = kRegMapFormatCompact8;
5861 new_addr_width = 1;
5862 } else {
5863 new_format = kRegMapFormatCompact16;
5864 new_addr_width = 2;
5865 }
5866 src_ptr++;
5867
5868 /* Now we know enough to allocate the new map. */
5869 new_data_size = (new_addr_width + reg_width) * num_entries;
5870 RegisterMap* new_map = new RegisterMap(new_format, reg_width, num_entries,
jeffhaoa0a764a2011-09-16 10:43:38 -07005871 new_data_size);
jeffhaod1f0fde2011-09-08 17:25:33 -07005872
5873 /* Write the start address and initial bits to the new map. */
jeffhaoe23d93c2011-09-15 14:48:43 -07005874 uint8_t* dst_ptr = new_map->data_;
jeffhaod1f0fde2011-09-08 17:25:33 -07005875
5876 *dst_ptr++ = addr & 0xff;
5877 if (new_addr_width > 1)
5878 *dst_ptr++ = (uint8_t) (addr >> 8);
5879
5880 memcpy(dst_ptr, src_ptr, reg_width);
5881
5882 int prev_addr = addr;
5883 const uint8_t* prev_bits = dst_ptr; /* point at uncompressed data */
5884
5885 dst_ptr += reg_width;
5886 src_ptr += reg_width;
5887
5888 /* Walk through, uncompressing one line at a time. */
5889 int entry;
5890 for (entry = 1; entry < num_entries; entry++) {
5891 int addr_diff;
5892 uint8_t key;
5893
5894 key = *src_ptr++;
5895
5896 /* Get the address. */
5897 if ((key & 0x07) == 7) {
5898 /* Address diff follows in ULEB128. */
5899 addr_diff = DecodeUnsignedLeb128(&src_ptr);
5900 } else {
5901 addr_diff = (key & 0x07) +1;
5902 }
5903
5904 addr = prev_addr + addr_diff;
5905 *dst_ptr++ = addr & 0xff;
5906 if (new_addr_width > 1)
5907 *dst_ptr++ = (uint8_t) (addr >> 8);
5908
5909 /* Unpack the bits. */
5910 if ((key & 0x08) != 0) {
5911 int bit_count = (key >> 4);
5912 if (bit_count == 0) {
5913 /* No bits changed, just copy previous. */
5914 memcpy(dst_ptr, prev_bits, reg_width);
5915 } else if (bit_count == 15) {
5916 /* Full copy of bit vector is present; ignore prev_bits. */
5917 memcpy(dst_ptr, src_ptr, reg_width);
5918 src_ptr += reg_width;
5919 } else {
5920 /* Copy previous bits and modify listed indices. */
5921 memcpy(dst_ptr, prev_bits, reg_width);
5922 while (bit_count--) {
5923 int bit_index = DecodeUnsignedLeb128(&src_ptr);
5924 ToggleBit(dst_ptr, bit_index);
5925 }
5926 }
5927 } else {
5928 /* Copy previous bits and modify the specified one. */
5929 memcpy(dst_ptr, prev_bits, reg_width);
5930
5931 /* One bit, from 0-15 inclusive, was changed. */
5932 ToggleBit(dst_ptr, key >> 4);
5933 }
5934
5935 prev_addr = addr;
5936 prev_bits = dst_ptr;
5937 dst_ptr += reg_width;
5938 }
5939
jeffhaoe23d93c2011-09-15 14:48:43 -07005940 if (dst_ptr - new_map->data_ != new_data_size) {
5941 LOG(ERROR) << "ERROR: output " << dst_ptr - new_map->data_
jeffhaod1f0fde2011-09-08 17:25:33 -07005942 << " bytes, expected " << new_data_size;
5943 free(new_map);
5944 return NULL;
5945 }
5946
5947 if (src_ptr - src_start != expected_src_len) {
5948 LOG(ERROR) << "ERROR: consumed " << src_ptr - src_start
5949 << " bytes, expected " << expected_src_len;
5950 free(new_map);
5951 return NULL;
5952 }
5953
5954 return new_map;
5955}
5956
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005957} // namespace art