Search in sources :

Example 6 with ASTCondition

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

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

the class CPApplication method inASTDoWhileNode.

public void inASTDoWhileNode(ASTDoWhileNode node) {
    Object obj = cp.getAfterSet(node);
    if (obj == null)
        return;
    if (!(obj instanceof CPFlowSet))
        return;
    // after set is a non null CPFlowSet
    CPFlowSet afterSet = (CPFlowSet) obj;
    ASTCondition cond = node.get_Condition();
    // System.out.println("Do While Statement with condition: "+cond);
    // System.out.println("After set is: "+afterSet.toString());
    changedCondition(cond, afterSet);
}
Also used : ASTCondition(soot.dava.internal.AST.ASTCondition) CPFlowSet(soot.dava.toolkits.base.AST.structuredAnalysis.CPFlowSet)

Example 8 with ASTCondition

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

the class CPApplication method inASTWhileNode.

public void inASTWhileNode(ASTWhileNode node) {
    Object obj = cp.getAfterSet(node);
    if (obj == null)
        return;
    if (!(obj instanceof CPFlowSet))
        return;
    // after set is a non null CPFlowSet
    CPFlowSet afterSet = (CPFlowSet) obj;
    ASTCondition cond = node.get_Condition();
    // System.out.println("While Statement with condition: "+cond);
    // System.out.println("After set is: "+afterSet.toString());
    changedCondition(cond, afterSet);
}
Also used : ASTCondition(soot.dava.internal.AST.ASTCondition) CPFlowSet(soot.dava.toolkits.base.AST.structuredAnalysis.CPFlowSet)

Example 9 with ASTCondition

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

the class CPApplication method inASTIfElseNode.

public void inASTIfElseNode(ASTIfElseNode 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;
    ASTCondition cond = node.get_Condition();
    // System.out.println("IfElse Statement with condition: "+cond);
    // System.out.println("Before set is: "+beforeSet.toString());
    changedCondition(cond, beforeSet);
}
Also used : ASTCondition(soot.dava.internal.AST.ASTCondition) CPFlowSet(soot.dava.toolkits.base.AST.structuredAnalysis.CPFlowSet)

Example 10 with ASTCondition

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

the class EliminateConditions method eliminateForTry.

public Boolean eliminateForTry(ASTNode node) {
    ASTCondition cond = null;
    if (node instanceof ASTControlFlowNode)
        cond = ((ASTControlFlowNode) node).get_Condition();
    else
        return null;
    if (cond == null || !(cond instanceof ASTUnaryCondition))
        return null;
    ASTUnaryCondition unary = (ASTUnaryCondition) cond;
    Value unaryValue = unary.getValue();
    boolean notted = false;
    if (unaryValue instanceof DNotExpr) {
        notted = true;
        unaryValue = ((DNotExpr) unaryValue).getOp();
    }
    Boolean isBoolean = isBooleanConstant(unaryValue);
    if (isBoolean == null) {
        // not a constant
        return null;
    }
    boolean trueOrFalse = isBoolean.booleanValue();
    if (notted) {
        // since it is notted we reverse the booleans
        trueOrFalse = !trueOrFalse;
    }
    AST.apply(finder);
    Object temp = finder.getParentOf(node);
    if (temp == null)
        return null;
    if (!(temp instanceof ASTTryNode))
        throw new RuntimeException("eliminateTry called when parent was not a try node");
    ASTTryNode parent = (ASTTryNode) temp;
    List<Object> tryBody = parent.get_TryBody();
    int index = tryBody.indexOf(node);
    if (index >= 0) {
        // bound the body containing Node
        bodyContainingNode = tryBody;
        return new Boolean(trueOrFalse);
    }
    List<Object> catchList = parent.get_CatchList();
    Iterator<Object> it = catchList.iterator();
    while (it.hasNext()) {
        ASTTryNode.container catchBody = (ASTTryNode.container) it.next();
        List<Object> body = (List<Object>) catchBody.o;
        index = body.indexOf(node);
        if (index >= 0) {
            // bound the body containing Node
            bodyContainingNode = body;
            return new Boolean(trueOrFalse);
        }
    }
    return null;
}
Also used : ASTTryNode(soot.dava.internal.AST.ASTTryNode) DNotExpr(soot.dava.internal.javaRep.DNotExpr) ASTCondition(soot.dava.internal.AST.ASTCondition) ASTControlFlowNode(soot.dava.internal.AST.ASTControlFlowNode) Value(soot.Value) List(java.util.List) ASTUnaryCondition(soot.dava.internal.AST.ASTUnaryCondition)

Aggregations

ASTCondition (soot.dava.internal.AST.ASTCondition)13 ASTUnaryCondition (soot.dava.internal.AST.ASTUnaryCondition)5 CPFlowSet (soot.dava.toolkits.base.AST.structuredAnalysis.CPFlowSet)5 Value (soot.Value)4 List (java.util.List)3 ASTAggregatedCondition (soot.dava.internal.AST.ASTAggregatedCondition)3 ASTBinaryCondition (soot.dava.internal.AST.ASTBinaryCondition)3 DNotExpr (soot.dava.internal.javaRep.DNotExpr)3 ASTAndCondition (soot.dava.internal.AST.ASTAndCondition)2 ASTControlFlowNode (soot.dava.internal.AST.ASTControlFlowNode)2 ASTOrCondition (soot.dava.internal.AST.ASTOrCondition)2 Local (soot.Local)1 SootField (soot.SootField)1 DecompilationException (soot.dava.DecompilationException)1 ASTIfNode (soot.dava.internal.AST.ASTIfNode)1 ASTNode (soot.dava.internal.AST.ASTNode)1 ASTTryNode (soot.dava.internal.AST.ASTTryNode)1 AugmentedStmt (soot.dava.internal.asg.AugmentedStmt)1 ASTParentNodeFinder (soot.dava.toolkits.base.AST.traversals.ASTParentNodeFinder)1 ConditionExpr (soot.jimple.ConditionExpr)1