Search in sources :

Example 16 with SootField

use of soot.SootField in project soot by Sable.

the class ConstantFieldValueFinder method valuesForPrimTypeFields.

/*
	 * This method gives values to all the fields in all the classes if they can be determined statically
	 * We only care about fields which have primitive types
	 */
private void valuesForPrimTypeFields() {
    // go through all the classes
    Iterator classIt = appClasses.iterator();
    while (classIt.hasNext()) {
        SootClass s = (SootClass) classIt.next();
        debug("\nvaluesforPrimTypeFields", "Processing class " + s.getName());
        String declaringClass = s.getName();
        Iterator fieldIt = s.getFields().iterator();
        while (fieldIt.hasNext()) {
            SootField f = (SootField) fieldIt.next();
            String fieldName = f.getName();
            Type fieldType = f.getType();
            if (!(fieldType instanceof PrimType))
                continue;
            String combined = declaringClass + combiner + fieldName;
            classNameFieldNameToSootFieldMapping.put(combined, f);
            Object value = null;
            // check for constant value tags
            if (fieldType instanceof DoubleType && f.hasTag("DoubleConstantValueTag")) {
                double val = ((DoubleConstantValueTag) f.getTag("DoubleConstantValueTag")).getDoubleValue();
                value = new Double(val);
            } else if (fieldType instanceof FloatType && f.hasTag("FloatConstantValueTag")) {
                float val = ((FloatConstantValueTag) f.getTag("FloatConstantValueTag")).getFloatValue();
                value = new Float(val);
            } else if (fieldType instanceof LongType && f.hasTag("LongConstantValueTag")) {
                long val = ((LongConstantValueTag) f.getTag("LongConstantValueTag")).getLongValue();
                value = new Long(val);
            } else if (fieldType instanceof CharType && f.hasTag("IntegerConstantValueTag")) {
                int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
                value = new Integer(val);
            } else if (fieldType instanceof BooleanType && f.hasTag("IntegerConstantValueTag")) {
                int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
                if (val == 0)
                    value = new Boolean(false);
                else
                    value = new Boolean(true);
            } else if ((fieldType instanceof IntType || fieldType instanceof ByteType || fieldType instanceof ShortType) && f.hasTag("IntegerConstantValueTag")) {
                int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
                value = new Integer(val);
            }
            // if there was a constant value tag we have its value now
            if (value != null) {
                debug("TAGGED value found for field: " + combined);
                primTypeFieldValueToUse.put(combined, value);
                // continue with next field
                continue;
            }
            // see if the field was never assigned in which case it gets default values
            Object temp = fieldToValues.get(combined);
            if (temp == null) {
                if (fieldType instanceof DoubleType)
                    value = new Double(0);
                else if (fieldType instanceof FloatType)
                    value = new Float(0);
                else if (fieldType instanceof LongType)
                    value = new Long(0);
                else if (fieldType instanceof BooleanType)
                    value = new Boolean(false);
                else if ((fieldType instanceof IntType || fieldType instanceof ByteType || fieldType instanceof ShortType) || fieldType instanceof CharType) {
                    value = new Integer(0);
                } else
                    throw new DecompilationException("Unknown primitive type...please report to developer");
                primTypeFieldValueToUse.put(combined, value);
                debug("DEFAULT value for field: " + combined);
                // continue with next field
                continue;
            }
            // havent got a tag with value and havent use default since SOME method did define the field atleast once
            // there was some value assigned!!!!!!!!!
            debug("CHECKING USER ASSIGNED VALUES FOR: " + combined);
            ArrayList values = (ArrayList) temp;
            // check if they are all constants and that too the same constant
            Iterator it = values.iterator();
            NumericConstant tempConstant = null;
            while (it.hasNext()) {
                Value val = (Value) it.next();
                if (!(val instanceof NumericConstant)) {
                    tempConstant = null;
                    debug("Not numeric constant hence giving up");
                    break;
                }
                if (tempConstant == null) {
                    tempConstant = (NumericConstant) val;
                } else {
                    // check that this value is the same as previous
                    if (!tempConstant.equals(val)) {
                        tempConstant = null;
                        break;
                    }
                }
            }
            if (tempConstant == null) {
                // continue with next field cant do anything about this one
                continue;
            }
            if (tempConstant instanceof LongConstant) {
                Long tempVal = new Long(((LongConstant) tempConstant).value);
                if (tempVal.compareTo(new Long(0)) == 0)
                    primTypeFieldValueToUse.put(combined, tempVal);
                else
                    debug("Not assigning the agreed value since that is not the default value for " + combined);
            } else if (tempConstant instanceof DoubleConstant) {
                Double tempVal = new Double(((DoubleConstant) tempConstant).value);
                if (tempVal.compareTo(new Double(0)) == 0)
                    primTypeFieldValueToUse.put(combined, tempVal);
                else
                    debug("Not assigning the agreed value since that is not the default value for " + combined);
            } else if (tempConstant instanceof FloatConstant) {
                Float tempVal = new Float(((FloatConstant) tempConstant).value);
                if (tempVal.compareTo(new Float(0)) == 0)
                    primTypeFieldValueToUse.put(combined, tempVal);
                else
                    debug("Not assigning the agreed value since that is not the default value for " + combined);
            } else if (tempConstant instanceof IntConstant) {
                Integer tempVal = new Integer(((IntConstant) tempConstant).value);
                if (tempVal.compareTo(new Integer(0)) == 0) {
                    SootField tempField = classNameFieldNameToSootFieldMapping.get(combined);
                    if (tempField.getType() instanceof BooleanType) {
                        primTypeFieldValueToUse.put(combined, new Boolean(false));
                    // System.out.println("puttingvalue false for"+combined);
                    } else {
                        primTypeFieldValueToUse.put(combined, tempVal);
                    // System.out.println("puttingvalue 0 for"+combined);
                    }
                } else
                    debug("Not assigning the agreed value since that is not the default value for " + combined);
            } else {
                throw new DecompilationException("Un handled Numberic Constant....report to programmer");
            }
        }
    // all fields of the class
    }
// all classes
}
Also used : DoubleConstant(soot.jimple.DoubleConstant) LongType(soot.LongType) FloatConstant(soot.jimple.FloatConstant) ArrayList(java.util.ArrayList) DecompilationException(soot.dava.DecompilationException) ByteType(soot.ByteType) FloatType(soot.FloatType) IntType(soot.IntType) Iterator(java.util.Iterator) PrimType(soot.PrimType) LongConstantValueTag(soot.tagkit.LongConstantValueTag) IntConstant(soot.jimple.IntConstant) LongConstant(soot.jimple.LongConstant) ShortType(soot.ShortType) BooleanType(soot.BooleanType) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) SootClass(soot.SootClass) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) ShortType(soot.ShortType) CharType(soot.CharType) LongType(soot.LongType) BooleanType(soot.BooleanType) ByteType(soot.ByteType) Type(soot.Type) PrimType(soot.PrimType) DoubleType(soot.DoubleType) NumericConstant(soot.jimple.NumericConstant) Value(soot.Value) SootField(soot.SootField) CharType(soot.CharType)

