Search in sources :

Example 46 with Unit

use of soot.Unit in project soot by Sable.

the class ConstantValueToInitializerTransformer method getOrCreateInitializer.

private SootMethod getOrCreateInitializer(SootClass sc, Set<SootField> alreadyInitialized) {
    SootMethod smInit;
    // Create a static initializer if we don't already have one
    smInit = sc.getMethodByNameUnsafe("<clinit>");
    if (smInit == null) {
        smInit = Scene.v().makeSootMethod("<clinit>", Collections.<Type>emptyList(), VoidType.v());
        smInit.setActiveBody(Jimple.v().newBody(smInit));
        sc.addMethod(smInit);
        smInit.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
    } else if (smInit.isPhantom())
        return null;
    else {
        smInit.retrieveActiveBody();
        // somewhere
        for (Unit u : smInit.getActiveBody().getUnits()) {
            Stmt s = (Stmt) u;
            for (ValueBox vb : s.getDefBoxes()) if (vb.getValue() instanceof FieldRef)
                alreadyInitialized.add(((FieldRef) vb.getValue()).getField());
        }
    }
    return smInit;
}
Also used : Type(soot.Type) VoidType(soot.VoidType) FieldRef(soot.jimple.FieldRef) ValueBox(soot.ValueBox) SootMethod(soot.SootMethod) Unit(soot.Unit) ReturnVoidStmt(soot.jimple.ReturnVoidStmt) Stmt(soot.jimple.Stmt)

Example 47 with Unit

use of soot.Unit in project soot by Sable.

the class ConstantValueToInitializerTransformer method transformClass.

public void transformClass(SootClass sc) {
    SootMethod smInit = null;
    Set<SootField> alreadyInitialized = new HashSet<SootField>();
    for (SootField sf : sc.getFields()) {
        // different constructors might assign different values.
        if (!sf.isStatic() || !sf.isFinal())
            continue;
        // generate a second one
        if (alreadyInitialized.contains(sf))
            continue;
        // Look for constant values
        for (Tag t : sf.getTags()) {
            Stmt initStmt = null;
            if (t instanceof DoubleConstantValueTag) {
                double value = ((DoubleConstantValueTag) t).getDoubleValue();
                initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), DoubleConstant.v(value));
            } else if (t instanceof FloatConstantValueTag) {
                float value = ((FloatConstantValueTag) t).getFloatValue();
                initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), FloatConstant.v(value));
            } else if (t instanceof IntegerConstantValueTag) {
                int value = ((IntegerConstantValueTag) t).getIntValue();
                initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), IntConstant.v(value));
            } else if (t instanceof LongConstantValueTag) {
                long value = ((LongConstantValueTag) t).getLongValue();
                initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), LongConstant.v(value));
            } else if (t instanceof StringConstantValueTag) {
                String value = ((StringConstantValueTag) t).getStringValue();
                initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), StringConstant.v(value));
            }
            if (initStmt != null) {
                if (smInit == null)
                    smInit = getOrCreateInitializer(sc, alreadyInitialized);
                if (smInit != null)
                    smInit.getActiveBody().getUnits().addFirst(initStmt);
            }
        }
    }
    if (smInit != null) {
        Chain<Unit> units = smInit.getActiveBody().getUnits();
        if (units.isEmpty() || !(units.getLast() instanceof ReturnVoidStmt))
            units.add(Jimple.v().newReturnVoidStmt());
    }
}
Also used : ReturnVoidStmt(soot.jimple.ReturnVoidStmt) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) Unit(soot.Unit) ReturnVoidStmt(soot.jimple.ReturnVoidStmt) Stmt(soot.jimple.Stmt) SootMethod(soot.SootMethod) LongConstantValueTag(soot.tagkit.LongConstantValueTag) SootField(soot.SootField) Tag(soot.tagkit.Tag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) LongConstantValueTag(soot.tagkit.LongConstantValueTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) HashSet(java.util.HashSet) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag)

Example 48 with Unit

use of soot.Unit in project soot by Sable.

the class BadFields method handleMethod.

