Search in sources :

Example 1 with ASTNode

use of soot.dava.internal.AST.ASTNode in project soot by Sable.

the class MethodCallFinder method createChangedBodyPart.

/*
	 * Given an invoke stmt this method finds the parent of this stmt which
	 * should always be a StatementSequenceNode Then the sequence is broken into
	 * three parts. The first part contains stmts till above the invoke stmt.
	 * The second part contains the body argument which is the body of the
	 * inlined method and the third part are the stmts below the invoke stmt
	 */
public List<ASTStatementSequenceNode> createChangedBodyPart(InvokeStmt s, List body, ASTParentNodeFinder finder) {
    // get parent node of invoke stmt
    Object parent = finder.getParentOf(s);
    if (parent == null) {
        throw new RuntimeException("MethodCall FInder: parent of invoke stmt not found");
    }
    ASTNode parentNode = (ASTNode) parent;
    if (!(parentNode instanceof ASTStatementSequenceNode)) {
        throw new RuntimeException("MethodCall FInder: parent node not a stmt seq node");
    }
    ASTStatementSequenceNode orignal = (ASTStatementSequenceNode) parentNode;
    // copying the stmts till above the inoke stmt into one stmt sequence
    // node
    List<AugmentedStmt> newInitialNode = new ArrayList<AugmentedStmt>();
    Iterator<AugmentedStmt> it = orignal.getStatements().iterator();
    while (it.hasNext()) {
        AugmentedStmt as = it.next();
        Stmt tempStmt = as.get_Stmt();
        if (tempStmt != s) {
            newInitialNode.add(as);
        } else {
            // stmt we break
            break;
        }
    }
    // copy remaining stmts into the AFTER stmt sequence node
    List<AugmentedStmt> newSecondNode = new ArrayList<AugmentedStmt>();
    while (it.hasNext()) {
        newSecondNode.add(it.next());
    }
    List<ASTStatementSequenceNode> toReturn = new ArrayList<ASTStatementSequenceNode>();
    if (newInitialNode.size() != 0)
        toReturn.add(new ASTStatementSequenceNode(newInitialNode));
    // add inline methods body
    toReturn.addAll(body);
    if (newSecondNode.size() != 0)
        toReturn.add(new ASTStatementSequenceNode(newSecondNode));
    return toReturn;
}
Also used : ASTNode(soot.dava.internal.AST.ASTNode) ArrayList(java.util.ArrayList) ASTStatementSequenceNode(soot.dava.internal.AST.ASTStatementSequenceNode) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) InvokeStmt(soot.jimple.InvokeStmt) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) Stmt(soot.jimple.Stmt) GThrowStmt(soot.grimp.internal.GThrowStmt)

Example 2 with ASTNode

use of soot.dava.internal.AST.ASTNode in project soot by Sable.

the class ConstantFieldValueFinder method computeFieldToValuesAssignedList.

/*
	 * Go through all the methods in the application and make a mapping of className+methodName ---> values assigned
	 * There can obviously be more than one value assigned to each field
	 */
