forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDexMethod.java
More file actions
174 lines (153 loc) · 6.03 KB
/
DexMethod.java
File metadata and controls
174 lines (153 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package soot.dexpler;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 2012 Michael Markert, Frank Hartmann
*
* (c) 2012 University of Luxembourg - Interdisciplinary Centre for
* Security Reliability and Trust (SnT) - All rights reserved
* Alexandre Bartel
*
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
import java.util.ArrayList;
import java.util.List;
import org.jf.dexlib2.iface.Annotation;
import org.jf.dexlib2.iface.AnnotationElement;
import org.jf.dexlib2.iface.DexFile;
import org.jf.dexlib2.iface.Method;
import org.jf.dexlib2.iface.MultiDexContainer.DexEntry;
import org.jf.dexlib2.iface.value.ArrayEncodedValue;
import org.jf.dexlib2.iface.value.EncodedValue;
import org.jf.dexlib2.iface.value.TypeEncodedValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import soot.Body;
import soot.MethodSource;
import soot.Modifier;
import soot.Scene;
import soot.SootClass;
import soot.SootMethod;
import soot.SootResolver;
import soot.Type;
import soot.jimple.Jimple;
import soot.jimple.toolkits.typing.TypeAssigner;
import soot.options.Options;
/**
* DexMethod is a container for all methods that are declared in a class. It holds information about its name, the class it
* belongs to, its access flags, thrown exceptions, the return type and parameter types as well as the encoded method itself.
*
*/
public class DexMethod {
private static final Logger logger = LoggerFactory.getLogger(DexMethod.class);
protected final DexEntry<? extends DexFile> dexEntry;
protected final SootClass declaringClass;
public DexMethod(final DexEntry<? extends DexFile> dexFile, final SootClass declaringClass) {
this.dexEntry = dexFile;
this.declaringClass = declaringClass;
}
/**
* Retrieve the SootMethod equivalent of this method
*
* @return the SootMethod of this method
*/
public SootMethod makeSootMethod(final Method method) {
int accessFlags = method.getAccessFlags();
// get the name of the method
String name = method.getName();
List<SootClass> thrownExceptions = getThrownExceptions(method);
List<Type> parameterTypes = getParameterTypes(method);
// retrieve the return type of this method
Type returnType = DexType.toSoot(method.getReturnType());
// Build soot method by all available parameters
SootMethod sm = declaringClass.getMethodUnsafe(name, parameterTypes, returnType);
if (sm == null) {
sm = Scene.v().makeSootMethod(name, parameterTypes, returnType, accessFlags, thrownExceptions);
}
// if the method is abstract or native, no code needs to be transformed
int flags = method.getAccessFlags();
if (Modifier.isAbstract(flags) || Modifier.isNative(flags)
|| (Options.v().oaat() && declaringClass.resolvingLevel() <= SootClass.SIGNATURES)) {
return sm;
}
// sets the method source by adding its body as the active body
sm.setSource(createMethodSource(method));
return sm;
}
protected MethodSource createMethodSource(final Method method) {
return new MethodSource() {
@Override
public Body getBody(SootMethod m, String phaseName) {
Body b = Jimple.v().newBody(m);
try {
// add the body of this code item
DexBody dexBody = new DexBody(dexEntry, method, declaringClass.getType());
dexBody.jimplify(b, m);
} catch (InvalidDalvikBytecodeException e) {
String msg = "Warning: Invalid bytecode in method " + m + ": " + e;
logger.debug("" + msg);
Util.emptyBody(b);
Util.addExceptionAfterUnit(b, "java.lang.RuntimeException", b.getUnits().getLast(),
"Soot has detected that this method contains invalid Dalvik bytecode,"
+ " which would have throw an exception at runtime. [" + msg + "]");
TypeAssigner.v().transform(b);
}
m.setActiveBody(b);
return m.getActiveBody();
}
};
}
protected List<Type> getParameterTypes(final Method method) {
// retrieve all parameter types
List<Type> parameterTypes = new ArrayList<Type>();
if (method.getParameters() != null) {
List<? extends CharSequence> parameters = method.getParameterTypes();
for (CharSequence t : parameters) {
Type type = DexType.toSoot(t.toString());
parameterTypes.add(type);
}
}
return parameterTypes;
}
protected List<SootClass> getThrownExceptions(final Method method) {
// the following snippet retrieves all exceptions that this method
// throws by analyzing its annotations
List<SootClass> thrownExceptions = new ArrayList<SootClass>();
for (Annotation a : method.getAnnotations()) {
Type atype = DexType.toSoot(a.getType());
String atypes = atype.toString();
if (!(atypes.equals("dalvik.annotation.Throws"))) {
continue;
}
for (AnnotationElement ae : a.getElements()) {
EncodedValue ev = ae.getValue();
if (ev instanceof ArrayEncodedValue) {
for (EncodedValue evSub : ((ArrayEncodedValue) ev).getValue()) {
if (evSub instanceof TypeEncodedValue) {
TypeEncodedValue valueType = (TypeEncodedValue) evSub;
String exceptionName = valueType.getValue();
String dottedName = Util.dottedClassName(exceptionName);
thrownExceptions.add(SootResolver.v().makeClassRef(dottedName));
}
}
}
}
}
return thrownExceptions;
}
}