use of soot.Unit in project soot by Sable.
the class ConstantValueToInitializerTransformer method getOrCreateInitializer.
private SootMethod getOrCreateInitializer(SootClass sc, Set<SootField> alreadyInitialized) {
SootMethod smInit;
// Create a static initializer if we don't already have one
smInit = sc.getMethodByNameUnsafe("<clinit>");
if (smInit == null) {
smInit = Scene.v().makeSootMethod("<clinit>", Collections.<Type>emptyList(), VoidType.v());
smInit.setActiveBody(Jimple.v().newBody(smInit));
sc.addMethod(smInit);
smInit.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
} else if (smInit.isPhantom())
return null;
else {
smInit.retrieveActiveBody();
// somewhere
for (Unit u : smInit.getActiveBody().getUnits()) {
Stmt s = (Stmt) u;
for (ValueBox vb : s.getDefBoxes()) if (vb.getValue() instanceof FieldRef)
alreadyInitialized.add(((FieldRef) vb.getValue()).getField());
}
}
return smInit;
}
use of soot.Unit in project soot by Sable.
the class ConstantValueToInitializerTransformer method transformClass.
public void transformClass(SootClass sc) {
SootMethod smInit = null;
Set<SootField> alreadyInitialized = new HashSet<SootField>();
for (SootField sf : sc.getFields()) {
// different constructors might assign different values.
if (!sf.isStatic() || !sf.isFinal())
continue;
// generate a second one
if (alreadyInitialized.contains(sf))
continue;
// Look for constant values
for (Tag t : sf.getTags()) {
Stmt initStmt = null;
if (t instanceof DoubleConstantValueTag) {
double value = ((DoubleConstantValueTag) t).getDoubleValue();
initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), DoubleConstant.v(value));
} else if (t instanceof FloatConstantValueTag) {
float value = ((FloatConstantValueTag) t).getFloatValue();
initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), FloatConstant.v(value));
} else if (t instanceof IntegerConstantValueTag) {
int value = ((IntegerConstantValueTag) t).getIntValue();
initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), IntConstant.v(value));
} else if (t instanceof LongConstantValueTag) {
long value = ((LongConstantValueTag) t).getLongValue();
initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), LongConstant.v(value));
} else if (t instanceof StringConstantValueTag) {
String value = ((StringConstantValueTag) t).getStringValue();
initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), StringConstant.v(value));
}
if (initStmt != null) {
if (smInit == null)
smInit = getOrCreateInitializer(sc, alreadyInitialized);
if (smInit != null)
smInit.getActiveBody().getUnits().addFirst(initStmt);
}
}
}
if (smInit != null) {
Chain<Unit> units = smInit.getActiveBody().getUnits();
if (units.isEmpty() || !(units.getLast() instanceof ReturnVoidStmt))
units.add(Jimple.v().newReturnVoidStmt());
}
}
use of soot.Unit in project soot by Sable.
the class BadFields method handleMethod.
private void handleMethod(SootMethod m) {
if (!m.isConcrete())
return;
for (Iterator<ValueBox> bIt = m.retrieveActiveBody().getUseAndDefBoxes().iterator(); bIt.hasNext(); ) {
final ValueBox b = bIt.next();
Value v = b.getValue();
if (!(v instanceof StaticFieldRef))
continue;
StaticFieldRef sfr = (StaticFieldRef) v;
SootField f = sfr.getField();
if (!f.getDeclaringClass().getName().equals("java.lang.System"))
continue;
if (f.getName().equals("err")) {
logger.debug("" + "Use of System.err in " + m);
}
if (f.getName().equals("out")) {
logger.debug("" + "Use of System.out in " + m);
}
}
for (Iterator<Unit> sIt = m.getActiveBody().getUnits().iterator(); sIt.hasNext(); ) {
final Stmt s = (Stmt) sIt.next();
if (!s.containsInvokeExpr())
continue;
InvokeExpr ie = s.getInvokeExpr();
SootMethod target = ie.getMethod();
if (target.getDeclaringClass().getName().equals("java.lang.System") && target.getName().equals("exit")) {
warn("" + m + " calls System.exit");
}
}
if (m.getName().equals("<clinit>")) {
for (Iterator<Unit> sIt = m.getActiveBody().getUnits().iterator(); sIt.hasNext(); ) {
final Stmt s = (Stmt) sIt.next();
for (Iterator<ValueBox> bIt = s.getUseBoxes().iterator(); bIt.hasNext(); ) {
final ValueBox b = bIt.next();
Value v = b.getValue();
if (v instanceof FieldRef) {
warn(m.getName() + " reads field " + v);
}
}
if (!s.containsInvokeExpr())
continue;
InvokeExpr ie = s.getInvokeExpr();
SootMethod target = ie.getMethod();
calls(target);
}
}
}
use of soot.Unit in project soot by Sable.
the class UnitGraph method buildUnexceptionalEdges.
/**
* Utility method for <tt>UnitGraph</tt> constructors. It computes the edges
* corresponding to unexceptional control flow.
*
* @param unitToSuccs
* A {@link Map} from {@link Unit}s to {@link List}s of
* {@link Unit}s. This is an ``out parameter''; callers must pass
* an empty {@link Map}. <tt>buildUnexceptionalEdges</tt> will
* add a mapping for every <tt>Unit</tt> in the body to a list of
* its unexceptional successors.
*
* @param unitToPreds
* A {@link Map} from {@link Unit}s to {@link List}s of
* {@link Unit}s. This is an ``out parameter''; callers must pass
* an empty {@link Map}. <tt>buildUnexceptionalEdges</tt> will
* add a mapping for every <tt>Unit</tt> in the body to a list of
* its unexceptional predecessors.
*/
protected void buildUnexceptionalEdges(Map<Unit, List<Unit>> unitToSuccs, Map<Unit, List<Unit>> unitToPreds) {
Iterator<Unit> unitIt = unitChain.iterator();
Unit currentUnit, nextUnit;
nextUnit = unitIt.hasNext() ? (Unit) unitIt.next() : null;
while (nextUnit != null) {
currentUnit = nextUnit;
nextUnit = unitIt.hasNext() ? (Unit) unitIt.next() : null;
ArrayList<Unit> successors = new ArrayList<Unit>();
if (currentUnit.fallsThrough()) {
// Add the next unit as the successor
if (nextUnit != null) {
successors.add(nextUnit);
List<Unit> preds = unitToPreds.get(nextUnit);
if (preds == null) {
preds = new ArrayList<Unit>();
unitToPreds.put(nextUnit, preds);
}
preds.add(currentUnit);
}
}
if (currentUnit.branches()) {
for (UnitBox targetBox : currentUnit.getUnitBoxes()) {
Unit target = targetBox.getUnit();
// target it falls through to, so we screen for duplicates:
if (!successors.contains(target)) {
successors.add(target);
List<Unit> preds = unitToPreds.get(target);
if (preds == null) {
preds = new ArrayList<Unit>();
unitToPreds.put(target, preds);
}
preds.add(currentUnit);
}
}
}
// Store away successors
if (!successors.isEmpty()) {
successors.trimToSize();
unitToSuccs.put(currentUnit, successors);
}
}
}
use of soot.Unit in project soot by Sable.
the class UnitGraph method getExtendedBasicBlockPathBetween.
/**
* Look for a path in graph, from def to use. This path has to lie inside an
* extended basic block (and this property implies uniqueness.). The path
* returned includes from and to.
*
* @param from
* start point for the path.
* @param to
* end point for the path.
* @return null if there is no such path.
*/
public List<Unit> getExtendedBasicBlockPathBetween(Unit from, Unit to) {
UnitGraph g = this;
// if this holds, we're doomed to failure!!!
if (g.getPredsOf(to).size() > 1)
return null;
// pathStack := list of succs lists
// pathStackIndex := last visited index in pathStack
LinkedList<Unit> pathStack = new LinkedList<Unit>();
LinkedList<Integer> pathStackIndex = new LinkedList<Integer>();
pathStack.add(from);
pathStackIndex.add(new Integer(0));
int psiMax = (g.getSuccsOf(pathStack.get(0))).size();
int level = 0;
while (pathStackIndex.get(0).intValue() != psiMax) {
int p = (pathStackIndex.get(level)).intValue();
List<Unit> succs = g.getSuccsOf((pathStack.get(level)));
if (p >= succs.size()) {
// no more succs - backtrack to previous level.
pathStack.remove(level);
pathStackIndex.remove(level);
level--;
int q = pathStackIndex.get(level).intValue();
pathStackIndex.set(level, new Integer(q + 1));
continue;
}
Unit betweenUnit = (Unit) (succs.get(p));
// we win!
if (betweenUnit == to) {
pathStack.add(to);
return pathStack;
}
// check preds of betweenUnit to see if we should visit its kids.
if (g.getPredsOf(betweenUnit).size() > 1) {
pathStackIndex.set(level, new Integer(p + 1));
continue;
}
// visit kids of betweenUnit.
level++;
pathStackIndex.add(new Integer(0));
pathStack.add(betweenUnit);
}
return null;
}
Aggregations