我使用ASM来检测一个类文件,被检测的类抛出了VerifyError。错误信息如下:
java.lang.VerifyError: StackMapTable error: bad offset
Exception Details:
Location:
org/joda/time/TestAllPackages.suite()Ljunit/framework/Test; @0: invokestatic
Reason:
Invalid stackmap specification.
Current Frame:
bci: @88
flags: { }
locals: { }
stack: { }
Bytecode:
0x0000000: b800 5bb6 005f 3f1e 1276 b800 67bb 0002
0x0000010: 59b7 0003 4d2c b800 04b6 0005 2cb8 0006
0x0000020: b600 052c b800 07b6 0005 2cb8 0008 b600
0x0000030: 052c b800 09b6 0005 2cb8 000a b600 052c
0x0000040: b800 0bb6 0005 2c1e 1276 b800 74b0 a700
0x0000050: 0a1e 1276 b800 74bf
Exception Handler Table:
bci [0, 78] => handler: 81
Stackmap Table:
same_locals_1_stack_item_extended(@81,Object[#85])
same_frame(@88)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at org.apache.maven.surefire.common.junit3.JUnit3Reflector.createInstanceFromSuiteMethod(JUnit3Reflector.java:153)
at org.apache.maven.surefire.common.junit3.JUnit3Reflector.constructTestObject(JUnit3Reflector.java:124)
at org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:75)
at org.apache.maven.surefire.junit.JUnit3Provider.executeTestSet(JUnit3Provider.java:140)
at org.apache.maven.surefire.junit.JUnit3Provider.invoke(JUnit3Provider.java:113)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:379)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:340)
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:125)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:413)
插装的类相当简单:org.joda.time.TestAllPackages.class。我手动使用visitFrames
插入那些堆栈帧(没有使用COMPUTE_FRAMES,这很痛苦),因为我想自己正确地编写visitFrames
。
我真的很想知道发生了什么,也就是说,类文件有什么问题,因为我没有发现任何可疑的东西。
谢谢!
顺便说一句,我的仪表代码是这样的:
...
private Label tryStart = new Label();
private Label tryEnd = new Label();
private Label catchStart = new Label();
private Label catchEnd = new Label();
public XxxMethodVisitor(MethodVisitor mv, String methodName, int access, String desc, String className, boolean isStatic, boolean isPublic) {
super(Config.ASM_VERSION, mv);
this.methodName = methodName;
this.className = className;
this.selfDesc = desc;
selfReturnType = Type.getReturnType(desc);
selfMethodId = String.format("%s%c%s%c%s", className, SEPARATOR, methodName, SEPARATOR, desc);
this.isStatic = isStatic;
this.isPublic = isPublic;
}
// start of the method
public void visitCode() {
mv.visitCode();
// mv.visitFrame(F_NEW, initLocals.length, initLocals, 0, new Object[]{});
mv.visitTryCatchBlock(tryStart, tryEnd, catchStart, "java/lang/Throwable");
mv.visitLabel(tryStart);
...
}
public void visitEnd() {
mv.visitLabel(tryEnd);
mv.visitJumpInsn(GOTO, catchEnd);
mv.visitLabel(catchStart);
// exception caught
mv.visitFrame(F_SAME1, 0, null, 1, new Object[] {"java/lang/Throwable"});
...
mv.visitInsn(ATHROW);
mv.visitLabel(catchEnd);
mv.visitFrame(F_SAME, 0, null, 0, null);
super.visitEnd();
}
我不知道为什么验证错误是抱怨堆栈映射表,当有一个更明显的问题:goto
指令分支到偏移84,这是刚刚过去的suite
方法的结束。这是我在检查你链接到的类时发现的。来自验证错误的转储看起来不同,但它引用了"bci: @88",这也是越界的。