Skip to content

fix: prevent false native crash reports from handled exceptions in Native AOT apps on Linux and macOS - #5989

Merged
ric-oliv merged 2 commits into
mainfrom
jpnurmi/fix/native-preload
Nov 25, 2026
Merged

ric-oliv merged 2 commits into
mainfrom
jpnurmi/fix/native-preload

Conversation

@jpnurmi

@jpnurmi jpnurmi commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

This is the desktop counterpart of #5127 for Android and #5126 for iOS.

In Native AOT apps on Linux and macOS, sentry-native installed its signal handlers on top of the .NET runtime's, so a SIGSEGV raised by a managed null dereference reached sentry-native first. The native SDK recorded it as a native crash before chaining to the runtime, which then threw the NullReferenceException as usual. The app kept running, but the next launch reported a false crash.

Link a constructor object that preloads sentry-native before main, so it installs its handlers before the runtime does. The runtime then handles managed faults itself and passes native crashes on to sentry-native.

Refs #4867

@github-actions github-actions Bot added the risk: high PR risk score: high label Nov 23, 2026
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

PR risk: high

For commit d992e2d.

Main concerns: dependencies customers inherit, reach, host performance.

Rules that block low also fired:

  • R6: build or release configuration changed: .gitattributes, .github/workflows/build.yml, .github/workflows/verify-api.yml, scripts/build-sentry-native.ps1, src/Sentry/Platforms/Native/CMakeLists.txt and 2 more

This label is a hint for reviewers, not an approval.

