use of soot.SootField in project soot by Sable.
the class FieldRenamer method internalTransform.
protected void internalTransform(String phaseName, Map<String, String> options) {
if (output) {
if (rename_fields) {
out.println("Transforming Field Names and Adding Opaque Predicates...");
} else {
out.println("Adding Opaques...");
}
}
RefType boolRef = Scene.v().getRefType(booleanClassName);
BodyBuilder.retrieveAllBodies();
BodyBuilder.retrieveAllNames();
for (SootClass sc : Scene.v().getApplicationClasses()) {
String className = sc.getName();
if (className.contains(".")) {
className = className.substring(className.lastIndexOf(".") + 1, className.length());
}
oldToNewFieldNames.put(className, className);
if (rename_fields) {
if (output) {
out.println("\tClassName: " + className);
}
// rename all the fields in the class
for (SootField f : sc.getFields()) {
int weight = soot.jbco.Main.getWeight(phaseName, f.getName());
if (weight > 0) {
renameField(className, f);
}
}
}
// skip interfaces - they can only hold final fields
if (sc.isInterface()) {
continue;
}
// add one opaq predicate for true and one for false to each class
String bool = "opPred1";
Type t = Rand.getInt() % 2 == 0 ? BooleanType.v() : boolRef;
while (oldToNewFieldNames.containsKey(bool)) {
bool += "_";
}
SootField f = Scene.v().makeSootField(bool, t, Modifier.PUBLIC | Modifier.STATIC);
renameField(className, f);
opaquePreds1ByClass.put(sc, f);
sc.addField(f);
setBooleanTo(sc, f, true);
bool = "opPred2";
t = t == BooleanType.v() ? boolRef : BooleanType.v();
while (oldToNewFieldNames.containsKey(bool)) {
bool += "_";
}
f = Scene.v().makeSootField(bool, t, Modifier.PUBLIC | Modifier.STATIC);
renameField(className, f);
opaquePreds2ByClass.put(sc, f);
sc.addField(f);
if (t == boolRef) {
setBooleanTo(sc, f, false);
}
}
buildOpaquePairings();
if (!rename_fields) {
return;
}
if (output) {
out.println("\r\tUpdating field references in bytecode");
}
for (SootClass sc : Scene.v().getApplicationClasses()) {
for (SootMethod m : sc.getMethods()) {
if (!m.isConcrete()) {
continue;
}
if (!m.hasActiveBody()) {
m.retrieveActiveBody();
}
for (Unit unit : m.getActiveBody().getUnits()) {
for (ValueBox box : unit.getUseAndDefBoxes()) {
Value value = box.getValue();
if (value instanceof FieldRef) {
FieldRef fieldRef = (FieldRef) value;
SootFieldRef sootFieldRef = fieldRef.getFieldRef();
if (sootFieldRef.declaringClass().isLibraryClass()) {
continue;
}
String oldName = sootFieldRef.name();
String fullName = sootFieldRef.declaringClass().getName() + '.' + oldName;
String newName = oldToNewFieldNames.get(oldName);
if (newName == null || namesToNotRename.contains(fullName)) {
continue;
}
if (newName.equals(oldName)) {
System.out.println("Strange.. Should not find a field with the same old and new name.");
}
sootFieldRef = Scene.v().makeFieldRef(sootFieldRef.declaringClass(), newName, sootFieldRef.type(), sootFieldRef.isStatic());
fieldRef.setFieldRef(sootFieldRef);
try {
sootFieldRef.resolve();
} catch (Exception e) {
System.err.println("********ERROR Updating " + sootFieldRef.name() + " to " + newName);
System.err.println("Fields of " + sootFieldRef.declaringClass().getName() + ": " + sootFieldRef.declaringClass().getFields());
throw new RuntimeException(e);
}
}
}
}
}
}
}
use of soot.SootField in project soot by Sable.
the class JimpleBodyBuilder method handleOuterClassThisInit.
/**
* adds this field for the outer class
*/
private void handleOuterClassThisInit(soot.SootMethod sootMethod) {
// static inner classes are different
SootField this0Field = body.getMethod().getDeclaringClass().getFieldByNameUnsafe("this$0");
if (this0Field != null) {
soot.jimple.FieldRef fieldRef = soot.jimple.Jimple.v().newInstanceFieldRef(specialThisLocal, this0Field.makeRef());
soot.jimple.AssignStmt stmt = soot.jimple.Jimple.v().newAssignStmt(fieldRef, outerClassParamLocal);
body.getUnits().add(stmt);
}
}
use of soot.SootField in project soot by Sable.
the class EvalResults method checkAliasAnalysis.
/**
* Count how many aliased base pointers appeared in all user's functions.
*/
public void checkAliasAnalysis() {
Set<IVarAbstraction> access_expr = new HashSet<IVarAbstraction>();
ArrayList<IVarAbstraction> al = new ArrayList<IVarAbstraction>();
Value[] values = new Value[2];
for (SootMethod sm : ptsProvider.getAllReachableMethods()) {
if (sm.isJavaLibraryMethod())
continue;
if (!sm.isConcrete())
continue;
if (!sm.hasActiveBody()) {
sm.retrieveActiveBody();
}
if (!ptsProvider.isValidMethod(sm))
continue;
// access_expr.clear();
for (Iterator<Unit> stmts = sm.getActiveBody().getUnits().iterator(); stmts.hasNext(); ) {
Stmt st = (Stmt) stmts.next();
if (st instanceof AssignStmt) {
AssignStmt a = (AssignStmt) st;
values[0] = a.getLeftOp();
values[1] = a.getRightOp();
for (Value v : values) {
// expression: p.f
if (v instanceof InstanceFieldRef) {
InstanceFieldRef ifr = (InstanceFieldRef) v;
final SootField field = ifr.getField();
if (!(field.getType() instanceof RefType))
continue;
LocalVarNode vn = ptsProvider.findLocalVarNode((Local) ifr.getBase());
if (vn == null)
continue;
if (ptsProvider.isExceptionPointer(vn))
continue;
IVarAbstraction pn = ptsProvider.findInternalNode(vn);
if (pn == null)
continue;
pn = pn.getRepresentative();
if (pn.hasPTResult())
access_expr.add(pn);
}
}
}
}
}
access_expr.remove(null);
al.addAll(access_expr);
access_expr = null;
// Next, we pair up all the pointers
Date begin = new Date();
int size = al.size();
for (int i = 0; i < size; ++i) {
IVarAbstraction pn = al.get(i);
VarNode n1 = (VarNode) pn.getWrappedNode();
for (int j = i + 1; j < size; ++j) {
IVarAbstraction qn = al.get(j);
VarNode n2 = (VarNode) qn.getWrappedNode();
if (pn.heap_sensitive_intersection(qn))
evalRes.n_hs_alias++;
// We directly use the SPARK points-to sets
if (n1.getP2Set().hasNonEmptyIntersection(n2.getP2Set()))
evalRes.n_hi_alias++;
}
}
evalRes.n_alias_pairs = size * (size - 1) / 2;
Date end = new Date();
ptsProvider.ps.println();
ptsProvider.ps.println("--------> Alias Pairs Evaluation <---------");
ptsProvider.ps.println("Number of pointer pairs in app code: " + evalRes.n_alias_pairs);
ptsProvider.ps.printf("Heap sensitive alias pairs (by Geom): %d, Percentage = %.3f%%\n", evalRes.n_hs_alias, (double) evalRes.n_hs_alias / evalRes.n_alias_pairs * 100);
ptsProvider.ps.printf("Heap insensitive alias pairs (by SPARK): %d, Percentage = %.3f%%\n", evalRes.n_hi_alias, (double) evalRes.n_hi_alias / evalRes.n_alias_pairs * 100);
ptsProvider.ps.printf("Using time: %dms \n", end.getTime() - begin.getTime());
ptsProvider.ps.println();
}
use of soot.SootField in project soot by Sable.
the class EvalResults method estimateHeapDefuseGraph.
/**
* Estimate the size of the def-use graph for the heap memory. The heap
* graph is estimated without context information.
*/
public void estimateHeapDefuseGraph() {
final Map<IVarAbstraction, int[]> defUseCounterForGeom = new HashMap<IVarAbstraction, int[]>();
final Map<AllocDotField, int[]> defUseCounterForSpark = new HashMap<AllocDotField, int[]>();
Date begin = new Date();
for (SootMethod sm : ptsProvider.getAllReachableMethods()) {
if (sm.isJavaLibraryMethod())
continue;
if (!sm.isConcrete())
continue;
if (!sm.hasActiveBody()) {
sm.retrieveActiveBody();
}
if (!ptsProvider.isValidMethod(sm))
continue;
// We first gather all the memory access expressions
for (Iterator<Unit> stmts = sm.getActiveBody().getUnits().iterator(); stmts.hasNext(); ) {
Stmt st = (Stmt) stmts.next();
if (!(st instanceof AssignStmt))
continue;
AssignStmt a = (AssignStmt) st;
final Value lValue = a.getLeftOp();
final Value rValue = a.getRightOp();
InstanceFieldRef ifr = null;
if (lValue instanceof InstanceFieldRef) {
// Def statement
ifr = (InstanceFieldRef) lValue;
} else if (rValue instanceof InstanceFieldRef) {
// Use statement
ifr = (InstanceFieldRef) rValue;
}
if (ifr != null) {
final SootField field = ifr.getField();
LocalVarNode vn = ptsProvider.findLocalVarNode((Local) ifr.getBase());
if (vn == null)
continue;
IVarAbstraction pn = ptsProvider.findInternalNode(vn);
if (pn == null)
continue;
pn = pn.getRepresentative();
if (!pn.hasPTResult())
continue;
// Spark
vn.getP2Set().forall(new P2SetVisitor() {
@Override
public void visit(Node n) {
IVarAbstraction padf = ptsProvider.findAndInsertInstanceField((AllocNode) n, field);
AllocDotField adf = (AllocDotField) padf.getWrappedNode();
int[] defUseUnit = defUseCounterForSpark.get(adf);
if (defUseUnit == null) {
defUseUnit = new int[2];
defUseCounterForSpark.put(adf, defUseUnit);
}
if (lValue instanceof InstanceFieldRef) {
defUseUnit[0]++;
} else {
defUseUnit[1]++;
}
}
});
// Geom
Set<AllocNode> objsSet = pn.get_all_points_to_objects();
for (AllocNode obj : objsSet) {
/*
* We will create a lot of instance fields. Because in
* points-to analysis, we concern only the reference
* type fields. But here, we concern all the fields read
* write including the primitive type fields.
*/
IVarAbstraction padf = ptsProvider.findAndInsertInstanceField(obj, field);
int[] defUseUnit = defUseCounterForGeom.get(padf);
if (defUseUnit == null) {
defUseUnit = new int[2];
defUseCounterForGeom.put(padf, defUseUnit);
}
if (lValue instanceof InstanceFieldRef) {
defUseUnit[0]++;
} else {
defUseUnit[1]++;
}
}
}
}
}
for (int[] defUseUnit : defUseCounterForSpark.values()) {
evalRes.n_spark_du_pairs += ((long) defUseUnit[0]) * defUseUnit[1];
}
for (int[] defUseUnit : defUseCounterForGeom.values()) {
evalRes.n_geom_du_pairs += ((long) defUseUnit[0]) * defUseUnit[1];
}
Date end = new Date();
ptsProvider.ps.println();
ptsProvider.ps.println("-----------> Heap Def Use Graph Evaluation <------------");
ptsProvider.ps.println("The edges in the heap def-use graph is (by Geom): " + evalRes.n_geom_du_pairs);
ptsProvider.ps.println("The edges in the heap def-use graph is (by Spark): " + evalRes.n_spark_du_pairs);
ptsProvider.ps.printf("Using time: %dms \n", end.getTime() - begin.getTime());
ptsProvider.ps.println();
}
use of soot.SootField in project soot by Sable.
the class SootUtil method hasRecursiveField.
public static boolean hasRecursiveField(SootClass sootClass) {
Chain fields = sootClass.getFields();
for (Iterator iter = fields.iterator(); iter.hasNext(); ) {
SootField sootField = (SootField) iter.next();
Type type = sootField.getType();
if (type instanceof RefType) {
RefType refType = (RefType) type;
SootClass sootClass2 = refType.getSootClass();
if (sootClass == sootClass2) {
return true;
}
}
}
return false;
}
Aggregations