Switch targets are not inflated in CodeModel if no StackMap.

Adam Sotona adam.sotona at oracle.com
Mon Sep 2 08:43:59 UTC 2024


Yes, this is a bug in labels inflation for older class files versions.
I’ve filled a bug https://bugs.openjdk.org/browse/JDK-8339368 based on your test case and started work on a fix.

Thank you for discovering it!

From: classfile-api-dev <classfile-api-dev-retn at openjdk.org> on behalf of david32768 at btinternet.com <david32768 at btinternet.com>
Date: Saturday, 31 August 2024 at 9:18
To: classfile-api-dev at openjdk.org <classfile-api-dev at openjdk.org>
Subject: Switch targets are not inflated in CodeModel if no StackMap.

Method jdk/internal/CodeImpl.inflateJumpTargets should also inflate switch instructions.



Perhaps BranchInstruction, JsrInstruction, TableSwitchInstruction, LookupSwitchInstruction should implement an interface with method List<Label> targets().



build test method
```
   private static void build(CodeBuilder cob) {
       Label L1 = cob.newLabel();
       Label L2 = cob.newLabel();
       SwitchCase sc1 = SwitchCase.of(0,L2);
       List<SwitchCase> cases = new ArrayList<>();
       cases.add(sc1);
       cob.iload(0)
          .lookupswitch(L1, cases)
          .labelBinding(L2)
          .iconst_1()
          .ireturn()
          .labelBinding(L1)
          .iconst_0()
          .ireturn();
   }

```

print of elements in CodeModel
```
Load[OP=ILOAD_0, slot=0]
LookupSwitch[OP=LOOKUPSWITCH]
UnboundIntrinsicConstantInstruction[op=ICONST_1]
Return[OP=IRETURN]
UnboundIntrinsicConstantInstruction[op=ICONST_0]
Return[OP=IRETURN]

```

test driver class
```
   public static void main(String[] args) throws IOException {
       byte[] bytes = ClassFile.of()
               .build(ClassDesc.of("TestSwitchInflate"),
                     clb -> clb.withFlags(ClassFile.ACC_PUBLIC  | ClassFile.ACC_SUPER)
               .withVersion(49,0)
               .withMethodBody("Table", MethodTypeDesc.ofDescriptor("(I)I"),
                    ClassFile.ACC_STATIC, SwitchInflate::build));
       ClassFile.of().parse(bytes)
           .methods().stream()
                .flatMap(m -> m.code().stream())
               .flatMap(c -> c.elementStream())
               .forEach(System.out::println);
   }

```

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20240902/182c8ea6/attachment-0001.htm>


More information about the classfile-api-dev mailing list