Search in sources :

Example 1 with DNewInvokeExpr

use of soot.dava.internal.javaRep.DNewInvokeExpr in project soot by Sable.

the class SuperFirstStmtHandler method createStmtAccordingToType.

public AugmentedStmt createStmtAccordingToType(Type tempType, Value tempVal, Local newLocal, SootMethodRef getMethodRef) {
    if (tempType instanceof RefType) {
        return createAugmentedStmtToAdd(newLocal, getMethodRef, tempVal);
    } else if (tempType instanceof PrimType) {
        // The value is a primitive type
        // create wrapper object new Integer(tempVal)
        PrimType t = (PrimType) tempType;
        // create ArgList to be sent to DNewInvokeExpr constructor
        ArrayList argList = new ArrayList();
        argList.add(tempVal);
        // LongType, ShortType
        if (t == BooleanType.v()) {
            // create TypeList to be sent to makeMethodRef
            ArrayList typeList = new ArrayList();
            typeList.add(IntType.v());
            DNewInvokeExpr argForStore = new DNewInvokeExpr(RefType.v("java.lang.Boolean"), makeMethodRef("Boolean", typeList), argList);
            return createAugmentedStmtToAdd(newLocal, getMethodRef, argForStore);
        } else if (t == ByteType.v()) {
            // create TypeList to be sent to makeMethodRef
            ArrayList typeList = new ArrayList();
            typeList.add(ByteType.v());
            DNewInvokeExpr argForStore = new DNewInvokeExpr(RefType.v("java.lang.Byte"), makeMethodRef("Byte", typeList), argList);
            return createAugmentedStmtToAdd(newLocal, getMethodRef, argForStore);
        } else if (t == CharType.v()) {
            // create TypeList to be sent to makeMethodRef
            ArrayList typeList = new ArrayList();
            typeList.add(CharType.v());
            DNewInvokeExpr argForStore = new DNewInvokeExpr(RefType.v("java.lang.Character"), makeMethodRef("Character", typeList), argList);
            return createAugmentedStmtToAdd(newLocal, getMethodRef, argForStore);
        } else if (t == DoubleType.v()) {
            // create TypeList to be sent to makeMethodRef
            ArrayList typeList = new ArrayList();
            typeList.add(DoubleType.v());
            DNewInvokeExpr argForStore = new DNewInvokeExpr(RefType.v("java.lang.Double"), makeMethodRef("Double", typeList), argList);
            return createAugmentedStmtToAdd(newLocal, getMethodRef, argForStore);
        } else if (t == FloatType.v()) {
            // create TypeList to be sent to makeMethodRef
            ArrayList typeList = new ArrayList();
            typeList.add(FloatType.v());
            DNewInvokeExpr argForStore = new DNewInvokeExpr(RefType.v("java.lang.Float"), makeMethodRef("Float", typeList), argList);
            return createAugmentedStmtToAdd(newLocal, getMethodRef, argForStore);
        } else if (t == IntType.v()) {
            // create TypeList to be sent to makeMethodRef
            ArrayList typeList = new ArrayList();
            typeList.add(IntType.v());
            DNewInvokeExpr argForStore = new DNewInvokeExpr(RefType.v("java.lang.Integer"), makeMethodRef("Integer", typeList), argList);
            return createAugmentedStmtToAdd(newLocal, getMethodRef, argForStore);
        } else if (t == LongType.v()) {
            // create TypeList to be sent to makeMethodRef
            ArrayList typeList = new ArrayList();
            typeList.add(LongType.v());
            DNewInvokeExpr argForStore = new DNewInvokeExpr(RefType.v("java.lang.Long"), makeMethodRef("Long", typeList), argList);
            return createAugmentedStmtToAdd(newLocal, getMethodRef, argForStore);
        } else if (t == ShortType.v()) {
            // create TypeList to be sent to makeMethodRef
            ArrayList typeList = new ArrayList();
            typeList.add(ShortType.v());
            DNewInvokeExpr argForStore = new DNewInvokeExpr(RefType.v("java.lang.Short"), makeMethodRef("Short", typeList), argList);
            return createAugmentedStmtToAdd(newLocal, getMethodRef, argForStore);
        } else {
            throw new DecompilationException("UNHANDLED PRIMTYPE:" + tempType);
        }
    } else // end of primitivetypes
    {
        throw new DecompilationException("The type:" + tempType + " is neither a reftype or a primtype");
    }
}
Also used : RefType(soot.RefType) DNewInvokeExpr(soot.dava.internal.javaRep.DNewInvokeExpr) ArrayList(java.util.ArrayList) PrimType(soot.PrimType) DecompilationException(soot.dava.DecompilationException)

Example 2 with DNewInvokeExpr

use of soot.dava.internal.javaRep.DNewInvokeExpr in project soot by Sable.

the class DavaBody method javafy_invoke_expr.

private void javafy_invoke_expr(ValueBox vb) {
    InvokeExpr ie = (InvokeExpr) vb.getValue();
    String className = ie.getMethodRef().declaringClass().toString();
    String packageName = ie.getMethodRef().declaringClass().getJavaPackageName();
    String classPackageName = packageName;
    if (className.lastIndexOf('.') > 0) {
        // 0 doesnt make sense
        classPackageName = className.substring(0, className.lastIndexOf('.'));
    }
    if (!packageName.equals(classPackageName))
        throw new DecompilationException("Unable to retrieve package name for identifier. Please report to developer.");
    addToImportList(className);
    for (int i = 0; i < ie.getArgCount(); i++) {
        Value arg = ie.getArg(i);
        if (arg instanceof IntConstant)
            ie.getArgBox(i).setValue(DIntConstant.v(((IntConstant) arg).value, ie.getMethodRef().parameterType(i)));
        else
            javafy(ie.getArgBox(i));
    }
    if (ie instanceof InstanceInvokeExpr) {
        javafy(((InstanceInvokeExpr) ie).getBaseBox());
        if (ie instanceof VirtualInvokeExpr) {
            VirtualInvokeExpr vie = (VirtualInvokeExpr) ie;
            vb.setValue(new DVirtualInvokeExpr(vie.getBase(), vie.getMethodRef(), vie.getArgs(), thisLocals));
        } else if (ie instanceof SpecialInvokeExpr) {
            SpecialInvokeExpr sie = (SpecialInvokeExpr) ie;
            vb.setValue(new DSpecialInvokeExpr(sie.getBase(), sie.getMethodRef(), sie.getArgs()));
        } else if (ie instanceof InterfaceInvokeExpr) {
            InterfaceInvokeExpr iie = (InterfaceInvokeExpr) ie;
            vb.setValue(new DInterfaceInvokeExpr(iie.getBase(), iie.getMethodRef(), iie.getArgs()));
        } else
            throw new RuntimeException("InstanceInvokeExpr " + ie + " not javafied correctly");
    } else if (ie instanceof StaticInvokeExpr) {
        StaticInvokeExpr sie = (StaticInvokeExpr) ie;
        if (sie instanceof NewInvokeExpr) {
            NewInvokeExpr nie = (NewInvokeExpr) sie;
            RefType rt = nie.getBaseType();
            className = rt.getSootClass().toString();
            packageName = rt.getSootClass().getJavaPackageName();
            classPackageName = packageName;
            if (className.lastIndexOf('.') > 0) {
                // 0 doesnt make sense
                classPackageName = className.substring(0, className.lastIndexOf('.'));
            }
            if (!packageName.equals(classPackageName))
                throw new DecompilationException("Unable to retrieve package name for identifier. Please report to developer.");
            addToImportList(className);
            vb.setValue(new DNewInvokeExpr((RefType) nie.getType(), nie.getMethodRef(), nie.getArgs()));
        } else {
            SootMethodRef methodRef = sie.getMethodRef();
            className = methodRef.declaringClass().toString();
            packageName = methodRef.declaringClass().getJavaPackageName();
            classPackageName = packageName;
            if (className.lastIndexOf('.') > 0) {
                // 0 doesnt make sense
                classPackageName = className.substring(0, className.lastIndexOf('.'));
            }
            if (!packageName.equals(classPackageName))
                throw new DecompilationException("Unable to retrieve package name for identifier. Please report to developer.");
            addToImportList(className);
            // addPackage(methodRef.declaringClass().getJavaPackageName());
            vb.setValue(new DStaticInvokeExpr(methodRef, sie.getArgs()));
        }
    } else
        throw new RuntimeException("InvokeExpr " + ie + " not javafied correctly");
}
Also used : DNewInvokeExpr(soot.dava.internal.javaRep.DNewInvokeExpr) NewInvokeExpr(soot.grimp.NewInvokeExpr) DSpecialInvokeExpr(soot.dava.internal.javaRep.DSpecialInvokeExpr) SootMethodRef(soot.SootMethodRef) SpecialInvokeExpr(soot.jimple.SpecialInvokeExpr) DSpecialInvokeExpr(soot.dava.internal.javaRep.DSpecialInvokeExpr) InstanceInvokeExpr(soot.jimple.InstanceInvokeExpr) InterfaceInvokeExpr(soot.jimple.InterfaceInvokeExpr) DInterfaceInvokeExpr(soot.dava.internal.javaRep.DInterfaceInvokeExpr) DInterfaceInvokeExpr(soot.dava.internal.javaRep.DInterfaceInvokeExpr) DVirtualInvokeExpr(soot.dava.internal.javaRep.DVirtualInvokeExpr) DStaticInvokeExpr(soot.dava.internal.javaRep.DStaticInvokeExpr) StaticInvokeExpr(soot.jimple.StaticInvokeExpr) RefType(soot.RefType) DNewInvokeExpr(soot.dava.internal.javaRep.DNewInvokeExpr) DNewInvokeExpr(soot.dava.internal.javaRep.DNewInvokeExpr) InterfaceInvokeExpr(soot.jimple.InterfaceInvokeExpr) SpecialInvokeExpr(soot.jimple.SpecialInvokeExpr) DInterfaceInvokeExpr(soot.dava.internal.javaRep.DInterfaceInvokeExpr) InstanceInvokeExpr(soot.jimple.InstanceInvokeExpr) NewInvokeExpr(soot.grimp.NewInvokeExpr) DVirtualInvokeExpr(soot.dava.internal.javaRep.DVirtualInvokeExpr) VirtualInvokeExpr(soot.jimple.VirtualInvokeExpr) InvokeExpr(soot.jimple.InvokeExpr) DStaticInvokeExpr(soot.dava.internal.javaRep.DStaticInvokeExpr) StaticInvokeExpr(soot.jimple.StaticInvokeExpr) DSpecialInvokeExpr(soot.dava.internal.javaRep.DSpecialInvokeExpr) DStaticInvokeExpr(soot.dava.internal.javaRep.DStaticInvokeExpr) Value(soot.Value) IntConstant(soot.jimple.IntConstant) DIntConstant(soot.dava.internal.javaRep.DIntConstant) DVirtualInvokeExpr(soot.dava.internal.javaRep.DVirtualInvokeExpr) VirtualInvokeExpr(soot.jimple.VirtualInvokeExpr)

