Search in sources :

Example 1 with ASTBinaryCondition

use of soot.dava.internal.AST.ASTBinaryCondition in project soot by Sable.

the class SimplifyConditions method applyDeMorgans.

/*
     * !z0 && !z1  ---->   !(z0 || z1)
     * !z0 || !z1  ---->   !(z0 && z1)
     * 
     * Send null if no change else send new condition CONDITION
     */
public ASTCondition applyDeMorgans(ASTAggregatedCondition aggCond) {
    ASTCondition left = aggCond.getLeftOp();
    ASTCondition right = aggCond.getRightOp();
    if (aggCond.isNotted() && left instanceof ASTBinaryCondition && right instanceof ASTBinaryCondition) {
        // we can remove the not sign by simply flipping the two conditions
        // ! (  x==y &&  a<b )
        left.flip();
        right.flip();
        if (aggCond instanceof ASTAndCondition)
            aggCond = new ASTOrCondition(left, right);
        else
            aggCond = new ASTAndCondition(left, right);
        return aggCond;
    }
    if ((left.isNotted() && right.isNotted() && (!(left instanceof ASTBinaryCondition) && !(right instanceof ASTBinaryCondition))) || (left.isNotted() && aggCond.isNotted() && !(left instanceof ASTBinaryCondition)) || (right.isNotted() && aggCond.isNotted() && !(right instanceof ASTBinaryCondition))) {
        // both are notted and atleast one is not a binaryCondition
        left.flip();
        right.flip();
        ASTAggregatedCondition newCond;
        if (aggCond instanceof ASTAndCondition)
            newCond = new ASTOrCondition(left, right);
        else
            newCond = new ASTAndCondition(left, right);
        if (aggCond.isNotted())
            return newCond;
        else {
            newCond.flip();
            return newCond;
        }
    }
    return null;
}
Also used : ASTBinaryCondition(soot.dava.internal.AST.ASTBinaryCondition) ASTAggregatedCondition(soot.dava.internal.AST.ASTAggregatedCondition) ASTCondition(soot.dava.internal.AST.ASTCondition) ASTAndCondition(soot.dava.internal.AST.ASTAndCondition) ASTOrCondition(soot.dava.internal.AST.ASTOrCondition)

Example 2 with ASTBinaryCondition

use of soot.dava.internal.AST.ASTBinaryCondition 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 3 with ASTBinaryCondition

use of soot.dava.internal.AST.ASTBinaryCondition in project soot by Sable.

the class CP method checkForValueHints.

/*
	 * The isElseBranch flag is true if the caller is the else branch of the
	 * ifelse statement. In that case we might be able to send something for the
	 * else branch
	 */
public CPTuple checkForValueHints(ASTCondition cond, CPFlowSet input, boolean isElseBranch) {
    if (cond instanceof ASTUnaryCondition) {
        // check for lone boolean if(notDone)
        ASTUnaryCondition unary = (ASTUnaryCondition) cond;
        Value unaryValue = unary.getValue();
        boolean NOTTED = false;
        // Get the real value if this is a notted expression
        if (unaryValue instanceof DNotExpr) {
            unaryValue = ((DNotExpr) unaryValue).getOp();
            NOTTED = true;
        }
        if (!(unaryValue instanceof Local)) {
            // inset
            return null;
        }
        // the unary value is a local add the value to the inset which woul
        // dbe present in the if branch
        CPVariable variable = new CPVariable((Local) unaryValue);
        // false and vice verse
        if (!isElseBranch) {
            // we are in the if branch hence notted true would mean the
            // variable is actually false here
            Boolean boolVal = new Boolean(!NOTTED);
            return new CPTuple(localClassName, variable, boolVal);
        } else {
            // in the else branch NOTTED true means the variable is true
            Boolean boolVal = new Boolean(NOTTED);
            return new CPTuple(localClassName, variable, boolVal);
        }
    } else if (cond instanceof ASTBinaryCondition) {
        ASTBinaryCondition binary = (ASTBinaryCondition) cond;
        ConditionExpr expr = binary.getConditionExpr();
        Boolean equal = null;
        String symbol = expr.getSymbol();
        if (symbol.indexOf("==") > -1) {
            // System.out.println("!!!!!!!!!1 FOUND == in binary comparison operaiton");
            equal = new Boolean(true);
        } else if (symbol.indexOf("!=") > -1) {
            equal = new Boolean(false);
        // System.out.println("!!!!!!!!!!!!!! FOUND != in binary comparison operaiton");
        } else {
            // System.out.println("symbol is"+symbol);
            return null;
        }
        // we have a comparison the truth value of equal tells whether we
        // are doing == or !=
        Value a = expr.getOp1();
        Value b = expr.getOp2();
        // see if its possible to deduce a hint from these values
        CPTuple tuple = createCPTupleIfPossible(a, b, input);
        // if the tuple is not created
        if (tuple == null)
            return null;
        // we have to make sure is that the == and != are taken into account
        if (equal.booleanValue()) {
            // branch a is in fact equal to b
            if (!isElseBranch)
                return tuple;
            else
                return null;
        } else {
            if (isElseBranch)
                return tuple;
            else
                return null;
        }
    }
    return null;
}
Also used : ASTBinaryCondition(soot.dava.internal.AST.ASTBinaryCondition) DNotExpr(soot.dava.internal.javaRep.DNotExpr) ConditionExpr(soot.jimple.ConditionExpr) Value(soot.Value) Local(soot.Local) ASTUnaryCondition(soot.dava.internal.AST.ASTUnaryCondition)