private void handleMethod(SootMethod m) {
    if (!m.isConcrete())
        return;
    for (Iterator<ValueBox> bIt = m.retrieveActiveBody().getUseAndDefBoxes().iterator(); bIt.hasNext(); ) {
        final ValueBox b = bIt.next();
        Value v = b.getValue();
        if (!(v instanceof StaticFieldRef))
            continue;
        StaticFieldRef sfr = (StaticFieldRef) v;
        SootField f = sfr.getField();
        if (!f.getDeclaringClass().getName().equals("java.lang.System"))
            continue;
        if (f.getName().equals("err")) {
            logger.debug("" + "Use of System.err in " + m);
        }
        if (f.getName().equals("out")) {
            logger.debug("" + "Use of System.out in " + m);
        }
    }
    for (Iterator<Unit> sIt = m.getActiveBody().getUnits().iterator(); sIt.hasNext(); ) {
        final Stmt s = (Stmt) sIt.next();
        if (!s.containsInvokeExpr())
            continue;
        InvokeExpr ie = s.getInvokeExpr();
        SootMethod target = ie.getMethod();
        if (target.getDeclaringClass().getName().equals("java.lang.System") && target.getName().equals("exit")) {
            warn("" + m + " calls System.exit");
        }
    }
    if (m.getName().equals("<clinit>")) {
        for (Iterator<Unit> sIt = m.getActiveBody().getUnits().iterator(); sIt.hasNext(); ) {
            final Stmt s = (Stmt) sIt.next();
            for (Iterator<ValueBox> bIt = s.getUseBoxes().iterator(); bIt.hasNext(); ) {
                final ValueBox b = bIt.next();
                Value v = b.getValue();
                if (v instanceof FieldRef) {
                    warn(m.getName() + " reads field " + v);
                }
            }
            if (!s.containsInvokeExpr())
                continue;
            InvokeExpr ie = s.getInvokeExpr();
            SootMethod target = ie.getMethod();
            calls(target);
        }
    }
}
Also used : InvokeExpr(soot.jimple.InvokeExpr) StaticFieldRef(soot.jimple.StaticFieldRef) FieldRef(soot.jimple.FieldRef) ValueBox(soot.ValueBox) Value(soot.Value) SootMethod(soot.SootMethod) SootField(soot.SootField) Unit(soot.Unit) StaticFieldRef(soot.jimple.StaticFieldRef) Stmt(soot.jimple.Stmt)

Example 49 with Unit

use of soot.Unit in project soot by Sable.

the class UnitGraph method buildUnexceptionalEdges.

/**
 * Utility method for <tt>UnitGraph</tt> constructors. It computes the edges
 * corresponding to unexceptional control flow.
 *
 * @param unitToSuccs
 *            A {@link Map} from {@link Unit}s to {@link List}s of
 *            {@link Unit}s. This is an ``out parameter''; callers must pass
 *            an empty {@link Map}. <tt>buildUnexceptionalEdges</tt> will
 *            add a mapping for every <tt>Unit</tt> in the body to a list of
 *            its unexceptional successors.
 *
 * @param unitToPreds
 *            A {@link Map} from {@link Unit}s to {@link List}s of
 *            {@link Unit}s. This is an ``out parameter''; callers must pass
 *            an empty {@link Map}. <tt>buildUnexceptionalEdges</tt> will
 *            add a mapping for every <tt>Unit</tt> in the body to a list of
 *            its unexceptional predecessors.
 */