private void computeFieldToValuesAssignedList() {
    // go through all the classes
    Iterator classIt = appClasses.iterator();
    while (classIt.hasNext()) {
        SootClass s = (SootClass) classIt.next();
        debug("\ncomputeMethodSummaries", "Processing class " + s.getName());
        // go though all the methods
        Iterator methodIt = s.methodIterator();
        while (methodIt.hasNext()) {
            SootMethod m = (SootMethod) methodIt.next();
            DavaBody body = null;
            if (m.hasActiveBody()) {
                /*
					 * Added to try to fix the no active body found exception
					 */
                body = (DavaBody) m.getActiveBody();
            } else {
                continue;
            }
            ASTNode AST = (ASTNode) body.getUnits().getFirst();
            // find all definitions in the program
            AllDefinitionsFinder defFinder = new AllDefinitionsFinder();
            AST.apply(defFinder);
            Iterator<DefinitionStmt> allDefIt = defFinder.getAllDefs().iterator();
            // go through each definition
            while (allDefIt.hasNext()) {
                DefinitionStmt stmt = allDefIt.next();
                // debug("DefinitionStmt")
                Value left = stmt.getLeftOp();
                /*
					 * Only care if we have fieldRef on the left
					 */
                if (!(left instanceof FieldRef)) {
                    continue;
                }
                // we know definition is to a field
                debug("computeMethodSummaries method: " + m.getName(), "Field ref is: " + left);
                // Information we want to store is class of field and name of field and the right op
                FieldRef ref = (FieldRef) left;
                SootField field = ref.getField();
                /*
					 * Only care about fields with primtype
					 */
                if (!(field.getType() instanceof PrimType))
                    continue;
                String fieldName = field.getName();
                String declaringClass = field.getDeclaringClass().getName();
                debug("\tField Name: " + fieldName);
                debug("\tField DeclaringClass: " + declaringClass);
                // get the valueList for this class+field combo
                String combined = declaringClass + combiner + fieldName;
                Object temp = fieldToValues.get(combined);
                ArrayList valueList;
                if (temp == null) {
                    // no value of this field was yet assigned
                    valueList = new ArrayList();
                    fieldToValues.put(combined, valueList);
                } else {
                    valueList = (ArrayList) temp;
                }
                valueList.add(stmt.getRightOp());
            }
        // going through all the definitions
        }
    // going through methods of class s
    }
// going through  classes
}
Also used : AllDefinitionsFinder(soot.dava.toolkits.base.AST.traversals.AllDefinitionsFinder) FieldRef(soot.jimple.FieldRef) ArrayList(java.util.ArrayList) SootClass(soot.SootClass) DavaBody(soot.dava.DavaBody) Iterator(java.util.Iterator) ASTNode(soot.dava.internal.AST.ASTNode) Value(soot.Value) SootMethod(soot.SootMethod) PrimType(soot.PrimType) SootField(soot.SootField) DefinitionStmt(soot.jimple.DefinitionStmt)

Example 3 with ASTNode

use of soot.dava.internal.AST.ASTNode in project soot by Sable.

the class StructuredAnalysis method process.

/*
	 * The parameter body contains the body to be analysed It can be an ASTNode,
	 * a Stmt, an augmentedStmt or a list of ASTNodes The input is any data that
	 * is gathered plus any info needed for making decisions during the analysis
	 */
public DavaFlowSet<E> process(Object body, DavaFlowSet<E> input) {
    if (body instanceof ASTNode) {
        beforeSets.put(body, input);
        DavaFlowSet<E> temp = processASTNode((ASTNode) body, input);
        afterSets.put(body, temp);
        return temp;
    } else if (body instanceof Stmt) {
        beforeSets.put(body, input);
        DavaFlowSet<E> result = processAbruptStatements((Stmt) body, input);
        afterSets.put(body, result);
        return result;
    } else if (body instanceof AugmentedStmt) {
        AugmentedStmt as = (AugmentedStmt) body;
        Stmt s = as.get_Stmt();
        beforeSets.put(s, input);
        DavaFlowSet<E> result = processAbruptStatements(s, input);
        afterSets.put(s, result);
        return result;
    } else if (body instanceof List) {
        // this should always be a list of ASTNodes
        Iterator it = ((List) body).iterator();
        DavaFlowSet<E> result = input;
        while (it.hasNext()) {
            Object temp = it.next();
            if (!(temp instanceof ASTNode))
                throw new RuntimeException("Body sent to be processed by " + "StructuredAnalysis contains a list which does not have ASTNodes");
            else {
                /*
					 * As we are simply going through a list of ASTNodes The
					 * output of the previous becomes the input of the next
					 */
                beforeSets.put(temp, result);
                result = processASTNode((ASTNode) temp, result);
                afterSets.put(temp, result);
            }
        }
        // the List
        return result;
    } else {
        throw new RuntimeException("Body sent to be processed by " + "StructuredAnalysis is not a valid body");
    }
}
Also used : ASTNode(soot.dava.internal.AST.ASTNode) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) ReturnVoidStmt(soot.jimple.ReturnVoidStmt) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) ReturnStmt(soot.jimple.ReturnStmt) DAbruptStmt(soot.dava.internal.javaRep.DAbruptStmt) Stmt(soot.jimple.Stmt) RetStmt(soot.jimple.RetStmt)

