StringTokenizer st = new StringTokenizer(extDirs, File.pathSeparatorChar+"");
while(st.hasMoreTokens()) {
String goo = st.nextToken();
- System.out.println("goo " + goo);
File f = new File(goo);
if (!f.exists() || !f.isDirectory()) continue;
String[] jars = f.list();
args2[args.length + 6] = "-p";
args2[args.length + 7] = "jtp";
args2[args.length + 8] = "enabled:false";
- //args2[args.length + 9] = "-keep-line-number";
args2[args.length + 9] = "-allow-phantom-refs";
args2[args.length + 10] = "-allow-phantom-refs";
args2[args.length + 11] = "-cp";
soot.Main.main(args2);
}
- 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 = newLocal(b, v.getType());
- if (v instanceof IdentityRef) {
- b.getUnits().addFirst(Jimple.v().newIdentityStmt(l, v));
- } else {
- b.getUnits().addFirst(Jimple.v().newAssignStmt(l, v));
- }
- return l;
- }
- public static Local viaLocal(Value v, Body b, Unit u) {
- Local l = newLocal(b, v.getType());
- if (v instanceof IdentityRef) {
- b.getUnits().insertBefore(Jimple.v().newIdentityStmt(l, v), u);
- } else {
- b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, v), u);
- }
- return l;
- }
-
- HashMap map = new HashMap();
- HashSet size_added = new HashSet();
-
+ // The main loop //////////////////////////////////////////////////////////////////////////////
public void internalTransform(String phaseName, Map options) {
- System.out.println("begun");
init();
List ac = new LinkedList();
for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
}
for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
SootClass sc = (SootClass)it.next();
- if (implementsGladiator(sc)) {
- System.out.println(" application class: " + sc.getName());
- System.out.println(" application class: " + getArenaForGladiator(sc).getName());
- sc.setApplicationClass();
- getArenaForGladiator(sc).setApplicationClass();
- ac.add(sc);
- ac.add(getArenaForGladiator(sc));
- for(Iterator i2 = sc.getMethods().iterator(); i2.hasNext();) {
- SootMethod m = (SootMethod)i2.next();
- if (m.isConcrete()) m.retrieveActiveBody();
- }
- for(Iterator i2 = getArenaForGladiator(sc).getMethods().iterator(); i2.hasNext();) {
- SootMethod m = (SootMethod)i2.next();
- if (m.isConcrete()) m.retrieveActiveBody();
- }
- }
+ if (!implementsGladiator(sc)) continue;
+ System.out.println(" application class: " + sc.getName());
+ System.out.println(" application class: " + getArenaForGladiator(sc).getName());
+ sc.setApplicationClass();
+ getArenaForGladiator(sc).setApplicationClass();
+ ac.add(sc);
+ ac.add(getArenaForGladiator(sc));
}
for(Iterator it = ac.iterator(); it.hasNext();) {
SootClass sc = (SootClass)it.next();
- if (sc.isInterface()) continue;
- if (!implementsGladiator(sc)) continue;
- System.out.println("fixing Gladiator class: " + sc);
- fixClass(sc);
+ if (sc.isInterface() || !implementsGladiator(sc)) continue;
+ processGladiatorClass(sc);
}
for(Iterator it = ac.iterator(); it.hasNext();) {
SootClass sc = (SootClass)it.next();
if (sc.isInterface()) continue;
- System.out.println(" updating class: " + sc);
- nuke(sc);
+ processClass(sc);
}
}
- boolean implementsGladiator(SootClass c) { return c.implementsInterface("org.ibex.arenaj.Gladiator"); }
- boolean implementsGladiator(Type t) {return (t instanceof RefType)&&implementsGladiator(((RefType)t).getSootClass());}
- String getArenaName(SootClass sc) { return sc.getName().substring(0, sc.getName().lastIndexOf('$')); }
- String getGladiatorName(SootClass sc) { return sc.getName().substring(sc.getName().lastIndexOf('$')+1); }
- SootClass getArenaForGladiator(SootClass sc) { return Scene.v().getSootClass(getArenaName(sc)); }
- InstanceFieldRef newIFR(Body b, SootFieldRef fr) { return Jimple.v().newInstanceFieldRef(b.getThisLocal(), fr); }
- void assign(Body b, Value left, Value right) { b.getUnits().add(Jimple.v().newAssignStmt(left, right)); }
- void assign(Body b, Value l, Value r, Stmt w) { b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, r), w); }
- Type getSliceElementType(Type t) { return implementsGladiator(t) ? IntType.v() : t; }
- SootFieldRef getSliceForField(SootField f) { return getSliceForField(f.makeRef()); }
- SootFieldRef getSliceForField(SootFieldRef f) {
- return Scene.v().makeFieldRef(getArenaForGladiator(f.declaringClass()),
- getGladiatorName(f.declaringClass())+"$$"+f.name(),
- getSliceElementType(f.type()).makeArrayType(),
- false);
- }
+ // Initializers //////////////////////////////////////////////////////////////////////////////
public Type objectType;
public List arrayCopyTypes = new LinkedList();
true);
}
- //////////////////////////////////////////////////////////////////////////////
+ // Helpers //////////////////////////////////////////////////////////////////////////////
- public void fixClass(SootClass sc) {
- SootClass mc = getArenaForGladiator(sc);
- String subname = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1);
- String incFuncName = subname + "$$inc";
+ private int tfr = 0;
+ public Local newLocal(Body b, Type t) {
+ Local l = Jimple.v().newLocal("tmpRef" + (tfr++), t);
+ b.getLocals().add(l);
+ return l;
+ }
+
+ public Local viaLocal(Value v, Body b, Unit u) {
+ Local l = newLocal(b, v.getType());
+ if (v instanceof IdentityRef) b.getUnits().insertBefore(Jimple.v().newIdentityStmt(l, v), u);
+ else b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, v), u);
+ return l;
+ }
+
+ boolean implementsGladiator(SootClass c) { return c.implementsInterface("org.ibex.arenaj.Gladiator"); }
+ boolean implementsGladiator(Type t) {return (t instanceof RefType)&&implementsGladiator(((RefType)t).getSootClass());}
+ String getArenaName(SootClass sc) { return sc.getName().substring(0, sc.getName().lastIndexOf('$')); }
+ String getGladiatorName(SootClass sc) { return sc.getName().substring(sc.getName().lastIndexOf('$')+1); }
+ SootClass getArenaForGladiator(SootClass sc) { return Scene.v().getSootClass(getArenaName(sc)); }
+ InstanceFieldRef newIFR(Body b, SootFieldRef fr) { return Jimple.v().newInstanceFieldRef(b.getThisLocal(), fr); }
+ void assign(Body b, Value left, Value right) { b.getUnits().add(Jimple.v().newAssignStmt(left, right)); }
+ void assign(Body b, Value l, Value r, Stmt w) { b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, r), w); }
+ Type getSliceElementType(Type t) { return implementsGladiator(t) ? IntType.v() : t; }
+ SootFieldRef getSliceForField(SootField f) { return getSliceForField(f.makeRef()); }
+ SootFieldRef getSliceForField(SootFieldRef f) {
+ return Scene.v().makeFieldRef(getArenaForGladiator(f.declaringClass()),
+ getGladiatorName(f.declaringClass())+"$$"+f.name(),
+ getSliceElementType(f.type()).makeArrayType(),
+ false);
+ }
+
+
+ // Operations performed on the Gladiator class //////////////////////////////////////////////////////////////////////////
+
+ public SootMethod createIncMethod(SootClass sc) {
+ String incFuncName = getGladiatorName(sc) + "$$inc";
+ SootClass arena = getArenaForGladiator(sc);
SootMethod method = new SootMethod(incFuncName, new LinkedList(), IntType.v(), Modifier.PRIVATE, new LinkedList());
+ arena.addMethod(method);
+ Body incBody = Jimple.v().newBody(method);
+ method.setActiveBody(incBody);
+ ((JimpleBody)incBody).insertIdentityStmts();
+ return method;
+ }
+
+ public void processGladiatorClass(SootClass sc) {
+
+ // Set up the Arena zero-arg constructor
+
+ SootClass arena = getArenaForGladiator(sc);
+ SootMethod arenaInitMethod = null;
+ for(Iterator it = arena.getMethods().iterator(); it.hasNext();) {
+ SootMethod m = (SootMethod)it.next();
+ if (m.getName().equals("<init>")) {
+ if (arenaInitMethod != null) throw new Error("class " + arena.getName() + " has two constructors");
+ arenaInitMethod = m;
+ }
+ }
+ JimpleBody arenaInitBody = (JimpleBody)arenaInitMethod.getActiveBody();
+
+ SootField maxField = new SootField(getGladiatorName(sc) + "$$max", IntType.v());
+ arena.addField(maxField);
+ assign(arenaInitBody, newIFR(arenaInitBody, maxField.makeRef()), IntConstant.v(initialSize),
+ arenaInitBody.getFirstNonIdentityStmt());
+
+ SootField sfr = new SootField(getGladiatorName(sc) + "$$size", IntType.v());
+ arena.addField(sfr);
+ assign(arenaInitBody, newIFR(arenaInitBody, sfr.makeRef()), IntConstant.v(0),
+ arenaInitBody.getFirstNonIdentityStmt());
+
+ SootMethod incMethod = createIncMethod(sc);
+ Body incBody = incMethod.getActiveBody();
+
- mc.setApplicationClass();
- SootMethod initMethod = mc.getMethod("<init>", new LinkedList());
- if (!initMethod.hasActiveBody()) initMethod.setActiveBody((JimpleBody)Jimple.v().newBody(initMethod));
- Body mcInitBody = initMethod.getActiveBody();
-
- mc.addMethod(method);
- Body body = Jimple.v().newBody(method);
- method.setActiveBody(body);
- ((JimpleBody)body).insertIdentityStmts();
- Local l = newLocal(body, IntType.v());
- Local l2 = newLocal(body, IntType.v());
- Local l3 = newLocal(body, IntType.v());
- Local l4 = newLocal(body, IntType.v());
-
- Stmt where = ((JimpleBody)mcInitBody).getFirstNonIdentityStmt();
- SootField maxField = new SootField(subname + "$$max", IntType.v());
- mc.addField(maxField);
- assign(mcInitBody, newIFR(mcInitBody, maxField.makeRef()), IntConstant.v(initialSize), where);
-
- SootField sfr = new SootField(subname + "$$size", IntType.v());
- mc.addField(sfr);
- assign(mcInitBody, newIFR(mcInitBody, sfr.makeRef()), IntConstant.v(0), where);
-
- assign(body, l, newIFR(body, sfr.makeRef()));
- assign(body, l2, Jimple.v().newAddExpr(l, IntConstant.v(1)));
- assign(body, newIFR(body, sfr.makeRef()), l2);
- assign(body, l3, newIFR(body, maxField.makeRef()));
+ // Now build the $$inc method
+
+ Local l = newLocal(incBody, IntType.v());
+ Local l2 = newLocal(incBody, IntType.v());
+ Local l3 = newLocal(incBody, IntType.v());
+ Local l4 = newLocal(incBody, IntType.v());
+
+ assign(incBody, l, newIFR(incBody, sfr.makeRef()));
+ assign(incBody, l2, Jimple.v().newAddExpr(l, IntConstant.v(1)));
+ assign(incBody, newIFR(incBody, sfr.makeRef()), l2);
+ assign(incBody, l3, newIFR(incBody, maxField.makeRef()));
Stmt returnStmt = Jimple.v().newReturnStmt(l2);
- body.getUnits().add(Jimple.v().newIfStmt(Jimple.v().newLtExpr(l2, l3), returnStmt));
+ incBody.getUnits().add(Jimple.v().newIfStmt(Jimple.v().newLtExpr(l2, l3), returnStmt));
+
+ assign(incBody, l4, Jimple.v().newShlExpr(l3, IntConstant.v(1)));
+ assign(incBody, newIFR(incBody, maxField.makeRef()), l4);
+
- assign(body, l4, Jimple.v().newShlExpr(l3, IntConstant.v(1)));
- assign(body, newIFR(body, maxField.makeRef()), l4);
+ // Finally, iterate over the Gladiator's fields, updating the $$inc method and Arena's zero-arg constructor as we go
for(Iterator it = sc.getFields().iterator(); it.hasNext();) {
SootField f = (SootField)it.next();
Type t = getSliceElementType(f.getType());
- mc.addField(f = new SootField(getGladiatorName(sc) + "$$" + f.getName(), t.makeArrayType(), 0));
+ arena.addField(f = new SootField(getGladiatorName(sc) + "$$" + f.getName(), t.makeArrayType(), 0));
Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(initialSize));
- Local newArrLocal = newLocal(mcInitBody, f.getType());
- mcInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newIFR(mcInitBody, f.makeRef()), newArrLocal));
- mcInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
+ Local newArrLocal = newLocal(arenaInitBody, f.getType());
+ arenaInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newIFR(arenaInitBody, f.makeRef()), newArrLocal));
+ arenaInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
- Local ll0 = newLocal(body, f.getType());
- Local ll = newLocal(body, f.getType());
- assign(body, ll0, newIFR(body, f.makeRef()));
- assign(body, ll, Jimple.v().newNewArrayExpr(t, l4));
+ Local ll0 = newLocal(incBody, f.getType());
+ Local ll = newLocal(incBody, f.getType());
+ assign(incBody, ll0, newIFR(incBody, f.makeRef()));
+ assign(incBody, ll, Jimple.v().newNewArrayExpr(t, l4));
List args = new LinkedList();
args.add(ll0);
args.add(ll);
args.add(IntConstant.v(0));
args.add(l3);
- body.getUnits().add(Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(arrayCopy, args)));
- assign(body, newIFR(body, f.makeRef()), ll);
+ incBody.getUnits().add(Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(arrayCopy, args)));
+ assign(incBody, newIFR(incBody, f.makeRef()), ll);
}
for(Iterator it = sc.getMethods().iterator(); it.hasNext();) {
SootMethod m = (SootMethod)it.next();
- if (!m.isConcrete()) continue;
+ if (!m.isConcrete() || !m.hasActiveBody()) continue;
boolean doremove = true;
- Body mbody = m.getActiveBody();
+ Body mincBody = m.getActiveBody();
if (implementsGladiator(m.getDeclaringClass()) && m.getName().equals("<init>")) {
+ System.out.println("processing ctor " + sc.getName() + "." + m.getSignature());
doremove = false;
SootClass c = m.getDeclaringClass();
- String name = /*c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + */"$init";
+ String name = "$init";
List li = m.getParameterTypes();
c.removeMethod(m);
SootMethod nm = new SootMethod(name, li, implementsGladiator(m.getReturnType()) ? IntType.v() : m.getReturnType());
bod.importBodyContentsFrom(m.getActiveBody());
nm.setActiveBody(bod);
m = nm;
- mbody = bod;
- mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
- mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
+ mincBody = bod;
+
+ for(Iterator it2 = mincBody.getUnits().snapshotIterator(); it2.hasNext(); ) {
+ Unit u = (Unit)it2.next();
+ if (u instanceof DefinitionStmt) {
+ DefinitionStmt ds = (DefinitionStmt)u;
+ if (ds.getLeftOp() instanceof ThisRef)
+ mincBody.getUnits().remove(u);
+ else if (ds.getLeftOp() instanceof FieldRef) {
+ if (((FieldRef)ds.getLeftOp()).getFieldRef().name().endsWith("this$0"))
+ mincBody.getUnits().remove(u);
+ }
+ } else if (u instanceof InvokeStmt) {
+ InvokeExpr ie = ((InvokeStmt)u).getInvokeExpr();
+ SootMethodRef meth = ie.getMethodRef();
+ if (meth.declaringClass().getName().equals("java.lang.Object") && meth.name().equals("<init>"))
+ mincBody.getUnits().remove(u);
+ }
+ }
+
} else {
System.out.println("examining " + sc.getName() + "." + m.getSignature());
m.retrieveActiveBody();
List list = new LinkedList();
list.addAll(m.getParameterTypes());
list.add(IntType.v());
- for(Iterator i = list.iterator(); i.hasNext();) System.out.println(i.next());
SootMethod m2 = new SootMethod(name, list, m.getReturnType());
getArenaForGladiator(sc).addMethod(m2);
JimpleBody ab = (JimpleBody)Jimple.v().newBody(m2);
- ab.importBodyContentsFrom(mbody);
+ ab.importBodyContentsFrom(mincBody);
m2.setActiveBody(ab);
Local loc = Jimple.v().newLocal("tmpRef" + (tfr++), getArenaForGladiator(sc).getType());
ab.getLocals().add(loc);
}
*/
if (vb.getValue() instanceof ThisRef) {
- System.out.println(s);
vb.setValue(loc);
}
}
if (doremove) sc.removeMethod(m);
}
- body.getUnits().add(returnStmt);
+ incBody.getUnits().add(returnStmt);
}
- public void nuke(SootClass c) {
+ // Operations performed on all classes ////////////////////////////////////////////////////////////////////////////
+
+ public void processClass(SootClass c) {
for(Iterator it = c.getFields().iterator(); it.hasNext();) {
SootField f = (SootField)it.next();
Type t = f.getType();
SootMethod m = (SootMethod)it.next();
Body b = null;
if (m.getName().endsWith("$$inc")) continue;
- if (m.hasActiveBody()) b = fixBody(m.getActiveBody(), c, m);
+ if (m.hasActiveBody()) b = processBody(m.getActiveBody(), c, m);
List l2 = new LinkedList();
List l = m.getParameterTypes();
for(Iterator it2 = l.iterator(); it2.hasNext();) {
}
- protected Body fixBody(Body body, SootClass ownerClass, SootMethod smeth) {
+ protected Body processBody(Body body, SootClass ownerClass, SootMethod smeth) {
if (body instanceof JimpleBody) {
JimpleBody b2 = Jimple.v().newBody(smeth);
b2.importBodyContentsFrom(body);
body = b2;
}
Chain units = body.getUnits();
- System.out.println("fixBody("+body.getMethod()+")");
for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) {
Local l = (Local)it.next();
if (implementsGladiator(l.getType())) l.setType(IntType.v());
}
+ if (!smeth.isStatic())
+ body.getThisLocal().setType(ownerClass.getType());
for(int qq=0; qq<2; qq++) for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
Stmt s = (Stmt) stmtIt.next();
if (s instanceof DefinitionStmt) {
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();) {
+ List l2l = new LinkedList();
+ l2l.addAll(l);
+ for(Iterator it = l2l.iterator(); it.hasNext();) {
Object o = it.next();
if (o instanceof ValueBox) {
ValueBox vb = (ValueBox)o;
NewExpr ne = (NewExpr)v;
if (implementsGladiator(ne.getBaseType())) {
SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
- SootClass mc = getArenaForGladiator(sc);
+ SootClass arena = getArenaForGladiator(sc);
String incFuncName = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$inc";
- SootMethodRef smr = Scene.v().makeMethodRef(mc, incFuncName, new LinkedList(), IntType.v(), false);
+ SootMethodRef smr = Scene.v().makeMethodRef(arena, incFuncName, new LinkedList(), IntType.v(), false);
Expr invokeExpr = Jimple.v().newSpecialInvokeExpr(body.getThisLocal(), smr);
Local ll = viaLocal(invokeExpr, body, s);
vb.setValue(ll);
v = ll;
- continue;
+ qq = 0;
+ break;
}
} else
//pl.remove(0);
}
pl.add(IntType.v());
- li.add(viaLocal(iie.getBase(),body,s));
+ li.add(/*viaLocal(*/iie.getBase()/*,body,s)*/);
SootClass sc = mr.declaringClass();
name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + name;
mr = Scene.v().makeMethodRef(getArenaForGladiator(sc),
} else if (v instanceof CastExpr) {
CastExpr ce = (CastExpr)v;
if (implementsGladiator(ce.getCastType())) {
- SootClass mc = getArenaForGladiator(((RefType)ce.getCastType()).getSootClass());
+ SootClass arena = getArenaForGladiator(((RefType)ce.getCastType()).getSootClass());
SootClass ic = Scene.v().getSootClass("java.lang.Integer");
ce.setCastType(ic.getType());
break;
}
- } else if (v instanceof InstanceFieldRef) {
- InstanceFieldRef ifr = (InstanceFieldRef)v;
+ } else if (v instanceof FieldRef) {
+ FieldRef ifr = (FieldRef)v;
SootFieldRef fr = ifr.getFieldRef();
Type t = fr.type();
if (implementsGladiator(fr.declaringClass()) && fr.name().equals("this$0")) {
- System.out.println("******************");
vb.setValue(body.getThisLocal());
- System.out.println("s is " + s);
} else if (implementsGladiator(fr.declaringClass())) {
- SootClass mc = getArenaForGladiator(fr.declaringClass());
- InstanceFieldRef sfr = newIFR(body, getSliceForField(fr));
- vb.setValue(Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase()));
+ SootClass arena = getArenaForGladiator(fr.declaringClass());
+ if (fr.isStatic()) {
+ vb.setValue(newIFR(body, getSliceForField(fr)));
+ } else {
+ InstanceFieldRef sfr = newIFR(body, getSliceForField(fr));
+ vb.setValue(Jimple.v().newArrayRef(viaLocal(sfr, body, s), ((InstanceFieldRef)ifr).getBase()));
+ }
}
if ((t instanceof RefType) && implementsGladiator(((RefType)t).getSootClass())) {
SootClass tc = ((RefType)t).getSootClass();
- SootClass mc = getArenaForGladiator(tc);
- ifr.setFieldRef(Scene.v().makeFieldRef(mc, fr.name(), IntType.v(), false));
+ SootClass arena = getArenaForGladiator(tc);
+ ifr.setFieldRef(Scene.v().makeFieldRef(arena, fr.name(), IntType.v(), fr.isStatic()));
+ } else if (t instanceof ArrayType) {
+ ArrayType at = (ArrayType)t;
+ Type et = at.getElementType();
+ if (et instanceof RefType && implementsGladiator(((RefType)et).getSootClass()))
+ ifr.setFieldRef(Scene.v().makeFieldRef(fr.declaringClass(),
+ fr.name(),
+ IntType.v().makeArrayType(),
+ fr.isStatic()));
}
}