RFR: 8372696: Allow boot classes to explicitly opt-in for final field trusting

Chen Liang liach at openjdk.org
Thu Nov 27 19:01:52 UTC 2025


On Thu, 27 Nov 2025 18:45:29 GMT, Alan Bateman <alanb at openjdk.org> wrote:

>> Currently, the hotspot compiler (as in ciField) trusts final fields in hidden classes, record classes, and selected jdk packages. Some classes in the JDK wish to be trusted, but they cannot apply package-wide opt-in due to other legacy classes in the package, such as java.util.
>> 
>> They currently can use `@Stable` as a workaround, but this is fragile because a stable final field may hold a trusted null, zero, or false value, which is currently treated as non-constant by ciField.
>> 
>> We should add an annotation to opt-in for a whole class, mainly for legacy packages. This would benefit greatly some of our classes already using a lot of Stable, such as java.util.Optional, whose empty instance is now constant-foldable, as demonstrated in a new IR test.
>> 
>> Paging @minborg who requested Optional folding for review.
>> 
>> I think we can remove redundant Stable in a few other java.util classes after this patch is integrated. I plan to do that in subsequent patches.
>
> src/java.base/share/classes/jdk/internal/vm/annotation/TrustFinalFields.java line 61:
> 
>> 59: /// fields in classes specified by this annotation.
>> 60: ///
>> 61: /// This annotation is only recognized on privileged code and is ignored elsewhere.
> 
> "privileged code" hints of protection domains, permissions or security manager. Some of the annotations are limited to classes defined by the boot loader, is it the case here too?

I took this sentence from `@AOTSafeClassInitializer`.  The term "privileged" comes from this variable in `classFileParser.cpp`:
https://github.com/openjdk/jdk/blob/d94c52ccf2fed3fc66d25a34254c9b581c175fa1/src/hotspot/share/classfile/classFileParser.cpp#L1818-L1820

The other annotations have this note, which seems incorrect from the hotspot excerpt:

@implNote
This annotation only takes effect for fields of classes loaded by the boot
loader.  Annotations on fields of classes loaded outside of the boot loader
are ignored.


This behavior seems to be originally changed by 6964a690ed9a23d4c0692da2dfbced46e1436355, referring to an inaccessible issue.

What should I do with this? Should I leave this as-is and create a separate patch to update this comment for vm.annotation annotations, or fix this first and have the separate patch fix other annotations later?

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/28540#discussion_r2569787223


More information about the hotspot-dev mailing list