use of soot.jimple.StaticFieldRef in project soot by Sable.
the class ArrayIndexLivenessAnalysis method getGenAndKillSetForDefnStmt.
private void getGenAndKillSetForDefnStmt(DefinitionStmt asstmt, HashMap<Stmt, HashSet<Value>> absgen, HashSet<Object> genset, HashSet<Value> absgenset, HashSet<Value> killset, HashSet<Value> condset) {
/* kill left hand side */
Value lhs = asstmt.getLeftOp();
Value rhs = asstmt.getRightOp();
boolean killarrayrelated = false;
boolean killallarrayref = false;
if (fieldin) {
if (lhs instanceof Local) {
HashSet<Value> related = localToFieldRef.get(lhs);
if (related != null)
killset.addAll(related);
} else if (lhs instanceof StaticFieldRef) {
killset.add(lhs);
condset.add(lhs);
} else if (lhs instanceof InstanceFieldRef) {
SootField field = ((InstanceFieldRef) lhs).getField();
HashSet<Value> related = fieldToFieldRef.get(field);
if (related != null)
killset.addAll(related);
condset.add(lhs);
}
if (asstmt.containsInvokeExpr()) {
/*
Value expr = asstmt.getInvokeExpr();
List parameters = ((InvokeExpr)expr).getArgs();
// add the method invocation
boolean killall = false;
if (expr instanceof InstanceInvokeExpr)
killall = true;
else
{
for (int i=0; i<parameters.size(); i++)
{
Value para = (Value)parameters.get(i);
if (para.getType() instanceof RefType)
{
killall = true;
break;
}
}
}
if (killall)
{
killset.addAll(allInstFieldRefs);
}
*/
killset.addAll(allFieldRefs);
}
}
if (arrayin) {
// a = ... or i = ...
if (lhs instanceof Local) {
killarrayrelated = true;
} else // a[i] = ...
if (lhs instanceof ArrayRef) {
killallarrayref = true;
condset.add(lhs);
}
// invokeexpr kills all array references.
if (asstmt.containsInvokeExpr()) {
killallarrayref = true;
}
}
if (csin) {
HashSet<Value> exprs = localToExpr.get(lhs);
if (exprs != null)
killset.addAll(exprs);
if (rhs instanceof BinopExpr) {
Value op1 = ((BinopExpr) rhs).getOp1();
Value op2 = ((BinopExpr) rhs).getOp2();
if (rhs instanceof AddExpr) {
if ((op1 instanceof Local) && (op2 instanceof Local))
genset.add(rhs);
} else if (rhs instanceof MulExpr) {
if ((op1 instanceof Local) || (op2 instanceof Local))
genset.add(rhs);
} else if (rhs instanceof SubExpr) {
if (op2 instanceof Local)
genset.add(rhs);
}
}
}
if ((lhs instanceof Local) && (fullSet.contains(lhs))) {
killset.add(lhs);
/* speculatively add lhs as live condition. */
condset.add(lhs);
} else if (lhs instanceof ArrayRef) {
/* a[i] generate a and i. */
Value base = ((ArrayRef) lhs).getBase();
Value index = ((ArrayRef) lhs).getIndex();
absgenset.add(base);
if (index instanceof Local) {
absgenset.add(index);
}
}
if (rhs instanceof Local) {
/*
if (lhs instanceof Local && fullSet.contains(rhs))
genset.add(rhs);
*/
if (fullSet.contains(rhs))
genset.add(rhs);
/*
if (fieldin && (lhs instanceof FieldRef))
genset.add(rhs);
*/
} else if (rhs instanceof FieldRef) {
if (fieldin)
genset.add(rhs);
} else if (rhs instanceof ArrayRef) {
/* lhs=a[i]. */
Value base = ((ArrayRef) rhs).getBase();
Value index = ((ArrayRef) rhs).getIndex();
absgenset.add(base);
if (index instanceof Local) {
absgenset.add(index);
}
if (arrayin) {
genset.add(rhs);
if (rectarray)
genset.add(Array2ndDimensionSymbol.v(base));
}
} else if (rhs instanceof NewArrayExpr) {
/* a = new A[i]; */
Value size = ((NewArrayExpr) rhs).getSize();
if (size instanceof Local)
genset.add(size);
} else if (rhs instanceof NewMultiArrayExpr) {
/* a = new A[i][]...;*/
/* More precisely, we should track other dimensions. */
List sizes = ((NewMultiArrayExpr) rhs).getSizes();
Iterator sizeIt = sizes.iterator();
while (sizeIt.hasNext()) {
Value size = (Value) sizeIt.next();
if (size instanceof Local)
genset.add(size);
}
} else if (rhs instanceof LengthExpr) {
/* lhs = lengthof rhs */
Value op = ((LengthExpr) rhs).getOp();
genset.add(op);
} else if (rhs instanceof JAddExpr) {
/* lhs = rhs+c, lhs=c+rhs */
Value op1 = ((JAddExpr) rhs).getOp1();
Value op2 = ((JAddExpr) rhs).getOp2();
if ((op1 instanceof IntConstant) && (op2 instanceof Local)) {
genset.add(op2);
} else if ((op2 instanceof IntConstant) && (op1 instanceof Local)) {
genset.add(op1);
}
} else if (rhs instanceof JSubExpr) {
Value op1 = ((JSubExpr) rhs).getOp1();
Value op2 = ((JSubExpr) rhs).getOp2();
if ((op1 instanceof Local) && (op2 instanceof IntConstant)) {
genset.add(op1);
}
}
if (arrayin) {
if (killarrayrelated)
killArrayRelated.put(asstmt, lhs);
if (killallarrayref)
killAllArrayRef.put(asstmt, new Boolean(true));
}
}
use of soot.jimple.StaticFieldRef 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.jimple.StaticFieldRef in project soot by Sable.
the class DexNullTransformer method internalTransform.
@Override
protected void internalTransform(final Body body, String phaseName, Map<String, String> options) {
final DexDefUseAnalysis localDefs = new DexDefUseAnalysis(body);
AbstractStmtSwitch checkDef = new // Alex: should also end as
AbstractStmtSwitch() {
// soon as detected as not
// used as an object
@Override
public void caseAssignStmt(AssignStmt stmt) {
Value r = stmt.getRightOp();
if (r instanceof FieldRef) {
usedAsObject = isObject(((FieldRef) r).getFieldRef().type());
doBreak = true;
return;
} else if (r instanceof ArrayRef) {
ArrayRef ar = (ArrayRef) r;
if (ar.getType() instanceof UnknownType) {
// isObject
usedAsObject = stmt.hasTag("ObjectOpTag");
// (findArrayType
// (g,
// localDefs,
// localUses,
// stmt));
} else {
usedAsObject = isObject(ar.getType());
}
doBreak = true;
return;
} else if (r instanceof StringConstant || r instanceof NewExpr || r instanceof NewArrayExpr) {
usedAsObject = true;
doBreak = true;
return;
} else if (r instanceof CastExpr) {
usedAsObject = isObject(((CastExpr) r).getCastType());
doBreak = true;
return;
} else if (r instanceof InvokeExpr) {
usedAsObject = isObject(((InvokeExpr) r).getType());
doBreak = true;
return;
} else if (r instanceof LengthExpr) {
usedAsObject = false;
doBreak = true;
return;
// introduces alias
}
}
@Override
public void caseIdentityStmt(IdentityStmt stmt) {
if (stmt.getLeftOp() == l) {
usedAsObject = isObject(stmt.getRightOp().getType());
doBreak = true;
return;
}
}
};
AbstractStmtSwitch checkUse = new AbstractStmtSwitch() {
private boolean examineInvokeExpr(InvokeExpr e) {
List<Value> args = e.getArgs();
List<Type> argTypes = e.getMethodRef().parameterTypes();
assert args.size() == argTypes.size();
for (int i = 0; i < args.size(); i++) {
if (args.get(i) == l && isObject(argTypes.get(i))) {
return true;
}
}
// check for base
SootMethodRef sm = e.getMethodRef();
if (!sm.isStatic()) {
if (e instanceof AbstractInvokeExpr) {
AbstractInstanceInvokeExpr aiiexpr = (AbstractInstanceInvokeExpr) e;
Value b = aiiexpr.getBase();
if (b == l) {
return true;
}
}
}
return false;
}
@Override
public void caseInvokeStmt(InvokeStmt stmt) {
InvokeExpr e = stmt.getInvokeExpr();
usedAsObject = examineInvokeExpr(e);
doBreak = true;
return;
}
@Override
public void caseAssignStmt(AssignStmt stmt) {
Value left = stmt.getLeftOp();
Value r = stmt.getRightOp();
if (left instanceof ArrayRef) {
ArrayRef ar = (ArrayRef) left;
if (ar.getIndex() == l) {
doBreak = true;
return;
} else if (ar.getBase() == l) {
usedAsObject = true;
doBreak = true;
return;
}
}
if (left instanceof InstanceFieldRef) {
InstanceFieldRef ifr = (InstanceFieldRef) left;
if (ifr.getBase() == l) {
usedAsObject = true;
doBreak = true;
return;
}
}
// used to assign
if (stmt.getRightOp() == l) {
Value l = stmt.getLeftOp();
if (l instanceof StaticFieldRef && isObject(((StaticFieldRef) l).getFieldRef().type())) {
usedAsObject = true;
doBreak = true;
return;
} else if (l instanceof InstanceFieldRef && isObject(((InstanceFieldRef) l).getFieldRef().type())) {
usedAsObject = true;
doBreak = true;
return;
} else if (l instanceof ArrayRef) {
Type aType = ((ArrayRef) l).getType();
if (aType instanceof UnknownType) {
usedAsObject = stmt.hasTag(// isObject(
"ObjectOpTag");
// findArrayType(g,
// localDefs,
// localUses,
// stmt));
} else {
usedAsObject = isObject(aType);
}
doBreak = true;
return;
}
}
// is used as value (does not exclude assignment)
if (r instanceof FieldRef) {
// isObject(((FieldRef)
usedAsObject = true;
// r).getFieldRef().type());
doBreak = true;
return;
} else if (r instanceof ArrayRef) {
ArrayRef ar = (ArrayRef) r;
if (ar.getBase() == l) {
usedAsObject = true;
} else {
// used as index
usedAsObject = false;
}
doBreak = true;
return;
} else if (r instanceof StringConstant || r instanceof NewExpr) {
throw new RuntimeException("NOT POSSIBLE StringConstant or NewExpr at " + stmt);
} else if (r instanceof NewArrayExpr) {
usedAsObject = false;
doBreak = true;
return;
} else if (r instanceof CastExpr) {
usedAsObject = isObject(((CastExpr) r).getCastType());
doBreak = true;
return;
} else if (r instanceof InvokeExpr) {
usedAsObject = examineInvokeExpr((InvokeExpr) stmt.getRightOp());
doBreak = true;
return;
} else if (r instanceof LengthExpr) {
usedAsObject = true;
doBreak = true;
return;
} else if (r instanceof BinopExpr) {
usedAsObject = false;
doBreak = true;
return;
}
}
@Override
public void caseIdentityStmt(IdentityStmt stmt) {
if (stmt.getLeftOp() == l)
throw new RuntimeException("IMPOSSIBLE 0");
}
@Override
public void caseEnterMonitorStmt(EnterMonitorStmt stmt) {
usedAsObject = stmt.getOp() == l;
doBreak = true;
return;
}
@Override
public void caseExitMonitorStmt(ExitMonitorStmt stmt) {
usedAsObject = stmt.getOp() == l;
doBreak = true;
return;
}
@Override
public void caseReturnStmt(ReturnStmt stmt) {
usedAsObject = stmt.getOp() == l && isObject(body.getMethod().getReturnType());
doBreak = true;
return;
}
@Override
public void caseThrowStmt(ThrowStmt stmt) {
usedAsObject = stmt.getOp() == l;
doBreak = true;
return;
}
};
for (Local loc : getNullCandidates(body)) {
usedAsObject = false;
Set<Unit> defs = localDefs.collectDefinitionsWithAliases(loc);
// process normally
doBreak = false;
for (Unit u : defs) {
// put correct local in l
if (u instanceof DefinitionStmt) {
l = (Local) ((DefinitionStmt) u).getLeftOp();
} else if (u instanceof IfStmt) {
throw new RuntimeException("ERROR: def can not be something else than Assign or Identity statement! (def: " + u + " class: " + u.getClass() + "");
}
// check defs
u.apply(checkDef);
if (doBreak)
break;
// check uses
for (Unit use : localDefs.getUsesOf(l)) {
use.apply(checkUse);
if (doBreak)
break;
}
// for uses
if (doBreak)
break;
}
// change values
if (usedAsObject) {
for (Unit u : defs) {
replaceWithNull(u);
Set<Value> defLocals = new HashSet<Value>();
for (ValueBox vb : u.getDefBoxes()) defLocals.add(vb.getValue());
Local l = (Local) ((DefinitionStmt) u).getLeftOp();
for (Unit uuse : localDefs.getUsesOf(l)) {
Stmt use = (Stmt) uuse;
// If we have a[x] = 0 and a is an object, we may not conclude 0 -> null
if (!use.containsArrayRef() || !defLocals.contains(use.getArrayRef().getBase()))
replaceWithNull(use);
}
}
}
// end if
}
// Check for inlined zero values
AbstractStmtSwitch inlinedZeroValues = new AbstractStmtSwitch() {
final NullConstant nullConstant = NullConstant.v();
@Override
public void caseAssignStmt(AssignStmt stmt) {
// Case a = 0 with a being an object
if (isObject(stmt.getLeftOp().getType()) && isConstZero(stmt.getRightOp())) {
stmt.setRightOp(nullConstant);
return;
}
// Case a = (Object) 0
if (stmt.getRightOp() instanceof CastExpr) {
CastExpr ce = (CastExpr) stmt.getRightOp();
if (isObject(ce.getCastType()) && isConstZero(ce.getOp())) {
stmt.setRightOp(nullConstant);
}
}
// Case a[0] = 0
if (stmt.getLeftOp() instanceof ArrayRef && isConstZero(stmt.getRightOp())) {
ArrayRef ar = (ArrayRef) stmt.getLeftOp();
if (isObjectArray(ar.getBase(), body) || stmt.hasTag("ObjectOpTag")) {
stmt.setRightOp(nullConstant);
}
}
}
private boolean isConstZero(Value rightOp) {
if (rightOp instanceof IntConstant && ((IntConstant) rightOp).value == 0)
return true;
if (rightOp instanceof LongConstant && ((LongConstant) rightOp).value == 0)
return true;
return false;
}
@Override
public void caseReturnStmt(ReturnStmt stmt) {
if (stmt.getOp() instanceof IntConstant && isObject(body.getMethod().getReturnType())) {
IntConstant iconst = (IntConstant) stmt.getOp();
assert iconst.value == 0;
stmt.setOp(nullConstant);
}
}
@Override
public void caseEnterMonitorStmt(EnterMonitorStmt stmt) {
if (stmt.getOp() instanceof IntConstant && ((IntConstant) stmt.getOp()).value == 0)
stmt.setOp(nullConstant);
}
@Override
public void caseExitMonitorStmt(ExitMonitorStmt stmt) {
if (stmt.getOp() instanceof IntConstant && ((IntConstant) stmt.getOp()).value == 0)
stmt.setOp(nullConstant);
}
};
final NullConstant nullConstant = NullConstant.v();
for (Unit u : body.getUnits()) {
u.apply(inlinedZeroValues);
if (u instanceof Stmt) {
Stmt stmt = (Stmt) u;
if (stmt.containsInvokeExpr()) {
InvokeExpr invExpr = stmt.getInvokeExpr();
for (int i = 0; i < invExpr.getArgCount(); i++) if (isObject(invExpr.getMethodRef().parameterType(i)))
if (invExpr.getArg(i) instanceof IntConstant) {
IntConstant iconst = (IntConstant) invExpr.getArg(i);
assert iconst.value == 0;
invExpr.setArg(i, nullConstant);
}
}
}
}
}
use of soot.jimple.StaticFieldRef in project soot by Sable.
the class OnFlyCallGraphBuilder method getImplicitTargets.
private void getImplicitTargets(SootMethod source) {
final SootClass scl = source.getDeclaringClass();
if (source.isNative() || source.isPhantom())
return;
if (source.getSubSignature().indexOf("<init>") >= 0) {
handleInit(source, scl);
}
Body b = source.retrieveActiveBody();
for (Unit u : b.getUnits()) {
final Stmt s = (Stmt) u;
if (s.containsInvokeExpr()) {
InvokeExpr ie = s.getInvokeExpr();
SootMethodRef methodRef = ie.getMethodRef();
switch(methodRef.declaringClass().getName()) {
case "java.lang.reflect.Method":
if (methodRef.getSubSignature().getString().equals("java.lang.Object invoke(java.lang.Object,java.lang.Object[])"))
reflectionModel.methodInvoke(source, s);
break;
case "java.lang.Class":
if (methodRef.getSubSignature().getString().equals("java.lang.Object newInstance()"))
reflectionModel.classNewInstance(source, s);
break;
case "java.lang.reflect.Constructor":
if (methodRef.getSubSignature().getString().equals("java.lang.Object newInstance(java.lang.Object[]))"))
reflectionModel.contructorNewInstance(source, s);
break;
}
if (methodRef.getSubSignature() == sigForName) {
reflectionModel.classForName(source, s);
}
if (ie instanceof StaticInvokeExpr) {
SootClass cl = ie.getMethodRef().declaringClass();
for (SootMethod clinit : EntryPoints.v().clinitsOf(cl)) {
addEdge(source, s, clinit, Kind.CLINIT);
}
}
}
if (s.containsFieldRef()) {
FieldRef fr = s.getFieldRef();
if (fr instanceof StaticFieldRef) {
SootClass cl = fr.getFieldRef().declaringClass();
for (SootMethod clinit : EntryPoints.v().clinitsOf(cl)) {
addEdge(source, s, clinit, Kind.CLINIT);
}
}
}
if (s instanceof AssignStmt) {
Value rhs = ((AssignStmt) s).getRightOp();
if (rhs instanceof NewExpr) {
NewExpr r = (NewExpr) rhs;
SootClass cl = r.getBaseType().getSootClass();
for (SootMethod clinit : EntryPoints.v().clinitsOf(cl)) {
addEdge(source, s, clinit, Kind.CLINIT);
}
} else if (rhs instanceof NewArrayExpr || rhs instanceof NewMultiArrayExpr) {
Type t = rhs.getType();
if (t instanceof ArrayType)
t = ((ArrayType) t).baseType;
if (t instanceof RefType) {
SootClass cl = ((RefType) t).getSootClass();
for (SootMethod clinit : EntryPoints.v().clinitsOf(cl)) {
addEdge(source, s, clinit, Kind.CLINIT);
}
}
}
}
}
}
use of soot.jimple.StaticFieldRef in project robovm by robovm.
the class MethodCompiler method canAccessDirectly.
// private Value callOrInvoke(Unit unit, Value fn, Value ... args) {
// Variable result = null;
// Type returnType = ((FunctionType) fn.getType()).getReturnType();
// if (returnType != VOID) {
// result = this.function.newVariable(returnType);
// }
// List<Trap> traps = getTrapsAt(unit);
// if (!traps.isEmpty()) {
// Label label = new Label();
// BasicBlockRef to = function.newBasicBlockRef(label);
// BasicBlockRef unwind = function.newBasicBlockRef(new Label(traps));
// function.add(new Invoke(result, fn, to, unwind, args));
// function.newBasicBlock(label);
// recordedTraps.add(traps);
// } else {
// function.add(new Call(result, fn, args));
// }
// return result == null ? null : result.ref();
// }
private boolean canAccessDirectly(FieldRef ref) {
SootClass sootClass = this.sootMethod.getDeclaringClass();
SootFieldRef fieldRef = ref.getFieldRef();
if (!fieldRef.declaringClass().equals(sootClass)) {
return false;
}
try {
SootField field = sootClass.getField(fieldRef.name(), fieldRef.type());
/*
* The field exists.
*/
if (field.isStatic()) {
// If not we want an exception to be thrown so we need a trampoline.
return ref instanceof StaticFieldRef;
}
// If not we want an exception to be thrown so we need a trampoline.
return ref instanceof InstanceFieldRef;
} catch (RuntimeException e) {
// isn't declared in the class.
return false;
}
}
Aggregations