Example 17 with SootField

use of soot.SootField in project soot by Sable.

the class ConstantFieldValueFinder method computeFieldToValuesAssignedList.

/*
	 * Go through all the methods in the application and make a mapping of className+methodName ---> values assigned
	 * There can obviously be more than one value assigned to each field
	 */
private void computeFieldToValuesAssignedList() {
    // go through all the classes
    Iterator classIt = appClasses.iterator();
    while (classIt.hasNext()) {
        SootClass s = (SootClass) classIt.next();
        debug("\ncomputeMethodSummaries", "Processing class " + s.getName());
        // go though all the methods
        Iterator methodIt = s.methodIterator();
        while (methodIt.hasNext()) {
            SootMethod m = (SootMethod) methodIt.next();
            DavaBody body = null;
            if (m.hasActiveBody()) {
                /*
					 * Added to try to fix the no active body found exception
					 */
                body = (DavaBody) m.getActiveBody();
            } else {
                continue;
            }
            ASTNode AST = (ASTNode) body.getUnits().getFirst();
            // find all definitions in the program
            AllDefinitionsFinder defFinder = new AllDefinitionsFinder();
            AST.apply(defFinder);
            Iterator<DefinitionStmt> allDefIt = defFinder.getAllDefs().iterator();
            // go through each definition
            while (allDefIt.hasNext()) {
                DefinitionStmt stmt = allDefIt.next();
                // debug("DefinitionStmt")
                Value left = stmt.getLeftOp();
                /*
					 * Only care if we have fieldRef on the left
					 */
                if (!(left instanceof FieldRef)) {
                    continue;
                }
                // we know definition is to a field
                debug("computeMethodSummaries method: " + m.getName(), "Field ref is: " + left);
                // Information we want to store is class of field and name of field and the right op
                FieldRef ref = (FieldRef) left;
                SootField field = ref.getField();
                /*
					 * Only care about fields with primtype
					 */
                if (!(field.getType() instanceof PrimType))
                    continue;
                String fieldName = field.getName();
                String declaringClass = field.getDeclaringClass().getName();
                debug("\tField Name: " + fieldName);
                debug("\tField DeclaringClass: " + declaringClass);
                // get the valueList for this class+field combo
                String combined = declaringClass + combiner + fieldName;
                Object temp = fieldToValues.get(combined);
                ArrayList valueList;
                if (temp == null) {
                    // no value of this field was yet assigned
                    valueList = new ArrayList();
                    fieldToValues.put(combined, valueList);
                } else {
                    valueList = (ArrayList) temp;
                }
                valueList.add(stmt.getRightOp());
            }
        // going through all the definitions
        }
    // going through methods of class s
    }
// going through  classes
}
Also used : AllDefinitionsFinder(soot.dava.toolkits.base.AST.traversals.AllDefinitionsFinder) FieldRef(soot.jimple.FieldRef) ArrayList(java.util.ArrayList) SootClass(soot.SootClass) DavaBody(soot.dava.DavaBody) Iterator(java.util.Iterator) ASTNode(soot.dava.internal.AST.ASTNode) Value(soot.Value) SootMethod(soot.SootMethod) PrimType(soot.PrimType) SootField(soot.SootField) DefinitionStmt(soot.jimple.DefinitionStmt)

