Search in sources :

Example 1 with ASTIfNode

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

the class IfElseSplitter method outASTIfElseNode.

public void outASTIfElseNode(ASTIfElseNode node) {
    // if some pattern has already matched cant do another one in this go
    if (transform)
        return;
    List<Object> subBodies = node.get_SubBodies();
    if (subBodies.size() != 2)
        throw new DecompilationException("IfelseNode without two subBodies. report to developer");
    List<Object> ifBody = (List<Object>) subBodies.get(0);
    List<Object> elseBody = (List<Object>) subBodies.get(1);
    boolean patternMatched = tryBodyPattern(ifBody, node.get_Label(), elseBody);
    List<Object> newIfBody = null;
    List<Object> outerScopeBody = null;
    boolean negateIfCondition = false;
    if (patternMatched) {
        if (DEBUG)
            System.out.println("First pattern matched");
        newIfBody = ifBody;
        outerScopeBody = elseBody;
        negateIfCondition = false;
    } else {
        patternMatched = tryBodyPattern(elseBody, node.get_Label(), ifBody);
        if (patternMatched) {
            if (DEBUG)
                System.out.println("Second pattern matched");
            newIfBody = elseBody;
            outerScopeBody = ifBody;
            negateIfCondition = true;
        }
    }
    // if at this point newIfBody and outerScopeBody are non null we got ourselves a transformation :)
    if (newIfBody != null && outerScopeBody != null) {
        ASTCondition cond = node.get_Condition();
        if (negateIfCondition)
            cond.flip();
        ASTIfNode newNode = new ASTIfNode(node.get_Label(), cond, newIfBody);
        if (DEBUG) {
            System.out.println("New IF Node is: " + newNode.toString());
            System.out.println("Outer scope body list is:\n");
            for (int i = 0; i < outerScopeBody.size(); i++) System.out.println("\n\n " + outerScopeBody.get(i).toString());
        }
        ASTParentNodeFinder finder = new ASTParentNodeFinder();
        methodNode.apply(finder);
        Object returned = finder.getParentOf(node);
        if (returned == null) {
            // coundnt find parent so cant do anything
            return;
        }
        /*
			 * Setting globals since everything is ready for transformation
			 * BECAUSE we cant modify the parent here we are going to do some 
			 * bad coding style
			 * store the information needed for this into globals
			 * set a flag
			 * and the outASTMethod checks for this
			 */
        parent = (ASTNode) returned;
        toReplace = node;
        toInsert = newNode;
        bodyAfterInsert = outerScopeBody;
        transform = true;
    }
}
Also used : ASTIfNode(soot.dava.internal.AST.ASTIfNode) ASTParentNodeFinder(soot.dava.toolkits.base.AST.traversals.ASTParentNodeFinder) DecompilationException(soot.dava.DecompilationException) List(java.util.List) ASTCondition(soot.dava.internal.AST.ASTCondition)

Example 2 with ASTIfNode

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

the class EliminateConditions method change.

