use of soot.Unit in project soot by Sable.
the class Region method getUnits.
public List<Unit> getUnits() {
if (this.m_units == null) {
this.m_units = new LinkedList<Unit>();
for (Iterator<Block> itr = this.m_blocks.iterator(); itr.hasNext(); ) {
Block b = itr.next();
for (Iterator<Unit> itr1 = b.iterator(); itr1.hasNext(); ) {
Unit u = itr1.next();
((LinkedList<Unit>) this.m_units).addLast(u);
}
}
}
return this.m_units;
}
use of soot.Unit in project soot by Sable.
the class HashChain method insertOnEdge.
/**
* Inserts instrumentation in a manner such that the resulting control flow
* graph (CFG) of the program will contain <code>toInsert</code> on an edge
* that is defined by <code>point_source</code> and
* <code>point_target</code>.
*
* @param toInsert
* instrumentation to be added in the Chain
* @param point_src
* the source point of an edge in CFG
* @param point_tgt
* the target point of an edge
*/
public void insertOnEdge(Collection<? extends E> toInsert, E point_src, E point_tgt) {
if (toInsert == null)
throw new RuntimeException("Bad idea! You tried to insert " + "a null object into a Chain!");
// is null
if (point_src == null && point_tgt != null) {
((Unit) point_tgt).redirectJumpsToThisTo((Unit) toInsert.iterator().next());
insertBefore(toInsert, point_tgt);
return;
}
// is null
if (point_src != null && point_tgt == null) {
insertAfter(toInsert, point_src);
return;
}
// Throw an exception if both source and target is null
if (point_src == null && point_tgt == null) {
throw new RuntimeException("insertOnEdge failed! Both source and target points are null.");
}
// 2- Insert 'toInsert' after 'source' in Chain
if (getSuccOf(point_src) == point_tgt) {
List<UnitBox> boxes = ((Unit) point_src).getUnitBoxes();
for (UnitBox box : boxes) {
if (box.getUnit() == point_tgt) {
box.setUnit((Unit) toInsert.iterator().next());
}
}
insertAfter(toInsert, point_src);
return;
}
// If the target is not right after the source in chain then,
// 1- Redirect all jumps (if any) from 'source' to 'target', to
// 'toInsert[0]'
// (source->target) ==> (source->toInsert[0])
// 1.1- if there are no jumps from source to target, then such an edge
// does not exist. Throw an exception.
// 2- Insert 'toInsert' before 'target' in Chain
// 3- If required, add a 'goto target' statement so that no other edge
// executes 'toInsert'
boolean validEdgeFound = false;
E originalPred = getPredOf(point_tgt);
List<UnitBox> boxes = ((Unit) point_src).getUnitBoxes();
for (UnitBox box : boxes) {
if (box.getUnit() == point_tgt) {
if (point_src instanceof GotoStmt) {
box.setUnit((Unit) toInsert.iterator().next());
insertAfter(toInsert, point_src);
E goto_unit = (E) new JGotoStmt((Unit) point_tgt);
if (toInsert instanceof List) {
List l = ((List) toInsert);
insertAfter(goto_unit, (E) l.get(l.size() - 1));
} else
insertAfter(goto_unit, (E) toInsert.toArray()[toInsert.size() - 1]);
return;
}
box.setUnit((Unit) toInsert.iterator().next());
validEdgeFound = true;
}
}
if (validEdgeFound) {
insertBefore(toInsert, point_tgt);
if (originalPred != point_src) {
if (originalPred instanceof GotoStmt)
return;
E goto_unit = (E) new JGotoStmt((Unit) point_tgt);
insertBefore(goto_unit, (E) toInsert.iterator().next());
}
return;
}
// The following code handles such scenarios.
if (getSuccOf(point_src) instanceof GotoStmt) {
if (((Unit) getSuccOf(point_src)).getUnitBoxes().get(0).getUnit() == point_tgt) {
((Unit) getSuccOf(point_src)).redirectJumpsToThisTo((Unit) toInsert.iterator().next());
insertBefore(toInsert, getSuccOf(point_src));
return;
}
}
// Return an exception.
throw new RuntimeException("insertOnEdge failed! No such edge found. The edge on which you want to insert an instrumentation is invalid.");
}
use of soot.Unit in project soot by Sable.
the class CFGViewer method print_cfg.
protected void print_cfg(Body body) {
DirectedGraph<Unit> graph = graphtype.buildGraph(body);
DotGraph canvas = graphtype.drawGraph(drawer, graph, body);
String methodname = body.getMethod().getSubSignature();
String classname = body.getMethod().getDeclaringClass().getName().replaceAll("\\$", "\\.");
String filename = soot.SourceLocator.v().getOutputDir();
if (filename.length() > 0) {
filename = filename + java.io.File.separator;
}
filename = filename + classname + " " + methodname.replace(java.io.File.separatorChar, '.') + DotGraph.DOT_EXTENSION;
logger.debug("Generate dot file in " + filename);
canvas.plot(filename);
}
use of soot.Unit in project soot by Sable.
the class UnitThrowAnalysisTest method testJTableSwitchStmt.
@Test
public void testJTableSwitchStmt() {
Stmt target = Jimple.v().newAssignStmt(Jimple.v().newLocal("local0", IntType.v()), IntConstant.v(0));
Stmt s = Jimple.v().newTableSwitchStmt(IntConstant.v(1), 0, 1, Arrays.asList(new Unit[] { target }), target);
assertTrue(ExceptionTestUtility.sameMembers(utility.VM_ERRORS, Collections.EMPTY_SET, unitAnalysis.mightThrow(s)));
assertEquals(utility.VM_ERRORS_PLUS_SUPERTYPES, utility.catchableSubset(unitAnalysis.mightThrow(s)));
}
use of soot.Unit in project soot by Sable.
the class GraphComparer method nodeToString.
/**
* Utility method to return a {@link String} representation of a
* graph node.
*
* @param node an {@link Object} representing a node in a
* {@link DirectedGraph}.
*
* @param printer either a {@link LabeledUnitPrinter} for printing the
* {@link Unit}s in the graph represented by <tt>node</tt>'s
* graph, if node is part of a control flow graph, or <tt>null</tt>, if
* <tt>node</tt> is not part of a control flow graph.
*
* @return a {@link String} representation of <tt>node</tt>.
*/
private static String nodeToString(Object node, LabeledUnitPrinter printer) {
String result = null;
if (printer == null) {
result = node.toString();
} else if (node instanceof Unit) {
((Unit) node).toString(printer);
result = printer.toString();
} else if (node instanceof Block) {
StringBuffer buffer = new StringBuffer();
Iterator units = ((Block) node).iterator();
while (units.hasNext()) {
Unit unit = (Unit) units.next();
String targetLabel = (String) printer.labels().get(unit);
if (targetLabel != null) {
buffer.append(targetLabel).append(": ");
}
unit.toString(printer);
buffer.append(printer.toString()).append("; ");
}
result = buffer.toString();
}
return result;
}
Aggregations