Search in sources :

Example 1 with Chain

use of soot.util.Chain in project soot by Sable.

the class InterProceduralAnalyses method applyInterProceduralAnalyses.

/*
	 * Method is invoked by postProcessDava in PackManager
	 * if the transformations flag is true
	 * 
	 * All interproceduralAnalyses should be applied in here
	 */
public static void applyInterProceduralAnalyses() {
    Chain classes = Scene.v().getApplicationClasses();
    if (DEBUG)
        System.out.println("\n\nInvoking redundantFielduseEliminator");
    ConstantFieldValueFinder finder = new ConstantFieldValueFinder(classes);
    HashMap<String, Object> constantValueFields = finder.getFieldsWithConstantValues();
    if (DEBUG)
        finder.printConstantValueFields();
    /*
		 * The code above this gathers interprocedural information
		 * the code below this USES the interprocedural results
		 */
    Iterator it = classes.iterator();
    while (it.hasNext()) {
        // go though all the methods
        SootClass s = (SootClass) it.next();
        Iterator methodIt = s.methodIterator();
        while (methodIt.hasNext()) {
            SootMethod m = (SootMethod) methodIt.next();
            /*
				 * Adding try block to handle RuntimeException no active body found
				 */
            DavaBody body = null;
            if (m.hasActiveBody()) {
                body = (DavaBody) m.getActiveBody();
            } else {
                continue;
            }
            ASTNode AST = (ASTNode) body.getUnits().getFirst();
            if (!(AST instanceof ASTMethodNode))
                continue;
            Map options = PhaseOptions.v().getPhaseOptions("db.deobfuscate");
            boolean deobfuscate = PhaseOptions.getBoolean(options, "enabled");
            // System.out.println("force is "+force);
            if (deobfuscate) {
                if (DEBUG)
                    System.out.println("\nSTART CP Class:" + s.getName() + " Method: " + m.getName());
                CPApplication CPApp = new CPApplication((ASTMethodNode) AST, constantValueFields, finder.getClassNameFieldNameToSootFieldMapping());
                AST.apply(CPApp);
                if (DEBUG)
                    System.out.println("DONE CP for " + m.getName());
            }
            // expression simplification
            // SimplifyExpressions.DEBUG=true;
            AST.apply(new SimplifyExpressions());
            // SimplifyConditions.DEBUG=true;
            AST.apply(new SimplifyConditions());
            // condition elimination
            // EliminateConditions.DEBUG=true;
            AST.apply(new EliminateConditions((ASTMethodNode) AST));
            // the above should ALWAYS be followed by an unreachable code eliminator
            AST.apply(new UnreachableCodeEliminator(AST));
            // local variable cleanup
            AST.apply(new LocalVariableCleaner(AST));
            // VERY EXPENSIVE STAGE of redoing all analyses!!!!
            if (deobfuscate) {
                if (DEBUG)
                    System.out.println("reinvoking analyzeAST");
                UselessLabelFinder.DEBUG = false;
                body.analyzeAST();
            }
            // renaming should be applied as the last stage
            options = PhaseOptions.v().getPhaseOptions("db.renamer");
            boolean renamer = PhaseOptions.getBoolean(options, "enabled");
            // System.out.println("renaming is"+renamer);
            if (renamer) {
                applyRenamerAnalyses(AST, body);
            }
            // remove returns from void methods
            VoidReturnRemover.cleanClass(s);
        }
    }
}
Also used : Chain(soot.util.Chain) EliminateConditions(soot.dava.toolkits.base.AST.transformations.EliminateConditions) SootClass(soot.SootClass) UnreachableCodeEliminator(soot.dava.toolkits.base.AST.transformations.UnreachableCodeEliminator) DavaBody(soot.dava.DavaBody) SimplifyConditions(soot.dava.toolkits.base.AST.transformations.SimplifyConditions) Iterator(java.util.Iterator) ASTNode(soot.dava.internal.AST.ASTNode) SootMethod(soot.SootMethod) LocalVariableCleaner(soot.dava.toolkits.base.AST.transformations.LocalVariableCleaner) ASTMethodNode(soot.dava.internal.AST.ASTMethodNode) HashMap(java.util.HashMap) Map(java.util.Map) SimplifyExpressions(soot.dava.toolkits.base.AST.transformations.SimplifyExpressions) CPApplication(soot.dava.toolkits.base.AST.transformations.CPApplication)

Example 2 with Chain

use of soot.util.Chain in project soot by Sable.

the class DeInliningFinalFields method inASTMethodNode.

