StackMapTable format error: bad type array size in method

Adam Sotona adam.sotona at oracle.com
Tue Aug 20 09:32:25 UTC 2024


Hello,
There is a problem in StackCounter calculation of maxLocals in combination with custom StackMapTableAttribute covering “dead code”.

Feel free to report it as a bug or let me know and I’ll fill it.

Thank you,
Adam

From: classfile-api-dev <classfile-api-dev-retn at openjdk.org> on behalf of Rafael Winterhalter <rafael.wth at gmail.com>
Date: Monday, 19 August 2024 at 22:23
To: classfile-api-dev <classfile-api-dev at openjdk.org>
Subject: StackMapTable format error: bad type array size in method
Hello,
when testing some of the corner cases within the unit tests of Byte Buddy, I found some further errors when it comes to dead code and using ClassWriter. All those cases work when using ASM. As a reproducer, consider the following:

    ClassFile classFile = ClassFile.of(ClassFile.StackMapsOption.DROP_STACK_MAPS);
    byte[] bytes = classFile.build(ClassDesc.of("foo.Bar"), classBuilder -> classBuilder.withMethod(
            "foo",
            MethodTypeDesc.ofDescriptor("()J"),
            0,
            methodBuilder -> {
                methodBuilder.withCode(codeBuilder -> {
                    codeBuilder.new_(ClassDesc.of(RuntimeException.class.getName()));
                    codeBuilder.dup();
                    codeBuilder.invokespecial(ClassDesc.of(RuntimeException.class.getName()),
                            "<init>",
                            MethodTypeDesc.ofDescriptor("()V"));
                    codeBuilder.athrow();
                    Label f2 = codeBuilder.newBoundLabel();
                    codeBuilder.lstore(1);
                    Label f3 = codeBuilder.newBoundLabel();
                    codeBuilder.lload(1);
                    codeBuilder.lreturn();
                    codeBuilder.with(StackMapTableAttribute.of(List.of(
                            StackMapFrameInfo.of(f2,
                                    List.of(StackMapFrameInfo.ObjectVerificationTypeInfo.of(ClassDesc.of("foo.Bar"))),
                                    List.of(StackMapFrameInfo.SimpleVerificationTypeInfo.ITEM_LONG)),
                            StackMapFrameInfo.of(f3,
                                    List.of(StackMapFrameInfo.ObjectVerificationTypeInfo.of(ClassDesc.of("foo.Bar")),
                                            StackMapFrameInfo.SimpleVerificationTypeInfo.ITEM_LONG),
                                    List.of())
                    )));
                });
            }));
    new ClassLoader() {
        @Override
        protected Class<?> findClass(String name) throws ClassNotFoundException {
            if (name.equals("foo.Bar")) {
                return defineClass(name, bytes, 0, bytes.length);
            } else {
                return super.findClass(name);
            }
        }
    }.findClass("foo.Bar").getMethods();
It gives a class format error. Java agents sometimes have to process all kinds of strange byte code, so ideally these cases should be supported. Is this a bug in the stack map frame attribute writer?

Thanks! Rafael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20240820/94f82593/attachment-0001.htm>


More information about the classfile-api-dev mailing list