Skip to content

[svm] Run the class initializer simulation before folding IsStaticFinalFieldInitializedNode - #14922

Draft
MarkusBernhardt wants to merge 1 commit into
oracle:masterfrom
MarkusBernhardt:static-final-field-check-ensure-simulation
Draft

MarkusBernhardt wants to merge 1 commit into
oracle:masterfrom
MarkusBernhardt:static-final-field-check-ensure-simulation

Conversation

@MarkusBernhardt

Copy link
Copy Markdown

Problem

For a foldable static final field of a class that is initialized at run time, StaticFinalFieldFoldingPhase replaces the static field load with IsStaticFinalFieldInitialized(f) ? <folded value> : <uninitialized value>. During inlining before analysis, InlineBeforeAnalysisGraphDecoderImpl.handleIsStaticFinalFieldInitializedNode folds the check to true if 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 for LoadFieldNode and EnsureClassInitializedNode first call processClassInitializer. It waits for the type's metadata task (getInitializeMetaDataTask().ensureDone()) or runs the simulation itself (trySimulateClassInitializer). Commit 04b8225 added this call to the LoadFieldNode handler, 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 is null for 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:

  • reflection and other roots;
  • methods of dynamic proxy and annotation proxy classes;
  • virtual calls on types that are instantiated without an allocation in code.

The class Javadoc of SimulateClassInitializerSupport explains 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.

  • The annotation proxy class of an application annotation is initialized at run time, and its class initializer is simulated.
  • In some builds, some proxy methods (equals, hashCode, toString) passed phi(null, <Method constant>) to InvocationHandler.invoke.
  • The analysis therefore could not treat the Method parameter of AnnotationInvocationHandler.invoke as non-null.
  • The compiled method got an extra null check (997 instead of 979 bytes, which became 32 bytes with alignment), and that shifted the rest of the image.

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, as handleLoadFieldNode does.

  • Deterministic by construction. When processClassInitializer returns, a simulation result for the declaring class has been published:

    • For a reachable type, the metadata task always tries the simulation (DynamicHubInitializer.buildClassInitializationInfo calls trySimulateClassInitializer), and ensureDone waits for that task.
    • For a type that is not reachable yet, trySimulateClassInitializer publishes 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. StaticFinalFieldFoldingPhase creates the IsStaticFinalFieldInitializedNode in place of a static LoadFieldNode of the same field. Without the folding phase, handleLoadFieldNode would make the same processClassInitializer call at the same point of the same graph. The change restores exactly that call. The simulation's own decoder, SimulateClassInitializerGraphDecoder.handleIsStaticFinalFieldInitializedNode, likewise calls trySimulateClassInitializer before it uses the result.

Test

New test com.oracle.svm.test.clinit.SimulatedStaticFinalFieldFoldingTest, run with its own feature via @NativeImageBuildArgs:

  • Eight holder classes, initialized at run time.
    • Each has a class initializer that can be simulated (static final Object VALUE = "valueN").
    • Each has a method read() that reads VALUE and calls the empty method nullValueObserved() if the value is null.
  • In beforeAnalysis, the feature registers each read() for reflection, so the method is reached only through its reflection accessor. In afterAnalysis, it fails the build if any nullValueObserved() is reachable.
  • The holder classes themselves are registered for reflection only after the first analysis iteration. Registering a class for reflection makes it reachable right away, which would start the simulation earlier.
  • At run time, the test invokes each read() reflectively and checks the value.

Results on darwin-aarch64:

  • On master 23da1fd25b8 without the fix: fails in 3 of 3 runs. In those runs, 7, 5 and 8 of the 8 classes saw null.
  • With the fix: passes in 6 of 6 runs. mx clinittest and mx checkstyle --primary also pass.

The outcome on master depends on task scheduling. We also used a standalone probe with 16 such classes, counting affected classes per build:

Registration Affected classes per build (of 16)
Reflection 13–15
Direct root registration 2–6
Direct root registration, heavier class initializer 15
--parallelism=1 0

The 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

…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]>
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Nov 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant