Search in sources :

Example 56 with SootField

use of soot.SootField in project soot by Sable.

the class CPApplication method inASTSwitchNode.

public void inASTSwitchNode(ASTSwitchNode node) {
    Object obj = cp.getBeforeSet(node);
    if (obj == null)
        return;
    if (!(obj instanceof CPFlowSet))
        return;
    // before set is a non null CPFlowSet
    CPFlowSet beforeSet = (CPFlowSet) obj;
    Value key = node.get_Key();
    if (key instanceof Local) {
        Local useLocal = (Local) key;
        // System.out.println("switch key is a local: "+useLocal);
        Object value = beforeSet.contains(className, useLocal.toString());
        if (value != null) {
            // System.out.println("switch key Local "+useLocal+"is present in before set with value"+value);
            // create constant value for the value and replace this local
            // use with the constant value use
            Value newValue = CPHelper.createConstant(value);
            if (newValue != null) {
                // System.out.println("Substituted the switch key local use with the constant value"+newValue);
                node.set_Key(newValue);
            } else {
            // System.out.println("FAILED TO Substitute the local use with the constant value");
            }
        }
    } else if (key instanceof FieldRef) {
        FieldRef useField = (FieldRef) key;
        // System.out.println("switch key is a FieldRef which is: "+useField);
        SootField usedSootField = useField.getField();
        Object value = beforeSet.contains(usedSootField.getDeclaringClass().getName(), usedSootField.getName().toString());
        if (value != null) {
            // System.out.println("FieldRef "+usedSootField+"is present in before set with value"+value);
            // create constant value for the value and replace this local
            // use with the constant value use
            Value newValue = CPHelper.createConstant(value);
            if (newValue != null) {
                // System.out.println("Substituted the constant field ref use with the constant value"+newValue);
                node.set_Key(newValue);
            } else {
            // System.out.println("FAILED TO Substitute the constant field ref use with the constant value");
            }
        }
    }
}
Also used : FieldRef(soot.jimple.FieldRef) Value(soot.Value) Local(soot.Local) SootField(soot.SootField) CPFlowSet(soot.dava.toolkits.base.AST.structuredAnalysis.CPFlowSet)

Example 57 with SootField

use of soot.SootField in project soot by Sable.

the class CPApplication method changedCondition.

/*
	 * Given a unary/binary or aggregated condition this method is used to find
	 * all the useBoxes or locals or fieldref in the case of unary conditions
	 * and then the set is checked for appropriate substitutions
	 */
public ASTCondition changedCondition(ASTCondition cond, CPFlowSet set) {
    if (cond instanceof ASTAggregatedCondition) {
        ASTCondition left = changedCondition(((ASTAggregatedCondition) cond).getLeftOp(), set);
        ASTCondition right = changedCondition(((ASTAggregatedCondition) cond).getRightOp(), set);
        ((ASTAggregatedCondition) cond).setLeftOp(left);
        ((ASTAggregatedCondition) cond).setRightOp(right);
        // System.out.println("New condition is: "+cond);
        return cond;
    } else if (cond instanceof ASTUnaryCondition) {
        Value val = ((ASTUnaryCondition) cond).getValue();
        if (val instanceof Local) {
            Object value = set.contains(className, ((Local) val).toString());
            if (value != null) {
                // System.out.println("if Condition Local "+((Local)val)+"is present in before set with value"+value);
                // create constant value for the value and replace this
                // local use with the constant value use
                Value newValue = CPHelper.createConstant(value);
                if (newValue != null) {
                    // System.out.println("Substituted the local use with the constant value"+newValue);
                    ((ASTUnaryCondition) cond).setValue(newValue);
                } else {
                // System.out.println("FAILED TO Substitute the local use with the constant value");
                }
            }
        } else if (val instanceof FieldRef) {
            FieldRef useField = (FieldRef) val;
            SootField usedSootField = useField.getField();
            Object value = set.contains(usedSootField.getDeclaringClass().getName(), usedSootField.getName().toString());
            if (value != null) {
                // System.out.println("if condition FieldRef "+usedSootField+"is present in before set with value"+value);
                // create constant value for the value and replace this
                // field use with the constant value use
                Value newValue = CPHelper.createConstant(value);
                if (newValue != null) {
                    // System.out.println("Substituted the constant field ref use with the constant value"+newValue);
                    ((ASTUnaryCondition) cond).setValue(newValue);
                } else {
                // System.out.println("FAILED TO Substitute the constant field ref use with the constant value");
                }
            }
        } else {
            substituteUses(val.getUseBoxes(), set);
        }
        // System.out.println("New condition is: "+cond);
        return cond;
    } else if (cond instanceof ASTBinaryCondition) {
        // get uses from binaryCondition
        Value val = ((ASTBinaryCondition) cond).getConditionExpr();
        substituteUses(val.getUseBoxes(), set);
        // System.out.println("New condition is: "+cond);
        return cond;
    } else {
        throw new RuntimeException("Method getUseList in ASTUsesAndDefs encountered unknown condition type");
    }
}
Also used : ASTBinaryCondition(soot.dava.internal.AST.ASTBinaryCondition) FieldRef(soot.jimple.FieldRef) Value(soot.Value) Local(soot.Local) SootField(soot.SootField) ASTAggregatedCondition(soot.dava.internal.AST.ASTAggregatedCondition) ASTUnaryCondition(soot.dava.internal.AST.ASTUnaryCondition) ASTCondition(soot.dava.internal.AST.ASTCondition)