public void inASTMethodNode(ASTMethodNode node) {
    DavaBody davaBody = node.getDavaBody();
    sootMethod = davaBody.getMethod();
    // System.out.println("Deiniling  method: "+sootMethod.getName());
    sootClass = sootMethod.getDeclaringClass();
    finalFields = new HashMap<Comparable, SootField>();
    ArrayList fieldChain = new ArrayList();
    Chain appClasses = Scene.v().getApplicationClasses();
    Iterator it = appClasses.iterator();
    while (it.hasNext()) {
        SootClass tempClass = (SootClass) it.next();
        // System.out.println("DeInlining"+tempClass.getName());
        Chain tempChain = tempClass.getFields();
        Iterator tempIt = tempChain.iterator();
        while (tempIt.hasNext()) {
            fieldChain.add(tempIt.next());
        }
    }
    // Iterator fieldIt = sootClass.getFields().iterator();
    Iterator fieldIt = fieldChain.iterator();
    while (fieldIt.hasNext()) {
        SootField f = (SootField) fieldIt.next();
        if (f.isFinal()) {
            // check for constant value tags
            Type fieldType = f.getType();
            if (fieldType instanceof DoubleType && f.hasTag("DoubleConstantValueTag")) {
                double val = ((DoubleConstantValueTag) f.getTag("DoubleConstantValueTag")).getDoubleValue();
                finalFields.put(new Double(val), f);
            } else if (fieldType instanceof FloatType && f.hasTag("FloatConstantValueTag")) {
                float val = ((FloatConstantValueTag) f.getTag("FloatConstantValueTag")).getFloatValue();
                finalFields.put(new Float(val), f);
            } else if (fieldType instanceof LongType && f.hasTag("LongConstantValueTag")) {
                long val = ((LongConstantValueTag) f.getTag("LongConstantValueTag")).getLongValue();
                finalFields.put(new Long(val), f);
            } else if (fieldType instanceof CharType && f.hasTag("IntegerConstantValueTag")) {
                int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
                finalFields.put(new Integer(val), f);
            } else if (fieldType instanceof BooleanType && f.hasTag("IntegerConstantValueTag")) {
                int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
                if (val == 0)
                    finalFields.put(new Boolean(false), f);
                else
                    finalFields.put(new Boolean(true), f);
            } else if ((fieldType instanceof IntType || fieldType instanceof ByteType || fieldType instanceof ShortType) && f.hasTag("IntegerConstantValueTag")) {
                int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
                finalFields.put(new Integer(val), f);
            } else if (f.hasTag("StringConstantValueTag")) {
                String val = ((StringConstantValueTag) f.getTag("StringConstantValueTag")).getStringValue();
                // System.out.println("adding string constant"+val);
                finalFields.put(val, f);
            }
        }
    // end if final
    }
// going through fields
}
Also used : Chain(soot.util.Chain)

Example 3 with Chain

use of soot.util.Chain in project soot by Sable.

the class VoidReturnRemover method removeReturn.

private static void removeReturn(SootMethod method) {
    // check if this is a void method
    if (!(method.getReturnType() instanceof VoidType))
        return;
    // get the methodnode
    if (!method.hasActiveBody())
        return;
    Chain units = ((DavaBody) method.getActiveBody()).getUnits();
    if (units.size() != 1)
        return;
    ASTNode AST = (ASTNode) units.getFirst();
    if (!(AST instanceof ASTMethodNode))
        throw new RuntimeException("Starting node of DavaBody AST is not an ASTMethodNode");
    ASTMethodNode node = (ASTMethodNode) AST;
    // check there is only 1 subBody
    List<Object> subBodies = node.get_SubBodies();
    if (subBodies.size() != 1)
        return;
    List subBody = (List) subBodies.get(0);
    // see if the last of this is a stmtseq node
    if (subBody.size() == 0) {
        // nothing inside subBody
        return;
    }
    // check last node is a ASTStatementSequenceNode
    ASTNode last = (ASTNode) subBody.get(subBody.size() - 1);
    if (!(last instanceof ASTStatementSequenceNode))
        return;
    // get last statement
    List<AugmentedStmt> stmts = ((ASTStatementSequenceNode) last).getStatements();
    if (stmts.size() == 0) {
        // no stmts inside statement sequence node
        subBody.remove(subBody.size() - 1);
        return;
    }
    AugmentedStmt lastas = (AugmentedStmt) stmts.get(stmts.size() - 1);
    Stmt lastStmt = lastas.get_Stmt();
    if (!(lastStmt instanceof ReturnVoidStmt))
        return;
    // we can remove the lastStmt
    stmts.remove(stmts.size() - 1);
    /*
	     we need to check if we have made the size 0 in
	     which case the stmtSeq Node should also be removed
	     */
    if (stmts.size() == 0)
        subBody.remove(subBody.size() - 1);
}
Also used : Chain(soot.util.Chain)

