import org.apache.bcel.classfile.*;
import org.apache.bcel.util.*;
-// Reachability rules:
-
-// - a constructor is reachable iff it is called
-// - a static method is reachable iff it is called
-// - a nonstatic method is reachable
-// - a static field is reachable iff it is referenced
-// - a nonstatic field is reachable iff it is referenced
-// - <clinit> is reachable iff any methods, static methods, fields, or constructors are reachable
-
-// - if a method is reachable, all the methods it overrides are reachable
-
-// try eliminating instance methods/fields if no ctor is reachable...
-// distinguish between overloaded forms of a given method
-// slim down the set of loadAllMethod() classes
-// field pruning?
-
-// FIXME: nonstatic method invocation or field access implies that object will be constructed (ie hint)
-
+// Field pruning
public class BytecodePruner {
public static final boolean deleteMethods = false;
bcp.loadAllMethods("java.util.Hashtable$HashIterator");
bcp.loadMethod("java.util.SimpleTimeZone.useDaylightTime");
- /*
- bcp.loadAllMethods("java.lang.Throwable");
- bcp.loadAllMethods("java.io.PrintStream");
- bcp.loadAllMethods("java.util.PropertyPermission");
- bcp.loadAllMethods("java.security.cert.Certificate");
- bcp.loadAllMethods("java.security.cert.CertificateEncodingException");
- bcp.loadMethod("java.util.TimeZone.getAvailableIDs");
- bcp.loadMethod("java.util.TimeZone.getDefaultTimeZoneId");
- bcp.loadAllMethods("gnu.gcj.runtime.StringBuffer");
- bcp.loadAllMethods("gnu.gcj.runtime.VMClassLoader");
- bcp.visitJavaClass(repo.loadClass("gnu.gcj.runtime.JNIWeakRef"));
- */
- /*
- bcp.visitJavaClass(repo.loadClass("gnu.gcj.protocol.http.Handler"));
- bcp.visitJavaClass(repo.loadClass("gnu.gcj.protocol.file.Handler"));
- bcp.visitJavaClass(repo.loadClass("gnu.gcj.protocol.jar.Handler"));
- bcp.visitJavaClass(repo.loadClass("gnu.gcj.protocol.core.Handler"));
- */
bcp.visitJavaClass(repo.loadClass("gnu.gcj.runtime.FinalizerThread"));
bcp.visitJavaClass(repo.loadClass("gnu.gcj.runtime.FirstThread"));
- // SecurityManager hacks to avoid java.security?
- // URL and all descendents? Probably impossible.
- // ObjectInput/ObjectOutput? Serialization?
-
+ /*
+ 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");
+ bcp.loadAllMethods("gnu.gcj.convert.Input_UTF8"); // retrieved via reflection
+ bcp.loadAllMethods("gnu.gcj.convert.Output_UTF8"); // retrieved via reflection
+ bcp.loadAllMethods("gnu.gcj.convert.Input_8859_1"); // retrieved via reflection
+ bcp.loadAllMethods("gnu.gcj.convert.Output_8859_1"); // retrieved via reflection
+ bcp.loadAllMethods("gnu.gcj.convert.Input_ASCII"); // retrieved via reflection
+ bcp.loadAllMethods("gnu.gcj.convert.Output_ASCII"); // retrieved via reflection
+ 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
+ */
// often called from native subclasses....
+
bcp.loadAllMethods("org.ibex.Surface");
bcp.loadAllMethods("org.ibex.Template$TemplateHelper$1");
bcp.loadAllMethods("org.ibex.Surface$DoubleBufferedSurface");
bcp.loadAllMethods("org.ibex.plat.X11$X11PixelBuffer");
bcp.loadAllMethods("org.ibex.plat.X11$X11Surface");
bcp.loadAllMethods("org.ibex.XMLRPC");
-
bcp.loadAllMethods("java.util.Date");
bcp.loadAllMethods("java.text.DateFormat");
bcp.loadAllMethods("java.text.NumberFormat");
-
Method[] meths = getMethods(repo.loadClass("org.ibex.plat.Linux"));
for(int i=0; i<meths.length; i++) {
if (meths[i].getName().equals("main"))
public BytecodePruner() { }
public static void dump(JavaClass clazz) throws Exception {
- if (clazz.getClassName().startsWith("java.sql.")) return;
if (!dest.contains(clazz)) return;
ConstantPoolGen newcpg = new ConstantPoolGen(clazz.getConstantPool());
cg.setMinor(0);
cg.setConstantPool(newcpg);
Field[] fields = clazz.getFields();
- int numFields = 0;
for(int i=0; i<fields.length; i++)
if (!dest.contains(fields[i]) && false) {
System.out.println(" pruning " + clazz.getClassName() + "." + fields[i].getName());
- fields[i] = null;
- } else numFields++;
+ cg.removeField(fields[i]);
+ }
- // superprune: URLClassLoader, convert.In/Output other than needed, unneeded locales
- // reflective metadata is killing us...
-
Method[] methods = getMethods(clazz);
int numMethods = 0;
boolean good = false;
- for(int i=0; i<methods.length; i++)
- if (clazz.getClassName().startsWith("gnu.gcj.runtime.")
- || clazz.getClassName().startsWith("java.io.FileDescriptor")
- || clazz.getClassName().startsWith("org.ibex.")
- || clazz.getClassName().startsWith("java.lang.")) {
- good = true;
- } else if (dest.contains(methods[i])) {
+ for(int i=0; i<methods.length; i++) {
+ if (dest.contains(methods[i]) ||
+ clazz.getClassName().startsWith("java.io.FileDescriptor")
+ || clazz.getClassName().startsWith("gnu.gcj.runtime.")
+ || clazz.getClassName().startsWith("java.lang.")
+ ) {
if (!methods[i].getName().equals("<clinit>")) good = true;
} else {
if (methods[i].getCode() == null) {
System.out.println(" empty codeblock: " + clazz.getClassName() + "." + methods[i].getName());
} else {
System.out.println(" pruning " + clazz.getClassName() + "." + methods[i].getName());
- if (!deleteMethods) {
- MethodGen mg = new MethodGen(methods[i], clazz.getClassName(), newcpg);
- mg.removeExceptions();
- InstructionList il = new InstructionList();
- mg.setInstructionList(il);
-
- InstructionHandle ih_0 = il.append(factory.createNew("java.lang.UnsatisfiedLinkError"));
- il.append(InstructionConstants.DUP);
- il.append(factory.createInvoke("java.lang.UnsatisfiedLinkError",
- "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
- il.append(InstructionConstants.ATHROW);
-
- mg.setMaxStack();
- mg.setMaxLocals();
- mg.removeExceptions();
- mg.removeLocalVariables();
- mg.removeExceptionHandlers();
- mg.removeLineNumbers();
-
- cg.replaceMethod(methods[i], mg.getMethod());
- il.dispose();
- } else {
- cg.removeMethod(methods[i]);
- }
+ if (deleteMethods) { cg.removeMethod(methods[i]); continue; }
+ MethodGen mg = new MethodGen(methods[i], clazz.getClassName(), newcpg);
+ mg.removeExceptions();
+ InstructionList il = new InstructionList();
+ mg.setInstructionList(il);
+ InstructionHandle ih_0 = il.append(factory.createNew("java.lang.UnsatisfiedLinkError"));
+ il.append(InstructionConstants.DUP);
+ il.append(factory.createInvoke("java.lang.UnsatisfiedLinkError",
+ "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
+ il.append(InstructionConstants.ATHROW);
+ mg.setMaxStack();
+ mg.setMaxLocals();
+ mg.removeExceptions();
+ mg.removeLocalVariables();
+ mg.removeExceptionHandlers();
+ mg.removeLineNumbers();
+ cg.replaceMethod(methods[i], mg.getMethod());
+ il.dispose();
}
}
- if ((clazz.getClassName().startsWith("gnu.java.locale.LocaleInformation") &&
- !clazz.getClassName().endsWith("LocaleInformation_en") &&
- !clazz.getClassName().endsWith("LocaleInformation") &&
- !clazz.getClassName().endsWith("LocaleInformation_en_US"))
- ||
- ((clazz.getClassName().startsWith("gnu.gcj.convert.Input_") ||
- clazz.getClassName().startsWith("gnu.gcj.convert.Output_")) &&
- !(clazz.getClassName().endsWith("ASCII") ||
- clazz.getClassName().endsWith("UTF8") ||
- clazz.getClassName().endsWith("iconv") ||
- clazz.getClassName().endsWith("8859_1")))
- ||
- (!good &&
- !clazz.isInterface() &&
- !clazz.isAbstract() &&
- !clazz.getClassName().startsWith("java.io.") &&
- !clazz.getClassName().startsWith("java.lang.") &&
- !clazz.getClassName().startsWith("gnu.") &&
- !clazz.getClassName().endsWith("Error") &&
- !clazz.getClassName().endsWith("Exception") &&
- !clazz.getClassName().endsWith("Permission"))) {
-
- //System.out.println("DROPPING " + clazz.getClassName());
- //return;
}
+
new File(outdir + "/" + new File(clazz.getClassName().replace('.', '/')).getParent()).mkdirs();
System.out.println("dumping " + clazz.getClassName());
cg.getJavaClass().dump(outdir + "/" + clazz.getClassName().replace('.', '/') + ".class");
public void visitJavaMethod(JavaClass jc, Method method) throws Exception {
visitJavaClass(jc);
if (jc.getClassName().equals("java.util.Date") && method.getName().equals("readObject")) return;
- //if (jc.getClassName().equals("java.net.URLClassLoader")) return;
if (jc.getClassName().indexOf("SharedLib") != -1) return;
if (jc.getClassName().indexOf("Datagram") != -1) return;
if (dest.contains(method)) return;
+
+ // gcj bug; gcj can't compile this method from a .class file input
+ //if (jc.getClassName().equals("java.lang.System") && method.getName().equals("runFinalizersOnExit")) return;
+
dest.add(method);
level += 2;
for(int i=0; i<level; i++) System.out.print(" ");
}
public void visitJavaClass(JavaClass clazz) throws Exception {
- /*
- if (clazz.getClassName().startsWith("java.security.") &&
- !clazz.getClassName().equals("java.security.cert.Certificate")) return;
- */
if (dest.contains(clazz)) return;
dest.add(clazz);
Object[] subclasses = s.toArray();
for(int i=0; i<subclasses.length; i++) {
JavaClass subclass = (JavaClass)subclasses[i];
+ if (subclass == c) continue;
for(int j=0; j<level; j++) System.out.print(" ");
System.out.println(" [subclass " + subclass.getClassName() + "]");
- if (subclass == c) return;
markMethodInSubclasses(c, m, subclass, cpg);
}
}