Search in sources :

Example 1 with LocalDefs

use of soot.toolkits.scalar.LocalDefs 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 LocalDefs

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

the class Validate method validateArrays.

public static void validateArrays(Body b) {
    Set<DefinitionStmt> definitions = new HashSet<DefinitionStmt>();
    Set<Unit> unitWithArrayRef = new HashSet<Unit>();
    for (Unit u : b.getUnits()) {
        if (u instanceof DefinitionStmt) {
            DefinitionStmt s = (DefinitionStmt) u;
            definitions.add(s);
        }
        List<ValueBox> uses = u.getUseBoxes();
        for (ValueBox vb : uses) {
            Value v = vb.getValue();
            if (v instanceof ArrayRef) {
                unitWithArrayRef.add(u);
            }
        }
    }
    final LocalDefs localDefs = LocalDefs.Factory.newLocalDefs(b, true);
    Set<Unit> toReplace = new HashSet<Unit>();
    for (Unit u : unitWithArrayRef) {
        boolean ok = false;
        List<ValueBox> uses = u.getUseBoxes();
        for (ValueBox vb : uses) {
            Value v = vb.getValue();
            if (v instanceof ArrayRef) {
                ArrayRef ar = (ArrayRef) v;
                Local base = (Local) ar.getBase();
                List<Unit> defs = localDefs.getDefsOfAt(base, u);
                // add aliases
                Set<Unit> alreadyHandled = new HashSet<Unit>();
                while (true) {
                    boolean isMore = false;
                    for (Unit d : defs) {
                        if (alreadyHandled.contains(d))
                            continue;
                        if (d instanceof AssignStmt) {
                            AssignStmt ass = (AssignStmt) d;
                            Value r = ass.getRightOp();
                            if (r instanceof Local) {
                                defs.addAll(localDefs.getDefsOfAt((Local) r, d));
                                alreadyHandled.add(d);
                                isMore = true;
                                break;
                            } else if (r instanceof ArrayRef) {
                                ArrayRef arrayRef = (ArrayRef) r;
                                Local l = (Local) arrayRef.getBase();
                                defs.addAll(localDefs.getDefsOfAt(l, d));
                                alreadyHandled.add(d);
                                isMore = true;
                                break;
                            }
                        }
                    }
                    if (!isMore)
                        break;
                }
                // System.out.println("def size "+ defs.size());
                for (Unit def : defs) {
                    // System.out.println("def u "+ def);
                    Value r = null;
                    if (def instanceof IdentityStmt) {
                        IdentityStmt idstmt = (IdentityStmt) def;
                        r = idstmt.getRightOp();
                    } else if (def instanceof AssignStmt) {
                        AssignStmt assStmt = (AssignStmt) def;
                        r = assStmt.getRightOp();
                    } else {
                        throw new RuntimeException("error: definition statement not an IdentityStmt nor an AssignStmt! " + def);
                    }
                    Type t = null;
                    if (r instanceof InvokeExpr) {
                        InvokeExpr ie = (InvokeExpr) r;
                        t = ie.getType();
                        // System.out.println("ie type: "+ t +" "+ t.getClass());
                        if (t instanceof ArrayType)
                            ok = true;
                    } else if (r instanceof FieldRef) {
                        FieldRef ref = (FieldRef) r;
                        t = ref.getType();
                        // System.out.println("fr type: "+ t +" "+ t.getClass());
                        if (t instanceof ArrayType)
                            ok = true;
                    } else if (r instanceof IdentityRef) {
                        IdentityRef ir = (IdentityRef) r;
                        t = ir.getType();
                        if (t instanceof ArrayType)
                            ok = true;
                    } else if (r instanceof CastExpr) {
                        CastExpr c = (CastExpr) r;
                        t = c.getType();
                        if (t instanceof ArrayType)
                            ok = true;
                    } else if (r instanceof ArrayRef) {
                    // we also check that this arrayref is correctly defined
                    } else if (r instanceof NewArrayExpr) {
                        ok = true;
                    } else if (r instanceof Local) {
                    } else if (r instanceof Constant) {
                    } else {
                        throw new RuntimeException("error: unknown right hand side of definition stmt " + def);
                    }
                    if (ok)
                        break;
                }
                if (ok)
                    break;
            }
        }
        if (!ok) {
            toReplace.add(u);
        }
    }
    int i = 0;
    for (Unit u : toReplace) {
        System.out.println("warning: incorrect array def, replacing unit " + u);
        // new object
        RefType throwableType = RefType.v("java.lang.Throwable");
        Local ttt = Jimple.v().newLocal("ttt_" + ++i, throwableType);
        b.getLocals().add(ttt);
        Value r = Jimple.v().newNewExpr(throwableType);
        Unit initLocalUnit = Jimple.v().newAssignStmt(ttt, r);
        // call <init> method with a string parameter for message
        List<String> pTypes = new ArrayList<String>();
        pTypes.add("java.lang.String");
        boolean isStatic = false;
        SootMethodRef mRef = Validate.makeMethodRef("java.lang.Throwable", "<init>", "", pTypes, isStatic);
        List<Value> parameters = new ArrayList<Value>();
        parameters.add(StringConstant.v("Soot updated this instruction"));
        InvokeExpr ie = Jimple.v().newSpecialInvokeExpr(ttt, mRef, parameters);
        Unit initMethod = Jimple.v().newInvokeStmt(ie);
        // throw exception
        Unit newUnit = Jimple.v().newThrowStmt(ttt);
        // change instruction in body
        b.getUnits().swapWith(u, newUnit);
        b.getUnits().insertBefore(initMethod, newUnit);
        b.getUnits().insertBefore(initLocalUnit, initMethod);
    // Exception a = throw new Exception();
    }
    DeadAssignmentEliminator.v().transform(b);
    UnusedLocalEliminator.v().transform(b);
    NopEliminator.v().transform(b);
    UnreachableCodeEliminator.v().transform(b);
}
Also used : AssignStmt(soot.jimple.AssignStmt) Constant(soot.jimple.Constant) StringConstant(soot.jimple.StringConstant) ArrayList(java.util.ArrayList) Unit(soot.Unit) ArrayRef(soot.jimple.ArrayRef) ArrayType(soot.ArrayType) RefType(soot.RefType) InvokeExpr(soot.jimple.InvokeExpr) IdentityRef(soot.jimple.IdentityRef) CastExpr(soot.jimple.CastExpr) IdentityStmt(soot.jimple.IdentityStmt) HashSet(java.util.HashSet) FieldRef(soot.jimple.FieldRef) SootMethodRef(soot.SootMethodRef) Local(soot.Local) LocalDefs(soot.toolkits.scalar.LocalDefs) RefType(soot.RefType) ArrayType(soot.ArrayType) Type(soot.Type) VoidType(soot.VoidType) NewArrayExpr(soot.jimple.NewArrayExpr) ValueBox(soot.ValueBox) Value(soot.Value) DefinitionStmt(soot.jimple.DefinitionStmt)

