Search in sources :

Example 1 with LocalUses

use of soot.toolkits.scalar.LocalUses in project soot by Sable.

the class DeadAssignmentEliminator method internalTransform.

/**
 * Eliminates dead code in a linear fashion.  Complexity is linear
 * with respect to the statements.
 *
 * Does not work on grimp code because of the check on the right hand
 * side for side effects.
 */
@Override
protected void internalTransform(Body b, String phaseName, Map<String, String> options) {
    boolean eliminateOnlyStackLocals = PhaseOptions.getBoolean(options, "only-stack-locals");
    final Options soptions = Options.v();
    if (soptions.verbose()) {
        logger.debug("[" + b.getMethod().getName() + "] Eliminating dead code...");
    }
    if (soptions.time()) {
        Timers.v().deadCodeTimer.start();
    }
    Chain<Unit> units = b.getUnits();
    Deque<Unit> q = new ArrayDeque<Unit>(units.size());
    // Make a first pass through the statements, noting
    // the statements we must absolutely keep.
    boolean isStatic = b.getMethod().isStatic();
    boolean allEssential = true;
    boolean checkInvoke = false;
    Local thisLocal = null;
    for (Iterator<Unit> it = units.iterator(); it.hasNext(); ) {
        Unit s = it.next();
        boolean isEssential = true;
        if (s instanceof NopStmt) {
            // Hack: do not remove nop if is is used for a Trap
            // which is at the very end of the code.
            boolean removeNop = it.hasNext();
            if (!removeNop) {
                removeNop = true;
                for (Trap t : b.getTraps()) {
                    if (t.getEndUnit() == s) {
                        removeNop = false;
                        break;
                    }
                }
            }
            if (removeNop) {
                it.remove();
                continue;
            }
        } else if (s instanceof AssignStmt) {
            AssignStmt as = (AssignStmt) s;
            Value lhs = as.getLeftOp();
            Value rhs = as.getRightOp();
            // Stmt is of the form a = a which is useless
            if (lhs == rhs && lhs instanceof Local) {
                it.remove();
                continue;
            }
            if (lhs instanceof Local && (!eliminateOnlyStackLocals || ((Local) lhs).getName().startsWith("$") || lhs.getType() instanceof NullType)) {
                isEssential = false;
                if (!checkInvoke) {
                    checkInvoke = as.containsInvokeExpr();
                }
                if (rhs instanceof CastExpr) {
                    // CastExpr          : can trigger ClassCastException, but null-casts never fail
                    CastExpr ce = (CastExpr) rhs;
                    Type t = ce.getCastType();
                    Value v = ce.getOp();
                    isEssential = !(v instanceof NullConstant && t instanceof RefType);
                } else if (rhs instanceof InvokeExpr || rhs instanceof ArrayRef || rhs instanceof NewExpr || rhs instanceof NewArrayExpr || rhs instanceof NewMultiArrayExpr) {
                    // ArrayRef          : can have side effects (like throwing a null pointer exception)
                    // InvokeExpr        : can have side effects (like throwing a null pointer exception)
                    // NewArrayExpr      : can throw exception
                    // NewMultiArrayExpr : can throw exception
                    // NewExpr           : can trigger class initialization
                    isEssential = true;
                } else if (rhs instanceof FieldRef) {
                    // Can trigger class initialization
                    isEssential = true;
                    if (rhs instanceof InstanceFieldRef) {
                        InstanceFieldRef ifr = (InstanceFieldRef) rhs;
                        if (!isStatic && thisLocal == null) {
                            thisLocal = b.getThisLocal();
                        }
                        // Any InstanceFieldRef may have side effects,
                        // unless the base is reading from 'this'
                        // in a non-static method
                        isEssential = (isStatic || thisLocal != ifr.getBase());
                    }
                } else if (rhs instanceof DivExpr || rhs instanceof RemExpr) {
                    BinopExpr expr = (BinopExpr) rhs;
                    Type t1 = expr.getOp1().getType();
                    Type t2 = expr.getOp2().getType();
                    // Can trigger a division by zero
                    boolean t2Int = t2 instanceof IntType;
                    isEssential = t2Int || t1 instanceof IntType || t1 instanceof LongType || t2 instanceof LongType || t1 instanceof UnknownType || t2 instanceof UnknownType;
                    if (isEssential && t2Int) {
                        Value v = expr.getOp2();
                        if (v instanceof IntConstant) {
                            IntConstant i = (IntConstant) v;
                            isEssential = (i.value == 0);
                        } else
                            // could be 0, we don't know
                            isEssential = true;
                    }
                    if (isEssential && t2 instanceof LongType) {
                        Value v = expr.getOp2();
                        if (v instanceof LongConstant) {
                            LongConstant l = (LongConstant) v;
                            isEssential = (l.value == 0);
                        } else
                            // could be 0, we don't know
                            isEssential = true;
                    }
                }
            }
        }
        if (isEssential) {
            q.addFirst(s);
        }
        allEssential &= isEssential;
    }
    if (checkInvoke || !allEssential) {
        // Add all the statements which are used to compute values
        // for the essential statements, recursively
        final LocalDefs localDefs = LocalDefs.Factory.newLocalDefs(b);
        if (!allEssential) {
            Set<Unit> essential = new HashSet<Unit>(b.getUnits().size());
            while (!q.isEmpty()) {
                Unit s = q.removeFirst();
                if (essential.add(s)) {
                    for (ValueBox box : s.getUseBoxes()) {
                        Value v = box.getValue();
                        if (v instanceof Local) {
                            Local l = (Local) v;
                            List<Unit> defs = localDefs.getDefsOfAt(l, s);
                            if (defs != null)
                                q.addAll(defs);
                        }
                    }
                }
            }
            // Remove the dead statements
            units.retainAll(essential);
        }
        if (checkInvoke) {
            final LocalUses localUses = LocalUses.Factory.newLocalUses(b, localDefs);
            // Eliminate dead assignments from invokes such as x = f(), where
            // x is no longer used
            List<AssignStmt> postProcess = new ArrayList<AssignStmt>();
            for (Unit u : units) {
                if (u instanceof AssignStmt) {
                    AssignStmt s = (AssignStmt) u;
                    if (s.containsInvokeExpr()) {
                        // Just find one use of l which is essential
                        boolean deadAssignment = true;
                        for (UnitValueBoxPair pair : localUses.getUsesOf(s)) {
                            if (units.contains(pair.unit)) {
                                deadAssignment = false;
                                break;
                            }
                        }
                        if (deadAssignment) {
                            postProcess.add(s);
                        }
                    }
                }
            }
            final Jimple jimple = Jimple.v();
            for (AssignStmt s : postProcess) {
                // Transform it into a simple invoke.
                Stmt newInvoke = jimple.newInvokeStmt(s.getInvokeExpr());
                newInvoke.addAllTagsOf(s);
                units.swapWith(s, newInvoke);
                // If we have a callgraph, we need to fix it
                if (Scene.v().hasCallGraph())
                    Scene.v().getCallGraph().swapEdgesOutOf(s, newInvoke);
            }
        }
    }
    if (soptions.time()) {
        Timers.v().deadCodeTimer.end();
    }
}
Also used : Options(soot.options.Options) PhaseOptions(soot.PhaseOptions) LongType(soot.LongType) AssignStmt(soot.jimple.AssignStmt) NewMultiArrayExpr(soot.jimple.NewMultiArrayExpr) ArrayList(java.util.ArrayList) Unit(soot.Unit) IntType(soot.IntType) NopStmt(soot.jimple.NopStmt) AssignStmt(soot.jimple.AssignStmt) Stmt(soot.jimple.Stmt) RefType(soot.RefType) ArrayRef(soot.jimple.ArrayRef) InvokeExpr(soot.jimple.InvokeExpr) CastExpr(soot.jimple.CastExpr) InstanceFieldRef(soot.jimple.InstanceFieldRef) IntConstant(soot.jimple.IntConstant) UnitValueBoxPair(soot.toolkits.scalar.UnitValueBoxPair) HashSet(java.util.HashSet) LongConstant(soot.jimple.LongConstant) FieldRef(soot.jimple.FieldRef) InstanceFieldRef(soot.jimple.InstanceFieldRef) Local(soot.Local) NullConstant(soot.jimple.NullConstant) Trap(soot.Trap) LocalUses(soot.toolkits.scalar.LocalUses) LocalDefs(soot.toolkits.scalar.LocalDefs) ArrayDeque(java.util.ArrayDeque) UnknownType(soot.UnknownType) RefType(soot.RefType) Type(soot.Type) UnknownType(soot.UnknownType) IntType(soot.IntType) LongType(soot.LongType) NullType(soot.NullType) DivExpr(soot.jimple.DivExpr) NewArrayExpr(soot.jimple.NewArrayExpr) NopStmt(soot.jimple.NopStmt) RemExpr(soot.jimple.RemExpr) ValueBox(soot.ValueBox) Value(soot.Value) NewExpr(soot.jimple.NewExpr) Jimple(soot.jimple.Jimple) NullType(soot.NullType) BinopExpr(soot.jimple.BinopExpr)

