use of soot.toolkits.graph.ExceptionalUnitGraph in project soot by Sable.
the class MyMain method main.
public static void main(String[] args) {
PackManager.v().getPack("jtp").add(new Transform("jtp.myTransform", new BodyTransformer() {
protected void internalTransform(Body body, String phase, Map options) {
new MyAnalysis(new ExceptionalUnitGraph(body));
// use G.v().out instead of System.out so that Soot can
// redirect this output to the Eclipse console
G.v().out.println(body.getMethod());
}
}));
soot.Main.main(args);
}
use of soot.toolkits.graph.ExceptionalUnitGraph in project soot by Sable.
the class DexNullArrayRefTransformer method internalTransform.
protected void internalTransform(final Body body, String phaseName, Map<String, String> options) {
final ExceptionalUnitGraph g = new ExceptionalUnitGraph(body, DalvikThrowAnalysis.v());
final LocalDefs defs = LocalDefs.Factory.newLocalDefs(g);
final LocalCreation lc = new LocalCreation(body.getLocals(), "ex");
boolean changed = false;
for (Iterator<Unit> unitIt = body.getUnits().snapshotIterator(); unitIt.hasNext(); ) {
Stmt s = (Stmt) unitIt.next();
if (s.containsArrayRef()) {
// Check array reference
Value base = s.getArrayRef().getBase();
if (isAlwaysNullBefore(s, (Local) base, defs)) {
createThrowStmt(body, s, lc);
changed = true;
}
} else if (s instanceof AssignStmt) {
AssignStmt ass = (AssignStmt) s;
Value rightOp = ass.getRightOp();
if (rightOp instanceof LengthExpr) {
// Check lengthof expression
LengthExpr l = (LengthExpr) ass.getRightOp();
Value base = l.getOp();
if (base instanceof IntConstant) {
IntConstant ic = (IntConstant) base;
if (ic.value == 0) {
createThrowStmt(body, s, lc);
changed = true;
}
} else if (base == NullConstant.v() || isAlwaysNullBefore(s, (Local) base, defs)) {
createThrowStmt(body, s, lc);
changed = true;
}
}
}
}
if (changed)
UnreachableCodeEliminator.v().transform(body);
}
use of soot.toolkits.graph.ExceptionalUnitGraph in project soot by Sable.
the class DexReturnValuePropagator method internalTransform.
@Override
protected void internalTransform(Body body, String phaseName, Map<String, String> options) {
ExceptionalUnitGraph graph = new ExceptionalUnitGraph(body, DalvikThrowAnalysis.v(), true);
LocalDefs localDefs = LocalDefs.Factory.newLocalDefs(graph);
LocalUses localUses = null;
LocalCreation localCreation = null;
// a copy statement, we take the original operand
for (Unit u : body.getUnits()) if (u instanceof ReturnStmt) {
ReturnStmt retStmt = (ReturnStmt) u;
if (retStmt.getOp() instanceof Local) {
List<Unit> defs = localDefs.getDefsOfAt((Local) retStmt.getOp(), retStmt);
if (defs.size() == 1 && defs.get(0) instanceof AssignStmt) {
AssignStmt assign = (AssignStmt) defs.get(0);
final Value rightOp = assign.getRightOp();
final Value leftOp = assign.getLeftOp();
// Copy over the left side if it is a local
if (rightOp instanceof Local) {
// to return a;
if (!isRedefined((Local) rightOp, u, assign, graph))
retStmt.setOp(rightOp);
} else if (rightOp instanceof Constant) {
retStmt.setOp(rightOp);
} else // we rename the local to help splitting
if (rightOp instanceof FieldRef) {
if (localUses == null)
localUses = LocalUses.Factory.newLocalUses(body, localDefs);
if (localUses.getUsesOf(assign).size() == 1) {
if (localCreation == null)
localCreation = new LocalCreation(body.getLocals(), "ret");
Local newLocal = localCreation.newLocal(leftOp.getType());
assign.setLeftOp(newLocal);
retStmt.setOp(newLocal);
}
}
}
}
}
}
use of soot.toolkits.graph.ExceptionalUnitGraph in project soot by Sable.
the class TrapMinimizer method internalTransform.
@Override
protected void internalTransform(Body b, String phaseName, Map<String, String> options) {
// If we have less then two traps, there's nothing to do here
if (b.getTraps().size() == 0)
return;
ExceptionalUnitGraph eug = new ExceptionalUnitGraph(b, DalvikThrowAnalysis.v(), Options.v().omit_excepting_unit_edges());
Set<Unit> unitsWithMonitor = getUnitsWithMonitor(eug);
Map<Trap, List<Trap>> replaceTrapBy = new HashMap<Trap, List<Trap>>(b.getTraps().size());
boolean updateTrap = false;
for (Trap tr : b.getTraps()) {
// will contain the new
List<Trap> newTraps = new ArrayList<Trap>();
// traps
// points to the first unit
Unit firstTrapStmt = tr.getBeginUnit();
// in the trap
// true if there is an edge from the
boolean goesToHandler = false;
// unit to the handler of the
// current trap
updateTrap = false;
for (Unit u = tr.getBeginUnit(); u != tr.getEndUnit(); u = b.getUnits().getSuccOf(u)) {
if (goesToHandler) {
goesToHandler = false;
} else {
// if the previous unit has no exceptional edge to the
// handler,
// update firstTrapStmt to point to the current unit
firstTrapStmt = u;
}
// active monitor, we need to keep the block
if (tr.getException().getName().equals("java.lang.Throwable") && unitsWithMonitor.contains(u))
goesToHandler = true;
// handler
if (!goesToHandler)
if (DalvikThrowAnalysis.v().mightThrow(u).catchableAs(tr.getException().getType())) {
// to be inside the new minimized catch block.
for (ExceptionDest<Unit> ed : eug.getExceptionDests(u)) {
if (ed.getTrap() == tr) {
goesToHandler = true;
break;
}
}
}
if (!goesToHandler) {
// if the current unit does not have an edge to the current
// trap's handler,
// add a new trap starting at firstTrapStmt ending at the
// unit before the
// current unit 'u'.
updateTrap = true;
if (// do not add an empty trap, but set
firstTrapStmt == u)
// updateTrap to true
continue;
Trap t = Jimple.v().newTrap(tr.getException(), firstTrapStmt, u, tr.getHandlerUnit());
newTraps.add(t);
} else {
// next unit is outside the current trap.
if (b.getUnits().getSuccOf(u) == tr.getEndUnit() && updateTrap) {
Trap t = Jimple.v().newTrap(tr.getException(), firstTrapStmt, tr.getEndUnit(), tr.getHandlerUnit());
newTraps.add(t);
}
}
}
// cannot throw any exceptions)
if (updateTrap) {
replaceTrapBy.put(tr, newTraps);
}
}
// replace traps where necessary
for (Trap k : replaceTrapBy.keySet()) {
// we must keep
b.getTraps().insertAfter(replaceTrapBy.get(k), k);
// the order
b.getTraps().remove(k);
}
}
use of soot.toolkits.graph.ExceptionalUnitGraph in project soot by Sable.
the class RemoveRedundantPushStores method internalTransform.
protected void internalTransform(Body b, String phaseName, Map<String, String> options) {
// removes all redundant load-stores
boolean changed = true;
PatchingChain<Unit> units = b.getUnits();
while (changed) {
changed = false;
Unit prevprevprev = null, prevprev = null, prev = null;
ExceptionalUnitGraph eug = new ExceptionalUnitGraph(b);
Iterator<Unit> it = units.snapshotIterator();
while (it.hasNext()) {
Unit u = it.next();
if (prev != null && prev instanceof PushInst && u instanceof StoreInst) {
if (prevprev != null && prevprev instanceof StoreInst && prevprevprev != null && prevprevprev instanceof PushInst) {
Local lprev = ((StoreInst) prevprev).getLocal();
Local l = ((StoreInst) u).getLocal();
if (l == lprev && eug.getSuccsOf(prevprevprev).size() == 1 && eug.getSuccsOf(prevprev).size() == 1) {
fixJumps(prevprevprev, prev, b.getTraps());
fixJumps(prevprev, u, b.getTraps());
units.remove(prevprevprev);
units.remove(prevprev);
changed = true;
break;
}
}
}
prevprevprev = prevprev;
prevprev = prev;
prev = u;
}
}
// end while changes have been made
}
Aggregations