JDK-8313258 : RuntimeInvisibleTypeAnnotationsAttribute.annotations() API Index out of Bound error
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang.classfile
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2023-07-27
  • Updated: 2024-02-14
  • Resolved: 2023-09-14
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 22
22 b16Fixed
Related Reports
Relates :  
Relates :  
Description
Test file javac/annotations/typeAnnotations/classfile/CombinationsTargetTest3.java use s ClassfileTestHelper.java to catch an RuntimeInvisibleTypeAnnotationsAttribute and count annotations number like this (within pattern match in switch):
```
                    case RuntimeInvisibleTypeAnnotationsAttribute invtAttr -> {
                        List <TypeAnnotation> tAnnots = invtAttr.annotations();
                        tinvisibles += tAnnots.size();
                        allt += tAnnots.size();
                    }
```
When calling (RuntimeInvisibleTypeAnnotationsAttribute) invtAttr.annotations() API, there is an Index out of Bound error in the CodeImpl.getLabel.

I attached the error screenshot. Test 25 was successfully compiled but fall into error.

This is the source code snippet in CodeImpl.getLabel method (line 102). 
```
    @Override
    public int labelToBci(Label label) {
        LabelImpl lab = (LabelImpl) label;
        if (lab.labelContext() != this)
            throw new IllegalArgumentException(String.format("Illegal label reuse; context=%s, label=%s",
                                                             this, lab.labelContext()));
        return lab.getBCI();
    }
```

I checked the original test with old sun.tools.classfile API, and found the bci  (which is 45) is correct. The size of LabelImpl[] (which is 37 according to the error message) generates the error.
Comments
Changeset: 6d47fc6d Author: Adam Sotona <asotona@openjdk.org> Date: 2023-09-14 18:29:01 +0000 URL: https://git.openjdk.org/jdk/commit/6d47fc6d5b81d6764af322cc17653683f79a89de
14-09-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/15511 Date: 2023-08-31 11:09:05 +0000
31-08-2023

Problematic source code is attached in Test25.java. From txt javap output, it appears the repeating annotation container AC is incorrectly placed on the testm method, while the non-repeating B is correctly placed on the lambda implementation method. This is more of a javac bug than a Classfile API bug. That said, the Classfile API still needs better handling for out-of-bound offset requests from type annotation attributes, that it should throw IAE; similar invalid attributes can appear from Classfile transformation as well.
10-08-2023