Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is stackmap table in jvm bytecode?

Tags:

java

bytecode

I am learning ASM library for generating bytecode. At some point I made some mistake with bad local variable type and got an error:

Exception in thread "main" java.lang.VerifyError: Bad local variable type
Exception Details:
  Location:
    Loops.start()V @56: aload_1
  Reason:
    Type top (current frame, locals[1]) is not assignable to reference type


  Stackmap Table:
    full_frame(@24,{Object[#2],Object[#9]},{Integer})
    full_frame(@25,{Object[#2],Object[#9]},{Integer,Integer})
    same_locals_1_stack_item_frame(@44,Integer)
    full_frame(@45,{Object[#2],Object[#9]},{Integer,Integer})
    full_frame(@48,{Object[#2]},{Integer})
    full_frame(@80,{Object[#2],Integer},{Integer})
    full_frame(@81,{Object[#2],Integer},{Integer,Integer})
    full_frame(@87,{Object[#2]},{Integer})
    full_frame(@119,{Object[#2],Integer},{Integer})
    full_frame(@120,{Object[#2],Integer},{Integer,Integer})
    same_locals_1_stack_item_frame(@123,Integer)

The problem was not difficult to find and fix but I'm curious what is a this stackmap table thing?

like image 945
Jakub Dziworski Avatar asked May 18 '16 20:05

Jakub Dziworski


1 Answers

StackMapTable is an attribute in classes compiled with Java 6 or higher. It is used by the JVM during the process of verification by type checking.

Basically, a stack map frame defines the expected types of local variables and the operand stack (i.e. the state of the frame) of a method during its execution. During run-time, The JVM throws the VerifyError if expected and actual types are incompatible.

To save space, not every instruction has a corresponding frame. The table only defines frames for potential jump targets or exception handlers. Other frames can be easily inferred from these ones. You can see in your table above that frames are defined only for some bytecode offsets.

like image 97
dejvuth Avatar answered Oct 04 '22 03:10

dejvuth