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();
92 public boolean implementsGladiator(SootClass c) { return c.implementsInterface("org.ibex.arenaj.Gladiator"); }
93 public SootField getSliceForField(SootField f) { return getSliceForField(f.makeRef()); }
94 public SootField getSliceForField(SootFieldRef f) {
95 SootClass c = f.declaringClass();
96 SootClass oc = Scene.v().getSootClass(c.getName().substring(0, c.getName().lastIndexOf('$')));
97 String sig = f.declaringClass().getName()+"."+f.name();
98 if (map.get(sig) != null) return (SootField)map.get(sig);
100 if (t instanceof RefType && implementsGladiator(((RefType)t).getSootClass())) t = IntType.v();
101 SootField nf = new SootField(c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$" + f.name(),
106 Body body = getInitBody(oc);
107 Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(initialSize));
108 Local newArrLocal = Jimple.v().newLocal("tmpRef" + (tfr++), f.type().makeArrayType());
109 body.getLocals().add(newArrLocal);
110 InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), nf.makeRef());
111 body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, newArrLocal));
112 body.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
118 public Body getInitBody(SootClass c) {
119 c.setApplicationClass();
120 List list = new LinkedList();
121 SootMethod m = c.getMethod("<init>", list);
122 if (!m.hasActiveBody()) {
123 JimpleBody b = (JimpleBody)Jimple.v().newBody(m);
126 return m.getActiveBody();
129 public boolean implementsGladiator(Type t) { return (t instanceof RefType)&&implementsGladiator(((RefType)t).getSootClass()); }
131 public void internalTransform(String phaseName, Map options) {
132 System.out.println("begun");
134 List ac = new LinkedList();
135 for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
136 SootClass sc = (SootClass)it.next();
137 sc.setLibraryClass();
139 for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
140 SootClass sc = (SootClass)it.next();
141 if (implementsGladiator(sc)) {
142 System.out.println(" application class: " + sc.getName());
143 System.out.println(" application class: " + getParent(sc).getName());
144 sc.setApplicationClass();
145 getParent(sc).setApplicationClass();
147 ac.add(getParent(sc));
148 for(Iterator i2 = sc.getMethods().iterator(); i2.hasNext();) {
149 SootMethod m = (SootMethod)i2.next();
150 if (m.isConcrete()) m.retrieveActiveBody();
152 for(Iterator i2 = getParent(sc).getMethods().iterator(); i2.hasNext();) {
153 SootMethod m = (SootMethod)i2.next();
154 if (m.isConcrete()) m.retrieveActiveBody();
158 for(Iterator it = ac.iterator(); it.hasNext();) {
159 SootClass sc = (SootClass)it.next();
160 if (sc.isInterface()) continue;
161 if (!implementsGladiator(sc)) continue;
162 System.out.println("fixing Gladiator class: " + sc);
165 for(Iterator it = ac.iterator(); it.hasNext();) {
166 SootClass sc = (SootClass)it.next();
167 if (sc.isInterface()) continue;
168 System.out.println(" updating class: " + sc);
173 public SootClass getParent(SootClass sc) {
174 return Scene.v().getSootClass(sc.getName().substring(0, sc.getName().lastIndexOf('$')));
177 public static InstanceFieldRef newIFR(Body body, SootFieldRef fr) {
178 return Jimple.v().newInstanceFieldRef(body.getThisLocal(), fr);
181 public Type objectType;
182 public List arrayCopyTypes = new LinkedList();
183 public SootMethodRef arrayCopy;
185 objectType = Scene.v().getSootClass("java.lang.Object").getType();
186 arrayCopyTypes.add(objectType);
187 arrayCopyTypes.add(IntType.v());
188 arrayCopyTypes.add(objectType);
189 arrayCopyTypes.add(IntType.v());
190 arrayCopyTypes.add(IntType.v());
192 Scene.v().makeMethodRef(Scene.v().getSootClass("java.lang.System"),
199 public static void assign(Body b, Value left, Value right) { b.getUnits().add(Jimple.v().newAssignStmt(left, right)); }
200 public static void assign(Body b, Value left, Value right, Stmt where) {
201 b.getUnits().insertBefore(Jimple.v().newAssignStmt(left, right), where);
204 public void fixClass(SootClass sc) {
205 SootClass mc = getParent(sc);
206 String subname = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1);
207 String incFuncName = subname + "$$inc";
208 SootMethod method = new SootMethod(incFuncName, new LinkedList(), IntType.v(), Modifier.PRIVATE, new LinkedList());
209 Body mcInitBody = getInitBody(mc);
210 mc.addMethod(method);
211 Body body = Jimple.v().newBody(method);
212 method.setActiveBody(body);
213 ((JimpleBody)body).insertIdentityStmts();
214 Local l = newLocal(body, IntType.v());
215 Local l2 = newLocal(body, IntType.v());
216 Local l3 = newLocal(body, IntType.v());
217 Local l4 = newLocal(body, IntType.v());
219 Stmt where = ((JimpleBody)mcInitBody).getFirstNonIdentityStmt();
220 SootField maxField = new SootField(subname + "$$max", IntType.v());
221 mc.addField(maxField);
222 assign(mcInitBody, newIFR(mcInitBody, maxField.makeRef()), IntConstant.v(initialSize), where);
224 SootField sfr = new SootField(subname + "$$size", IntType.v());
226 assign(mcInitBody, newIFR(mcInitBody, sfr.makeRef()), IntConstant.v(0), where);
228 assign(body, l, newIFR(body, sfr.makeRef()));
229 assign(body, l2, Jimple.v().newAddExpr(l, IntConstant.v(1)));
230 assign(body, newIFR(body, sfr.makeRef()), l2);
231 assign(body, l3, newIFR(body, maxField.makeRef()));
233 Stmt returnStmt = Jimple.v().newReturnStmt(l2);
234 body.getUnits().add(Jimple.v().newIfStmt(Jimple.v().newLtExpr(l2, l3), returnStmt));
236 assign(body, l4, Jimple.v().newShlExpr(l3, IntConstant.v(1)));
237 assign(body, newIFR(body, maxField.makeRef()), l4);
239 for(Iterator it = sc.getFields().iterator(); it.hasNext();) {
240 SootField f = getSliceForField((SootField)it.next());
241 Local ll0 = newLocal(body, f.getType());
242 assign(body, ll0, newIFR(body, f.makeRef()));
243 Local ll = newLocal(body, f.getType());
244 assign(body, ll, Jimple.v().newNewArrayExpr(((ArrayType)f.getType()).getElementType(), l4));
245 List args = new LinkedList();
247 args.add(IntConstant.v(0));
249 args.add(IntConstant.v(0));
251 body.getUnits().add(Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(arrayCopy, args)));
252 body.getUnits().add(Jimple.v().newAssignStmt(newIFR(body, f.makeRef()), ll));
254 for(Iterator it = sc.getMethods().iterator(); it.hasNext();) {
255 SootMethod m = (SootMethod)it.next();
256 if (!m.isConcrete()) continue;
257 boolean doremove = true;
258 Body mbody = m.getActiveBody();
259 if (implementsGladiator(m.getDeclaringClass()) && m.getName().equals("<init>")) {
261 SootClass c = m.getDeclaringClass();
262 String name = /*c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + */"$init";
263 List li = m.getParameterTypes();
265 SootMethod nm = new SootMethod(name, li, implementsGladiator(m.getReturnType()) ? IntType.v() : m.getReturnType());
266 JimpleBody bod = Jimple.v().newBody(nm);
267 bod.importBodyContentsFrom(m.getActiveBody());
268 nm.setActiveBody(bod);
271 mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
272 mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
274 System.out.println("examining " + sc.getName() + "." + m.getSignature());
275 m.retrieveActiveBody();
277 if (m.isStatic()) continue;
279 String name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + m.getName();
280 List list = new LinkedList();
281 list.addAll(m.getParameterTypes());
282 list.add(IntType.v());
283 for(Iterator i = list.iterator(); i.hasNext();) System.out.println(i.next());
284 SootMethod m2 = new SootMethod(name, list, m.getReturnType());
285 getParent(sc).addMethod(m2);
287 JimpleBody ab = (JimpleBody)Jimple.v().newBody(m2);
288 ab.importBodyContentsFrom(mbody);
289 m2.setActiveBody(ab);
290 Local loc = Jimple.v().newLocal("tmpRef" + (tfr++), getParent(sc).getType());
291 ab.getLocals().add(loc);
292 // FIXME: insert assignment to this
294 for(Iterator z = ab.getLocals().iterator(); z.hasNext();) {
295 Local loc2 = (Local)z.next();
296 if (implementsGladiator(loc2.getType())) {
297 loc2.setType(IntType.v());
301 Chain units = ab.getUnits();
302 boolean touched = false;
303 Local loc0 = Jimple.v().newLocal("tmpRef" + (tfr++), getParent(sc).getType());
304 ab.getLocals().add(loc0);
305 for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
306 Stmt s = (Stmt) stmtIt.next();
307 if (s instanceof IdentityStmt) {
308 IdentityStmt is = (IdentityStmt)s;
309 Local left = (Local)is.getLeftOp();
310 if (is.getRightOp() instanceof ThisRef) {
311 left.setType(IntType.v());
312 is.getRightOpBox().setValue(Jimple.v().newParameterRef(IntType.v(), m.getParameterCount()));
314 units.addFirst(Jimple.v().newIdentityStmt(loc0, Jimple.v().newThisRef(getParent(sc).getType())));
320 for(Iterator i = s.getUseAndDefBoxes().iterator(); i.hasNext();) {
322 if (o instanceof ValueBox) {
323 ValueBox vb = (ValueBox)o;
326 if (o instanceof Local && implementsGladiator(((Local)o).getType())) {
327 System.out.println("thunking");
331 if (vb.getValue() instanceof ThisRef) {
332 System.out.println(s);
340 if (doremove) sc.removeMethod(m);
343 body.getUnits().add(returnStmt);
346 public void nuke(SootClass c) {
347 for(Iterator it = c.getFields().iterator(); it.hasNext();) {
348 SootField f = (SootField)it.next();
349 Type t = f.getType();
350 if (t instanceof RefType) {
351 RefType rt = (RefType)t;
352 SootClass sc = rt.getSootClass();
353 if (implementsGladiator(sc)) f.setType(IntType.v());
354 } else if (t instanceof ArrayType) {
355 ArrayType at = (ArrayType)t;
356 t = at.getElementType();
357 if (!(t instanceof RefType)) continue;
358 RefType rt = (RefType)t;
359 SootClass sc = rt.getSootClass();
360 if (implementsGladiator(sc)) f.setType(IntType.v().makeArrayType());
364 List list = new LinkedList(); list.addAll(c.getMethods());
365 for(Iterator it = list.iterator(); it.hasNext();) {
366 SootMethod m = (SootMethod)it.next();
368 if (m.getName().endsWith("$$inc")) continue;
369 if (m.hasActiveBody()) b = fixBody(m.getActiveBody(), c, m);
370 List l2 = new LinkedList();
371 List l = m.getParameterTypes();
372 for(Iterator it2 = l.iterator(); it2.hasNext();) {
373 Type t = (Type)it2.next();
374 l2.add(implementsGladiator(t) ? IntType.v() : t);
376 Type t = m.getReturnType();
377 if (implementsGladiator(t)) {
379 if (m.hasActiveBody()) {
380 Body bod = m.getActiveBody();
381 for(Iterator stmtIt = bod.getUnits().snapshotIterator(); stmtIt.hasNext();) {
382 Stmt s = (Stmt) stmtIt.next();
383 if (s instanceof ReturnStmt) {
384 if (((ReturnStmt)s).getOp().getType() instanceof NullType) {
385 ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
391 String name = m.getName();
392 SootMethod meth = new SootMethod(name, l2, implementsGladiator(t) ? IntType.v() : t, m.getModifiers());
394 JimpleBody b2 = Jimple.v().newBody(meth);
395 b2.importBodyContentsFrom(b);
396 meth.setActiveBody(b2);
404 protected Body fixBody(Body body, SootClass ownerClass, SootMethod smeth) {
405 if (body instanceof JimpleBody) {
406 JimpleBody b2 = Jimple.v().newBody(smeth);
407 b2.importBodyContentsFrom(body);
410 Chain units = body.getUnits();
411 System.out.println("fixBody("+body.getMethod()+")");
412 for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) {
413 Local l = (Local)it.next();
414 if (implementsGladiator(l.getType())) l.setType(IntType.v());
416 for(int qq=0; qq<2; qq++) for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
417 Stmt s = (Stmt) stmtIt.next();
418 if (s instanceof DefinitionStmt) {
419 DefinitionStmt ds = (DefinitionStmt)s;
420 if (ds.getLeftOp().getType() instanceof PrimType && ds.getRightOp().getType() instanceof NullType) {
421 ds.getRightOpBox().setValue(IntConstant.v(-1));
424 if (implementsGladiator(smeth.getReturnType()) && s instanceof ReturnStmt)
425 if (((ReturnStmt)s).getOp().getType() instanceof NullType)
426 ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
427 List l = s.getUseAndDefBoxes();
428 for(Iterator it = l.iterator(); it.hasNext();) {
429 Object o = it.next();
430 if (o instanceof ValueBox) {
431 ValueBox vb = (ValueBox)o;
432 Value v = vb.getValue();
434 if (v instanceof BinopExpr) {
435 BinopExpr boe = (BinopExpr)v;
436 Type t1 = boe.getOp1().getType();
437 Type t2 = boe.getOp2().getType();
438 if (t1 instanceof PrimType && t2 instanceof NullType) boe.setOp2(IntConstant.v(-1));
439 if (t2 instanceof PrimType && t1 instanceof NullType) boe.setOp1(IntConstant.v(-1));
442 if (v instanceof NewExpr) {
443 NewExpr ne = (NewExpr)v;
444 if (implementsGladiator(ne.getBaseType())) {
445 SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
446 SootClass mc = getParent(sc);
447 String incFuncName = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$inc";
448 SootMethodRef smr = Scene.v().makeMethodRef(mc, incFuncName, new LinkedList(), IntType.v(), false);
449 Expr invokeExpr = Jimple.v().newSpecialInvokeExpr(body.getThisLocal(), smr);
450 Local ll = viaLocal(invokeExpr, body, s);
457 if (v instanceof InvokeExpr) {
458 InvokeExpr ie = (InvokeExpr)v;
459 SootMethodRef mr = ie.getMethodRef();
460 String name = mr.name();
461 if (v instanceof InstanceInvokeExpr && implementsGladiator(mr.declaringClass())) {
462 InstanceInvokeExpr iie = (InstanceInvokeExpr)v;
463 List li = new LinkedList();
464 li.addAll(iie.getArgs());
465 LinkedList pl = new LinkedList();
466 for(Iterator it2 = mr.parameterTypes().iterator(); it2.hasNext();) {
467 Type t = (Type)it2.next();
468 pl.add(implementsGladiator(t) ? IntType.v() : t);
470 if (mr.name().equals("<init>") && implementsGladiator(mr.declaringClass())) {
476 li.add(viaLocal(iie.getBase(),body,s));
477 SootClass sc = mr.declaringClass();
478 name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + name;
479 mr = Scene.v().makeMethodRef(getParent(sc),
482 implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
484 ie = Jimple.v().newVirtualInvokeExpr(body.getThisLocal(), mr, li);
487 } else if (!(v instanceof StaticInvokeExpr)) {
488 List l0 = mr.parameterTypes();
489 List l2 = new LinkedList();
490 for(Iterator it2 = l0.iterator(); it2.hasNext();) {
491 Type t = (Type)it2.next();
492 l2.add(implementsGladiator(t) ? IntType.v() : t);
494 mr = Scene.v().makeMethodRef(mr.declaringClass(),
497 implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
503 for(int i=0; i<ie.getArgCount(); i++) {
504 ValueBox b = ie.getArgBox(i);
505 Value val = b.getValue();
506 if (mr.parameterType(i) instanceof RefType && val.getType() instanceof PrimType) {
507 SootClass intClass = Scene.v().getSootClass("java.lang.Integer");
508 List typelist = new LinkedList();
509 typelist.add(IntType.v());
510 SootMethod intMethod = intClass.getMethod("<init>", typelist);
511 Local loc = viaLocal(Jimple.v().newNewExpr(RefType.v(intClass)), body, s);
512 List list = new LinkedList();
514 units.insertBefore(Jimple.v().newInvokeStmt(Jimple.v().newSpecialInvokeExpr(loc,
520 if (val != null && val.getType() instanceof NullType && mr.parameterType(i) instanceof IntType) {
521 b.setValue(IntConstant.v(-1));
526 } else if (v instanceof CastExpr) {
527 CastExpr ce = (CastExpr)v;
528 if (implementsGladiator(ce.getCastType())) {
529 SootClass mc = getParent(((RefType)ce.getCastType()).getSootClass());
530 SootClass ic = Scene.v().getSootClass("java.lang.Integer");
531 ce.setCastType(ic.getType());
533 Local l1 = Jimple.v().newLocal("tmpRef" + (tfr++), ic.getType()); body.getLocals().add(l1);
534 Local l2 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v()); body.getLocals().add(l2);
536 Stmt s2 = Jimple.v().newAssignStmt(l1, Jimple.v().newCastExpr(ce.getOp(), ic.getType()));
537 body.getUnits().insertBefore(s2, s);
539 Stmt isNull = Jimple.v().newAssignStmt(l2, IntConstant.v(-1));
540 body.getUnits().insertAfter(isNull, s2);
542 Stmt ifStmt = Jimple.v().newIfStmt(Jimple.v().newEqExpr(l1, NullConstant.v()), s);
543 body.getUnits().insertAfter(ifStmt, isNull);
545 SootMethodRef mr = Scene.v().makeMethodRef(ic, "intValue", new LinkedList(), IntType.v(), false);
547 Jimple.v().newAssignStmt(l2, Jimple.v().newVirtualInvokeExpr(l1, mr, new LinkedList()));
548 body.getUnits().insertAfter(isNotNull, ifStmt);
555 } else if (v instanceof InstanceFieldRef) {
556 InstanceFieldRef ifr = (InstanceFieldRef)v;
557 SootFieldRef fr = ifr.getFieldRef();
559 if (implementsGladiator(fr.declaringClass()) && fr.name().equals("this$0")) {
560 System.out.println("******************");
561 vb.setValue(body.getThisLocal());
562 System.out.println("s is " + s);
563 } else if (implementsGladiator(fr.declaringClass())) {
564 SootClass mc = getParent(fr.declaringClass());
565 SootFieldRef sf = getSliceForField(fr).makeRef();
566 InstanceFieldRef sfr = newIFR(body, sf);
567 vb.setValue(Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase()));
569 if ((t instanceof RefType) && implementsGladiator(((RefType)t).getSootClass())) {
570 SootClass tc = ((RefType)t).getSootClass();
571 SootClass mc = getParent(tc);
572 ifr.setFieldRef(Scene.v().makeFieldRef(mc, fr.name(), IntType.v(), false));