Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8342697

Parameter arrays that are capped during annotation processing report incorrect length

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 24
    • tools
    • None
    • behavioral
    • low
    • Certain Java programs that would previously fail at runtime will now be rejected by the javac compiler
    • Implementation

      Summary

      Certain programs that comply with the JLS may fail at runtime due to limitations in the VM, as described by the JVMS.

      Problem

      The JVMS defines the RuntimeVisibleAnnotations, RuntimeInvisibleAnnotations, RuntimeVisibleParameterAnnotations, RuntimeInvisibleParameterAnnotations, RuntimeVisibleTypeAnnotations and RuntimeInvisibleTypeAnnotations Attributes such that any array must have a length greater than zero and less than 65536. While the JLS does not impose an upper limit on array lengths, the classfile format does.

      Users writing code that violates the JVMS constraints for annotations may encounter runtime failures, which could lead to unexpected behavior.

      Solution

      The javac compiler will reject annotations and type annotations with a retention policy of CLASS or RUNTIME if any array element has more than 65535 elements and a compile-time error will be generated.

      Annotations and type annotations with SOURCE retention are not impacted by this check.

      Specification

      Of note for this CSR are sections §4.7.16 titled "The RuntimeVisibleAnnotations Attribute", §4.7.17 titled "the RuntimeInvisibleAnnotations Attribute", §4.7.18 titled "The RuntimeVisibleParameterAnnotations Attribute", §4.7.19 titled "The RuntimeInvisibleParameterAnnotations Attribute", §4.7.20 titled "The RuntimeVisibleTypeAnnotations Attribute" and §4.7.21 titled "The RuntimeInvisibleTypeAnnotations Attribute" of the JVMS 23 specification:

      section §4.7.16 reads:

      The RuntimeVisibleAnnotations attribute has the following format:
      
      RuntimeVisibleAnnotations_attribute {
          u2         attribute_name_index;
          u4         attribute_length;
          u2         num_annotations;
          annotation annotations[num_annotations];
      }

      section §4.7.17 reads:

      The RuntimeInvisibleAnnotations attribute has the following format:
      
      RuntimeInvisibleAnnotations_attribute {
          u2         attribute_name_index;
          u4         attribute_length;
          u2         num_annotations;
          annotation annotations[num_annotations];
      }

      section §4.7.18 reads:

      The RuntimeVisibleParameterAnnotations attribute has the following format:
      
      RuntimeVisibleParameterAnnotations_attribute {
          u2 attribute_name_index;
          u4 attribute_length;
          u1 num_parameters;
          {   u2         num_annotations;
              annotation annotations[num_annotations];
          } parameter_annotations[num_parameters];
      }

      num_annotations, the array's size is explicitly limited to the u2 type, this is the key reason why we cannot exceed 65535 (unsigned 16-bit value). The same limit on the num_annotations field is imposed on all other annotation types mentioned earlier.

      No specifications are proposed to be changed under this CSR.

      Note

      The proposed text for the compile-time error is: Annotation array element too large in "<Annotation>"

            nbenalla Nizar Benalla
            lfoltan Lois Foltan
            Vicente Arturo Romero Zaldivar
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: