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; }
/** * Given a body and a unit handling an exception, returns the list of exception types possibly caught by the handler. */ public static List<RefType> getExceptionTypesOf(Unit u, Body body) { List<RefType> possibleTypes = new ArrayList<RefType>(); for (Trap trap : body.getTraps()) { if (trap.getHandlerUnit() == u) { possibleTypes.add(RefType.v(trap.getException().getName())); } } return possibleTypes; } }
private void register_AreasOfProtection(SootMethod m) { if (registeredMethods.contains(m)) { return; } registeredMethods.add(m); if (m.hasActiveBody() == false) { return; } Body b = m.getActiveBody(); Chain stmts = b.getUnits(); Iterator trapIt = b.getTraps().iterator(); while (trapIt.hasNext()) { Trap t = (Trap) trapIt.next(); SootClass exception = t.getException(); Iterator sit = stmts.iterator(t.getBeginUnit(), stmts.getPredOf(t.getEndUnit())); while (sit.hasNext()) { Stmt s = (Stmt) sit.next(); HashSet<SootClass> handled = null; if ((handled = protectionSet.get(s)) == null) { handled = new HashSet<SootClass>(); protectionSet.put(s, handled); } if (handled.contains(exception) == false) { handled.add(exception); } } } }
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; } }
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; }
/** * 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; }
if (t1.getException().getName().equals("java.lang.Throwable")) { for (Iterator<Trap> t2It = b.getTraps().snapshotIterator(); t2It.hasNext();) { Trap t2 = t2It.next(); if (t1 != t2 && t1.getBeginUnit() == t2.getBeginUnit() && t1.getEndUnit() == t2.getEndUnit() && t2.getException().getName().equals("java.lang.Throwable")) { if (t3 != t1 && t3 != t2 && t3.getException().getName().equals("java.lang.Throwable")) { if (trapCoversUnit(b, t3, t1.getHandlerUnit()) && t3.getHandlerUnit() == t2.getHandlerUnit()) {
/** * 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; }
RefType catcher = trap.getException().getType(); for (Iterator<Unit> unitIt = units.iterator(trap.getBeginUnit(), units.getPredOf(trap.getEndUnit())); unitIt .hasNext();) {
Label end = branchTargetLabels.get(trap.getEndUnit()); Label handler = branchTargetLabels.get(trap.getHandlerUnit()); String type = slashify(trap.getException().getName()); mv.visitTryCatchBlock(start, end, handler, type);
if (tr.getException().getName().equals("java.lang.Throwable") && unitsWithMonitor.contains(u)) { goesToHandler = true; if (DalvikThrowAnalysis.v().mightThrow(u).catchableAs(tr.getException().getType())) { Trap t = Jimple.v().newTrap(tr.getException(), firstTrapStmt, u, tr.getHandlerUnit()); newTraps.add(t); } else { Trap t = Jimple.v().newTrap(tr.getException(), firstTrapStmt, tr.getEndUnit(), tr.getHandlerUnit()); newTraps.add(t);
Trap trap = trapIt.next(); out.println(" catch " + Scene.v().quotedNameOf(trap.getException().getName()) + " from " + up.labels().get(trap.getBeginUnit()) + " to " + up.labels().get(trap.getEndUnit()) + " with " + up.labels().get(trap.getHandlerUnit()) + ";");
if (trapsForUnit != null) { for (Trap t : trapsForUnit) { Pair p = curStmtSet.whichCatchableAs(t.getException().getType()); curStmtSet = curStmtSet.remove(p.getCaught());
boolean isCatchAll = trap.getException().getName().equals("java.lang.Throwable"); Unit firstTrappedUnit = trap.getBeginUnit(); Unit firstTrappedThrower = null;
Trap newTrap = Jimple.v().newTrap(to.t1.getException(), to.t1.getBeginUnit(), to.t2Start, to.t1.getHandlerUnit()); safeAddTrap(b, newTrap, to.t1); to.t1.setBeginUnit(to.t2Start); if (to.t1.getException() != to.t2.getException()) { Trap newTrap = Jimple.v().newTrap(to.t2.getException(), to.t1.getBeginUnit(), firstEndUnit, to.t2.getHandlerUnit()); safeAddTrap(b, newTrap, to.t2); } else if (to.t1.getHandlerUnit() != to.t2.getHandlerUnit()) { = Jimple.v().newTrap(to.t1.getException(), to.t1.getBeginUnit(), firstEndUnit, to.t1.getHandlerUnit()); safeAddTrap(b, newTrap, to.t1); if (to.t1.getException() != to.t2.getException()) { Trap newTrap2 = Jimple.v().newTrap(to.t1.getException(), to.t1.getBeginUnit(), firstEndUnit, to.t1.getHandlerUnit()); safeAddTrap(b, newTrap2, to.t1); to.t1.setBeginUnit(firstEndUnit);
getTraps().add(Baf.v().newTrap(trap.getException(), stmtToFirstInstruction.get(trap.getBeginUnit()), stmtToFirstInstruction.get(trap.getEndUnit()), stmtToFirstInstruction.get(trap.getHandlerUnit())));
b.getTraps().add(Jimple.v().newTrap(t.getException(), newBegin, newEnd, newHandler));
@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); } }