Skip to content

fix(native): prevent malloc deadlocks in signal handling - #2336

Merged
jpnurmi merged 7 commits into
v1from
jpnurmi/fix/native-malloc-locked
Nov 24, 2026
Merged

jpnurmi merged 7 commits into
v1from
jpnurmi/fix/native-malloc-locked

Conversation

@jpnurmi

@jpnurmi jpnurmi commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Fix potential deadlocks when crashing with the system allocator lock held, e.g. on heap corruption inside malloc or free, by removing allocations from the crash path:

  • Call sentry__enter_signal_handler() in the native signal handler, so the scope skips its locks and thread-locals there.
  • Access the scope's thread-locals through non-inlined accessors. Clang hoisted the access above sentry__block_for_signal_handler(), and the first access on a thread allocates on macOS (_tlv_get_addr).
  • Format JSON doubles with stbsp_vsnprintf on Apple platforms. libc vsnprintf allocates (__Balloc_D2A).

Add test_native_crash_malloc_locked, which crashes on a new thread with the allocator locked: force_lock on the default malloc zone on macOS, HeapLock on Windows, and a blocking malloc interposer on glibc. It skips on Windows, where the crashed thread can re-enter the heap lock.

See also:

A crash while the crashed thread holds the macOS malloc lock aborted
the process inside the native crash handler, before the daemon could
send the report:

    _os_unfair_lock_recursive_abort
    _xzm_xzone_malloc_freelist_outlined
    dyld::ThreadLocalVariables::instantiateVariable
    _tlv_get_addr
    sentry__scope_save_active_trace

The scope skips its thread-locals and locks on the thread that entered
the signal handler state. The inproc, breakpad and crashpad handlers
enter it, but the native handler did not.

Ref: getsentry/sentry-cocoa#8271
The first thread-local access on a thread allocates on macOS. Clang
hoisted the inlined `g_scope_access` access above the
`sentry__block_for_signal_handler()` check, so crash handling still
called `_tlv_get_addr` with the malloc lock held:

    _os_unfair_lock_recursive_abort
    _tlv_get_addr
    sentry__scope_save_active_trace

Read the thread-locals through non-inlined accessors that cannot be
hoisted above the check.

Ref: getsentry/sentry-cocoa#8271
libc `vsnprintf` allocates while formatting doubles, which aborted
crash handling with the malloc lock held:

    _os_unfair_lock_recursive_abort
    __Balloc_D2A
    __dtoa
    sentry__snprintf_c
    sentry__jsonwriter_write_double

Use the stack-only `stbsp_vsnprintf` that Linux already uses.

Ref: getsentry/sentry-cocoa#8271
`sentry_test_malloc_lock` builds the integration fixture with a
`crash-malloc-locked` scenario, which locks the allocator on a new
thread and crashes there. It locks the default malloc zone on macOS
and the process heap on Windows, and replaces `malloc` with a blocking
version on glibc. Other configurations exit with 77 to skip.

Ref: getsentry/sentry-cocoa#8271
@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.87%. Comparing base (f7d9010) to head (f70e331).
⚠️ Report is 1 commits behind head on v1.

Additional details and impacted files
@@            Coverage Diff             @@
##               v1    #2336      +/-   ##
==========================================
- Coverage   75.87%   75.87%   -0.01%     
==========================================
  Files         105      105              
  Lines       31254    31264      +10     
  Branches     5830     5830              
==========================================
+ Hits        23715    23721       +6     
- Misses       6089     6097       +8     
+ Partials     1450     1446       -4     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@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 57a41b4. Configure here.

Comment thread tests/fixtures/malloc_lock.c Resolved
`HeapLock` is recursive for its owner thread, so the crashed thread
could still allocate in the exception filter, and the test could not
catch allocations in the crash handler on Windows. Drop the Windows
lock from the fixture and skip the test there.

Ref: #2336 (comment)
@jpnurmi
jpnurmi merged commit 8e1af43 into v1 Nov 24, 2026
120 of 121 checks passed
@jpnurmi
jpnurmi deleted the jpnurmi/fix/native-malloc-locked branch November 24, 2026 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants