1 package edu.berkeley.cs.megacz;
8 public class Transformer extends BodyTransformer {
10 private static Transformer instance = new Transformer();
11 private Transformer() {}
12 public static Transformer v() { return instance; }
14 public static void main(String[] args) {
15 if(args.length == 0) {
16 System.out.println("Syntax: java " + v().getClass().getName() + " [soot options]");
19 PackManager.v().getPack("jtp").add(new Transform("jtp.instrumenter", Transformer.v()));
20 // Just in case, resolve the PrintStream SootClass.
21 Scene.v().addBasicClass("java.io.PrintStream",SootClass.SIGNATURES);
26 public static Local viaLocal(Value v, Body b, Unit u) {
27 Local l = Jimple.v().newLocal("tmpRef" + (tfr++), v.getType());
29 b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, v), u);
33 HashMap map = new HashMap();
34 HashSet size_added = new HashSet();
36 public boolean isGladiator(SootClass c) { return c.implementsInterface("edu.berkeley.cs.megacz.Gladiator"); }
37 public boolean isGladiatorField(SootField f)
38 { return isGladiator(f.getDeclaringClass()) && !f.getName().equals("this") && f.getName().indexOf('$')==-1; }
39 public boolean isGladiatorFieldRef(SootFieldRef f) {
40 return isGladiator(f.declaringClass()) && !f.name().equals("this") && f.name().indexOf('$')==-1;
42 public SootField getGladiatorField(SootField f) { return getGladiatorField(f.makeRef()); }
43 public SootField getGladiatorField(SootFieldRef f) {
44 SootClass c = f.declaringClass();
45 SootClass oc = Scene.v().getSootClass(c.getName().substring(0, c.getName().lastIndexOf('$')));
46 String sig = f.declaringClass().getName()+"."+f.name();
47 if (map.get(sig) != null) return (SootField)map.get(sig);
49 if (t instanceof RefType && isGladiator(((RefType)t).getSootClass())) t = IntType.v();
50 SootField nf = new SootField(c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$" + f.name(),
54 Body body = oc.getMethod("<init>", new LinkedList()).getActiveBody();
55 Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(100000));
56 Local newArrLocal = Jimple.v().newLocal("tmpRef" + (tfr++), f.type().makeArrayType());
57 body.getLocals().add(newArrLocal);
58 InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), nf.makeRef());
59 body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, newArrLocal));
60 body.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
66 public SootFieldRef getGladiatorFieldSizeRef(SootClass c) {
67 SootClass mc = Scene.v().getMainClass();
68 String name = c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "_size";
69 if (map.get(c) == null) {
70 SootField f = new SootField(name, IntType.v());
73 Body body = mc.getMethod("<init>", new LinkedList()).getActiveBody();
74 InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), f.makeRef());
75 body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, IntConstant.v(0)));
79 return Scene.v().makeFieldRef(mc, name, IntType.v(), false);
82 public boolean isGladiatorType(Type t) {
83 return (t instanceof RefType) && isGladiator(((RefType)t).getSootClass());
86 public SootMethodRef convert(SootMethodRef mr) {
87 List l = mr.parameterTypes();
88 List l2 = new LinkedList();
89 for(Iterator it2 = l.iterator(); it2.hasNext();) {
90 Type t = (Type)it2.next();
91 l2.add(isGladiatorType(t) ? IntType.v() : t);
93 return Scene.v().makeMethodRef(mr.declaringClass(),
96 isGladiatorType(mr.returnType()) ? IntType.v() : mr.returnType(),
100 boolean done = false;
101 public void internalTransform(Body bz, String phaseName, Map options) {
102 System.out.println("============= " + phaseName);
105 //nuke(Scene.v().getSootClass("edu.berkeley.cs.megacz.Test$Inner"));
106 //nuke(Scene.v().getSootClass("edu.berkeley.cs.megacz.Test"));
107 nuke(Scene.v().getSootClass("org.ibex.graphics.Mesh"));
108 nuke(Scene.v().getSootClass("org.ibex.graphics.Mesh$Triangle"));
110 public void nuke(SootClass c) {
111 for(Iterator it = c.getFields().iterator(); it.hasNext();) {
112 SootField f = (SootField)it.next();
113 Type t = f.getType();
114 if (t instanceof RefType) {
115 RefType rt = (RefType)t;
116 SootClass sc = rt.getSootClass();
117 if (isGladiator(sc)) f.setType(IntType.v());
118 } else if (t instanceof ArrayType) {
119 ArrayType at = (ArrayType)t;
120 t = at.getElementType();
121 if (!(t instanceof RefType)) continue;
122 RefType rt = (RefType)t;
123 SootClass sc = rt.getSootClass();
124 if (isGladiator(sc)) f.setType(IntType.v().makeArrayType());
128 List list = new LinkedList(); list.addAll(c.getMethods());
129 for(Iterator it = list.iterator(); it.hasNext();) {
130 SootMethod m = (SootMethod)it.next();
131 if (m.hasActiveBody()) m.setActiveBody(fixBody(m.getActiveBody()));
132 if (m.getName().equals("<init>")) continue;
133 if (m.getName().equals("<clinit>")) continue;
134 //if (map.get(m) != null) continue;
135 //map.put(m, Boolean.TRUE);
136 System.out.println(m.getName() + " -- " + m.getActiveBody());
137 List l2 = new LinkedList();
138 List l = m.getParameterTypes();
139 for(Iterator it2 = l.iterator(); it2.hasNext();) {
140 Type t = (Type)it2.next();
142 l2.add(isGladiatorType(t) ? IntType.v() : t);
143 //if (isGladiatorType(t)) System.out.println("ought to swap " + t);
145 Type t = m.getReturnType();
147 if (isGladiatorType(t)) {
149 Body bod = m.getActiveBody();
150 for(Iterator stmtIt = bod.getUnits().snapshotIterator(); stmtIt.hasNext();) {
151 Stmt s = (Stmt) stmtIt.next();
152 if (s instanceof ReturnStmt) {
153 if (((ReturnStmt)s).getOp().getType() instanceof NullType) {
154 ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
159 SootMethod meth = new SootMethod(m.getName(), l2, t, m.getModifiers());
160 meth.setActiveBody(m.getActiveBody());
166 protected Body fixBody(Body body) {
168 Chain units = body.getUnits();
169 for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) {
170 Local l = (Local)it.next();
171 if (isGladiatorType(l.getType())) l.setType(IntType.v());
173 for(int qq=0; qq<2; qq++) for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
174 Stmt s = (Stmt) stmtIt.next();
175 List l = s.getUseAndDefBoxes();
176 if (s instanceof DefinitionStmt) {
177 DefinitionStmt ds = (DefinitionStmt)s;
178 if (ds.getLeftOp().getType() instanceof PrimType && ds.getRightOp().getType() instanceof NullType) {
179 System.out.println("*************************");
180 ds.getRightOpBox().setValue(IntConstant.v(-1));
183 for(Iterator it = l.iterator(); it.hasNext();) {
184 Object o = it.next();
185 if (o instanceof ValueBox) {
186 ValueBox vb = (ValueBox)o;
187 Value v = vb.getValue();
189 if (v instanceof BinopExpr) {
190 BinopExpr boe = (BinopExpr)v;
191 if (boe.getOp1().getType() instanceof PrimType && boe.getOp2().getType() instanceof NullType) {
192 System.out.println("*************************");
193 boe.setOp2(IntConstant.v(-1));
195 if (boe.getOp2().getType() instanceof PrimType && boe.getOp1().getType() instanceof NullType) {
196 System.out.println("*************************");
197 boe.setOp1(IntConstant.v(-1));
201 if (v instanceof InvokeExpr) {
202 InvokeExpr ie = (InvokeExpr)v;
203 SootMethodRef mr = ie.getMethodRef();
204 if (isGladiator(mr.declaringClass())) {
205 body.getUnits().remove(s);
210 for(int i=0; i<ie.getArgCount(); i++) {
211 ValueBox b = ie.getArgBox(i);
212 Value val = b.getValue();
213 if (mr.parameterType(i) instanceof RefType && val.getType() instanceof PrimType) {
214 SootClass intClass = Scene.v().getSootClass("java.lang.Integer");
215 List typelist = new LinkedList();
216 typelist.add(IntType.v());
217 SootMethod intMethod = intClass.getMethod("<init>", typelist);
218 Local loc = viaLocal(Jimple.v().newNewExpr(RefType.v(intClass)), body, s);
219 List list = new LinkedList();
221 units.insertBefore(Jimple.v().newInvokeStmt(Jimple.v().newSpecialInvokeExpr(loc,
227 if (val != null && val.getType() instanceof NullType && mr.parameterType(i) instanceof IntType) {
228 b.setValue(IntConstant.v(-1));
232 } else if (v instanceof NewExpr) {
233 NewExpr ne = (NewExpr)v;
234 if (isGladiatorType(ne.getBaseType())) {
235 System.out.println("******");
236 SootClass mc = Scene.v().getMainClass();
237 SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
238 System.out.println(sc);
239 InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(body.getThisLocal(),
240 getGladiatorFieldSizeRef(sc));
241 Local ll = viaLocal(sfr, body, s);
242 Local ll2 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v());
243 body.getLocals().add(ll2);
244 Stmt stmt = Jimple.v().newAssignStmt(ll2, Jimple.v().newAddExpr(ll, IntConstant.v(1)));
245 units.insertBefore(stmt, s);
246 units.insertAfter(Jimple.v().newAssignStmt(sfr, ll2), stmt);
250 } else if (v instanceof CastExpr) {
251 CastExpr ce = (CastExpr)v;
252 if (isGladiatorType(ce.getCastType())) {
253 //SootClass intClass = Scene.v().getSootClass("java.lang.Integer");
254 //SootClass intClass = Scene.v().getSootClass("java.lang.Integer");
255 SootClass mc = Scene.v().getMainClass();
256 LinkedList list = new LinkedList();
257 list.add(Scene.v().getSootClass("java.lang.Object").getType());
258 SootMethodRef mr = Scene.v().makeMethodRef(mc, "zap", list, IntType.v(), true);
259 Local loc = viaLocal(ce.getOp(), body, s);
260 List list2 = new LinkedList();
262 vb.setValue(viaLocal(Jimple.v().newStaticInvokeExpr(mr, list2), body, s));
265 } else if (v instanceof InstanceFieldRef) {
266 InstanceFieldRef ifr = (InstanceFieldRef)v;
267 SootFieldRef fr = ifr.getFieldRef();
269 if (isGladiatorFieldRef(fr)) {
270 SootClass mc = Scene.v().getMainClass();
271 SootFieldRef sf = getGladiatorField(fr).makeRef();
272 InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), sf);
273 ArrayRef ar = Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase());
276 if ((t instanceof RefType) && isGladiator(((RefType)t).getSootClass())) {
277 SootClass tc = ((RefType)t).getSootClass();
278 SootClass mc = Scene.v().getMainClass();
279 ifr.setFieldRef(Scene.v().makeFieldRef(mc, fr.name(), IntType.v(), false));