Skip to content

JIT: static field read after a beforefieldinit static constructor can observe the pre-constructor value #136042

Closed

Description

@lewing

Description

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).

Repro

using System;
using System.Runtime.CompilerServices;

class Program
{
    static int Counter;

    class G
    {
        public static int Value = ++Counter;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static (int, int, int) Test()
    {
        int before = Counter;
        int value = G.Value;
        return (before, value, Counter);
    }

    static void Main() => Console.WriteLine(Test());
}
Configuration Output
.NET 11.0 rc1, default tiering (0, 1, 1)
.NET 11.0 rc1, DOTNET_TieredCompilation=0 (0, 1, 0)
.NET 11.0 rc1, DOTNET_TC_QuickJit=0 (0, 1, 0)
crossgen2 (osx-arm64) R2R, DOTNET_TieredCompilation=0 (0, 1, 0)

(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".

Note

This issue was drafted with GitHub Copilot.

Activity

  1. added
    area-CodeGen-coreclr
    on Oct 19, 2026
  2. added
    untriaged
    on Oct 19, 2026
  3. dotnet-policy-service commented on Oct 19, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. removed
    untriaged
    on Oct 26, 2026
  5. added a commit that references this issue on Oct 26, 2026
    7c0a954
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclr

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions