CodeVerify.h revision cc05ad238516f1303687aba4a978e24e57c0c07a
1/*
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 verifier.
19 */
20#ifndef _DALVIK_CODEVERIFY
21#define _DALVIK_CODEVERIFY
22
23#include "analysis/VerifySubs.h"
24
25
26/*
27 * Enumeration for register type values.  The "hi" piece of a 64-bit value
28 * MUST immediately follow the "lo" piece in the enumeration, so we can check
29 * that hi==lo+1.
30 *
31 * Assignment of constants:
32 *   [-MAXINT,-32768)   : integer
33 *   [-32768,-128)      : short
34 *   [-128,0)           : byte
35 *   0                  : zero
36 *   1                  : one
37 *   [2,128)            : posbyte
38 *   [128,32768)        : posshort
39 *   [32768,65536)      : char
40 *   [65536,MAXINT]     : integer
41 *
42 * Allowed "implicit" widening conversions:
43 *   zero -> boolean, posbyte, byte, posshort, short, char, integer, ref (null)
44 *   one -> boolean, posbyte, byte, posshort, short, char, integer
45 *   boolean -> posbyte, byte, posshort, short, char, integer
46 *   posbyte -> posshort, short, integer, char
47 *   byte -> short, integer
48 *   posshort -> integer, char
49 *   short -> integer
50 *   char -> integer
51 *
52 * In addition, all of the above can convert to "float".
53 *
54 * We're more careful with integer values than the spec requires.  The
55 * motivation is to restrict byte/char/short to the correct range of values.
56 * For example, if a method takes a byte argument, we don't want to allow
57 * the code to load the constant "1024" and pass it in.
58 */
59enum {
60    kRegTypeUnknown = 0,    /* initial state; use value=0 so calloc works */
61    kRegTypeUninit = 1,     /* MUST be odd to distinguish from pointer */
62    kRegTypeConflict,       /* merge clash makes this reg's type unknowable */
63
64    /*
65     * Category-1nr types.  The order of these is chiseled into a couple
66     * of tables, so don't add, remove, or reorder if you can avoid it.
67     */
68#define kRegType1nrSTART    kRegTypeFloat
69    kRegTypeFloat,
70    kRegTypeZero,           /* 32-bit 0, could be Boolean, Int, Float, or Ref */
71    kRegTypeOne,            /* 32-bit 1, could be Boolean, Int, Float */
72    kRegTypeBoolean,        /* must be 0 or 1 */
73    kRegTypePosByte,        /* byte, known positive (can become char) */
74    kRegTypeByte,
75    kRegTypePosShort,       /* short, known positive (can become char) */
76    kRegTypeShort,
77    kRegTypeChar,
78    kRegTypeInteger,
79#define kRegType1nrEND      kRegTypeInteger
80
81    kRegTypeLongLo,         /* lower-numbered register; endian-independent */
82    kRegTypeLongHi,
83    kRegTypeDoubleLo,
84    kRegTypeDoubleHi,
85
86    /*
87     * Enumeration max; this is used with "full" (32-bit) RegType values.
88     *
89     * Anything larger than this is a ClassObject or uninit ref.  Mask off
90     * all but the low 8 bits; if you're left with kRegTypeUninit, pull
91     * the uninit index out of the high 24.  Because kRegTypeUninit has an
92     * odd value, there is no risk of a particular ClassObject pointer bit
93     * pattern being confused for it (assuming our class object allocator
94     * uses word alignment).
95     */
96    kRegTypeMAX
97};
98#define kRegTypeUninitMask  0xff
99#define kRegTypeUninitShift 8
100
101extern const char gDvmMergeTab[kRegTypeMAX][kRegTypeMAX];
102
103
104/*
105 * Returns "true" if the flags indicate that this address holds the start
106 * of an instruction.
107 */
108INLINE bool dvmInsnIsOpcode(const InsnFlags* insnFlags, int addr) {
109    return (insnFlags[addr] & kInsnFlagWidthMask) != 0;
110}
111
112/*
113 * Extract the unsigned 16-bit instruction width from "flags".
114 */
115INLINE int dvmInsnGetWidth(const InsnFlags* insnFlags, int addr) {
116    return insnFlags[addr] & kInsnFlagWidthMask;
117}
118
119/*
120 * Changed?
121 */
122INLINE bool dvmInsnIsChanged(const InsnFlags* insnFlags, int addr) {
123    return (insnFlags[addr] & kInsnFlagChanged) != 0;
124}
125INLINE void dvmInsnSetChanged(InsnFlags* insnFlags, int addr, bool changed)
126{
127    if (changed)
128        insnFlags[addr] |= kInsnFlagChanged;
129    else
130        insnFlags[addr] &= ~kInsnFlagChanged;
131}
132
133/*
134 * Visited?
135 */
136INLINE bool dvmInsnIsVisited(const InsnFlags* insnFlags, int addr) {
137    return (insnFlags[addr] & kInsnFlagVisited) != 0;
138}
139INLINE void dvmInsnSetVisited(InsnFlags* insnFlags, int addr, bool changed)
140{
141    if (changed)
142        insnFlags[addr] |= kInsnFlagVisited;
143    else
144        insnFlags[addr] &= ~kInsnFlagVisited;
145}
146
147/*
148 * Visited or changed?
149 */
150INLINE bool dvmInsnIsVisitedOrChanged(const InsnFlags* insnFlags, int addr) {
151    return (insnFlags[addr] & (kInsnFlagVisited|kInsnFlagChanged)) != 0;
152}
153
154/*
155 * In a "try" block?
156 */
157INLINE bool dvmInsnIsInTry(const InsnFlags* insnFlags, int addr) {
158    return (insnFlags[addr] & kInsnFlagInTry) != 0;
159}
160INLINE void dvmInsnSetInTry(InsnFlags* insnFlags, int addr, bool inTry)
161{
162    assert(inTry);
163    //if (inTry)
164        insnFlags[addr] |= kInsnFlagInTry;
165    //else
166    //    insnFlags[addr] &= ~kInsnFlagInTry;
167}
168
169/*
170 * Instruction is a branch target or exception handler?
171 */
172INLINE bool dvmInsnIsBranchTarget(const InsnFlags* insnFlags, int addr) {
173    return (insnFlags[addr] & kInsnFlagBranchTarget) != 0;
174}
175INLINE void dvmInsnSetBranchTarget(InsnFlags* insnFlags, int addr,
176    bool isBranch)
177{
178    assert(isBranch);
179    //if (isBranch)
180        insnFlags[addr] |= kInsnFlagBranchTarget;
181    //else
182    //    insnFlags[addr] &= ~kInsnFlagBranchTarget;
183}
184
185/*
186 * Instruction is a GC point?
187 */
188INLINE bool dvmInsnIsGcPoint(const InsnFlags* insnFlags, int addr) {
189    return (insnFlags[addr] & kInsnFlagGcPoint) != 0;
190}
191INLINE void dvmInsnSetGcPoint(InsnFlags* insnFlags, int addr,
192    bool isBranch)
193{
194    assert(isBranch);
195    //if (isBranch)
196        insnFlags[addr] |= kInsnFlagGcPoint;
197    //else
198    //    insnFlags[addr] &= ~kInsnFlagGcPoint;
199}
200
201
202/*
203 * Table that maps uninitialized instances to classes, based on the
204 * address of the new-instance instruction.
205 */
206typedef struct UninitInstanceMap {
207    int numEntries;
208    struct {
209        int             addr;   /* code offset, or -1 for method arg ("this") */
210        ClassObject*    clazz;  /* class created at this address */
211    } map[1];
212} UninitInstanceMap;
213#define kUninitThisArgAddr  (-1)
214#define kUninitThisArgSlot  0
215
216/*
217 * Create a new UninitInstanceMap.
218 */
219UninitInstanceMap* dvmCreateUninitInstanceMap(const Method* meth,
220    const InsnFlags* insnFlags, int newInstanceCount);
221
222/*
223 * Release the storage associated with an UninitInstanceMap.
224 */
225void dvmFreeUninitInstanceMap(UninitInstanceMap* uninitMap);
226
227/*
228 * Associate a class with an address.  Returns the map slot index, or -1
229 * if the address isn't listed in the map (shouldn't happen) or if a
230 * different class is already associated with the address (shouldn't
231 * happen either).
232 */
233//int dvmSetUninitInstance(UninitInstanceMap* uninitMap, int addr,
234//    ClassObject* clazz);
235
236/*
237 * Return the class associated with an uninitialized reference.  Pass in
238 * the map index.
239 */
240//ClassObject* dvmGetUninitInstance(const UninitInstanceMap* uninitMap, int idx);
241
242/*
243 * Clear the class associated with an uninitialized reference.  Pass in
244 * the map index.
245 */
246//void dvmClearUninitInstance(UninitInstanceMap* uninitMap, int idx);
247
248
249/*
250 * Verify bytecode in "meth".  "insnFlags" should be populated with
251 * instruction widths and "in try" flags.
252 */
253bool dvmVerifyCodeFlow(const Method* meth, InsnFlags* insnFlags,
254    UninitInstanceMap* uninitMap);
255
256#endif /*_DALVIK_CODEVERIFY*/
257