Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this simple Java bytecode cause a StackOverflow error?
    primarykey
    data
    text
    <p>I need to instrument native methods to make a simple static call before executing normally. Because the methods are native, I have to use the <a href="http://download.oracle.com/javase/6/docs/api/java/lang/instrument/Instrumentation.html#setNativeMethodPrefix%28java.lang.instrument.ClassFileTransformer,%20java.lang.String%29" rel="nofollow">"setNativePrefix" feature</a> and wrap the native methods with an intermediate call with the original method signature. </p> <p>After what I thought was a simple bytecode change to accomplish this, I'm getting a StackOverflowError right before the wrapper method gets executed even though the stack is basically empty. Here's my test class:</p> <pre><code>public class SimpleTest { public static void main(String[] args) throws IOException { Perf.getPerf().highResCounter(); } } </code></pre> <p>Normally, that program would produce nothing on the console. However, my instrumented bytecode executes a println() before executing the native method $wrapper$highResCounter(). This can be seen in the relevant Perf class bytecode after instrumentation:</p> <pre><code>public long highResCounter() { getstatic PrintStream System.out ldc String Constant "this is an instrumented println" invokevirtual void PrintStream.println(String) aload 0 invokevirtual long Perf.$wrapped$highResCounter() lreturn } public native long $wrapped$highResCounter(); </code></pre> <p>I'm sort of new to Java bytecode, so I probably made a mistake here. Here's the output of the program, which shows that the println() gets executed, but somewhere after the first <em>invokevirtual</em> call a StackOverflowError is thrown:</p> <pre><code>this is an instrumented println call Exception in thread "main" java.lang.StackOverflowError at com.foo.SimpleTest.main(SimpleTest.java:17) </code></pre> <p>What could be causing this StackOverflowError? And how do I fix it?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload