blob: 8187354bc98468ce5dc37867a9d599cadb7c8eac [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"
32#include "analysis/RegisterMap.h"
33#include "libdex/DexCatch.h"
34#include "libdex/InstrUtils.h"
35
36#include <stddef.h>
37
38
39/*
40 * We don't need to store the register data for many instructions, because
41 * we either only need it at branch points (for verification) or GC points
42 * and branches (for verification + type-precise register analysis).
43 */
44typedef enum RegisterTrackingMode {
45 kTrackRegsBranches,
46 kTrackRegsGcPoints,
47 kTrackRegsAll
48} RegisterTrackingMode;
49
50/*
51 * Set this to enable dead code scanning. This is not required, but it's
52 * very useful when testing changes to the verifier (to make sure we're not
53 * skipping over stuff) and for checking the optimized output from "dx".
54 * The only reason not to do it is that it slightly increases the time
55 * required to perform verification.
56 */
57#define DEAD_CODE_SCAN true
58
59static bool gDebugVerbose = false; // TODO: remove this
60
61#if 0
62int gDvm__totalInstr = 0;
63int gDvm__gcInstr = 0;
64int gDvm__gcData = 0;
65int gDvm__gcSimpleData = 0;
66#endif
67
68/*
69 * Selectively enable verbose debug logging -- use this to activate
70 * dumpRegTypes() calls for all instructions in the specified method.
71 */
72static inline bool doVerboseLogging(const Method* meth) {
73 return false; /* COMMENT OUT to enable verbose debugging */
74
The Android Open Source Project99409882009-03-18 22:20:24 -070075 const char* cd = "Landroid/net/http/Request;";
76 const char* mn = "readResponse";
77 const char* sg = "(Landroid/net/http/AndroidHttpClientConnection;)V";
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080078 return (strcmp(meth->clazz->descriptor, cd) == 0 &&
79 dvmCompareNameDescriptorAndMethod(mn, sg, meth) == 0);
80}
81
82#define SHOW_REG_DETAILS (0 /*| DRT_SHOW_REF_TYPES | DRT_SHOW_LOCALS*/)
83
84/*
85 * We need an extra "pseudo register" to hold the return type briefly. It
86 * can be category 1 or 2, so we need two slots.
87 */
88#define kExtraRegs 2
89#define RESULT_REGISTER(_insnRegCount) (_insnRegCount)
90
91/*
92 * Big fat collection of registers.
93 */
94typedef struct RegisterTable {
95 /*
96 * Array of RegType arrays, one per address in the method. We only
97 * set the pointers for certain addresses, based on what we're trying
98 * to accomplish.
99 */
100 RegType** addrRegs;
101
102 /*
103 * Number of registers we track for each instruction. This is equal
104 * to the method's declared "registersSize" plus kExtraRegs.
105 */
106 int insnRegCountPlus;
107
108 /*
109 * A single large alloc, with all of the storage needed for addrRegs.
110 */
111 RegType* regAlloc;
112} RegisterTable;
113
114
115/* fwd */
116static void checkMergeTab(void);
117static bool isInitMethod(const Method* meth);
118static RegType getInvocationThis(const RegType* insnRegs,\
Andy McFadden62a75162009-04-17 17:23:37 -0700119 const int insnRegCount, const DecodedInstruction* pDecInsn,
120 VerifyError* pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800121static void verifyRegisterType(const RegType* insnRegs, const int insnRegCount,\
Andy McFadden62a75162009-04-17 17:23:37 -0700122 u4 vsrc, RegType checkType, VerifyError* pFailure);
Andy McFaddenb51ea112009-05-08 16:50:17 -0700123static bool doCodeVerification(Method* meth, InsnFlags* insnFlags,\
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800124 RegisterTable* regTable, UninitInstanceMap* uninitMap);
Andy McFaddenb51ea112009-05-08 16:50:17 -0700125static bool verifyInstruction(Method* meth, InsnFlags* insnFlags,\
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800126 RegisterTable* regTable, RegType* workRegs, int insnIdx,
127 UninitInstanceMap* uninitMap, int* pStartGuess);
128static ClassObject* findCommonSuperclass(ClassObject* c1, ClassObject* c2);
129static void dumpRegTypes(const Method* meth, const InsnFlags* insnFlags,\
130 const RegType* addrRegs, int addr, const char* addrName,
131 const UninitInstanceMap* uninitMap, int displayFlags);
132
133/* bit values for dumpRegTypes() "displayFlags" */
134enum {
135 DRT_SIMPLE = 0,
136 DRT_SHOW_REF_TYPES = 0x01,
137 DRT_SHOW_LOCALS = 0x02,
138};
139
140
141/*
142 * ===========================================================================
143 * RegType and UninitInstanceMap utility functions
144 * ===========================================================================
145 */
146
147#define __ kRegTypeUnknown
148#define _U kRegTypeUninit
149#define _X kRegTypeConflict
150#define _F kRegTypeFloat
151#define _0 kRegTypeZero
152#define _1 kRegTypeOne
153#define _Z kRegTypeBoolean
154#define _b kRegTypePosByte
155#define _B kRegTypeByte
156#define _s kRegTypePosShort
157#define _S kRegTypeShort
158#define _C kRegTypeChar
159#define _I kRegTypeInteger
160#define _J kRegTypeLongLo
161#define _j kRegTypeLongHi
162#define _D kRegTypeDoubleLo
163#define _d kRegTypeDoubleHi
164
165/*
166 * Merge result table for primitive values. The table is symmetric along
167 * the diagonal.
168 *
169 * Note that 32-bit int/float do not merge into 64-bit long/double. This
170 * is a register merge, not a widening conversion. Only the "implicit"
171 * widening within a category, e.g. byte to short, is allowed.
172 *
173 * Because Dalvik does not draw a distinction between int and float, we
174 * have to allow free exchange between 32-bit int/float and 64-bit
175 * long/double.
176 *
177 * Note that Uninit+Uninit=Uninit. This holds true because we only
178 * use this when the RegType value is exactly equal to kRegTypeUninit, which
179 * can only happen for the zeroeth entry in the table.
180 *
181 * "Unknown" never merges with anything known. The only time a register
182 * transitions from "unknown" to "known" is when we're executing code
183 * for the first time, and we handle that with a simple copy.
184 */
185const char gDvmMergeTab[kRegTypeMAX][kRegTypeMAX] =
186{
187 /* chk: _ U X F 0 1 Z b B s S C I J j D d */
188 { /*_*/ __,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X },
189 { /*U*/ _X,_U,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X },
190 { /*X*/ _X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X },
191 { /*F*/ _X,_X,_X,_F,_F,_F,_F,_F,_F,_F,_F,_F,_F,_X,_X,_X,_X },
192 { /*0*/ _X,_X,_X,_F,_0,_Z,_Z,_b,_B,_s,_S,_C,_I,_X,_X,_X,_X },
193 { /*1*/ _X,_X,_X,_F,_Z,_1,_Z,_b,_B,_s,_S,_C,_I,_X,_X,_X,_X },
194 { /*Z*/ _X,_X,_X,_F,_Z,_Z,_Z,_b,_B,_s,_S,_C,_I,_X,_X,_X,_X },
195 { /*b*/ _X,_X,_X,_F,_b,_b,_b,_b,_B,_s,_S,_C,_I,_X,_X,_X,_X },
196 { /*B*/ _X,_X,_X,_F,_B,_B,_B,_B,_B,_S,_S,_I,_I,_X,_X,_X,_X },
197 { /*s*/ _X,_X,_X,_F,_s,_s,_s,_s,_S,_s,_S,_C,_I,_X,_X,_X,_X },
198 { /*S*/ _X,_X,_X,_F,_S,_S,_S,_S,_S,_S,_S,_I,_I,_X,_X,_X,_X },
199 { /*C*/ _X,_X,_X,_F,_C,_C,_C,_C,_I,_C,_I,_C,_I,_X,_X,_X,_X },
200 { /*I*/ _X,_X,_X,_F,_I,_I,_I,_I,_I,_I,_I,_I,_I,_X,_X,_X,_X },
201 { /*J*/ _X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_J,_X,_J,_X },
202 { /*j*/ _X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_j,_X,_j },
203 { /*D*/ _X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_J,_X,_D,_X },
204 { /*d*/ _X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_X,_j,_X,_d },
205};
206
207#undef __
208#undef _U
209#undef _X
210#undef _F
211#undef _0
212#undef _1
213#undef _Z
214#undef _b
215#undef _B
216#undef _s
217#undef _S
218#undef _C
219#undef _I
220#undef _J
221#undef _j
222#undef _D
223#undef _d
224
225#ifndef NDEBUG
226/*
227 * Verify symmetry in the conversion table.
228 */
229static void checkMergeTab(void)
230{
231 int i, j;
232
233 for (i = 0; i < kRegTypeMAX; i++) {
234 for (j = i; j < kRegTypeMAX; j++) {
235 if (gDvmMergeTab[i][j] != gDvmMergeTab[j][i]) {
236 LOGE("Symmetry violation: %d,%d vs %d,%d\n", i, j, j, i);
237 dvmAbort();
238 }
239 }
240 }
241}
242#endif
243
244/*
245 * Determine whether we can convert "srcType" to "checkType", where
246 * "checkType" is one of the category-1 non-reference types.
247 *
248 * 32-bit int and float are interchangeable.
249 */
250static bool canConvertTo1nr(RegType srcType, RegType checkType)
251{
252 static const char convTab
253 [kRegType1nrEND-kRegType1nrSTART+1][kRegType1nrEND-kRegType1nrSTART+1] =
254 {
255 /* chk: F 0 1 Z b B s S C I */
256 { /*F*/ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
257 { /*0*/ 1, 1, 0, 1, 1, 1, 1, 1, 1, 1 },
258 { /*1*/ 1, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
259 { /*Z*/ 1, 0, 0, 1, 1, 1, 1, 1, 1, 1 },
260 { /*b*/ 1, 0, 0, 0, 1, 1, 1, 1, 1, 1 },
261 { /*B*/ 1, 0, 0, 0, 0, 1, 0, 1, 0, 1 },
262 { /*s*/ 1, 0, 0, 0, 0, 0, 1, 1, 1, 1 },
263 { /*S*/ 1, 0, 0, 0, 0, 0, 0, 1, 0, 1 },
264 { /*C*/ 1, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
265 { /*I*/ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
266 };
267
268 assert(checkType >= kRegType1nrSTART && checkType <= kRegType1nrEND);
269#if 0
270 if (checkType < kRegType1nrSTART || checkType > kRegType1nrEND) {
271 LOG_VFY("Unexpected checkType %d (srcType=%d)\n", checkType, srcType);
272 assert(false);
273 return false;
274 }
275#endif
276
277 //printf("convTab[%d][%d] = %d\n", srcType, checkType,
278 // convTab[srcType-kRegType1nrSTART][checkType-kRegType1nrSTART]);
279 if (srcType >= kRegType1nrSTART && srcType <= kRegType1nrEND)
280 return (bool) convTab[srcType-kRegType1nrSTART][checkType-kRegType1nrSTART];
281
282 return false;
283}
284
285/*
286 * Determine whether the types are compatible. In Dalvik, 64-bit doubles
287 * and longs are interchangeable.
288 */
289static bool canConvertTo2(RegType srcType, RegType checkType)
290{
291 return ((srcType == kRegTypeLongLo || srcType == kRegTypeDoubleLo) &&
292 (checkType == kRegTypeLongLo || checkType == kRegTypeDoubleLo));
293}
294
295/*
296 * Determine whether or not "instrType" and "targetType" are compatible,
297 * for purposes of getting or setting a value in a field or array. The
298 * idea is that an instruction with a category 1nr type (say, aget-short
299 * or iput-boolean) is accessing a static field, instance field, or array
300 * entry, and we want to make sure sure that the operation is legal.
301 *
302 * At a minimum, source and destination must have the same width. We
303 * further refine this to assert that "short" and "char" are not
304 * compatible, because the sign-extension is different on the "get"
305 * operations. As usual, "float" and "int" are interoperable.
306 *
307 * We're not considering the actual contents of the register, so we'll
308 * never get "pseudo-types" like kRegTypeZero or kRegTypePosShort. We
309 * could get kRegTypeUnknown in "targetType" if a field or array class
310 * lookup failed. Category 2 types and references are checked elsewhere.
311 */
312static bool checkFieldArrayStore1nr(RegType instrType, RegType targetType)
313{
314 if (instrType == targetType)
315 return true; /* quick positive; most common case */
316
317 if ((instrType == kRegTypeInteger && targetType == kRegTypeFloat) ||
318 (instrType == kRegTypeFloat && targetType == kRegTypeInteger))
319 {
320 return true;
321 }
322
323 return false;
324}
325
326/*
327 * Convert a VM PrimitiveType enum value to the equivalent RegType value.
328 */
329static RegType primitiveTypeToRegType(PrimitiveType primType)
330{
The Android Open Source Project99409882009-03-18 22:20:24 -0700331 static const struct {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800332 RegType regType; /* type equivalent */
333 PrimitiveType primType; /* verification */
334 } convTab[] = {
335 /* must match order of enum in Object.h */
336 { kRegTypeBoolean, PRIM_BOOLEAN },
337 { kRegTypeChar, PRIM_CHAR },
338 { kRegTypeFloat, PRIM_FLOAT },
339 { kRegTypeDoubleLo, PRIM_DOUBLE },
340 { kRegTypeByte, PRIM_BYTE },
341 { kRegTypeShort, PRIM_SHORT },
342 { kRegTypeInteger, PRIM_INT },
343 { kRegTypeLongLo, PRIM_LONG },
344 // PRIM_VOID
345 };
346
347 if (primType < 0 || primType > (int) (sizeof(convTab) / sizeof(convTab[0])))
348 {
349 assert(false);
350 return kRegTypeUnknown;
351 }
352
353 assert(convTab[primType].primType == primType);
354 return convTab[primType].regType;
355}
356
357/*
358 * Create a new uninitialized instance map.
359 *
360 * The map is allocated and populated with address entries. The addresses
361 * appear in ascending order to allow binary searching.
362 *
363 * Very few methods have 10 or more new-instance instructions; the
364 * majority have 0 or 1. Occasionally a static initializer will have 200+.
365 */
366UninitInstanceMap* dvmCreateUninitInstanceMap(const Method* meth,
367 const InsnFlags* insnFlags, int newInstanceCount)
368{
369 const int insnsSize = dvmGetMethodInsnsSize(meth);
370 const u2* insns = meth->insns;
371 UninitInstanceMap* uninitMap;
372 bool isInit = false;
373 int idx, addr;
374
375 if (isInitMethod(meth)) {
376 newInstanceCount++;
377 isInit = true;
378 }
379
380 /*
381 * Allocate the header and map as a single unit.
382 *
383 * TODO: consider having a static instance so we can avoid allocations.
384 * I don't think the verifier is guaranteed to be single-threaded when
385 * running in the VM (rather than dexopt), so that must be taken into
386 * account.
387 */
388 int size = offsetof(UninitInstanceMap, map) +
389 newInstanceCount * sizeof(uninitMap->map[0]);
390 uninitMap = calloc(1, size);
391 if (uninitMap == NULL)
392 return NULL;
393 uninitMap->numEntries = newInstanceCount;
394
395 idx = 0;
396 if (isInit) {
397 uninitMap->map[idx++].addr = kUninitThisArgAddr;
398 }
399
400 /*
401 * Run through and find the new-instance instructions.
402 */
403 for (addr = 0; addr < insnsSize; /**/) {
404 int width = dvmInsnGetWidth(insnFlags, addr);
405
406 if ((*insns & 0xff) == OP_NEW_INSTANCE)
407 uninitMap->map[idx++].addr = addr;
408
409 addr += width;
410 insns += width;
411 }
412
413 assert(idx == newInstanceCount);
414 return uninitMap;
415}
416
417/*
418 * Free the map.
419 */
420void dvmFreeUninitInstanceMap(UninitInstanceMap* uninitMap)
421{
422 free(uninitMap);
423}
424
425/*
426 * Set the class object associated with the instruction at "addr".
427 *
428 * Returns the map slot index, or -1 if the address isn't listed in the map
429 * (shouldn't happen) or if a class is already associated with the address
430 * (bad bytecode).
431 *
432 * Entries, once set, do not change -- a given address can only allocate
433 * one type of object.
434 */
435int dvmSetUninitInstance(UninitInstanceMap* uninitMap, int addr,
436 ClassObject* clazz)
437{
438 int idx;
439
440 assert(clazz != NULL);
441
442 /* TODO: binary search when numEntries > 8 */
443 for (idx = uninitMap->numEntries - 1; idx >= 0; idx--) {
444 if (uninitMap->map[idx].addr == addr) {
445 if (uninitMap->map[idx].clazz != NULL &&
446 uninitMap->map[idx].clazz != clazz)
447 {
448 LOG_VFY("VFY: addr %d already set to %p, not setting to %p\n",
449 addr, uninitMap->map[idx].clazz, clazz);
450 return -1; // already set to something else??
451 }
452 uninitMap->map[idx].clazz = clazz;
453 return idx;
454 }
455 }
456
457 LOG_VFY("VFY: addr %d not found in uninit map\n", addr);
458 assert(false); // shouldn't happen
459 return -1;
460}
461
462/*
463 * Get the class object at the specified index.
464 */
465ClassObject* dvmGetUninitInstance(const UninitInstanceMap* uninitMap, int idx)
466{
467 assert(idx >= 0 && idx < uninitMap->numEntries);
468 return uninitMap->map[idx].clazz;
469}
470
471/* determine if "type" is actually an object reference (init/uninit/zero) */
472static inline bool regTypeIsReference(RegType type) {
473 return (type > kRegTypeMAX || type == kRegTypeUninit ||
474 type == kRegTypeZero);
475}
476
477/* determine if "type" is an uninitialized object reference */
478static inline bool regTypeIsUninitReference(RegType type) {
479 return ((type & kRegTypeUninitMask) == kRegTypeUninit);
480}
481
482/* convert the initialized reference "type" to a ClassObject pointer */
483/* (does not expect uninit ref types or "zero") */
484static ClassObject* regTypeInitializedReferenceToClass(RegType type)
485{
486 assert(regTypeIsReference(type) && type != kRegTypeZero);
487 if ((type & 0x01) == 0) {
488 return (ClassObject*) type;
489 } else {
490 //LOG_VFY("VFY: attempted to use uninitialized reference\n");
491 return NULL;
492 }
493}
494
495/* extract the index into the uninitialized instance map table */
496static inline int regTypeToUninitIndex(RegType type) {
497 assert(regTypeIsUninitReference(type));
498 return (type & ~kRegTypeUninitMask) >> kRegTypeUninitShift;
499}
500
501/* convert the reference "type" to a ClassObject pointer */
502static ClassObject* regTypeReferenceToClass(RegType type,
503 const UninitInstanceMap* uninitMap)
504{
505 assert(regTypeIsReference(type) && type != kRegTypeZero);
506 if (regTypeIsUninitReference(type)) {
507 assert(uninitMap != NULL);
508 return dvmGetUninitInstance(uninitMap, regTypeToUninitIndex(type));
509 } else {
510 return (ClassObject*) type;
511 }
512}
513
514/* convert the ClassObject pointer to an (initialized) register type */
515static inline RegType regTypeFromClass(ClassObject* clazz) {
516 return (u4) clazz;
517}
518
519/* return the RegType for the uninitialized reference in slot "uidx" */
520static RegType regTypeFromUninitIndex(int uidx) {
521 return (u4) (kRegTypeUninit | (uidx << kRegTypeUninitShift));
522}
523
524
525/*
526 * ===========================================================================
527 * Signature operations
528 * ===========================================================================
529 */
530
531/*
532 * Is this method a constructor?
533 */
534static bool isInitMethod(const Method* meth)
535{
536 return (*meth->name == '<' && strcmp(meth->name+1, "init>") == 0);
537}
538
539/*
540 * Is this method a class initializer?
541 */
542static bool isClassInitMethod(const Method* meth)
543{
544 return (*meth->name == '<' && strcmp(meth->name+1, "clinit>") == 0);
545}
546
547/*
548 * Look up a class reference given as a simple string descriptor.
Andy McFadden62a75162009-04-17 17:23:37 -0700549 *
550 * If we can't find it, return a generic substitute when possible.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800551 */
552static ClassObject* lookupClassByDescriptor(const Method* meth,
Andy McFadden62a75162009-04-17 17:23:37 -0700553 const char* pDescriptor, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800554{
555 /*
556 * The javac compiler occasionally puts references to nonexistent
557 * classes in signatures. For example, if you have a non-static
558 * inner class with no constructor, the compiler provides
559 * a private <init> for you. Constructing the class
560 * requires <init>(parent), but the outer class can't call
561 * that because the method is private. So the compiler
562 * generates a package-scope <init>(parent,bogus) method that
563 * just calls the regular <init> (the "bogus" part being necessary
564 * to distinguish the signature of the synthetic method).
565 * Treating the bogus class as an instance of java.lang.Object
566 * allows the verifier to process the class successfully.
567 */
568
569 //LOGI("Looking up '%s'\n", typeStr);
570 ClassObject* clazz;
571 clazz = dvmFindClassNoInit(pDescriptor, meth->clazz->classLoader);
572 if (clazz == NULL) {
573 dvmClearOptException(dvmThreadSelf());
574 if (strchr(pDescriptor, '$') != NULL) {
575 LOGV("VFY: unable to find class referenced in signature (%s)\n",
576 pDescriptor);
577 } else {
578 LOG_VFY("VFY: unable to find class referenced in signature (%s)\n",
579 pDescriptor);
580 }
581
582 if (pDescriptor[0] == '[') {
583 /* We are looking at an array descriptor. */
584
585 /*
586 * There should never be a problem loading primitive arrays.
587 */
588 if (pDescriptor[1] != 'L' && pDescriptor[1] != '[') {
589 LOG_VFY("VFY: invalid char in signature in '%s'\n",
590 pDescriptor);
Andy McFadden62a75162009-04-17 17:23:37 -0700591 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800592 }
593
594 /*
595 * Try to continue with base array type. This will let
596 * us pass basic stuff (e.g. get array len) that wouldn't
597 * fly with an Object. This is NOT correct if the
598 * missing type is a primitive array, but we should never
599 * have a problem loading those. (I'm not convinced this
600 * is correct or even useful. Just use Object here?)
601 */
602 clazz = dvmFindClassNoInit("[Ljava/lang/Object;",
603 meth->clazz->classLoader);
604 } else if (pDescriptor[0] == 'L') {
605 /*
606 * We are looking at a non-array reference descriptor;
607 * try to continue with base reference type.
608 */
609 clazz = gDvm.classJavaLangObject;
610 } else {
611 /* We are looking at a primitive type. */
612 LOG_VFY("VFY: invalid char in signature in '%s'\n", pDescriptor);
Andy McFadden62a75162009-04-17 17:23:37 -0700613 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800614 }
615
616 if (clazz == NULL) {
Andy McFadden62a75162009-04-17 17:23:37 -0700617 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800618 }
619 }
620
621 if (dvmIsPrimitiveClass(clazz)) {
622 LOG_VFY("VFY: invalid use of primitive type '%s'\n", pDescriptor);
Andy McFadden62a75162009-04-17 17:23:37 -0700623 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800624 clazz = NULL;
625 }
626
627 return clazz;
628}
629
630/*
631 * Look up a class reference in a signature. Could be an arg or the
632 * return value.
633 *
634 * Advances "*pSig" to the last character in the signature (that is, to
635 * the ';').
636 *
637 * NOTE: this is also expected to verify the signature.
638 */
639static ClassObject* lookupSignatureClass(const Method* meth, const char** pSig,
Andy McFadden62a75162009-04-17 17:23:37 -0700640 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800641{
642 const char* sig = *pSig;
643 const char* endp = sig;
644
645 assert(sig != NULL && *sig == 'L');
646
647 while (*++endp != ';' && *endp != '\0')
648 ;
649 if (*endp != ';') {
650 LOG_VFY("VFY: bad signature component '%s' (missing ';')\n", sig);
Andy McFadden62a75162009-04-17 17:23:37 -0700651 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800652 return NULL;
653 }
654
655 endp++; /* Advance past the ';'. */
656 int typeLen = endp - sig;
657 char typeStr[typeLen+1]; /* +1 for the '\0' */
658 memcpy(typeStr, sig, typeLen);
659 typeStr[typeLen] = '\0';
660
661 *pSig = endp - 1; /* - 1 so that *pSig points at, not past, the ';' */
662
Andy McFadden62a75162009-04-17 17:23:37 -0700663 return lookupClassByDescriptor(meth, typeStr, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800664}
665
666/*
667 * Look up an array class reference in a signature. Could be an arg or the
668 * return value.
669 *
670 * Advances "*pSig" to the last character in the signature.
671 *
672 * NOTE: this is also expected to verify the signature.
673 */
674static ClassObject* lookupSignatureArrayClass(const Method* meth,
Andy McFadden62a75162009-04-17 17:23:37 -0700675 const char** pSig, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800676{
677 const char* sig = *pSig;
678 const char* endp = sig;
679
680 assert(sig != NULL && *sig == '[');
681
682 /* find the end */
683 while (*++endp == '[' && *endp != '\0')
684 ;
685
686 if (*endp == 'L') {
687 while (*++endp != ';' && *endp != '\0')
688 ;
689 if (*endp != ';') {
690 LOG_VFY("VFY: bad signature component '%s' (missing ';')\n", sig);
Andy McFadden62a75162009-04-17 17:23:37 -0700691 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800692 return NULL;
693 }
694 }
695
696 int typeLen = endp - sig +1;
697 char typeStr[typeLen+1];
698 memcpy(typeStr, sig, typeLen);
699 typeStr[typeLen] = '\0';
700
701 *pSig = endp;
702
Andy McFadden62a75162009-04-17 17:23:37 -0700703 return lookupClassByDescriptor(meth, typeStr, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800704}
705
706/*
707 * Set the register types for the first instruction in the method based on
708 * the method signature.
709 *
710 * This has the side-effect of validating the signature.
711 *
712 * Returns "true" on success.
713 */
714static bool setTypesFromSignature(const Method* meth, RegType* regTypes,
715 UninitInstanceMap* uninitMap)
716{
717 DexParameterIterator iterator;
718 int actualArgs, expectedArgs, argStart;
Andy McFadden62a75162009-04-17 17:23:37 -0700719 VerifyError failure = VERIFY_ERROR_NONE;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800720
721 dexParameterIteratorInit(&iterator, &meth->prototype);
722 argStart = meth->registersSize - meth->insSize;
723 expectedArgs = meth->insSize; /* long/double count as two */
724 actualArgs = 0;
725
726 assert(argStart >= 0); /* should have been verified earlier */
727
728 /*
729 * Include the "this" pointer.
730 */
731 if (!dvmIsStaticMethod(meth)) {
732 /*
733 * If this is a constructor for a class other than java.lang.Object,
734 * mark the first ("this") argument as uninitialized. This restricts
735 * field access until the superclass constructor is called.
736 */
737 if (isInitMethod(meth) && meth->clazz != gDvm.classJavaLangObject) {
738 int uidx = dvmSetUninitInstance(uninitMap, kUninitThisArgAddr,
739 meth->clazz);
740 assert(uidx == 0);
741 regTypes[argStart + actualArgs] = regTypeFromUninitIndex(uidx);
742 } else {
743 regTypes[argStart + actualArgs] = regTypeFromClass(meth->clazz);
744 }
745 actualArgs++;
746 }
747
748 for (;;) {
749 const char* descriptor = dexParameterIteratorNextDescriptor(&iterator);
750
751 if (descriptor == NULL) {
752 break;
753 }
754
755 if (actualArgs >= expectedArgs) {
756 LOG_VFY("VFY: expected %d args, found more (%s)\n",
757 expectedArgs, descriptor);
758 goto bad_sig;
759 }
760
761 switch (*descriptor) {
762 case 'L':
763 case '[':
764 /*
765 * We assume that reference arguments are initialized. The
766 * only way it could be otherwise (assuming the caller was
767 * verified) is if the current method is <init>, but in that
768 * case it's effectively considered initialized the instant
769 * we reach here (in the sense that we can return without
770 * doing anything or call virtual methods).
771 */
772 {
773 ClassObject* clazz =
Andy McFadden62a75162009-04-17 17:23:37 -0700774 lookupClassByDescriptor(meth, descriptor, &failure);
775 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800776 goto bad_sig;
777 regTypes[argStart + actualArgs] = regTypeFromClass(clazz);
778 }
779 actualArgs++;
780 break;
781 case 'Z':
782 regTypes[argStart + actualArgs] = kRegTypeBoolean;
783 actualArgs++;
784 break;
785 case 'C':
786 regTypes[argStart + actualArgs] = kRegTypeChar;
787 actualArgs++;
788 break;
789 case 'B':
790 regTypes[argStart + actualArgs] = kRegTypeByte;
791 actualArgs++;
792 break;
793 case 'I':
794 regTypes[argStart + actualArgs] = kRegTypeInteger;
795 actualArgs++;
796 break;
797 case 'S':
798 regTypes[argStart + actualArgs] = kRegTypeShort;
799 actualArgs++;
800 break;
801 case 'F':
802 regTypes[argStart + actualArgs] = kRegTypeFloat;
803 actualArgs++;
804 break;
805 case 'D':
806 regTypes[argStart + actualArgs] = kRegTypeDoubleLo;
807 regTypes[argStart + actualArgs +1] = kRegTypeDoubleHi;
808 actualArgs += 2;
809 break;
810 case 'J':
811 regTypes[argStart + actualArgs] = kRegTypeLongLo;
812 regTypes[argStart + actualArgs +1] = kRegTypeLongHi;
813 actualArgs += 2;
814 break;
815 default:
816 LOG_VFY("VFY: unexpected signature type char '%c'\n", *descriptor);
817 goto bad_sig;
818 }
819 }
820
821 if (actualArgs != expectedArgs) {
822 LOG_VFY("VFY: expected %d args, found %d\n", expectedArgs, actualArgs);
823 goto bad_sig;
824 }
825
826 const char* descriptor = dexProtoGetReturnType(&meth->prototype);
827
828 /*
829 * Validate return type. We don't do the type lookup; just want to make
830 * sure that it has the right format. Only major difference from the
831 * method argument format is that 'V' is supported.
832 */
833 switch (*descriptor) {
834 case 'I':
835 case 'C':
836 case 'S':
837 case 'B':
838 case 'Z':
839 case 'V':
840 case 'F':
841 case 'D':
842 case 'J':
843 if (*(descriptor+1) != '\0')
844 goto bad_sig;
845 break;
846 case '[':
847 /* single/multi, object/primitive */
848 while (*++descriptor == '[')
849 ;
850 if (*descriptor == 'L') {
851 while (*++descriptor != ';' && *descriptor != '\0')
852 ;
853 if (*descriptor != ';')
854 goto bad_sig;
855 } else {
856 if (*(descriptor+1) != '\0')
857 goto bad_sig;
858 }
859 break;
860 case 'L':
861 /* could be more thorough here, but shouldn't be required */
862 while (*++descriptor != ';' && *descriptor != '\0')
863 ;
864 if (*descriptor != ';')
865 goto bad_sig;
866 break;
867 default:
868 goto bad_sig;
869 }
870
871 return true;
872
873//fail:
874// LOG_VFY_METH(meth, "VFY: bad sig\n");
875// return false;
876
877bad_sig:
878 {
879 char* desc = dexProtoCopyMethodDescriptor(&meth->prototype);
880 LOG_VFY("VFY: bad signature '%s' for %s.%s\n",
881 desc, meth->clazz->descriptor, meth->name);
882 free(desc);
883 }
884 return false;
885}
886
887/*
888 * Return the register type for the method. We can't just use the
889 * already-computed DalvikJniReturnType, because if it's a reference type
890 * we need to do the class lookup.
891 *
892 * Returned references are assumed to be initialized.
893 *
894 * Returns kRegTypeUnknown for "void".
895 */
896static RegType getMethodReturnType(const Method* meth)
897{
898 RegType type;
899 const char* descriptor = dexProtoGetReturnType(&meth->prototype);
900
901 switch (*descriptor) {
902 case 'I':
903 type = kRegTypeInteger;
904 break;
905 case 'C':
906 type = kRegTypeChar;
907 break;
908 case 'S':
909 type = kRegTypeShort;
910 break;
911 case 'B':
912 type = kRegTypeByte;
913 break;
914 case 'Z':
915 type = kRegTypeBoolean;
916 break;
917 case 'V':
918 type = kRegTypeUnknown;
919 break;
920 case 'F':
921 type = kRegTypeFloat;
922 break;
923 case 'D':
924 type = kRegTypeDoubleLo;
925 break;
926 case 'J':
927 type = kRegTypeLongLo;
928 break;
929 case 'L':
930 case '[':
931 {
Andy McFadden62a75162009-04-17 17:23:37 -0700932 VerifyError failure = VERIFY_ERROR_NONE;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800933 ClassObject* clazz =
Andy McFadden62a75162009-04-17 17:23:37 -0700934 lookupClassByDescriptor(meth, descriptor, &failure);
935 assert(VERIFY_OK(failure));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800936 type = regTypeFromClass(clazz);
937 }
938 break;
939 default:
940 /* we verified signature return type earlier, so this is impossible */
941 assert(false);
942 type = kRegTypeConflict;
943 break;
944 }
945
946 return type;
947}
948
949/*
950 * Convert a single-character signature value (i.e. a primitive type) to
951 * the corresponding RegType. This is intended for access to object fields
952 * holding primitive types.
953 *
954 * Returns kRegTypeUnknown for objects, arrays, and void.
955 */
956static RegType primSigCharToRegType(char sigChar)
957{
958 RegType type;
959
960 switch (sigChar) {
961 case 'I':
962 type = kRegTypeInteger;
963 break;
964 case 'C':
965 type = kRegTypeChar;
966 break;
967 case 'S':
968 type = kRegTypeShort;
969 break;
970 case 'B':
971 type = kRegTypeByte;
972 break;
973 case 'Z':
974 type = kRegTypeBoolean;
975 break;
976 case 'F':
977 type = kRegTypeFloat;
978 break;
979 case 'D':
980 type = kRegTypeDoubleLo;
981 break;
982 case 'J':
983 type = kRegTypeLongLo;
984 break;
985 case 'V':
986 case 'L':
987 case '[':
988 type = kRegTypeUnknown;
989 break;
990 default:
991 assert(false);
992 type = kRegTypeUnknown;
993 break;
994 }
995
996 return type;
997}
998
999/*
1000 * Verify the arguments to a method. We're executing in "method", making
1001 * a call to the method reference in vB.
1002 *
1003 * If this is a "direct" invoke, we allow calls to <init>. For calls to
1004 * <init>, the first argument may be an uninitialized reference. Otherwise,
1005 * calls to anything starting with '<' will be rejected, as will any
1006 * uninitialized reference arguments.
1007 *
1008 * For non-static method calls, this will verify that the method call is
1009 * appropriate for the "this" argument.
1010 *
1011 * The method reference is in vBBBB. The "isRange" parameter determines
1012 * whether we use 0-4 "args" values or a range of registers defined by
1013 * vAA and vCCCC.
1014 *
1015 * Widening conversions on integers and references are allowed, but
1016 * narrowing conversions are not.
1017 *
Andy McFadden62a75162009-04-17 17:23:37 -07001018 * Returns the resolved method on success, NULL on failure (with *pFailure
1019 * set appropriately).
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001020 */
1021static Method* verifyInvocationArgs(const Method* meth, const RegType* insnRegs,
1022 const int insnRegCount, const DecodedInstruction* pDecInsn,
1023 UninitInstanceMap* uninitMap, MethodType methodType, bool isRange,
Andy McFadden62a75162009-04-17 17:23:37 -07001024 bool isSuper, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001025{
1026 Method* resMethod;
1027 char* sigOriginal = NULL;
1028
1029 /*
1030 * Resolve the method. This could be an abstract or concrete method
1031 * depending on what sort of call we're making.
1032 */
1033 if (methodType == METHOD_INTERFACE) {
1034 resMethod = dvmOptResolveInterfaceMethod(meth->clazz, pDecInsn->vB);
1035 } else {
Andy McFadden62a75162009-04-17 17:23:37 -07001036 resMethod = dvmOptResolveMethod(meth->clazz, pDecInsn->vB, methodType,
1037 pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001038 }
1039 if (resMethod == NULL) {
1040 /* failed; print a meaningful failure message */
1041 DexFile* pDexFile = meth->clazz->pDvmDex->pDexFile;
1042 const DexMethodId* pMethodId;
1043 const char* methodName;
1044 char* methodDesc;
1045 const char* classDescriptor;
1046
1047 pMethodId = dexGetMethodId(pDexFile, pDecInsn->vB);
1048 methodName = dexStringById(pDexFile, pMethodId->nameIdx);
1049 methodDesc = dexCopyDescriptorFromMethodId(pDexFile, pMethodId);
1050 classDescriptor = dexStringByTypeIdx(pDexFile, pMethodId->classIdx);
1051
1052 if (!gDvm.optimizing) {
1053 char* dotMissingClass = dvmDescriptorToDot(classDescriptor);
1054 char* dotMethClass = dvmDescriptorToDot(meth->clazz->descriptor);
1055 //char* curMethodDesc =
1056 // dexProtoCopyMethodDescriptor(&meth->prototype);
1057
Andy McFaddenb51ea112009-05-08 16:50:17 -07001058 LOGI("Could not find method %s.%s, referenced from "
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001059 "method %s.%s\n",
1060 dotMissingClass, methodName/*, methodDesc*/,
1061 dotMethClass, meth->name/*, curMethodDesc*/);
1062
1063 free(dotMissingClass);
1064 free(dotMethClass);
1065 //free(curMethodDesc);
1066 }
1067
1068 LOG_VFY("VFY: unable to resolve %s method %u: %s.%s %s\n",
1069 dvmMethodTypeStr(methodType), pDecInsn->vB,
1070 classDescriptor, methodName, methodDesc);
1071 free(methodDesc);
Andy McFaddenb51ea112009-05-08 16:50:17 -07001072 if (VERIFY_OK(*pFailure)) /* not set for interface resolve */
1073 *pFailure = VERIFY_ERROR_NO_METHOD;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001074 goto fail;
1075 }
1076
1077 /*
1078 * Only time you can explicitly call a method starting with '<' is when
1079 * making a "direct" invocation on "<init>". There are additional
1080 * restrictions but we don't enforce them here.
1081 */
1082 if (resMethod->name[0] == '<') {
1083 if (methodType != METHOD_DIRECT || !isInitMethod(resMethod)) {
1084 LOG_VFY("VFY: invalid call to %s.%s\n",
1085 resMethod->clazz->descriptor, resMethod->name);
1086 goto bad_sig;
1087 }
1088 }
1089
1090 /*
1091 * If we're using invoke-super(method), make sure that the executing
1092 * method's class' superclass has a vtable entry for the target method.
1093 */
1094 if (isSuper) {
1095 assert(methodType == METHOD_VIRTUAL);
1096 ClassObject* super = meth->clazz->super;
1097 if (super == NULL || resMethod->methodIndex > super->vtableCount) {
1098 char* desc = dexProtoCopyMethodDescriptor(&resMethod->prototype);
1099 LOG_VFY("VFY: invalid invoke-super from %s.%s to super %s.%s %s\n",
1100 meth->clazz->descriptor, meth->name,
1101 (super == NULL) ? "-" : super->descriptor,
1102 resMethod->name, desc);
1103 free(desc);
Andy McFadden62a75162009-04-17 17:23:37 -07001104 *pFailure = VERIFY_ERROR_NO_METHOD;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001105 goto fail;
1106 }
1107 }
1108
1109 /*
1110 * We use vAA as our expected arg count, rather than resMethod->insSize,
1111 * because we need to match the call to the signature. Also, we might
1112 * might be calling through an abstract method definition (which doesn't
1113 * have register count values).
1114 */
1115 sigOriginal = dexProtoCopyMethodDescriptor(&resMethod->prototype);
1116 const char* sig = sigOriginal;
1117 int expectedArgs = pDecInsn->vA;
1118 int actualArgs = 0;
1119
1120 if (!isRange && expectedArgs > 5) {
1121 LOG_VFY("VFY: invalid arg count in non-range invoke (%d)\n",
1122 pDecInsn->vA);
1123 goto fail;
1124 }
1125 if (expectedArgs > meth->outsSize) {
1126 LOG_VFY("VFY: invalid arg count (%d) exceeds outsSize (%d)\n",
1127 expectedArgs, meth->outsSize);
1128 goto fail;
1129 }
1130
1131 if (*sig++ != '(')
1132 goto bad_sig;
1133
1134 /*
1135 * Check the "this" argument, which must be an instance of the class
1136 * that declared the method. For an interface class, we don't do the
1137 * full interface merge, so we can't do a rigorous check here (which
1138 * is okay since we have to do it at runtime).
1139 */
1140 if (!dvmIsStaticMethod(resMethod)) {
1141 ClassObject* actualThisRef;
1142 RegType actualArgType;
1143
1144 actualArgType = getInvocationThis(insnRegs, insnRegCount, pDecInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07001145 pFailure);
1146 if (!VERIFY_OK(*pFailure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001147 goto fail;
1148
1149 if (regTypeIsUninitReference(actualArgType) && resMethod->name[0] != '<')
1150 {
1151 LOG_VFY("VFY: 'this' arg must be initialized\n");
1152 goto fail;
1153 }
1154 if (methodType != METHOD_INTERFACE && actualArgType != kRegTypeZero) {
1155 actualThisRef = regTypeReferenceToClass(actualArgType, uninitMap);
1156 if (!dvmInstanceof(actualThisRef, resMethod->clazz)) {
1157 LOG_VFY("VFY: 'this' arg '%s' not instance of '%s'\n",
1158 actualThisRef->descriptor,
1159 resMethod->clazz->descriptor);
1160 goto fail;
1161 }
1162 }
1163 actualArgs++;
1164 }
1165
1166 /*
1167 * Process the target method's signature. This signature may or may not
1168 * have been verified, so we can't assume it's properly formed.
1169 */
1170 while (*sig != '\0' && *sig != ')') {
1171 if (actualArgs >= expectedArgs) {
1172 LOG_VFY("VFY: expected %d args, found more (%c)\n",
1173 expectedArgs, *sig);
1174 goto bad_sig;
1175 }
1176
1177 u4 getReg;
1178 if (isRange)
1179 getReg = pDecInsn->vC + actualArgs;
1180 else
1181 getReg = pDecInsn->arg[actualArgs];
1182
1183 switch (*sig) {
1184 case 'L':
1185 {
Andy McFadden62a75162009-04-17 17:23:37 -07001186 ClassObject* clazz = lookupSignatureClass(meth, &sig, pFailure);
1187 if (!VERIFY_OK(*pFailure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001188 goto bad_sig;
1189 verifyRegisterType(insnRegs, insnRegCount, getReg,
Andy McFadden62a75162009-04-17 17:23:37 -07001190 regTypeFromClass(clazz), pFailure);
1191 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001192 LOG_VFY("VFY: bad arg %d (into %s)\n",
1193 actualArgs, clazz->descriptor);
1194 goto bad_sig;
1195 }
1196 }
1197 actualArgs++;
1198 break;
1199 case '[':
1200 {
1201 ClassObject* clazz =
Andy McFadden62a75162009-04-17 17:23:37 -07001202 lookupSignatureArrayClass(meth, &sig, pFailure);
1203 if (!VERIFY_OK(*pFailure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001204 goto bad_sig;
1205 verifyRegisterType(insnRegs, insnRegCount, getReg,
Andy McFadden62a75162009-04-17 17:23:37 -07001206 regTypeFromClass(clazz), pFailure);
1207 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001208 LOG_VFY("VFY: bad arg %d (into %s)\n",
1209 actualArgs, clazz->descriptor);
1210 goto bad_sig;
1211 }
1212 }
1213 actualArgs++;
1214 break;
1215 case 'Z':
1216 verifyRegisterType(insnRegs, insnRegCount, getReg,
Andy McFadden62a75162009-04-17 17:23:37 -07001217 kRegTypeBoolean, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001218 actualArgs++;
1219 break;
1220 case 'C':
1221 verifyRegisterType(insnRegs, insnRegCount, getReg,
Andy McFadden62a75162009-04-17 17:23:37 -07001222 kRegTypeChar, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001223 actualArgs++;
1224 break;
1225 case 'B':
1226 verifyRegisterType(insnRegs, insnRegCount, getReg,
Andy McFadden62a75162009-04-17 17:23:37 -07001227 kRegTypeByte, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001228 actualArgs++;
1229 break;
1230 case 'I':
1231 verifyRegisterType(insnRegs, insnRegCount, getReg,
Andy McFadden62a75162009-04-17 17:23:37 -07001232 kRegTypeInteger, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001233 actualArgs++;
1234 break;
1235 case 'S':
1236 verifyRegisterType(insnRegs, insnRegCount, getReg,
Andy McFadden62a75162009-04-17 17:23:37 -07001237 kRegTypeShort, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001238 actualArgs++;
1239 break;
1240 case 'F':
1241 verifyRegisterType(insnRegs, insnRegCount, getReg,
Andy McFadden62a75162009-04-17 17:23:37 -07001242 kRegTypeFloat, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001243 actualArgs++;
1244 break;
1245 case 'D':
1246 verifyRegisterType(insnRegs, insnRegCount, getReg,
Andy McFadden62a75162009-04-17 17:23:37 -07001247 kRegTypeDoubleLo, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001248 actualArgs += 2;
1249 break;
1250 case 'J':
1251 verifyRegisterType(insnRegs, insnRegCount, getReg,
Andy McFadden62a75162009-04-17 17:23:37 -07001252 kRegTypeLongLo, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001253 actualArgs += 2;
1254 break;
1255 default:
1256 LOG_VFY("VFY: invocation target: bad signature type char '%c'\n",
1257 *sig);
1258 goto bad_sig;
1259 }
1260
1261 sig++;
1262 }
1263 if (*sig != ')') {
1264 char* desc = dexProtoCopyMethodDescriptor(&resMethod->prototype);
1265 LOG_VFY("VFY: invocation target: bad signature '%s'\n", desc);
1266 free(desc);
1267 goto bad_sig;
1268 }
1269
1270 if (actualArgs != expectedArgs) {
1271 LOG_VFY("VFY: expected %d args, found %d\n", expectedArgs, actualArgs);
1272 goto bad_sig;
1273 }
1274
1275 free(sigOriginal);
1276 return resMethod;
1277
1278bad_sig:
1279 if (resMethod != NULL) {
1280 char* desc = dexProtoCopyMethodDescriptor(&resMethod->prototype);
1281 LOG_VFY("VFY: rejecting call to %s.%s %s\n",
Andy McFadden62a75162009-04-17 17:23:37 -07001282 resMethod->clazz->descriptor, resMethod->name, desc);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001283 free(desc);
1284 }
1285
1286fail:
1287 free(sigOriginal);
Andy McFadden62a75162009-04-17 17:23:37 -07001288 if (*pFailure == VERIFY_ERROR_NONE)
1289 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001290 return NULL;
1291}
1292
1293/*
1294 * Get the class object for the type of data stored in a field. This isn't
1295 * stored in the Field struct, so we have to recover it from the signature.
1296 *
1297 * This only works for reference types. Don't call this for primitive types.
1298 *
1299 * If we can't find the class, we return java.lang.Object, so that
1300 * verification can continue if a field is only accessed in trivial ways.
1301 */
1302static ClassObject* getFieldClass(const Method* meth, const Field* field)
1303{
1304 ClassObject* fieldClass;
1305 const char* signature = field->signature;
1306
1307 if ((*signature == 'L') || (*signature == '[')) {
1308 fieldClass = dvmFindClassNoInit(signature,
1309 meth->clazz->classLoader);
1310 } else {
1311 return NULL;
1312 }
1313
1314 if (fieldClass == NULL) {
1315 dvmClearOptException(dvmThreadSelf());
1316 LOGV("VFY: unable to find class '%s' for field %s.%s, trying Object\n",
1317 field->signature, meth->clazz->descriptor, field->name);
1318 fieldClass = gDvm.classJavaLangObject;
1319 } else {
1320 assert(!dvmIsPrimitiveClass(fieldClass));
1321 }
1322 return fieldClass;
1323}
1324
1325
1326/*
1327 * ===========================================================================
1328 * Register operations
1329 * ===========================================================================
1330 */
1331
1332/*
1333 * Get the type of register N, verifying that the register is valid.
1334 *
Andy McFadden62a75162009-04-17 17:23:37 -07001335 * Sets "*pFailure" appropriately if the register number is out of range.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001336 */
1337static inline RegType getRegisterType(const RegType* insnRegs,
Andy McFadden62a75162009-04-17 17:23:37 -07001338 const int insnRegCount, u4 vsrc, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001339{
1340 RegType type;
1341
1342 if (vsrc >= (u4) insnRegCount) {
Andy McFadden62a75162009-04-17 17:23:37 -07001343 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001344 return kRegTypeUnknown;
1345 } else {
1346 return insnRegs[vsrc];
1347 }
1348}
1349
1350/*
1351 * Get the value from a register, and cast it to a ClassObject. Sets
Andy McFadden62a75162009-04-17 17:23:37 -07001352 * "*pFailure" if something fails.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001353 *
1354 * This fails if the register holds an uninitialized class.
1355 *
1356 * If the register holds kRegTypeZero, this returns a NULL pointer.
1357 */
1358static ClassObject* getClassFromRegister(const RegType* insnRegs,
Andy McFadden62a75162009-04-17 17:23:37 -07001359 const int insnRegCount, u4 vsrc, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001360{
1361 ClassObject* clazz = NULL;
1362 RegType type;
1363
1364 /* get the element type of the array held in vsrc */
Andy McFadden62a75162009-04-17 17:23:37 -07001365 type = getRegisterType(insnRegs, insnRegCount, vsrc, pFailure);
1366 if (!VERIFY_OK(*pFailure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001367 goto bail;
1368
1369 /* if "always zero", we allow it to fail at runtime */
1370 if (type == kRegTypeZero)
1371 goto bail;
1372
1373 if (!regTypeIsReference(type)) {
1374 LOG_VFY("VFY: tried to get class from non-ref register v%d (type=%d)\n",
1375 vsrc, type);
Andy McFadden62a75162009-04-17 17:23:37 -07001376 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001377 goto bail;
1378 }
1379 if (regTypeIsUninitReference(type)) {
1380 LOG_VFY("VFY: register %u holds uninitialized reference\n", vsrc);
Andy McFadden62a75162009-04-17 17:23:37 -07001381 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001382 goto bail;
1383 }
1384
1385 clazz = regTypeInitializedReferenceToClass(type);
1386
1387bail:
1388 return clazz;
1389}
1390
1391/*
1392 * Get the "this" pointer from a non-static method invocation. This
1393 * returns the RegType so the caller can decide whether it needs the
1394 * reference to be initialized or not. (Can also return kRegTypeZero
1395 * if the reference can only be zero at this point.)
1396 *
1397 * The argument count is in vA, and the first argument is in vC, for both
1398 * "simple" and "range" versions. We just need to make sure vA is >= 1
1399 * and then return vC.
1400 */
1401static RegType getInvocationThis(const RegType* insnRegs,
Andy McFadden62a75162009-04-17 17:23:37 -07001402 const int insnRegCount, const DecodedInstruction* pDecInsn,
1403 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001404{
1405 RegType thisType = kRegTypeUnknown;
1406
1407 if (pDecInsn->vA < 1) {
1408 LOG_VFY("VFY: invoke lacks 'this'\n");
Andy McFadden62a75162009-04-17 17:23:37 -07001409 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001410 goto bail;
1411 }
1412
1413 /* get the element type of the array held in vsrc */
Andy McFadden62a75162009-04-17 17:23:37 -07001414 thisType = getRegisterType(insnRegs, insnRegCount, pDecInsn->vC, pFailure);
1415 if (!VERIFY_OK(*pFailure)) {
1416 LOG_VFY("VFY: failed to get 'this' from register %u\n", pDecInsn->vC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001417 goto bail;
1418 }
1419
1420 if (!regTypeIsReference(thisType)) {
1421 LOG_VFY("VFY: tried to get class from non-ref register v%d (type=%d)\n",
1422 pDecInsn->vC, thisType);
Andy McFadden62a75162009-04-17 17:23:37 -07001423 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001424 goto bail;
1425 }
1426
1427bail:
1428 return thisType;
1429}
1430
1431/*
1432 * Set the type of register N, verifying that the register is valid. If
1433 * "newType" is the "Lo" part of a 64-bit value, register N+1 will be
1434 * set to "newType+1".
1435 *
Andy McFadden62a75162009-04-17 17:23:37 -07001436 * Sets "*pFailure" if the register number is out of range.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001437 */
1438static void setRegisterType(RegType* insnRegs, const int insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07001439 u4 vdst, RegType newType, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001440{
1441 //LOGD("set-reg v%u = %d\n", vdst, newType);
1442 switch (newType) {
1443 case kRegTypeUnknown:
1444 case kRegTypeBoolean:
1445 case kRegTypeOne:
1446 case kRegTypeByte:
1447 case kRegTypePosByte:
1448 case kRegTypeShort:
1449 case kRegTypePosShort:
1450 case kRegTypeChar:
1451 case kRegTypeInteger:
1452 case kRegTypeFloat:
1453 case kRegTypeZero:
1454 if (vdst >= (u4) insnRegCount) {
Andy McFadden62a75162009-04-17 17:23:37 -07001455 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001456 } else {
1457 insnRegs[vdst] = newType;
1458 }
1459 break;
1460 case kRegTypeLongLo:
1461 case kRegTypeDoubleLo:
1462 if (vdst+1 >= (u4) insnRegCount) {
Andy McFadden62a75162009-04-17 17:23:37 -07001463 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001464 } else {
1465 insnRegs[vdst] = newType;
1466 insnRegs[vdst+1] = newType+1;
1467 }
1468 break;
1469 case kRegTypeLongHi:
1470 case kRegTypeDoubleHi:
1471 /* should never set these explicitly */
Andy McFadden62a75162009-04-17 17:23:37 -07001472 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001473 break;
1474
1475 case kRegTypeUninit:
1476 default:
1477 if (regTypeIsReference(newType)) {
1478 if (vdst >= (u4) insnRegCount) {
Andy McFadden62a75162009-04-17 17:23:37 -07001479 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001480 break;
1481 }
1482 insnRegs[vdst] = newType;
1483
1484 /*
1485 * In most circumstances we won't see a reference to a primitive
1486 * class here (e.g. "D"), since that would mean the object in the
1487 * register is actually a primitive type. It can happen as the
1488 * result of an assumed-successful check-cast instruction in
1489 * which the second argument refers to a primitive class. (In
1490 * practice, such an instruction will always throw an exception.)
1491 *
1492 * This is not an issue for instructions like const-class, where
1493 * the object in the register is a java.lang.Class instance.
1494 */
1495 break;
1496 }
1497 /* bad - fall through */
1498
1499 case kRegTypeConflict: // should only be set during a merge
1500 LOG_VFY("Unexpected set type %d\n", newType);
1501 assert(false);
Andy McFadden62a75162009-04-17 17:23:37 -07001502 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001503 break;
1504 }
1505}
1506
1507/*
1508 * Verify that the contents of the specified register have the specified
1509 * type (or can be converted to it through an implicit widening conversion).
1510 *
1511 * In theory we could use this to modify the type of the source register,
1512 * e.g. a generic 32-bit constant, once used as a float, would thereafter
1513 * remain a float. There is no compelling reason to require this though.
1514 *
1515 * If "vsrc" is a reference, both it and the "vsrc" register must be
1516 * initialized ("vsrc" may be Zero). This will verify that the value in
1517 * the register is an instance of checkType, or if checkType is an
1518 * interface, verify that the register implements checkType.
1519 */
1520static void verifyRegisterType(const RegType* insnRegs, const int insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07001521 u4 vsrc, RegType checkType, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001522{
1523 if (vsrc >= (u4) insnRegCount) {
Andy McFadden62a75162009-04-17 17:23:37 -07001524 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001525 return;
1526 }
1527
1528 RegType srcType = insnRegs[vsrc];
1529
1530 //LOGD("check-reg v%u = %d\n", vsrc, checkType);
1531 switch (checkType) {
1532 case kRegTypeFloat:
1533 case kRegTypeBoolean:
1534 case kRegTypePosByte:
1535 case kRegTypeByte:
1536 case kRegTypePosShort:
1537 case kRegTypeShort:
1538 case kRegTypeChar:
1539 case kRegTypeInteger:
1540 if (!canConvertTo1nr(srcType, checkType)) {
1541 LOG_VFY("VFY: register1 v%u type %d, wanted %d\n",
1542 vsrc, srcType, checkType);
Andy McFadden62a75162009-04-17 17:23:37 -07001543 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001544 }
1545 break;
1546 case kRegTypeLongLo:
1547 case kRegTypeDoubleLo:
1548 if (vsrc+1 >= (u4) insnRegCount) {
1549 LOG_VFY("VFY: register2 v%u out of range (%d)\n",
1550 vsrc, insnRegCount);
Andy McFadden62a75162009-04-17 17:23:37 -07001551 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001552 } else if (insnRegs[vsrc+1] != srcType+1) {
1553 LOG_VFY("VFY: register2 v%u-%u values %d,%d\n",
1554 vsrc, vsrc+1, insnRegs[vsrc], insnRegs[vsrc+1]);
Andy McFadden62a75162009-04-17 17:23:37 -07001555 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001556 } else if (!canConvertTo2(srcType, checkType)) {
1557 LOG_VFY("VFY: register2 v%u type %d, wanted %d\n",
1558 vsrc, srcType, checkType);
Andy McFadden62a75162009-04-17 17:23:37 -07001559 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001560 }
1561 break;
1562
1563 case kRegTypeLongHi:
1564 case kRegTypeDoubleHi:
1565 case kRegTypeZero:
1566 case kRegTypeOne:
1567 case kRegTypeUnknown:
1568 case kRegTypeConflict:
1569 /* should never be checking for these explicitly */
1570 assert(false);
Andy McFadden62a75162009-04-17 17:23:37 -07001571 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001572 return;
1573 case kRegTypeUninit:
1574 default:
1575 /* make sure checkType is initialized reference */
1576 if (!regTypeIsReference(checkType)) {
1577 LOG_VFY("VFY: unexpected check type %d\n", checkType);
1578 assert(false);
Andy McFadden62a75162009-04-17 17:23:37 -07001579 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001580 break;
1581 }
1582 if (regTypeIsUninitReference(checkType)) {
1583 LOG_VFY("VFY: uninitialized ref not expected as reg check\n");
Andy McFadden62a75162009-04-17 17:23:37 -07001584 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001585 break;
1586 }
1587 /* make sure srcType is initialized reference or always-NULL */
1588 if (!regTypeIsReference(srcType)) {
1589 LOG_VFY("VFY: register1 v%u type %d, wanted ref\n", vsrc, srcType);
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(srcType)) {
1594 LOG_VFY("VFY: register1 v%u holds uninitialized ref\n", vsrc);
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 /* if the register isn't Zero, make sure it's an instance of check */
1599 if (srcType != kRegTypeZero) {
1600 ClassObject* srcClass = regTypeInitializedReferenceToClass(srcType);
1601 ClassObject* checkClass = regTypeInitializedReferenceToClass(checkType);
1602 assert(srcClass != NULL);
1603 assert(checkClass != NULL);
1604
1605 if (dvmIsInterfaceClass(checkClass)) {
1606 /*
1607 * All objects implement all interfaces as far as the
1608 * verifier is concerned. The runtime has to sort it out.
1609 * See comments above findCommonSuperclass.
1610 */
1611 /*
1612 if (srcClass != checkClass &&
1613 !dvmImplements(srcClass, checkClass))
1614 {
1615 LOG_VFY("VFY: %s does not implement %s\n",
1616 srcClass->descriptor, checkClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07001617 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001618 }
1619 */
1620 } else {
1621 if (!dvmInstanceof(srcClass, checkClass)) {
1622 LOG_VFY("VFY: %s is not instance of %s\n",
1623 srcClass->descriptor, checkClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07001624 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001625 }
1626 }
1627 }
1628 break;
1629 }
1630}
1631
1632/*
1633 * Set the type of the "result" register. Mostly this exists to expand
1634 * "insnRegCount" to encompass the result register.
1635 */
1636static void setResultRegisterType(RegType* insnRegs, const int insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07001637 RegType newType, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001638{
1639 setRegisterType(insnRegs, insnRegCount + kExtraRegs,
Andy McFadden62a75162009-04-17 17:23:37 -07001640 RESULT_REGISTER(insnRegCount), newType, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001641}
1642
1643
1644/*
1645 * Update all registers holding "uninitType" to instead hold the
1646 * corresponding initialized reference type. This is called when an
1647 * appropriate <init> method is invoked -- all copies of the reference
1648 * must be marked as initialized.
1649 */
1650static void markRefsAsInitialized(RegType* insnRegs, int insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07001651 UninitInstanceMap* uninitMap, RegType uninitType, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001652{
1653 ClassObject* clazz;
1654 RegType initType;
1655 int i, changed;
1656
1657 clazz = dvmGetUninitInstance(uninitMap, regTypeToUninitIndex(uninitType));
1658 if (clazz == NULL) {
1659 LOGE("VFY: unable to find type=0x%x (idx=%d)\n",
1660 uninitType, regTypeToUninitIndex(uninitType));
Andy McFadden62a75162009-04-17 17:23:37 -07001661 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001662 return;
1663 }
1664 initType = regTypeFromClass(clazz);
1665
1666 changed = 0;
1667 for (i = 0; i < insnRegCount; i++) {
1668 if (insnRegs[i] == uninitType) {
1669 insnRegs[i] = initType;
1670 changed++;
1671 }
1672 }
1673 //LOGD("VFY: marked %d registers as initialized\n", changed);
1674 assert(changed > 0);
1675
1676 return;
1677}
1678
1679/*
1680 * We're creating a new instance of class C at address A. Any registers
1681 * holding instances previously created at address A must be initialized
1682 * by now. If not, we mark them as "conflict" to prevent them from being
1683 * used (otherwise, markRefsAsInitialized would mark the old ones and the
1684 * new ones at the same time).
1685 */
1686static void markUninitRefsAsInvalid(RegType* insnRegs, int insnRegCount,
1687 UninitInstanceMap* uninitMap, RegType uninitType)
1688{
1689 int i, changed;
1690
1691 changed = 0;
1692 for (i = 0; i < insnRegCount; i++) {
1693 if (insnRegs[i] == uninitType) {
1694 insnRegs[i] = kRegTypeConflict;
1695 changed++;
1696 }
1697 }
1698
1699 //if (changed)
1700 // LOGD("VFY: marked %d uninitialized registers as invalid\n", changed);
1701}
1702
1703/*
1704 * Find the start of the register set for the specified instruction in
1705 * the current method.
1706 */
1707static inline RegType* getRegisterLine(const RegisterTable* regTable,
1708 int insnIdx)
1709{
1710 return regTable->addrRegs[insnIdx];
1711}
1712
1713/*
1714 * Copy a bunch of registers.
1715 */
1716static inline void copyRegisters(RegType* dst, const RegType* src,
1717 int numRegs)
1718{
1719 memcpy(dst, src, numRegs * sizeof(RegType));
1720}
1721
1722/*
1723 * Compare a bunch of registers.
1724 *
1725 * Returns 0 if they match. Using this for a sort is unwise, since the
1726 * value can change based on machine endianness.
1727 */
1728static inline int compareRegisters(const RegType* src1, const RegType* src2,
1729 int numRegs)
1730{
1731 return memcmp(src1, src2, numRegs * sizeof(RegType));
1732}
1733
1734/*
1735 * Register type categories, for type checking.
1736 *
1737 * The spec says category 1 includes boolean, byte, char, short, int, float,
1738 * reference, and returnAddress. Category 2 includes long and double.
1739 *
1740 * We treat object references separately, so we have "category1nr". We
1741 * don't support jsr/ret, so there is no "returnAddress" type.
1742 */
1743typedef enum TypeCategory {
1744 kTypeCategoryUnknown = 0,
1745 kTypeCategory1nr, // byte, char, int, float, boolean
1746 kTypeCategory2, // long, double
1747 kTypeCategoryRef, // object reference
1748} TypeCategory;
1749
1750/*
1751 * See if "type" matches "cat". All we're really looking for here is that
1752 * we're not mixing and matching 32-bit and 64-bit quantities, and we're
1753 * not mixing references with numerics. (For example, the arguments to
1754 * "a < b" could be integers of different sizes, but they must both be
1755 * integers. Dalvik is less specific about int vs. float, so we treat them
1756 * as equivalent here.)
1757 *
1758 * For category 2 values, "type" must be the "low" half of the value.
1759 *
Andy McFadden62a75162009-04-17 17:23:37 -07001760 * Sets "*pFailure" if something looks wrong.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001761 */
Andy McFadden62a75162009-04-17 17:23:37 -07001762static void checkTypeCategory(RegType type, TypeCategory cat,
1763 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001764{
1765 switch (cat) {
1766 case kTypeCategory1nr:
1767 switch (type) {
1768 case kRegTypeFloat:
1769 case kRegTypeZero:
1770 case kRegTypeOne:
1771 case kRegTypeBoolean:
1772 case kRegTypePosByte:
1773 case kRegTypeByte:
1774 case kRegTypePosShort:
1775 case kRegTypeShort:
1776 case kRegTypeChar:
1777 case kRegTypeInteger:
1778 break;
1779 default:
Andy McFadden62a75162009-04-17 17:23:37 -07001780 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001781 break;
1782 }
1783 break;
1784
1785 case kTypeCategory2:
1786 switch (type) {
1787 case kRegTypeLongLo:
1788 case kRegTypeDoubleLo:
1789 break;
1790 default:
Andy McFadden62a75162009-04-17 17:23:37 -07001791 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001792 break;
1793 }
1794 break;
1795
1796 case kTypeCategoryRef:
1797 if (type != kRegTypeZero && !regTypeIsReference(type))
Andy McFadden62a75162009-04-17 17:23:37 -07001798 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001799 break;
1800
1801 default:
1802 assert(false);
Andy McFadden62a75162009-04-17 17:23:37 -07001803 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001804 break;
1805 }
1806}
1807
1808/*
1809 * For a category 2 register pair, verify that "typeh" is the appropriate
1810 * high part for "typel".
1811 *
1812 * Does not verify that "typel" is in fact the low part of a 64-bit
1813 * register pair.
1814 */
Andy McFadden62a75162009-04-17 17:23:37 -07001815static void checkWidePair(RegType typel, RegType typeh, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001816{
1817 if ((typeh != typel+1))
Andy McFadden62a75162009-04-17 17:23:37 -07001818 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001819}
1820
1821/*
1822 * Implement category-1 "move" instructions. Copy a 32-bit value from
1823 * "vsrc" to "vdst".
1824 *
1825 * "insnRegCount" is the number of registers available. The "vdst" and
1826 * "vsrc" values are checked against this.
1827 */
1828static void copyRegister1(RegType* insnRegs, int insnRegCount, u4 vdst,
Andy McFadden62a75162009-04-17 17:23:37 -07001829 u4 vsrc, TypeCategory cat, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001830{
Andy McFadden62a75162009-04-17 17:23:37 -07001831 RegType type = getRegisterType(insnRegs, insnRegCount, vsrc, pFailure);
1832 if (VERIFY_OK(*pFailure))
1833 checkTypeCategory(type, cat, pFailure);
1834 if (VERIFY_OK(*pFailure))
1835 setRegisterType(insnRegs, insnRegCount, vdst, type, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001836
Andy McFadden62a75162009-04-17 17:23:37 -07001837 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001838 LOG_VFY("VFY: copy1 v%u<-v%u type=%d cat=%d\n", vdst, vsrc, type, cat);
1839 }
1840}
1841
1842/*
1843 * Implement category-2 "move" instructions. Copy a 64-bit value from
1844 * "vsrc" to "vdst". This copies both halves of the register.
1845 */
1846static void copyRegister2(RegType* insnRegs, int insnRegCount, u4 vdst,
Andy McFadden62a75162009-04-17 17:23:37 -07001847 u4 vsrc, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001848{
Andy McFadden62a75162009-04-17 17:23:37 -07001849 RegType typel = getRegisterType(insnRegs, insnRegCount, vsrc, pFailure);
1850 RegType typeh = getRegisterType(insnRegs, insnRegCount, vsrc+1, pFailure);
1851 if (VERIFY_OK(*pFailure)) {
1852 checkTypeCategory(typel, kTypeCategory2, pFailure);
1853 checkWidePair(typel, typeh, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001854 }
Andy McFadden62a75162009-04-17 17:23:37 -07001855 if (VERIFY_OK(*pFailure))
1856 setRegisterType(insnRegs, insnRegCount, vdst, typel, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001857
Andy McFadden62a75162009-04-17 17:23:37 -07001858 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001859 LOG_VFY("VFY: copy2 v%u<-v%u type=%d/%d\n", vdst, vsrc, typel, typeh);
1860 }
1861}
1862
1863/*
1864 * Implement "move-result". Copy the category-1 value from the result
1865 * register to another register, and reset the result register.
1866 *
1867 * We can't just call copyRegister1 with an altered insnRegCount,
1868 * because that would affect the test on "vdst" as well.
1869 */
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
1876 vsrc = RESULT_REGISTER(insnRegCount);
Andy McFadden62a75162009-04-17 17:23:37 -07001877 type = getRegisterType(insnRegs, insnRegCount + kExtraRegs, vsrc, pFailure);
1878 if (VERIFY_OK(*pFailure))
1879 checkTypeCategory(type, cat, pFailure);
1880 if (VERIFY_OK(*pFailure)) {
1881 setRegisterType(insnRegs, insnRegCount, vdst, type, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001882 insnRegs[vsrc] = kRegTypeUnknown;
1883 }
1884
Andy McFadden62a75162009-04-17 17:23:37 -07001885 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001886 LOG_VFY("VFY: copyRes1 v%u<-v%u cat=%d type=%d\n",
1887 vdst, vsrc, cat, type);
1888 }
1889}
1890
1891/*
1892 * Implement "move-result-wide". Copy the category-2 value from the result
1893 * register to another register, and reset the result register.
1894 *
1895 * We can't just call copyRegister2 with an altered insnRegCount,
1896 * because that would affect the test on "vdst" as well.
1897 */
1898static void copyResultRegister2(RegType* insnRegs, const int insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07001899 u4 vdst, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001900{
1901 RegType typel, typeh;
1902 u4 vsrc;
1903
1904 vsrc = RESULT_REGISTER(insnRegCount);
Andy McFadden62a75162009-04-17 17:23:37 -07001905 typel = getRegisterType(insnRegs, insnRegCount + kExtraRegs, vsrc,
1906 pFailure);
1907 typeh = getRegisterType(insnRegs, insnRegCount + kExtraRegs, vsrc+1,
1908 pFailure);
1909 if (VERIFY_OK(*pFailure)) {
1910 checkTypeCategory(typel, kTypeCategory2, pFailure);
1911 checkWidePair(typel, typeh, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001912 }
Andy McFadden62a75162009-04-17 17:23:37 -07001913 if (VERIFY_OK(*pFailure)) {
1914 setRegisterType(insnRegs, insnRegCount, vdst, typel, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001915 insnRegs[vsrc] = kRegTypeUnknown;
1916 insnRegs[vsrc+1] = kRegTypeUnknown;
1917 }
1918
Andy McFadden62a75162009-04-17 17:23:37 -07001919 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001920 LOG_VFY("VFY: copyRes2 v%u<-v%u type=%d/%d\n",
1921 vdst, vsrc, typel, typeh);
1922 }
1923}
1924
1925/*
1926 * Verify types for a simple two-register instruction (e.g. "neg-int").
1927 * "dstType" is stored into vA, and "srcType" is verified against vB.
1928 */
1929static void checkUnop(RegType* insnRegs, const int insnRegCount,
1930 DecodedInstruction* pDecInsn, RegType dstType, RegType srcType,
Andy McFadden62a75162009-04-17 17:23:37 -07001931 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001932{
Andy McFadden62a75162009-04-17 17:23:37 -07001933 verifyRegisterType(insnRegs, insnRegCount, pDecInsn->vB, srcType, pFailure);
1934 setRegisterType(insnRegs, insnRegCount, pDecInsn->vA, dstType, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001935}
1936
1937/*
1938 * We're performing an operation like "and-int/2addr" that can be
1939 * performed on booleans as well as integers. We get no indication of
1940 * boolean-ness, but we can infer it from the types of the arguments.
1941 *
1942 * Assumes we've already validated reg1/reg2.
1943 *
Andy McFaddenb5f64bc2009-06-10 14:11:07 -07001944 * TODO: consider generalizing this. The key principle is that the
1945 * result of a bitwise operation can only be as wide as the widest of
1946 * the operands. You can safely AND/OR/XOR two chars together and know
1947 * you still have a char, so it's reasonable for the compiler or "dx"
1948 * to skip the int-to-char instruction. (We need to do this for boolean
1949 * because there is no int-to-boolean operation.)
1950 *
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001951 * Returns true if both args are Boolean, Zero, or One.
1952 */
1953static bool upcastBooleanOp(RegType* insnRegs, const int insnRegCount,
1954 u4 reg1, u4 reg2)
1955{
1956 RegType type1, type2;
1957
1958 type1 = insnRegs[reg1];
1959 type2 = insnRegs[reg2];
1960
1961 if ((type1 == kRegTypeBoolean || type1 == kRegTypeZero ||
1962 type1 == kRegTypeOne) &&
1963 (type2 == kRegTypeBoolean || type2 == kRegTypeZero ||
1964 type2 == kRegTypeOne))
1965 {
1966 return true;
1967 }
1968 return false;
1969}
1970
1971/*
1972 * Verify types for A two-register instruction with a literal constant
1973 * (e.g. "add-int/lit8"). "dstType" is stored into vA, and "srcType" is
1974 * verified against vB.
1975 *
1976 * If "checkBooleanOp" is set, we use the constant value in vC.
1977 */
1978static void checkLitop(RegType* insnRegs, const int insnRegCount,
1979 DecodedInstruction* pDecInsn, RegType dstType, RegType srcType,
Andy McFadden62a75162009-04-17 17:23:37 -07001980 bool checkBooleanOp, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001981{
Andy McFadden62a75162009-04-17 17:23:37 -07001982 verifyRegisterType(insnRegs, insnRegCount, pDecInsn->vB, srcType, pFailure);
1983 if (VERIFY_OK(*pFailure) && checkBooleanOp) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001984 assert(dstType == kRegTypeInteger);
1985 /* check vB with the call, then check the constant manually */
1986 if (upcastBooleanOp(insnRegs, insnRegCount, pDecInsn->vB, pDecInsn->vB)
1987 && (pDecInsn->vC == 0 || pDecInsn->vC == 1))
1988 {
1989 dstType = kRegTypeBoolean;
1990 }
1991 }
Andy McFadden62a75162009-04-17 17:23:37 -07001992 setRegisterType(insnRegs, insnRegCount, pDecInsn->vA, dstType, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001993}
1994
1995/*
1996 * Verify types for a simple three-register instruction (e.g. "add-int").
1997 * "dstType" is stored into vA, and "srcType1"/"srcType2" are verified
1998 * against vB/vC.
1999 */
2000static void checkBinop(RegType* insnRegs, const int insnRegCount,
2001 DecodedInstruction* pDecInsn, RegType dstType, RegType srcType1,
Andy McFadden62a75162009-04-17 17:23:37 -07002002 RegType srcType2, bool checkBooleanOp, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002003{
Andy McFadden62a75162009-04-17 17:23:37 -07002004 verifyRegisterType(insnRegs, insnRegCount, pDecInsn->vB, srcType1,
2005 pFailure);
2006 verifyRegisterType(insnRegs, insnRegCount, pDecInsn->vC, srcType2,
2007 pFailure);
2008 if (VERIFY_OK(*pFailure) && checkBooleanOp) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002009 assert(dstType == kRegTypeInteger);
2010 if (upcastBooleanOp(insnRegs, insnRegCount, pDecInsn->vB, pDecInsn->vC))
2011 dstType = kRegTypeBoolean;
2012 }
Andy McFadden62a75162009-04-17 17:23:37 -07002013 setRegisterType(insnRegs, insnRegCount, pDecInsn->vA, dstType, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002014}
2015
2016/*
2017 * Verify types for a binary "2addr" operation. "srcType1"/"srcType2"
2018 * are verified against vA/vB, then "dstType" is stored into vA.
2019 */
2020static void checkBinop2addr(RegType* insnRegs, const int insnRegCount,
2021 DecodedInstruction* pDecInsn, RegType dstType, RegType srcType1,
Andy McFadden62a75162009-04-17 17:23:37 -07002022 RegType srcType2, bool checkBooleanOp, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002023{
Andy McFadden62a75162009-04-17 17:23:37 -07002024 verifyRegisterType(insnRegs, insnRegCount, pDecInsn->vA, srcType1,
2025 pFailure);
2026 verifyRegisterType(insnRegs, insnRegCount, pDecInsn->vB, srcType2,
2027 pFailure);
2028 if (VERIFY_OK(*pFailure) && checkBooleanOp) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002029 assert(dstType == kRegTypeInteger);
2030 if (upcastBooleanOp(insnRegs, insnRegCount, pDecInsn->vA, pDecInsn->vB))
2031 dstType = kRegTypeBoolean;
2032 }
Andy McFadden62a75162009-04-17 17:23:37 -07002033 setRegisterType(insnRegs, insnRegCount, pDecInsn->vA, dstType, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002034}
2035
Andy McFadden80d25ea2009-06-12 07:26:17 -07002036/*
2037 * Treat right-shifting as a narrowing conversion when possible.
2038 *
2039 * For example, right-shifting an int 24 times results in a value that can
2040 * be treated as a byte.
2041 *
2042 * Things get interesting when contemplating sign extension. Right-
2043 * shifting an integer by 16 yields a value that can be represented in a
2044 * "short" but not a "char", but an unsigned right shift by 16 yields a
2045 * value that belongs in a char rather than a short. (Consider what would
2046 * happen if the result of the shift were cast to a char or short and then
2047 * cast back to an int. If sign extension, or the lack thereof, causes
2048 * a change in the 32-bit representation, then the conversion was lossy.)
2049 *
2050 * A signed right shift by 17 on an integer results in a short. An unsigned
2051 * right shfit by 17 on an integer results in a posshort, which can be
2052 * assigned to a short or a char.
2053 *
2054 * An unsigned right shift on a short can actually expand the result into
2055 * a 32-bit integer. For example, 0xfffff123 >>> 8 becomes 0x00fffff1,
2056 * which can't be represented in anything smaller than an int.
2057 *
2058 * javac does not generate code that takes advantage of this, but some
2059 * of the code optimizers do. It's generally a peephole optimization
2060 * that replaces a particular sequence, e.g. (bipush 24, ishr, i2b) is
2061 * replaced by (bipush 24, ishr). Knowing that shifting a short 8 times
2062 * to the right yields a byte is really more than we need to handle the
2063 * code that's out there, but support is not much more complex than just
2064 * handling integer.
2065 *
2066 * Right-shifting never yields a boolean value.
2067 *
2068 * Returns the new register type.
2069 */
2070static RegType adjustForRightShift(RegType* workRegs, const int insnRegCount,
2071 int reg, unsigned int shiftCount, bool isUnsignedShift,
2072 VerifyError* pFailure)
2073{
2074 RegType srcType = getRegisterType(workRegs, insnRegCount, reg, pFailure);
2075 RegType newType;
2076
2077 /* no-op */
2078 if (shiftCount == 0)
2079 return srcType;
2080
2081 /* safe defaults */
2082 if (isUnsignedShift)
2083 newType = kRegTypeInteger;
2084 else
2085 newType = srcType;
2086
2087 if (shiftCount >= 32) {
2088 LOG_VFY("Got unexpectedly large shift count %u\n", shiftCount);
2089 /* fail? */
2090 return newType;
2091 }
2092
2093 switch (srcType) {
2094 case kRegTypeInteger: /* 32-bit signed value */
2095 case kRegTypeFloat: /* (allowed; treat same as int) */
2096 if (isUnsignedShift) {
2097 if (shiftCount > 24)
2098 newType = kRegTypePosByte;
2099 else if (shiftCount >= 16)
2100 newType = kRegTypeChar;
2101 } else {
2102 if (shiftCount >= 24)
2103 newType = kRegTypeByte;
2104 else if (shiftCount >= 16)
2105 newType = kRegTypeShort;
2106 }
2107 break;
2108 case kRegTypeShort: /* 16-bit signed value */
2109 if (isUnsignedShift) {
2110 /* default (kRegTypeInteger) is correct */
2111 } else {
2112 if (shiftCount >= 8)
2113 newType = kRegTypeByte;
2114 }
2115 break;
2116 case kRegTypePosShort: /* 15-bit unsigned value */
2117 if (shiftCount >= 8)
2118 newType = kRegTypePosByte;
2119 break;
2120 case kRegTypeChar: /* 16-bit unsigned value */
2121 if (shiftCount > 8)
2122 newType = kRegTypePosByte;
2123 break;
2124 case kRegTypeByte: /* 8-bit signed value */
2125 /* defaults (u=kRegTypeInteger / s=srcType) are correct */
2126 break;
2127 case kRegTypePosByte: /* 7-bit unsigned value */
2128 /* always use newType=srcType */
2129 newType = srcType;
2130 break;
2131 case kRegTypeZero: /* 1-bit unsigned value */
2132 case kRegTypeOne:
2133 case kRegTypeBoolean:
2134 /* unnecessary? */
2135 newType = kRegTypeZero;
2136 break;
2137 default:
2138 /* long, double, references; shouldn't be here! */
2139 assert(false);
2140 break;
2141 }
2142
2143 if (newType != srcType) {
2144 LOGVV("narrowing: %d(%d) --> %d to %d\n",
2145 shiftCount, isUnsignedShift, srcType, newType);
2146 } else {
2147 LOGVV("not narrowed: %d(%d) --> %d\n",
2148 shiftCount, isUnsignedShift, srcType);
2149 }
2150 return newType;
2151}
2152
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002153
2154/*
2155 * ===========================================================================
2156 * Register merge
2157 * ===========================================================================
2158 */
2159
2160/*
2161 * Compute the "class depth" of a class. This is the distance from the
2162 * class to the top of the tree, chasing superclass links. java.lang.Object
2163 * has a class depth of 0.
2164 */
2165static int getClassDepth(ClassObject* clazz)
2166{
2167 int depth = 0;
2168
2169 while (clazz->super != NULL) {
2170 clazz = clazz->super;
2171 depth++;
2172 }
2173 return depth;
2174}
2175
2176/*
2177 * Given two classes, walk up the superclass tree to find a common
2178 * ancestor. (Called from findCommonSuperclass().)
2179 *
2180 * TODO: consider caching the class depth in the class object so we don't
2181 * have to search for it here.
2182 */
2183static ClassObject* digForSuperclass(ClassObject* c1, ClassObject* c2)
2184{
2185 int depth1, depth2;
2186
2187 depth1 = getClassDepth(c1);
2188 depth2 = getClassDepth(c2);
2189
2190 if (gDebugVerbose) {
2191 LOGVV("COMMON: %s(%d) + %s(%d)\n",
2192 c1->descriptor, depth1, c2->descriptor, depth2);
2193 }
2194
2195 /* pull the deepest one up */
2196 if (depth1 > depth2) {
2197 while (depth1 > depth2) {
2198 c1 = c1->super;
2199 depth1--;
2200 }
2201 } else {
2202 while (depth2 > depth1) {
2203 c2 = c2->super;
2204 depth2--;
2205 }
2206 }
2207
2208 /* walk up in lock-step */
2209 while (c1 != c2) {
2210 c1 = c1->super;
2211 c2 = c2->super;
2212
2213 assert(c1 != NULL && c2 != NULL);
2214 }
2215
2216 if (gDebugVerbose) {
2217 LOGVV(" : --> %s\n", c1->descriptor);
2218 }
2219 return c1;
2220}
2221
2222/*
2223 * Merge two array classes. We can't use the general "walk up to the
2224 * superclass" merge because the superclass of an array is always Object.
2225 * We want String[] + Integer[] = Object[]. This works for higher dimensions
2226 * as well, e.g. String[][] + Integer[][] = Object[][].
2227 *
2228 * If Foo1 and Foo2 are subclasses of Foo, Foo1[] + Foo2[] = Foo[].
2229 *
2230 * If Class implements Type, Class[] + Type[] = Type[].
2231 *
2232 * If the dimensions don't match, we want to convert to an array of Object
2233 * with the least dimension, e.g. String[][] + String[][][][] = Object[][].
2234 *
2235 * This gets a little awkward because we may have to ask the VM to create
2236 * a new array type with the appropriate element and dimensions. However, we
2237 * shouldn't be doing this often.
2238 */
2239static ClassObject* findCommonArraySuperclass(ClassObject* c1, ClassObject* c2)
2240{
2241 ClassObject* arrayClass = NULL;
2242 ClassObject* commonElem;
2243 int i, numDims;
2244
2245 assert(c1->arrayDim > 0);
2246 assert(c2->arrayDim > 0);
2247
2248 if (c1->arrayDim == c2->arrayDim) {
2249 //commonElem = digForSuperclass(c1->elementClass, c2->elementClass);
2250 commonElem = findCommonSuperclass(c1->elementClass, c2->elementClass);
2251 numDims = c1->arrayDim;
2252 } else {
2253 if (c1->arrayDim < c2->arrayDim)
2254 numDims = c1->arrayDim;
2255 else
2256 numDims = c2->arrayDim;
2257 commonElem = c1->super; // == java.lang.Object
2258 }
2259
2260 /* walk from the element to the (multi-)dimensioned array type */
2261 for (i = 0; i < numDims; i++) {
2262 arrayClass = dvmFindArrayClassForElement(commonElem);
2263 commonElem = arrayClass;
2264 }
2265
2266 LOGVV("ArrayMerge '%s' + '%s' --> '%s'\n",
2267 c1->descriptor, c2->descriptor, arrayClass->descriptor);
2268 return arrayClass;
2269}
2270
2271/*
2272 * Find the first common superclass of the two classes. We're not
2273 * interested in common interfaces.
2274 *
2275 * The easiest way to do this for concrete classes is to compute the "class
2276 * depth" of each, move up toward the root of the deepest one until they're
2277 * at the same depth, then walk both up to the root until they match.
2278 *
2279 * If both classes are arrays of non-primitive types, we need to merge
2280 * based on array depth and element type.
2281 *
2282 * If one class is an interface, we check to see if the other class/interface
2283 * (or one of its predecessors) implements the interface. If so, we return
2284 * the interface; otherwise, we return Object.
2285 *
2286 * NOTE: we continue the tradition of "lazy interface handling". To wit,
2287 * suppose we have three classes:
2288 * One implements Fancy, Free
2289 * Two implements Fancy, Free
2290 * Three implements Free
2291 * where Fancy and Free are unrelated interfaces. The code requires us
2292 * to merge One into Two. Ideally we'd use a common interface, which
2293 * gives us a choice between Fancy and Free, and no guidance on which to
2294 * use. If we use Free, we'll be okay when Three gets merged in, but if
2295 * we choose Fancy, we're hosed. The "ideal" solution is to create a
2296 * set of common interfaces and carry that around, merging further references
2297 * into it. This is a pain. The easy solution is to simply boil them
2298 * down to Objects and let the runtime invokeinterface call fail, which
2299 * is what we do.
2300 */
2301static ClassObject* findCommonSuperclass(ClassObject* c1, ClassObject* c2)
2302{
2303 assert(!dvmIsPrimitiveClass(c1) && !dvmIsPrimitiveClass(c2));
2304
2305 if (c1 == c2)
2306 return c1;
2307
2308 if (dvmIsInterfaceClass(c1) && dvmImplements(c2, c1)) {
2309 if (gDebugVerbose)
2310 LOGVV("COMMON/I1: %s + %s --> %s\n",
2311 c1->descriptor, c2->descriptor, c1->descriptor);
2312 return c1;
2313 }
2314 if (dvmIsInterfaceClass(c2) && dvmImplements(c1, c2)) {
2315 if (gDebugVerbose)
2316 LOGVV("COMMON/I2: %s + %s --> %s\n",
2317 c1->descriptor, c2->descriptor, c2->descriptor);
2318 return c2;
2319 }
2320
2321 if (dvmIsArrayClass(c1) && dvmIsArrayClass(c2) &&
2322 !dvmIsPrimitiveClass(c1->elementClass) &&
2323 !dvmIsPrimitiveClass(c2->elementClass))
2324 {
2325 return findCommonArraySuperclass(c1, c2);
2326 }
2327
2328 return digForSuperclass(c1, c2);
2329}
2330
2331/*
2332 * Merge two RegType values.
2333 *
2334 * Sets "*pChanged" to "true" if the result doesn't match "type1".
2335 */
2336static RegType mergeTypes(RegType type1, RegType type2, bool* pChanged)
2337{
2338 RegType result;
2339
2340 /*
2341 * Check for trivial case so we don't have to hit memory.
2342 */
2343 if (type1 == type2)
2344 return type1;
2345
2346 /*
2347 * Use the table if we can, and reject any attempts to merge something
2348 * from the table with a reference type.
2349 *
2350 * The uninitialized table entry at index zero *will* show up as a
2351 * simple kRegTypeUninit value. Since this cannot be merged with
2352 * anything but itself, the rules do the right thing.
2353 */
2354 if (type1 < kRegTypeMAX) {
2355 if (type2 < kRegTypeMAX) {
2356 result = gDvmMergeTab[type1][type2];
2357 } else {
2358 /* simple + reference == conflict, usually */
2359 if (type1 == kRegTypeZero)
2360 result = type2;
2361 else
2362 result = kRegTypeConflict;
2363 }
2364 } else {
2365 if (type2 < kRegTypeMAX) {
2366 /* reference + simple == conflict, usually */
2367 if (type2 == kRegTypeZero)
2368 result = type1;
2369 else
2370 result = kRegTypeConflict;
2371 } else {
2372 /* merging two references */
2373 if (regTypeIsUninitReference(type1) ||
2374 regTypeIsUninitReference(type2))
2375 {
2376 /* can't merge uninit with anything but self */
2377 result = kRegTypeConflict;
2378 } else {
2379 ClassObject* clazz1 = regTypeInitializedReferenceToClass(type1);
2380 ClassObject* clazz2 = regTypeInitializedReferenceToClass(type2);
2381 ClassObject* mergedClass;
2382
2383 mergedClass = findCommonSuperclass(clazz1, clazz2);
2384 assert(mergedClass != NULL);
2385 result = regTypeFromClass(mergedClass);
2386 }
2387 }
2388 }
2389
2390 if (result != type1)
2391 *pChanged = true;
2392 return result;
2393}
2394
2395/*
2396 * Control can transfer to "nextInsn".
2397 *
2398 * Merge the registers from "workRegs" into "regTypes" at "nextInsn", and
2399 * set the "changed" flag on the target address if the registers have changed.
2400 */
2401static void updateRegisters(const Method* meth, InsnFlags* insnFlags,
2402 RegisterTable* regTable, int nextInsn, const RegType* workRegs)
2403{
2404 RegType* targetRegs = getRegisterLine(regTable, nextInsn);
2405 const int insnRegCount = meth->registersSize;
2406
2407#if 0
2408 if (!dvmInsnIsBranchTarget(insnFlags, nextInsn)) {
2409 LOGE("insnFlags[0x%x]=0x%08x\n", nextInsn, insnFlags[nextInsn]);
2410 LOGE(" In %s.%s %s\n",
2411 meth->clazz->descriptor, meth->name, meth->descriptor);
2412 assert(false);
2413 }
2414#endif
2415
2416 if (!dvmInsnIsVisitedOrChanged(insnFlags, nextInsn)) {
2417 /*
2418 * We haven't processed this instruction before, and we haven't
2419 * touched the registers here, so there's nothing to "merge". Copy
2420 * the registers over and mark it as changed. (This is the only
2421 * way a register can transition out of "unknown", so this is not
2422 * just an optimization.)
2423 */
2424 LOGVV("COPY into 0x%04x\n", nextInsn);
2425 copyRegisters(targetRegs, workRegs, insnRegCount + kExtraRegs);
2426 dvmInsnSetChanged(insnFlags, nextInsn, true);
2427 } else {
2428 if (gDebugVerbose) {
2429 LOGVV("MERGE into 0x%04x\n", nextInsn);
2430 //dumpRegTypes(meth, insnFlags, targetRegs, 0, "targ", NULL, 0);
2431 //dumpRegTypes(meth, insnFlags, workRegs, 0, "work", NULL, 0);
2432 }
2433 /* merge registers, set Changed only if different */
2434 bool changed = false;
2435 int i;
2436
2437 for (i = 0; i < insnRegCount + kExtraRegs; i++) {
2438 targetRegs[i] = mergeTypes(targetRegs[i], workRegs[i], &changed);
2439 }
2440
2441 if (gDebugVerbose) {
2442 //LOGI(" RESULT (changed=%d)\n", changed);
2443 //dumpRegTypes(meth, insnFlags, targetRegs, 0, "rslt", NULL, 0);
2444 }
2445
2446 if (changed)
2447 dvmInsnSetChanged(insnFlags, nextInsn, true);
2448 }
2449}
2450
2451
2452/*
2453 * ===========================================================================
2454 * Utility functions
2455 * ===========================================================================
2456 */
2457
2458/*
2459 * Look up an instance field, specified by "fieldIdx", that is going to be
2460 * accessed in object "objType". This resolves the field and then verifies
2461 * that the class containing the field is an instance of the reference in
2462 * "objType".
2463 *
2464 * It is possible for "objType" to be kRegTypeZero, meaning that we might
2465 * have a null reference. This is a runtime problem, so we allow it,
2466 * skipping some of the type checks.
2467 *
2468 * In general, "objType" must be an initialized reference. However, we
2469 * allow it to be uninitialized if this is an "<init>" method and the field
2470 * is declared within the "objType" class.
2471 *
Andy McFadden62a75162009-04-17 17:23:37 -07002472 * Returns an InstField on success, returns NULL and sets "*pFailure"
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002473 * on failure.
2474 */
2475static InstField* getInstField(const Method* meth,
2476 const UninitInstanceMap* uninitMap, RegType objType, int fieldIdx,
Andy McFadden62a75162009-04-17 17:23:37 -07002477 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002478{
2479 InstField* instField = NULL;
2480 ClassObject* objClass;
2481 bool mustBeLocal = false;
2482
2483 if (!regTypeIsReference(objType)) {
Andy McFadden62a75162009-04-17 17:23:37 -07002484 LOG_VFY("VFY: attempt to access field in non-reference type %d\n",
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002485 objType);
Andy McFadden62a75162009-04-17 17:23:37 -07002486 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002487 goto bail;
2488 }
2489
Andy McFadden62a75162009-04-17 17:23:37 -07002490 instField = dvmOptResolveInstField(meth->clazz, fieldIdx, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002491 if (instField == NULL) {
2492 LOG_VFY("VFY: unable to resolve instance field %u\n", fieldIdx);
Andy McFadden62a75162009-04-17 17:23:37 -07002493 assert(!VERIFY_OK(*pFailure));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002494 goto bail;
2495 }
2496
2497 if (objType == kRegTypeZero)
2498 goto bail;
2499
2500 /*
2501 * Access to fields in uninitialized objects is allowed if this is
2502 * the <init> method for the object and the field in question is
2503 * declared by this class.
2504 */
2505 objClass = regTypeReferenceToClass(objType, uninitMap);
2506 assert(objClass != NULL);
2507 if (regTypeIsUninitReference(objType)) {
2508 if (!isInitMethod(meth) || meth->clazz != objClass) {
2509 LOG_VFY("VFY: attempt to access field via uninitialized ref\n");
Andy McFadden62a75162009-04-17 17:23:37 -07002510 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002511 goto bail;
2512 }
2513 mustBeLocal = true;
2514 }
2515
2516 if (!dvmInstanceof(objClass, instField->field.clazz)) {
2517 LOG_VFY("VFY: invalid field access (field %s.%s, through %s ref)\n",
2518 instField->field.clazz->descriptor, instField->field.name,
2519 objClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07002520 *pFailure = VERIFY_ERROR_NO_FIELD;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002521 goto bail;
2522 }
2523
2524 if (mustBeLocal) {
2525 /* for uninit ref, make sure it's defined by this class, not super */
2526 if (instField < objClass->ifields ||
2527 instField >= objClass->ifields + objClass->ifieldCount)
2528 {
2529 LOG_VFY("VFY: invalid constructor field access (field %s in %s)\n",
2530 instField->field.name, objClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07002531 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002532 goto bail;
2533 }
2534 }
2535
2536bail:
2537 return instField;
2538}
2539
2540/*
2541 * Look up a static field.
2542 *
Andy McFadden62a75162009-04-17 17:23:37 -07002543 * Returns a StaticField on success, returns NULL and sets "*pFailure"
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002544 * on failure.
2545 */
2546static StaticField* getStaticField(const Method* meth, int fieldIdx,
Andy McFadden62a75162009-04-17 17:23:37 -07002547 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002548{
2549 StaticField* staticField;
2550
Andy McFadden62a75162009-04-17 17:23:37 -07002551 staticField = dvmOptResolveStaticField(meth->clazz, fieldIdx, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002552 if (staticField == NULL) {
2553 DexFile* pDexFile = meth->clazz->pDvmDex->pDexFile;
2554 const DexFieldId* pFieldId;
2555
2556 pFieldId = dexGetFieldId(pDexFile, fieldIdx);
2557
2558 LOG_VFY("VFY: unable to resolve static field %u (%s) in %s\n", fieldIdx,
2559 dexStringById(pDexFile, pFieldId->nameIdx),
2560 dexStringByTypeIdx(pDexFile, pFieldId->classIdx));
Andy McFadden62a75162009-04-17 17:23:37 -07002561 assert(!VERIFY_OK(*pFailure));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002562 goto bail;
2563 }
2564
2565bail:
2566 return staticField;
2567}
2568
2569/*
2570 * If "field" is marked "final", make sure this is the either <clinit>
2571 * or <init> as appropriate.
2572 *
Andy McFadden62a75162009-04-17 17:23:37 -07002573 * Sets "*pFailure" on failure.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002574 */
2575static void checkFinalFieldAccess(const Method* meth, const Field* field,
Andy McFadden62a75162009-04-17 17:23:37 -07002576 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002577{
2578 if (!dvmIsFinalField(field))
2579 return;
2580
2581 /* make sure we're in the same class */
2582 if (meth->clazz != field->clazz) {
2583 LOG_VFY_METH(meth, "VFY: can't modify final field %s.%s\n",
2584 field->clazz->descriptor, field->name);
Andy McFaddenb51ea112009-05-08 16:50:17 -07002585 *pFailure = VERIFY_ERROR_ACCESS_FIELD;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002586 return;
2587 }
2588
2589 /*
Andy McFadden62a75162009-04-17 17:23:37 -07002590 * The VM spec descriptions of putfield and putstatic say that
2591 * IllegalAccessError is only thrown when the instructions appear
2592 * outside the declaring class. Our earlier attempts to restrict
2593 * final field modification to constructors are, therefore, wrong.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002594 */
2595#if 0
2596 /* make sure we're in the right kind of constructor */
2597 if (dvmIsStaticField(field)) {
2598 if (!isClassInitMethod(meth)) {
2599 LOG_VFY_METH(meth,
2600 "VFY: can't modify final static field outside <clinit>\n");
Andy McFadden62a75162009-04-17 17:23:37 -07002601 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002602 }
2603 } else {
2604 if (!isInitMethod(meth)) {
2605 LOG_VFY_METH(meth,
2606 "VFY: can't modify final field outside <init>\n");
Andy McFadden62a75162009-04-17 17:23:37 -07002607 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002608 }
2609 }
2610#endif
2611}
2612
2613/*
2614 * Make sure that the register type is suitable for use as an array index.
2615 *
Andy McFadden62a75162009-04-17 17:23:37 -07002616 * Sets "*pFailure" if not.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002617 */
2618static void checkArrayIndexType(const Method* meth, RegType regType,
Andy McFadden62a75162009-04-17 17:23:37 -07002619 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002620{
Andy McFadden62a75162009-04-17 17:23:37 -07002621 if (VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002622 /*
2623 * The 1nr types are interchangeable at this level. We could
2624 * do something special if we can definitively identify it as a
2625 * float, but there's no real value in doing so.
2626 */
Andy McFadden62a75162009-04-17 17:23:37 -07002627 checkTypeCategory(regType, kTypeCategory1nr, pFailure);
2628 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002629 LOG_VFY_METH(meth, "Invalid reg type for array index (%d)\n",
2630 regType);
2631 }
2632 }
2633}
2634
2635/*
2636 * Check constraints on constructor return. Specifically, make sure that
2637 * the "this" argument got initialized.
2638 *
2639 * The "this" argument to <init> uses code offset kUninitThisArgAddr, which
2640 * puts it at the start of the list in slot 0. If we see a register with
2641 * an uninitialized slot 0 reference, we know it somehow didn't get
2642 * initialized.
2643 *
2644 * Returns "true" if all is well.
2645 */
2646static bool checkConstructorReturn(const Method* meth, const RegType* insnRegs,
2647 const int insnRegCount)
2648{
2649 int i;
2650
2651 if (!isInitMethod(meth))
2652 return true;
2653
2654 RegType uninitThis = regTypeFromUninitIndex(kUninitThisArgSlot);
2655
2656 for (i = 0; i < insnRegCount; i++) {
2657 if (insnRegs[i] == uninitThis) {
2658 LOG_VFY("VFY: <init> returning without calling superclass init\n");
2659 return false;
2660 }
2661 }
2662 return true;
2663}
2664
2665/*
2666 * Verify that the target instruction is not "move-exception". It's important
2667 * that the only way to execute a move-exception is as the first instruction
2668 * of an exception handler.
2669 *
2670 * Returns "true" if all is well, "false" if the target instruction is
2671 * move-exception.
2672 */
2673static bool checkMoveException(const Method* meth, int insnIdx,
2674 const char* logNote)
2675{
2676 assert(insnIdx >= 0 && insnIdx < (int)dvmGetMethodInsnsSize(meth));
2677
2678 if ((meth->insns[insnIdx] & 0xff) == OP_MOVE_EXCEPTION) {
2679 LOG_VFY("VFY: invalid use of move-exception\n");
2680 return false;
2681 }
2682 return true;
2683}
2684
2685/*
2686 * For the "move-exception" instruction at "insnIdx", which must be at an
2687 * exception handler address, determine the first common superclass of
2688 * all exceptions that can land here. (For javac output, we're probably
2689 * looking at multiple spans of bytecode covered by one "try" that lands
2690 * at an exception-specific "catch", but in general the handler could be
2691 * shared for multiple exceptions.)
2692 *
2693 * Returns NULL if no matching exception handler can be found, or if the
2694 * exception is not a subclass of Throwable.
2695 */
Andy McFadden62a75162009-04-17 17:23:37 -07002696static ClassObject* getCaughtExceptionType(const Method* meth, int insnIdx,
2697 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002698{
Andy McFadden62a75162009-04-17 17:23:37 -07002699 VerifyError localFailure;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002700 const DexCode* pCode;
2701 DexFile* pDexFile;
2702 ClassObject* commonSuper = NULL;
Andy McFadden62a75162009-04-17 17:23:37 -07002703 bool foundPossibleHandler = false;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002704 u4 handlersSize;
2705 u4 offset;
2706 u4 i;
2707
2708 pDexFile = meth->clazz->pDvmDex->pDexFile;
2709 pCode = dvmGetMethodCode(meth);
2710
2711 if (pCode->triesSize != 0) {
2712 handlersSize = dexGetHandlersSize(pCode);
2713 offset = dexGetFirstHandlerOffset(pCode);
2714 } else {
2715 handlersSize = 0;
2716 offset = 0;
2717 }
2718
2719 for (i = 0; i < handlersSize; i++) {
2720 DexCatchIterator iterator;
2721 dexCatchIteratorInit(&iterator, pCode, offset);
2722
2723 for (;;) {
2724 const DexCatchHandler* handler = dexCatchIteratorNext(&iterator);
2725
2726 if (handler == NULL) {
2727 break;
2728 }
2729
2730 if (handler->address == (u4) insnIdx) {
2731 ClassObject* clazz;
Andy McFadden62a75162009-04-17 17:23:37 -07002732 foundPossibleHandler = true;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002733
2734 if (handler->typeIdx == kDexNoIndex)
2735 clazz = gDvm.classJavaLangThrowable;
2736 else
Andy McFadden62a75162009-04-17 17:23:37 -07002737 clazz = dvmOptResolveClass(meth->clazz, handler->typeIdx,
2738 &localFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002739
2740 if (clazz == NULL) {
2741 LOG_VFY("VFY: unable to resolve exception class %u (%s)\n",
2742 handler->typeIdx,
2743 dexStringByTypeIdx(pDexFile, handler->typeIdx));
Andy McFadden62a75162009-04-17 17:23:37 -07002744 /* TODO: do we want to keep going? If we don't fail
2745 * this we run the risk of having a non-Throwable
2746 * introduced at runtime. However, that won't pass
2747 * an instanceof test, so is essentially harmless. */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002748 } else {
2749 if (commonSuper == NULL)
2750 commonSuper = clazz;
2751 else
2752 commonSuper = findCommonSuperclass(clazz, commonSuper);
2753 }
2754 }
2755 }
2756
2757 offset = dexCatchIteratorGetEndOffset(&iterator, pCode);
2758 }
2759
2760 if (commonSuper == NULL) {
Andy McFadden62a75162009-04-17 17:23:37 -07002761 /* no catch blocks, or no catches with classes we can find */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002762 LOG_VFY_METH(meth,
2763 "VFY: unable to find exception handler at addr 0x%x\n", insnIdx);
Andy McFadden62a75162009-04-17 17:23:37 -07002764 *pFailure = VERIFY_ERROR_GENERIC;
2765 } else {
2766 // TODO: verify the class is an instance of Throwable?
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002767 }
2768
2769 return commonSuper;
2770}
2771
2772/*
2773 * Initialize the RegisterTable.
2774 *
2775 * Every instruction address can have a different set of information about
2776 * what's in which register, but for verification purposes we only need to
2777 * store it at branch target addresses (because we merge into that).
2778 *
2779 * By zeroing out the storage we are effectively initializing the register
2780 * information to kRegTypeUnknown.
2781 */
2782static bool initRegisterTable(const Method* meth, const InsnFlags* insnFlags,
2783 RegisterTable* regTable, RegisterTrackingMode trackRegsFor)
2784{
2785 const int insnsSize = dvmGetMethodInsnsSize(meth);
2786 int i;
2787
2788 regTable->insnRegCountPlus = meth->registersSize + kExtraRegs;
2789 regTable->addrRegs = (RegType**) calloc(insnsSize, sizeof(RegType*));
2790 if (regTable->addrRegs == NULL)
2791 return false;
2792
2793 assert(insnsSize > 0);
2794
2795 /*
2796 * "All" means "every address that holds the start of an instruction".
2797 * "Branches" and "GcPoints" mean just those addresses.
2798 *
2799 * "GcPoints" fills about half the addresses, "Branches" about 15%.
2800 */
2801 int interestingCount = 0;
2802 //int insnCount = 0;
2803
2804 for (i = 0; i < insnsSize; i++) {
2805 bool interesting;
2806
2807 switch (trackRegsFor) {
2808 case kTrackRegsAll:
2809 interesting = dvmInsnIsOpcode(insnFlags, i);
2810 break;
2811 case kTrackRegsGcPoints:
2812 interesting = dvmInsnIsGcPoint(insnFlags, i) ||
2813 dvmInsnIsBranchTarget(insnFlags, i);
2814 break;
2815 case kTrackRegsBranches:
2816 interesting = dvmInsnIsBranchTarget(insnFlags, i);
2817 break;
2818 default:
2819 dvmAbort();
2820 return false;
2821 }
2822
2823 if (interesting)
2824 interestingCount++;
2825
2826 /* count instructions, for display only */
2827 //if (dvmInsnIsOpcode(insnFlags, i))
2828 // insnCount++;
2829 }
2830
2831 regTable->regAlloc = (RegType*)
2832 calloc(regTable->insnRegCountPlus * interestingCount, sizeof(RegType));
2833 if (regTable->regAlloc == NULL)
2834 return false;
2835
2836 RegType* regPtr = regTable->regAlloc;
2837 for (i = 0; i < insnsSize; i++) {
2838 bool interesting;
2839
2840 switch (trackRegsFor) {
2841 case kTrackRegsAll:
2842 interesting = dvmInsnIsOpcode(insnFlags, i);
2843 break;
2844 case kTrackRegsGcPoints:
2845 interesting = dvmInsnIsGcPoint(insnFlags, i) ||
2846 dvmInsnIsBranchTarget(insnFlags, i);
2847 break;
2848 case kTrackRegsBranches:
2849 interesting = dvmInsnIsBranchTarget(insnFlags, i);
2850 break;
2851 default:
2852 dvmAbort();
2853 return false;
2854 }
2855
2856 if (interesting) {
2857 regTable->addrRegs[i] = regPtr;
2858 regPtr += regTable->insnRegCountPlus;
2859 }
2860 }
2861
2862 //LOGD("Tracking registers for %d, total %d of %d(%d) (%d%%)\n",
2863 // TRACK_REGS_FOR, interestingCount, insnCount, insnsSize,
2864 // (interestingCount*100) / insnCount);
2865
2866 assert(regPtr - regTable->regAlloc ==
2867 regTable->insnRegCountPlus * interestingCount);
2868 assert(regTable->addrRegs[0] != NULL);
2869 return true;
2870}
2871
2872
2873/*
2874 * Verify that the arguments in a filled-new-array instruction are valid.
2875 *
2876 * "resClass" is the class refered to by pDecInsn->vB.
2877 */
2878static void verifyFilledNewArrayRegs(const Method* meth,
2879 const RegType* insnRegs, const int insnRegCount,
2880 const DecodedInstruction* pDecInsn, ClassObject* resClass, bool isRange,
Andy McFadden62a75162009-04-17 17:23:37 -07002881 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002882{
2883 u4 argCount = pDecInsn->vA;
2884 RegType expectedType;
2885 PrimitiveType elemType;
2886 unsigned int ui;
2887
2888 assert(dvmIsArrayClass(resClass));
2889 elemType = resClass->elementClass->primitiveType;
2890 if (elemType == PRIM_NOT) {
2891 expectedType = regTypeFromClass(resClass->elementClass);
2892 } else {
2893 expectedType = primitiveTypeToRegType(elemType);
2894 }
2895 //LOGI("filled-new-array: %s -> %d\n", resClass->descriptor, expectedType);
2896
2897 /*
2898 * Verify each register. If "argCount" is bad, verifyRegisterType()
2899 * will run off the end of the list and fail. It's legal, if silly,
2900 * for argCount to be zero.
2901 */
2902 for (ui = 0; ui < argCount; ui++) {
2903 u4 getReg;
2904
2905 if (isRange)
2906 getReg = pDecInsn->vC + ui;
2907 else
2908 getReg = pDecInsn->arg[ui];
2909
Andy McFadden62a75162009-04-17 17:23:37 -07002910 verifyRegisterType(insnRegs, insnRegCount, getReg, expectedType,
2911 pFailure);
2912 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002913 LOG_VFY("VFY: filled-new-array arg %u(%u) not valid\n", ui, getReg);
2914 return;
2915 }
2916 }
2917}
2918
2919
2920/*
Andy McFaddenb51ea112009-05-08 16:50:17 -07002921 * Replace an instruction with "throw-verification-error". This allows us to
2922 * defer error reporting until the code path is first used.
2923 *
Andy McFadden861b3382010-03-05 15:58:31 -08002924 * This is expected to be called during "just in time" verification, not
2925 * from within dexopt. (Verification failures in dexopt will result in
2926 * postponement of verification to first use of the class.)
2927 *
Andy McFaddenb51ea112009-05-08 16:50:17 -07002928 * The throw-verification-error instruction requires two code units. Some
2929 * of the replaced instructions require three; the third code unit will
2930 * receive a "nop". The instruction's length will be left unchanged
2931 * in "insnFlags".
2932 *
Andy McFadden96516932009-10-28 17:39:02 -07002933 * The verifier explicitly locks out breakpoint activity, so there should
2934 * be no clashes with the debugger.
2935 *
Andy McFaddenb51ea112009-05-08 16:50:17 -07002936 * IMPORTANT: this may replace meth->insns with a pointer to a new copy of
2937 * the instructions.
2938 *
2939 * Returns "true" on success.
2940 */
2941static bool replaceFailingInstruction(Method* meth, InsnFlags* insnFlags,
2942 int insnIdx, VerifyError failure)
2943{
Andy McFaddenaf0e8382009-08-28 10:38:37 -07002944 VerifyErrorRefType refType;
Andy McFaddenb51ea112009-05-08 16:50:17 -07002945 const u2* oldInsns = meth->insns + insnIdx;
2946 u2 oldInsn = *oldInsns;
2947 bool result = false;
2948
Andy McFaddenb51ea112009-05-08 16:50:17 -07002949 //LOGD(" was 0x%04x\n", oldInsn);
2950 u2* newInsns = (u2*) meth->insns + insnIdx;
2951
2952 /*
2953 * Generate the new instruction out of the old.
2954 *
2955 * First, make sure this is an instruction we're expecting to stomp on.
2956 */
2957 switch (oldInsn & 0xff) {
2958 case OP_CONST_CLASS: // insn[1] == class ref, 2 bytes
2959 case OP_CHECK_CAST:
2960 case OP_INSTANCE_OF:
2961 case OP_NEW_INSTANCE:
2962 case OP_NEW_ARRAY:
Andy McFaddenb51ea112009-05-08 16:50:17 -07002963 case OP_FILLED_NEW_ARRAY: // insn[1] == class ref, 3 bytes
2964 case OP_FILLED_NEW_ARRAY_RANGE:
Andy McFaddenaf0e8382009-08-28 10:38:37 -07002965 refType = VERIFY_ERROR_REF_CLASS;
2966 break;
Andy McFaddenb51ea112009-05-08 16:50:17 -07002967
2968 case OP_IGET: // insn[1] == field ref, 2 bytes
2969 case OP_IGET_BOOLEAN:
2970 case OP_IGET_BYTE:
2971 case OP_IGET_CHAR:
2972 case OP_IGET_SHORT:
2973 case OP_IGET_WIDE:
2974 case OP_IGET_OBJECT:
2975 case OP_IPUT:
2976 case OP_IPUT_BOOLEAN:
2977 case OP_IPUT_BYTE:
2978 case OP_IPUT_CHAR:
2979 case OP_IPUT_SHORT:
2980 case OP_IPUT_WIDE:
2981 case OP_IPUT_OBJECT:
2982 case OP_SGET:
2983 case OP_SGET_BOOLEAN:
2984 case OP_SGET_BYTE:
2985 case OP_SGET_CHAR:
2986 case OP_SGET_SHORT:
2987 case OP_SGET_WIDE:
2988 case OP_SGET_OBJECT:
2989 case OP_SPUT:
2990 case OP_SPUT_BOOLEAN:
2991 case OP_SPUT_BYTE:
2992 case OP_SPUT_CHAR:
2993 case OP_SPUT_SHORT:
2994 case OP_SPUT_WIDE:
2995 case OP_SPUT_OBJECT:
Andy McFaddenaf0e8382009-08-28 10:38:37 -07002996 refType = VERIFY_ERROR_REF_FIELD;
2997 break;
Andy McFaddenb51ea112009-05-08 16:50:17 -07002998
2999 case OP_INVOKE_VIRTUAL: // insn[1] == method ref, 3 bytes
3000 case OP_INVOKE_VIRTUAL_RANGE:
3001 case OP_INVOKE_SUPER:
3002 case OP_INVOKE_SUPER_RANGE:
3003 case OP_INVOKE_DIRECT:
3004 case OP_INVOKE_DIRECT_RANGE:
3005 case OP_INVOKE_STATIC:
3006 case OP_INVOKE_STATIC_RANGE:
3007 case OP_INVOKE_INTERFACE:
3008 case OP_INVOKE_INTERFACE_RANGE:
Andy McFaddenaf0e8382009-08-28 10:38:37 -07003009 refType = VERIFY_ERROR_REF_METHOD;
Andy McFaddenb51ea112009-05-08 16:50:17 -07003010 break;
Andy McFaddenaf0e8382009-08-28 10:38:37 -07003011
Andy McFaddenb51ea112009-05-08 16:50:17 -07003012 default:
3013 /* could handle this in a generic way, but this is probably safer */
3014 LOG_VFY("GLITCH: verifier asked to replace opcode 0x%02x\n",
3015 oldInsn & 0xff);
3016 goto bail;
3017 }
3018
3019 /* write a NOP over the third code unit, if necessary */
3020 int width = dvmInsnGetWidth(insnFlags, insnIdx);
3021 switch (width) {
3022 case 2:
3023 /* nothing to do */
3024 break;
3025 case 3:
Andy McFadden96516932009-10-28 17:39:02 -07003026 dvmDexChangeDex2(meth->clazz->pDvmDex, newInsns+2, OP_NOP);
3027 //newInsns[2] = OP_NOP;
Andy McFaddenb51ea112009-05-08 16:50:17 -07003028 break;
3029 default:
3030 /* whoops */
3031 LOGE("ERROR: stomped a %d-unit instruction with a verifier error\n",
3032 width);
3033 dvmAbort();
3034 }
3035
3036 /* encode the opcode, with the failure code in the high byte */
Andy McFadden96516932009-10-28 17:39:02 -07003037 u2 newVal = OP_THROW_VERIFICATION_ERROR |
Andy McFaddenaf0e8382009-08-28 10:38:37 -07003038 (failure << 8) | (refType << (8 + kVerifyErrorRefTypeShift));
Andy McFadden96516932009-10-28 17:39:02 -07003039 //newInsns[0] = newVal;
3040 dvmDexChangeDex2(meth->clazz->pDvmDex, newInsns, newVal);
Andy McFaddenb51ea112009-05-08 16:50:17 -07003041
3042 result = true;
3043
3044bail:
Andy McFaddenb51ea112009-05-08 16:50:17 -07003045 return result;
3046}
3047
Andy McFadden861b3382010-03-05 15:58:31 -08003048/*
3049 * Replace {iget,iput,sget,sput}-wide with the -wide-volatile form.
3050 *
3051 * If this is called during dexopt, we can modify the instruction in
3052 * place. If this happens during just-in-time verification, we need to
3053 * use the DEX read/write page feature.
3054 *
3055 * NOTE:
3056 * This shouldn't really be tied to verification. It ought to be a
3057 * separate pass that is run before or after the verifier. However, that
3058 * requires a bunch of extra code, and the only advantage of doing so is
3059 * that the feature isn't disabled when verification is turned off. At
3060 * some point we may need to revisit this choice.
3061 */
3062static void replaceVolatileInstruction(Method* meth, InsnFlags* insnFlags,
3063 int insnIdx)
3064{
3065 u2* oldInsns = (u2*)meth->insns + insnIdx;
3066 u2 oldInsn = *oldInsns;
3067 u2 newVal;
3068
3069 switch (oldInsn & 0xff) {
3070 case OP_IGET_WIDE: newVal = OP_IGET_WIDE_VOLATILE; break;
3071 case OP_IPUT_WIDE: newVal = OP_IPUT_WIDE_VOLATILE; break;
3072 case OP_SGET_WIDE: newVal = OP_SGET_WIDE_VOLATILE; break;
3073 case OP_SPUT_WIDE: newVal = OP_SPUT_WIDE_VOLATILE; break;
3074 default:
3075 LOGE("wide-volatile op mismatch (0x%x)\n", oldInsn);
3076 dvmAbort();
3077 return; // in-lieu-of noreturn attribute
3078 }
3079
3080 /* merge new opcode into 16-bit code unit */
3081 newVal |= (oldInsn & 0xff00);
3082
3083 if (gDvm.optimizing) {
3084 /* dexopt time, alter the output */
3085 *oldInsns = newVal;
3086 } else {
3087 /* runtime, make the page read/write */
3088 dvmDexChangeDex2(meth->clazz->pDvmDex, oldInsns, newVal);
3089 }
3090}
3091
Andy McFaddenb51ea112009-05-08 16:50:17 -07003092
3093/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003094 * ===========================================================================
3095 * Entry point and driver loop
3096 * ===========================================================================
3097 */
3098
3099/*
3100 * Entry point for the detailed code-flow analysis.
3101 */
Andy McFaddenb51ea112009-05-08 16:50:17 -07003102bool dvmVerifyCodeFlow(Method* meth, InsnFlags* insnFlags,
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003103 UninitInstanceMap* uninitMap)
3104{
3105 bool result = false;
3106 const int insnsSize = dvmGetMethodInsnsSize(meth);
3107 const u2* insns = meth->insns;
3108 const bool generateRegisterMap = gDvm.generateRegisterMaps;
3109 int i, offset;
3110 bool isConditional;
3111 RegisterTable regTable;
3112
3113 memset(&regTable, 0, sizeof(regTable));
3114
3115#ifndef NDEBUG
3116 checkMergeTab(); // only need to do this if table gets updated
3117#endif
3118
3119 /*
3120 * We rely on these for verification of const-class, const-string,
3121 * and throw instructions. Make sure we have them.
3122 */
3123 if (gDvm.classJavaLangClass == NULL)
3124 gDvm.classJavaLangClass =
3125 dvmFindSystemClassNoInit("Ljava/lang/Class;");
3126 if (gDvm.classJavaLangString == NULL)
3127 gDvm.classJavaLangString =
3128 dvmFindSystemClassNoInit("Ljava/lang/String;");
Andy McFadden686e1e22009-05-26 16:56:30 -07003129 if (gDvm.classJavaLangThrowable == NULL) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003130 gDvm.classJavaLangThrowable =
3131 dvmFindSystemClassNoInit("Ljava/lang/Throwable;");
Andy McFadden686e1e22009-05-26 16:56:30 -07003132 gDvm.offJavaLangThrowable_cause =
3133 dvmFindFieldOffset(gDvm.classJavaLangThrowable,
3134 "cause", "Ljava/lang/Throwable;");
3135 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003136 if (gDvm.classJavaLangObject == NULL)
3137 gDvm.classJavaLangObject =
3138 dvmFindSystemClassNoInit("Ljava/lang/Object;");
3139
3140 if (meth->registersSize * insnsSize > 2*1024*1024) {
3141 /* should probably base this on actual memory requirements */
3142 LOG_VFY_METH(meth,
3143 "VFY: arbitrarily rejecting large method (regs=%d count=%d)\n",
3144 meth->registersSize, insnsSize);
3145 goto bail;
3146 }
3147
3148 /*
3149 * Create register lists, and initialize them to "Unknown". If we're
3150 * also going to create the register map, we need to retain the
3151 * register lists for a larger set of addresses.
3152 */
3153 if (!initRegisterTable(meth, insnFlags, &regTable,
3154 generateRegisterMap ? kTrackRegsGcPoints : kTrackRegsBranches))
3155 goto bail;
3156
3157 /*
3158 * Initialize the types of the registers that correspond to the
3159 * method arguments. We can determine this from the method signature.
3160 */
3161 if (!setTypesFromSignature(meth, regTable.addrRegs[0], uninitMap))
3162 goto bail;
3163
3164 /*
3165 * Run the verifier.
3166 */
3167 if (!doCodeVerification(meth, insnFlags, &regTable, uninitMap))
3168 goto bail;
3169
3170 /*
3171 * Generate a register map.
3172 */
3173 if (generateRegisterMap) {
3174 RegisterMap* pMap;
3175 VerifierData vd;
3176
3177 vd.method = meth;
3178 vd.insnsSize = insnsSize;
3179 vd.insnRegCount = meth->registersSize;
3180 vd.insnFlags = insnFlags;
3181 vd.addrRegs = regTable.addrRegs;
3182
3183 pMap = dvmGenerateRegisterMapV(&vd);
3184 if (pMap != NULL) {
3185 /*
3186 * Tuck it into the Method struct. It will either get used
3187 * directly or, if we're in dexopt, will be packed up and
3188 * appended to the DEX file.
3189 */
3190 dvmSetRegisterMap((Method*)meth, pMap);
3191 }
3192 }
3193
3194 /*
3195 * Success.
3196 */
3197 result = true;
3198
3199bail:
3200 free(regTable.addrRegs);
3201 free(regTable.regAlloc);
3202 return result;
3203}
3204
3205/*
3206 * Grind through the instructions.
3207 *
3208 * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit
3209 * on the first instruction, process it (setting additional "changed" bits),
3210 * and repeat until there are no more.
3211 *
3212 * v3 4.11.1.1
3213 * - (N/A) operand stack is always the same size
3214 * - operand stack [registers] contain the correct types of values
3215 * - local variables [registers] contain the correct types of values
3216 * - methods are invoked with the appropriate arguments
3217 * - fields are assigned using values of appropriate types
3218 * - opcodes have the correct type values in operand registers
3219 * - there is never an uninitialized class instance in a local variable in
3220 * code protected by an exception handler (operand stack is okay, because
3221 * the operand stack is discarded when an exception is thrown) [can't
3222 * know what's a local var w/o the debug info -- should fall out of
3223 * register typing]
3224 *
3225 * v3 4.11.1.2
3226 * - execution cannot fall off the end of the code
3227 *
3228 * (We also do many of the items described in the "static checks" sections,
3229 * because it's easier to do them here.)
3230 *
3231 * We need an array of RegType values, one per register, for every
3232 * instruction. In theory this could become quite large -- up to several
3233 * megabytes for a monster function. For self-preservation we reject
3234 * anything that requires more than a certain amount of memory. (Typical
3235 * "large" should be on the order of 4K code units * 8 registers.) This
3236 * will likely have to be adjusted.
3237 *
3238 *
3239 * The spec forbids backward branches when there's an uninitialized reference
3240 * in a register. The idea is to prevent something like this:
3241 * loop:
3242 * move r1, r0
3243 * new-instance r0, MyClass
3244 * ...
3245 * if-eq rN, loop // once
3246 * initialize r0
3247 *
3248 * This leaves us with two different instances, both allocated by the
3249 * same instruction, but only one is initialized. The scheme outlined in
3250 * v3 4.11.1.4 wouldn't catch this, so they work around it by preventing
3251 * backward branches. We achieve identical results without restricting
3252 * code reordering by specifying that you can't execute the new-instance
3253 * instruction if a register contains an uninitialized instance created
3254 * by that same instrutcion.
3255 */
Andy McFaddenb51ea112009-05-08 16:50:17 -07003256static bool doCodeVerification(Method* meth, InsnFlags* insnFlags,
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003257 RegisterTable* regTable, UninitInstanceMap* uninitMap)
3258{
3259 const int insnsSize = dvmGetMethodInsnsSize(meth);
3260 const u2* insns = meth->insns;
3261 RegType workRegs[meth->registersSize + kExtraRegs];
3262 bool result = false;
3263 bool debugVerbose = false;
3264 int insnIdx, startGuess, prevAddr;
3265
3266 /*
3267 * Begin by marking the first instruction as "changed".
3268 */
3269 dvmInsnSetChanged(insnFlags, 0, true);
3270
3271 if (doVerboseLogging(meth)) {
3272 IF_LOGI() {
3273 char* desc = dexProtoCopyMethodDescriptor(&meth->prototype);
3274 LOGI("Now verifying: %s.%s %s (ins=%d regs=%d)\n",
3275 meth->clazz->descriptor, meth->name, desc,
3276 meth->insSize, meth->registersSize);
3277 LOGI(" ------ [0 4 8 12 16 20 24 28 32 36\n");
3278 free(desc);
3279 }
3280 debugVerbose = true;
3281 gDebugVerbose = true;
3282 } else {
3283 gDebugVerbose = false;
3284 }
3285
3286 startGuess = 0;
3287
3288 /*
3289 * Continue until no instructions are marked "changed".
3290 */
3291 while (true) {
3292 /*
3293 * Find the first marked one. Use "startGuess" as a way to find
3294 * one quickly.
3295 */
3296 for (insnIdx = startGuess; insnIdx < insnsSize; insnIdx++) {
3297 if (dvmInsnIsChanged(insnFlags, insnIdx))
3298 break;
3299 }
3300
3301 if (insnIdx == insnsSize) {
3302 if (startGuess != 0) {
3303 /* try again, starting from the top */
3304 startGuess = 0;
3305 continue;
3306 } else {
3307 /* all flags are clear */
3308 break;
3309 }
3310 }
3311
3312 /*
3313 * We carry the working set of registers from instruction to
3314 * instruction. If this address can be the target of a branch
3315 * (or throw) instruction, or if we're skipping around chasing
3316 * "changed" flags, we need to load the set of registers from
3317 * the table.
3318 *
3319 * Because we always prefer to continue on to the next instruction,
3320 * we should never have a situation where we have a stray
3321 * "changed" flag set on an instruction that isn't a branch target.
3322 */
3323 if (dvmInsnIsBranchTarget(insnFlags, insnIdx)) {
3324 RegType* insnRegs = getRegisterLine(regTable, insnIdx);
3325 assert(insnRegs != NULL);
3326 copyRegisters(workRegs, insnRegs, meth->registersSize + kExtraRegs);
3327
3328 if (debugVerbose) {
3329 dumpRegTypes(meth, insnFlags, workRegs, insnIdx, NULL,uninitMap,
3330 SHOW_REG_DETAILS);
3331 }
3332
3333 } else {
3334 if (debugVerbose) {
3335 dumpRegTypes(meth, insnFlags, workRegs, insnIdx, NULL,uninitMap,
3336 SHOW_REG_DETAILS);
3337 }
3338
3339#ifndef NDEBUG
3340 /*
3341 * Sanity check: retrieve the stored register line (assuming
3342 * a full table) and make sure it actually matches.
3343 */
3344 RegType* insnRegs = getRegisterLine(regTable, insnIdx);
3345 if (insnRegs != NULL &&
3346 compareRegisters(workRegs, insnRegs,
3347 meth->registersSize + kExtraRegs) != 0)
3348 {
3349 char* desc = dexProtoCopyMethodDescriptor(&meth->prototype);
3350 LOG_VFY("HUH? workRegs diverged in %s.%s %s\n",
3351 meth->clazz->descriptor, meth->name, desc);
3352 free(desc);
3353 dumpRegTypes(meth, insnFlags, workRegs, 0, "work",
3354 uninitMap, DRT_SHOW_REF_TYPES | DRT_SHOW_LOCALS);
3355 dumpRegTypes(meth, insnFlags, insnRegs, 0, "insn",
3356 uninitMap, DRT_SHOW_REF_TYPES | DRT_SHOW_LOCALS);
3357 }
3358#endif
3359 }
3360
3361 //LOGI("process %s.%s %s %d\n",
3362 // meth->clazz->descriptor, meth->name, meth->descriptor, insnIdx);
3363 if (!verifyInstruction(meth, insnFlags, regTable, workRegs, insnIdx,
3364 uninitMap, &startGuess))
3365 {
3366 //LOGD("+++ %s bailing at %d\n", meth->name, insnIdx);
3367 goto bail;
3368 }
3369
3370#if 0
3371 {
3372 static const int gcMask = kInstrCanBranch | kInstrCanSwitch |
3373 kInstrCanThrow | kInstrCanReturn;
3374 OpCode opCode = *(meth->insns + insnIdx) & 0xff;
3375 int flags = dexGetInstrFlags(gDvm.instrFlags, opCode);
3376
3377 /* 8, 16, 32, or 32*n -bit regs */
3378 int regWidth = (meth->registersSize + 7) / 8;
3379 if (regWidth == 3)
3380 regWidth = 4;
3381 if (regWidth > 4) {
3382 regWidth = ((regWidth + 3) / 4) * 4;
3383 if (false) {
3384 LOGW("WOW: %d regs -> %d %s.%s\n",
3385 meth->registersSize, regWidth,
3386 meth->clazz->descriptor, meth->name);
3387 //x = true;
3388 }
3389 }
3390
3391 if ((flags & gcMask) != 0) {
3392 /* this is a potential GC point */
3393 gDvm__gcInstr++;
3394
3395 if (insnsSize < 256)
3396 gDvm__gcData += 1;
3397 else
3398 gDvm__gcData += 2;
3399 gDvm__gcData += regWidth;
3400 }
3401 gDvm__gcSimpleData += regWidth;
3402
3403 gDvm__totalInstr++;
3404 }
3405#endif
3406
3407 /*
3408 * Clear "changed" and mark as visited.
3409 */
3410 dvmInsnSetVisited(insnFlags, insnIdx, true);
3411 dvmInsnSetChanged(insnFlags, insnIdx, false);
3412 }
3413
Andy McFaddenb51ea112009-05-08 16:50:17 -07003414 if (DEAD_CODE_SCAN && !IS_METHOD_FLAG_SET(meth, METHOD_ISWRITABLE)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003415 /*
Andy McFaddenb51ea112009-05-08 16:50:17 -07003416 * Scan for dead code. There's nothing "evil" about dead code
3417 * (besides the wasted space), but it indicates a flaw somewhere
3418 * down the line, possibly in the verifier.
3419 *
3420 * If we've rewritten "always throw" instructions into the stream,
3421 * we are almost certainly going to have some dead code.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003422 */
3423 int deadStart = -1;
3424 for (insnIdx = 0; insnIdx < insnsSize;
3425 insnIdx += dvmInsnGetWidth(insnFlags, insnIdx))
3426 {
3427 /*
3428 * Switch-statement data doesn't get "visited" by scanner. It
3429 * may or may not be preceded by a padding NOP.
3430 */
3431 int instr = meth->insns[insnIdx];
3432 if (instr == kPackedSwitchSignature ||
3433 instr == kSparseSwitchSignature ||
3434 instr == kArrayDataSignature ||
3435 (instr == OP_NOP &&
3436 (meth->insns[insnIdx+1] == kPackedSwitchSignature ||
3437 meth->insns[insnIdx+1] == kSparseSwitchSignature ||
3438 meth->insns[insnIdx+1] == kArrayDataSignature)))
3439 {
3440 dvmInsnSetVisited(insnFlags, insnIdx, true);
3441 }
3442
3443 if (!dvmInsnIsVisited(insnFlags, insnIdx)) {
3444 if (deadStart < 0)
3445 deadStart = insnIdx;
3446 } else if (deadStart >= 0) {
3447 IF_LOGD() {
3448 char* desc =
3449 dexProtoCopyMethodDescriptor(&meth->prototype);
3450 LOGD("VFY: dead code 0x%04x-%04x in %s.%s %s\n",
3451 deadStart, insnIdx-1,
3452 meth->clazz->descriptor, meth->name, desc);
3453 free(desc);
3454 }
3455
3456 deadStart = -1;
3457 }
3458 }
3459 if (deadStart >= 0) {
3460 IF_LOGD() {
3461 char* desc = dexProtoCopyMethodDescriptor(&meth->prototype);
3462 LOGD("VFY: dead code 0x%04x-%04x in %s.%s %s\n",
3463 deadStart, insnIdx-1,
3464 meth->clazz->descriptor, meth->name, desc);
3465 free(desc);
3466 }
3467 }
3468 }
3469
3470 result = true;
3471
3472bail:
3473 return result;
3474}
3475
3476
3477/*
3478 * Perform verification for a single instruction.
3479 *
3480 * This requires fully decoding the instruction to determine the effect
3481 * it has on registers.
3482 *
3483 * Finds zero or more following instructions and sets the "changed" flag
3484 * if execution at that point needs to be (re-)evaluated. Register changes
3485 * are merged into "regTypes" at the target addresses. Does not set or
3486 * clear any other flags in "insnFlags".
Andy McFaddenb51ea112009-05-08 16:50:17 -07003487 *
3488 * This may alter meth->insns if we need to replace an instruction with
3489 * throw-verification-error.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003490 */
Andy McFaddenb51ea112009-05-08 16:50:17 -07003491static bool verifyInstruction(Method* meth, InsnFlags* insnFlags,
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003492 RegisterTable* regTable, RegType* workRegs, int insnIdx,
3493 UninitInstanceMap* uninitMap, int* pStartGuess)
3494{
3495 const int insnsSize = dvmGetMethodInsnsSize(meth);
3496 const u2* insns = meth->insns + insnIdx;
3497 bool result = false;
3498
3499 /*
3500 * Once we finish decoding the instruction, we need to figure out where
3501 * we can go from here. There are three possible ways to transfer
3502 * control to another statement:
3503 *
3504 * (1) Continue to the next instruction. Applies to all but
3505 * unconditional branches, method returns, and exception throws.
3506 * (2) Branch to one or more possible locations. Applies to branches
3507 * and switch statements.
3508 * (3) Exception handlers. Applies to any instruction that can
3509 * throw an exception that is handled by an encompassing "try"
3510 * block. (We simplify this to be any instruction that can
3511 * throw any exception.)
3512 *
3513 * We can also return, in which case there is no successor instruction
3514 * from this point.
3515 *
3516 * The behavior can be determined from the InstrFlags.
3517 */
3518
3519 const DexFile* pDexFile = meth->clazz->pDvmDex->pDexFile;
3520 RegType entryRegs[meth->registersSize + kExtraRegs];
3521 ClassObject* resClass;
3522 const char* className;
3523 int branchTarget = 0;
3524 const int insnRegCount = meth->registersSize;
3525 RegType tmpType;
3526 DecodedInstruction decInsn;
3527 bool justSetResult = false;
Andy McFadden62a75162009-04-17 17:23:37 -07003528 VerifyError failure = VERIFY_ERROR_NONE;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003529
3530#ifndef NDEBUG
3531 memset(&decInsn, 0x81, sizeof(decInsn));
3532#endif
3533 dexDecodeInstruction(gDvm.instrFormat, insns, &decInsn);
3534
Andy McFaddenb51ea112009-05-08 16:50:17 -07003535 int nextFlags = dexGetInstrFlags(gDvm.instrFlags, decInsn.opCode);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003536
3537 /*
3538 * Make a copy of the previous register state. If the instruction
3539 * throws an exception, we merge *this* into the destination rather
3540 * than workRegs, because we don't want the result from the "successful"
3541 * code path (e.g. a check-cast that "improves" a type) to be visible
3542 * to the exception handler.
3543 */
3544 if ((nextFlags & kInstrCanThrow) != 0 && dvmInsnIsInTry(insnFlags, insnIdx))
3545 {
3546 copyRegisters(entryRegs, workRegs, meth->registersSize + kExtraRegs);
3547 } else {
3548#ifndef NDEBUG
3549 memset(entryRegs, 0xdd,
3550 (meth->registersSize + kExtraRegs) * sizeof(RegType));
3551#endif
3552 }
3553
3554 switch (decInsn.opCode) {
3555 case OP_NOP:
3556 /*
3557 * A "pure" NOP has no effect on anything. Data tables start with
3558 * a signature that looks like a NOP; if we see one of these in
3559 * the course of executing code then we have a problem.
3560 */
3561 if (decInsn.vA != 0) {
3562 LOG_VFY("VFY: encountered data table in instruction stream\n");
Andy McFadden62a75162009-04-17 17:23:37 -07003563 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003564 }
3565 break;
3566
3567 case OP_MOVE:
3568 case OP_MOVE_FROM16:
3569 case OP_MOVE_16:
3570 copyRegister1(workRegs, insnRegCount, decInsn.vA, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07003571 kTypeCategory1nr, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003572 break;
3573 case OP_MOVE_WIDE:
3574 case OP_MOVE_WIDE_FROM16:
3575 case OP_MOVE_WIDE_16:
Andy McFadden62a75162009-04-17 17:23:37 -07003576 copyRegister2(workRegs, insnRegCount, decInsn.vA, decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003577 break;
3578 case OP_MOVE_OBJECT:
3579 case OP_MOVE_OBJECT_FROM16:
3580 case OP_MOVE_OBJECT_16:
3581 copyRegister1(workRegs, insnRegCount, decInsn.vA, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07003582 kTypeCategoryRef, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003583 break;
3584
3585 /*
3586 * The move-result instructions copy data out of a "pseudo-register"
3587 * with the results from the last method invocation. In practice we
3588 * might want to hold the result in an actual CPU register, so the
3589 * Dalvik spec requires that these only appear immediately after an
3590 * invoke or filled-new-array.
3591 *
3592 * These calls invalidate the "result" register. (This is now
3593 * redundant with the reset done below, but it can make the debug info
3594 * easier to read in some cases.)
3595 */
3596 case OP_MOVE_RESULT:
3597 copyResultRegister1(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003598 kTypeCategory1nr, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003599 break;
3600 case OP_MOVE_RESULT_WIDE:
Andy McFadden62a75162009-04-17 17:23:37 -07003601 copyResultRegister2(workRegs, insnRegCount, decInsn.vA, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003602 break;
3603 case OP_MOVE_RESULT_OBJECT:
3604 copyResultRegister1(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003605 kTypeCategoryRef, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003606 break;
3607
3608 case OP_MOVE_EXCEPTION:
3609 /*
3610 * This statement can only appear as the first instruction in an
3611 * exception handler (though not all exception handlers need to
3612 * have one of these). We verify that as part of extracting the
3613 * exception type from the catch block list.
3614 *
3615 * "resClass" will hold the closest common superclass of all
3616 * exceptions that can be handled here.
3617 */
Andy McFadden62a75162009-04-17 17:23:37 -07003618 resClass = getCaughtExceptionType(meth, insnIdx, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003619 if (resClass == NULL) {
Andy McFadden62a75162009-04-17 17:23:37 -07003620 assert(!VERIFY_OK(failure));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003621 } else {
3622 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003623 regTypeFromClass(resClass), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003624 }
3625 break;
3626
3627 case OP_RETURN_VOID:
Andy McFadden62a75162009-04-17 17:23:37 -07003628 if (!checkConstructorReturn(meth, workRegs, insnRegCount)) {
3629 failure = VERIFY_ERROR_GENERIC;
3630 } else if (getMethodReturnType(meth) != kRegTypeUnknown) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003631 LOG_VFY("VFY: return-void not expected\n");
Andy McFadden62a75162009-04-17 17:23:37 -07003632 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003633 }
3634 break;
3635 case OP_RETURN:
Andy McFadden62a75162009-04-17 17:23:37 -07003636 if (!checkConstructorReturn(meth, workRegs, insnRegCount)) {
3637 failure = VERIFY_ERROR_GENERIC;
3638 } else {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003639 /* check the method signature */
3640 RegType returnType = getMethodReturnType(meth);
Andy McFadden62a75162009-04-17 17:23:37 -07003641 checkTypeCategory(returnType, kTypeCategory1nr, &failure);
3642 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003643 LOG_VFY("VFY: return-32 not expected\n");
3644
3645 /* check the register contents */
3646 returnType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003647 &failure);
3648 checkTypeCategory(returnType, kTypeCategory1nr, &failure);
3649 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003650 LOG_VFY("VFY: return-32 on invalid register v%d\n", decInsn.vA);
3651 }
3652 break;
3653 case OP_RETURN_WIDE:
Andy McFadden62a75162009-04-17 17:23:37 -07003654 if (!checkConstructorReturn(meth, workRegs, insnRegCount)) {
3655 failure = VERIFY_ERROR_GENERIC;
3656 } else {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003657 RegType returnType, returnTypeHi;
3658
3659 /* check the method signature */
3660 returnType = getMethodReturnType(meth);
Andy McFadden62a75162009-04-17 17:23:37 -07003661 checkTypeCategory(returnType, kTypeCategory2, &failure);
3662 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003663 LOG_VFY("VFY: return-wide not expected\n");
3664
3665 /* check the register contents */
3666 returnType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003667 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003668 returnTypeHi = getRegisterType(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07003669 decInsn.vA +1, &failure);
3670 if (VERIFY_OK(failure)) {
3671 checkTypeCategory(returnType, kTypeCategory2, &failure);
3672 checkWidePair(returnType, returnTypeHi, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003673 }
Andy McFadden62a75162009-04-17 17:23:37 -07003674 if (!VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003675 LOG_VFY("VFY: return-wide on invalid register pair v%d\n",
3676 decInsn.vA);
3677 }
3678 }
3679 break;
3680 case OP_RETURN_OBJECT:
Andy McFadden62a75162009-04-17 17:23:37 -07003681 if (!checkConstructorReturn(meth, workRegs, insnRegCount)) {
3682 failure = VERIFY_ERROR_GENERIC;
3683 } else {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003684 RegType returnType = getMethodReturnType(meth);
Andy McFadden62a75162009-04-17 17:23:37 -07003685 checkTypeCategory(returnType, kTypeCategoryRef, &failure);
3686 if (!VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003687 LOG_VFY("VFY: return-object not expected\n");
3688 break;
3689 }
3690
3691 /* returnType is the *expected* return type, not register value */
3692 assert(returnType != kRegTypeZero);
3693 assert(!regTypeIsUninitReference(returnType));
3694
3695 /*
3696 * Verify that the reference in vAA is an instance of the type
3697 * in "returnType". The Zero type is allowed here. If the
3698 * method is declared to return an interface, then any
3699 * initialized reference is acceptable.
3700 *
3701 * Note getClassFromRegister fails if the register holds an
3702 * uninitialized reference, so we do not allow them to be
3703 * returned.
3704 */
3705 ClassObject* declClass;
3706
3707 declClass = regTypeInitializedReferenceToClass(returnType);
3708 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07003709 decInsn.vA, &failure);
3710 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003711 break;
3712 if (resClass != NULL) {
3713 if (!dvmIsInterfaceClass(declClass) &&
3714 !dvmInstanceof(resClass, declClass))
3715 {
Andy McFadden86c86432009-05-27 14:40:12 -07003716 LOG_VFY("VFY: returning %s (cl=%p), declared %s (cl=%p)\n",
3717 resClass->descriptor, resClass->classLoader,
3718 declClass->descriptor, declClass->classLoader);
Andy McFadden62a75162009-04-17 17:23:37 -07003719 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003720 break;
3721 }
3722 }
3723 }
3724 break;
3725
3726 case OP_CONST_4:
3727 case OP_CONST_16:
3728 case OP_CONST:
3729 /* could be boolean, int, float, or a null reference */
3730 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003731 dvmDetermineCat1Const((s4)decInsn.vB), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003732 break;
3733 case OP_CONST_HIGH16:
3734 /* could be boolean, int, float, or a null reference */
3735 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003736 dvmDetermineCat1Const((s4) decInsn.vB << 16), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003737 break;
3738 case OP_CONST_WIDE_16:
3739 case OP_CONST_WIDE_32:
3740 case OP_CONST_WIDE:
3741 case OP_CONST_WIDE_HIGH16:
3742 /* could be long or double; default to long and allow conversion */
3743 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003744 kRegTypeLongLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003745 break;
3746 case OP_CONST_STRING:
3747 case OP_CONST_STRING_JUMBO:
3748 assert(gDvm.classJavaLangString != NULL);
3749 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003750 regTypeFromClass(gDvm.classJavaLangString), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003751 break;
3752 case OP_CONST_CLASS:
3753 assert(gDvm.classJavaLangClass != NULL);
3754 /* make sure we can resolve the class; access check is important */
Andy McFadden62a75162009-04-17 17:23:37 -07003755 resClass = dvmOptResolveClass(meth->clazz, decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003756 if (resClass == NULL) {
3757 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vB);
3758 dvmLogUnableToResolveClass(badClassDesc, meth);
3759 LOG_VFY("VFY: unable to resolve const-class %d (%s) in %s\n",
3760 decInsn.vB, badClassDesc, meth->clazz->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003761 assert(failure != VERIFY_ERROR_GENERIC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003762 } else {
3763 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003764 regTypeFromClass(gDvm.classJavaLangClass), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003765 }
3766 break;
3767
3768 case OP_MONITOR_ENTER:
3769 case OP_MONITOR_EXIT:
Andy McFadden62a75162009-04-17 17:23:37 -07003770 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vA, &failure);
3771 if (VERIFY_OK(failure)) {
3772 if (!regTypeIsReference(tmpType)) {
3773 LOG_VFY("VFY: monitor op on non-object\n");
3774 failure = VERIFY_ERROR_GENERIC;
3775 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003776 }
3777 break;
3778
3779 case OP_CHECK_CAST:
3780 /*
3781 * If this instruction succeeds, we will promote register vA to
3782 * the type in vB. (This could be a demotion -- not expected, so
3783 * we don't try to address it.)
3784 *
3785 * If it fails, an exception is thrown, which we deal with later
3786 * by ignoring the update to decInsn.vA when branching to a handler.
3787 */
Andy McFadden62a75162009-04-17 17:23:37 -07003788 resClass = dvmOptResolveClass(meth->clazz, decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003789 if (resClass == NULL) {
3790 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vB);
3791 dvmLogUnableToResolveClass(badClassDesc, meth);
3792 LOG_VFY("VFY: unable to resolve check-cast %d (%s) in %s\n",
3793 decInsn.vB, 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 RegType origType;
3797
3798 origType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003799 &failure);
3800 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003801 break;
3802 if (!regTypeIsReference(origType)) {
3803 LOG_VFY("VFY: check-cast on non-reference in v%u\n",decInsn.vA);
Andy McFadden62a75162009-04-17 17:23:37 -07003804 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003805 break;
3806 }
3807 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003808 regTypeFromClass(resClass), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003809 }
3810 break;
3811 case OP_INSTANCE_OF:
3812 /* make sure we're checking a reference type */
Andy McFadden62a75162009-04-17 17:23:37 -07003813 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vB, &failure);
3814 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003815 break;
3816 if (!regTypeIsReference(tmpType)) {
3817 LOG_VFY("VFY: vB not a reference (%d)\n", tmpType);
Andy McFadden62a75162009-04-17 17:23:37 -07003818 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003819 break;
3820 }
3821
3822 /* make sure we can resolve the class; access check is important */
Andy McFadden62a75162009-04-17 17:23:37 -07003823 resClass = dvmOptResolveClass(meth->clazz, decInsn.vC, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003824 if (resClass == NULL) {
3825 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vC);
3826 dvmLogUnableToResolveClass(badClassDesc, meth);
3827 LOG_VFY("VFY: unable to resolve instanceof %d (%s) in %s\n",
3828 decInsn.vC, badClassDesc, meth->clazz->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003829 assert(failure != VERIFY_ERROR_GENERIC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003830 } else {
3831 /* result is boolean */
3832 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003833 kRegTypeBoolean, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003834 }
3835 break;
3836
3837 case OP_ARRAY_LENGTH:
3838 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07003839 decInsn.vB, &failure);
3840 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003841 break;
3842 if (resClass != NULL && !dvmIsArrayClass(resClass)) {
3843 LOG_VFY("VFY: array-length on non-array\n");
Andy McFadden62a75162009-04-17 17:23:37 -07003844 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003845 break;
3846 }
3847 setRegisterType(workRegs, insnRegCount, decInsn.vA, kRegTypeInteger,
Andy McFadden62a75162009-04-17 17:23:37 -07003848 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003849 break;
3850
3851 case OP_NEW_INSTANCE:
Andy McFadden62a75162009-04-17 17:23:37 -07003852 resClass = dvmOptResolveClass(meth->clazz, decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003853 if (resClass == NULL) {
3854 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vB);
3855 dvmLogUnableToResolveClass(badClassDesc, meth);
3856 LOG_VFY("VFY: unable to resolve new-instance %d (%s) in %s\n",
3857 decInsn.vB, badClassDesc, meth->clazz->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003858 assert(failure != VERIFY_ERROR_GENERIC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003859 } else {
3860 RegType uninitType;
3861
Andy McFaddenb51ea112009-05-08 16:50:17 -07003862 /* can't create an instance of an interface or abstract class */
3863 if (dvmIsAbstractClass(resClass) || dvmIsInterfaceClass(resClass)) {
3864 LOG_VFY("VFY: new-instance on interface or abstract class %s\n",
3865 resClass->descriptor);
3866 failure = VERIFY_ERROR_INSTANTIATION;
3867 break;
3868 }
3869
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003870 /* add resolved class to uninit map if not already there */
3871 int uidx = dvmSetUninitInstance(uninitMap, insnIdx, resClass);
3872 assert(uidx >= 0);
3873 uninitType = regTypeFromUninitIndex(uidx);
3874
3875 /*
3876 * Any registers holding previous allocations from this address
3877 * that have not yet been initialized must be marked invalid.
3878 */
3879 markUninitRefsAsInvalid(workRegs, insnRegCount, uninitMap,
3880 uninitType);
3881
3882 /* add the new uninitialized reference to the register ste */
3883 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003884 uninitType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003885 }
3886 break;
3887 case OP_NEW_ARRAY:
Andy McFadden62a75162009-04-17 17:23:37 -07003888 resClass = dvmOptResolveClass(meth->clazz, decInsn.vC, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003889 if (resClass == NULL) {
3890 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vC);
3891 dvmLogUnableToResolveClass(badClassDesc, meth);
3892 LOG_VFY("VFY: unable to resolve new-array %d (%s) in %s\n",
3893 decInsn.vC, badClassDesc, meth->clazz->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003894 assert(failure != VERIFY_ERROR_GENERIC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003895 } else if (!dvmIsArrayClass(resClass)) {
3896 LOG_VFY("VFY: new-array on non-array class\n");
Andy McFadden62a75162009-04-17 17:23:37 -07003897 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003898 } else {
3899 /* make sure "size" register is valid type */
3900 verifyRegisterType(workRegs, insnRegCount, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07003901 kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003902 /* set register type to array class */
3903 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003904 regTypeFromClass(resClass), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003905 }
3906 break;
3907 case OP_FILLED_NEW_ARRAY:
3908 case OP_FILLED_NEW_ARRAY_RANGE:
Andy McFadden62a75162009-04-17 17:23:37 -07003909 resClass = dvmOptResolveClass(meth->clazz, decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003910 if (resClass == NULL) {
3911 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vB);
3912 dvmLogUnableToResolveClass(badClassDesc, meth);
3913 LOG_VFY("VFY: unable to resolve filled-array %d (%s) in %s\n",
3914 decInsn.vB, badClassDesc, meth->clazz->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003915 assert(failure != VERIFY_ERROR_GENERIC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003916 } else if (!dvmIsArrayClass(resClass)) {
3917 LOG_VFY("VFY: filled-new-array on non-array class\n");
Andy McFadden62a75162009-04-17 17:23:37 -07003918 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003919 } else {
3920 bool isRange = (decInsn.opCode == OP_FILLED_NEW_ARRAY_RANGE);
3921
3922 /* check the arguments to the instruction */
3923 verifyFilledNewArrayRegs(meth, workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07003924 resClass, isRange, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003925 /* filled-array result goes into "result" register */
3926 setResultRegisterType(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07003927 regTypeFromClass(resClass), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003928 justSetResult = true;
3929 }
3930 break;
3931
3932 case OP_CMPL_FLOAT:
3933 case OP_CMPG_FLOAT:
3934 verifyRegisterType(workRegs, insnRegCount, decInsn.vB, kRegTypeFloat,
Andy McFadden62a75162009-04-17 17:23:37 -07003935 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003936 verifyRegisterType(workRegs, insnRegCount, decInsn.vC, kRegTypeFloat,
Andy McFadden62a75162009-04-17 17:23:37 -07003937 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003938 setRegisterType(workRegs, insnRegCount, decInsn.vA, kRegTypeBoolean,
Andy McFadden62a75162009-04-17 17:23:37 -07003939 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003940 break;
3941 case OP_CMPL_DOUBLE:
3942 case OP_CMPG_DOUBLE:
3943 verifyRegisterType(workRegs, insnRegCount, decInsn.vB, kRegTypeDoubleLo,
Andy McFadden62a75162009-04-17 17:23:37 -07003944 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003945 verifyRegisterType(workRegs, insnRegCount, decInsn.vC, kRegTypeDoubleLo,
Andy McFadden62a75162009-04-17 17:23:37 -07003946 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003947 setRegisterType(workRegs, insnRegCount, decInsn.vA, kRegTypeBoolean,
Andy McFadden62a75162009-04-17 17:23:37 -07003948 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003949 break;
3950 case OP_CMP_LONG:
3951 verifyRegisterType(workRegs, insnRegCount, decInsn.vB, kRegTypeLongLo,
Andy McFadden62a75162009-04-17 17:23:37 -07003952 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003953 verifyRegisterType(workRegs, insnRegCount, decInsn.vC, kRegTypeLongLo,
Andy McFadden62a75162009-04-17 17:23:37 -07003954 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003955 setRegisterType(workRegs, insnRegCount, decInsn.vA, kRegTypeBoolean,
Andy McFadden62a75162009-04-17 17:23:37 -07003956 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003957 break;
3958
3959 case OP_THROW:
3960 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07003961 decInsn.vA, &failure);
3962 if (VERIFY_OK(failure) && resClass != NULL) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003963 if (!dvmInstanceof(resClass, gDvm.classJavaLangThrowable)) {
3964 LOG_VFY("VFY: thrown class %s not instanceof Throwable\n",
3965 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003966 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003967 }
3968 }
3969 break;
3970
3971 case OP_GOTO:
3972 case OP_GOTO_16:
3973 case OP_GOTO_32:
3974 /* no effect on or use of registers */
3975 break;
3976
3977 case OP_PACKED_SWITCH:
3978 case OP_SPARSE_SWITCH:
3979 /* verify that vAA is an integer, or can be converted to one */
3980 verifyRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003981 kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003982 break;
3983
3984 case OP_FILL_ARRAY_DATA:
3985 {
3986 RegType valueType;
3987 const u2 *arrayData;
3988 u2 elemWidth;
3989
3990 /* Similar to the verification done for APUT */
3991 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07003992 decInsn.vA, &failure);
3993 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003994 break;
3995
3996 /* resClass can be null if the reg type is Zero */
3997 if (resClass == NULL)
3998 break;
3999
4000 if (!dvmIsArrayClass(resClass) || resClass->arrayDim != 1 ||
4001 resClass->elementClass->primitiveType == PRIM_NOT ||
4002 resClass->elementClass->primitiveType == PRIM_VOID)
4003 {
4004 LOG_VFY("VFY: invalid fill-array-data on %s\n",
4005 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004006 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004007 break;
4008 }
4009
4010 valueType = primitiveTypeToRegType(
4011 resClass->elementClass->primitiveType);
4012 assert(valueType != kRegTypeUnknown);
4013
4014 /*
4015 * Now verify if the element width in the table matches the element
4016 * width declared in the array
4017 */
4018 arrayData = insns + (insns[1] | (((s4)insns[2]) << 16));
4019 if (arrayData[0] != kArrayDataSignature) {
4020 LOG_VFY("VFY: invalid magic for array-data\n");
Andy McFadden62a75162009-04-17 17:23:37 -07004021 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004022 break;
4023 }
4024
4025 switch (resClass->elementClass->primitiveType) {
4026 case PRIM_BOOLEAN:
4027 case PRIM_BYTE:
4028 elemWidth = 1;
4029 break;
4030 case PRIM_CHAR:
4031 case PRIM_SHORT:
4032 elemWidth = 2;
4033 break;
4034 case PRIM_FLOAT:
4035 case PRIM_INT:
4036 elemWidth = 4;
4037 break;
4038 case PRIM_DOUBLE:
4039 case PRIM_LONG:
4040 elemWidth = 8;
4041 break;
4042 default:
4043 elemWidth = 0;
4044 break;
4045 }
4046
4047 /*
4048 * Since we don't compress the data in Dex, expect to see equal
4049 * width of data stored in the table and expected from the array
4050 * class.
4051 */
4052 if (arrayData[1] != elemWidth) {
4053 LOG_VFY("VFY: array-data size mismatch (%d vs %d)\n",
4054 arrayData[1], elemWidth);
Andy McFadden62a75162009-04-17 17:23:37 -07004055 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004056 }
4057 }
4058 break;
4059
4060 case OP_IF_EQ:
4061 case OP_IF_NE:
4062 {
4063 RegType type1, type2;
4064 bool tmpResult;
4065
Andy McFadden62a75162009-04-17 17:23:37 -07004066 type1 = getRegisterType(workRegs, insnRegCount, decInsn.vA,
4067 &failure);
4068 type2 = getRegisterType(workRegs, insnRegCount, decInsn.vB,
4069 &failure);
4070 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004071 break;
4072
4073 /* both references? */
4074 if (regTypeIsReference(type1) && regTypeIsReference(type2))
4075 break;
4076
4077 /* both category-1nr? */
Andy McFadden62a75162009-04-17 17:23:37 -07004078 checkTypeCategory(type1, kTypeCategory1nr, &failure);
4079 checkTypeCategory(type2, kTypeCategory1nr, &failure);
4080 if (!VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004081 LOG_VFY("VFY: args to if-eq/if-ne must both be refs or cat1\n");
4082 break;
4083 }
4084 }
4085 break;
4086 case OP_IF_LT:
4087 case OP_IF_GE:
4088 case OP_IF_GT:
4089 case OP_IF_LE:
Andy McFadden62a75162009-04-17 17:23:37 -07004090 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vA, &failure);
4091 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004092 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004093 checkTypeCategory(tmpType, kTypeCategory1nr, &failure);
4094 if (!VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004095 LOG_VFY("VFY: args to 'if' must be cat-1nr\n");
4096 break;
4097 }
Andy McFadden62a75162009-04-17 17:23:37 -07004098 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vB, &failure);
4099 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004100 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004101 checkTypeCategory(tmpType, kTypeCategory1nr, &failure);
4102 if (!VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004103 LOG_VFY("VFY: args to 'if' must be cat-1nr\n");
4104 break;
4105 }
4106 break;
4107 case OP_IF_EQZ:
4108 case OP_IF_NEZ:
Andy McFadden62a75162009-04-17 17:23:37 -07004109 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vA, &failure);
4110 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004111 break;
4112 if (regTypeIsReference(tmpType))
4113 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004114 checkTypeCategory(tmpType, kTypeCategory1nr, &failure);
4115 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004116 LOG_VFY("VFY: expected cat-1 arg to if\n");
4117 break;
4118 case OP_IF_LTZ:
4119 case OP_IF_GEZ:
4120 case OP_IF_GTZ:
4121 case OP_IF_LEZ:
Andy McFadden62a75162009-04-17 17:23:37 -07004122 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vA, &failure);
4123 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004124 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004125 checkTypeCategory(tmpType, kTypeCategory1nr, &failure);
4126 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004127 LOG_VFY("VFY: expected cat-1 arg to if\n");
4128 break;
4129
4130 case OP_AGET:
4131 tmpType = kRegTypeInteger;
4132 goto aget_1nr_common;
4133 case OP_AGET_BOOLEAN:
4134 tmpType = kRegTypeBoolean;
4135 goto aget_1nr_common;
4136 case OP_AGET_BYTE:
4137 tmpType = kRegTypeByte;
4138 goto aget_1nr_common;
4139 case OP_AGET_CHAR:
4140 tmpType = kRegTypeChar;
4141 goto aget_1nr_common;
4142 case OP_AGET_SHORT:
4143 tmpType = kRegTypeShort;
4144 goto aget_1nr_common;
4145aget_1nr_common:
4146 {
4147 RegType srcType, indexType;
4148
4149 indexType = getRegisterType(workRegs, insnRegCount, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004150 &failure);
4151 checkArrayIndexType(meth, indexType, &failure);
4152 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004153 break;
4154
4155 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07004156 decInsn.vB, &failure);
4157 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004158 break;
4159 if (resClass != NULL) {
4160 /* verify the class */
4161 if (!dvmIsArrayClass(resClass) || resClass->arrayDim != 1 ||
4162 resClass->elementClass->primitiveType == PRIM_NOT)
4163 {
4164 LOG_VFY("VFY: invalid aget-1nr target %s\n",
4165 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004166 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004167 break;
4168 }
4169
4170 /* make sure array type matches instruction */
4171 srcType = primitiveTypeToRegType(
4172 resClass->elementClass->primitiveType);
4173
4174 if (!checkFieldArrayStore1nr(tmpType, srcType)) {
4175 LOG_VFY("VFY: invalid aget-1nr, array type=%d with"
4176 " inst type=%d (on %s)\n",
4177 srcType, tmpType, resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004178 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004179 break;
4180 }
4181
4182 }
4183 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004184 tmpType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004185 }
4186 break;
4187
4188 case OP_AGET_WIDE:
4189 {
4190 RegType dstType, indexType;
4191
4192 indexType = getRegisterType(workRegs, insnRegCount, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004193 &failure);
4194 checkArrayIndexType(meth, indexType, &failure);
4195 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004196 break;
4197
4198 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07004199 decInsn.vB, &failure);
4200 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004201 break;
4202 if (resClass != NULL) {
4203 /* verify the class */
4204 if (!dvmIsArrayClass(resClass) || resClass->arrayDim != 1 ||
4205 resClass->elementClass->primitiveType == PRIM_NOT)
4206 {
4207 LOG_VFY("VFY: invalid aget-wide target %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 /* try to refine "dstType" */
4214 switch (resClass->elementClass->primitiveType) {
4215 case PRIM_LONG:
4216 dstType = kRegTypeLongLo;
4217 break;
4218 case PRIM_DOUBLE:
4219 dstType = kRegTypeDoubleLo;
4220 break;
4221 default:
4222 LOG_VFY("VFY: invalid aget-wide on %s\n",
4223 resClass->descriptor);
4224 dstType = kRegTypeUnknown;
Andy McFadden62a75162009-04-17 17:23:37 -07004225 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004226 break;
4227 }
4228 } else {
4229 /*
4230 * Null array ref; this code path will fail at runtime. We
4231 * know this is either long or double, and we don't really
4232 * discriminate between those during verification, so we
4233 * call it a long.
4234 */
4235 dstType = kRegTypeLongLo;
4236 }
4237 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004238 dstType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004239 }
4240 break;
4241
4242 case OP_AGET_OBJECT:
4243 {
4244 RegType dstType, indexType;
4245
4246 indexType = getRegisterType(workRegs, insnRegCount, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004247 &failure);
4248 checkArrayIndexType(meth, indexType, &failure);
4249 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004250 break;
4251
4252 /* get the class of the array we're pulling an object from */
4253 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07004254 decInsn.vB, &failure);
4255 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004256 break;
4257 if (resClass != NULL) {
4258 ClassObject* elementClass;
4259
4260 assert(resClass != NULL);
4261 if (!dvmIsArrayClass(resClass)) {
4262 LOG_VFY("VFY: aget-object on non-array class\n");
Andy McFadden62a75162009-04-17 17:23:37 -07004263 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004264 break;
4265 }
4266 assert(resClass->elementClass != NULL);
4267
4268 /*
4269 * Find the element class. resClass->elementClass indicates
4270 * the basic type, which won't be what we want for a
4271 * multi-dimensional array.
4272 */
4273 if (resClass->descriptor[1] == '[') {
4274 assert(resClass->arrayDim > 1);
4275 elementClass = dvmFindArrayClass(&resClass->descriptor[1],
4276 resClass->classLoader);
4277 } else if (resClass->descriptor[1] == 'L') {
4278 assert(resClass->arrayDim == 1);
4279 elementClass = resClass->elementClass;
4280 } else {
4281 LOG_VFY("VFY: aget-object on non-ref array class (%s)\n",
4282 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004283 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004284 break;
4285 }
4286
4287 dstType = regTypeFromClass(elementClass);
4288 } else {
4289 /*
4290 * The array reference is NULL, so the current code path will
4291 * throw an exception. For proper merging with later code
4292 * paths, and correct handling of "if-eqz" tests on the
4293 * result of the array get, we want to treat this as a null
4294 * reference.
4295 */
4296 dstType = kRegTypeZero;
4297 }
4298 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004299 dstType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004300 }
4301 break;
4302 case OP_APUT:
4303 tmpType = kRegTypeInteger;
4304 goto aput_1nr_common;
4305 case OP_APUT_BOOLEAN:
4306 tmpType = kRegTypeBoolean;
4307 goto aput_1nr_common;
4308 case OP_APUT_BYTE:
4309 tmpType = kRegTypeByte;
4310 goto aput_1nr_common;
4311 case OP_APUT_CHAR:
4312 tmpType = kRegTypeChar;
4313 goto aput_1nr_common;
4314 case OP_APUT_SHORT:
4315 tmpType = kRegTypeShort;
4316 goto aput_1nr_common;
4317aput_1nr_common:
4318 {
4319 RegType srcType, dstType, indexType;
4320
4321 indexType = getRegisterType(workRegs, insnRegCount, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004322 &failure);
4323 checkArrayIndexType(meth, indexType, &failure);
4324 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004325 break;
4326
4327 /* make sure the source register has the correct type */
4328 srcType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004329 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004330 if (!canConvertTo1nr(srcType, tmpType)) {
4331 LOG_VFY("VFY: invalid reg type %d on aput instr (need %d)\n",
4332 srcType, tmpType);
Andy McFadden62a75162009-04-17 17:23:37 -07004333 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004334 break;
4335 }
4336
4337 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07004338 decInsn.vB, &failure);
4339 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004340 break;
4341
4342 /* resClass can be null if the reg type is Zero */
4343 if (resClass == NULL)
4344 break;
4345
4346 if (!dvmIsArrayClass(resClass) || resClass->arrayDim != 1 ||
4347 resClass->elementClass->primitiveType == PRIM_NOT)
4348 {
4349 LOG_VFY("VFY: invalid aput-1nr on %s\n", resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004350 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004351 break;
4352 }
4353
4354 /* verify that instruction matches array */
4355 dstType = primitiveTypeToRegType(
4356 resClass->elementClass->primitiveType);
4357 assert(dstType != kRegTypeUnknown);
4358
4359 if (!checkFieldArrayStore1nr(tmpType, dstType)) {
4360 LOG_VFY("VFY: invalid aput-1nr on %s (inst=%d dst=%d)\n",
4361 resClass->descriptor, tmpType, dstType);
Andy McFadden62a75162009-04-17 17:23:37 -07004362 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004363 break;
4364 }
4365 }
4366 break;
4367 case OP_APUT_WIDE:
4368 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004369 &failure);
4370 checkArrayIndexType(meth, tmpType, &failure);
4371 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004372 break;
4373
Andy McFadden62a75162009-04-17 17:23:37 -07004374 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vA, &failure);
4375 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004376 RegType typeHi =
Andy McFadden62a75162009-04-17 17:23:37 -07004377 getRegisterType(workRegs, insnRegCount, decInsn.vA+1, &failure);
4378 checkTypeCategory(tmpType, kTypeCategory2, &failure);
4379 checkWidePair(tmpType, typeHi, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004380 }
Andy McFadden62a75162009-04-17 17:23:37 -07004381 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004382 break;
4383
4384 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07004385 decInsn.vB, &failure);
4386 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004387 break;
4388 if (resClass != NULL) {
4389 /* verify the class and try to refine "dstType" */
4390 if (!dvmIsArrayClass(resClass) || resClass->arrayDim != 1 ||
4391 resClass->elementClass->primitiveType == PRIM_NOT)
4392 {
4393 LOG_VFY("VFY: invalid aput-wide on %s\n",
4394 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004395 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004396 break;
4397 }
4398
4399 switch (resClass->elementClass->primitiveType) {
4400 case PRIM_LONG:
4401 case PRIM_DOUBLE:
4402 /* these are okay */
4403 break;
4404 default:
4405 LOG_VFY("VFY: invalid aput-wide on %s\n",
4406 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004407 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004408 break;
4409 }
4410 }
4411 break;
4412 case OP_APUT_OBJECT:
4413 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004414 &failure);
4415 checkArrayIndexType(meth, tmpType, &failure);
4416 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004417 break;
4418
4419 /* get the ref we're storing; Zero is okay, Uninit is not */
4420 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07004421 decInsn.vA, &failure);
4422 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004423 break;
4424 if (resClass != NULL) {
4425 ClassObject* arrayClass;
4426 ClassObject* elementClass;
4427
4428 /*
4429 * Get the array class. If the array ref is null, we won't
4430 * have type information (and we'll crash at runtime with a
4431 * null pointer exception).
4432 */
4433 arrayClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07004434 decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004435
4436 if (arrayClass != NULL) {
4437 /* see if the array holds a compatible type */
4438 if (!dvmIsArrayClass(arrayClass)) {
4439 LOG_VFY("VFY: invalid aput-object on %s\n",
4440 arrayClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004441 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004442 break;
4443 }
4444
4445 /*
4446 * Find the element class. resClass->elementClass indicates
4447 * the basic type, which won't be what we want for a
4448 * multi-dimensional array.
4449 *
4450 * All we want to check here is that the element type is a
4451 * reference class. We *don't* check instanceof here, because
4452 * you can still put a String into a String[] after the latter
4453 * has been cast to an Object[].
4454 */
4455 if (arrayClass->descriptor[1] == '[') {
4456 assert(arrayClass->arrayDim > 1);
4457 elementClass = dvmFindArrayClass(&arrayClass->descriptor[1],
4458 arrayClass->classLoader);
4459 } else {
4460 assert(arrayClass->arrayDim == 1);
4461 elementClass = arrayClass->elementClass;
4462 }
4463 if (elementClass->primitiveType != PRIM_NOT) {
4464 LOG_VFY("VFY: invalid aput-object of %s into %s\n",
4465 resClass->descriptor, arrayClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004466 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004467 break;
4468 }
4469 }
4470 }
4471 break;
4472
4473 case OP_IGET:
4474 tmpType = kRegTypeInteger;
4475 goto iget_1nr_common;
4476 case OP_IGET_BOOLEAN:
4477 tmpType = kRegTypeBoolean;
4478 goto iget_1nr_common;
4479 case OP_IGET_BYTE:
4480 tmpType = kRegTypeByte;
4481 goto iget_1nr_common;
4482 case OP_IGET_CHAR:
4483 tmpType = kRegTypeChar;
4484 goto iget_1nr_common;
4485 case OP_IGET_SHORT:
4486 tmpType = kRegTypeShort;
4487 goto iget_1nr_common;
4488iget_1nr_common:
4489 {
4490 ClassObject* fieldClass;
4491 InstField* instField;
4492 RegType objType, fieldType;
4493
4494 objType = getRegisterType(workRegs, insnRegCount, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07004495 &failure);
4496 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004497 break;
4498 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004499 &failure);
4500 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004501 break;
4502
4503 /* make sure the field's type is compatible with expectation */
4504 fieldType = primSigCharToRegType(instField->field.signature[0]);
4505 if (fieldType == kRegTypeUnknown ||
4506 !checkFieldArrayStore1nr(tmpType, fieldType))
4507 {
4508 LOG_VFY("VFY: invalid iget-1nr of %s.%s (inst=%d field=%d)\n",
4509 instField->field.clazz->descriptor,
4510 instField->field.name, tmpType, fieldType);
Andy McFadden62a75162009-04-17 17:23:37 -07004511 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004512 break;
4513 }
4514
Andy McFadden62a75162009-04-17 17:23:37 -07004515 setRegisterType(workRegs, insnRegCount, decInsn.vA, tmpType,
4516 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004517 }
4518 break;
4519 case OP_IGET_WIDE:
Andy McFadden861b3382010-03-05 15:58:31 -08004520 case OP_IGET_WIDE_VOLATILE:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004521 {
4522 RegType dstType;
4523 ClassObject* fieldClass;
4524 InstField* instField;
4525 RegType objType;
4526
4527 objType = getRegisterType(workRegs, insnRegCount, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07004528 &failure);
4529 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004530 break;
4531 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004532 &failure);
4533 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004534 break;
4535 /* check the type, which should be prim */
4536 switch (instField->field.signature[0]) {
4537 case 'D':
4538 dstType = kRegTypeDoubleLo;
4539 break;
4540 case 'J':
4541 dstType = kRegTypeLongLo;
4542 break;
4543 default:
4544 LOG_VFY("VFY: invalid iget-wide of %s.%s\n",
4545 instField->field.clazz->descriptor,
4546 instField->field.name);
4547 dstType = kRegTypeUnknown;
Andy McFadden62a75162009-04-17 17:23:37 -07004548 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004549 break;
4550 }
Andy McFadden62a75162009-04-17 17:23:37 -07004551 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004552 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004553 dstType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004554 }
Andy McFadden861b3382010-03-05 15:58:31 -08004555 if (VERIFY_OK(failure)) {
4556 if (decInsn.opCode != OP_IGET_WIDE_VOLATILE &&
4557 dvmIsVolatileField(&instField->field))
4558 {
4559 replaceVolatileInstruction(meth, insnFlags, insnIdx);
4560 }
4561 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004562 }
4563 break;
4564 case OP_IGET_OBJECT:
4565 {
4566 ClassObject* fieldClass;
4567 InstField* instField;
4568 RegType objType;
4569
4570 objType = getRegisterType(workRegs, insnRegCount, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07004571 &failure);
4572 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004573 break;
4574 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004575 &failure);
4576 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004577 break;
4578 fieldClass = getFieldClass(meth, &instField->field);
4579 if (fieldClass == NULL) {
4580 /* class not found or primitive type */
4581 LOG_VFY("VFY: unable to recover field class from '%s'\n",
4582 instField->field.signature);
Andy McFadden62a75162009-04-17 17:23:37 -07004583 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004584 break;
4585 }
Andy McFadden62a75162009-04-17 17:23:37 -07004586 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004587 assert(!dvmIsPrimitiveClass(fieldClass));
4588 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004589 regTypeFromClass(fieldClass), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004590 }
4591 }
4592 break;
4593 case OP_IPUT:
4594 tmpType = kRegTypeInteger;
4595 goto iput_1nr_common;
4596 case OP_IPUT_BOOLEAN:
4597 tmpType = kRegTypeBoolean;
4598 goto iput_1nr_common;
4599 case OP_IPUT_BYTE:
4600 tmpType = kRegTypeByte;
4601 goto iput_1nr_common;
4602 case OP_IPUT_CHAR:
4603 tmpType = kRegTypeChar;
4604 goto iput_1nr_common;
4605 case OP_IPUT_SHORT:
4606 tmpType = kRegTypeShort;
4607 goto iput_1nr_common;
4608iput_1nr_common:
4609 {
4610 RegType srcType, fieldType, objType;
4611 ClassObject* fieldClass;
4612 InstField* instField;
4613
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004614 srcType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004615 &failure);
Andy McFaddenb5f64bc2009-06-10 14:11:07 -07004616
4617 /*
4618 * javac generates synthetic functions that write byte values
4619 * into boolean fields.
4620 */
4621 if (tmpType == kRegTypeBoolean && srcType == kRegTypeByte)
4622 srcType = kRegTypeBoolean;
4623
4624 /* make sure the source register has the correct type */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004625 if (!canConvertTo1nr(srcType, tmpType)) {
4626 LOG_VFY("VFY: invalid reg type %d on iput instr (need %d)\n",
4627 srcType, tmpType);
Andy McFadden62a75162009-04-17 17:23:37 -07004628 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004629 break;
4630 }
4631
4632 objType = getRegisterType(workRegs, insnRegCount, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07004633 &failure);
4634 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004635 break;
4636 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004637 &failure);
4638 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004639 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004640 checkFinalFieldAccess(meth, &instField->field, &failure);
4641 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004642 break;
4643
4644 /* get type of field we're storing into */
4645 fieldType = primSigCharToRegType(instField->field.signature[0]);
4646 if (fieldType == kRegTypeUnknown ||
4647 !checkFieldArrayStore1nr(tmpType, fieldType))
4648 {
4649 LOG_VFY("VFY: invalid iput-1nr of %s.%s (inst=%d field=%d)\n",
4650 instField->field.clazz->descriptor,
4651 instField->field.name, tmpType, fieldType);
Andy McFadden62a75162009-04-17 17:23:37 -07004652 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004653 break;
4654 }
4655 }
4656 break;
4657 case OP_IPUT_WIDE:
Andy McFadden861b3382010-03-05 15:58:31 -08004658 case OP_IPUT_WIDE_VOLATILE:
Andy McFadden62a75162009-04-17 17:23:37 -07004659 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vA, &failure);
4660 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004661 RegType typeHi =
Andy McFadden62a75162009-04-17 17:23:37 -07004662 getRegisterType(workRegs, insnRegCount, decInsn.vA+1, &failure);
4663 checkTypeCategory(tmpType, kTypeCategory2, &failure);
4664 checkWidePair(tmpType, typeHi, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004665 }
Andy McFadden62a75162009-04-17 17:23:37 -07004666 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004667 ClassObject* fieldClass;
4668 InstField* instField;
4669 RegType objType;
4670
4671 objType = getRegisterType(workRegs, insnRegCount, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07004672 &failure);
4673 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004674 break;
4675 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004676 &failure);
4677 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004678 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004679 checkFinalFieldAccess(meth, &instField->field, &failure);
4680 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004681 break;
4682
4683 /* check the type, which should be prim */
4684 switch (instField->field.signature[0]) {
4685 case 'D':
4686 case 'J':
4687 /* these are okay (and interchangeable) */
4688 break;
4689 default:
4690 LOG_VFY("VFY: invalid iput-wide of %s.%s\n",
4691 instField->field.clazz->descriptor,
4692 instField->field.name);
Andy McFadden62a75162009-04-17 17:23:37 -07004693 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004694 break;
4695 }
Andy McFadden861b3382010-03-05 15:58:31 -08004696 if (VERIFY_OK(failure)) {
4697 if (decInsn.opCode != OP_IPUT_WIDE_VOLATILE &&
4698 dvmIsVolatileField(&instField->field))
4699 {
4700 replaceVolatileInstruction(meth, insnFlags, insnIdx);
4701 }
4702 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004703 }
4704 break;
4705 case OP_IPUT_OBJECT:
4706 {
4707 ClassObject* fieldClass;
4708 ClassObject* valueClass;
4709 InstField* instField;
4710 RegType objType, valueType;
4711
4712 objType = getRegisterType(workRegs, insnRegCount, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07004713 &failure);
4714 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004715 break;
4716 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004717 &failure);
4718 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004719 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004720 checkFinalFieldAccess(meth, &instField->field, &failure);
4721 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004722 break;
4723
4724 fieldClass = getFieldClass(meth, &instField->field);
4725 if (fieldClass == NULL) {
4726 LOG_VFY("VFY: unable to recover field class from '%s'\n",
4727 instField->field.signature);
Andy McFadden62a75162009-04-17 17:23:37 -07004728 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004729 break;
4730 }
4731
4732 valueType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004733 &failure);
4734 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004735 break;
4736 if (!regTypeIsReference(valueType)) {
4737 LOG_VFY("VFY: storing non-ref v%d into ref field '%s' (%s)\n",
4738 decInsn.vA, instField->field.name,
4739 fieldClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004740 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004741 break;
4742 }
4743 if (valueType != kRegTypeZero) {
4744 valueClass = regTypeInitializedReferenceToClass(valueType);
4745 if (valueClass == NULL) {
4746 LOG_VFY("VFY: storing uninit ref v%d into ref field\n",
4747 decInsn.vA);
Andy McFadden62a75162009-04-17 17:23:37 -07004748 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004749 break;
4750 }
4751 /* allow if field is any interface or field is base class */
4752 if (!dvmIsInterfaceClass(fieldClass) &&
4753 !dvmInstanceof(valueClass, fieldClass))
4754 {
4755 LOG_VFY("VFY: storing type '%s' into field type '%s' (%s.%s)\n",
4756 valueClass->descriptor, fieldClass->descriptor,
4757 instField->field.clazz->descriptor,
4758 instField->field.name);
Andy McFadden62a75162009-04-17 17:23:37 -07004759 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004760 break;
4761 }
4762 }
4763 }
4764 break;
4765
4766 case OP_SGET:
4767 tmpType = kRegTypeInteger;
4768 goto sget_1nr_common;
4769 case OP_SGET_BOOLEAN:
4770 tmpType = kRegTypeBoolean;
4771 goto sget_1nr_common;
4772 case OP_SGET_BYTE:
4773 tmpType = kRegTypeByte;
4774 goto sget_1nr_common;
4775 case OP_SGET_CHAR:
4776 tmpType = kRegTypeChar;
4777 goto sget_1nr_common;
4778 case OP_SGET_SHORT:
4779 tmpType = kRegTypeShort;
4780 goto sget_1nr_common;
4781sget_1nr_common:
4782 {
4783 StaticField* staticField;
4784 RegType fieldType;
4785
Andy McFadden62a75162009-04-17 17:23:37 -07004786 staticField = getStaticField(meth, decInsn.vB, &failure);
4787 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004788 break;
4789
4790 /*
4791 * Make sure the field's type is compatible with expectation.
4792 * We can get ourselves into trouble if we mix & match loads
4793 * and stores with different widths, so rather than just checking
4794 * "canConvertTo1nr" we require that the field types have equal
4795 * widths. (We can't generally require an exact type match,
4796 * because e.g. "int" and "float" are interchangeable.)
4797 */
4798 fieldType = primSigCharToRegType(staticField->field.signature[0]);
4799 if (!checkFieldArrayStore1nr(tmpType, fieldType)) {
4800 LOG_VFY("VFY: invalid sget-1nr of %s.%s (inst=%d actual=%d)\n",
4801 staticField->field.clazz->descriptor,
4802 staticField->field.name, tmpType, fieldType);
Andy McFadden62a75162009-04-17 17:23:37 -07004803 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004804 break;
4805 }
4806
Andy McFadden62a75162009-04-17 17:23:37 -07004807 setRegisterType(workRegs, insnRegCount, decInsn.vA, tmpType,
4808 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004809 }
4810 break;
4811 case OP_SGET_WIDE:
Andy McFadden861b3382010-03-05 15:58:31 -08004812 case OP_SGET_WIDE_VOLATILE:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004813 {
4814 StaticField* staticField;
4815 RegType dstType;
4816
Andy McFadden62a75162009-04-17 17:23:37 -07004817 staticField = getStaticField(meth, decInsn.vB, &failure);
4818 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004819 break;
4820 /* check the type, which should be prim */
4821 switch (staticField->field.signature[0]) {
4822 case 'D':
4823 dstType = kRegTypeDoubleLo;
4824 break;
4825 case 'J':
4826 dstType = kRegTypeLongLo;
4827 break;
4828 default:
4829 LOG_VFY("VFY: invalid sget-wide of %s.%s\n",
4830 staticField->field.clazz->descriptor,
4831 staticField->field.name);
4832 dstType = kRegTypeUnknown;
Andy McFadden62a75162009-04-17 17:23:37 -07004833 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004834 break;
4835 }
Andy McFadden62a75162009-04-17 17:23:37 -07004836 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004837 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004838 dstType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004839 }
Andy McFadden861b3382010-03-05 15:58:31 -08004840 if (VERIFY_OK(failure)) {
4841 if (decInsn.opCode != OP_SGET_WIDE_VOLATILE &&
4842 dvmIsVolatileField(&staticField->field))
4843 {
4844 replaceVolatileInstruction(meth, insnFlags, insnIdx);
4845 }
4846 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004847 }
4848 break;
4849 case OP_SGET_OBJECT:
4850 {
4851 StaticField* staticField;
4852 ClassObject* fieldClass;
4853
Andy McFadden62a75162009-04-17 17:23:37 -07004854 staticField = getStaticField(meth, decInsn.vB, &failure);
4855 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004856 break;
4857 fieldClass = getFieldClass(meth, &staticField->field);
4858 if (fieldClass == NULL) {
4859 LOG_VFY("VFY: unable to recover field class from '%s'\n",
4860 staticField->field.signature);
Andy McFadden62a75162009-04-17 17:23:37 -07004861 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004862 break;
4863 }
4864 if (dvmIsPrimitiveClass(fieldClass)) {
4865 LOG_VFY("VFY: attempt to get prim field with sget-object\n");
Andy McFadden62a75162009-04-17 17:23:37 -07004866 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004867 break;
4868 }
4869 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004870 regTypeFromClass(fieldClass), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004871 }
4872 break;
4873 case OP_SPUT:
4874 tmpType = kRegTypeInteger;
4875 goto sput_1nr_common;
4876 case OP_SPUT_BOOLEAN:
4877 tmpType = kRegTypeBoolean;
4878 goto sput_1nr_common;
4879 case OP_SPUT_BYTE:
4880 tmpType = kRegTypeByte;
4881 goto sput_1nr_common;
4882 case OP_SPUT_CHAR:
4883 tmpType = kRegTypeChar;
4884 goto sput_1nr_common;
4885 case OP_SPUT_SHORT:
4886 tmpType = kRegTypeShort;
4887 goto sput_1nr_common;
4888sput_1nr_common:
4889 {
4890 RegType srcType, fieldType;
4891 StaticField* staticField;
4892
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004893 srcType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004894 &failure);
Andy McFaddenb5f64bc2009-06-10 14:11:07 -07004895
4896 /*
4897 * javac generates synthetic functions that write byte values
4898 * into boolean fields.
4899 */
4900 if (tmpType == kRegTypeBoolean && srcType == kRegTypeByte)
4901 srcType = kRegTypeBoolean;
4902
4903 /* make sure the source register has the correct type */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004904 if (!canConvertTo1nr(srcType, tmpType)) {
Andy McFaddenb5f64bc2009-06-10 14:11:07 -07004905 LOG_VFY("VFY: invalid reg type %d on sput instr (need %d)\n",
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004906 srcType, tmpType);
Andy McFadden62a75162009-04-17 17:23:37 -07004907 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004908 break;
4909 }
4910
Andy McFadden62a75162009-04-17 17:23:37 -07004911 staticField = getStaticField(meth, decInsn.vB, &failure);
4912 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004913 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004914 checkFinalFieldAccess(meth, &staticField->field, &failure);
4915 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004916 break;
4917
4918 /*
4919 * Get type of field we're storing into. We know that the
4920 * contents of the register match the instruction, but we also
4921 * need to ensure that the instruction matches the field type.
4922 * Using e.g. sput-short to write into a 32-bit integer field
4923 * can lead to trouble if we do 16-bit writes.
4924 */
4925 fieldType = primSigCharToRegType(staticField->field.signature[0]);
4926 if (!checkFieldArrayStore1nr(tmpType, fieldType)) {
4927 LOG_VFY("VFY: invalid sput-1nr of %s.%s (inst=%d actual=%d)\n",
4928 staticField->field.clazz->descriptor,
4929 staticField->field.name, tmpType, fieldType);
Andy McFadden62a75162009-04-17 17:23:37 -07004930 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004931 break;
4932 }
4933 }
4934 break;
4935 case OP_SPUT_WIDE:
Andy McFadden861b3382010-03-05 15:58:31 -08004936 case OP_SPUT_WIDE_VOLATILE:
Andy McFadden62a75162009-04-17 17:23:37 -07004937 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vA, &failure);
4938 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004939 RegType typeHi =
Andy McFadden62a75162009-04-17 17:23:37 -07004940 getRegisterType(workRegs, insnRegCount, decInsn.vA+1, &failure);
4941 checkTypeCategory(tmpType, kTypeCategory2, &failure);
4942 checkWidePair(tmpType, typeHi, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004943 }
Andy McFadden62a75162009-04-17 17:23:37 -07004944 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004945 StaticField* staticField;
4946
Andy McFadden62a75162009-04-17 17:23:37 -07004947 staticField = getStaticField(meth, decInsn.vB, &failure);
4948 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004949 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004950 checkFinalFieldAccess(meth, &staticField->field, &failure);
4951 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004952 break;
4953
4954 /* check the type, which should be prim */
4955 switch (staticField->field.signature[0]) {
4956 case 'D':
4957 case 'J':
4958 /* these are okay */
4959 break;
4960 default:
4961 LOG_VFY("VFY: invalid sput-wide of %s.%s\n",
4962 staticField->field.clazz->descriptor,
4963 staticField->field.name);
Andy McFadden62a75162009-04-17 17:23:37 -07004964 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004965 break;
4966 }
Andy McFadden861b3382010-03-05 15:58:31 -08004967 if (VERIFY_OK(failure)) {
4968 if (decInsn.opCode != OP_SPUT_WIDE_VOLATILE &&
4969 dvmIsVolatileField(&staticField->field))
4970 {
4971 replaceVolatileInstruction(meth, insnFlags, insnIdx);
4972 }
4973 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004974 }
4975 break;
4976 case OP_SPUT_OBJECT:
4977 {
4978 ClassObject* fieldClass;
4979 ClassObject* valueClass;
4980 StaticField* staticField;
4981 RegType valueType;
4982
Andy McFadden62a75162009-04-17 17:23:37 -07004983 staticField = getStaticField(meth, decInsn.vB, &failure);
4984 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004985 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004986 checkFinalFieldAccess(meth, &staticField->field, &failure);
4987 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004988 break;
4989
4990 fieldClass = getFieldClass(meth, &staticField->field);
4991 if (fieldClass == NULL) {
4992 LOG_VFY("VFY: unable to recover field class from '%s'\n",
4993 staticField->field.signature);
Andy McFadden62a75162009-04-17 17:23:37 -07004994 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004995 break;
4996 }
4997
4998 valueType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004999 &failure);
5000 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005001 break;
5002 if (!regTypeIsReference(valueType)) {
5003 LOG_VFY("VFY: storing non-ref v%d into ref field '%s' (%s)\n",
5004 decInsn.vA, staticField->field.name,
5005 fieldClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07005006 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005007 break;
5008 }
5009 if (valueType != kRegTypeZero) {
5010 valueClass = regTypeInitializedReferenceToClass(valueType);
5011 if (valueClass == NULL) {
5012 LOG_VFY("VFY: storing uninit ref v%d into ref field\n",
5013 decInsn.vA);
Andy McFadden62a75162009-04-17 17:23:37 -07005014 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005015 break;
5016 }
5017 /* allow if field is any interface or field is base class */
5018 if (!dvmIsInterfaceClass(fieldClass) &&
5019 !dvmInstanceof(valueClass, fieldClass))
5020 {
5021 LOG_VFY("VFY: storing type '%s' into field type '%s' (%s.%s)\n",
5022 valueClass->descriptor, fieldClass->descriptor,
5023 staticField->field.clazz->descriptor,
5024 staticField->field.name);
Andy McFadden62a75162009-04-17 17:23:37 -07005025 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005026 break;
5027 }
5028 }
5029 }
5030 break;
5031
5032 case OP_INVOKE_VIRTUAL:
5033 case OP_INVOKE_VIRTUAL_RANGE:
5034 case OP_INVOKE_SUPER:
5035 case OP_INVOKE_SUPER_RANGE:
5036 {
5037 Method* calledMethod;
5038 RegType returnType;
5039 bool isRange;
5040 bool isSuper;
5041
5042 isRange = (decInsn.opCode == OP_INVOKE_VIRTUAL_RANGE ||
5043 decInsn.opCode == OP_INVOKE_SUPER_RANGE);
5044 isSuper = (decInsn.opCode == OP_INVOKE_SUPER ||
5045 decInsn.opCode == OP_INVOKE_SUPER_RANGE);
5046
5047 calledMethod = verifyInvocationArgs(meth, workRegs, insnRegCount,
5048 &decInsn, uninitMap, METHOD_VIRTUAL, isRange,
Andy McFadden62a75162009-04-17 17:23:37 -07005049 isSuper, &failure);
5050 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005051 break;
5052 returnType = getMethodReturnType(calledMethod);
Andy McFadden62a75162009-04-17 17:23:37 -07005053 setResultRegisterType(workRegs, insnRegCount, returnType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005054 justSetResult = true;
5055 }
5056 break;
5057 case OP_INVOKE_DIRECT:
5058 case OP_INVOKE_DIRECT_RANGE:
5059 {
5060 RegType returnType;
5061 Method* calledMethod;
5062 bool isRange;
5063
5064 isRange = (decInsn.opCode == OP_INVOKE_DIRECT_RANGE);
5065 calledMethod = verifyInvocationArgs(meth, workRegs, insnRegCount,
5066 &decInsn, uninitMap, METHOD_DIRECT, isRange,
Andy McFadden62a75162009-04-17 17:23:37 -07005067 false, &failure);
5068 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005069 break;
5070
5071 /*
5072 * Some additional checks when calling <init>. We know from
5073 * the invocation arg check that the "this" argument is an
5074 * instance of calledMethod->clazz. Now we further restrict
5075 * that to require that calledMethod->clazz is the same as
5076 * this->clazz or this->super, allowing the latter only if
5077 * the "this" argument is the same as the "this" argument to
5078 * this method (which implies that we're in <init> ourselves).
5079 */
5080 if (isInitMethod(calledMethod)) {
5081 RegType thisType;
5082 thisType = getInvocationThis(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07005083 &decInsn, &failure);
5084 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005085 break;
5086
5087 /* no null refs allowed (?) */
5088 if (thisType == kRegTypeZero) {
5089 LOG_VFY("VFY: unable to initialize null ref\n");
Andy McFadden62a75162009-04-17 17:23:37 -07005090 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005091 break;
5092 }
5093
5094 ClassObject* thisClass;
5095
5096 thisClass = regTypeReferenceToClass(thisType, uninitMap);
5097 assert(thisClass != NULL);
5098
5099 /* must be in same class or in superclass */
5100 if (calledMethod->clazz == thisClass->super) {
5101 if (thisClass != meth->clazz) {
5102 LOG_VFY("VFY: invoke-direct <init> on super only "
5103 "allowed for 'this' in <init>");
Andy McFadden62a75162009-04-17 17:23:37 -07005104 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005105 break;
5106 }
5107 } else if (calledMethod->clazz != thisClass) {
5108 LOG_VFY("VFY: invoke-direct <init> must be on current "
5109 "class or super\n");
Andy McFadden62a75162009-04-17 17:23:37 -07005110 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005111 break;
5112 }
5113
5114 /* arg must be an uninitialized reference */
5115 if (!regTypeIsUninitReference(thisType)) {
5116 LOG_VFY("VFY: can only initialize the uninitialized\n");
Andy McFadden62a75162009-04-17 17:23:37 -07005117 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005118 break;
5119 }
5120
5121 /*
5122 * Replace the uninitialized reference with an initialized
5123 * one, and clear the entry in the uninit map. We need to
5124 * do this for all registers that have the same object
5125 * instance in them, not just the "this" register.
5126 */
5127 int uidx = regTypeToUninitIndex(thisType);
5128 markRefsAsInitialized(workRegs, insnRegCount, uninitMap,
Andy McFadden62a75162009-04-17 17:23:37 -07005129 thisType, &failure);
5130 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005131 break;
5132 }
5133 returnType = getMethodReturnType(calledMethod);
5134 setResultRegisterType(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07005135 returnType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005136 justSetResult = true;
5137 }
5138 break;
5139 case OP_INVOKE_STATIC:
5140 case OP_INVOKE_STATIC_RANGE:
5141 {
5142 RegType returnType;
5143 Method* calledMethod;
5144 bool isRange;
5145
5146 isRange = (decInsn.opCode == OP_INVOKE_STATIC_RANGE);
5147 calledMethod = verifyInvocationArgs(meth, workRegs, insnRegCount,
5148 &decInsn, uninitMap, METHOD_STATIC, isRange,
Andy McFadden62a75162009-04-17 17:23:37 -07005149 false, &failure);
5150 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005151 break;
5152
5153 returnType = getMethodReturnType(calledMethod);
Andy McFadden62a75162009-04-17 17:23:37 -07005154 setResultRegisterType(workRegs, insnRegCount, returnType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005155 justSetResult = true;
5156 }
5157 break;
5158 case OP_INVOKE_INTERFACE:
5159 case OP_INVOKE_INTERFACE_RANGE:
5160 {
5161 RegType /*thisType,*/ returnType;
5162 Method* absMethod;
5163 bool isRange;
5164
5165 isRange = (decInsn.opCode == OP_INVOKE_INTERFACE_RANGE);
5166 absMethod = verifyInvocationArgs(meth, workRegs, insnRegCount,
5167 &decInsn, uninitMap, METHOD_INTERFACE, isRange,
Andy McFadden62a75162009-04-17 17:23:37 -07005168 false, &failure);
5169 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005170 break;
5171
5172#if 0 /* can't do this here, fails on dalvik test 052-verifier-fun */
5173 /*
5174 * Get the type of the "this" arg, which should always be an
5175 * interface class. Because we don't do a full merge on
5176 * interface classes, this might have reduced to Object.
5177 */
5178 thisType = getInvocationThis(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07005179 &decInsn, &failure);
5180 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005181 break;
5182
5183 if (thisType == kRegTypeZero) {
5184 /* null pointer always passes (and always fails at runtime) */
5185 } else {
5186 ClassObject* thisClass;
5187
5188 thisClass = regTypeInitializedReferenceToClass(thisType);
5189 if (thisClass == NULL) {
5190 LOG_VFY("VFY: interface call on uninitialized\n");
Andy McFadden62a75162009-04-17 17:23:37 -07005191 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005192 break;
5193 }
5194
5195 /*
5196 * Either "thisClass" needs to be the interface class that
5197 * defined absMethod, or absMethod's class needs to be one
5198 * of the interfaces implemented by "thisClass". (Or, if
5199 * we couldn't complete the merge, this will be Object.)
5200 */
5201 if (thisClass != absMethod->clazz &&
5202 thisClass != gDvm.classJavaLangObject &&
5203 !dvmImplements(thisClass, absMethod->clazz))
5204 {
5205 LOG_VFY("VFY: unable to match absMethod '%s' with %s interfaces\n",
5206 absMethod->name, thisClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07005207 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005208 break;
5209 }
5210 }
5211#endif
5212
5213 /*
5214 * We don't have an object instance, so we can't find the
5215 * concrete method. However, all of the type information is
5216 * in the abstract method, so we're good.
5217 */
5218 returnType = getMethodReturnType(absMethod);
Andy McFadden62a75162009-04-17 17:23:37 -07005219 setResultRegisterType(workRegs, insnRegCount, returnType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005220 justSetResult = true;
5221 }
5222 break;
5223
5224 case OP_NEG_INT:
5225 case OP_NOT_INT:
5226 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005227 kRegTypeInteger, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005228 break;
5229 case OP_NEG_LONG:
5230 case OP_NOT_LONG:
5231 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005232 kRegTypeLongLo, kRegTypeLongLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005233 break;
5234 case OP_NEG_FLOAT:
5235 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005236 kRegTypeFloat, kRegTypeFloat, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005237 break;
5238 case OP_NEG_DOUBLE:
5239 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005240 kRegTypeDoubleLo, kRegTypeDoubleLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005241 break;
5242 case OP_INT_TO_LONG:
5243 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005244 kRegTypeLongLo, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005245 break;
5246 case OP_INT_TO_FLOAT:
5247 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005248 kRegTypeFloat, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005249 break;
5250 case OP_INT_TO_DOUBLE:
5251 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005252 kRegTypeDoubleLo, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005253 break;
5254 case OP_LONG_TO_INT:
5255 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005256 kRegTypeInteger, kRegTypeLongLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005257 break;
5258 case OP_LONG_TO_FLOAT:
5259 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005260 kRegTypeFloat, kRegTypeLongLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005261 break;
5262 case OP_LONG_TO_DOUBLE:
5263 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005264 kRegTypeDoubleLo, kRegTypeLongLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005265 break;
5266 case OP_FLOAT_TO_INT:
5267 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005268 kRegTypeInteger, kRegTypeFloat, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005269 break;
5270 case OP_FLOAT_TO_LONG:
5271 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005272 kRegTypeLongLo, kRegTypeFloat, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005273 break;
5274 case OP_FLOAT_TO_DOUBLE:
5275 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005276 kRegTypeDoubleLo, kRegTypeFloat, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005277 break;
5278 case OP_DOUBLE_TO_INT:
5279 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005280 kRegTypeInteger, kRegTypeDoubleLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005281 break;
5282 case OP_DOUBLE_TO_LONG:
5283 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005284 kRegTypeLongLo, kRegTypeDoubleLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005285 break;
5286 case OP_DOUBLE_TO_FLOAT:
5287 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005288 kRegTypeFloat, kRegTypeDoubleLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005289 break;
5290 case OP_INT_TO_BYTE:
5291 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005292 kRegTypeByte, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005293 break;
5294 case OP_INT_TO_CHAR:
5295 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005296 kRegTypeChar, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005297 break;
5298 case OP_INT_TO_SHORT:
5299 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005300 kRegTypeShort, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005301 break;
5302
5303 case OP_ADD_INT:
5304 case OP_SUB_INT:
5305 case OP_MUL_INT:
5306 case OP_REM_INT:
5307 case OP_DIV_INT:
5308 case OP_SHL_INT:
5309 case OP_SHR_INT:
5310 case OP_USHR_INT:
5311 checkBinop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005312 kRegTypeInteger, kRegTypeInteger, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005313 break;
5314 case OP_AND_INT:
5315 case OP_OR_INT:
5316 case OP_XOR_INT:
5317 checkBinop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005318 kRegTypeInteger, kRegTypeInteger, kRegTypeInteger, true, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005319 break;
5320 case OP_ADD_LONG:
5321 case OP_SUB_LONG:
5322 case OP_MUL_LONG:
5323 case OP_DIV_LONG:
5324 case OP_REM_LONG:
5325 case OP_AND_LONG:
5326 case OP_OR_LONG:
5327 case OP_XOR_LONG:
5328 checkBinop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005329 kRegTypeLongLo, kRegTypeLongLo, kRegTypeLongLo, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005330 break;
5331 case OP_SHL_LONG:
5332 case OP_SHR_LONG:
5333 case OP_USHR_LONG:
5334 /* shift distance is Int, making these different from other binops */
5335 checkBinop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005336 kRegTypeLongLo, kRegTypeLongLo, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005337 break;
5338 case OP_ADD_FLOAT:
5339 case OP_SUB_FLOAT:
5340 case OP_MUL_FLOAT:
5341 case OP_DIV_FLOAT:
5342 case OP_REM_FLOAT:
5343 checkBinop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005344 kRegTypeFloat, kRegTypeFloat, kRegTypeFloat, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005345 break;
5346 case OP_ADD_DOUBLE:
5347 case OP_SUB_DOUBLE:
5348 case OP_MUL_DOUBLE:
5349 case OP_DIV_DOUBLE:
5350 case OP_REM_DOUBLE:
5351 checkBinop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005352 kRegTypeDoubleLo, kRegTypeDoubleLo, kRegTypeDoubleLo, false,
5353 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005354 break;
5355 case OP_ADD_INT_2ADDR:
5356 case OP_SUB_INT_2ADDR:
5357 case OP_MUL_INT_2ADDR:
5358 case OP_REM_INT_2ADDR:
5359 case OP_SHL_INT_2ADDR:
5360 case OP_SHR_INT_2ADDR:
5361 case OP_USHR_INT_2ADDR:
5362 checkBinop2addr(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005363 kRegTypeInteger, kRegTypeInteger, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005364 break;
5365 case OP_AND_INT_2ADDR:
5366 case OP_OR_INT_2ADDR:
5367 case OP_XOR_INT_2ADDR:
5368 checkBinop2addr(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005369 kRegTypeInteger, kRegTypeInteger, kRegTypeInteger, true, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005370 break;
5371 case OP_DIV_INT_2ADDR:
5372 checkBinop2addr(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005373 kRegTypeInteger, kRegTypeInteger, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005374 break;
5375 case OP_ADD_LONG_2ADDR:
5376 case OP_SUB_LONG_2ADDR:
5377 case OP_MUL_LONG_2ADDR:
5378 case OP_DIV_LONG_2ADDR:
5379 case OP_REM_LONG_2ADDR:
5380 case OP_AND_LONG_2ADDR:
5381 case OP_OR_LONG_2ADDR:
5382 case OP_XOR_LONG_2ADDR:
5383 checkBinop2addr(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005384 kRegTypeLongLo, kRegTypeLongLo, kRegTypeLongLo, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005385 break;
5386 case OP_SHL_LONG_2ADDR:
5387 case OP_SHR_LONG_2ADDR:
5388 case OP_USHR_LONG_2ADDR:
5389 checkBinop2addr(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005390 kRegTypeLongLo, kRegTypeLongLo, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005391 break;
5392 case OP_ADD_FLOAT_2ADDR:
5393 case OP_SUB_FLOAT_2ADDR:
5394 case OP_MUL_FLOAT_2ADDR:
5395 case OP_DIV_FLOAT_2ADDR:
5396 case OP_REM_FLOAT_2ADDR:
5397 checkBinop2addr(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005398 kRegTypeFloat, kRegTypeFloat, kRegTypeFloat, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005399 break;
5400 case OP_ADD_DOUBLE_2ADDR:
5401 case OP_SUB_DOUBLE_2ADDR:
5402 case OP_MUL_DOUBLE_2ADDR:
5403 case OP_DIV_DOUBLE_2ADDR:
5404 case OP_REM_DOUBLE_2ADDR:
5405 checkBinop2addr(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005406 kRegTypeDoubleLo, kRegTypeDoubleLo, kRegTypeDoubleLo, false,
5407 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005408 break;
5409 case OP_ADD_INT_LIT16:
5410 case OP_RSUB_INT:
5411 case OP_MUL_INT_LIT16:
5412 case OP_DIV_INT_LIT16:
5413 case OP_REM_INT_LIT16:
5414 checkLitop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005415 kRegTypeInteger, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005416 break;
5417 case OP_AND_INT_LIT16:
5418 case OP_OR_INT_LIT16:
5419 case OP_XOR_INT_LIT16:
5420 checkLitop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005421 kRegTypeInteger, kRegTypeInteger, true, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005422 break;
5423 case OP_ADD_INT_LIT8:
5424 case OP_RSUB_INT_LIT8:
5425 case OP_MUL_INT_LIT8:
5426 case OP_DIV_INT_LIT8:
5427 case OP_REM_INT_LIT8:
5428 case OP_SHL_INT_LIT8:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005429 checkLitop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005430 kRegTypeInteger, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005431 break;
Andy McFadden80d25ea2009-06-12 07:26:17 -07005432 case OP_SHR_INT_LIT8:
5433 tmpType = adjustForRightShift(workRegs, insnRegCount,
5434 decInsn.vB, decInsn.vC, false, &failure);
5435 checkLitop(workRegs, insnRegCount, &decInsn,
5436 tmpType, kRegTypeInteger, false, &failure);
5437 break;
5438 case OP_USHR_INT_LIT8:
5439 tmpType = adjustForRightShift(workRegs, insnRegCount,
5440 decInsn.vB, decInsn.vC, true, &failure);
5441 checkLitop(workRegs, insnRegCount, &decInsn,
5442 tmpType, kRegTypeInteger, false, &failure);
5443 break;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005444 case OP_AND_INT_LIT8:
5445 case OP_OR_INT_LIT8:
5446 case OP_XOR_INT_LIT8:
5447 checkLitop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005448 kRegTypeInteger, kRegTypeInteger, true, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005449 break;
5450
Andy McFaddenb51ea112009-05-08 16:50:17 -07005451 /*
5452 * This falls into the general category of "optimized" instructions,
5453 * which don't generally appear during verification. Because it's
5454 * inserted in the course of verification, we can expect to see it here.
5455 */
5456 case OP_THROW_VERIFICATION_ERROR:
5457 break;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005458
5459 /*
5460 * Verifying "quickened" instructions is tricky, because we have
5461 * discarded the original field/method information. The byte offsets
5462 * and vtable indices only have meaning in the context of an object
5463 * instance.
5464 *
5465 * If a piece of code declares a local reference variable, assigns
5466 * null to it, and then issues a virtual method call on it, we
5467 * cannot evaluate the method call during verification. This situation
5468 * isn't hard to handle, since we know the call will always result in an
5469 * NPE, and the arguments and return value don't matter. Any code that
5470 * depends on the result of the method call is inaccessible, so the
5471 * fact that we can't fully verify anything that comes after the bad
5472 * call is not a problem.
5473 *
5474 * We must also consider the case of multiple code paths, only some of
5475 * which involve a null reference. We can completely verify the method
5476 * if we sidestep the results of executing with a null reference.
5477 * For example, if on the first pass through the code we try to do a
5478 * virtual method invocation through a null ref, we have to skip the
5479 * method checks and have the method return a "wildcard" type (which
5480 * merges with anything to become that other thing). The move-result
5481 * will tell us if it's a reference, single-word numeric, or double-word
5482 * value. We continue to perform the verification, and at the end of
5483 * the function any invocations that were never fully exercised are
5484 * marked as null-only.
5485 *
5486 * We would do something similar for the field accesses. The field's
5487 * type, once known, can be used to recover the width of short integers.
5488 * If the object reference was null, the field-get returns the "wildcard"
5489 * type, which is acceptable for any operation.
5490 */
5491 case OP_EXECUTE_INLINE:
Andy McFaddenb0a05412009-11-19 10:23:41 -08005492 case OP_EXECUTE_INLINE_RANGE:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005493 case OP_INVOKE_DIRECT_EMPTY:
5494 case OP_IGET_QUICK:
5495 case OP_IGET_WIDE_QUICK:
5496 case OP_IGET_OBJECT_QUICK:
5497 case OP_IPUT_QUICK:
5498 case OP_IPUT_WIDE_QUICK:
5499 case OP_IPUT_OBJECT_QUICK:
5500 case OP_INVOKE_VIRTUAL_QUICK:
5501 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
5502 case OP_INVOKE_SUPER_QUICK:
5503 case OP_INVOKE_SUPER_QUICK_RANGE:
Andy McFadden62a75162009-04-17 17:23:37 -07005504 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005505 break;
5506
Andy McFadden96516932009-10-28 17:39:02 -07005507 /* these should never appear during verification */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005508 case OP_UNUSED_3E:
5509 case OP_UNUSED_3F:
5510 case OP_UNUSED_40:
5511 case OP_UNUSED_41:
5512 case OP_UNUSED_42:
5513 case OP_UNUSED_43:
5514 case OP_UNUSED_73:
5515 case OP_UNUSED_79:
5516 case OP_UNUSED_7A:
5517 case OP_UNUSED_E3:
5518 case OP_UNUSED_E4:
5519 case OP_UNUSED_E5:
5520 case OP_UNUSED_E6:
5521 case OP_UNUSED_E7:
Andy McFadden96516932009-10-28 17:39:02 -07005522 case OP_BREAKPOINT:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005523 case OP_UNUSED_F1:
5524 case OP_UNUSED_FC:
5525 case OP_UNUSED_FD:
5526 case OP_UNUSED_FE:
5527 case OP_UNUSED_FF:
Andy McFadden62a75162009-04-17 17:23:37 -07005528 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005529 break;
5530
5531 /*
5532 * DO NOT add a "default" clause here. Without it the compiler will
5533 * complain if an instruction is missing (which is desirable).
5534 */
5535 }
5536
Andy McFadden62a75162009-04-17 17:23:37 -07005537 if (!VERIFY_OK(failure)) {
Andy McFaddenb51ea112009-05-08 16:50:17 -07005538 if (failure == VERIFY_ERROR_GENERIC || gDvm.optimizing) {
5539 /* immediate failure, reject class */
5540 LOG_VFY_METH(meth, "VFY: rejecting opcode 0x%02x at 0x%04x\n",
5541 decInsn.opCode, insnIdx);
5542 goto bail;
5543 } else {
5544 /* replace opcode and continue on */
5545 LOGD("VFY: replacing opcode 0x%02x at 0x%04x\n",
5546 decInsn.opCode, insnIdx);
5547 if (!replaceFailingInstruction(meth, insnFlags, insnIdx, failure)) {
5548 LOG_VFY_METH(meth, "VFY: rejecting opcode 0x%02x at 0x%04x\n",
5549 decInsn.opCode, insnIdx);
5550 goto bail;
5551 }
5552 /* IMPORTANT: meth->insns may have been changed */
5553 insns = meth->insns + insnIdx;
5554
5555 /* continue on as if we just handled a throw-verification-error */
5556 failure = VERIFY_ERROR_NONE;
5557 nextFlags = kInstrCanThrow;
5558 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005559 }
5560
5561 /*
5562 * If we didn't just set the result register, clear it out. This
5563 * ensures that you can only use "move-result" immediately after the
5564 * result is set.
5565 */
5566 if (!justSetResult) {
5567 int reg = RESULT_REGISTER(insnRegCount);
5568 workRegs[reg] = workRegs[reg+1] = kRegTypeUnknown;
5569 }
5570
5571 /*
5572 * Handle "continue". Tag the next consecutive instruction.
5573 */
5574 if ((nextFlags & kInstrCanContinue) != 0) {
5575 int insnWidth = dvmInsnGetWidth(insnFlags, insnIdx);
5576 if (insnIdx+insnWidth >= insnsSize) {
5577 LOG_VFY_METH(meth,
5578 "VFY: execution can walk off end of code area (from 0x%x)\n",
5579 insnIdx);
5580 goto bail;
5581 }
5582
5583 /*
5584 * The only way to get to a move-exception instruction is to get
5585 * thrown there. Make sure the next instruction isn't one.
5586 */
5587 if (!checkMoveException(meth, insnIdx+insnWidth, "next"))
5588 goto bail;
5589
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005590 if (getRegisterLine(regTable, insnIdx+insnWidth) != NULL) {
Andy McFadden06b7a282009-05-11 10:44:52 -07005591 /*
5592 * Merge registers into what we have for the next instruction,
5593 * and set the "changed" flag if needed.
5594 */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005595 updateRegisters(meth, insnFlags, regTable, insnIdx+insnWidth,
5596 workRegs);
5597 } else {
The Android Open Source Project99409882009-03-18 22:20:24 -07005598 /*
Andy McFadden06b7a282009-05-11 10:44:52 -07005599 * We're not recording register data for the next instruction,
5600 * so we don't know what the prior state was. We have to
5601 * assume that something has changed and re-evaluate it.
The Android Open Source Project99409882009-03-18 22:20:24 -07005602 */
5603 dvmInsnSetChanged(insnFlags, insnIdx+insnWidth, true);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005604 }
5605 }
5606
5607 /*
5608 * Handle "branch". Tag the branch target.
5609 *
5610 * NOTE: instructions like OP_EQZ provide information about the state
5611 * of the register when the branch is taken or not taken. For example,
5612 * somebody could get a reference field, check it for zero, and if the
5613 * branch is taken immediately store that register in a boolean field
5614 * since the value is known to be zero. We do not currently account for
5615 * that, and will reject the code.
5616 */
5617 if ((nextFlags & kInstrCanBranch) != 0) {
5618 bool isConditional;
5619
5620 if (!dvmGetBranchTarget(meth, insnFlags, insnIdx, &branchTarget,
5621 &isConditional))
5622 {
5623 /* should never happen after static verification */
5624 LOG_VFY_METH(meth, "VFY: bad branch at %d\n", insnIdx);
5625 goto bail;
5626 }
5627 assert(isConditional || (nextFlags & kInstrCanContinue) == 0);
5628 assert(!isConditional || (nextFlags & kInstrCanContinue) != 0);
5629
5630 if (!checkMoveException(meth, insnIdx+branchTarget, "branch"))
5631 goto bail;
5632
The Android Open Source Project99409882009-03-18 22:20:24 -07005633 /* update branch target, set "changed" if appropriate */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005634 updateRegisters(meth, insnFlags, regTable, insnIdx+branchTarget,
5635 workRegs);
5636 }
5637
5638 /*
5639 * Handle "switch". Tag all possible branch targets.
5640 *
5641 * We've already verified that the table is structurally sound, so we
5642 * just need to walk through and tag the targets.
5643 */
5644 if ((nextFlags & kInstrCanSwitch) != 0) {
5645 int offsetToSwitch = insns[1] | (((s4)insns[2]) << 16);
5646 const u2* switchInsns = insns + offsetToSwitch;
5647 int switchCount = switchInsns[1];
5648 int offsetToTargets, targ;
5649
5650 if ((*insns & 0xff) == OP_PACKED_SWITCH) {
5651 /* 0=sig, 1=count, 2/3=firstKey */
5652 offsetToTargets = 4;
5653 } else {
5654 /* 0=sig, 1=count, 2..count*2 = keys */
5655 assert((*insns & 0xff) == OP_SPARSE_SWITCH);
5656 offsetToTargets = 2 + 2*switchCount;
5657 }
5658
5659 /* verify each switch target */
5660 for (targ = 0; targ < switchCount; targ++) {
5661 int offset, absOffset;
5662
5663 /* offsets are 32-bit, and only partly endian-swapped */
5664 offset = switchInsns[offsetToTargets + targ*2] |
5665 (((s4) switchInsns[offsetToTargets + targ*2 +1]) << 16);
5666 absOffset = insnIdx + offset;
5667
5668 assert(absOffset >= 0 && absOffset < insnsSize);
5669
5670 if (!checkMoveException(meth, absOffset, "switch"))
5671 goto bail;
5672
5673 updateRegisters(meth, insnFlags, regTable, absOffset, workRegs);
5674 }
5675 }
5676
5677 /*
5678 * Handle instructions that can throw and that are sitting in a
5679 * "try" block. (If they're not in a "try" block when they throw,
5680 * control transfers out of the method.)
5681 */
5682 if ((nextFlags & kInstrCanThrow) != 0 && dvmInsnIsInTry(insnFlags, insnIdx))
5683 {
5684 DexFile* pDexFile = meth->clazz->pDvmDex->pDexFile;
5685 const DexCode* pCode = dvmGetMethodCode(meth);
5686 DexCatchIterator iterator;
5687
5688 if (dexFindCatchHandler(&iterator, pCode, insnIdx)) {
5689 for (;;) {
5690 DexCatchHandler* handler = dexCatchIteratorNext(&iterator);
5691
5692 if (handler == NULL) {
5693 break;
5694 }
5695
5696 /* note we use entryRegs, not workRegs */
5697 updateRegisters(meth, insnFlags, regTable, handler->address,
5698 entryRegs);
5699 }
5700 }
5701 }
5702
5703 /*
5704 * Update startGuess. Advance to the next instruction of that's
5705 * possible, otherwise use the branch target if one was found. If
5706 * neither of those exists we're in a return or throw; leave startGuess
5707 * alone and let the caller sort it out.
5708 */
5709 if ((nextFlags & kInstrCanContinue) != 0) {
5710 *pStartGuess = insnIdx + dvmInsnGetWidth(insnFlags, insnIdx);
5711 } else if ((nextFlags & kInstrCanBranch) != 0) {
5712 /* we're still okay if branchTarget is zero */
5713 *pStartGuess = insnIdx + branchTarget;
5714 }
5715
5716 assert(*pStartGuess >= 0 && *pStartGuess < insnsSize &&
5717 dvmInsnGetWidth(insnFlags, *pStartGuess) != 0);
5718
5719 result = true;
5720
5721bail:
5722 return result;
5723}
5724
Andy McFaddenb51ea112009-05-08 16:50:17 -07005725
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005726/*
5727 * callback function used in dumpRegTypes to print local vars
5728 * valid at a given address.
5729 */
5730static void logLocalsCb(void *cnxt, u2 reg, u4 startAddress, u4 endAddress,
5731 const char *name, const char *descriptor,
5732 const char *signature)
5733{
5734 int addr = *((int *)cnxt);
5735
5736 if (addr >= (int) startAddress && addr < (int) endAddress)
5737 {
5738 LOGI(" %2d: '%s' %s\n", reg, name, descriptor);
5739 }
5740}
5741
5742/*
5743 * Dump the register types for the specifed address to the log file.
5744 */
5745static void dumpRegTypes(const Method* meth, const InsnFlags* insnFlags,
5746 const RegType* addrRegs, int addr, const char* addrName,
5747 const UninitInstanceMap* uninitMap, int displayFlags)
5748{
5749 int regCount = meth->registersSize;
5750 int fullRegCount = regCount + kExtraRegs;
5751 bool branchTarget = dvmInsnIsBranchTarget(insnFlags, addr);
5752 int i;
5753
5754 assert(addr >= 0 && addr < (int) dvmGetMethodInsnsSize(meth));
5755
5756 int regCharSize = fullRegCount + (fullRegCount-1)/4 + 2 +1;
5757 char regChars[regCharSize +1];
5758 memset(regChars, ' ', regCharSize);
5759 regChars[0] = '[';
5760 if (regCount == 0)
5761 regChars[1] = ']';
5762 else
5763 regChars[1 + (regCount-1) + (regCount-1)/4 +1] = ']';
5764 regChars[regCharSize] = '\0';
5765
5766 //const RegType* addrRegs = getRegisterLine(regTable, addr);
5767
5768 for (i = 0; i < regCount + kExtraRegs; i++) {
5769 char tch;
5770
5771 switch (addrRegs[i]) {
5772 case kRegTypeUnknown: tch = '.'; break;
5773 case kRegTypeConflict: tch = 'X'; break;
5774 case kRegTypeFloat: tch = 'F'; break;
5775 case kRegTypeZero: tch = '0'; break;
5776 case kRegTypeOne: tch = '1'; break;
5777 case kRegTypeBoolean: tch = 'Z'; break;
5778 case kRegTypePosByte: tch = 'b'; break;
5779 case kRegTypeByte: tch = 'B'; break;
5780 case kRegTypePosShort: tch = 's'; break;
5781 case kRegTypeShort: tch = 'S'; break;
5782 case kRegTypeChar: tch = 'C'; break;
5783 case kRegTypeInteger: tch = 'I'; break;
5784 case kRegTypeLongLo: tch = 'J'; break;
5785 case kRegTypeLongHi: tch = 'j'; break;
5786 case kRegTypeDoubleLo: tch = 'D'; break;
5787 case kRegTypeDoubleHi: tch = 'd'; break;
5788 default:
5789 if (regTypeIsReference(addrRegs[i])) {
5790 if (regTypeIsUninitReference(addrRegs[i]))
5791 tch = 'U';
5792 else
5793 tch = 'L';
5794 } else {
5795 tch = '*';
5796 assert(false);
5797 }
5798 break;
5799 }
5800
5801 if (i < regCount)
5802 regChars[1 + i + (i/4)] = tch;
5803 else
5804 regChars[1 + i + (i/4) + 2] = tch;
5805 }
5806
5807 if (addr == 0 && addrName != NULL)
5808 LOGI("%c%s %s\n", branchTarget ? '>' : ' ', addrName, regChars);
5809 else
5810 LOGI("%c0x%04x %s\n", branchTarget ? '>' : ' ', addr, regChars);
5811
5812 if (displayFlags & DRT_SHOW_REF_TYPES) {
5813 for (i = 0; i < regCount + kExtraRegs; i++) {
5814 if (regTypeIsReference(addrRegs[i]) && addrRegs[i] != kRegTypeZero)
5815 {
5816 ClassObject* clazz;
5817
5818 clazz = regTypeReferenceToClass(addrRegs[i], uninitMap);
5819 assert(dvmValidateObject((Object*)clazz));
5820 if (i < regCount) {
5821 LOGI(" %2d: 0x%08x %s%s\n",
5822 i, addrRegs[i],
5823 regTypeIsUninitReference(addrRegs[i]) ? "[U]" : "",
5824 clazz->descriptor);
5825 } else {
5826 LOGI(" RS: 0x%08x %s%s\n",
5827 addrRegs[i],
5828 regTypeIsUninitReference(addrRegs[i]) ? "[U]" : "",
5829 clazz->descriptor);
5830 }
5831 }
5832 }
5833 }
5834 if (displayFlags & DRT_SHOW_LOCALS) {
5835 dexDecodeDebugInfo(meth->clazz->pDvmDex->pDexFile,
5836 dvmGetMethodCode(meth),
5837 meth->clazz->descriptor,
5838 meth->prototype.protoIdx,
5839 meth->accessFlags,
5840 NULL, logLocalsCb, &addr);
5841 }
5842}
5843