Search in sources :

Example 26 with SootField

use of soot.SootField 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 27 with SootField

use of soot.SootField 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 28 with SootField

use of soot.SootField in project soot by Sable.

the class BadFields method handleClass.

private void handleClass(SootClass cl) {
    for (Iterator<SootField> fIt = cl.getFields().iterator(); fIt.hasNext(); ) {
        final SootField f = fIt.next();
        if (!f.isStatic())
            continue;
        String typeName = f.getType().toString();
        if (typeName.equals("java.lang.Class"))
            continue;
        if (f.isFinal()) {
            if (f.getType() instanceof PrimType)
                continue;
            if (typeName.equals("java.io.PrintStream"))
                continue;
            if (typeName.equals("java.lang.String"))
                continue;
            if (typeName.equals("java.lang.Object"))
                continue;
            if (typeName.equals("java.lang.Integer"))
                continue;
            if (typeName.equals("java.lang.Boolean"))
                continue;
        }
        warn("Bad field " + f);
    }
}
Also used : PrimType(soot.PrimType) SootField(soot.SootField)

Example 29 with SootField

use of soot.SootField in project soot by Sable.

the class PointsToAnalysis method main.

public static void main(String[] args) {
    loadClass("Item", false);
    loadClass("Container", false);
    SootClass c = loadClass(args[1], true);
    soot.Scene.v().loadNecessaryClasses();
    soot.Scene.v().setEntryPoints(EntryPoints.v().all());
    if (args[0].equals("paddle"))
        setPaddlePointsToAnalysis();
    else if (args[0].equals("spark"))
        setSparkPointsToAnalysis();
    SootField f = getField("Container", "item");
    Map /*<Local>*/
    ls = getLocals(c, args[2], "Container");
    printLocalIntersects(ls);
    printFieldIntersects(ls, f);
}
Also used : SootField(soot.SootField) SootClass(soot.SootClass) HashMap(java.util.HashMap) Map(java.util.Map)

Example 30 with SootField

use of soot.SootField in project soot by Sable.

the class DexField method makeSootField.

/**
 * @return the Soot equivalent of a field
 */
public static SootField makeSootField(Field f) {
    String name = f.getName();
    Type type = DexType.toSoot(f.getType());
    int flags = f.getAccessFlags();
    SootField sf = Scene.v().makeSootField(name, type, flags);
    if (Modifier.isFinal(flags))
        DexField.addConstantTag(sf, f);
    return sf;
}
Also used : Type(soot.Type) SootField(soot.SootField)

Aggregations

SootField (soot.SootField)73 SootMethod (soot.SootMethod)29 SootClass (soot.SootClass)26 RefType (soot.RefType)22 ArrayList (java.util.ArrayList)19 Value (soot.Value)17 Iterator (java.util.Iterator)15 Local (soot.Local)14 Type (soot.Type)14 Unit (soot.Unit)13 FieldRef (soot.jimple.FieldRef)12 BooleanType (soot.BooleanType)10 PrimType (soot.PrimType)10 VoidType (soot.VoidType)10 Stmt (soot.jimple.Stmt)10 ByteType (soot.ByteType)8 CharType (soot.CharType)8 DoubleType (soot.DoubleType)8 FloatType (soot.FloatType)8 IntType (soot.IntType)8