@jpnurmi jpnurmi changed the title fix: don't report caught NullReferenceException as a native crash fix(native): don't report caught NullReferenceException as a native crash Nov 23, 2026
@jpnurmi jpnurmi changed the title fix(native): don't report caught NullReferenceException as a native crash fix(native): preload signal handlers before .NET runtime Nov 23, 2026
@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.15%. Comparing base (e7078a0) to head (d992e2d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5989      +/-   ##
==========================================
+ Coverage   78.12%   78.15%   +0.02%     
==========================================
  Files         537      537              
  Lines       20077    20077              
  Branches     4041     4041              
==========================================
+ Hits        15686    15692       +6     
+ Misses       3545     3542       -3     
+ Partials      846      843       -3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jpnurmi
jpnurmi force-pushed the jpnurmi/fix/native-preload branch from 4586a19 to 54ae6e7 Compare November 23, 2026 15:12
@github-actions github-actions Bot added risk: high PR risk score: high and removed risk: high PR risk score: high labels Nov 23, 2026

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@github-actions github-actions Bot added risk: high PR risk score: high and removed risk: high PR risk score: high labels Nov 23, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 3533d85. Configure here.

Comment thread .github/workflows/build.yml Resolved
@jpnurmi
jpnurmi force-pushed the jpnurmi/fix/native-preload branch from 3533d85 to fa2cd39 Compare November 23, 2026 16:21
@github-actions github-actions Bot added risk: high PR risk score: high and removed risk: high PR risk score: high labels Nov 23, 2026
@jpnurmi
jpnurmi force-pushed the jpnurmi/fix/native-preload branch from fa2cd39 to 3fdc82c Compare November 24, 2026 07:54
@github-actions github-actions Bot added risk: high PR risk score: high and removed risk: high PR risk score: high labels Nov 24, 2026
@jpnurmi
jpnurmi changed the base branch from main to jpnurmi/ref/native-http November 24, 2026 07:56
@github-actions github-actions Bot added risk: high PR risk score: high and removed risk: high PR risk score: high labels Nov 24, 2026
@jpnurmi
jpnurmi added this pull request to stack #6000 November 24, 2026 07:58
@github-actions

Copy link
Copy Markdown
Contributor

Mutation testing breaks the lines a PR changes on purpose, one mutation at a time, and checks that a test fails each time. A changed line no test runs fails the check, and a mutant that survives the tests only warns. Full report

Sentry: tests caught 3 of 5 mutants on the changed lines

Location Mutation Replaced with Status
src/Sentry/Internal/Http/GzipRequestBodyHandler.cs:93 Statement mutation ; Survived
src/Sentry/Platforms/Native/CFunctions.cs:139 Statement mutation ; NoCoverage

@jamescrosswell

Copy link
Copy Markdown
Collaborator

How sentry-preload.o ends up running before the .NET runtime

For any other reviewers who had to scratch their head to work out what was going on here: this is how sentry-preload.c changes the order of the signal handlers at startup.

Build and link

  1. build-sentry-native.ps1 builds the new wrapper CMakeLists.txt. That compiles sentry-preload.c into a standalone object and installs it as sentry-native/<rid>/sentry-preload.o, which the package ships next to libsentry-native.a.
  2. In the app, buildTransitive/Sentry.Native.targets adds that object as a NativeLibrary for linux-* and osx-* RIDs. During a NativeAOT publish, ILC passes NativeLibrary items to the linker.
  3. The linker always links a .o on its command line. It drops an archive member that nothing references, which is why this function isn't inside libsentry-native.a. ILC links its own main (libbootstrapper.o) the same way.
  4. __attribute__((constructor)) puts the function in .init_array (ELF) or __mod_init_func (Mach-O). The loader runs everything in that list before main.

At startup

sequenceDiagram
    participant L as Loader (ld.so / dyld)
    participant P as sentry-preload.o
    participant M as main (libbootstrapper.o)
    participant R as NativeAOT runtime
    participant A as App code
    participant S as sentry-native

    L->>P: run static initializers, before main
    P->>S: sentry__backend_preload()
    S->>S: install signal handlers, g_preloaded = 1
    L->>M: main()
    M->>R: RhInitialize()
    R->>R: install SIGSEGV/SIGFPE handlers on top,<br/>keeping sentry-native's as the previous ones
    M->>A: __managed__Main() → Program.Main
    A->>S: SentrySdk.Init → sentry_init()
    S->>S: already preloaded, so the handlers stay put

    Note over R,S: Later, on a fault
    R->>R: SIGSEGV in managed code → NullReferenceException
    R->>S: SIGSEGV in native code → chain to the previous handler
    S->>S: capture the native crash
Loading

Why constructor order doesn't matter

Nothing guarantees the order in which constructors run, but this doesn't depend on it, because the runtime installs its handlers from main, not from a constructor. In the NativeAOT runtime packs for osx-arm64 11.0.0 and linux-x64 10.0.5:

  • Only UnixSignals.cpp.o (AddSignalHandler) calls sigaction, and the runtime reaches it through RhInitialize → PalInit → InitializeHardwareExceptionHandling.
  • The runtime objects that do have static initializers are all GC code, and none of them reference that path.
  • libbootstrapper.o has no initializer.

libbootstrapperdll.o, which NativeAOT uses for shared libraries, does start the runtime from a constructor. Sentry only enables native support for Exe and WinExe projects, so that case doesn't arise.

@jamescrosswell jamescrosswell left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Tested locally on macOS arm64 (net10.0 and net11.0): the new test fails on the base commit, which reports a false SIGSEGV crash on the next launch, and passes with this change. The existing native-crash test passes in both cases.

@jpnurmi
jpnurmi force-pushed the jpnurmi/fix/native-preload branch from 3fdc82c to 5bf8b2c Compare November 25, 2026 07:41
@github-actions github-actions Bot removed the risk: high PR risk score: high label Nov 25, 2026
@github-actions github-actions Bot added the risk: high PR risk score: high label Nov 25, 2026
Base automatically changed from jpnurmi/ref/native-http to main November 25, 2026 10:17
jpnurmi and others added 2 commits November 25, 2026 11:17
This is the desktop counterpart of #5127 for Android and #5126 for iOS.

In Native AOT apps on Linux and macOS, sentry-native installed its
signal handlers on top of the .NET runtime's, so a `SIGSEGV` raised by a
managed null dereference reached sentry-native first. It recorded a
native crash before chaining to the runtime, which then threw the
`NullReferenceException` as usual. The app kept running, but the next
launch reported a false crash.

Link a constructor object that preloads sentry-native before `main`,
so it installs its handlers before the runtime does. The runtime then
handles managed faults itself and passes native crashes on to
sentry-native.

Ref: #4867

Co-authored-by: Claude Opus 5.5 <[email protected]>
Pin the native sources to LF, so Windows hashes them the same as macOS
and Linux.
@ric-oliv
ric-oliv force-pushed the jpnurmi/fix/native-preload branch from 5bf8b2c to d992e2d Compare November 25, 2026 10:17
@github-actions github-actions Bot added risk: high PR risk score: high and removed risk: high PR risk score: high labels Nov 25, 2026
@ric-oliv

Copy link
Copy Markdown
Member

Optional: the title becomes the changelog line, and it describes the mechanism rather than what users see. The Android and iOS counterparts were titled by symptom ("fix: prevent redundant native exceptions on Android/CoreCLR").
Maybe something like fix: prevent false native crash reports from handled exceptions in Native AOT apps on Linux and macOS?

@jpnurmi jpnurmi changed the title fix(native): preload signal handlers before .NET runtime fix: prevent false native crash reports from handled exceptions in Native AOT apps on Linux and macOS Nov 25, 2026

@ric-oliv ric-oliv 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.

Thanks, @jpnurmi !

@ric-oliv
ric-oliv merged commit c27c560 into main Nov 25, 2026
61 checks passed
@ric-oliv
ric-oliv deleted the jpnurmi/fix/native-preload branch November 25, 2026 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants