Search in sources :

Example 1 with DVirtualInvokeExpr

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

the class SuperFirstStmtHandler method getProperCasting.

public Value getProperCasting(Type tempType, DVirtualInvokeExpr tempInvokeExpr) {
    if (tempType instanceof RefType) {
        // System.out.println("This is a reftype:"+tempType);
        return new GCastExpr(tempInvokeExpr, tempType);
    } else if (tempType instanceof PrimType) {
        PrimType t = (PrimType) tempType;
        if (t == BooleanType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Boolean"));
            // booleanValue
            SootMethod tempMethod = Scene.v().makeSootMethod("booleanValue", new ArrayList(), BooleanType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Boolean"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == ByteType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Byte"));
            // byteValue
            SootMethod tempMethod = Scene.v().makeSootMethod("byteValue", new ArrayList(), ByteType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Byte"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == CharType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Character"));
            // charValue
            SootMethod tempMethod = Scene.v().makeSootMethod("charValue", new ArrayList(), CharType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Character"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == DoubleType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Double"));
            // doubleValue
            SootMethod tempMethod = Scene.v().makeSootMethod("doubleValue", new ArrayList(), DoubleType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Double"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == FloatType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Float"));
            // floatValue
            SootMethod tempMethod = Scene.v().makeSootMethod("floatValue", new ArrayList(), FloatType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Float"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == IntType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Integer"));
            // intValue
            SootMethod tempMethod = Scene.v().makeSootMethod("intValue", new ArrayList(), IntType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Integer"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == LongType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Long"));
            // longValue
            SootMethod tempMethod = Scene.v().makeSootMethod("longValue", new ArrayList(), LongType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Long"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == ShortType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Short"));
            // shortValue
            SootMethod tempMethod = Scene.v().makeSootMethod("shortValue", new ArrayList(), ShortType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Short"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else {
            throw new DecompilationException("Unhandle primType:" + tempType);
        }
    } else {
        throw new DecompilationException("The type:" + tempType + " was not a reftye or primtype. PLEASE REPORT.");
    }
}
Also used : SootMethodRef(soot.SootMethodRef) ArrayList(java.util.ArrayList) DecompilationException(soot.dava.DecompilationException) SootClass(soot.SootClass) RefType(soot.RefType) DVirtualInvokeExpr(soot.dava.internal.javaRep.DVirtualInvokeExpr) Value(soot.Value) PrimType(soot.PrimType) SootMethod(soot.SootMethod) GCastExpr(soot.grimp.internal.GCastExpr) HashSet(java.util.HashSet)

Example 2 with DVirtualInvokeExpr

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

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

use of soot.dava.internal.javaRep.DVirtualInvokeExpr 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 5 with DVirtualInvokeExpr

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

the class SuperFirstStmtHandler method createAugmentedStmtToAdd.

private AugmentedStmt createAugmentedStmtToAdd(Local newLocal, SootMethodRef getMethodRef, Value tempVal) {
    ArrayList tempArgList = new ArrayList();
    tempArgList.add(tempVal);
    DVirtualInvokeExpr tempInvokeExpr = new DVirtualInvokeExpr(newLocal, getMethodRef, tempArgList, new HashSet<Object>());
    // create Invoke Stmt with virtualInvoke as the expression
    GInvokeStmt s = new GInvokeStmt(tempInvokeExpr);
    return new AugmentedStmt(s);
}
Also used : DVirtualInvokeExpr(soot.dava.internal.javaRep.DVirtualInvokeExpr) GInvokeStmt(soot.grimp.internal.GInvokeStmt) ArrayList(java.util.ArrayList) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt)

Aggregations

DVirtualInvokeExpr (soot.dava.internal.javaRep.DVirtualInvokeExpr)7 ArrayList (java.util.ArrayList)6 Value (soot.Value)5 RefType (soot.RefType)4 SootMethodRef (soot.SootMethodRef)4 GInvokeStmt (soot.grimp.internal.GInvokeStmt)4 List (java.util.List)3 PrimType (soot.PrimType)3 SootClass (soot.SootClass)3 DecompilationException (soot.dava.DecompilationException)3 AugmentedStmt (soot.dava.internal.asg.AugmentedStmt)3 DIntConstant (soot.dava.internal.javaRep.DIntConstant)3 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 BooleanType (soot.BooleanType)2 ByteType (soot.ByteType)2 CharType (soot.CharType)2 DoubleType (soot.DoubleType)2 FloatType (soot.FloatType)2 IntType (soot.IntType)2