checkpoint
authoradam <adam@megacz.com>
Tue, 10 May 2005 03:35:32 +0000 (03:35 +0000)
committeradam <adam@megacz.com>
Tue, 10 May 2005 03:35:32 +0000 (03:35 +0000)
darcs-hash:20050510033532-5007d-6c4dcadb382ad229c2b632f384f4f465b20632c7.gz

src/org/ibex/arenaj/Main.java

index 27e2643..674dfb6 100644 (file)
@@ -19,20 +19,23 @@ public class Main extends SceneTransformer {
             System.exit(0);
         }
         PackManager.v().getPack("wjtp").add(new Transform("wjtp.tx", Main.v()));
-        String[] args2 = new String[args.length + 11];
+        String[] args2 = new String[args.length + 14];
         System.arraycopy(args, 0, args2, 0, args.length-1);
         String sootcp =
             System.getProperty("java.class.path") + 
             File.pathSeparator +
             System.getProperty("sun.boot.class.path");
-        if (System.getProperty("java.ext.dirs") != null) {
-            StringTokenizer st = new StringTokenizer(File.pathSeparatorChar+"", System.getProperty("java.ext.dirs"));
+        String extDirs = System.getProperty("java.ext.dirs");
+        if (extDirs != null) {
+            StringTokenizer st = new StringTokenizer(extDirs, File.pathSeparatorChar+"");
             while(st.hasMoreTokens()) {
-                File f = new File(st.nextToken());
+                String goo = st.nextToken();
+                System.out.println("goo " + goo);
+                File f = new File(goo);
                 if (!f.exists() || !f.isDirectory()) continue;
                 String[] jars = f.list();
                 for(int i=0; i<jars.length; i++)
-                    sootcp += File.pathSeparator + jars[i];
+                    sootcp += File.pathSeparator + f.getPath() + File.separatorChar + jars[i];
             }
         }
         args2[args.length - 1] = "-w";
@@ -42,19 +45,39 @@ public class Main extends SceneTransformer {
         args2[args.length + 3] = "-p";
         args2[args.length + 4] = "wjap";
         args2[args.length + 5] = "enabled:false";
-        args2[args.length + 6] = "-keep-line-number";
-        args2[args.length + 7] = "-keep-line-number";
-        args2[args.length + 8] = "-cp";
-        args2[args.length + 9] = sootcp;
-        args2[args.length + 10] = args[args.length-1];
+        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";
+        args2[args.length + 12] = sootcp;
+        System.out.println("sootcp => " + sootcp);
+        args2[args.length + 13] = args[args.length-1];
+        for(int i=0; i<args2.length; i++) System.out.println("args2["+i+"] = " + args2[i]);
         soot.Main.main(args2);
     }
 
     static int tfr = 0;
+    public static Local viaLocal(Value v, Body b) {
+        Local l = Jimple.v().newLocal("tmpRef" + (tfr++), v.getType());
+        b.getLocals().add(l);
+        if (v instanceof IdentityRef) {
+            b.getUnits().addFirst(Jimple.v().newIdentityStmt(l, v));
+        } else {
+            b.getUnits().addFirst(Jimple.v().newAssignStmt(l, v));
+        }
+        return l;
+    }
     public static Local viaLocal(Value v, Body b, Unit u) {
         Local l = Jimple.v().newLocal("tmpRef" + (tfr++), v.getType());
         b.getLocals().add(l);
-        b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, v), u);
+        if (v instanceof IdentityRef) {
+            b.getUnits().insertBefore(Jimple.v().newIdentityStmt(l, v), u);
+        } else {
+            b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, v), u);
+        }
         return l;
     }
 
@@ -79,18 +102,31 @@ public class Main extends SceneTransformer {
                                      t.makeArrayType(),
                                      0);
         oc.addField(nf);
-        Body body = oc.getMethod("<init>", new LinkedList()).getActiveBody();
+
+        Body body = getInitBody(oc);
         Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(initialSize));
         Local newArrLocal = Jimple.v().newLocal("tmpRef" + (tfr++), f.type().makeArrayType());
         body.getLocals().add(newArrLocal);
-        InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), nf.makeRef());
+        InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(thisLocal(c,body), nf.makeRef());
         body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, newArrLocal));
         body.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
+
         map.put(sig, nf);
         return nf;
     }
 
-    private Body body;
+    public Body getInitBody(SootClass c) {
+        c.setApplicationClass();
+        List list = new LinkedList();
+        SootMethod m = c.getMethod("<init>", list);
+        if (!m.hasActiveBody()) {
+            JimpleBody b = (JimpleBody)Jimple.v().newBody(m);
+            m.setActiveBody(b);
+        }
+        return m.getActiveBody();
+    }
+
+    public Local thisLocal(SootClass c, Body b) { return viaLocal(Jimple.v().newThisRef(c.getType()), b); }
     public SootFieldRef getGladiatorFieldSizeRef(SootClass c) {
         SootClass mc = getParent(c);
         String name = c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$$size";
@@ -98,8 +134,8 @@ public class Main extends SceneTransformer {
             SootField f = new SootField(name, IntType.v());
             mc.addField(f);
 
-            Body body = mc.getMethod("<init>", new LinkedList()).getActiveBody();
-            InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), f.makeRef());
+            Body body = getInitBody(mc);
+            InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(thisLocal(c,body), f.makeRef());
             body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, IntConstant.v(0)));
 
             map.put(c, f);
@@ -113,8 +149,8 @@ public class Main extends SceneTransformer {
             SootField f = new SootField(name, IntType.v());
             mc.addField(f);
 
-            Body body = mc.getMethod("<init>", new LinkedList()).getActiveBody();
-            InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), f.makeRef());
+            Body body = getInitBody(mc);
+            InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(thisLocal(c,body), f.makeRef());
             body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, IntConstant.v(initialSize)));
 
             map.put(c, f);
@@ -122,34 +158,48 @@ public class Main extends SceneTransformer {
         return Scene.v().makeFieldRef(mc, name, IntType.v(), false);
     }
 
-    public boolean isGladiatorType(Type t) {
-        return (t instanceof RefType) && isGladiator(((RefType)t).getSootClass());
-    }
+    public boolean isGladiatorType(Type t) { return (t instanceof RefType) && isGladiator(((RefType)t).getSootClass()); }
 
-    public SootMethodRef convert(SootMethodRef mr) {
-        List l = mr.parameterTypes();
-        List l2 = new LinkedList();
-        for(Iterator it2 = l.iterator(); it2.hasNext();) {
-            Type t = (Type)it2.next();
-            l2.add(isGladiatorType(t) ? IntType.v() : t);
-        }
-        return Scene.v().makeMethodRef(mr.declaringClass(),
-                                       mr.name(),
-                                       l2,
-                                       isGladiatorType(mr.returnType()) ? IntType.v() : mr.returnType(),
-                                       mr.isStatic());
-    }
+
+    
 
     boolean done = false;
     public void internalTransform(String phaseName, Map options) {
-        for(Iterator it = Scene.v().getApplicationClasses().iterator(); it.hasNext();) {
+        System.out.println("begun");
+        List ac = new LinkedList();
+        for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
+            SootClass sc = (SootClass)it.next();
+            sc.setLibraryClass();
+        }
+        for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
             SootClass sc = (SootClass)it.next();
+            if (isGladiator(sc)) {
+                System.out.println("  application class: " + sc.getName());
+                System.out.println("  application class: " + getParent(sc).getName());
+                sc.setApplicationClass();
+                getParent(sc).setApplicationClass();
+                ac.add(sc);
+                ac.add(getParent(sc));
+                for(Iterator i2 = sc.getMethods().iterator(); i2.hasNext();) {
+                    SootMethod m = (SootMethod)i2.next();
+                    if (m.isConcrete()) m.retrieveActiveBody();
+                }
+                for(Iterator i2 = getParent(sc).getMethods().iterator(); i2.hasNext();) {
+                    SootMethod m = (SootMethod)i2.next();
+                    if (m.isConcrete()) m.retrieveActiveBody();
+                }
+            }
+        }
+        for(Iterator it = ac.iterator(); it.hasNext();) {
+            SootClass sc = (SootClass)it.next();
+            if (sc.isInterface()) continue;
             if (!isGladiator(sc)) continue;
             System.out.println("fixing Gladiator class: " + sc);
             fixClass(sc);
         }
-        for(Iterator it = Scene.v().getApplicationClasses().iterator(); it.hasNext();) {
+        for(Iterator it = ac.iterator(); it.hasNext();) {
             SootClass sc = (SootClass)it.next();
+            if (sc.isInterface()) continue;
             System.out.println("        updating class: " + sc);
             nuke(sc);
         }
@@ -166,8 +216,8 @@ public class Main extends SceneTransformer {
                                            IntType.v(), Modifier.PRIVATE,
                                            new LinkedList());
         mc.addMethod(method);
-        method.setActiveBody(body = Jimple.v().newBody(method));
-        body.setMethod(method);
+        Body body = Jimple.v().newBody(method);
+        method.setActiveBody(body);
         ((JimpleBody)body).insertIdentityStmts();
         Local l = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v());
         body.getLocals().add(l);
