Skip to content

Native AOT: a static profile (MibcFile) makes ILC miscompile a foreach over IEnumerable<int> passed an int[] #137063

Closed

Description

@mwadams

Description

With a static profile (MibcFile), the native AOT compiler compiles a method of the Corvus.Regex.Ecma262 package so that it gives wrong results. The same program is right on the JIT, and right under native AOT without the profile.

The method is Dfa.DfaBuilder.Closure (source at 1.0.13). It pushes the elements of an IEnumerable<int> from onto a Stack<int> with foreach, then walks a graph from them into a List<int>. Callers pass an int[] (a collection expression [start], or an element of a List<int[]>) or a List<int>. Under native AOT with the profile, the result comes out empty, so regular expressions that this automaton answers match nothing.

Reproduction Steps

Everything is in this gist: https://gist.github.com/mwadams/3015ee973f837c6718425f41b40e4c86

docker run --rm -v "$PWD:/g" -w /g mcr.microsoft.com/dotnet/sdk:10.0-noble-aot ./run.sh

run.sh publishes a 30-line program with PublishAot and OptimizationPreference=Speed twice, without and with the profile, and runs each.

Expected behavior

== UseProfile=true
5/5 right

Actual behavior

== UseProfile=false
5/5 right
== UseProfile=true
WRONG /^[0-9]+(ns|ms|us|µs|s|m|h)$/ on "30s": False (UTF-16), False (UTF-8), expected True
WRONG /^[0-9]+(ns|ms|us|µs|s|m|h)$/ on "1ms": False (UTF-16), False (UTF-8), expected True
WRONG /^(0|[1-9]\d*)\.(0|[1-9]\d*)$/ on "1.2": False (UTF-16), False (UTF-8), expected True
2/5 right

Regression?

Not known. It is wrong on every version tried (below).

Known Workarounds

  • Publish without the profile.
  • Mark Closure [MethodImpl(MethodImplOptions.NoOptimization)].
  • Change from to ReadOnlySpan<int> and List<int> overloads, so there is no interface call. This is what Corvus.Regex.Ecma262 1.0.15 does (corvus-dotnet/Corvus.Regex.Ecma262#58).

Configuration

  • mcr.microsoft.com/dotnet/sdk:10.0-noble-aot: SDK 10.0.403, runtime 10.0.14.
  • mcr.microsoft.com/dotnet/sdk:10.0.401-noble-aot: SDK 10.0.401, runtime 10.0.12.
  • SDK 11.0.100-rc.1.26425.128, targeting net11.0.
  • linux-x64 (WSL2 and Docker), AMD64. Also seen on the jsonschema-benchmark's Azure runners (AMD EPYC 9V45).

Other information

How it was narrowed down:

  • The profile is the part of a JIT-recorded profile (dotnet-pgo create-mibc over an instrumented run with DOTNET_TieredPGO=1) that covers Corvus.Regex.Ecma262 and System.Private.CoreLib, taken with dotnet-pgo merge --include-reference.

  • Building the library from source and marking groups of the profiled methods NoOptimization isolated Closure. Marking that method alone makes every answer right. The other DFA methods in the profile do not change the result.

  • The profile's data for Closure includes type histograms at the foreach:

    • IEnumerable<int>.GetEnumerator at IL offset 26: List<int> and int[].
    • MoveNext/Current at 35, 54 and 70: List<int>.Enumerator and SZGenericArrayEnumerator<int>.

    So the guarded devirtualisation of the array's interface enumeration looks like the place where it goes wrong.

  • A standalone copy of the method's shape (the same foreach over IEnumerable<int> into a Stack<int>, called with int[] and List<int>), with its own JIT-recorded profile, compiles correctly, so I could not cut it down further than the package.

It was found because the jsonschema-benchmark's native AOT harness for Corvus.Text.Json started refusing valid instances. Corvus.Text.Json ships a profile for native AOT, which brought this in.

Activity

  1. added
    untriaged
    on Nov 6, 2026
  2. dotnet-policy-service commented on Nov 6, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
    See info in area-owners.md if you want to be subscribed.

  3. added
    area-CodeGen-coreclr
    and removed on Nov 6, 2026
  4. dotnet-policy-service commented on Nov 6, 2026

    @dotnet-policy-service
    Contributor

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

  5. self-assigned this
    on Nov 6, 2026
  6. removed
    untriaged
    on Nov 6, 2026
  7. AndyAyersMS commented on Nov 6, 2026

    @AndyAyersMS
    Member

    In polymorphic profiles (especially with our randomized sampling) there is no guarantee that the dominating type at each GDV check of an enumerator local will match the dominating enumerator type. CEA needs to validate the type for each check, which it is not doing currently. Fix looks to be straightforward. Bug is still there in main.

  8. AndyAyersMS commented on Nov 6, 2026

    @AndyAyersMS
    Member

    This bug is a consequence of multi-guess GDV, which is enabled by default in NAOT. You can work around this by dropping back to single-guess GDV, adding

    <ItemGroup>
      <IlcArg Include="--codegenopt:JitGuardedDevirtualizationMaxTypeChecks=1" />
    </ItemGroup>
    

    to the project file.

    Once the fix is merged to main, I'll propose a backport to 10.0 and 11.0.

  9. mwadams commented on Nov 7, 2026

    @mwadams
    ContributorAuthor

    (The workaround works for us, BTW - thanks.)

  10. added a commit that references this issue on Nov 13, 2026
    7a535aa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrbug

Type

No type

Projects

  • Status
    No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions