checkpoint
[org.ibex.arenaj.git] / src / org / ibex / arenaj / Main.java
index ea31f02..abf7552 100644 (file)
@@ -30,7 +30,6 @@ public class Main extends SceneTransformer {
             StringTokenizer st = new StringTokenizer(extDirs, File.pathSeparatorChar+"");
             while(st.hasMoreTokens()) {
                 String goo = st.nextToken();
-                System.out.println("goo " + goo);
                 File f = new File(goo);
                 if (!f.exists() || !f.isDirectory()) continue;
                 String[] jars = f.list();
@@ -48,7 +47,6 @@ public class Main extends SceneTransformer {
         args2[args.length + 6] = "-p";
         args2[args.length + 7] = "jtp";
         args2[args.length + 8] = "enabled:false";
-        //args2[args.length + 9] = "-keep-line-number";
         args2[args.length + 9] = "-allow-phantom-refs";
         args2[args.length + 10] = "-allow-phantom-refs";
         args2[args.length + 11] = "-cp";
@@ -62,7 +60,6 @@ public class Main extends SceneTransformer {
     // The main loop //////////////////////////////////////////////////////////////////////////////
 
     public void internalTransform(String phaseName, Map options) {
-        System.out.println("begun");
         init();
         List ac = new LinkedList();
         for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
@@ -71,24 +68,13 @@ public class Main extends SceneTransformer {
         }
         for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
             SootClass sc = (SootClass)it.next();
-            if (implementsGladiator(sc)) {
-                System.out.println("  application class: " + sc.getName());
-                System.out.println("  application class: " + getArenaForGladiator(sc).getName());
-                sc.setApplicationClass();
-                getArenaForGladiator(sc).setApplicationClass();
-                ac.add(sc);
-                ac.add(getArenaForGladiator(sc));
-                /*
-                for(Iterator i2 = sc.getMethods().iterator(); i2.hasNext();) {
-                    SootMethod m = (SootMethod)i2.next();
-                    if (m.isConcrete()) m.retrieveActiveBody();
-                }
-                for(Iterator i2 = getArenaForGladiator(sc).getMethods().iterator(); i2.hasNext();) {
-                    SootMethod m = (SootMethod)i2.next();
-                    if (m.isConcrete()) m.retrieveActiveBody();
-                }
-                */
-            }
+            if (!implementsGladiator(sc)) continue;
+            System.out.println("  application class: " + sc.getName());
+            System.out.println("  application class: " + getArenaForGladiator(sc).getName());
+            sc.setApplicationClass();
+            getArenaForGladiator(sc).setApplicationClass();
+            ac.add(sc);
+            ac.add(getArenaForGladiator(sc));
         }
         for(Iterator it = ac.iterator(); it.hasNext();) {
             SootClass sc = (SootClass)it.next();
@@ -158,60 +144,81 @@ public class Main extends SceneTransformer {
 
     // Operations performed on the Gladiator class //////////////////////////////////////////////////////////////////////////
 
-    public void processGladiatorClass(SootClass sc) {
-        SootClass mc = getArenaForGladiator(sc);
-        String subname = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1);
-        String incFuncName = subname + "$$inc";
+    public SootMethod createIncMethod(SootClass sc) {
+        String incFuncName = getGladiatorName(sc) + "$$inc";
+        SootClass arena = getArenaForGladiator(sc);
         SootMethod method = new SootMethod(incFuncName, new LinkedList(), IntType.v(), Modifier.PRIVATE, new LinkedList());
+        arena.addMethod(method);
+        Body incBody = Jimple.v().newBody(method);
+        method.setActiveBody(incBody);
+        ((JimpleBody)incBody).insertIdentityStmts();
+        return method;
+    }
+
+    public void processGladiatorClass(SootClass sc) {
+
+        // Set up the Arena zero-arg constructor 
+
+        SootClass            arena = getArenaForGladiator(sc);
+        SootMethod arenaInitMethod = null;
+        for(Iterator it = arena.getMethods().iterator(); it.hasNext();) {
+            SootMethod m = (SootMethod)it.next();
+            if (m.getName().equals("<init>")) {
+                if (arenaInitMethod != null) throw new Error("class " + arena.getName() + " has two constructors");
+                arenaInitMethod = m;
+            }
+        }
+        JimpleBody   arenaInitBody = (JimpleBody)arenaInitMethod.getActiveBody();
+
+        SootField maxField = new SootField(getGladiatorName(sc) + "$$max", IntType.v());
+        arena.addField(maxField);
+        assign(arenaInitBody, newIFR(arenaInitBody, maxField.makeRef()), IntConstant.v(initialSize),
+               arenaInitBody.getFirstNonIdentityStmt());
 
-        mc.setApplicationClass();
-        SootMethod initMethod = mc.getMethod("<init>", new LinkedList());
-        if (!initMethod.hasActiveBody()) initMethod.setActiveBody((JimpleBody)Jimple.v().newBody(initMethod));
-        Body mcInitBody = initMethod.getActiveBody();
-
-        mc.addMethod(method);
-        Body body = Jimple.v().newBody(method);
-        method.setActiveBody(body);
-        ((JimpleBody)body).insertIdentityStmts();
-        Local l  =  newLocal(body, IntType.v());
-        Local l2 =  newLocal(body, IntType.v());
-        Local l3 =  newLocal(body, IntType.v());
-        Local l4 =  newLocal(body, IntType.v());
-
-        Stmt where = ((JimpleBody)mcInitBody).getFirstNonIdentityStmt();
-        SootField maxField = new SootField(subname + "$$max", IntType.v());
-        mc.addField(maxField);
-        assign(mcInitBody, newIFR(mcInitBody, maxField.makeRef()), IntConstant.v(initialSize), where);
-
-        SootField sfr = new SootField(subname + "$$size", IntType.v());
-        mc.addField(sfr);
-        assign(mcInitBody, newIFR(mcInitBody, sfr.makeRef()), IntConstant.v(0), where);
-        
-        assign(body, l,                                 newIFR(body, sfr.makeRef()));
-        assign(body, l2,                                Jimple.v().newAddExpr(l, IntConstant.v(1)));
-        assign(body, newIFR(body, sfr.makeRef()),       l2);
-        assign(body, l3,                                newIFR(body, maxField.makeRef()));
+        SootField sfr = new SootField(getGladiatorName(sc) + "$$size", IntType.v());
+        arena.addField(sfr);
+        assign(arenaInitBody, newIFR(arenaInitBody, sfr.makeRef()), IntConstant.v(0),
+               arenaInitBody.getFirstNonIdentityStmt());
+
+        SootMethod incMethod = createIncMethod(sc);
+        Body         incBody = incMethod.getActiveBody();
+
+
+        // Now build the $$inc method
+
+        Local l  =  newLocal(incBody, IntType.v());
+        Local l2 =  newLocal(incBody, IntType.v());
+        Local l3 =  newLocal(incBody, IntType.v());
+        Local l4 =  newLocal(incBody, IntType.v());
+       
+        assign(incBody, l,                                 newIFR(incBody, sfr.makeRef()));
+        assign(incBody, l2,                                Jimple.v().newAddExpr(l, IntConstant.v(1)));
+        assign(incBody, newIFR(incBody, sfr.makeRef()),       l2);
+        assign(incBody, l3,                                newIFR(incBody, maxField.makeRef()));
 
         Stmt returnStmt = Jimple.v().newReturnStmt(l2);
-        body.getUnits().add(Jimple.v().newIfStmt(Jimple.v().newLtExpr(l2, l3), returnStmt));
+        incBody.getUnits().add(Jimple.v().newIfStmt(Jimple.v().newLtExpr(l2, l3), returnStmt));
+
+        assign(incBody,  l4,                               Jimple.v().newShlExpr(l3, IntConstant.v(1)));
+        assign(incBody,  newIFR(incBody, maxField.makeRef()), l4);
+
 
-        assign(body,  l4,                               Jimple.v().newShlExpr(l3, IntConstant.v(1)));
-        assign(body,  newIFR(body, maxField.makeRef()), l4);
+        // Finally, iterate over the Gladiator's fields, updating the $$inc method and Arena's zero-arg constructor as we go
 
         for(Iterator it = sc.getFields().iterator(); it.hasNext();) {
             SootField f = (SootField)it.next();
             Type t      = getSliceElementType(f.getType());
-            mc.addField(f = new SootField(getGladiatorName(sc) + "$$" + f.getName(), t.makeArrayType(), 0));
+            arena.addField(f = new SootField(getGladiatorName(sc) + "$$" + f.getName(), t.makeArrayType(), 0));
 
             Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(initialSize));
-            Local newArrLocal = newLocal(mcInitBody, f.getType());
-            mcInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newIFR(mcInitBody, f.makeRef()), newArrLocal));
-            mcInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
+            Local newArrLocal = newLocal(arenaInitBody, f.getType());
+            arenaInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newIFR(arenaInitBody, f.makeRef()), newArrLocal));
+            arenaInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
 
-            Local ll0 = newLocal(body, f.getType());
-            Local ll = newLocal(body, f.getType());
-            assign(body, ll0, newIFR(body,  f.makeRef()));
-            assign(body, ll,  Jimple.v().newNewArrayExpr(t, l4));
+            Local ll0 = newLocal(incBody, f.getType());
+            Local ll = newLocal(incBody, f.getType());
+            assign(incBody, ll0, newIFR(incBody,  f.makeRef()));
+            assign(incBody, ll,  Jimple.v().newNewArrayExpr(t, l4));
 
             List args = new LinkedList();
             args.add(ll0);
@@ -219,19 +226,20 @@ public class Main extends SceneTransformer {
             args.add(ll);
             args.add(IntConstant.v(0));
             args.add(l3);
-            body.getUnits().add(Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(arrayCopy, args)));
-            assign(body, newIFR(body,  f.makeRef()), ll);
+            incBody.getUnits().add(Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(arrayCopy, args)));
+            assign(incBody, newIFR(incBody,  f.makeRef()), ll);
         }
 
         for(Iterator it = sc.getMethods().iterator(); it.hasNext();) {
             SootMethod m = (SootMethod)it.next();
-            if (!m.isConcrete()) continue;
+            if (!m.isConcrete() || !m.hasActiveBody()) continue;
             boolean doremove = true;
-            Body mbody = m.getActiveBody();
+            Body mincBody = m.getActiveBody();
             if (implementsGladiator(m.getDeclaringClass()) && m.getName().equals("<init>")) {
+                System.out.println("processing ctor " + sc.getName() + "." + m.getSignature());
                 doremove = false;
                 SootClass c = m.getDeclaringClass();
-                String name = /*c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + */"$init";
+                String name = "$init";
                 List li = m.getParameterTypes();
                 c.removeMethod(m);
                 SootMethod nm = new SootMethod(name, li, implementsGladiator(m.getReturnType()) ? IntType.v() : m.getReturnType());
@@ -239,9 +247,26 @@ public class Main extends SceneTransformer {
                 bod.importBodyContentsFrom(m.getActiveBody());
                 nm.setActiveBody(bod);
                 m = nm;
-                mbody = bod;
-                mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
-                mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
+                mincBody = bod;
+
+                for(Iterator it2 = mincBody.getUnits().snapshotIterator(); it2.hasNext(); ) {
+                    Unit u = (Unit)it2.next();
+                    if (u instanceof DefinitionStmt) {
+                        DefinitionStmt ds = (DefinitionStmt)u;
+                        if (ds.getLeftOp() instanceof ThisRef)
+                            mincBody.getUnits().remove(u);
+                        else if (ds.getLeftOp() instanceof FieldRef) {
+                            if (((FieldRef)ds.getLeftOp()).getFieldRef().name().endsWith("this$0"))
+                                mincBody.getUnits().remove(u);
+                        }
+                    } else if (u instanceof InvokeStmt) {
+                        InvokeExpr ie = ((InvokeStmt)u).getInvokeExpr();
+                        SootMethodRef meth = ie.getMethodRef();
+                        if (meth.declaringClass().getName().equals("java.lang.Object") && meth.name().equals("<init>"))
+                            mincBody.getUnits().remove(u);
+                    }
+                }
+
             } else {
                 System.out.println("examining " + sc.getName() + "." + m.getSignature());
                 m.retrieveActiveBody();
@@ -252,12 +277,11 @@ public class Main extends SceneTransformer {
             List list = new LinkedList();
             list.addAll(m.getParameterTypes());
             list.add(IntType.v());
-            for(Iterator i = list.iterator(); i.hasNext();) System.out.println(i.next());
             SootMethod m2 = new SootMethod(name, list, m.getReturnType());
             getArenaForGladiator(sc).addMethod(m2);
 
             JimpleBody ab = (JimpleBody)Jimple.v().newBody(m2);
-            ab.importBodyContentsFrom(mbody);
+            ab.importBodyContentsFrom(mincBody);
             m2.setActiveBody(ab);
             Local loc = Jimple.v().newLocal("tmpRef" + (tfr++), getArenaForGladiator(sc).getType());
             ab.getLocals().add(loc);
@@ -301,7 +325,6 @@ public class Main extends SceneTransformer {
                         }
                         */
                         if (vb.getValue() instanceof ThisRef) {
-                            System.out.println(s);
                             vb.setValue(loc);
                         }
                     }
@@ -312,7 +335,7 @@ public class Main extends SceneTransformer {
             if (doremove) sc.removeMethod(m);
 
         }
-        body.getUnits().add(returnStmt);
+        incBody.getUnits().add(returnStmt);
     }
 
     // Operations performed on all classes ////////////////////////////////////////////////////////////////////////////
@@ -382,11 +405,12 @@ public class Main extends SceneTransformer {
             body = b2;
         }
         Chain units = body.getUnits();
-        System.out.println("processBody("+body.getMethod()+")");
         for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) {
             Local l = (Local)it.next();
             if (implementsGladiator(l.getType())) l.setType(IntType.v());
         }
+        if (!smeth.isStatic())
+            body.getThisLocal().setType(ownerClass.getType());
         for(int qq=0; qq<2; qq++) for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
             Stmt s = (Stmt) stmtIt.next();
             if (s instanceof DefinitionStmt) {
@@ -399,7 +423,9 @@ public class Main extends SceneTransformer {
                 if (((ReturnStmt)s).getOp().getType() instanceof NullType)
                     ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
             List l = s.getUseAndDefBoxes();
-            for(Iterator it = l.iterator(); it.hasNext();) {
+            List l2l = new LinkedList();
+            l2l.addAll(l);
+            for(Iterator it = l2l.iterator(); it.hasNext();) {
                 Object o = it.next();
                 if (o instanceof ValueBox) {
                     ValueBox vb = (ValueBox)o;
@@ -417,14 +443,15 @@ public class Main extends SceneTransformer {
                         NewExpr ne = (NewExpr)v;
                         if (implementsGladiator(ne.getBaseType())) {
                             SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
-                            SootClass mc = getArenaForGladiator(sc);
+                            SootClass arena = getArenaForGladiator(sc);
                             String incFuncName = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$inc";
-                            SootMethodRef smr = Scene.v().makeMethodRef(mc, incFuncName, new LinkedList(), IntType.v(), false);
+                            SootMethodRef smr = Scene.v().makeMethodRef(arena, incFuncName, new LinkedList(), IntType.v(), false);
                             Expr invokeExpr = Jimple.v().newSpecialInvokeExpr(body.getThisLocal(), smr);
                             Local ll = viaLocal(invokeExpr, body, s);
                             vb.setValue(ll);
                             v = ll;
-                            continue;
+                            qq = 0;
+                            break;
                         } 
                     } else
 
@@ -447,7 +474,7 @@ public class Main extends SceneTransformer {
                                 //pl.remove(0);
                             }
                             pl.add(IntType.v());
-                            li.add(viaLocal(iie.getBase(),body,s));
+                            li.add(/*viaLocal(*/iie.getBase()/*,body,s)*/);
                             SootClass sc = mr.declaringClass();
                             name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + name;
                             mr = Scene.v().makeMethodRef(getArenaForGladiator(sc),
@@ -500,7 +527,7 @@ public class Main extends SceneTransformer {
                     } else if (v instanceof CastExpr) {
                         CastExpr ce = (CastExpr)v;
                         if (implementsGladiator(ce.getCastType())) {
-                            SootClass mc = getArenaForGladiator(((RefType)ce.getCastType()).getSootClass());
+                            SootClass arena = getArenaForGladiator(((RefType)ce.getCastType()).getSootClass());
                             SootClass ic = Scene.v().getSootClass("java.lang.Integer");
                             ce.setCastType(ic.getType());
 
@@ -526,23 +553,33 @@ public class Main extends SceneTransformer {
                             break;
                         }
 
-                    } else if (v instanceof InstanceFieldRef) {
-                        InstanceFieldRef ifr = (InstanceFieldRef)v;
+                    } else if (v instanceof FieldRef) {
+                        FieldRef ifr = (FieldRef)v;
                         SootFieldRef fr = ifr.getFieldRef();
                         Type t = fr.type();
                         if (implementsGladiator(fr.declaringClass()) && fr.name().equals("this$0")) {
-                            System.out.println("******************");
                             vb.setValue(body.getThisLocal());
-                            System.out.println("s is " + s);
                         } else if (implementsGladiator(fr.declaringClass())) {
-                            SootClass mc = getArenaForGladiator(fr.declaringClass());
-                            InstanceFieldRef sfr = newIFR(body,  getSliceForField(fr));
-                            vb.setValue(Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase()));
+                            SootClass arena = getArenaForGladiator(fr.declaringClass());
+                            if (fr.isStatic()) {
+                                vb.setValue(newIFR(body, getSliceForField(fr)));
+                            } else {
+                                InstanceFieldRef sfr = newIFR(body, getSliceForField(fr));
+                                vb.setValue(Jimple.v().newArrayRef(viaLocal(sfr, body, s), ((InstanceFieldRef)ifr).getBase()));
+                            }
                         }
                         if ((t instanceof RefType) && implementsGladiator(((RefType)t).getSootClass())) {
                             SootClass tc = ((RefType)t).getSootClass();
-                            SootClass mc = getArenaForGladiator(tc);
-                            ifr.setFieldRef(Scene.v().makeFieldRef(mc, fr.name(), IntType.v(), false));
+                            SootClass arena = getArenaForGladiator(tc);
+                            ifr.setFieldRef(Scene.v().makeFieldRef(arena, fr.name(), IntType.v(), fr.isStatic()));
+                        } else if (t instanceof ArrayType) {
+                            ArrayType at = (ArrayType)t;
+                            Type et = at.getElementType();
+                            if (et instanceof RefType && implementsGladiator(((RefType)et).getSootClass()))
+                                ifr.setFieldRef(Scene.v().makeFieldRef(fr.declaringClass(),
+                                                                       fr.name(),
+                                                                       IntType.v().makeArrayType(),
+                                                                       fr.isStatic()));
                         }
                     }