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 System.out.println("goo " + goo);
34 File f = new File(goo);
35 if (!f.exists() || !f.isDirectory()) continue;
36 String[] jars = f.list();
37 for(int i=0; i<jars.length; i++)
38 sootcp += File.pathSeparator + f.getPath() + File.separatorChar + jars[i];
41 args2[args.length - 1] = "-w";
42 args2[args.length + 0] = "-p";
43 args2[args.length + 1] = "cg";
44 args2[args.length + 2] = "enabled:false";
45 args2[args.length + 3] = "-p";
46 args2[args.length + 4] = "wjap";
47 args2[args.length + 5] = "enabled:false";
48 args2[args.length + 6] = "-p";
49 args2[args.length + 7] = "jtp";
50 args2[args.length + 8] = "enabled:false";
51 args2[args.length + 9] = "-allow-phantom-refs";
52 args2[args.length + 10] = "-allow-phantom-refs";
53 args2[args.length + 11] = "-cp";
54 args2[args.length + 12] = sootcp;
55 System.out.println("sootcp => " + sootcp);
56 args2[args.length + 13] = args[args.length-1];
57 for(int i=0; i<args2.length; i++) System.out.println("args2["+i+"] = " + args2[i]);
58 soot.Main.main(args2);
61 // The main loop //////////////////////////////////////////////////////////////////////////////
63 public void internalTransform(String phaseName, Map options) {
65 List ac = new LinkedList();
66 for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
67 SootClass sc = (SootClass)it.next();
70 for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
71 SootClass sc = (SootClass)it.next();
72 if (!implementsGladiator(sc)) continue;
73 System.out.println(" application class: " + sc.getName());
74 System.out.println(" application class: " + getArenaForGladiator(sc).getName());
75 sc.setApplicationClass();
76 getArenaForGladiator(sc).setApplicationClass();
78 ac.add(getArenaForGladiator(sc));
80 for(Iterator it = ac.iterator(); it.hasNext();) {
81 SootClass sc = (SootClass)it.next();
82 if (sc.isInterface() || !implementsGladiator(sc)) continue;
83 processGladiatorClass(sc);
85 for(Iterator it = ac.iterator(); it.hasNext();) {
86 SootClass sc = (SootClass)it.next();
87 if (sc.isInterface()) continue;
92 // Initializers //////////////////////////////////////////////////////////////////////////////
94 public Type objectType;
95 public List arrayCopyTypes = new LinkedList();
96 public SootMethodRef arrayCopy;
98 objectType = Scene.v().getSootClass("java.lang.Object").getType();
99 arrayCopyTypes.add(objectType);
100 arrayCopyTypes.add(IntType.v());
101 arrayCopyTypes.add(objectType);
102 arrayCopyTypes.add(IntType.v());
103 arrayCopyTypes.add(IntType.v());
105 Scene.v().makeMethodRef(Scene.v().getSootClass("java.lang.System"),
112 // Helpers //////////////////////////////////////////////////////////////////////////////
115 public Local newLocal(Body b, Type t) {
116 Local l = Jimple.v().newLocal("tmpRef" + (tfr++), t);
117 b.getLocals().add(l);
121 public Local viaLocal(Value v, Body b, Unit u) {
122 Local l = newLocal(b, v.getType());
123 if (v instanceof IdentityRef) b.getUnits().insertBefore(Jimple.v().newIdentityStmt(l, v), u);
124 else b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, v), u);
128 boolean implementsGladiator(SootClass c) { return c.implementsInterface("org.ibex.arenaj.Gladiator"); }
129 boolean implementsGladiator(Type t) {return (t instanceof RefType)&&implementsGladiator(((RefType)t).getSootClass());}
130 String getArenaName(SootClass sc) { return sc.getName().substring(0, sc.getName().lastIndexOf('$')); }
131 String getGladiatorName(SootClass sc) { return sc.getName().substring(sc.getName().lastIndexOf('$')+1); }
132 SootClass getArenaForGladiator(SootClass sc) { return Scene.v().getSootClass(getArenaName(sc)); }
133 InstanceFieldRef newIFR(Body b, SootFieldRef fr) { return Jimple.v().newInstanceFieldRef(b.getThisLocal(), fr); }
134 void assign(Body b, Value left, Value right) { b.getUnits().add(Jimple.v().newAssignStmt(left, right)); }
135 void assign(Body b, Value l, Value r, Stmt w) { b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, r), w); }
136 Type getSliceElementType(Type t) { return implementsGladiator(t) ? IntType.v() : t; }
137 SootFieldRef getSliceForField(SootField f) { return getSliceForField(f.makeRef()); }
138 SootFieldRef getSliceForField(SootFieldRef f) {
139 return Scene.v().makeFieldRef(getArenaForGladiator(f.declaringClass()),
140 getGladiatorName(f.declaringClass())+"$$"+f.name(),
141 getSliceElementType(f.type()).makeArrayType(),
146 // Operations performed on the Gladiator class //////////////////////////////////////////////////////////////////////////
148 public SootMethod createIncMethod(SootClass sc) {
149 String incFuncName = getGladiatorName(sc) + "$$inc";
150 SootClass arena = getArenaForGladiator(sc);
151 SootMethod method = new SootMethod(incFuncName, new LinkedList(), IntType.v(), Modifier.PRIVATE, new LinkedList());
152 arena.addMethod(method);
153 Body incBody = Jimple.v().newBody(method);
154 method.setActiveBody(incBody);
155 ((JimpleBody)incBody).insertIdentityStmts();
159 public void processGladiatorClass(SootClass sc) {
161 // Set up the Arena zero-arg constructor
163 SootClass arena = getArenaForGladiator(sc);
164 SootMethod arenaInitMethod = arena.getMethod("<init>", new LinkedList());
165 JimpleBody arenaInitBody = (JimpleBody)arenaInitMethod.getActiveBody();
167 SootField maxField = new SootField(getGladiatorName(sc) + "$$max", IntType.v());
168 arena.addField(maxField);
169 assign(arenaInitBody, newIFR(arenaInitBody, maxField.makeRef()), IntConstant.v(initialSize),
170 arenaInitBody.getFirstNonIdentityStmt());
172 SootField sfr = new SootField(getGladiatorName(sc) + "$$size", IntType.v());
174 assign(arenaInitBody, newIFR(arenaInitBody, sfr.makeRef()), IntConstant.v(0),
175 arenaInitBody.getFirstNonIdentityStmt());
177 SootMethod incMethod = createIncMethod(sc);
178 Body incBody = incMethod.getActiveBody();
181 // Now build the $$inc method
183 Local l = newLocal(incBody, IntType.v());
184 Local l2 = newLocal(incBody, IntType.v());
185 Local l3 = newLocal(incBody, IntType.v());
186 Local l4 = newLocal(incBody, IntType.v());
188 assign(incBody, l, newIFR(incBody, sfr.makeRef()));
189 assign(incBody, l2, Jimple.v().newAddExpr(l, IntConstant.v(1)));
190 assign(incBody, newIFR(incBody, sfr.makeRef()), l2);
191 assign(incBody, l3, newIFR(incBody, maxField.makeRef()));
193 Stmt returnStmt = Jimple.v().newReturnStmt(l2);
194 incBody.getUnits().add(Jimple.v().newIfStmt(Jimple.v().newLtExpr(l2, l3), returnStmt));
196 assign(incBody, l4, Jimple.v().newShlExpr(l3, IntConstant.v(1)));
197 assign(incBody, newIFR(incBody, maxField.makeRef()), l4);
200 // Finally, iterate over the Gladiator's fields, updating the $$inc method and Arena's zero-arg constructor as we go
202 for(Iterator it = sc.getFields().iterator(); it.hasNext();) {
203 SootField f = (SootField)it.next();
204 Type t = getSliceElementType(f.getType());
205 arena.addField(f = new SootField(getGladiatorName(sc) + "$$" + f.getName(), t.makeArrayType(), 0));
207 Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(initialSize));
208 Local newArrLocal = newLocal(arenaInitBody, f.getType());
209 arenaInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newIFR(arenaInitBody, f.makeRef()), newArrLocal));
210 arenaInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
212 Local ll0 = newLocal(incBody, f.getType());
213 Local ll = newLocal(incBody, f.getType());
214 assign(incBody, ll0, newIFR(incBody, f.makeRef()));
215 assign(incBody, ll, Jimple.v().newNewArrayExpr(t, l4));
217 List args = new LinkedList();
219 args.add(IntConstant.v(0));
221 args.add(IntConstant.v(0));
223 incBody.getUnits().add(Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(arrayCopy, args)));
224 assign(incBody, newIFR(incBody, f.makeRef()), ll);
227 for(Iterator it = sc.getMethods().iterator(); it.hasNext();) {
228 SootMethod m = (SootMethod)it.next();
229 if (!m.isConcrete()) continue;
230 boolean doremove = true;
231 Body mincBody = m.getActiveBody();
232 if (implementsGladiator(m.getDeclaringClass()) && m.getName().equals("<init>")) {
234 SootClass c = m.getDeclaringClass();
235 String name = /*c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + */"$init";
236 List li = m.getParameterTypes();
238 SootMethod nm = new SootMethod(name, li, implementsGladiator(m.getReturnType()) ? IntType.v() : m.getReturnType());
239 JimpleBody bod = Jimple.v().newBody(nm);
240 bod.importBodyContentsFrom(m.getActiveBody());
241 nm.setActiveBody(bod);
244 mincBody.getUnits().remove(((JimpleBody)mincBody).getFirstNonIdentityStmt());
245 mincBody.getUnits().remove(((JimpleBody)mincBody).getFirstNonIdentityStmt());
247 System.out.println("examining " + sc.getName() + "." + m.getSignature());
248 m.retrieveActiveBody();
250 if (m.isStatic()) continue;
252 String name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + m.getName();
253 List list = new LinkedList();
254 list.addAll(m.getParameterTypes());
255 list.add(IntType.v());
256 for(Iterator i = list.iterator(); i.hasNext();) System.out.println(i.next());
257 SootMethod m2 = new SootMethod(name, list, m.getReturnType());
258 getArenaForGladiator(sc).addMethod(m2);
260 JimpleBody ab = (JimpleBody)Jimple.v().newBody(m2);
261 ab.importBodyContentsFrom(mincBody);
262 m2.setActiveBody(ab);
263 Local loc = Jimple.v().newLocal("tmpRef" + (tfr++), getArenaForGladiator(sc).getType());
264 ab.getLocals().add(loc);
265 // FIXME: insert assignment to this
267 for(Iterator z = ab.getLocals().iterator(); z.hasNext();) {
268 Local loc2 = (Local)z.next();
269 if (implementsGladiator(loc2.getType())) {
270 loc2.setType(IntType.v());
274 Chain units = ab.getUnits();
275 boolean touched = false;
276 Local loc0 = Jimple.v().newLocal("tmpRef" + (tfr++), getArenaForGladiator(sc).getType());
277 ab.getLocals().add(loc0);
278 for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
279 Stmt s = (Stmt) stmtIt.next();
280 if (s instanceof IdentityStmt) {
281 IdentityStmt is = (IdentityStmt)s;
282 Local left = (Local)is.getLeftOp();
283 if (is.getRightOp() instanceof ThisRef) {
284 left.setType(IntType.v());
285 is.getRightOpBox().setValue(Jimple.v().newParameterRef(IntType.v(), m.getParameterCount()));
287 units.addFirst(Jimple.v().newIdentityStmt(loc0, Jimple.v().newThisRef(getArenaForGladiator(sc).getType())));
293 for(Iterator i = s.getUseAndDefBoxes().iterator(); i.hasNext();) {
295 if (o instanceof ValueBox) {
296 ValueBox vb = (ValueBox)o;
299 if (o instanceof Local && implementsGladiator(((Local)o).getType())) {
300 System.out.println("thunking");
304 if (vb.getValue() instanceof ThisRef) {
305 System.out.println(s);
313 if (doremove) sc.removeMethod(m);
316 incBody.getUnits().add(returnStmt);
319 // Operations performed on all classes ////////////////////////////////////////////////////////////////////////////
321 public void processClass(SootClass c) {
322 for(Iterator it = c.getFields().iterator(); it.hasNext();) {
323 SootField f = (SootField)it.next();
324 Type t = f.getType();
325 if (t instanceof RefType) {
326 RefType rt = (RefType)t;
327 SootClass sc = rt.getSootClass();
328 if (implementsGladiator(sc)) f.setType(IntType.v());
329 } else if (t instanceof ArrayType) {
330 ArrayType at = (ArrayType)t;
331 t = at.getElementType();
332 if (!(t instanceof RefType)) continue;
333 RefType rt = (RefType)t;
334 SootClass sc = rt.getSootClass();
335 if (implementsGladiator(sc)) f.setType(IntType.v().makeArrayType());
339 List list = new LinkedList(); list.addAll(c.getMethods());
340 for(Iterator it = list.iterator(); it.hasNext();) {
341 SootMethod m = (SootMethod)it.next();
343 if (m.getName().endsWith("$$inc")) continue;
344 if (m.hasActiveBody()) b = processBody(m.getActiveBody(), c, m);
345 List l2 = new LinkedList();
346 List l = m.getParameterTypes();
347 for(Iterator it2 = l.iterator(); it2.hasNext();) {
348 Type t = (Type)it2.next();
349 l2.add(implementsGladiator(t) ? IntType.v() : t);
351 Type t = m.getReturnType();
352 if (implementsGladiator(t)) {
354 if (m.hasActiveBody()) {
355 Body bod = m.getActiveBody();
356 for(Iterator stmtIt = bod.getUnits().snapshotIterator(); stmtIt.hasNext();) {
357 Stmt s = (Stmt) stmtIt.next();
358 if (s instanceof ReturnStmt) {
359 if (((ReturnStmt)s).getOp().getType() instanceof NullType) {
360 ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
366 String name = m.getName();
367 SootMethod meth = new SootMethod(name, l2, implementsGladiator(t) ? IntType.v() : t, m.getModifiers());
369 JimpleBody b2 = Jimple.v().newBody(meth);
370 b2.importBodyContentsFrom(b);
371 meth.setActiveBody(b2);
379 protected Body processBody(Body body, SootClass ownerClass, SootMethod smeth) {
380 if (body instanceof JimpleBody) {
381 JimpleBody b2 = Jimple.v().newBody(smeth);
382 b2.importBodyContentsFrom(body);
385 Chain units = body.getUnits();
386 System.out.println("processBody("+body.getMethod()+")");
387 for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) {
388 Local l = (Local)it.next();
389 if (implementsGladiator(l.getType())) l.setType(IntType.v());
391 for(int qq=0; qq<2; qq++) for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
392 Stmt s = (Stmt) stmtIt.next();
393 if (s instanceof DefinitionStmt) {
394 DefinitionStmt ds = (DefinitionStmt)s;
395 if (ds.getLeftOp().getType() instanceof PrimType && ds.getRightOp().getType() instanceof NullType) {
396 ds.getRightOpBox().setValue(IntConstant.v(-1));
399 if (implementsGladiator(smeth.getReturnType()) && s instanceof ReturnStmt)
400 if (((ReturnStmt)s).getOp().getType() instanceof NullType)
401 ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
402 List l = s.getUseAndDefBoxes();
403 for(Iterator it = l.iterator(); it.hasNext();) {
404 Object o = it.next();
405 if (o instanceof ValueBox) {
406 ValueBox vb = (ValueBox)o;
407 Value v = vb.getValue();
409 if (v instanceof BinopExpr) {
410 BinopExpr boe = (BinopExpr)v;
411 Type t1 = boe.getOp1().getType();
412 Type t2 = boe.getOp2().getType();
413 if (t1 instanceof PrimType && t2 instanceof NullType) boe.setOp2(IntConstant.v(-1));
414 if (t2 instanceof PrimType && t1 instanceof NullType) boe.setOp1(IntConstant.v(-1));
417 if (v instanceof NewExpr) {
418 NewExpr ne = (NewExpr)v;
419 if (implementsGladiator(ne.getBaseType())) {
420 SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
421 SootClass arena = getArenaForGladiator(sc);
422 String incFuncName = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$inc";
423 SootMethodRef smr = Scene.v().makeMethodRef(arena, incFuncName, new LinkedList(), IntType.v(), false);
424 Expr invokeExpr = Jimple.v().newSpecialInvokeExpr(body.getThisLocal(), smr);
425 Local ll = viaLocal(invokeExpr, body, s);
432 if (v instanceof InvokeExpr) {
433 InvokeExpr ie = (InvokeExpr)v;
434 SootMethodRef mr = ie.getMethodRef();
435 String name = mr.name();
436 if (v instanceof InstanceInvokeExpr && implementsGladiator(mr.declaringClass())) {
437 InstanceInvokeExpr iie = (InstanceInvokeExpr)v;
438 List li = new LinkedList();
439 li.addAll(iie.getArgs());
440 LinkedList pl = new LinkedList();
441 for(Iterator it2 = mr.parameterTypes().iterator(); it2.hasNext();) {
442 Type t = (Type)it2.next();
443 pl.add(implementsGladiator(t) ? IntType.v() : t);
445 if (mr.name().equals("<init>") && implementsGladiator(mr.declaringClass())) {
451 li.add(viaLocal(iie.getBase(),body,s));
452 SootClass sc = mr.declaringClass();
453 name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + name;
454 mr = Scene.v().makeMethodRef(getArenaForGladiator(sc),
457 implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
459 ie = Jimple.v().newVirtualInvokeExpr(body.getThisLocal(), mr, li);
462 } else if (!(v instanceof StaticInvokeExpr)) {
463 List l0 = mr.parameterTypes();
464 List l2 = new LinkedList();
465 for(Iterator it2 = l0.iterator(); it2.hasNext();) {
466 Type t = (Type)it2.next();
467 l2.add(implementsGladiator(t) ? IntType.v() : t);
469 mr = Scene.v().makeMethodRef(mr.declaringClass(),
472 implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
478 for(int i=0; i<ie.getArgCount(); i++) {
479 ValueBox b = ie.getArgBox(i);
480 Value val = b.getValue();
481 if (mr.parameterType(i) instanceof RefType && val.getType() instanceof PrimType) {
482 SootClass intClass = Scene.v().getSootClass("java.lang.Integer");
483 List typelist = new LinkedList();
484 typelist.add(IntType.v());
485 SootMethod intMethod = intClass.getMethod("<init>", typelist);
486 Local loc = viaLocal(Jimple.v().newNewExpr(RefType.v(intClass)), body, s);
487 List list = new LinkedList();
489 units.insertBefore(Jimple.v().newInvokeStmt(Jimple.v().newSpecialInvokeExpr(loc,
495 if (val != null && val.getType() instanceof NullType && mr.parameterType(i) instanceof IntType) {
496 b.setValue(IntConstant.v(-1));
501 } else if (v instanceof CastExpr) {
502 CastExpr ce = (CastExpr)v;
503 if (implementsGladiator(ce.getCastType())) {
504 SootClass arena = getArenaForGladiator(((RefType)ce.getCastType()).getSootClass());
505 SootClass ic = Scene.v().getSootClass("java.lang.Integer");
506 ce.setCastType(ic.getType());
508 Local l1 = Jimple.v().newLocal("tmpRef" + (tfr++), ic.getType()); body.getLocals().add(l1);
509 Local l2 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v()); body.getLocals().add(l2);
511 Stmt s2 = Jimple.v().newAssignStmt(l1, Jimple.v().newCastExpr(ce.getOp(), ic.getType()));
512 body.getUnits().insertBefore(s2, s);
514 Stmt isNull = Jimple.v().newAssignStmt(l2, IntConstant.v(-1));
515 body.getUnits().insertAfter(isNull, s2);
517 Stmt ifStmt = Jimple.v().newIfStmt(Jimple.v().newEqExpr(l1, NullConstant.v()), s);
518 body.getUnits().insertAfter(ifStmt, isNull);
520 SootMethodRef mr = Scene.v().makeMethodRef(ic, "intValue", new LinkedList(), IntType.v(), false);
522 Jimple.v().newAssignStmt(l2, Jimple.v().newVirtualInvokeExpr(l1, mr, new LinkedList()));
523 body.getUnits().insertAfter(isNotNull, ifStmt);
530 } else if (v instanceof InstanceFieldRef) {
531 InstanceFieldRef ifr = (InstanceFieldRef)v;
532 SootFieldRef fr = ifr.getFieldRef();
534 if (implementsGladiator(fr.declaringClass()) && fr.name().equals("this$0")) {
535 System.out.println("******************");
536 vb.setValue(body.getThisLocal());
537 System.out.println("s is " + s);
538 } else if (implementsGladiator(fr.declaringClass())) {
539 SootClass arena = getArenaForGladiator(fr.declaringClass());
540 InstanceFieldRef sfr = newIFR(body, getSliceForField(fr));
541 vb.setValue(Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase()));
543 if ((t instanceof RefType) && implementsGladiator(((RefType)t).getSootClass())) {
544 SootClass tc = ((RefType)t).getSootClass();
545 SootClass arena = getArenaForGladiator(tc);
546 ifr.setFieldRef(Scene.v().makeFieldRef(arena, fr.name(), IntType.v(), false));