X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Futil%2FBytecodePruner.java;h=eb162ff40f39c7deb2ad0ee65539e91362c262c3;hp=459bcca99120e321cc02d92861ea4464811be2cd;hb=0f015aed3b9062a628fb55c673755dfba82e5c61;hpb=c28ed126fbc51fb5794bb367f51a8755dc20140e diff --git a/src/org/ibex/util/BytecodePruner.java b/src/org/ibex/util/BytecodePruner.java index 459bcca..eb162ff 100644 --- a/src/org/ibex/util/BytecodePruner.java +++ b/src/org/ibex/util/BytecodePruner.java @@ -7,24 +7,7 @@ import org.apache.bcel.generic.*; 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 -// - 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; @@ -41,6 +24,18 @@ public class BytecodePruner { Method[] meths = getMethods(repo.loadClass(classname)); for(int i=0; i= 3 && s[1].equals("-o")) { outdir = s[2]; start += 2; } repo = SyntheticRepository.getInstance(new ClassPath(s[0])); - BytecodePruner bcp = new BytecodePruner(); + BytecodePruner bcp = new BytecodePruner(); for(int i=start; i")) 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", - "", 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", + "", 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"); @@ -298,10 +249,13 @@ public class BytecodePruner { 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