Example 4 with ASTNode

use of soot.dava.internal.AST.ASTNode in project soot by Sable.

the class IfElseSplitter method bodyTargetsLabel.

/*
	 * Check that label is non null and the string inside is non null... if yes return false
	 * Check that the given list (sequeneof ASTNodes have no abrupt edge targeting the label.
	 *
	 */
public boolean bodyTargetsLabel(SETNodeLabel label, List<Object> body) {
    // no SETNodeLabel is good
    if (label == null)
        return false;
    // SETNodeLabel but with no string is also good
    if (label.toString() == null)
        return false;
    final String strLabel = label.toString();
    // go through the body use traversal to find whether there is an abrupt stmt targeting this
    Iterator<Object> it = body.iterator();
    targeted = false;
    while (it.hasNext()) {
        ASTNode temp = (ASTNode) it.next();
        temp.apply(new DepthFirstAdapter() {

            // set targeted to true if DAbruptStmt targets it
            public void inStmt(Stmt s) {
                // only interested in abrupt stmts
                if (!(s instanceof DAbruptStmt))
                    return;
                DAbruptStmt abrupt = (DAbruptStmt) s;
                SETNodeLabel label = abrupt.getLabel();
                if (label != null && label.toString() != null && label.toString().equals(strLabel)) {
                    targeted = true;
                }
            }
        });
        if (targeted)
            break;
    }
    return targeted;
}
Also used : DepthFirstAdapter(soot.dava.toolkits.base.AST.analysis.DepthFirstAdapter) SETNodeLabel(soot.dava.internal.SET.SETNodeLabel) ASTNode(soot.dava.internal.AST.ASTNode) DAbruptStmt(soot.dava.internal.javaRep.DAbruptStmt) ReturnStmt(soot.jimple.ReturnStmt) DAbruptStmt(soot.dava.internal.javaRep.DAbruptStmt) Stmt(soot.jimple.Stmt) ReturnVoidStmt(soot.jimple.ReturnVoidStmt) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt)

Example 5 with ASTNode

use of soot.dava.internal.AST.ASTNode 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

ASTNode (soot.dava.internal.AST.ASTNode)29 ArrayList (java.util.ArrayList)14 List (java.util.List)13 Iterator (java.util.Iterator)12 AugmentedStmt (soot.dava.internal.asg.AugmentedStmt)9 Stmt (soot.jimple.Stmt)9 ASTMethodNode (soot.dava.internal.AST.ASTMethodNode)7 ASTStatementSequenceNode (soot.dava.internal.AST.ASTStatementSequenceNode)7 DefinitionStmt (soot.jimple.DefinitionStmt)6 SootClass (soot.SootClass)4 SootMethod (soot.SootMethod)4 ASTControlFlowNode (soot.dava.internal.AST.ASTControlFlowNode)4 ASTSwitchNode (soot.dava.internal.AST.ASTSwitchNode)4 DVariableDeclarationStmt (soot.dava.internal.javaRep.DVariableDeclarationStmt)4 Body (soot.Body)3 Value (soot.Value)3 DavaBody (soot.dava.DavaBody)3 DecompilationException (soot.dava.DecompilationException)3 ASTTryNode (soot.dava.internal.AST.ASTTryNode)3 GAssignStmt (soot.grimp.internal.GAssignStmt)3