Search in sources :

Example 21 with AugmentedStmt

use of soot.dava.internal.asg.AugmentedStmt in project soot by Sable.

the class SuperFirstStmtHandler method createNewASTPreInitMethod.

/*
	 * January 23rd New Algorithm Leave originalASTMethod unchanged Clone
	 * everything and copy only those which are needed in the
	 * newASTPreInitMethod
	 */
private void createNewASTPreInitMethod(ASTStatementSequenceNode initNode) {
    List<Object> newPreinitBody = new ArrayList<Object>();
    // start adding ASTNodes into newPreinitBody from the
    // originalASTMethod's body until we reach initNode
    List<Object> originalASTMethodSubBodies = originalASTMethod.get_SubBodies();
    if (originalASTMethodSubBodies.size() != 1)
        throw new CorruptASTException("size of ASTMethodNode subBody not 1");
    List<Object> oldASTBody = (List<Object>) originalASTMethodSubBodies.get(0);
    Iterator<Object> it = oldASTBody.iterator();
    boolean sanity = false;
    while (it.hasNext()) {
        // going through originalASTMethodNode's ASTNodes
        ASTNode tempNode = (ASTNode) it.next();
        // enter only if its not the initNode
        if (tempNode instanceof ASTStatementSequenceNode) {
            if ((((ASTStatementSequenceNode) tempNode).getStatements()).equals(initNode.getStatements())) {
                sanity = true;
                break;
            } else {
                // this was not the initNode so we add
                newPreinitBody.add(tempNode);
            }
        } else {
            // not a stmtseq so simply add it
            newPreinitBody.add(tempNode);
        }
    }
    if (!sanity) {
        // means we never found the initNode which shouldnt happen
        throw new DecompilationException("never found the init node");
    }
    // at this moment newPreinitBody contains all of X except for any stmts
    // above the this.init call in the stmtseq node
    // copy those
    List<AugmentedStmt> newStmts = new ArrayList<AugmentedStmt>();
    for (AugmentedStmt augStmt : initNode.getStatements()) {
        Stmt stmtTemp = augStmt.get_Stmt();
        if (stmtTemp == originalConstructorUnit) {
            break;
        }
        // adding any stmt until constructorUnit into topList for
        // newMethodNode
        /*
			 * notice we dont need to clone these because these will be removed
			 * from the other method from which we are copying these
			 */
        newStmts.add(augStmt);
    }
    if (newStmts.size() > 0) {
        newPreinitBody.add(new ASTStatementSequenceNode(newStmts));
    }
    // setDeclarations in newNode
    // The LocalVariableCleaner which is called in the end of DavaBody will
    // clear up any declarations that are not required
    List<AugmentedStmt> newPreinitDeclarations = new ArrayList<AugmentedStmt>();
    for (AugmentedStmt as : originalASTMethod.getDeclarations().getStatements()) {
        DVariableDeclarationStmt varDecStmt = (DVariableDeclarationStmt) as.get_Stmt();
        newPreinitDeclarations.add(new AugmentedStmt((DVariableDeclarationStmt) varDecStmt.clone()));
    }
    ASTStatementSequenceNode newDecs = new ASTStatementSequenceNode(new ArrayList<AugmentedStmt>());
    if (newPreinitDeclarations.size() > 0) {
        newDecs = new ASTStatementSequenceNode(newPreinitDeclarations);
        // DONT FORGET TO SET THE DECLARATIONS IN THE METHOD ONCE IT IS
        // CREATED
        // newASTPreInitMethod.setDeclarations(newDecs);
        // when we copied the body X the first Node copied was the
        // Declarations from the originalASTMethod
        // replace that with this new one
        newPreinitBody.remove(0);
        newPreinitBody.add(0, newDecs);
    }
    // otherwise super is infact the first stmt
    if (newPreinitBody.size() < 1) {
        // System.out.println("Method node empty doing nothing returning");
        // meaning ASTMethodNode for this method
        newASTPreInitMethod = null;
        // not created
        return;
    }
    // so we have any declarations followed by body X
    // NEXT THING SHOULD BE CODE TO CREATE A DAVAHANDLER AND STORE THE ARGS
    // TO SUPER IN IT
    // HOWEVER WE WILL DELAY THIS TILL UNTIL WE ARE READY TO FINALIZE the
    // PREINIT
    // reason for delaying is that even though we know that the body is not
    // empty the body
    // could be made empty by the transformations which act in the finalize
    // method
    // have to put the newPreinitBody into an list of subBodies which goes
    // into the newASTPreInitMethod
    newASTPreInitMethod = new ASTMethodNode(newPreinitBody);
    // dont forget to set the declarations
    newASTPreInitMethod.setDeclarations(newDecs);
}
Also used : DVariableDeclarationStmt(soot.dava.internal.javaRep.DVariableDeclarationStmt) CorruptASTException(soot.dava.CorruptASTException) ArrayList(java.util.ArrayList) DecompilationException(soot.dava.DecompilationException) ASTStatementSequenceNode(soot.dava.internal.AST.ASTStatementSequenceNode) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) GReturnStmt(soot.grimp.internal.GReturnStmt) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) DVariableDeclarationStmt(soot.dava.internal.javaRep.DVariableDeclarationStmt) GInvokeStmt(soot.grimp.internal.GInvokeStmt) Stmt(soot.jimple.Stmt) GAssignStmt(soot.grimp.internal.GAssignStmt) DefinitionStmt(soot.jimple.DefinitionStmt) ASTNode(soot.dava.internal.AST.ASTNode) List(java.util.List) ArrayList(java.util.ArrayList) ASTMethodNode(soot.dava.internal.AST.ASTMethodNode)