Example 3 with LocalDefs

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

the class TypeResolver method split_new.

/* Taken from the soot.jimple.toolkits.typing.TypeResolver class of Soot
	version 2.2.5. */
private void split_new() {
    LocalDefs defs = LocalDefs.Factory.newLocalDefs(jb);
    PatchingChain<Unit> units = this.jb.getUnits();
    Stmt[] stmts = new Stmt[units.size()];
    units.toArray(stmts);
    final Jimple jimple = Jimple.v();
    for (Stmt stmt : stmts) {
        if (stmt instanceof InvokeStmt) {
            InvokeStmt invoke = (InvokeStmt) stmt;
            if (invoke.getInvokeExpr() instanceof SpecialInvokeExpr) {
                SpecialInvokeExpr special = (SpecialInvokeExpr) invoke.getInvokeExpr();
                if (special.getMethodRef().name().equals("<init>")) {
                    List<Unit> deflist = defs.getDefsOfAt((Local) special.getBase(), invoke);
                    while (deflist.size() == 1) {
                        Stmt stmt2 = (Stmt) deflist.get(0);
                        if (stmt2 instanceof AssignStmt) {
                            AssignStmt assign = (AssignStmt) stmt2;
                            if (assign.getRightOp() instanceof Local) {
                                deflist = defs.getDefsOfAt((Local) assign.getRightOp(), assign);
                                continue;
                            } else if (assign.getRightOp() instanceof NewExpr) {
                                Local newlocal = jimple.newLocal("tmp", null);
                                newlocal.setName("tmp$" + System.identityHashCode(newlocal));
                                this.jb.getLocals().add(newlocal);
                                special.setBase(newlocal);
                                DefinitionStmt assignStmt = jimple.newAssignStmt(assign.getLeftOp(), newlocal);
                                Unit u = Util.findLastIdentityUnit(jb, assign);
                                units.insertAfter(assignStmt, u);
                                assign.setLeftOp(newlocal);
                                this.addLocal(newlocal);
                                this.initAssignment(assignStmt);
                            }
                        }
                        break;
                    }
                }
            }
        }
    }
}
Also used : InvokeStmt(soot.jimple.InvokeStmt) AssignStmt(soot.jimple.AssignStmt) SpecialInvokeExpr(soot.jimple.SpecialInvokeExpr) Local(soot.Local) Unit(soot.Unit) LocalDefs(soot.toolkits.scalar.LocalDefs) InvokeStmt(soot.jimple.InvokeStmt) Stmt(soot.jimple.Stmt) AssignStmt(soot.jimple.AssignStmt) DefinitionStmt(soot.jimple.DefinitionStmt) NewExpr(soot.jimple.NewExpr) Jimple(soot.jimple.Jimple) DefinitionStmt(soot.jimple.DefinitionStmt)

