java堆栈帧_什么样的Java代码需要堆栈图帧?

理论

Java VM规范§4.10.1(按类型检查验证)指定当需要堆栈映射帧时.起初它给出了一个非正式的描述:

The intent is that a stack map frame must appear at the beginning of each basic block in a method. The stack map frame specifies the verification type of each operand stack entry and of each local variable at the start of each basic block.

§4.10.1.6(带有代码的类型检查方法)给出了详细的规范. goto命令需要堆栈映射帧:

It is illegal to have code after an unconditional branch without a stack map frame being provided for it.

和所有其他分支命令:

Branching to a target is type safe if the target has an associated stack frame, Frame, and the current stack frame, StackFrame, is assignable to Frame.

异常处理程序的开始也需要一个堆栈映射框架:

An instruction satisfies an exception handler if the instructions’s outgoing type state is ExcStackFrame, and the handler’s target (the initial instruction of the handler code) is type safe assuming an incoming type state T.

最后,§4.10.1.9(类型检查指令)指定哪些指令需要具有堆栈映射帧的分支目标.在类型规则中查找targetIsTypeSafe;指令goto,如果*,lookupswitch和tableswitch都有它.

即使以下代码需要堆栈图框架:

public static class GuineaPig {

public GuineaPig() {

int i = 1;

if (i > 0) {

// code branch to require stackmap frames

}

}

}

如果它们丢失,则代码将失败,但有异常:

java.lang.VerifyError: Expecting a stackmap frame at branch target 10

Exception Details:

Location:

net/orfjackal/retrolambda/Java6MissingStackMapFrameFixerTest$GuineaPig.()V @7: ifle

Reason:

Expected stackmap frame at this location.

Bytecode:

0000000: 2ab7 000c 043c 1b9e 0003 b1

at java.lang.Class.getDeclaredConstructors0(Native Method)

at java.lang.Class.privateGetDeclaredConstructors(Class.java:2658)

at java.lang.Class.getConstructor0(Class.java:2964)

at java.lang.Class.newInstance(Class.java:403)

这是字节码:

public net.orfjackal.retrolambda.Java6MissingStackMapFrameFixerTest$GuineaPig();

descriptor: ()V

flags: ACC_PUBLIC

Code:

stack=1, locals=2, args_size=1

0: aload_0

1: invokespecial #1 // Method java/lang/Object."":()V

4: iconst_1

5: istore_1

6: iload_1

7: ifle 10

10: return

LineNumberTable:

line 22: 0

line 23: 4

line 24: 6

line 27: 10

LocalVariableTable:

Start Length Slot Name Signature

0 11 0 this Lnet/orfjackal/retrolambda/Java6MissingStackMapFrameFixerTest$GuineaPig;

6 5 1 i I

StackMapTable: number_of_entries = 1

frame_type = 255 /* full_frame */

offset_delta = 10

locals = [ class net/orfjackal/retrolambda/Java6MissingStackMapFrameFixerTest$GuineaPig, int ]

stack = []

附:我花了一些时间来弄清楚这一点,因为默认情况下,我使用代码覆盖运行我的单元测试,并且IDEA的代码覆盖工具显然会自动重新计算所有类的堆栈映射框架,这样我的测试工作就将其删除.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值