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

Correct Attribute traversal and writing for Code attributes

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Unresolved
    • Icon: P4 P4
    • 25
    • core-libs
    • None
    • behavioral
    • low
    • Hide
      Custom and unknown attributes are now delivered in code traversal; this may affect users who manually find and send custom attributes to code builders, but I believe this has not happened yet given the recent release of ClassFile API and no one has ever reported this erroneous non-delivery behavior.
      Show
      Custom and unknown attributes are now delivered in code traversal; this may affect users who manually find and send custom attributes to code builders, but I believe this has not happened yet given the recent release of ClassFile API and no one has ever reported this erroneous non-delivery behavior.
    • Java API
    • SE

      Summary

      Deliver CustomAttribute and UnknownAttribute in the streaming traversal of CodeModel as a CompoundElement, and adapt the labels in a StackMapTableAttribute when an existing one is sent to another CodeBuilder with incompatible labels.

      Problem

      1. UnknownAttribute and CustomAttribute are not delivered in the traversal of a CodeModel, and can only be accessed via the functionalites of AttributedElement. They are, however, delivered in the traversal of ClassModel, FieldModel, or MethodModel. Discussions revealed that this was probably an oversight in implementation instead of a deliberate design decison.

      2. StackMapTableAttribute may become invalid if it is reused after a code transformation. It should be expanded to refer to correct updated code labels/offsets. Other code-bound attributes are modeled as PseudoInstruction, and CodeBuilder does not accept those code-bound attributes and we do not need to update them.

      Solution

      1. Start delivering CustomAttribute and UnknownAttribute in code model traversal, and remove the text about their non-delivery; make UnknownAttribute extend CodeElement so it becomes eligible for delivery.
      2. When the labels in a CodeBuilder is not compatible with the original labels from a parsed StackMapTableAttribute, expand this attribute and rewrite its labels to restore its validity. (See AttributeMapper.AttributeStability.LABELS for more details) Other attribute are already automatically rewritten as they are decomposed into PseudoInstruction and reconstructed unconditionally.

      Specification

      diff --git a/src/java.base/share/classes/java/lang/classfile/CodeElement.java b/src/java.base/share/classes/java/lang/classfile/CodeElement.java
      index 9ca5138334403..3ad2ff16b355b 100644
      --- a/src/java.base/share/classes/java/lang/classfile/CodeElement.java
      +++ b/src/java.base/share/classes/java/lang/classfile/CodeElement.java
      @@ -49,5 +50,5 @@
       public sealed interface CodeElement extends ClassFileElement
               permits Instruction, PseudoInstruction,
                       CustomAttribute, RuntimeVisibleTypeAnnotationsAttribute, RuntimeInvisibleTypeAnnotationsAttribute,
      -                StackMapTableAttribute {
      +                StackMapTableAttribute, UnknownAttribute {
       }
      diff --git a/src/java.base/share/classes/java/lang/classfile/CustomAttribute.java b/src/java.base/share/classes/java/lang/classfile/CustomAttribute.java
      index 9f924588770cd..c4d34d75dd5d6 100644
      --- a/src/java.base/share/classes/java/lang/classfile/CustomAttribute.java
      +++ b/src/java.base/share/classes/java/lang/classfile/CustomAttribute.java
      @@ -36,9 +36,6 @@
        * by {@link #attributeMapper}, and registered to the {@link
        * ClassFile.AttributeMapperOption} so the user-defined attributes can be read.
        * <p>
      - * User-defined attributes are currently not delivered in the traversal of a
      - * {@link CodeModel}.
      - * <p>
        * Accessor methods on user-defined attributes read from {@code class} files
        * may throw {@link IllegalArgumentException} if the attribute model is lazily
        * evaluated, and the evaluation encounters malformed {@code class} file format
      diff --git a/src/java.base/share/classes/java/lang/classfile/attribute/UnknownAttribute.java b/src/java.base/share/classes/java/lang/classfile/attribute/UnknownAttribute.java
      index c35942bc42c86..28a6e9e183a3e 100644
      --- a/src/java.base/share/classes/java/lang/classfile/attribute/UnknownAttribute.java
      +++ b/src/java.base/share/classes/java/lang/classfile/attribute/UnknownAttribute.java
      @@ -35,8 +35,6 @@
        * unknown if it is not recognized by one of the mappers in {@link Attributes}
        * and is not recognized by the {@link ClassFile.AttributesProcessingOption}.
        * <p>
      - * This attribute is not delivered in the traversal of a {@link CodeModel}.
      - * <p>
        * An unknown attribute may appear anywhere where an attribute may appear, and
        * has an {@linkplain AttributeStability#UNKNOWN unknown} data dependency.
        *
      @@ -45,7 +43,7 @@
        */
       public sealed interface UnknownAttribute
               extends Attribute<UnknownAttribute>,
      -                ClassElement, MethodElement, FieldElement
      +                ClassElement, MethodElement, FieldElement, CodeElement
               permits BoundAttribute.BoundUnknownAttribute {
      
           /**

            liach Chen Liang
            liach Chen Liang
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: