[jboss-jira] [JBoss JIRA] (JASSIST-148) StackOverflowError when using $sig and $class in call to static method

Wanja Gayk (Updated) (JIRA) jira-events at lists.jboss.org
Thu Jan 5 15:23:09 EST 2012


     [ https://issues.jboss.org/browse/JASSIST-148?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Wanja Gayk updated JASSIST-148:
-------------------------------

    Steps to Reproduce: 
 
Compile the following code and create JAR-File for the Agent.class. 
The manifest should look something like:

{panel:title=Manifest.mf|borderStyle=solid}
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
{panel}

Start the Main.class with the JVM-arg:

-javaagent:${project_loc}\Agent.jar

The Agent.class includes examples of the desired and undesired behaviors, mind the comments.

Main class code: 

{code:title=Main.java|borderStyle=solid}

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();
 }
}
{code}

{code:title=Agent.java|borderStyle=solid}

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 clazz) { //works
  System.out.println("test passed");
 }
 
 public static void beforeStatic(Class[] paramTypes) { //stackOverflow
  System.out.println("test passed");
 }
 
 public static void beforeStatic2(Class clazz) { //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 {

  //signal instrumentation:
  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);

   // insert method call at the beginning 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;
 }
}

{code}

  was:
 
Compile the following code and create JAR-File for the Agent.class. 
The manifest should look something like:

{panel:title=Manifest.mf|borderStyle=solid}
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
{panel}

Start the Main.class with the JVM-arg:

-javaagent:${project_loc}\Agent.jar

The Agent.class includes examples of the desired and undesired behaviors, mind the comments.

Main class code: 

{code:title=Main.java|borderStyle=solid}

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();
 }
}
{code}

{code:title=Agent.java|borderStyle=solid}

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 {

  //signal instrumentation:
  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);

   // insert method call at the beginning 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;
 }
}

{code}

       Forum Reference: http://community.jboss.org/thread/176637?tstart=0  (was: http://community.jboss.org/thread/176637?tstart=0)

    
> StackOverflowError when using $sig and $class in call to static method
> ----------------------------------------------------------------------
>
>                 Key: JASSIST-148
>                 URL: https://issues.jboss.org/browse/JASSIST-148
>             Project: Javassist
>          Issue Type: Bug
>    Affects Versions: 3.15.0-GA
>         Environment: Runtime Enviroment:
>  Java SE 6 Update 30 (64 bit)
> also reproduced with:
>  Java SE 7 Update 2 (64 bit)
> Compiler:
>  Eclipse Indigo (Version: 3.7.1, Build id: M20110909-1335).
> Operating System
>  Windows 7 Professional (64 bit)
> CPU:
>  Intel Core i7 860 (2,8 GHz)
>            Reporter: Wanja Gayk
>            Assignee: Shigeru Chiba
>
> Using $sig or $class as method parameter in "insertBefore"-expressions throws a StackOverflowError if the target is a static method.
> Calling nonstatic methods works like expected.
> Stack/Log-Traces:
> When 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)
> [...]
> When 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)
> [...]

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.jboss.org/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the jboss-jira mailing list