public boolean change(Boolean returned, ASTNode temp) {
    if (bodyContainingNode != null && returned != null && temp != null) {
        int index = bodyContainingNode.indexOf(temp);
        if (DEBUG)
            System.out.println("in change");
        if (temp instanceof ASTIfNode) {
            bodyContainingNode.remove(temp);
            if (returned.booleanValue()) {
                // if statement and value was true put the body of if into
                // the code
                // if its a labeled stmt we need a labeled block instead
                // notice that its okkay to put a labeled block since other
                // transformations might remove it
                String label = ((ASTLabeledNode) temp).get_Label().toString();
                if (label != null) {
                    ASTLabeledBlockNode labeledNode = new ASTLabeledBlockNode(((ASTLabeledNode) temp).get_Label(), (List<Object>) temp.get_SubBodies().get(0));
                    bodyContainingNode.add(index, labeledNode);
                } else {
                    bodyContainingNode.addAll(index, (List) temp.get_SubBodies().get(0));
                }
            }
            if (DEBUG)
                System.out.println("Removed if" + temp);
            return true;
        } else if (temp instanceof ASTIfElseNode) {
            bodyContainingNode.remove(temp);
            if (returned.booleanValue()) {
                // true so the if branch's body has to be added
                // if its a labeled stmt we need a labeled block instead
                // notice that its okkay to put a labeled block since other
                // transformations might remove it
                String label = ((ASTLabeledNode) temp).get_Label().toString();
                if (label != null) {
                    ASTLabeledBlockNode labeledNode = new ASTLabeledBlockNode(((ASTLabeledNode) temp).get_Label(), (List<Object>) temp.get_SubBodies().get(0));
                    bodyContainingNode.add(index, labeledNode);
                } else {
                    bodyContainingNode.addAll(index, (List) temp.get_SubBodies().get(0));
                }
            } else {
                // if its a labeled stmt we need a labeled block instead
                // notice that its okkay to put a labeled block since other
                // transformations might remove it
                String label = ((ASTLabeledNode) temp).get_Label().toString();
                if (label != null) {
                    ASTLabeledBlockNode labeledNode = new ASTLabeledBlockNode(((ASTLabeledNode) temp).get_Label(), (List<Object>) temp.get_SubBodies().get(1));
                    bodyContainingNode.add(index, labeledNode);
                } else {
                    bodyContainingNode.addAll(index, (List) temp.get_SubBodies().get(1));
                }
            }
            return true;
        } else if (temp instanceof ASTWhileNode && returned.booleanValue() == false) {
            // notice we only remove if ASTWhileNode has false condition
            bodyContainingNode.remove(temp);
            return true;
        } else if (temp instanceof ASTDoWhileNode && returned.booleanValue() == false) {
            // System.out.println("in try dowhile false");
            // remove the loop copy the body out since it gets executed once
            bodyContainingNode.remove(temp);
            bodyContainingNode.addAll(index, (List) temp.get_SubBodies().get(0));
            return true;
        } else if (temp instanceof ASTForLoopNode && returned.booleanValue() == false) {
            bodyContainingNode.remove(temp);
            ASTStatementSequenceNode newNode = new ASTStatementSequenceNode(((ASTForLoopNode) temp).getInit());
            bodyContainingNode.add(index, newNode);
            return true;
        }
    }
    return false;
}
Also used : ASTIfNode(soot.dava.internal.AST.ASTIfNode) ASTDoWhileNode(soot.dava.internal.AST.ASTDoWhileNode) ASTWhileNode(soot.dava.internal.AST.ASTWhileNode) List(java.util.List) ASTIfElseNode(soot.dava.internal.AST.ASTIfElseNode) ASTLabeledNode(soot.dava.internal.AST.ASTLabeledNode) ASTForLoopNode(soot.dava.internal.AST.ASTForLoopNode) ASTStatementSequenceNode(soot.dava.internal.AST.ASTStatementSequenceNode) ASTLabeledBlockNode(soot.dava.internal.AST.ASTLabeledBlockNode)

Example 3 with ASTIfNode

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

the class MethodCallFinder method replaceSubBody.