Example 22 with AugmentedStmt

use of soot.dava.internal.asg.AugmentedStmt in project soot by Sable.

the class SuperFirstStmtHandler method inASTStatementSequenceNode.

/*
	 * looking for an init stmt
	 */
public void inASTStatementSequenceNode(ASTStatementSequenceNode node) {
    for (AugmentedStmt as : node.getStatements()) {
        Unit u = as.get_Stmt();
        if (u == originalConstructorUnit) {
            // System.out.println("Found the constructorUnit"+u);
            // ONE: make sure the parent of the super() call is an
            // ASTMethodNode
            ASTParentNodeFinder parentFinder = new ASTParentNodeFinder();
            originalASTMethod.apply(parentFinder);
            Object tempParent = parentFinder.getParentOf(node);
            if (tempParent != originalASTMethod) {
                // System.out.println("ASTMethod node is not the parent of
                // constructorUnit");
                // notice since we cant remove one call of super there is no
                // point
                // in trying to remove any other calls to super
                removeInit();
                return;
            }
            // only gets here if the call to super is not nested within some
            // other construct
            /**
             *******************************************************
             */
            /**
             **************** CREATING PREINIT METHOD **************
             */
            /**
             *******************************************************
             */
            // CREATE UNIQUE METHOD
            // new method is initalized in
            createSootPreInitMethod();
            // newSootPreInitMethod
            // Create ASTMethodNode for this SootMethod
            // the field
            createNewASTPreInitMethod(node);
            if (newASTPreInitMethod == null) {
                // could not create ASTMethodNode for some reason or the
                // other
                // just silently return
                // System.out.println(">>>>>>>>>>>>>>>>Couldnt not create
                // ASTMethodNode for the new PreInitMethod");
                removeInit();
                return;
            }
            if (!finalizePreInitMethod()) {
                // shouldnt be creating PreInit
                // System.out.println(">>>>>>>>>>>>>>SHOULDNT BE CREATING
                // PREINIT");
                removeInit();
                return;
            }
            /**
             *******************************************************
             */
            /**
             ************ CREATING NEW CONSTRUCTOR *****************
             */
            /**
             *******************************************************
             */
            // create SootMethod for the constructor
            createNewConstructor();
            createNewASTConstructor(node);
            if (!createCallToSuper()) {
                // could not create call to super
                // still safe to simply exit
                // System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>Could not
                // create call to super...SuperFirstStmtHandler");
                removeInit();
                return;
            }
            finalizeConstructor();
            /**
             *******************************************************
             */
            if (changeOriginalAST()) {
                // System.out.println("Done Done Done");
                debug("SuperFirstStmtHandler....inASTStatementSeuqneNode", "Added PreInit");
                G.v().SootMethodAddedByDava = true;
                G.v().SootMethodsAdded.add(newSootPreInitMethod);
                G.v().SootMethodsAdded.add(newConstructor);
                /**
                 *******************************************************
                 */
                /**
                 **************** CREATING INNER CLASS *****************
                 */
                /**
                 *******************************************************
                 */
                // notice that inner class is created by DavaPrinter in the
                // printTo method
                // all we do is set a Global to true and later on when the
                // SootClass is being
                // output the inner class will be output also
                G.v().SootClassNeedsDavaSuperHandlerClass.add(originalSootClass);
            // System.out.println("\n\nSet SootMethodAddedByDava to
            // true\n\n");
            }
        }
    }
}
Also used : ASTParentNodeFinder(soot.dava.toolkits.base.AST.traversals.ASTParentNodeFinder) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) Unit(soot.Unit)

