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] = "-keep-line-number";
52 args2[args.length + 9] = "-allow-phantom-refs";
53 args2[args.length + 10] = "-allow-phantom-refs";
54 args2[args.length + 11] = "-cp";
55 args2[args.length + 12] = sootcp;
56 System.out.println("sootcp => " + sootcp);
57 args2[args.length + 13] = args[args.length-1];
58 for(int i=0; i<args2.length; i++) System.out.println("args2["+i+"] = " + args2[i]);
59 soot.Main.main(args2);
62 // The main loop //////////////////////////////////////////////////////////////////////////////
64 public void internalTransform(String phaseName, Map options) {
65 System.out.println("begun");
67 List ac = new LinkedList();
68 for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
69 SootClass sc = (SootClass)it.next();
72 for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
73 SootClass sc = (SootClass)it.next();
74 if (implementsGladiator(sc)) {
75 System.out.println(" application class: " + sc.getName());
76 System.out.println(" application class: " + getArenaForGladiator(sc).getName());
77 sc.setApplicationClass();
78 getArenaForGladiator(sc).setApplicationClass();
80 ac.add(getArenaForGladiator(sc));
82 for(Iterator i2 = sc.getMethods().iterator(); i2.hasNext();) {
83 SootMethod m = (SootMethod)i2.next();
84 if (m.isConcrete()) m.retrieveActiveBody();
86 for(Iterator i2 = getArenaForGladiator(sc).getMethods().iterator(); i2.hasNext();) {
87 SootMethod m = (SootMethod)i2.next();
88 if (m.isConcrete()) m.retrieveActiveBody();
93 for(Iterator it = ac.iterator(); it.hasNext();) {
94 SootClass sc = (SootClass)it.next();
95 if (sc.isInterface() || !implementsGladiator(sc)) continue;
96 processGladiatorClass(sc);
98 for(Iterator it = ac.iterator(); it.hasNext();) {
99 SootClass sc = (SootClass)it.next();
100 if (sc.isInterface()) continue;
105 // Initializers //////////////////////////////////////////////////////////////////////////////
107 public Type objectType;
108 public List arrayCopyTypes = new LinkedList();
109 public SootMethodRef arrayCopy;
111 objectType = Scene.v().getSootClass("java.lang.Object").getType();
112 arrayCopyTypes.add(objectType);
113 arrayCopyTypes.add(IntType.v());
114 arrayCopyTypes.add(objectType);
115 arrayCopyTypes.add(IntType.v());
116 arrayCopyTypes.add(IntType.v());
118 Scene.v().makeMethodRef(Scene.v().getSootClass("java.lang.System"),
125 // Helpers //////////////////////////////////////////////////////////////////////////////
128 public Local newLocal(Body b, Type t) {
129 Local l = Jimple.v().newLocal("tmpRef" + (tfr++), t);
130 b.getLocals().add(l);
134 public Local viaLocal(Value v, Body b, Unit u) {
135 Local l = newLocal(b, v.getType());
136 if (v instanceof IdentityRef) b.getUnits().insertBefore(Jimple.v().newIdentityStmt(l, v), u);
137 else b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, v), u);
141 boolean implementsGladiator(SootClass c) { return c.implementsInterface("org.ibex.arenaj.Gladiator"); }
142 boolean implementsGladiator(Type t) {return (t instanceof RefType)&&implementsGladiator(((RefType)t).getSootClass());}
143 String getArenaName(SootClass sc) { return sc.getName().substring(0, sc.getName().lastIndexOf('$')); }
144 String getGladiatorName(SootClass sc) { return sc.getName().substring(sc.getName().lastIndexOf('$')+1); }
145 SootClass getArenaForGladiator(SootClass sc) { return Scene.v().getSootClass(getArenaName(sc)); }
146 InstanceFieldRef newIFR(Body b, SootFieldRef fr) { return Jimple.v().newInstanceFieldRef(b.getThisLocal(), fr); }
147 void assign(Body b, Value left, Value right) { b.getUnits().add(Jimple.v().newAssignStmt(left, right)); }
148 void assign(Body b, Value l, Value r, Stmt w) { b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, r), w); }
149 Type getSliceElementType(Type t) { return implementsGladiator(t) ? IntType.v() : t; }
150 SootFieldRef getSliceForField(SootField f) { return getSliceForField(f.makeRef()); }
151 SootFieldRef getSliceForField(SootFieldRef f) {
152 return Scene.v().makeFieldRef(getArenaForGladiator(f.declaringClass()),
153 getGladiatorName(f.declaringClass())+"$$"+f.name(),
154 getSliceElementType(f.type()).makeArrayType(),
159 // Operations performed on the Gladiator class //////////////////////////////////////////////////////////////////////////
161 public void processGladiatorClass(SootClass sc) {
162 SootClass mc = getArenaForGladiator(sc);
163 String subname = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1);
164 String incFuncName = subname + "$$inc";
165 SootMethod method = new SootMethod(incFuncName, new LinkedList(), IntType.v(), Modifier.PRIVATE, new LinkedList());
167 mc.setApplicationClass();
168 SootMethod initMethod = mc.getMethod("<init>", new LinkedList());
169 if (!initMethod.hasActiveBody()) initMethod.setActiveBody((JimpleBody)Jimple.v().newBody(initMethod));
170 Body mcInitBody = initMethod.getActiveBody();
172 mc.addMethod(method);
173 Body body = Jimple.v().newBody(method);
174 method.setActiveBody(body);
175 ((JimpleBody)body).insertIdentityStmts();
176 Local l = newLocal(body, IntType.v());
177 Local l2 = newLocal(body, IntType.v());
178 Local l3 = newLocal(body, IntType.v());
179 Local l4 = newLocal(body, IntType.v());
181 Stmt where = ((JimpleBody)mcInitBody).getFirstNonIdentityStmt();
182 SootField maxField = new SootField(subname + "$$max", IntType.v());
183 mc.addField(maxField);
184 assign(mcInitBody, newIFR(mcInitBody, maxField.makeRef()), IntConstant.v(initialSize), where);
186 SootField sfr = new SootField(subname + "$$size", IntType.v());
188 assign(mcInitBody, newIFR(mcInitBody, sfr.makeRef()), IntConstant.v(0), where);
190 assign(body, l, newIFR(body, sfr.makeRef()));
191 assign(body, l2, Jimple.v().newAddExpr(l, IntConstant.v(1)));
192 assign(body, newIFR(body, sfr.makeRef()), l2);
193 assign(body, l3, newIFR(body, maxField.makeRef()));
195 Stmt returnStmt = Jimple.v().newReturnStmt(l2);
196 body.getUnits().add(Jimple.v().newIfStmt(Jimple.v().newLtExpr(l2, l3), returnStmt));
198 assign(body, l4, Jimple.v().newShlExpr(l3, IntConstant.v(1)));
199 assign(body, newIFR(body, maxField.makeRef()), l4);
201 for(Iterator it = sc.getFields().iterator(); it.hasNext();) {
202 SootField f = (SootField)it.next();
203 Type t = getSliceElementType(f.getType());
204 mc.addField(f = new SootField(getGladiatorName(sc) + "$$" + f.getName(), t.makeArrayType(), 0));
206 Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(initialSize));
207 Local newArrLocal = newLocal(mcInitBody, f.getType());
208 mcInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newIFR(mcInitBody, f.makeRef()), newArrLocal));
209 mcInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
211 Local ll0 = newLocal(body, f.getType());
212 Local ll = newLocal(body, f.getType());
213 assign(body, ll0, newIFR(body, f.makeRef()));
214 assign(body, ll, Jimple.v().newNewArrayExpr(t, l4));
216 List args = new LinkedList();
218 args.add(IntConstant.v(0));
220 args.add(IntConstant.v(0));
222 body.getUnits().add(Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(arrayCopy, args)));
223 assign(body, newIFR(body, f.makeRef()), ll);
226 for(Iterator it = sc.getMethods().iterator(); it.hasNext();) {
227 SootMethod m = (SootMethod)it.next();
228 if (!m.isConcrete()) continue;
229 boolean doremove = true;
230 Body mbody = m.getActiveBody();
231 if (implementsGladiator(m.getDeclaringClass()) && m.getName().equals("<init>")) {
233 SootClass c = m.getDeclaringClass();
234 String name = /*c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + */"$init";
235 List li = m.getParameterTypes();
237 SootMethod nm = new SootMethod(name, li, implementsGladiator(m.getReturnType()) ? IntType.v() : m.getReturnType());
238 JimpleBody bod = Jimple.v().newBody(nm);
239 bod.importBodyContentsFrom(m.getActiveBody());
240 nm.setActiveBody(bod);
243 mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
244 mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
246 System.out.println("examining " + sc.getName() + "." + m.getSignature());
247 m.retrieveActiveBody();
249 if (m.isStatic()) continue;
251 String name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + m.getName();
252 List list = new LinkedList();
253 list.addAll(m.getParameterTypes());
254 list.add(IntType.v());
255 for(Iterator i = list.iterator(); i.hasNext();) System.out.println(i.next());
256 SootMethod m2 = new SootMethod(name, list, m.getReturnType());
257 getArenaForGladiator(sc).addMethod(m2);
259 JimpleBody ab = (JimpleBody)Jimple.v().newBody(m2);
260 ab.importBodyContentsFrom(mbody);
261 m2.setActiveBody(ab);
262 Local loc = Jimple.v().newLocal("tmpRef" + (tfr++), getArenaForGladiator(sc).getType());
263 ab.getLocals().add(loc);
264 // FIXME: insert assignment to this
266 for(Iterator z = ab.getLocals().iterator(); z.hasNext();) {
267 Local loc2 = (Local)z.next();
268 if (implementsGladiator(loc2.getType())) {
269 loc2.setType(IntType.v());
273 Chain units = ab.getUnits();
274 boolean touched = false;
275 Local loc0 = Jimple.v().newLocal("tmpRef" + (tfr++), getArenaForGladiator(sc).getType());
276 ab.getLocals().add(loc0);
277 for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
278 Stmt s = (Stmt) stmtIt.next();
279 if (s instanceof IdentityStmt) {
280 IdentityStmt is = (IdentityStmt)s;
281 Local left = (Local)is.getLeftOp();
282 if (is.getRightOp() instanceof ThisRef) {
283 left.setType(IntType.v());
284 is.getRightOpBox().setValue(Jimple.v().newParameterRef(IntType.v(), m.getParameterCount()));
286 units.addFirst(Jimple.v().newIdentityStmt(loc0, Jimple.v().newThisRef(getArenaForGladiator(sc).getType())));
292 for(Iterator i = s.getUseAndDefBoxes().iterator(); i.hasNext();) {
294 if (o instanceof ValueBox) {
295 ValueBox vb = (ValueBox)o;
298 if (o instanceof Local && implementsGladiator(((Local)o).getType())) {
299 System.out.println("thunking");
303 if (vb.getValue() instanceof ThisRef) {
304 System.out.println(s);
312 if (doremove) sc.removeMethod(m);
315 body.getUnits().add(returnStmt);
318 // Operations performed on all classes ////////////////////////////////////////////////////////////////////////////
320 public void processClass(SootClass c) {
321 for(Iterator it = c.getFields().iterator(); it.hasNext();) {
322 SootField f = (SootField)it.next();
323 Type t = f.getType();
324 if (t instanceof RefType) {
325 RefType rt = (RefType)t;
326 SootClass sc = rt.getSootClass();
327 if (implementsGladiator(sc)) f.setType(IntType.v());
328 } else if (t instanceof ArrayType) {
329 ArrayType at = (ArrayType)t;
330 t = at.getElementType();
331 if (!(t instanceof RefType)) continue;
332 RefType rt = (RefType)t;
333 SootClass sc = rt.getSootClass();
334 if (implementsGladiator(sc)) f.setType(IntType.v().makeArrayType());
338 List list = new LinkedList(); list.addAll(c.getMethods());
339 for(Iterator it = list.iterator(); it.hasNext();) {
340 SootMethod m = (SootMethod)it.next();
342 if (m.getName().endsWith("$$inc")) continue;
343 if (m.hasActiveBody()) b = processBody(m.getActiveBody(), c, m);
344 List l2 = new LinkedList();
345 List l = m.getParameterTypes();
346 for(Iterator it2 = l.iterator(); it2.hasNext();) {
347 Type t = (Type)it2.next();
348 l2.add(implementsGladiator(t) ? IntType.v() : t);
350 Type t = m.getReturnType();
351 if (implementsGladiator(t)) {
353 if (m.hasActiveBody()) {
354 Body bod = m.getActiveBody();
355 for(Iterator stmtIt = bod.getUnits().snapshotIterator(); stmtIt.hasNext();) {
356 Stmt s = (Stmt) stmtIt.next();
357 if (s instanceof ReturnStmt) {
358 if (((ReturnStmt)s).getOp().getType() instanceof NullType) {
359 ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
365 String name = m.getName();
366 SootMethod meth = new SootMethod(name, l2, implementsGladiator(t) ? IntType.v() : t, m.getModifiers());
368 JimpleBody b2 = Jimple.v().newBody(meth);
369 b2.importBodyContentsFrom(b);
370 meth.setActiveBody(b2);
378 protected Body processBody(Body body, SootClass ownerClass, SootMethod smeth) {
379 if (body instanceof JimpleBody) {
380 JimpleBody b2 = Jimple.v().newBody(smeth);
381 b2.importBodyContentsFrom(body);
384 Chain units = body.getUnits();
385 System.out.println("processBody("+body.getMethod()+")");
386 for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) {
387 Local l = (Local)it.next();
388 if (implementsGladiator(l.getType())) l.setType(IntType.v());
390 for(int qq=0; qq<2; qq++) for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
391 Stmt s = (Stmt) stmtIt.next();
392 if (s instanceof DefinitionStmt) {
393 DefinitionStmt ds = (DefinitionStmt)s;
394 if (ds.getLeftOp().getType() instanceof PrimType && ds.getRightOp().getType() instanceof NullType) {
395 ds.getRightOpBox().setValue(IntConstant.v(-1));
398 if (implementsGladiator(smeth.getReturnType()) && s instanceof ReturnStmt)
399 if (((ReturnStmt)s).getOp().getType() instanceof NullType)
400 ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
401 List l = s.getUseAndDefBoxes();
402 for(Iterator it = l.iterator(); it.hasNext();) {
403 Object o = it.next();
404 if (o instanceof ValueBox) {
405 ValueBox vb = (ValueBox)o;
406 Value v = vb.getValue();
408 if (v instanceof BinopExpr) {
409 BinopExpr boe = (BinopExpr)v;
410 Type t1 = boe.getOp1().getType();
411 Type t2 = boe.getOp2().getType();
412 if (t1 instanceof PrimType && t2 instanceof NullType) boe.setOp2(IntConstant.v(-1));
413 if (t2 instanceof PrimType && t1 instanceof NullType) boe.setOp1(IntConstant.v(-1));
416 if (v instanceof NewExpr) {
417 NewExpr ne = (NewExpr)v;
418 if (implementsGladiator(ne.getBaseType())) {
419 SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
420 SootClass mc = getArenaForGladiator(sc);
421 String incFuncName = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$inc";
422 SootMethodRef smr = Scene.v().makeMethodRef(mc, incFuncName, new LinkedList(), IntType.v(), false);
423 Expr invokeExpr = Jimple.v().newSpecialInvokeExpr(body.getThisLocal(), smr);
424 Local ll = viaLocal(invokeExpr, body, s);
431 if (v instanceof InvokeExpr) {
432 InvokeExpr ie = (InvokeExpr)v;
433 SootMethodRef mr = ie.getMethodRef();
434 String name = mr.name();
435 if (v instanceof InstanceInvokeExpr && implementsGladiator(mr.declaringClass())) {
436 InstanceInvokeExpr iie = (InstanceInvokeExpr)v;
437 List li = new LinkedList();
438 li.addAll(iie.getArgs());
439 LinkedList pl = new LinkedList();
440 for(Iterator it2 = mr.parameterTypes().iterator(); it2.hasNext();) {
441 Type t = (Type)it2.next();
442 pl.add(implementsGladiator(t) ? IntType.v() : t);
444 if (mr.name().equals("<init>") && implementsGladiator(mr.declaringClass())) {
450 li.add(viaLocal(iie.getBase(),body,s));
451 SootClass sc = mr.declaringClass();
452 name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + name;
453 mr = Scene.v().makeMethodRef(getArenaForGladiator(sc),
456 implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
458 ie = Jimple.v().newVirtualInvokeExpr(body.getThisLocal(), mr, li);
461 } else if (!(v instanceof StaticInvokeExpr)) {
462 List l0 = mr.parameterTypes();
463 List l2 = new LinkedList();
464 for(Iterator it2 = l0.iterator(); it2.hasNext();) {
465 Type t = (Type)it2.next();
466 l2.add(implementsGladiator(t) ? IntType.v() : t);
468 mr = Scene.v().makeMethodRef(mr.declaringClass(),
471 implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
477 for(int i=0; i<ie.getArgCount(); i++) {
478 ValueBox b = ie.getArgBox(i);
479 Value val = b.getValue();
480 if (mr.parameterType(i) instanceof RefType && val.getType() instanceof PrimType) {
481 SootClass intClass = Scene.v().getSootClass("java.lang.Integer");
482 List typelist = new LinkedList();
483 typelist.add(IntType.v());
484 SootMethod intMethod = intClass.getMethod("<init>", typelist);
485 Local loc = viaLocal(Jimple.v().newNewExpr(RefType.v(intClass)), body, s);
486 List list = new LinkedList();
488 units.insertBefore(Jimple.v().newInvokeStmt(Jimple.v().newSpecialInvokeExpr(loc,
494 if (val != null && val.getType() instanceof NullType && mr.parameterType(i) instanceof IntType) {
495 b.setValue(IntConstant.v(-1));
500 } else if (v instanceof CastExpr) {
501 CastExpr ce = (CastExpr)v;
502 if (implementsGladiator(ce.getCastType())) {
503 SootClass mc = getArenaForGladiator(((RefType)ce.getCastType()).getSootClass());
504 SootClass ic = Scene.v().getSootClass("java.lang.Integer");
505 ce.setCastType(ic.getType());
507 Local l1 = Jimple.v().newLocal("tmpRef" + (tfr++), ic.getType()); body.getLocals().add(l1);
508 Local l2 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v()); body.getLocals().add(l2);
510 Stmt s2 = Jimple.v().newAssignStmt(l1, Jimple.v().newCastExpr(ce.getOp(), ic.getType()));
511 body.getUnits().insertBefore(s2, s);
513 Stmt isNull = Jimple.v().newAssignStmt(l2, IntConstant.v(-1));
514 body.getUnits().insertAfter(isNull, s2);
516 Stmt ifStmt = Jimple.v().newIfStmt(Jimple.v().newEqExpr(l1, NullConstant.v()), s);
517 body.getUnits().insertAfter(ifStmt, isNull);
519 SootMethodRef mr = Scene.v().makeMethodRef(ic, "intValue", new LinkedList(), IntType.v(), false);
521 Jimple.v().newAssignStmt(l2, Jimple.v().newVirtualInvokeExpr(l1, mr, new LinkedList()));
522 body.getUnits().insertAfter(isNotNull, ifStmt);
529 } else if (v instanceof InstanceFieldRef) {
530 InstanceFieldRef ifr = (InstanceFieldRef)v;
531 SootFieldRef fr = ifr.getFieldRef();
533 if (implementsGladiator(fr.declaringClass()) && fr.name().equals("this$0")) {
534 System.out.println("******************");
535 vb.setValue(body.getThisLocal());
536 System.out.println("s is " + s);
537 } else if (implementsGladiator(fr.declaringClass())) {
538 SootClass mc = getArenaForGladiator(fr.declaringClass());
539 InstanceFieldRef sfr = newIFR(body, getSliceForField(fr));
540 vb.setValue(Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase()));
542 if ((t instanceof RefType) && implementsGladiator(((RefType)t).getSootClass())) {
543 SootClass tc = ((RefType)t).getSootClass();
544 SootClass mc = getArenaForGladiator(tc);
545 ifr.setFieldRef(Scene.v().makeFieldRef(mc, fr.name(), IntType.v(), false));