NanoGoat
[org.ibex.core.git] / src / org / ibex / util / NanoGoat.java
similarity index 64%
rename from src/org/ibex/util/BytecodePruner.java
rename to src/org/ibex/util/NanoGoat.java
index a61c84d..7f1a50e 100644 (file)
@@ -7,16 +7,19 @@ import org.apache.bcel.generic.*;
 import org.apache.bcel.classfile.*;
 import org.apache.bcel.util.*;
 
-public class BytecodePruner {
+public class NanoGoat {
 
     public static final boolean deleteMethods = false;
     public static SyntheticRepository repo = null;
     public static HashSet dest = new HashSet();
+    public static HashSet constructed = new HashSet();
     public static String outdir = ".";
     public static Hashtable subclasses = new Hashtable();
+    public static Hashtable uponconstruction = new Hashtable();
+    public static Hashtable mark_if_constructed = new Hashtable();
     public static int level = 0;
 
-    public BytecodePruner() { }
+    public NanoGoat() { }
 
     public void loadAllMethods(String classname) throws Exception {
         visitJavaClass(repo.loadClass(classname));
@@ -25,16 +28,13 @@ public class BytecodePruner {
             visitJavaMethod(repo.loadClass(classname), meths[i]);
     }
 
-    public void loadField(String classAndMethodName) throws Exception {
-        String classname = classAndMethodName.substring(0, classAndMethodName.lastIndexOf('.'));
-        String methodname = classAndMethodName.substring(classAndMethodName.lastIndexOf('.') + 1);
+    public void loadAllStaticMethods(String classname) throws Exception {
         visitJavaClass(repo.loadClass(classname));
-        Field[] meths = repo.loadClass(classname).getFields();
+        Method[] meths = getMethods(repo.loadClass(classname));
         for(int i=0; i<meths.length; i++)
-            if (meths[i].getName().equals(methodname))
-                visitJavaField(meths[i]);
+            if (meths[i].isStatic())
+                visitJavaMethod(repo.loadClass(classname), meths[i]);
     }
-    
 
     public void loadMethod(String classAndMethodName) throws Exception {
         String classname = classAndMethodName.substring(0, classAndMethodName.lastIndexOf('.'));
@@ -48,10 +48,9 @@ public class BytecodePruner {
     }
     public static void main(String[] s) throws Exception {
         int start = 1;
-        if (s.length >= 3 && s[1].equals("-o")) { outdir = s[2]; start += 2; }
         repo = SyntheticRepository.getInstance(new ClassPath(s[0]));
 
-        BytecodePruner bcp = new BytecodePruner();
+        NanoGoat bcp = new NanoGoat();
         for(int i=start; i<s.length; i++) {
             try {
                 if (s[i].endsWith(".class")) {
@@ -60,9 +59,15 @@ public class BytecodePruner {
                     JavaClass cl = repo.loadClass(s[i].substring(0, s[i].lastIndexOf('.')));;
                     bcp.visitJavaClass(cl);
                     bcp.loadMethod(s[i]);
+                    Field[] fields = cl.getFields();
+                    for(int j=0; j<fields.length; j++) {
+                        if (fields[j].getName().equals(s[i].substring(s[i].lastIndexOf('.') + 1)))
+                            bcp.visitJavaField(fields[j], cl);
+                    }
                 }
             } catch (Exception e) {
                 System.out.println("WARNING: couldn't load class for " + s[i]);
+                e.printStackTrace();
             }
         }
 
@@ -72,23 +77,36 @@ public class BytecodePruner {
         bcp.loadMethod("java.lang.Thread.run");
         bcp.loadAllMethods("java.lang.SecurityContext");
         bcp.loadAllMethods("java.lang.ThreadDeath");
+        bcp.loadMethod("java.lang.Thread.run");                  // we call start(), but the VM calls run()...
+        bcp.loadMethod("java.lang.ref.Reference.enqueue");       // the GC calls this directly
+        bcp.loadAllMethods("gnu.gcj.runtime.StringBuffer");      // the compiler emits calls directly to this class
+        bcp.loadAllMethods("gnu.gcj.convert.Input_UTF8");        // retrieved via reflection
+        bcp.loadAllMethods("gnu.gcj.convert.Output_UTF8");       // retrieved via reflection
+        bcp.loadMethod("gnu.gcj.convert.BytesToUnicode.done");   // called by natString
+        bcp.loadAllStaticMethods("java.lang.reflect.Modifier");        // used all over natClass...
+
+        // the Interpreter.run() method's switchblock is too complex...
+        bcp.loadAllMethods("org.ibex.js.Interpreter$TryMarker");
+        bcp.loadAllMethods("org.ibex.js.Interpreter$CatchMarker");
+        bcp.loadAllMethods("org.ibex.js.Interpreter$LoopMarker");
+        bcp.loadAllMethods("org.ibex.js.Interpreter$FinallyData");
+        bcp.loadAllMethods("org.ibex.js.Interpreter$CallMarker");
+        bcp.loadAllMethods("org.ibex.js.Interpreter");
+        bcp.loadAllMethods("org.ibex.js.Interpreter$1");
+        bcp.loadAllMethods("org.ibex.js.Interpreter$Stub");
+        bcp.loadAllMethods("org.ibex.js.Trap$TrapScope");
+        bcp.loadMethod("org.ibex.js.JSScope.top");
+        bcp.loadAllMethods("org.ibex.Picture$1");
+        bcp.loadAllMethods("org.ibex.Ibex$Blessing");
+        bcp.loadAllMethods("org.ibex.util.SSL$entropySpinner");
+        bcp.loadAllMethods("org.ibex.HTTP$HTTPInputStream");
+        bcp.visitJavaClass(repo.loadClass("org.ibex.util.SSL"));
 
         bcp.loadAllMethods("java.util.Hashtable$HashIterator");
         bcp.loadMethod("java.util.SimpleTimeZone.useDaylightTime");
         bcp.visitJavaClass(repo.loadClass("gnu.gcj.runtime.FinalizerThread"));
         bcp.visitJavaClass(repo.loadClass("gnu.gcj.runtime.FirstThread"));
 
-        bcp.loadMethod("java.lang.Thread.run");                // we call start(), but the VM calls run()...
-        bcp.loadMethod("java.lang.ref.Reference.enqueue");     // the GC calls this directly
-        bcp.loadAllMethods("gnu.gcj.runtime.StringBuffer");    // the compiler emits calls directly to this class
-        bcp.loadAllMethods("gnu.gcj.convert.Input_UTF8");      // retrieved via reflection
-        bcp.loadAllMethods("gnu.gcj.convert.Output_UTF8");     // retrieved via reflection
-
-        bcp.loadAllMethods("gnu.gcj.protocol.http.Handler");
-        bcp.loadAllMethods("gnu.gcj.protocol.file.Handler");
-        bcp.loadAllMethods("gnu.gcj.protocol.core.Handler");
-        bcp.loadAllMethods("gnu.gcj.protocol.jar.Handler");
-
         // to ensure we get all the stuff that might be called from CNI
         bcp.loadAllMethods("org.ibex.plat.Linux");
         bcp.loadAllMethods("org.ibex.plat.X11");
@@ -106,21 +124,23 @@ public class BytecodePruner {
         System.out.println();
 
         System.out.println("Dumping...");
-        StringTokenizer st = new StringTokenizer(s[0], ":");
-        while(st.hasMoreTokens()) {
-            ZipFile zf = new ZipFile(st.nextToken());
-            Enumeration e = zf.entries();
-            while(e.hasMoreElements()) {
-                String ss = ((ZipEntry)e.nextElement()).getName();
-                if (!ss.endsWith(".class")) continue;
-                ss = ss.substring(0, ss.length() - 6);
-                ss = ss.replace('/', '.');
-                dump(repo.loadClass(ss));
-            }
+        ZipFile zf = new ZipFile(s[0]);
+        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(s[0] + ".tmp"));
+        Enumeration e = zf.entries();
+        while(e.hasMoreElements()) {
+            ZipEntry ze = ((ZipEntry)e.nextElement());
+            String ss = ze.getName();
+            if (!ss.endsWith(".class")) continue;
+            ss = ss.substring(0, ss.length() - 6);
+            ss = ss.replace('/', '.');
+            dump(repo.loadClass(ss), zos);
         }
+        zos.close();
+        zf.close();
+        new File(s[0] + ".tmp").renameTo(new File(s[0] + ".pruned"));
     }
 
-    public static void dump(JavaClass clazz) throws Exception {
+    public static void dump(JavaClass clazz, ZipOutputStream zos) throws Exception {
         if (!dest.contains(clazz)) return;
 
         ConstantPoolGen newcpg = new ConstantPoolGen(clazz.getConstantPool());
@@ -130,40 +150,35 @@ public class BytecodePruner {
         cg.setMinor(0);
         cg.setConstantPool(newcpg);
 
-        boolean constructed = false;
+        boolean isconstructed = false;
         Method[] methods = getMethods(clazz);
         for(int i=0; i<methods.length; i++)
             if (dest.contains(methods[i]) && methods[i].getName().equals("<init>"))
-                constructed = true;
+                isconstructed = true;
 
         // we can only prune static fields (to avoid altering object layout, which is hardcoded into
         // CNI code), but that's okay since instance fields don't contribute to binary size
         Field[] fields = clazz.getFields();
         for(int i=0; i<fields.length; i++) {
-            if (!dest.contains(fields[i]) && fields[i].isStatic()) { 
+            if ((!dest.contains(fields[i]) && fields[i].isStatic()) ||
+                ((!(constructed.contains(clazz))) && !fields[i].isStatic())) { 
                 System.out.println("  pruning field " + clazz.getClassName() + "." + fields[i].getName());
-                cg.removeField(fields[i]);
-            }
-            /*  -- there's really no point to this --
-            if (!constructed && !fields[i].isStatic()) {
-                System.out.println("  unconstructed: pruning field " + clazz.getClassName() + "." + fields[i].getName());
-                cg.removeField(fields[i]);
+                // FIXME this confuses gcj in jar-at-a-time mode
+                //cg.removeField(fields[i]);
             }
-            */
         }
 
         int numMethods = 0;
         boolean good = false;
         for(int i=0; i<methods.length; i++) {
-            if (dest.contains(methods[i]) && (constructed || !methods[i].isStatic())) {
-                if (!methods[i].getName().equals("<clinit>")) good = true;
+            if (dest.contains(methods[i]) && (isconstructed || methods[i].isStatic())) {
+                good = true;
             } else {
                 if (methods[i].getCode() == null) {
                     System.out.println("  empty codeblock: " + clazz.getClassName() + "." + methods[i].getName());
                 } else {
-                    System.out.println("  pruning " +(constructed?"":"unconstructed")+ " method " +
+                    System.out.println("  pruning " +(isconstructed?"":"unconstructed")+ " method " +
                                        clazz.getClassName() + "." + methods[i].getName());
-                    // FIXME: try deleteMethods
                     if (deleteMethods) { cg.removeMethod(methods[i]); continue; }
                     MethodGen mg = new MethodGen(methods[i], clazz.getClassName(), newcpg);
                     mg.removeExceptions();
@@ -186,7 +201,12 @@ public class BytecodePruner {
             }
         }
 
-        if (!good) {
+        // FIXME: chain up to superclass' <clinit>... that might remove the need for this hack
+        // FIXME: gcj compiling in jar-at-a-time mode can't be convinced to let classes outside the jar override
+        //        the ones inside the jar
+        good = true;
+
+        if (!good && !clazz.isAbstract() && !clazz.isInterface()) {
             System.out.println("DROPPING " + clazz.getClassName());
             JavaClass[] ifaces = clazz.getInterfaces();
             String[] ifacestrings = new String[ifaces.length];
@@ -200,8 +220,10 @@ public class BytecodePruner {
         } else {
             System.out.println("dumping " + clazz.getClassName());
         }
-        new File(outdir + "/" + new File(clazz.getClassName().replace('.', '/')).getParent()).mkdirs();
-        cg.getJavaClass().dump(outdir + "/" + clazz.getClassName().replace('.', '/') + ".class");
+        FilterOutputStream noclose = new FilterOutputStream(zos) { public void close() throws IOException { flush(); } };
+        zos.putNextEntry(new ZipEntry(clazz.getClassName().replace('.', '/')+".class"));
+        cg.getJavaClass().dump(noclose);
+        noclose.flush();
     }
 
     public JavaClass sig2class(String sig) throws Exception {
@@ -245,38 +267,73 @@ public class BytecodePruner {
         visitJavaClass(jc);
         if (jc.getClassName().indexOf("SharedLib") != -1) return;
         if (jc.getClassName().indexOf("Datagram") != -1) return;
+        if (jc.getClassName().startsWith("java.io.Object")) return;
+        if (jc.getClassName().startsWith("java.util.jar.")) return;
+        if (jc.getClassName().startsWith("java.net.Inet6")) return;
 
         // gcj bug; gcj can't compile this method from a .class file input; I have no idea why
         if (jc.getClassName().equals("java.lang.System") && method.getName().equals("runFinalizersOnExit")) return;
 
+        // we know these can't be constructed
+        if (method.getName().equals("<init>") && jc.getClassName().startsWith("java.lang.reflect.")) return;
+
         if (dest.contains(method)) return;
         dest.add(method);
 
-        if (method.isStatic()) loadMethod(jc.getClassName() + ".<clinit>");
+        if (method.getName().equals("<clinit>") && jc.getSuperClass() != null)
+            loadMethod(jc.getSuperClass().getClassName() + ".<clinit>");
+
+        if (method.isStatic() || method.getName().equals("<init>")) loadMethod(jc.getClassName() + ".<clinit>");
+        if (method.getName().equals("<init>")) {
+            // FIXME: generalize to all perinstancemethods
+            constructed.add(jc);
+            HashSet hs = (HashSet)uponconstruction.get(jc);
+            if (hs != null) {
+                Iterator it = hs.iterator();
+                while(it.hasNext()) visitJavaMethod(jc, (Method)it.next());
+            }
+            loadMethod(jc.getClassName() + ".equals");
+            loadMethod(jc.getClassName() + ".hashCode");
+            loadMethod(jc.getClassName() + ".toString");
+            loadMethod(jc.getClassName() + ".finalize");
+            loadMethod(jc.getClassName() + ".clone");
+        }
+
+        ConstantPoolGen cpg = new ConstantPoolGen(method.getConstantPool());
+        if (!method.isStatic() && !constructed.contains(jc)) {
+            HashSet hs = (HashSet)uponconstruction.get(jc);
+            if (hs == null) uponconstruction.put(jc, hs = new HashSet());
+            hs.add(method);
+            markMethodInSubclasses(jc, method, cpg);
+            dest.remove(method);
+            return;
+        }
 
         level += 2;
         for(int i=0; i<level; i++) System.out.print(" ");
-        ConstantPoolGen cpg = new ConstantPoolGen(method.getConstantPool());
-        System.out.println(jc.getClassName() + "." + getMethodSignature(method, cpg));
+        System.out.print(jc.getClassName() + "." + getMethodSignature(method, cpg));
         markMethodInSubclasses(jc, method, cpg);
-        if (method.getCode() == null) { level -= 2; return; }
+        if (method.getCode() == null) { System.out.println(); level -= 2; return; }
         byte[] code = method.getCode().getCode();
         InstructionList il = new InstructionList(code);
-        Instruction[] instructions = il.getInstructions();
+        InstructionHandle[] instructions = il.getInstructionHandles();
+        System.out.println(" [" + instructions.length + " instructions]");
         for(int i=0; i<instructions.length; i++){ 
-            Instruction instr = instructions[i];
+            Instruction instr = instructions[i].getInstruction();;
+            if (instr instanceof Select) {
+                InstructionHandle[] ih2 = ((Select)instr).getTargets();
+                InstructionHandle[] ih3 = new InstructionHandle[instructions.length + ih2.length];
+                System.arraycopy(instructions, 0, ih3, 0, instructions.length);
+                System.arraycopy(ih2, 0, ih3, instructions.length, ih2.length);
+                instructions = ih3;
+            }
             if (instr instanceof LoadClass) {
                 ObjectType ot = (ObjectType)((LoadClass)instr).getLoadClassType(cpg);
                 if (ot != null) loadMethod(ot.getClassName() + ".<clinit>");
             }
             if (instr instanceof CPInstruction) load(((CPInstruction)instr).getType(cpg));
-            if (instr instanceof TypedInstruction) {
-                try { load(((TypedInstruction)instr).getType(cpg)); } catch (Exception e) { /* DELIBERATE */ }
-            }
-            if (instr instanceof NEW) {
-                for(int j=0; j<level; j++) System.out.print(" ");
-                loadMethod(((NEW)instr).getLoadClassType(cpg).getClassName() + ".<init>");
-            }
+            if (instr instanceof TypedInstruction) load(((TypedInstruction)instr).getType(cpg));
+            if (instr instanceof NEW) loadMethod(((NEW)instr).getLoadClassType(cpg).getClassName() + ".<init>");
             if (instr instanceof org.apache.bcel.generic.FieldOrMethod)
                 load(((org.apache.bcel.generic.FieldOrMethod)instr).getClassType(cpg));
             if (instr instanceof org.apache.bcel.generic.FieldInstruction) {
@@ -286,7 +343,7 @@ public class BytecodePruner {
                 JavaClass jc2 = repo.loadClass(((ObjectType)((org.apache.bcel.generic.FieldInstruction)instr).
                                                 getLoadClassType(cpg)).getClassName());
                 Field[] fields = jc2.getFields();
-                for(int j=0; j<fields.length; j++) if (fields[j].getName().equals(fieldName)) visitJavaField(fields[j]);
+                for(int j=0; j<fields.length; j++) if (fields[j].getName().equals(fieldName)) visitJavaField(fields[j], jc2);
             }
             if (instr instanceof InvokeInstruction) {
                 InvokeInstruction ii = (InvokeInstruction)instr;
@@ -306,19 +363,19 @@ public class BytecodePruner {
                 if (!good) throw new Exception("couldn't find method " + getMethodSignature(ii, cpg) + " in " + c.getClassName());
             }
         }
-        level -= 2;
-
-        // FIXME: move this to the top
         Type[] argtypes = method.getArgumentTypes();
         for(int i=0; i<argtypes.length; i++) load(argtypes[i]);
         if (method.getExceptionTable() != null) {
             String[] exntypes = method.getExceptionTable().getExceptionNames();
             for(int i=0; i<exntypes.length; i++) load(exntypes[i]);
         }
+        level -= 2;
     }
 
-    public void visitJavaField(Field field) throws Exception {
-        if (!dest.contains(field)) dest.add(field);
+    public void visitJavaField(Field field, JavaClass clazz) throws Exception {
+        if (dest.contains(field)) return;
+        dest.add(field);
+        if (field.isStatic()) loadMethod(clazz.getClassName() + ".<clinit>");
     }
 
     public void visitJavaClass(JavaClass clazz) throws Exception {
@@ -326,6 +383,8 @@ public class BytecodePruner {
         dest.add(clazz);
 
         ConstantPoolGen cpg = new ConstantPoolGen(clazz.getConstantPool());
+        level += 2;
+        for(int i=0; i<level; i++) System.out.print(" ");
         System.out.println(clazz.getClassName() + ".class");
 
         JavaClass superclass = clazz.getSuperClass();
@@ -350,26 +409,23 @@ public class BytecodePruner {
 
         Field[] fields = clazz.getFields();
         for(int i=0; i<fields.length; i++) {
-            if (!fields[i].isStatic()) visitJavaField(fields[i]);
+            if (!fields[i].isStatic()) visitJavaField(fields[i], clazz);
             else {
                 Type t = fields[i].getType();
                 if (t instanceof ObjectType) load(t);
             }
         }
-
-        Method[] methods = getMethods(clazz);
-        for(int i=0; i<methods.length; i++) {
-            //if (methods[i].getName().equals("<clinit>")) visitJavaMethod(clazz, methods[i]);
-            if (methods[i].getName().equals("equals")) visitJavaMethod(clazz, methods[i]);
-            if (methods[i].getName().equals("hashCode")) visitJavaMethod(clazz, methods[i]);
-            if (methods[i].getName().equals("finalize")) visitJavaMethod(clazz, methods[i]);
-            if (methods[i].getName().equals("clone")) visitJavaMethod(clazz, methods[i]);
-            if (methods[i].getName().equals("toString")) visitJavaMethod(clazz, methods[i]);
-        }
+        level -= 2;
     }
 
     public void markMethodInSubclasses(JavaClass c, Method m, JavaClass subclass, ConstantPoolGen cpg) throws Exception {
         if (m.isStatic()) return;
+        if (m.getName().equals("<init>")) return;
+        if (m.getName().equals("equals")) return;
+        if (m.getName().equals("hashCode")) return;
+        if (m.getName().equals("clone")) return;
+        if (m.getName().equals("finalize")) return;
+        if (m.getName().equals("toString")) return;
         String sig = getMethodSignature(m, cpg);
         Method[] submethods = getMethods(subclass);
         for(int j=0; j<submethods.length; j++)
@@ -378,6 +434,7 @@ public class BytecodePruner {
     }
     public void markMethodInSubclasses(JavaClass c, Method m, ConstantPoolGen cpg) throws Exception {
         if (m.isStatic()) return;
+        if (m.getName().equals("<init>")) return;
         HashSet s = (HashSet)subclasses.get(c);
         if (s == null) return;
         Object[] subclasses = s.toArray();
@@ -390,12 +447,18 @@ public class BytecodePruner {
         
     public void remarkMethods(JavaClass c, ConstantPoolGen cpg) throws Exception {
         Method[] meths =getMethods(c);
-        for(int j=0; j<meths.length; j++) if (dest.contains(meths[j])) markMethodInSubclasses(c, meths[j], cpg);
+        for(int j=0; j<meths.length; j++)
+            if (dest.contains(meths[j]) ||
+                (uponconstruction.get(c) != null && ((HashSet)uponconstruction.get(c)).contains(meths[j])))
+                markMethodInSubclasses(c, meths[j], cpg);
     }
 
     public void remarkMethods(JavaClass c, JavaClass target, ConstantPoolGen cpg) throws Exception {
         Method[] meths = getMethods(c);
-        for(int j=0; j<meths.length; j++) if (dest.contains(meths[j])) markMethodInSubclasses(c, meths[j], target, cpg);
+        for(int j=0; j<meths.length; j++)
+            if (dest.contains(meths[j]) ||
+                (uponconstruction.get(c) != null && ((HashSet)uponconstruction.get(c)).contains(meths[j])))
+                markMethodInSubclasses(c, meths[j], target, cpg);
     }
 
     public static Hashtable methodsHashtable = new Hashtable();