Example 23 with AugmentedStmt

use of soot.dava.internal.asg.AugmentedStmt in project soot by Sable.

the class ShortcutIfGenerator method inASTStatementSequenceNode.

public void inASTStatementSequenceNode(ASTStatementSequenceNode node) {
    for (AugmentedStmt as : node.getStatements()) {
        Stmt s = as.get_Stmt();
        if (!(s instanceof DefinitionStmt))
            continue;
        DefinitionStmt ds = (DefinitionStmt) s;
        ValueBox rightBox = ds.getRightOpBox();
        Value right = rightBox.getValue();
        /*
			 * Going to match int i = (int) z where z is a boolean
			 * or int i= z i.e. without the cast
			 */
        // right type should contain the expected type on the left
        // in the case of the cast this is the cast type else just get the left type
        Type rightType = null;
        ValueBox OpBox = null;
        if (right instanceof CastExpr) {
            rightType = ((CastExpr) right).getCastType();
            OpBox = ((CastExpr) right).getOpBox();
        } else {
            rightType = ds.getLeftOp().getType();
            OpBox = rightBox;
        }
        if (!(rightType instanceof IntType)) {
            continue;
        }
        Value Op = OpBox.getValue();
        if (!(Op.getType() instanceof BooleanType)) {
            continue;
        }
        // ready for the switch
        ImmediateBox trueBox = new ImmediateBox(IntConstant.v(1));
        ImmediateBox falseBox = new ImmediateBox(IntConstant.v(0));
        DShortcutIf shortcut = new DShortcutIf(OpBox, trueBox, falseBox);
        if (DEBUG)
            System.out.println("created: " + shortcut);
        rightBox.setValue(shortcut);
    }
}
Also used : IntType(soot.IntType) Type(soot.Type) BooleanType(soot.BooleanType) ImmediateBox(soot.jimple.internal.ImmediateBox) ValueBox(soot.ValueBox) DShortcutIf(soot.dava.internal.javaRep.DShortcutIf) Value(soot.Value) CastExpr(soot.jimple.CastExpr) BooleanType(soot.BooleanType) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) DefinitionStmt(soot.jimple.DefinitionStmt) Stmt(soot.jimple.Stmt) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) DefinitionStmt(soot.jimple.DefinitionStmt) IntType(soot.IntType)

Example 24 with AugmentedStmt

use of soot.dava.internal.asg.AugmentedStmt in project soot by Sable.

the class UselessAbruptStmtRemover method caseASTStatementSequenceNode.

