Skip to content

Trim NativeAOT exception dispatch until a handler is added - #137331

Merged
MichalStrehovsky merged 7 commits into
dotnet:mainfrom
MichalStrehovsky:size/appcontext-exception-callbacks
Nov 12, 2026
Merged

MichalStrehovsky merged 7 commits into
dotnet:mainfrom
MichalStrehovsky:size/appcontext-exception-callbacks

Conversation

@MichalStrehovsky

Copy link
Copy Markdown
Member

Register managed exception callbacks from the existing AppDomain event add accessors. Keep the rooted runtime exports independent of AppContext's static initializer by storing their callback addresses in a cctor-free holder. Preserve unconditional crash-info capture.

The event add accessor's Interlocked.CompareExchange provides the publication fence; callback dispatch retains acquire reads. The original datastore field and its readers remain unchanged.

Standalone local HelloWorldSize probe, WASI NativeAOT Release, invariant globalization and size optimization: 722,933 -> 719,421 bytes, saving 3,512 bytes (0.49%) against the common base.

Validated with clr.nativeaotlibs+libs.sfx and the HelloWorldSize build. HelloWorldSize and the existing Exceptions smoke test both exited 100.

Register managed exception callbacks from the existing AppDomain event add
accessors. Keep the rooted runtime exports independent of AppContext's
static initializer by storing their callback addresses in a cctor-free
holder. Preserve unconditional crash-info capture.

The event add accessor's Interlocked.CompareExchange provides the
publication fence; callback dispatch retains acquire reads. The original
datastore field and its readers remain unchanged.

Standalone local HelloWorldSize probe, WASI NativeAOT Release, invariant
globalization and size optimization: 722,933 -> 719,421 bytes, saving
3,512 bytes (0.49%) against the common base.

Validated with clr.nativeaotlibs+libs.sfx and the HelloWorldSize build.
HelloWorldSize and the existing Exceptions smoke test both exited 100.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 2e62c26c-6da8-442d-98ac-dcb988c2846a
@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.

@MichalStrehovsky

Copy link
Copy Markdown
Member Author

All three #137333, #137332, #137331 combined save 50 kB, which is a lot more then each individually. Maybe I should have made a single PR but I assumed we might need to iterate on some.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The dispatcher change skips crash-info capture on NativeAOT’s direct unhandled-exception path, causing a handler’s Environment.FailFast(message) call to omit the supplied exception from the crash record.

1 open issue
What changed in this PR

This PR makes NativeAOT’s managed exception-event dispatch trimmable when no handlers are registered, reducing application size while keeping runtime exports available.

Changes:

  • Register dispatch callbacks through the existing AppDomain event add accessors.
  • Store callback pointers separately to avoid triggering AppContext initialization.
  • Move unconditional crash-info capture into the NativeAOT unhandled-exception export.
File Description
src/​libraries/​System.Private.CoreLib/​src/​System/​AppDomain.cs Registers NativeAOT callbacks when exception handlers are added.
src/​libraries/​System.Private.CoreLib/​src/​System/​AppContext.cs Removes NativeAOT export and crash-capture responsibilities from shared dispatch.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​AppContext.NativeAot.cs Adds isolated callback storage and conditional dispatch through runtime exports.

Review effort: Balanced

On NativeAOT, keep OnUnhandledException as the capturing entry point used
by both the runtime export and
ExceptionHandling.RaiseAppDomainUnhandledExceptionEvent. Rename only the
subscriber dispatcher to OnUnhandledExceptionInternal and register that
method as the callback.

This preserves the original exception if a handler calls
Environment.FailFast(message), without duplicating serialization or
evaluating Exception.Message again on the normal runtime path. The public
API caller and the other runtimes remain unchanged.

Use !MONO for the shared first-chance registration condition.

Validated with actual Wasmtime crash dumps: the API path lost the exception
before the fix; both API raise and runtime throw retain its type and message
afterward. The four existing RaiseEvent tests and Exceptions smoke test pass.
WASI NativeAOT Release builds are clean and HelloWorldSize remains 719,421
bytes, preserving the 3,512-byte standalone saving.

Addresses the review finding in dotnet#137331.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 2e62c26c-6da8-442d-98ac-dcb988c2846a
Rename the subscriber-only dispatcher and its registered callback target
to OnUnhandledExceptionCore, matching OnFirstChanceExceptionCore.

Keep the capturing OnUnhandledException entry point and the CoreCLR/Mono
method names unchanged.

Validated with a WASI Release NativeAOT CoreLib build, all four existing
RaiseEvent cases, and the Exceptions smoke test. Both test applications
exited 100.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 2e62c26c-6da8-442d-98ac-dcb988c2846a

@agocke agocke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

Replace the platform conditionals in AppDomain's event adders with
optional partial hooks. Provide the first-chance implementation for
CoreCLR and both implementations for NativeAOT; unsupported hooks are
erased without adding Mono no-op implementations.

Store NativeAOT callbacks as volatile managed function pointers. This
makes release/acquire semantics explicit, removes integer casts and
Volatile.Read calls, and avoids relying on the event adder's fence to
describe callback publication.

Validated WASI Release CoreLib builds for CoreCLR and NativeAOT, the four
existing RaiseEvent cases, the Exceptions smoke test, and HelloWorldSize.
All native test applications exited 100; HelloWorldSize remains 719,421
bytes and the AppContext initializer/dispatcher remain trimmed.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 2e62c26c-6da8-442d-98ac-dcb988c2846a
MichalStrehovsky and others added 2 commits November 12, 2026 12:09
Make AppDomain exception events simple forwarders and keep registration in AppContext. Use private backing events to preserve thread-safe subscription, update Mono VM field lookups, and remove redundant AppDomain partial files.

Validated CoreCLR and NativeAOT CoreLib builds, the Mono runtime and libraries build, 73 passing Mono AppDomain tests (one skipped), and a passing process-level exception-event probe. CoreCLR and NativeAOT execution tests were not run.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 2df08005-5848-4ba4-b1b5-e707ab480d0f
Select AppDomain.CurrentDomain in the shared first-chance exception dispatcher so NativeAOT and CoreCLR deliver the same sender. Remove the now-redundant sender argument from their entry points.

Test validation deferred to CI as requested. Test changes are not included.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 2df08005-5848-4ba4-b1b5-e707ab480d0f
Replace the first-chance exception adapter with a static local Callback and make the NativeAOT unhandled exception dispatcher private.

Validated the Checked NativeAOT CoreLib build and the Exceptions smoke test (exit 100). Existing local test edits are not included.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 2df08005-5848-4ba4-b1b5-e707ab480d0f
@MichalStrehovsky

Copy link
Copy Markdown
Member Author

/ba-g helix crash in an unrelated test

@MichalStrehovsky
MichalStrehovsky enabled auto-merge (squash) November 12, 2026 07:46
@MichalStrehovsky
MichalStrehovsky merged commit 3ec196f into dotnet:main Nov 12, 2026
146 of 148 checks passed
@MichalStrehovsky
MichalStrehovsky deleted the size/appcontext-exception-callbacks branch November 12, 2026 07:46
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants