Search in sources :

Example 11 with DecompilationException

use of soot.dava.DecompilationException in project soot by Sable.

the class SuperFirstStmtHandler method createCallToSuper.

/*
	 * super( (CAST-TYPE)handler.get(0),
	 * ((CAST-TYPE)handler.get(1)).CONVERSION(), .......);
	 * 
	 * returns false if we cant create the call to super
	 */
private boolean createCallToSuper() {
    // check that whether this call is even to be made or not
    if (originalConstructorExpr == null) {
        // System.out.println("originalConstructorExpr is null");
        return false;
    }
    // System.out.println("ConstructorExpr is non null...call to super has
    // to be made");
    // find the parent class of the current method being decompiled
    SootClass parentClass = originalSootClass.getSuperclass();
    // super method
    if (!(parentClass.declaresMethod("<init>", argsTwoTypes))) {
        // this name and ParamTypes");
        return false;
    }
    SootMethod superConstructor = parentClass.getMethod("<init>", argsTwoTypes);
    // create InstanceInvokeExpr
    // need Value base: this??
    // need SootMethod Ref...make sootmethoref of the super constructor
    // found
    // need list of thisLocals.........try empty arrayList since it doesnt
    // seem to be used anywhere??
    List argsForConstructor = new ArrayList();
    int count = 0;
    // have to make arg as "handler.get(0)"
    // 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);
    List tempArgList = null;
    Iterator typeIt = argsTwoTypes.iterator();
    while (typeIt.hasNext()) {
        Type tempType = (Type) typeIt.next();
        // takes care
        DIntConstant arg = DIntConstant.v(count, IntType.v());
        // of the
        // index
        count++;
        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);
        // the above virtualInvokeExpr is one of the args for the
        // constructor
        argsForConstructor.add(toAddExpr);
    }
    mustInitializeIndex = count;
    // we are done with creating all necessary args to the virtualinvoke
    // expr constructor
    DVirtualInvokeExpr virtualInvoke = new DVirtualInvokeExpr(originalConstructorExpr.getBase(), superConstructor.makeRef(), argsForConstructor, new HashSet<Object>());
    // set the constructors constructorExpr
    newConstructorDavaBody.set_ConstructorExpr(virtualInvoke);
    // create Invoke Stmt with virtualInvoke as the expression
    GInvokeStmt s = new GInvokeStmt(virtualInvoke);
    newConstructorDavaBody.set_ConstructorUnit(s);
    // return true if super call created
    return true;
}
Also used : SootMethodRef(soot.SootMethodRef) ArrayList(java.util.ArrayList) JimpleLocal(soot.jimple.internal.JimpleLocal) Local(soot.Local) DecompilationException(soot.dava.DecompilationException) SootClass(soot.SootClass) JimpleLocal(soot.jimple.internal.JimpleLocal) RefType(soot.RefType) DVirtualInvokeExpr(soot.dava.internal.javaRep.DVirtualInvokeExpr) 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) GInvokeStmt(soot.grimp.internal.GInvokeStmt) Iterator(java.util.Iterator) Value(soot.Value) SootMethod(soot.SootMethod) List(java.util.List) ArrayList(java.util.ArrayList)

Example 12 with DecompilationException

use of soot.dava.DecompilationException 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)

Aggregations

DecompilationException (soot.dava.DecompilationException)12 ArrayList (java.util.ArrayList)8 List (java.util.List)6 PrimType (soot.PrimType)6 RefType (soot.RefType)5 SootClass (soot.SootClass)5 Value (soot.Value)5 Iterator (java.util.Iterator)4 BooleanType (soot.BooleanType)4 ByteType (soot.ByteType)4 CharType (soot.CharType)4 DoubleType (soot.DoubleType)4 FloatType (soot.FloatType)4 IntType (soot.IntType)4 LongType (soot.LongType)4 ShortType (soot.ShortType)4 SootMethod (soot.SootMethod)4 SootMethodRef (soot.SootMethodRef)4 Type (soot.Type)4 AugmentedStmt (soot.dava.internal.asg.AugmentedStmt)4