Skip to content

Avoid Encoding.UTF8 in NativeAOT AppContext.InitializeDataStore - #137362

Open
Sergio0694 wants to merge 3 commits into
dotnet:mainfrom
Sergio0694:user/sergiopedri/fix-nativeaot-utf8-cctor-cycle
Open

Sergio0694 wants to merge 3 commits into
dotnet:mainfrom
Sergio0694:user/sergiopedri/fix-nativeaot-utf8-cctor-cycle

Conversation

@Sergio0694

@Sergio0694 Sergio0694 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

On NativeAOT, AppContext.InitializeDataStore uses Encoding.UTF8 to decode runtime knobs. AppContext can be class-constructed from Lock.TryInitializeStatics (via AppContextConfigHelper) when Cctor.Release contends on the global cctor lock. If that happens while UTF8Encoding's cctor is running on the same thread, the recursive cctor check returns early and Encoding.UTF8 is null, causing a null dereference and fail-fast:

UTF8Encoding..cctor
  (nested cctor) -> ClassConstructorRunner.Cctor.Release
    Lock.Enter -> TryInitializeStatics -> DetermineMaxSpinCount
      AppContext.GetData -> AppContext..cctor -> InitializeDataStore
        Encoding.UTF8 -> null

This was observed in a production crash in the Microsoft Store.

The fix decodes the knob strings with Utf8.ToUtf16, which has no static dependencies.

AppContext can be class-constructed from Lock.TryInitializeStatics (via
AppContextConfigHelper) when the class constructor global lock is
contended in Cctor.Release. If that happens while UTF8Encoding's class
constructor is running on the same thread, the recursive cctor check
returns early and Encoding.UTF8 is observed as null, causing a null
dereference and a fail-fast. Decode the knob strings with Utf8.ToUtf16,
which has no static dependencies.

Co-authored-by: Copilot App <[email protected]>
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 11, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@Sergio0694
Sergio0694 marked this pull request as ready for review November 11, 2026 18:04
@Sergio0694 Sergio0694 added the partner-impact This issue impacts a partner who needs to be kept updated label Nov 11, 2026
@Sergio0694

Copy link
Copy Markdown
Contributor Author

@MichalStrehovsky FYI I found this while triaging crashes in bucket ef8b6d98-dd66-77e5-f200-3b57e14bed54. Is this small enough that we could consider backporting? The crash in question was on our current retail build (which is .NET 10).

@MichalStrehovsky

Copy link
Copy Markdown
Member

We have another report of a similar recursion in #136031. I'm starting to think the problem is that Lock touches AppContext, not that AppContext is not low-level enough. I don't know what is the value in Lock configurability here, maybe we should ifdef it all out under native AOT.

@jkotas

jkotas commented Nov 11, 2026

Copy link
Copy Markdown
Member

I'm starting to think the problem is that Lock touches AppContext,

+1. Lock - at least the Lock that is used by Ctor runner - has to be low-level. It should not call back into AppContext or EventSource.

@Sergio0694

Copy link
Copy Markdown
Contributor Author

Is this anything I can help with? Happy to throw together a PR if you can confirm what the desired behavior should be, I assume just make the AOT build of Lock always use some fixed value instead? If so, what should that one be, the same that is the default one set in AppContext, or something else? 🙂

@MichalStrehovsky

Copy link
Copy Markdown
Member

I'm looking at the Lock implementation. It's doing a bunch of things to try and avoid reentrancy. I think we have two options:

  1. Low level usage of the Lock always uses the default tunings
  2. Low level usage of the Lock doesn't try to initialize the tunings, but if they are available (or become available) it uses them. Feels better but also more scary.

It should never emit to event source when the use is low level in either case.

I tried to throw copilot at it but twice it came up with something I didn't like. It feels like it was duplicating things with the pre-existing staged initialization (StaticsInitializationStage) and I didn't have a good feel of how to reconcile those two and copilot didn't either.

@jkotas

jkotas commented Nov 12, 2026

Copy link
Copy Markdown
Member

The Lock may also end up calling Wait that is another potential source of re-entrancy. What's out confidence that nothing under Wait can call static constructors (in particular on non-Windows where the Wait is implemented using WaitSubsystem)?

cc @jkoritzinsky

@jkoritzinsky

jkoritzinsky commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

WaitSubsystem specifically has [EagerStaticClassConstruction] on the only type with a class constructor to ensure we don't deadlock here. The other types don't but that's only because they don't have class-constructors at the moment.

If we were to use LowLevelLock for the class-constructor logic, we'd lose our tuning (which as @MichalStrehovsky pointed out is only conditionally available anyway, so it's already semi-unreliable) but we'd have a consistently reliable locking mechanism.

We'd also lose the ability to interrupt a thread via Thread.Interrupt when it's waiting on the cctor locks, but I'm pretty sure that we already don't support that on CoreCLR.

@jkotas

jkotas commented Nov 17, 2026

Copy link
Copy Markdown
Member

we'd lose our tuning

That should be perfectly acceptable for the cctor locks. They are not perf sensitive.

We'd also lose the ability to interrupt a thread via Thread.Interrupt when it's waiting on the cctor locks, but I'm pretty sure that we already don't support that on CoreCLR.

Right, ability to interrupt a thread via Thread.Interrupt when it's waiting on the cctor lock looks like a bug, not a feature.

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

area-NativeAOT-coreclr community-contribution Indicates that the PR has been added by a community member partner-impact This issue impacts a partner who needs to be kept updated

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants