Crash using AnonymousClassloader

Rémi Forax forax at univ-mlv.fr
Fri Jul 31 16:44:24 PDT 2009


I've found a crash using the anonymous class loader.
It appears when using the constructor without a host class,
I was not able to isolate the bug, sorry.

the code to reproduce the errors can be found here:
http://weblogs.java.net/blog/forax/archive/2009/07/dlr_expression_1.html

then patch ExprBuilder to create an error in the bytecode,
by example coment the line that add a return at the end of the constrcutor
and comments the line that use the verifier of ASM.

public MethodHandle createMethodHandle(Class<?> hostClass) {
     ...
-->comment the line below
     //init.visitInsn(Opcodes.RETURN);
     init.visitMaxs(0, 0);
     init.visitEnd();

     mv.accept(cw);

     byte[] byteArray = cw.toByteArray();

--> comment the two lines below
     //DEBUG !!
     //ClassReader reader = new ClassReader(byteArray);
     //CheckClassAdapter.verify(reader, false, new PrintWriter(System.out));


And change the AnonymousClassLoaderStub to use the constructor without 
parameters.

-----------------------------------------------------------------------------------------------------------------
public class AnonymousClassLoaderStub {
   private AnonymousClassLoaderStub() {
     // no instance
   }

   static Class<?> loadClass(Class<?> hostClass, byte[] byteArray) {
     try {
       Object anonymousClassLoader = 
ANONYMOUS_CLASSLOADER_CONSTRUCTOR.newInstance(/*hostClass*/);
       return (Class<?>)LOAD_CLASS.invoke(anonymousClassLoader, byteArray);
     } catch (InstantiationException e) {
       throw new AssertionError(e);
     } catch (IllegalAccessException e) {
       throw new AssertionError(e);
     } catch (InvocationTargetException e) {
       throw new AssertionError(e.getCause());
     }
   }

   private static final Constructor<?> ANONYMOUS_CLASSLOADER_CONSTRUCTOR;
   private static final Method LOAD_CLASS;
   static {
     try {
       Class<?> ANONYMOUS_CLASSLOADER_CLASS = 
Class.forName("sun.dyn.anon.AnonymousClassLoader");
       ANONYMOUS_CLASSLOADER_CONSTRUCTOR = 
ANONYMOUS_CLASSLOADER_CLASS.getConstructor(/*Class.class*/);
       LOAD_CLASS = ANONYMOUS_CLASSLOADER_CLASS.getMethod("loadClass", 
byte[].class);
     } catch (ClassNotFoundException e) {
       throw new AssertionError(e);
     } catch (NoSuchMethodException e) {
       throw new AssertionError(e);
     }
   }
}
---------------------------------------------------------------------------------------------------------------------------------------------------------

Rémi



More information about the mlvm-dev mailing list