3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-22 08:35:31 +00:00
z3/src
Mikulas Patocka bcd615f3c5 Make Ctrl-C handling thread-safe (#7603)
The Ctrl-C handling is not thread safe, there's a global variable g_obj
that is being accessed without any locking. The signal handlers are
per-process, not per-thread, so that different threads step over each
other's handlers. It is unpredictable in which thread the signal handler
runs, so the handler may race with the scoped_ctrl_c destructor.

Fix this by introducing the functions signal_lock and signal_unlock.
signal_lock blocks the SIGINT signal and then takes a mutex (so that the
signal handler can't be called while the mutex is held). signal_unlock
drops the mutex and restores the signal mask.

We protect all the global variables with signal_lock and signal_unlock.

Note that on Windows, the SIGINT handler is being run in a separate
thread (and there is no way how to block it), so we can use a simple
mutex to synchronize the signal handler with the other threads.

In class cancel_eh, the operator () may be called concurrently by the
timer code and the Ctrl-C code, but the operator () accesses class'
members without any locking. Fix this race condition by using the
functions signal_lock() and signal_unlock().

There is this possible call trace:
        SIGINT signal
        on_sigint
        a->m_cancel_eh()
        cancel_eh::operator()
        m_obj.inc_cancel
        reslimit::inc_cancel
        lock_guard lock(*g_rlimit_mux);

Here we take a mutex from a signal - this is subject to deadlock (if the
signal interrupted another piece of code where the mutex is already
held).

To fix this race, we remove g_rlimit_mux and replace it with
signal_lock() and signal_unlock(). signal_lock and signal_unlock block
the signal before grabbing the mutex, so the signal can't interrupt a
piece of code where the mutex is held and the deadlock won't happen.

Signed-off-by: Mikulas Patocka <mikulas@twibright.com>
2025-04-09 18:35:28 +02:00
..
ackermannization delete more default constructors 2024-09-23 12:59:04 +01:00
api list[ExprRef] doesn't build for python 2025-04-05 14:45:52 -07:00
ast turn on ite simplification by default 2025-03-26 11:30:08 -07:00
cmd_context fix #7560 2025-02-19 09:39:17 -08:00
math fix #7607 2025-04-05 11:58:47 -07:00
model revert flat default 2025-01-27 16:56:12 -08:00
muz fix #4117 2025-03-12 20:14:36 -07:00
nlsat replace Exists by ForAll in the mathematica lemmas 2025-03-27 12:32:37 -07:00
opt fix #7578 2025-03-09 17:01:42 -07:00
params turn on ite simplification by default 2025-03-26 11:30:08 -07:00
parsers inherit more exceptions from std::exception 2024-11-04 13:52:14 -08:00
qe fix #7572 and fix #7574 2025-03-07 10:46:29 -08:00
sat fix #7572 and fix #7574 2025-03-07 10:46:29 -08:00
shell fix #7461 2024-12-09 16:57:17 -08:00
smt rename function 2025-04-04 18:40:15 -07:00
solver fix #7590 logic alphabet soup 2025-03-19 08:57:32 -10:00
tactic household chores - move to iterators 2025-03-24 12:36:13 -07:00
test use iterators on goal and other refactoring 2025-03-16 20:04:04 -07:00
util Make Ctrl-C handling thread-safe (#7603) 2025-04-09 18:35:28 +02:00
CMakeLists.txt Sls (#7439) 2024-11-02 12:32:48 -07:00