Stack size miscalculated with try-catch blocks

Adam Sotona adam.sotona at oracle.com
Wed Oct 30 09:29:53 UTC 2024


Hi David,
Provided test case also calculates the maxstack correctly for me (with the recent codebase).

Thanks,
Adam

From: classfile-api-dev <classfile-api-dev-retn at openjdk.org> on behalf of david32768 at btinternet.com david32768 at btinternet.com <david32768 at btinternet.com>
Date: Wednesday, 30 October 2024 at 2:56
To: classfile-api-dev at openjdk.org <classfile-api-dev at openjdk.org>
Subject: Re: Stack size miscalculated with try-catch blocks

The problem is in StackMapGenerate.

The original method name of Frame.CheckStack is misleading as it adds 1 to index.

The recent changes assume it does not but there are still calls which assume this.



testcode
```
import module java.base;

public class MaxStack  {

   private static void build(CodeBuilder cob) {
       cob
          .iload(0)
          .i2l()
          .lreturn();
   }

   public static void main(String[] args) throws IOException {
       ClassFile cf = v; // MaxStack = 3
//        ClassFile cf = v; // MaxStack = 2
       byte[] bytes = cf
               .build(ClassDesc.of("TestMaxStack"),
           clb -> clb.withFlags(ClassFile.ACC_PUBLIC  | ClassFile.ACC_SUPER)
        .withVersion(49,0)
               .withMethodBody("Table", MethodTypeDesc.ofDescriptor("(I)J"),
                    ClassFile.ACC_STATIC,
             MaxStack::build));
       cf = ClassFile.of();
       ClassModel cm = cf.parse(bytes);
       cm.methods().stream()
               .flatMap(m -> m.attributes().stream())
        .filter(attr -> attr instanceof CodeAttribute)
        .map(attr -> ((CodeAttribute)attr).maxStack())
        .map(sz -> "MaxStack = " + sz)
               .forEach(System.out::println);
   }
}
```




------ Original Message ------
From: rafael.wth at gmail.com
To: classfile-api-dev at openjdk.org
Sent: Thursday, October 24th 2024, 07:33
Subject: Stack size miscalculated with try-catch blocks

Hello,

when testing a recent build of the JDK with my bridge to ASM, I encountered some test failures. This is related to the stack size of a method being calculated with an additional, unnecessary slot. This can be reproduced using https://github.com/raphw/asm-jdk-bridge

This can also be reproduced by inspecting the computed stack for:

void c() {
    try {
        throw new RuntimeException();
    } catch (RuntimeException e) {
    }
}
which will be computed as 3 and not 2.

Best regards, Rafael

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20241030/9e5d5582/attachment.htm>


More information about the classfile-api-dev mailing list