0

I am playing with Java bytecode generation (using Clojure with https://github.com/jgpc42/insn), Currently I am trying to generate a bytecode for a nested for loop, bellow is the relevant part of the generated bytecode, but when I try to run it I'm get a:

 Exception in thread "main" java.lang.VerifyError: Inconsistent stackmap frames at branch target 114

part of the bytecode related with the for:

     111: bipush        0
     113: istore_0
     114: iload_0
     115: bipush        3
     117: if_icmpge     154
     120: bipush        0
     122: istore_1
     123: iload_0
     124: bipush        3
     126: if_icmpge     147
     129: invokestatic  #14                 // Method my/pkg/RaspIvok.getInvoker:()Lrasp_lang/lib/core2/Invoker;
     132: invokestatic  #44                 // Method my/pkg/RaspIvok.getPrintln:()Lmy/pkg/Test/Println;
     135: ldc           #46                 // String Hello
     137: invokevirtual #49                 // Method rasp_lang/lib/core2/Invoker.call:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     140: pop
     141: iinc          1, 1
     144: goto          123
     147: pop
     148: iinc          0, 1
     151: goto          114
     154: return

Any help how to understand in order to fix the issue?

Thanks

1 Answer 1

2

At every bytecode instruction, the class verifier has an exact model of how big the stack is and what types are in each slot.

The code can 'flow' into 114 either naturally (just from the top) or via the GOTO at 151.

These two routes into item 114 do not have the same stack model. Either the types on it are different, or the sizes are different.

A quick glance, with in 'comment' what the effect is on the stack:

We enter at empty stack, then:

 +1    111: bipush        0
 -1    113: istore_0
 +1    114: iload_0
 +1    115: bipush        3
 -2    117: if_icmpge     154
 +1    120: bipush        0
 -1    122: istore_1
 +1    123: iload_0
 +1    124: bipush        3
 -2    126: if_icmpge     147
 +1    129: invokestatic  #14                 // Method my/pkg/RaspIvok.getInvoker:()Lrasp_lang/lib/core2/Invoker;
 +1    132: invokestatic  #44                 // Method my/pkg/RaspIvok.getPrintln:()Lmy/pkg/Test/Println;
 +1    135: ldc           #46                 // String Hello
 -3+1  137: invokevirtual #49                 // Method rasp_lang/lib/core2/Invoker.call:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
 -1    140: pop
 0     141: iinc          1, 1
 0     144: goto          123
 -1    147: pop
 0     148: iinc          0, 1
 0     151: goto          114
 0     154: return

Everything is in balance. Except for 147: pop which makes no sense and means the sum total is at -1 when you get to '151: goto 114', but is at 0 when you get to 114 'naturally'.

I don't know where 147: pop is coming from. It looks like, if you just get rid of that, this should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.