-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8372696: Allow boot classes to explicitly opt-in for final field trusting #28540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
liach
wants to merge
9
commits into
openjdk:master
Choose a base branch
from
liachmodded:feature/class-final-trusting
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+269
−14
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e1036da
Stage
liach 3cf5d0c
Merge branch 'master' of https://github.com/openjdk/jdk into feature/…
liach d18fcfb
Fixed optional and unit test
liach 5480f87
Issue number and test update
liach 77dd330
Merge branch 'master' of https://github.com/openjdk/jdk into feature/…
liach f02b9da
Spurious change
liach 712dbf1
Essay
liach 7a1cfa4
Doc tweaks
liach d353bdb
bracket styles
liach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/java.base/share/classes/jdk/internal/vm/annotation/TrustFinalFields.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. Oracle designates this | ||
| * particular file as subject to the "Classpath" exception as provided | ||
| * by Oracle in the LICENSE file that accompanied this code. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
|
|
||
| package jdk.internal.vm.annotation; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /// Indicates all instance final fields declared in the annotated class should | ||
| /// be trusted as constants by compilers in `ciField::is_constant`. | ||
| /// | ||
| /// The compiler already treats static final fields and instance final fields in | ||
| /// record classes and hidden classes as constant. All classes in select | ||
| /// packages (Defined in `trust_final_non_static_fields` in `ciField.cpp`) in | ||
| /// the boot class loader also have their instance final fields trusted. This | ||
| /// annotation is not necessary in these cases. | ||
| /// | ||
| /// The [Stable] annotation treats fields as constants once they are not the | ||
| /// zero or null value. In comparison, a non-stable final instance field | ||
| /// trusted by this annotation can treat zero and null values as constants. | ||
| /// | ||
| /// This annotation is suitable when constant treatment of final fields is | ||
| /// performance sensitive, yet package-wide final field constant treatment may | ||
| /// be at risk from user final field modifications. | ||
| /// | ||
| /// See `constant-folding.md` design document in the same directory as this file | ||
| /// for an overview and the best practices around constant folding, including | ||
| /// for this annotation. | ||
| /// | ||
| /// This annotation is only recognized on classes from the boot and platform | ||
| /// class loaders and is ignored elsewhere. | ||
| /// | ||
| /// @since 26 | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target(ElementType.TYPE) | ||
| public @interface TrustFinalFields { | ||
| } |
104 changes: 104 additions & 0 deletions
104
src/java.base/share/classes/jdk/internal/vm/annotation/constant-folding.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| Constant Folding | ||
| === | ||
|
|
||
| Hotspot compiler can fold constant field value access when it constructs its IR | ||
| (intermediate representation), unlocking significant performance improvements. | ||
| However, it is implemented as a dangerous primitive that can lead to incorrect | ||
| programs if used incorrectly. | ||
|
|
||
| ## What is constant folding? | ||
|
|
||
| Constant folding means a read of a variable of a constant value can be replaced | ||
| by the read constant value, during the construction of an IR graph. The | ||
| related logic resides in `ci/ciField.hpp` (compiler interface field). | ||
|
|
||
| For example, if a field `int a` has a constant value `4` and a field `int b` has | ||
| constant value `5`, the constant folding will replace the `a + b` in the IR with | ||
| a value of `9`. | ||
|
|
||
| ## How is a value determined to be constant? | ||
|
|
||
| Constantness is decided on a per-variable and per-value basis. This includes | ||
| the location of the variable so that a variable might have a constant value, | ||
| and the value of the variable so the read value is constant. | ||
|
|
||
| ### Field constants | ||
|
|
||
| Whether a field may have a constant value is determined by the | ||
| `ciField::is_constant()` method in Hotspot. The value of `_is_constant` is | ||
| determined in `ciField::initialize_from`. It is roughly as follows: | ||
|
|
||
| 1. A field may be constant if it is stable. | ||
| 2. Before Java 9, `putfield` in `<clinit>` could write to an instance final | ||
| field and `putstatic` in `<init>` could write to a static final field. Such | ||
| written final fields from pre-53-major classes are considered never constant. | ||
| 3. Otherwise, if a static final field is not `System.in`, `System.out`, or | ||
| `System.err`, it may be constant. | ||
| 4. If an instance final field comes from a record class, a hidden class, it may | ||
| be constant. | ||
| 5. If an instance final field is declared in a system class that is either: | ||
|
|
||
| 1. Marked `@TrustFinalFields` | ||
| 2. In one of the trusted system packages specified in | ||
| `trust_final_non_static_fields` in `ciField.cpp` | ||
|
|
||
| It may be constant. Note that such a field is not protected from reflective | ||
| modification through core reflection `Field.set`. | ||
| 6. If the field is `CallSite.target`, it may be constant. (This has extra | ||
| treatments like nmethod dependency, so is not quite as other constant | ||
| fields) | ||
|
|
||
| A `ciField` models a field declaration in the JVM, so an inherited field (as | ||
| in a field reference, static or instance) in a subclass or subinterface shares | ||
| the constantness settings. | ||
|
|
||
| After a field is considered to be possibly constant, its value is fetched from | ||
| the runtime and examined. If the field is stable, and the value is zero or null | ||
| (the default value), this read value is not constant. Only non-stable final | ||
| fields can have their zero or null values considered constant. | ||
|
|
||
| ### Array constants | ||
|
|
||
| If an array field is stable, the type system in the compiler of Hotspot marks | ||
| the array to be stable up to its declared level. (See the code of the most | ||
| generic variant of `Type::make_constant_from_field`) As a result, access to | ||
| nested array components _with a constant index_ can be treated as a constant | ||
| value, if the read value is not zero or null (the default value). | ||
|
|
||
| This means the stable annotation is not as helpful for random access (it only | ||
| elides loading the array reference), and null components in an "immutable" array | ||
| may cause surprising slowdowns. | ||
|
|
||
| ## How can I verify constant folding? | ||
|
|
||
| Since constant folding makes a huge difference in API performance characteristics, | ||
| tests are necessary to guarantee they happen. | ||
|
|
||
| The most reliable way to ensure folding is IR tests in the compiler; we can | ||
| expect compiler to eliminate known foldable IR structures when its inputs are | ||
| eligible. For example, in the initial constant folding example of `a + b`, an | ||
| IR test can verify the int addition is eliminated in the resulting IR by | ||
| constant folding. | ||
|
|
||
| An example test is `compiler.c2.irTests.constantFold.TestOptional`. Note that | ||
| IR tests need to be run in a debug (fastdebug) configure profile, which is not | ||
| used for most jdk library tests. | ||
|
|
||
| JMH benchmarks can be another way to verify, except they are costly to run and | ||
| their trend is hard to track. Prefer the compiler tests instead. | ||
|
|
||
| ## Relation to final mechanisms | ||
|
|
||
| ### `Field.trustedFinal` property | ||
|
|
||
| A `Field` object has a `trustedFinal` field, which when set to `true`, prevents | ||
| core reflection or method handles from creating a setter for this field in any | ||
| scenario. This is derived from `fieldDescriptor::is_trusted_final()`, which | ||
| designates final fields that are static, declared in a record class, or declared | ||
| in a hidden class as `trustedFinal`. This rule is different from the "may be | ||
| constant variable" rule from above; in particular, it does not protect the | ||
| instance final fields in eligible system classes per rule 5 above. | ||
|
|
||
| Note that a `Field` object also models a field declaration in the JVM like a | ||
| `ciField`, so an inherited field in a subclass or subinterface shares the | ||
| `trustedFinal` setting. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.