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 verification subroutines.
21 #include "analysis/CodeVerify.h"
22 #include "libdex/DexCatch.h"
23 #include "libdex/InstrUtils.h"
27 * Compute the width of the instruction at each address in the instruction
28 * stream. Addresses that are in the middle of an instruction, or that
29 * are part of switch table data, are not set (so the caller should probably
30 * initialize "insnFlags" to zero).
32 * If "pNewInstanceCount" is not NULL, it will be set to the number of
33 * new-instance instructions in the method.
35 * Logs an error and returns "false" on failure.
37 bool dvmComputeCodeWidths(const Method* meth, InsnFlags* insnFlags,
38 int* pNewInstanceCount)
40 const int insnCount = dvmGetMethodInsnsSize(meth);
41 const u2* insns = meth->insns;
43 int newInstanceCount = 0;
47 for (i = 0; i < insnCount; /**/) {
51 * Switch tables and array data tables are identified with
52 * "extended NOP" opcodes. They contain no executable code,
53 * so we can just skip past them.
55 if (*insns == kPackedSwitchSignature) {
56 width = 4 + insns[1] * 2;
57 } else if (*insns == kSparseSwitchSignature) {
58 width = 2 + insns[1] * 4;
59 } else if (*insns == kArrayDataSignature) {
60 u4 size = insns[2] | (((u4)insns[3]) << 16);
61 width = 4 + (insns[1] * size + 1) / 2;
63 int instr = *insns & 0xff;
64 width = dexGetInstrWidthAbs(gDvm.instrWidth, instr);
67 "VFY: invalid post-opt instruction (0x%x)\n", instr);
70 if (width < 0 || width > 5) {
71 LOGE("VFY: bizarre width value %d\n", width);
75 if (instr == OP_NEW_INSTANCE)
80 LOG_VFY_METH(meth, "VFY: insane width %d\n", width);
84 insnFlags[i] |= width;
88 if (i != (int) dvmGetMethodInsnsSize(meth)) {
89 LOG_VFY_METH(meth, "VFY: code did not end where expected (%d vs. %d)\n",
90 i, dvmGetMethodInsnsSize(meth));
95 if (pNewInstanceCount != NULL)
96 *pNewInstanceCount = newInstanceCount;
103 * Set the "in try" flags for all instructions protected by "try" statements.
104 * Also sets the "branch target" flags for exception handlers.
106 * Call this after widths have been set in "insnFlags".
108 * Returns "false" if something in the exception table looks fishy, but
109 * we're expecting the exception table to be somewhat sane.
111 bool dvmSetTryFlags(const Method* meth, InsnFlags* insnFlags)
113 u4 insnsSize = dvmGetMethodInsnsSize(meth);
114 DexFile* pDexFile = meth->clazz->pDvmDex->pDexFile;
115 const DexCode* pCode = dvmGetMethodCode(meth);
116 u4 triesSize = pCode->triesSize;
117 const DexTry* pTries;
122 if (triesSize == 0) {
126 pTries = dexGetTries(pCode);
127 handlersSize = dexGetHandlersSize(pCode);
129 for (i = 0; i < triesSize; i++) {
130 const DexTry* pTry = &pTries[i];
131 u4 start = pTry->startAddr;
132 u4 end = start + pTry->insnCount;
135 if ((start >= end) || (start >= insnsSize) || (end > insnsSize)) {
137 "VFY: bad exception entry: startAddr=%d endAddr=%d (size=%d)\n",
138 start, end, insnsSize);
142 if (dvmInsnGetWidth(insnFlags, start) == 0) {
144 "VFY: 'try' block starts inside an instruction (%d)\n",
149 for (addr = start; addr < end;
150 addr += dvmInsnGetWidth(insnFlags, addr))
152 assert(dvmInsnGetWidth(insnFlags, addr) != 0);
153 dvmInsnSetInTry(insnFlags, addr, true);
157 /* Iterate over each of the handlers to verify target addresses. */
158 offset = dexGetFirstHandlerOffset(pCode);
159 for (i = 0; i < handlersSize; i++) {
160 DexCatchIterator iterator;
161 dexCatchIteratorInit(&iterator, pCode, offset);
164 DexCatchHandler* handler = dexCatchIteratorNext(&iterator);
167 if (handler == NULL) {
171 addr = handler->address;
172 if (dvmInsnGetWidth(insnFlags, addr) == 0) {
174 "VFY: exception handler starts at bad address (%d)\n",
179 dvmInsnSetBranchTarget(insnFlags, addr, true);
182 offset = dexCatchIteratorGetEndOffset(&iterator, pCode);
189 * Verify a switch table. "curOffset" is the offset of the switch
192 bool dvmCheckSwitchTargets(const Method* meth, InsnFlags* insnFlags,
195 const int insnCount = dvmGetMethodInsnsSize(meth);
196 const u2* insns = meth->insns + curOffset;
197 const u2* switchInsns;
198 u2 expectedSignature;
199 int switchCount, tableSize;
200 int offsetToSwitch, offsetToKeys, offsetToTargets, targ;
201 int offset, absOffset;
203 assert(curOffset >= 0 && curOffset < insnCount);
205 /* make sure the start of the switch is in range */
206 offsetToSwitch = (s2) insns[1];
207 if (curOffset + offsetToSwitch < 0 ||
208 curOffset + offsetToSwitch + 2 >= insnCount)
211 "VFY: invalid switch start: at %d, switch offset %d, count %d\n",
212 curOffset, offsetToSwitch, insnCount);
216 /* offset to switch table is a relative branch-style offset */
217 switchInsns = insns + offsetToSwitch;
219 /* make sure the table is 32-bit aligned */
220 if ((((u4) switchInsns) & 0x03) != 0) {
222 "VFY: unaligned switch table: at %d, switch offset %d\n",
223 curOffset, offsetToSwitch);
227 switchCount = switchInsns[1];
229 if ((*insns & 0xff) == OP_PACKED_SWITCH) {
230 /* 0=sig, 1=count, 2/3=firstKey */
233 expectedSignature = kPackedSwitchSignature;
235 /* 0=sig, 1=count, 2..count*2 = keys */
237 offsetToTargets = 2 + 2*switchCount;
238 expectedSignature = kSparseSwitchSignature;
240 tableSize = offsetToTargets + switchCount*2;
242 if (switchInsns[0] != expectedSignature) {
244 "VFY: wrong signature for switch table (0x%04x, wanted 0x%04x)\n",
245 switchInsns[0], expectedSignature);
249 /* make sure the end of the switch is in range */
250 if (curOffset + offsetToSwitch + tableSize > insnCount) {
252 "VFY: invalid switch end: at %d, switch offset %d, end %d, count %d\n",
253 curOffset, offsetToSwitch, curOffset + offsetToSwitch + tableSize,
258 /* for a sparse switch, verify the keys are in ascending order */
259 if (offsetToKeys > 0 && switchCount > 1) {
262 lastKey = switchInsns[offsetToKeys] |
263 (switchInsns[offsetToKeys+1] << 16);
264 for (targ = 1; targ < switchCount; targ++) {
265 s4 key = (s4) switchInsns[offsetToKeys + targ*2] |
266 (s4) (switchInsns[offsetToKeys + targ*2 +1] << 16);
267 if (key <= lastKey) {
269 "VFY: invalid packed switch: last key=%d, this=%d\n",
278 /* verify each switch target */
279 for (targ = 0; targ < switchCount; targ++) {
280 offset = (s4) switchInsns[offsetToTargets + targ*2] |
281 (s4) (switchInsns[offsetToTargets + targ*2 +1] << 16);
282 absOffset = curOffset + offset;
284 if (absOffset < 0 || absOffset >= insnCount ||
285 !dvmInsnIsOpcode(insnFlags, absOffset))
288 "VFY: invalid switch target %d (-> 0x%x) at 0x%x[%d]\n",
289 offset, absOffset, curOffset, targ);
292 dvmInsnSetBranchTarget(insnFlags, absOffset, true);
299 * Verify that the target of a branch instruction is valid.
301 * We don't expect code to jump directly into an exception handler, but
302 * it's valid to do so as long as the target isn't a "move-exception"
303 * instruction. We verify that in a later stage.
305 * The VM spec doesn't forbid an instruction from branching to itself,
306 * but the Dalvik spec declares that only certain instructions can do so.
308 bool dvmCheckBranchTarget(const Method* meth, InsnFlags* insnFlags,
309 int curOffset, bool selfOkay)
311 const int insnCount = dvmGetMethodInsnsSize(meth);
312 const u2* insns = meth->insns + curOffset;
313 int offset, absOffset;
316 if (!dvmGetBranchTarget(meth, insnFlags, curOffset, &offset,
320 if (!selfOkay && offset == 0) {
321 LOG_VFY_METH(meth, "VFY: branch offset of zero not allowed at 0x%x\n",
327 * Check for 32-bit overflow. This isn't strictly necessary if we can
328 * depend on the VM to have identical "wrap-around" behavior, but
329 * it's unwise to depend on that.
331 if (((s8) curOffset + (s8) offset) != (s8)(curOffset + offset)) {
332 LOG_VFY_METH(meth, "VFY: branch target overflow 0x%x +%d\n",
336 absOffset = curOffset + offset;
337 if (absOffset < 0 || absOffset >= insnCount ||
338 !dvmInsnIsOpcode(insnFlags, absOffset))
341 "VFY: invalid branch target %d (-> 0x%x) at 0x%x\n",
342 offset, absOffset, curOffset);
345 dvmInsnSetBranchTarget(insnFlags, absOffset, true);
352 * Output a code verifier warning message. For the pre-verifier it's not
353 * a big deal if something fails (and it may even be expected), but if
354 * we're doing just-in-time verification it's significant.
356 void dvmLogVerifyFailure(const Method* meth, const char* format, ...)
361 if (gDvm.optimizing) {
363 //logLevel = ANDROID_LOG_DEBUG;
365 logLevel = ANDROID_LOG_WARN;
368 va_start(ap, format);
369 LOG_PRI_VA(logLevel, LOG_TAG, format, ap);
371 char* desc = dexProtoCopyMethodDescriptor(&meth->prototype);
372 LOG_PRI(logLevel, LOG_TAG, "VFY: rejected %s.%s %s\n",
373 meth->clazz->descriptor, meth->name, desc);
379 * Show a relatively human-readable message describing the failure to
382 * TODO: this is somewhat misleading when resolution fails because of
383 * illegal access rather than nonexistent class.
385 void dvmLogUnableToResolveClass(const char* missingClassDescr,
391 char* dotMissingClass = dvmDescriptorToDot(missingClassDescr);
392 char* dotFromClass = dvmDescriptorToDot(meth->clazz->descriptor);
393 //char* methodDescr = dexProtoCopyMethodDescriptor(&meth->prototype);
395 LOGE("Could not find class '%s', referenced from method %s.%s\n",
396 dotMissingClass, dotFromClass, meth->name/*, methodDescr*/);
398 free(dotMissingClass);
404 * Extract the relative offset from a branch instruction.
406 * Returns "false" on failure (e.g. this isn't a branch instruction).
408 bool dvmGetBranchTarget(const Method* meth, InsnFlags* insnFlags,
409 int curOffset, int* pOffset, bool* pConditional)
411 const u2* insns = meth->insns + curOffset;
414 switch (*insns & 0xff) {
416 *pOffset = ((s2) *insns) >> 8;
417 *pConditional = false;
420 *pOffset = insns[1] | (((u4) insns[2]) << 16);
421 *pConditional = false;
424 *pOffset = (s2) insns[1];
425 *pConditional = false;
439 *pOffset = (s2) insns[1];
440 *pConditional = true;
451 * Given a 32-bit constant, return the most-restricted RegType enum entry
452 * that can hold the value.
454 char dvmDetermineCat1Const(s4 value)
457 return kRegTypeInteger;
458 else if (value < -128)
459 return kRegTypeShort;
466 else if (value < 128)
467 return kRegTypePosByte;
468 else if (value < 32768)
469 return kRegTypePosShort;
470 else if (value < 65536)
473 return kRegTypeInteger;