Example 2 with LocalUses

use of soot.toolkits.scalar.LocalUses in project soot by Sable.

the class ConstructorFolder method internalTransform.

/**
 * This method change all new Obj/<init>(args) pairs to new Obj(args) idioms.
 */
protected void internalTransform(Body b, String phaseName, Map options) {
    GrimpBody body = (GrimpBody) b;
    if (Options.v().verbose())
        logger.debug("[" + body.getMethod().getName() + "] Folding constructors...");
    Chain units = body.getUnits();
    List<Unit> stmtList = new ArrayList<Unit>();
    stmtList.addAll(units);
    Iterator<Unit> it = stmtList.iterator();
    LocalUses localUses = LocalUses.Factory.newLocalUses(b);
    /* fold in NewExpr's with specialinvoke's */
    while (it.hasNext()) {
        Stmt s = (Stmt) it.next();
        if (!(s instanceof AssignStmt))
            continue;
        /* this should be generalized to ArrayRefs */
        Value lhs = ((AssignStmt) s).getLeftOp();
        if (!(lhs instanceof Local))
            continue;
        Value rhs = ((AssignStmt) s).getRightOp();
        if (!(rhs instanceof NewExpr))
            continue;
        /* TO BE IMPLEMENTED LATER: move any copy of the object reference
             for lhs down beyond the NewInvokeExpr, with the rationale
             being that you can't modify the object before the constructor
             call in any case.

             Also, do note that any new's (object creation) without
             corresponding constructors must be dead. */
        List lu = localUses.getUsesOf(s);
        Iterator luIter = lu.iterator();
        boolean MadeNewInvokeExpr = false;
        while (luIter.hasNext()) {
            Unit use = ((UnitValueBoxPair) (luIter.next())).unit;
            if (!(use instanceof InvokeStmt))
                continue;
            InvokeStmt is = (InvokeStmt) use;
            if (!(is.getInvokeExpr() instanceof SpecialInvokeExpr) || lhs != ((SpecialInvokeExpr) is.getInvokeExpr()).getBase())
                continue;
            SpecialInvokeExpr oldInvoke = ((SpecialInvokeExpr) is.getInvokeExpr());
            LinkedList invokeArgs = new LinkedList();
            for (int i = 0; i < oldInvoke.getArgCount(); i++) invokeArgs.add(oldInvoke.getArg(i));
            AssignStmt constructStmt = Grimp.v().newAssignStmt((AssignStmt) s);
            constructStmt.setRightOp(Grimp.v().newNewInvokeExpr(((NewExpr) rhs).getBaseType(), oldInvoke.getMethodRef(), invokeArgs));
            MadeNewInvokeExpr = true;
            use.redirectJumpsToThisTo(constructStmt);
            units.insertBefore(constructStmt, use);
            units.remove(use);
        }
        if (MadeNewInvokeExpr) {
            units.remove(s);
        }
    }
}
Also used : Chain(soot.util.Chain) InvokeStmt(soot.jimple.InvokeStmt) AssignStmt(soot.jimple.AssignStmt) SpecialInvokeExpr(soot.jimple.SpecialInvokeExpr) ArrayList(java.util.ArrayList) Local(soot.Local) SimpleLocalUses(soot.toolkits.scalar.SimpleLocalUses) LocalUses(soot.toolkits.scalar.LocalUses) Unit(soot.Unit) LinkedList(java.util.LinkedList) InvokeStmt(soot.jimple.InvokeStmt) Stmt(soot.jimple.Stmt) AssignStmt(soot.jimple.AssignStmt) Value(soot.Value) NewExpr(soot.jimple.NewExpr) Iterator(java.util.Iterator) UnitValueBoxPair(soot.toolkits.scalar.UnitValueBoxPair) GrimpBody(soot.grimp.GrimpBody) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 3 with LocalUses

