JDK-8322651 : 4.7.2: reject ConstantValue attributes on non-static fields
  • Type: Bug
  • Component: specification
  • Sub-Component: vm
  • Affected Version: 21
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2023-12-21
  • Updated: 2023-12-21
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.
Other
tbdUnresolved
Related Reports
Relates :  
Relates :  
Description
The ConstantValue attribute is specified to be "silently ignored" if the associated field has its ACC_STATIC flag set.

In practice, HotSpot treats a ConstantValue attribute appearing on a non-static field as a "non-predefined" attribute, not only ignoring the body of the attribute, but also ignoring duplicates, despite the rule that "There may be at most one ConstantValue attribute in the attributes table of a field_info structure".

This sort of flag-dependent categorization of attributes (as determined by ACC_STATIC) is not supported by 4.7, and envisions a finer-grained distinction between predefined and non-predefined attributes than the specification is equipped to handle. Of course, we can make a special-case rule, but the added complexity does not seem justified.

A non-static field with this attribute very likely expects the attribute to have some effect, and the creator of the class file would benefit from having the issue flagged at class loading. Non-static fields of existing classes are unlikely to have the attribute at all, since it has no run time effect.

Proposed change: or class or interface with ConstantValue applied to a non-static field violates format checking, and should fail to load.
Comments
Proposed changes: The `ConstantValue` attribute is a fixed-length attribute in the `attributes` table of a `field_info` structure ([4.5]). A `ConstantValue` attribute represents the value of a constant expression (JLS ยง15.28)~~, and is used as follows:~~ **.** **The field represented by the `field_info` structure is assigned the value represented by its `ConstantValue` attribute as part of the initialization of the class or interface declaring the field ([5.5]). This occurs prior to the invocation of the class or interface initialization method of that class or interface ([2.9.2]).** - ~~If the `ACC_STATIC` flag in the `access_flags` item of the `field_info` structure is set, then the field represented by the `field_info` structure is assigned the value represented by its `ConstantValue` attribute as part of the initialization of the class or interface declaring the field ([5.5]). This occurs prior to the invocation of the class or interface initialization method of that class or interface ([2.9.2]).~~ - ~~Otherwise, the Java Virtual Machine must silently ignore the attribute.~~ There may be at most one `ConstantValue` attribute in the `attributes` table of a `field_info` structure **representing a `static` field**. **There must not be a `ConstantValue` attribute in the `attributes` table of a `field_info` structure representing a non-`static` field.**
21-12-2023

Notice that the 'Code' attribute (4.7.3) is more strict: "If the method is either native or abstract, and is not a class or interface initialization method, then its method_info structure must not have a Code attribute in its attributes table."
21-12-2023