public boolean replaceSubBody(InvokeStmt s, List<ASTStatementSequenceNode> newChangedBodyPart, ASTParentNodeFinder finder) {
    // get the stmt seq node of invoke stmt
    Object stmtSeqNode = finder.getParentOf(s);
    // find the parent node of the stmt seq node
    Object ParentOfStmtSeq = finder.getParentOf(stmtSeqNode);
    if (ParentOfStmtSeq == null) {
        throw new RuntimeException("MethodCall FInder: parent of stmt seq node not found");
    }
    ASTNode node = (ASTNode) ParentOfStmtSeq;
    if (node instanceof ASTMethodNode) {
        // get the subBody to replace
        List<Object> subBodyToReplace = getSubBodyFromSingleSubBodyNode(node);
        List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
        ((ASTMethodNode) node).replaceBody(newBody);
        return true;
    } else if (node instanceof ASTSynchronizedBlockNode) {
        // get the subBody to replace
        List<Object> subBodyToReplace = getSubBodyFromSingleSubBodyNode(node);
        List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
        ((ASTSynchronizedBlockNode) node).replaceBody(newBody);
        return true;
    } else if (node instanceof ASTLabeledBlockNode) {
        // get the subBody to replace
        List<Object> subBodyToReplace = getSubBodyFromSingleSubBodyNode(node);
        List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
        ((ASTLabeledBlockNode) node).replaceBody(newBody);
        return true;
    } else if (node instanceof ASTUnconditionalLoopNode) {
        // get the subBody to replace
        List<Object> subBodyToReplace = getSubBodyFromSingleSubBodyNode(node);
        List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
        ((ASTUnconditionalLoopNode) node).replaceBody(newBody);
        return true;
    } else if (node instanceof ASTIfNode) {
        // get the subBody to replace
        List<Object> subBodyToReplace = getSubBodyFromSingleSubBodyNode(node);
        List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
        ((ASTIfNode) node).replaceBody(newBody);
        return true;
    } else if (node instanceof ASTWhileNode) {
        // get the subBody to replace
        List<Object> subBodyToReplace = getSubBodyFromSingleSubBodyNode(node);
        List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
        ((ASTWhileNode) node).replaceBody(newBody);
        return true;
    } else if (node instanceof ASTDoWhileNode) {
        // get the subBody to replace
        List<Object> subBodyToReplace = getSubBodyFromSingleSubBodyNode(node);
        List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
        ((ASTDoWhileNode) node).replaceBody(newBody);
        return true;
    } else if (node instanceof ASTForLoopNode) {
        // get the subBody to replace
        List<Object> subBodyToReplace = getSubBodyFromSingleSubBodyNode(node);
        List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
        ((ASTForLoopNode) node).replaceBody(newBody);
        return true;
    } else if (node instanceof ASTIfElseNode) {
        List<Object> subBodies = node.get_SubBodies();
        if (subBodies.size() != 2)
            throw new RuntimeException("Found an ifelse ASTNode which does not have two bodies");
        List<Object> ifBody = (List<Object>) subBodies.get(0);
        List<Object> elseBody = (List<Object>) subBodies.get(1);
        // find out which of these bodies has the stmt seq node with the
        // invoke stmt
        int subBodyNumber = -1;
        Iterator<Object> it = ifBody.iterator();
        while (it.hasNext()) {
            Object temp = it.next();
            if (temp == stmtSeqNode) {
                subBodyNumber = 0;
                break;
            }
        }
        if (subBodyNumber != 0) {
            it = elseBody.iterator();
            while (it.hasNext()) {
                Object temp = it.next();
                if (temp == stmtSeqNode) {
                    subBodyNumber = 1;
                    break;
                }
            }
        }
        List<Object> subBodyToReplace = null;
        if (subBodyNumber == 0)
            subBodyToReplace = ifBody;
        else if (subBodyNumber == 1)
            subBodyToReplace = elseBody;
        else
            throw new RuntimeException("Could not find the related ASTNode in the method");
        List<Object> newBody = createNewSubBody(subBodyToReplace, newChangedBodyPart, stmtSeqNode);
        if (subBodyNumber == 0) {
            ((ASTIfElseNode) node).replaceBody(newBody, elseBody);
            return true;
        } else if (subBodyNumber == 1) {
            ((ASTIfElseNode) node).replaceBody(ifBody, newBody);
            return true;
        }
    } else if (node instanceof ASTTryNode) {
        // NOTE THAT method INLINING Is currently only done in the tryBody
        // and not the catchBody
        // THe only reason for this being that mostly method calls are made
        // in the try and not the catch
        // get try body
        List<Object> tryBody = ((ASTTryNode) node).get_TryBody();
        Iterator<Object> it = tryBody.iterator();
        // find whether stmtSeqNode is in the tryBody
        boolean inTryBody = false;
        while (it.hasNext()) {
            ASTNode temp = (ASTNode) it.next();
            if (temp == stmtSeqNode) {
                inTryBody = true;
                break;
            }
        }
        if (!inTryBody) {
            // return without making any changes
            return false;
        }
        List<Object> newBody = createNewSubBody(tryBody, newChangedBodyPart, stmtSeqNode);
        ((ASTTryNode) node).replaceTryBody(newBody);
        return true;
    } else if (node instanceof ASTSwitchNode) {
        List<Object> indexList = ((ASTSwitchNode) node).getIndexList();
        Map<Object, List<Object>> index2BodyList = ((ASTSwitchNode) node).getIndex2BodyList();
        Iterator<Object> it = indexList.iterator();
        while (it.hasNext()) {
            // going through all the cases of the switch
            // statement
            Object currentIndex = it.next();
            List<Object> body = index2BodyList.get(currentIndex);
            if (body != null) {
                // this body is a list of ASTNodes
                // see if it contains stmtSeqNode
                boolean found = false;
                Iterator<Object> itBody = body.iterator();
                while (itBody.hasNext()) {
                    ASTNode temp = (ASTNode) itBody.next();
                    if (temp == stmtSeqNode) {
                        found = true;
                        break;
                    }
                }
                if (found) {
                    // this is the body which has the stmt seq node
                    List<Object> newBody = createNewSubBody(body, newChangedBodyPart, stmtSeqNode);
                    // put this body in the Map
                    index2BodyList.put(currentIndex, newBody);
                    // replace in actual switchNode
                    ((ASTSwitchNode) node).replaceIndex2BodyList(index2BodyList);
                    return true;
                }
            }
        // if body not null
        }
    // going through all cases
    }
    return false;
}
Also used : ASTSynchronizedBlockNode(soot.dava.internal.AST.ASTSynchronizedBlockNode) ASTIfNode(soot.dava.internal.AST.ASTIfNode) ASTTryNode(soot.dava.internal.AST.ASTTryNode) ASTDoWhileNode(soot.dava.internal.AST.ASTDoWhileNode) ASTWhileNode(soot.dava.internal.AST.ASTWhileNode) ASTNode(soot.dava.internal.AST.ASTNode) Iterator(java.util.Iterator) ASTMethodNode(soot.dava.internal.AST.ASTMethodNode) ArrayList(java.util.ArrayList) List(java.util.List) ASTUnconditionalLoopNode(soot.dava.internal.AST.ASTUnconditionalLoopNode) ASTForLoopNode(soot.dava.internal.AST.ASTForLoopNode) ASTIfElseNode(soot.dava.internal.AST.ASTIfElseNode) ASTSwitchNode(soot.dava.internal.AST.ASTSwitchNode) ASTLabeledBlockNode(soot.dava.internal.AST.ASTLabeledBlockNode)

