2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Dalvik bytecode verifier.
20 #ifndef _DALVIK_CODEVERIFY
21 #define _DALVIK_CODEVERIFY
23 #include "analysis/VerifySubs.h"
24 #include "analysis/VfyBasicBlock.h"
28 * Enumeration for register type values. The "hi" piece of a 64-bit value
29 * MUST immediately follow the "lo" piece in the enumeration, so we can check
32 * Assignment of constants:
33 * [-MAXINT,-32768) : integer
34 * [-32768,-128) : short
39 * [128,32768) : posshort
40 * [32768,65536) : char
41 * [65536,MAXINT] : integer
43 * Allowed "implicit" widening conversions:
44 * zero -> boolean, posbyte, byte, posshort, short, char, integer, ref (null)
45 * one -> boolean, posbyte, byte, posshort, short, char, integer
46 * boolean -> posbyte, byte, posshort, short, char, integer
47 * posbyte -> posshort, short, integer, char
48 * byte -> short, integer
49 * posshort -> integer, char
53 * In addition, all of the above can convert to "float".
55 * We're more careful with integer values than the spec requires. The
56 * motivation is to restrict byte/char/short to the correct range of values.
57 * For example, if a method takes a byte argument, we don't want to allow
58 * the code to load the constant "1024" and pass it in.
61 kRegTypeUnknown = 0, /* initial state; use value=0 so calloc works */
62 kRegTypeUninit = 1, /* MUST be odd to distinguish from pointer */
63 kRegTypeConflict, /* merge clash makes this reg's type unknowable */
66 * Category-1nr types. The order of these is chiseled into a couple
67 * of tables, so don't add, remove, or reorder if you can avoid it.
69 #define kRegType1nrSTART kRegTypeFloat
71 kRegTypeZero, /* 32-bit 0, could be Boolean, Int, Float, or Ref */
72 kRegTypeOne, /* 32-bit 1, could be Boolean, Int, Float */
73 kRegTypeBoolean, /* must be 0 or 1 */
74 kRegTypePosByte, /* byte, known positive (can become char) */
76 kRegTypePosShort, /* short, known positive (can become char) */
80 #define kRegType1nrEND kRegTypeInteger
82 kRegTypeLongLo, /* lower-numbered register; endian-independent */
88 * Enumeration max; this is used with "full" (32-bit) RegType values.
90 * Anything larger than this is a ClassObject or uninit ref. Mask off
91 * all but the low 8 bits; if you're left with kRegTypeUninit, pull
92 * the uninit index out of the high 24. Because kRegTypeUninit has an
93 * odd value, there is no risk of a particular ClassObject pointer bit
94 * pattern being confused for it (assuming our class object allocator
95 * uses word alignment).
99 #define kRegTypeUninitMask 0xff
100 #define kRegTypeUninitShift 8
103 * RegType holds information about the type of data held in a register.
104 * For most types it's a simple enum. For reference types it holds a
105 * pointer to the ClassObject, and for uninitialized references it holds
106 * an index into the UninitInstanceMap.
111 * A bit vector indicating which entries in the monitor stack are
112 * associated with this register. The low bit corresponds to the stack's
115 typedef u4 MonitorEntries;
116 #define kMaxMonitorStackDepth (sizeof(MonitorEntries) * 8)
119 * During verification, we associate one of these with every "interesting"
120 * instruction. We track the status of all registers, and (if the method
121 * has any monitor-enter instructions) maintain a stack of entered monitors
122 * (identified by code unit offset).
124 * If live-precise register maps are enabled, the "liveRegs" vector will
125 * be populated. Unlike the other lists of registers here, we do not
126 * track the liveness of the method result register (which is not visible
131 MonitorEntries* monitorEntries;
133 unsigned int monitorStackTop;
138 * Table that maps uninitialized instances to classes, based on the
139 * address of the new-instance instruction. One per method.
141 typedef struct UninitInstanceMap {
144 int addr; /* code offset, or -1 for method arg ("this") */
145 ClassObject* clazz; /* class created at this address */
148 #define kUninitThisArgAddr (-1)
149 #define kUninitThisArgSlot 0
152 * Various bits of data used by the verifier and register map generator.
154 typedef struct VerifierData {
156 * The method we're working on.
158 const Method* method;
161 * Number of code units of instructions in the method. A cache of the
162 * value calculated by dvmGetMethodInsnsSize().
167 * Number of registers we track for each instruction. This is equal
168 * to the method's declared "registersSize". (Does not include the
169 * pending return value.)
174 * Instruction widths and flags, one entry per code unit.
176 InsnFlags* insnFlags;
179 * Uninitialized instance map, used for tracking the movement of
180 * objects that have been allocated but not initialized.
182 UninitInstanceMap* uninitMap;
185 * Array of RegisterLine structs, one entry per code unit. We only need
186 * entries for code units that hold the start of an "interesting"
187 * instruction. For register map generation, we're only interested
190 RegisterLine* registerLines;
193 * The number of occurrences of specific opcodes.
195 size_t newInstanceCount;
196 size_t monitorEnterCount;
199 * Array of pointers to basic blocks, one entry per code unit. Used
200 * for liveness analysis.
202 VfyBasicBlock** basicBlocks;
206 /* table with static merge logic for primitive types */
207 extern const char gDvmMergeTab[kRegTypeMAX][kRegTypeMAX];
211 * Returns "true" if the flags indicate that this address holds the start
214 INLINE bool dvmInsnIsOpcode(const InsnFlags* insnFlags, int addr) {
215 return (insnFlags[addr] & kInsnFlagWidthMask) != 0;
219 * Extract the unsigned 16-bit instruction width from "flags".
221 INLINE int dvmInsnGetWidth(const InsnFlags* insnFlags, int addr) {
222 return insnFlags[addr] & kInsnFlagWidthMask;
228 INLINE bool dvmInsnIsChanged(const InsnFlags* insnFlags, int addr) {
229 return (insnFlags[addr] & kInsnFlagChanged) != 0;
231 INLINE void dvmInsnSetChanged(InsnFlags* insnFlags, int addr, bool changed)
234 insnFlags[addr] |= kInsnFlagChanged;
236 insnFlags[addr] &= ~kInsnFlagChanged;
242 INLINE bool dvmInsnIsVisited(const InsnFlags* insnFlags, int addr) {
243 return (insnFlags[addr] & kInsnFlagVisited) != 0;
245 INLINE void dvmInsnSetVisited(InsnFlags* insnFlags, int addr, bool changed)
248 insnFlags[addr] |= kInsnFlagVisited;
250 insnFlags[addr] &= ~kInsnFlagVisited;
254 * Visited or changed?
256 INLINE bool dvmInsnIsVisitedOrChanged(const InsnFlags* insnFlags, int addr) {
257 return (insnFlags[addr] & (kInsnFlagVisited|kInsnFlagChanged)) != 0;
263 INLINE bool dvmInsnIsInTry(const InsnFlags* insnFlags, int addr) {
264 return (insnFlags[addr] & kInsnFlagInTry) != 0;
266 INLINE void dvmInsnSetInTry(InsnFlags* insnFlags, int addr, bool inTry)
270 insnFlags[addr] |= kInsnFlagInTry;
272 // insnFlags[addr] &= ~kInsnFlagInTry;
276 * Instruction is a branch target or exception handler?
278 INLINE bool dvmInsnIsBranchTarget(const InsnFlags* insnFlags, int addr) {
279 return (insnFlags[addr] & kInsnFlagBranchTarget) != 0;
281 INLINE void dvmInsnSetBranchTarget(InsnFlags* insnFlags, int addr,
286 insnFlags[addr] |= kInsnFlagBranchTarget;
288 // insnFlags[addr] &= ~kInsnFlagBranchTarget;
292 * Instruction is a GC point?
294 INLINE bool dvmInsnIsGcPoint(const InsnFlags* insnFlags, int addr) {
295 return (insnFlags[addr] & kInsnFlagGcPoint) != 0;
297 INLINE void dvmInsnSetGcPoint(InsnFlags* insnFlags, int addr,
302 insnFlags[addr] |= kInsnFlagGcPoint;
304 // insnFlags[addr] &= ~kInsnFlagGcPoint;
309 * Create a new UninitInstanceMap.
311 UninitInstanceMap* dvmCreateUninitInstanceMap(const Method* meth,
312 const InsnFlags* insnFlags, int newInstanceCount);
315 * Release the storage associated with an UninitInstanceMap.
317 void dvmFreeUninitInstanceMap(UninitInstanceMap* uninitMap);
320 * Verify bytecode in "meth". "insnFlags" should be populated with
321 * instruction widths and "in try" flags.
323 bool dvmVerifyCodeFlow(VerifierData* vdata);
325 #endif /*_DALVIK_CODEVERIFY*/