public static boolean isExceptionCaughtAt(Chain<Unit> units, Unit u, Iterator<Trap> trapsIt) { while (trapsIt.hasNext()) { Trap t = trapsIt.next(); Iterator<Unit> it = units.iterator(t.getBeginUnit(), units.getPredOf(t.getEndUnit())); while (it.hasNext()) { if (u.equals(it.next())) { return true; } } } return false; }
protected void buildSimpleExceptionalEdges(Map unitToSuccs, Map unitToPreds) { for (Iterator<Trap> trapIt = body.getTraps().iterator(); trapIt.hasNext();) { Trap trap = trapIt.next(); Unit handler = trap.getHandlerUnit(); for (Iterator predIt = ((List) unitToPreds.get(trap.getBeginUnit())).iterator(); predIt.hasNext();) { Unit pred = (Unit) predIt.next(); addEdge(unitToSuccs, unitToPreds, pred, handler); } } } }
/** Returns a set of units which lie inside the range of any trap. */ public static Set<Unit> getTrappedUnitsOf(Body b) { Set<Unit> trapsSet = new HashSet<Unit>(); Chain<Unit> units = b.getUnits(); for (Trap t : b.getTraps()) { Iterator<Unit> it = units.iterator(t.getBeginUnit(), units.getPredOf(t.getEndUnit())); while (it.hasNext()) { trapsSet.add(it.next()); } } return trapsSet; }
/** Returns the list of traps caught at Unit u in Body b. */ public static List<Trap> getTrapsAt(Unit unit, Body b) { List<Trap> trapsList = new ArrayList<Trap>(); Chain<Unit> units = b.getUnits(); for (Trap t : b.getTraps()) { Iterator<Unit> it = units.iterator(t.getBeginUnit(), units.getPredOf(t.getEndUnit())); while (it.hasNext()) { if (unit.equals(it.next())) { trapsList.add(t); } } } return trapsList; }
@Override protected void internalTransform(Body b, String phaseName, Map<String, String> options) { for (Iterator<Trap> trapIt = b.getTraps().snapshotIterator(); trapIt.hasNext();) { Trap t = trapIt.next(); Unit beginUnit; while (!isDexInstruction(beginUnit = t.getBeginUnit()) && t.getBeginUnit() != t.getEndUnit()) { t.setBeginUnit(b.getUnits().getSuccOf(beginUnit)); } // If the trap is empty, we remove it if (t.getBeginUnit() == t.getEndUnit()) { trapIt.remove(); } } }
public boolean checkTraps(Unit u, Body b) { Iterator<Trap> it = b.getTraps().iterator(); while (it.hasNext()) { Trap t = it.next(); if (t.getBeginUnit() == u || t.getEndUnit() == u || t.getHandlerUnit() == u) { return true; } } return false; }
/** * Checks whether the given trap covers the given unit, i.e., there is an exceptional control flow from the given unit to * the given trap * * @param b * The body containing the unit and the trap * @param trap * The trap * @param unit * The unit * @return True if there can be an exceptional control flow from the given unit to the given trap */ private boolean trapCoversUnit(Body b, Trap trap, Unit unit) { for (Iterator<Unit> unitIt = b.getUnits().iterator(trap.getBeginUnit(), trap.getEndUnit()); unitIt.hasNext();) { Unit u = unitIt.next(); if (u == unit) { return true; } } return false; }
private List<Trap> getTrapsForUnit(Object o, Body b) { ArrayList<Trap> list = new ArrayList<Trap>(); Chain<Trap> traps = b.getTraps(); if (traps.size() != 0) { PatchingChain<Unit> units = b.getUnits(); Iterator<Trap> it = traps.iterator(); while (it.hasNext()) { Trap t = it.next(); Iterator<Unit> tit = units.iterator(t.getBeginUnit(), t.getEndUnit()); while (tit.hasNext()) { if (tit.next() == o) { list.add(t); break; } } } } return list; }
private static boolean isExceptionCaught(Unit unit, Chain<Unit> units, Chain<Trap> traps) { for (Trap trap : traps) { final Unit end = trap.getEndUnit(); if (end.equals(unit)) { return true; } final Iterator<Unit> unitsInTryIterator = units.iterator(trap.getBeginUnit(), units.getPredOf(end)); if (Iterators.contains(unitsInTryIterator, unit)) { return true; } } return false; }
private boolean isRewritable(Trap t) { // ignore traps that already catch their own begin unit if (t.getBeginUnit() == t.getHandlerUnit()) { return false; } // ignore runtime try blocks - these may have weird side-effects do to asynchronous exceptions SootClass exc = t.getException(); if (exc.getName().equals("java.lang.Throwable")) { return false; } do { if (exc.getName().equals("java.lang.RuntimeException")) { return false; } } while (exc.hasSuperclass() && (exc = exc.getSuperclass()) != null); return true; } }
public Trap newTrap(Trap trap) { return new GTrap(trap.getException(), trap.getBeginUnit(), trap.getEndUnit(), trap.getHandlerUnit()); }
/** * If exception e is caught at stmt s in body b, return the handler; otherwise, return null. */ static boolean isExceptionCaughtAt(SootClass e, Stmt stmt, Body b) { /* * Look through the traps t of b, checking to see if: - caught exception is e; - and, stmt lies between t.beginUnit and * t.endUnit */ Hierarchy h = new Hierarchy(); Iterator<Trap> trapsIt = b.getTraps().iterator(); while (trapsIt.hasNext()) { Trap t = trapsIt.next(); /* Ah ha, we might win. */ if (h.isClassSubclassOfIncluding(e, t.getException())) { Iterator<Unit> it = b.getUnits().iterator(t.getBeginUnit(), t.getEndUnit()); while (it.hasNext()) { if (stmt.equals(it.next())) { return true; } } } } return false; } }
/** * Gets two arbitrary overlapping traps in the given method body * * @param b * The body in which to look for overlapping traps * @return Two overlapping traps if they exist, otherwise null */ private TrapOverlap getNextOverlap(Body b) { for (Trap t1 : b.getTraps()) { // Look whether one of our trapped statements is the begin // statement of another trap for (Unit splitUnit = t1.getBeginUnit(); splitUnit != t1.getEndUnit(); splitUnit = b.getUnits().getSuccOf(splitUnit)) { for (Trap t2 : b.getTraps()) { if (t1 != t2 && (t1.getEndUnit() != t2.getEndUnit() || t1.getException() == t2.getException()) && t2.getBeginUnit() == splitUnit) { return new TrapOverlap(t1, t2, t2.getBeginUnit()); } } } } return null; }
@Override /** Verifies that the begin, end and handler units of each trap are in this body. */ public void validate(Body body, List<ValidationException> exception) { PatchingChain<Unit> units = body.getUnits(); for (Trap t : body.getTraps()) { if (!units.contains(t.getBeginUnit())) { exception.add(new ValidationException(t.getBeginUnit(), "begin not in chain" + " in " + body.getMethod())); } if (!units.contains(t.getEndUnit())) { exception.add(new ValidationException(t.getEndUnit(), "end not in chain" + " in " + body.getMethod())); } if (!units.contains(t.getHandlerUnit())) { exception.add(new ValidationException(t.getHandlerUnit(), "handler not in chain" + " in " + body.getMethod())); } } }
/** * If exception e is caught at unit u in body b, return true; otherwise, return false. */ public static boolean isExceptionCaughtAt(SootClass e, Unit u, Body b) { /* * Look through the traps t of b, checking to see if: - caught exception is e; - and, unit lies between t.beginUnit and * t.endUnit */ Hierarchy h = Scene.v().getActiveHierarchy(); Chain<Unit> units = b.getUnits(); for (Trap t : b.getTraps()) { /* Ah ha, we might win. */ if (h.isClassSubclassOfIncluding(e, t.getException())) { Iterator<Unit> it = units.iterator(t.getBeginUnit(), units.getPredOf(t.getEndUnit())); while (it.hasNext()) { if (u.equals(it.next())) { return true; } } } } return false; }
public void redirectTraps(Body b, Unit oldUnit, Unit newUnit) { Chain<Trap> traps = b.getTraps(); for (Trap trap : traps) { if (trap.getHandlerUnit() == oldUnit) { trap.setHandlerUnit(newUnit); } if (trap.getBeginUnit() == oldUnit) { trap.setBeginUnit(newUnit); } if (trap.getEndUnit() == oldUnit) { trap.setEndUnit(newUnit); } } }
/** * Adds a new trap to the given body only if the given trap is not empty * * @param b * The body to which to add the trap * @param newTrap * The trap to add * @param position * The position after which to insert the trap */ private void safeAddTrap(Body b, Trap newTrap, Trap position) { // Do not create any empty traps if (newTrap.getBeginUnit() != newTrap.getEndUnit()) { if (position != null) { b.getTraps().insertAfter(newTrap, position); } else { b.getTraps().add(newTrap); } } }
private void fixJumps(Unit from, Unit to, Chain<Trap> t) { from.redirectJumpsToThisTo(to); for (Trap trap : t) { if (trap.getBeginUnit() == from) { trap.setBeginUnit(to); } if (trap.getEndUnit() == from) { trap.setEndUnit(to); } if (trap.getHandlerUnit() == from) { trap.setHandlerUnit(to); } } } }
protected boolean isExceptionCaught(Body b, Stmt s, RefType throwType) { if (hierarchy == null) { hierarchy = new FastHierarchy(); } Iterator it = b.getTraps().iterator(); while (it.hasNext()) { Trap trap = (Trap) it.next(); if (trap.getException().getType().equals(throwType) || hierarchy.isSubclass(throwType.getSootClass(), (trap.getException().getType()).getSootClass())) { if (isThrowInStmtRange(b, (Stmt) trap.getBeginUnit(), (Stmt) trap.getEndUnit(), s)) { return true; } } } return false; }
public static void updateTraps(Unit oldu, Unit newu, Chain<Trap> traps) { int size = traps.size(); if (size == 0) { return; } Trap t = traps.getFirst(); do { if (t.getBeginUnit() == oldu) { t.setBeginUnit(newu); } if (t.getEndUnit() == oldu) { t.setEndUnit(newu); } if (t.getHandlerUnit() == oldu) { t.setHandlerUnit(newu); } } while ((--size > 0) && (t = traps.getSuccOf(t)) != null); }