Example 4 with Chain

use of soot.util.Chain 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 5 with Chain

use of soot.util.Chain in project soot by Sable.

the class ConstructorFolder method internalTransform.

/**
 * This method change all new Obj/<init>(args) pairs to new Obj(args) idioms.
 */
protected void internalTransform(Body b, String phaseName, Map options) {
    GrimpBody body = (GrimpBody) b;
    if (Options.v().verbose())
        logger.debug("[" + body.getMethod().getName() + "] Folding constructors...");
    Chain units = body.getUnits();
    List<Unit> stmtList = new ArrayList<Unit>();
    stmtList.addAll(units);
    Iterator<Unit> it = stmtList.iterator();
    LocalUses localUses = LocalUses.Factory.newLocalUses(b);
    /* fold in NewExpr's with specialinvoke's */
    while (it.hasNext()) {
        Stmt s = (Stmt) it.next();
        if (!(s instanceof AssignStmt))
            continue;
        /* this should be generalized to ArrayRefs */
        Value lhs = ((AssignStmt) s).getLeftOp();
        if (!(lhs instanceof Local))
            continue;
        Value rhs = ((AssignStmt) s).getRightOp();
        if (!(rhs instanceof NewExpr))
            continue;
        /* TO BE IMPLEMENTED LATER: move any copy of the object reference
             for lhs down beyond the NewInvokeExpr, with the rationale
             being that you can't modify the object before the constructor
             call in any case.

             Also, do note that any new's (object creation) without
             corresponding constructors must be dead. */
        List lu = localUses.getUsesOf(s);
        Iterator luIter = lu.iterator();
        boolean MadeNewInvokeExpr = false;
        while (luIter.hasNext()) {
            Unit use = ((UnitValueBoxPair) (luIter.next())).unit;
            if (!(use instanceof InvokeStmt))
                continue;
            InvokeStmt is = (InvokeStmt) use;
            if (!(is.getInvokeExpr() instanceof SpecialInvokeExpr) || lhs != ((SpecialInvokeExpr) is.getInvokeExpr()).getBase())
                continue;
            SpecialInvokeExpr oldInvoke = ((SpecialInvokeExpr) is.getInvokeExpr());
            LinkedList invokeArgs = new LinkedList();
            for (int i = 0; i < oldInvoke.getArgCount(); i++) invokeArgs.add(oldInvoke.getArg(i));
            AssignStmt constructStmt = Grimp.v().newAssignStmt((AssignStmt) s);
            constructStmt.setRightOp(Grimp.v().newNewInvokeExpr(((NewExpr) rhs).getBaseType(), oldInvoke.getMethodRef(), invokeArgs));
            MadeNewInvokeExpr = true;
            use.redirectJumpsToThisTo(constructStmt);
            units.insertBefore(constructStmt, use);
            units.remove(use);
        }
        if (MadeNewInvokeExpr) {
            units.remove(s);
        }
    }
}
Also used : Chain(soot.util.Chain) InvokeStmt(soot.jimple.InvokeStmt) AssignStmt(soot.jimple.AssignStmt) SpecialInvokeExpr(soot.jimple.SpecialInvokeExpr) ArrayList(java.util.ArrayList) Local(soot.Local) SimpleLocalUses(soot.toolkits.scalar.SimpleLocalUses) LocalUses(soot.toolkits.scalar.LocalUses) Unit(soot.Unit) LinkedList(java.util.LinkedList) InvokeStmt(soot.jimple.InvokeStmt) Stmt(soot.jimple.Stmt) AssignStmt(soot.jimple.AssignStmt) Value(soot.Value) NewExpr(soot.jimple.NewExpr) Iterator(java.util.Iterator) UnitValueBoxPair(soot.toolkits.scalar.UnitValueBoxPair) GrimpBody(soot.grimp.GrimpBody) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Aggregations

Chain (soot.util.Chain)15 Iterator (java.util.Iterator)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Local (soot.Local)7 SootMethod (soot.SootMethod)6 Value (soot.Value)6 Body (soot.Body)5 SootClass (soot.SootClass)5 Unit (soot.Unit)5 HashMap (java.util.HashMap)4 LinkedList (java.util.LinkedList)4 Type (soot.Type)4 IfStmt (soot.jimple.IfStmt)4 Map (java.util.Map)3 ValueBox (soot.ValueBox)3 ASTMethodNode (soot.dava.internal.AST.ASTMethodNode)3 ASTNode (soot.dava.internal.AST.ASTNode)3 JimpleBody (soot.jimple.JimpleBody)3 Stmt (soot.jimple.Stmt)3