use of soot.toolkits.scalar.LocalUses in project soot by Sable.

the class Aggregator method internalAggregate.

private static boolean internalAggregate(StmtBody body, Map<ValueBox, Zone> boxToZone, boolean onlyStackVars) {
    boolean hadAggregation = false;
    Chain<Unit> units = body.getUnits();
    ExceptionalUnitGraph graph = new ExceptionalUnitGraph(body);
    LocalDefs localDefs = LocalDefs.Factory.newLocalDefs(graph);
    LocalUses localUses = LocalUses.Factory.newLocalUses(body, localDefs);
    List<Unit> unitList = new PseudoTopologicalOrderer<Unit>().newList(graph, false);
    for (Unit u : unitList) {
        if (!(u instanceof AssignStmt))
            continue;
        AssignStmt s = (AssignStmt) u;
        Value lhs = s.getLeftOp();
        if (!(lhs instanceof Local))
            continue;
        Local lhsLocal = (Local) lhs;
        if (onlyStackVars && !lhsLocal.getName().startsWith("$"))
            continue;
        List<UnitValueBoxPair> lu = localUses.getUsesOf(s);
        if (lu.size() != 1)
            continue;
        UnitValueBoxPair usepair = lu.get(0);
        Unit use = usepair.unit;
        ValueBox useBox = usepair.valueBox;
        List<Unit> ld = localDefs.getDefsOfAt(lhsLocal, use);
        if (ld.size() != 1)
            continue;
        // Check to make sure aggregation pair in the same zone
        if (boxToZone.get(s.getRightOpBox()) != boxToZone.get(usepair.valueBox)) {
            continue;
        }
        /* we need to check the path between def and use */
        /* to see if there are any intervening re-defs of RHS */
        /* in fact, we should check that this path is unique. */
        /*
			 * if the RHS uses only locals, then we know what to do; if RHS has
			 * a method invocation f(a, b, c) or field access, we must ban field
			 * writes, other method calls and (as usual) writes to a, b, c.
			 */
        boolean cantAggr = false;
        boolean propagatingInvokeExpr = false;
        boolean propagatingFieldRef = false;
        boolean propagatingArrayRef = false;
        List<FieldRef> fieldRefList = new ArrayList<FieldRef>();
        List<Value> localsUsed = new ArrayList<Value>();
        for (ValueBox vb : s.getUseBoxes()) {
            Value v = vb.getValue();
            if (v instanceof Local) {
                localsUsed.add(v);
            } else if (v instanceof InvokeExpr) {
                propagatingInvokeExpr = true;
            } else if (v instanceof ArrayRef) {
                propagatingArrayRef = true;
            } else if (v instanceof FieldRef) {
                propagatingFieldRef = true;
                fieldRefList.add((FieldRef) v);
            }
        }
        // look for a path from s to use in graph.
        // only look in an extended basic block, though.
        List<Unit> path = graph.getExtendedBasicBlockPathBetween(s, use);
        if (path == null)
            continue;
        Iterator<Unit> pathIt = path.iterator();
        // skip s.
        if (pathIt.hasNext())
            pathIt.next();
        while (pathIt.hasNext() && !cantAggr) {
            Stmt between = (Stmt) (pathIt.next());
            if (between != use) {
                for (ValueBox vb : between.getDefBoxes()) {
                    Value v = vb.getValue();
                    if (localsUsed.contains(v)) {
                        cantAggr = true;
                        break;
                    }
                    if (propagatingInvokeExpr || propagatingFieldRef || propagatingArrayRef) {
                        if (v instanceof FieldRef) {
                            if (propagatingInvokeExpr) {
                                cantAggr = true;
                                break;
                            } else if (propagatingFieldRef) {
                                // aliased
                                for (FieldRef fieldRef : fieldRefList) {
                                    if (isSameField((FieldRef) v, fieldRef)) {
                                        cantAggr = true;
                                        break;
                                    }
                                }
                            }
                        } else if (v instanceof ArrayRef) {
                            if (propagatingInvokeExpr) {
                                // Cannot aggregate an invoke expr past an
                                // array write
                                cantAggr = true;
                                break;
                            } else if (propagatingArrayRef) {
                                // cannot aggregate an array read past a
                                // write
                                // this is somewhat conservative
                                // (if types differ they may not be aliased)
                                cantAggr = true;
                                break;
                            }
                        }
                    }
                }
                // Make sure not propagating past a {enter,exit}Monitor
                if (propagatingInvokeExpr && between instanceof MonitorStmt)
                    cantAggr = true;
            }
            // Check for intervening side effects due to method calls
            if (propagatingInvokeExpr || propagatingFieldRef || propagatingArrayRef) {
                for (final ValueBox box : between.getUseBoxes()) {
                    if (between == use && box == useBox) {
                        // side effects
                        break;
                    }
                    Value v = box.getValue();
                    if (v instanceof InvokeExpr || (propagatingInvokeExpr && (v instanceof FieldRef || v instanceof ArrayRef))) {
                        cantAggr = true;
                        break;
                    }
                }
            }
        }
        // we give up: can't aggregate.
        if (cantAggr) {
            continue;
        }
        /* assuming that the d-u chains are correct, */
        /* we need not check the actual contents of ld */
        Value aggregatee = s.getRightOp();
        if (usepair.valueBox.canContainValue(aggregatee)) {
            boolean wasSimpleCopy = isSimpleCopy(usepair.unit);
            usepair.valueBox.setValue(aggregatee);
            units.remove(s);
            hadAggregation = true;
            // followed by an invoke, the invoke gets the tags.
            if (wasSimpleCopy) {
                // usepair.unit.removeAllTags();
                usepair.unit.addAllTagsOf(s);
            }
        } else {
        /*
					 * if(Options.v().verbose()) {
					 * logger.debug("[debug] failed aggregation");
					 * logger.debug("[debug] tried to put "+aggregatee+
					 * " into "+usepair.stmt +
					 * ": in particular, "+usepair.valueBox);
					 * logger.debug("[debug] aggregatee instanceof Expr: "
					 * +(aggregatee instanceof Expr)); }
					 */
        }
    }
    return hadAggregation;
}
Also used : FieldRef(soot.jimple.FieldRef) AssignStmt(soot.jimple.AssignStmt) ArrayList(java.util.ArrayList) Local(soot.Local) LocalUses(soot.toolkits.scalar.LocalUses) Unit(soot.Unit) LocalDefs(soot.toolkits.scalar.LocalDefs) MonitorStmt(soot.jimple.MonitorStmt) MonitorStmt(soot.jimple.MonitorStmt) Stmt(soot.jimple.Stmt) AssignStmt(soot.jimple.AssignStmt) DefinitionStmt(soot.jimple.DefinitionStmt) ArrayRef(soot.jimple.ArrayRef) ExceptionalUnitGraph(soot.toolkits.graph.ExceptionalUnitGraph) InvokeExpr(soot.jimple.InvokeExpr) ValueBox(soot.ValueBox) Value(soot.Value) UnitValueBoxPair(soot.toolkits.scalar.UnitValueBoxPair)