Aggregations

List (java.util.List)3 ASTIfNode (soot.dava.internal.AST.ASTIfNode)3 ASTDoWhileNode (soot.dava.internal.AST.ASTDoWhileNode)2 ASTForLoopNode (soot.dava.internal.AST.ASTForLoopNode)2 ASTIfElseNode (soot.dava.internal.AST.ASTIfElseNode)2 ASTLabeledBlockNode (soot.dava.internal.AST.ASTLabeledBlockNode)2 ASTWhileNode (soot.dava.internal.AST.ASTWhileNode)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 DecompilationException (soot.dava.DecompilationException)1 ASTCondition (soot.dava.internal.AST.ASTCondition)1 ASTLabeledNode (soot.dava.internal.AST.ASTLabeledNode)1 ASTMethodNode (soot.dava.internal.AST.ASTMethodNode)1 ASTNode (soot.dava.internal.AST.ASTNode)1 ASTStatementSequenceNode (soot.dava.internal.AST.ASTStatementSequenceNode)1 ASTSwitchNode (soot.dava.internal.AST.ASTSwitchNode)1 ASTSynchronizedBlockNode (soot.dava.internal.AST.ASTSynchronizedBlockNode)1 ASTTryNode (soot.dava.internal.AST.ASTTryNode)1 ASTUnconditionalLoopNode (soot.dava.internal.AST.ASTUnconditionalLoopNode)1 ASTParentNodeFinder (soot.dava.toolkits.base.AST.traversals.ASTParentNodeFinder)1