Example 58 with SootField

use of soot.SootField in project soot by Sable.

the class CP method createConstantFieldsList.

/*
	 * Uses the results of the ConstantValueFinder to create a list of
	 * constantField CPTuple
	 */
private void createConstantFieldsList(HashMap<String, Object> constantFields, HashMap<String, SootField> classNameFieldNameToSootFieldMapping) {
    constantFieldTuples = new ArrayList<CPTuple>();
    Iterator<String> it = constantFields.keySet().iterator();
    // System.out.println("Adding constant fields to initial set: ");
    while (it.hasNext()) {
        String combined = it.next();
        int temp = combined.indexOf(ConstantFieldValueFinder.combiner, 0);
        if (temp > 0) {
            String className = combined.substring(0, temp);
            // String fieldName = combined.substring(temp+
            // ConstantFieldValueFinder.combiner.length());
            SootField field = classNameFieldNameToSootFieldMapping.get(combined);
            if (!(field.getType() instanceof PrimType)) {
                // we only care about PrimTypes
                continue;
            }
            // object type is double float long boolean or integer
            Object value = constantFields.get(combined);
            CPVariable var = new CPVariable(field);
            CPTuple newTuples = new CPTuple(className, var, value);
            constantFieldTuples.add(newTuples);
        // System.out.print("Class: "+className +
        // " Field: "+fieldName+" Value: "+value+"   ");
        } else {
            throw new DavaFlowAnalysisException("Second argument of VariableValuePair not a variable");
        }
    }
// System.out.println("");
}
Also used : DavaFlowAnalysisException(soot.dava.DavaFlowAnalysisException) PrimType(soot.PrimType) SootField(soot.SootField)

Example 59 with SootField

use of soot.SootField in project soot by Sable.

the class Renamer method isUniqueName.

/*
	 * Should return true if the name is unique
	 */
private boolean isUniqueName(String name) {
    Iterator it = getScopedLocals();
    // check that none of the locals uses this name
    while (it.hasNext()) {
        Local tempLocal = (Local) it.next();
        if (tempLocal.getName().equals(name)) {
            debug("isUniqueName", "New Name " + name + " is not unique (matches some local)..changing");
            return false;
        } else
            debug("isUniqueName", "New Name " + name + " is different from local " + tempLocal.getName());
    }
    it = getScopedFields();
    // check that none of the fields uses this name
    while (it.hasNext()) {
        SootField tempField = (SootField) it.next();
        if (tempField.getName().equals(name)) {
            debug("isUniqueName", "New Name " + name + " is not unique (matches field)..changing");
            return false;
        } else
            debug("isUniqueName", "New Name " + name + " is different from field " + tempField.getName());
    }
    return true;
}
Also used : Iterator(java.util.Iterator) Local(soot.Local) SootField(soot.SootField)

Example 60 with SootField

use of soot.SootField in project soot by Sable.

the class DexRefsChecker method internalTransform.

@Override
protected void internalTransform(final Body body, String phaseName, @SuppressWarnings("rawtypes") Map options) {
    for (Unit u : getRefCandidates(body)) {
        Stmt s = (Stmt) u;
        boolean hasField = false;
        FieldRef fr = null;
        SootField sf = null;
        if (s.containsFieldRef()) {
            fr = s.getFieldRef();
            sf = fr.getField();
            if (sf != null) {
                hasField = true;
            }
        } else {
            throw new RuntimeException("Unit '" + u + "' does not contain array ref nor field ref.");
        }
        if (!hasField) {
            System.out.println("Warning: add missing field '" + fr + "' to class!");
            SootClass sc = null;
            String frStr = fr.toString();
            if (frStr.contains(".<")) {
                sc = Scene.v().getSootClass(frStr.split(".<")[1].split(" ")[0].split(":")[0]);
            } else {
                sc = Scene.v().getSootClass(frStr.split(":")[0].replaceAll("^<", ""));
            }
            String fname = fr.toString().split(">")[0].split(" ")[2];
            int modifiers = soot.Modifier.PUBLIC;
            Type ftype = fr.getType();
            sc.addField(Scene.v().makeSootField(fname, ftype, modifiers));
        } else {
        // System.out.println("field "+ sf.getName() +" '"+ sf +"'
        // phantom: "+ isPhantom +" declared: "+ isDeclared);
        }
    }
// for if statements
}
Also used : Type(soot.Type) FieldRef(soot.jimple.FieldRef) SootField(soot.SootField) Unit(soot.Unit) SootClass(soot.SootClass) Stmt(soot.jimple.Stmt)

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