Example 4 with LocalUses

use of soot.toolkits.scalar.LocalUses in project soot by Sable.

the class DexReturnValuePropagator method internalTransform.

@Override
protected void internalTransform(Body body, String phaseName, Map<String, String> options) {
    ExceptionalUnitGraph graph = new ExceptionalUnitGraph(body, DalvikThrowAnalysis.v(), true);
    LocalDefs localDefs = LocalDefs.Factory.newLocalDefs(graph);
    LocalUses localUses = null;
    LocalCreation localCreation = null;
    // a copy statement, we take the original operand
    for (Unit u : body.getUnits()) if (u instanceof ReturnStmt) {
        ReturnStmt retStmt = (ReturnStmt) u;
        if (retStmt.getOp() instanceof Local) {
            List<Unit> defs = localDefs.getDefsOfAt((Local) retStmt.getOp(), retStmt);
            if (defs.size() == 1 && defs.get(0) instanceof AssignStmt) {
                AssignStmt assign = (AssignStmt) defs.get(0);
                final Value rightOp = assign.getRightOp();
                final Value leftOp = assign.getLeftOp();
                // Copy over the left side if it is a local
                if (rightOp instanceof Local) {
                    // to return a;
                    if (!isRedefined((Local) rightOp, u, assign, graph))
                        retStmt.setOp(rightOp);
                } else if (rightOp instanceof Constant) {
                    retStmt.setOp(rightOp);
                } else // we rename the local to help splitting
                if (rightOp instanceof FieldRef) {
                    if (localUses == null)
                        localUses = LocalUses.Factory.newLocalUses(body, localDefs);
                    if (localUses.getUsesOf(assign).size() == 1) {
                        if (localCreation == null)
                            localCreation = new LocalCreation(body.getLocals(), "ret");
                        Local newLocal = localCreation.newLocal(leftOp.getType());
                        assign.setLeftOp(newLocal);
                        retStmt.setOp(newLocal);
                    }
                }
            }
        }
    }
}
Also used : ExceptionalUnitGraph(soot.toolkits.graph.ExceptionalUnitGraph) FieldRef(soot.jimple.FieldRef) LocalCreation(soot.jimple.toolkits.scalar.LocalCreation) AssignStmt(soot.jimple.AssignStmt) Constant(soot.jimple.Constant) Value(soot.Value) Local(soot.Local) ArrayList(java.util.ArrayList) List(java.util.List) LocalUses(soot.toolkits.scalar.LocalUses) Unit(soot.Unit) LocalDefs(soot.toolkits.scalar.LocalDefs) ReturnStmt(soot.jimple.ReturnStmt)

Aggregations

ArrayList (java.util.ArrayList)4 Local (soot.Local)4 Unit (soot.Unit)4 Value (soot.Value)4 AssignStmt (soot.jimple.AssignStmt)4 LocalUses (soot.toolkits.scalar.LocalUses)4 FieldRef (soot.jimple.FieldRef)3 Stmt (soot.jimple.Stmt)3 LocalDefs (soot.toolkits.scalar.LocalDefs)3 UnitValueBoxPair (soot.toolkits.scalar.UnitValueBoxPair)3 List (java.util.List)2 ValueBox (soot.ValueBox)2 ArrayRef (soot.jimple.ArrayRef)2 InvokeExpr (soot.jimple.InvokeExpr)2 NewExpr (soot.jimple.NewExpr)2 ArrayDeque (java.util.ArrayDeque)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 IntType (soot.IntType)1