This repository has been archived by the owner on Jul 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
ExampleMain.java
175 lines (157 loc) · 7.05 KB
/
ExampleMain.java
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
/*******************************************************************************
* Copyright (c) 2018 Fraunhofer IEM, Paderborn, Germany.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Johannes Spaeth - initial API and implementation
*******************************************************************************/
package boomerang.example;
import boomerang.BackwardQuery;
import boomerang.Boomerang;
import boomerang.DefaultBoomerangOptions;
import boomerang.callgraph.ObservableDynamicICFG;
import boomerang.callgraph.ObservableICFG;
import boomerang.jimple.Statement;
import boomerang.jimple.Val;
import boomerang.preanalysis.BoomerangPretransformer;
import boomerang.results.BackwardBoomerangResults;
import boomerang.seedfactory.SeedFactory;
import boomerang.seedfactory.SimpleSeedFactory;
import soot.*;
import soot.jimple.Stmt;
import soot.jimple.toolkits.callgraph.ReachableMethods;
import soot.options.Options;
import soot.util.queue.QueueReader;
import wpds.impl.Weight.NoWeight;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class ExampleMain {
public static void main(String... args) {
String sootClassPath = getSootClassPath();
String mainClass = "boomerang.example.BoomerangExampleTarget";
setupSoot(sootClassPath, mainClass);
analyze();
}
private static String getSootClassPath() {
// Assume target folder to be directly in user dir; this should work in eclipse
String sootClassPath = System.getProperty("user.dir") + File.separator + "target" + File.separator + "classes";
File classPathDir = new File(sootClassPath);
if (!classPathDir.exists()) {
// We haven't found our target folder
// Check if if it is in the boomerangPDS in user dir; this should work in IntelliJ
sootClassPath = System.getProperty("user.dir") + File.separator + "boomerangPDS" + File.separator + "target"
+ File.separator + "classes";
classPathDir = new File(sootClassPath);
if (!classPathDir.exists()) {
// We haven't found our bytecode anyway, notify now instead of starting analysis anyway
throw new RuntimeException("Classpath could not be found.");
}
}
return sootClassPath;
}
private static void setupSoot(String sootClassPath, String mainClass) {
G.v().reset();
Options.v().set_whole_program(true);
Options.v().setPhaseOption("cg.spark", "on");
Options.v().set_output_format(Options.output_format_none);
Options.v().set_no_bodies_for_excluded(true);
Options.v().set_allow_phantom_refs(true);
List<String> includeList = new LinkedList<String>();
includeList.add("java.lang.*");
includeList.add("java.util.*");
includeList.add("java.io.*");
includeList.add("sun.misc.*");
includeList.add("java.net.*");
includeList.add("javax.servlet.*");
includeList.add("javax.crypto.*");
Options.v().set_include(includeList);
Options.v().setPhaseOption("jb", "use-original-names:true");
Options.v().set_soot_classpath(sootClassPath);
Options.v().set_prepend_classpath(true);
// Options.v().set_main_class(this.getTargetClass());
Scene.v().loadNecessaryClasses();
SootClass c = Scene.v().forceResolve(mainClass, SootClass.BODIES);
if (c != null) {
c.setApplicationClass();
}
for (SootMethod m : c.getMethods()) {
System.out.println(m);
}
}
private static void analyze() {
Transform transform = new Transform("wjtp.ifds", createAnalysisTransformer());
PackManager.v().getPack("wjtp").add(transform);
PackManager.v().getPack("cg").apply();
BoomerangPretransformer.v().apply();
PackManager.v().getPack("wjtp").apply();
}
private static Transformer createAnalysisTransformer() {
return new SceneTransformer() {
protected void internalTransform(String phaseName, @SuppressWarnings("rawtypes") Map options) {
// 1. Create a Boomerang solver.
Boomerang solver = new Boomerang(new DefaultBoomerangOptions() {
@Override
public boolean onTheFlyCallGraph() {
// Must be turned off if no SeedFactory is specified.
return false;
}
}) {
ObservableICFG<Unit, SootMethod> icfg = new ObservableDynamicICFG(this);
@Override
public ObservableICFG<Unit, SootMethod> icfg() {
return icfg;
}
@Override
public SeedFactory<NoWeight> getSeedFactory() {
return null;
}
};
// 2. Submit a query to the solver.
BackwardQuery query = createQuery();
System.out.println("Solving query: " + query);
BackwardBoomerangResults<NoWeight> backwardQueryResults = solver.solve(query);
solver.debugOutput();
System.out.println("All allocation sites of the query variable are:");
System.out.println(backwardQueryResults.getAllocationSites());
System.out.println("All aliasing access path of the query variable are:");
System.out.println(backwardQueryResults.getAllAliases());
}
private BackwardQuery createQuery() {
ReachableMethods reachableMethods = Scene.v().getReachableMethods();
QueueReader<MethodOrMethodContext> l = reachableMethods.listener();
while (l.hasNext()) {
MethodOrMethodContext next = l.next();
BackwardQuery q = getQuery(next.method());
if (q != null)
return q;
}
throw new RuntimeException("No method found that contains a call to method queryFor!");
}
private BackwardQuery getQuery(SootMethod method) {
if (!method.hasActiveBody()) {
return null;
}
for (Unit u : method.getActiveBody().getUnits()) {
if (!(u instanceof Stmt)) {
continue;
}
Stmt s = (Stmt) u;
if (!s.containsInvokeExpr()) {
continue;
}
if (s.toString().contains("queryFor")) {
Value arg = s.getInvokeExpr().getArg(0);
return new BackwardQuery(new Statement(s, method), new Val(arg, method));
}
}
return null;
}
};
}
}