Search in sources :

Example 1 with EliminateConditions

use of soot.dava.toolkits.base.AST.transformations.EliminateConditions 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)

Aggregations

HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 SootClass (soot.SootClass)1 SootMethod (soot.SootMethod)1 DavaBody (soot.dava.DavaBody)1 ASTMethodNode (soot.dava.internal.AST.ASTMethodNode)1 ASTNode (soot.dava.internal.AST.ASTNode)1 CPApplication (soot.dava.toolkits.base.AST.transformations.CPApplication)1 EliminateConditions (soot.dava.toolkits.base.AST.transformations.EliminateConditions)1 LocalVariableCleaner (soot.dava.toolkits.base.AST.transformations.LocalVariableCleaner)1 SimplifyConditions (soot.dava.toolkits.base.AST.transformations.SimplifyConditions)1 SimplifyExpressions (soot.dava.toolkits.base.AST.transformations.SimplifyExpressions)1 UnreachableCodeEliminator (soot.dava.toolkits.base.AST.transformations.UnreachableCodeEliminator)1 Chain (soot.util.Chain)1