@@ -221,7 +271,79 @@ public class Main extends SceneTransformer {
             body.getUnits().add(Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(arrayCopy, args)));
             body.getUnits().add(Jimple.v().newAssignStmt(ifr,ll));
         }
+        for(Iterator it = sc.getMethods().iterator(); it.hasNext();) {
+            SootMethod m = (SootMethod)it.next();
+            if (!m.isConcrete()) continue;
+            if (isGladiator(m.getDeclaringClass()) && m.getName().equals("<init>")) {
+                SootClass c = m.getDeclaringClass();
+                String name = c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$$$init";
+                List li = m.getParameterTypes();
+                li.remove(0);
+                li.add(IntType.v());
+                c.removeMethod(m);
+                SootMethod nm = new SootMethod(name, li, isGladiatorType(m.getReturnType()) ? IntType.v() : m.getReturnType());
+                JimpleBody bod = Jimple.v().newBody(nm);
+                getParent(c).addMethod(nm);
+                //bod.insertIdentityStmts();
+                bod.importBodyContentsFrom(m.getActiveBody());
+                nm.setActiveBody(bod);
+                continue;
+            }
+            System.out.println("examining " + sc.getName() + "." + m.getSignature());
+            m.retrieveActiveBody();
+            if (m.isStatic()) continue;
+
+            String name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + m.getName();
+            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());
+            getParent(sc).addMethod(m2);
 
+            JimpleBody ab = (JimpleBody)Jimple.v().newBody(m2);
+            ab.importBodyContentsFrom(m.getActiveBody());
+            m2.setActiveBody(ab);
+            //Local loc = Jimple.v().newLocal("tmpRef" + (tfr++), getParent(sc).getType());
+            //ab.getLocals().add(loc);
+            // FIXME: insert assignment to this
+
+            for(Iterator z = ab.getLocals().iterator(); z.hasNext();) {
+                Local loc2 = (Local)z.next();
+                if (isGladiatorType(loc2.getType())) {
+                    loc2.setType(IntType.v());
+                }
+            }
+
+            Chain units = ab.getUnits();
+            for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
+                Stmt s = (Stmt) stmtIt.next();
+                if (s instanceof IdentityStmt) {
+                    IdentityStmt is = (IdentityStmt)s;
+                    Local left = (Local)is.getLeftOp();
+                    if (is.getRightOp() instanceof ThisRef) {
+                        left.setType(IntType.v());
+                        is.getRightOpBox().setValue(Jimple.v().newParameterRef(IntType.v(), m.getParameterCount()));
+                        break;
+                    }
+                }
+                /*
+                for(Iterator i = s.getUseAndDefBoxes().iterator(); i.hasNext();) {
+                    Object o = i.next();
+                    if (o instanceof ValueBox) {
+                        ValueBox vb = (ValueBox)o;
+                        if (vb.getValue() instanceof ThisRef) {
+                            System.out.println(s);
+                            vb.setValue(loc);
+                        }
+                    }
+                }
+                */
+            }
+            
+            sc.removeMethod(m);
+
+        }
         body.getUnits().add(stmt);
     }
 
