forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEntryPoints.java
More file actions
263 lines (234 loc) · 8.36 KB
/
EntryPoints.java
File metadata and controls
263 lines (234 loc) · 8.36 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package soot;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 2003 Ondrej Lhotak
* %%
* 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 soot.dotnet.members.DotnetMethod;
import soot.options.Options;
import soot.util.NumberedString;
import soot.util.StringNumberer;
/**
* Returns the various potential entry points of a Java program.
*
* @author Ondrej Lhotak
*/
public class EntryPoints {
final NumberedString sigMain;
final NumberedString sigFinalize;
final NumberedString sigExit;
final NumberedString sigClinit;
final NumberedString sigInit;
final NumberedString sigStart;
final NumberedString sigRun;
final NumberedString sigObjRun;
final NumberedString sigForName;
public EntryPoints(Singletons.Global g) {
final StringNumberer subSigNumberer = Scene.v().getSubSigNumberer();
if (Options.v().src_prec() == Options.src_prec_dotnet) {
sigMain = subSigNumberer.findOrAdd(DotnetMethod.MAIN_METHOD_SIGNATURE);
sigFinalize = subSigNumberer.findOrAdd("void " + DotnetMethod.DESTRUCTOR_NAME + "()");
} else {
sigMain = subSigNumberer.findOrAdd(JavaMethods.SIG_MAIN);
sigFinalize = subSigNumberer.findOrAdd(JavaMethods.SIG_FINALIZE);
}
sigExit = subSigNumberer.findOrAdd(JavaMethods.SIG_EXIT);
sigClinit = subSigNumberer.findOrAdd(JavaMethods.SIG_CLINIT);
sigInit = subSigNumberer.findOrAdd(JavaMethods.SIG_INIT);
sigStart = subSigNumberer.findOrAdd(JavaMethods.SIG_START);
sigRun = subSigNumberer.findOrAdd(JavaMethods.SIG_RUN);
sigObjRun = subSigNumberer.findOrAdd(JavaMethods.SIG_OBJ_RUN);
sigForName = subSigNumberer.findOrAdd(JavaMethods.SIG_FOR_NAME);
}
public static EntryPoints v() {
return G.v().soot_EntryPoints();
}
protected void addMethod(List<SootMethod> set, SootClass cls, NumberedString methodSubSig) {
SootMethod sm = cls.getMethodUnsafe(methodSubSig);
if (sm != null) {
set.add(sm);
}
}
protected void addMethod(List<SootMethod> set, String methodSig) {
final Scene sc = Scene.v();
if (sc.containsMethod(methodSig)) {
set.add(sc.getMethod(methodSig));
}
}
/**
* Returns only the application entry points, not including entry points invoked implicitly by the VM.
*/
public List<SootMethod> application() {
List<SootMethod> ret = new ArrayList<SootMethod>();
final Scene sc = Scene.v();
if (sc.hasMainClass()) {
SootClass mainClass = sc.getMainClass();
addMethod(ret, mainClass, sigMain);
for (SootMethod clinit : clinitsOf(mainClass)) {
ret.add(clinit);
}
}
return ret;
}
/** Returns only the entry points invoked implicitly by the VM. */
public List<SootMethod> implicit() {
List<SootMethod> ret = new ArrayList<SootMethod>();
if (Options.v().src_prec() == Options.src_prec_dotnet) {
return ret;
}
addMethod(ret, JavaMethods.INITIALIZE_SYSTEM_CLASS);
addMethod(ret, JavaMethods.THREAD_GROUP_INIT);
// addMethod( ret, "<java.lang.ThreadGroup: void
// remove(java.lang.Thread)>");
addMethod(ret, JavaMethods.THREAD_EXIT);
addMethod(ret, JavaMethods.THREADGROUP_UNCAUGHT_EXCEPTION);
// addMethod( ret, "<java.lang.System: void
// loadLibrary(java.lang.String)>");
addMethod(ret, JavaMethods.CLASSLOADER_INIT);
addMethod(ret, JavaMethods.CLASSLOADER_LOAD_CLASS_INTERNAL);
addMethod(ret, JavaMethods.CLASSLOADER_CHECK_PACKAGE_ACC);
addMethod(ret, JavaMethods.CLASSLOADER_ADD_CLASS);
addMethod(ret, JavaMethods.CLASSLOADER_FIND_NATIVE);
addMethod(ret, JavaMethods.PRIV_ACTION_EXC_INIT);
// addMethod( ret, "<java.lang.ref.Finalizer: void
// register(java.lang.Object)>");
addMethod(ret, JavaMethods.RUN_FINALIZE);
addMethod(ret, JavaMethods.THREAD_INIT_RUNNABLE);
addMethod(ret, JavaMethods.THREAD_INIT_STRING);
return ret;
}
/** Returns all the entry points. */
public List<SootMethod> all() {
List<SootMethod> ret = new ArrayList<SootMethod>();
ret.addAll(application());
ret.addAll(implicit());
return ret;
}
/** Returns a list of all static initializers. */
public List<SootMethod> clinits() {
List<SootMethod> ret = new ArrayList<SootMethod>();
for (SootClass cl : Scene.v().getClasses()) {
addMethod(ret, cl, sigClinit);
}
return ret;
}
/** Returns a list of all constructors taking no arguments. */
public List<SootMethod> inits() {
List<SootMethod> ret = new ArrayList<SootMethod>();
for (SootClass cl : Scene.v().getClasses()) {
addMethod(ret, cl, sigInit);
}
return ret;
}
/** Returns a list of all constructors. */
public List<SootMethod> allInits() {
List<SootMethod> ret = new ArrayList<SootMethod>();
for (SootClass cl : Scene.v().getClasses()) {
for (SootMethod m : cl.getMethods()) {
if ("<init>".equals(m.getName())) {
ret.add(m);
}
}
}
return ret;
}
/** Returns a list of all concrete methods of all application classes. */
public List<SootMethod> methodsOfApplicationClasses() {
List<SootMethod> ret = new ArrayList<SootMethod>();
for (SootClass cl : Scene.v().getApplicationClasses()) {
for (SootMethod m : cl.getMethods()) {
if (m.isConcrete()) {
ret.add(m);
}
}
}
return ret;
}
/**
* Returns a list of all concrete main(String[]) methods of all application classes.
*/
public List<SootMethod> mainsOfApplicationClasses() {
List<SootMethod> ret = new ArrayList<SootMethod>();
for (SootClass cl : Scene.v().getApplicationClasses()) {
SootMethod m
= Options.v().src_prec() == Options.src_prec_dotnet ? cl.getMethodUnsafe(DotnetMethod.MAIN_METHOD_SIGNATURE)
: cl.getMethodUnsafe("void main(java.lang.String[])");
if (m != null && m.isConcrete()) {
ret.add(m);
}
}
return ret;
}
/** Returns a list of all clinits of class cl and its superclasses. */
public Iterable<SootMethod> clinitsOf(SootClass cl) {
// Do not create an actual list, since this method gets called quite often
// Instead, callers usually just want to iterate over the result.
SootMethod init = cl.getMethodUnsafe(sigClinit);
SootClass superClass = cl.getSuperclassUnsafe();
// check super classes until finds a constructor or no super class there anymore.
while (init == null && superClass != null) {
init = superClass.getMethodUnsafe(sigClinit);
superClass = superClass.getSuperclassUnsafe();
}
if (init == null) {
return Collections.emptyList();
}
SootMethod initStart = init;
return new Iterable<SootMethod>() {
@Override
public Iterator<SootMethod> iterator() {
return new Iterator<SootMethod>() {
SootMethod current = initStart;
@Override
public SootMethod next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
SootMethod n = current;
// Pre-fetch the next element
current = null;
SootClass currentClass = n.getDeclaringClass();
while (true) {
SootClass superClass = currentClass.getSuperclassUnsafe();
if (superClass == null) {
break;
}
SootMethod m = superClass.getMethodUnsafe(sigClinit);
if (m != null) {
current = m;
break;
}
currentClass = superClass;
}
return n;
}
@Override
public boolean hasNext() {
return current != null;
}
};
}
};
}
}