use of soot.dava.toolkits.base.AST.traversals.AllDefinitionsFinder 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
}
use of soot.dava.toolkits.base.AST.traversals.AllDefinitionsFinder in project soot by Sable.
the class SuperFirstStmtHandler method addDefsToLiveVariables.
/*
* newASTPreInitMethod at time of invocation just contains body X find all
* defs for this body
*/
private List<Local> addDefsToLiveVariables() {
// get all defs within x
AllDefinitionsFinder finder = new AllDefinitionsFinder();
newASTPreInitMethod.apply(finder);
List<DefinitionStmt> allDefs = finder.getAllDefs();
List<Local> uniqueLocals = new ArrayList<Local>();
List<DefinitionStmt> uniqueLocalDefs = new ArrayList<DefinitionStmt>();
// remove any defs for fields, and any which are done multiple times
Iterator<DefinitionStmt> it = allDefs.iterator();
while (it.hasNext()) {
DefinitionStmt s = it.next();
Value left = s.getLeftOp();
if (left instanceof Local) {
if (uniqueLocals.contains(left)) {
// a def for this local already encountered
int index = uniqueLocals.indexOf(left);
uniqueLocals.remove(index);
uniqueLocalDefs.remove(index);
} else {
// no def for this local yet
uniqueLocals.add((Local) left);
uniqueLocalDefs.add(s);
}
}
}
// at this point unique locals contains all locals defined and
// uniqueLocaldef list has a list of the corresponding definitions
// Now remove those unique locals and localdefs whose stmtseq node does
// not have the ASTMEthodNode as a parent
// This is a conservative step!!
ASTParentNodeFinder parentFinder = new ASTParentNodeFinder();
newASTPreInitMethod.apply(parentFinder);
List<DefinitionStmt> toRemoveDefs = new ArrayList<DefinitionStmt>();
it = uniqueLocalDefs.iterator();
while (it.hasNext()) {
DefinitionStmt s = it.next();
Object parent = parentFinder.getParentOf(s);
if (parent == null || (!(parent instanceof ASTStatementSequenceNode))) {
// shouldnt happen but if it does add this s to toRemove list
toRemoveDefs.add(s);
}
// parent is an ASTStatementsequence node. check that its parent is
// the ASTMethodNode
Object grandParent = parentFinder.getParentOf(parent);
if (grandParent == null || (!(grandParent instanceof ASTMethodNode))) {
// can happen if obfuscators are really smart. add s to toRemove
// list
toRemoveDefs.add(s);
}
}
// remove any defs and corresponding locals if present in the
// toRemoveDefs list
it = toRemoveDefs.iterator();
while (it.hasNext()) {
DefinitionStmt s = it.next();
int index = uniqueLocalDefs.indexOf(s);
uniqueLocals.remove(index);
uniqueLocalDefs.remove(index);
}
// the uniqueLocalDefs contains all those definitions to unique locals
// which are not deeply nested in the X body
// find all the uses of these definitions in the original method body
toRemoveDefs = new ArrayList<DefinitionStmt>();
ASTUsesAndDefs uDdU = new ASTUsesAndDefs(originalASTMethod);
originalASTMethod.apply(uDdU);
it = uniqueLocalDefs.iterator();
while (it.hasNext()) {
DefinitionStmt s = it.next();
Object temp = uDdU.getDUChain(s);
if (temp == null) {
// couldnt find uses
toRemoveDefs.add(s);
continue;
}
ArrayList uses = (ArrayList) temp;
// check if uses is non-empty
if (uses.size() == 0) {
toRemoveDefs.add(s);
}
// check for all the non zero uses
Iterator useIt = uses.iterator();
boolean onlyInConstructorUnit = true;
while (useIt.hasNext()) {
// a use is either a statement or a node(condition, synch,
// switch , for etc)
Object tempUse = useIt.next();
if (tempUse != originalConstructorUnit) {
onlyInConstructorUnit = false;
}
}
if (onlyInConstructorUnit) {
// mark it to be removed
toRemoveDefs.add(s);
}
}
// remove any defs and corresponding locals if present in the
// toRemoveDefs list
it = toRemoveDefs.iterator();
while (it.hasNext()) {
DefinitionStmt s = it.next();
int index = uniqueLocalDefs.indexOf(s);
uniqueLocals.remove(index);
uniqueLocalDefs.remove(index);
}
// the remaining uniquelocals are the ones which are needed for body Y
return uniqueLocals;
}
use of soot.dava.toolkits.base.AST.traversals.AllDefinitionsFinder in project soot by Sable.
the class ReachingDefs method newInitialFlow.
/*
* Initial flow into catch statements is empty meaning no definition reaches
*/
@Override
public DavaFlowSet<Stmt> newInitialFlow() {
DavaFlowSet<Stmt> initial = new DavaFlowSet<Stmt>();
// find all definitions in the program
AllDefinitionsFinder defFinder = new AllDefinitionsFinder();
((ASTNode) toAnalyze).apply(defFinder);
List<DefinitionStmt> allDefs = defFinder.getAllDefs();
// DefinitionStmts
for (DefinitionStmt def : allDefs) initial.add(def);
// initial is not the universal set of all definitions
return initial;
}
Aggregations