ASM - java.lang.VerifyError:操作数堆栈溢出异常

2024-05-16

我正在 Tomcat 8 和 JDK 8 中使用 ASM 5.0.3 字节代码库。 我也在用

ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_FRAMES);

and

classReader.accept(myClassVisitor, ClassReader.SKIP_FRAMES);

ASM 抛出以下异常

  Caused by: java.lang.VerifyError: Operand stack overflow
Exception Details:
  Location:      org/apache/tomcat/websocket/server/WsServerContainer.addEndpoint(Ljavax/websocket/server/ServerEndpointConfig;)V @0: aload_0
   Reason:
    Exceeded max stack size.
  Current Frame:
    bci: @0
    flags: { }
    locals: { 'org/apache/tomcat/websocket/server/WsServerContainer', 'javax/websocket/server/ServerEndpointConfig' }
    stack: { }
  Bytecode:
    0x0000000: 2ab4 000a 9900 1a2a b400 0b9a 0013 bb01
    0x0000010: 3d59 b200 4412 45b6 0046 b700 47bf 2ab4
    0x0000020: 000e c700 13bb 0043 59b2 0044 1248 b600
    0x0000030: 46b7 0047 bf2b b900 4901 004d bb00 4a59
    0x0000040: 2bb9 004b 0100 2bb9 004c 0100 2cb7 004d
    0x0000050: 4e2d b600 4ec7 0018 2db6 004f c700 112d
    0x0000060: b600 50c7 000a 2db6 0051 9900 122b b900
    0x0000070: 5201 0012 532d b900 5403 0057 bb00 5559
    0x0000080: 2cb7 0056 3a04 1904 b600 5799 0087 1904
    0x0000090: b600 58b8 0059 3a05 2ab4 0008 1905 b600
    0x00000a0: 5ac0 005b 3a06 1906 c700 29bb 005c 59b8
    0x00000b0: 005d b700 5e3a 062a b400 0819 0519 06b6
    0x00000c0: 005f 572a b400 0819 05b6 005a c000 5b3a
    0x00000d0: 0619 06bb 0060 592b 1904 b700 61b9 0062
    0x00000e0: 0200 9a00 2dbb 0043 59b2 0044 1263 06bd
    0x00000f0: 0064 5903 2c53 5904 2bb9 004b 0100 5359
    0x0000100: 052b b900 4b01 0053 b600 65b7 0047 bfa7
    0x0000110: 0043 2ab4 0007 2c2b b900 5403 00c0 0066
    0x0000120: 3a05 1905 c600 2ebb 0043 59b2 0044 1263
    0x0000130: 06bd 0064 5903 2c53 5904 1905 b900 4b01
    0x0000140: 0053 5905 2bb9 004b 0100 53b6 0065 b700
    0x0000150: 47bf 2a04 b500 0db1                    

    at org.apache.tomcat.websocket.server.WsSci.init(WsSci.java:131)
    at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:47)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5244)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

如何在不使用的情况下解决此问题-noverify option?

Edit #1:我也尝试使用简单的 COMPUTE_FRAMES ,如下所示:

ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_FRAMES);

我省略了以下 SKIP_FRAMES 语句:

//classReader.accept(myClassVisitor, ClassReader.SKIP_FRAMES);

我继续得到同样的"Caused by: java.lang.VerifyError: Operand stack overflow" error.

Edit #2:我已经尝试了以下所有组合,但遇到了相同的错误:java.lang.VerifyError: Operand stack overflow Reason:Exceeded max stack size.

  1. ClassWriter.COMPUTE_FRAMES with ClassReader.EXPAND_FRAMES and mv.visitMaxs(maxStack, maxLocals);
  2. ClassWriter.COMPUTE_FRAMES with ClassReader.EXPAND_FRAMES and mv.visitMaxs(-1, -1);
  3. ClassWriter.COMPUTE_FRAMES with ClassReader.SKIP_FRAMES and mv.visitMaxs(maxStack, maxLocals);
  4. ClassWriter.COMPUTE_FRAMES with ClassReader.SKIP_FRAMES and mv.visitMaxs(-1, -1);

