Search in sources :

Example 46 with Body

use of soot.Body 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)

Example 47 with Body

use of soot.Body in project soot by Sable.

the class CFGGraphType method loadAltGraph.

private static DirectedGraph loadAltGraph(String className, Body b) {
    try {
        Class<?> graphClass = AltClassLoader.v().loadClass(className);
        Class<?>[] paramTypes = new Class[] { Body.class };
        Constructor constructor = graphClass.getConstructor(paramTypes);
        DirectedGraph result = (DirectedGraph) constructor.newInstance(new Object[] { b });
        return result;
    }// don't need to declare them:  perhaps a shoddy tactic.
     catch (ClassNotFoundException e) {
        if (DEBUG) {
            logger.error(e.getMessage(), e);
        }
        throw new IllegalArgumentException("Unable to find " + className + " in alternate classpath: " + e.getMessage());
    } catch (NoSuchMethodException e) {
        if (DEBUG) {
            logger.error(e.getMessage(), e);
        }
        throw new IllegalArgumentException("There is no " + className + "(Body) constructor: " + e.getMessage());
    } catch (InstantiationException e) {
        if (DEBUG) {
            logger.error(e.getMessage(), e);
        }
        throw new IllegalArgumentException("Unable to instantiate " + className + " in alternate classpath: " + e.getMessage());
    } catch (IllegalAccessException e) {
        if (DEBUG) {
            logger.error(e.getMessage(), e);
        }
        throw new IllegalArgumentException("Unable to access " + className + "(Body) in alternate classpath: " + e.getMessage());
    } catch (InvocationTargetException e) {
        if (DEBUG) {
            logger.error(e.getMessage(), e);
        }
        throw new IllegalArgumentException("Unable to invoke " + className + "(Body) in alternate classpath: " + e.getMessage());
    }
}
Also used : Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException) DirectedGraph(soot.toolkits.graph.DirectedGraph) Body(soot.Body)

Example 48 with Body

use of soot.Body in project soot by Sable.

the class RunVeryBusyAnalysis method main.

public static void main(String[] args) {
    args = new String[] { "testers.VeryBusyClass" };
    if (args.length == 0) {
        System.out.println("Usage: java RunVeryBusyAnalysis class_to_analyse");
        System.exit(0);
    }
    String sep = File.separator;
    String pathSep = File.pathSeparator;
    String path = System.getProperty("java.home") + sep + "lib" + sep + "rt.jar";
    path += pathSep + "." + sep + "tutorial" + sep + "guide" + sep + "examples" + sep + "analysis_framework" + sep + "src";
    Options.v().set_soot_classpath(path);
    SootClass sClass = Scene.v().loadClassAndSupport(args[0]);
    sClass.setApplicationClass();
    Scene.v().loadNecessaryClasses();
    for (SootMethod m : sClass.getMethods()) {
        Body b = m.retrieveActiveBody();
        System.out.println("=======================================");
        System.out.println(m.toString());
        UnitGraph graph = new ExceptionalUnitGraph(b);
        VeryBusyExpressions vbe = new SimpleVeryBusyExpressions(graph);
        for (Unit u : graph) {
            List<AbstractBinopExpr> before = vbe.getBusyExpressionsBefore(u);
            List<AbstractBinopExpr> after = vbe.getBusyExpressionsAfter(u);
            UnitPrinter up = new NormalUnitPrinter(b);
            up.setIndent("");
            System.out.println("---------------------------------------");
            u.toString(up);
            System.out.println(up.output());
            System.out.print("Busy in: {");
            sep = "";
            for (AbstractBinopExpr e : before) {
                System.out.print(sep);
                System.out.print(e.toString());
                sep = ", ";
            }
            System.out.println("}");
            System.out.print("Busy out: {");
            sep = "";
            for (AbstractBinopExpr e : after) {
                System.out.print(sep);
                System.out.print(e.toString());
                sep = ", ";
            }
            System.out.println("}");
            System.out.println("---------------------------------------");
        }
        System.out.println("=======================================");
    }
}
Also used : NormalUnitPrinter(soot.NormalUnitPrinter) SimpleVeryBusyExpressions(dk.brics.soot.analyses.SimpleVeryBusyExpressions) SootClass(soot.SootClass) Unit(soot.Unit) SimpleVeryBusyExpressions(dk.brics.soot.analyses.SimpleVeryBusyExpressions) VeryBusyExpressions(dk.brics.soot.analyses.VeryBusyExpressions) ExceptionalUnitGraph(soot.toolkits.graph.ExceptionalUnitGraph) ExceptionalUnitGraph(soot.toolkits.graph.ExceptionalUnitGraph) UnitGraph(soot.toolkits.graph.UnitGraph) NormalUnitPrinter(soot.NormalUnitPrinter) UnitPrinter(soot.UnitPrinter) SootMethod(soot.SootMethod) Body(soot.Body)

Example 49 with Body

use of soot.Body in project soot by Sable.

the class GraphComparer method makeUnitPrinter.

/**
 * Utility method that returns a {@link LabeledUnitPrinter} for printing
 * the {@link Unit}s in graph.
 *
 * @param g the graph for which to return a {@link LabeledUnitPrinter}.
 * @return A {@link LabeledUnitPrinter} for printing the {@link Unit}s in
 * <tt>g</tt> if <tt>g</tt> is a control flow graph. Returns
 * <tt>null</tt> if <tt>g</tt> is not a control flow graph.
 */
private static LabeledUnitPrinter makeUnitPrinter(DirectedGraph g) {
    Body b = getGraphsBody(g);
    if (b == null) {
        return null;
    } else {
        BriefUnitPrinter printer = new BriefUnitPrinter(b);
        printer.noIndent();
        return printer;
    }
}
Also used : BriefUnitPrinter(soot.BriefUnitPrinter) Body(soot.Body)

Example 50 with Body

use of soot.Body 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);
}
Also used : ExceptionalUnitGraph(soot.toolkits.graph.ExceptionalUnitGraph) BodyTransformer(soot.BodyTransformer) Transform(soot.Transform) Body(soot.Body) Map(java.util.Map)

Aggregations

Body (soot.Body)57 Unit (soot.Unit)37 SootMethod (soot.SootMethod)32 Local (soot.Local)20 SootClass (soot.SootClass)19 Value (soot.Value)15 InvokeExpr (soot.jimple.InvokeExpr)14 StaticInvokeExpr (soot.jimple.StaticInvokeExpr)13 Type (soot.Type)12 Stmt (soot.jimple.Stmt)12 InstanceInvokeExpr (soot.jimple.InstanceInvokeExpr)11 JimpleBody (soot.jimple.JimpleBody)11 SpecialInvokeExpr (soot.jimple.SpecialInvokeExpr)11 RefType (soot.RefType)10 VoidType (soot.VoidType)10 VirtualInvokeExpr (soot.jimple.VirtualInvokeExpr)10 LinkedList (java.util.LinkedList)9 SootMethodRef (soot.SootMethodRef)9 ArrayList (java.util.ArrayList)8 PrimType (soot.PrimType)8