Re: ASM 4.0 RC2: Revisiting the NOP ATHROW bug
Eric Bruneton <[email protected]>
| Newsgroups | gmane.comp.java.objectweb.asm |
|---|---|
| Message-ID | <[email protected]> |
> The python program: > > def addsitedir(sitedir, known_paths=None): > if known_paths is None: > reset = 1 > try: > names = os.listdir(sitedir) > except os.error: > return > return known_paths > > if __name__ == '__main__': > _test() > > > Gives an error: > > Exception in thread "main" java.lang.VerifyError: Stack map does not > match the one at exception handler 103 in method > site$py._addsitedir$1(Lorg/python/core/PyFrame;Lorg/python/core/ThreadState;Lorg/python/core/PyObject;Lorg/python/core/PyObject;Lorg/python/core/PyObject;)Lorg/python/core/PyObject; > at offset 88 > > The generated class file is here: > http://pylonshq.com/pasties/1a43b011ac411cba6cc9fcb88ef5254a > The method of interest is _addsitedir which starts at line 442. > > The ASM generated code is at: > http://pylonshq.com/pasties/378d9ad2bc539623fb08d510f4da81cf and again > the method begins at line 93. ok, thanks. This time I have all the information I need :-) The problem has nothing to do with deadcode and NOP ATHROW. I think you were confused because you often have generated deadcode. The problem comes from the fact that you don't call visitTryCatchBlock *before* you visit the labels passed as arguments, as described in the Javadoc of visitTryCatchBlock. This is required for COMPUTE_FRAMES to work properly. You could have found this with a CheckClassAdapter in your generation chain (which also detects that you don't call visitCode, as required). See attached code. Eric
Test.java
(text/x-java, 6.7 KB)
import java.io.PrintWriter;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import static org.objectweb.asm.Opcodes.*;
import org.objectweb.asm.util.CheckClassAdapter;
import org.objectweb.asm.util.TraceClassVisitor;
public class Test {
public static void main(String[] args) throws Exception {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
CheckClassAdapter ca = new CheckClassAdapter(cw, false);
ClassVisitor cv = new TraceClassVisitor(ca, new PrintWriter(System.out, true));
cv.visit(V1_7, ACC_PUBLIC, "C", null, "java/lang/Object", null);
MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "_addsitedir$1", "(Lorg/python/core/PyFrame;Lorg/python/core/ThreadState;Lorg/python/core/PyObject;Lorg/python/core/PyObject;Lorg/python/core/PyObject;)Lorg/python/core/PyObject;", null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitVarInsn(ALOAD, 1);
mv.visitInsn(ICONST_4);
mv.visitMethodInsn(INVOKEVIRTUAL, "org/python/core/PyFrame", "setline", "(I)V");
mv.visitFieldInsn(GETSTATIC, "site$py", "_1", "Lorg/python/core/PyString;");
mv.visitInsn(POP);
mv.visitVarInsn(ALOAD, 1);
mv.visitInsn(ICONST_5);
mv.visitMethodInsn(INVOKEVIRTUAL, "org/python/core/PyFrame", "setline", "(I)V");
mv.visitVarInsn(ALOAD, 5);
mv.visitVarInsn(ASTORE, 6);
mv.visitVarInsn(ALOAD, 1);
mv.visitLdcInsn("None");
mv.visitMethodInsn(INVOKEVIRTUAL, "org/python/core/PyFrame", "getglobal", "(Ljava/lang/String;)Lorg/python/core/PyObject;");
mv.visitVarInsn(ALOAD, 6);
mv.visitInsn(SWAP);
mv.visitMethodInsn(INVOKEVIRTUAL, "org/python/core/PyObject", "_is", "(Lorg/python/core/PyObject;)Lorg/python/core/PyObject;");
mv.visitInsn(ACONST_NULL);
mv.visitVarInsn(ASTORE, 6);
mv.visitMethodInsn(INVOKEVIRTUAL, "org/python/core/PyObject", "__nonzero__", "()Z");
Label l1 = new Label();
mv.visitJumpInsn(IFEQ, l1);
mv.visitVarInsn(ALOAD, 1);
mv.visitIntInsn(BIPUSH, 6);
mv.visitMethodInsn(INVOKEVIRTUAL, "org/python/core/PyFrame", "setline", "(I)V");
mv.visitFieldInsn(GETSTATIC, "site$py", "_2", "Lorg/python/core/PyInteger;");
mv.visitVarInsn(ASTORE, 6);
mv.visitVarInsn(ALOAD, 1);
mv.visitInsn(ICONST_2);
mv.visitVarInsn(ALOAD, 6);
mv.visitMethodInsn(INVOKEVIRTUAL, "org/python/core/PyFrame", "setlocal", "(ILorg/python/core/PyObject;)V");
mv.visitInsn(ACONST_NULL);
mv.visitVarInsn(ASTORE, 6);
Label l2 = new Label();
mv.visitJumpInsn(GOTO, l2);
mv.visitLabel(l1);
mv.visitLabel(l2);
Label l3 = new Label();
Label l4 = new Label();
Label l6 = new Label();
// mv.visitTryCatchBlock(l3, l4, l6, "java/lang/Throwable"); // uncomment this line (and comment the same line below) to make this code work
mv.visitLabel(l3);
mv.visitVarInsn(ALOAD, 1);
mv.visitIntInsn(BIPUSH, 8);
mv.visitMethodInsn(INVOKEVIRTUAL, "org/python/core/PyFrame", "setline", "(I)V");
mv.visitVarInsn(ALOAD, 2);
mv.visitVarInsn(ALOAD, 1);
mv.visitLdcInsn("os");
mv.visitMethodInsn(INVOKEVIRTUAL, "org/python/core/PyFrame", "getglobal", "(Ljava/lang/String;)Lorg/python/core/PyObject;");
mv.visitLdcInsn("listdir");
mv.visitMethodInsn(INVOKEVIRTUAL, "org/python/core/PyObject", "__getattr__", "(Ljava/lang/String;)Lorg/python/core/PyObject;");
mv.visitVarInsn(ALOAD, 4);
mv.visitInvokeDynamicInsn("_", "(Lorg/python/core/ThreadState;Lorg/python/core/PyObject;Lorg/python/core/PyObject;)Lorg/python/core/PyObject;", new Handle(H_INVOKESTATIC, "org/python/core/opt/SpecializeCallSite", "bootstrap", "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"), new Object[]{});
mv.visitVarInsn(ASTORE, 6);
mv.visitVarInsn(ALOAD, 1);
mv.visitInsn(ICONST_3);
mv.visitVarInsn(ALOAD, 6);
mv.visitMethodInsn(INVOKEVIRTUAL, "org/python/core/PyFrame", "setlocal", "(ILorg/python/core/PyObject;)V");
mv.visitInsn(ACONST_NULL);
mv.visitVarInsn(ASTORE, 6);
mv.visitLabel(l4);
Label l5 = new Label();
mv.visitJumpInsn(GOTO, l5);
mv.visitLabel(l6);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKESTATIC, "org/python/core/Py", "setException", "(Ljava/lang/Throwable;Lorg/python/core/PyFrame;)Lorg/python/core/PyException;");
mv.visitVarInsn(ASTORE, 6);
mv.visitVarInsn(ALOAD, 6);
mv.visitVarInsn(ALOAD, 1);
mv.visitLdcInsn("os");
mv.visitMethodInsn(INVOKEVIRTUAL, "org/python/core/PyFrame", "getglobal", "(Ljava/lang/String;)Lorg/python/core/PyObject;");
mv.visitLdcInsn("error");
mv.visitMethodInsn(INVOKEVIRTUAL, "org/python/core/PyObject", "__getattr__", "(Ljava/lang/String;)Lorg/python/core/PyObject;");
mv.visitMethodInsn(INVOKEVIRTUAL, "org/python/core/PyException", "match", "(Lorg/python/core/PyObject;)Z");
Label l7 = new Label();
mv.visitJumpInsn(IFEQ, l7);
mv.visitVarInsn(ALOAD, 1);
mv.visitIntInsn(BIPUSH, 10);
mv.visitMethodInsn(INVOKEVIRTUAL, "org/python/core/PyFrame", "setline", "(I)V");
mv.visitVarInsn(ALOAD, 1);
mv.visitInsn(ICONST_M1);
mv.visitFieldInsn(PUTFIELD, "org/python/core/PyFrame", "f_lasti", "I");
mv.visitFieldInsn(GETSTATIC, "org/python/core/Py", "None", "Lorg/python/core/PyObject;");
mv.visitInsn(ARETURN);
Label l8 = new Label();
mv.visitJumpInsn(GOTO, l8);
mv.visitLabel(l7);
mv.visitVarInsn(ALOAD, 6);
mv.visitInsn(ATHROW);
mv.visitLabel(l5);
mv.visitLabel(l8);
mv.visitTryCatchBlock(l3, l4, l6, "java/lang/Throwable");
mv.visitVarInsn(ALOAD, 1);
mv.visitIntInsn(BIPUSH, 11);
mv.visitMethodInsn(INVOKEVIRTUAL, "org/python/core/PyFrame", "setline", "(I)V");
mv.visitVarInsn(ALOAD, 5);
mv.visitVarInsn(ASTORE, 6);
mv.visitVarInsn(ALOAD, 1);
mv.visitInsn(ICONST_M1);
mv.visitFieldInsn(PUTFIELD, "org/python/core/PyFrame", "f_lasti", "I");
mv.visitVarInsn(ALOAD, 6);
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
cv.visitEnd();
byte[] b = cw.toByteArray();
ClassReader cr = new ClassReader(b);
cr.accept(new TraceClassVisitor(new PrintWriter(System.out, true)), 0);
}
}
message-footer.txt
(text/plain, 238 B)
-- You receive this message as a subscriber of the [email protected] mailing list. To unsubscribe: mailto:[email protected] For general help: mailto:[email protected]?subject=help OW2 mailing lists service home page: http://www.ow2.org/wws