public Trap newTrap(Trap trap) { return new GTrap(trap.getException(), trap.getBeginUnit(), trap.getEndUnit(), trap.getHandlerUnit()); }
Trap t = trapsIt.next(); Iterator<Unit> unitIt = units.iterator(t.getBeginUnit(), t.getEndUnit()); Trap newTrap = (Trap) t.clone(); t.setBeginUnit(rangeStart); newTrap.setEndUnit(rangeStart); traps.insertAfter(newTrap, t); } else { throw new RuntimeException("inversed range?"); Trap firstTrap = (Trap) t.clone(); Trap secondTrap = (Trap) t.clone(); firstTrap.setEndUnit(rangeStart); secondTrap.setBeginUnit(rangeStart); secondTrap.setEndUnit(rangeEnd); t.setBeginUnit(rangeEnd);
while (it.hasNext()) { Trap item = it.next(); unitBoxList.addAll(item.getUnitBoxes());
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); } } }
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; }
/** * 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; }
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; }
private static RefType isHandlerUnit(Chain<Trap> traps, Unit h) { Iterator<Trap> it = traps.iterator(); while (it.hasNext()) { Trap t = (Trap) it.next(); if (t.getHandlerUnit() == h) { return t.getException().getType(); } } return null; }
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; } }
@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(); } } }
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); } } } }
headList.add(t.getBeginUnit()); trapList.add(t); trapCount++; units.insertBeforeNoRedirect(nop, headList.get(i)); headList.set(i, nop); t.setBeginUnit(nop); while (traps.hasNext()) { Trap t = traps.next(); Unit begUnit = t.getBeginUnit(); if (!isRewritable(t) || Rand.getInt(10) > weight) { continue; Unit handlerUnit = t.getHandlerUnit(); Unit newBeginUnit = Baf.v().newLoadInst(IntType.v(), controlLocal); units.insertBefore(newBeginUnit, begUnit); t.setBeginUnit(newBeginUnit); t.setHandlerUnit(newBeginUnit);
@Override public Unit getHandlerNode() { if (trap == null) { return null; } else { return trap.getHandlerUnit(); } }
@Override protected void internalTransform(Body b, String phaseName, Map<String, String> options) { for (Trap t : b.getTraps()) { // If the first statement already catches the exception, we're fine if (isCaughtExceptionRef(t.getHandlerUnit())) { continue; } // Add the exception reference Local l = new LocalGenerator(b).generateLocal(t.getException().getType()); Stmt caughtStmt = Jimple.v().newIdentityStmt(l, Jimple.v().newCaughtExceptionRef()); b.getUnits().add(caughtStmt); b.getUnits().add(Jimple.v().newGotoStmt(t.getHandlerUnit())); t.setHandlerUnit(caughtStmt); } }
private Set<Trap> handleRuntimeException() { Set<Trap> usedTraps = new HashSet<Trap>(); // handle the runtime exceptions first. for (Entry<Unit, List<Pair<Value, SootClass>>> entry : runtimeExceptions.entrySet()) { Unit u = entry.getKey(); List<Trap> surroundingTraps = getTrapsGuardingUnit(u, body); for (Pair<Value, SootClass> pair : entry.getValue()) { Trap trap = null; for (Trap t : surroundingTraps) { if (hierarchy.isClassSubclassOfIncluding(pair.getSecond(), t.getException())) { trap = t; break; } } if (trap != null) { handleCaughtRuntimeException(u, pair.getFirst(), pair.getSecond(), trap); usedTraps.add(trap); } else { // re-throw the exception handleUncaughtRuntimeException(u, pair.getFirst(), pair.getSecond()); } } } return usedTraps; }
HashMap<Trap, Unit> trapsToHandler = new HashMap<Trap, Unit>(); for (Trap t : b.getTraps()) { trapsToHandler.put(t, t.getHandlerUnit()); t.setHandlerUnit(trapsToHandler.get(t));
Trap copy = (Trap) original.clone();
if (b.getUnits().getLast() == u) { for (Trap t : b.getTraps()) { if (t.getEndUnit() == u) { keepNop = true;
/** * 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; } }
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); } } } }