Getting started to write Dataflow analysis in Soot. (Without standard Soot templates)
- See basic steps to access Class and methods from Soot API , following Soot APIs are provided to implement Dataflow Analysis. Assuming, you have a SootMethod object as
sootMethod
, using following snippet get all the Basic Blocks in a method.
Body methodBody = sootMethod.retrieveActiveBody();
BlockGraph blockGraph = new BriefBlockGraph(methodBody);
- (Optional) Create a List object, if you want to re-order basic blocks such as Topological sort or other operations. Since, graph can not be modified directly.
List<Block> blocks = new ArrayList<Block>(blockGraph.getBlocks());
- Get Basic Blocks and iterate over the instructions.
Iterator<Block> graphIt = blockGraph.getBlocks().iterator();
while (graphIt.hasNext()) {
Block block = graphIt.next();
...
}
- Use Following API to retrieve all the successor(s) of the basic blocs. (Tip: you will get multiple successors for branch)
List<Block> successors = block.getSuccs();
- Use Following API to get all the predecessor(s) of the basic block.
List<Block> predecessors = block.getPreds();
- Next, iterate over the Block object and go through the statements.
Iterator<Unit> blockIt = block.iterator();
while (blockIt.hasNext()) {
Unit unit = blockIt.next();
...
}
- Soot Provides a special data structure FlowSet, Which provides basic set operations such as Union between two sets.
// import soot.toolkits.scalar.FlowSet;
// import soot.toolkits.scalar.ArraySparseSet;
FlowSet<T> firstSet = new ArraySparseSet<T>(); //create set object
// use firstSet.add(object); to add elements
FlowSet<T> secondSet = new ArraySparseSet<T>(); //create another object and add elements
FlowSet<T> thirdSet = new ArraySparseSet<T>(); //keep this object empty to store results
firstSet.union(secondSet, thirdSet); // writes Union (firstSet+secondSet) to thirdSet
- Similarly, intersection and difference operations can be performed.
firstSet.difference(secondSet, thirdSet); // writes difference (firstSet-secondSet) to thirdSet
firstSet.intersection(secondSet, thirdSet); // writes Common elements of both sets to thirdSet