Question

I'm fairly new to Java bytecode. I'm using BCEL to generate bytecode, but I get an error message when I try to use the generated code. (In hindsight, it looks like ObjectWeb ASM is more advanced and more commonly used than BCEL.)

Here is the error message:

Exception in thread "main" java.lang.VerifyError: 
(class: cb/io/FileDescriptor, method: set 
 signature: (I)J) 
Stack size too large

Here is the method:

// Method descriptor #4 (I)J
// Stack: 0, Locals: 1
private static long set(int arg1);
  0  lconst_0
  1  lreturn
    Local variable table:
      [pc: 0, pc: 2] local: arg1 index: 0 type: int

From what I understand, the local variable table is correct because it matches the input parameter. I haven't added a 'this' variable because the method is static.

If I'm not mistaken, lconst_0 loads a long value of 0 onto the stack, and lreturn consumes that value and returns it.

What am I doing wrong? Is there enough information here to tell?

Thanks!

Was it helpful?

Solution

Your have to declare the maximum stack size you use for every Java method. ASM for example can calculate the values automatically, which saves you a lot of trouble. The maximum stack size is the absolute maximum amount of slots you use in the method (at once, not accumulated). The calculations can get very tricky when doing branching.

You're loading a long value here, longs consume 2 stack slots (in fact, every 64 bit value (except pointers) consume 2 stack slots), so you have to set a maximum stack size of 2 here.

The Stack size too large just tells you that your method consumes more stack slots as are specified in the method descriptor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top