1 package org.ibex.arenaj;
8 public class Main extends SceneTransformer {
10 public static final int initialSize = 1000;
12 private static Main instance = new Main();
14 public static Main v() { return instance; }
16 public static void main(String[] args) throws Exception {
17 if(args.length == 0) {
18 System.out.println("Syntax: java " + v().getClass().getName() + " [soot options]");
21 PackManager.v().getPack("wjtp").add(new Transform("wjtp.tx", Main.v()));
22 String[] args2 = new String[args.length + 14];
23 System.arraycopy(args, 0, args2, 0, args.length-1);
25 System.getProperty("java.class.path") +
27 System.getProperty("sun.boot.class.path");
28 String extDirs = System.getProperty("java.ext.dirs");
29 if (extDirs != null) {
30 StringTokenizer st = new StringTokenizer(extDirs, File.pathSeparatorChar+"");
31 while(st.hasMoreTokens()) {
32 String goo = st.nextToken();
33 File f = new File(goo);
34 if (!f.exists() || !f.isDirectory()) continue;
35 String[] jars = f.list();
36 for(int i=0; i<jars.length; i++)
37 sootcp += File.pathSeparator + f.getPath() + File.separatorChar + jars[i];
40 args2[args.length - 1] = "-w";
41 args2[args.length + 0] = "-p";
42 args2[args.length + 1] = "cg";
43 args2[args.length + 2] = "enabled:false";
44 args2[args.length + 3] = "-p";
45 args2[args.length + 4] = "wjap";
46 args2[args.length + 5] = "enabled:false";
47 args2[args.length + 6] = "-p";
48 args2[args.length + 7] = "jtp";
49 args2[args.length + 8] = "enabled:false";
50 args2[args.length + 9] = "-allow-phantom-refs";
51 args2[args.length + 10] = "-allow-phantom-refs";
52 args2[args.length + 11] = "-cp";
53 args2[args.length + 12] = sootcp;
54 System.out.println("sootcp => " + sootcp);
55 args2[args.length + 13] = args[args.length-1];
56 for(int i=0; i<args2.length; i++) System.out.println("args2["+i+"] = " + args2[i]);
57 soot.Main.main(args2);
60 // The main loop //////////////////////////////////////////////////////////////////////////////
62 public void internalTransform(String phaseName, Map options) {
64 List ac = new LinkedList();
65 for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
66 SootClass sc = (SootClass)it.next();
69 for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
70 SootClass sc = (SootClass)it.next();
71 if (!implementsGladiator(sc)) continue;
72 System.out.println(" application class: " + sc.getName());
73 System.out.println(" application class: " + getArenaForGladiator(sc).getName());
74 sc.setApplicationClass();
75 getArenaForGladiator(sc).setApplicationClass();
77 ac.add(getArenaForGladiator(sc));
79 for(Iterator it = ac.iterator(); it.hasNext();) {
80 SootClass sc = (SootClass)it.next();
81 if (sc.isInterface() || !implementsGladiator(sc)) continue;
82 processGladiatorClass(sc);
84 for(Iterator it = ac.iterator(); it.hasNext();) {
85 SootClass sc = (SootClass)it.next();
86 if (sc.isInterface()) continue;
91 // Initializers //////////////////////////////////////////////////////////////////////////////
93 public Type objectType;
94 public List arrayCopyTypes = new LinkedList();
95 public SootMethodRef arrayCopy;
97 objectType = Scene.v().getSootClass("java.lang.Object").getType();
98 arrayCopyTypes.add(objectType);
99 arrayCopyTypes.add(IntType.v());
100 arrayCopyTypes.add(objectType);
101 arrayCopyTypes.add(IntType.v());
102 arrayCopyTypes.add(IntType.v());
104 Scene.v().makeMethodRef(Scene.v().getSootClass("java.lang.System"),
111 // Helpers //////////////////////////////////////////////////////////////////////////////
114 public Local newLocal(Body b, Type t) {
115 Local l = Jimple.v().newLocal("tmpRef" + (tfr++), t);
116 b.getLocals().add(l);
120 public Local viaLocal(Value v, Body b, Unit u) {
121 Local l = newLocal(b, v.getType());
122 if (v instanceof IdentityRef) b.getUnits().insertBefore(Jimple.v().newIdentityStmt(l, v), u);
123 else b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, v), u);
127 boolean implementsGladiator(SootClass c) { return c.implementsInterface("org.ibex.arenaj.Gladiator"); }
128 boolean implementsGladiator(Type t) {return (t instanceof RefType)&&implementsGladiator(((RefType)t).getSootClass());}
129 String getArenaName(SootClass sc) { return sc.getName().substring(0, sc.getName().lastIndexOf('$')); }
130 String getGladiatorName(SootClass sc) { return sc.getName().substring(sc.getName().lastIndexOf('$')+1); }
131 SootClass getArenaForGladiator(SootClass sc) { return Scene.v().getSootClass(getArenaName(sc)); }
132 InstanceFieldRef newIFR(Body b, SootFieldRef fr) { return Jimple.v().newInstanceFieldRef(b.getThisLocal(), fr); }
133 void assign(Body b, Value left, Value right) { b.getUnits().add(Jimple.v().newAssignStmt(left, right)); }
134 void assign(Body b, Value l, Value r, Stmt w) { b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, r), w); }
135 Type getSliceElementType(Type t) { return implementsGladiator(t) ? IntType.v() : t; }
136 SootFieldRef getSliceForField(SootField f) { return getSliceForField(f.makeRef()); }
137 SootFieldRef getSliceForField(SootFieldRef f) {
138 return Scene.v().makeFieldRef(getArenaForGladiator(f.declaringClass()),
139 getGladiatorName(f.declaringClass())+"$$"+f.name(),
140 getSliceElementType(f.type()).makeArrayType(),
145 // Operations performed on the Gladiator class //////////////////////////////////////////////////////////////////////////
147 public SootMethod createIncMethod(SootClass sc) {
148 String incFuncName = getGladiatorName(sc) + "$$inc";
149 SootClass arena = getArenaForGladiator(sc);
150 SootMethod method = new SootMethod(incFuncName, new LinkedList(), IntType.v(), Modifier.PRIVATE, new LinkedList());
151 arena.addMethod(method);
152 Body incBody = Jimple.v().newBody(method);
153 method.setActiveBody(incBody);
154 ((JimpleBody)incBody).insertIdentityStmts();
158 public void processGladiatorClass(SootClass sc) {
160 // Set up the Arena zero-arg constructor
162 SootClass arena = getArenaForGladiator(sc);
163 SootMethod arenaInitMethod = null;
164 for(Iterator it = arena.getMethods().iterator(); it.hasNext();) {
165 SootMethod m = (SootMethod)it.next();
166 if (m.getName().equals("<init>")) {
167 if (arenaInitMethod != null) throw new Error("class " + arena.getName() + " has two constructors");
171 JimpleBody arenaInitBody = (JimpleBody)arenaInitMethod.getActiveBody();
173 SootField maxField = new SootField(getGladiatorName(sc) + "$$max", IntType.v());
174 arena.addField(maxField);
175 assign(arenaInitBody, newIFR(arenaInitBody, maxField.makeRef()), IntConstant.v(initialSize),
176 arenaInitBody.getFirstNonIdentityStmt());
178 SootField sfr = new SootField(getGladiatorName(sc) + "$$size", IntType.v());
180 assign(arenaInitBody, newIFR(arenaInitBody, sfr.makeRef()), IntConstant.v(0),
181 arenaInitBody.getFirstNonIdentityStmt());
183 SootMethod incMethod = createIncMethod(sc);
184 Body incBody = incMethod.getActiveBody();
187 // Now build the $$inc method
189 Local l = newLocal(incBody, IntType.v());
190 Local l2 = newLocal(incBody, IntType.v());
191 Local l3 = newLocal(incBody, IntType.v());
192 Local l4 = newLocal(incBody, IntType.v());
194 assign(incBody, l, newIFR(incBody, sfr.makeRef()));
195 assign(incBody, l2, Jimple.v().newAddExpr(l, IntConstant.v(1)));
196 assign(incBody, newIFR(incBody, sfr.makeRef()), l2);
197 assign(incBody, l3, newIFR(incBody, maxField.makeRef()));
199 Stmt returnStmt = Jimple.v().newReturnStmt(l2);
200 incBody.getUnits().add(Jimple.v().newIfStmt(Jimple.v().newLtExpr(l2, l3), returnStmt));
202 assign(incBody, l4, Jimple.v().newShlExpr(l3, IntConstant.v(1)));
203 assign(incBody, newIFR(incBody, maxField.makeRef()), l4);
206 // Finally, iterate over the Gladiator's fields, updating the $$inc method and Arena's zero-arg constructor as we go
208 for(Iterator it = sc.getFields().iterator(); it.hasNext();) {
209 SootField f = (SootField)it.next();
210 Type t = getSliceElementType(f.getType());
211 arena.addField(f = new SootField(getGladiatorName(sc) + "$$" + f.getName(), t.makeArrayType(), 0));
213 Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(initialSize));
214 Local newArrLocal = newLocal(arenaInitBody, f.getType());
215 arenaInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newIFR(arenaInitBody, f.makeRef()), newArrLocal));
216 arenaInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
218 Local ll0 = newLocal(incBody, f.getType());
219 Local ll = newLocal(incBody, f.getType());
220 assign(incBody, ll0, newIFR(incBody, f.makeRef()));
221 assign(incBody, ll, Jimple.v().newNewArrayExpr(t, l4));
223 List args = new LinkedList();
225 args.add(IntConstant.v(0));
227 args.add(IntConstant.v(0));
229 incBody.getUnits().add(Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(arrayCopy, args)));
230 assign(incBody, newIFR(incBody, f.makeRef()), ll);
233 for(Iterator it = sc.getMethods().iterator(); it.hasNext();) {
234 SootMethod m = (SootMethod)it.next();
235 if (!m.isConcrete()) continue;
236 boolean doremove = true;
237 Body mincBody = m.getActiveBody();
238 if (implementsGladiator(m.getDeclaringClass()) && m.getName().equals("<init>")) {
239 System.out.println("processing ctor " + sc.getName() + "." + m.getSignature());
241 SootClass c = m.getDeclaringClass();
242 String name = "$init";
243 List li = m.getParameterTypes();
245 SootMethod nm = new SootMethod(name, li, implementsGladiator(m.getReturnType()) ? IntType.v() : m.getReturnType());
246 JimpleBody bod = Jimple.v().newBody(nm);
247 bod.importBodyContentsFrom(m.getActiveBody());
248 nm.setActiveBody(bod);
252 for(Iterator it2 = mincBody.getUnits().snapshotIterator(); it2.hasNext(); ) {
253 Unit u = (Unit)it2.next();
254 if (u instanceof DefinitionStmt) {
255 DefinitionStmt ds = (DefinitionStmt)u;
256 if (ds.getLeftOp() instanceof ThisRef)
257 mincBody.getUnits().remove(u);
258 else if (ds.getLeftOp() instanceof FieldRef) {
259 if (((FieldRef)ds.getLeftOp()).getFieldRef().name().endsWith("this$0"))
260 mincBody.getUnits().remove(u);
262 } else if (u instanceof InvokeStmt) {
263 InvokeExpr ie = ((InvokeStmt)u).getInvokeExpr();
264 SootMethod meth = ie.getMethod();
265 System.out.println(meth.getName());
266 if (meth.getDeclaringClass().getName().equals("java.lang.Object") && meth.getName().equals("<init>"))
267 mincBody.getUnits().remove(u);
272 System.out.println("examining " + sc.getName() + "." + m.getSignature());
273 m.retrieveActiveBody();
275 if (m.isStatic()) continue;
277 String name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + m.getName();
278 List list = new LinkedList();
279 list.addAll(m.getParameterTypes());
280 list.add(IntType.v());
281 SootMethod m2 = new SootMethod(name, list, m.getReturnType());
282 getArenaForGladiator(sc).addMethod(m2);
284 JimpleBody ab = (JimpleBody)Jimple.v().newBody(m2);
285 ab.importBodyContentsFrom(mincBody);
286 m2.setActiveBody(ab);
287 Local loc = Jimple.v().newLocal("tmpRef" + (tfr++), getArenaForGladiator(sc).getType());
288 ab.getLocals().add(loc);
289 // FIXME: insert assignment to this
291 for(Iterator z = ab.getLocals().iterator(); z.hasNext();) {
292 Local loc2 = (Local)z.next();
293 if (implementsGladiator(loc2.getType())) {
294 loc2.setType(IntType.v());
298 Chain units = ab.getUnits();
299 boolean touched = false;
300 Local loc0 = Jimple.v().newLocal("tmpRef" + (tfr++), getArenaForGladiator(sc).getType());
301 ab.getLocals().add(loc0);
302 for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
303 Stmt s = (Stmt) stmtIt.next();
304 if (s instanceof IdentityStmt) {
305 IdentityStmt is = (IdentityStmt)s;
306 Local left = (Local)is.getLeftOp();
307 if (is.getRightOp() instanceof ThisRef) {
308 left.setType(IntType.v());
309 is.getRightOpBox().setValue(Jimple.v().newParameterRef(IntType.v(), m.getParameterCount()));
311 units.addFirst(Jimple.v().newIdentityStmt(loc0, Jimple.v().newThisRef(getArenaForGladiator(sc).getType())));
317 for(Iterator i = s.getUseAndDefBoxes().iterator(); i.hasNext();) {
319 if (o instanceof ValueBox) {
320 ValueBox vb = (ValueBox)o;
323 if (o instanceof Local && implementsGladiator(((Local)o).getType())) {
324 System.out.println("thunking");
328 if (vb.getValue() instanceof ThisRef) {
336 if (doremove) sc.removeMethod(m);
339 incBody.getUnits().add(returnStmt);
342 // Operations performed on all classes ////////////////////////////////////////////////////////////////////////////
344 public void processClass(SootClass c) {
345 for(Iterator it = c.getFields().iterator(); it.hasNext();) {
346 SootField f = (SootField)it.next();
347 Type t = f.getType();
348 if (t instanceof RefType) {
349 RefType rt = (RefType)t;
350 SootClass sc = rt.getSootClass();
351 if (implementsGladiator(sc)) f.setType(IntType.v());
352 } else if (t instanceof ArrayType) {
353 ArrayType at = (ArrayType)t;
354 t = at.getElementType();
355 if (!(t instanceof RefType)) continue;
356 RefType rt = (RefType)t;
357 SootClass sc = rt.getSootClass();
358 if (implementsGladiator(sc)) f.setType(IntType.v().makeArrayType());
362 List list = new LinkedList(); list.addAll(c.getMethods());
363 for(Iterator it = list.iterator(); it.hasNext();) {
364 SootMethod m = (SootMethod)it.next();
366 if (m.getName().endsWith("$$inc")) continue;
367 if (m.hasActiveBody()) b = processBody(m.getActiveBody(), c, m);
368 List l2 = new LinkedList();
369 List l = m.getParameterTypes();
370 for(Iterator it2 = l.iterator(); it2.hasNext();) {
371 Type t = (Type)it2.next();
372 l2.add(implementsGladiator(t) ? IntType.v() : t);
374 Type t = m.getReturnType();
375 if (implementsGladiator(t)) {
377 if (m.hasActiveBody()) {
378 Body bod = m.getActiveBody();
379 for(Iterator stmtIt = bod.getUnits().snapshotIterator(); stmtIt.hasNext();) {
380 Stmt s = (Stmt) stmtIt.next();
381 if (s instanceof ReturnStmt) {
382 if (((ReturnStmt)s).getOp().getType() instanceof NullType) {
383 ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
389 String name = m.getName();
390 SootMethod meth = new SootMethod(name, l2, implementsGladiator(t) ? IntType.v() : t, m.getModifiers());
392 JimpleBody b2 = Jimple.v().newBody(meth);
393 b2.importBodyContentsFrom(b);
394 meth.setActiveBody(b2);
402 protected Body processBody(Body body, SootClass ownerClass, SootMethod smeth) {
403 if (body instanceof JimpleBody) {
404 JimpleBody b2 = Jimple.v().newBody(smeth);
405 b2.importBodyContentsFrom(body);
408 Chain units = body.getUnits();
409 for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) {
410 Local l = (Local)it.next();
411 if (implementsGladiator(l.getType())) l.setType(IntType.v());
413 for(int qq=0; qq<2; qq++) for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
414 Stmt s = (Stmt) stmtIt.next();
415 if (s instanceof DefinitionStmt) {
416 DefinitionStmt ds = (DefinitionStmt)s;
417 if (ds.getLeftOp().getType() instanceof PrimType && ds.getRightOp().getType() instanceof NullType) {
418 ds.getRightOpBox().setValue(IntConstant.v(-1));
421 if (implementsGladiator(smeth.getReturnType()) && s instanceof ReturnStmt)
422 if (((ReturnStmt)s).getOp().getType() instanceof NullType)
423 ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
424 List l = s.getUseAndDefBoxes();
425 for(Iterator it = l.iterator(); it.hasNext();) {
426 Object o = it.next();
427 if (o instanceof ValueBox) {
428 ValueBox vb = (ValueBox)o;
429 Value v = vb.getValue();
431 if (v instanceof BinopExpr) {
432 BinopExpr boe = (BinopExpr)v;
433 Type t1 = boe.getOp1().getType();
434 Type t2 = boe.getOp2().getType();
435 if (t1 instanceof PrimType && t2 instanceof NullType) boe.setOp2(IntConstant.v(-1));
436 if (t2 instanceof PrimType && t1 instanceof NullType) boe.setOp1(IntConstant.v(-1));
439 if (v instanceof NewExpr) {
440 NewExpr ne = (NewExpr)v;
441 if (implementsGladiator(ne.getBaseType())) {
442 SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
443 SootClass arena = getArenaForGladiator(sc);
444 String incFuncName = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$inc";
445 SootMethodRef smr = Scene.v().makeMethodRef(arena, incFuncName, new LinkedList(), IntType.v(), false);
446 Expr invokeExpr = Jimple.v().newSpecialInvokeExpr(body.getThisLocal(), smr);
447 Local ll = viaLocal(invokeExpr, body, s);
454 if (v instanceof InvokeExpr) {
455 InvokeExpr ie = (InvokeExpr)v;
456 SootMethodRef mr = ie.getMethodRef();
457 String name = mr.name();
458 if (v instanceof InstanceInvokeExpr && implementsGladiator(mr.declaringClass())) {
459 InstanceInvokeExpr iie = (InstanceInvokeExpr)v;
460 List li = new LinkedList();
461 li.addAll(iie.getArgs());
462 LinkedList pl = new LinkedList();
463 for(Iterator it2 = mr.parameterTypes().iterator(); it2.hasNext();) {
464 Type t = (Type)it2.next();
465 pl.add(implementsGladiator(t) ? IntType.v() : t);
467 if (mr.name().equals("<init>") && implementsGladiator(mr.declaringClass())) {
473 li.add(viaLocal(iie.getBase(),body,s));
474 SootClass sc = mr.declaringClass();
475 name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + name;
476 mr = Scene.v().makeMethodRef(getArenaForGladiator(sc),
479 implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
481 ie = Jimple.v().newVirtualInvokeExpr(body.getThisLocal(), mr, li);
484 } else if (!(v instanceof StaticInvokeExpr)) {
485 List l0 = mr.parameterTypes();
486 List l2 = new LinkedList();
487 for(Iterator it2 = l0.iterator(); it2.hasNext();) {
488 Type t = (Type)it2.next();
489 l2.add(implementsGladiator(t) ? IntType.v() : t);
491 mr = Scene.v().makeMethodRef(mr.declaringClass(),
494 implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
500 for(int i=0; i<ie.getArgCount(); i++) {
501 ValueBox b = ie.getArgBox(i);
502 Value val = b.getValue();
503 if (mr.parameterType(i) instanceof RefType && val.getType() instanceof PrimType) {
504 SootClass intClass = Scene.v().getSootClass("java.lang.Integer");
505 List typelist = new LinkedList();
506 typelist.add(IntType.v());
507 SootMethod intMethod = intClass.getMethod("<init>", typelist);
508 Local loc = viaLocal(Jimple.v().newNewExpr(RefType.v(intClass)), body, s);
509 List list = new LinkedList();
511 units.insertBefore(Jimple.v().newInvokeStmt(Jimple.v().newSpecialInvokeExpr(loc,
517 if (val != null && val.getType() instanceof NullType && mr.parameterType(i) instanceof IntType) {
518 b.setValue(IntConstant.v(-1));
523 } else if (v instanceof CastExpr) {
524 CastExpr ce = (CastExpr)v;
525 if (implementsGladiator(ce.getCastType())) {
526 SootClass arena = getArenaForGladiator(((RefType)ce.getCastType()).getSootClass());
527 SootClass ic = Scene.v().getSootClass("java.lang.Integer");
528 ce.setCastType(ic.getType());
530 Local l1 = Jimple.v().newLocal("tmpRef" + (tfr++), ic.getType()); body.getLocals().add(l1);
531 Local l2 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v()); body.getLocals().add(l2);
533 Stmt s2 = Jimple.v().newAssignStmt(l1, Jimple.v().newCastExpr(ce.getOp(), ic.getType()));
534 body.getUnits().insertBefore(s2, s);
536 Stmt isNull = Jimple.v().newAssignStmt(l2, IntConstant.v(-1));
537 body.getUnits().insertAfter(isNull, s2);
539 Stmt ifStmt = Jimple.v().newIfStmt(Jimple.v().newEqExpr(l1, NullConstant.v()), s);
540 body.getUnits().insertAfter(ifStmt, isNull);
542 SootMethodRef mr = Scene.v().makeMethodRef(ic, "intValue", new LinkedList(), IntType.v(), false);
544 Jimple.v().newAssignStmt(l2, Jimple.v().newVirtualInvokeExpr(l1, mr, new LinkedList()));
545 body.getUnits().insertAfter(isNotNull, ifStmt);
552 } else if (v instanceof InstanceFieldRef) {
553 InstanceFieldRef ifr = (InstanceFieldRef)v;
554 SootFieldRef fr = ifr.getFieldRef();
556 if (implementsGladiator(fr.declaringClass()) && fr.name().equals("this$0")) {
557 vb.setValue(body.getThisLocal());
558 } else if (implementsGladiator(fr.declaringClass())) {
559 SootClass arena = getArenaForGladiator(fr.declaringClass());
560 InstanceFieldRef sfr = newIFR(body, getSliceForField(fr));
561 vb.setValue(Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase()));
563 if ((t instanceof RefType) && implementsGladiator(((RefType)t).getSootClass())) {
564 SootClass tc = ((RefType)t).getSootClass();
565 SootClass arena = getArenaForGladiator(tc);
566 ifr.setFieldRef(Scene.v().makeFieldRef(arena, fr.name(), IntType.v(), false));