Example 18 with SootField

use of soot.SootField in project soot by Sable.

the class CPApplication method substituteUses.

public void substituteUses(List useBoxes, CPFlowSet beforeSet) {
    Iterator useIt = useBoxes.iterator();
    while (useIt.hasNext()) {
        Object useObj = useIt.next();
        Value use = ((ValueBox) useObj).getValue();
        if (use instanceof Local) {
            Local useLocal = (Local) use;
            // System.out.println("local is: "+useLocal);
            Object value = beforeSet.contains(className, useLocal.toString());
            if (value != null) {
                // System.out.println("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 local use with the constant value"+newValue);
                    ((ValueBox) useObj).setValue(newValue);
                } else {
                // System.out.println("FAILED TO Substitute the local use with the constant value");
                }
            }
        } else if (use instanceof FieldRef) {
            FieldRef useField = (FieldRef) use;
            // System.out.println("FieldRef 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);
                    ((ValueBox) useObj).setValue(newValue);
                } else {
                // System.out.println("FAILED TO Substitute the constant field ref use with the constant value");
                }
            }
        }
    }
}
Also used : FieldRef(soot.jimple.FieldRef) ValueBox(soot.ValueBox) Iterator(java.util.Iterator) Value(soot.Value) Local(soot.Local) SootField(soot.SootField)

Example 19 with SootField

use of soot.SootField in project soot by Sable.

the class LockAllocationBodyTransformer method getLockFor.