Example 4 with ASTBinaryCondition

use of soot.dava.internal.AST.ASTBinaryCondition in project soot by Sable.

the class SimplifyConditions method simplifyTheCondition.

/*
	 * In a loop keep simplifying the condition as much as possible
	 * 
	 */
public ASTCondition simplifyTheCondition(ASTCondition cond) {
    if (cond instanceof ASTAggregatedCondition) {
        ASTAggregatedCondition aggCond = (ASTAggregatedCondition) cond;
        ASTCondition leftCond = simplifyTheCondition(aggCond.getLeftOp());
        ASTCondition rightCond = simplifyTheCondition(aggCond.getRightOp());
        // if returned values are non null then set leftop /rightop to new condition
        if (leftCond != null) {
            aggCond.setLeftOp(leftCond);
        }
        if (rightCond != null) {
            aggCond.setRightOp(rightCond);
        }
        ASTCondition returned = simplifyIfAtleastOneConstant(aggCond);
        if (returned != null) {
            changed = true;
            return returned;
        }
        returned = applyDeMorgans(aggCond);
        if (returned != null) {
            changed = true;
            return returned;
        }
        return aggCond;
    } else if (cond instanceof ASTUnaryCondition) {
        // dont do anything with unary conditions
        ASTUnaryCondition unary = (ASTUnaryCondition) cond;
        /*
			 * if unary is a noted constant simplify it
			 * !true to be converted to false
			 * !false to be converted to true
			 */
        Value unaryVal = unary.getValue();
        if (unaryVal instanceof DNotExpr) {
            if (DEBUG)
                System.out.println("Found NotExpr in unary COndition" + unaryVal);
            DNotExpr notted = (DNotExpr) unaryVal;
            Value internal = notted.getOp();
            Boolean isIt = isBooleanConstant(internal);
            if (isIt != null) {
                // convert !true to false
                if (isIt.booleanValue()) {
                    // true
                    if (DEBUG)
                        System.out.println("CONVERTED !true to false");
                    changed = true;
                    return new ASTUnaryCondition(DIntConstant.v(0, BooleanType.v()));
                } else if (!isIt.booleanValue()) {
                    // false
                    if (DEBUG)
                        System.out.println("CONVERTED !false to true");
                    changed = true;
                    return new ASTUnaryCondition(DIntConstant.v(1, BooleanType.v()));
                } else
                    throw new RuntimeException("BooleanType found with value different than 0 or 1");
            } else {
                if (DEBUG)
                    System.out.println("Not boolean type");
            }
        }
        return unary;
    } else if (cond instanceof ASTBinaryCondition) {
        ASTBinaryCondition binary = (ASTBinaryCondition) cond;
        ConditionExpr expr = binary.getConditionExpr();
        // returns null if no change
        ASTUnaryCondition temp = evaluateBinaryCondition(expr);
        if (DEBUG)
            System.out.println("changed binary condition " + cond + " to" + temp);
        if (temp != null)
            changed = true;
        return temp;
    } else {
        throw new RuntimeException("Method getUseList in ASTUsesAndDefs encountered unknown condition type");
    }
}
Also used : ASTBinaryCondition(soot.dava.internal.AST.ASTBinaryCondition) DNotExpr(soot.dava.internal.javaRep.DNotExpr) ConditionExpr(soot.jimple.ConditionExpr) Value(soot.Value) ASTAggregatedCondition(soot.dava.internal.AST.ASTAggregatedCondition) ASTUnaryCondition(soot.dava.internal.AST.ASTUnaryCondition) ASTCondition(soot.dava.internal.AST.ASTCondition)

Aggregations

ASTBinaryCondition (soot.dava.internal.AST.ASTBinaryCondition)4 Value (soot.Value)3 ASTAggregatedCondition (soot.dava.internal.AST.ASTAggregatedCondition)3 ASTCondition (soot.dava.internal.AST.ASTCondition)3 ASTUnaryCondition (soot.dava.internal.AST.ASTUnaryCondition)3 Local (soot.Local)2 DNotExpr (soot.dava.internal.javaRep.DNotExpr)2 ConditionExpr (soot.jimple.ConditionExpr)2 SootField (soot.SootField)1 ASTAndCondition (soot.dava.internal.AST.ASTAndCondition)1 ASTOrCondition (soot.dava.internal.AST.ASTOrCondition)1 FieldRef (soot.jimple.FieldRef)1