blob: 790d1d9759e5423eaca0dae241374ac6466e4dd1 [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * Dalvik bytecode structural verifier. The only public entry point
19 * (except for a few shared utility functions) is dvmVerifyCodeFlow().
20 *
21 * TODO: might benefit from a signature-->class lookup cache. Could avoid
22 * some string-peeling and wouldn't need to compute hashes.
23 *
24 * TODO: we do too much stuff in here that could be done in the static
25 * verification pass. It's convenient, because we have all of the
26 * necessary information, but it's more efficient to do it over in
27 * DexVerify.c because in here we may have to process instructions
28 * multiple times.
29 */
30#include "Dalvik.h"
31#include "analysis/CodeVerify.h"
Andy McFadden2e1ee502010-03-24 13:25:53 -070032#include "analysis/Optimize.h"
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080033#include "analysis/RegisterMap.h"
34#include "libdex/DexCatch.h"
35#include "libdex/InstrUtils.h"
36
37#include <stddef.h>
38
39
40/*
41 * We don't need to store the register data for many instructions, because
42 * we either only need it at branch points (for verification) or GC points
43 * and branches (for verification + type-precise register analysis).
44 */
45typedef enum RegisterTrackingMode {
46 kTrackRegsBranches,
47 kTrackRegsGcPoints,
48 kTrackRegsAll
49} RegisterTrackingMode;
50
51/*
52 * Set this to enable dead code scanning. This is not required, but it's
53 * very useful when testing changes to the verifier (to make sure we're not
54 * skipping over stuff) and for checking the optimized output from "dx".
55 * The only reason not to do it is that it slightly increases the time
56 * required to perform verification.
57 */
58#define DEAD_CODE_SCAN true
59
60static bool gDebugVerbose = false; // TODO: remove this
61
62#if 0
63int gDvm__totalInstr = 0;
64int gDvm__gcInstr = 0;
65int gDvm__gcData = 0;
66int gDvm__gcSimpleData = 0;
67#endif
68
69/*
70 * Selectively enable verbose debug logging -- use this to activate
71 * dumpRegTypes() calls for all instructions in the specified method.
72 */
73static inline bool doVerboseLogging(const Method* meth) {
74 return false; /* COMMENT OUT to enable verbose debugging */
75
The Android Open Source Project99409882009-03-18 22:20:24 -070076 const char* cd = "Landroid/net/http/Request;";
77 const char* mn = "readResponse";
78 const char* sg = "(Landroid/net/http/AndroidHttpClientConnection;)V";
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080079 return (strcmp(meth->clazz->descriptor, cd) == 0 &&
80 dvmCompareNameDescriptorAndMethod(mn, sg, meth) == 0);
81}
82
83#define SHOW_REG_DETAILS (0 /*| DRT_SHOW_REF_TYPES | DRT_SHOW_LOCALS*/)
84
85/*
86 * We need an extra "pseudo register" to hold the return type briefly. It
87 * can be category 1 or 2, so we need two slots.
88 */
89#define kExtraRegs 2
90#define RESULT_REGISTER(_insnRegCount) (_insnRegCount)
91
92/*
93 * Big fat collection of registers.
94 */
95typedef struct RegisterTable {
96 /*
97 * Array of RegType arrays, one per address in the method. We only
98 * set the pointers for certain addresses, based on what we're trying
99 * to accomplish.
100 */
101 RegType** addrRegs;
102
103 /*
104 * Number of registers we track for each instruction. This is equal
105 * to the method's declared "registersSize" plus kExtraRegs.
106 */
107 int insnRegCountPlus;
108
109 /*
110 * A single large alloc, with all of the storage needed for addrRegs.
111 */
112 RegType* regAlloc;
113} RegisterTable;
114
115
116/* fwd */
Carl Shapiroe3c01da2010-05-20 22:54:18 -0700117#ifndef NDEBUG
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800118static void checkMergeTab(void);
Carl Shapiroe3c01da2010-05-20 22:54:18 -0700119#endif
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800120static bool isInitMethod(const Method* meth);
121static RegType getInvocationThis(const RegType* insnRegs,\
Andy McFaddend3250112010-11-03 14:32:42 -0700122 const DecodedInstruction* pDecInsn, VerifyError* pFailure);
123static void verifyRegisterType(const RegType* insnRegs, \
Andy McFadden62a75162009-04-17 17:23:37 -0700124 u4 vsrc, RegType checkType, VerifyError* pFailure);
Andy McFadden228a6b02010-05-04 15:02:32 -0700125static bool doCodeVerification(const Method* meth, InsnFlags* insnFlags,\
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800126 RegisterTable* regTable, UninitInstanceMap* uninitMap);
Andy McFadden228a6b02010-05-04 15:02:32 -0700127static bool verifyInstruction(const Method* meth, InsnFlags* insnFlags,\
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800128 RegisterTable* regTable, RegType* workRegs, int insnIdx,
129 UninitInstanceMap* uninitMap, int* pStartGuess);
130static ClassObject* findCommonSuperclass(ClassObject* c1, ClassObject* c2);
131static void dumpRegTypes(const Method* meth, const InsnFlags* insnFlags,\
132 const RegType* addrRegs, int addr, const char* addrName,
133 const UninitInstanceMap* uninitMap, int displayFlags);
134
135/* bit values for dumpRegTypes() "displayFlags" */
136enum {
137 DRT_SIMPLE = 0,
138 DRT_SHOW_REF_TYPES = 0x01,
139 DRT_SHOW_LOCALS = 0x02,
140};
141
142
143/*
144 * ===========================================================================
145 * RegType and UninitInstanceMap utility functions
146 * ===========================================================================
147 */
148
149#define __ kRegTypeUnknown
150#define _U kRegTypeUninit
151#define _X kRegTypeConflict
152#define _F kRegTypeFloat
153#define _0 kRegTypeZero
154#define _1 kRegTypeOne
155#define _Z kRegTypeBoolean
156#define _b kRegTypePosByte
157#define _B kRegTypeByte
158#define _s kRegTypePosShort
159#define _S kRegTypeShort
160#define _C kRegTypeChar
161#define _I kRegTypeInteger
162#define _J kRegTypeLongLo
163#define _j kRegTypeLongHi
164#define _D kRegTypeDoubleLo
165#define _d kRegTypeDoubleHi
166
167/*
168 * Merge result table for primitive values. The table is symmetric along
169 * the diagonal.
170 *
171 * Note that 32-bit int/float do not merge into 64-bit long/double. This
172 * is a register merge, not a widening conversion. Only the "implicit"
173 * widening within a category, e.g. byte to short, is allowed.
174 *
175 * Because Dalvik does not draw a distinction between int and float, we
176 * have to allow free exchange between 32-bit int/float and 64-bit
177 * long/double.
178 *
179 * Note that Uninit+Uninit=Uninit. This holds true because we only
180 * use this when the RegType value is exactly equal to kRegTypeUninit, which
181 * can only happen for the zeroeth entry in the table.
182 *
183 * "Unknown" never merges with anything known. The only time a register
184 * transitions from "unknown" to "known" is when we're executing code
185 * for the first time, and we handle that with a simple copy.
186 */
187const char gDvmMergeTab[kRegTypeMAX][kRegTypeMAX] =
188{
189 /* chk: _ U X F 0 1 Z b B s S C I J j D d */
190 { /*_*/ __,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X },
191 { /*U*/ _X,_U,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X },
192 { /*X*/ _X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X },
193 { /*F*/ _X,_X,_X,_F,_F,_F,_F,_F,_F,_F,_F,_F,_F,_X,_X,_X,_X },
194 { /*0*/ _X,_X,_X,_F,_0,_Z,_Z,_b,_B,_s,_S,_C,_I,_X,_X,_X,_X },
195 { /*1*/ _X,_X,_X,_F,_Z,_1,_Z,_b,_B,_s,_S,_C,_I,_X,_X,_X,_X },
196 { /*Z*/ _X,_X,_X,_F,_Z,_Z,_Z,_b,_B,_s,_S,_C,_I,_X,_X,_X,_X },
197 { /*b*/ _X,_X,_X,_F,_b,_b,_b,_b,_B,_s,_S,_C,_I,_X,_X,_X,_X },
198 { /*B*/ _X,_X,_X,_F,_B,_B,_B,_B,_B,_S,_S,_I,_I,_X,_X,_X,_X },
199 { /*s*/ _X,_X,_X,_F,_s,_s,_s,_s,_S,_s,_S,_C,_I,_X,_X,_X,_X },
200 { /*S*/ _X,_X,_X,_F,_S,_S,_S,_S,_S,_S,_S,_I,_I,_X,_X,_X,_X },
201 { /*C*/ _X,_X,_X,_F,_C,_C,_C,_C,_I,_C,_I,_C,_I,_X,_X,_X,_X },
202 { /*I*/ _X,_X,_X,_F,_I,_I,_I,_I,_I,_I,_I,_I,_I,_X,_X,_X,_X },
203 { /*J*/ _X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_J,_X,_J,_X },
204 { /*j*/ _X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_j,_X,_j },
205 { /*D*/ _X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_J,_X,_D,_X },
206 { /*d*/ _X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_j,_X,_d },
207};
208
209#undef __
210#undef _U
211#undef _X
212#undef _F
213#undef _0
214#undef _1
215#undef _Z
216#undef _b
217#undef _B
218#undef _s
219#undef _S
220#undef _C
221#undef _I
222#undef _J
223#undef _j
224#undef _D
225#undef _d
226
227#ifndef NDEBUG
228/*
229 * Verify symmetry in the conversion table.
230 */
231static void checkMergeTab(void)
232{
233 int i, j;
234
235 for (i = 0; i < kRegTypeMAX; i++) {
236 for (j = i; j < kRegTypeMAX; j++) {
237 if (gDvmMergeTab[i][j] != gDvmMergeTab[j][i]) {
238 LOGE("Symmetry violation: %d,%d vs %d,%d\n", i, j, j, i);
239 dvmAbort();
240 }
241 }
242 }
243}
244#endif
245
246/*
247 * Determine whether we can convert "srcType" to "checkType", where
248 * "checkType" is one of the category-1 non-reference types.
249 *
250 * 32-bit int and float are interchangeable.
251 */
252static bool canConvertTo1nr(RegType srcType, RegType checkType)
253{
254 static const char convTab
255 [kRegType1nrEND-kRegType1nrSTART+1][kRegType1nrEND-kRegType1nrSTART+1] =
256 {
257 /* chk: F 0 1 Z b B s S C I */
258 { /*F*/ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
259 { /*0*/ 1, 1, 0, 1, 1, 1, 1, 1, 1, 1 },
260 { /*1*/ 1, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
261 { /*Z*/ 1, 0, 0, 1, 1, 1, 1, 1, 1, 1 },
262 { /*b*/ 1, 0, 0, 0, 1, 1, 1, 1, 1, 1 },
263 { /*B*/ 1, 0, 0, 0, 0, 1, 0, 1, 0, 1 },
264 { /*s*/ 1, 0, 0, 0, 0, 0, 1, 1, 1, 1 },
265 { /*S*/ 1, 0, 0, 0, 0, 0, 0, 1, 0, 1 },
266 { /*C*/ 1, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
267 { /*I*/ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
268 };
269
270 assert(checkType >= kRegType1nrSTART && checkType <= kRegType1nrEND);
271#if 0
272 if (checkType < kRegType1nrSTART || checkType > kRegType1nrEND) {
273 LOG_VFY("Unexpected checkType %d (srcType=%d)\n", checkType, srcType);
274 assert(false);
275 return false;
276 }
277#endif
278
279 //printf("convTab[%d][%d] = %d\n", srcType, checkType,
280 // convTab[srcType-kRegType1nrSTART][checkType-kRegType1nrSTART]);
281 if (srcType >= kRegType1nrSTART && srcType <= kRegType1nrEND)
282 return (bool) convTab[srcType-kRegType1nrSTART][checkType-kRegType1nrSTART];
283
284 return false;
285}
286
287/*
288 * Determine whether the types are compatible. In Dalvik, 64-bit doubles
289 * and longs are interchangeable.
290 */
291static bool canConvertTo2(RegType srcType, RegType checkType)
292{
293 return ((srcType == kRegTypeLongLo || srcType == kRegTypeDoubleLo) &&
294 (checkType == kRegTypeLongLo || checkType == kRegTypeDoubleLo));
295}
296
297/*
298 * Determine whether or not "instrType" and "targetType" are compatible,
299 * for purposes of getting or setting a value in a field or array. The
300 * idea is that an instruction with a category 1nr type (say, aget-short
301 * or iput-boolean) is accessing a static field, instance field, or array
302 * entry, and we want to make sure sure that the operation is legal.
303 *
304 * At a minimum, source and destination must have the same width. We
305 * further refine this to assert that "short" and "char" are not
306 * compatible, because the sign-extension is different on the "get"
307 * operations. As usual, "float" and "int" are interoperable.
308 *
309 * We're not considering the actual contents of the register, so we'll
310 * never get "pseudo-types" like kRegTypeZero or kRegTypePosShort. We
311 * could get kRegTypeUnknown in "targetType" if a field or array class
312 * lookup failed. Category 2 types and references are checked elsewhere.
313 */
314static bool checkFieldArrayStore1nr(RegType instrType, RegType targetType)
315{
316 if (instrType == targetType)
317 return true; /* quick positive; most common case */
318
319 if ((instrType == kRegTypeInteger && targetType == kRegTypeFloat) ||
320 (instrType == kRegTypeFloat && targetType == kRegTypeInteger))
321 {
322 return true;
323 }
324
325 return false;
326}
327
328/*
329 * Convert a VM PrimitiveType enum value to the equivalent RegType value.
330 */
331static RegType primitiveTypeToRegType(PrimitiveType primType)
332{
The Android Open Source Project99409882009-03-18 22:20:24 -0700333 static const struct {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800334 RegType regType; /* type equivalent */
335 PrimitiveType primType; /* verification */
336 } convTab[] = {
337 /* must match order of enum in Object.h */
338 { kRegTypeBoolean, PRIM_BOOLEAN },
339 { kRegTypeChar, PRIM_CHAR },
340 { kRegTypeFloat, PRIM_FLOAT },
341 { kRegTypeDoubleLo, PRIM_DOUBLE },
342 { kRegTypeByte, PRIM_BYTE },
343 { kRegTypeShort, PRIM_SHORT },
344 { kRegTypeInteger, PRIM_INT },
345 { kRegTypeLongLo, PRIM_LONG },
346 // PRIM_VOID
347 };
348
349 if (primType < 0 || primType > (int) (sizeof(convTab) / sizeof(convTab[0])))
350 {
351 assert(false);
352 return kRegTypeUnknown;
353 }
354
355 assert(convTab[primType].primType == primType);
356 return convTab[primType].regType;
357}
358
359/*
Andy McFadden470cbbb2010-11-04 16:31:37 -0700360 * Given a 32-bit constant, return the most-restricted RegType enum entry
361 * that can hold the value.
362 */
363static char determineCat1Const(s4 value)
364{
365 if (value < -32768)
366 return kRegTypeInteger;
367 else if (value < -128)
368 return kRegTypeShort;
369 else if (value < 0)
370 return kRegTypeByte;
371 else if (value == 0)
372 return kRegTypeZero;
373 else if (value == 1)
374 return kRegTypeOne;
375 else if (value < 128)
376 return kRegTypePosByte;
377 else if (value < 32768)
378 return kRegTypePosShort;
379 else if (value < 65536)
380 return kRegTypeChar;
381 else
382 return kRegTypeInteger;
383}
384
385/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800386 * Create a new uninitialized instance map.
387 *
388 * The map is allocated and populated with address entries. The addresses
389 * appear in ascending order to allow binary searching.
390 *
391 * Very few methods have 10 or more new-instance instructions; the
392 * majority have 0 or 1. Occasionally a static initializer will have 200+.
393 */
394UninitInstanceMap* dvmCreateUninitInstanceMap(const Method* meth,
395 const InsnFlags* insnFlags, int newInstanceCount)
396{
397 const int insnsSize = dvmGetMethodInsnsSize(meth);
398 const u2* insns = meth->insns;
399 UninitInstanceMap* uninitMap;
400 bool isInit = false;
401 int idx, addr;
402
403 if (isInitMethod(meth)) {
404 newInstanceCount++;
405 isInit = true;
406 }
407
408 /*
409 * Allocate the header and map as a single unit.
410 *
411 * TODO: consider having a static instance so we can avoid allocations.
412 * I don't think the verifier is guaranteed to be single-threaded when
413 * running in the VM (rather than dexopt), so that must be taken into
414 * account.
415 */
416 int size = offsetof(UninitInstanceMap, map) +
417 newInstanceCount * sizeof(uninitMap->map[0]);
418 uninitMap = calloc(1, size);
419 if (uninitMap == NULL)
420 return NULL;
421 uninitMap->numEntries = newInstanceCount;
422
423 idx = 0;
424 if (isInit) {
425 uninitMap->map[idx++].addr = kUninitThisArgAddr;
426 }
427
428 /*
429 * Run through and find the new-instance instructions.
430 */
431 for (addr = 0; addr < insnsSize; /**/) {
432 int width = dvmInsnGetWidth(insnFlags, addr);
433
434 if ((*insns & 0xff) == OP_NEW_INSTANCE)
435 uninitMap->map[idx++].addr = addr;
436
437 addr += width;
438 insns += width;
439 }
440
441 assert(idx == newInstanceCount);
442 return uninitMap;
443}
444
445/*
446 * Free the map.
447 */
448void dvmFreeUninitInstanceMap(UninitInstanceMap* uninitMap)
449{
450 free(uninitMap);
451}
452
453/*
454 * Set the class object associated with the instruction at "addr".
455 *
456 * Returns the map slot index, or -1 if the address isn't listed in the map
457 * (shouldn't happen) or if a class is already associated with the address
458 * (bad bytecode).
459 *
460 * Entries, once set, do not change -- a given address can only allocate
461 * one type of object.
462 */
463int dvmSetUninitInstance(UninitInstanceMap* uninitMap, int addr,
464 ClassObject* clazz)
465{
466 int idx;
467
468 assert(clazz != NULL);
469
470 /* TODO: binary search when numEntries > 8 */
471 for (idx = uninitMap->numEntries - 1; idx >= 0; idx--) {
472 if (uninitMap->map[idx].addr == addr) {
473 if (uninitMap->map[idx].clazz != NULL &&
474 uninitMap->map[idx].clazz != clazz)
475 {
476 LOG_VFY("VFY: addr %d already set to %p, not setting to %p\n",
477 addr, uninitMap->map[idx].clazz, clazz);
478 return -1; // already set to something else??
479 }
480 uninitMap->map[idx].clazz = clazz;
481 return idx;
482 }
483 }
484
485 LOG_VFY("VFY: addr %d not found in uninit map\n", addr);
486 assert(false); // shouldn't happen
487 return -1;
488}
489
490/*
491 * Get the class object at the specified index.
492 */
493ClassObject* dvmGetUninitInstance(const UninitInstanceMap* uninitMap, int idx)
494{
495 assert(idx >= 0 && idx < uninitMap->numEntries);
496 return uninitMap->map[idx].clazz;
497}
498
499/* determine if "type" is actually an object reference (init/uninit/zero) */
500static inline bool regTypeIsReference(RegType type) {
501 return (type > kRegTypeMAX || type == kRegTypeUninit ||
502 type == kRegTypeZero);
503}
504
505/* determine if "type" is an uninitialized object reference */
506static inline bool regTypeIsUninitReference(RegType type) {
507 return ((type & kRegTypeUninitMask) == kRegTypeUninit);
508}
509
510/* convert the initialized reference "type" to a ClassObject pointer */
511/* (does not expect uninit ref types or "zero") */
512static ClassObject* regTypeInitializedReferenceToClass(RegType type)
513{
514 assert(regTypeIsReference(type) && type != kRegTypeZero);
515 if ((type & 0x01) == 0) {
516 return (ClassObject*) type;
517 } else {
518 //LOG_VFY("VFY: attempted to use uninitialized reference\n");
519 return NULL;
520 }
521}
522
523/* extract the index into the uninitialized instance map table */
524static inline int regTypeToUninitIndex(RegType type) {
525 assert(regTypeIsUninitReference(type));
526 return (type & ~kRegTypeUninitMask) >> kRegTypeUninitShift;
527}
528
529/* convert the reference "type" to a ClassObject pointer */
530static ClassObject* regTypeReferenceToClass(RegType type,
531 const UninitInstanceMap* uninitMap)
532{
533 assert(regTypeIsReference(type) && type != kRegTypeZero);
534 if (regTypeIsUninitReference(type)) {
535 assert(uninitMap != NULL);
536 return dvmGetUninitInstance(uninitMap, regTypeToUninitIndex(type));
537 } else {
538 return (ClassObject*) type;
539 }
540}
541
542/* convert the ClassObject pointer to an (initialized) register type */
543static inline RegType regTypeFromClass(ClassObject* clazz) {
544 return (u4) clazz;
545}
546
547/* return the RegType for the uninitialized reference in slot "uidx" */
548static RegType regTypeFromUninitIndex(int uidx) {
549 return (u4) (kRegTypeUninit | (uidx << kRegTypeUninitShift));
550}
551
552
553/*
554 * ===========================================================================
555 * Signature operations
556 * ===========================================================================
557 */
558
559/*
560 * Is this method a constructor?
561 */
562static bool isInitMethod(const Method* meth)
563{
564 return (*meth->name == '<' && strcmp(meth->name+1, "init>") == 0);
565}
566
567/*
568 * Is this method a class initializer?
569 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -0700570#if 0
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800571static bool isClassInitMethod(const Method* meth)
572{
573 return (*meth->name == '<' && strcmp(meth->name+1, "clinit>") == 0);
574}
Carl Shapiroe3c01da2010-05-20 22:54:18 -0700575#endif
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800576
577/*
578 * Look up a class reference given as a simple string descriptor.
Andy McFadden62a75162009-04-17 17:23:37 -0700579 *
580 * If we can't find it, return a generic substitute when possible.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800581 */
582static ClassObject* lookupClassByDescriptor(const Method* meth,
Andy McFadden62a75162009-04-17 17:23:37 -0700583 const char* pDescriptor, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800584{
585 /*
586 * The javac compiler occasionally puts references to nonexistent
587 * classes in signatures. For example, if you have a non-static
588 * inner class with no constructor, the compiler provides
589 * a private <init> for you. Constructing the class
590 * requires <init>(parent), but the outer class can't call
591 * that because the method is private. So the compiler
592 * generates a package-scope <init>(parent,bogus) method that
593 * just calls the regular <init> (the "bogus" part being necessary
594 * to distinguish the signature of the synthetic method).
595 * Treating the bogus class as an instance of java.lang.Object
596 * allows the verifier to process the class successfully.
597 */
598
599 //LOGI("Looking up '%s'\n", typeStr);
600 ClassObject* clazz;
601 clazz = dvmFindClassNoInit(pDescriptor, meth->clazz->classLoader);
602 if (clazz == NULL) {
603 dvmClearOptException(dvmThreadSelf());
604 if (strchr(pDescriptor, '$') != NULL) {
605 LOGV("VFY: unable to find class referenced in signature (%s)\n",
606 pDescriptor);
607 } else {
608 LOG_VFY("VFY: unable to find class referenced in signature (%s)\n",
609 pDescriptor);
610 }
611
612 if (pDescriptor[0] == '[') {
613 /* We are looking at an array descriptor. */
614
615 /*
616 * There should never be a problem loading primitive arrays.
617 */
618 if (pDescriptor[1] != 'L' && pDescriptor[1] != '[') {
619 LOG_VFY("VFY: invalid char in signature in '%s'\n",
620 pDescriptor);
Andy McFadden62a75162009-04-17 17:23:37 -0700621 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800622 }
623
624 /*
625 * Try to continue with base array type. This will let
626 * us pass basic stuff (e.g. get array len) that wouldn't
627 * fly with an Object. This is NOT correct if the
628 * missing type is a primitive array, but we should never
629 * have a problem loading those. (I'm not convinced this
630 * is correct or even useful. Just use Object here?)
631 */
632 clazz = dvmFindClassNoInit("[Ljava/lang/Object;",
633 meth->clazz->classLoader);
634 } else if (pDescriptor[0] == 'L') {
635 /*
636 * We are looking at a non-array reference descriptor;
637 * try to continue with base reference type.
638 */
639 clazz = gDvm.classJavaLangObject;
640 } else {
641 /* We are looking at a primitive type. */
642 LOG_VFY("VFY: invalid char in signature in '%s'\n", pDescriptor);
Andy McFadden62a75162009-04-17 17:23:37 -0700643 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800644 }
645
646 if (clazz == NULL) {
Andy McFadden62a75162009-04-17 17:23:37 -0700647 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800648 }
649 }
650
651 if (dvmIsPrimitiveClass(clazz)) {
652 LOG_VFY("VFY: invalid use of primitive type '%s'\n", pDescriptor);
Andy McFadden62a75162009-04-17 17:23:37 -0700653 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800654 clazz = NULL;
655 }
656
657 return clazz;
658}
659
660/*
661 * Look up a class reference in a signature. Could be an arg or the
662 * return value.
663 *
664 * Advances "*pSig" to the last character in the signature (that is, to
665 * the ';').
666 *
667 * NOTE: this is also expected to verify the signature.
668 */
669static ClassObject* lookupSignatureClass(const Method* meth, const char** pSig,
Andy McFadden62a75162009-04-17 17:23:37 -0700670 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800671{
672 const char* sig = *pSig;
673 const char* endp = sig;
674
675 assert(sig != NULL && *sig == 'L');
676
677 while (*++endp != ';' && *endp != '\0')
678 ;
679 if (*endp != ';') {
680 LOG_VFY("VFY: bad signature component '%s' (missing ';')\n", sig);
Andy McFadden62a75162009-04-17 17:23:37 -0700681 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800682 return NULL;
683 }
684
685 endp++; /* Advance past the ';'. */
686 int typeLen = endp - sig;
687 char typeStr[typeLen+1]; /* +1 for the '\0' */
688 memcpy(typeStr, sig, typeLen);
689 typeStr[typeLen] = '\0';
690
691 *pSig = endp - 1; /* - 1 so that *pSig points at, not past, the ';' */
692
Andy McFadden62a75162009-04-17 17:23:37 -0700693 return lookupClassByDescriptor(meth, typeStr, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800694}
695
696/*
697 * Look up an array class reference in a signature. Could be an arg or the
698 * return value.
699 *
700 * Advances "*pSig" to the last character in the signature.
701 *
702 * NOTE: this is also expected to verify the signature.
703 */
704static ClassObject* lookupSignatureArrayClass(const Method* meth,
Andy McFadden62a75162009-04-17 17:23:37 -0700705 const char** pSig, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800706{
707 const char* sig = *pSig;
708 const char* endp = sig;
709
710 assert(sig != NULL && *sig == '[');
711
712 /* find the end */
713 while (*++endp == '[' && *endp != '\0')
714 ;
715
716 if (*endp == 'L') {
717 while (*++endp != ';' && *endp != '\0')
718 ;
719 if (*endp != ';') {
720 LOG_VFY("VFY: bad signature component '%s' (missing ';')\n", sig);
Andy McFadden62a75162009-04-17 17:23:37 -0700721 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800722 return NULL;
723 }
724 }
725
726 int typeLen = endp - sig +1;
727 char typeStr[typeLen+1];
728 memcpy(typeStr, sig, typeLen);
729 typeStr[typeLen] = '\0';
730
731 *pSig = endp;
732
Andy McFadden62a75162009-04-17 17:23:37 -0700733 return lookupClassByDescriptor(meth, typeStr, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800734}
735
736/*
737 * Set the register types for the first instruction in the method based on
738 * the method signature.
739 *
740 * This has the side-effect of validating the signature.
741 *
742 * Returns "true" on success.
743 */
744static bool setTypesFromSignature(const Method* meth, RegType* regTypes,
745 UninitInstanceMap* uninitMap)
746{
747 DexParameterIterator iterator;
748 int actualArgs, expectedArgs, argStart;
Andy McFadden62a75162009-04-17 17:23:37 -0700749 VerifyError failure = VERIFY_ERROR_NONE;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800750
751 dexParameterIteratorInit(&iterator, &meth->prototype);
752 argStart = meth->registersSize - meth->insSize;
753 expectedArgs = meth->insSize; /* long/double count as two */
754 actualArgs = 0;
755
756 assert(argStart >= 0); /* should have been verified earlier */
757
758 /*
759 * Include the "this" pointer.
760 */
761 if (!dvmIsStaticMethod(meth)) {
762 /*
763 * If this is a constructor for a class other than java.lang.Object,
764 * mark the first ("this") argument as uninitialized. This restricts
765 * field access until the superclass constructor is called.
766 */
767 if (isInitMethod(meth) && meth->clazz != gDvm.classJavaLangObject) {
768 int uidx = dvmSetUninitInstance(uninitMap, kUninitThisArgAddr,
769 meth->clazz);
770 assert(uidx == 0);
771 regTypes[argStart + actualArgs] = regTypeFromUninitIndex(uidx);
772 } else {
773 regTypes[argStart + actualArgs] = regTypeFromClass(meth->clazz);
774 }
775 actualArgs++;
776 }
777
778 for (;;) {
779 const char* descriptor = dexParameterIteratorNextDescriptor(&iterator);
780
781 if (descriptor == NULL) {
782 break;
783 }
784
785 if (actualArgs >= expectedArgs) {
786 LOG_VFY("VFY: expected %d args, found more (%s)\n",
787 expectedArgs, descriptor);
788 goto bad_sig;
789 }
790
791 switch (*descriptor) {
792 case 'L':
793 case '[':
794 /*
795 * We assume that reference arguments are initialized. The
796 * only way it could be otherwise (assuming the caller was
797 * verified) is if the current method is <init>, but in that
798 * case it's effectively considered initialized the instant
799 * we reach here (in the sense that we can return without
800 * doing anything or call virtual methods).
801 */
802 {
803 ClassObject* clazz =
Andy McFadden62a75162009-04-17 17:23:37 -0700804 lookupClassByDescriptor(meth, descriptor, &failure);
805 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800806 goto bad_sig;
807 regTypes[argStart + actualArgs] = regTypeFromClass(clazz);
808 }
809 actualArgs++;
810 break;
811 case 'Z':
812 regTypes[argStart + actualArgs] = kRegTypeBoolean;
813 actualArgs++;
814 break;
815 case 'C':
816 regTypes[argStart + actualArgs] = kRegTypeChar;
817 actualArgs++;
818 break;
819 case 'B':
820 regTypes[argStart + actualArgs] = kRegTypeByte;
821 actualArgs++;
822 break;
823 case 'I':
824 regTypes[argStart + actualArgs] = kRegTypeInteger;
825 actualArgs++;
826 break;
827 case 'S':
828 regTypes[argStart + actualArgs] = kRegTypeShort;
829 actualArgs++;
830 break;
831 case 'F':
832 regTypes[argStart + actualArgs] = kRegTypeFloat;
833 actualArgs++;
834 break;
835 case 'D':
836 regTypes[argStart + actualArgs] = kRegTypeDoubleLo;
837 regTypes[argStart + actualArgs +1] = kRegTypeDoubleHi;
838 actualArgs += 2;
839 break;
840 case 'J':
841 regTypes[argStart + actualArgs] = kRegTypeLongLo;
842 regTypes[argStart + actualArgs +1] = kRegTypeLongHi;
843 actualArgs += 2;
844 break;
845 default:
846 LOG_VFY("VFY: unexpected signature type char '%c'\n", *descriptor);
847 goto bad_sig;
848 }
849 }
850
851 if (actualArgs != expectedArgs) {
852 LOG_VFY("VFY: expected %d args, found %d\n", expectedArgs, actualArgs);
853 goto bad_sig;
854 }
855
856 const char* descriptor = dexProtoGetReturnType(&meth->prototype);
857
858 /*
859 * Validate return type. We don't do the type lookup; just want to make
860 * sure that it has the right format. Only major difference from the
861 * method argument format is that 'V' is supported.
862 */
863 switch (*descriptor) {
864 case 'I':
865 case 'C':
866 case 'S':
867 case 'B':
868 case 'Z':
869 case 'V':
870 case 'F':
871 case 'D':
872 case 'J':
873 if (*(descriptor+1) != '\0')
874 goto bad_sig;
875 break;
876 case '[':
877 /* single/multi, object/primitive */
878 while (*++descriptor == '[')
879 ;
880 if (*descriptor == 'L') {
881 while (*++descriptor != ';' && *descriptor != '\0')
882 ;
883 if (*descriptor != ';')
884 goto bad_sig;
885 } else {
886 if (*(descriptor+1) != '\0')
887 goto bad_sig;
888 }
889 break;
890 case 'L':
891 /* could be more thorough here, but shouldn't be required */
892 while (*++descriptor != ';' && *descriptor != '\0')
893 ;
894 if (*descriptor != ';')
895 goto bad_sig;
896 break;
897 default:
898 goto bad_sig;
899 }
900
901 return true;
902
903//fail:
904// LOG_VFY_METH(meth, "VFY: bad sig\n");
905// return false;
906
907bad_sig:
908 {
909 char* desc = dexProtoCopyMethodDescriptor(&meth->prototype);
910 LOG_VFY("VFY: bad signature '%s' for %s.%s\n",
911 desc, meth->clazz->descriptor, meth->name);
912 free(desc);
913 }
914 return false;
915}
916
917/*
918 * Return the register type for the method. We can't just use the
919 * already-computed DalvikJniReturnType, because if it's a reference type
920 * we need to do the class lookup.
921 *
922 * Returned references are assumed to be initialized.
923 *
924 * Returns kRegTypeUnknown for "void".
925 */
926static RegType getMethodReturnType(const Method* meth)
927{
928 RegType type;
929 const char* descriptor = dexProtoGetReturnType(&meth->prototype);
930
931 switch (*descriptor) {
932 case 'I':
933 type = kRegTypeInteger;
934 break;
935 case 'C':
936 type = kRegTypeChar;
937 break;
938 case 'S':
939 type = kRegTypeShort;
940 break;
941 case 'B':
942 type = kRegTypeByte;
943 break;
944 case 'Z':
945 type = kRegTypeBoolean;
946 break;
947 case 'V':
948 type = kRegTypeUnknown;
949 break;
950 case 'F':
951 type = kRegTypeFloat;
952 break;
953 case 'D':
954 type = kRegTypeDoubleLo;
955 break;
956 case 'J':
957 type = kRegTypeLongLo;
958 break;
959 case 'L':
960 case '[':
961 {
Andy McFadden62a75162009-04-17 17:23:37 -0700962 VerifyError failure = VERIFY_ERROR_NONE;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800963 ClassObject* clazz =
Andy McFadden62a75162009-04-17 17:23:37 -0700964 lookupClassByDescriptor(meth, descriptor, &failure);
965 assert(VERIFY_OK(failure));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800966 type = regTypeFromClass(clazz);
967 }
968 break;
969 default:
970 /* we verified signature return type earlier, so this is impossible */
971 assert(false);
972 type = kRegTypeConflict;
973 break;
974 }
975
976 return type;
977}
978
979/*
980 * Convert a single-character signature value (i.e. a primitive type) to
981 * the corresponding RegType. This is intended for access to object fields
982 * holding primitive types.
983 *
984 * Returns kRegTypeUnknown for objects, arrays, and void.
985 */
986static RegType primSigCharToRegType(char sigChar)
987{
988 RegType type;
989
990 switch (sigChar) {
991 case 'I':
992 type = kRegTypeInteger;
993 break;
994 case 'C':
995 type = kRegTypeChar;
996 break;
997 case 'S':
998 type = kRegTypeShort;
999 break;
1000 case 'B':
1001 type = kRegTypeByte;
1002 break;
1003 case 'Z':
1004 type = kRegTypeBoolean;
1005 break;
1006 case 'F':
1007 type = kRegTypeFloat;
1008 break;
1009 case 'D':
1010 type = kRegTypeDoubleLo;
1011 break;
1012 case 'J':
1013 type = kRegTypeLongLo;
1014 break;
1015 case 'V':
1016 case 'L':
1017 case '[':
1018 type = kRegTypeUnknown;
1019 break;
1020 default:
1021 assert(false);
1022 type = kRegTypeUnknown;
1023 break;
1024 }
1025
1026 return type;
1027}
1028
1029/*
Andy McFadden99a33e72010-10-10 12:59:11 -07001030 * See if the method matches the MethodType.
1031 */
1032static bool isCorrectInvokeKind(MethodType methodType, Method* resMethod)
1033{
1034 switch (methodType) {
1035 case METHOD_DIRECT:
1036 return dvmIsDirectMethod(resMethod);
1037 case METHOD_STATIC:
1038 return dvmIsStaticMethod(resMethod);
1039 case METHOD_VIRTUAL:
1040 case METHOD_INTERFACE:
1041 return !dvmIsDirectMethod(resMethod);
1042 default:
1043 return false;
1044 }
1045}
1046
1047/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001048 * Verify the arguments to a method. We're executing in "method", making
1049 * a call to the method reference in vB.
1050 *
1051 * If this is a "direct" invoke, we allow calls to <init>. For calls to
1052 * <init>, the first argument may be an uninitialized reference. Otherwise,
1053 * calls to anything starting with '<' will be rejected, as will any
1054 * uninitialized reference arguments.
1055 *
1056 * For non-static method calls, this will verify that the method call is
1057 * appropriate for the "this" argument.
1058 *
1059 * The method reference is in vBBBB. The "isRange" parameter determines
1060 * whether we use 0-4 "args" values or a range of registers defined by
1061 * vAA and vCCCC.
1062 *
1063 * Widening conversions on integers and references are allowed, but
1064 * narrowing conversions are not.
1065 *
Andy McFadden62a75162009-04-17 17:23:37 -07001066 * Returns the resolved method on success, NULL on failure (with *pFailure
1067 * set appropriately).
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001068 */
1069static Method* verifyInvocationArgs(const Method* meth, const RegType* insnRegs,
1070 const int insnRegCount, const DecodedInstruction* pDecInsn,
1071 UninitInstanceMap* uninitMap, MethodType methodType, bool isRange,
Andy McFadden62a75162009-04-17 17:23:37 -07001072 bool isSuper, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001073{
1074 Method* resMethod;
1075 char* sigOriginal = NULL;
1076
1077 /*
1078 * Resolve the method. This could be an abstract or concrete method
1079 * depending on what sort of call we're making.
1080 */
1081 if (methodType == METHOD_INTERFACE) {
1082 resMethod = dvmOptResolveInterfaceMethod(meth->clazz, pDecInsn->vB);
1083 } else {
Andy McFadden62a75162009-04-17 17:23:37 -07001084 resMethod = dvmOptResolveMethod(meth->clazz, pDecInsn->vB, methodType,
1085 pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001086 }
1087 if (resMethod == NULL) {
1088 /* failed; print a meaningful failure message */
1089 DexFile* pDexFile = meth->clazz->pDvmDex->pDexFile;
1090 const DexMethodId* pMethodId;
1091 const char* methodName;
1092 char* methodDesc;
1093 const char* classDescriptor;
1094
1095 pMethodId = dexGetMethodId(pDexFile, pDecInsn->vB);
1096 methodName = dexStringById(pDexFile, pMethodId->nameIdx);
1097 methodDesc = dexCopyDescriptorFromMethodId(pDexFile, pMethodId);
1098 classDescriptor = dexStringByTypeIdx(pDexFile, pMethodId->classIdx);
1099
1100 if (!gDvm.optimizing) {
1101 char* dotMissingClass = dvmDescriptorToDot(classDescriptor);
1102 char* dotMethClass = dvmDescriptorToDot(meth->clazz->descriptor);
1103 //char* curMethodDesc =
1104 // dexProtoCopyMethodDescriptor(&meth->prototype);
1105
Andy McFadden99a33e72010-10-10 12:59:11 -07001106 LOGI("Could not find method %s.%s, referenced from method %s.%s\n",
1107 dotMissingClass, methodName/*, methodDesc*/,
1108 dotMethClass, meth->name/*, curMethodDesc*/);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001109
1110 free(dotMissingClass);
1111 free(dotMethClass);
1112 //free(curMethodDesc);
1113 }
1114
1115 LOG_VFY("VFY: unable to resolve %s method %u: %s.%s %s\n",
1116 dvmMethodTypeStr(methodType), pDecInsn->vB,
1117 classDescriptor, methodName, methodDesc);
1118 free(methodDesc);
Andy McFaddenb51ea112009-05-08 16:50:17 -07001119 if (VERIFY_OK(*pFailure)) /* not set for interface resolve */
1120 *pFailure = VERIFY_ERROR_NO_METHOD;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001121 goto fail;
1122 }
1123
1124 /*
1125 * Only time you can explicitly call a method starting with '<' is when
1126 * making a "direct" invocation on "<init>". There are additional
1127 * restrictions but we don't enforce them here.
1128 */
1129 if (resMethod->name[0] == '<') {
1130 if (methodType != METHOD_DIRECT || !isInitMethod(resMethod)) {
1131 LOG_VFY("VFY: invalid call to %s.%s\n",
1132 resMethod->clazz->descriptor, resMethod->name);
1133 goto bad_sig;
1134 }
1135 }
1136
1137 /*
Andy McFadden99a33e72010-10-10 12:59:11 -07001138 * See if the method type implied by the invoke instruction matches the
1139 * access flags for the target method.
1140 */
1141 if (!isCorrectInvokeKind(methodType, resMethod)) {
1142 LOG_VFY("VFY: invoke type does not match method type of %s.%s\n",
1143 resMethod->clazz->descriptor, resMethod->name);
1144 goto fail;
1145 }
1146
1147 /*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001148 * If we're using invoke-super(method), make sure that the executing
1149 * method's class' superclass has a vtable entry for the target method.
1150 */
1151 if (isSuper) {
1152 assert(methodType == METHOD_VIRTUAL);
1153 ClassObject* super = meth->clazz->super;
1154 if (super == NULL || resMethod->methodIndex > super->vtableCount) {
1155 char* desc = dexProtoCopyMethodDescriptor(&resMethod->prototype);
1156 LOG_VFY("VFY: invalid invoke-super from %s.%s to super %s.%s %s\n",
1157 meth->clazz->descriptor, meth->name,
1158 (super == NULL) ? "-" : super->descriptor,
1159 resMethod->name, desc);
1160 free(desc);
Andy McFadden62a75162009-04-17 17:23:37 -07001161 *pFailure = VERIFY_ERROR_NO_METHOD;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001162 goto fail;
1163 }
1164 }
1165
1166 /*
1167 * We use vAA as our expected arg count, rather than resMethod->insSize,
1168 * because we need to match the call to the signature. Also, we might
1169 * might be calling through an abstract method definition (which doesn't
1170 * have register count values).
1171 */
1172 sigOriginal = dexProtoCopyMethodDescriptor(&resMethod->prototype);
1173 const char* sig = sigOriginal;
1174 int expectedArgs = pDecInsn->vA;
1175 int actualArgs = 0;
1176
Andy McFaddend3250112010-11-03 14:32:42 -07001177 /* caught by static verifier */
1178 assert(isRange || expectedArgs <= 5);
1179
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001180 if (expectedArgs > meth->outsSize) {
1181 LOG_VFY("VFY: invalid arg count (%d) exceeds outsSize (%d)\n",
1182 expectedArgs, meth->outsSize);
1183 goto fail;
1184 }
1185
1186 if (*sig++ != '(')
1187 goto bad_sig;
1188
1189 /*
1190 * Check the "this" argument, which must be an instance of the class
1191 * that declared the method. For an interface class, we don't do the
1192 * full interface merge, so we can't do a rigorous check here (which
1193 * is okay since we have to do it at runtime).
1194 */
1195 if (!dvmIsStaticMethod(resMethod)) {
1196 ClassObject* actualThisRef;
1197 RegType actualArgType;
1198
Andy McFaddend3250112010-11-03 14:32:42 -07001199 actualArgType = getInvocationThis(insnRegs, pDecInsn, pFailure);
Andy McFadden62a75162009-04-17 17:23:37 -07001200 if (!VERIFY_OK(*pFailure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001201 goto fail;
1202
1203 if (regTypeIsUninitReference(actualArgType) && resMethod->name[0] != '<')
1204 {
1205 LOG_VFY("VFY: 'this' arg must be initialized\n");
1206 goto fail;
1207 }
1208 if (methodType != METHOD_INTERFACE && actualArgType != kRegTypeZero) {
1209 actualThisRef = regTypeReferenceToClass(actualArgType, uninitMap);
1210 if (!dvmInstanceof(actualThisRef, resMethod->clazz)) {
1211 LOG_VFY("VFY: 'this' arg '%s' not instance of '%s'\n",
1212 actualThisRef->descriptor,
1213 resMethod->clazz->descriptor);
1214 goto fail;
1215 }
1216 }
1217 actualArgs++;
1218 }
1219
1220 /*
1221 * Process the target method's signature. This signature may or may not
1222 * have been verified, so we can't assume it's properly formed.
1223 */
1224 while (*sig != '\0' && *sig != ')') {
1225 if (actualArgs >= expectedArgs) {
1226 LOG_VFY("VFY: expected %d args, found more (%c)\n",
1227 expectedArgs, *sig);
1228 goto bad_sig;
1229 }
1230
1231 u4 getReg;
1232 if (isRange)
1233 getReg = pDecInsn->vC + actualArgs;
1234 else
1235 getReg = pDecInsn->arg[actualArgs];
1236
1237 switch (*sig) {
1238 case 'L':
1239 {
Andy McFadden62a75162009-04-17 17:23:37 -07001240 ClassObject* clazz = lookupSignatureClass(meth, &sig, pFailure);
1241 if (!VERIFY_OK(*pFailure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001242 goto bad_sig;
Andy McFaddend3250112010-11-03 14:32:42 -07001243 verifyRegisterType(insnRegs, getReg,
Andy McFadden62a75162009-04-17 17:23:37 -07001244 regTypeFromClass(clazz), pFailure);
1245 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001246 LOG_VFY("VFY: bad arg %d (into %s)\n",
1247 actualArgs, clazz->descriptor);
1248 goto bad_sig;
1249 }
1250 }
1251 actualArgs++;
1252 break;
1253 case '[':
1254 {
1255 ClassObject* clazz =
Andy McFadden62a75162009-04-17 17:23:37 -07001256 lookupSignatureArrayClass(meth, &sig, pFailure);
1257 if (!VERIFY_OK(*pFailure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001258 goto bad_sig;
Andy McFaddend3250112010-11-03 14:32:42 -07001259 verifyRegisterType(insnRegs, getReg,
Andy McFadden62a75162009-04-17 17:23:37 -07001260 regTypeFromClass(clazz), pFailure);
1261 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001262 LOG_VFY("VFY: bad arg %d (into %s)\n",
1263 actualArgs, clazz->descriptor);
1264 goto bad_sig;
1265 }
1266 }
1267 actualArgs++;
1268 break;
1269 case 'Z':
Andy McFaddend3250112010-11-03 14:32:42 -07001270 verifyRegisterType(insnRegs, getReg, kRegTypeBoolean, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001271 actualArgs++;
1272 break;
1273 case 'C':
Andy McFaddend3250112010-11-03 14:32:42 -07001274 verifyRegisterType(insnRegs, getReg, kRegTypeChar, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001275 actualArgs++;
1276 break;
1277 case 'B':
Andy McFaddend3250112010-11-03 14:32:42 -07001278 verifyRegisterType(insnRegs, getReg, kRegTypeByte, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001279 actualArgs++;
1280 break;
1281 case 'I':
Andy McFaddend3250112010-11-03 14:32:42 -07001282 verifyRegisterType(insnRegs, getReg, kRegTypeInteger, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001283 actualArgs++;
1284 break;
1285 case 'S':
Andy McFaddend3250112010-11-03 14:32:42 -07001286 verifyRegisterType(insnRegs, getReg, kRegTypeShort, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001287 actualArgs++;
1288 break;
1289 case 'F':
Andy McFaddend3250112010-11-03 14:32:42 -07001290 verifyRegisterType(insnRegs, getReg, kRegTypeFloat, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001291 actualArgs++;
1292 break;
1293 case 'D':
Andy McFaddend3250112010-11-03 14:32:42 -07001294 verifyRegisterType(insnRegs, getReg, kRegTypeDoubleLo, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001295 actualArgs += 2;
1296 break;
1297 case 'J':
Andy McFaddend3250112010-11-03 14:32:42 -07001298 verifyRegisterType(insnRegs, getReg, kRegTypeLongLo, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001299 actualArgs += 2;
1300 break;
1301 default:
1302 LOG_VFY("VFY: invocation target: bad signature type char '%c'\n",
1303 *sig);
1304 goto bad_sig;
1305 }
1306
1307 sig++;
1308 }
1309 if (*sig != ')') {
1310 char* desc = dexProtoCopyMethodDescriptor(&resMethod->prototype);
1311 LOG_VFY("VFY: invocation target: bad signature '%s'\n", desc);
1312 free(desc);
1313 goto bad_sig;
1314 }
1315
1316 if (actualArgs != expectedArgs) {
1317 LOG_VFY("VFY: expected %d args, found %d\n", expectedArgs, actualArgs);
1318 goto bad_sig;
1319 }
1320
1321 free(sigOriginal);
1322 return resMethod;
1323
1324bad_sig:
1325 if (resMethod != NULL) {
1326 char* desc = dexProtoCopyMethodDescriptor(&resMethod->prototype);
1327 LOG_VFY("VFY: rejecting call to %s.%s %s\n",
Andy McFadden62a75162009-04-17 17:23:37 -07001328 resMethod->clazz->descriptor, resMethod->name, desc);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001329 free(desc);
1330 }
1331
1332fail:
1333 free(sigOriginal);
Andy McFadden62a75162009-04-17 17:23:37 -07001334 if (*pFailure == VERIFY_ERROR_NONE)
1335 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001336 return NULL;
1337}
1338
1339/*
1340 * Get the class object for the type of data stored in a field. This isn't
1341 * stored in the Field struct, so we have to recover it from the signature.
1342 *
1343 * This only works for reference types. Don't call this for primitive types.
1344 *
1345 * If we can't find the class, we return java.lang.Object, so that
1346 * verification can continue if a field is only accessed in trivial ways.
1347 */
1348static ClassObject* getFieldClass(const Method* meth, const Field* field)
1349{
1350 ClassObject* fieldClass;
1351 const char* signature = field->signature;
1352
1353 if ((*signature == 'L') || (*signature == '[')) {
1354 fieldClass = dvmFindClassNoInit(signature,
1355 meth->clazz->classLoader);
1356 } else {
1357 return NULL;
1358 }
1359
1360 if (fieldClass == NULL) {
1361 dvmClearOptException(dvmThreadSelf());
1362 LOGV("VFY: unable to find class '%s' for field %s.%s, trying Object\n",
1363 field->signature, meth->clazz->descriptor, field->name);
1364 fieldClass = gDvm.classJavaLangObject;
1365 } else {
1366 assert(!dvmIsPrimitiveClass(fieldClass));
1367 }
1368 return fieldClass;
1369}
1370
1371
1372/*
1373 * ===========================================================================
1374 * Register operations
1375 * ===========================================================================
1376 */
1377
1378/*
Andy McFaddend3250112010-11-03 14:32:42 -07001379 * Get the type of register N.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001380 *
Andy McFaddend3250112010-11-03 14:32:42 -07001381 * The register index was validated during the static pass, so we don't
1382 * need to check it here.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001383 */
Andy McFaddend3250112010-11-03 14:32:42 -07001384static inline RegType getRegisterType(const RegType* insnRegs, u4 vsrc)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001385{
Andy McFaddend3250112010-11-03 14:32:42 -07001386 return insnRegs[vsrc];
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001387}
1388
1389/*
1390 * Get the value from a register, and cast it to a ClassObject. Sets
Andy McFadden62a75162009-04-17 17:23:37 -07001391 * "*pFailure" if something fails.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001392 *
1393 * This fails if the register holds an uninitialized class.
1394 *
1395 * If the register holds kRegTypeZero, this returns a NULL pointer.
1396 */
1397static ClassObject* getClassFromRegister(const RegType* insnRegs,
Andy McFaddend3250112010-11-03 14:32:42 -07001398 u4 vsrc, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001399{
1400 ClassObject* clazz = NULL;
1401 RegType type;
1402
1403 /* get the element type of the array held in vsrc */
Andy McFaddend3250112010-11-03 14:32:42 -07001404 type = getRegisterType(insnRegs, vsrc);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001405
1406 /* if "always zero", we allow it to fail at runtime */
1407 if (type == kRegTypeZero)
1408 goto bail;
1409
1410 if (!regTypeIsReference(type)) {
1411 LOG_VFY("VFY: tried to get class from non-ref register v%d (type=%d)\n",
1412 vsrc, type);
Andy McFadden62a75162009-04-17 17:23:37 -07001413 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001414 goto bail;
1415 }
1416 if (regTypeIsUninitReference(type)) {
1417 LOG_VFY("VFY: register %u holds uninitialized reference\n", vsrc);
Andy McFadden62a75162009-04-17 17:23:37 -07001418 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001419 goto bail;
1420 }
1421
1422 clazz = regTypeInitializedReferenceToClass(type);
1423
1424bail:
1425 return clazz;
1426}
1427
1428/*
1429 * Get the "this" pointer from a non-static method invocation. This
1430 * returns the RegType so the caller can decide whether it needs the
1431 * reference to be initialized or not. (Can also return kRegTypeZero
1432 * if the reference can only be zero at this point.)
1433 *
1434 * The argument count is in vA, and the first argument is in vC, for both
1435 * "simple" and "range" versions. We just need to make sure vA is >= 1
1436 * and then return vC.
1437 */
1438static RegType getInvocationThis(const RegType* insnRegs,
Andy McFaddend3250112010-11-03 14:32:42 -07001439 const DecodedInstruction* pDecInsn, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001440{
1441 RegType thisType = kRegTypeUnknown;
1442
1443 if (pDecInsn->vA < 1) {
1444 LOG_VFY("VFY: invoke lacks 'this'\n");
Andy McFadden62a75162009-04-17 17:23:37 -07001445 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001446 goto bail;
1447 }
1448
1449 /* get the element type of the array held in vsrc */
Andy McFaddend3250112010-11-03 14:32:42 -07001450 thisType = getRegisterType(insnRegs, pDecInsn->vC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001451 if (!regTypeIsReference(thisType)) {
1452 LOG_VFY("VFY: tried to get class from non-ref register v%d (type=%d)\n",
1453 pDecInsn->vC, thisType);
Andy McFadden62a75162009-04-17 17:23:37 -07001454 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001455 goto bail;
1456 }
1457
1458bail:
1459 return thisType;
1460}
1461
1462/*
1463 * Set the type of register N, verifying that the register is valid. If
1464 * "newType" is the "Lo" part of a 64-bit value, register N+1 will be
1465 * set to "newType+1".
1466 *
Andy McFaddend3250112010-11-03 14:32:42 -07001467 * The register index was validated during the static pass, so we don't
1468 * need to check it here.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001469 */
Andy McFaddend3250112010-11-03 14:32:42 -07001470static void setRegisterType(RegType* insnRegs, u4 vdst, RegType newType)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001471{
1472 //LOGD("set-reg v%u = %d\n", vdst, newType);
1473 switch (newType) {
1474 case kRegTypeUnknown:
1475 case kRegTypeBoolean:
1476 case kRegTypeOne:
1477 case kRegTypeByte:
1478 case kRegTypePosByte:
1479 case kRegTypeShort:
1480 case kRegTypePosShort:
1481 case kRegTypeChar:
1482 case kRegTypeInteger:
1483 case kRegTypeFloat:
1484 case kRegTypeZero:
Andy McFaddend3250112010-11-03 14:32:42 -07001485 case kRegTypeUninit:
1486 insnRegs[vdst] = newType;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001487 break;
1488 case kRegTypeLongLo:
1489 case kRegTypeDoubleLo:
Andy McFaddend3250112010-11-03 14:32:42 -07001490 insnRegs[vdst] = newType;
1491 insnRegs[vdst+1] = newType+1;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001492 break;
1493 case kRegTypeLongHi:
1494 case kRegTypeDoubleHi:
1495 /* should never set these explicitly */
Andy McFaddend3250112010-11-03 14:32:42 -07001496 LOGE("BUG: explicit set of high register type\n");
1497 dvmAbort();
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001498 break;
1499
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001500 default:
Andy McFaddend3250112010-11-03 14:32:42 -07001501 /* can't switch for ref types, so we check explicitly */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001502 if (regTypeIsReference(newType)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001503 insnRegs[vdst] = newType;
1504
1505 /*
1506 * In most circumstances we won't see a reference to a primitive
1507 * class here (e.g. "D"), since that would mean the object in the
1508 * register is actually a primitive type. It can happen as the
1509 * result of an assumed-successful check-cast instruction in
1510 * which the second argument refers to a primitive class. (In
1511 * practice, such an instruction will always throw an exception.)
1512 *
1513 * This is not an issue for instructions like const-class, where
1514 * the object in the register is a java.lang.Class instance.
1515 */
1516 break;
1517 }
Andy McFaddend3250112010-11-03 14:32:42 -07001518 /* bad type - fall through */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001519
1520 case kRegTypeConflict: // should only be set during a merge
Andy McFaddend3250112010-11-03 14:32:42 -07001521 LOGE("BUG: set register to unknown type %d\n", newType);
1522 dvmAbort();
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001523 break;
1524 }
1525}
1526
1527/*
1528 * Verify that the contents of the specified register have the specified
1529 * type (or can be converted to it through an implicit widening conversion).
1530 *
1531 * In theory we could use this to modify the type of the source register,
1532 * e.g. a generic 32-bit constant, once used as a float, would thereafter
1533 * remain a float. There is no compelling reason to require this though.
1534 *
1535 * If "vsrc" is a reference, both it and the "vsrc" register must be
1536 * initialized ("vsrc" may be Zero). This will verify that the value in
1537 * the register is an instance of checkType, or if checkType is an
1538 * interface, verify that the register implements checkType.
1539 */
Andy McFaddend3250112010-11-03 14:32:42 -07001540static void verifyRegisterType(const RegType* insnRegs, u4 vsrc,
1541 RegType checkType, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001542{
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001543 RegType srcType = insnRegs[vsrc];
1544
1545 //LOGD("check-reg v%u = %d\n", vsrc, checkType);
1546 switch (checkType) {
1547 case kRegTypeFloat:
1548 case kRegTypeBoolean:
1549 case kRegTypePosByte:
1550 case kRegTypeByte:
1551 case kRegTypePosShort:
1552 case kRegTypeShort:
1553 case kRegTypeChar:
1554 case kRegTypeInteger:
1555 if (!canConvertTo1nr(srcType, checkType)) {
1556 LOG_VFY("VFY: register1 v%u type %d, wanted %d\n",
1557 vsrc, srcType, checkType);
Andy McFadden62a75162009-04-17 17:23:37 -07001558 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001559 }
1560 break;
1561 case kRegTypeLongLo:
1562 case kRegTypeDoubleLo:
Andy McFaddend3250112010-11-03 14:32:42 -07001563 if (insnRegs[vsrc+1] != srcType+1) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001564 LOG_VFY("VFY: register2 v%u-%u values %d,%d\n",
1565 vsrc, vsrc+1, insnRegs[vsrc], insnRegs[vsrc+1]);
Andy McFadden62a75162009-04-17 17:23:37 -07001566 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001567 } else if (!canConvertTo2(srcType, checkType)) {
1568 LOG_VFY("VFY: register2 v%u type %d, wanted %d\n",
1569 vsrc, srcType, checkType);
Andy McFadden62a75162009-04-17 17:23:37 -07001570 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001571 }
1572 break;
1573
1574 case kRegTypeLongHi:
1575 case kRegTypeDoubleHi:
1576 case kRegTypeZero:
1577 case kRegTypeOne:
1578 case kRegTypeUnknown:
1579 case kRegTypeConflict:
1580 /* should never be checking for these explicitly */
1581 assert(false);
Andy McFadden62a75162009-04-17 17:23:37 -07001582 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001583 return;
1584 case kRegTypeUninit:
1585 default:
1586 /* make sure checkType is initialized reference */
1587 if (!regTypeIsReference(checkType)) {
1588 LOG_VFY("VFY: unexpected check type %d\n", checkType);
1589 assert(false);
Andy McFadden62a75162009-04-17 17:23:37 -07001590 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001591 break;
1592 }
1593 if (regTypeIsUninitReference(checkType)) {
1594 LOG_VFY("VFY: uninitialized ref not expected as reg check\n");
Andy McFadden62a75162009-04-17 17:23:37 -07001595 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001596 break;
1597 }
1598 /* make sure srcType is initialized reference or always-NULL */
1599 if (!regTypeIsReference(srcType)) {
1600 LOG_VFY("VFY: register1 v%u type %d, wanted ref\n", vsrc, srcType);
Andy McFadden62a75162009-04-17 17:23:37 -07001601 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001602 break;
1603 }
1604 if (regTypeIsUninitReference(srcType)) {
1605 LOG_VFY("VFY: register1 v%u holds uninitialized ref\n", vsrc);
Andy McFadden62a75162009-04-17 17:23:37 -07001606 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001607 break;
1608 }
1609 /* if the register isn't Zero, make sure it's an instance of check */
1610 if (srcType != kRegTypeZero) {
1611 ClassObject* srcClass = regTypeInitializedReferenceToClass(srcType);
1612 ClassObject* checkClass = regTypeInitializedReferenceToClass(checkType);
1613 assert(srcClass != NULL);
1614 assert(checkClass != NULL);
1615
1616 if (dvmIsInterfaceClass(checkClass)) {
1617 /*
1618 * All objects implement all interfaces as far as the
1619 * verifier is concerned. The runtime has to sort it out.
1620 * See comments above findCommonSuperclass.
1621 */
1622 /*
1623 if (srcClass != checkClass &&
1624 !dvmImplements(srcClass, checkClass))
1625 {
1626 LOG_VFY("VFY: %s does not implement %s\n",
1627 srcClass->descriptor, checkClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07001628 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001629 }
1630 */
1631 } else {
1632 if (!dvmInstanceof(srcClass, checkClass)) {
1633 LOG_VFY("VFY: %s is not instance of %s\n",
1634 srcClass->descriptor, checkClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07001635 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001636 }
1637 }
1638 }
1639 break;
1640 }
1641}
1642
1643/*
Andy McFaddend3250112010-11-03 14:32:42 -07001644 * Set the type of the "result" register.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001645 */
1646static void setResultRegisterType(RegType* insnRegs, const int insnRegCount,
Andy McFaddend3250112010-11-03 14:32:42 -07001647 RegType newType)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001648{
Andy McFaddend3250112010-11-03 14:32:42 -07001649 setRegisterType(insnRegs, RESULT_REGISTER(insnRegCount), newType);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001650}
1651
1652
1653/*
1654 * Update all registers holding "uninitType" to instead hold the
1655 * corresponding initialized reference type. This is called when an
1656 * appropriate <init> method is invoked -- all copies of the reference
1657 * must be marked as initialized.
1658 */
1659static void markRefsAsInitialized(RegType* insnRegs, int insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07001660 UninitInstanceMap* uninitMap, RegType uninitType, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001661{
1662 ClassObject* clazz;
1663 RegType initType;
1664 int i, changed;
1665
1666 clazz = dvmGetUninitInstance(uninitMap, regTypeToUninitIndex(uninitType));
1667 if (clazz == NULL) {
1668 LOGE("VFY: unable to find type=0x%x (idx=%d)\n",
1669 uninitType, regTypeToUninitIndex(uninitType));
Andy McFadden62a75162009-04-17 17:23:37 -07001670 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001671 return;
1672 }
1673 initType = regTypeFromClass(clazz);
1674
1675 changed = 0;
1676 for (i = 0; i < insnRegCount; i++) {
1677 if (insnRegs[i] == uninitType) {
1678 insnRegs[i] = initType;
1679 changed++;
1680 }
1681 }
1682 //LOGD("VFY: marked %d registers as initialized\n", changed);
1683 assert(changed > 0);
1684
1685 return;
1686}
1687
1688/*
1689 * We're creating a new instance of class C at address A. Any registers
1690 * holding instances previously created at address A must be initialized
1691 * by now. If not, we mark them as "conflict" to prevent them from being
1692 * used (otherwise, markRefsAsInitialized would mark the old ones and the
1693 * new ones at the same time).
1694 */
1695static void markUninitRefsAsInvalid(RegType* insnRegs, int insnRegCount,
1696 UninitInstanceMap* uninitMap, RegType uninitType)
1697{
1698 int i, changed;
1699
1700 changed = 0;
1701 for (i = 0; i < insnRegCount; i++) {
1702 if (insnRegs[i] == uninitType) {
1703 insnRegs[i] = kRegTypeConflict;
1704 changed++;
1705 }
1706 }
1707
1708 //if (changed)
1709 // LOGD("VFY: marked %d uninitialized registers as invalid\n", changed);
1710}
1711
1712/*
1713 * Find the start of the register set for the specified instruction in
1714 * the current method.
1715 */
1716static inline RegType* getRegisterLine(const RegisterTable* regTable,
1717 int insnIdx)
1718{
1719 return regTable->addrRegs[insnIdx];
1720}
1721
1722/*
1723 * Copy a bunch of registers.
1724 */
1725static inline void copyRegisters(RegType* dst, const RegType* src,
1726 int numRegs)
1727{
1728 memcpy(dst, src, numRegs * sizeof(RegType));
1729}
1730
1731/*
1732 * Compare a bunch of registers.
1733 *
1734 * Returns 0 if they match. Using this for a sort is unwise, since the
1735 * value can change based on machine endianness.
1736 */
1737static inline int compareRegisters(const RegType* src1, const RegType* src2,
1738 int numRegs)
1739{
1740 return memcmp(src1, src2, numRegs * sizeof(RegType));
1741}
1742
1743/*
1744 * Register type categories, for type checking.
1745 *
1746 * The spec says category 1 includes boolean, byte, char, short, int, float,
1747 * reference, and returnAddress. Category 2 includes long and double.
1748 *
1749 * We treat object references separately, so we have "category1nr". We
1750 * don't support jsr/ret, so there is no "returnAddress" type.
1751 */
1752typedef enum TypeCategory {
1753 kTypeCategoryUnknown = 0,
1754 kTypeCategory1nr, // byte, char, int, float, boolean
1755 kTypeCategory2, // long, double
1756 kTypeCategoryRef, // object reference
1757} TypeCategory;
1758
1759/*
1760 * See if "type" matches "cat". All we're really looking for here is that
1761 * we're not mixing and matching 32-bit and 64-bit quantities, and we're
1762 * not mixing references with numerics. (For example, the arguments to
1763 * "a < b" could be integers of different sizes, but they must both be
1764 * integers. Dalvik is less specific about int vs. float, so we treat them
1765 * as equivalent here.)
1766 *
1767 * For category 2 values, "type" must be the "low" half of the value.
1768 *
Andy McFadden62a75162009-04-17 17:23:37 -07001769 * Sets "*pFailure" if something looks wrong.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001770 */
Andy McFadden62a75162009-04-17 17:23:37 -07001771static void checkTypeCategory(RegType type, TypeCategory cat,
1772 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001773{
1774 switch (cat) {
1775 case kTypeCategory1nr:
1776 switch (type) {
1777 case kRegTypeFloat:
1778 case kRegTypeZero:
1779 case kRegTypeOne:
1780 case kRegTypeBoolean:
1781 case kRegTypePosByte:
1782 case kRegTypeByte:
1783 case kRegTypePosShort:
1784 case kRegTypeShort:
1785 case kRegTypeChar:
1786 case kRegTypeInteger:
1787 break;
1788 default:
Andy McFadden62a75162009-04-17 17:23:37 -07001789 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001790 break;
1791 }
1792 break;
1793
1794 case kTypeCategory2:
1795 switch (type) {
1796 case kRegTypeLongLo:
1797 case kRegTypeDoubleLo:
1798 break;
1799 default:
Andy McFadden62a75162009-04-17 17:23:37 -07001800 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001801 break;
1802 }
1803 break;
1804
1805 case kTypeCategoryRef:
1806 if (type != kRegTypeZero && !regTypeIsReference(type))
Andy McFadden62a75162009-04-17 17:23:37 -07001807 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001808 break;
1809
1810 default:
1811 assert(false);
Andy McFadden62a75162009-04-17 17:23:37 -07001812 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001813 break;
1814 }
1815}
1816
1817/*
1818 * For a category 2 register pair, verify that "typeh" is the appropriate
1819 * high part for "typel".
1820 *
1821 * Does not verify that "typel" is in fact the low part of a 64-bit
1822 * register pair.
1823 */
Andy McFadden62a75162009-04-17 17:23:37 -07001824static void checkWidePair(RegType typel, RegType typeh, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001825{
1826 if ((typeh != typel+1))
Andy McFadden62a75162009-04-17 17:23:37 -07001827 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001828}
1829
1830/*
1831 * Implement category-1 "move" instructions. Copy a 32-bit value from
1832 * "vsrc" to "vdst".
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001833 */
Andy McFaddend3250112010-11-03 14:32:42 -07001834static void copyRegister1(RegType* insnRegs, u4 vdst, u4 vsrc,
1835 TypeCategory cat, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001836{
Andy McFaddend3250112010-11-03 14:32:42 -07001837 RegType type = getRegisterType(insnRegs, vsrc);
1838 checkTypeCategory(type, cat, pFailure);
Andy McFadden62a75162009-04-17 17:23:37 -07001839 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001840 LOG_VFY("VFY: copy1 v%u<-v%u type=%d cat=%d\n", vdst, vsrc, type, cat);
Andy McFaddend3250112010-11-03 14:32:42 -07001841 } else {
1842 setRegisterType(insnRegs, vdst, type);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001843 }
Andy McFaddend3250112010-11-03 14:32:42 -07001844
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001845}
1846
1847/*
1848 * Implement category-2 "move" instructions. Copy a 64-bit value from
1849 * "vsrc" to "vdst". This copies both halves of the register.
1850 */
Andy McFaddend3250112010-11-03 14:32:42 -07001851static void copyRegister2(RegType* insnRegs, u4 vdst,
Andy McFadden62a75162009-04-17 17:23:37 -07001852 u4 vsrc, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001853{
Andy McFaddend3250112010-11-03 14:32:42 -07001854 RegType typel = getRegisterType(insnRegs, vsrc);
1855 RegType typeh = getRegisterType(insnRegs, vsrc+1);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001856
Andy McFaddend3250112010-11-03 14:32:42 -07001857 checkTypeCategory(typel, kTypeCategory2, pFailure);
1858 checkWidePair(typel, typeh, pFailure);
Andy McFadden62a75162009-04-17 17:23:37 -07001859 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001860 LOG_VFY("VFY: copy2 v%u<-v%u type=%d/%d\n", vdst, vsrc, typel, typeh);
Andy McFaddend3250112010-11-03 14:32:42 -07001861 } else {
1862 setRegisterType(insnRegs, vdst, typel);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001863 }
1864}
1865
1866/*
1867 * Implement "move-result". Copy the category-1 value from the result
1868 * register to another register, and reset the result register.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001869 */
1870static void copyResultRegister1(RegType* insnRegs, const int insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07001871 u4 vdst, TypeCategory cat, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001872{
1873 RegType type;
1874 u4 vsrc;
1875
Andy McFaddend3250112010-11-03 14:32:42 -07001876 assert(vdst < (u4) insnRegCount);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001877
Andy McFaddend3250112010-11-03 14:32:42 -07001878 vsrc = RESULT_REGISTER(insnRegCount);
1879 type = getRegisterType(insnRegs, vsrc);
1880 checkTypeCategory(type, cat, pFailure);
Andy McFadden62a75162009-04-17 17:23:37 -07001881 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001882 LOG_VFY("VFY: copyRes1 v%u<-v%u cat=%d type=%d\n",
1883 vdst, vsrc, cat, type);
Andy McFaddend3250112010-11-03 14:32:42 -07001884 } else {
1885 setRegisterType(insnRegs, vdst, type);
1886 insnRegs[vsrc] = kRegTypeUnknown;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001887 }
1888}
1889
1890/*
1891 * Implement "move-result-wide". Copy the category-2 value from the result
1892 * register to another register, and reset the result register.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001893 */
1894static void copyResultRegister2(RegType* insnRegs, const int insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07001895 u4 vdst, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001896{
1897 RegType typel, typeh;
1898 u4 vsrc;
1899
Andy McFaddend3250112010-11-03 14:32:42 -07001900 assert(vdst < (u4) insnRegCount);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001901
Andy McFaddend3250112010-11-03 14:32:42 -07001902 vsrc = RESULT_REGISTER(insnRegCount);
1903 typel = getRegisterType(insnRegs, vsrc);
1904 typeh = getRegisterType(insnRegs, vsrc+1);
1905 checkTypeCategory(typel, kTypeCategory2, pFailure);
1906 checkWidePair(typel, typeh, pFailure);
Andy McFadden62a75162009-04-17 17:23:37 -07001907 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001908 LOG_VFY("VFY: copyRes2 v%u<-v%u type=%d/%d\n",
1909 vdst, vsrc, typel, typeh);
Andy McFaddend3250112010-11-03 14:32:42 -07001910 } else {
1911 setRegisterType(insnRegs, vdst, typel);
1912 insnRegs[vsrc] = kRegTypeUnknown;
1913 insnRegs[vsrc+1] = kRegTypeUnknown;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001914 }
1915}
1916
1917/*
1918 * Verify types for a simple two-register instruction (e.g. "neg-int").
1919 * "dstType" is stored into vA, and "srcType" is verified against vB.
1920 */
Andy McFaddend3250112010-11-03 14:32:42 -07001921static void checkUnop(RegType* insnRegs, DecodedInstruction* pDecInsn,
1922 RegType dstType, RegType srcType, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001923{
Andy McFaddend3250112010-11-03 14:32:42 -07001924 verifyRegisterType(insnRegs, pDecInsn->vB, srcType, pFailure);
1925 setRegisterType(insnRegs, pDecInsn->vA, dstType);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001926}
1927
1928/*
1929 * We're performing an operation like "and-int/2addr" that can be
1930 * performed on booleans as well as integers. We get no indication of
1931 * boolean-ness, but we can infer it from the types of the arguments.
1932 *
1933 * Assumes we've already validated reg1/reg2.
1934 *
Andy McFaddenb5f64bc2009-06-10 14:11:07 -07001935 * TODO: consider generalizing this. The key principle is that the
1936 * result of a bitwise operation can only be as wide as the widest of
1937 * the operands. You can safely AND/OR/XOR two chars together and know
1938 * you still have a char, so it's reasonable for the compiler or "dx"
1939 * to skip the int-to-char instruction. (We need to do this for boolean
1940 * because there is no int-to-boolean operation.)
1941 *
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001942 * Returns true if both args are Boolean, Zero, or One.
1943 */
Andy McFaddend3250112010-11-03 14:32:42 -07001944static bool upcastBooleanOp(RegType* insnRegs, u4 reg1, u4 reg2)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001945{
1946 RegType type1, type2;
1947
1948 type1 = insnRegs[reg1];
1949 type2 = insnRegs[reg2];
1950
1951 if ((type1 == kRegTypeBoolean || type1 == kRegTypeZero ||
1952 type1 == kRegTypeOne) &&
1953 (type2 == kRegTypeBoolean || type2 == kRegTypeZero ||
1954 type2 == kRegTypeOne))
1955 {
1956 return true;
1957 }
1958 return false;
1959}
1960
1961/*
1962 * Verify types for A two-register instruction with a literal constant
1963 * (e.g. "add-int/lit8"). "dstType" is stored into vA, and "srcType" is
1964 * verified against vB.
1965 *
1966 * If "checkBooleanOp" is set, we use the constant value in vC.
1967 */
Andy McFaddend3250112010-11-03 14:32:42 -07001968static void checkLitop(RegType* insnRegs, DecodedInstruction* pDecInsn,
1969 RegType dstType, RegType srcType, bool checkBooleanOp,
1970 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001971{
Andy McFaddend3250112010-11-03 14:32:42 -07001972 verifyRegisterType(insnRegs, pDecInsn->vB, srcType, pFailure);
Andy McFadden62a75162009-04-17 17:23:37 -07001973 if (VERIFY_OK(*pFailure) && checkBooleanOp) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001974 assert(dstType == kRegTypeInteger);
1975 /* check vB with the call, then check the constant manually */
Andy McFaddend3250112010-11-03 14:32:42 -07001976 if (upcastBooleanOp(insnRegs, pDecInsn->vB, pDecInsn->vB)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001977 && (pDecInsn->vC == 0 || pDecInsn->vC == 1))
1978 {
1979 dstType = kRegTypeBoolean;
1980 }
1981 }
Andy McFaddend3250112010-11-03 14:32:42 -07001982 setRegisterType(insnRegs, pDecInsn->vA, dstType);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001983}
1984
1985/*
1986 * Verify types for a simple three-register instruction (e.g. "add-int").
1987 * "dstType" is stored into vA, and "srcType1"/"srcType2" are verified
1988 * against vB/vC.
1989 */
Andy McFaddend3250112010-11-03 14:32:42 -07001990static void checkBinop(RegType* insnRegs, DecodedInstruction* pDecInsn,
1991 RegType dstType, RegType srcType1, RegType srcType2, bool checkBooleanOp,
1992 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001993{
Andy McFaddend3250112010-11-03 14:32:42 -07001994 verifyRegisterType(insnRegs, pDecInsn->vB, srcType1, pFailure);
1995 verifyRegisterType(insnRegs, pDecInsn->vC, srcType2, pFailure);
Andy McFadden62a75162009-04-17 17:23:37 -07001996 if (VERIFY_OK(*pFailure) && checkBooleanOp) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001997 assert(dstType == kRegTypeInteger);
Andy McFaddend3250112010-11-03 14:32:42 -07001998 if (upcastBooleanOp(insnRegs, pDecInsn->vB, pDecInsn->vC))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001999 dstType = kRegTypeBoolean;
2000 }
Andy McFaddend3250112010-11-03 14:32:42 -07002001 setRegisterType(insnRegs, pDecInsn->vA, dstType);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002002}
2003
2004/*
2005 * Verify types for a binary "2addr" operation. "srcType1"/"srcType2"
2006 * are verified against vA/vB, then "dstType" is stored into vA.
2007 */
Andy McFaddend3250112010-11-03 14:32:42 -07002008static void checkBinop2addr(RegType* insnRegs, DecodedInstruction* pDecInsn,
2009 RegType dstType, RegType srcType1, RegType srcType2, bool checkBooleanOp,
2010 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002011{
Andy McFaddend3250112010-11-03 14:32:42 -07002012 verifyRegisterType(insnRegs, pDecInsn->vA, srcType1, pFailure);
2013 verifyRegisterType(insnRegs, pDecInsn->vB, srcType2, pFailure);
Andy McFadden62a75162009-04-17 17:23:37 -07002014 if (VERIFY_OK(*pFailure) && checkBooleanOp) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002015 assert(dstType == kRegTypeInteger);
Andy McFaddend3250112010-11-03 14:32:42 -07002016 if (upcastBooleanOp(insnRegs, pDecInsn->vA, pDecInsn->vB))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002017 dstType = kRegTypeBoolean;
2018 }
Andy McFaddend3250112010-11-03 14:32:42 -07002019 setRegisterType(insnRegs, pDecInsn->vA, dstType);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002020}
2021
Andy McFadden80d25ea2009-06-12 07:26:17 -07002022/*
2023 * Treat right-shifting as a narrowing conversion when possible.
2024 *
2025 * For example, right-shifting an int 24 times results in a value that can
2026 * be treated as a byte.
2027 *
2028 * Things get interesting when contemplating sign extension. Right-
2029 * shifting an integer by 16 yields a value that can be represented in a
2030 * "short" but not a "char", but an unsigned right shift by 16 yields a
2031 * value that belongs in a char rather than a short. (Consider what would
2032 * happen if the result of the shift were cast to a char or short and then
2033 * cast back to an int. If sign extension, or the lack thereof, causes
2034 * a change in the 32-bit representation, then the conversion was lossy.)
2035 *
2036 * A signed right shift by 17 on an integer results in a short. An unsigned
2037 * right shfit by 17 on an integer results in a posshort, which can be
2038 * assigned to a short or a char.
2039 *
2040 * An unsigned right shift on a short can actually expand the result into
2041 * a 32-bit integer. For example, 0xfffff123 >>> 8 becomes 0x00fffff1,
2042 * which can't be represented in anything smaller than an int.
2043 *
2044 * javac does not generate code that takes advantage of this, but some
2045 * of the code optimizers do. It's generally a peephole optimization
2046 * that replaces a particular sequence, e.g. (bipush 24, ishr, i2b) is
2047 * replaced by (bipush 24, ishr). Knowing that shifting a short 8 times
2048 * to the right yields a byte is really more than we need to handle the
2049 * code that's out there, but support is not much more complex than just
2050 * handling integer.
2051 *
2052 * Right-shifting never yields a boolean value.
2053 *
2054 * Returns the new register type.
2055 */
Andy McFaddend3250112010-11-03 14:32:42 -07002056static RegType adjustForRightShift(RegType* workRegs, int reg,
2057 unsigned int shiftCount, bool isUnsignedShift, VerifyError* pFailure)
Andy McFadden80d25ea2009-06-12 07:26:17 -07002058{
Andy McFaddend3250112010-11-03 14:32:42 -07002059 RegType srcType = getRegisterType(workRegs, reg);
Andy McFadden80d25ea2009-06-12 07:26:17 -07002060 RegType newType;
2061
2062 /* no-op */
2063 if (shiftCount == 0)
2064 return srcType;
2065
2066 /* safe defaults */
2067 if (isUnsignedShift)
2068 newType = kRegTypeInteger;
2069 else
2070 newType = srcType;
2071
2072 if (shiftCount >= 32) {
2073 LOG_VFY("Got unexpectedly large shift count %u\n", shiftCount);
2074 /* fail? */
2075 return newType;
2076 }
2077
2078 switch (srcType) {
2079 case kRegTypeInteger: /* 32-bit signed value */
2080 case kRegTypeFloat: /* (allowed; treat same as int) */
2081 if (isUnsignedShift) {
2082 if (shiftCount > 24)
2083 newType = kRegTypePosByte;
2084 else if (shiftCount >= 16)
2085 newType = kRegTypeChar;
2086 } else {
2087 if (shiftCount >= 24)
2088 newType = kRegTypeByte;
2089 else if (shiftCount >= 16)
2090 newType = kRegTypeShort;
2091 }
2092 break;
2093 case kRegTypeShort: /* 16-bit signed value */
2094 if (isUnsignedShift) {
2095 /* default (kRegTypeInteger) is correct */
2096 } else {
2097 if (shiftCount >= 8)
2098 newType = kRegTypeByte;
2099 }
2100 break;
2101 case kRegTypePosShort: /* 15-bit unsigned value */
2102 if (shiftCount >= 8)
2103 newType = kRegTypePosByte;
2104 break;
2105 case kRegTypeChar: /* 16-bit unsigned value */
2106 if (shiftCount > 8)
2107 newType = kRegTypePosByte;
2108 break;
2109 case kRegTypeByte: /* 8-bit signed value */
2110 /* defaults (u=kRegTypeInteger / s=srcType) are correct */
2111 break;
2112 case kRegTypePosByte: /* 7-bit unsigned value */
2113 /* always use newType=srcType */
2114 newType = srcType;
2115 break;
2116 case kRegTypeZero: /* 1-bit unsigned value */
2117 case kRegTypeOne:
2118 case kRegTypeBoolean:
2119 /* unnecessary? */
2120 newType = kRegTypeZero;
2121 break;
2122 default:
2123 /* long, double, references; shouldn't be here! */
2124 assert(false);
2125 break;
2126 }
2127
2128 if (newType != srcType) {
2129 LOGVV("narrowing: %d(%d) --> %d to %d\n",
2130 shiftCount, isUnsignedShift, srcType, newType);
2131 } else {
2132 LOGVV("not narrowed: %d(%d) --> %d\n",
2133 shiftCount, isUnsignedShift, srcType);
2134 }
2135 return newType;
2136}
2137
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002138
2139/*
2140 * ===========================================================================
2141 * Register merge
2142 * ===========================================================================
2143 */
2144
2145/*
2146 * Compute the "class depth" of a class. This is the distance from the
2147 * class to the top of the tree, chasing superclass links. java.lang.Object
2148 * has a class depth of 0.
2149 */
2150static int getClassDepth(ClassObject* clazz)
2151{
2152 int depth = 0;
2153
2154 while (clazz->super != NULL) {
2155 clazz = clazz->super;
2156 depth++;
2157 }
2158 return depth;
2159}
2160
2161/*
2162 * Given two classes, walk up the superclass tree to find a common
2163 * ancestor. (Called from findCommonSuperclass().)
2164 *
2165 * TODO: consider caching the class depth in the class object so we don't
2166 * have to search for it here.
2167 */
2168static ClassObject* digForSuperclass(ClassObject* c1, ClassObject* c2)
2169{
2170 int depth1, depth2;
2171
2172 depth1 = getClassDepth(c1);
2173 depth2 = getClassDepth(c2);
2174
2175 if (gDebugVerbose) {
2176 LOGVV("COMMON: %s(%d) + %s(%d)\n",
2177 c1->descriptor, depth1, c2->descriptor, depth2);
2178 }
2179
2180 /* pull the deepest one up */
2181 if (depth1 > depth2) {
2182 while (depth1 > depth2) {
2183 c1 = c1->super;
2184 depth1--;
2185 }
2186 } else {
2187 while (depth2 > depth1) {
2188 c2 = c2->super;
2189 depth2--;
2190 }
2191 }
2192
2193 /* walk up in lock-step */
2194 while (c1 != c2) {
2195 c1 = c1->super;
2196 c2 = c2->super;
2197
2198 assert(c1 != NULL && c2 != NULL);
2199 }
2200
2201 if (gDebugVerbose) {
2202 LOGVV(" : --> %s\n", c1->descriptor);
2203 }
2204 return c1;
2205}
2206
2207/*
2208 * Merge two array classes. We can't use the general "walk up to the
2209 * superclass" merge because the superclass of an array is always Object.
2210 * We want String[] + Integer[] = Object[]. This works for higher dimensions
2211 * as well, e.g. String[][] + Integer[][] = Object[][].
2212 *
2213 * If Foo1 and Foo2 are subclasses of Foo, Foo1[] + Foo2[] = Foo[].
2214 *
2215 * If Class implements Type, Class[] + Type[] = Type[].
2216 *
2217 * If the dimensions don't match, we want to convert to an array of Object
2218 * with the least dimension, e.g. String[][] + String[][][][] = Object[][].
2219 *
Andy McFaddenc2d74dd2010-10-25 16:13:46 -07002220 * Arrays of primitive types effectively have one less dimension when
2221 * merging. int[] + float[] = Object, int[] + String[] = Object,
2222 * int[][] + float[][] = Object[], int[][] + String[] = Object[]. (The
2223 * only time this function doesn't return an array class is when one of
2224 * the arguments is a 1-dimensional primitive array.)
2225 *
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002226 * This gets a little awkward because we may have to ask the VM to create
2227 * a new array type with the appropriate element and dimensions. However, we
2228 * shouldn't be doing this often.
2229 */
2230static ClassObject* findCommonArraySuperclass(ClassObject* c1, ClassObject* c2)
2231{
2232 ClassObject* arrayClass = NULL;
2233 ClassObject* commonElem;
Andy McFaddenc2d74dd2010-10-25 16:13:46 -07002234 int arrayDim1, arrayDim2;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002235 int i, numDims;
Andy McFaddenc2d74dd2010-10-25 16:13:46 -07002236 bool hasPrimitive = false;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002237
Andy McFaddenc2d74dd2010-10-25 16:13:46 -07002238 arrayDim1 = c1->arrayDim;
2239 arrayDim2 = c2->arrayDim;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002240 assert(c1->arrayDim > 0);
2241 assert(c2->arrayDim > 0);
2242
Andy McFaddenc2d74dd2010-10-25 16:13:46 -07002243 if (dvmIsPrimitiveClass(c1->elementClass)) {
2244 arrayDim1--;
2245 hasPrimitive = true;
2246 }
2247 if (dvmIsPrimitiveClass(c2->elementClass)) {
2248 arrayDim2--;
2249 hasPrimitive = true;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002250 }
2251
Andy McFaddenc2d74dd2010-10-25 16:13:46 -07002252 if (!hasPrimitive && arrayDim1 == arrayDim2) {
2253 /*
2254 * Two arrays of reference types with equal dimensions. Try to
2255 * find a good match.
2256 */
2257 commonElem = findCommonSuperclass(c1->elementClass, c2->elementClass);
2258 numDims = arrayDim1;
2259 } else {
2260 /*
2261 * Mismatched array depths and/or array(s) of primitives. We want
2262 * Object, or an Object array with appropriate dimensions.
2263 *
2264 * We initialize arrayClass to Object here, because it's possible
2265 * for us to set numDims=0.
2266 */
2267 if (arrayDim1 < arrayDim2)
2268 numDims = arrayDim1;
2269 else
2270 numDims = arrayDim2;
2271 arrayClass = commonElem = c1->super; // == java.lang.Object
2272 }
2273
2274 /*
2275 * Find an appropriately-dimensioned array class. This is easiest
2276 * to do iteratively, using the array class found by the current round
2277 * as the element type for the next round.
2278 */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002279 for (i = 0; i < numDims; i++) {
2280 arrayClass = dvmFindArrayClassForElement(commonElem);
2281 commonElem = arrayClass;
2282 }
Andy McFaddenc2d74dd2010-10-25 16:13:46 -07002283 assert(arrayClass != NULL);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002284
2285 LOGVV("ArrayMerge '%s' + '%s' --> '%s'\n",
2286 c1->descriptor, c2->descriptor, arrayClass->descriptor);
2287 return arrayClass;
2288}
2289
2290/*
2291 * Find the first common superclass of the two classes. We're not
2292 * interested in common interfaces.
2293 *
2294 * The easiest way to do this for concrete classes is to compute the "class
2295 * depth" of each, move up toward the root of the deepest one until they're
2296 * at the same depth, then walk both up to the root until they match.
2297 *
Andy McFaddenc2d74dd2010-10-25 16:13:46 -07002298 * If both classes are arrays, we need to merge based on array depth and
2299 * element type.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002300 *
2301 * If one class is an interface, we check to see if the other class/interface
2302 * (or one of its predecessors) implements the interface. If so, we return
2303 * the interface; otherwise, we return Object.
2304 *
2305 * NOTE: we continue the tradition of "lazy interface handling". To wit,
2306 * suppose we have three classes:
2307 * One implements Fancy, Free
2308 * Two implements Fancy, Free
2309 * Three implements Free
2310 * where Fancy and Free are unrelated interfaces. The code requires us
2311 * to merge One into Two. Ideally we'd use a common interface, which
2312 * gives us a choice between Fancy and Free, and no guidance on which to
2313 * use. If we use Free, we'll be okay when Three gets merged in, but if
2314 * we choose Fancy, we're hosed. The "ideal" solution is to create a
2315 * set of common interfaces and carry that around, merging further references
2316 * into it. This is a pain. The easy solution is to simply boil them
2317 * down to Objects and let the runtime invokeinterface call fail, which
2318 * is what we do.
2319 */
2320static ClassObject* findCommonSuperclass(ClassObject* c1, ClassObject* c2)
2321{
2322 assert(!dvmIsPrimitiveClass(c1) && !dvmIsPrimitiveClass(c2));
2323
2324 if (c1 == c2)
2325 return c1;
2326
2327 if (dvmIsInterfaceClass(c1) && dvmImplements(c2, c1)) {
2328 if (gDebugVerbose)
2329 LOGVV("COMMON/I1: %s + %s --> %s\n",
2330 c1->descriptor, c2->descriptor, c1->descriptor);
2331 return c1;
2332 }
2333 if (dvmIsInterfaceClass(c2) && dvmImplements(c1, c2)) {
2334 if (gDebugVerbose)
2335 LOGVV("COMMON/I2: %s + %s --> %s\n",
2336 c1->descriptor, c2->descriptor, c2->descriptor);
2337 return c2;
2338 }
2339
Andy McFaddenc2d74dd2010-10-25 16:13:46 -07002340 if (dvmIsArrayClass(c1) && dvmIsArrayClass(c2)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002341 return findCommonArraySuperclass(c1, c2);
2342 }
2343
2344 return digForSuperclass(c1, c2);
2345}
2346
2347/*
2348 * Merge two RegType values.
2349 *
2350 * Sets "*pChanged" to "true" if the result doesn't match "type1".
2351 */
2352static RegType mergeTypes(RegType type1, RegType type2, bool* pChanged)
2353{
2354 RegType result;
2355
2356 /*
2357 * Check for trivial case so we don't have to hit memory.
2358 */
2359 if (type1 == type2)
2360 return type1;
2361
2362 /*
2363 * Use the table if we can, and reject any attempts to merge something
2364 * from the table with a reference type.
2365 *
Andy McFadden470cbbb2010-11-04 16:31:37 -07002366 * Uninitialized references are composed of the enum ORed with an
2367 * index value. The uninitialized table entry at index zero *will*
2368 * show up as a simple kRegTypeUninit value. Since this cannot be
2369 * merged with anything but itself, the rules do the right thing.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002370 */
2371 if (type1 < kRegTypeMAX) {
2372 if (type2 < kRegTypeMAX) {
2373 result = gDvmMergeTab[type1][type2];
2374 } else {
2375 /* simple + reference == conflict, usually */
2376 if (type1 == kRegTypeZero)
2377 result = type2;
2378 else
2379 result = kRegTypeConflict;
2380 }
2381 } else {
2382 if (type2 < kRegTypeMAX) {
2383 /* reference + simple == conflict, usually */
2384 if (type2 == kRegTypeZero)
2385 result = type1;
2386 else
2387 result = kRegTypeConflict;
2388 } else {
2389 /* merging two references */
2390 if (regTypeIsUninitReference(type1) ||
2391 regTypeIsUninitReference(type2))
2392 {
2393 /* can't merge uninit with anything but self */
2394 result = kRegTypeConflict;
2395 } else {
2396 ClassObject* clazz1 = regTypeInitializedReferenceToClass(type1);
2397 ClassObject* clazz2 = regTypeInitializedReferenceToClass(type2);
2398 ClassObject* mergedClass;
2399
2400 mergedClass = findCommonSuperclass(clazz1, clazz2);
2401 assert(mergedClass != NULL);
2402 result = regTypeFromClass(mergedClass);
2403 }
2404 }
2405 }
2406
2407 if (result != type1)
2408 *pChanged = true;
2409 return result;
2410}
2411
2412/*
2413 * Control can transfer to "nextInsn".
2414 *
2415 * Merge the registers from "workRegs" into "regTypes" at "nextInsn", and
2416 * set the "changed" flag on the target address if the registers have changed.
2417 */
2418static void updateRegisters(const Method* meth, InsnFlags* insnFlags,
2419 RegisterTable* regTable, int nextInsn, const RegType* workRegs)
2420{
2421 RegType* targetRegs = getRegisterLine(regTable, nextInsn);
2422 const int insnRegCount = meth->registersSize;
2423
Andy McFaddend3250112010-11-03 14:32:42 -07002424 assert(targetRegs != NULL);
2425
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002426#if 0
2427 if (!dvmInsnIsBranchTarget(insnFlags, nextInsn)) {
2428 LOGE("insnFlags[0x%x]=0x%08x\n", nextInsn, insnFlags[nextInsn]);
2429 LOGE(" In %s.%s %s\n",
2430 meth->clazz->descriptor, meth->name, meth->descriptor);
2431 assert(false);
2432 }
2433#endif
2434
2435 if (!dvmInsnIsVisitedOrChanged(insnFlags, nextInsn)) {
2436 /*
2437 * We haven't processed this instruction before, and we haven't
2438 * touched the registers here, so there's nothing to "merge". Copy
2439 * the registers over and mark it as changed. (This is the only
2440 * way a register can transition out of "unknown", so this is not
2441 * just an optimization.)
2442 */
2443 LOGVV("COPY into 0x%04x\n", nextInsn);
2444 copyRegisters(targetRegs, workRegs, insnRegCount + kExtraRegs);
2445 dvmInsnSetChanged(insnFlags, nextInsn, true);
Andy McFadden470cbbb2010-11-04 16:31:37 -07002446#ifdef VERIFIER_STATS
2447 gDvm.verifierStats.copyRegCount++;
2448#endif
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002449 } else {
2450 if (gDebugVerbose) {
2451 LOGVV("MERGE into 0x%04x\n", nextInsn);
2452 //dumpRegTypes(meth, insnFlags, targetRegs, 0, "targ", NULL, 0);
2453 //dumpRegTypes(meth, insnFlags, workRegs, 0, "work", NULL, 0);
2454 }
2455 /* merge registers, set Changed only if different */
2456 bool changed = false;
2457 int i;
2458
2459 for (i = 0; i < insnRegCount + kExtraRegs; i++) {
2460 targetRegs[i] = mergeTypes(targetRegs[i], workRegs[i], &changed);
2461 }
2462
2463 if (gDebugVerbose) {
2464 //LOGI(" RESULT (changed=%d)\n", changed);
2465 //dumpRegTypes(meth, insnFlags, targetRegs, 0, "rslt", NULL, 0);
2466 }
Andy McFadden470cbbb2010-11-04 16:31:37 -07002467#ifdef VERIFIER_STATS
2468 gDvm.verifierStats.mergeRegCount++;
2469 if (changed)
2470 gDvm.verifierStats.mergeRegChanged++;
2471#endif
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002472
2473 if (changed)
2474 dvmInsnSetChanged(insnFlags, nextInsn, true);
2475 }
2476}
2477
2478
2479/*
2480 * ===========================================================================
2481 * Utility functions
2482 * ===========================================================================
2483 */
2484
2485/*
2486 * Look up an instance field, specified by "fieldIdx", that is going to be
2487 * accessed in object "objType". This resolves the field and then verifies
2488 * that the class containing the field is an instance of the reference in
2489 * "objType".
2490 *
2491 * It is possible for "objType" to be kRegTypeZero, meaning that we might
2492 * have a null reference. This is a runtime problem, so we allow it,
2493 * skipping some of the type checks.
2494 *
2495 * In general, "objType" must be an initialized reference. However, we
2496 * allow it to be uninitialized if this is an "<init>" method and the field
2497 * is declared within the "objType" class.
2498 *
Andy McFadden62a75162009-04-17 17:23:37 -07002499 * Returns an InstField on success, returns NULL and sets "*pFailure"
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002500 * on failure.
2501 */
2502static InstField* getInstField(const Method* meth,
2503 const UninitInstanceMap* uninitMap, RegType objType, int fieldIdx,
Andy McFadden62a75162009-04-17 17:23:37 -07002504 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002505{
2506 InstField* instField = NULL;
2507 ClassObject* objClass;
2508 bool mustBeLocal = false;
2509
2510 if (!regTypeIsReference(objType)) {
Andy McFadden62a75162009-04-17 17:23:37 -07002511 LOG_VFY("VFY: attempt to access field in non-reference type %d\n",
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002512 objType);
Andy McFadden62a75162009-04-17 17:23:37 -07002513 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002514 goto bail;
2515 }
2516
Andy McFadden62a75162009-04-17 17:23:37 -07002517 instField = dvmOptResolveInstField(meth->clazz, fieldIdx, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002518 if (instField == NULL) {
2519 LOG_VFY("VFY: unable to resolve instance field %u\n", fieldIdx);
Andy McFadden62a75162009-04-17 17:23:37 -07002520 assert(!VERIFY_OK(*pFailure));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002521 goto bail;
2522 }
2523
2524 if (objType == kRegTypeZero)
2525 goto bail;
2526
2527 /*
2528 * Access to fields in uninitialized objects is allowed if this is
2529 * the <init> method for the object and the field in question is
2530 * declared by this class.
2531 */
2532 objClass = regTypeReferenceToClass(objType, uninitMap);
2533 assert(objClass != NULL);
2534 if (regTypeIsUninitReference(objType)) {
2535 if (!isInitMethod(meth) || meth->clazz != objClass) {
2536 LOG_VFY("VFY: attempt to access field via uninitialized ref\n");
Andy McFadden62a75162009-04-17 17:23:37 -07002537 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002538 goto bail;
2539 }
2540 mustBeLocal = true;
2541 }
2542
2543 if (!dvmInstanceof(objClass, instField->field.clazz)) {
2544 LOG_VFY("VFY: invalid field access (field %s.%s, through %s ref)\n",
2545 instField->field.clazz->descriptor, instField->field.name,
2546 objClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07002547 *pFailure = VERIFY_ERROR_NO_FIELD;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002548 goto bail;
2549 }
2550
2551 if (mustBeLocal) {
2552 /* for uninit ref, make sure it's defined by this class, not super */
2553 if (instField < objClass->ifields ||
2554 instField >= objClass->ifields + objClass->ifieldCount)
2555 {
2556 LOG_VFY("VFY: invalid constructor field access (field %s in %s)\n",
2557 instField->field.name, objClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07002558 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002559 goto bail;
2560 }
2561 }
2562
2563bail:
2564 return instField;
2565}
2566
2567/*
2568 * Look up a static field.
2569 *
Andy McFadden62a75162009-04-17 17:23:37 -07002570 * Returns a StaticField on success, returns NULL and sets "*pFailure"
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002571 * on failure.
2572 */
2573static StaticField* getStaticField(const Method* meth, int fieldIdx,
Andy McFadden62a75162009-04-17 17:23:37 -07002574 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002575{
2576 StaticField* staticField;
2577
Andy McFadden62a75162009-04-17 17:23:37 -07002578 staticField = dvmOptResolveStaticField(meth->clazz, fieldIdx, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002579 if (staticField == NULL) {
2580 DexFile* pDexFile = meth->clazz->pDvmDex->pDexFile;
2581 const DexFieldId* pFieldId;
2582
2583 pFieldId = dexGetFieldId(pDexFile, fieldIdx);
2584
2585 LOG_VFY("VFY: unable to resolve static field %u (%s) in %s\n", fieldIdx,
2586 dexStringById(pDexFile, pFieldId->nameIdx),
2587 dexStringByTypeIdx(pDexFile, pFieldId->classIdx));
Andy McFadden62a75162009-04-17 17:23:37 -07002588 assert(!VERIFY_OK(*pFailure));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002589 goto bail;
2590 }
2591
2592bail:
2593 return staticField;
2594}
2595
2596/*
2597 * If "field" is marked "final", make sure this is the either <clinit>
2598 * or <init> as appropriate.
2599 *
Andy McFadden62a75162009-04-17 17:23:37 -07002600 * Sets "*pFailure" on failure.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002601 */
2602static void checkFinalFieldAccess(const Method* meth, const Field* field,
Andy McFadden62a75162009-04-17 17:23:37 -07002603 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002604{
2605 if (!dvmIsFinalField(field))
2606 return;
2607
2608 /* make sure we're in the same class */
2609 if (meth->clazz != field->clazz) {
2610 LOG_VFY_METH(meth, "VFY: can't modify final field %s.%s\n",
2611 field->clazz->descriptor, field->name);
Andy McFaddenb51ea112009-05-08 16:50:17 -07002612 *pFailure = VERIFY_ERROR_ACCESS_FIELD;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002613 return;
2614 }
2615
2616 /*
Andy McFadden62a75162009-04-17 17:23:37 -07002617 * The VM spec descriptions of putfield and putstatic say that
2618 * IllegalAccessError is only thrown when the instructions appear
2619 * outside the declaring class. Our earlier attempts to restrict
2620 * final field modification to constructors are, therefore, wrong.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002621 */
2622#if 0
2623 /* make sure we're in the right kind of constructor */
2624 if (dvmIsStaticField(field)) {
2625 if (!isClassInitMethod(meth)) {
2626 LOG_VFY_METH(meth,
2627 "VFY: can't modify final static field outside <clinit>\n");
Andy McFadden62a75162009-04-17 17:23:37 -07002628 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002629 }
2630 } else {
2631 if (!isInitMethod(meth)) {
2632 LOG_VFY_METH(meth,
2633 "VFY: can't modify final field outside <init>\n");
Andy McFadden62a75162009-04-17 17:23:37 -07002634 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002635 }
2636 }
2637#endif
2638}
2639
2640/*
2641 * Make sure that the register type is suitable for use as an array index.
2642 *
Andy McFadden62a75162009-04-17 17:23:37 -07002643 * Sets "*pFailure" if not.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002644 */
2645static void checkArrayIndexType(const Method* meth, RegType regType,
Andy McFadden62a75162009-04-17 17:23:37 -07002646 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002647{
Andy McFadden62a75162009-04-17 17:23:37 -07002648 if (VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002649 /*
2650 * The 1nr types are interchangeable at this level. We could
2651 * do something special if we can definitively identify it as a
2652 * float, but there's no real value in doing so.
2653 */
Andy McFadden62a75162009-04-17 17:23:37 -07002654 checkTypeCategory(regType, kTypeCategory1nr, pFailure);
2655 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002656 LOG_VFY_METH(meth, "Invalid reg type for array index (%d)\n",
2657 regType);
2658 }
2659 }
2660}
2661
2662/*
2663 * Check constraints on constructor return. Specifically, make sure that
2664 * the "this" argument got initialized.
2665 *
2666 * The "this" argument to <init> uses code offset kUninitThisArgAddr, which
2667 * puts it at the start of the list in slot 0. If we see a register with
2668 * an uninitialized slot 0 reference, we know it somehow didn't get
2669 * initialized.
2670 *
2671 * Returns "true" if all is well.
2672 */
2673static bool checkConstructorReturn(const Method* meth, const RegType* insnRegs,
2674 const int insnRegCount)
2675{
2676 int i;
2677
2678 if (!isInitMethod(meth))
2679 return true;
2680
2681 RegType uninitThis = regTypeFromUninitIndex(kUninitThisArgSlot);
2682
2683 for (i = 0; i < insnRegCount; i++) {
2684 if (insnRegs[i] == uninitThis) {
2685 LOG_VFY("VFY: <init> returning without calling superclass init\n");
2686 return false;
2687 }
2688 }
2689 return true;
2690}
2691
2692/*
2693 * Verify that the target instruction is not "move-exception". It's important
2694 * that the only way to execute a move-exception is as the first instruction
2695 * of an exception handler.
2696 *
2697 * Returns "true" if all is well, "false" if the target instruction is
2698 * move-exception.
2699 */
2700static bool checkMoveException(const Method* meth, int insnIdx,
2701 const char* logNote)
2702{
2703 assert(insnIdx >= 0 && insnIdx < (int)dvmGetMethodInsnsSize(meth));
2704
2705 if ((meth->insns[insnIdx] & 0xff) == OP_MOVE_EXCEPTION) {
2706 LOG_VFY("VFY: invalid use of move-exception\n");
2707 return false;
2708 }
2709 return true;
2710}
2711
2712/*
2713 * For the "move-exception" instruction at "insnIdx", which must be at an
2714 * exception handler address, determine the first common superclass of
2715 * all exceptions that can land here. (For javac output, we're probably
2716 * looking at multiple spans of bytecode covered by one "try" that lands
2717 * at an exception-specific "catch", but in general the handler could be
2718 * shared for multiple exceptions.)
2719 *
2720 * Returns NULL if no matching exception handler can be found, or if the
2721 * exception is not a subclass of Throwable.
2722 */
Andy McFadden62a75162009-04-17 17:23:37 -07002723static ClassObject* getCaughtExceptionType(const Method* meth, int insnIdx,
2724 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002725{
Andy McFadden62a75162009-04-17 17:23:37 -07002726 VerifyError localFailure;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002727 const DexCode* pCode;
2728 DexFile* pDexFile;
2729 ClassObject* commonSuper = NULL;
Andy McFadden62a75162009-04-17 17:23:37 -07002730 bool foundPossibleHandler = false;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002731 u4 handlersSize;
2732 u4 offset;
2733 u4 i;
2734
2735 pDexFile = meth->clazz->pDvmDex->pDexFile;
2736 pCode = dvmGetMethodCode(meth);
2737
2738 if (pCode->triesSize != 0) {
2739 handlersSize = dexGetHandlersSize(pCode);
2740 offset = dexGetFirstHandlerOffset(pCode);
2741 } else {
2742 handlersSize = 0;
2743 offset = 0;
2744 }
2745
2746 for (i = 0; i < handlersSize; i++) {
2747 DexCatchIterator iterator;
2748 dexCatchIteratorInit(&iterator, pCode, offset);
2749
2750 for (;;) {
2751 const DexCatchHandler* handler = dexCatchIteratorNext(&iterator);
2752
2753 if (handler == NULL) {
2754 break;
2755 }
2756
2757 if (handler->address == (u4) insnIdx) {
2758 ClassObject* clazz;
Andy McFadden62a75162009-04-17 17:23:37 -07002759 foundPossibleHandler = true;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002760
2761 if (handler->typeIdx == kDexNoIndex)
2762 clazz = gDvm.classJavaLangThrowable;
2763 else
Andy McFadden62a75162009-04-17 17:23:37 -07002764 clazz = dvmOptResolveClass(meth->clazz, handler->typeIdx,
2765 &localFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002766
2767 if (clazz == NULL) {
2768 LOG_VFY("VFY: unable to resolve exception class %u (%s)\n",
2769 handler->typeIdx,
2770 dexStringByTypeIdx(pDexFile, handler->typeIdx));
Andy McFadden62a75162009-04-17 17:23:37 -07002771 /* TODO: do we want to keep going? If we don't fail
2772 * this we run the risk of having a non-Throwable
2773 * introduced at runtime. However, that won't pass
2774 * an instanceof test, so is essentially harmless. */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002775 } else {
2776 if (commonSuper == NULL)
2777 commonSuper = clazz;
2778 else
2779 commonSuper = findCommonSuperclass(clazz, commonSuper);
2780 }
2781 }
2782 }
2783
2784 offset = dexCatchIteratorGetEndOffset(&iterator, pCode);
2785 }
2786
2787 if (commonSuper == NULL) {
Andy McFadden62a75162009-04-17 17:23:37 -07002788 /* no catch blocks, or no catches with classes we can find */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002789 LOG_VFY_METH(meth,
2790 "VFY: unable to find exception handler at addr 0x%x\n", insnIdx);
Andy McFadden62a75162009-04-17 17:23:37 -07002791 *pFailure = VERIFY_ERROR_GENERIC;
2792 } else {
2793 // TODO: verify the class is an instance of Throwable?
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002794 }
2795
2796 return commonSuper;
2797}
2798
2799/*
2800 * Initialize the RegisterTable.
2801 *
2802 * Every instruction address can have a different set of information about
2803 * what's in which register, but for verification purposes we only need to
2804 * store it at branch target addresses (because we merge into that).
2805 *
2806 * By zeroing out the storage we are effectively initializing the register
2807 * information to kRegTypeUnknown.
2808 */
2809static bool initRegisterTable(const Method* meth, const InsnFlags* insnFlags,
2810 RegisterTable* regTable, RegisterTrackingMode trackRegsFor)
2811{
2812 const int insnsSize = dvmGetMethodInsnsSize(meth);
2813 int i;
2814
2815 regTable->insnRegCountPlus = meth->registersSize + kExtraRegs;
2816 regTable->addrRegs = (RegType**) calloc(insnsSize, sizeof(RegType*));
2817 if (regTable->addrRegs == NULL)
2818 return false;
2819
2820 assert(insnsSize > 0);
2821
2822 /*
2823 * "All" means "every address that holds the start of an instruction".
2824 * "Branches" and "GcPoints" mean just those addresses.
2825 *
2826 * "GcPoints" fills about half the addresses, "Branches" about 15%.
2827 */
2828 int interestingCount = 0;
2829 //int insnCount = 0;
2830
2831 for (i = 0; i < insnsSize; i++) {
2832 bool interesting;
2833
2834 switch (trackRegsFor) {
2835 case kTrackRegsAll:
2836 interesting = dvmInsnIsOpcode(insnFlags, i);
2837 break;
2838 case kTrackRegsGcPoints:
2839 interesting = dvmInsnIsGcPoint(insnFlags, i) ||
2840 dvmInsnIsBranchTarget(insnFlags, i);
2841 break;
2842 case kTrackRegsBranches:
2843 interesting = dvmInsnIsBranchTarget(insnFlags, i);
2844 break;
2845 default:
2846 dvmAbort();
2847 return false;
2848 }
2849
2850 if (interesting)
2851 interestingCount++;
2852
2853 /* count instructions, for display only */
2854 //if (dvmInsnIsOpcode(insnFlags, i))
2855 // insnCount++;
2856 }
2857
2858 regTable->regAlloc = (RegType*)
2859 calloc(regTable->insnRegCountPlus * interestingCount, sizeof(RegType));
2860 if (regTable->regAlloc == NULL)
2861 return false;
2862
2863 RegType* regPtr = regTable->regAlloc;
2864 for (i = 0; i < insnsSize; i++) {
2865 bool interesting;
2866
2867 switch (trackRegsFor) {
2868 case kTrackRegsAll:
2869 interesting = dvmInsnIsOpcode(insnFlags, i);
2870 break;
2871 case kTrackRegsGcPoints:
2872 interesting = dvmInsnIsGcPoint(insnFlags, i) ||
2873 dvmInsnIsBranchTarget(insnFlags, i);
2874 break;
2875 case kTrackRegsBranches:
2876 interesting = dvmInsnIsBranchTarget(insnFlags, i);
2877 break;
2878 default:
2879 dvmAbort();
2880 return false;
2881 }
2882
2883 if (interesting) {
2884 regTable->addrRegs[i] = regPtr;
2885 regPtr += regTable->insnRegCountPlus;
2886 }
2887 }
2888
2889 //LOGD("Tracking registers for %d, total %d of %d(%d) (%d%%)\n",
2890 // TRACK_REGS_FOR, interestingCount, insnCount, insnsSize,
2891 // (interestingCount*100) / insnCount);
2892
2893 assert(regPtr - regTable->regAlloc ==
2894 regTable->insnRegCountPlus * interestingCount);
2895 assert(regTable->addrRegs[0] != NULL);
2896 return true;
2897}
2898
2899
2900/*
2901 * Verify that the arguments in a filled-new-array instruction are valid.
2902 *
2903 * "resClass" is the class refered to by pDecInsn->vB.
2904 */
2905static void verifyFilledNewArrayRegs(const Method* meth,
Andy McFaddend3250112010-11-03 14:32:42 -07002906 const RegType* insnRegs, const DecodedInstruction* pDecInsn,
2907 ClassObject* resClass, bool isRange, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002908{
2909 u4 argCount = pDecInsn->vA;
2910 RegType expectedType;
2911 PrimitiveType elemType;
2912 unsigned int ui;
2913
2914 assert(dvmIsArrayClass(resClass));
2915 elemType = resClass->elementClass->primitiveType;
2916 if (elemType == PRIM_NOT) {
2917 expectedType = regTypeFromClass(resClass->elementClass);
2918 } else {
2919 expectedType = primitiveTypeToRegType(elemType);
2920 }
2921 //LOGI("filled-new-array: %s -> %d\n", resClass->descriptor, expectedType);
2922
2923 /*
2924 * Verify each register. If "argCount" is bad, verifyRegisterType()
2925 * will run off the end of the list and fail. It's legal, if silly,
2926 * for argCount to be zero.
2927 */
2928 for (ui = 0; ui < argCount; ui++) {
2929 u4 getReg;
2930
2931 if (isRange)
2932 getReg = pDecInsn->vC + ui;
2933 else
2934 getReg = pDecInsn->arg[ui];
2935
Andy McFaddend3250112010-11-03 14:32:42 -07002936 verifyRegisterType(insnRegs, getReg, expectedType, pFailure);
Andy McFadden62a75162009-04-17 17:23:37 -07002937 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002938 LOG_VFY("VFY: filled-new-array arg %u(%u) not valid\n", ui, getReg);
2939 return;
2940 }
2941 }
2942}
2943
2944
2945/*
Andy McFaddenb51ea112009-05-08 16:50:17 -07002946 * Replace an instruction with "throw-verification-error". This allows us to
2947 * defer error reporting until the code path is first used.
2948 *
Andy McFadden861b3382010-03-05 15:58:31 -08002949 * This is expected to be called during "just in time" verification, not
2950 * from within dexopt. (Verification failures in dexopt will result in
2951 * postponement of verification to first use of the class.)
2952 *
Andy McFaddenb51ea112009-05-08 16:50:17 -07002953 * The throw-verification-error instruction requires two code units. Some
2954 * of the replaced instructions require three; the third code unit will
2955 * receive a "nop". The instruction's length will be left unchanged
2956 * in "insnFlags".
2957 *
Andy McFadden96516932009-10-28 17:39:02 -07002958 * The verifier explicitly locks out breakpoint activity, so there should
2959 * be no clashes with the debugger.
2960 *
Andy McFaddenb51ea112009-05-08 16:50:17 -07002961 * Returns "true" on success.
2962 */
Andy McFadden228a6b02010-05-04 15:02:32 -07002963static bool replaceFailingInstruction(const Method* meth, InsnFlags* insnFlags,
Andy McFaddenb51ea112009-05-08 16:50:17 -07002964 int insnIdx, VerifyError failure)
2965{
Andy McFaddenaf0e8382009-08-28 10:38:37 -07002966 VerifyErrorRefType refType;
Andy McFaddenb51ea112009-05-08 16:50:17 -07002967 const u2* oldInsns = meth->insns + insnIdx;
2968 u2 oldInsn = *oldInsns;
2969 bool result = false;
2970
Andy McFaddenfb119e62010-06-28 16:21:20 -07002971 if (gDvm.optimizing)
2972 LOGD("Weird: RFI during dexopt?");
2973
Andy McFaddenb51ea112009-05-08 16:50:17 -07002974 //LOGD(" was 0x%04x\n", oldInsn);
2975 u2* newInsns = (u2*) meth->insns + insnIdx;
2976
2977 /*
2978 * Generate the new instruction out of the old.
2979 *
2980 * First, make sure this is an instruction we're expecting to stomp on.
2981 */
2982 switch (oldInsn & 0xff) {
2983 case OP_CONST_CLASS: // insn[1] == class ref, 2 bytes
2984 case OP_CHECK_CAST:
2985 case OP_INSTANCE_OF:
2986 case OP_NEW_INSTANCE:
2987 case OP_NEW_ARRAY:
Andy McFaddenb51ea112009-05-08 16:50:17 -07002988 case OP_FILLED_NEW_ARRAY: // insn[1] == class ref, 3 bytes
2989 case OP_FILLED_NEW_ARRAY_RANGE:
Andy McFaddenaf0e8382009-08-28 10:38:37 -07002990 refType = VERIFY_ERROR_REF_CLASS;
2991 break;
Andy McFaddenb51ea112009-05-08 16:50:17 -07002992
2993 case OP_IGET: // insn[1] == field ref, 2 bytes
2994 case OP_IGET_BOOLEAN:
2995 case OP_IGET_BYTE:
2996 case OP_IGET_CHAR:
2997 case OP_IGET_SHORT:
2998 case OP_IGET_WIDE:
2999 case OP_IGET_OBJECT:
3000 case OP_IPUT:
3001 case OP_IPUT_BOOLEAN:
3002 case OP_IPUT_BYTE:
3003 case OP_IPUT_CHAR:
3004 case OP_IPUT_SHORT:
3005 case OP_IPUT_WIDE:
3006 case OP_IPUT_OBJECT:
3007 case OP_SGET:
3008 case OP_SGET_BOOLEAN:
3009 case OP_SGET_BYTE:
3010 case OP_SGET_CHAR:
3011 case OP_SGET_SHORT:
3012 case OP_SGET_WIDE:
3013 case OP_SGET_OBJECT:
3014 case OP_SPUT:
3015 case OP_SPUT_BOOLEAN:
3016 case OP_SPUT_BYTE:
3017 case OP_SPUT_CHAR:
3018 case OP_SPUT_SHORT:
3019 case OP_SPUT_WIDE:
3020 case OP_SPUT_OBJECT:
Andy McFaddenaf0e8382009-08-28 10:38:37 -07003021 refType = VERIFY_ERROR_REF_FIELD;
3022 break;
Andy McFaddenb51ea112009-05-08 16:50:17 -07003023
3024 case OP_INVOKE_VIRTUAL: // insn[1] == method ref, 3 bytes
3025 case OP_INVOKE_VIRTUAL_RANGE:
3026 case OP_INVOKE_SUPER:
3027 case OP_INVOKE_SUPER_RANGE:
3028 case OP_INVOKE_DIRECT:
3029 case OP_INVOKE_DIRECT_RANGE:
3030 case OP_INVOKE_STATIC:
3031 case OP_INVOKE_STATIC_RANGE:
3032 case OP_INVOKE_INTERFACE:
3033 case OP_INVOKE_INTERFACE_RANGE:
Andy McFaddenaf0e8382009-08-28 10:38:37 -07003034 refType = VERIFY_ERROR_REF_METHOD;
Andy McFaddenb51ea112009-05-08 16:50:17 -07003035 break;
Andy McFaddenaf0e8382009-08-28 10:38:37 -07003036
Andy McFaddenb51ea112009-05-08 16:50:17 -07003037 default:
3038 /* could handle this in a generic way, but this is probably safer */
3039 LOG_VFY("GLITCH: verifier asked to replace opcode 0x%02x\n",
3040 oldInsn & 0xff);
3041 goto bail;
3042 }
3043
3044 /* write a NOP over the third code unit, if necessary */
3045 int width = dvmInsnGetWidth(insnFlags, insnIdx);
3046 switch (width) {
3047 case 2:
3048 /* nothing to do */
3049 break;
3050 case 3:
Andy McFadden96516932009-10-28 17:39:02 -07003051 dvmDexChangeDex2(meth->clazz->pDvmDex, newInsns+2, OP_NOP);
3052 //newInsns[2] = OP_NOP;
Andy McFaddenb51ea112009-05-08 16:50:17 -07003053 break;
3054 default:
3055 /* whoops */
3056 LOGE("ERROR: stomped a %d-unit instruction with a verifier error\n",
3057 width);
3058 dvmAbort();
3059 }
3060
3061 /* encode the opcode, with the failure code in the high byte */
Andy McFadden96516932009-10-28 17:39:02 -07003062 u2 newVal = OP_THROW_VERIFICATION_ERROR |
Andy McFaddenaf0e8382009-08-28 10:38:37 -07003063 (failure << 8) | (refType << (8 + kVerifyErrorRefTypeShift));
Andy McFadden96516932009-10-28 17:39:02 -07003064 //newInsns[0] = newVal;
3065 dvmDexChangeDex2(meth->clazz->pDvmDex, newInsns, newVal);
Andy McFaddenb51ea112009-05-08 16:50:17 -07003066
3067 result = true;
3068
3069bail:
Andy McFaddenb51ea112009-05-08 16:50:17 -07003070 return result;
3071}
3072
3073
3074/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003075 * ===========================================================================
3076 * Entry point and driver loop
3077 * ===========================================================================
3078 */
3079
3080/*
Andy McFadden470cbbb2010-11-04 16:31:37 -07003081 * Entry point for the detailed code-flow analysis of a single method.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003082 */
Andy McFadden228a6b02010-05-04 15:02:32 -07003083bool dvmVerifyCodeFlow(VerifierData* vdata)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003084{
3085 bool result = false;
Andy McFadden228a6b02010-05-04 15:02:32 -07003086 const Method* meth = vdata->method;
3087 const int insnsSize = vdata->insnsSize;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003088 const bool generateRegisterMap = gDvm.generateRegisterMaps;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003089 RegisterTable regTable;
3090
3091 memset(&regTable, 0, sizeof(regTable));
3092
Andy McFadden470cbbb2010-11-04 16:31:37 -07003093#ifdef VERIFIER_STATS
3094 gDvm.verifierStats.methodsExamined++;
3095#endif
3096
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003097#ifndef NDEBUG
3098 checkMergeTab(); // only need to do this if table gets updated
3099#endif
3100
3101 /*
3102 * We rely on these for verification of const-class, const-string,
3103 * and throw instructions. Make sure we have them.
3104 */
3105 if (gDvm.classJavaLangClass == NULL)
3106 gDvm.classJavaLangClass =
3107 dvmFindSystemClassNoInit("Ljava/lang/Class;");
3108 if (gDvm.classJavaLangString == NULL)
3109 gDvm.classJavaLangString =
3110 dvmFindSystemClassNoInit("Ljava/lang/String;");
Andy McFadden686e1e22009-05-26 16:56:30 -07003111 if (gDvm.classJavaLangThrowable == NULL) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003112 gDvm.classJavaLangThrowable =
3113 dvmFindSystemClassNoInit("Ljava/lang/Throwable;");
Andy McFadden686e1e22009-05-26 16:56:30 -07003114 gDvm.offJavaLangThrowable_cause =
3115 dvmFindFieldOffset(gDvm.classJavaLangThrowable,
3116 "cause", "Ljava/lang/Throwable;");
3117 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003118 if (gDvm.classJavaLangObject == NULL)
3119 gDvm.classJavaLangObject =
3120 dvmFindSystemClassNoInit("Ljava/lang/Object;");
3121
Andy McFadden6be954f2010-06-14 13:37:49 -07003122 if (meth->registersSize * insnsSize > 4*1024*1024) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003123 LOG_VFY_METH(meth,
Andy McFadden34e314a2010-09-28 13:58:16 -07003124 "VFY: warning: method is huge (regs=%d insnsSize=%d)\n",
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003125 meth->registersSize, insnsSize);
Andy McFadden34e314a2010-09-28 13:58:16 -07003126 /* might be bogus data, might be some huge generated method */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003127 }
3128
3129 /*
3130 * Create register lists, and initialize them to "Unknown". If we're
3131 * also going to create the register map, we need to retain the
3132 * register lists for a larger set of addresses.
3133 */
Andy McFadden228a6b02010-05-04 15:02:32 -07003134 if (!initRegisterTable(meth, vdata->insnFlags, &regTable,
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003135 generateRegisterMap ? kTrackRegsGcPoints : kTrackRegsBranches))
3136 goto bail;
3137
Andy McFadden228a6b02010-05-04 15:02:32 -07003138 vdata->addrRegs = NULL; /* don't set this until we need it */
3139
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003140 /*
3141 * Initialize the types of the registers that correspond to the
3142 * method arguments. We can determine this from the method signature.
3143 */
Andy McFadden228a6b02010-05-04 15:02:32 -07003144 if (!setTypesFromSignature(meth, regTable.addrRegs[0], vdata->uninitMap))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003145 goto bail;
3146
3147 /*
3148 * Run the verifier.
3149 */
Andy McFadden228a6b02010-05-04 15:02:32 -07003150 if (!doCodeVerification(meth, vdata->insnFlags, &regTable, vdata->uninitMap))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003151 goto bail;
3152
3153 /*
3154 * Generate a register map.
3155 */
3156 if (generateRegisterMap) {
Andy McFadden228a6b02010-05-04 15:02:32 -07003157 vdata->addrRegs = regTable.addrRegs;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003158
Andy McFadden228a6b02010-05-04 15:02:32 -07003159 RegisterMap* pMap = dvmGenerateRegisterMapV(vdata);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003160 if (pMap != NULL) {
3161 /*
3162 * Tuck it into the Method struct. It will either get used
3163 * directly or, if we're in dexopt, will be packed up and
3164 * appended to the DEX file.
3165 */
3166 dvmSetRegisterMap((Method*)meth, pMap);
3167 }
3168 }
3169
3170 /*
3171 * Success.
3172 */
3173 result = true;
3174
3175bail:
3176 free(regTable.addrRegs);
3177 free(regTable.regAlloc);
3178 return result;
3179}
3180
3181/*
3182 * Grind through the instructions.
3183 *
3184 * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit
3185 * on the first instruction, process it (setting additional "changed" bits),
3186 * and repeat until there are no more.
3187 *
3188 * v3 4.11.1.1
3189 * - (N/A) operand stack is always the same size
3190 * - operand stack [registers] contain the correct types of values
3191 * - local variables [registers] contain the correct types of values
3192 * - methods are invoked with the appropriate arguments
3193 * - fields are assigned using values of appropriate types
3194 * - opcodes have the correct type values in operand registers
3195 * - there is never an uninitialized class instance in a local variable in
3196 * code protected by an exception handler (operand stack is okay, because
3197 * the operand stack is discarded when an exception is thrown) [can't
3198 * know what's a local var w/o the debug info -- should fall out of
3199 * register typing]
3200 *
3201 * v3 4.11.1.2
3202 * - execution cannot fall off the end of the code
3203 *
3204 * (We also do many of the items described in the "static checks" sections,
3205 * because it's easier to do them here.)
3206 *
3207 * We need an array of RegType values, one per register, for every
3208 * instruction. In theory this could become quite large -- up to several
3209 * megabytes for a monster function. For self-preservation we reject
3210 * anything that requires more than a certain amount of memory. (Typical
3211 * "large" should be on the order of 4K code units * 8 registers.) This
3212 * will likely have to be adjusted.
3213 *
3214 *
3215 * The spec forbids backward branches when there's an uninitialized reference
3216 * in a register. The idea is to prevent something like this:
3217 * loop:
3218 * move r1, r0
3219 * new-instance r0, MyClass
3220 * ...
3221 * if-eq rN, loop // once
3222 * initialize r0
3223 *
3224 * This leaves us with two different instances, both allocated by the
3225 * same instruction, but only one is initialized. The scheme outlined in
3226 * v3 4.11.1.4 wouldn't catch this, so they work around it by preventing
3227 * backward branches. We achieve identical results without restricting
3228 * code reordering by specifying that you can't execute the new-instance
3229 * instruction if a register contains an uninitialized instance created
3230 * by that same instrutcion.
3231 */
Andy McFadden228a6b02010-05-04 15:02:32 -07003232static bool doCodeVerification(const Method* meth, InsnFlags* insnFlags,
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003233 RegisterTable* regTable, UninitInstanceMap* uninitMap)
3234{
3235 const int insnsSize = dvmGetMethodInsnsSize(meth);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003236 RegType workRegs[meth->registersSize + kExtraRegs];
3237 bool result = false;
3238 bool debugVerbose = false;
Carl Shapiroe3c01da2010-05-20 22:54:18 -07003239 int insnIdx, startGuess;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003240
3241 /*
3242 * Begin by marking the first instruction as "changed".
3243 */
3244 dvmInsnSetChanged(insnFlags, 0, true);
3245
3246 if (doVerboseLogging(meth)) {
3247 IF_LOGI() {
3248 char* desc = dexProtoCopyMethodDescriptor(&meth->prototype);
3249 LOGI("Now verifying: %s.%s %s (ins=%d regs=%d)\n",
3250 meth->clazz->descriptor, meth->name, desc,
3251 meth->insSize, meth->registersSize);
3252 LOGI(" ------ [0 4 8 12 16 20 24 28 32 36\n");
3253 free(desc);
3254 }
3255 debugVerbose = true;
3256 gDebugVerbose = true;
3257 } else {
3258 gDebugVerbose = false;
3259 }
3260
3261 startGuess = 0;
3262
3263 /*
3264 * Continue until no instructions are marked "changed".
3265 */
3266 while (true) {
3267 /*
3268 * Find the first marked one. Use "startGuess" as a way to find
3269 * one quickly.
3270 */
3271 for (insnIdx = startGuess; insnIdx < insnsSize; insnIdx++) {
3272 if (dvmInsnIsChanged(insnFlags, insnIdx))
3273 break;
3274 }
3275
3276 if (insnIdx == insnsSize) {
3277 if (startGuess != 0) {
3278 /* try again, starting from the top */
3279 startGuess = 0;
3280 continue;
3281 } else {
3282 /* all flags are clear */
3283 break;
3284 }
3285 }
3286
3287 /*
3288 * We carry the working set of registers from instruction to
3289 * instruction. If this address can be the target of a branch
3290 * (or throw) instruction, or if we're skipping around chasing
3291 * "changed" flags, we need to load the set of registers from
3292 * the table.
3293 *
3294 * Because we always prefer to continue on to the next instruction,
3295 * we should never have a situation where we have a stray
3296 * "changed" flag set on an instruction that isn't a branch target.
3297 */
3298 if (dvmInsnIsBranchTarget(insnFlags, insnIdx)) {
3299 RegType* insnRegs = getRegisterLine(regTable, insnIdx);
3300 assert(insnRegs != NULL);
3301 copyRegisters(workRegs, insnRegs, meth->registersSize + kExtraRegs);
3302
3303 if (debugVerbose) {
3304 dumpRegTypes(meth, insnFlags, workRegs, insnIdx, NULL,uninitMap,
3305 SHOW_REG_DETAILS);
3306 }
3307
3308 } else {
3309 if (debugVerbose) {
3310 dumpRegTypes(meth, insnFlags, workRegs, insnIdx, NULL,uninitMap,
3311 SHOW_REG_DETAILS);
3312 }
3313
3314#ifndef NDEBUG
3315 /*
3316 * Sanity check: retrieve the stored register line (assuming
3317 * a full table) and make sure it actually matches.
3318 */
3319 RegType* insnRegs = getRegisterLine(regTable, insnIdx);
3320 if (insnRegs != NULL &&
3321 compareRegisters(workRegs, insnRegs,
3322 meth->registersSize + kExtraRegs) != 0)
3323 {
3324 char* desc = dexProtoCopyMethodDescriptor(&meth->prototype);
3325 LOG_VFY("HUH? workRegs diverged in %s.%s %s\n",
3326 meth->clazz->descriptor, meth->name, desc);
3327 free(desc);
3328 dumpRegTypes(meth, insnFlags, workRegs, 0, "work",
3329 uninitMap, DRT_SHOW_REF_TYPES | DRT_SHOW_LOCALS);
3330 dumpRegTypes(meth, insnFlags, insnRegs, 0, "insn",
3331 uninitMap, DRT_SHOW_REF_TYPES | DRT_SHOW_LOCALS);
3332 }
3333#endif
3334 }
3335
3336 //LOGI("process %s.%s %s %d\n",
3337 // meth->clazz->descriptor, meth->name, meth->descriptor, insnIdx);
3338 if (!verifyInstruction(meth, insnFlags, regTable, workRegs, insnIdx,
3339 uninitMap, &startGuess))
3340 {
3341 //LOGD("+++ %s bailing at %d\n", meth->name, insnIdx);
3342 goto bail;
3343 }
3344
3345#if 0
3346 {
3347 static const int gcMask = kInstrCanBranch | kInstrCanSwitch |
3348 kInstrCanThrow | kInstrCanReturn;
3349 OpCode opCode = *(meth->insns + insnIdx) & 0xff;
3350 int flags = dexGetInstrFlags(gDvm.instrFlags, opCode);
3351
3352 /* 8, 16, 32, or 32*n -bit regs */
3353 int regWidth = (meth->registersSize + 7) / 8;
3354 if (regWidth == 3)
3355 regWidth = 4;
3356 if (regWidth > 4) {
3357 regWidth = ((regWidth + 3) / 4) * 4;
3358 if (false) {
3359 LOGW("WOW: %d regs -> %d %s.%s\n",
3360 meth->registersSize, regWidth,
3361 meth->clazz->descriptor, meth->name);
3362 //x = true;
3363 }
3364 }
3365
3366 if ((flags & gcMask) != 0) {
3367 /* this is a potential GC point */
3368 gDvm__gcInstr++;
3369
3370 if (insnsSize < 256)
3371 gDvm__gcData += 1;
3372 else
3373 gDvm__gcData += 2;
3374 gDvm__gcData += regWidth;
3375 }
3376 gDvm__gcSimpleData += regWidth;
3377
3378 gDvm__totalInstr++;
3379 }
3380#endif
3381
3382 /*
3383 * Clear "changed" and mark as visited.
3384 */
3385 dvmInsnSetVisited(insnFlags, insnIdx, true);
3386 dvmInsnSetChanged(insnFlags, insnIdx, false);
3387 }
3388
Andy McFaddenb51ea112009-05-08 16:50:17 -07003389 if (DEAD_CODE_SCAN && !IS_METHOD_FLAG_SET(meth, METHOD_ISWRITABLE)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003390 /*
Andy McFaddenb51ea112009-05-08 16:50:17 -07003391 * Scan for dead code. There's nothing "evil" about dead code
3392 * (besides the wasted space), but it indicates a flaw somewhere
3393 * down the line, possibly in the verifier.
3394 *
3395 * If we've rewritten "always throw" instructions into the stream,
3396 * we are almost certainly going to have some dead code.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003397 */
3398 int deadStart = -1;
3399 for (insnIdx = 0; insnIdx < insnsSize;
3400 insnIdx += dvmInsnGetWidth(insnFlags, insnIdx))
3401 {
3402 /*
3403 * Switch-statement data doesn't get "visited" by scanner. It
3404 * may or may not be preceded by a padding NOP.
3405 */
3406 int instr = meth->insns[insnIdx];
3407 if (instr == kPackedSwitchSignature ||
3408 instr == kSparseSwitchSignature ||
3409 instr == kArrayDataSignature ||
3410 (instr == OP_NOP &&
3411 (meth->insns[insnIdx+1] == kPackedSwitchSignature ||
3412 meth->insns[insnIdx+1] == kSparseSwitchSignature ||
3413 meth->insns[insnIdx+1] == kArrayDataSignature)))
3414 {
3415 dvmInsnSetVisited(insnFlags, insnIdx, true);
3416 }
3417
3418 if (!dvmInsnIsVisited(insnFlags, insnIdx)) {
3419 if (deadStart < 0)
3420 deadStart = insnIdx;
3421 } else if (deadStart >= 0) {
3422 IF_LOGD() {
3423 char* desc =
3424 dexProtoCopyMethodDescriptor(&meth->prototype);
3425 LOGD("VFY: dead code 0x%04x-%04x in %s.%s %s\n",
3426 deadStart, insnIdx-1,
3427 meth->clazz->descriptor, meth->name, desc);
3428 free(desc);
3429 }
3430
3431 deadStart = -1;
3432 }
3433 }
3434 if (deadStart >= 0) {
3435 IF_LOGD() {
3436 char* desc = dexProtoCopyMethodDescriptor(&meth->prototype);
3437 LOGD("VFY: dead code 0x%04x-%04x in %s.%s %s\n",
3438 deadStart, insnIdx-1,
3439 meth->clazz->descriptor, meth->name, desc);
3440 free(desc);
3441 }
3442 }
3443 }
3444
3445 result = true;
3446
3447bail:
3448 return result;
3449}
3450
3451
3452/*
3453 * Perform verification for a single instruction.
3454 *
3455 * This requires fully decoding the instruction to determine the effect
3456 * it has on registers.
3457 *
3458 * Finds zero or more following instructions and sets the "changed" flag
3459 * if execution at that point needs to be (re-)evaluated. Register changes
3460 * are merged into "regTypes" at the target addresses. Does not set or
3461 * clear any other flags in "insnFlags".
Andy McFaddenb51ea112009-05-08 16:50:17 -07003462 *
3463 * This may alter meth->insns if we need to replace an instruction with
3464 * throw-verification-error.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003465 */
Andy McFadden228a6b02010-05-04 15:02:32 -07003466static bool verifyInstruction(const Method* meth, InsnFlags* insnFlags,
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003467 RegisterTable* regTable, RegType* workRegs, int insnIdx,
3468 UninitInstanceMap* uninitMap, int* pStartGuess)
3469{
3470 const int insnsSize = dvmGetMethodInsnsSize(meth);
3471 const u2* insns = meth->insns + insnIdx;
3472 bool result = false;
3473
Andy McFadden470cbbb2010-11-04 16:31:37 -07003474#ifdef VERIFIER_STATS
3475 if (dvmInsnIsVisited(insnFlags, insnIdx)) {
3476 gDvm.verifierStats.instrsReexamined++;
3477 } else {
3478 gDvm.verifierStats.instrsExamined++;
3479 }
3480#endif
3481
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003482 /*
3483 * Once we finish decoding the instruction, we need to figure out where
3484 * we can go from here. There are three possible ways to transfer
3485 * control to another statement:
3486 *
3487 * (1) Continue to the next instruction. Applies to all but
3488 * unconditional branches, method returns, and exception throws.
3489 * (2) Branch to one or more possible locations. Applies to branches
3490 * and switch statements.
3491 * (3) Exception handlers. Applies to any instruction that can
3492 * throw an exception that is handled by an encompassing "try"
Andy McFadden228a6b02010-05-04 15:02:32 -07003493 * block.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003494 *
3495 * We can also return, in which case there is no successor instruction
3496 * from this point.
3497 *
Andy McFadden228a6b02010-05-04 15:02:32 -07003498 * The behavior can be determined from the InstructionFlags.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003499 */
3500
3501 const DexFile* pDexFile = meth->clazz->pDvmDex->pDexFile;
3502 RegType entryRegs[meth->registersSize + kExtraRegs];
3503 ClassObject* resClass;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003504 int branchTarget = 0;
3505 const int insnRegCount = meth->registersSize;
3506 RegType tmpType;
3507 DecodedInstruction decInsn;
3508 bool justSetResult = false;
Andy McFadden62a75162009-04-17 17:23:37 -07003509 VerifyError failure = VERIFY_ERROR_NONE;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003510
3511#ifndef NDEBUG
3512 memset(&decInsn, 0x81, sizeof(decInsn));
3513#endif
Dan Bornstein44a38f42010-11-10 17:34:32 -08003514 dexDecodeInstruction(&gDvm.instrInfo, insns, &decInsn);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003515
Dan Bornstein44a38f42010-11-10 17:34:32 -08003516 int nextFlags = dexGetInstrFlags(gDvm.instrInfo.flags, decInsn.opCode);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003517
3518 /*
3519 * Make a copy of the previous register state. If the instruction
3520 * throws an exception, we merge *this* into the destination rather
3521 * than workRegs, because we don't want the result from the "successful"
3522 * code path (e.g. a check-cast that "improves" a type) to be visible
3523 * to the exception handler.
3524 */
3525 if ((nextFlags & kInstrCanThrow) != 0 && dvmInsnIsInTry(insnFlags, insnIdx))
3526 {
3527 copyRegisters(entryRegs, workRegs, meth->registersSize + kExtraRegs);
3528 } else {
3529#ifndef NDEBUG
3530 memset(entryRegs, 0xdd,
3531 (meth->registersSize + kExtraRegs) * sizeof(RegType));
3532#endif
3533 }
3534
3535 switch (decInsn.opCode) {
3536 case OP_NOP:
3537 /*
3538 * A "pure" NOP has no effect on anything. Data tables start with
3539 * a signature that looks like a NOP; if we see one of these in
3540 * the course of executing code then we have a problem.
3541 */
3542 if (decInsn.vA != 0) {
3543 LOG_VFY("VFY: encountered data table in instruction stream\n");
Andy McFadden62a75162009-04-17 17:23:37 -07003544 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003545 }
3546 break;
3547
3548 case OP_MOVE:
3549 case OP_MOVE_FROM16:
3550 case OP_MOVE_16:
Andy McFaddend3250112010-11-03 14:32:42 -07003551 copyRegister1(workRegs, decInsn.vA, decInsn.vB, kTypeCategory1nr,
3552 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003553 break;
3554 case OP_MOVE_WIDE:
3555 case OP_MOVE_WIDE_FROM16:
3556 case OP_MOVE_WIDE_16:
Andy McFaddend3250112010-11-03 14:32:42 -07003557 copyRegister2(workRegs, decInsn.vA, decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003558 break;
3559 case OP_MOVE_OBJECT:
3560 case OP_MOVE_OBJECT_FROM16:
3561 case OP_MOVE_OBJECT_16:
Andy McFaddend3250112010-11-03 14:32:42 -07003562 copyRegister1(workRegs, decInsn.vA, decInsn.vB, kTypeCategoryRef,
3563 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003564 break;
3565
3566 /*
3567 * The move-result instructions copy data out of a "pseudo-register"
3568 * with the results from the last method invocation. In practice we
3569 * might want to hold the result in an actual CPU register, so the
3570 * Dalvik spec requires that these only appear immediately after an
3571 * invoke or filled-new-array.
3572 *
3573 * These calls invalidate the "result" register. (This is now
3574 * redundant with the reset done below, but it can make the debug info
3575 * easier to read in some cases.)
3576 */
3577 case OP_MOVE_RESULT:
3578 copyResultRegister1(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003579 kTypeCategory1nr, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003580 break;
3581 case OP_MOVE_RESULT_WIDE:
Andy McFadden62a75162009-04-17 17:23:37 -07003582 copyResultRegister2(workRegs, insnRegCount, decInsn.vA, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003583 break;
3584 case OP_MOVE_RESULT_OBJECT:
3585 copyResultRegister1(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003586 kTypeCategoryRef, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003587 break;
3588
3589 case OP_MOVE_EXCEPTION:
3590 /*
3591 * This statement can only appear as the first instruction in an
3592 * exception handler (though not all exception handlers need to
3593 * have one of these). We verify that as part of extracting the
3594 * exception type from the catch block list.
3595 *
3596 * "resClass" will hold the closest common superclass of all
3597 * exceptions that can be handled here.
3598 */
Andy McFadden62a75162009-04-17 17:23:37 -07003599 resClass = getCaughtExceptionType(meth, insnIdx, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003600 if (resClass == NULL) {
Andy McFadden62a75162009-04-17 17:23:37 -07003601 assert(!VERIFY_OK(failure));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003602 } else {
Andy McFaddend3250112010-11-03 14:32:42 -07003603 setRegisterType(workRegs, decInsn.vA, regTypeFromClass(resClass));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003604 }
3605 break;
3606
3607 case OP_RETURN_VOID:
Andy McFadden62a75162009-04-17 17:23:37 -07003608 if (!checkConstructorReturn(meth, workRegs, insnRegCount)) {
3609 failure = VERIFY_ERROR_GENERIC;
3610 } else if (getMethodReturnType(meth) != kRegTypeUnknown) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003611 LOG_VFY("VFY: return-void not expected\n");
Andy McFadden62a75162009-04-17 17:23:37 -07003612 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003613 }
3614 break;
3615 case OP_RETURN:
Andy McFadden62a75162009-04-17 17:23:37 -07003616 if (!checkConstructorReturn(meth, workRegs, insnRegCount)) {
3617 failure = VERIFY_ERROR_GENERIC;
3618 } else {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003619 /* check the method signature */
3620 RegType returnType = getMethodReturnType(meth);
Andy McFadden62a75162009-04-17 17:23:37 -07003621 checkTypeCategory(returnType, kTypeCategory1nr, &failure);
3622 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003623 LOG_VFY("VFY: return-32 not expected\n");
3624
3625 /* check the register contents */
Andy McFaddend3250112010-11-03 14:32:42 -07003626 returnType = getRegisterType(workRegs, decInsn.vA);
Andy McFadden62a75162009-04-17 17:23:37 -07003627 checkTypeCategory(returnType, kTypeCategory1nr, &failure);
3628 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003629 LOG_VFY("VFY: return-32 on invalid register v%d\n", decInsn.vA);
3630 }
3631 break;
3632 case OP_RETURN_WIDE:
Andy McFadden62a75162009-04-17 17:23:37 -07003633 if (!checkConstructorReturn(meth, workRegs, insnRegCount)) {
3634 failure = VERIFY_ERROR_GENERIC;
3635 } else {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003636 RegType returnType, returnTypeHi;
3637
3638 /* check the method signature */
3639 returnType = getMethodReturnType(meth);
Andy McFadden62a75162009-04-17 17:23:37 -07003640 checkTypeCategory(returnType, kTypeCategory2, &failure);
3641 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003642 LOG_VFY("VFY: return-wide not expected\n");
3643
3644 /* check the register contents */
Andy McFaddend3250112010-11-03 14:32:42 -07003645 returnType = getRegisterType(workRegs, decInsn.vA);
3646 returnTypeHi = getRegisterType(workRegs, decInsn.vA +1);
3647 checkTypeCategory(returnType, kTypeCategory2, &failure);
3648 checkWidePair(returnType, returnTypeHi, &failure);
Andy McFadden62a75162009-04-17 17:23:37 -07003649 if (!VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003650 LOG_VFY("VFY: return-wide on invalid register pair v%d\n",
3651 decInsn.vA);
3652 }
3653 }
3654 break;
3655 case OP_RETURN_OBJECT:
Andy McFadden62a75162009-04-17 17:23:37 -07003656 if (!checkConstructorReturn(meth, workRegs, insnRegCount)) {
3657 failure = VERIFY_ERROR_GENERIC;
3658 } else {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003659 RegType returnType = getMethodReturnType(meth);
Andy McFadden62a75162009-04-17 17:23:37 -07003660 checkTypeCategory(returnType, kTypeCategoryRef, &failure);
3661 if (!VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003662 LOG_VFY("VFY: return-object not expected\n");
3663 break;
3664 }
3665
3666 /* returnType is the *expected* return type, not register value */
3667 assert(returnType != kRegTypeZero);
3668 assert(!regTypeIsUninitReference(returnType));
3669
3670 /*
3671 * Verify that the reference in vAA is an instance of the type
3672 * in "returnType". The Zero type is allowed here. If the
3673 * method is declared to return an interface, then any
3674 * initialized reference is acceptable.
3675 *
3676 * Note getClassFromRegister fails if the register holds an
3677 * uninitialized reference, so we do not allow them to be
3678 * returned.
3679 */
3680 ClassObject* declClass;
3681
3682 declClass = regTypeInitializedReferenceToClass(returnType);
Andy McFaddend3250112010-11-03 14:32:42 -07003683 resClass = getClassFromRegister(workRegs, decInsn.vA, &failure);
Andy McFadden62a75162009-04-17 17:23:37 -07003684 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003685 break;
3686 if (resClass != NULL) {
3687 if (!dvmIsInterfaceClass(declClass) &&
3688 !dvmInstanceof(resClass, declClass))
3689 {
Andy McFadden86c86432009-05-27 14:40:12 -07003690 LOG_VFY("VFY: returning %s (cl=%p), declared %s (cl=%p)\n",
3691 resClass->descriptor, resClass->classLoader,
3692 declClass->descriptor, declClass->classLoader);
Andy McFadden62a75162009-04-17 17:23:37 -07003693 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003694 break;
3695 }
3696 }
3697 }
3698 break;
3699
3700 case OP_CONST_4:
3701 case OP_CONST_16:
3702 case OP_CONST:
3703 /* could be boolean, int, float, or a null reference */
Andy McFaddend3250112010-11-03 14:32:42 -07003704 setRegisterType(workRegs, decInsn.vA,
Andy McFadden470cbbb2010-11-04 16:31:37 -07003705 determineCat1Const((s4)decInsn.vB));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003706 break;
3707 case OP_CONST_HIGH16:
3708 /* could be boolean, int, float, or a null reference */
Andy McFaddend3250112010-11-03 14:32:42 -07003709 setRegisterType(workRegs, decInsn.vA,
Andy McFadden470cbbb2010-11-04 16:31:37 -07003710 determineCat1Const((s4) decInsn.vB << 16));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003711 break;
3712 case OP_CONST_WIDE_16:
3713 case OP_CONST_WIDE_32:
3714 case OP_CONST_WIDE:
3715 case OP_CONST_WIDE_HIGH16:
3716 /* could be long or double; default to long and allow conversion */
Andy McFaddend3250112010-11-03 14:32:42 -07003717 setRegisterType(workRegs, decInsn.vA, kRegTypeLongLo);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003718 break;
3719 case OP_CONST_STRING:
3720 case OP_CONST_STRING_JUMBO:
3721 assert(gDvm.classJavaLangString != NULL);
Andy McFaddend3250112010-11-03 14:32:42 -07003722 setRegisterType(workRegs, decInsn.vA,
3723 regTypeFromClass(gDvm.classJavaLangString));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003724 break;
3725 case OP_CONST_CLASS:
3726 assert(gDvm.classJavaLangClass != NULL);
3727 /* make sure we can resolve the class; access check is important */
Andy McFadden62a75162009-04-17 17:23:37 -07003728 resClass = dvmOptResolveClass(meth->clazz, decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003729 if (resClass == NULL) {
3730 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vB);
3731 dvmLogUnableToResolveClass(badClassDesc, meth);
3732 LOG_VFY("VFY: unable to resolve const-class %d (%s) in %s\n",
3733 decInsn.vB, badClassDesc, meth->clazz->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003734 assert(failure != VERIFY_ERROR_GENERIC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003735 } else {
Andy McFaddend3250112010-11-03 14:32:42 -07003736 setRegisterType(workRegs, decInsn.vA,
3737 regTypeFromClass(gDvm.classJavaLangClass));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003738 }
3739 break;
3740
3741 case OP_MONITOR_ENTER:
3742 case OP_MONITOR_EXIT:
Andy McFaddend3250112010-11-03 14:32:42 -07003743 tmpType = getRegisterType(workRegs, decInsn.vA);
3744 if (!regTypeIsReference(tmpType)) {
3745 LOG_VFY("VFY: monitor op on non-object\n");
3746 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003747 }
3748 break;
3749
3750 case OP_CHECK_CAST:
3751 /*
3752 * If this instruction succeeds, we will promote register vA to
3753 * the type in vB. (This could be a demotion -- not expected, so
3754 * we don't try to address it.)
3755 *
3756 * If it fails, an exception is thrown, which we deal with later
3757 * by ignoring the update to decInsn.vA when branching to a handler.
3758 */
Andy McFadden62a75162009-04-17 17:23:37 -07003759 resClass = dvmOptResolveClass(meth->clazz, decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003760 if (resClass == NULL) {
3761 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vB);
3762 dvmLogUnableToResolveClass(badClassDesc, meth);
3763 LOG_VFY("VFY: unable to resolve check-cast %d (%s) in %s\n",
3764 decInsn.vB, badClassDesc, meth->clazz->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003765 assert(failure != VERIFY_ERROR_GENERIC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003766 } else {
3767 RegType origType;
3768
Andy McFaddend3250112010-11-03 14:32:42 -07003769 origType = getRegisterType(workRegs, decInsn.vA);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003770 if (!regTypeIsReference(origType)) {
3771 LOG_VFY("VFY: check-cast on non-reference in v%u\n",decInsn.vA);
Andy McFadden62a75162009-04-17 17:23:37 -07003772 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003773 break;
3774 }
Andy McFaddend3250112010-11-03 14:32:42 -07003775 setRegisterType(workRegs, decInsn.vA, regTypeFromClass(resClass));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003776 }
3777 break;
3778 case OP_INSTANCE_OF:
3779 /* make sure we're checking a reference type */
Andy McFaddend3250112010-11-03 14:32:42 -07003780 tmpType = getRegisterType(workRegs, decInsn.vB);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003781 if (!regTypeIsReference(tmpType)) {
3782 LOG_VFY("VFY: vB not a reference (%d)\n", tmpType);
Andy McFadden62a75162009-04-17 17:23:37 -07003783 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003784 break;
3785 }
3786
3787 /* make sure we can resolve the class; access check is important */
Andy McFadden62a75162009-04-17 17:23:37 -07003788 resClass = dvmOptResolveClass(meth->clazz, decInsn.vC, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003789 if (resClass == NULL) {
3790 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vC);
3791 dvmLogUnableToResolveClass(badClassDesc, meth);
3792 LOG_VFY("VFY: unable to resolve instanceof %d (%s) in %s\n",
3793 decInsn.vC, badClassDesc, meth->clazz->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003794 assert(failure != VERIFY_ERROR_GENERIC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003795 } else {
3796 /* result is boolean */
Andy McFaddend3250112010-11-03 14:32:42 -07003797 setRegisterType(workRegs, decInsn.vA, kRegTypeBoolean);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003798 }
3799 break;
3800
3801 case OP_ARRAY_LENGTH:
Andy McFaddend3250112010-11-03 14:32:42 -07003802 resClass = getClassFromRegister(workRegs, decInsn.vB, &failure);
Andy McFadden62a75162009-04-17 17:23:37 -07003803 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003804 break;
3805 if (resClass != NULL && !dvmIsArrayClass(resClass)) {
3806 LOG_VFY("VFY: array-length on non-array\n");
Andy McFadden62a75162009-04-17 17:23:37 -07003807 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003808 break;
3809 }
Andy McFaddend3250112010-11-03 14:32:42 -07003810 setRegisterType(workRegs, decInsn.vA, kRegTypeInteger);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003811 break;
3812
3813 case OP_NEW_INSTANCE:
Andy McFadden62a75162009-04-17 17:23:37 -07003814 resClass = dvmOptResolveClass(meth->clazz, decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003815 if (resClass == NULL) {
3816 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vB);
3817 dvmLogUnableToResolveClass(badClassDesc, meth);
3818 LOG_VFY("VFY: unable to resolve new-instance %d (%s) in %s\n",
3819 decInsn.vB, badClassDesc, meth->clazz->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003820 assert(failure != VERIFY_ERROR_GENERIC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003821 } else {
3822 RegType uninitType;
3823
Andy McFaddenb51ea112009-05-08 16:50:17 -07003824 /* can't create an instance of an interface or abstract class */
3825 if (dvmIsAbstractClass(resClass) || dvmIsInterfaceClass(resClass)) {
3826 LOG_VFY("VFY: new-instance on interface or abstract class %s\n",
3827 resClass->descriptor);
3828 failure = VERIFY_ERROR_INSTANTIATION;
3829 break;
3830 }
3831
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003832 /* add resolved class to uninit map if not already there */
3833 int uidx = dvmSetUninitInstance(uninitMap, insnIdx, resClass);
3834 assert(uidx >= 0);
3835 uninitType = regTypeFromUninitIndex(uidx);
3836
3837 /*
3838 * Any registers holding previous allocations from this address
3839 * that have not yet been initialized must be marked invalid.
3840 */
3841 markUninitRefsAsInvalid(workRegs, insnRegCount, uninitMap,
3842 uninitType);
3843
3844 /* add the new uninitialized reference to the register ste */
Andy McFaddend3250112010-11-03 14:32:42 -07003845 setRegisterType(workRegs, decInsn.vA, uninitType);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003846 }
3847 break;
3848 case OP_NEW_ARRAY:
Andy McFadden62a75162009-04-17 17:23:37 -07003849 resClass = dvmOptResolveClass(meth->clazz, decInsn.vC, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003850 if (resClass == NULL) {
3851 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vC);
3852 dvmLogUnableToResolveClass(badClassDesc, meth);
3853 LOG_VFY("VFY: unable to resolve new-array %d (%s) in %s\n",
3854 decInsn.vC, badClassDesc, meth->clazz->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003855 assert(failure != VERIFY_ERROR_GENERIC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003856 } else if (!dvmIsArrayClass(resClass)) {
3857 LOG_VFY("VFY: new-array on non-array class\n");
Andy McFadden62a75162009-04-17 17:23:37 -07003858 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003859 } else {
3860 /* make sure "size" register is valid type */
Andy McFaddend3250112010-11-03 14:32:42 -07003861 verifyRegisterType(workRegs, decInsn.vB, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003862 /* set register type to array class */
Andy McFaddend3250112010-11-03 14:32:42 -07003863 setRegisterType(workRegs, decInsn.vA, regTypeFromClass(resClass));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003864 }
3865 break;
3866 case OP_FILLED_NEW_ARRAY:
3867 case OP_FILLED_NEW_ARRAY_RANGE:
Andy McFadden62a75162009-04-17 17:23:37 -07003868 resClass = dvmOptResolveClass(meth->clazz, decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003869 if (resClass == NULL) {
3870 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vB);
3871 dvmLogUnableToResolveClass(badClassDesc, meth);
3872 LOG_VFY("VFY: unable to resolve filled-array %d (%s) in %s\n",
3873 decInsn.vB, badClassDesc, meth->clazz->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003874 assert(failure != VERIFY_ERROR_GENERIC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003875 } else if (!dvmIsArrayClass(resClass)) {
3876 LOG_VFY("VFY: filled-new-array on non-array class\n");
Andy McFadden62a75162009-04-17 17:23:37 -07003877 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003878 } else {
3879 bool isRange = (decInsn.opCode == OP_FILLED_NEW_ARRAY_RANGE);
3880
3881 /* check the arguments to the instruction */
Andy McFaddend3250112010-11-03 14:32:42 -07003882 verifyFilledNewArrayRegs(meth, workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07003883 resClass, isRange, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003884 /* filled-array result goes into "result" register */
3885 setResultRegisterType(workRegs, insnRegCount,
Andy McFaddend3250112010-11-03 14:32:42 -07003886 regTypeFromClass(resClass));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003887 justSetResult = true;
3888 }
3889 break;
3890
3891 case OP_CMPL_FLOAT:
3892 case OP_CMPG_FLOAT:
Andy McFaddend3250112010-11-03 14:32:42 -07003893 verifyRegisterType(workRegs, decInsn.vB, kRegTypeFloat, &failure);
3894 verifyRegisterType(workRegs, decInsn.vC, kRegTypeFloat, &failure);
3895 setRegisterType(workRegs, decInsn.vA, kRegTypeBoolean);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003896 break;
3897 case OP_CMPL_DOUBLE:
3898 case OP_CMPG_DOUBLE:
Andy McFaddend3250112010-11-03 14:32:42 -07003899 verifyRegisterType(workRegs, decInsn.vB, kRegTypeDoubleLo, &failure);
3900 verifyRegisterType(workRegs, decInsn.vC, kRegTypeDoubleLo, &failure);
3901 setRegisterType(workRegs, decInsn.vA, kRegTypeBoolean);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003902 break;
3903 case OP_CMP_LONG:
Andy McFaddend3250112010-11-03 14:32:42 -07003904 verifyRegisterType(workRegs, decInsn.vB, kRegTypeLongLo, &failure);
3905 verifyRegisterType(workRegs, decInsn.vC, kRegTypeLongLo, &failure);
3906 setRegisterType(workRegs, decInsn.vA, kRegTypeBoolean);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003907 break;
3908
3909 case OP_THROW:
Andy McFaddend3250112010-11-03 14:32:42 -07003910 resClass = getClassFromRegister(workRegs, decInsn.vA, &failure);
Andy McFadden62a75162009-04-17 17:23:37 -07003911 if (VERIFY_OK(failure) && resClass != NULL) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003912 if (!dvmInstanceof(resClass, gDvm.classJavaLangThrowable)) {
3913 LOG_VFY("VFY: thrown class %s not instanceof Throwable\n",
3914 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003915 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003916 }
3917 }
3918 break;
3919
3920 case OP_GOTO:
3921 case OP_GOTO_16:
3922 case OP_GOTO_32:
3923 /* no effect on or use of registers */
3924 break;
3925
3926 case OP_PACKED_SWITCH:
3927 case OP_SPARSE_SWITCH:
3928 /* verify that vAA is an integer, or can be converted to one */
Andy McFaddend3250112010-11-03 14:32:42 -07003929 verifyRegisterType(workRegs, decInsn.vA, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003930 break;
3931
3932 case OP_FILL_ARRAY_DATA:
3933 {
3934 RegType valueType;
3935 const u2 *arrayData;
3936 u2 elemWidth;
3937
3938 /* Similar to the verification done for APUT */
Andy McFaddend3250112010-11-03 14:32:42 -07003939 resClass = getClassFromRegister(workRegs, decInsn.vA, &failure);
Andy McFadden62a75162009-04-17 17:23:37 -07003940 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003941 break;
3942
3943 /* resClass can be null if the reg type is Zero */
3944 if (resClass == NULL)
3945 break;
3946
3947 if (!dvmIsArrayClass(resClass) || resClass->arrayDim != 1 ||
3948 resClass->elementClass->primitiveType == PRIM_NOT ||
3949 resClass->elementClass->primitiveType == PRIM_VOID)
3950 {
3951 LOG_VFY("VFY: invalid fill-array-data on %s\n",
3952 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003953 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003954 break;
3955 }
3956
3957 valueType = primitiveTypeToRegType(
3958 resClass->elementClass->primitiveType);
3959 assert(valueType != kRegTypeUnknown);
3960
3961 /*
3962 * Now verify if the element width in the table matches the element
3963 * width declared in the array
3964 */
3965 arrayData = insns + (insns[1] | (((s4)insns[2]) << 16));
3966 if (arrayData[0] != kArrayDataSignature) {
3967 LOG_VFY("VFY: invalid magic for array-data\n");
Andy McFadden62a75162009-04-17 17:23:37 -07003968 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003969 break;
3970 }
3971
3972 switch (resClass->elementClass->primitiveType) {
3973 case PRIM_BOOLEAN:
3974 case PRIM_BYTE:
3975 elemWidth = 1;
3976 break;
3977 case PRIM_CHAR:
3978 case PRIM_SHORT:
3979 elemWidth = 2;
3980 break;
3981 case PRIM_FLOAT:
3982 case PRIM_INT:
3983 elemWidth = 4;
3984 break;
3985 case PRIM_DOUBLE:
3986 case PRIM_LONG:
3987 elemWidth = 8;
3988 break;
3989 default:
3990 elemWidth = 0;
3991 break;
3992 }
3993
3994 /*
3995 * Since we don't compress the data in Dex, expect to see equal
3996 * width of data stored in the table and expected from the array
3997 * class.
3998 */
3999 if (arrayData[1] != elemWidth) {
4000 LOG_VFY("VFY: array-data size mismatch (%d vs %d)\n",
4001 arrayData[1], elemWidth);
Andy McFadden62a75162009-04-17 17:23:37 -07004002 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004003 }
4004 }
4005 break;
4006
4007 case OP_IF_EQ:
4008 case OP_IF_NE:
4009 {
4010 RegType type1, type2;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004011
Andy McFaddend3250112010-11-03 14:32:42 -07004012 type1 = getRegisterType(workRegs, decInsn.vA);
4013 type2 = getRegisterType(workRegs, decInsn.vB);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004014
4015 /* both references? */
4016 if (regTypeIsReference(type1) && regTypeIsReference(type2))
4017 break;
4018
4019 /* both category-1nr? */
Andy McFadden62a75162009-04-17 17:23:37 -07004020 checkTypeCategory(type1, kTypeCategory1nr, &failure);
4021 checkTypeCategory(type2, kTypeCategory1nr, &failure);
4022 if (!VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004023 LOG_VFY("VFY: args to if-eq/if-ne must both be refs or cat1\n");
4024 break;
4025 }
4026 }
4027 break;
4028 case OP_IF_LT:
4029 case OP_IF_GE:
4030 case OP_IF_GT:
4031 case OP_IF_LE:
Andy McFaddend3250112010-11-03 14:32:42 -07004032 tmpType = getRegisterType(workRegs, decInsn.vA);
Andy McFadden62a75162009-04-17 17:23:37 -07004033 checkTypeCategory(tmpType, kTypeCategory1nr, &failure);
4034 if (!VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004035 LOG_VFY("VFY: args to 'if' must be cat-1nr\n");
4036 break;
4037 }
Andy McFaddend3250112010-11-03 14:32:42 -07004038 tmpType = getRegisterType(workRegs, decInsn.vB);
Andy McFadden62a75162009-04-17 17:23:37 -07004039 checkTypeCategory(tmpType, kTypeCategory1nr, &failure);
4040 if (!VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004041 LOG_VFY("VFY: args to 'if' must be cat-1nr\n");
4042 break;
4043 }
4044 break;
4045 case OP_IF_EQZ:
4046 case OP_IF_NEZ:
Andy McFaddend3250112010-11-03 14:32:42 -07004047 tmpType = getRegisterType(workRegs, decInsn.vA);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004048 if (regTypeIsReference(tmpType))
4049 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004050 checkTypeCategory(tmpType, kTypeCategory1nr, &failure);
4051 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004052 LOG_VFY("VFY: expected cat-1 arg to if\n");
4053 break;
4054 case OP_IF_LTZ:
4055 case OP_IF_GEZ:
4056 case OP_IF_GTZ:
4057 case OP_IF_LEZ:
Andy McFaddend3250112010-11-03 14:32:42 -07004058 tmpType = getRegisterType(workRegs, decInsn.vA);
Andy McFadden62a75162009-04-17 17:23:37 -07004059 checkTypeCategory(tmpType, kTypeCategory1nr, &failure);
4060 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004061 LOG_VFY("VFY: expected cat-1 arg to if\n");
4062 break;
4063
4064 case OP_AGET:
4065 tmpType = kRegTypeInteger;
4066 goto aget_1nr_common;
4067 case OP_AGET_BOOLEAN:
4068 tmpType = kRegTypeBoolean;
4069 goto aget_1nr_common;
4070 case OP_AGET_BYTE:
4071 tmpType = kRegTypeByte;
4072 goto aget_1nr_common;
4073 case OP_AGET_CHAR:
4074 tmpType = kRegTypeChar;
4075 goto aget_1nr_common;
4076 case OP_AGET_SHORT:
4077 tmpType = kRegTypeShort;
4078 goto aget_1nr_common;
4079aget_1nr_common:
4080 {
4081 RegType srcType, indexType;
4082
Andy McFaddend3250112010-11-03 14:32:42 -07004083 indexType = getRegisterType(workRegs, decInsn.vC);
Andy McFadden62a75162009-04-17 17:23:37 -07004084 checkArrayIndexType(meth, indexType, &failure);
4085 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004086 break;
4087
Andy McFaddend3250112010-11-03 14:32:42 -07004088 resClass = getClassFromRegister(workRegs, decInsn.vB, &failure);
Andy McFadden62a75162009-04-17 17:23:37 -07004089 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004090 break;
4091 if (resClass != NULL) {
4092 /* verify the class */
4093 if (!dvmIsArrayClass(resClass) || resClass->arrayDim != 1 ||
4094 resClass->elementClass->primitiveType == PRIM_NOT)
4095 {
4096 LOG_VFY("VFY: invalid aget-1nr target %s\n",
4097 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004098 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004099 break;
4100 }
4101
4102 /* make sure array type matches instruction */
4103 srcType = primitiveTypeToRegType(
4104 resClass->elementClass->primitiveType);
4105
4106 if (!checkFieldArrayStore1nr(tmpType, srcType)) {
4107 LOG_VFY("VFY: invalid aget-1nr, array type=%d with"
4108 " inst type=%d (on %s)\n",
4109 srcType, tmpType, resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004110 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004111 break;
4112 }
4113
4114 }
Andy McFaddend3250112010-11-03 14:32:42 -07004115 setRegisterType(workRegs, decInsn.vA, tmpType);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004116 }
4117 break;
4118
4119 case OP_AGET_WIDE:
4120 {
4121 RegType dstType, indexType;
4122
Andy McFaddend3250112010-11-03 14:32:42 -07004123 indexType = getRegisterType(workRegs, decInsn.vC);
Andy McFadden62a75162009-04-17 17:23:37 -07004124 checkArrayIndexType(meth, indexType, &failure);
4125 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004126 break;
4127
Andy McFaddend3250112010-11-03 14:32:42 -07004128 resClass = getClassFromRegister(workRegs, decInsn.vB, &failure);
Andy McFadden62a75162009-04-17 17:23:37 -07004129 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004130 break;
4131 if (resClass != NULL) {
4132 /* verify the class */
4133 if (!dvmIsArrayClass(resClass) || resClass->arrayDim != 1 ||
4134 resClass->elementClass->primitiveType == PRIM_NOT)
4135 {
4136 LOG_VFY("VFY: invalid aget-wide target %s\n",
4137 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004138 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004139 break;
4140 }
4141
4142 /* try to refine "dstType" */
4143 switch (resClass->elementClass->primitiveType) {
4144 case PRIM_LONG:
4145 dstType = kRegTypeLongLo;
4146 break;
4147 case PRIM_DOUBLE:
4148 dstType = kRegTypeDoubleLo;
4149 break;
4150 default:
4151 LOG_VFY("VFY: invalid aget-wide on %s\n",
4152 resClass->descriptor);
4153 dstType = kRegTypeUnknown;
Andy McFadden62a75162009-04-17 17:23:37 -07004154 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004155 break;
4156 }
4157 } else {
4158 /*
4159 * Null array ref; this code path will fail at runtime. We
4160 * know this is either long or double, and we don't really
4161 * discriminate between those during verification, so we
4162 * call it a long.
4163 */
4164 dstType = kRegTypeLongLo;
4165 }
Andy McFaddend3250112010-11-03 14:32:42 -07004166 setRegisterType(workRegs, decInsn.vA, dstType);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004167 }
4168 break;
4169
4170 case OP_AGET_OBJECT:
4171 {
4172 RegType dstType, indexType;
4173
Andy McFaddend3250112010-11-03 14:32:42 -07004174 indexType = getRegisterType(workRegs, decInsn.vC);
Andy McFadden62a75162009-04-17 17:23:37 -07004175 checkArrayIndexType(meth, indexType, &failure);
4176 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004177 break;
4178
4179 /* get the class of the array we're pulling an object from */
Andy McFaddend3250112010-11-03 14:32:42 -07004180 resClass = getClassFromRegister(workRegs, decInsn.vB, &failure);
Andy McFadden62a75162009-04-17 17:23:37 -07004181 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004182 break;
4183 if (resClass != NULL) {
4184 ClassObject* elementClass;
4185
4186 assert(resClass != NULL);
4187 if (!dvmIsArrayClass(resClass)) {
4188 LOG_VFY("VFY: aget-object on non-array class\n");
Andy McFadden62a75162009-04-17 17:23:37 -07004189 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004190 break;
4191 }
4192 assert(resClass->elementClass != NULL);
4193
4194 /*
4195 * Find the element class. resClass->elementClass indicates
4196 * the basic type, which won't be what we want for a
4197 * multi-dimensional array.
4198 */
4199 if (resClass->descriptor[1] == '[') {
4200 assert(resClass->arrayDim > 1);
4201 elementClass = dvmFindArrayClass(&resClass->descriptor[1],
4202 resClass->classLoader);
4203 } else if (resClass->descriptor[1] == 'L') {
4204 assert(resClass->arrayDim == 1);
4205 elementClass = resClass->elementClass;
4206 } else {
4207 LOG_VFY("VFY: aget-object on non-ref array class (%s)\n",
4208 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004209 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004210 break;
4211 }
4212
4213 dstType = regTypeFromClass(elementClass);
4214 } else {
4215 /*
4216 * The array reference is NULL, so the current code path will
4217 * throw an exception. For proper merging with later code
4218 * paths, and correct handling of "if-eqz" tests on the
4219 * result of the array get, we want to treat this as a null
4220 * reference.
4221 */
4222 dstType = kRegTypeZero;
4223 }
Andy McFaddend3250112010-11-03 14:32:42 -07004224 setRegisterType(workRegs, decInsn.vA, dstType);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004225 }
4226 break;
4227 case OP_APUT:
4228 tmpType = kRegTypeInteger;
4229 goto aput_1nr_common;
4230 case OP_APUT_BOOLEAN:
4231 tmpType = kRegTypeBoolean;
4232 goto aput_1nr_common;
4233 case OP_APUT_BYTE:
4234 tmpType = kRegTypeByte;
4235 goto aput_1nr_common;
4236 case OP_APUT_CHAR:
4237 tmpType = kRegTypeChar;
4238 goto aput_1nr_common;
4239 case OP_APUT_SHORT:
4240 tmpType = kRegTypeShort;
4241 goto aput_1nr_common;
4242aput_1nr_common:
4243 {
4244 RegType srcType, dstType, indexType;
4245
Andy McFaddend3250112010-11-03 14:32:42 -07004246 indexType = getRegisterType(workRegs, decInsn.vC);
Andy McFadden62a75162009-04-17 17:23:37 -07004247 checkArrayIndexType(meth, indexType, &failure);
4248 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004249 break;
4250
4251 /* make sure the source register has the correct type */
Andy McFaddend3250112010-11-03 14:32:42 -07004252 srcType = getRegisterType(workRegs, decInsn.vA);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004253 if (!canConvertTo1nr(srcType, tmpType)) {
4254 LOG_VFY("VFY: invalid reg type %d on aput instr (need %d)\n",
4255 srcType, tmpType);
Andy McFadden62a75162009-04-17 17:23:37 -07004256 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004257 break;
4258 }
4259
Andy McFaddend3250112010-11-03 14:32:42 -07004260 resClass = getClassFromRegister(workRegs, decInsn.vB, &failure);
Andy McFadden62a75162009-04-17 17:23:37 -07004261 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004262 break;
4263
4264 /* resClass can be null if the reg type is Zero */
4265 if (resClass == NULL)
4266 break;
4267
4268 if (!dvmIsArrayClass(resClass) || resClass->arrayDim != 1 ||
4269 resClass->elementClass->primitiveType == PRIM_NOT)
4270 {
4271 LOG_VFY("VFY: invalid aput-1nr on %s\n", resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004272 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004273 break;
4274 }
4275
4276 /* verify that instruction matches array */
4277 dstType = primitiveTypeToRegType(
4278 resClass->elementClass->primitiveType);
4279 assert(dstType != kRegTypeUnknown);
4280
4281 if (!checkFieldArrayStore1nr(tmpType, dstType)) {
4282 LOG_VFY("VFY: invalid aput-1nr on %s (inst=%d dst=%d)\n",
4283 resClass->descriptor, tmpType, dstType);
Andy McFadden62a75162009-04-17 17:23:37 -07004284 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004285 break;
4286 }
4287 }
4288 break;
4289 case OP_APUT_WIDE:
Andy McFaddend3250112010-11-03 14:32:42 -07004290 tmpType = getRegisterType(workRegs, decInsn.vC);
Andy McFadden62a75162009-04-17 17:23:37 -07004291 checkArrayIndexType(meth, tmpType, &failure);
4292 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004293 break;
4294
Andy McFaddend3250112010-11-03 14:32:42 -07004295 tmpType = getRegisterType(workRegs, decInsn.vA);
4296 {
4297 RegType typeHi = getRegisterType(workRegs, decInsn.vA+1);
Andy McFadden62a75162009-04-17 17:23:37 -07004298 checkTypeCategory(tmpType, kTypeCategory2, &failure);
4299 checkWidePair(tmpType, typeHi, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004300 }
Andy McFadden62a75162009-04-17 17:23:37 -07004301 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004302 break;
4303
Andy McFaddend3250112010-11-03 14:32:42 -07004304 resClass = getClassFromRegister(workRegs, decInsn.vB, &failure);
Andy McFadden62a75162009-04-17 17:23:37 -07004305 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004306 break;
4307 if (resClass != NULL) {
4308 /* verify the class and try to refine "dstType" */
4309 if (!dvmIsArrayClass(resClass) || resClass->arrayDim != 1 ||
4310 resClass->elementClass->primitiveType == PRIM_NOT)
4311 {
4312 LOG_VFY("VFY: invalid aput-wide on %s\n",
4313 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004314 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004315 break;
4316 }
4317
4318 switch (resClass->elementClass->primitiveType) {
4319 case PRIM_LONG:
4320 case PRIM_DOUBLE:
4321 /* these are okay */
4322 break;
4323 default:
4324 LOG_VFY("VFY: invalid aput-wide on %s\n",
4325 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004326 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004327 break;
4328 }
4329 }
4330 break;
4331 case OP_APUT_OBJECT:
Andy McFaddend3250112010-11-03 14:32:42 -07004332 tmpType = getRegisterType(workRegs, decInsn.vC);
Andy McFadden62a75162009-04-17 17:23:37 -07004333 checkArrayIndexType(meth, tmpType, &failure);
4334 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004335 break;
4336
4337 /* get the ref we're storing; Zero is okay, Uninit is not */
Andy McFaddend3250112010-11-03 14:32:42 -07004338 resClass = getClassFromRegister(workRegs, decInsn.vA, &failure);
Andy McFadden62a75162009-04-17 17:23:37 -07004339 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004340 break;
4341 if (resClass != NULL) {
4342 ClassObject* arrayClass;
4343 ClassObject* elementClass;
4344
4345 /*
4346 * Get the array class. If the array ref is null, we won't
4347 * have type information (and we'll crash at runtime with a
4348 * null pointer exception).
4349 */
Andy McFaddend3250112010-11-03 14:32:42 -07004350 arrayClass = getClassFromRegister(workRegs, decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004351
4352 if (arrayClass != NULL) {
4353 /* see if the array holds a compatible type */
4354 if (!dvmIsArrayClass(arrayClass)) {
4355 LOG_VFY("VFY: invalid aput-object on %s\n",
4356 arrayClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004357 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004358 break;
4359 }
4360
4361 /*
4362 * Find the element class. resClass->elementClass indicates
4363 * the basic type, which won't be what we want for a
4364 * multi-dimensional array.
4365 *
4366 * All we want to check here is that the element type is a
4367 * reference class. We *don't* check instanceof here, because
4368 * you can still put a String into a String[] after the latter
4369 * has been cast to an Object[].
4370 */
4371 if (arrayClass->descriptor[1] == '[') {
4372 assert(arrayClass->arrayDim > 1);
4373 elementClass = dvmFindArrayClass(&arrayClass->descriptor[1],
4374 arrayClass->classLoader);
4375 } else {
4376 assert(arrayClass->arrayDim == 1);
4377 elementClass = arrayClass->elementClass;
4378 }
4379 if (elementClass->primitiveType != PRIM_NOT) {
4380 LOG_VFY("VFY: invalid aput-object of %s into %s\n",
4381 resClass->descriptor, arrayClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004382 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004383 break;
4384 }
4385 }
4386 }
4387 break;
4388
4389 case OP_IGET:
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07004390 case OP_IGET_VOLATILE:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004391 tmpType = kRegTypeInteger;
4392 goto iget_1nr_common;
4393 case OP_IGET_BOOLEAN:
4394 tmpType = kRegTypeBoolean;
4395 goto iget_1nr_common;
4396 case OP_IGET_BYTE:
4397 tmpType = kRegTypeByte;
4398 goto iget_1nr_common;
4399 case OP_IGET_CHAR:
4400 tmpType = kRegTypeChar;
4401 goto iget_1nr_common;
4402 case OP_IGET_SHORT:
4403 tmpType = kRegTypeShort;
4404 goto iget_1nr_common;
4405iget_1nr_common:
4406 {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004407 InstField* instField;
4408 RegType objType, fieldType;
4409
Andy McFaddend3250112010-11-03 14:32:42 -07004410 objType = getRegisterType(workRegs, decInsn.vB);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004411 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004412 &failure);
4413 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004414 break;
4415
4416 /* make sure the field's type is compatible with expectation */
4417 fieldType = primSigCharToRegType(instField->field.signature[0]);
4418 if (fieldType == kRegTypeUnknown ||
4419 !checkFieldArrayStore1nr(tmpType, fieldType))
4420 {
4421 LOG_VFY("VFY: invalid iget-1nr of %s.%s (inst=%d field=%d)\n",
4422 instField->field.clazz->descriptor,
4423 instField->field.name, tmpType, fieldType);
Andy McFadden62a75162009-04-17 17:23:37 -07004424 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004425 break;
4426 }
4427
Andy McFaddend3250112010-11-03 14:32:42 -07004428 setRegisterType(workRegs, decInsn.vA, tmpType);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004429 }
4430 break;
4431 case OP_IGET_WIDE:
Andy McFadden861b3382010-03-05 15:58:31 -08004432 case OP_IGET_WIDE_VOLATILE:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004433 {
4434 RegType dstType;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004435 InstField* instField;
4436 RegType objType;
4437
Andy McFaddend3250112010-11-03 14:32:42 -07004438 objType = getRegisterType(workRegs, decInsn.vB);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004439 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004440 &failure);
4441 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004442 break;
4443 /* check the type, which should be prim */
4444 switch (instField->field.signature[0]) {
4445 case 'D':
4446 dstType = kRegTypeDoubleLo;
4447 break;
4448 case 'J':
4449 dstType = kRegTypeLongLo;
4450 break;
4451 default:
4452 LOG_VFY("VFY: invalid iget-wide of %s.%s\n",
4453 instField->field.clazz->descriptor,
4454 instField->field.name);
4455 dstType = kRegTypeUnknown;
Andy McFadden62a75162009-04-17 17:23:37 -07004456 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004457 break;
4458 }
Andy McFadden62a75162009-04-17 17:23:37 -07004459 if (VERIFY_OK(failure)) {
Andy McFaddend3250112010-11-03 14:32:42 -07004460 setRegisterType(workRegs, decInsn.vA, dstType);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004461 }
4462 }
4463 break;
4464 case OP_IGET_OBJECT:
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07004465 case OP_IGET_OBJECT_VOLATILE:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004466 {
4467 ClassObject* fieldClass;
4468 InstField* instField;
4469 RegType objType;
4470
Andy McFaddend3250112010-11-03 14:32:42 -07004471 objType = getRegisterType(workRegs, decInsn.vB);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004472 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004473 &failure);
4474 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004475 break;
4476 fieldClass = getFieldClass(meth, &instField->field);
4477 if (fieldClass == NULL) {
4478 /* class not found or primitive type */
4479 LOG_VFY("VFY: unable to recover field class from '%s'\n",
4480 instField->field.signature);
Andy McFadden62a75162009-04-17 17:23:37 -07004481 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004482 break;
4483 }
Andy McFadden62a75162009-04-17 17:23:37 -07004484 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004485 assert(!dvmIsPrimitiveClass(fieldClass));
Andy McFaddend3250112010-11-03 14:32:42 -07004486 setRegisterType(workRegs, decInsn.vA,
4487 regTypeFromClass(fieldClass));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004488 }
4489 }
4490 break;
4491 case OP_IPUT:
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07004492 case OP_IPUT_VOLATILE:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004493 tmpType = kRegTypeInteger;
4494 goto iput_1nr_common;
4495 case OP_IPUT_BOOLEAN:
4496 tmpType = kRegTypeBoolean;
4497 goto iput_1nr_common;
4498 case OP_IPUT_BYTE:
4499 tmpType = kRegTypeByte;
4500 goto iput_1nr_common;
4501 case OP_IPUT_CHAR:
4502 tmpType = kRegTypeChar;
4503 goto iput_1nr_common;
4504 case OP_IPUT_SHORT:
4505 tmpType = kRegTypeShort;
4506 goto iput_1nr_common;
4507iput_1nr_common:
4508 {
4509 RegType srcType, fieldType, objType;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004510 InstField* instField;
4511
Andy McFaddend3250112010-11-03 14:32:42 -07004512 srcType = getRegisterType(workRegs, decInsn.vA);
Andy McFaddenb5f64bc2009-06-10 14:11:07 -07004513
4514 /*
4515 * javac generates synthetic functions that write byte values
4516 * into boolean fields.
4517 */
4518 if (tmpType == kRegTypeBoolean && srcType == kRegTypeByte)
4519 srcType = kRegTypeBoolean;
4520
4521 /* make sure the source register has the correct type */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004522 if (!canConvertTo1nr(srcType, tmpType)) {
4523 LOG_VFY("VFY: invalid reg type %d on iput instr (need %d)\n",
4524 srcType, tmpType);
Andy McFadden62a75162009-04-17 17:23:37 -07004525 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004526 break;
4527 }
4528
Andy McFaddend3250112010-11-03 14:32:42 -07004529 objType = getRegisterType(workRegs, decInsn.vB);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004530 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004531 &failure);
4532 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004533 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004534 checkFinalFieldAccess(meth, &instField->field, &failure);
4535 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004536 break;
4537
4538 /* get type of field we're storing into */
4539 fieldType = primSigCharToRegType(instField->field.signature[0]);
4540 if (fieldType == kRegTypeUnknown ||
4541 !checkFieldArrayStore1nr(tmpType, fieldType))
4542 {
4543 LOG_VFY("VFY: invalid iput-1nr of %s.%s (inst=%d field=%d)\n",
4544 instField->field.clazz->descriptor,
4545 instField->field.name, tmpType, fieldType);
Andy McFadden62a75162009-04-17 17:23:37 -07004546 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004547 break;
4548 }
4549 }
4550 break;
4551 case OP_IPUT_WIDE:
Andy McFadden861b3382010-03-05 15:58:31 -08004552 case OP_IPUT_WIDE_VOLATILE:
Andy McFaddend3250112010-11-03 14:32:42 -07004553 tmpType = getRegisterType(workRegs, decInsn.vA);
4554 {
4555 RegType typeHi = getRegisterType(workRegs, decInsn.vA+1);
Andy McFadden62a75162009-04-17 17:23:37 -07004556 checkTypeCategory(tmpType, kTypeCategory2, &failure);
4557 checkWidePair(tmpType, typeHi, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004558 }
Andy McFadden62a75162009-04-17 17:23:37 -07004559 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004560 InstField* instField;
4561 RegType objType;
4562
Andy McFaddend3250112010-11-03 14:32:42 -07004563 objType = getRegisterType(workRegs, decInsn.vB);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004564 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004565 &failure);
4566 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004567 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004568 checkFinalFieldAccess(meth, &instField->field, &failure);
4569 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004570 break;
4571
4572 /* check the type, which should be prim */
4573 switch (instField->field.signature[0]) {
4574 case 'D':
4575 case 'J':
4576 /* these are okay (and interchangeable) */
4577 break;
4578 default:
4579 LOG_VFY("VFY: invalid iput-wide of %s.%s\n",
4580 instField->field.clazz->descriptor,
4581 instField->field.name);
Andy McFadden62a75162009-04-17 17:23:37 -07004582 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004583 break;
4584 }
4585 }
4586 break;
4587 case OP_IPUT_OBJECT:
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07004588 case OP_IPUT_OBJECT_VOLATILE:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004589 {
4590 ClassObject* fieldClass;
4591 ClassObject* valueClass;
4592 InstField* instField;
4593 RegType objType, valueType;
4594
Andy McFaddend3250112010-11-03 14:32:42 -07004595 objType = getRegisterType(workRegs, decInsn.vB);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004596 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004597 &failure);
4598 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004599 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004600 checkFinalFieldAccess(meth, &instField->field, &failure);
4601 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004602 break;
4603
4604 fieldClass = getFieldClass(meth, &instField->field);
4605 if (fieldClass == NULL) {
4606 LOG_VFY("VFY: unable to recover field class from '%s'\n",
4607 instField->field.signature);
Andy McFadden62a75162009-04-17 17:23:37 -07004608 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004609 break;
4610 }
4611
Andy McFaddend3250112010-11-03 14:32:42 -07004612 valueType = getRegisterType(workRegs, decInsn.vA);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004613 if (!regTypeIsReference(valueType)) {
4614 LOG_VFY("VFY: storing non-ref v%d into ref field '%s' (%s)\n",
4615 decInsn.vA, instField->field.name,
4616 fieldClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004617 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004618 break;
4619 }
4620 if (valueType != kRegTypeZero) {
4621 valueClass = regTypeInitializedReferenceToClass(valueType);
4622 if (valueClass == NULL) {
4623 LOG_VFY("VFY: storing uninit ref v%d into ref field\n",
4624 decInsn.vA);
Andy McFadden62a75162009-04-17 17:23:37 -07004625 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004626 break;
4627 }
4628 /* allow if field is any interface or field is base class */
4629 if (!dvmIsInterfaceClass(fieldClass) &&
4630 !dvmInstanceof(valueClass, fieldClass))
4631 {
4632 LOG_VFY("VFY: storing type '%s' into field type '%s' (%s.%s)\n",
4633 valueClass->descriptor, fieldClass->descriptor,
4634 instField->field.clazz->descriptor,
4635 instField->field.name);
Andy McFadden62a75162009-04-17 17:23:37 -07004636 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004637 break;
4638 }
4639 }
4640 }
4641 break;
4642
4643 case OP_SGET:
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07004644 case OP_SGET_VOLATILE:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004645 tmpType = kRegTypeInteger;
4646 goto sget_1nr_common;
4647 case OP_SGET_BOOLEAN:
4648 tmpType = kRegTypeBoolean;
4649 goto sget_1nr_common;
4650 case OP_SGET_BYTE:
4651 tmpType = kRegTypeByte;
4652 goto sget_1nr_common;
4653 case OP_SGET_CHAR:
4654 tmpType = kRegTypeChar;
4655 goto sget_1nr_common;
4656 case OP_SGET_SHORT:
4657 tmpType = kRegTypeShort;
4658 goto sget_1nr_common;
4659sget_1nr_common:
4660 {
4661 StaticField* staticField;
4662 RegType fieldType;
4663
Andy McFadden62a75162009-04-17 17:23:37 -07004664 staticField = getStaticField(meth, decInsn.vB, &failure);
4665 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004666 break;
4667
4668 /*
4669 * Make sure the field's type is compatible with expectation.
4670 * We can get ourselves into trouble if we mix & match loads
4671 * and stores with different widths, so rather than just checking
4672 * "canConvertTo1nr" we require that the field types have equal
4673 * widths. (We can't generally require an exact type match,
4674 * because e.g. "int" and "float" are interchangeable.)
4675 */
4676 fieldType = primSigCharToRegType(staticField->field.signature[0]);
4677 if (!checkFieldArrayStore1nr(tmpType, fieldType)) {
4678 LOG_VFY("VFY: invalid sget-1nr of %s.%s (inst=%d actual=%d)\n",
4679 staticField->field.clazz->descriptor,
4680 staticField->field.name, tmpType, fieldType);
Andy McFadden62a75162009-04-17 17:23:37 -07004681 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004682 break;
4683 }
4684
Andy McFaddend3250112010-11-03 14:32:42 -07004685 setRegisterType(workRegs, decInsn.vA, tmpType);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004686 }
4687 break;
4688 case OP_SGET_WIDE:
Andy McFadden861b3382010-03-05 15:58:31 -08004689 case OP_SGET_WIDE_VOLATILE:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004690 {
4691 StaticField* staticField;
4692 RegType dstType;
4693
Andy McFadden62a75162009-04-17 17:23:37 -07004694 staticField = getStaticField(meth, decInsn.vB, &failure);
4695 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004696 break;
4697 /* check the type, which should be prim */
4698 switch (staticField->field.signature[0]) {
4699 case 'D':
4700 dstType = kRegTypeDoubleLo;
4701 break;
4702 case 'J':
4703 dstType = kRegTypeLongLo;
4704 break;
4705 default:
4706 LOG_VFY("VFY: invalid sget-wide of %s.%s\n",
4707 staticField->field.clazz->descriptor,
4708 staticField->field.name);
4709 dstType = kRegTypeUnknown;
Andy McFadden62a75162009-04-17 17:23:37 -07004710 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004711 break;
4712 }
Andy McFadden62a75162009-04-17 17:23:37 -07004713 if (VERIFY_OK(failure)) {
Andy McFaddend3250112010-11-03 14:32:42 -07004714 setRegisterType(workRegs, decInsn.vA, dstType);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004715 }
4716 }
4717 break;
4718 case OP_SGET_OBJECT:
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07004719 case OP_SGET_OBJECT_VOLATILE:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004720 {
4721 StaticField* staticField;
4722 ClassObject* fieldClass;
4723
Andy McFadden62a75162009-04-17 17:23:37 -07004724 staticField = getStaticField(meth, decInsn.vB, &failure);
4725 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004726 break;
4727 fieldClass = getFieldClass(meth, &staticField->field);
4728 if (fieldClass == NULL) {
4729 LOG_VFY("VFY: unable to recover field class from '%s'\n",
4730 staticField->field.signature);
Andy McFadden62a75162009-04-17 17:23:37 -07004731 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004732 break;
4733 }
4734 if (dvmIsPrimitiveClass(fieldClass)) {
4735 LOG_VFY("VFY: attempt to get prim field with sget-object\n");
Andy McFadden62a75162009-04-17 17:23:37 -07004736 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004737 break;
4738 }
Andy McFaddend3250112010-11-03 14:32:42 -07004739 setRegisterType(workRegs, decInsn.vA, regTypeFromClass(fieldClass));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004740 }
4741 break;
4742 case OP_SPUT:
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07004743 case OP_SPUT_VOLATILE:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004744 tmpType = kRegTypeInteger;
4745 goto sput_1nr_common;
4746 case OP_SPUT_BOOLEAN:
4747 tmpType = kRegTypeBoolean;
4748 goto sput_1nr_common;
4749 case OP_SPUT_BYTE:
4750 tmpType = kRegTypeByte;
4751 goto sput_1nr_common;
4752 case OP_SPUT_CHAR:
4753 tmpType = kRegTypeChar;
4754 goto sput_1nr_common;
4755 case OP_SPUT_SHORT:
4756 tmpType = kRegTypeShort;
4757 goto sput_1nr_common;
4758sput_1nr_common:
4759 {
4760 RegType srcType, fieldType;
4761 StaticField* staticField;
4762
Andy McFaddend3250112010-11-03 14:32:42 -07004763 srcType = getRegisterType(workRegs, decInsn.vA);
Andy McFaddenb5f64bc2009-06-10 14:11:07 -07004764
4765 /*
4766 * javac generates synthetic functions that write byte values
4767 * into boolean fields.
4768 */
4769 if (tmpType == kRegTypeBoolean && srcType == kRegTypeByte)
4770 srcType = kRegTypeBoolean;
4771
4772 /* make sure the source register has the correct type */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004773 if (!canConvertTo1nr(srcType, tmpType)) {
Andy McFaddenb5f64bc2009-06-10 14:11:07 -07004774 LOG_VFY("VFY: invalid reg type %d on sput instr (need %d)\n",
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004775 srcType, tmpType);
Andy McFadden62a75162009-04-17 17:23:37 -07004776 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004777 break;
4778 }
4779
Andy McFadden62a75162009-04-17 17:23:37 -07004780 staticField = getStaticField(meth, decInsn.vB, &failure);
4781 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004782 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004783 checkFinalFieldAccess(meth, &staticField->field, &failure);
4784 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004785 break;
4786
4787 /*
4788 * Get type of field we're storing into. We know that the
4789 * contents of the register match the instruction, but we also
4790 * need to ensure that the instruction matches the field type.
4791 * Using e.g. sput-short to write into a 32-bit integer field
4792 * can lead to trouble if we do 16-bit writes.
4793 */
4794 fieldType = primSigCharToRegType(staticField->field.signature[0]);
4795 if (!checkFieldArrayStore1nr(tmpType, fieldType)) {
4796 LOG_VFY("VFY: invalid sput-1nr of %s.%s (inst=%d actual=%d)\n",
4797 staticField->field.clazz->descriptor,
4798 staticField->field.name, tmpType, fieldType);
Andy McFadden62a75162009-04-17 17:23:37 -07004799 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004800 break;
4801 }
4802 }
4803 break;
4804 case OP_SPUT_WIDE:
Andy McFadden861b3382010-03-05 15:58:31 -08004805 case OP_SPUT_WIDE_VOLATILE:
Andy McFaddend3250112010-11-03 14:32:42 -07004806 tmpType = getRegisterType(workRegs, decInsn.vA);
4807 {
4808 RegType typeHi = getRegisterType(workRegs, decInsn.vA+1);
Andy McFadden62a75162009-04-17 17:23:37 -07004809 checkTypeCategory(tmpType, kTypeCategory2, &failure);
4810 checkWidePair(tmpType, typeHi, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004811 }
Andy McFadden62a75162009-04-17 17:23:37 -07004812 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004813 StaticField* staticField;
4814
Andy McFadden62a75162009-04-17 17:23:37 -07004815 staticField = getStaticField(meth, decInsn.vB, &failure);
4816 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004817 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004818 checkFinalFieldAccess(meth, &staticField->field, &failure);
4819 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004820 break;
4821
4822 /* check the type, which should be prim */
4823 switch (staticField->field.signature[0]) {
4824 case 'D':
4825 case 'J':
4826 /* these are okay */
4827 break;
4828 default:
4829 LOG_VFY("VFY: invalid sput-wide of %s.%s\n",
4830 staticField->field.clazz->descriptor,
4831 staticField->field.name);
Andy McFadden62a75162009-04-17 17:23:37 -07004832 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004833 break;
4834 }
4835 }
4836 break;
4837 case OP_SPUT_OBJECT:
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07004838 case OP_SPUT_OBJECT_VOLATILE:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004839 {
4840 ClassObject* fieldClass;
4841 ClassObject* valueClass;
4842 StaticField* staticField;
4843 RegType valueType;
4844
Andy McFadden62a75162009-04-17 17:23:37 -07004845 staticField = getStaticField(meth, decInsn.vB, &failure);
4846 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004847 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004848 checkFinalFieldAccess(meth, &staticField->field, &failure);
4849 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004850 break;
4851
4852 fieldClass = getFieldClass(meth, &staticField->field);
4853 if (fieldClass == NULL) {
4854 LOG_VFY("VFY: unable to recover field class from '%s'\n",
4855 staticField->field.signature);
Andy McFadden62a75162009-04-17 17:23:37 -07004856 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004857 break;
4858 }
4859
Andy McFaddend3250112010-11-03 14:32:42 -07004860 valueType = getRegisterType(workRegs, decInsn.vA);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004861 if (!regTypeIsReference(valueType)) {
4862 LOG_VFY("VFY: storing non-ref v%d into ref field '%s' (%s)\n",
4863 decInsn.vA, staticField->field.name,
4864 fieldClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004865 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004866 break;
4867 }
4868 if (valueType != kRegTypeZero) {
4869 valueClass = regTypeInitializedReferenceToClass(valueType);
4870 if (valueClass == NULL) {
4871 LOG_VFY("VFY: storing uninit ref v%d into ref field\n",
4872 decInsn.vA);
Andy McFadden62a75162009-04-17 17:23:37 -07004873 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004874 break;
4875 }
4876 /* allow if field is any interface or field is base class */
4877 if (!dvmIsInterfaceClass(fieldClass) &&
4878 !dvmInstanceof(valueClass, fieldClass))
4879 {
4880 LOG_VFY("VFY: storing type '%s' into field type '%s' (%s.%s)\n",
4881 valueClass->descriptor, fieldClass->descriptor,
4882 staticField->field.clazz->descriptor,
4883 staticField->field.name);
Andy McFadden62a75162009-04-17 17:23:37 -07004884 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004885 break;
4886 }
4887 }
4888 }
4889 break;
4890
4891 case OP_INVOKE_VIRTUAL:
4892 case OP_INVOKE_VIRTUAL_RANGE:
4893 case OP_INVOKE_SUPER:
4894 case OP_INVOKE_SUPER_RANGE:
4895 {
4896 Method* calledMethod;
4897 RegType returnType;
4898 bool isRange;
4899 bool isSuper;
4900
4901 isRange = (decInsn.opCode == OP_INVOKE_VIRTUAL_RANGE ||
4902 decInsn.opCode == OP_INVOKE_SUPER_RANGE);
4903 isSuper = (decInsn.opCode == OP_INVOKE_SUPER ||
4904 decInsn.opCode == OP_INVOKE_SUPER_RANGE);
4905
4906 calledMethod = verifyInvocationArgs(meth, workRegs, insnRegCount,
4907 &decInsn, uninitMap, METHOD_VIRTUAL, isRange,
Andy McFadden62a75162009-04-17 17:23:37 -07004908 isSuper, &failure);
4909 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004910 break;
4911 returnType = getMethodReturnType(calledMethod);
Andy McFaddend3250112010-11-03 14:32:42 -07004912 setResultRegisterType(workRegs, insnRegCount, returnType);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004913 justSetResult = true;
4914 }
4915 break;
4916 case OP_INVOKE_DIRECT:
4917 case OP_INVOKE_DIRECT_RANGE:
4918 {
4919 RegType returnType;
4920 Method* calledMethod;
4921 bool isRange;
4922
4923 isRange = (decInsn.opCode == OP_INVOKE_DIRECT_RANGE);
4924 calledMethod = verifyInvocationArgs(meth, workRegs, insnRegCount,
4925 &decInsn, uninitMap, METHOD_DIRECT, isRange,
Andy McFadden62a75162009-04-17 17:23:37 -07004926 false, &failure);
4927 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004928 break;
4929
4930 /*
4931 * Some additional checks when calling <init>. We know from
4932 * the invocation arg check that the "this" argument is an
4933 * instance of calledMethod->clazz. Now we further restrict
4934 * that to require that calledMethod->clazz is the same as
4935 * this->clazz or this->super, allowing the latter only if
4936 * the "this" argument is the same as the "this" argument to
4937 * this method (which implies that we're in <init> ourselves).
4938 */
4939 if (isInitMethod(calledMethod)) {
4940 RegType thisType;
Andy McFaddend3250112010-11-03 14:32:42 -07004941 thisType = getInvocationThis(workRegs, &decInsn, &failure);
Andy McFadden62a75162009-04-17 17:23:37 -07004942 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004943 break;
4944
4945 /* no null refs allowed (?) */
4946 if (thisType == kRegTypeZero) {
4947 LOG_VFY("VFY: unable to initialize null ref\n");
Andy McFadden62a75162009-04-17 17:23:37 -07004948 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004949 break;
4950 }
4951
4952 ClassObject* thisClass;
4953
4954 thisClass = regTypeReferenceToClass(thisType, uninitMap);
4955 assert(thisClass != NULL);
4956
4957 /* must be in same class or in superclass */
4958 if (calledMethod->clazz == thisClass->super) {
4959 if (thisClass != meth->clazz) {
4960 LOG_VFY("VFY: invoke-direct <init> on super only "
4961 "allowed for 'this' in <init>");
Andy McFadden62a75162009-04-17 17:23:37 -07004962 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004963 break;
4964 }
4965 } else if (calledMethod->clazz != thisClass) {
4966 LOG_VFY("VFY: invoke-direct <init> must be on current "
4967 "class or super\n");
Andy McFadden62a75162009-04-17 17:23:37 -07004968 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004969 break;
4970 }
4971
4972 /* arg must be an uninitialized reference */
4973 if (!regTypeIsUninitReference(thisType)) {
4974 LOG_VFY("VFY: can only initialize the uninitialized\n");
Andy McFadden62a75162009-04-17 17:23:37 -07004975 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004976 break;
4977 }
4978
4979 /*
4980 * Replace the uninitialized reference with an initialized
4981 * one, and clear the entry in the uninit map. We need to
4982 * do this for all registers that have the same object
4983 * instance in them, not just the "this" register.
4984 */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004985 markRefsAsInitialized(workRegs, insnRegCount, uninitMap,
Andy McFadden62a75162009-04-17 17:23:37 -07004986 thisType, &failure);
4987 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004988 break;
4989 }
4990 returnType = getMethodReturnType(calledMethod);
Andy McFaddend3250112010-11-03 14:32:42 -07004991 setResultRegisterType(workRegs, insnRegCount, returnType);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004992 justSetResult = true;
4993 }
4994 break;
4995 case OP_INVOKE_STATIC:
4996 case OP_INVOKE_STATIC_RANGE:
4997 {
4998 RegType returnType;
4999 Method* calledMethod;
5000 bool isRange;
5001
5002 isRange = (decInsn.opCode == OP_INVOKE_STATIC_RANGE);
5003 calledMethod = verifyInvocationArgs(meth, workRegs, insnRegCount,
5004 &decInsn, uninitMap, METHOD_STATIC, isRange,
Andy McFadden62a75162009-04-17 17:23:37 -07005005 false, &failure);
5006 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005007 break;
5008
5009 returnType = getMethodReturnType(calledMethod);
Andy McFaddend3250112010-11-03 14:32:42 -07005010 setResultRegisterType(workRegs, insnRegCount, returnType);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005011 justSetResult = true;
5012 }
5013 break;
5014 case OP_INVOKE_INTERFACE:
5015 case OP_INVOKE_INTERFACE_RANGE:
5016 {
5017 RegType /*thisType,*/ returnType;
5018 Method* absMethod;
5019 bool isRange;
5020
5021 isRange = (decInsn.opCode == OP_INVOKE_INTERFACE_RANGE);
5022 absMethod = verifyInvocationArgs(meth, workRegs, insnRegCount,
5023 &decInsn, uninitMap, METHOD_INTERFACE, isRange,
Andy McFadden62a75162009-04-17 17:23:37 -07005024 false, &failure);
5025 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005026 break;
5027
5028#if 0 /* can't do this here, fails on dalvik test 052-verifier-fun */
5029 /*
5030 * Get the type of the "this" arg, which should always be an
5031 * interface class. Because we don't do a full merge on
5032 * interface classes, this might have reduced to Object.
5033 */
Andy McFaddend3250112010-11-03 14:32:42 -07005034 thisType = getInvocationThis(workRegs, &decInsn, &failure);
Andy McFadden62a75162009-04-17 17:23:37 -07005035 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005036 break;
5037
5038 if (thisType == kRegTypeZero) {
5039 /* null pointer always passes (and always fails at runtime) */
5040 } else {
5041 ClassObject* thisClass;
5042
5043 thisClass = regTypeInitializedReferenceToClass(thisType);
5044 if (thisClass == NULL) {
5045 LOG_VFY("VFY: interface call on uninitialized\n");
Andy McFadden62a75162009-04-17 17:23:37 -07005046 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005047 break;
5048 }
5049
5050 /*
5051 * Either "thisClass" needs to be the interface class that
5052 * defined absMethod, or absMethod's class needs to be one
5053 * of the interfaces implemented by "thisClass". (Or, if
5054 * we couldn't complete the merge, this will be Object.)
5055 */
5056 if (thisClass != absMethod->clazz &&
5057 thisClass != gDvm.classJavaLangObject &&
5058 !dvmImplements(thisClass, absMethod->clazz))
5059 {
5060 LOG_VFY("VFY: unable to match absMethod '%s' with %s interfaces\n",
5061 absMethod->name, thisClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07005062 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005063 break;
5064 }
5065 }
5066#endif
5067
5068 /*
5069 * We don't have an object instance, so we can't find the
5070 * concrete method. However, all of the type information is
5071 * in the abstract method, so we're good.
5072 */
5073 returnType = getMethodReturnType(absMethod);
Andy McFaddend3250112010-11-03 14:32:42 -07005074 setResultRegisterType(workRegs, insnRegCount, returnType);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005075 justSetResult = true;
5076 }
5077 break;
5078
5079 case OP_NEG_INT:
5080 case OP_NOT_INT:
Andy McFaddend3250112010-11-03 14:32:42 -07005081 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005082 kRegTypeInteger, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005083 break;
5084 case OP_NEG_LONG:
5085 case OP_NOT_LONG:
Andy McFaddend3250112010-11-03 14:32:42 -07005086 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005087 kRegTypeLongLo, kRegTypeLongLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005088 break;
5089 case OP_NEG_FLOAT:
Andy McFaddend3250112010-11-03 14:32:42 -07005090 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005091 kRegTypeFloat, kRegTypeFloat, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005092 break;
5093 case OP_NEG_DOUBLE:
Andy McFaddend3250112010-11-03 14:32:42 -07005094 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005095 kRegTypeDoubleLo, kRegTypeDoubleLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005096 break;
5097 case OP_INT_TO_LONG:
Andy McFaddend3250112010-11-03 14:32:42 -07005098 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005099 kRegTypeLongLo, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005100 break;
5101 case OP_INT_TO_FLOAT:
Andy McFaddend3250112010-11-03 14:32:42 -07005102 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005103 kRegTypeFloat, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005104 break;
5105 case OP_INT_TO_DOUBLE:
Andy McFaddend3250112010-11-03 14:32:42 -07005106 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005107 kRegTypeDoubleLo, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005108 break;
5109 case OP_LONG_TO_INT:
Andy McFaddend3250112010-11-03 14:32:42 -07005110 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005111 kRegTypeInteger, kRegTypeLongLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005112 break;
5113 case OP_LONG_TO_FLOAT:
Andy McFaddend3250112010-11-03 14:32:42 -07005114 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005115 kRegTypeFloat, kRegTypeLongLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005116 break;
5117 case OP_LONG_TO_DOUBLE:
Andy McFaddend3250112010-11-03 14:32:42 -07005118 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005119 kRegTypeDoubleLo, kRegTypeLongLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005120 break;
5121 case OP_FLOAT_TO_INT:
Andy McFaddend3250112010-11-03 14:32:42 -07005122 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005123 kRegTypeInteger, kRegTypeFloat, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005124 break;
5125 case OP_FLOAT_TO_LONG:
Andy McFaddend3250112010-11-03 14:32:42 -07005126 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005127 kRegTypeLongLo, kRegTypeFloat, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005128 break;
5129 case OP_FLOAT_TO_DOUBLE:
Andy McFaddend3250112010-11-03 14:32:42 -07005130 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005131 kRegTypeDoubleLo, kRegTypeFloat, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005132 break;
5133 case OP_DOUBLE_TO_INT:
Andy McFaddend3250112010-11-03 14:32:42 -07005134 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005135 kRegTypeInteger, kRegTypeDoubleLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005136 break;
5137 case OP_DOUBLE_TO_LONG:
Andy McFaddend3250112010-11-03 14:32:42 -07005138 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005139 kRegTypeLongLo, kRegTypeDoubleLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005140 break;
5141 case OP_DOUBLE_TO_FLOAT:
Andy McFaddend3250112010-11-03 14:32:42 -07005142 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005143 kRegTypeFloat, kRegTypeDoubleLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005144 break;
5145 case OP_INT_TO_BYTE:
Andy McFaddend3250112010-11-03 14:32:42 -07005146 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005147 kRegTypeByte, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005148 break;
5149 case OP_INT_TO_CHAR:
Andy McFaddend3250112010-11-03 14:32:42 -07005150 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005151 kRegTypeChar, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005152 break;
5153 case OP_INT_TO_SHORT:
Andy McFaddend3250112010-11-03 14:32:42 -07005154 checkUnop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005155 kRegTypeShort, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005156 break;
5157
5158 case OP_ADD_INT:
5159 case OP_SUB_INT:
5160 case OP_MUL_INT:
5161 case OP_REM_INT:
5162 case OP_DIV_INT:
5163 case OP_SHL_INT:
5164 case OP_SHR_INT:
5165 case OP_USHR_INT:
Andy McFaddend3250112010-11-03 14:32:42 -07005166 checkBinop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005167 kRegTypeInteger, kRegTypeInteger, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005168 break;
5169 case OP_AND_INT:
5170 case OP_OR_INT:
5171 case OP_XOR_INT:
Andy McFaddend3250112010-11-03 14:32:42 -07005172 checkBinop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005173 kRegTypeInteger, kRegTypeInteger, kRegTypeInteger, true, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005174 break;
5175 case OP_ADD_LONG:
5176 case OP_SUB_LONG:
5177 case OP_MUL_LONG:
5178 case OP_DIV_LONG:
5179 case OP_REM_LONG:
5180 case OP_AND_LONG:
5181 case OP_OR_LONG:
5182 case OP_XOR_LONG:
Andy McFaddend3250112010-11-03 14:32:42 -07005183 checkBinop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005184 kRegTypeLongLo, kRegTypeLongLo, kRegTypeLongLo, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005185 break;
5186 case OP_SHL_LONG:
5187 case OP_SHR_LONG:
5188 case OP_USHR_LONG:
5189 /* shift distance is Int, making these different from other binops */
Andy McFaddend3250112010-11-03 14:32:42 -07005190 checkBinop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005191 kRegTypeLongLo, kRegTypeLongLo, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005192 break;
5193 case OP_ADD_FLOAT:
5194 case OP_SUB_FLOAT:
5195 case OP_MUL_FLOAT:
5196 case OP_DIV_FLOAT:
5197 case OP_REM_FLOAT:
Andy McFaddend3250112010-11-03 14:32:42 -07005198 checkBinop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005199 kRegTypeFloat, kRegTypeFloat, kRegTypeFloat, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005200 break;
5201 case OP_ADD_DOUBLE:
5202 case OP_SUB_DOUBLE:
5203 case OP_MUL_DOUBLE:
5204 case OP_DIV_DOUBLE:
5205 case OP_REM_DOUBLE:
Andy McFaddend3250112010-11-03 14:32:42 -07005206 checkBinop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005207 kRegTypeDoubleLo, kRegTypeDoubleLo, kRegTypeDoubleLo, false,
5208 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005209 break;
5210 case OP_ADD_INT_2ADDR:
5211 case OP_SUB_INT_2ADDR:
5212 case OP_MUL_INT_2ADDR:
5213 case OP_REM_INT_2ADDR:
5214 case OP_SHL_INT_2ADDR:
5215 case OP_SHR_INT_2ADDR:
5216 case OP_USHR_INT_2ADDR:
Andy McFaddend3250112010-11-03 14:32:42 -07005217 checkBinop2addr(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005218 kRegTypeInteger, kRegTypeInteger, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005219 break;
5220 case OP_AND_INT_2ADDR:
5221 case OP_OR_INT_2ADDR:
5222 case OP_XOR_INT_2ADDR:
Andy McFaddend3250112010-11-03 14:32:42 -07005223 checkBinop2addr(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005224 kRegTypeInteger, kRegTypeInteger, kRegTypeInteger, true, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005225 break;
5226 case OP_DIV_INT_2ADDR:
Andy McFaddend3250112010-11-03 14:32:42 -07005227 checkBinop2addr(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005228 kRegTypeInteger, kRegTypeInteger, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005229 break;
5230 case OP_ADD_LONG_2ADDR:
5231 case OP_SUB_LONG_2ADDR:
5232 case OP_MUL_LONG_2ADDR:
5233 case OP_DIV_LONG_2ADDR:
5234 case OP_REM_LONG_2ADDR:
5235 case OP_AND_LONG_2ADDR:
5236 case OP_OR_LONG_2ADDR:
5237 case OP_XOR_LONG_2ADDR:
Andy McFaddend3250112010-11-03 14:32:42 -07005238 checkBinop2addr(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005239 kRegTypeLongLo, kRegTypeLongLo, kRegTypeLongLo, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005240 break;
5241 case OP_SHL_LONG_2ADDR:
5242 case OP_SHR_LONG_2ADDR:
5243 case OP_USHR_LONG_2ADDR:
Andy McFaddend3250112010-11-03 14:32:42 -07005244 checkBinop2addr(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005245 kRegTypeLongLo, kRegTypeLongLo, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005246 break;
5247 case OP_ADD_FLOAT_2ADDR:
5248 case OP_SUB_FLOAT_2ADDR:
5249 case OP_MUL_FLOAT_2ADDR:
5250 case OP_DIV_FLOAT_2ADDR:
5251 case OP_REM_FLOAT_2ADDR:
Andy McFaddend3250112010-11-03 14:32:42 -07005252 checkBinop2addr(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005253 kRegTypeFloat, kRegTypeFloat, kRegTypeFloat, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005254 break;
5255 case OP_ADD_DOUBLE_2ADDR:
5256 case OP_SUB_DOUBLE_2ADDR:
5257 case OP_MUL_DOUBLE_2ADDR:
5258 case OP_DIV_DOUBLE_2ADDR:
5259 case OP_REM_DOUBLE_2ADDR:
Andy McFaddend3250112010-11-03 14:32:42 -07005260 checkBinop2addr(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005261 kRegTypeDoubleLo, kRegTypeDoubleLo, kRegTypeDoubleLo, false,
5262 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005263 break;
5264 case OP_ADD_INT_LIT16:
5265 case OP_RSUB_INT:
5266 case OP_MUL_INT_LIT16:
5267 case OP_DIV_INT_LIT16:
5268 case OP_REM_INT_LIT16:
Andy McFaddend3250112010-11-03 14:32:42 -07005269 checkLitop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005270 kRegTypeInteger, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005271 break;
5272 case OP_AND_INT_LIT16:
5273 case OP_OR_INT_LIT16:
5274 case OP_XOR_INT_LIT16:
Andy McFaddend3250112010-11-03 14:32:42 -07005275 checkLitop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005276 kRegTypeInteger, kRegTypeInteger, true, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005277 break;
5278 case OP_ADD_INT_LIT8:
5279 case OP_RSUB_INT_LIT8:
5280 case OP_MUL_INT_LIT8:
5281 case OP_DIV_INT_LIT8:
5282 case OP_REM_INT_LIT8:
5283 case OP_SHL_INT_LIT8:
Andy McFaddend3250112010-11-03 14:32:42 -07005284 checkLitop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005285 kRegTypeInteger, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005286 break;
Andy McFadden80d25ea2009-06-12 07:26:17 -07005287 case OP_SHR_INT_LIT8:
Andy McFaddend3250112010-11-03 14:32:42 -07005288 tmpType = adjustForRightShift(workRegs,
Andy McFadden80d25ea2009-06-12 07:26:17 -07005289 decInsn.vB, decInsn.vC, false, &failure);
Andy McFaddend3250112010-11-03 14:32:42 -07005290 checkLitop(workRegs, &decInsn,
Andy McFadden80d25ea2009-06-12 07:26:17 -07005291 tmpType, kRegTypeInteger, false, &failure);
5292 break;
5293 case OP_USHR_INT_LIT8:
Andy McFaddend3250112010-11-03 14:32:42 -07005294 tmpType = adjustForRightShift(workRegs,
Andy McFadden80d25ea2009-06-12 07:26:17 -07005295 decInsn.vB, decInsn.vC, true, &failure);
Andy McFaddend3250112010-11-03 14:32:42 -07005296 checkLitop(workRegs, &decInsn,
Andy McFadden80d25ea2009-06-12 07:26:17 -07005297 tmpType, kRegTypeInteger, false, &failure);
5298 break;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005299 case OP_AND_INT_LIT8:
5300 case OP_OR_INT_LIT8:
5301 case OP_XOR_INT_LIT8:
Andy McFaddend3250112010-11-03 14:32:42 -07005302 checkLitop(workRegs, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005303 kRegTypeInteger, kRegTypeInteger, true, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005304 break;
5305
Andy McFaddenb51ea112009-05-08 16:50:17 -07005306 /*
5307 * This falls into the general category of "optimized" instructions,
5308 * which don't generally appear during verification. Because it's
5309 * inserted in the course of verification, we can expect to see it here.
5310 */
5311 case OP_THROW_VERIFICATION_ERROR:
5312 break;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005313
5314 /*
5315 * Verifying "quickened" instructions is tricky, because we have
5316 * discarded the original field/method information. The byte offsets
5317 * and vtable indices only have meaning in the context of an object
5318 * instance.
5319 *
5320 * If a piece of code declares a local reference variable, assigns
5321 * null to it, and then issues a virtual method call on it, we
5322 * cannot evaluate the method call during verification. This situation
5323 * isn't hard to handle, since we know the call will always result in an
5324 * NPE, and the arguments and return value don't matter. Any code that
5325 * depends on the result of the method call is inaccessible, so the
5326 * fact that we can't fully verify anything that comes after the bad
5327 * call is not a problem.
5328 *
5329 * We must also consider the case of multiple code paths, only some of
5330 * which involve a null reference. We can completely verify the method
5331 * if we sidestep the results of executing with a null reference.
5332 * For example, if on the first pass through the code we try to do a
5333 * virtual method invocation through a null ref, we have to skip the
5334 * method checks and have the method return a "wildcard" type (which
5335 * merges with anything to become that other thing). The move-result
5336 * will tell us if it's a reference, single-word numeric, or double-word
5337 * value. We continue to perform the verification, and at the end of
5338 * the function any invocations that were never fully exercised are
5339 * marked as null-only.
5340 *
5341 * We would do something similar for the field accesses. The field's
5342 * type, once known, can be used to recover the width of short integers.
5343 * If the object reference was null, the field-get returns the "wildcard"
5344 * type, which is acceptable for any operation.
5345 */
5346 case OP_EXECUTE_INLINE:
Andy McFaddenb0a05412009-11-19 10:23:41 -08005347 case OP_EXECUTE_INLINE_RANGE:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005348 case OP_INVOKE_DIRECT_EMPTY:
5349 case OP_IGET_QUICK:
5350 case OP_IGET_WIDE_QUICK:
5351 case OP_IGET_OBJECT_QUICK:
5352 case OP_IPUT_QUICK:
5353 case OP_IPUT_WIDE_QUICK:
5354 case OP_IPUT_OBJECT_QUICK:
5355 case OP_INVOKE_VIRTUAL_QUICK:
5356 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
5357 case OP_INVOKE_SUPER_QUICK:
5358 case OP_INVOKE_SUPER_QUICK_RANGE:
Andy McFadden291758c2010-09-10 08:04:52 -07005359 case OP_RETURN_VOID_BARRIER:
Andy McFadden62a75162009-04-17 17:23:37 -07005360 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005361 break;
5362
Andy McFadden96516932009-10-28 17:39:02 -07005363 /* these should never appear during verification */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005364 case OP_UNUSED_3E:
5365 case OP_UNUSED_3F:
5366 case OP_UNUSED_40:
5367 case OP_UNUSED_41:
5368 case OP_UNUSED_42:
5369 case OP_UNUSED_43:
5370 case OP_UNUSED_73:
5371 case OP_UNUSED_79:
5372 case OP_UNUSED_7A:
Andy McFadden96516932009-10-28 17:39:02 -07005373 case OP_BREAKPOINT:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005374 case OP_UNUSED_FF:
Andy McFadden62a75162009-04-17 17:23:37 -07005375 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005376 break;
5377
5378 /*
5379 * DO NOT add a "default" clause here. Without it the compiler will
5380 * complain if an instruction is missing (which is desirable).
5381 */
5382 }
5383
Andy McFadden62a75162009-04-17 17:23:37 -07005384 if (!VERIFY_OK(failure)) {
Andy McFaddenb51ea112009-05-08 16:50:17 -07005385 if (failure == VERIFY_ERROR_GENERIC || gDvm.optimizing) {
5386 /* immediate failure, reject class */
5387 LOG_VFY_METH(meth, "VFY: rejecting opcode 0x%02x at 0x%04x\n",
5388 decInsn.opCode, insnIdx);
5389 goto bail;
5390 } else {
5391 /* replace opcode and continue on */
5392 LOGD("VFY: replacing opcode 0x%02x at 0x%04x\n",
5393 decInsn.opCode, insnIdx);
5394 if (!replaceFailingInstruction(meth, insnFlags, insnIdx, failure)) {
5395 LOG_VFY_METH(meth, "VFY: rejecting opcode 0x%02x at 0x%04x\n",
5396 decInsn.opCode, insnIdx);
5397 goto bail;
5398 }
5399 /* IMPORTANT: meth->insns may have been changed */
5400 insns = meth->insns + insnIdx;
5401
5402 /* continue on as if we just handled a throw-verification-error */
5403 failure = VERIFY_ERROR_NONE;
5404 nextFlags = kInstrCanThrow;
5405 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005406 }
5407
5408 /*
5409 * If we didn't just set the result register, clear it out. This
5410 * ensures that you can only use "move-result" immediately after the
Andy McFadden2e1ee502010-03-24 13:25:53 -07005411 * result is set. (We could check this statically, but it's not
5412 * expensive and it makes our debugging output cleaner.)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005413 */
5414 if (!justSetResult) {
5415 int reg = RESULT_REGISTER(insnRegCount);
5416 workRegs[reg] = workRegs[reg+1] = kRegTypeUnknown;
5417 }
5418
5419 /*
5420 * Handle "continue". Tag the next consecutive instruction.
5421 */
5422 if ((nextFlags & kInstrCanContinue) != 0) {
5423 int insnWidth = dvmInsnGetWidth(insnFlags, insnIdx);
5424 if (insnIdx+insnWidth >= insnsSize) {
5425 LOG_VFY_METH(meth,
5426 "VFY: execution can walk off end of code area (from 0x%x)\n",
5427 insnIdx);
5428 goto bail;
5429 }
5430
5431 /*
5432 * The only way to get to a move-exception instruction is to get
5433 * thrown there. Make sure the next instruction isn't one.
5434 */
5435 if (!checkMoveException(meth, insnIdx+insnWidth, "next"))
5436 goto bail;
5437
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005438 if (getRegisterLine(regTable, insnIdx+insnWidth) != NULL) {
Andy McFadden06b7a282009-05-11 10:44:52 -07005439 /*
5440 * Merge registers into what we have for the next instruction,
5441 * and set the "changed" flag if needed.
5442 */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005443 updateRegisters(meth, insnFlags, regTable, insnIdx+insnWidth,
5444 workRegs);
5445 } else {
The Android Open Source Project99409882009-03-18 22:20:24 -07005446 /*
Andy McFadden06b7a282009-05-11 10:44:52 -07005447 * We're not recording register data for the next instruction,
5448 * so we don't know what the prior state was. We have to
5449 * assume that something has changed and re-evaluate it.
The Android Open Source Project99409882009-03-18 22:20:24 -07005450 */
5451 dvmInsnSetChanged(insnFlags, insnIdx+insnWidth, true);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005452 }
5453 }
5454
5455 /*
5456 * Handle "branch". Tag the branch target.
5457 *
5458 * NOTE: instructions like OP_EQZ provide information about the state
5459 * of the register when the branch is taken or not taken. For example,
5460 * somebody could get a reference field, check it for zero, and if the
5461 * branch is taken immediately store that register in a boolean field
5462 * since the value is known to be zero. We do not currently account for
5463 * that, and will reject the code.
5464 */
5465 if ((nextFlags & kInstrCanBranch) != 0) {
5466 bool isConditional;
5467
5468 if (!dvmGetBranchTarget(meth, insnFlags, insnIdx, &branchTarget,
5469 &isConditional))
5470 {
5471 /* should never happen after static verification */
5472 LOG_VFY_METH(meth, "VFY: bad branch at %d\n", insnIdx);
5473 goto bail;
5474 }
5475 assert(isConditional || (nextFlags & kInstrCanContinue) == 0);
5476 assert(!isConditional || (nextFlags & kInstrCanContinue) != 0);
5477
5478 if (!checkMoveException(meth, insnIdx+branchTarget, "branch"))
5479 goto bail;
5480
The Android Open Source Project99409882009-03-18 22:20:24 -07005481 /* update branch target, set "changed" if appropriate */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005482 updateRegisters(meth, insnFlags, regTable, insnIdx+branchTarget,
5483 workRegs);
5484 }
5485
5486 /*
5487 * Handle "switch". Tag all possible branch targets.
5488 *
5489 * We've already verified that the table is structurally sound, so we
5490 * just need to walk through and tag the targets.
5491 */
5492 if ((nextFlags & kInstrCanSwitch) != 0) {
5493 int offsetToSwitch = insns[1] | (((s4)insns[2]) << 16);
5494 const u2* switchInsns = insns + offsetToSwitch;
5495 int switchCount = switchInsns[1];
5496 int offsetToTargets, targ;
5497
5498 if ((*insns & 0xff) == OP_PACKED_SWITCH) {
5499 /* 0=sig, 1=count, 2/3=firstKey */
5500 offsetToTargets = 4;
5501 } else {
5502 /* 0=sig, 1=count, 2..count*2 = keys */
5503 assert((*insns & 0xff) == OP_SPARSE_SWITCH);
5504 offsetToTargets = 2 + 2*switchCount;
5505 }
5506
5507 /* verify each switch target */
5508 for (targ = 0; targ < switchCount; targ++) {
5509 int offset, absOffset;
5510
5511 /* offsets are 32-bit, and only partly endian-swapped */
5512 offset = switchInsns[offsetToTargets + targ*2] |
5513 (((s4) switchInsns[offsetToTargets + targ*2 +1]) << 16);
5514 absOffset = insnIdx + offset;
5515
5516 assert(absOffset >= 0 && absOffset < insnsSize);
5517
5518 if (!checkMoveException(meth, absOffset, "switch"))
5519 goto bail;
5520
5521 updateRegisters(meth, insnFlags, regTable, absOffset, workRegs);
5522 }
5523 }
5524
5525 /*
5526 * Handle instructions that can throw and that are sitting in a
5527 * "try" block. (If they're not in a "try" block when they throw,
5528 * control transfers out of the method.)
5529 */
5530 if ((nextFlags & kInstrCanThrow) != 0 && dvmInsnIsInTry(insnFlags, insnIdx))
5531 {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005532 const DexCode* pCode = dvmGetMethodCode(meth);
5533 DexCatchIterator iterator;
5534
5535 if (dexFindCatchHandler(&iterator, pCode, insnIdx)) {
5536 for (;;) {
5537 DexCatchHandler* handler = dexCatchIteratorNext(&iterator);
5538
5539 if (handler == NULL) {
5540 break;
5541 }
5542
5543 /* note we use entryRegs, not workRegs */
5544 updateRegisters(meth, insnFlags, regTable, handler->address,
5545 entryRegs);
5546 }
5547 }
5548 }
5549
5550 /*
5551 * Update startGuess. Advance to the next instruction of that's
5552 * possible, otherwise use the branch target if one was found. If
5553 * neither of those exists we're in a return or throw; leave startGuess
5554 * alone and let the caller sort it out.
5555 */
5556 if ((nextFlags & kInstrCanContinue) != 0) {
5557 *pStartGuess = insnIdx + dvmInsnGetWidth(insnFlags, insnIdx);
5558 } else if ((nextFlags & kInstrCanBranch) != 0) {
5559 /* we're still okay if branchTarget is zero */
5560 *pStartGuess = insnIdx + branchTarget;
5561 }
5562
5563 assert(*pStartGuess >= 0 && *pStartGuess < insnsSize &&
5564 dvmInsnGetWidth(insnFlags, *pStartGuess) != 0);
5565
5566 result = true;
5567
5568bail:
5569 return result;
5570}
5571
Andy McFaddenb51ea112009-05-08 16:50:17 -07005572
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005573/*
5574 * callback function used in dumpRegTypes to print local vars
5575 * valid at a given address.
5576 */
5577static void logLocalsCb(void *cnxt, u2 reg, u4 startAddress, u4 endAddress,
5578 const char *name, const char *descriptor,
5579 const char *signature)
5580{
5581 int addr = *((int *)cnxt);
5582
5583 if (addr >= (int) startAddress && addr < (int) endAddress)
5584 {
5585 LOGI(" %2d: '%s' %s\n", reg, name, descriptor);
5586 }
5587}
5588
5589/*
5590 * Dump the register types for the specifed address to the log file.
5591 */
5592static void dumpRegTypes(const Method* meth, const InsnFlags* insnFlags,
5593 const RegType* addrRegs, int addr, const char* addrName,
5594 const UninitInstanceMap* uninitMap, int displayFlags)
5595{
5596 int regCount = meth->registersSize;
5597 int fullRegCount = regCount + kExtraRegs;
5598 bool branchTarget = dvmInsnIsBranchTarget(insnFlags, addr);
5599 int i;
5600
5601 assert(addr >= 0 && addr < (int) dvmGetMethodInsnsSize(meth));
5602
5603 int regCharSize = fullRegCount + (fullRegCount-1)/4 + 2 +1;
5604 char regChars[regCharSize +1];
5605 memset(regChars, ' ', regCharSize);
5606 regChars[0] = '[';
5607 if (regCount == 0)
5608 regChars[1] = ']';
5609 else
5610 regChars[1 + (regCount-1) + (regCount-1)/4 +1] = ']';
5611 regChars[regCharSize] = '\0';
5612
5613 //const RegType* addrRegs = getRegisterLine(regTable, addr);
5614
5615 for (i = 0; i < regCount + kExtraRegs; i++) {
5616 char tch;
5617
5618 switch (addrRegs[i]) {
5619 case kRegTypeUnknown: tch = '.'; break;
5620 case kRegTypeConflict: tch = 'X'; break;
5621 case kRegTypeFloat: tch = 'F'; break;
5622 case kRegTypeZero: tch = '0'; break;
5623 case kRegTypeOne: tch = '1'; break;
5624 case kRegTypeBoolean: tch = 'Z'; break;
5625 case kRegTypePosByte: tch = 'b'; break;
5626 case kRegTypeByte: tch = 'B'; break;
5627 case kRegTypePosShort: tch = 's'; break;
5628 case kRegTypeShort: tch = 'S'; break;
5629 case kRegTypeChar: tch = 'C'; break;
5630 case kRegTypeInteger: tch = 'I'; break;
5631 case kRegTypeLongLo: tch = 'J'; break;
5632 case kRegTypeLongHi: tch = 'j'; break;
5633 case kRegTypeDoubleLo: tch = 'D'; break;
5634 case kRegTypeDoubleHi: tch = 'd'; break;
5635 default:
5636 if (regTypeIsReference(addrRegs[i])) {
5637 if (regTypeIsUninitReference(addrRegs[i]))
5638 tch = 'U';
5639 else
5640 tch = 'L';
5641 } else {
5642 tch = '*';
5643 assert(false);
5644 }
5645 break;
5646 }
5647
5648 if (i < regCount)
5649 regChars[1 + i + (i/4)] = tch;
5650 else
5651 regChars[1 + i + (i/4) + 2] = tch;
5652 }
5653
5654 if (addr == 0 && addrName != NULL)
5655 LOGI("%c%s %s\n", branchTarget ? '>' : ' ', addrName, regChars);
5656 else
5657 LOGI("%c0x%04x %s\n", branchTarget ? '>' : ' ', addr, regChars);
5658
5659 if (displayFlags & DRT_SHOW_REF_TYPES) {
5660 for (i = 0; i < regCount + kExtraRegs; i++) {
5661 if (regTypeIsReference(addrRegs[i]) && addrRegs[i] != kRegTypeZero)
5662 {
5663 ClassObject* clazz;
5664
5665 clazz = regTypeReferenceToClass(addrRegs[i], uninitMap);
5666 assert(dvmValidateObject((Object*)clazz));
5667 if (i < regCount) {
5668 LOGI(" %2d: 0x%08x %s%s\n",
5669 i, addrRegs[i],
5670 regTypeIsUninitReference(addrRegs[i]) ? "[U]" : "",
5671 clazz->descriptor);
5672 } else {
5673 LOGI(" RS: 0x%08x %s%s\n",
5674 addrRegs[i],
5675 regTypeIsUninitReference(addrRegs[i]) ? "[U]" : "",
5676 clazz->descriptor);
5677 }
5678 }
5679 }
5680 }
5681 if (displayFlags & DRT_SHOW_LOCALS) {
5682 dexDecodeDebugInfo(meth->clazz->pDvmDex->pDexFile,
5683 dvmGetMethodCode(meth),
5684 meth->clazz->descriptor,
5685 meth->prototype.protoIdx,
5686 meth->accessFlags,
5687 NULL, logLocalsCb, &addr);
5688 }
5689}