Example 4 with LocalDefs

use of soot.toolkits.scalar.LocalDefs 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 5 with LocalDefs

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

the class CopyPropagator method internalTransform.

/**
 * Cascaded copy propagator.
 *
 * If it encounters situations of the form: A: a = ...; B: ... x = a; C:...
 * use (x); where a has only one definition, and x has only one definition
 * (B), then it can propagate immediately without checking between B and C
 * for redefinitions of a (namely) A because they cannot occur. In this case
 * the propagator is global.
 *
 * Otherwise, if a has multiple definitions then it only checks for
 * redefinitions of Propagates constants and copies in extended basic
 * blocks.
 *
 * Does not propagate stack locals when the "only-regular-locals" option is
 * true.
 */
protected void internalTransform(Body b, String phaseName, Map<String, String> opts) {
    CPOptions options = new CPOptions(opts);
    StmtBody stmtBody = (StmtBody) b;
    int fastCopyPropagationCount = 0;
    int slowCopyPropagationCount = 0;
    if (Options.v().verbose())
        logger.debug("[" + stmtBody.getMethod().getName() + "] Propagating copies...");
    if (Options.v().time())
        Timers.v().propagatorTimer.start();
    Chain<Unit> units = stmtBody.getUnits();
    Map<Local, Integer> localToDefCount = new HashMap<Local, Integer>();
    // Count number of definitions for each local.
    for (Unit u : units) {
        Stmt s = (Stmt) u;
        if (s instanceof DefinitionStmt && ((DefinitionStmt) s).getLeftOp() instanceof Local) {
            Local l = (Local) ((DefinitionStmt) s).getLeftOp();
            if (!localToDefCount.containsKey(l))
                localToDefCount.put(l, new Integer(1));
            else
                localToDefCount.put(l, new Integer(localToDefCount.get(l).intValue() + 1));
        }
    }
    if (throwAnalysis == null)
        throwAnalysis = Scene.v().getDefaultThrowAnalysis();
    if (forceOmitExceptingUnitEdges == false)
        forceOmitExceptingUnitEdges = Options.v().omit_excepting_unit_edges();
    // Go through the definitions, building the webs
    UnitGraph graph = new ExceptionalUnitGraph(stmtBody, throwAnalysis, forceOmitExceptingUnitEdges);
    LocalDefs localDefs = LocalDefs.Factory.newLocalDefs(graph);
    // Perform a local propagation pass.
    {
        Iterator<Unit> stmtIt = (new PseudoTopologicalOrderer<Unit>()).newList(graph, false).iterator();
        while (stmtIt.hasNext()) {
            Stmt stmt = (Stmt) stmtIt.next();
            for (ValueBox useBox : stmt.getUseBoxes()) {
                if (useBox.getValue() instanceof Local) {
                    Local l = (Local) useBox.getValue();
                    // null due to typing, we always inline that constant.
                    if (!(l.getType() instanceof NullType)) {
                        if (options.only_regular_locals() && l.getName().startsWith("$"))
                            continue;
                        if (options.only_stack_locals() && !l.getName().startsWith("$"))
                            continue;
                    }
                    List<Unit> defsOfUse = localDefs.getDefsOfAt(l, stmt);
                    // We can propagate the definition if we either only
                    // have
                    // one definition or all definitions are side-effect
                    // free
                    // and equal. For starters, we only support constants in
                    // the case of multiple definitions.
                    boolean propagateDef = defsOfUse.size() == 1;
                    if (!propagateDef && defsOfUse.size() > 0) {
                        boolean agrees = true;
                        Constant constVal = null;
                        for (Unit defUnit : defsOfUse) {
                            boolean defAgrees = false;
                            if (defUnit instanceof AssignStmt) {
                                AssignStmt assign = (AssignStmt) defUnit;
                                if (assign.getRightOp() instanceof Constant) {
                                    if (constVal == null) {
                                        constVal = (Constant) assign.getRightOp();
                                        defAgrees = true;
                                    } else if (constVal.equals(assign.getRightOp()))
                                        defAgrees = true;
                                }
                            }
                            agrees &= defAgrees;
                        }
                        propagateDef = agrees;
                    }
                    if (propagateDef) {
                        DefinitionStmt def = (DefinitionStmt) defsOfUse.get(0);
                        if (def.getRightOp() instanceof Constant) {
                            if (useBox.canContainValue(def.getRightOp())) {
                                useBox.setValue(def.getRightOp());
                            }
                        } else if (def.getRightOp() instanceof CastExpr) {
                            CastExpr ce = (CastExpr) def.getRightOp();
                            if (ce.getCastType() instanceof RefLikeType) {
                                boolean isConstNull = ce.getOp() instanceof IntConstant && ((IntConstant) ce.getOp()).value == 0;
                                isConstNull |= ce.getOp() instanceof LongConstant && ((LongConstant) ce.getOp()).value == 0;
                                if (isConstNull) {
                                    if (useBox.canContainValue(NullConstant.v())) {
                                        useBox.setValue(NullConstant.v());
                                    }
                                }
                            }
                        } else if (def.getRightOp() instanceof Local) {
                            Local m = (Local) def.getRightOp();
                            if (l != m) {
                                Integer defCount = localToDefCount.get(m);
                                if (defCount == null || defCount == 0)
                                    throw new RuntimeException("Variable " + m + " used without definition!");
                                if (defCount == 1) {
                                    useBox.setValue(m);
                                    fastCopyPropagationCount++;
                                    continue;
                                }
                                List<Unit> path = graph.getExtendedBasicBlockPathBetween(def, stmt);
                                if (path == null) {
                                    // no path in the extended basic block
                                    continue;
                                }
                                Iterator<Unit> pathIt = path.iterator();
                                // Skip first node
                                pathIt.next();
                                // Make sure that m is not redefined along
                                // path
                                {
                                    boolean isRedefined = false;
                                    while (pathIt.hasNext()) {
                                        Stmt s = (Stmt) pathIt.next();
                                        if (stmt == s) {
                                            break;
                                        }
                                        if (s instanceof DefinitionStmt) {
                                            if (((DefinitionStmt) s).getLeftOp() == m) {
                                                isRedefined = true;
                                                break;
                                            }
                                        }
                                    }
                                    if (isRedefined)
                                        continue;
                                }
                                useBox.setValue(m);
                                slowCopyPropagationCount++;
                            }
                        }
                    }
                }
            }
        }
    }
    if (Options.v().verbose())
        logger.debug("[" + stmtBody.getMethod().getName() + "]     Propagated: " + fastCopyPropagationCount + " fast copies  " + slowCopyPropagationCount + " slow copies");
    if (Options.v().time())
        Timers.v().propagatorTimer.end();
}
Also used : HashMap(java.util.HashMap) NullConstant(soot.jimple.NullConstant) Constant(soot.jimple.Constant) LongConstant(soot.jimple.LongConstant) IntConstant(soot.jimple.IntConstant) AssignStmt(soot.jimple.AssignStmt) Unit(soot.Unit) Stmt(soot.jimple.Stmt) AssignStmt(soot.jimple.AssignStmt) DefinitionStmt(soot.jimple.DefinitionStmt) RefLikeType(soot.RefLikeType) ExceptionalUnitGraph(soot.toolkits.graph.ExceptionalUnitGraph) Iterator(java.util.Iterator) CastExpr(soot.jimple.CastExpr) IntConstant(soot.jimple.IntConstant) List(java.util.List) LongConstant(soot.jimple.LongConstant) PseudoTopologicalOrderer(soot.toolkits.graph.PseudoTopologicalOrderer) Local(soot.Local) LocalDefs(soot.toolkits.scalar.LocalDefs) ExceptionalUnitGraph(soot.toolkits.graph.ExceptionalUnitGraph) UnitGraph(soot.toolkits.graph.UnitGraph) ValueBox(soot.ValueBox) CPOptions(soot.options.CPOptions) NullType(soot.NullType) StmtBody(soot.jimple.StmtBody) DefinitionStmt(soot.jimple.DefinitionStmt)

Aggregations

Local (soot.Local)9 Unit (soot.Unit)9 AssignStmt (soot.jimple.AssignStmt)9 LocalDefs (soot.toolkits.scalar.LocalDefs)9 Stmt (soot.jimple.Stmt)7 Value (soot.Value)5 DefinitionStmt (soot.jimple.DefinitionStmt)5 ArrayList (java.util.ArrayList)4 ValueBox (soot.ValueBox)4 FieldRef (soot.jimple.FieldRef)4 NewExpr (soot.jimple.NewExpr)4 ArrayRef (soot.jimple.ArrayRef)3 CastExpr (soot.jimple.CastExpr)3 Constant (soot.jimple.Constant)3 IntConstant (soot.jimple.IntConstant)3 InvokeExpr (soot.jimple.InvokeExpr)3 InvokeStmt (soot.jimple.InvokeStmt)3 ExceptionalUnitGraph (soot.toolkits.graph.ExceptionalUnitGraph)3 LocalUses (soot.toolkits.scalar.LocalUses)3 HashSet (java.util.HashSet)2