Repository navigation
Description
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.shrun.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
fromtoReadOnlySpan<int>andList<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-mibcover an instrumented run withDOTNET_TieredPGO=1) that covers Corvus.Regex.Ecma262 and System.Private.CoreLib, taken withdotnet-pgo merge --include-reference. -
Building the library from source and marking groups of the profiled methods
NoOptimizationisolatedClosure. 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
Closureincludes type histograms at theforeach:IEnumerable<int>.GetEnumeratorat IL offset 26:List<int>andint[].MoveNext/Currentat 35, 54 and 70:List<int>.EnumeratorandSZGenericArrayEnumerator<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
foreachoverIEnumerable<int>into aStack<int>, called withint[]andList<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.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon Nov 6, 2026
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Nov 6, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
AndyAyersMS commentedon Nov 6, 2026
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.
AndyAyersMS commentedon Nov 6, 2026
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
to the project file.
Once the fix is merged to main, I'll propose a backport to 10.0 and 11.0.
mwadams commentedon Nov 7, 2026
(The workaround works for us, BTW - thanks.)
JIT: check guard types during CEA cloning (dotnet#137092)