VerifySubs.cpp revision 60fc806b679a3655c228b4093058c59941a49cfe
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 verification subroutines.
19 */
20#include "Dalvik.h"
21#include "analysis/CodeVerify.h"
22#include "libdex/InstrUtils.h"
23
24
25/*
26 * This is used when debugging to apply a magnifying glass to the
27 * verification of a particular method.
28 */
29bool dvmWantVerboseVerification(const Method* meth)
30{
31    return false;       /* COMMENT OUT to enable verbose debugging */
32
33    const char* cd = "Lcom/android/server/am/ActivityManagerService;";
34    const char* mn = "trimApplications";
35    const char* sg = "()V";
36    return (strcmp(meth->clazz->descriptor, cd) == 0 &&
37            dvmCompareNameDescriptorAndMethod(mn, sg, meth) == 0);
38}
39
40/*
41 * Output a code verifier warning message.  For the pre-verifier it's not
42 * a big deal if something fails (and it may even be expected), but if
43 * we're doing just-in-time verification it's significant.
44 */
45void dvmLogVerifyFailure(const Method* meth, const char* format, ...)
46{
47    va_list ap;
48    int logLevel;
49
50    if (gDvm.optimizing) {
51        return;
52        //logLevel = ANDROID_LOG_DEBUG;
53    } else {
54        logLevel = ANDROID_LOG_WARN;
55    }
56
57    va_start(ap, format);
58    LOG_PRI_VA(logLevel, LOG_TAG, format, ap);
59    if (meth != NULL) {
60        char* desc = dexProtoCopyMethodDescriptor(&meth->prototype);
61        LOG_PRI(logLevel, LOG_TAG, "VFY:  rejected %s.%s %s",
62            meth->clazz->descriptor, meth->name, desc);
63        free(desc);
64    }
65}
66
67/*
68 * Show a relatively human-readable message describing the failure to
69 * resolve a class.
70 *
71 * TODO: this is somewhat misleading when resolution fails because of
72 * illegal access rather than nonexistent class.
73 */
74void dvmLogUnableToResolveClass(const char* missingClassDescr,
75    const Method* meth)
76{
77    if (gDvm.optimizing)
78        return;
79
80    char* dotMissingClass = dvmHumanReadableDescriptor(missingClassDescr);
81    char* dotFromClass = dvmHumanReadableDescriptor(meth->clazz->descriptor);
82    //char* methodDescr = dexProtoCopyMethodDescriptor(&meth->prototype);
83
84    LOGE("Could not find class '%s', referenced from method %s.%s",
85        dotMissingClass, dotFromClass, meth->name/*, methodDescr*/);
86
87    free(dotMissingClass);
88    free(dotFromClass);
89    //free(methodDescr);
90}
91
92/*
93 * Extract the relative offset from a branch instruction.
94 *
95 * Returns "false" on failure (e.g. this isn't a branch instruction).
96 */
97bool dvmGetBranchOffset(const Method* meth, const InsnFlags* insnFlags,
98    int curOffset, s4* pOffset, bool* pConditional)
99{
100    const u2* insns = meth->insns + curOffset;
101
102    switch (*insns & 0xff) {
103    case OP_GOTO:
104        *pOffset = ((s2) *insns) >> 8;
105        *pConditional = false;
106        break;
107    case OP_GOTO_32:
108        *pOffset = insns[1] | (((u4) insns[2]) << 16);
109        *pConditional = false;
110        break;
111    case OP_GOTO_16:
112        *pOffset = (s2) insns[1];
113        *pConditional = false;
114        break;
115    case OP_IF_EQ:
116    case OP_IF_NE:
117    case OP_IF_LT:
118    case OP_IF_GE:
119    case OP_IF_GT:
120    case OP_IF_LE:
121    case OP_IF_EQZ:
122    case OP_IF_NEZ:
123    case OP_IF_LTZ:
124    case OP_IF_GEZ:
125    case OP_IF_GTZ:
126    case OP_IF_LEZ:
127        *pOffset = (s2) insns[1];
128        *pConditional = true;
129        break;
130    default:
131        return false;
132        break;
133    }
134
135    return true;
136}
137