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 SootField getGladiatorField(SootField f) {
40 SootClass c = f.getDeclaringClass();
41 SootClass oc = Scene.v().getSootClass(c.getName().substring(0, c.getName().lastIndexOf('$')));
42 if (map.get(f) != null) return (SootField)map.get(f);
43 SootField nf = new SootField(c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$" + f.getName(),
44 f.getType().makeArrayType(),
47 Body body = oc.getMethod("<init>", new LinkedList()).getActiveBody();
48 Expr newArr = Jimple.v().newNewArrayExpr(f.getType(), IntConstant.v(1000));
49 Local newArrLocal = Jimple.v().newLocal("tmpRef" + (tfr++), f.getType().makeArrayType());
50 body.getLocals().add(newArrLocal);
51 InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), nf.makeRef());
52 body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, newArrLocal));
53 body.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
58 public SootFieldRef getGladiatorFieldSizeRef(SootClass c) {
59 SootClass mc = Scene.v().getMainClass();
60 String name = c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "_size";
61 if (map.get(c) == null) {
62 SootField f = new SootField(name, IntType.v());
64 Body body = mc.getMethod("<init>", new LinkedList()).getActiveBody();
65 InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), f.makeRef());
66 body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, IntConstant.v(0)));
70 return Scene.v().makeFieldRef(mc, name, IntType.v(), false);
73 public boolean isGladiatorType(Type t) {
74 return (t instanceof RefType) && isGladiator(((RefType)t).getSootClass());
77 protected void internalTransform(Body body, String phaseName, Map options) {
78 SootClass c = body.getMethod().getDeclaringClass();
80 for(Iterator it = c.getFields().iterator(); it.hasNext();) {
81 SootField f = (SootField)it.next();
83 if (!(t instanceof RefType)) continue;
84 if (isGladiator(((RefType)t).getSootClass())) f.setType(IntType.v());
87 for(Iterator it = c.methodIterator(); it.hasNext();) {
88 SootMethod m = (SootMethod)it.next();
89 System.out.println(m.getName() + " -- " + m.getActiveBody());
91 List l2 = new LinkedList();
92 List l = m.getParameterTypes();
93 for(Iterator it2 = l.iterator(); it2.hasNext();) {
94 Type t = (Type)it2.next();
95 l2.add(isGladiatorType(t) ? IntType.v() : t);
97 m.setParameterTypes(l2);
98 Type t = m.getReturnType();
99 m.setReturnType(isGladiatorType(t) ? IntType.v() : t);
103 for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) {
104 Local l = (Local)it.next();
105 if (isGladiatorType(l.getType())) l.setType(IntType.v());
108 Chain units = body.getUnits();
109 Iterator stmtIt = units.snapshotIterator();
110 while(stmtIt.hasNext()) {
111 Stmt s = (Stmt) stmtIt.next();
112 List l = s.getUseAndDefBoxes();
113 for(Iterator it = l.iterator(); it.hasNext();) {
114 Object o = it.next();
115 if (o instanceof ValueBox) {
116 ValueBox vb = (ValueBox)o;
117 Value v = vb.getValue();
119 if (v instanceof InvokeExpr) {
120 InvokeExpr ie = (InvokeExpr)v;
121 SootMethod m = ie.getMethod();
122 if (isGladiator(m.getDeclaringClass())) {
123 body.getUnits().remove(s);
127 } else if (v instanceof NewExpr) {
128 NewExpr ne = (NewExpr)v;
129 if (isGladiatorType(ne.getBaseType())) {
130 System.out.println("******");
131 SootClass mc = Scene.v().getMainClass();
132 SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
133 System.out.println(sc);
134 InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(body.getThisLocal(),
135 getGladiatorFieldSizeRef(sc));
136 Local ll = viaLocal(sfr, body, s);
137 Local ll2 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v());
138 body.getLocals().add(ll2);
139 Stmt stmt = Jimple.v().newAssignStmt(ll2, Jimple.v().newAddExpr(ll, IntConstant.v(1)));
140 units.insertBefore(stmt, s);
141 units.insertAfter(Jimple.v().newAssignStmt(sfr, ll2), stmt);
145 } else if (v instanceof InstanceFieldRef) {
146 InstanceFieldRef ifr = (InstanceFieldRef)v;
147 Type t = ifr.getType();
148 if ((t instanceof RefType) && isGladiator(((RefType)t).getSootClass())) {
149 SootClass tc = ((RefType)t).getSootClass();
150 SootClass mc = Scene.v().getMainClass();
151 SootFieldRef fr = Scene.v().makeFieldRef(mc, "z", IntType.v(), false);
154 SootField f = ifr.getField();
155 if (isGladiatorField(f)) {
156 f = getGladiatorField(f);
157 SootClass mc = Scene.v().getMainClass();
158 SootField sf = mc.getField(f.getName(), f.getType());
159 InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), sf.makeRef());
160 ArrayRef ar = Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase());