Javassist 3.15.0-GA, StackOverflowError with $sig in call to static method, can anyone confirm?
wgayk Dec 28, 2011 11:07 AMCheers,
here's an Example of a possible bug I've found.
Could anyone confirm this or prove me an idiot, so I can either file a bug report or go kill myself in shame?
Using $sig or $class as method parameter only seems to work on nonstatic methods, on static methods I get a StackOverflow Error.
Have I missed something in the dicumentation or is that a serious bug?
I could reproduce this with the latest JRE6 and the current JRE7:
Using $sig:
instrumenting class: de/plush/brix/instrumentation/Main (995 bytes)
instrumenting class: javassist/runtime/Desc (3280 bytes)
Exception in thread "main" java.lang.StackOverflowError
at javassist.runtime.Desc.getParams(Desc.java)
at javassist.runtime.Desc.getParams(Desc.java)
at javassist.runtime.Desc.getParams(Desc.java)
[..]
Using $class:
instrumenting class: de/plush/brix/instrumentation/Main (995 bytes)
instrumenting class: javassist/runtime/Desc (3280 bytes)
Exception in thread "main" java.lang.StackOverflowError
at javassist.runtime.Desc.getClazz(Desc.java)
at javassist.runtime.Desc.getClazz(Desc.java)
[...]
Compile the following code, create JAR-File, have a manifest like:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.7.0_02-b13 (Oracle Corporation)
Premain-Class: de.plush.brix.instrumentation.Agent
Class-Path: javassist.jar
Start some Main.class with the jvm-arg:
-javaagent:${project_loc}\Agent.jar
And watch it happen.
package de.plush.brix.instrumentation;
public class Main {
void foo() {
System.out.println("Hello world");
}
public static void main(final String[] args) {
new Main().foo();
}
}
package de.plush.brix.instrumentation;
import java.io.IOException;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.Modifier;
import java.security.ProtectionDomain;
import java.util.Arrays;
import javassist.ByteArrayClassPath;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;
public class Agent implements ClassFileTransformer {
public Agent(final String agentArgs, final Instrumentation instrumentation) {
instrumentation.addTransformer(this);
}
public static void premain(final String agentArgs, final Instrumentation inst) {
new Agent(agentArgs, inst);
}
public static void before(Class[] paramTypes) { //works
System.out.println("test passed");
}
public static void before(Class paramTypes) { //works
System.out.println("test passed");
}
public static void beforeStatic(Class[] paramTypes) { //stackOverflow
System.out.println("test passed");
}
public static void beforeStatic2(Class paramTypes) { //stackOverflow
System.out.println("test passed");
}
@Override
public byte[] transform(final ClassLoader loader, final String className, final Class<?> classBeingRedefined, final ProtectionDomain protectionDomain,
final byte[] classfileBuffer) throws IllegalClassFormatException {
System.out.println("instrumenting class: " + className + " (" + classfileBuffer.length + " bytes)");
try {
final String javaClassName = className.replace('/', '.');
// turn bytecodes into a Javassist CtClass
final CtClass clazz = createClass(classfileBuffer, javaClassName);
// add output at the beginning and end of each method
for (final CtMethod method : clazz.getMethods()) {
if (!isInherited(javaClassName, method)) {
if (Modifier.isStatic(method.getModifiers())) {
method.insertBefore("{"+Agent.class.getName() + ".beforeStatic($sig);}"); // -- $sig kills it!
// method.insertBefore("{"+Agent.class.getName() + ".beforeStatic2($class);}"); // -- $class kills it too!
} else {
method.insertBefore("{"+Agent.class.getName() + ".before($sig);}"); // -- works
// method.insertBefore("{"+Agent.class.getName() + ".before2($class);}"); // --works too
}
}
}
return clazz.toBytecode();
} catch (final NotFoundException e) {
throw new RuntimeException(e);
} catch (final CannotCompileException e) {
if (!e.getMessage().contains("no method body")) {
throw new RuntimeException(e);
}
// don't care
} catch (final IOException e) {
throw new RuntimeException(e);
}
return classfileBuffer;
}
private boolean isInherited(final String javaClassName, final CtMethod method) {
return !method.getLongName().startsWith(javaClassName);
}
private CtClass createClass(final byte[] classfileBuffer, final String javassistClassName) throws NotFoundException {
final ClassPool cp = ClassPool.getDefault();
cp.insertClassPath(new ByteArrayClassPath(javassistClassName, classfileBuffer));
final CtClass cc = cp.get(javassistClassName);
return cc;
}
}