public class Main extends SceneTransformer {
- public static final int initialSize = 100;
+ public static final int initialSize = 1000;
private static Main instance = new Main();
private Main() { }
}
static int tfr = 0;
+
+ public static Local newLocal(Body b, Type t) {
+ Local l = Jimple.v().newLocal("tmpRef" + (tfr++), t);
+ b.getLocals().add(l);
+ return l;
+ }
+
public static Local viaLocal(Value v, Body b) {
Local l = Jimple.v().newLocal("tmpRef" + (tfr++), v.getType());
b.getLocals().add(l);
HashMap map = new HashMap();
HashSet size_added = new HashSet();
- public boolean isGladiator(SootClass c) { return c.implementsInterface("org.ibex.arenaj.Gladiator"); }
- public boolean isGladiatorField(SootField f)
- { return isGladiator(f.getDeclaringClass()) && !f.getName().equals("this") && f.getName().indexOf('$')==-1; }
- public boolean isGladiatorFieldRef(SootFieldRef f) {
- return isGladiator(f.declaringClass()) && !f.name().equals("this") && f.name().indexOf('$')==-1;
- }
- public SootField getGladiatorField(SootField f) { return getGladiatorField(f.makeRef()); }
- public SootField getGladiatorField(SootFieldRef f) {
+ public boolean implementsGladiator(SootClass c) { return c.implementsInterface("org.ibex.arenaj.Gladiator"); }
+ public SootField getSliceForField(SootField f) { return getSliceForField(f.makeRef()); }
+ public SootField getSliceForField(SootFieldRef f) {
SootClass c = f.declaringClass();
SootClass oc = Scene.v().getSootClass(c.getName().substring(0, c.getName().lastIndexOf('$')));
String sig = f.declaringClass().getName()+"."+f.name();
if (map.get(sig) != null) return (SootField)map.get(sig);
Type t = f.type();
- if (t instanceof RefType && isGladiator(((RefType)t).getSootClass())) t = IntType.v();
+ if (t instanceof RefType && implementsGladiator(((RefType)t).getSootClass())) t = IntType.v();
SootField nf = new SootField(c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$" + f.name(),
t.makeArrayType(),
0);
Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(initialSize));
Local newArrLocal = Jimple.v().newLocal("tmpRef" + (tfr++), f.type().makeArrayType());
body.getLocals().add(newArrLocal);
- InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(thisLocal(c,body), nf.makeRef());
+ InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), nf.makeRef());
body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, newArrLocal));
body.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
return m.getActiveBody();
}
- public Local thisLocal(SootClass c, Body b) { return viaLocal(Jimple.v().newThisRef(c.getType()), b); }
- public SootFieldRef getGladiatorFieldSizeRef(SootClass c) {
- SootClass mc = getParent(c);
- String name = c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$$size";
- if (map.get(name) == null) {
- SootField f = new SootField(name, IntType.v());
- mc.addField(f);
-
- Body body = getInitBody(mc);
- InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(thisLocal(c,body), f.makeRef());
- body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, IntConstant.v(0)));
+ public boolean implementsGladiator(Type t) { return (t instanceof RefType)&&implementsGladiator(((RefType)t).getSootClass()); }
- map.put(c, f);
- }
- return Scene.v().makeFieldRef(mc, name, IntType.v(), false);
- }
- public SootFieldRef getGladiatorFieldMaxRef(SootClass c) {
- SootClass mc = getParent(c);
- String name = c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$$max";
- if (map.get(name) == null) {
- SootField f = new SootField(name, IntType.v());
- mc.addField(f);
-
- Body body = getInitBody(mc);
- InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(thisLocal(c,body), f.makeRef());
- body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, IntConstant.v(initialSize)));
-
- map.put(c, f);
- }
- return Scene.v().makeFieldRef(mc, name, IntType.v(), false);
- }
-
- public boolean isGladiatorType(Type t) { return (t instanceof RefType) && isGladiator(((RefType)t).getSootClass()); }
-
-
-
-
- boolean done = false;
public void internalTransform(String phaseName, Map options) {
System.out.println("begun");
List ac = new LinkedList();
}
for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
SootClass sc = (SootClass)it.next();
- if (isGladiator(sc)) {
+ if (implementsGladiator(sc)) {
System.out.println(" application class: " + sc.getName());
System.out.println(" application class: " + getParent(sc).getName());
sc.setApplicationClass();
for(Iterator it = ac.iterator(); it.hasNext();) {
SootClass sc = (SootClass)it.next();
if (sc.isInterface()) continue;
- if (!isGladiator(sc)) continue;
+ if (!implementsGladiator(sc)) continue;
System.out.println("fixing Gladiator class: " + sc);
fixClass(sc);
}
public void fixClass(SootClass sc) {
SootClass mc = getParent(sc);
- String incFuncName = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$inc";
+ String subname = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1);
+ String incFuncName = subname + "$$inc";
SootMethod method = new SootMethod(incFuncName, new LinkedList(),
IntType.v(), Modifier.PRIVATE,
new LinkedList());
+ Body mcInitBody = getInitBody(mc);
mc.addMethod(method);
Body body = Jimple.v().newBody(method);
method.setActiveBody(body);
((JimpleBody)body).insertIdentityStmts();
- Local l = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v());
- body.getLocals().add(l);
- Local l2 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v());
- body.getLocals().add(l2);
- Local l3 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v());
- body.getLocals().add(l3);
- InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), getGladiatorFieldSizeRef(sc));
- body.getUnits().add(Jimple.v().newAssignStmt(l, sfr));
+ Local l = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v()); body.getLocals().add(l);
+ Local l2 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v()); body.getLocals().add(l2);
+ Local l3 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v()); body.getLocals().add(l3);
+
+ Stmt where = ((JimpleBody)mcInitBody).getFirstNonIdentityStmt();
+ SootField maxField = new SootField(subname + "$$max", IntType.v());
+ mc.addField(maxField);
+ mcInitBody.getUnits()
+ .insertBefore(Jimple.v().newAssignStmt(Jimple.v().newInstanceFieldRef(mcInitBody.getThisLocal(), maxField.makeRef()),
+ IntConstant.v(initialSize)),
+ where);
+
+ SootField sfr = new SootField(subname + "$$size", IntType.v());
+ mc.addField(sfr);
+ mcInitBody.getUnits()
+ .insertBefore(Jimple.v().newAssignStmt(Jimple.v().newInstanceFieldRef(mcInitBody.getThisLocal(), sfr.makeRef()),
+ IntConstant.v(0)),
+ where);
+
+ body.getUnits().add(Jimple.v().newAssignStmt(l, Jimple.v().newInstanceFieldRef(body.getThisLocal(), sfr.makeRef())));
body.getUnits().add(Jimple.v().newAssignStmt(l2, Jimple.v().newAddExpr(l, IntConstant.v(1))));
- InstanceFieldRef maxField = Jimple.v().newInstanceFieldRef(body.getThisLocal(), getGladiatorFieldMaxRef(sc));
- body.getUnits().add(Jimple.v().newAssignStmt(l3, maxField));
+ body.getUnits().add(Jimple.v().newAssignStmt(Jimple.v().newInstanceFieldRef(body.getThisLocal(),sfr.makeRef()),l2));
+ body.getUnits().add(Jimple.v().newAssignStmt(l3,Jimple.v().newInstanceFieldRef(body.getThisLocal(),maxField.makeRef())));
Stmt stmt = Jimple.v().newReturnStmt(l2);
body.getUnits().add(Jimple.v().newIfStmt(Jimple.v().newLtExpr(l2, l3), stmt));
Local l4 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v());
body.getLocals().add(l4);
body.getUnits().add(Jimple.v().newAssignStmt(l4, Jimple.v().newShlExpr(l3, IntConstant.v(1))));
- body.getUnits().add(Jimple.v().newAssignStmt(maxField, l4));
+ body.getUnits().add(Jimple.v().newAssignStmt(Jimple.v().newInstanceFieldRef(body.getThisLocal(), maxField.makeRef()), l4));
for(Iterator it = sc.getFields().iterator(); it.hasNext();) {
- SootField f = getGladiatorField((SootField)it.next());
+ SootField f = getSliceForField((SootField)it.next());
InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), f.makeRef());
Local ll0 = Jimple.v().newLocal("tmpRef" + (tfr++), f.getType());
body.getLocals().add(ll0);
for(Iterator it = sc.getMethods().iterator(); it.hasNext();) {
SootMethod m = (SootMethod)it.next();
if (!m.isConcrete()) continue;
- if (isGladiator(m.getDeclaringClass()) && m.getName().equals("<init>")) {
+ boolean doremove = true;
+ Body mbody = m.getActiveBody();
+ if (implementsGladiator(m.getDeclaringClass()) && m.getName().equals("<init>")) {
+ doremove = false;
SootClass c = m.getDeclaringClass();
- String name = c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$$$init";
+ String name = /*c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + */"$init";
List li = m.getParameterTypes();
- li.remove(0);
- li.add(IntType.v());
+ //li.add(IntType.v());
c.removeMethod(m);
- SootMethod nm = new SootMethod(name, li, isGladiatorType(m.getReturnType()) ? IntType.v() : m.getReturnType());
+ SootMethod nm = new SootMethod(name, li, implementsGladiator(m.getReturnType()) ? IntType.v() : m.getReturnType());
JimpleBody bod = Jimple.v().newBody(nm);
- getParent(c).addMethod(nm);
- //bod.insertIdentityStmts();
bod.importBodyContentsFrom(m.getActiveBody());
nm.setActiveBody(bod);
- continue;
+ m = nm;
+ mbody = bod;
+ mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
+ mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
+ } else {
+ System.out.println("examining " + sc.getName() + "." + m.getSignature());
+ m.retrieveActiveBody();
}
- System.out.println("examining " + sc.getName() + "." + m.getSignature());
- m.retrieveActiveBody();
if (m.isStatic()) continue;
String name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + m.getName();
getParent(sc).addMethod(m2);
JimpleBody ab = (JimpleBody)Jimple.v().newBody(m2);
- ab.importBodyContentsFrom(m.getActiveBody());
+ ab.importBodyContentsFrom(mbody);
m2.setActiveBody(ab);
- //Local loc = Jimple.v().newLocal("tmpRef" + (tfr++), getParent(sc).getType());
- //ab.getLocals().add(loc);
+ Local loc = Jimple.v().newLocal("tmpRef" + (tfr++), getParent(sc).getType());
+ ab.getLocals().add(loc);
// FIXME: insert assignment to this
for(Iterator z = ab.getLocals().iterator(); z.hasNext();) {
Local loc2 = (Local)z.next();
- if (isGladiatorType(loc2.getType())) {
+ if (implementsGladiator(loc2.getType())) {
loc2.setType(IntType.v());
}
}
Chain units = ab.getUnits();
+ boolean touched = false;
+ Local loc0 = Jimple.v().newLocal("tmpRef" + (tfr++), getParent(sc).getType());
+ ab.getLocals().add(loc0);
for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
Stmt s = (Stmt) stmtIt.next();
if (s instanceof IdentityStmt) {
if (is.getRightOp() instanceof ThisRef) {
left.setType(IntType.v());
is.getRightOpBox().setValue(Jimple.v().newParameterRef(IntType.v(), m.getParameterCount()));
- break;
+ if (!touched) {
+ units.addFirst(Jimple.v().newIdentityStmt(loc0, Jimple.v().newThisRef(getParent(sc).getType())));
+ touched = true;
+ }
}
}
- /*
+
for(Iterator i = s.getUseAndDefBoxes().iterator(); i.hasNext();) {
Object o = i.next();
if (o instanceof ValueBox) {
ValueBox vb = (ValueBox)o;
+ o = vb.getValue();
+ /*
+ if (o instanceof Local && implementsGladiator(((Local)o).getType())) {
+ System.out.println("thunking");
+ vb.setValue(loc0);
+ }
+ */
if (vb.getValue() instanceof ThisRef) {
System.out.println(s);
vb.setValue(loc);
}
}
}
- */
+
}
- sc.removeMethod(m);
+ if (doremove) sc.removeMethod(m);
}
body.getUnits().add(stmt);
if (t instanceof RefType) {
RefType rt = (RefType)t;
SootClass sc = rt.getSootClass();
- if (isGladiator(sc)) f.setType(IntType.v());
+ if (implementsGladiator(sc)) f.setType(IntType.v());
} else if (t instanceof ArrayType) {
ArrayType at = (ArrayType)t;
t = at.getElementType();
if (!(t instanceof RefType)) continue;
RefType rt = (RefType)t;
SootClass sc = rt.getSootClass();
- if (isGladiator(sc)) f.setType(IntType.v().makeArrayType());
+ if (implementsGladiator(sc)) f.setType(IntType.v().makeArrayType());
}
}
for(Iterator it = list.iterator(); it.hasNext();) {
SootMethod m = (SootMethod)it.next();
Body b = null;
+ if (m.getName().endsWith("$$inc")) continue;
if (m.hasActiveBody()) b = fixBody(m.getActiveBody(), c, m);
List l2 = new LinkedList();
List l = m.getParameterTypes();
for(Iterator it2 = l.iterator(); it2.hasNext();) {
Type t = (Type)it2.next();
- l2.add(isGladiatorType(t) ? IntType.v() : t);
+ l2.add(implementsGladiator(t) ? IntType.v() : t);
}
Type t = m.getReturnType();
- if (isGladiatorType(t)) {
+ if (implementsGladiator(t)) {
t = IntType.v();
if (m.hasActiveBody()) {
Body bod = m.getActiveBody();
}
}
String name = m.getName();
- SootMethod meth = new SootMethod(name, l2, isGladiatorType(t) ? IntType.v() : t, m.getModifiers());
+ SootMethod meth = new SootMethod(name, l2, implementsGladiator(t) ? IntType.v() : t, m.getModifiers());
if (b != null) {
JimpleBody b2 = Jimple.v().newBody(meth);
b2.importBodyContentsFrom(b);
protected Body fixBody(Body body, SootClass ownerClass, SootMethod smeth) {
if (body instanceof JimpleBody) {
JimpleBody b2 = Jimple.v().newBody(smeth);
- //b2.insertIdentityStmts();
b2.importBodyContentsFrom(body);
body = b2;
}
System.out.println("fixBody("+body.getMethod()+")");
for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) {
Local l = (Local)it.next();
- if (isGladiatorType(l.getType())) l.setType(IntType.v());
+ if (implementsGladiator(l.getType())) l.setType(IntType.v());
}
for(int qq=0; qq<2; qq++) for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
Stmt s = (Stmt) stmtIt.next();
ds.getRightOpBox().setValue(IntConstant.v(-1));
}
}
+ if (implementsGladiator(smeth.getReturnType()) && s instanceof ReturnStmt)
+ if (((ReturnStmt)s).getOp().getType() instanceof NullType)
+ ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
List l = s.getUseAndDefBoxes();
for(Iterator it = l.iterator(); it.hasNext();) {
Object o = it.next();
if (v instanceof BinopExpr) {
BinopExpr boe = (BinopExpr)v;
- if (boe.getOp1().getType() instanceof PrimType && boe.getOp2().getType() instanceof NullType) {
- boe.setOp2(IntConstant.v(-1));
- }
- if (boe.getOp2().getType() instanceof PrimType && boe.getOp1().getType() instanceof NullType) {
- boe.setOp1(IntConstant.v(-1));
- }
+ Type t1 = boe.getOp1().getType();
+ Type t2 = boe.getOp2().getType();
+ if (t1 instanceof PrimType && t2 instanceof NullType) boe.setOp2(IntConstant.v(-1));
+ if (t2 instanceof PrimType && t1 instanceof NullType) boe.setOp1(IntConstant.v(-1));
}
if (v instanceof NewExpr) {
NewExpr ne = (NewExpr)v;
- if (isGladiatorType(ne.getBaseType())) {
+ if (implementsGladiator(ne.getBaseType())) {
SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
SootClass mc = getParent(sc);
String incFuncName = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$inc";
SootMethodRef smr = Scene.v().makeMethodRef(mc, incFuncName, new LinkedList(), IntType.v(), false);
- Expr invokeExpr = Jimple.v().newSpecialInvokeExpr(thisLocal(mc,body), smr);
+ Expr invokeExpr = Jimple.v().newSpecialInvokeExpr(body.getThisLocal(), smr);
Local ll = viaLocal(invokeExpr, body, s);
vb.setValue(ll);
v = ll;
InvokeExpr ie = (InvokeExpr)v;
SootMethodRef mr = ie.getMethodRef();
String name = mr.name();
- if (v instanceof InstanceInvokeExpr && isGladiator(mr.declaringClass())) {
+ if (v instanceof InstanceInvokeExpr && implementsGladiator(mr.declaringClass())) {
InstanceInvokeExpr iie = (InstanceInvokeExpr)v;
List li = new LinkedList();
li.addAll(iie.getArgs());
LinkedList pl = new LinkedList();
- pl.addAll(mr.parameterTypes());
- if (mr.name().equals("<init>") && isGladiator(mr.declaringClass())) {
+ for(Iterator it2 = mr.parameterTypes().iterator(); it2.hasNext();) {
+ Type t = (Type)it2.next();
+ pl.add(implementsGladiator(t) ? IntType.v() : t);
+ }
+ if (mr.name().equals("<init>") && implementsGladiator(mr.declaringClass())) {
name = "$init";
- li.remove(0);
- pl.remove(0);
- //pl.addFirst(body.getThisLocal());
+ //li.remove(0);
+ //pl.remove(0);
}
pl.add(IntType.v());
li.add(viaLocal(iie.getBase(),body,s));
mr = Scene.v().makeMethodRef(getParent(sc),
name,
pl,
- isGladiatorType(mr.returnType()) ? IntType.v() : mr.returnType(),
+ implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
false);
- ie = Jimple.v().newVirtualInvokeExpr(viaLocal(thisLocal(getParent(sc),body),body,s), mr, li);
+ ie = Jimple.v().newVirtualInvokeExpr(body.getThisLocal(), mr, li);
vb.setValue(v = ie);
- } else {
+
+ } else if (!(v instanceof StaticInvokeExpr)) {
List l0 = mr.parameterTypes();
List l2 = new LinkedList();
for(Iterator it2 = l0.iterator(); it2.hasNext();) {
Type t = (Type)it2.next();
- l2.add(isGladiatorType(t) ? IntType.v() : t);
+ l2.add(implementsGladiator(t) ? IntType.v() : t);
}
mr = Scene.v().makeMethodRef(mr.declaringClass(),
mr.name(),
l2,
- isGladiatorType(mr.returnType()) ? IntType.v() : mr.returnType(),
+ implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
mr.isStatic());
ie.setMethodRef(mr);
vb.setValue(v = ie);
} else if (v instanceof CastExpr) {
CastExpr ce = (CastExpr)v;
- if (isGladiatorType(ce.getCastType())) {
+ if (implementsGladiator(ce.getCastType())) {
SootClass mc = getParent(((RefType)ce.getCastType()).getSootClass());
SootClass ic = Scene.v().getSootClass("java.lang.Integer");
ce.setCastType(ic.getType());
- // FIXME deal with null
+
+ Local l1 = Jimple.v().newLocal("tmpRef" + (tfr++), ic.getType()); body.getLocals().add(l1);
+ Local l2 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v()); body.getLocals().add(l2);
+
+ Stmt s2 = Jimple.v().newAssignStmt(l1, Jimple.v().newCastExpr(ce.getOp(), ic.getType()));
+ body.getUnits().insertBefore(s2, s);
+
+ Stmt isNull = Jimple.v().newAssignStmt(l2, IntConstant.v(-1));
+ body.getUnits().insertAfter(isNull, s2);
+
+ Stmt ifStmt = Jimple.v().newIfStmt(Jimple.v().newEqExpr(l1, NullConstant.v()), s);
+ body.getUnits().insertAfter(ifStmt, isNull);
+
SootMethodRef mr = Scene.v().makeMethodRef(ic, "intValue", new LinkedList(), IntType.v(), false);
- InvokeExpr iie = Jimple.v().newVirtualInvokeExpr(viaLocal(ce, body, s),
- mr,
- new LinkedList());
- vb.setValue(viaLocal(iie, body, s));
- qq = 0;
+ Stmt isNotNull =
+ Jimple.v().newAssignStmt(l2, Jimple.v().newVirtualInvokeExpr(l1, mr, new LinkedList()));
+ body.getUnits().insertAfter(isNotNull, ifStmt);
+
+ vb.setValue(l2);
+ qq = 0; // ???
break;
}
InstanceFieldRef ifr = (InstanceFieldRef)v;
SootFieldRef fr = ifr.getFieldRef();
Type t = fr.type();
- if (isGladiatorFieldRef(fr)) {
- SootClass mc = getParent(fr.declaringClass());
- SootFieldRef sf = getGladiatorField(fr).makeRef();
- InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(thisLocal(mc, body), sf);
+ if (implementsGladiator(fr.declaringClass()) && fr.name().equals("this$0")) {
+ System.out.println("******************");
+ vb.setValue(body.getThisLocal());
System.out.println("s is " + s);
- ArrayRef ar = Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase());
- vb.setValue(ar);
+ } else if (implementsGladiator(fr.declaringClass())) {
+ SootClass mc = getParent(fr.declaringClass());
+ SootFieldRef sf = getSliceForField(fr).makeRef();
+ InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), sf);
+ vb.setValue(Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase()));
}
- if ((t instanceof RefType) && isGladiator(((RefType)t).getSootClass())) {
+ if ((t instanceof RefType) && implementsGladiator(((RefType)t).getSootClass())) {
SootClass tc = ((RefType)t).getSootClass();
SootClass mc = getParent(tc);
ifr.setFieldRef(Scene.v().makeFieldRef(mc, fr.name(), IntType.v(), false));
}
}
+
}
}
}