编辑#3。这是我的建议适配器代码

public class PojoMethodAdviceAdapter extends AdviceAdapter  {
private String methodName;
private String className;
private String description;
private boolean excludeCheckFlag = false;
private int okFlag =  newLocal(Type.getType("Z")); //newLocal(Type.BOOLEAN_TYPE); 
private int classFileVersion;

Label startFinally = new Label();

private static Hashtable excludeMethods = new  Hashtable();
private static final String yesString = "Yes";
private boolean isValid;

static{
    excludeMethods.put("<clinit>", yesString);
    excludeMethods.put("<init>", yesString);
}

public PojoMethodAdviceAdapter(int access , MethodVisitor mv , String methodName, String description, String className, int classFileVersion){
    super(Opcodes.ASM5 , mv, access, methodName, description);
    this.className = className;
    this.methodName = methodName;
    this.description = description;
    this.excludeCheckFlag = true;
    this.isValid = true;
    this.classFileVersion = classFileVersion;

    String yesStr = (String)excludeMethods.get(this.methodName);
    if(yesStr!=null && yesStr.equals(yesString))
        isValid = false;
    if(this.methodName.indexOf("$") > 0)
        isValid = false;
    if(isValid){
        System.out.println(" [POJO MethodAdviceAdapter] :"+className+" \t "+methodName +" \t  "+description);
    }
}

public void visitCode() {
    super.visitCode(); 
    mv.visitLabel(startFinally);
}

protected void onMethodEnter(){
    if(isValid) {
        mv.visitInsn(Opcodes.ICONST_0);
        mv.visitVarInsn(ISTORE, okFlag);

        mv.visitLdcInsn(className);
        mv.visitLdcInsn(methodName);
        mv.visitLdcInsn(description);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/mini/agent/trace/RootTracer", "pojoMethodBegin", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z", false);
        mv.visitVarInsn(ISTORE, okFlag);
    }
}

protected void onMethodExit(int opcode){
    if(opcode!=ATHROW) {
        onFinally(opcode);
    }
}

public void visitMaxs(int maxStack, int maxLocals){
    Label endFinally = new Label();
    mv.visitTryCatchBlock(startFinally, endFinally, endFinally, null);
    mv.visitLabel(endFinally);
    onFinally(ATHROW);
    mv.visitInsn(ATHROW);
    if(classFileVersion <= 50){
        mv.visitMaxs(maxStack, maxLocals);
    }
    else{
        mv.visitMaxs(0, 0);
    }
}

private void onFinally(int opcode){
    if(isValid){
        if(opcode == ATHROW){
            mv.visitInsn(Opcodes.DUP);
            mv.visitLdcInsn(className);
            mv.visitLdcInsn(methodName);
            mv.visitLdcInsn(description);
            mv.visitVarInsn(ILOAD, okFlag);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/mini/agent/trace/RootTracer", "recordPOJOException", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)V", false);
        }

        mv.visitLdcInsn(className);
        mv.visitLdcInsn(methodName);
        mv.visitLdcInsn(description);
        mv.visitVarInsn(ILOAD, okFlag);
        mv.visitLdcInsn(opcode);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/mini/agent/trace/RootTracer", "pojoMethodEnd", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZI)V", false);
    }
}

}


尝试使用 TraceClassVisitor 看看哪里出错了。根据我的经验,当标签写入不正确时,就会发生这种情况,迫使它写入 jsr/ret 指令(在这种情况下,JVM 应该简单地拒绝该类,尽管在计算帧时 asm 会崩溃),并且最常见的是,忘记在调用visitEnd之前的methodVisitor。在我的代码中我使用

visitMaxs(mv.getMaxLocals(),mv.getMaxLocals());

我不记得这样做的原因是什么,但是当我这样做时,我的 ClassWriter 就可以工作,但我希望这会有所帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ASM - java.lang.VerifyError:操作数堆栈溢出异常 的相关文章

随机推荐