Example 3 with DNewInvokeExpr

use of soot.dava.internal.javaRep.DNewInvokeExpr in project soot by Sable.

the class NewStringBufferSimplification method inExprOrRefValueBox.

public void inExprOrRefValueBox(ValueBox argBox) {
    if (DEBUG)
        System.out.println("ValBox is: " + argBox.toString());
    Value tempArgValue = argBox.getValue();
    if (DEBUG)
        System.out.println("arg value is: " + tempArgValue);
    if (!(tempArgValue instanceof DVirtualInvokeExpr)) {
        if (DEBUG)
            System.out.println("Not a DVirtualInvokeExpr" + tempArgValue.getClass());
        return;
    }
    // check this is a toString for StringBuffer
    if (DEBUG)
        System.out.println("arg value is a virtual invokeExpr");
    DVirtualInvokeExpr vInvokeExpr = ((DVirtualInvokeExpr) tempArgValue);
    // need this try catch since DavaStmtHandler expr will not have a "getMethod"
    try {
        if (!(vInvokeExpr.getMethod().toString().equals("<java.lang.StringBuffer: java.lang.String toString()>")))
            return;
    } catch (Exception e) {
        return;
    }
    if (DEBUG)
        System.out.println("Ends in toString()");
    Value base = vInvokeExpr.getBase();
    List args = new ArrayList();
    while (base instanceof DVirtualInvokeExpr) {
        DVirtualInvokeExpr tempV = (DVirtualInvokeExpr) base;
        if (DEBUG)
            System.out.println("base method is " + tempV.getMethod());
        if (!tempV.getMethod().toString().startsWith("<java.lang.StringBuffer: java.lang.StringBuffer append")) {
            if (DEBUG)
                System.out.println("Found a virtual invoke which is not a append" + tempV.getMethod());
            return;
        }
        args.add(0, tempV.getArg(0));
        // System.out.println("Append: "+((DVirtualInvokeExpr)base).getArg(0) );
        // move to next base
        base = ((DVirtualInvokeExpr) base).getBase();
    }
    if (!(base instanceof DNewInvokeExpr))
        return;
    if (DEBUG)
        System.out.println("New expr is " + ((DNewInvokeExpr) base).getMethod());
    if (!((DNewInvokeExpr) base).getMethod().toString().equals("<java.lang.StringBuffer: void <init>()>"))
        return;
    /*
    	 * The arg is a new invoke expr of StringBuffer and all the appends are present in the args list
    	 */
    if (DEBUG)
        System.out.println("Found a new StringBuffer.append list in it");
    // argBox contains the new StringBuffer
    Iterator it = args.iterator();
    Value newVal = null;
    while (it.hasNext()) {
        Value temp = (Value) it.next();
        if (newVal == null)
            newVal = temp;
        else {
            // create newVal + temp
            newVal = new GAddExpr(newVal, temp);
        }
    }
    if (DEBUG)
        System.out.println("New expression for System.out.println is" + newVal);
    argBox.setValue(newVal);
}
Also used : DVirtualInvokeExpr(soot.dava.internal.javaRep.DVirtualInvokeExpr) DNewInvokeExpr(soot.dava.internal.javaRep.DNewInvokeExpr) GAddExpr(soot.grimp.internal.GAddExpr) Value(soot.Value) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList)

Example 4 with DNewInvokeExpr

use of soot.dava.internal.javaRep.DNewInvokeExpr 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)

Aggregations

DNewInvokeExpr (soot.dava.internal.javaRep.DNewInvokeExpr)4 ArrayList (java.util.ArrayList)3 RefType (soot.RefType)3 Value (soot.Value)3 Iterator (java.util.Iterator)2 List (java.util.List)2 PrimType (soot.PrimType)2 SootMethodRef (soot.SootMethodRef)2 DecompilationException (soot.dava.DecompilationException)2 DVirtualInvokeExpr (soot.dava.internal.javaRep.DVirtualInvokeExpr)2 BooleanType (soot.BooleanType)1 ByteType (soot.ByteType)1 CharType (soot.CharType)1 DoubleType (soot.DoubleType)1 FloatType (soot.FloatType)1 IntType (soot.IntType)1 Local (soot.Local)1 LongType (soot.LongType)1 ShortType (soot.ShortType)1 SootClass (soot.SootClass)1