Repository navigation
[svm] Run the class initializer simulation before folding IsStaticFinalFieldInitializedNode - #14922
Draft
MarkusBernhardt wants to merge 1 commit into
Draft
MarkusBernhardt wants to merge 1 commit into
MarkusBernhardt wants to merge 1 commit into
Conversation
…dInitializedNode InlineBeforeAnalysisGraphDecoderImpl folded IsStaticFinalFieldInitializedNode only if a simulation result for the declaring class was already published. Unlike for static LoadFieldNode and EnsureClassInitializedNode, it did not first wait for or run the simulation (processClassInitializer). When the field is read in a method of its declaring class, nothing else in the graph starts the simulation. Whether the static analysis saw the folded value or the uninitialized value of the field therefore depended on the order of analysis tasks, which made image builds non-deterministic. Call processClassInitializer before the lookup, as for static field loads. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
For a foldable static final field of a class that is initialized at run time,
StaticFinalFieldFoldingPhasereplaces the static field load withIsStaticFinalFieldInitialized(f) ? <folded value> : <uninitialized value>. During inlining before analysis,InlineBeforeAnalysisGraphDecoderImpl.handleIsStaticFinalFieldInitializedNodefolds the check totrueif the class initializer of the declaring class is simulated.The handler only looks for a simulation result that has already been published (
isSimulatedOrInitializedAtBuildTime). The sibling handlers forLoadFieldNodeandEnsureClassInitializedNodefirst callprocessClassInitializer. It waits for the type's metadata task (getInitializeMetaDataTask().ensureDone()) or runs the simulation itself (trySimulateClassInitializer). Commit 04b8225 added this call to theLoadFieldNodehandler, but not to this one.Suppose the field is read in a method of its own declaring class. Then there is no class initialization check before the read (it is in the caller), so nothing in the graph starts the simulation. It runs in the metadata task that is posted when the type becomes reachable. If the reading method is decoded before that task has published the result, the check stays in the graph, and the static analysis sees
phi(<uninitialized value>, <folded value>), which isnullfor an object field. It keeps that state for the rest of the analysis, even though the class is simulated and the field has the folded value at run time.Compilation later folds the check from the hosted view. So the compiled code of the reading method is the same, but the stamps derived from the analysis differ for everything the value flows into.
This affects methods that are reached without a caller whose graph starts the simulation first:
The class Javadoc of
SimulateClassInitializerSupportexplains that simulation results are used during inlining before analysis, rather than during parsing, to avoid using them in an order-dependent way. This handler is the one place in inlining before analysis that uses a simulation result without first making sure it exists.Symptom
We found this while making native-image builds bit-for-bit reproducible.
equals,hashCode,toString) passedphi(null, <Method constant>)toInvocationHandler.invoke.Methodparameter ofAnnotationInvocationHandler.invokeas non-null.With GraalVM 25.4.4.1.1, this happened in 7 of 56 builds, including a build with
--parallelism=1. With this change applied to the same toolchain, it happened in 0 of 32 builds.Fix
Call
processClassInitializer(field.getDeclaringClass())before the lookup, ashandleLoadFieldNodedoes.Deterministic by construction. When
processClassInitializerreturns, a simulation result for the declaring class has been published:DynamicHubInitializer.buildClassInitializationInfocallstrySimulateClassInitializer), andensureDonewaits for that task.trySimulateClassInitializerpublishes the results of the whole cluster before it returns.So whether the check is folded depends only on the simulation result, not on which analysis task ran first.
No new deadlock risk.
StaticFinalFieldFoldingPhasecreates theIsStaticFinalFieldInitializedNodein place of a staticLoadFieldNodeof the same field. Without the folding phase,handleLoadFieldNodewould make the sameprocessClassInitializercall at the same point of the same graph. The change restores exactly that call. The simulation's own decoder,SimulateClassInitializerGraphDecoder.handleIsStaticFinalFieldInitializedNode, likewise callstrySimulateClassInitializerbefore it uses the result.Test
New test
com.oracle.svm.test.clinit.SimulatedStaticFinalFieldFoldingTest, run with its own feature via@NativeImageBuildArgs:static final Object VALUE = "valueN").read()that readsVALUEand calls the empty methodnullValueObserved()if the value isnull.beforeAnalysis, the feature registers eachread()for reflection, so the method is reached only through its reflection accessor. InafterAnalysis, it fails the build if anynullValueObserved()is reachable.read()reflectively and checks the value.Results on darwin-aarch64:
23da1fd25b8without the fix: fails in 3 of 3 runs. In those runs, 7, 5 and 8 of the 8 classes sawnull.mx clinittestandmx checkstyle --primaryalso pass.The outcome on master depends on task scheduling. We also used a standalone probe with 16 such classes, counting affected classes per build:
--parallelism=1The test therefore depends on the default analysis parallelism to detect the bug. With the fix, no class was affected in any configuration, and every class was simulated in every build.
Notes
🤖 Generated with Claude Code