最后了解 JVM 字节码中的本地 var 位置

Understanding local var position in JVM bytecode on finally

我无法理解 ASMified Java 字节码上的变量定位。我有以下 Java 代码:

public class TryCatch {
    public static void main(String[] args) {
        String test1 = null;
        try {
            String test2 ="try-inside-begin";
            System.out.println("try-outside-begin");
            try {
                System.out.println(test2);
                System.out.println(test1.length());
                System.out.println("try-inside-end");
            } catch (NullPointerException e) {
                test2 = "catch-inside: " + e.getMessage();
                throw new Exception(test2, e);
            }
            System.out.println("try-outside-end");
        } catch (Exception e) {
            System.out.println("catch-outside: " + e.getMessage());
        } finally {
            System.out.println("finally");
        }
    }
}

它成为 main 的以下字节码:

  TRYCATCHBLOCK L0 L1 L2 java/lang/NullPointerException
  TRYCATCHBLOCK L3 L4 L5 java/lang/Exception
  TRYCATCHBLOCK L3 L4 L6 null
  TRYCATCHBLOCK L5 L7 L6 null
  TRYCATCHBLOCK L6 L8 L6 null
 L9
  LINENUMBER 5 L9
  ACONST_NULL
  ASTORE 1
 L3
  LINENUMBER 7 L3
  LDC "try-inside-begin"
  ASTORE 2
 L10
  LINENUMBER 8 L10
  GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
  LDC "try-outside-begin"
  INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
 L0
  LINENUMBER 10 L0
  GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
  ALOAD 2
  INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
 L11
  LINENUMBER 11 L11
  GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
  ALOAD 1
  INVOKEVIRTUAL java/lang/String.length ()I
  INVOKEVIRTUAL java/io/PrintStream.println (I)V
 L12
  LINENUMBER 12 L12
  GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
  LDC "try-inside-end"
  INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
 L1
  LINENUMBER 16 L1
  GOTO L13
 L2
  LINENUMBER 13 L2
 FRAME FULL [[Ljava/lang/String; java/lang/String java/lang/String] [java/lang/NullPointerException]
  ASTORE 3
 L14
  LINENUMBER 14 L14
  NEW java/lang/StringBuilder
  DUP
  INVOKESPECIAL java/lang/StringBuilder.<init> ()V
  LDC "catch-inside: "
  INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
  ALOAD 3
  INVOKEVIRTUAL java/lang/NullPointerException.getMessage ()Ljava/lang/String;
  INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
  INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
  ASTORE 2
 L15
  LINENUMBER 15 L15
  NEW java/lang/Exception
  DUP
  ALOAD 2
  ALOAD 3
  INVOKESPECIAL java/lang/Exception.<init> (Ljava/lang/String;Ljava/lang/Throwable;)V
  ATHROW
 L13
  LINENUMBER 17 L13
 FRAME SAME
  GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
  LDC "try-outside-end"
  INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
 L4
  LINENUMBER 21 L4
  GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
  LDC "finally"
  INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
 L16
  LINENUMBER 22 L16
  GOTO L17
 L5
  LINENUMBER 18 L5
 FRAME FULL [[Ljava/lang/String; java/lang/String] [java/lang/Exception]
  ASTORE 2
 L18
  LINENUMBER 19 L18
  GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
  NEW java/lang/StringBuilder
  DUP
  INVOKESPECIAL java/lang/StringBuilder.<init> ()V
  LDC "catch-outside: "
  INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
  ALOAD 2
  INVOKEVIRTUAL java/lang/Exception.getMessage ()Ljava/lang/String;
  INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
  INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
  INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
 L7
  LINENUMBER 21 L7
  GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
  LDC "finally"
  INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
 L19
  LINENUMBER 22 L19
  GOTO L17
 L6
  LINENUMBER 21 L6
 FRAME SAME1 java/lang/Throwable
  ASTORE 4
 L8
  GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
  LDC "finally"
  INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
  ALOAD 4
  ATHROW
 L17
  LINENUMBER 23 L17
 FRAME SAME
  RETURN
  MAXSTACK = 4
  MAXLOCALS = 5

请注意 ASTORE 4/ALOAD 4 离底部有多近。为什么是 4 而不是 3?由于 SAME1 帧是 "same locals as the previous frame and with a single value on the stack" 并且前一帧只有两个局部变量(参考:FRAME FULL [[Ljava/lang/String; java/lang/String] [java/lang/Exception])。

我已经阅读了 the spec,但我也不清楚为什么它不是 3。

栈帧描述局部变量的状态和操作数栈在它出现的地方。后面的指令当然可以修改正常的东西。正如您正确识别的那样,L6 的堆栈帧表示当控制流到达 L6 时有两个局部变量。下面指令存储到slot 4,完全合法

这可能有助于理解堆栈映射的用途。最初,根本没有堆栈映射,验证者使用推理来计算方法中每个点的局部变量。当遇到控制流时,它会合并那个点的值并迭代直到收敛。

不幸的是,这很慢,所以为了加快速度,Oracle 添加了堆栈映射。这实质上是在加入控制流的任何点预先计算验证结果。这样,验证者可以对代码进行一次线性传递,因为控制流不会改变结果。当验证器遇到控制流时,它检查当前状态是否与在跳转目标处声明的堆栈帧匹配,如果不匹配,则抛出错误。在线性代码段中,显然没有必要包含堆栈帧,因为验证器可以做与之前相同的事情。

堆栈帧不是用来调试的,它们是用来加速验证的,所以它们包含验证所需的最少信息。如果编译器假设在 指令处插入一个堆栈帧,那么 astore 4 之后的堆栈帧当然会在第 4 个槽中显示一个新变量。

至于为什么它可以使用插槽 3 而使用插槽 4,那只是编译器的心血来潮。也许它简化了 javac 的实现,但这只是推测。