blob: b37b9a9d33bfad8d4ab26a7b510ca41c278c1772 [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 *
1944 * Returns true if both args are Boolean, Zero, or One.
1945 */
1946static bool upcastBooleanOp(RegType* insnRegs, const int insnRegCount,
1947 u4 reg1, u4 reg2)
1948{
1949 RegType type1, type2;
1950
1951 type1 = insnRegs[reg1];
1952 type2 = insnRegs[reg2];
1953
1954 if ((type1 == kRegTypeBoolean || type1 == kRegTypeZero ||
1955 type1 == kRegTypeOne) &&
1956 (type2 == kRegTypeBoolean || type2 == kRegTypeZero ||
1957 type2 == kRegTypeOne))
1958 {
1959 return true;
1960 }
1961 return false;
1962}
1963
1964/*
1965 * Verify types for A two-register instruction with a literal constant
1966 * (e.g. "add-int/lit8"). "dstType" is stored into vA, and "srcType" is
1967 * verified against vB.
1968 *
1969 * If "checkBooleanOp" is set, we use the constant value in vC.
1970 */
1971static void checkLitop(RegType* insnRegs, const int insnRegCount,
1972 DecodedInstruction* pDecInsn, RegType dstType, RegType srcType,
Andy McFadden62a75162009-04-17 17:23:37 -07001973 bool checkBooleanOp, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001974{
Andy McFadden62a75162009-04-17 17:23:37 -07001975 verifyRegisterType(insnRegs, insnRegCount, pDecInsn->vB, srcType, pFailure);
1976 if (VERIFY_OK(*pFailure) && checkBooleanOp) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001977 assert(dstType == kRegTypeInteger);
1978 /* check vB with the call, then check the constant manually */
1979 if (upcastBooleanOp(insnRegs, insnRegCount, pDecInsn->vB, pDecInsn->vB)
1980 && (pDecInsn->vC == 0 || pDecInsn->vC == 1))
1981 {
1982 dstType = kRegTypeBoolean;
1983 }
1984 }
Andy McFadden62a75162009-04-17 17:23:37 -07001985 setRegisterType(insnRegs, insnRegCount, pDecInsn->vA, dstType, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001986}
1987
1988/*
1989 * Verify types for a simple three-register instruction (e.g. "add-int").
1990 * "dstType" is stored into vA, and "srcType1"/"srcType2" are verified
1991 * against vB/vC.
1992 */
1993static void checkBinop(RegType* insnRegs, const int insnRegCount,
1994 DecodedInstruction* pDecInsn, RegType dstType, RegType srcType1,
Andy McFadden62a75162009-04-17 17:23:37 -07001995 RegType srcType2, bool checkBooleanOp, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001996{
Andy McFadden62a75162009-04-17 17:23:37 -07001997 verifyRegisterType(insnRegs, insnRegCount, pDecInsn->vB, srcType1,
1998 pFailure);
1999 verifyRegisterType(insnRegs, insnRegCount, pDecInsn->vC, srcType2,
2000 pFailure);
2001 if (VERIFY_OK(*pFailure) && checkBooleanOp) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002002 assert(dstType == kRegTypeInteger);
2003 if (upcastBooleanOp(insnRegs, insnRegCount, pDecInsn->vB, pDecInsn->vC))
2004 dstType = kRegTypeBoolean;
2005 }
Andy McFadden62a75162009-04-17 17:23:37 -07002006 setRegisterType(insnRegs, insnRegCount, pDecInsn->vA, dstType, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002007}
2008
2009/*
2010 * Verify types for a binary "2addr" operation. "srcType1"/"srcType2"
2011 * are verified against vA/vB, then "dstType" is stored into vA.
2012 */
2013static void checkBinop2addr(RegType* insnRegs, const int insnRegCount,
2014 DecodedInstruction* pDecInsn, RegType dstType, RegType srcType1,
Andy McFadden62a75162009-04-17 17:23:37 -07002015 RegType srcType2, bool checkBooleanOp, VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002016{
Andy McFadden62a75162009-04-17 17:23:37 -07002017 verifyRegisterType(insnRegs, insnRegCount, pDecInsn->vA, srcType1,
2018 pFailure);
2019 verifyRegisterType(insnRegs, insnRegCount, pDecInsn->vB, srcType2,
2020 pFailure);
2021 if (VERIFY_OK(*pFailure) && checkBooleanOp) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002022 assert(dstType == kRegTypeInteger);
2023 if (upcastBooleanOp(insnRegs, insnRegCount, pDecInsn->vA, pDecInsn->vB))
2024 dstType = kRegTypeBoolean;
2025 }
Andy McFadden62a75162009-04-17 17:23:37 -07002026 setRegisterType(insnRegs, insnRegCount, pDecInsn->vA, dstType, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002027}
2028
2029
2030/*
2031 * ===========================================================================
2032 * Register merge
2033 * ===========================================================================
2034 */
2035
2036/*
2037 * Compute the "class depth" of a class. This is the distance from the
2038 * class to the top of the tree, chasing superclass links. java.lang.Object
2039 * has a class depth of 0.
2040 */
2041static int getClassDepth(ClassObject* clazz)
2042{
2043 int depth = 0;
2044
2045 while (clazz->super != NULL) {
2046 clazz = clazz->super;
2047 depth++;
2048 }
2049 return depth;
2050}
2051
2052/*
2053 * Given two classes, walk up the superclass tree to find a common
2054 * ancestor. (Called from findCommonSuperclass().)
2055 *
2056 * TODO: consider caching the class depth in the class object so we don't
2057 * have to search for it here.
2058 */
2059static ClassObject* digForSuperclass(ClassObject* c1, ClassObject* c2)
2060{
2061 int depth1, depth2;
2062
2063 depth1 = getClassDepth(c1);
2064 depth2 = getClassDepth(c2);
2065
2066 if (gDebugVerbose) {
2067 LOGVV("COMMON: %s(%d) + %s(%d)\n",
2068 c1->descriptor, depth1, c2->descriptor, depth2);
2069 }
2070
2071 /* pull the deepest one up */
2072 if (depth1 > depth2) {
2073 while (depth1 > depth2) {
2074 c1 = c1->super;
2075 depth1--;
2076 }
2077 } else {
2078 while (depth2 > depth1) {
2079 c2 = c2->super;
2080 depth2--;
2081 }
2082 }
2083
2084 /* walk up in lock-step */
2085 while (c1 != c2) {
2086 c1 = c1->super;
2087 c2 = c2->super;
2088
2089 assert(c1 != NULL && c2 != NULL);
2090 }
2091
2092 if (gDebugVerbose) {
2093 LOGVV(" : --> %s\n", c1->descriptor);
2094 }
2095 return c1;
2096}
2097
2098/*
2099 * Merge two array classes. We can't use the general "walk up to the
2100 * superclass" merge because the superclass of an array is always Object.
2101 * We want String[] + Integer[] = Object[]. This works for higher dimensions
2102 * as well, e.g. String[][] + Integer[][] = Object[][].
2103 *
2104 * If Foo1 and Foo2 are subclasses of Foo, Foo1[] + Foo2[] = Foo[].
2105 *
2106 * If Class implements Type, Class[] + Type[] = Type[].
2107 *
2108 * If the dimensions don't match, we want to convert to an array of Object
2109 * with the least dimension, e.g. String[][] + String[][][][] = Object[][].
2110 *
2111 * This gets a little awkward because we may have to ask the VM to create
2112 * a new array type with the appropriate element and dimensions. However, we
2113 * shouldn't be doing this often.
2114 */
2115static ClassObject* findCommonArraySuperclass(ClassObject* c1, ClassObject* c2)
2116{
2117 ClassObject* arrayClass = NULL;
2118 ClassObject* commonElem;
2119 int i, numDims;
2120
2121 assert(c1->arrayDim > 0);
2122 assert(c2->arrayDim > 0);
2123
2124 if (c1->arrayDim == c2->arrayDim) {
2125 //commonElem = digForSuperclass(c1->elementClass, c2->elementClass);
2126 commonElem = findCommonSuperclass(c1->elementClass, c2->elementClass);
2127 numDims = c1->arrayDim;
2128 } else {
2129 if (c1->arrayDim < c2->arrayDim)
2130 numDims = c1->arrayDim;
2131 else
2132 numDims = c2->arrayDim;
2133 commonElem = c1->super; // == java.lang.Object
2134 }
2135
2136 /* walk from the element to the (multi-)dimensioned array type */
2137 for (i = 0; i < numDims; i++) {
2138 arrayClass = dvmFindArrayClassForElement(commonElem);
2139 commonElem = arrayClass;
2140 }
2141
2142 LOGVV("ArrayMerge '%s' + '%s' --> '%s'\n",
2143 c1->descriptor, c2->descriptor, arrayClass->descriptor);
2144 return arrayClass;
2145}
2146
2147/*
2148 * Find the first common superclass of the two classes. We're not
2149 * interested in common interfaces.
2150 *
2151 * The easiest way to do this for concrete classes is to compute the "class
2152 * depth" of each, move up toward the root of the deepest one until they're
2153 * at the same depth, then walk both up to the root until they match.
2154 *
2155 * If both classes are arrays of non-primitive types, we need to merge
2156 * based on array depth and element type.
2157 *
2158 * If one class is an interface, we check to see if the other class/interface
2159 * (or one of its predecessors) implements the interface. If so, we return
2160 * the interface; otherwise, we return Object.
2161 *
2162 * NOTE: we continue the tradition of "lazy interface handling". To wit,
2163 * suppose we have three classes:
2164 * One implements Fancy, Free
2165 * Two implements Fancy, Free
2166 * Three implements Free
2167 * where Fancy and Free are unrelated interfaces. The code requires us
2168 * to merge One into Two. Ideally we'd use a common interface, which
2169 * gives us a choice between Fancy and Free, and no guidance on which to
2170 * use. If we use Free, we'll be okay when Three gets merged in, but if
2171 * we choose Fancy, we're hosed. The "ideal" solution is to create a
2172 * set of common interfaces and carry that around, merging further references
2173 * into it. This is a pain. The easy solution is to simply boil them
2174 * down to Objects and let the runtime invokeinterface call fail, which
2175 * is what we do.
2176 */
2177static ClassObject* findCommonSuperclass(ClassObject* c1, ClassObject* c2)
2178{
2179 assert(!dvmIsPrimitiveClass(c1) && !dvmIsPrimitiveClass(c2));
2180
2181 if (c1 == c2)
2182 return c1;
2183
2184 if (dvmIsInterfaceClass(c1) && dvmImplements(c2, c1)) {
2185 if (gDebugVerbose)
2186 LOGVV("COMMON/I1: %s + %s --> %s\n",
2187 c1->descriptor, c2->descriptor, c1->descriptor);
2188 return c1;
2189 }
2190 if (dvmIsInterfaceClass(c2) && dvmImplements(c1, c2)) {
2191 if (gDebugVerbose)
2192 LOGVV("COMMON/I2: %s + %s --> %s\n",
2193 c1->descriptor, c2->descriptor, c2->descriptor);
2194 return c2;
2195 }
2196
2197 if (dvmIsArrayClass(c1) && dvmIsArrayClass(c2) &&
2198 !dvmIsPrimitiveClass(c1->elementClass) &&
2199 !dvmIsPrimitiveClass(c2->elementClass))
2200 {
2201 return findCommonArraySuperclass(c1, c2);
2202 }
2203
2204 return digForSuperclass(c1, c2);
2205}
2206
2207/*
2208 * Merge two RegType values.
2209 *
2210 * Sets "*pChanged" to "true" if the result doesn't match "type1".
2211 */
2212static RegType mergeTypes(RegType type1, RegType type2, bool* pChanged)
2213{
2214 RegType result;
2215
2216 /*
2217 * Check for trivial case so we don't have to hit memory.
2218 */
2219 if (type1 == type2)
2220 return type1;
2221
2222 /*
2223 * Use the table if we can, and reject any attempts to merge something
2224 * from the table with a reference type.
2225 *
2226 * The uninitialized table entry at index zero *will* show up as a
2227 * simple kRegTypeUninit value. Since this cannot be merged with
2228 * anything but itself, the rules do the right thing.
2229 */
2230 if (type1 < kRegTypeMAX) {
2231 if (type2 < kRegTypeMAX) {
2232 result = gDvmMergeTab[type1][type2];
2233 } else {
2234 /* simple + reference == conflict, usually */
2235 if (type1 == kRegTypeZero)
2236 result = type2;
2237 else
2238 result = kRegTypeConflict;
2239 }
2240 } else {
2241 if (type2 < kRegTypeMAX) {
2242 /* reference + simple == conflict, usually */
2243 if (type2 == kRegTypeZero)
2244 result = type1;
2245 else
2246 result = kRegTypeConflict;
2247 } else {
2248 /* merging two references */
2249 if (regTypeIsUninitReference(type1) ||
2250 regTypeIsUninitReference(type2))
2251 {
2252 /* can't merge uninit with anything but self */
2253 result = kRegTypeConflict;
2254 } else {
2255 ClassObject* clazz1 = regTypeInitializedReferenceToClass(type1);
2256 ClassObject* clazz2 = regTypeInitializedReferenceToClass(type2);
2257 ClassObject* mergedClass;
2258
2259 mergedClass = findCommonSuperclass(clazz1, clazz2);
2260 assert(mergedClass != NULL);
2261 result = regTypeFromClass(mergedClass);
2262 }
2263 }
2264 }
2265
2266 if (result != type1)
2267 *pChanged = true;
2268 return result;
2269}
2270
2271/*
2272 * Control can transfer to "nextInsn".
2273 *
2274 * Merge the registers from "workRegs" into "regTypes" at "nextInsn", and
2275 * set the "changed" flag on the target address if the registers have changed.
2276 */
2277static void updateRegisters(const Method* meth, InsnFlags* insnFlags,
2278 RegisterTable* regTable, int nextInsn, const RegType* workRegs)
2279{
2280 RegType* targetRegs = getRegisterLine(regTable, nextInsn);
2281 const int insnRegCount = meth->registersSize;
2282
2283#if 0
2284 if (!dvmInsnIsBranchTarget(insnFlags, nextInsn)) {
2285 LOGE("insnFlags[0x%x]=0x%08x\n", nextInsn, insnFlags[nextInsn]);
2286 LOGE(" In %s.%s %s\n",
2287 meth->clazz->descriptor, meth->name, meth->descriptor);
2288 assert(false);
2289 }
2290#endif
2291
2292 if (!dvmInsnIsVisitedOrChanged(insnFlags, nextInsn)) {
2293 /*
2294 * We haven't processed this instruction before, and we haven't
2295 * touched the registers here, so there's nothing to "merge". Copy
2296 * the registers over and mark it as changed. (This is the only
2297 * way a register can transition out of "unknown", so this is not
2298 * just an optimization.)
2299 */
2300 LOGVV("COPY into 0x%04x\n", nextInsn);
2301 copyRegisters(targetRegs, workRegs, insnRegCount + kExtraRegs);
2302 dvmInsnSetChanged(insnFlags, nextInsn, true);
2303 } else {
2304 if (gDebugVerbose) {
2305 LOGVV("MERGE into 0x%04x\n", nextInsn);
2306 //dumpRegTypes(meth, insnFlags, targetRegs, 0, "targ", NULL, 0);
2307 //dumpRegTypes(meth, insnFlags, workRegs, 0, "work", NULL, 0);
2308 }
2309 /* merge registers, set Changed only if different */
2310 bool changed = false;
2311 int i;
2312
2313 for (i = 0; i < insnRegCount + kExtraRegs; i++) {
2314 targetRegs[i] = mergeTypes(targetRegs[i], workRegs[i], &changed);
2315 }
2316
2317 if (gDebugVerbose) {
2318 //LOGI(" RESULT (changed=%d)\n", changed);
2319 //dumpRegTypes(meth, insnFlags, targetRegs, 0, "rslt", NULL, 0);
2320 }
2321
2322 if (changed)
2323 dvmInsnSetChanged(insnFlags, nextInsn, true);
2324 }
2325}
2326
2327
2328/*
2329 * ===========================================================================
2330 * Utility functions
2331 * ===========================================================================
2332 */
2333
2334/*
2335 * Look up an instance field, specified by "fieldIdx", that is going to be
2336 * accessed in object "objType". This resolves the field and then verifies
2337 * that the class containing the field is an instance of the reference in
2338 * "objType".
2339 *
2340 * It is possible for "objType" to be kRegTypeZero, meaning that we might
2341 * have a null reference. This is a runtime problem, so we allow it,
2342 * skipping some of the type checks.
2343 *
2344 * In general, "objType" must be an initialized reference. However, we
2345 * allow it to be uninitialized if this is an "<init>" method and the field
2346 * is declared within the "objType" class.
2347 *
Andy McFadden62a75162009-04-17 17:23:37 -07002348 * Returns an InstField on success, returns NULL and sets "*pFailure"
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002349 * on failure.
2350 */
2351static InstField* getInstField(const Method* meth,
2352 const UninitInstanceMap* uninitMap, RegType objType, int fieldIdx,
Andy McFadden62a75162009-04-17 17:23:37 -07002353 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002354{
2355 InstField* instField = NULL;
2356 ClassObject* objClass;
2357 bool mustBeLocal = false;
2358
2359 if (!regTypeIsReference(objType)) {
Andy McFadden62a75162009-04-17 17:23:37 -07002360 LOG_VFY("VFY: attempt to access field in non-reference type %d\n",
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002361 objType);
Andy McFadden62a75162009-04-17 17:23:37 -07002362 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002363 goto bail;
2364 }
2365
Andy McFadden62a75162009-04-17 17:23:37 -07002366 instField = dvmOptResolveInstField(meth->clazz, fieldIdx, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002367 if (instField == NULL) {
2368 LOG_VFY("VFY: unable to resolve instance field %u\n", fieldIdx);
Andy McFadden62a75162009-04-17 17:23:37 -07002369 assert(!VERIFY_OK(*pFailure));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002370 goto bail;
2371 }
2372
2373 if (objType == kRegTypeZero)
2374 goto bail;
2375
2376 /*
2377 * Access to fields in uninitialized objects is allowed if this is
2378 * the <init> method for the object and the field in question is
2379 * declared by this class.
2380 */
2381 objClass = regTypeReferenceToClass(objType, uninitMap);
2382 assert(objClass != NULL);
2383 if (regTypeIsUninitReference(objType)) {
2384 if (!isInitMethod(meth) || meth->clazz != objClass) {
2385 LOG_VFY("VFY: attempt to access field via uninitialized ref\n");
Andy McFadden62a75162009-04-17 17:23:37 -07002386 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002387 goto bail;
2388 }
2389 mustBeLocal = true;
2390 }
2391
2392 if (!dvmInstanceof(objClass, instField->field.clazz)) {
2393 LOG_VFY("VFY: invalid field access (field %s.%s, through %s ref)\n",
2394 instField->field.clazz->descriptor, instField->field.name,
2395 objClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07002396 *pFailure = VERIFY_ERROR_NO_FIELD;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002397 goto bail;
2398 }
2399
2400 if (mustBeLocal) {
2401 /* for uninit ref, make sure it's defined by this class, not super */
2402 if (instField < objClass->ifields ||
2403 instField >= objClass->ifields + objClass->ifieldCount)
2404 {
2405 LOG_VFY("VFY: invalid constructor field access (field %s in %s)\n",
2406 instField->field.name, objClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07002407 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002408 goto bail;
2409 }
2410 }
2411
2412bail:
2413 return instField;
2414}
2415
2416/*
2417 * Look up a static field.
2418 *
Andy McFadden62a75162009-04-17 17:23:37 -07002419 * Returns a StaticField on success, returns NULL and sets "*pFailure"
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002420 * on failure.
2421 */
2422static StaticField* getStaticField(const Method* meth, int fieldIdx,
Andy McFadden62a75162009-04-17 17:23:37 -07002423 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002424{
2425 StaticField* staticField;
2426
Andy McFadden62a75162009-04-17 17:23:37 -07002427 staticField = dvmOptResolveStaticField(meth->clazz, fieldIdx, pFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002428 if (staticField == NULL) {
2429 DexFile* pDexFile = meth->clazz->pDvmDex->pDexFile;
2430 const DexFieldId* pFieldId;
2431
2432 pFieldId = dexGetFieldId(pDexFile, fieldIdx);
2433
2434 LOG_VFY("VFY: unable to resolve static field %u (%s) in %s\n", fieldIdx,
2435 dexStringById(pDexFile, pFieldId->nameIdx),
2436 dexStringByTypeIdx(pDexFile, pFieldId->classIdx));
Andy McFadden62a75162009-04-17 17:23:37 -07002437 assert(!VERIFY_OK(*pFailure));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002438 goto bail;
2439 }
2440
2441bail:
2442 return staticField;
2443}
2444
2445/*
2446 * If "field" is marked "final", make sure this is the either <clinit>
2447 * or <init> as appropriate.
2448 *
Andy McFadden62a75162009-04-17 17:23:37 -07002449 * Sets "*pFailure" on failure.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002450 */
2451static void checkFinalFieldAccess(const Method* meth, const Field* field,
Andy McFadden62a75162009-04-17 17:23:37 -07002452 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002453{
2454 if (!dvmIsFinalField(field))
2455 return;
2456
2457 /* make sure we're in the same class */
2458 if (meth->clazz != field->clazz) {
2459 LOG_VFY_METH(meth, "VFY: can't modify final field %s.%s\n",
2460 field->clazz->descriptor, field->name);
Andy McFaddenb51ea112009-05-08 16:50:17 -07002461 *pFailure = VERIFY_ERROR_ACCESS_FIELD;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002462 return;
2463 }
2464
2465 /*
Andy McFadden62a75162009-04-17 17:23:37 -07002466 * The VM spec descriptions of putfield and putstatic say that
2467 * IllegalAccessError is only thrown when the instructions appear
2468 * outside the declaring class. Our earlier attempts to restrict
2469 * final field modification to constructors are, therefore, wrong.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002470 */
2471#if 0
2472 /* make sure we're in the right kind of constructor */
2473 if (dvmIsStaticField(field)) {
2474 if (!isClassInitMethod(meth)) {
2475 LOG_VFY_METH(meth,
2476 "VFY: can't modify final static field outside <clinit>\n");
Andy McFadden62a75162009-04-17 17:23:37 -07002477 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002478 }
2479 } else {
2480 if (!isInitMethod(meth)) {
2481 LOG_VFY_METH(meth,
2482 "VFY: can't modify final field outside <init>\n");
Andy McFadden62a75162009-04-17 17:23:37 -07002483 *pFailure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002484 }
2485 }
2486#endif
2487}
2488
2489/*
2490 * Make sure that the register type is suitable for use as an array index.
2491 *
Andy McFadden62a75162009-04-17 17:23:37 -07002492 * Sets "*pFailure" if not.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002493 */
2494static void checkArrayIndexType(const Method* meth, RegType regType,
Andy McFadden62a75162009-04-17 17:23:37 -07002495 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002496{
Andy McFadden62a75162009-04-17 17:23:37 -07002497 if (VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002498 /*
2499 * The 1nr types are interchangeable at this level. We could
2500 * do something special if we can definitively identify it as a
2501 * float, but there's no real value in doing so.
2502 */
Andy McFadden62a75162009-04-17 17:23:37 -07002503 checkTypeCategory(regType, kTypeCategory1nr, pFailure);
2504 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002505 LOG_VFY_METH(meth, "Invalid reg type for array index (%d)\n",
2506 regType);
2507 }
2508 }
2509}
2510
2511/*
2512 * Check constraints on constructor return. Specifically, make sure that
2513 * the "this" argument got initialized.
2514 *
2515 * The "this" argument to <init> uses code offset kUninitThisArgAddr, which
2516 * puts it at the start of the list in slot 0. If we see a register with
2517 * an uninitialized slot 0 reference, we know it somehow didn't get
2518 * initialized.
2519 *
2520 * Returns "true" if all is well.
2521 */
2522static bool checkConstructorReturn(const Method* meth, const RegType* insnRegs,
2523 const int insnRegCount)
2524{
2525 int i;
2526
2527 if (!isInitMethod(meth))
2528 return true;
2529
2530 RegType uninitThis = regTypeFromUninitIndex(kUninitThisArgSlot);
2531
2532 for (i = 0; i < insnRegCount; i++) {
2533 if (insnRegs[i] == uninitThis) {
2534 LOG_VFY("VFY: <init> returning without calling superclass init\n");
2535 return false;
2536 }
2537 }
2538 return true;
2539}
2540
2541/*
2542 * Verify that the target instruction is not "move-exception". It's important
2543 * that the only way to execute a move-exception is as the first instruction
2544 * of an exception handler.
2545 *
2546 * Returns "true" if all is well, "false" if the target instruction is
2547 * move-exception.
2548 */
2549static bool checkMoveException(const Method* meth, int insnIdx,
2550 const char* logNote)
2551{
2552 assert(insnIdx >= 0 && insnIdx < (int)dvmGetMethodInsnsSize(meth));
2553
2554 if ((meth->insns[insnIdx] & 0xff) == OP_MOVE_EXCEPTION) {
2555 LOG_VFY("VFY: invalid use of move-exception\n");
2556 return false;
2557 }
2558 return true;
2559}
2560
2561/*
2562 * For the "move-exception" instruction at "insnIdx", which must be at an
2563 * exception handler address, determine the first common superclass of
2564 * all exceptions that can land here. (For javac output, we're probably
2565 * looking at multiple spans of bytecode covered by one "try" that lands
2566 * at an exception-specific "catch", but in general the handler could be
2567 * shared for multiple exceptions.)
2568 *
2569 * Returns NULL if no matching exception handler can be found, or if the
2570 * exception is not a subclass of Throwable.
2571 */
Andy McFadden62a75162009-04-17 17:23:37 -07002572static ClassObject* getCaughtExceptionType(const Method* meth, int insnIdx,
2573 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002574{
Andy McFadden62a75162009-04-17 17:23:37 -07002575 VerifyError localFailure;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002576 const DexCode* pCode;
2577 DexFile* pDexFile;
2578 ClassObject* commonSuper = NULL;
Andy McFadden62a75162009-04-17 17:23:37 -07002579 bool foundPossibleHandler = false;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002580 u4 handlersSize;
2581 u4 offset;
2582 u4 i;
2583
2584 pDexFile = meth->clazz->pDvmDex->pDexFile;
2585 pCode = dvmGetMethodCode(meth);
2586
2587 if (pCode->triesSize != 0) {
2588 handlersSize = dexGetHandlersSize(pCode);
2589 offset = dexGetFirstHandlerOffset(pCode);
2590 } else {
2591 handlersSize = 0;
2592 offset = 0;
2593 }
2594
2595 for (i = 0; i < handlersSize; i++) {
2596 DexCatchIterator iterator;
2597 dexCatchIteratorInit(&iterator, pCode, offset);
2598
2599 for (;;) {
2600 const DexCatchHandler* handler = dexCatchIteratorNext(&iterator);
2601
2602 if (handler == NULL) {
2603 break;
2604 }
2605
2606 if (handler->address == (u4) insnIdx) {
2607 ClassObject* clazz;
Andy McFadden62a75162009-04-17 17:23:37 -07002608 foundPossibleHandler = true;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002609
2610 if (handler->typeIdx == kDexNoIndex)
2611 clazz = gDvm.classJavaLangThrowable;
2612 else
Andy McFadden62a75162009-04-17 17:23:37 -07002613 clazz = dvmOptResolveClass(meth->clazz, handler->typeIdx,
2614 &localFailure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002615
2616 if (clazz == NULL) {
2617 LOG_VFY("VFY: unable to resolve exception class %u (%s)\n",
2618 handler->typeIdx,
2619 dexStringByTypeIdx(pDexFile, handler->typeIdx));
Andy McFadden62a75162009-04-17 17:23:37 -07002620 /* TODO: do we want to keep going? If we don't fail
2621 * this we run the risk of having a non-Throwable
2622 * introduced at runtime. However, that won't pass
2623 * an instanceof test, so is essentially harmless. */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002624 } else {
2625 if (commonSuper == NULL)
2626 commonSuper = clazz;
2627 else
2628 commonSuper = findCommonSuperclass(clazz, commonSuper);
2629 }
2630 }
2631 }
2632
2633 offset = dexCatchIteratorGetEndOffset(&iterator, pCode);
2634 }
2635
2636 if (commonSuper == NULL) {
Andy McFadden62a75162009-04-17 17:23:37 -07002637 /* no catch blocks, or no catches with classes we can find */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002638 LOG_VFY_METH(meth,
2639 "VFY: unable to find exception handler at addr 0x%x\n", insnIdx);
Andy McFadden62a75162009-04-17 17:23:37 -07002640 *pFailure = VERIFY_ERROR_GENERIC;
2641 } else {
2642 // TODO: verify the class is an instance of Throwable?
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002643 }
2644
2645 return commonSuper;
2646}
2647
2648/*
2649 * Initialize the RegisterTable.
2650 *
2651 * Every instruction address can have a different set of information about
2652 * what's in which register, but for verification purposes we only need to
2653 * store it at branch target addresses (because we merge into that).
2654 *
2655 * By zeroing out the storage we are effectively initializing the register
2656 * information to kRegTypeUnknown.
2657 */
2658static bool initRegisterTable(const Method* meth, const InsnFlags* insnFlags,
2659 RegisterTable* regTable, RegisterTrackingMode trackRegsFor)
2660{
2661 const int insnsSize = dvmGetMethodInsnsSize(meth);
2662 int i;
2663
2664 regTable->insnRegCountPlus = meth->registersSize + kExtraRegs;
2665 regTable->addrRegs = (RegType**) calloc(insnsSize, sizeof(RegType*));
2666 if (regTable->addrRegs == NULL)
2667 return false;
2668
2669 assert(insnsSize > 0);
2670
2671 /*
2672 * "All" means "every address that holds the start of an instruction".
2673 * "Branches" and "GcPoints" mean just those addresses.
2674 *
2675 * "GcPoints" fills about half the addresses, "Branches" about 15%.
2676 */
2677 int interestingCount = 0;
2678 //int insnCount = 0;
2679
2680 for (i = 0; i < insnsSize; i++) {
2681 bool interesting;
2682
2683 switch (trackRegsFor) {
2684 case kTrackRegsAll:
2685 interesting = dvmInsnIsOpcode(insnFlags, i);
2686 break;
2687 case kTrackRegsGcPoints:
2688 interesting = dvmInsnIsGcPoint(insnFlags, i) ||
2689 dvmInsnIsBranchTarget(insnFlags, i);
2690 break;
2691 case kTrackRegsBranches:
2692 interesting = dvmInsnIsBranchTarget(insnFlags, i);
2693 break;
2694 default:
2695 dvmAbort();
2696 return false;
2697 }
2698
2699 if (interesting)
2700 interestingCount++;
2701
2702 /* count instructions, for display only */
2703 //if (dvmInsnIsOpcode(insnFlags, i))
2704 // insnCount++;
2705 }
2706
2707 regTable->regAlloc = (RegType*)
2708 calloc(regTable->insnRegCountPlus * interestingCount, sizeof(RegType));
2709 if (regTable->regAlloc == NULL)
2710 return false;
2711
2712 RegType* regPtr = regTable->regAlloc;
2713 for (i = 0; i < insnsSize; i++) {
2714 bool interesting;
2715
2716 switch (trackRegsFor) {
2717 case kTrackRegsAll:
2718 interesting = dvmInsnIsOpcode(insnFlags, i);
2719 break;
2720 case kTrackRegsGcPoints:
2721 interesting = dvmInsnIsGcPoint(insnFlags, i) ||
2722 dvmInsnIsBranchTarget(insnFlags, i);
2723 break;
2724 case kTrackRegsBranches:
2725 interesting = dvmInsnIsBranchTarget(insnFlags, i);
2726 break;
2727 default:
2728 dvmAbort();
2729 return false;
2730 }
2731
2732 if (interesting) {
2733 regTable->addrRegs[i] = regPtr;
2734 regPtr += regTable->insnRegCountPlus;
2735 }
2736 }
2737
2738 //LOGD("Tracking registers for %d, total %d of %d(%d) (%d%%)\n",
2739 // TRACK_REGS_FOR, interestingCount, insnCount, insnsSize,
2740 // (interestingCount*100) / insnCount);
2741
2742 assert(regPtr - regTable->regAlloc ==
2743 regTable->insnRegCountPlus * interestingCount);
2744 assert(regTable->addrRegs[0] != NULL);
2745 return true;
2746}
2747
2748
2749/*
2750 * Verify that the arguments in a filled-new-array instruction are valid.
2751 *
2752 * "resClass" is the class refered to by pDecInsn->vB.
2753 */
2754static void verifyFilledNewArrayRegs(const Method* meth,
2755 const RegType* insnRegs, const int insnRegCount,
2756 const DecodedInstruction* pDecInsn, ClassObject* resClass, bool isRange,
Andy McFadden62a75162009-04-17 17:23:37 -07002757 VerifyError* pFailure)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002758{
2759 u4 argCount = pDecInsn->vA;
2760 RegType expectedType;
2761 PrimitiveType elemType;
2762 unsigned int ui;
2763
2764 assert(dvmIsArrayClass(resClass));
2765 elemType = resClass->elementClass->primitiveType;
2766 if (elemType == PRIM_NOT) {
2767 expectedType = regTypeFromClass(resClass->elementClass);
2768 } else {
2769 expectedType = primitiveTypeToRegType(elemType);
2770 }
2771 //LOGI("filled-new-array: %s -> %d\n", resClass->descriptor, expectedType);
2772
2773 /*
2774 * Verify each register. If "argCount" is bad, verifyRegisterType()
2775 * will run off the end of the list and fail. It's legal, if silly,
2776 * for argCount to be zero.
2777 */
2778 for (ui = 0; ui < argCount; ui++) {
2779 u4 getReg;
2780
2781 if (isRange)
2782 getReg = pDecInsn->vC + ui;
2783 else
2784 getReg = pDecInsn->arg[ui];
2785
Andy McFadden62a75162009-04-17 17:23:37 -07002786 verifyRegisterType(insnRegs, insnRegCount, getReg, expectedType,
2787 pFailure);
2788 if (!VERIFY_OK(*pFailure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002789 LOG_VFY("VFY: filled-new-array arg %u(%u) not valid\n", ui, getReg);
2790 return;
2791 }
2792 }
2793}
2794
2795
2796/*
Andy McFaddenb51ea112009-05-08 16:50:17 -07002797 * Replace an instruction with "throw-verification-error". This allows us to
2798 * defer error reporting until the code path is first used.
2799 *
2800 * The throw-verification-error instruction requires two code units. Some
2801 * of the replaced instructions require three; the third code unit will
2802 * receive a "nop". The instruction's length will be left unchanged
2803 * in "insnFlags".
2804 *
2805 * IMPORTANT: this may replace meth->insns with a pointer to a new copy of
2806 * the instructions.
2807 *
2808 * Returns "true" on success.
2809 */
2810static bool replaceFailingInstruction(Method* meth, InsnFlags* insnFlags,
2811 int insnIdx, VerifyError failure)
2812{
2813 const u2* oldInsns = meth->insns + insnIdx;
2814 u2 oldInsn = *oldInsns;
2815 bool result = false;
2816
2817 dvmMakeCodeReadWrite(meth);
2818
2819 //LOGD(" was 0x%04x\n", oldInsn);
2820 u2* newInsns = (u2*) meth->insns + insnIdx;
2821
2822 /*
2823 * Generate the new instruction out of the old.
2824 *
2825 * First, make sure this is an instruction we're expecting to stomp on.
2826 */
2827 switch (oldInsn & 0xff) {
2828 case OP_CONST_CLASS: // insn[1] == class ref, 2 bytes
2829 case OP_CHECK_CAST:
2830 case OP_INSTANCE_OF:
2831 case OP_NEW_INSTANCE:
2832 case OP_NEW_ARRAY:
2833
2834 case OP_FILLED_NEW_ARRAY: // insn[1] == class ref, 3 bytes
2835 case OP_FILLED_NEW_ARRAY_RANGE:
2836
2837 case OP_IGET: // insn[1] == field ref, 2 bytes
2838 case OP_IGET_BOOLEAN:
2839 case OP_IGET_BYTE:
2840 case OP_IGET_CHAR:
2841 case OP_IGET_SHORT:
2842 case OP_IGET_WIDE:
2843 case OP_IGET_OBJECT:
2844 case OP_IPUT:
2845 case OP_IPUT_BOOLEAN:
2846 case OP_IPUT_BYTE:
2847 case OP_IPUT_CHAR:
2848 case OP_IPUT_SHORT:
2849 case OP_IPUT_WIDE:
2850 case OP_IPUT_OBJECT:
2851 case OP_SGET:
2852 case OP_SGET_BOOLEAN:
2853 case OP_SGET_BYTE:
2854 case OP_SGET_CHAR:
2855 case OP_SGET_SHORT:
2856 case OP_SGET_WIDE:
2857 case OP_SGET_OBJECT:
2858 case OP_SPUT:
2859 case OP_SPUT_BOOLEAN:
2860 case OP_SPUT_BYTE:
2861 case OP_SPUT_CHAR:
2862 case OP_SPUT_SHORT:
2863 case OP_SPUT_WIDE:
2864 case OP_SPUT_OBJECT:
2865
2866 case OP_INVOKE_VIRTUAL: // insn[1] == method ref, 3 bytes
2867 case OP_INVOKE_VIRTUAL_RANGE:
2868 case OP_INVOKE_SUPER:
2869 case OP_INVOKE_SUPER_RANGE:
2870 case OP_INVOKE_DIRECT:
2871 case OP_INVOKE_DIRECT_RANGE:
2872 case OP_INVOKE_STATIC:
2873 case OP_INVOKE_STATIC_RANGE:
2874 case OP_INVOKE_INTERFACE:
2875 case OP_INVOKE_INTERFACE_RANGE:
2876 break;
2877 default:
2878 /* could handle this in a generic way, but this is probably safer */
2879 LOG_VFY("GLITCH: verifier asked to replace opcode 0x%02x\n",
2880 oldInsn & 0xff);
2881 goto bail;
2882 }
2883
2884 /* write a NOP over the third code unit, if necessary */
2885 int width = dvmInsnGetWidth(insnFlags, insnIdx);
2886 switch (width) {
2887 case 2:
2888 /* nothing to do */
2889 break;
2890 case 3:
2891 newInsns[2] = OP_NOP;
2892 break;
2893 default:
2894 /* whoops */
2895 LOGE("ERROR: stomped a %d-unit instruction with a verifier error\n",
2896 width);
2897 dvmAbort();
2898 }
2899
2900 /* encode the opcode, with the failure code in the high byte */
2901 newInsns[0] = OP_THROW_VERIFICATION_ERROR | (failure << 8);
2902
2903 result = true;
2904
2905bail:
2906 dvmMakeCodeReadOnly(meth);
2907 return result;
2908}
2909
2910
2911/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002912 * ===========================================================================
2913 * Entry point and driver loop
2914 * ===========================================================================
2915 */
2916
2917/*
2918 * Entry point for the detailed code-flow analysis.
2919 */
Andy McFaddenb51ea112009-05-08 16:50:17 -07002920bool dvmVerifyCodeFlow(Method* meth, InsnFlags* insnFlags,
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002921 UninitInstanceMap* uninitMap)
2922{
2923 bool result = false;
2924 const int insnsSize = dvmGetMethodInsnsSize(meth);
2925 const u2* insns = meth->insns;
2926 const bool generateRegisterMap = gDvm.generateRegisterMaps;
2927 int i, offset;
2928 bool isConditional;
2929 RegisterTable regTable;
2930
2931 memset(&regTable, 0, sizeof(regTable));
2932
2933#ifndef NDEBUG
2934 checkMergeTab(); // only need to do this if table gets updated
2935#endif
2936
2937 /*
2938 * We rely on these for verification of const-class, const-string,
2939 * and throw instructions. Make sure we have them.
2940 */
2941 if (gDvm.classJavaLangClass == NULL)
2942 gDvm.classJavaLangClass =
2943 dvmFindSystemClassNoInit("Ljava/lang/Class;");
2944 if (gDvm.classJavaLangString == NULL)
2945 gDvm.classJavaLangString =
2946 dvmFindSystemClassNoInit("Ljava/lang/String;");
2947 if (gDvm.classJavaLangThrowable == NULL)
2948 gDvm.classJavaLangThrowable =
2949 dvmFindSystemClassNoInit("Ljava/lang/Throwable;");
2950 if (gDvm.classJavaLangObject == NULL)
2951 gDvm.classJavaLangObject =
2952 dvmFindSystemClassNoInit("Ljava/lang/Object;");
2953
2954 if (meth->registersSize * insnsSize > 2*1024*1024) {
2955 /* should probably base this on actual memory requirements */
2956 LOG_VFY_METH(meth,
2957 "VFY: arbitrarily rejecting large method (regs=%d count=%d)\n",
2958 meth->registersSize, insnsSize);
2959 goto bail;
2960 }
2961
2962 /*
2963 * Create register lists, and initialize them to "Unknown". If we're
2964 * also going to create the register map, we need to retain the
2965 * register lists for a larger set of addresses.
2966 */
2967 if (!initRegisterTable(meth, insnFlags, &regTable,
2968 generateRegisterMap ? kTrackRegsGcPoints : kTrackRegsBranches))
2969 goto bail;
2970
2971 /*
2972 * Initialize the types of the registers that correspond to the
2973 * method arguments. We can determine this from the method signature.
2974 */
2975 if (!setTypesFromSignature(meth, regTable.addrRegs[0], uninitMap))
2976 goto bail;
2977
2978 /*
2979 * Run the verifier.
2980 */
2981 if (!doCodeVerification(meth, insnFlags, &regTable, uninitMap))
2982 goto bail;
2983
2984 /*
2985 * Generate a register map.
2986 */
2987 if (generateRegisterMap) {
2988 RegisterMap* pMap;
2989 VerifierData vd;
2990
2991 vd.method = meth;
2992 vd.insnsSize = insnsSize;
2993 vd.insnRegCount = meth->registersSize;
2994 vd.insnFlags = insnFlags;
2995 vd.addrRegs = regTable.addrRegs;
2996
2997 pMap = dvmGenerateRegisterMapV(&vd);
2998 if (pMap != NULL) {
2999 /*
3000 * Tuck it into the Method struct. It will either get used
3001 * directly or, if we're in dexopt, will be packed up and
3002 * appended to the DEX file.
3003 */
3004 dvmSetRegisterMap((Method*)meth, pMap);
3005 }
3006 }
3007
3008 /*
3009 * Success.
3010 */
3011 result = true;
3012
3013bail:
3014 free(regTable.addrRegs);
3015 free(regTable.regAlloc);
3016 return result;
3017}
3018
3019/*
3020 * Grind through the instructions.
3021 *
3022 * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit
3023 * on the first instruction, process it (setting additional "changed" bits),
3024 * and repeat until there are no more.
3025 *
3026 * v3 4.11.1.1
3027 * - (N/A) operand stack is always the same size
3028 * - operand stack [registers] contain the correct types of values
3029 * - local variables [registers] contain the correct types of values
3030 * - methods are invoked with the appropriate arguments
3031 * - fields are assigned using values of appropriate types
3032 * - opcodes have the correct type values in operand registers
3033 * - there is never an uninitialized class instance in a local variable in
3034 * code protected by an exception handler (operand stack is okay, because
3035 * the operand stack is discarded when an exception is thrown) [can't
3036 * know what's a local var w/o the debug info -- should fall out of
3037 * register typing]
3038 *
3039 * v3 4.11.1.2
3040 * - execution cannot fall off the end of the code
3041 *
3042 * (We also do many of the items described in the "static checks" sections,
3043 * because it's easier to do them here.)
3044 *
3045 * We need an array of RegType values, one per register, for every
3046 * instruction. In theory this could become quite large -- up to several
3047 * megabytes for a monster function. For self-preservation we reject
3048 * anything that requires more than a certain amount of memory. (Typical
3049 * "large" should be on the order of 4K code units * 8 registers.) This
3050 * will likely have to be adjusted.
3051 *
3052 *
3053 * The spec forbids backward branches when there's an uninitialized reference
3054 * in a register. The idea is to prevent something like this:
3055 * loop:
3056 * move r1, r0
3057 * new-instance r0, MyClass
3058 * ...
3059 * if-eq rN, loop // once
3060 * initialize r0
3061 *
3062 * This leaves us with two different instances, both allocated by the
3063 * same instruction, but only one is initialized. The scheme outlined in
3064 * v3 4.11.1.4 wouldn't catch this, so they work around it by preventing
3065 * backward branches. We achieve identical results without restricting
3066 * code reordering by specifying that you can't execute the new-instance
3067 * instruction if a register contains an uninitialized instance created
3068 * by that same instrutcion.
3069 */
Andy McFaddenb51ea112009-05-08 16:50:17 -07003070static bool doCodeVerification(Method* meth, InsnFlags* insnFlags,
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003071 RegisterTable* regTable, UninitInstanceMap* uninitMap)
3072{
3073 const int insnsSize = dvmGetMethodInsnsSize(meth);
3074 const u2* insns = meth->insns;
3075 RegType workRegs[meth->registersSize + kExtraRegs];
3076 bool result = false;
3077 bool debugVerbose = false;
3078 int insnIdx, startGuess, prevAddr;
3079
3080 /*
3081 * Begin by marking the first instruction as "changed".
3082 */
3083 dvmInsnSetChanged(insnFlags, 0, true);
3084
3085 if (doVerboseLogging(meth)) {
3086 IF_LOGI() {
3087 char* desc = dexProtoCopyMethodDescriptor(&meth->prototype);
3088 LOGI("Now verifying: %s.%s %s (ins=%d regs=%d)\n",
3089 meth->clazz->descriptor, meth->name, desc,
3090 meth->insSize, meth->registersSize);
3091 LOGI(" ------ [0 4 8 12 16 20 24 28 32 36\n");
3092 free(desc);
3093 }
3094 debugVerbose = true;
3095 gDebugVerbose = true;
3096 } else {
3097 gDebugVerbose = false;
3098 }
3099
3100 startGuess = 0;
3101
3102 /*
3103 * Continue until no instructions are marked "changed".
3104 */
3105 while (true) {
3106 /*
3107 * Find the first marked one. Use "startGuess" as a way to find
3108 * one quickly.
3109 */
3110 for (insnIdx = startGuess; insnIdx < insnsSize; insnIdx++) {
3111 if (dvmInsnIsChanged(insnFlags, insnIdx))
3112 break;
3113 }
3114
3115 if (insnIdx == insnsSize) {
3116 if (startGuess != 0) {
3117 /* try again, starting from the top */
3118 startGuess = 0;
3119 continue;
3120 } else {
3121 /* all flags are clear */
3122 break;
3123 }
3124 }
3125
3126 /*
3127 * We carry the working set of registers from instruction to
3128 * instruction. If this address can be the target of a branch
3129 * (or throw) instruction, or if we're skipping around chasing
3130 * "changed" flags, we need to load the set of registers from
3131 * the table.
3132 *
3133 * Because we always prefer to continue on to the next instruction,
3134 * we should never have a situation where we have a stray
3135 * "changed" flag set on an instruction that isn't a branch target.
3136 */
3137 if (dvmInsnIsBranchTarget(insnFlags, insnIdx)) {
3138 RegType* insnRegs = getRegisterLine(regTable, insnIdx);
3139 assert(insnRegs != NULL);
3140 copyRegisters(workRegs, insnRegs, meth->registersSize + kExtraRegs);
3141
3142 if (debugVerbose) {
3143 dumpRegTypes(meth, insnFlags, workRegs, insnIdx, NULL,uninitMap,
3144 SHOW_REG_DETAILS);
3145 }
3146
3147 } else {
3148 if (debugVerbose) {
3149 dumpRegTypes(meth, insnFlags, workRegs, insnIdx, NULL,uninitMap,
3150 SHOW_REG_DETAILS);
3151 }
3152
3153#ifndef NDEBUG
3154 /*
3155 * Sanity check: retrieve the stored register line (assuming
3156 * a full table) and make sure it actually matches.
3157 */
3158 RegType* insnRegs = getRegisterLine(regTable, insnIdx);
3159 if (insnRegs != NULL &&
3160 compareRegisters(workRegs, insnRegs,
3161 meth->registersSize + kExtraRegs) != 0)
3162 {
3163 char* desc = dexProtoCopyMethodDescriptor(&meth->prototype);
3164 LOG_VFY("HUH? workRegs diverged in %s.%s %s\n",
3165 meth->clazz->descriptor, meth->name, desc);
3166 free(desc);
3167 dumpRegTypes(meth, insnFlags, workRegs, 0, "work",
3168 uninitMap, DRT_SHOW_REF_TYPES | DRT_SHOW_LOCALS);
3169 dumpRegTypes(meth, insnFlags, insnRegs, 0, "insn",
3170 uninitMap, DRT_SHOW_REF_TYPES | DRT_SHOW_LOCALS);
3171 }
3172#endif
3173 }
3174
3175 //LOGI("process %s.%s %s %d\n",
3176 // meth->clazz->descriptor, meth->name, meth->descriptor, insnIdx);
3177 if (!verifyInstruction(meth, insnFlags, regTable, workRegs, insnIdx,
3178 uninitMap, &startGuess))
3179 {
3180 //LOGD("+++ %s bailing at %d\n", meth->name, insnIdx);
3181 goto bail;
3182 }
3183
3184#if 0
3185 {
3186 static const int gcMask = kInstrCanBranch | kInstrCanSwitch |
3187 kInstrCanThrow | kInstrCanReturn;
3188 OpCode opCode = *(meth->insns + insnIdx) & 0xff;
3189 int flags = dexGetInstrFlags(gDvm.instrFlags, opCode);
3190
3191 /* 8, 16, 32, or 32*n -bit regs */
3192 int regWidth = (meth->registersSize + 7) / 8;
3193 if (regWidth == 3)
3194 regWidth = 4;
3195 if (regWidth > 4) {
3196 regWidth = ((regWidth + 3) / 4) * 4;
3197 if (false) {
3198 LOGW("WOW: %d regs -> %d %s.%s\n",
3199 meth->registersSize, regWidth,
3200 meth->clazz->descriptor, meth->name);
3201 //x = true;
3202 }
3203 }
3204
3205 if ((flags & gcMask) != 0) {
3206 /* this is a potential GC point */
3207 gDvm__gcInstr++;
3208
3209 if (insnsSize < 256)
3210 gDvm__gcData += 1;
3211 else
3212 gDvm__gcData += 2;
3213 gDvm__gcData += regWidth;
3214 }
3215 gDvm__gcSimpleData += regWidth;
3216
3217 gDvm__totalInstr++;
3218 }
3219#endif
3220
3221 /*
3222 * Clear "changed" and mark as visited.
3223 */
3224 dvmInsnSetVisited(insnFlags, insnIdx, true);
3225 dvmInsnSetChanged(insnFlags, insnIdx, false);
3226 }
3227
Andy McFaddenb51ea112009-05-08 16:50:17 -07003228 if (DEAD_CODE_SCAN && !IS_METHOD_FLAG_SET(meth, METHOD_ISWRITABLE)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003229 /*
Andy McFaddenb51ea112009-05-08 16:50:17 -07003230 * Scan for dead code. There's nothing "evil" about dead code
3231 * (besides the wasted space), but it indicates a flaw somewhere
3232 * down the line, possibly in the verifier.
3233 *
3234 * If we've rewritten "always throw" instructions into the stream,
3235 * we are almost certainly going to have some dead code.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003236 */
3237 int deadStart = -1;
3238 for (insnIdx = 0; insnIdx < insnsSize;
3239 insnIdx += dvmInsnGetWidth(insnFlags, insnIdx))
3240 {
3241 /*
3242 * Switch-statement data doesn't get "visited" by scanner. It
3243 * may or may not be preceded by a padding NOP.
3244 */
3245 int instr = meth->insns[insnIdx];
3246 if (instr == kPackedSwitchSignature ||
3247 instr == kSparseSwitchSignature ||
3248 instr == kArrayDataSignature ||
3249 (instr == OP_NOP &&
3250 (meth->insns[insnIdx+1] == kPackedSwitchSignature ||
3251 meth->insns[insnIdx+1] == kSparseSwitchSignature ||
3252 meth->insns[insnIdx+1] == kArrayDataSignature)))
3253 {
3254 dvmInsnSetVisited(insnFlags, insnIdx, true);
3255 }
3256
3257 if (!dvmInsnIsVisited(insnFlags, insnIdx)) {
3258 if (deadStart < 0)
3259 deadStart = insnIdx;
3260 } else if (deadStart >= 0) {
3261 IF_LOGD() {
3262 char* desc =
3263 dexProtoCopyMethodDescriptor(&meth->prototype);
3264 LOGD("VFY: dead code 0x%04x-%04x in %s.%s %s\n",
3265 deadStart, insnIdx-1,
3266 meth->clazz->descriptor, meth->name, desc);
3267 free(desc);
3268 }
3269
3270 deadStart = -1;
3271 }
3272 }
3273 if (deadStart >= 0) {
3274 IF_LOGD() {
3275 char* desc = dexProtoCopyMethodDescriptor(&meth->prototype);
3276 LOGD("VFY: dead code 0x%04x-%04x in %s.%s %s\n",
3277 deadStart, insnIdx-1,
3278 meth->clazz->descriptor, meth->name, desc);
3279 free(desc);
3280 }
3281 }
3282 }
3283
3284 result = true;
3285
3286bail:
3287 return result;
3288}
3289
3290
3291/*
3292 * Perform verification for a single instruction.
3293 *
3294 * This requires fully decoding the instruction to determine the effect
3295 * it has on registers.
3296 *
3297 * Finds zero or more following instructions and sets the "changed" flag
3298 * if execution at that point needs to be (re-)evaluated. Register changes
3299 * are merged into "regTypes" at the target addresses. Does not set or
3300 * clear any other flags in "insnFlags".
Andy McFaddenb51ea112009-05-08 16:50:17 -07003301 *
3302 * This may alter meth->insns if we need to replace an instruction with
3303 * throw-verification-error.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003304 */
Andy McFaddenb51ea112009-05-08 16:50:17 -07003305static bool verifyInstruction(Method* meth, InsnFlags* insnFlags,
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003306 RegisterTable* regTable, RegType* workRegs, int insnIdx,
3307 UninitInstanceMap* uninitMap, int* pStartGuess)
3308{
3309 const int insnsSize = dvmGetMethodInsnsSize(meth);
3310 const u2* insns = meth->insns + insnIdx;
3311 bool result = false;
3312
3313 /*
3314 * Once we finish decoding the instruction, we need to figure out where
3315 * we can go from here. There are three possible ways to transfer
3316 * control to another statement:
3317 *
3318 * (1) Continue to the next instruction. Applies to all but
3319 * unconditional branches, method returns, and exception throws.
3320 * (2) Branch to one or more possible locations. Applies to branches
3321 * and switch statements.
3322 * (3) Exception handlers. Applies to any instruction that can
3323 * throw an exception that is handled by an encompassing "try"
3324 * block. (We simplify this to be any instruction that can
3325 * throw any exception.)
3326 *
3327 * We can also return, in which case there is no successor instruction
3328 * from this point.
3329 *
3330 * The behavior can be determined from the InstrFlags.
3331 */
3332
3333 const DexFile* pDexFile = meth->clazz->pDvmDex->pDexFile;
3334 RegType entryRegs[meth->registersSize + kExtraRegs];
3335 ClassObject* resClass;
3336 const char* className;
3337 int branchTarget = 0;
3338 const int insnRegCount = meth->registersSize;
3339 RegType tmpType;
3340 DecodedInstruction decInsn;
3341 bool justSetResult = false;
Andy McFadden62a75162009-04-17 17:23:37 -07003342 VerifyError failure = VERIFY_ERROR_NONE;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003343
3344#ifndef NDEBUG
3345 memset(&decInsn, 0x81, sizeof(decInsn));
3346#endif
3347 dexDecodeInstruction(gDvm.instrFormat, insns, &decInsn);
3348
Andy McFaddenb51ea112009-05-08 16:50:17 -07003349 int nextFlags = dexGetInstrFlags(gDvm.instrFlags, decInsn.opCode);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003350
3351 /*
3352 * Make a copy of the previous register state. If the instruction
3353 * throws an exception, we merge *this* into the destination rather
3354 * than workRegs, because we don't want the result from the "successful"
3355 * code path (e.g. a check-cast that "improves" a type) to be visible
3356 * to the exception handler.
3357 */
3358 if ((nextFlags & kInstrCanThrow) != 0 && dvmInsnIsInTry(insnFlags, insnIdx))
3359 {
3360 copyRegisters(entryRegs, workRegs, meth->registersSize + kExtraRegs);
3361 } else {
3362#ifndef NDEBUG
3363 memset(entryRegs, 0xdd,
3364 (meth->registersSize + kExtraRegs) * sizeof(RegType));
3365#endif
3366 }
3367
3368 switch (decInsn.opCode) {
3369 case OP_NOP:
3370 /*
3371 * A "pure" NOP has no effect on anything. Data tables start with
3372 * a signature that looks like a NOP; if we see one of these in
3373 * the course of executing code then we have a problem.
3374 */
3375 if (decInsn.vA != 0) {
3376 LOG_VFY("VFY: encountered data table in instruction stream\n");
Andy McFadden62a75162009-04-17 17:23:37 -07003377 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003378 }
3379 break;
3380
3381 case OP_MOVE:
3382 case OP_MOVE_FROM16:
3383 case OP_MOVE_16:
3384 copyRegister1(workRegs, insnRegCount, decInsn.vA, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07003385 kTypeCategory1nr, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003386 break;
3387 case OP_MOVE_WIDE:
3388 case OP_MOVE_WIDE_FROM16:
3389 case OP_MOVE_WIDE_16:
Andy McFadden62a75162009-04-17 17:23:37 -07003390 copyRegister2(workRegs, insnRegCount, decInsn.vA, decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003391 break;
3392 case OP_MOVE_OBJECT:
3393 case OP_MOVE_OBJECT_FROM16:
3394 case OP_MOVE_OBJECT_16:
3395 copyRegister1(workRegs, insnRegCount, decInsn.vA, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07003396 kTypeCategoryRef, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003397 break;
3398
3399 /*
3400 * The move-result instructions copy data out of a "pseudo-register"
3401 * with the results from the last method invocation. In practice we
3402 * might want to hold the result in an actual CPU register, so the
3403 * Dalvik spec requires that these only appear immediately after an
3404 * invoke or filled-new-array.
3405 *
3406 * These calls invalidate the "result" register. (This is now
3407 * redundant with the reset done below, but it can make the debug info
3408 * easier to read in some cases.)
3409 */
3410 case OP_MOVE_RESULT:
3411 copyResultRegister1(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003412 kTypeCategory1nr, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003413 break;
3414 case OP_MOVE_RESULT_WIDE:
Andy McFadden62a75162009-04-17 17:23:37 -07003415 copyResultRegister2(workRegs, insnRegCount, decInsn.vA, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003416 break;
3417 case OP_MOVE_RESULT_OBJECT:
3418 copyResultRegister1(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003419 kTypeCategoryRef, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003420 break;
3421
3422 case OP_MOVE_EXCEPTION:
3423 /*
3424 * This statement can only appear as the first instruction in an
3425 * exception handler (though not all exception handlers need to
3426 * have one of these). We verify that as part of extracting the
3427 * exception type from the catch block list.
3428 *
3429 * "resClass" will hold the closest common superclass of all
3430 * exceptions that can be handled here.
3431 */
Andy McFadden62a75162009-04-17 17:23:37 -07003432 resClass = getCaughtExceptionType(meth, insnIdx, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003433 if (resClass == NULL) {
Andy McFadden62a75162009-04-17 17:23:37 -07003434 assert(!VERIFY_OK(failure));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003435 } else {
3436 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003437 regTypeFromClass(resClass), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003438 }
3439 break;
3440
3441 case OP_RETURN_VOID:
Andy McFadden62a75162009-04-17 17:23:37 -07003442 if (!checkConstructorReturn(meth, workRegs, insnRegCount)) {
3443 failure = VERIFY_ERROR_GENERIC;
3444 } else if (getMethodReturnType(meth) != kRegTypeUnknown) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003445 LOG_VFY("VFY: return-void not expected\n");
Andy McFadden62a75162009-04-17 17:23:37 -07003446 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003447 }
3448 break;
3449 case OP_RETURN:
Andy McFadden62a75162009-04-17 17:23:37 -07003450 if (!checkConstructorReturn(meth, workRegs, insnRegCount)) {
3451 failure = VERIFY_ERROR_GENERIC;
3452 } else {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003453 /* check the method signature */
3454 RegType returnType = getMethodReturnType(meth);
Andy McFadden62a75162009-04-17 17:23:37 -07003455 checkTypeCategory(returnType, kTypeCategory1nr, &failure);
3456 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003457 LOG_VFY("VFY: return-32 not expected\n");
3458
3459 /* check the register contents */
3460 returnType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003461 &failure);
3462 checkTypeCategory(returnType, kTypeCategory1nr, &failure);
3463 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003464 LOG_VFY("VFY: return-32 on invalid register v%d\n", decInsn.vA);
3465 }
3466 break;
3467 case OP_RETURN_WIDE:
Andy McFadden62a75162009-04-17 17:23:37 -07003468 if (!checkConstructorReturn(meth, workRegs, insnRegCount)) {
3469 failure = VERIFY_ERROR_GENERIC;
3470 } else {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003471 RegType returnType, returnTypeHi;
3472
3473 /* check the method signature */
3474 returnType = getMethodReturnType(meth);
Andy McFadden62a75162009-04-17 17:23:37 -07003475 checkTypeCategory(returnType, kTypeCategory2, &failure);
3476 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003477 LOG_VFY("VFY: return-wide not expected\n");
3478
3479 /* check the register contents */
3480 returnType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003481 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003482 returnTypeHi = getRegisterType(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07003483 decInsn.vA +1, &failure);
3484 if (VERIFY_OK(failure)) {
3485 checkTypeCategory(returnType, kTypeCategory2, &failure);
3486 checkWidePair(returnType, returnTypeHi, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003487 }
Andy McFadden62a75162009-04-17 17:23:37 -07003488 if (!VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003489 LOG_VFY("VFY: return-wide on invalid register pair v%d\n",
3490 decInsn.vA);
3491 }
3492 }
3493 break;
3494 case OP_RETURN_OBJECT:
Andy McFadden62a75162009-04-17 17:23:37 -07003495 if (!checkConstructorReturn(meth, workRegs, insnRegCount)) {
3496 failure = VERIFY_ERROR_GENERIC;
3497 } else {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003498 RegType returnType = getMethodReturnType(meth);
Andy McFadden62a75162009-04-17 17:23:37 -07003499 checkTypeCategory(returnType, kTypeCategoryRef, &failure);
3500 if (!VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003501 LOG_VFY("VFY: return-object not expected\n");
3502 break;
3503 }
3504
3505 /* returnType is the *expected* return type, not register value */
3506 assert(returnType != kRegTypeZero);
3507 assert(!regTypeIsUninitReference(returnType));
3508
3509 /*
3510 * Verify that the reference in vAA is an instance of the type
3511 * in "returnType". The Zero type is allowed here. If the
3512 * method is declared to return an interface, then any
3513 * initialized reference is acceptable.
3514 *
3515 * Note getClassFromRegister fails if the register holds an
3516 * uninitialized reference, so we do not allow them to be
3517 * returned.
3518 */
3519 ClassObject* declClass;
3520
3521 declClass = regTypeInitializedReferenceToClass(returnType);
3522 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07003523 decInsn.vA, &failure);
3524 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003525 break;
3526 if (resClass != NULL) {
3527 if (!dvmIsInterfaceClass(declClass) &&
3528 !dvmInstanceof(resClass, declClass))
3529 {
3530 LOG_VFY("VFY: returning %s, declared %s\n",
3531 resClass->descriptor, declClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003532 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003533 break;
3534 }
3535 }
3536 }
3537 break;
3538
3539 case OP_CONST_4:
3540 case OP_CONST_16:
3541 case OP_CONST:
3542 /* could be boolean, int, float, or a null reference */
3543 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003544 dvmDetermineCat1Const((s4)decInsn.vB), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003545 break;
3546 case OP_CONST_HIGH16:
3547 /* could be boolean, int, float, or a null reference */
3548 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003549 dvmDetermineCat1Const((s4) decInsn.vB << 16), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003550 break;
3551 case OP_CONST_WIDE_16:
3552 case OP_CONST_WIDE_32:
3553 case OP_CONST_WIDE:
3554 case OP_CONST_WIDE_HIGH16:
3555 /* could be long or double; default to long and allow conversion */
3556 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003557 kRegTypeLongLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003558 break;
3559 case OP_CONST_STRING:
3560 case OP_CONST_STRING_JUMBO:
3561 assert(gDvm.classJavaLangString != NULL);
3562 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003563 regTypeFromClass(gDvm.classJavaLangString), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003564 break;
3565 case OP_CONST_CLASS:
3566 assert(gDvm.classJavaLangClass != NULL);
3567 /* make sure we can resolve the class; access check is important */
Andy McFadden62a75162009-04-17 17:23:37 -07003568 resClass = dvmOptResolveClass(meth->clazz, decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003569 if (resClass == NULL) {
3570 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vB);
3571 dvmLogUnableToResolveClass(badClassDesc, meth);
3572 LOG_VFY("VFY: unable to resolve const-class %d (%s) in %s\n",
3573 decInsn.vB, badClassDesc, meth->clazz->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003574 assert(failure != VERIFY_ERROR_GENERIC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003575 } else {
3576 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003577 regTypeFromClass(gDvm.classJavaLangClass), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003578 }
3579 break;
3580
3581 case OP_MONITOR_ENTER:
3582 case OP_MONITOR_EXIT:
Andy McFadden62a75162009-04-17 17:23:37 -07003583 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vA, &failure);
3584 if (VERIFY_OK(failure)) {
3585 if (!regTypeIsReference(tmpType)) {
3586 LOG_VFY("VFY: monitor op on non-object\n");
3587 failure = VERIFY_ERROR_GENERIC;
3588 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003589 }
3590 break;
3591
3592 case OP_CHECK_CAST:
3593 /*
3594 * If this instruction succeeds, we will promote register vA to
3595 * the type in vB. (This could be a demotion -- not expected, so
3596 * we don't try to address it.)
3597 *
3598 * If it fails, an exception is thrown, which we deal with later
3599 * by ignoring the update to decInsn.vA when branching to a handler.
3600 */
Andy McFadden62a75162009-04-17 17:23:37 -07003601 resClass = dvmOptResolveClass(meth->clazz, decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003602 if (resClass == NULL) {
3603 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vB);
3604 dvmLogUnableToResolveClass(badClassDesc, meth);
3605 LOG_VFY("VFY: unable to resolve check-cast %d (%s) in %s\n",
3606 decInsn.vB, badClassDesc, meth->clazz->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003607 assert(failure != VERIFY_ERROR_GENERIC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003608 } else {
3609 RegType origType;
3610
3611 origType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003612 &failure);
3613 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003614 break;
3615 if (!regTypeIsReference(origType)) {
3616 LOG_VFY("VFY: check-cast on non-reference in v%u\n",decInsn.vA);
Andy McFadden62a75162009-04-17 17:23:37 -07003617 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003618 break;
3619 }
3620 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003621 regTypeFromClass(resClass), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003622 }
3623 break;
3624 case OP_INSTANCE_OF:
3625 /* make sure we're checking a reference type */
Andy McFadden62a75162009-04-17 17:23:37 -07003626 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vB, &failure);
3627 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003628 break;
3629 if (!regTypeIsReference(tmpType)) {
3630 LOG_VFY("VFY: vB not a reference (%d)\n", tmpType);
Andy McFadden62a75162009-04-17 17:23:37 -07003631 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003632 break;
3633 }
3634
3635 /* make sure we can resolve the class; access check is important */
Andy McFadden62a75162009-04-17 17:23:37 -07003636 resClass = dvmOptResolveClass(meth->clazz, decInsn.vC, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003637 if (resClass == NULL) {
3638 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vC);
3639 dvmLogUnableToResolveClass(badClassDesc, meth);
3640 LOG_VFY("VFY: unable to resolve instanceof %d (%s) in %s\n",
3641 decInsn.vC, badClassDesc, meth->clazz->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003642 assert(failure != VERIFY_ERROR_GENERIC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003643 } else {
3644 /* result is boolean */
3645 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003646 kRegTypeBoolean, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003647 }
3648 break;
3649
3650 case OP_ARRAY_LENGTH:
3651 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07003652 decInsn.vB, &failure);
3653 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003654 break;
3655 if (resClass != NULL && !dvmIsArrayClass(resClass)) {
3656 LOG_VFY("VFY: array-length on non-array\n");
Andy McFadden62a75162009-04-17 17:23:37 -07003657 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003658 break;
3659 }
3660 setRegisterType(workRegs, insnRegCount, decInsn.vA, kRegTypeInteger,
Andy McFadden62a75162009-04-17 17:23:37 -07003661 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003662 break;
3663
3664 case OP_NEW_INSTANCE:
Andy McFadden62a75162009-04-17 17:23:37 -07003665 resClass = dvmOptResolveClass(meth->clazz, decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003666 if (resClass == NULL) {
3667 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vB);
3668 dvmLogUnableToResolveClass(badClassDesc, meth);
3669 LOG_VFY("VFY: unable to resolve new-instance %d (%s) in %s\n",
3670 decInsn.vB, badClassDesc, meth->clazz->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003671 assert(failure != VERIFY_ERROR_GENERIC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003672 } else {
3673 RegType uninitType;
3674
Andy McFaddenb51ea112009-05-08 16:50:17 -07003675 /* can't create an instance of an interface or abstract class */
3676 if (dvmIsAbstractClass(resClass) || dvmIsInterfaceClass(resClass)) {
3677 LOG_VFY("VFY: new-instance on interface or abstract class %s\n",
3678 resClass->descriptor);
3679 failure = VERIFY_ERROR_INSTANTIATION;
3680 break;
3681 }
3682
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003683 /* add resolved class to uninit map if not already there */
3684 int uidx = dvmSetUninitInstance(uninitMap, insnIdx, resClass);
3685 assert(uidx >= 0);
3686 uninitType = regTypeFromUninitIndex(uidx);
3687
3688 /*
3689 * Any registers holding previous allocations from this address
3690 * that have not yet been initialized must be marked invalid.
3691 */
3692 markUninitRefsAsInvalid(workRegs, insnRegCount, uninitMap,
3693 uninitType);
3694
3695 /* add the new uninitialized reference to the register ste */
3696 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003697 uninitType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003698 }
3699 break;
3700 case OP_NEW_ARRAY:
Andy McFadden62a75162009-04-17 17:23:37 -07003701 resClass = dvmOptResolveClass(meth->clazz, decInsn.vC, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003702 if (resClass == NULL) {
3703 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vC);
3704 dvmLogUnableToResolveClass(badClassDesc, meth);
3705 LOG_VFY("VFY: unable to resolve new-array %d (%s) in %s\n",
3706 decInsn.vC, badClassDesc, meth->clazz->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003707 assert(failure != VERIFY_ERROR_GENERIC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003708 } else if (!dvmIsArrayClass(resClass)) {
3709 LOG_VFY("VFY: new-array on non-array class\n");
Andy McFadden62a75162009-04-17 17:23:37 -07003710 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003711 } else {
3712 /* make sure "size" register is valid type */
3713 verifyRegisterType(workRegs, insnRegCount, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07003714 kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003715 /* set register type to array class */
3716 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003717 regTypeFromClass(resClass), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003718 }
3719 break;
3720 case OP_FILLED_NEW_ARRAY:
3721 case OP_FILLED_NEW_ARRAY_RANGE:
Andy McFadden62a75162009-04-17 17:23:37 -07003722 resClass = dvmOptResolveClass(meth->clazz, decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003723 if (resClass == NULL) {
3724 const char* badClassDesc = dexStringByTypeIdx(pDexFile, decInsn.vB);
3725 dvmLogUnableToResolveClass(badClassDesc, meth);
3726 LOG_VFY("VFY: unable to resolve filled-array %d (%s) in %s\n",
3727 decInsn.vB, badClassDesc, meth->clazz->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003728 assert(failure != VERIFY_ERROR_GENERIC);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003729 } else if (!dvmIsArrayClass(resClass)) {
3730 LOG_VFY("VFY: filled-new-array on non-array class\n");
Andy McFadden62a75162009-04-17 17:23:37 -07003731 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003732 } else {
3733 bool isRange = (decInsn.opCode == OP_FILLED_NEW_ARRAY_RANGE);
3734
3735 /* check the arguments to the instruction */
3736 verifyFilledNewArrayRegs(meth, workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07003737 resClass, isRange, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003738 /* filled-array result goes into "result" register */
3739 setResultRegisterType(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07003740 regTypeFromClass(resClass), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003741 justSetResult = true;
3742 }
3743 break;
3744
3745 case OP_CMPL_FLOAT:
3746 case OP_CMPG_FLOAT:
3747 verifyRegisterType(workRegs, insnRegCount, decInsn.vB, kRegTypeFloat,
Andy McFadden62a75162009-04-17 17:23:37 -07003748 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003749 verifyRegisterType(workRegs, insnRegCount, decInsn.vC, kRegTypeFloat,
Andy McFadden62a75162009-04-17 17:23:37 -07003750 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003751 setRegisterType(workRegs, insnRegCount, decInsn.vA, kRegTypeBoolean,
Andy McFadden62a75162009-04-17 17:23:37 -07003752 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003753 break;
3754 case OP_CMPL_DOUBLE:
3755 case OP_CMPG_DOUBLE:
3756 verifyRegisterType(workRegs, insnRegCount, decInsn.vB, kRegTypeDoubleLo,
Andy McFadden62a75162009-04-17 17:23:37 -07003757 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003758 verifyRegisterType(workRegs, insnRegCount, decInsn.vC, kRegTypeDoubleLo,
Andy McFadden62a75162009-04-17 17:23:37 -07003759 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003760 setRegisterType(workRegs, insnRegCount, decInsn.vA, kRegTypeBoolean,
Andy McFadden62a75162009-04-17 17:23:37 -07003761 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003762 break;
3763 case OP_CMP_LONG:
3764 verifyRegisterType(workRegs, insnRegCount, decInsn.vB, kRegTypeLongLo,
Andy McFadden62a75162009-04-17 17:23:37 -07003765 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003766 verifyRegisterType(workRegs, insnRegCount, decInsn.vC, kRegTypeLongLo,
Andy McFadden62a75162009-04-17 17:23:37 -07003767 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003768 setRegisterType(workRegs, insnRegCount, decInsn.vA, kRegTypeBoolean,
Andy McFadden62a75162009-04-17 17:23:37 -07003769 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003770 break;
3771
3772 case OP_THROW:
3773 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07003774 decInsn.vA, &failure);
3775 if (VERIFY_OK(failure) && resClass != NULL) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003776 if (!dvmInstanceof(resClass, gDvm.classJavaLangThrowable)) {
3777 LOG_VFY("VFY: thrown class %s not instanceof Throwable\n",
3778 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003779 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003780 }
3781 }
3782 break;
3783
3784 case OP_GOTO:
3785 case OP_GOTO_16:
3786 case OP_GOTO_32:
3787 /* no effect on or use of registers */
3788 break;
3789
3790 case OP_PACKED_SWITCH:
3791 case OP_SPARSE_SWITCH:
3792 /* verify that vAA is an integer, or can be converted to one */
3793 verifyRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003794 kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003795 break;
3796
3797 case OP_FILL_ARRAY_DATA:
3798 {
3799 RegType valueType;
3800 const u2 *arrayData;
3801 u2 elemWidth;
3802
3803 /* Similar to the verification done for APUT */
3804 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07003805 decInsn.vA, &failure);
3806 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003807 break;
3808
3809 /* resClass can be null if the reg type is Zero */
3810 if (resClass == NULL)
3811 break;
3812
3813 if (!dvmIsArrayClass(resClass) || resClass->arrayDim != 1 ||
3814 resClass->elementClass->primitiveType == PRIM_NOT ||
3815 resClass->elementClass->primitiveType == PRIM_VOID)
3816 {
3817 LOG_VFY("VFY: invalid fill-array-data on %s\n",
3818 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003819 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003820 break;
3821 }
3822
3823 valueType = primitiveTypeToRegType(
3824 resClass->elementClass->primitiveType);
3825 assert(valueType != kRegTypeUnknown);
3826
3827 /*
3828 * Now verify if the element width in the table matches the element
3829 * width declared in the array
3830 */
3831 arrayData = insns + (insns[1] | (((s4)insns[2]) << 16));
3832 if (arrayData[0] != kArrayDataSignature) {
3833 LOG_VFY("VFY: invalid magic for array-data\n");
Andy McFadden62a75162009-04-17 17:23:37 -07003834 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003835 break;
3836 }
3837
3838 switch (resClass->elementClass->primitiveType) {
3839 case PRIM_BOOLEAN:
3840 case PRIM_BYTE:
3841 elemWidth = 1;
3842 break;
3843 case PRIM_CHAR:
3844 case PRIM_SHORT:
3845 elemWidth = 2;
3846 break;
3847 case PRIM_FLOAT:
3848 case PRIM_INT:
3849 elemWidth = 4;
3850 break;
3851 case PRIM_DOUBLE:
3852 case PRIM_LONG:
3853 elemWidth = 8;
3854 break;
3855 default:
3856 elemWidth = 0;
3857 break;
3858 }
3859
3860 /*
3861 * Since we don't compress the data in Dex, expect to see equal
3862 * width of data stored in the table and expected from the array
3863 * class.
3864 */
3865 if (arrayData[1] != elemWidth) {
3866 LOG_VFY("VFY: array-data size mismatch (%d vs %d)\n",
3867 arrayData[1], elemWidth);
Andy McFadden62a75162009-04-17 17:23:37 -07003868 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003869 }
3870 }
3871 break;
3872
3873 case OP_IF_EQ:
3874 case OP_IF_NE:
3875 {
3876 RegType type1, type2;
3877 bool tmpResult;
3878
Andy McFadden62a75162009-04-17 17:23:37 -07003879 type1 = getRegisterType(workRegs, insnRegCount, decInsn.vA,
3880 &failure);
3881 type2 = getRegisterType(workRegs, insnRegCount, decInsn.vB,
3882 &failure);
3883 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003884 break;
3885
3886 /* both references? */
3887 if (regTypeIsReference(type1) && regTypeIsReference(type2))
3888 break;
3889
3890 /* both category-1nr? */
Andy McFadden62a75162009-04-17 17:23:37 -07003891 checkTypeCategory(type1, kTypeCategory1nr, &failure);
3892 checkTypeCategory(type2, kTypeCategory1nr, &failure);
3893 if (!VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003894 LOG_VFY("VFY: args to if-eq/if-ne must both be refs or cat1\n");
3895 break;
3896 }
3897 }
3898 break;
3899 case OP_IF_LT:
3900 case OP_IF_GE:
3901 case OP_IF_GT:
3902 case OP_IF_LE:
Andy McFadden62a75162009-04-17 17:23:37 -07003903 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vA, &failure);
3904 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003905 break;
Andy McFadden62a75162009-04-17 17:23:37 -07003906 checkTypeCategory(tmpType, kTypeCategory1nr, &failure);
3907 if (!VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003908 LOG_VFY("VFY: args to 'if' must be cat-1nr\n");
3909 break;
3910 }
Andy McFadden62a75162009-04-17 17:23:37 -07003911 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vB, &failure);
3912 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003913 break;
Andy McFadden62a75162009-04-17 17:23:37 -07003914 checkTypeCategory(tmpType, kTypeCategory1nr, &failure);
3915 if (!VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003916 LOG_VFY("VFY: args to 'if' must be cat-1nr\n");
3917 break;
3918 }
3919 break;
3920 case OP_IF_EQZ:
3921 case OP_IF_NEZ:
Andy McFadden62a75162009-04-17 17:23:37 -07003922 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vA, &failure);
3923 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003924 break;
3925 if (regTypeIsReference(tmpType))
3926 break;
Andy McFadden62a75162009-04-17 17:23:37 -07003927 checkTypeCategory(tmpType, kTypeCategory1nr, &failure);
3928 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003929 LOG_VFY("VFY: expected cat-1 arg to if\n");
3930 break;
3931 case OP_IF_LTZ:
3932 case OP_IF_GEZ:
3933 case OP_IF_GTZ:
3934 case OP_IF_LEZ:
Andy McFadden62a75162009-04-17 17:23:37 -07003935 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vA, &failure);
3936 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003937 break;
Andy McFadden62a75162009-04-17 17:23:37 -07003938 checkTypeCategory(tmpType, kTypeCategory1nr, &failure);
3939 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003940 LOG_VFY("VFY: expected cat-1 arg to if\n");
3941 break;
3942
3943 case OP_AGET:
3944 tmpType = kRegTypeInteger;
3945 goto aget_1nr_common;
3946 case OP_AGET_BOOLEAN:
3947 tmpType = kRegTypeBoolean;
3948 goto aget_1nr_common;
3949 case OP_AGET_BYTE:
3950 tmpType = kRegTypeByte;
3951 goto aget_1nr_common;
3952 case OP_AGET_CHAR:
3953 tmpType = kRegTypeChar;
3954 goto aget_1nr_common;
3955 case OP_AGET_SHORT:
3956 tmpType = kRegTypeShort;
3957 goto aget_1nr_common;
3958aget_1nr_common:
3959 {
3960 RegType srcType, indexType;
3961
3962 indexType = getRegisterType(workRegs, insnRegCount, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07003963 &failure);
3964 checkArrayIndexType(meth, indexType, &failure);
3965 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003966 break;
3967
3968 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07003969 decInsn.vB, &failure);
3970 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003971 break;
3972 if (resClass != NULL) {
3973 /* verify the class */
3974 if (!dvmIsArrayClass(resClass) || resClass->arrayDim != 1 ||
3975 resClass->elementClass->primitiveType == PRIM_NOT)
3976 {
3977 LOG_VFY("VFY: invalid aget-1nr target %s\n",
3978 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003979 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003980 break;
3981 }
3982
3983 /* make sure array type matches instruction */
3984 srcType = primitiveTypeToRegType(
3985 resClass->elementClass->primitiveType);
3986
3987 if (!checkFieldArrayStore1nr(tmpType, srcType)) {
3988 LOG_VFY("VFY: invalid aget-1nr, array type=%d with"
3989 " inst type=%d (on %s)\n",
3990 srcType, tmpType, resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07003991 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003992 break;
3993 }
3994
3995 }
3996 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07003997 tmpType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08003998 }
3999 break;
4000
4001 case OP_AGET_WIDE:
4002 {
4003 RegType dstType, indexType;
4004
4005 indexType = getRegisterType(workRegs, insnRegCount, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004006 &failure);
4007 checkArrayIndexType(meth, indexType, &failure);
4008 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004009 break;
4010
4011 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07004012 decInsn.vB, &failure);
4013 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004014 break;
4015 if (resClass != NULL) {
4016 /* verify the class */
4017 if (!dvmIsArrayClass(resClass) || resClass->arrayDim != 1 ||
4018 resClass->elementClass->primitiveType == PRIM_NOT)
4019 {
4020 LOG_VFY("VFY: invalid aget-wide target %s\n",
4021 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004022 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004023 break;
4024 }
4025
4026 /* try to refine "dstType" */
4027 switch (resClass->elementClass->primitiveType) {
4028 case PRIM_LONG:
4029 dstType = kRegTypeLongLo;
4030 break;
4031 case PRIM_DOUBLE:
4032 dstType = kRegTypeDoubleLo;
4033 break;
4034 default:
4035 LOG_VFY("VFY: invalid aget-wide on %s\n",
4036 resClass->descriptor);
4037 dstType = kRegTypeUnknown;
Andy McFadden62a75162009-04-17 17:23:37 -07004038 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004039 break;
4040 }
4041 } else {
4042 /*
4043 * Null array ref; this code path will fail at runtime. We
4044 * know this is either long or double, and we don't really
4045 * discriminate between those during verification, so we
4046 * call it a long.
4047 */
4048 dstType = kRegTypeLongLo;
4049 }
4050 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004051 dstType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004052 }
4053 break;
4054
4055 case OP_AGET_OBJECT:
4056 {
4057 RegType dstType, indexType;
4058
4059 indexType = getRegisterType(workRegs, insnRegCount, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004060 &failure);
4061 checkArrayIndexType(meth, indexType, &failure);
4062 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004063 break;
4064
4065 /* get the class of the array we're pulling an object from */
4066 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07004067 decInsn.vB, &failure);
4068 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004069 break;
4070 if (resClass != NULL) {
4071 ClassObject* elementClass;
4072
4073 assert(resClass != NULL);
4074 if (!dvmIsArrayClass(resClass)) {
4075 LOG_VFY("VFY: aget-object on non-array class\n");
Andy McFadden62a75162009-04-17 17:23:37 -07004076 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004077 break;
4078 }
4079 assert(resClass->elementClass != NULL);
4080
4081 /*
4082 * Find the element class. resClass->elementClass indicates
4083 * the basic type, which won't be what we want for a
4084 * multi-dimensional array.
4085 */
4086 if (resClass->descriptor[1] == '[') {
4087 assert(resClass->arrayDim > 1);
4088 elementClass = dvmFindArrayClass(&resClass->descriptor[1],
4089 resClass->classLoader);
4090 } else if (resClass->descriptor[1] == 'L') {
4091 assert(resClass->arrayDim == 1);
4092 elementClass = resClass->elementClass;
4093 } else {
4094 LOG_VFY("VFY: aget-object on non-ref array class (%s)\n",
4095 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004096 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004097 break;
4098 }
4099
4100 dstType = regTypeFromClass(elementClass);
4101 } else {
4102 /*
4103 * The array reference is NULL, so the current code path will
4104 * throw an exception. For proper merging with later code
4105 * paths, and correct handling of "if-eqz" tests on the
4106 * result of the array get, we want to treat this as a null
4107 * reference.
4108 */
4109 dstType = kRegTypeZero;
4110 }
4111 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004112 dstType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004113 }
4114 break;
4115 case OP_APUT:
4116 tmpType = kRegTypeInteger;
4117 goto aput_1nr_common;
4118 case OP_APUT_BOOLEAN:
4119 tmpType = kRegTypeBoolean;
4120 goto aput_1nr_common;
4121 case OP_APUT_BYTE:
4122 tmpType = kRegTypeByte;
4123 goto aput_1nr_common;
4124 case OP_APUT_CHAR:
4125 tmpType = kRegTypeChar;
4126 goto aput_1nr_common;
4127 case OP_APUT_SHORT:
4128 tmpType = kRegTypeShort;
4129 goto aput_1nr_common;
4130aput_1nr_common:
4131 {
4132 RegType srcType, dstType, indexType;
4133
4134 indexType = getRegisterType(workRegs, insnRegCount, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004135 &failure);
4136 checkArrayIndexType(meth, indexType, &failure);
4137 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004138 break;
4139
4140 /* make sure the source register has the correct type */
4141 srcType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004142 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004143 if (!canConvertTo1nr(srcType, tmpType)) {
4144 LOG_VFY("VFY: invalid reg type %d on aput instr (need %d)\n",
4145 srcType, tmpType);
Andy McFadden62a75162009-04-17 17:23:37 -07004146 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004147 break;
4148 }
4149
4150 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07004151 decInsn.vB, &failure);
4152 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004153 break;
4154
4155 /* resClass can be null if the reg type is Zero */
4156 if (resClass == NULL)
4157 break;
4158
4159 if (!dvmIsArrayClass(resClass) || resClass->arrayDim != 1 ||
4160 resClass->elementClass->primitiveType == PRIM_NOT)
4161 {
4162 LOG_VFY("VFY: invalid aput-1nr on %s\n", resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004163 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004164 break;
4165 }
4166
4167 /* verify that instruction matches array */
4168 dstType = primitiveTypeToRegType(
4169 resClass->elementClass->primitiveType);
4170 assert(dstType != kRegTypeUnknown);
4171
4172 if (!checkFieldArrayStore1nr(tmpType, dstType)) {
4173 LOG_VFY("VFY: invalid aput-1nr on %s (inst=%d dst=%d)\n",
4174 resClass->descriptor, tmpType, dstType);
Andy McFadden62a75162009-04-17 17:23:37 -07004175 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004176 break;
4177 }
4178 }
4179 break;
4180 case OP_APUT_WIDE:
4181 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004182 &failure);
4183 checkArrayIndexType(meth, tmpType, &failure);
4184 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004185 break;
4186
Andy McFadden62a75162009-04-17 17:23:37 -07004187 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vA, &failure);
4188 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004189 RegType typeHi =
Andy McFadden62a75162009-04-17 17:23:37 -07004190 getRegisterType(workRegs, insnRegCount, decInsn.vA+1, &failure);
4191 checkTypeCategory(tmpType, kTypeCategory2, &failure);
4192 checkWidePair(tmpType, typeHi, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004193 }
Andy McFadden62a75162009-04-17 17:23:37 -07004194 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004195 break;
4196
4197 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07004198 decInsn.vB, &failure);
4199 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004200 break;
4201 if (resClass != NULL) {
4202 /* verify the class and try to refine "dstType" */
4203 if (!dvmIsArrayClass(resClass) || resClass->arrayDim != 1 ||
4204 resClass->elementClass->primitiveType == PRIM_NOT)
4205 {
4206 LOG_VFY("VFY: invalid aput-wide on %s\n",
4207 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004208 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004209 break;
4210 }
4211
4212 switch (resClass->elementClass->primitiveType) {
4213 case PRIM_LONG:
4214 case PRIM_DOUBLE:
4215 /* these are okay */
4216 break;
4217 default:
4218 LOG_VFY("VFY: invalid aput-wide on %s\n",
4219 resClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004220 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004221 break;
4222 }
4223 }
4224 break;
4225 case OP_APUT_OBJECT:
4226 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004227 &failure);
4228 checkArrayIndexType(meth, tmpType, &failure);
4229 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004230 break;
4231
4232 /* get the ref we're storing; Zero is okay, Uninit is not */
4233 resClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07004234 decInsn.vA, &failure);
4235 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004236 break;
4237 if (resClass != NULL) {
4238 ClassObject* arrayClass;
4239 ClassObject* elementClass;
4240
4241 /*
4242 * Get the array class. If the array ref is null, we won't
4243 * have type information (and we'll crash at runtime with a
4244 * null pointer exception).
4245 */
4246 arrayClass = getClassFromRegister(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07004247 decInsn.vB, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004248
4249 if (arrayClass != NULL) {
4250 /* see if the array holds a compatible type */
4251 if (!dvmIsArrayClass(arrayClass)) {
4252 LOG_VFY("VFY: invalid aput-object on %s\n",
4253 arrayClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004254 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004255 break;
4256 }
4257
4258 /*
4259 * Find the element class. resClass->elementClass indicates
4260 * the basic type, which won't be what we want for a
4261 * multi-dimensional array.
4262 *
4263 * All we want to check here is that the element type is a
4264 * reference class. We *don't* check instanceof here, because
4265 * you can still put a String into a String[] after the latter
4266 * has been cast to an Object[].
4267 */
4268 if (arrayClass->descriptor[1] == '[') {
4269 assert(arrayClass->arrayDim > 1);
4270 elementClass = dvmFindArrayClass(&arrayClass->descriptor[1],
4271 arrayClass->classLoader);
4272 } else {
4273 assert(arrayClass->arrayDim == 1);
4274 elementClass = arrayClass->elementClass;
4275 }
4276 if (elementClass->primitiveType != PRIM_NOT) {
4277 LOG_VFY("VFY: invalid aput-object of %s into %s\n",
4278 resClass->descriptor, arrayClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004279 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004280 break;
4281 }
4282 }
4283 }
4284 break;
4285
4286 case OP_IGET:
4287 tmpType = kRegTypeInteger;
4288 goto iget_1nr_common;
4289 case OP_IGET_BOOLEAN:
4290 tmpType = kRegTypeBoolean;
4291 goto iget_1nr_common;
4292 case OP_IGET_BYTE:
4293 tmpType = kRegTypeByte;
4294 goto iget_1nr_common;
4295 case OP_IGET_CHAR:
4296 tmpType = kRegTypeChar;
4297 goto iget_1nr_common;
4298 case OP_IGET_SHORT:
4299 tmpType = kRegTypeShort;
4300 goto iget_1nr_common;
4301iget_1nr_common:
4302 {
4303 ClassObject* fieldClass;
4304 InstField* instField;
4305 RegType objType, fieldType;
4306
4307 objType = getRegisterType(workRegs, insnRegCount, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07004308 &failure);
4309 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004310 break;
4311 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004312 &failure);
4313 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004314 break;
4315
4316 /* make sure the field's type is compatible with expectation */
4317 fieldType = primSigCharToRegType(instField->field.signature[0]);
4318 if (fieldType == kRegTypeUnknown ||
4319 !checkFieldArrayStore1nr(tmpType, fieldType))
4320 {
4321 LOG_VFY("VFY: invalid iget-1nr of %s.%s (inst=%d field=%d)\n",
4322 instField->field.clazz->descriptor,
4323 instField->field.name, tmpType, fieldType);
Andy McFadden62a75162009-04-17 17:23:37 -07004324 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004325 break;
4326 }
4327
Andy McFadden62a75162009-04-17 17:23:37 -07004328 setRegisterType(workRegs, insnRegCount, decInsn.vA, tmpType,
4329 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004330 }
4331 break;
4332 case OP_IGET_WIDE:
4333 {
4334 RegType dstType;
4335 ClassObject* fieldClass;
4336 InstField* instField;
4337 RegType objType;
4338
4339 objType = getRegisterType(workRegs, insnRegCount, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07004340 &failure);
4341 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004342 break;
4343 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004344 &failure);
4345 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004346 break;
4347 /* check the type, which should be prim */
4348 switch (instField->field.signature[0]) {
4349 case 'D':
4350 dstType = kRegTypeDoubleLo;
4351 break;
4352 case 'J':
4353 dstType = kRegTypeLongLo;
4354 break;
4355 default:
4356 LOG_VFY("VFY: invalid iget-wide of %s.%s\n",
4357 instField->field.clazz->descriptor,
4358 instField->field.name);
4359 dstType = kRegTypeUnknown;
Andy McFadden62a75162009-04-17 17:23:37 -07004360 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004361 break;
4362 }
Andy McFadden62a75162009-04-17 17:23:37 -07004363 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004364 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004365 dstType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004366 }
4367 }
4368 break;
4369 case OP_IGET_OBJECT:
4370 {
4371 ClassObject* fieldClass;
4372 InstField* instField;
4373 RegType objType;
4374
4375 objType = getRegisterType(workRegs, insnRegCount, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07004376 &failure);
4377 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004378 break;
4379 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004380 &failure);
4381 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004382 break;
4383 fieldClass = getFieldClass(meth, &instField->field);
4384 if (fieldClass == NULL) {
4385 /* class not found or primitive type */
4386 LOG_VFY("VFY: unable to recover field class from '%s'\n",
4387 instField->field.signature);
Andy McFadden62a75162009-04-17 17:23:37 -07004388 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004389 break;
4390 }
Andy McFadden62a75162009-04-17 17:23:37 -07004391 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004392 assert(!dvmIsPrimitiveClass(fieldClass));
4393 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004394 regTypeFromClass(fieldClass), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004395 }
4396 }
4397 break;
4398 case OP_IPUT:
4399 tmpType = kRegTypeInteger;
4400 goto iput_1nr_common;
4401 case OP_IPUT_BOOLEAN:
4402 tmpType = kRegTypeBoolean;
4403 goto iput_1nr_common;
4404 case OP_IPUT_BYTE:
4405 tmpType = kRegTypeByte;
4406 goto iput_1nr_common;
4407 case OP_IPUT_CHAR:
4408 tmpType = kRegTypeChar;
4409 goto iput_1nr_common;
4410 case OP_IPUT_SHORT:
4411 tmpType = kRegTypeShort;
4412 goto iput_1nr_common;
4413iput_1nr_common:
4414 {
4415 RegType srcType, fieldType, objType;
4416 ClassObject* fieldClass;
4417 InstField* instField;
4418
4419 /* make sure the source register has the correct type */
4420 srcType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004421 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004422 if (!canConvertTo1nr(srcType, tmpType)) {
4423 LOG_VFY("VFY: invalid reg type %d on iput instr (need %d)\n",
4424 srcType, tmpType);
Andy McFadden62a75162009-04-17 17:23:37 -07004425 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004426 break;
4427 }
4428
4429 objType = getRegisterType(workRegs, insnRegCount, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07004430 &failure);
4431 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004432 break;
4433 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004434 &failure);
4435 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004436 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004437 checkFinalFieldAccess(meth, &instField->field, &failure);
4438 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004439 break;
4440
4441 /* get type of field we're storing into */
4442 fieldType = primSigCharToRegType(instField->field.signature[0]);
4443 if (fieldType == kRegTypeUnknown ||
4444 !checkFieldArrayStore1nr(tmpType, fieldType))
4445 {
4446 LOG_VFY("VFY: invalid iput-1nr of %s.%s (inst=%d field=%d)\n",
4447 instField->field.clazz->descriptor,
4448 instField->field.name, tmpType, fieldType);
Andy McFadden62a75162009-04-17 17:23:37 -07004449 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004450 break;
4451 }
4452 }
4453 break;
4454 case OP_IPUT_WIDE:
Andy McFadden62a75162009-04-17 17:23:37 -07004455 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vA, &failure);
4456 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004457 RegType typeHi =
Andy McFadden62a75162009-04-17 17:23:37 -07004458 getRegisterType(workRegs, insnRegCount, decInsn.vA+1, &failure);
4459 checkTypeCategory(tmpType, kTypeCategory2, &failure);
4460 checkWidePair(tmpType, typeHi, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004461 }
Andy McFadden62a75162009-04-17 17:23:37 -07004462 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004463 ClassObject* fieldClass;
4464 InstField* instField;
4465 RegType objType;
4466
4467 objType = getRegisterType(workRegs, insnRegCount, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07004468 &failure);
4469 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004470 break;
4471 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004472 &failure);
4473 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004474 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004475 checkFinalFieldAccess(meth, &instField->field, &failure);
4476 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004477 break;
4478
4479 /* check the type, which should be prim */
4480 switch (instField->field.signature[0]) {
4481 case 'D':
4482 case 'J':
4483 /* these are okay (and interchangeable) */
4484 break;
4485 default:
4486 LOG_VFY("VFY: invalid iput-wide of %s.%s\n",
4487 instField->field.clazz->descriptor,
4488 instField->field.name);
Andy McFadden62a75162009-04-17 17:23:37 -07004489 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004490 break;
4491 }
4492 }
4493 break;
4494 case OP_IPUT_OBJECT:
4495 {
4496 ClassObject* fieldClass;
4497 ClassObject* valueClass;
4498 InstField* instField;
4499 RegType objType, valueType;
4500
4501 objType = getRegisterType(workRegs, insnRegCount, decInsn.vB,
Andy McFadden62a75162009-04-17 17:23:37 -07004502 &failure);
4503 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004504 break;
4505 instField = getInstField(meth, uninitMap, objType, decInsn.vC,
Andy McFadden62a75162009-04-17 17:23:37 -07004506 &failure);
4507 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004508 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004509 checkFinalFieldAccess(meth, &instField->field, &failure);
4510 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004511 break;
4512
4513 fieldClass = getFieldClass(meth, &instField->field);
4514 if (fieldClass == NULL) {
4515 LOG_VFY("VFY: unable to recover field class from '%s'\n",
4516 instField->field.signature);
Andy McFadden62a75162009-04-17 17:23:37 -07004517 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004518 break;
4519 }
4520
4521 valueType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004522 &failure);
4523 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004524 break;
4525 if (!regTypeIsReference(valueType)) {
4526 LOG_VFY("VFY: storing non-ref v%d into ref field '%s' (%s)\n",
4527 decInsn.vA, instField->field.name,
4528 fieldClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004529 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004530 break;
4531 }
4532 if (valueType != kRegTypeZero) {
4533 valueClass = regTypeInitializedReferenceToClass(valueType);
4534 if (valueClass == NULL) {
4535 LOG_VFY("VFY: storing uninit ref v%d into ref field\n",
4536 decInsn.vA);
Andy McFadden62a75162009-04-17 17:23:37 -07004537 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004538 break;
4539 }
4540 /* allow if field is any interface or field is base class */
4541 if (!dvmIsInterfaceClass(fieldClass) &&
4542 !dvmInstanceof(valueClass, fieldClass))
4543 {
4544 LOG_VFY("VFY: storing type '%s' into field type '%s' (%s.%s)\n",
4545 valueClass->descriptor, fieldClass->descriptor,
4546 instField->field.clazz->descriptor,
4547 instField->field.name);
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 }
4551 }
4552 }
4553 break;
4554
4555 case OP_SGET:
4556 tmpType = kRegTypeInteger;
4557 goto sget_1nr_common;
4558 case OP_SGET_BOOLEAN:
4559 tmpType = kRegTypeBoolean;
4560 goto sget_1nr_common;
4561 case OP_SGET_BYTE:
4562 tmpType = kRegTypeByte;
4563 goto sget_1nr_common;
4564 case OP_SGET_CHAR:
4565 tmpType = kRegTypeChar;
4566 goto sget_1nr_common;
4567 case OP_SGET_SHORT:
4568 tmpType = kRegTypeShort;
4569 goto sget_1nr_common;
4570sget_1nr_common:
4571 {
4572 StaticField* staticField;
4573 RegType fieldType;
4574
Andy McFadden62a75162009-04-17 17:23:37 -07004575 staticField = getStaticField(meth, decInsn.vB, &failure);
4576 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004577 break;
4578
4579 /*
4580 * Make sure the field's type is compatible with expectation.
4581 * We can get ourselves into trouble if we mix & match loads
4582 * and stores with different widths, so rather than just checking
4583 * "canConvertTo1nr" we require that the field types have equal
4584 * widths. (We can't generally require an exact type match,
4585 * because e.g. "int" and "float" are interchangeable.)
4586 */
4587 fieldType = primSigCharToRegType(staticField->field.signature[0]);
4588 if (!checkFieldArrayStore1nr(tmpType, fieldType)) {
4589 LOG_VFY("VFY: invalid sget-1nr of %s.%s (inst=%d actual=%d)\n",
4590 staticField->field.clazz->descriptor,
4591 staticField->field.name, tmpType, fieldType);
Andy McFadden62a75162009-04-17 17:23:37 -07004592 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004593 break;
4594 }
4595
Andy McFadden62a75162009-04-17 17:23:37 -07004596 setRegisterType(workRegs, insnRegCount, decInsn.vA, tmpType,
4597 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004598 }
4599 break;
4600 case OP_SGET_WIDE:
4601 {
4602 StaticField* staticField;
4603 RegType dstType;
4604
Andy McFadden62a75162009-04-17 17:23:37 -07004605 staticField = getStaticField(meth, decInsn.vB, &failure);
4606 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004607 break;
4608 /* check the type, which should be prim */
4609 switch (staticField->field.signature[0]) {
4610 case 'D':
4611 dstType = kRegTypeDoubleLo;
4612 break;
4613 case 'J':
4614 dstType = kRegTypeLongLo;
4615 break;
4616 default:
4617 LOG_VFY("VFY: invalid sget-wide of %s.%s\n",
4618 staticField->field.clazz->descriptor,
4619 staticField->field.name);
4620 dstType = kRegTypeUnknown;
Andy McFadden62a75162009-04-17 17:23:37 -07004621 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004622 break;
4623 }
Andy McFadden62a75162009-04-17 17:23:37 -07004624 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004625 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004626 dstType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004627 }
4628 }
4629 break;
4630 case OP_SGET_OBJECT:
4631 {
4632 StaticField* staticField;
4633 ClassObject* fieldClass;
4634
Andy McFadden62a75162009-04-17 17:23:37 -07004635 staticField = getStaticField(meth, decInsn.vB, &failure);
4636 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004637 break;
4638 fieldClass = getFieldClass(meth, &staticField->field);
4639 if (fieldClass == NULL) {
4640 LOG_VFY("VFY: unable to recover field class from '%s'\n",
4641 staticField->field.signature);
Andy McFadden62a75162009-04-17 17:23:37 -07004642 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004643 break;
4644 }
4645 if (dvmIsPrimitiveClass(fieldClass)) {
4646 LOG_VFY("VFY: attempt to get prim field with sget-object\n");
Andy McFadden62a75162009-04-17 17:23:37 -07004647 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004648 break;
4649 }
4650 setRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004651 regTypeFromClass(fieldClass), &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004652 }
4653 break;
4654 case OP_SPUT:
4655 tmpType = kRegTypeInteger;
4656 goto sput_1nr_common;
4657 case OP_SPUT_BOOLEAN:
4658 tmpType = kRegTypeBoolean;
4659 goto sput_1nr_common;
4660 case OP_SPUT_BYTE:
4661 tmpType = kRegTypeByte;
4662 goto sput_1nr_common;
4663 case OP_SPUT_CHAR:
4664 tmpType = kRegTypeChar;
4665 goto sput_1nr_common;
4666 case OP_SPUT_SHORT:
4667 tmpType = kRegTypeShort;
4668 goto sput_1nr_common;
4669sput_1nr_common:
4670 {
4671 RegType srcType, fieldType;
4672 StaticField* staticField;
4673
4674 /* make sure the source register has the correct type */
4675 srcType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004676 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004677 if (!canConvertTo1nr(srcType, tmpType)) {
4678 LOG_VFY("VFY: invalid reg type %d on iput instr (need %d)\n",
4679 srcType, tmpType);
Andy McFadden62a75162009-04-17 17:23:37 -07004680 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004681 break;
4682 }
4683
Andy McFadden62a75162009-04-17 17:23:37 -07004684 staticField = getStaticField(meth, decInsn.vB, &failure);
4685 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004686 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004687 checkFinalFieldAccess(meth, &staticField->field, &failure);
4688 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004689 break;
4690
4691 /*
4692 * Get type of field we're storing into. We know that the
4693 * contents of the register match the instruction, but we also
4694 * need to ensure that the instruction matches the field type.
4695 * Using e.g. sput-short to write into a 32-bit integer field
4696 * can lead to trouble if we do 16-bit writes.
4697 */
4698 fieldType = primSigCharToRegType(staticField->field.signature[0]);
4699 if (!checkFieldArrayStore1nr(tmpType, fieldType)) {
4700 LOG_VFY("VFY: invalid sput-1nr of %s.%s (inst=%d actual=%d)\n",
4701 staticField->field.clazz->descriptor,
4702 staticField->field.name, tmpType, fieldType);
Andy McFadden62a75162009-04-17 17:23:37 -07004703 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004704 break;
4705 }
4706 }
4707 break;
4708 case OP_SPUT_WIDE:
Andy McFadden62a75162009-04-17 17:23:37 -07004709 tmpType = getRegisterType(workRegs, insnRegCount, decInsn.vA, &failure);
4710 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004711 RegType typeHi =
Andy McFadden62a75162009-04-17 17:23:37 -07004712 getRegisterType(workRegs, insnRegCount, decInsn.vA+1, &failure);
4713 checkTypeCategory(tmpType, kTypeCategory2, &failure);
4714 checkWidePair(tmpType, typeHi, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004715 }
Andy McFadden62a75162009-04-17 17:23:37 -07004716 if (VERIFY_OK(failure)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004717 StaticField* staticField;
4718
Andy McFadden62a75162009-04-17 17:23:37 -07004719 staticField = getStaticField(meth, decInsn.vB, &failure);
4720 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004721 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004722 checkFinalFieldAccess(meth, &staticField->field, &failure);
4723 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004724 break;
4725
4726 /* check the type, which should be prim */
4727 switch (staticField->field.signature[0]) {
4728 case 'D':
4729 case 'J':
4730 /* these are okay */
4731 break;
4732 default:
4733 LOG_VFY("VFY: invalid sput-wide of %s.%s\n",
4734 staticField->field.clazz->descriptor,
4735 staticField->field.name);
Andy McFadden62a75162009-04-17 17:23:37 -07004736 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004737 break;
4738 }
4739 }
4740 break;
4741 case OP_SPUT_OBJECT:
4742 {
4743 ClassObject* fieldClass;
4744 ClassObject* valueClass;
4745 StaticField* staticField;
4746 RegType valueType;
4747
Andy McFadden62a75162009-04-17 17:23:37 -07004748 staticField = getStaticField(meth, decInsn.vB, &failure);
4749 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004750 break;
Andy McFadden62a75162009-04-17 17:23:37 -07004751 checkFinalFieldAccess(meth, &staticField->field, &failure);
4752 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004753 break;
4754
4755 fieldClass = getFieldClass(meth, &staticField->field);
4756 if (fieldClass == NULL) {
4757 LOG_VFY("VFY: unable to recover field class from '%s'\n",
4758 staticField->field.signature);
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 valueType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
Andy McFadden62a75162009-04-17 17:23:37 -07004764 &failure);
4765 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004766 break;
4767 if (!regTypeIsReference(valueType)) {
4768 LOG_VFY("VFY: storing non-ref v%d into ref field '%s' (%s)\n",
4769 decInsn.vA, staticField->field.name,
4770 fieldClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004771 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004772 break;
4773 }
4774 if (valueType != kRegTypeZero) {
4775 valueClass = regTypeInitializedReferenceToClass(valueType);
4776 if (valueClass == NULL) {
4777 LOG_VFY("VFY: storing uninit ref v%d into ref field\n",
4778 decInsn.vA);
Andy McFadden62a75162009-04-17 17:23:37 -07004779 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004780 break;
4781 }
4782 /* allow if field is any interface or field is base class */
4783 if (!dvmIsInterfaceClass(fieldClass) &&
4784 !dvmInstanceof(valueClass, fieldClass))
4785 {
4786 LOG_VFY("VFY: storing type '%s' into field type '%s' (%s.%s)\n",
4787 valueClass->descriptor, fieldClass->descriptor,
4788 staticField->field.clazz->descriptor,
4789 staticField->field.name);
Andy McFadden62a75162009-04-17 17:23:37 -07004790 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004791 break;
4792 }
4793 }
4794 }
4795 break;
4796
4797 case OP_INVOKE_VIRTUAL:
4798 case OP_INVOKE_VIRTUAL_RANGE:
4799 case OP_INVOKE_SUPER:
4800 case OP_INVOKE_SUPER_RANGE:
4801 {
4802 Method* calledMethod;
4803 RegType returnType;
4804 bool isRange;
4805 bool isSuper;
4806
4807 isRange = (decInsn.opCode == OP_INVOKE_VIRTUAL_RANGE ||
4808 decInsn.opCode == OP_INVOKE_SUPER_RANGE);
4809 isSuper = (decInsn.opCode == OP_INVOKE_SUPER ||
4810 decInsn.opCode == OP_INVOKE_SUPER_RANGE);
4811
4812 calledMethod = verifyInvocationArgs(meth, workRegs, insnRegCount,
4813 &decInsn, uninitMap, METHOD_VIRTUAL, isRange,
Andy McFadden62a75162009-04-17 17:23:37 -07004814 isSuper, &failure);
4815 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004816 break;
4817 returnType = getMethodReturnType(calledMethod);
Andy McFadden62a75162009-04-17 17:23:37 -07004818 setResultRegisterType(workRegs, insnRegCount, returnType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004819 justSetResult = true;
4820 }
4821 break;
4822 case OP_INVOKE_DIRECT:
4823 case OP_INVOKE_DIRECT_RANGE:
4824 {
4825 RegType returnType;
4826 Method* calledMethod;
4827 bool isRange;
4828
4829 isRange = (decInsn.opCode == OP_INVOKE_DIRECT_RANGE);
4830 calledMethod = verifyInvocationArgs(meth, workRegs, insnRegCount,
4831 &decInsn, uninitMap, METHOD_DIRECT, isRange,
Andy McFadden62a75162009-04-17 17:23:37 -07004832 false, &failure);
4833 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004834 break;
4835
4836 /*
4837 * Some additional checks when calling <init>. We know from
4838 * the invocation arg check that the "this" argument is an
4839 * instance of calledMethod->clazz. Now we further restrict
4840 * that to require that calledMethod->clazz is the same as
4841 * this->clazz or this->super, allowing the latter only if
4842 * the "this" argument is the same as the "this" argument to
4843 * this method (which implies that we're in <init> ourselves).
4844 */
4845 if (isInitMethod(calledMethod)) {
4846 RegType thisType;
4847 thisType = getInvocationThis(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07004848 &decInsn, &failure);
4849 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004850 break;
4851
4852 /* no null refs allowed (?) */
4853 if (thisType == kRegTypeZero) {
4854 LOG_VFY("VFY: unable to initialize null ref\n");
Andy McFadden62a75162009-04-17 17:23:37 -07004855 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004856 break;
4857 }
4858
4859 ClassObject* thisClass;
4860
4861 thisClass = regTypeReferenceToClass(thisType, uninitMap);
4862 assert(thisClass != NULL);
4863
4864 /* must be in same class or in superclass */
4865 if (calledMethod->clazz == thisClass->super) {
4866 if (thisClass != meth->clazz) {
4867 LOG_VFY("VFY: invoke-direct <init> on super only "
4868 "allowed for 'this' in <init>");
Andy McFadden62a75162009-04-17 17:23:37 -07004869 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004870 break;
4871 }
4872 } else if (calledMethod->clazz != thisClass) {
4873 LOG_VFY("VFY: invoke-direct <init> must be on current "
4874 "class or super\n");
Andy McFadden62a75162009-04-17 17:23:37 -07004875 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004876 break;
4877 }
4878
4879 /* arg must be an uninitialized reference */
4880 if (!regTypeIsUninitReference(thisType)) {
4881 LOG_VFY("VFY: can only initialize the uninitialized\n");
Andy McFadden62a75162009-04-17 17:23:37 -07004882 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004883 break;
4884 }
4885
4886 /*
4887 * Replace the uninitialized reference with an initialized
4888 * one, and clear the entry in the uninit map. We need to
4889 * do this for all registers that have the same object
4890 * instance in them, not just the "this" register.
4891 */
4892 int uidx = regTypeToUninitIndex(thisType);
4893 markRefsAsInitialized(workRegs, insnRegCount, uninitMap,
Andy McFadden62a75162009-04-17 17:23:37 -07004894 thisType, &failure);
4895 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004896 break;
4897 }
4898 returnType = getMethodReturnType(calledMethod);
4899 setResultRegisterType(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07004900 returnType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004901 justSetResult = true;
4902 }
4903 break;
4904 case OP_INVOKE_STATIC:
4905 case OP_INVOKE_STATIC_RANGE:
4906 {
4907 RegType returnType;
4908 Method* calledMethod;
4909 bool isRange;
4910
4911 isRange = (decInsn.opCode == OP_INVOKE_STATIC_RANGE);
4912 calledMethod = verifyInvocationArgs(meth, workRegs, insnRegCount,
4913 &decInsn, uninitMap, METHOD_STATIC, isRange,
Andy McFadden62a75162009-04-17 17:23:37 -07004914 false, &failure);
4915 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004916 break;
4917
4918 returnType = getMethodReturnType(calledMethod);
Andy McFadden62a75162009-04-17 17:23:37 -07004919 setResultRegisterType(workRegs, insnRegCount, returnType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004920 justSetResult = true;
4921 }
4922 break;
4923 case OP_INVOKE_INTERFACE:
4924 case OP_INVOKE_INTERFACE_RANGE:
4925 {
4926 RegType /*thisType,*/ returnType;
4927 Method* absMethod;
4928 bool isRange;
4929
4930 isRange = (decInsn.opCode == OP_INVOKE_INTERFACE_RANGE);
4931 absMethod = verifyInvocationArgs(meth, workRegs, insnRegCount,
4932 &decInsn, uninitMap, METHOD_INTERFACE, isRange,
Andy McFadden62a75162009-04-17 17:23:37 -07004933 false, &failure);
4934 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004935 break;
4936
4937#if 0 /* can't do this here, fails on dalvik test 052-verifier-fun */
4938 /*
4939 * Get the type of the "this" arg, which should always be an
4940 * interface class. Because we don't do a full merge on
4941 * interface classes, this might have reduced to Object.
4942 */
4943 thisType = getInvocationThis(workRegs, insnRegCount,
Andy McFadden62a75162009-04-17 17:23:37 -07004944 &decInsn, &failure);
4945 if (!VERIFY_OK(failure))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004946 break;
4947
4948 if (thisType == kRegTypeZero) {
4949 /* null pointer always passes (and always fails at runtime) */
4950 } else {
4951 ClassObject* thisClass;
4952
4953 thisClass = regTypeInitializedReferenceToClass(thisType);
4954 if (thisClass == NULL) {
4955 LOG_VFY("VFY: interface call on uninitialized\n");
Andy McFadden62a75162009-04-17 17:23:37 -07004956 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004957 break;
4958 }
4959
4960 /*
4961 * Either "thisClass" needs to be the interface class that
4962 * defined absMethod, or absMethod's class needs to be one
4963 * of the interfaces implemented by "thisClass". (Or, if
4964 * we couldn't complete the merge, this will be Object.)
4965 */
4966 if (thisClass != absMethod->clazz &&
4967 thisClass != gDvm.classJavaLangObject &&
4968 !dvmImplements(thisClass, absMethod->clazz))
4969 {
4970 LOG_VFY("VFY: unable to match absMethod '%s' with %s interfaces\n",
4971 absMethod->name, thisClass->descriptor);
Andy McFadden62a75162009-04-17 17:23:37 -07004972 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004973 break;
4974 }
4975 }
4976#endif
4977
4978 /*
4979 * We don't have an object instance, so we can't find the
4980 * concrete method. However, all of the type information is
4981 * in the abstract method, so we're good.
4982 */
4983 returnType = getMethodReturnType(absMethod);
Andy McFadden62a75162009-04-17 17:23:37 -07004984 setResultRegisterType(workRegs, insnRegCount, returnType, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004985 justSetResult = true;
4986 }
4987 break;
4988
4989 case OP_NEG_INT:
4990 case OP_NOT_INT:
4991 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07004992 kRegTypeInteger, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004993 break;
4994 case OP_NEG_LONG:
4995 case OP_NOT_LONG:
4996 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07004997 kRegTypeLongLo, kRegTypeLongLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08004998 break;
4999 case OP_NEG_FLOAT:
5000 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005001 kRegTypeFloat, kRegTypeFloat, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005002 break;
5003 case OP_NEG_DOUBLE:
5004 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005005 kRegTypeDoubleLo, kRegTypeDoubleLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005006 break;
5007 case OP_INT_TO_LONG:
5008 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005009 kRegTypeLongLo, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005010 break;
5011 case OP_INT_TO_FLOAT:
5012 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005013 kRegTypeFloat, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005014 break;
5015 case OP_INT_TO_DOUBLE:
5016 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005017 kRegTypeDoubleLo, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005018 break;
5019 case OP_LONG_TO_INT:
5020 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005021 kRegTypeInteger, kRegTypeLongLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005022 break;
5023 case OP_LONG_TO_FLOAT:
5024 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005025 kRegTypeFloat, kRegTypeLongLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005026 break;
5027 case OP_LONG_TO_DOUBLE:
5028 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005029 kRegTypeDoubleLo, kRegTypeLongLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005030 break;
5031 case OP_FLOAT_TO_INT:
5032 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005033 kRegTypeInteger, kRegTypeFloat, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005034 break;
5035 case OP_FLOAT_TO_LONG:
5036 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005037 kRegTypeLongLo, kRegTypeFloat, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005038 break;
5039 case OP_FLOAT_TO_DOUBLE:
5040 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005041 kRegTypeDoubleLo, kRegTypeFloat, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005042 break;
5043 case OP_DOUBLE_TO_INT:
5044 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005045 kRegTypeInteger, kRegTypeDoubleLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005046 break;
5047 case OP_DOUBLE_TO_LONG:
5048 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005049 kRegTypeLongLo, kRegTypeDoubleLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005050 break;
5051 case OP_DOUBLE_TO_FLOAT:
5052 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005053 kRegTypeFloat, kRegTypeDoubleLo, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005054 break;
5055 case OP_INT_TO_BYTE:
5056 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005057 kRegTypeByte, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005058 break;
5059 case OP_INT_TO_CHAR:
5060 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005061 kRegTypeChar, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005062 break;
5063 case OP_INT_TO_SHORT:
5064 checkUnop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005065 kRegTypeShort, kRegTypeInteger, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005066 break;
5067
5068 case OP_ADD_INT:
5069 case OP_SUB_INT:
5070 case OP_MUL_INT:
5071 case OP_REM_INT:
5072 case OP_DIV_INT:
5073 case OP_SHL_INT:
5074 case OP_SHR_INT:
5075 case OP_USHR_INT:
5076 checkBinop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005077 kRegTypeInteger, kRegTypeInteger, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005078 break;
5079 case OP_AND_INT:
5080 case OP_OR_INT:
5081 case OP_XOR_INT:
5082 checkBinop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005083 kRegTypeInteger, kRegTypeInteger, kRegTypeInteger, true, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005084 break;
5085 case OP_ADD_LONG:
5086 case OP_SUB_LONG:
5087 case OP_MUL_LONG:
5088 case OP_DIV_LONG:
5089 case OP_REM_LONG:
5090 case OP_AND_LONG:
5091 case OP_OR_LONG:
5092 case OP_XOR_LONG:
5093 checkBinop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005094 kRegTypeLongLo, kRegTypeLongLo, kRegTypeLongLo, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005095 break;
5096 case OP_SHL_LONG:
5097 case OP_SHR_LONG:
5098 case OP_USHR_LONG:
5099 /* shift distance is Int, making these different from other binops */
5100 checkBinop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005101 kRegTypeLongLo, kRegTypeLongLo, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005102 break;
5103 case OP_ADD_FLOAT:
5104 case OP_SUB_FLOAT:
5105 case OP_MUL_FLOAT:
5106 case OP_DIV_FLOAT:
5107 case OP_REM_FLOAT:
5108 checkBinop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005109 kRegTypeFloat, kRegTypeFloat, kRegTypeFloat, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005110 break;
5111 case OP_ADD_DOUBLE:
5112 case OP_SUB_DOUBLE:
5113 case OP_MUL_DOUBLE:
5114 case OP_DIV_DOUBLE:
5115 case OP_REM_DOUBLE:
5116 checkBinop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005117 kRegTypeDoubleLo, kRegTypeDoubleLo, kRegTypeDoubleLo, false,
5118 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005119 break;
5120 case OP_ADD_INT_2ADDR:
5121 case OP_SUB_INT_2ADDR:
5122 case OP_MUL_INT_2ADDR:
5123 case OP_REM_INT_2ADDR:
5124 case OP_SHL_INT_2ADDR:
5125 case OP_SHR_INT_2ADDR:
5126 case OP_USHR_INT_2ADDR:
5127 checkBinop2addr(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005128 kRegTypeInteger, kRegTypeInteger, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005129 break;
5130 case OP_AND_INT_2ADDR:
5131 case OP_OR_INT_2ADDR:
5132 case OP_XOR_INT_2ADDR:
5133 checkBinop2addr(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005134 kRegTypeInteger, kRegTypeInteger, kRegTypeInteger, true, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005135 break;
5136 case OP_DIV_INT_2ADDR:
5137 checkBinop2addr(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005138 kRegTypeInteger, kRegTypeInteger, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005139 break;
5140 case OP_ADD_LONG_2ADDR:
5141 case OP_SUB_LONG_2ADDR:
5142 case OP_MUL_LONG_2ADDR:
5143 case OP_DIV_LONG_2ADDR:
5144 case OP_REM_LONG_2ADDR:
5145 case OP_AND_LONG_2ADDR:
5146 case OP_OR_LONG_2ADDR:
5147 case OP_XOR_LONG_2ADDR:
5148 checkBinop2addr(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005149 kRegTypeLongLo, kRegTypeLongLo, kRegTypeLongLo, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005150 break;
5151 case OP_SHL_LONG_2ADDR:
5152 case OP_SHR_LONG_2ADDR:
5153 case OP_USHR_LONG_2ADDR:
5154 checkBinop2addr(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005155 kRegTypeLongLo, kRegTypeLongLo, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005156 break;
5157 case OP_ADD_FLOAT_2ADDR:
5158 case OP_SUB_FLOAT_2ADDR:
5159 case OP_MUL_FLOAT_2ADDR:
5160 case OP_DIV_FLOAT_2ADDR:
5161 case OP_REM_FLOAT_2ADDR:
5162 checkBinop2addr(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005163 kRegTypeFloat, kRegTypeFloat, kRegTypeFloat, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005164 break;
5165 case OP_ADD_DOUBLE_2ADDR:
5166 case OP_SUB_DOUBLE_2ADDR:
5167 case OP_MUL_DOUBLE_2ADDR:
5168 case OP_DIV_DOUBLE_2ADDR:
5169 case OP_REM_DOUBLE_2ADDR:
5170 checkBinop2addr(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005171 kRegTypeDoubleLo, kRegTypeDoubleLo, kRegTypeDoubleLo, false,
5172 &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005173 break;
5174 case OP_ADD_INT_LIT16:
5175 case OP_RSUB_INT:
5176 case OP_MUL_INT_LIT16:
5177 case OP_DIV_INT_LIT16:
5178 case OP_REM_INT_LIT16:
5179 checkLitop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005180 kRegTypeInteger, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005181 break;
5182 case OP_AND_INT_LIT16:
5183 case OP_OR_INT_LIT16:
5184 case OP_XOR_INT_LIT16:
5185 checkLitop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005186 kRegTypeInteger, kRegTypeInteger, true, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005187 break;
5188 case OP_ADD_INT_LIT8:
5189 case OP_RSUB_INT_LIT8:
5190 case OP_MUL_INT_LIT8:
5191 case OP_DIV_INT_LIT8:
5192 case OP_REM_INT_LIT8:
5193 case OP_SHL_INT_LIT8:
5194 case OP_SHR_INT_LIT8:
5195 case OP_USHR_INT_LIT8:
5196 checkLitop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005197 kRegTypeInteger, kRegTypeInteger, false, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005198 break;
5199 case OP_AND_INT_LIT8:
5200 case OP_OR_INT_LIT8:
5201 case OP_XOR_INT_LIT8:
5202 checkLitop(workRegs, insnRegCount, &decInsn,
Andy McFadden62a75162009-04-17 17:23:37 -07005203 kRegTypeInteger, kRegTypeInteger, true, &failure);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005204 break;
5205
Andy McFaddenb51ea112009-05-08 16:50:17 -07005206 /*
5207 * This falls into the general category of "optimized" instructions,
5208 * which don't generally appear during verification. Because it's
5209 * inserted in the course of verification, we can expect to see it here.
5210 */
5211 case OP_THROW_VERIFICATION_ERROR:
5212 break;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005213
5214 /*
5215 * Verifying "quickened" instructions is tricky, because we have
5216 * discarded the original field/method information. The byte offsets
5217 * and vtable indices only have meaning in the context of an object
5218 * instance.
5219 *
5220 * If a piece of code declares a local reference variable, assigns
5221 * null to it, and then issues a virtual method call on it, we
5222 * cannot evaluate the method call during verification. This situation
5223 * isn't hard to handle, since we know the call will always result in an
5224 * NPE, and the arguments and return value don't matter. Any code that
5225 * depends on the result of the method call is inaccessible, so the
5226 * fact that we can't fully verify anything that comes after the bad
5227 * call is not a problem.
5228 *
5229 * We must also consider the case of multiple code paths, only some of
5230 * which involve a null reference. We can completely verify the method
5231 * if we sidestep the results of executing with a null reference.
5232 * For example, if on the first pass through the code we try to do a
5233 * virtual method invocation through a null ref, we have to skip the
5234 * method checks and have the method return a "wildcard" type (which
5235 * merges with anything to become that other thing). The move-result
5236 * will tell us if it's a reference, single-word numeric, or double-word
5237 * value. We continue to perform the verification, and at the end of
5238 * the function any invocations that were never fully exercised are
5239 * marked as null-only.
5240 *
5241 * We would do something similar for the field accesses. The field's
5242 * type, once known, can be used to recover the width of short integers.
5243 * If the object reference was null, the field-get returns the "wildcard"
5244 * type, which is acceptable for any operation.
5245 */
5246 case OP_EXECUTE_INLINE:
5247 case OP_INVOKE_DIRECT_EMPTY:
5248 case OP_IGET_QUICK:
5249 case OP_IGET_WIDE_QUICK:
5250 case OP_IGET_OBJECT_QUICK:
5251 case OP_IPUT_QUICK:
5252 case OP_IPUT_WIDE_QUICK:
5253 case OP_IPUT_OBJECT_QUICK:
5254 case OP_INVOKE_VIRTUAL_QUICK:
5255 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
5256 case OP_INVOKE_SUPER_QUICK:
5257 case OP_INVOKE_SUPER_QUICK_RANGE:
Andy McFadden62a75162009-04-17 17:23:37 -07005258 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005259 break;
5260
5261 /* these should never appear */
5262 case OP_UNUSED_3E:
5263 case OP_UNUSED_3F:
5264 case OP_UNUSED_40:
5265 case OP_UNUSED_41:
5266 case OP_UNUSED_42:
5267 case OP_UNUSED_43:
5268 case OP_UNUSED_73:
5269 case OP_UNUSED_79:
5270 case OP_UNUSED_7A:
5271 case OP_UNUSED_E3:
5272 case OP_UNUSED_E4:
5273 case OP_UNUSED_E5:
5274 case OP_UNUSED_E6:
5275 case OP_UNUSED_E7:
5276 case OP_UNUSED_E8:
5277 case OP_UNUSED_E9:
5278 case OP_UNUSED_EA:
5279 case OP_UNUSED_EB:
5280 case OP_UNUSED_EC:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005281 case OP_UNUSED_EF:
5282 case OP_UNUSED_F1:
5283 case OP_UNUSED_FC:
5284 case OP_UNUSED_FD:
5285 case OP_UNUSED_FE:
5286 case OP_UNUSED_FF:
Andy McFadden62a75162009-04-17 17:23:37 -07005287 failure = VERIFY_ERROR_GENERIC;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005288 break;
5289
5290 /*
5291 * DO NOT add a "default" clause here. Without it the compiler will
5292 * complain if an instruction is missing (which is desirable).
5293 */
5294 }
5295
Andy McFadden62a75162009-04-17 17:23:37 -07005296 if (!VERIFY_OK(failure)) {
Andy McFaddenb51ea112009-05-08 16:50:17 -07005297 if (failure == VERIFY_ERROR_GENERIC || gDvm.optimizing) {
5298 /* immediate failure, reject class */
5299 LOG_VFY_METH(meth, "VFY: rejecting opcode 0x%02x at 0x%04x\n",
5300 decInsn.opCode, insnIdx);
5301 goto bail;
5302 } else {
5303 /* replace opcode and continue on */
5304 LOGD("VFY: replacing opcode 0x%02x at 0x%04x\n",
5305 decInsn.opCode, insnIdx);
5306 if (!replaceFailingInstruction(meth, insnFlags, insnIdx, failure)) {
5307 LOG_VFY_METH(meth, "VFY: rejecting opcode 0x%02x at 0x%04x\n",
5308 decInsn.opCode, insnIdx);
5309 goto bail;
5310 }
5311 /* IMPORTANT: meth->insns may have been changed */
5312 insns = meth->insns + insnIdx;
5313
5314 /* continue on as if we just handled a throw-verification-error */
5315 failure = VERIFY_ERROR_NONE;
5316 nextFlags = kInstrCanThrow;
5317 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005318 }
5319
5320 /*
5321 * If we didn't just set the result register, clear it out. This
5322 * ensures that you can only use "move-result" immediately after the
5323 * result is set.
5324 */
5325 if (!justSetResult) {
5326 int reg = RESULT_REGISTER(insnRegCount);
5327 workRegs[reg] = workRegs[reg+1] = kRegTypeUnknown;
5328 }
5329
5330 /*
5331 * Handle "continue". Tag the next consecutive instruction.
5332 */
5333 if ((nextFlags & kInstrCanContinue) != 0) {
5334 int insnWidth = dvmInsnGetWidth(insnFlags, insnIdx);
5335 if (insnIdx+insnWidth >= insnsSize) {
5336 LOG_VFY_METH(meth,
5337 "VFY: execution can walk off end of code area (from 0x%x)\n",
5338 insnIdx);
5339 goto bail;
5340 }
5341
5342 /*
5343 * The only way to get to a move-exception instruction is to get
5344 * thrown there. Make sure the next instruction isn't one.
5345 */
5346 if (!checkMoveException(meth, insnIdx+insnWidth, "next"))
5347 goto bail;
5348
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005349 if (getRegisterLine(regTable, insnIdx+insnWidth) != NULL) {
Andy McFadden06b7a282009-05-11 10:44:52 -07005350 /*
5351 * Merge registers into what we have for the next instruction,
5352 * and set the "changed" flag if needed.
5353 */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005354 updateRegisters(meth, insnFlags, regTable, insnIdx+insnWidth,
5355 workRegs);
5356 } else {
The Android Open Source Project99409882009-03-18 22:20:24 -07005357 /*
Andy McFadden06b7a282009-05-11 10:44:52 -07005358 * We're not recording register data for the next instruction,
5359 * so we don't know what the prior state was. We have to
5360 * assume that something has changed and re-evaluate it.
The Android Open Source Project99409882009-03-18 22:20:24 -07005361 */
5362 dvmInsnSetChanged(insnFlags, insnIdx+insnWidth, true);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005363 }
5364 }
5365
5366 /*
5367 * Handle "branch". Tag the branch target.
5368 *
5369 * NOTE: instructions like OP_EQZ provide information about the state
5370 * of the register when the branch is taken or not taken. For example,
5371 * somebody could get a reference field, check it for zero, and if the
5372 * branch is taken immediately store that register in a boolean field
5373 * since the value is known to be zero. We do not currently account for
5374 * that, and will reject the code.
5375 */
5376 if ((nextFlags & kInstrCanBranch) != 0) {
5377 bool isConditional;
5378
5379 if (!dvmGetBranchTarget(meth, insnFlags, insnIdx, &branchTarget,
5380 &isConditional))
5381 {
5382 /* should never happen after static verification */
5383 LOG_VFY_METH(meth, "VFY: bad branch at %d\n", insnIdx);
5384 goto bail;
5385 }
5386 assert(isConditional || (nextFlags & kInstrCanContinue) == 0);
5387 assert(!isConditional || (nextFlags & kInstrCanContinue) != 0);
5388
5389 if (!checkMoveException(meth, insnIdx+branchTarget, "branch"))
5390 goto bail;
5391
The Android Open Source Project99409882009-03-18 22:20:24 -07005392 /* update branch target, set "changed" if appropriate */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005393 updateRegisters(meth, insnFlags, regTable, insnIdx+branchTarget,
5394 workRegs);
5395 }
5396
5397 /*
5398 * Handle "switch". Tag all possible branch targets.
5399 *
5400 * We've already verified that the table is structurally sound, so we
5401 * just need to walk through and tag the targets.
5402 */
5403 if ((nextFlags & kInstrCanSwitch) != 0) {
5404 int offsetToSwitch = insns[1] | (((s4)insns[2]) << 16);
5405 const u2* switchInsns = insns + offsetToSwitch;
5406 int switchCount = switchInsns[1];
5407 int offsetToTargets, targ;
5408
5409 if ((*insns & 0xff) == OP_PACKED_SWITCH) {
5410 /* 0=sig, 1=count, 2/3=firstKey */
5411 offsetToTargets = 4;
5412 } else {
5413 /* 0=sig, 1=count, 2..count*2 = keys */
5414 assert((*insns & 0xff) == OP_SPARSE_SWITCH);
5415 offsetToTargets = 2 + 2*switchCount;
5416 }
5417
5418 /* verify each switch target */
5419 for (targ = 0; targ < switchCount; targ++) {
5420 int offset, absOffset;
5421
5422 /* offsets are 32-bit, and only partly endian-swapped */
5423 offset = switchInsns[offsetToTargets + targ*2] |
5424 (((s4) switchInsns[offsetToTargets + targ*2 +1]) << 16);
5425 absOffset = insnIdx + offset;
5426
5427 assert(absOffset >= 0 && absOffset < insnsSize);
5428
5429 if (!checkMoveException(meth, absOffset, "switch"))
5430 goto bail;
5431
5432 updateRegisters(meth, insnFlags, regTable, absOffset, workRegs);
5433 }
5434 }
5435
5436 /*
5437 * Handle instructions that can throw and that are sitting in a
5438 * "try" block. (If they're not in a "try" block when they throw,
5439 * control transfers out of the method.)
5440 */
5441 if ((nextFlags & kInstrCanThrow) != 0 && dvmInsnIsInTry(insnFlags, insnIdx))
5442 {
5443 DexFile* pDexFile = meth->clazz->pDvmDex->pDexFile;
5444 const DexCode* pCode = dvmGetMethodCode(meth);
5445 DexCatchIterator iterator;
5446
5447 if (dexFindCatchHandler(&iterator, pCode, insnIdx)) {
5448 for (;;) {
5449 DexCatchHandler* handler = dexCatchIteratorNext(&iterator);
5450
5451 if (handler == NULL) {
5452 break;
5453 }
5454
5455 /* note we use entryRegs, not workRegs */
5456 updateRegisters(meth, insnFlags, regTable, handler->address,
5457 entryRegs);
5458 }
5459 }
5460 }
5461
5462 /*
5463 * Update startGuess. Advance to the next instruction of that's
5464 * possible, otherwise use the branch target if one was found. If
5465 * neither of those exists we're in a return or throw; leave startGuess
5466 * alone and let the caller sort it out.
5467 */
5468 if ((nextFlags & kInstrCanContinue) != 0) {
5469 *pStartGuess = insnIdx + dvmInsnGetWidth(insnFlags, insnIdx);
5470 } else if ((nextFlags & kInstrCanBranch) != 0) {
5471 /* we're still okay if branchTarget is zero */
5472 *pStartGuess = insnIdx + branchTarget;
5473 }
5474
5475 assert(*pStartGuess >= 0 && *pStartGuess < insnsSize &&
5476 dvmInsnGetWidth(insnFlags, *pStartGuess) != 0);
5477
5478 result = true;
5479
5480bail:
5481 return result;
5482}
5483
Andy McFaddenb51ea112009-05-08 16:50:17 -07005484
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08005485/*
5486 * callback function used in dumpRegTypes to print local vars
5487 * valid at a given address.
5488 */
5489static void logLocalsCb(void *cnxt, u2 reg, u4 startAddress, u4 endAddress,
5490 const char *name, const char *descriptor,
5491 const char *signature)
5492{
5493 int addr = *((int *)cnxt);
5494
5495 if (addr >= (int) startAddress && addr < (int) endAddress)
5496 {
5497 LOGI(" %2d: '%s' %s\n", reg, name, descriptor);
5498 }
5499}
5500
5501/*
5502 * Dump the register types for the specifed address to the log file.
5503 */
5504static void dumpRegTypes(const Method* meth, const InsnFlags* insnFlags,
5505 const RegType* addrRegs, int addr, const char* addrName,
5506 const UninitInstanceMap* uninitMap, int displayFlags)
5507{
5508 int regCount = meth->registersSize;
5509 int fullRegCount = regCount + kExtraRegs;
5510 bool branchTarget = dvmInsnIsBranchTarget(insnFlags, addr);
5511 int i;
5512
5513 assert(addr >= 0 && addr < (int) dvmGetMethodInsnsSize(meth));
5514
5515 int regCharSize = fullRegCount + (fullRegCount-1)/4 + 2 +1;
5516 char regChars[regCharSize +1];
5517 memset(regChars, ' ', regCharSize);
5518 regChars[0] = '[';
5519 if (regCount == 0)
5520 regChars[1] = ']';
5521 else
5522 regChars[1 + (regCount-1) + (regCount-1)/4 +1] = ']';
5523 regChars[regCharSize] = '\0';
5524
5525 //const RegType* addrRegs = getRegisterLine(regTable, addr);
5526
5527 for (i = 0; i < regCount + kExtraRegs; i++) {
5528 char tch;
5529
5530 switch (addrRegs[i]) {
5531 case kRegTypeUnknown: tch = '.'; break;
5532 case kRegTypeConflict: tch = 'X'; break;
5533 case kRegTypeFloat: tch = 'F'; break;
5534 case kRegTypeZero: tch = '0'; break;
5535 case kRegTypeOne: tch = '1'; break;
5536 case kRegTypeBoolean: tch = 'Z'; break;
5537 case kRegTypePosByte: tch = 'b'; break;
5538 case kRegTypeByte: tch = 'B'; break;
5539 case kRegTypePosShort: tch = 's'; break;
5540 case kRegTypeShort: tch = 'S'; break;
5541 case kRegTypeChar: tch = 'C'; break;
5542 case kRegTypeInteger: tch = 'I'; break;
5543 case kRegTypeLongLo: tch = 'J'; break;
5544 case kRegTypeLongHi: tch = 'j'; break;
5545 case kRegTypeDoubleLo: tch = 'D'; break;
5546 case kRegTypeDoubleHi: tch = 'd'; break;
5547 default:
5548 if (regTypeIsReference(addrRegs[i])) {
5549 if (regTypeIsUninitReference(addrRegs[i]))
5550 tch = 'U';
5551 else
5552 tch = 'L';
5553 } else {
5554 tch = '*';
5555 assert(false);
5556 }
5557 break;
5558 }
5559
5560 if (i < regCount)
5561 regChars[1 + i + (i/4)] = tch;
5562 else
5563 regChars[1 + i + (i/4) + 2] = tch;
5564 }
5565
5566 if (addr == 0 && addrName != NULL)
5567 LOGI("%c%s %s\n", branchTarget ? '>' : ' ', addrName, regChars);
5568 else
5569 LOGI("%c0x%04x %s\n", branchTarget ? '>' : ' ', addr, regChars);
5570
5571 if (displayFlags & DRT_SHOW_REF_TYPES) {
5572 for (i = 0; i < regCount + kExtraRegs; i++) {
5573 if (regTypeIsReference(addrRegs[i]) && addrRegs[i] != kRegTypeZero)
5574 {
5575 ClassObject* clazz;
5576
5577 clazz = regTypeReferenceToClass(addrRegs[i], uninitMap);
5578 assert(dvmValidateObject((Object*)clazz));
5579 if (i < regCount) {
5580 LOGI(" %2d: 0x%08x %s%s\n",
5581 i, addrRegs[i],
5582 regTypeIsUninitReference(addrRegs[i]) ? "[U]" : "",
5583 clazz->descriptor);
5584 } else {
5585 LOGI(" RS: 0x%08x %s%s\n",
5586 addrRegs[i],
5587 regTypeIsUninitReference(addrRegs[i]) ? "[U]" : "",
5588 clazz->descriptor);
5589 }
5590 }
5591 }
5592 }
5593 if (displayFlags & DRT_SHOW_LOCALS) {
5594 dexDecodeDebugInfo(meth->clazz->pDvmDex->pDexFile,
5595 dvmGetMethodCode(meth),
5596 meth->clazz->descriptor,
5597 meth->prototype.protoIdx,
5598 meth->accessFlags,
5599 NULL, logLocalsCb, &addr);
5600 }
5601}
5602