-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathHelloSoot.java
76 lines (65 loc) · 2.63 KB
/
HelloSoot.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
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package dev.navids.soottutorial.hellosoot;
import dev.navids.soottutorial.visual.Visualizer;
import soot.*;
import soot.jimple.JimpleBody;
import soot.jimple.internal.JIfStmt;
import soot.options.Options;
import soot.toolkits.graph.ClassicCompleteUnitGraph;
import soot.toolkits.graph.UnitGraph;
import java.io.File;
public class HelloSoot {
public static String sourceDirectory = System.getProperty("user.dir") + File.separator + "demo" + File.separator + "HelloSoot";
public static String clsName = "FizzBuzz";
public static String methodName = "printFizzBuzz";
public static void setupSoot() {
G.reset();
Options.v().set_prepend_classpath(true);
Options.v().set_allow_phantom_refs(true);
Options.v().set_soot_classpath(sourceDirectory);
SootClass sc = Scene.v().loadClassAndSupport(clsName);
sc.setApplicationClass();
Scene.v().loadNecessaryClasses();
}
public static void main(String[] args) {
setupSoot();
// Retrieve printFizzBuzz's body
SootClass mainClass = Scene.v().getSootClass(clsName);
SootMethod sm = mainClass.getMethodByName(methodName);
JimpleBody body = (JimpleBody) sm.retrieveActiveBody();
// Print some information about printFizzBuzz
System.out.println("Method Signature: " + sm.getSignature());
System.out.println("--------------");
System.out.println("Argument(s):");
for (Local l : body.getParameterLocals()) {
System.out.println(l.getName() + " : " + l.getType());
}
System.out.println("--------------");
System.out.println("This: " + body.getThisLocal());
System.out.println("--------------");
System.out.println("Units:");
int c = 1;
for (Unit u : body.getUnits()) {
System.out.println("(" + c + ") " + u.toString());
c++;
}
System.out.println("--------------");
// Print statements that have branch conditions
System.out.println("Branch Statements:");
for (Unit u : body.getUnits()) {
if (u instanceof JIfStmt)
System.out.println(u.toString());
}
// Draw the control-flow graph of the method if 'draw' is provided in arguments
boolean drawGraph = false;
if (args.length > 0 && args[0].equals("draw"))
drawGraph = true;
if (drawGraph) {
UnitGraph ug = new ClassicCompleteUnitGraph(sm.getActiveBody());
Visualizer.v().addUnitGraph(ug);
Visualizer.v().draw();
}
}
}