Skip to content

fix(DSM): Use async-signal-safe mutex in SIGSEGV handler#9893

Open
adambratschikaye wants to merge 2 commits intomasterfrom
abk/signal-mutex
Open

fix(DSM): Use async-signal-safe mutex in SIGSEGV handler#9893
adambratschikaye wants to merge 2 commits intomasterfrom
abk/signal-mutex

Conversation

@adambratschikaye
Copy link
Copy Markdown
Contributor

std::sync::Mutex uses pthread_mutex_lock internally, which is not async-signal-safe. The SIGSEGV handler acquires two mutexes during execution: the SigsegvMemoryTracker lock and the
subtract_instruction_counter lock. If the main thread holds either of these when a page fault fires, the handler will call pthread_mutex_lock re-entrantly on the same thread, which is undefined behaviour and can deadlock the process.

Introduce SignalMutex, a thin wrapper around an AtomicBool that uses compare_exchange (Acquire/Relaxed) to take the lock and a Release store to release it. All three operations are async-signal-safe. If the flag is already set when the handler tries to acquire it, SignalMutex panics immediately rather than blocking, turning a silent hang into a loud crash that is easier to diagnose.

std::sync::Mutex uses pthread_mutex_lock internally, which is not
async-signal-safe. The SIGSEGV handler acquires two mutexes during
execution: the SigsegvMemoryTracker lock and the
subtract_instruction_counter lock. If the main thread holds either of
these when a page fault fires, the handler will call pthread_mutex_lock
re-entrantly on the same thread, which is undefined behaviour and can
deadlock the process.

Introduce SignalMutex, a thin wrapper around an AtomicBool that uses
compare_exchange (Acquire/Relaxed) to take the lock and a Release store
to release it. All three operations are async-signal-safe. If the flag
is already set when the handler tries to acquire it, SignalMutex panics
immediately rather than blocking, turning a silent hang into a loud
crash that is easier to diagnose.
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.

2 participants