Search in sources :

Example 1 with LocalVariableCleaner

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

use of soot.dava.toolkits.base.AST.transformations.LocalVariableCleaner in project soot by Sable.

the class DavaBody method applyStructuralAnalyses.

private void applyStructuralAnalyses(ASTNode AST) {
    // TESTING REACHING DEFS
    // ReachingDefs defs = new ReachingDefs(AST);
    // AST.apply(new tester(true,defs));
    // TESTING REACHING COPIES
    // ReachingCopies copies = new ReachingCopies(AST);
    // AST.apply(new tester(true,copies));
    // TESTING ASTUSESANDDEFS
    // AST.apply(new ASTUsesAndDefs(AST));
    /*
		 * Structural flow analyses.....
		 */
    // CopyPropagation.DEBUG=true;
    debug("applyStructureAnalyses", "invoking copy propagation");
    CopyPropagation prop = new CopyPropagation(AST);
    AST.apply(prop);
    debug("applyStructureAnalyses", "invoking copy propagation DONE");
    // copy propagation should be followed by LocalVariableCleaner to get max effect
    // ASTUsesAndDefs.DEBUG=true;
    debug("applyStructureAnalyses", "Local Variable Cleaner started");
    AST.apply(new LocalVariableCleaner(AST));
    debug("applyStructureAnalyses", "Local Variable Cleaner DONE");
}
Also used : LocalVariableCleaner(soot.dava.toolkits.base.AST.transformations.LocalVariableCleaner) CopyPropagation(soot.dava.toolkits.base.AST.traversals.CopyPropagation)

Aggregations

LocalVariableCleaner (soot.dava.toolkits.base.AST.transformations.LocalVariableCleaner)2 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 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 CopyPropagation (soot.dava.toolkits.base.AST.traversals.CopyPropagation)1 Chain (soot.util.Chain)1