Created
January 17, 2017 09:20
-
-
Save bdqnghi/9d8d990b29caeb4e5157d7df35e083ce to your computer and use it in GitHub Desktop.
Soot example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.software.mining; | |
import polyglot.visit.DataFlow; | |
import soot.*; | |
import soot.jimple.toolkits.callgraph.CallGraph; | |
import soot.jimple.toolkits.callgraph.Edge; | |
import soot.jimple.toolkits.callgraph.Targets; | |
import soot.options.Options; | |
import soot.toolkits.graph.ExceptionalUnitGraph; | |
import soot.toolkits.graph.pdg.HashMutablePDG; | |
import soot.toolkits.graph.pdg.ProgramDependenceGraph; | |
import soot.util.cfgcmd.CFGToDotGraph; | |
import soot.util.dot.DotGraph; | |
import java.util.*; | |
public class CFG { | |
private DotGraph dot = new DotGraph("callgraph"); | |
private static HashMap<String, Boolean> visited = new | |
HashMap<String, Boolean>(); | |
public CFG() { | |
} | |
public static void main(String[] args) { | |
soot.G.reset(); | |
Options.v().set_process_dir(Collections.singletonList("/home/quocnghi/Downloads/rhino1_7R5/js.jar")); | |
// Options.v().set_prepend_classpath(true); | |
Options.v().set_src_prec(Options.src_prec_java); | |
Options.v().set_whole_program(true); | |
Options.v().set_allow_phantom_refs(true); | |
Options.v().set_output_format(Options.output_format_none); | |
Options.v().setPhaseOption("cg.spark", "verbose:false"); | |
Scene.v().loadNecessaryClasses(); | |
SootClass entryClass = Scene.v().getSootClassUnsafe("org.mozilla.javascript.tools.shell.Main"); | |
SootMethod entryMethod = entryClass.getMethodByNameUnsafe("exec"); | |
Options.v().set_main_class(entryMethod.getSignature()); | |
Scene.v().setEntryPoints(Collections.singletonList(entryMethod)); | |
PackManager.v().runPacks(); | |
CallGraph callGraph = Scene.v().getCallGraph(); | |
drawCallGraph(callGraph); | |
// drawMethodDependenceGraph(entryMethod); | |
// drawProcedureDependenceGraph(entryMethod); | |
} | |
private static void drawMethodDependenceGraph(SootMethod entryMethod){ | |
Body body = entryMethod.retrieveActiveBody(); | |
ExceptionalUnitGraph exceptionalUnitGraph = new ExceptionalUnitGraph(body); | |
CFGToDotGraph cfgForMethod = new CFGToDotGraph(); | |
cfgForMethod.drawCFG(exceptionalUnitGraph); | |
DotGraph cfgDot = cfgForMethod.drawCFG(exceptionalUnitGraph); | |
cfgDot.plot("/home/quocnghi/Downloads/graph/cfg.dot"); | |
} | |
private static void drawProcedureDependenceGraph(SootMethod entryMethod){ | |
Body body = entryMethod.retrieveActiveBody(); | |
ExceptionalUnitGraph exceptionalUnitGraph = new ExceptionalUnitGraph(body); | |
HashMutablePDG hashMutablePDG = new HashMutablePDG(exceptionalUnitGraph); | |
CFGToDotGraph pdgForMethod = new CFGToDotGraph(); | |
DotGraph pdgDot = pdgForMethod.drawCFG(hashMutablePDG,body); | |
pdgDot.plot("/home/quocnghi/Downloads/graph/pdg.dot"); | |
} | |
private static void drawCallGraph(CallGraph callGraph){ | |
DotGraph dot = new DotGraph("callgraph"); | |
Iterator<Edge> iteratorEdges = callGraph.iterator(); | |
int i = 0; | |
System.out.println("Call Graph size : "+ callGraph.size()); | |
while (iteratorEdges.hasNext()) { | |
Edge edge = iteratorEdges.next(); | |
String node_src = edge.getSrc().toString(); | |
String node_tgt = edge.getTgt().toString(); | |
dot.drawEdge(node_src, node_tgt); | |
System.out.println(i++); | |
} | |
dot.plot("/home/quocnghi/Downloads/graph/callgraph.dot"); | |
} | |
// private static void visit(CallGraph cg, SootMethod k) { | |
// String identifier = k.getName(); | |
// | |
// visited.put(k.getSignature(), true); | |
// | |
// | |
// dot.drawNode(identifier); | |
// | |
// | |
// //iterate over unvisited parents | |
// Iterator<MethodOrMethodContext> ptargets = new Targets(cg.edgesInto(k)); | |
// | |
// | |
// if (ptargets != null) { | |
// while (ptargets.hasNext()) { | |
// SootMethod p = (SootMethod) ptargets.next(); | |
// | |
// | |
// if (p == null) System.out.println("p is null"); | |
// | |
// | |
// if (!visited.containsKey(p.getSignature())) | |
// visit(cg, p); | |
// } | |
// } | |
// | |
// | |
// //iterate over unvisited children | |
// Iterator<MethodOrMethodContext> ctargets = new Targets(cg.edgesOutOf(k)); | |
// | |
// | |
// if (ctargets != null) { | |
// while (ctargets.hasNext()) { | |
// SootMethod c = (SootMethod) ctargets.next(); | |
// if (c == null) System.out.println("c is null"); | |
// dot.drawEdge(identifier, c.getName()); | |
// | |
// | |
// if (!visited.containsKey(c.getSignature())) | |
// visit(cg, c); | |
// } | |
// } | |
// } | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment