【问题标题】:If(true) in JVM. How to generate an appropriate instruction?如果(真)在 JVM 中。如何生成合适的指令?
【发布时间】:2018-03-19 18:35:26
【问题描述】:

我正在尝试生成一个简单的条件跳转指令。这是课程:

public static Class<?> getKlass2(){
    String className = "TestClass";
    ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);

    classWriter.visit(V1_8, ACC_PUBLIC, className, null, getInternalName(Object.class), null);

    MethodVisitor mv = classWriter.visitMethod(ACC_PUBLIC + ACC_STATIC, "m", "()Z",null, null);
    Label trueLable = new Label();
    Label afterFalseLable = new Label();
    mv.visitFieldInsn(GETSTATIC, getInternalName(Boolean.class), "TRUE", "Ljava/lang/Boolean;");
    mv.visitMethodInsn(INVOKEVIRTUAL, getInternalName(Boolean.class), "booleanValue", "()Z", false);
    mv.visitJumpInsn(IFEQ, trueLable);
    mv.visitInsn(ICONST_1);
    mv.visitJumpInsn(GOTO, afterFalseLable);
    mv.visitLabel(trueLable);
    mv.visitInsn(ICONST_0);
    mv.visitFrame(F_APPEND, 0, null, 0, null);
    mv.visitLabel(afterFalseLable);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    //converting classWriter.toByteArray() to Class<?> instance
}

加载类时出现以下错误:

Expecting a stackmap frame at branch target 13

Exception Details:
  Location:
    TestClass.m()Z @6: ifeq
  Reason:
    Expected stackmap frame at this location.
  Bytecode:
    0x0000000: b200 0cb6 000f 9900 0704 a700 0403 ac  
  Stackmap Table:
    same_frame_extended(@14)

但是课程的代码对我来说似乎没问题:

public class TestClass {
  public static boolean m();
    Code:
       0: getstatic     #12                 // Field java/lang/Boolean.TRUE:Ljava/lang/Boolean;
       3: invokevirtual #15                 // Method java/lang/Boolean.booleanValue:()Z
       6: ifeq          13
       9: iconst_1
      10: goto          14
      13: iconst_0
      14: ireturn
}

所以我尝试手动添加一个框架:

mv.visitFrame(F_APPEND, 0, new Object[]{ }, 0, new Object[]{ trueLabel });

但它也因同样的异常而失败。他们是否希望重新创建具有相同操作数堆栈的帧? 但这似乎毫无意义,因为我无法从 Java 代码直接访问操作数堆栈。

我做错了什么?

【问题讨论】:

  • 您不想使用new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES) 为ASM 自动为您生成StackMapTable 吗?
  • @apangin 没错。谢谢!!!!
  • @apangin 不要使用COMPUTE_MAXS | COMPUTE_FRAMESthe documentation 明确声明“computeFrames 意味着 computeMaxs”,因此只需 COMPUTE_FRAMES 就足够了。

标签: java class if-statement jvm java-bytecode-asm


【解决方案1】:

您可以简单地将COMPUTE_FRAMES 指定给ClassWriter 的构造函数,让ASM 为您计算max stack&locals 和堆栈映射表框架条目。正如文档所述,“...computeFrames 意味着 computeMaxs”。

但是,我始终建议尝试了解堆栈图,因为从头开始计算不仅成本高昂,而且存在基本限制(如 this answer 中所述)。由于您应该已经知道堆栈帧应该是什么样子,因此对这些知识进行编码应该不会太难。由于这也意味着知道局部变量和操作数堆栈条目的最大数量,因此手动指定它们也是一致的。

但是,您尝试的解决方案还很遥远:

mv.visitFrame(F_APPEND, 0, new Object[]{ }, 0, new Object[]{ trueLabel });

F_APPEND 表示已添加新的变量,这与您添加stack 条目的明显意图不符。此外,仅当您引用NEW 指令的位置以表示未初始化的对象时,将标签指定为堆栈条目才有效。但是在这里,你推送了一个INTEGER 值。

正确的代码如下所示:

String className = "TestClass";
ClassWriter classWriter = new ClassWriter(0);
classWriter.visit(V1_8, ACC_PUBLIC, className, null, getInternalName(Object.class), null);
MethodVisitor mv = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "m", "()Z",null, null);
Label trueLabel = new Label();
Label afterFalseLabel = new Label();
mv.visitFieldInsn(GETSTATIC, getInternalName(Boolean.class),"TRUE","Ljava/lang/Boolean;");
mv.visitMethodInsn(INVOKEVIRTUAL,getInternalName(Boolean.class),"booleanValue","()Z",false);
mv.visitJumpInsn(IFEQ, trueLabel);
mv.visitInsn(ICONST_1);
mv.visitJumpInsn(GOTO, afterFalseLabel);
// target of IFEQ, the frame matches the initial frame (no variables, no stack entries)
mv.visitFrame(F_SAME, 0, null, 0, null);
mv.visitLabel(trueLabel);
mv.visitInsn(ICONST_0);
// merge point of the two branches, now having an INTEGER on the stack
mv.visitFrame(F_SAME1, 0, null, 1, new Object[]{ INTEGER });
mv.visitLabel(afterFalseLabel);
mv.visitInsn(IRETURN);
// no variable at all, at most one stack entry (the integer)
mv.visitMaxs(1, 0);
mv.visitEnd();
//converting classWriter.toByteArray() to Class<?> instance

注意,对于特殊的压缩帧类型,大部分参数都是隐含的,对于F_SAME,所有其他参数都无关紧要,对于F_SAME1,只有最后一个参数中指定的新堆栈条目类型很重要。

但您不需要处理不同类型的压缩帧。如果有疑问,您始终可以指定 F_NEW 并提供假定堆栈框架布局的完整描述。唯一的区别是(稍微)更大的类文件。对于动态类生成,这可能完全无关紧要,即使对于在部署之前添加到应用程序的生成类,差异也可以忽略不计:

String className = "TestClass";
ClassWriter classWriter = new ClassWriter(0);
classWriter.visit(V1_8, ACC_PUBLIC, className, null, getInternalName(Object.class), null);
MethodVisitor mv = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "m", "()Z",null, null);
Label trueLabel = new Label();
Label afterFalseLabel = new Label();
mv.visitFieldInsn(GETSTATIC, getInternalName(Boolean.class),"TRUE","Ljava/lang/Boolean;");
mv.visitMethodInsn(INVOKEVIRTUAL,getInternalName(Boolean.class),"booleanValue","()Z",false);
mv.visitJumpInsn(IFEQ, trueLabel);
mv.visitInsn(ICONST_1);
mv.visitJumpInsn(GOTO, afterFalseLabel);
// target of IFEQ, the frame state is "no variables, no stack entries"
mv.visitFrame(F_NEW, 0, null, 0, null);
mv.visitLabel(trueLabel);
mv.visitInsn(ICONST_0);
// merge point of the two branches, frame state is "no variables, one INTEGER on the stack"
mv.visitFrame(F_NEW, 0, null, 1, new Object[]{ INTEGER });
mv.visitLabel(afterFalseLabel);
mv.visitInsn(IRETURN);
// no variable at all, at most one stack entry (the integer)
mv.visitMaxs(1, 0);
mv.visitEnd();

顺便说一句,我觉得将抽象名称生成(如getInternalName( Boolean.class))与硬编码签名(如"Ljava/lang/Boolean;")结合起来有点奇怪。两者都是有效的,但最好是始终如一地决定哪种方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 2012-05-18
    • 1970-01-01
    • 2012-01-17
    • 2016-08-11
    相关资源
    最近更新 更多