Stack size miscalculated with try-catch blocks

david32768@btinternet.com david32768@btinternet.com david32768 at btinternet.com
Wed Oct 30 01:55:53 UTC 2024


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 = 
ClassFile.of(ClassFile.StackMapsOption.GENERATE_STACK_MAPS); // MaxStack 
= 3
//        ClassFile cf = ClassFile.of(); // 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 
<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/7851555d/attachment.htm>


More information about the classfile-api-dev mailing list