Search in sources :

Example 31 with FieldRef

use of soot.jimple.FieldRef in project soot by Sable.

the class FieldRenamer method internalTransform.

protected void internalTransform(String phaseName, Map<String, String> options) {
    if (output) {
        if (rename_fields) {
            out.println("Transforming Field Names and Adding Opaque Predicates...");
        } else {
            out.println("Adding Opaques...");
        }
    }
    RefType boolRef = Scene.v().getRefType(booleanClassName);
    BodyBuilder.retrieveAllBodies();
    BodyBuilder.retrieveAllNames();
    for (SootClass sc : Scene.v().getApplicationClasses()) {
        String className = sc.getName();
        if (className.contains(".")) {
            className = className.substring(className.lastIndexOf(".") + 1, className.length());
        }
        oldToNewFieldNames.put(className, className);
        if (rename_fields) {
            if (output) {
                out.println("\tClassName: " + className);
            }
            // rename all the fields in the class
            for (SootField f : sc.getFields()) {
                int weight = soot.jbco.Main.getWeight(phaseName, f.getName());
                if (weight > 0) {
                    renameField(className, f);
                }
            }
        }
        // skip interfaces - they can only hold final fields
        if (sc.isInterface()) {
            continue;
        }
        // add one opaq predicate for true and one for false to each class
        String bool = "opPred1";
        Type t = Rand.getInt() % 2 == 0 ? BooleanType.v() : boolRef;
        while (oldToNewFieldNames.containsKey(bool)) {
            bool += "_";
        }
        SootField f = Scene.v().makeSootField(bool, t, Modifier.PUBLIC | Modifier.STATIC);
        renameField(className, f);
        opaquePreds1ByClass.put(sc, f);
        sc.addField(f);
        setBooleanTo(sc, f, true);
        bool = "opPred2";
        t = t == BooleanType.v() ? boolRef : BooleanType.v();
        while (oldToNewFieldNames.containsKey(bool)) {
            bool += "_";
        }
        f = Scene.v().makeSootField(bool, t, Modifier.PUBLIC | Modifier.STATIC);
        renameField(className, f);
        opaquePreds2ByClass.put(sc, f);
        sc.addField(f);
        if (t == boolRef) {
            setBooleanTo(sc, f, false);
        }
    }
    buildOpaquePairings();
    if (!rename_fields) {
        return;
    }
    if (output) {
        out.println("\r\tUpdating field references in bytecode");
    }
    for (SootClass sc : Scene.v().getApplicationClasses()) {
        for (SootMethod m : sc.getMethods()) {
            if (!m.isConcrete()) {
                continue;
            }
            if (!m.hasActiveBody()) {
                m.retrieveActiveBody();
            }
            for (Unit unit : m.getActiveBody().getUnits()) {
                for (ValueBox box : unit.getUseAndDefBoxes()) {
                    Value value = box.getValue();
                    if (value instanceof FieldRef) {
                        FieldRef fieldRef = (FieldRef) value;
                        SootFieldRef sootFieldRef = fieldRef.getFieldRef();
                        if (sootFieldRef.declaringClass().isLibraryClass()) {
                            continue;
                        }
                        String oldName = sootFieldRef.name();
                        String fullName = sootFieldRef.declaringClass().getName() + '.' + oldName;
                        String newName = oldToNewFieldNames.get(oldName);
                        if (newName == null || namesToNotRename.contains(fullName)) {
                            continue;
                        }
                        if (newName.equals(oldName)) {
                            System.out.println("Strange.. Should not find a field with the same old and new name.");
                        }
                        sootFieldRef = Scene.v().makeFieldRef(sootFieldRef.declaringClass(), newName, sootFieldRef.type(), sootFieldRef.isStatic());
                        fieldRef.setFieldRef(sootFieldRef);
                        try {
                            sootFieldRef.resolve();
                        } catch (Exception e) {
                            System.err.println("********ERROR Updating " + sootFieldRef.name() + " to " + newName);
                            System.err.println("Fields of " + sootFieldRef.declaringClass().getName() + ": " + sootFieldRef.declaringClass().getFields());
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }
    }
}
Also used : FieldRef(soot.jimple.FieldRef) SootFieldRef(soot.SootFieldRef) SootClass(soot.SootClass) Unit(soot.Unit) SootFieldRef(soot.SootFieldRef) RefType(soot.RefType) RefType(soot.RefType) BooleanType(soot.BooleanType) IntegerType(soot.IntegerType) Type(soot.Type) VoidType(soot.VoidType) ValueBox(soot.ValueBox) Value(soot.Value) SootMethod(soot.SootMethod) SootField(soot.SootField)

Aggregations

FieldRef (soot.jimple.FieldRef)31 Value (soot.Value)25 Local (soot.Local)21 Unit (soot.Unit)20 Stmt (soot.jimple.Stmt)17 ArrayRef (soot.jimple.ArrayRef)14 InvokeExpr (soot.jimple.InvokeExpr)14 AssignStmt (soot.jimple.AssignStmt)13 SootField (soot.SootField)12 Type (soot.Type)12 DefinitionStmt (soot.jimple.DefinitionStmt)12 InstanceFieldRef (soot.jimple.InstanceFieldRef)12 SootMethod (soot.SootMethod)10 ValueBox (soot.ValueBox)10 ArrayList (java.util.ArrayList)9 NewArrayExpr (soot.jimple.NewArrayExpr)9 CastExpr (soot.jimple.CastExpr)8 StaticFieldRef (soot.jimple.StaticFieldRef)8 HashSet (java.util.HashSet)7 Iterator (java.util.Iterator)7