public static Value getLockFor(EquivalentValue lockEqVal) {
    Value lock = lockEqVal.getValue();
    if (lock instanceof InstanceFieldRef)
        return lock;
    if (// it would be better to lock the array
    lock instanceof ArrayRef)
        // ref for each value of the index!
        return ((ArrayRef) lock).getBase();
    if (lock instanceof Local)
        return lock;
    if (lock instanceof StaticFieldRef || lock instanceof NewStaticLock) {
        if (lockEqValToLock.containsKey(lockEqVal))
            return lockEqValToLock.get(lockEqVal);
        SootClass lockClass = null;
        if (lock instanceof StaticFieldRef) {
            StaticFieldRef sfrLock = (StaticFieldRef) lock;
            lockClass = sfrLock.getField().getDeclaringClass();
        } else if (lock instanceof NewStaticLock) {
            DeadlockAvoidanceEdge dae = (DeadlockAvoidanceEdge) lock;
            lockClass = dae.getLockClass();
        }
        SootMethod clinitMethod = null;
        JimpleBody clinitBody = null;
        Stmt firstStmt = null;
        boolean addingNewClinit = !lockClass.declaresMethod("void <clinit>()");
        if (addingNewClinit) {
            clinitMethod = Scene.v().makeSootMethod("<clinit>", new ArrayList(), VoidType.v(), Modifier.PUBLIC | Modifier.STATIC);
            clinitBody = Jimple.v().newBody(clinitMethod);
            clinitMethod.setActiveBody(clinitBody);
            lockClass.addMethod(clinitMethod);
        } else {
            clinitMethod = lockClass.getMethod("void <clinit>()");
            clinitBody = (JimpleBody) clinitMethod.getActiveBody();
            firstStmt = clinitBody.getFirstNonIdentityStmt();
        }
        PatchingChain<Unit> clinitUnits = clinitBody.getUnits();
        Local lockLocal = Jimple.v().newLocal("objectLockLocal" + lockNumber, RefType.v("java.lang.Object"));
        // lockNumber is increased below
        // TODO: add name conflict
        clinitBody.getLocals().add(lockLocal);
        // avoidance code
        // assign new object to lock obj
        Stmt newStmt = Jimple.v().newAssignStmt(lockLocal, Jimple.v().newNewExpr(RefType.v("java.lang.Object")));
        if (addingNewClinit)
            clinitUnits.add(newStmt);
        else
            clinitUnits.insertBeforeNoRedirect(newStmt, firstStmt);
        // initialize new object
        SootClass objectClass = Scene.v().loadClassAndSupport("java.lang.Object");
        RefType type = RefType.v(objectClass);
        SootMethod initMethod = objectClass.getMethod("void <init>()");
        Stmt initStmt = Jimple.v().newInvokeStmt(Jimple.v().newSpecialInvokeExpr(lockLocal, initMethod.makeRef(), Collections.EMPTY_LIST));
        if (addingNewClinit)
            clinitUnits.add(initStmt);
        else
            clinitUnits.insertBeforeNoRedirect(initStmt, firstStmt);
        // copy new object to global static lock object (for use by other
        // fns)
        SootField actualLockObject = Scene.v().makeSootField("objectLockGlobal" + lockNumber, RefType.v("java.lang.Object"), Modifier.STATIC | Modifier.PUBLIC);
        lockNumber++;
        lockClass.addField(actualLockObject);
        StaticFieldRef actualLockSfr = Jimple.v().newStaticFieldRef(actualLockObject.makeRef());
        Stmt assignStmt = Jimple.v().newAssignStmt(actualLockSfr, lockLocal);
        if (addingNewClinit)
            clinitUnits.add(assignStmt);
        else
            clinitUnits.insertBeforeNoRedirect(assignStmt, firstStmt);
        if (addingNewClinit)
            clinitUnits.add(Jimple.v().newReturnVoidStmt());
        lockEqValToLock.put(lockEqVal, actualLockSfr);
        return actualLockSfr;
    }
    throw new RuntimeException("Unknown type of lock (" + lock + "): expected FieldRef, ArrayRef, or Local");
}
Also used : ArrayList(java.util.ArrayList) FakeJimpleLocal(soot.jimple.toolkits.infoflow.FakeJimpleLocal) Local(soot.Local) SootClass(soot.SootClass) Unit(soot.Unit) StaticFieldRef(soot.jimple.StaticFieldRef) Stmt(soot.jimple.Stmt) ArrayRef(soot.jimple.ArrayRef) RefType(soot.RefType) EquivalentValue(soot.EquivalentValue) Value(soot.Value) InstanceFieldRef(soot.jimple.InstanceFieldRef) SootMethod(soot.SootMethod) SootField(soot.SootField) JimpleBody(soot.jimple.JimpleBody)

Example 20 with SootField

use of soot.SootField in project soot by Sable.

the class JimpleBodyBuilder method handleFinalLocalInits.

private void handleFinalLocalInits() {
    ArrayList<SootField> finalsList = ((PolyglotMethodSource) body.getMethod().getSource()).getFinalsList();
    if (finalsList == null)
        return;
    int paramCount = paramRefCount - finalsList.size();
    Iterator<SootField> it = finalsList.iterator();
    while (it.hasNext()) {
        soot.SootField sf = it.next();
        soot.jimple.FieldRef fieldRef = soot.jimple.Jimple.v().newInstanceFieldRef(specialThisLocal, sf.makeRef());
        soot.jimple.AssignStmt stmt = soot.jimple.Jimple.v().newAssignStmt(fieldRef, body.getParameterLocal(paramCount));
        body.getUnits().add(stmt);
        paramCount++;
    }
}
Also used : SootField(soot.SootField) 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