Search code examples
javabytecodejava-bytecode-asm

Reordering LabelNodes causes their offset to mess up in Java ASM


I have the following sample code:

    Label L1654589030
    Line 3, L1654589030
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init>()V
    RETURN
    Label L466002798

I perform a basic transformation, reordering instructions and labels and inserting two GOTO instructions:

    GOTO L1654589030
    Label L466002798
    RETURN
    Label L1654589030
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init>()V
    GOTO L466002798

However, when frames are being calculated by ASM, it throws an ArrayIndexOutOfBoundsException at org.objectweb.asm.ClassReader.createDebugLabel. In createDebugLabel, ASM tries to access a label at offset 65539, thus crashing. I can work this issue around by replacing the LabelNode I moved up with a new LabelNode, but that means I'll also have to update all instructions referencing it. Why is issue occurring?

EDIT1 | CheckClassAdapter returns the following dump:

<init>()V
00000 Main  :  :     GOTO L0
00001 Main  :  :    L1
00002 Main  :  :    FRAME FULL [com/company/Main] []
00003 Main  :  :     RETURN
00004 Main  :  :    L0
00005 Main  :  :    FRAME FULL [U] []
00006 Main  :  :     ALOAD 0
00007 Main  : Main  :     INVOKESPECIAL java/lang/Object.<init> ()V
00008 Main  :  :     GOTO L1

FRAME FULL should not be there, but how can I fix that?


Solution

  • SOLVED: The MethodNode.localVariables -> start / end labels didn't form a region after I reordered their instruction nodes. I had to manually update each variable.