checkpoint
[org.ibex.arenaj.git] / src / org / ibex / arenaj / Main.java
index 78e1c58..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();
@@ -161,7 +160,14 @@ public class Main extends SceneTransformer {
         // Set up the Arena zero-arg constructor 
 
         SootClass            arena = getArenaForGladiator(sc);
-        SootMethod arenaInitMethod = arena.getMethod("<init>", new LinkedList());
+        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());
@@ -226,13 +232,14 @@ public class Main extends SceneTransformer {
 
         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 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());
@@ -241,8 +248,25 @@ public class Main extends SceneTransformer {
                 nm.setActiveBody(bod);
                 m = nm;
                 mincBody = bod;
-                mincBody.getUnits().remove(((JimpleBody)mincBody).getFirstNonIdentityStmt());
-                mincBody.getUnits().remove(((JimpleBody)mincBody).getFirstNonIdentityStmt());
+
+                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();
@@ -253,7 +277,6 @@ 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);
 
@@ -302,7 +325,6 @@ public class Main extends SceneTransformer {
                         }
                         */
                         if (vb.getValue() instanceof ThisRef) {
-                            System.out.println(s);
                             vb.setValue(loc);
                         }
                     }
@@ -383,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) {
@@ -400,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;
@@ -425,7 +450,8 @@ public class Main extends SceneTransformer {
                             Local ll = viaLocal(invokeExpr, body, s);
                             vb.setValue(ll);
                             v = ll;
-                            continue;
+                            qq = 0;
+                            break;
                         } 
                     } else
 
@@ -448,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),
@@ -527,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 arena = getArenaForGladiator(fr.declaringClass());
-                            InstanceFieldRef sfr = newIFR(body,  getSliceForField(fr));
-                            vb.setValue(Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase()));
+                            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 arena = getArenaForGladiator(tc);
-                            ifr.setFieldRef(Scene.v().makeFieldRef(arena, fr.name(), IntType.v(), false));
+                            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()));
                         }
                     }