Search in sources :

Example 1 with Pair

use of soot.toolkits.exceptions.ThrowableSet.Pair in project soot by Sable.

the class UnitThrowAnalysis method mightThrow.

/**
 * Returns the set of types that might be thrown as a result of
 * calling the specified method.
 *
 * @param sm method whose exceptions are to be returned.
 * @param doneSet The set of methods that were already processed
 *
 * @return a representation of the set of {@link
 * java.lang.Throwable Throwable} types that <code>m</code> might
 * throw.
 */
private ThrowableSet mightThrow(SootMethod sm, Set<SootMethod> doneSet) {
    // Do not run in loops
    if (!doneSet.add(sm))
        return ThrowableSet.Manager.v().EMPTY;
    // unsound, but would otherwise always bloat our result set.
    if (!sm.hasActiveBody())
        return ThrowableSet.Manager.v().EMPTY;
    // We need a mapping between unit and exception
    final PatchingChain<Unit> units = sm.getActiveBody().getUnits();
    Map<Unit, Collection<Trap>> unitToTraps = sm.getActiveBody().getTraps().isEmpty() ? null : new HashMap<Unit, Collection<Trap>>();
    for (Trap t : sm.getActiveBody().getTraps()) {
        for (Iterator<Unit> unitIt = units.iterator(t.getBeginUnit(), units.getPredOf(t.getEndUnit())); unitIt.hasNext(); ) {
            Unit unit = unitIt.next();
            Collection<Trap> unitsForTrap = unitToTraps.get(unit);
            if (unitsForTrap == null) {
                unitsForTrap = new ArrayList<Trap>();
                unitToTraps.put(unit, unitsForTrap);
            }
            unitsForTrap.add(t);
        }
    }
    ThrowableSet methodSet = ThrowableSet.Manager.v().EMPTY;
    if (sm.hasActiveBody()) {
        Body methodBody = sm.getActiveBody();
        for (Unit u : methodBody.getUnits()) {
            if (u instanceof Stmt) {
                Stmt stmt = (Stmt) u;
                ThrowableSet curStmtSet;
                if (stmt.containsInvokeExpr()) {
                    InvokeExpr inv = stmt.getInvokeExpr();
                    curStmtSet = mightThrow(inv.getMethod(), doneSet);
                } else
                    curStmtSet = mightThrow(u);
                // The exception might be caught along the way
                if (unitToTraps != null) {
                    Collection<Trap> trapsForUnit = unitToTraps.get(stmt);
                    if (trapsForUnit != null)
                        for (Trap t : trapsForUnit) {
                            Pair p = curStmtSet.whichCatchableAs(t.getException().getType());
                            curStmtSet = curStmtSet.remove(p.getCaught());
                        }
                }
                methodSet = methodSet.add(curStmtSet);
            }
        }
    }
    return methodSet;
}
Also used : Trap(soot.Trap) Unit(soot.Unit) BreakpointStmt(soot.jimple.BreakpointStmt) EnterMonitorStmt(soot.jimple.EnterMonitorStmt) NopStmt(soot.jimple.NopStmt) GotoStmt(soot.jimple.GotoStmt) RetStmt(soot.jimple.RetStmt) IfStmt(soot.jimple.IfStmt) LookupSwitchStmt(soot.jimple.LookupSwitchStmt) Stmt(soot.jimple.Stmt) ReturnVoidStmt(soot.jimple.ReturnVoidStmt) InvokeStmt(soot.jimple.InvokeStmt) AssignStmt(soot.jimple.AssignStmt) ThrowStmt(soot.jimple.ThrowStmt) IdentityStmt(soot.jimple.IdentityStmt) TableSwitchStmt(soot.jimple.TableSwitchStmt) ReturnStmt(soot.jimple.ReturnStmt) ExitMonitorStmt(soot.jimple.ExitMonitorStmt) InterfaceInvokeExpr(soot.jimple.InterfaceInvokeExpr) DynamicInvokeExpr(soot.jimple.DynamicInvokeExpr) SpecialInvokeExpr(soot.jimple.SpecialInvokeExpr) InstanceInvokeExpr(soot.jimple.InstanceInvokeExpr) NewInvokeExpr(soot.grimp.NewInvokeExpr) VirtualInvokeExpr(soot.jimple.VirtualInvokeExpr) InvokeExpr(soot.jimple.InvokeExpr) StaticInvokeExpr(soot.jimple.StaticInvokeExpr) Collection(java.util.Collection) Body(soot.Body) Pair(soot.toolkits.exceptions.ThrowableSet.Pair)

Aggregations

Collection (java.util.Collection)1 Body (soot.Body)1 Trap (soot.Trap)1 Unit (soot.Unit)1 NewInvokeExpr (soot.grimp.NewInvokeExpr)1 AssignStmt (soot.jimple.AssignStmt)1 BreakpointStmt (soot.jimple.BreakpointStmt)1 DynamicInvokeExpr (soot.jimple.DynamicInvokeExpr)1 EnterMonitorStmt (soot.jimple.EnterMonitorStmt)1 ExitMonitorStmt (soot.jimple.ExitMonitorStmt)1 GotoStmt (soot.jimple.GotoStmt)1 IdentityStmt (soot.jimple.IdentityStmt)1 IfStmt (soot.jimple.IfStmt)1 InstanceInvokeExpr (soot.jimple.InstanceInvokeExpr)1 InterfaceInvokeExpr (soot.jimple.InterfaceInvokeExpr)1 InvokeExpr (soot.jimple.InvokeExpr)1 InvokeStmt (soot.jimple.InvokeStmt)1 LookupSwitchStmt (soot.jimple.LookupSwitchStmt)1 NopStmt (soot.jimple.NopStmt)1 RetStmt (soot.jimple.RetStmt)1