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;
public void loadAllMethods(String classname) throws Exception {
visitJavaClass(repo.loadClass(classname));
Method[] meths = getMethods(repo.loadClass(classname));
- for(int i=0; i<meths.length; i++) visitJavaMethod(repo.loadClass(classname), meths[i]);
+ for(int i=0; i<meths.length; i++)
+ 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);
+ visitJavaClass(repo.loadClass(classname));
+ Field[] meths = repo.loadClass(classname).getFields();
+ for(int i=0; i<meths.length; i++)
+ if (meths[i].getName().equals(methodname))
+ visitJavaField(meths[i]);
+ }
+
+
public void loadMethod(String classAndMethodName) throws Exception {
String classname = classAndMethodName.substring(0, classAndMethodName.lastIndexOf('.'));
String methodname = classAndMethodName.substring(classAndMethodName.lastIndexOf('.') + 1);
// we call start(), but the VM calls run()...
bcp.loadMethod("java.lang.Thread.run");
bcp.loadAllMethods("java.lang.SecurityContext");
- bcp.visitJavaClass(repo.loadClass("java.awt.AWTPermission"));
- bcp.visitJavaClass(repo.loadClass("gnu.classpath.Configuration"));
+ //bcp.visitJavaClass(repo.loadClass("java.awt.AWTPermission"));
+ //bcp.visitJavaClass(repo.loadClass("gnu.classpath.Configuration"));
bcp.loadAllMethods("java.lang.ThreadDeath");
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?
-
- // 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.Surface$3");
- bcp.loadAllMethods("org.ibex.Surface$2");
- bcp.loadAllMethods("org.ibex.Picture");
- bcp.loadAllMethods("org.ibex.PixelBuffer");
- bcp.loadAllMethods("org.ibex.Platform");
- bcp.loadAllMethods("org.ibex.Scheduler");
- bcp.loadAllMethods("org.ibex.plat.X11");
- bcp.loadAllMethods("org.ibex.plat.X11$X11Picture");
- bcp.loadAllMethods("org.ibex.plat.X11$X11PixelBuffer");
- bcp.loadAllMethods("org.ibex.plat.X11$X11Surface");
- bcp.loadAllMethods("org.ibex.XMLRPC");
-
+ 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.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.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"))
bcp.visitJavaMethod(repo.loadClass("org.ibex.plat.Linux"), meths[i]);
}
System.out.println();
-
System.out.println("Dumping...");
StringTokenizer st = new StringTokenizer(s[0], ":");
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.setMajor(46);
cg.setMinor(0);
cg.setConstantPool(newcpg);
+
+ // 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();
- 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++;
+ if (!dest.contains(fields[i]) && fields[i].isStatic()) {
+ System.out.println(" pruning field " + clazz.getClassName() + "." + fields[i].getName());
+ 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 load(Type t) throws Exception {
if (t == null) return;
- //String sig = t.getSignature();
if (t instanceof ArrayType) load(((ArrayType)t).getElementType());
if (!(t instanceof ObjectType)) return;
load(((ObjectType)t).getClassName());
public static int level = 0;
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;
+
+ /*
+ // HACK (okay?)
+ // not inferrable: CNI call to abstract method
+ if (jc.getClassName().equals("java.util.TimeZone") && method.getName().equals("getDefaultTimeZoneId"))
+ loadMethod("java.util.SimpleTimeZone.useDaylightTime");
+ */
+
dest.add(method);
level += 2;
for(int i=0; i<level; i++) System.out.print(" ");
Instruction instr = instructions[i];
if (instr instanceof LoadClass) load(((LoadClass)instr).getLoadClassType(cpg));
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 org.apache.bcel.generic.FieldOrMethod)
+ load(((org.apache.bcel.generic.FieldOrMethod)instr).getClassType(cpg));
+ if (instr instanceof org.apache.bcel.generic.FieldInstruction) {
+ load(((org.apache.bcel.generic.FieldInstruction)instr).getFieldType(cpg));
+ load(((org.apache.bcel.generic.FieldInstruction)instr).getType(cpg));
+ String fieldName = ((org.apache.bcel.generic.FieldInstruction)instr).getFieldName(cpg);
+ 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]);
+ }
if (instr instanceof InvokeInstruction) {
InvokeInstruction ii = (InvokeInstruction)instr;
String ii_sig = getMethodSignature(ii, cpg);
}
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);
}
Field[] fields = clazz.getFields();
+ for(int i=0; i<fields.length; i++) {
+ if (!fields[i].isStatic())
+ visitJavaField(fields[i]);
+ }
+
Method[] methods = getMethods(clazz);
- for(int i=0; i<fields.length; i++) visitJavaField(fields[i]);
for(int i=0; i<methods.length; i++) {
if (methods[i].getName().equals("<clinit>")) visitJavaMethod(clazz, methods[i]);
-
// only if ctor reachable (?)
if (methods[i].getName().equals("equals")) visitJavaMethod(clazz, methods[i]);
if (methods[i].getName().equals("hashCode")) visitJavaMethod(clazz, methods[i]);
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);
}
}