Search in sources :

Example 6 with ASTStatementSequenceNode

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

the class DavaStaticBlockCleaner method inline.

/*
     * Method called with a sootMethod to decide whether this method should be inlined or not
     * returns null if it shouldnt be inlined
     *
     * A method can be inlined if it belongs to the same class and also if its static....(why???)
     */
public ASTMethodNode inline(SootMethod maybeInline) {
    if (sootClass != null) {
        // 1, method should belong to the same class as the clinit method
        if (sootClass.declaresMethod(maybeInline.getSubSignature())) {
            if (Modifier.isStatic(maybeInline.getModifiers())) {
                // retireve the active body
                if (!maybeInline.hasActiveBody())
                    throw new RuntimeException("method " + maybeInline.getName() + " has no active body!");
                Body bod = maybeInline.getActiveBody();
                Chain units = ((DavaBody) bod).getUnits();
                if (units.size() != 1) {
                    throw new RuntimeException("DavaBody AST doesn't have single root.");
                }
                ASTNode ASTtemp = (ASTNode) units.getFirst();
                if (!(ASTtemp instanceof ASTMethodNode))
                    throw new RuntimeException("Starting node of DavaBody AST is not an ASTMethodNode");
                // restricting to methods which do not have any variables declared
                ASTMethodNode toReturn = (ASTMethodNode) ASTtemp;
                ASTStatementSequenceNode declarations = toReturn.getDeclarations();
                if (declarations.getStatements().size() == 0) {
                    // System.out.println("No declarations in the method. we can inline this method");
                    return toReturn;
                }
            }
        }
    }
    // meaning dont inline
    return null;
}
Also used : Chain(soot.util.Chain) ASTNode(soot.dava.internal.AST.ASTNode) ASTMethodNode(soot.dava.internal.AST.ASTMethodNode) ASTStatementSequenceNode(soot.dava.internal.AST.ASTStatementSequenceNode) Body(soot.Body)

Example 7 with ASTStatementSequenceNode

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

the class IfElseSplitter method getLastStmt.

/*
	 * Given a list of ASTNodes see if the last astnode is a StatementSequenceNode
	 * if not return null
	 * else, return the last statement in this node
	 */
public Stmt getLastStmt(List<Object> body) {
    if (body.size() == 0)
        return null;
    ASTNode lastNode = (ASTNode) body.get(body.size() - 1);
    if (!(lastNode instanceof ASTStatementSequenceNode))
        return null;
    ASTStatementSequenceNode stmtNode = (ASTStatementSequenceNode) lastNode;
    List<AugmentedStmt> stmts = stmtNode.getStatements();
    if (stmts.size() == 0)
        return null;
    AugmentedStmt lastStmt = stmts.get(stmts.size() - 1);
    return lastStmt.get_Stmt();
}
Also used : ASTNode(soot.dava.internal.AST.ASTNode) ASTStatementSequenceNode(soot.dava.internal.AST.ASTStatementSequenceNode) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt)

Example 8 with ASTStatementSequenceNode

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

the class SuperFirstStmtHandler method createDavaStoreStmts.

/*
	 * Create the following code:
	 * 
	 * DavaSuperHandler handler; handler = new DavaSuperHandler(); //code to
	 * evaluate all args in args2
	 * 
	 * //evaluate 1st arg in args2 --------- handler.store(firstArg);
	 * 
	 * //evaluate 2nd arg in args2 --------- handler.store(secondArg);
	 * 
	 * //AND SO ON TILL ALL ARGS ARE FINISHED
	 * 
	 * return handler;
	 */
