Repository navigation
Avoid Encoding.UTF8 in NativeAOT AppContext.InitializeDataStore - #137362
Sergio0694 wants to merge 3 commits into
Conversation
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]>
|
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. |
|
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib |
Co-authored-by: Copilot App <[email protected]>
Co-authored-by: Copilot App <[email protected]>
|
@MichalStrehovsky FYI I found this while triaging crashes in bucket |
|
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. |
+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. |
|
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 |
|
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:
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 ( |
|
The Lock may also end up calling |
|
WaitSubsystem specifically has If we were to use We'd also lose the ability to interrupt a thread via |
That should be perfectly acceptable for the cctor locks. They are not perf sensitive.
Right, ability to interrupt a thread via Thread.Interrupt when it's waiting on the cctor lock looks like a bug, not a feature. |
On NativeAOT,
AppContext.InitializeDataStoreusesEncoding.UTF8to decode runtime knobs.AppContextcan be class-constructed fromLock.TryInitializeStatics(viaAppContextConfigHelper) whenCctor.Releasecontends on the global cctor lock. If that happens whileUTF8Encoding's cctor is running on the same thread, the recursive cctor check returns early andEncoding.UTF8isnull, causing a null dereference and fail-fast: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.