public void caseASTStatementSequenceNode(ASTStatementSequenceNode node) {
    Iterator<AugmentedStmt> it = node.getStatements().iterator();
    AugmentedStmt remove = null;
    ASTLabeledNode target = null;
    while (it.hasNext()) {
        AugmentedStmt as = it.next();
        Stmt s = as.get_Stmt();
        // we only care about break and continue stmts
        if (!(s instanceof DAbruptStmt)) {
            continue;
        }
        DAbruptStmt abrupt = (DAbruptStmt) s;
        String label = abrupt.getLabel().toString();
        if (label == null) {
            // analysis with implicit abrupt flow but not needed currently
            continue;
        }
        if (it.hasNext()) {
            // afterwards...that is for sure dead code
            throw new DecompilationException("Dead code detected. Report to developer");
        }
        // get the target node
        Object temp = mapper.getTarget(label);
        if (temp == null) {
            continue;
        // throw new DecompilationException("Could not find target for abrupt stmt"+abrupt.toString());
        }
        target = (ASTLabeledNode) temp;
        // will need to find parents of ancestors see if we need to initialize the finder
        if (finder == null) {
            finder = new ASTParentNodeFinder();
            methodNode.apply(finder);
        }
        if (DEBUG)
            System.out.println("Starting useless check for abrupt stmt: " + abrupt);
        // start condition is that ancestor is the stmt seq node
        ASTNode ancestor = node;
        while (ancestor != target) {
            Object tempParent = finder.getParentOf(ancestor);
            if (tempParent == null)
                throw new DecompilationException("Parent found was null!!. Report to Developer");
            ASTNode ancestorsParent = (ASTNode) tempParent;
            if (DEBUG)
                System.out.println("\tCurrent ancestorsParent has type" + ancestorsParent.getClass());
            // ancestor should be last child of ancestorsParent
            if (!checkChildLastInParent(ancestor, ancestorsParent)) {
                if (DEBUG)
                    System.out.println("\t\tCurrent ancestorParent has more children after this ancestor");
                // return from the method since this is the last stmt and we cant do anything
                return;
            }
            // ancestorsParent should not be a loop of any kind OR A SWITCH
            if (ancestorsParent instanceof ASTWhileNode || ancestorsParent instanceof ASTDoWhileNode || ancestorsParent instanceof ASTUnconditionalLoopNode || ancestorsParent instanceof ASTForLoopNode || ancestorsParent instanceof ASTSwitchNode) {
                if (DEBUG)
                    System.out.println("\t\tAncestorsParent is a loop shouldnt remove abrupt stmt");
                return;
            }
            ancestor = ancestorsParent;
        }
        if (DEBUG)
            System.out.println("\tGot to target without returning means we can remove stmt");
        remove = as;
    }
    if (remove != null) {
        List<AugmentedStmt> stmts = node.getStatements();
        stmts.remove(remove);
        if (DEBUG)
            System.out.println("\tRemoved abrupt stmt");
        if (target != null) {
            if (DEBUG)
                System.out.println("Invoking findAndKill on the target");
            UselessLabelFinder.v().findAndKill(target);
        }
        // TODO what if we just emptied a stmt seq block??
        // not doing this for the moment
        // set modified flag make finder null
        G.v().ASTTransformations_modified = true;
        finder = null;
    }
}
Also used : ASTParentNodeFinder(soot.dava.toolkits.base.AST.traversals.ASTParentNodeFinder) DecompilationException(soot.dava.DecompilationException) ASTLabeledNode(soot.dava.internal.AST.ASTLabeledNode) DAbruptStmt(soot.dava.internal.javaRep.DAbruptStmt) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) DAbruptStmt(soot.dava.internal.javaRep.DAbruptStmt) Stmt(soot.jimple.Stmt) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) ASTDoWhileNode(soot.dava.internal.AST.ASTDoWhileNode) ASTWhileNode(soot.dava.internal.AST.ASTWhileNode) ASTNode(soot.dava.internal.AST.ASTNode) ASTUnconditionalLoopNode(soot.dava.internal.AST.ASTUnconditionalLoopNode) ASTForLoopNode(soot.dava.internal.AST.ASTForLoopNode) ASTSwitchNode(soot.dava.internal.AST.ASTSwitchNode)

Example 25 with AugmentedStmt

use of soot.dava.internal.asg.AugmentedStmt in project soot by Sable.

the class ExceptionNode method splitOff_ExceptionNode.

public void splitOff_ExceptionNode(IterableSet<AugmentedStmt> newTryBody, AugmentedStmtGraph asg, IterableSet<ExceptionNode> enlist) {
    IterableSet<AugmentedStmt> oldTryBody = new IterableSet<AugmentedStmt>();
    oldTryBody.addAll(tryBody);
    IterableSet<AugmentedStmt> oldBody = new IterableSet<AugmentedStmt>();
    oldBody.addAll(body);
    for (AugmentedStmt as : newTryBody) {
        if (remove(as) == false) {
            StringBuffer b = new StringBuffer();
            for (AugmentedStmt auBody : newTryBody) b.append("\n" + auBody.toString());
            b.append("\n-");
            for (AugmentedStmt auBody : oldTryBody) b.append("\n" + auBody.toString());
            b.append("\n-");
            for (AugmentedStmt auBody : oldBody) b.append("\n" + auBody.toString());
            b.append("\n-");
            throw new RuntimeException("Tried to split off a new try body that isn't in the old one.\n" + as + "\n - " + b.toString());
        }
    }
    asg.clone_Body(catchBody);
    AugmentedStmt oldCatchTarget = handlerAugmentedStmt, newCatchTarget = asg.get_CloneOf(handlerAugmentedStmt);
    for (AugmentedStmt as : newTryBody) {
        as.remove_CSucc(oldCatchTarget);
        oldCatchTarget.remove_CPred(as);
    }
    for (AugmentedStmt as : tryBody) {
        as.remove_CSucc(newCatchTarget);
        newCatchTarget.remove_CPred(as);
    }
    for (ExceptionNode en : enlist) {
        if (this == en)
            continue;
        if (catchBody.isSupersetOf(en.get_Body())) {
            IterableSet<AugmentedStmt> clonedTryBody = new IterableSet<AugmentedStmt>();
            for (AugmentedStmt au : en.get_TryBody()) clonedTryBody.add(asg.get_CloneOf(au));
            enlist.addLast(new ExceptionNode(clonedTryBody, en.exception, asg.get_CloneOf(en.handlerAugmentedStmt)));
        }
    }
    enlist.addLast(new ExceptionNode(newTryBody, exception, asg.get_CloneOf(handlerAugmentedStmt)));
    for (ExceptionNode en : enlist) en.refresh_CatchBody(ExceptionFinder.v());
    asg.find_Dominators();
}
Also used : AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) IterableSet(soot.util.IterableSet)

Aggregations

AugmentedStmt (soot.dava.internal.asg.AugmentedStmt)27 Stmt (soot.jimple.Stmt)16 ArrayList (java.util.ArrayList)12 List (java.util.List)9 DefinitionStmt (soot.jimple.DefinitionStmt)9 ASTNode (soot.dava.internal.AST.ASTNode)8 ASTStatementSequenceNode (soot.dava.internal.AST.ASTStatementSequenceNode)8 Value (soot.Value)7 Iterator (java.util.Iterator)6 Type (soot.Type)6 GInvokeStmt (soot.grimp.internal.GInvokeStmt)6 DVariableDeclarationStmt (soot.dava.internal.javaRep.DVariableDeclarationStmt)5 GAssignStmt (soot.grimp.internal.GAssignStmt)5 GReturnStmt (soot.grimp.internal.GReturnStmt)5 IntType (soot.IntType)4 ValueBox (soot.ValueBox)4 DecompilationException (soot.dava.DecompilationException)4 DAbruptStmt (soot.dava.internal.javaRep.DAbruptStmt)4 BooleanType (soot.BooleanType)3 ByteType (soot.ByteType)3