private void createDavaStoreStmts() {
    List<AugmentedStmt> davaHandlerStmts = new ArrayList<AugmentedStmt>();
    // create object of DavaSuperHandler handler
    SootClass sootClass = new SootClass("DavaSuperHandler");
    Type localType = sootClass.getType();
    Local newLocal = new JimpleLocal("handler", localType);
    /*
		 * Create * DavaSuperHandler handler; *
		 */
    DVariableDeclarationStmt varStmt = null;
    varStmt = new DVariableDeclarationStmt(localType, newPreInitDavaBody);
    varStmt.addLocal(newLocal);
    AugmentedStmt as = new AugmentedStmt(varStmt);
    davaHandlerStmts.add(as);
    /*
		 * create * handler = new DavaSuperHandler(); *
		 */
    // create RHS
    DNewInvokeExpr invokeExpr = new DNewInvokeExpr(RefType.v(sootClass), makeMethodRef("DavaSuperHandler", new ArrayList()), new ArrayList());
    // create LHS
    GAssignStmt initialization = new GAssignStmt(newLocal, invokeExpr);
    // add to stmts
    davaHandlerStmts.add(new AugmentedStmt(initialization));
    /*
		 * create * handler.store(firstArg); *
		 */
    // best done in a loop for all args
    Iterator typeIt = argsTwoTypes.iterator();
    Iterator valIt = argsTwoValues.iterator();
    // make reference to a method of name store takes one Object arg belongs
    // to DavaSuperHandler
    ArrayList tempList = new ArrayList();
    // SHOULD BE OBJECT
    tempList.add(RefType.v("java.lang.Object"));
    SootMethod method = Scene.v().makeSootMethod("store", tempList, VoidType.v());
    // set the declaring class of new method to be the DavaSuperHandler
    // class
    method.setDeclaringClass(sootClass);
    SootMethodRef getMethodRef = method.makeRef();
    while (typeIt.hasNext() && valIt.hasNext()) {
        Type tempType = (Type) typeIt.next();
        Value tempVal = (Value) valIt.next();
        AugmentedStmt toAdd = createStmtAccordingToType(tempType, tempVal, newLocal, getMethodRef);
        davaHandlerStmts.add(toAdd);
    }
    // sanity check
    if (typeIt.hasNext() || valIt.hasNext())
        throw new DecompilationException("Error creating DavaHandler stmts");
    /*
		 * code to add defs
		 */
    List<Local> uniqueLocals = addDefsToLiveVariables();
    Iterator<Local> localIt = uniqueLocals.iterator();
    while (localIt.hasNext()) {
        Local local = localIt.next();
        AugmentedStmt toAdd = createStmtAccordingToType(local.getType(), local, newLocal, getMethodRef);
        davaHandlerStmts.add(toAdd);
    }
    // set the mustInitialize field to uniqueLocals so that before Y we can
    // assign these locals
    mustInitialize = uniqueLocals;
    /*
		 * create * return handler; *
		 */
    GReturnStmt returnStmt = new GReturnStmt(newLocal);
    davaHandlerStmts.add(new AugmentedStmt(returnStmt));
    // the appropriate dava handler stmts are all in place within
    // davaHandlerStmts
    // store them in an ASTSTatementSequenceNode
    ASTStatementSequenceNode addedNode = new ASTStatementSequenceNode(davaHandlerStmts);
    // add to method body
    List<Object> subBodies = newASTPreInitMethod.get_SubBodies();
    if (subBodies.size() != 1)
        throw new CorruptASTException("ASTMethodNode does not have one subBody");
    List<Object> body = (List<Object>) subBodies.get(0);
    body.add(addedNode);
    newASTPreInitMethod.replaceBody(body);
}
Also used : GAssignStmt(soot.grimp.internal.GAssignStmt) DVariableDeclarationStmt(soot.dava.internal.javaRep.DVariableDeclarationStmt) SootMethodRef(soot.SootMethodRef) CorruptASTException(soot.dava.CorruptASTException) ArrayList(java.util.ArrayList) JimpleLocal(soot.jimple.internal.JimpleLocal) Local(soot.Local) DecompilationException(soot.dava.DecompilationException) ASTStatementSequenceNode(soot.dava.internal.AST.ASTStatementSequenceNode) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) SootClass(soot.SootClass) GReturnStmt(soot.grimp.internal.GReturnStmt) JimpleLocal(soot.jimple.internal.JimpleLocal) DNewInvokeExpr(soot.dava.internal.javaRep.DNewInvokeExpr) RefType(soot.RefType) ShortType(soot.ShortType) BooleanType(soot.BooleanType) ByteType(soot.ByteType) Type(soot.Type) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) CharType(soot.CharType) LongType(soot.LongType) PrimType(soot.PrimType) VoidType(soot.VoidType) Iterator(java.util.Iterator) Value(soot.Value) SootMethod(soot.SootMethod) List(java.util.List) ArrayList(java.util.ArrayList)

Example 9 with ASTStatementSequenceNode

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

the class SuperFirstStmtHandler method createNewASTConstructor.

public void createNewASTConstructor(ASTStatementSequenceNode initNode) {
    List<Object> newConstructorBody = new ArrayList<Object>();
    List<AugmentedStmt> newStmts = new ArrayList<AugmentedStmt>();
    /*
		 * add any definitions to live variables that might be in body X
		 */
    // we have gotten argsTwoType size() out of the handler so thats the
    // index count
    // mustInitialize has the live variables that need to be initialized
    // create new ReftType for DavaSuperHandler
    RefType type = (new SootClass("DavaSuperHandler")).getType();
    // make JimpleLocal to be used in each arg
    // takes care of
    Local jimpleLocal = new JimpleLocal("handler", type);
    // handler
    // make reference to a method of name get takes one int arg belongs to
    // DavaSuperHandler
    ArrayList tempList = new ArrayList();
    tempList.add(IntType.v());
    SootMethodRef getMethodRef = makeMethodRef("get", tempList);
    // Iterator typeIt = argsTwoTypes.iterator();
    if (mustInitialize != null) {
        Iterator<Local> initIt = mustInitialize.iterator();
        while (initIt.hasNext()) {
            Local initLocal = initIt.next();
            Type tempType = initLocal.getType();
            // takes
            DIntConstant arg = DIntConstant.v(mustInitializeIndex, IntType.v());
            // care
            // of
            // the
            // index
            mustInitializeIndex++;
            ArrayList tempArgList = new ArrayList();
            tempArgList.add(arg);
            DVirtualInvokeExpr tempInvokeExpr = new DVirtualInvokeExpr(jimpleLocal, getMethodRef, tempArgList, new HashSet<Object>());
            // NECESASARY CASTING OR RETRIEVAL OF PRIM TYPES TO BE DONE HERE
            Value toAddExpr = getProperCasting(tempType, tempInvokeExpr);
            if (toAddExpr == null)
                throw new DecompilationException("UNABLE TO CREATE TOADDEXPR:" + tempType);
            // need to create a def stmt with the local on the left and
            // toAddExpr on the right
            GAssignStmt assign = new GAssignStmt(initLocal, toAddExpr);
            newStmts.add(new AugmentedStmt(assign));
        }
    }
    // add any statements following the this.<init> statement
    Iterator<AugmentedStmt> it = initNode.getStatements().iterator();
    while (it.hasNext()) {
        AugmentedStmt augStmt = (AugmentedStmt) it.next();
        Stmt stmtTemp = augStmt.get_Stmt();
        if (stmtTemp == originalConstructorUnit) {
            break;
        }
    }
    while (it.hasNext()) {
        /*
			 * notice we dont need to clone these because these will be removed
			 * from the other method from which we are copying these
			 */
        newStmts.add(it.next());
    }
    if (newStmts.size() > 0) {
        newConstructorBody.add(new ASTStatementSequenceNode(newStmts));
    }
    // adding body Y now
    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> itOld = oldASTBody.iterator();
    boolean sanity = false;
    while (itOld.hasNext()) {
        // going through originalASTMethodNode's ASTNodes
        ASTNode tempNode = (ASTNode) itOld.next();
        // enter only if its not the initNode
        if (tempNode instanceof ASTStatementSequenceNode) {
            if ((((ASTStatementSequenceNode) tempNode).getStatements()).equals(initNode.getStatements())) {
                sanity = true;
                break;
            }
        }
    }
    if (!sanity) {
        // means we never found the initNode which shouldnt happen
        throw new DecompilationException("never found the init node");
    }
    // Y are all the nodes following the initNode
    while (itOld.hasNext()) {
        newConstructorBody.add(itOld.next());
    }
    // setDeclarations in newNode
    // The LocalVariableCleaner which is called in the end of DavaBody will
    // clear up any declarations that are not required
    List<AugmentedStmt> newConstructorDeclarations = new ArrayList<AugmentedStmt>();
    for (AugmentedStmt as : originalASTMethod.getDeclarations().getStatements()) {
        DVariableDeclarationStmt varDecStmt = (DVariableDeclarationStmt) as.get_Stmt();
        newConstructorDeclarations.add(new AugmentedStmt((DVariableDeclarationStmt) varDecStmt.clone()));
    }
    ASTStatementSequenceNode newDecs = new ASTStatementSequenceNode(new ArrayList<AugmentedStmt>());
    if (newConstructorDeclarations.size() > 0) {
        newDecs = new ASTStatementSequenceNode(newConstructorDeclarations);
        // DONT FORGET TO SET THE DECLARATIONS IN THE METHOD ONCE IT IS
        // CREATED
        // newASTConstructorMethod.setDeclarations(newDecs);
        // declarations are always the first element
        newConstructorBody.add(0, newDecs);
    }
    // so we have any declarations followed by body Y
    // have to put the newConstructorBody into an list of subBodies which
    // goes into the newASTConstructorMethod
    newASTConstructorMethod = new ASTMethodNode(newConstructorBody);
    // dont forget to set the declarations
    newASTConstructorMethod.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) JimpleLocal(soot.jimple.internal.JimpleLocal) 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) RefType(soot.RefType) DVirtualInvokeExpr(soot.dava.internal.javaRep.DVirtualInvokeExpr) ASTNode(soot.dava.internal.AST.ASTNode) List(java.util.List) ArrayList(java.util.ArrayList) ASTMethodNode(soot.dava.internal.AST.ASTMethodNode) GAssignStmt(soot.grimp.internal.GAssignStmt) SootMethodRef(soot.SootMethodRef) JimpleLocal(soot.jimple.internal.JimpleLocal) Local(soot.Local) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) SootClass(soot.SootClass) RefType(soot.RefType) ShortType(soot.ShortType) BooleanType(soot.BooleanType) ByteType(soot.ByteType) Type(soot.Type) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) CharType(soot.CharType) LongType(soot.LongType) PrimType(soot.PrimType) VoidType(soot.VoidType) DIntConstant(soot.dava.internal.javaRep.DIntConstant) Value(soot.Value)

Example 10 with ASTStatementSequenceNode

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

the class SuperFirstStmtHandler method changeOriginalAST.

/*
	 * Remove the entire body and replace with the statement this(args1,
	 * B.preInit(args1));
	 */
public boolean changeOriginalAST() {
    // argsOne followed by a method call to preInit
    if (originalConstructorExpr == null) {
        // System.out.println("originalConstructorExpr is null");
        return false;
    }
    List thisArgList = new ArrayList();
    thisArgList.addAll(argsOneValues);
    DStaticInvokeExpr newInvokeExpr = new DStaticInvokeExpr(newSootPreInitMethod.makeRef(), argsOneValues);
    thisArgList.add(newInvokeExpr);
    // the methodRef of themethod to be called is the new constructor we
    // created
    InstanceInvokeExpr tempExpr = new DSpecialInvokeExpr(originalConstructorExpr.getBase(), newConstructor.makeRef(), thisArgList);
    originalDavaBody.set_ConstructorExpr(tempExpr);
    // create Invoke Stmt with tempExpr as the expression
    GInvokeStmt s = new GInvokeStmt(tempExpr);
    originalDavaBody.set_ConstructorUnit(s);
    // originalASTMethod has to be made empty
    originalASTMethod.setDeclarations(new ASTStatementSequenceNode(new ArrayList<AugmentedStmt>()));
    originalASTMethod.replaceBody(new ArrayList<Object>());
    return true;
}
Also used : DSpecialInvokeExpr(soot.dava.internal.javaRep.DSpecialInvokeExpr) DStaticInvokeExpr(soot.dava.internal.javaRep.DStaticInvokeExpr) GInvokeStmt(soot.grimp.internal.GInvokeStmt) ArrayList(java.util.ArrayList) InstanceInvokeExpr(soot.jimple.InstanceInvokeExpr) List(java.util.List) ArrayList(java.util.ArrayList) ASTStatementSequenceNode(soot.dava.internal.AST.ASTStatementSequenceNode)

Aggregations

ASTStatementSequenceNode (soot.dava.internal.AST.ASTStatementSequenceNode)12 ArrayList (java.util.ArrayList)9 List (java.util.List)8 AugmentedStmt (soot.dava.internal.asg.AugmentedStmt)8 ASTNode (soot.dava.internal.AST.ASTNode)7 ASTMethodNode (soot.dava.internal.AST.ASTMethodNode)5 DVariableDeclarationStmt (soot.dava.internal.javaRep.DVariableDeclarationStmt)5 GAssignStmt (soot.grimp.internal.GAssignStmt)5 GInvokeStmt (soot.grimp.internal.GInvokeStmt)5 GReturnStmt (soot.grimp.internal.GReturnStmt)5 DefinitionStmt (soot.jimple.DefinitionStmt)5 Stmt (soot.jimple.Stmt)5 Iterator (java.util.Iterator)4 Local (soot.Local)3 RefType (soot.RefType)3 SootClass (soot.SootClass)3 SootMethodRef (soot.SootMethodRef)3 Value (soot.Value)3 CorruptASTException (soot.dava.CorruptASTException)3 DecompilationException (soot.dava.DecompilationException)3