use of soot.CharType in project soot by soot-oss.
the class Util method emptyBody.
/**
* Remove all statements except from IdentityStatements for parameters. Return default value (null or zero or nothing
* depending on the return type).
*
* @param jBody
*/
public static void emptyBody(Body jBody) {
// identity statements
List<Unit> idStmts = new ArrayList<Unit>();
List<Local> idLocals = new ArrayList<Local>();
for (Unit u : jBody.getUnits()) {
if (u instanceof IdentityStmt) {
IdentityStmt i = (IdentityStmt) u;
if (i.getRightOp() instanceof ParameterRef || i.getRightOp() instanceof ThisRef) {
idStmts.add(u);
idLocals.add((Local) i.getLeftOp());
}
}
}
jBody.getUnits().clear();
jBody.getLocals().clear();
jBody.getTraps().clear();
final LocalGenerator lg = Scene.v().createLocalGenerator(jBody);
for (Unit u : idStmts) {
jBody.getUnits().add(u);
}
for (Local l : idLocals) {
jBody.getLocals().add(l);
}
Type rType = jBody.getMethod().getReturnType();
jBody.getUnits().add(Jimple.v().newNopStmt());
if (rType instanceof VoidType) {
jBody.getUnits().add(Jimple.v().newReturnVoidStmt());
} else {
Type t = jBody.getMethod().getReturnType();
Local l = lg.generateLocal(t);
AssignStmt ass = null;
if (t instanceof RefType || t instanceof ArrayType) {
ass = Jimple.v().newAssignStmt(l, NullConstant.v());
} else if (t instanceof LongType) {
ass = Jimple.v().newAssignStmt(l, LongConstant.v(0));
} else if (t instanceof FloatType) {
ass = Jimple.v().newAssignStmt(l, FloatConstant.v(0.0f));
} else if (t instanceof IntType) {
ass = Jimple.v().newAssignStmt(l, IntConstant.v(0));
} else if (t instanceof DoubleType) {
ass = Jimple.v().newAssignStmt(l, DoubleConstant.v(0));
} else if (t instanceof BooleanType || t instanceof ByteType || t instanceof CharType || t instanceof ShortType) {
ass = Jimple.v().newAssignStmt(l, IntConstant.v(0));
} else {
throw new RuntimeException("error: return type unknown: " + t + " class: " + t.getClass());
}
jBody.getUnits().add(ass);
jBody.getUnits().add(Jimple.v().newReturnStmt(l));
}
}
use of soot.CharType in project soot by soot-oss.
the class TypeAssigner method compareTypings.
/* Returns -1 if a < b, +1 if b < a, 0 if a = b and 3 otherwise. */
private static int compareTypings(JimpleBody a, JimpleBody b) {
int r = 0;
Iterator<Local> ib = b.getLocals().iterator();
for (Local v : a.getLocals()) {
Type ta = v.getType(), tb = ib.next().getType();
if (soot.jimple.toolkits.typing.fast.TypeResolver.typesEqual(ta, tb)) {
continue;
} else if ((ta instanceof CharType && (tb instanceof ByteType || tb instanceof ShortType)) || (tb instanceof CharType && (ta instanceof ByteType || ta instanceof ShortType))) {
continue;
} else if (soot.jimple.toolkits.typing.fast.AugHierarchy.ancestor_(ta, tb)) {
if (r == -1) {
return 3;
} else {
r = 1;
}
} else if (soot.jimple.toolkits.typing.fast.AugHierarchy.ancestor_(tb, ta)) {
if (r == 1) {
return 3;
} else {
r = -1;
}
} else {
return 3;
}
}
return r;
}
use of soot.CharType in project soot by soot-oss.
the class MethodCallFinder method createDefaultStmt.
public AugmentedStmt createDefaultStmt(Object field) {
Value ref = null;
Type fieldType = null;
if (field instanceof SootField) {
// have to make a static field ref
SootFieldRef tempFieldRef = ((SootField) field).makeRef();
fieldType = ((SootField) field).getType();
if (((SootField) field).isStatic()) {
ref = new DStaticFieldRef(tempFieldRef, true);
} else {
ref = new DInstanceFieldRef(new JimpleLocal("this", fieldType), tempFieldRef, new HashSet<Object>());
}
} else if (field instanceof Local) {
ref = (Local) field;
fieldType = ((Local) field).getType();
}
GAssignStmt assignStmt = null;
if (fieldType instanceof RefType) {
assignStmt = new GAssignStmt(ref, NullConstant.v());
} else if (fieldType instanceof DoubleType) {
assignStmt = new GAssignStmt(ref, DoubleConstant.v(0));
} else if (fieldType instanceof FloatType) {
assignStmt = new GAssignStmt(ref, FloatConstant.v(0));
} else if (fieldType instanceof LongType) {
assignStmt = new GAssignStmt(ref, LongConstant.v(0));
} else if (fieldType instanceof IntType || fieldType instanceof ByteType || fieldType instanceof ShortType || fieldType instanceof CharType || fieldType instanceof BooleanType) {
assignStmt = new GAssignStmt(ref, DIntConstant.v(0, fieldType));
}
if (assignStmt != null) {
// System.out.println("AssignStmt is"+assignStmt);
AugmentedStmt as = new AugmentedStmt(assignStmt);
return as;
} else {
return null;
}
}
use of soot.CharType in project soot by soot-oss.
the class CP method createInitialInput.
/*
* constant fields added with KNOWN CONSTANT VALUE formals added with TOP locals added with 0 other fields IGNORED
*/
public void createInitialInput() {
initialInput = new ArrayList<CPTuple>();
// adding constant fields
initialInput.addAll(constantFieldTuples);
// String className =
// analyze.getDavaBody().getMethod().getDeclaringClass().getName();
// adding formals
formals = new ArrayList<CPTuple>();
// System.out.println("Adding following formals: with TOP");
Collection col = methodNode.getDavaBody().get_ParamMap().values();
Iterator it = col.iterator();
while (it.hasNext()) {
Object temp = it.next();
if (temp instanceof Local) {
Local tempLocal = (Local) temp;
if (!(tempLocal.getType() instanceof PrimType)) {
continue;
}
CPVariable newVar = new CPVariable(tempLocal);
// new tuple set to top since this is a formal and we dont know
// what value we will get into it
CPTuple newTuple = new CPTuple(localClassName, newVar, true);
initialInput.add(newTuple);
formals.add(newTuple);
// System.out.print("\t"+tempLocal.getName());
}
}
// System.out.println();
// adding locals
List decLocals = methodNode.getDeclaredLocals();
it = decLocals.iterator();
locals = new ArrayList<CPTuple>();
// System.out.println("Adding following locals with default values:");
while (it.hasNext()) {
Object temp = it.next();
if (temp instanceof Local) {
Local tempLocal = (Local) temp;
Type localType = tempLocal.getType();
if (!(localType instanceof PrimType)) {
continue;
}
CPVariable newVar = new CPVariable(tempLocal);
// store the default value into this object
Object value;
// depending on its type
if (localType instanceof BooleanType) {
value = new Boolean(false);
} else if (localType instanceof ByteType) {
value = new Integer(0);
} else if (localType instanceof CharType) {
value = new Integer(0);
} else if (localType instanceof DoubleType) {
value = new Double(0);
} else if (localType instanceof FloatType) {
value = new Float(0);
} else if (localType instanceof IntType) {
value = new Integer(0);
} else if (localType instanceof LongType) {
value = new Long(0);
} else if (localType instanceof ShortType) {
value = new Integer(0);
} else {
throw new DavaFlowAnalysisException("Unknown PrimType");
}
CPTuple newTuple = new CPTuple(localClassName, newVar, value);
/*
* Commenting the next line since we dont want initial Input to have any locals in it all locals are considered
* bottom initially
*/
// initialInput.add(newTuple);
locals.add(newTuple);
// System.out.print("\t"+tempLocal.getName());
}
// was a local
}
// System.out.println();
}
use of soot.CharType in project soot by soot-oss.
the class LocalsToBitField method internalTransform.
@SuppressWarnings("fallthrough")
protected void internalTransform(Body b, String phaseName, Map<String, String> options) {
int weight = soot.jbco.Main.getWeight(phaseName, b.getMethod().getSignature());
if (weight == 0) {
return;
}
// build mapping of baf locals to jimple locals
Chain<Local> bLocals = b.getLocals();
PatchingChain<Unit> u = b.getUnits();
Unit first = null;
List<Value> params = new ArrayList<Value>();
Iterator<Unit> uit = u.iterator();
while (uit.hasNext()) {
Unit unit = uit.next();
if (unit instanceof IdentityInst) {
IdentityInst ii = (IdentityInst) unit;
if (ii.getRightOpBox().getValue() instanceof ParameterRef) {
Value v = ii.getLeftOp();
if (v instanceof Local) {
params.add(v);
first = unit;
}
}
}
}
// build mapping of baf locals to jimple locals
Map<Local, Local> bafToJLocals = new HashMap<Local, Local>();
Iterator<Local> jlocIt = soot.jbco.Main.methods2JLocals.get(b.getMethod()).iterator();
while (jlocIt.hasNext()) {
Local jl = jlocIt.next();
Iterator<Local> blocIt = bLocals.iterator();
while (blocIt.hasNext()) {
Local bl = blocIt.next();
if (bl.getName().equals(jl.getName())) {
bafToJLocals.put(bl, jl);
break;
}
}
}
List<Local> booleans = new ArrayList<Local>();
List<Local> bytes = new ArrayList<Local>();
List<Local> chars = new ArrayList<Local>();
List<Local> ints = new ArrayList<Local>();
Map<Local, Integer> sizes = new HashMap<Local, Integer>();
Iterator<Local> blocs = bLocals.iterator();
while (blocs.hasNext()) {
Local bl = blocs.next();
if (params.contains(bl)) {
continue;
}
locals++;
Local jlocal = bafToJLocals.get(bl);
if (jlocal != null) {
Type t = jlocal.getType();
if (t instanceof PrimType && !(t instanceof DoubleType || t instanceof LongType) && Rand.getInt(10) <= weight) {
if (t instanceof BooleanType) {
booleans.add(bl);
sizes.put(bl, 1);
} else if (t instanceof ByteType) {
bytes.add(bl);
sizes.put(bl, 8);
} else if (t instanceof CharType) {
// || t instanceof ShortType) {
chars.add(bl);
sizes.put(bl, 16);
} else if (t instanceof IntType) {
ints.add(bl);
sizes.put(bl, 32);
}
}
}
}
int count = 0;
Map<Local, Local> bafToNewLocs = new HashMap<Local, Local>();
int total = booleans.size() + bytes.size() * 8 + chars.size() * 16 + ints.size() * 32;
Map<Local, Map<Local, Integer>> newLocs = new HashMap<Local, Map<Local, Integer>>();
while (total >= 32 && booleans.size() + bytes.size() + chars.size() + ints.size() > 2) {
// soot.jbco.util.Rand.getInt(2) > 0 ?
Local nloc = Baf.v().newLocal("newDumby" + count++, LongType.v());
// DoubleType.v() : LongType.v());
Map<Local, Integer> nlocMap = new HashMap<Local, Integer>();
boolean done = false;
int index = 0;
while (index < 64 && !done) {
int max = 64 - index;
max = max > 31 ? 4 : max > 15 ? 3 : max > 7 ? 2 : 1;
int rand = Rand.getInt(max);
max = index;
switch(rand) {
case 3:
if (ints.size() > 0) {
Local l = ints.remove(Rand.getInt(ints.size()));
nlocMap.put(l, index);
index = index + 32;
bafToNewLocs.put(l, nloc);
index = getNewIndex(index, ints, chars, bytes, booleans);
break;
}
case 2:
if (chars.size() > 0) {
Local l = chars.remove(Rand.getInt(chars.size()));
nlocMap.put(l, index);
index = index + 16;
bafToNewLocs.put(l, nloc);
index = getNewIndex(index, ints, chars, bytes, booleans);
break;
}
case 1:
if (bytes.size() > 0) {
Local l = bytes.remove(Rand.getInt(bytes.size()));
nlocMap.put(l, index);
index = index + 8;
bafToNewLocs.put(l, nloc);
index = getNewIndex(index, ints, chars, bytes, booleans);
break;
}
case 0:
if (booleans.size() > 0) {
Local l = booleans.remove(Rand.getInt(booleans.size()));
nlocMap.put(l, index++);
bafToNewLocs.put(l, nloc);
index = getNewIndex(index, ints, chars, bytes, booleans);
break;
}
}
// end switch
if (max == index) {
done = true;
}
}
newLocs.put(nloc, nlocMap);
bLocals.add(nloc);
if (first != null) {
u.insertAfter(Baf.v().newStoreInst(LongType.v(), nloc), first);
u.insertAfter(Baf.v().newPushInst(LongConstant.v(0)), first);
} else {
u.addFirst(Baf.v().newStoreInst(LongType.v(), nloc));
u.addFirst(Baf.v().newPushInst(LongConstant.v(0)));
}
total = booleans.size() + bytes.size() * 8 + chars.size() * 16 + ints.size() * 32;
}
if (bafToNewLocs.size() == 0) {
return;
}
Iterator<Unit> it = u.snapshotIterator();
while (it.hasNext()) {
Unit unit = it.next();
if (unit instanceof StoreInst) {
StoreInst si = (StoreInst) unit;
Local bafLoc = si.getLocal();
Local nloc = bafToNewLocs.get(bafLoc);
if (nloc != null) {
Local jloc = bafToJLocals.get(bafLoc);
int index = newLocs.get(nloc).get(bafLoc);
int size = sizes.get(bafLoc);
long longmask = ~((size == 1 ? 0x1L : size == 8 ? 0xFFL : size == 16 ? 0xFFFFL : 0xFFFFFFFFL) << index);
u.insertBefore(Baf.v().newPrimitiveCastInst(jloc.getType(), LongType.v()), unit);
if (index > 0) {
u.insertBefore(Baf.v().newPushInst(IntConstant.v(index)), unit);
u.insertBefore(Baf.v().newShlInst(LongType.v()), unit);
}
u.insertBefore(Baf.v().newPushInst(LongConstant.v(~longmask)), unit);
u.insertBefore(Baf.v().newAndInst(LongType.v()), unit);
u.insertBefore(Baf.v().newLoadInst(LongType.v(), nloc), unit);
u.insertBefore(Baf.v().newPushInst(LongConstant.v(longmask)), unit);
u.insertBefore(Baf.v().newAndInst(LongType.v()), unit);
u.insertBefore(Baf.v().newXorInst(LongType.v()), unit);
u.insertBefore(Baf.v().newStoreInst(LongType.v(), nloc), unit);
u.remove(unit);
}
} else if (unit instanceof LoadInst) {
LoadInst li = (LoadInst) unit;
Local bafLoc = li.getLocal();
Local nloc = bafToNewLocs.get(bafLoc);
if (nloc != null) {
int index = newLocs.get(nloc).get(bafLoc);
int size = sizes.get(bafLoc);
long longmask = (size == 1 ? 0x1L : size == 8 ? 0xFFL : size == 16 ? 0xFFFFL : 0xFFFFFFFFL) << index;
u.insertBefore(Baf.v().newLoadInst(LongType.v(), nloc), unit);
u.insertBefore(Baf.v().newPushInst(LongConstant.v(longmask)), unit);
u.insertBefore(Baf.v().newAndInst(LongType.v()), unit);
if (index > 0) {
u.insertBefore(Baf.v().newPushInst(IntConstant.v(index)), unit);
u.insertBefore(Baf.v().newShrInst(LongType.v()), unit);
}
Type origType = bafToJLocals.get(bafLoc).getType();
Type t = getType(origType);
u.insertBefore(Baf.v().newPrimitiveCastInst(LongType.v(), t), unit);
if (!(origType instanceof IntType) && !(origType instanceof BooleanType)) {
u.insertBefore(Baf.v().newPrimitiveCastInst(t, origType), unit);
}
u.remove(unit);
}
} else if (unit instanceof IncInst) {
IncInst ii = (IncInst) unit;
Local bafLoc = ii.getLocal();
Local nloc = bafToNewLocs.get(bafLoc);
if (nloc != null) {
Type jlocType = getType(bafToJLocals.get(bafLoc).getType());
int index = newLocs.get(nloc).get(bafLoc);
int size = sizes.get(bafLoc);
long longmask = (size == 1 ? 0x1L : size == 8 ? 0xFFL : size == 16 ? 0xFFFFL : 0xFFFFFFFFL) << index;
u.insertBefore(Baf.v().newPushInst(ii.getConstant()), unit);
u.insertBefore(Baf.v().newLoadInst(LongType.v(), nloc), unit);
u.insertBefore(Baf.v().newPushInst(LongConstant.v(longmask)), unit);
u.insertBefore(Baf.v().newAndInst(LongType.v()), unit);
if (index > 0) {
u.insertBefore(Baf.v().newPushInst(IntConstant.v(index)), unit);
u.insertBefore(Baf.v().newShrInst(LongType.v()), unit);
}
u.insertBefore(Baf.v().newPrimitiveCastInst(LongType.v(), ii.getConstant().getType()), unit);
u.insertBefore(Baf.v().newAddInst(ii.getConstant().getType()), unit);
u.insertBefore(Baf.v().newPrimitiveCastInst(jlocType, LongType.v()), unit);
if (index > 0) {
u.insertBefore(Baf.v().newPushInst(IntConstant.v(index)), unit);
u.insertBefore(Baf.v().newShlInst(LongType.v()), unit);
}
longmask = ~longmask;
u.insertBefore(Baf.v().newLoadInst(LongType.v(), nloc), unit);
u.insertBefore(Baf.v().newPushInst(LongConstant.v(longmask)), unit);
u.insertBefore(Baf.v().newAndInst(LongType.v()), unit);
u.insertBefore(Baf.v().newXorInst(LongType.v()), unit);
u.insertBefore(Baf.v().newStoreInst(LongType.v(), nloc), unit);
u.remove(unit);
}
}
}
Iterator<Local> localIterator = bLocals.snapshotIterator();
while (localIterator.hasNext()) {
Local l = localIterator.next();
if (bafToNewLocs.containsKey(l)) {
bLocals.remove(l);
replaced++;
}
}
}
Aggregations