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);
64 public static Local newLocal(Body b, Type t) {
65 Local l = Jimple.v().newLocal("tmpRef" + (tfr++), t);
70 public static Local viaLocal(Value v, Body b) {
71 Local l = newLocal(b, v.getType());
72 if (v instanceof IdentityRef) {
73 b.getUnits().addFirst(Jimple.v().newIdentityStmt(l, v));
75 b.getUnits().addFirst(Jimple.v().newAssignStmt(l, v));
79 public static Local viaLocal(Value v, Body b, Unit u) {
80 Local l = newLocal(b, v.getType());
81 if (v instanceof IdentityRef) {
82 b.getUnits().insertBefore(Jimple.v().newIdentityStmt(l, v), u);
84 b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, v), u);
89 HashMap map = new HashMap();
90 HashSet size_added = new HashSet();
93 public void internalTransform(String phaseName, Map options) {
94 System.out.println("begun");
96 List ac = new LinkedList();
97 for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
98 SootClass sc = (SootClass)it.next();
101 for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
102 SootClass sc = (SootClass)it.next();
103 if (implementsGladiator(sc)) {
104 System.out.println(" application class: " + sc.getName());
105 System.out.println(" application class: " + getArenaForGladiator(sc).getName());
106 sc.setApplicationClass();
107 getArenaForGladiator(sc).setApplicationClass();
109 ac.add(getArenaForGladiator(sc));
110 for(Iterator i2 = sc.getMethods().iterator(); i2.hasNext();) {
111 SootMethod m = (SootMethod)i2.next();
112 if (m.isConcrete()) m.retrieveActiveBody();
114 for(Iterator i2 = getArenaForGladiator(sc).getMethods().iterator(); i2.hasNext();) {
115 SootMethod m = (SootMethod)i2.next();
116 if (m.isConcrete()) m.retrieveActiveBody();
120 for(Iterator it = ac.iterator(); it.hasNext();) {
121 SootClass sc = (SootClass)it.next();
122 if (sc.isInterface()) continue;
123 if (!implementsGladiator(sc)) continue;
124 System.out.println("fixing Gladiator class: " + sc);
127 for(Iterator it = ac.iterator(); it.hasNext();) {
128 SootClass sc = (SootClass)it.next();
129 if (sc.isInterface()) continue;
130 System.out.println(" updating class: " + sc);
135 boolean implementsGladiator(SootClass c) { return c.implementsInterface("org.ibex.arenaj.Gladiator"); }
136 boolean implementsGladiator(Type t) {return (t instanceof RefType)&&implementsGladiator(((RefType)t).getSootClass());}
137 String getArenaName(SootClass sc) { return sc.getName().substring(0, sc.getName().lastIndexOf('$')); }
138 String getGladiatorName(SootClass sc) { return sc.getName().substring(sc.getName().lastIndexOf('$')+1); }
139 SootClass getArenaForGladiator(SootClass sc) { return Scene.v().getSootClass(getArenaName(sc)); }
140 InstanceFieldRef newIFR(Body b, SootFieldRef fr) { return Jimple.v().newInstanceFieldRef(b.getThisLocal(), fr); }
141 void assign(Body b, Value left, Value right) { b.getUnits().add(Jimple.v().newAssignStmt(left, right)); }
142 void assign(Body b, Value l, Value r, Stmt w) { b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, r), w); }
143 Type getSliceElementType(Type t) { return implementsGladiator(t) ? IntType.v() : t; }
144 SootFieldRef getSliceForField(SootField f) { return getSliceForField(f.makeRef()); }
145 SootFieldRef getSliceForField(SootFieldRef f) {
146 return Scene.v().makeFieldRef(getArenaForGladiator(f.declaringClass()),
147 getGladiatorName(f.declaringClass())+"$$"+f.name(),
148 getSliceElementType(f.type()).makeArrayType(),
152 public Type objectType;
153 public List arrayCopyTypes = new LinkedList();
154 public SootMethodRef arrayCopy;
156 objectType = Scene.v().getSootClass("java.lang.Object").getType();
157 arrayCopyTypes.add(objectType);
158 arrayCopyTypes.add(IntType.v());
159 arrayCopyTypes.add(objectType);
160 arrayCopyTypes.add(IntType.v());
161 arrayCopyTypes.add(IntType.v());
163 Scene.v().makeMethodRef(Scene.v().getSootClass("java.lang.System"),
170 //////////////////////////////////////////////////////////////////////////////
172 public void fixClass(SootClass sc) {
173 SootClass mc = getArenaForGladiator(sc);
174 String subname = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1);
175 String incFuncName = subname + "$$inc";
176 SootMethod method = new SootMethod(incFuncName, new LinkedList(), IntType.v(), Modifier.PRIVATE, new LinkedList());
178 mc.setApplicationClass();
179 SootMethod initMethod = mc.getMethod("<init>", new LinkedList());
180 if (!initMethod.hasActiveBody()) initMethod.setActiveBody((JimpleBody)Jimple.v().newBody(initMethod));
181 Body mcInitBody = initMethod.getActiveBody();
183 mc.addMethod(method);
184 Body body = Jimple.v().newBody(method);
185 method.setActiveBody(body);
186 ((JimpleBody)body).insertIdentityStmts();
187 Local l = newLocal(body, IntType.v());
188 Local l2 = newLocal(body, IntType.v());
189 Local l3 = newLocal(body, IntType.v());
190 Local l4 = newLocal(body, IntType.v());
192 Stmt where = ((JimpleBody)mcInitBody).getFirstNonIdentityStmt();
193 SootField maxField = new SootField(subname + "$$max", IntType.v());
194 mc.addField(maxField);
195 assign(mcInitBody, newIFR(mcInitBody, maxField.makeRef()), IntConstant.v(initialSize), where);
197 SootField sfr = new SootField(subname + "$$size", IntType.v());
199 assign(mcInitBody, newIFR(mcInitBody, sfr.makeRef()), IntConstant.v(0), where);
201 assign(body, l, newIFR(body, sfr.makeRef()));
202 assign(body, l2, Jimple.v().newAddExpr(l, IntConstant.v(1)));
203 assign(body, newIFR(body, sfr.makeRef()), l2);
204 assign(body, l3, newIFR(body, maxField.makeRef()));
206 Stmt returnStmt = Jimple.v().newReturnStmt(l2);
207 body.getUnits().add(Jimple.v().newIfStmt(Jimple.v().newLtExpr(l2, l3), returnStmt));
209 assign(body, l4, Jimple.v().newShlExpr(l3, IntConstant.v(1)));
210 assign(body, newIFR(body, maxField.makeRef()), l4);
212 for(Iterator it = sc.getFields().iterator(); it.hasNext();) {
213 SootField f = (SootField)it.next();
214 Type t = getSliceElementType(f.getType());
215 mc.addField(f = new SootField(getGladiatorName(sc) + "$$" + f.getName(), t.makeArrayType(), 0));
217 Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(initialSize));
218 Local newArrLocal = newLocal(mcInitBody, f.getType());
219 mcInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newIFR(mcInitBody, f.makeRef()), newArrLocal));
220 mcInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
222 Local ll0 = newLocal(body, f.getType());
223 Local ll = newLocal(body, f.getType());
224 assign(body, ll0, newIFR(body, f.makeRef()));
225 assign(body, ll, Jimple.v().newNewArrayExpr(t, l4));
227 List args = new LinkedList();
229 args.add(IntConstant.v(0));
231 args.add(IntConstant.v(0));
233 body.getUnits().add(Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(arrayCopy, args)));
234 assign(body, newIFR(body, f.makeRef()), ll);
237 for(Iterator it = sc.getMethods().iterator(); it.hasNext();) {
238 SootMethod m = (SootMethod)it.next();
239 if (!m.isConcrete()) continue;
240 boolean doremove = true;
241 Body mbody = m.getActiveBody();
242 if (implementsGladiator(m.getDeclaringClass()) && m.getName().equals("<init>")) {
244 SootClass c = m.getDeclaringClass();
245 String name = /*c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + */"$init";
246 List li = m.getParameterTypes();
248 SootMethod nm = new SootMethod(name, li, implementsGladiator(m.getReturnType()) ? IntType.v() : m.getReturnType());
249 JimpleBody bod = Jimple.v().newBody(nm);
250 bod.importBodyContentsFrom(m.getActiveBody());
251 nm.setActiveBody(bod);
254 mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
255 mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
257 System.out.println("examining " + sc.getName() + "." + m.getSignature());
258 m.retrieveActiveBody();
260 if (m.isStatic()) continue;
262 String name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + m.getName();
263 List list = new LinkedList();
264 list.addAll(m.getParameterTypes());
265 list.add(IntType.v());
266 for(Iterator i = list.iterator(); i.hasNext();) System.out.println(i.next());
267 SootMethod m2 = new SootMethod(name, list, m.getReturnType());
268 getArenaForGladiator(sc).addMethod(m2);
270 JimpleBody ab = (JimpleBody)Jimple.v().newBody(m2);
271 ab.importBodyContentsFrom(mbody);
272 m2.setActiveBody(ab);
273 Local loc = Jimple.v().newLocal("tmpRef" + (tfr++), getArenaForGladiator(sc).getType());
274 ab.getLocals().add(loc);
275 // FIXME: insert assignment to this
277 for(Iterator z = ab.getLocals().iterator(); z.hasNext();) {
278 Local loc2 = (Local)z.next();
279 if (implementsGladiator(loc2.getType())) {
280 loc2.setType(IntType.v());
284 Chain units = ab.getUnits();
285 boolean touched = false;
286 Local loc0 = Jimple.v().newLocal("tmpRef" + (tfr++), getArenaForGladiator(sc).getType());
287 ab.getLocals().add(loc0);
288 for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
289 Stmt s = (Stmt) stmtIt.next();
290 if (s instanceof IdentityStmt) {
291 IdentityStmt is = (IdentityStmt)s;
292 Local left = (Local)is.getLeftOp();
293 if (is.getRightOp() instanceof ThisRef) {
294 left.setType(IntType.v());
295 is.getRightOpBox().setValue(Jimple.v().newParameterRef(IntType.v(), m.getParameterCount()));
297 units.addFirst(Jimple.v().newIdentityStmt(loc0, Jimple.v().newThisRef(getArenaForGladiator(sc).getType())));
303 for(Iterator i = s.getUseAndDefBoxes().iterator(); i.hasNext();) {
305 if (o instanceof ValueBox) {
306 ValueBox vb = (ValueBox)o;
309 if (o instanceof Local && implementsGladiator(((Local)o).getType())) {
310 System.out.println("thunking");
314 if (vb.getValue() instanceof ThisRef) {
315 System.out.println(s);
323 if (doremove) sc.removeMethod(m);
326 body.getUnits().add(returnStmt);
329 public void nuke(SootClass c) {
330 for(Iterator it = c.getFields().iterator(); it.hasNext();) {
331 SootField f = (SootField)it.next();
332 Type t = f.getType();
333 if (t instanceof RefType) {
334 RefType rt = (RefType)t;
335 SootClass sc = rt.getSootClass();
336 if (implementsGladiator(sc)) f.setType(IntType.v());
337 } else if (t instanceof ArrayType) {
338 ArrayType at = (ArrayType)t;
339 t = at.getElementType();
340 if (!(t instanceof RefType)) continue;
341 RefType rt = (RefType)t;
342 SootClass sc = rt.getSootClass();
343 if (implementsGladiator(sc)) f.setType(IntType.v().makeArrayType());
347 List list = new LinkedList(); list.addAll(c.getMethods());
348 for(Iterator it = list.iterator(); it.hasNext();) {
349 SootMethod m = (SootMethod)it.next();
351 if (m.getName().endsWith("$$inc")) continue;
352 if (m.hasActiveBody()) b = fixBody(m.getActiveBody(), c, m);
353 List l2 = new LinkedList();
354 List l = m.getParameterTypes();
355 for(Iterator it2 = l.iterator(); it2.hasNext();) {
356 Type t = (Type)it2.next();
357 l2.add(implementsGladiator(t) ? IntType.v() : t);
359 Type t = m.getReturnType();
360 if (implementsGladiator(t)) {
362 if (m.hasActiveBody()) {
363 Body bod = m.getActiveBody();
364 for(Iterator stmtIt = bod.getUnits().snapshotIterator(); stmtIt.hasNext();) {
365 Stmt s = (Stmt) stmtIt.next();
366 if (s instanceof ReturnStmt) {
367 if (((ReturnStmt)s).getOp().getType() instanceof NullType) {
368 ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
374 String name = m.getName();
375 SootMethod meth = new SootMethod(name, l2, implementsGladiator(t) ? IntType.v() : t, m.getModifiers());
377 JimpleBody b2 = Jimple.v().newBody(meth);
378 b2.importBodyContentsFrom(b);
379 meth.setActiveBody(b2);
387 protected Body fixBody(Body body, SootClass ownerClass, SootMethod smeth) {
388 if (body instanceof JimpleBody) {
389 JimpleBody b2 = Jimple.v().newBody(smeth);
390 b2.importBodyContentsFrom(body);
393 Chain units = body.getUnits();
394 System.out.println("fixBody("+body.getMethod()+")");
395 for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) {
396 Local l = (Local)it.next();
397 if (implementsGladiator(l.getType())) l.setType(IntType.v());
399 for(int qq=0; qq<2; qq++) for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
400 Stmt s = (Stmt) stmtIt.next();
401 if (s instanceof DefinitionStmt) {
402 DefinitionStmt ds = (DefinitionStmt)s;
403 if (ds.getLeftOp().getType() instanceof PrimType && ds.getRightOp().getType() instanceof NullType) {
404 ds.getRightOpBox().setValue(IntConstant.v(-1));
407 if (implementsGladiator(smeth.getReturnType()) && s instanceof ReturnStmt)
408 if (((ReturnStmt)s).getOp().getType() instanceof NullType)
409 ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
410 List l = s.getUseAndDefBoxes();
411 for(Iterator it = l.iterator(); it.hasNext();) {
412 Object o = it.next();
413 if (o instanceof ValueBox) {
414 ValueBox vb = (ValueBox)o;
415 Value v = vb.getValue();
417 if (v instanceof BinopExpr) {
418 BinopExpr boe = (BinopExpr)v;
419 Type t1 = boe.getOp1().getType();
420 Type t2 = boe.getOp2().getType();
421 if (t1 instanceof PrimType && t2 instanceof NullType) boe.setOp2(IntConstant.v(-1));
422 if (t2 instanceof PrimType && t1 instanceof NullType) boe.setOp1(IntConstant.v(-1));
425 if (v instanceof NewExpr) {
426 NewExpr ne = (NewExpr)v;
427 if (implementsGladiator(ne.getBaseType())) {
428 SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
429 SootClass mc = getArenaForGladiator(sc);
430 String incFuncName = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$inc";
431 SootMethodRef smr = Scene.v().makeMethodRef(mc, incFuncName, new LinkedList(), IntType.v(), false);
432 Expr invokeExpr = Jimple.v().newSpecialInvokeExpr(body.getThisLocal(), smr);
433 Local ll = viaLocal(invokeExpr, body, s);
440 if (v instanceof InvokeExpr) {
441 InvokeExpr ie = (InvokeExpr)v;
442 SootMethodRef mr = ie.getMethodRef();
443 String name = mr.name();
444 if (v instanceof InstanceInvokeExpr && implementsGladiator(mr.declaringClass())) {
445 InstanceInvokeExpr iie = (InstanceInvokeExpr)v;
446 List li = new LinkedList();
447 li.addAll(iie.getArgs());
448 LinkedList pl = new LinkedList();
449 for(Iterator it2 = mr.parameterTypes().iterator(); it2.hasNext();) {
450 Type t = (Type)it2.next();
451 pl.add(implementsGladiator(t) ? IntType.v() : t);
453 if (mr.name().equals("<init>") && implementsGladiator(mr.declaringClass())) {
459 li.add(viaLocal(iie.getBase(),body,s));
460 SootClass sc = mr.declaringClass();
461 name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + name;
462 mr = Scene.v().makeMethodRef(getArenaForGladiator(sc),
465 implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
467 ie = Jimple.v().newVirtualInvokeExpr(body.getThisLocal(), mr, li);
470 } else if (!(v instanceof StaticInvokeExpr)) {
471 List l0 = mr.parameterTypes();
472 List l2 = new LinkedList();
473 for(Iterator it2 = l0.iterator(); it2.hasNext();) {
474 Type t = (Type)it2.next();
475 l2.add(implementsGladiator(t) ? IntType.v() : t);
477 mr = Scene.v().makeMethodRef(mr.declaringClass(),
480 implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
486 for(int i=0; i<ie.getArgCount(); i++) {
487 ValueBox b = ie.getArgBox(i);
488 Value val = b.getValue();
489 if (mr.parameterType(i) instanceof RefType && val.getType() instanceof PrimType) {
490 SootClass intClass = Scene.v().getSootClass("java.lang.Integer");
491 List typelist = new LinkedList();
492 typelist.add(IntType.v());
493 SootMethod intMethod = intClass.getMethod("<init>", typelist);
494 Local loc = viaLocal(Jimple.v().newNewExpr(RefType.v(intClass)), body, s);
495 List list = new LinkedList();
497 units.insertBefore(Jimple.v().newInvokeStmt(Jimple.v().newSpecialInvokeExpr(loc,
503 if (val != null && val.getType() instanceof NullType && mr.parameterType(i) instanceof IntType) {
504 b.setValue(IntConstant.v(-1));
509 } else if (v instanceof CastExpr) {
510 CastExpr ce = (CastExpr)v;
511 if (implementsGladiator(ce.getCastType())) {
512 SootClass mc = getArenaForGladiator(((RefType)ce.getCastType()).getSootClass());
513 SootClass ic = Scene.v().getSootClass("java.lang.Integer");
514 ce.setCastType(ic.getType());
516 Local l1 = Jimple.v().newLocal("tmpRef" + (tfr++), ic.getType()); body.getLocals().add(l1);
517 Local l2 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v()); body.getLocals().add(l2);
519 Stmt s2 = Jimple.v().newAssignStmt(l1, Jimple.v().newCastExpr(ce.getOp(), ic.getType()));
520 body.getUnits().insertBefore(s2, s);
522 Stmt isNull = Jimple.v().newAssignStmt(l2, IntConstant.v(-1));
523 body.getUnits().insertAfter(isNull, s2);
525 Stmt ifStmt = Jimple.v().newIfStmt(Jimple.v().newEqExpr(l1, NullConstant.v()), s);
526 body.getUnits().insertAfter(ifStmt, isNull);
528 SootMethodRef mr = Scene.v().makeMethodRef(ic, "intValue", new LinkedList(), IntType.v(), false);
530 Jimple.v().newAssignStmt(l2, Jimple.v().newVirtualInvokeExpr(l1, mr, new LinkedList()));
531 body.getUnits().insertAfter(isNotNull, ifStmt);
538 } else if (v instanceof InstanceFieldRef) {
539 InstanceFieldRef ifr = (InstanceFieldRef)v;
540 SootFieldRef fr = ifr.getFieldRef();
542 if (implementsGladiator(fr.declaringClass()) && fr.name().equals("this$0")) {
543 System.out.println("******************");
544 vb.setValue(body.getThisLocal());
545 System.out.println("s is " + s);
546 } else if (implementsGladiator(fr.declaringClass())) {
547 SootClass mc = getArenaForGladiator(fr.declaringClass());
548 InstanceFieldRef sfr = newIFR(body, getSliceForField(fr));
549 vb.setValue(Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase()));
551 if ((t instanceof RefType) && implementsGladiator(((RefType)t).getSootClass())) {
552 SootClass tc = ((RefType)t).getSootClass();
553 SootClass mc = getArenaForGladiator(tc);
554 ifr.setFieldRef(Scene.v().makeFieldRef(mc, fr.name(), IntType.v(), false));