You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JIT: static field read after a beforefieldinit static constructor can observe the pre-constructor value #136042
For a beforefieldinit class whose static constructor writes another static, a read of that other static after the first access to the class can return its value from before the constructor ran. This happens in fully optimized code (TieredCompilation=0, TC_QuickJit=0, and ReadyToRun).
(0, 1, 0) doesn't match any legal initialization order. Since value is 1, the constructor ran and incremented Counter, so the final read of Counter must observe 1.
Analysis
Static base helper calls for beforefieldinit classes get GTF_CALL_HOISTABLE. In R2R this is importer.cpp (CORINFO_FIELD_STATIC_SHARED_STATIC_HELPER); in JIT it is fgGetStaticsCCtorHelper in flowgraph.cpp. Besides allowing loop hoisting, value numbering (valuenum.cpp, fgValueNumberHelperCall) and the loop memory summary (optimizer.cpp) use the flag to treat the call as not modifying the heap. That holds for hoisting, because beforefieldinit lets the constructor run earlier, but not for memory effects: the constructor does modify the heap the first time it runs. In the R2R disassembly the third Counter load reuses the first load's register after CORINFO_HELP_READYTORUN_NONGCSTATIC_BASE (#136037 has the details).
The shared-generic path (CORINFO_FIELD_STATIC_GENERICS_STATIC_HELPER) doesn't set the flag, so the generic version of this repro is correct on main. #136037 set it there and exposed the same bug; that PR has been moved back to draft.
A fix probably needs to separate "may be hoisted or run earlier" from "has no heap side effects".
Activity
dotnet-policy-service commentedon Oct 19, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
JIT: treat static base helpers that may run a cctor as heap writes (d…