@@ -246,10 +368,8 @@ public class Main extends SceneTransformer {
         List list = new LinkedList(); list.addAll(c.getMethods());
         for(Iterator it = list.iterator(); it.hasNext();) {
             SootMethod m = (SootMethod)it.next();
-            if (isGladiator(c) && m.getName().equals("<init>")) continue;
-            if (m.hasActiveBody() && !m.isStatic()) m.setActiveBody(fixBody(m.getActiveBody()));
-            if (m.getName().equals("<init>")) continue;
-            if (m.getName().equals("<clinit>")) continue;
+            Body b = null;
+            if (m.hasActiveBody()) b = fixBody(m.getActiveBody(), c, m);
             List l2 = new LinkedList();
             List l = m.getParameterTypes();
             for(Iterator it2 = l.iterator(); it2.hasNext();) {
@@ -257,42 +377,55 @@ public class Main extends SceneTransformer {
                 l2.add(isGladiatorType(t) ? IntType.v() : t);
             }
             Type t = m.getReturnType();
-            c.removeMethod(m);
             if (isGladiatorType(t)) {
                 t = IntType.v();
-                Body bod = m.getActiveBody();
-                for(Iterator stmtIt = bod.getUnits().snapshotIterator(); stmtIt.hasNext();) {
-                    Stmt s = (Stmt) stmtIt.next();
-                    if (s instanceof ReturnStmt) {
-                        if (((ReturnStmt)s).getOp().getType() instanceof NullType) {
-                            ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
+                if (m.hasActiveBody()) {
+                    Body bod = m.getActiveBody();
+                    for(Iterator stmtIt = bod.getUnits().snapshotIterator(); stmtIt.hasNext();) {
+                        Stmt s = (Stmt) stmtIt.next();
+                        if (s instanceof ReturnStmt) {
+                            if (((ReturnStmt)s).getOp().getType() instanceof NullType) {
+                                ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
+                            }
                         }
                     }
                 }
             }
-            SootMethod meth = new SootMethod(m.getName(), l2, t, m.getModifiers());
-            meth.setActiveBody(m.getActiveBody());
+            String name = m.getName();
+            SootMethod meth = new SootMethod(name, l2, isGladiatorType(t) ? IntType.v() : t, m.getModifiers());
+            if (b != null) {
+                JimpleBody b2 = Jimple.v().newBody(meth);
+                b2.importBodyContentsFrom(b);
+                meth.setActiveBody(b2);
+            }
+            c.removeMethod(m);
             c.addMethod(meth);
         }
 
     }
 
-    protected Body fixBody(Body body) {
-        this.body = body;
+    protected Body fixBody(Body body, SootClass ownerClass, SootMethod smeth) {
+        if (body instanceof JimpleBody) {
+            JimpleBody b2 = Jimple.v().newBody(smeth);
+            //b2.insertIdentityStmts();
+            b2.importBodyContentsFrom(body);
+            body = b2;
+        }
         Chain units = body.getUnits();
+        System.out.println("fixBody("+body.getMethod()+")");
         for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) {
             Local l = (Local)it.next();
             if (isGladiatorType(l.getType())) l.setType(IntType.v());
         }
         for(int qq=0; qq<2; qq++) for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
             Stmt s = (Stmt) stmtIt.next();
-            List l = s.getUseAndDefBoxes();
             if (s instanceof DefinitionStmt) {
                 DefinitionStmt ds = (DefinitionStmt)s;
                 if (ds.getLeftOp().getType() instanceof PrimType && ds.getRightOp().getType() instanceof NullType) {
                     ds.getRightOpBox().setValue(IntConstant.v(-1));
                 }
             }
+            List l = s.getUseAndDefBoxes();
             for(Iterator it = l.iterator(); it.hasNext();) {
                 Object o = it.next();
                 if (o instanceof ValueBox) {
@@ -309,16 +442,64 @@ public class Main extends SceneTransformer {
                         }
                     }
 
+                    if (v instanceof NewExpr) {
+                        NewExpr ne = (NewExpr)v;
+                        if (isGladiatorType(ne.getBaseType())) {
+                            SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
+                            SootClass mc = getParent(sc);
+                            String incFuncName = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$inc";
+                            SootMethodRef smr = Scene.v().makeMethodRef(mc, incFuncName, new LinkedList(), IntType.v(), false);
+                            Expr invokeExpr = Jimple.v().newSpecialInvokeExpr(thisLocal(mc,body), smr);
+                            Local ll = viaLocal(invokeExpr, body, s);
+                            vb.setValue(ll);
+                            v = ll;
+                            continue;
+                        } 
+                    } else
+
                     if (v instanceof InvokeExpr) {
                         InvokeExpr ie = (InvokeExpr)v;
                         SootMethodRef mr = ie.getMethodRef();
-                        if (isGladiator(mr.declaringClass())) {
-                            body.getUnits().remove(s);
-                            qq = 0;
-                            break;
+                        String name = mr.name();
+                        if (v instanceof InstanceInvokeExpr && isGladiator(mr.declaringClass())) {
+                            InstanceInvokeExpr iie = (InstanceInvokeExpr)v;
+                            List li = new LinkedList();
+                            li.addAll(iie.getArgs());
+                            LinkedList pl = new LinkedList();
+                            pl.addAll(mr.parameterTypes());
+                            if (mr.name().equals("<init>") && isGladiator(mr.declaringClass())) {
+                                name = "$init";
+                                li.remove(0);
+                                pl.remove(0);
+                                //pl.addFirst(body.getThisLocal());
+                            }
+                            pl.add(IntType.v());
+                            li.add(viaLocal(iie.getBase(),body,s));
+                            SootClass sc = mr.declaringClass();
+                            name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + name;
+                            mr = Scene.v().makeMethodRef(getParent(sc),
+                                                         name,
+                                                         pl,
+                                                         isGladiatorType(mr.returnType()) ? IntType.v() : mr.returnType(),
+                                                         false);
+                            ie = Jimple.v().newVirtualInvokeExpr(viaLocal(thisLocal(getParent(sc),body),body,s), mr, li);
+                            vb.setValue(v = ie);
+                        } else {
+                            List l0 = mr.parameterTypes();
+                            List l2 = new LinkedList();
+                            for(Iterator it2 = l0.iterator(); it2.hasNext();) {
+                                Type t = (Type)it2.next();
+                                l2.add(isGladiatorType(t) ? IntType.v() : t);
+                            }
+                            mr = Scene.v().makeMethodRef(mr.declaringClass(),
+                                                         mr.name(),
+                                                         l2,
+                                                         isGladiatorType(mr.returnType()) ? IntType.v() : mr.returnType(),
+                                                         mr.isStatic());
+                            ie.setMethodRef(mr);
+                            vb.setValue(v = ie);                            
                         }
-                        mr = convert(mr);
-                        ie.setMethodRef(mr);
+
                         for(int i=0; i<ie.getArgCount(); i++) {
                             ValueBox b = ie.getArgBox(i);
                             Value val = b.getValue();
@@ -341,29 +522,21 @@ public class Main extends SceneTransformer {
                             }
                         }
 
-                    } else if (v instanceof NewExpr) {
-                        NewExpr ne = (NewExpr)v;
-                        if (isGladiatorType(ne.getBaseType())) {
-                            SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
-                            SootClass mc = getParent(sc);
-                            String incFuncName = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$inc";
-                            SootMethodRef smr = Scene.v().makeMethodRef(mc, incFuncName, new LinkedList(), IntType.v(), false);
-                            Expr invokeExpr = Jimple.v().newSpecialInvokeExpr(body.getThisLocal(), smr);
-                            Local ll = viaLocal(invokeExpr, body, s);
-                            vb.setValue(ll);
-                        }
 
                     } else if (v instanceof CastExpr) {
                         CastExpr ce = (CastExpr)v;
                         if (isGladiatorType(ce.getCastType())) {
                             SootClass mc = getParent(((RefType)ce.getCastType()).getSootClass());
-                            LinkedList list = new LinkedList();
-                            list.add(Scene.v().getSootClass("java.lang.Object").getType());
-                            SootMethodRef mr = Scene.v().makeMethodRef(mc, "zap", list, IntType.v(), true);
-                            Local loc = viaLocal(ce.getOp(), body, s);
-                            List list2 = new LinkedList();
-                            list2.add(loc);
-                            vb.setValue(viaLocal(Jimple.v().newStaticInvokeExpr(mr, list2), body, s));
+                            SootClass ic = Scene.v().getSootClass("java.lang.Integer");
+                            ce.setCastType(ic.getType());
+                            // FIXME deal with null
+                            SootMethodRef mr = Scene.v().makeMethodRef(ic, "intValue", new LinkedList(), IntType.v(), false);
+                            InvokeExpr iie = Jimple.v().newVirtualInvokeExpr(viaLocal(ce, body, s),
+                                                                             mr,
+                                                                             new LinkedList());
+                            vb.setValue(viaLocal(iie, body, s));
+                            qq = 0;
+                            break;
                         }
 
                     } else if (v instanceof InstanceFieldRef) {
@@ -373,7 +546,8 @@ public class Main extends SceneTransformer {
                         if (isGladiatorFieldRef(fr)) {
                             SootClass mc = getParent(fr.declaringClass());
                             SootFieldRef sf = getGladiatorField(fr).makeRef();
-                            InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), sf);
+                            InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(thisLocal(mc, body), sf);
+                            System.out.println("s is " + s);
                             ArrayRef ar = Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase());
                             vb.setValue(ar);
                         }
@@ -386,6 +560,7 @@ public class Main extends SceneTransformer {
                 }
             }
         }
+        body.validate();
         return body;
     }
 }