protected void buildUnexceptionalEdges(Map<Unit, List<Unit>> unitToSuccs, Map<Unit, List<Unit>> unitToPreds) {
    Iterator<Unit> unitIt = unitChain.iterator();
    Unit currentUnit, nextUnit;
    nextUnit = unitIt.hasNext() ? (Unit) unitIt.next() : null;
    while (nextUnit != null) {
        currentUnit = nextUnit;
        nextUnit = unitIt.hasNext() ? (Unit) unitIt.next() : null;
        ArrayList<Unit> successors = new ArrayList<Unit>();
        if (currentUnit.fallsThrough()) {
            // Add the next unit as the successor
            if (nextUnit != null) {
                successors.add(nextUnit);
                List<Unit> preds = unitToPreds.get(nextUnit);
                if (preds == null) {
                    preds = new ArrayList<Unit>();
                    unitToPreds.put(nextUnit, preds);
                }
                preds.add(currentUnit);
            }
        }
        if (currentUnit.branches()) {
            for (UnitBox targetBox : currentUnit.getUnitBoxes()) {
                Unit target = targetBox.getUnit();
                // target it falls through to, so we screen for duplicates:
                if (!successors.contains(target)) {
                    successors.add(target);
                    List<Unit> preds = unitToPreds.get(target);
                    if (preds == null) {
                        preds = new ArrayList<Unit>();
                        unitToPreds.put(target, preds);
                    }
                    preds.add(currentUnit);
                }
            }
        }
        // Store away successors
        if (!successors.isEmpty()) {
            successors.trimToSize();
            unitToSuccs.put(currentUnit, successors);
        }
    }
}
Also used : UnitBox(soot.UnitBox) ArrayList(java.util.ArrayList) Unit(soot.Unit)

Example 50 with Unit

use of soot.Unit in project soot by Sable.

the class UnitGraph method getExtendedBasicBlockPathBetween.

/**
 * Look for a path in graph, from def to use. This path has to lie inside an
 * extended basic block (and this property implies uniqueness.). The path
 * returned includes from and to.
 *
 * @param from
 *            start point for the path.
 * @param to
 *            end point for the path.
 * @return null if there is no such path.
 */
public List<Unit> getExtendedBasicBlockPathBetween(Unit from, Unit to) {
    UnitGraph g = this;
    // if this holds, we're doomed to failure!!!
    if (g.getPredsOf(to).size() > 1)
        return null;
    // pathStack := list of succs lists
    // pathStackIndex := last visited index in pathStack
    LinkedList<Unit> pathStack = new LinkedList<Unit>();
    LinkedList<Integer> pathStackIndex = new LinkedList<Integer>();
    pathStack.add(from);
    pathStackIndex.add(new Integer(0));
    int psiMax = (g.getSuccsOf(pathStack.get(0))).size();
    int level = 0;
    while (pathStackIndex.get(0).intValue() != psiMax) {
        int p = (pathStackIndex.get(level)).intValue();
        List<Unit> succs = g.getSuccsOf((pathStack.get(level)));
        if (p >= succs.size()) {
            // no more succs - backtrack to previous level.
            pathStack.remove(level);
            pathStackIndex.remove(level);
            level--;
            int q = pathStackIndex.get(level).intValue();
            pathStackIndex.set(level, new Integer(q + 1));
            continue;
        }
        Unit betweenUnit = (Unit) (succs.get(p));
        // we win!
        if (betweenUnit == to) {
            pathStack.add(to);
            return pathStack;
        }
        // check preds of betweenUnit to see if we should visit its kids.
        if (g.getPredsOf(betweenUnit).size() > 1) {
            pathStackIndex.set(level, new Integer(p + 1));
            continue;
        }
        // visit kids of betweenUnit.
        level++;
        pathStackIndex.add(new Integer(0));
        pathStack.add(betweenUnit);
    }
    return null;
}
Also used : Unit(soot.Unit) LinkedList(java.util.LinkedList)

Aggregations

Unit (soot.Unit)240 Local (soot.Local)77 Stmt (soot.jimple.Stmt)77 Value (soot.Value)74 ArrayList (java.util.ArrayList)65 AssignStmt (soot.jimple.AssignStmt)58 SootMethod (soot.SootMethod)47 Body (soot.Body)37 InvokeStmt (soot.jimple.InvokeStmt)35 Type (soot.Type)34 HashSet (java.util.HashSet)33 ValueBox (soot.ValueBox)33 InvokeExpr (soot.jimple.InvokeExpr)33 Trap (soot.Trap)32 RefType (soot.RefType)30 IdentityStmt (soot.jimple.IdentityStmt)28 HashMap (java.util.HashMap)27 IfStmt (soot.jimple.IfStmt)27 DefinitionStmt (soot.jimple.DefinitionStmt)25 List (java.util.List)23