From 853b655983869e7b2eb35969dccb81f16d5af2fc Mon Sep 17 00:00:00 2001 From: phcorp <725023+phcorp@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:44:15 +0100 Subject: [PATCH 1/5] Fix GH-25541: Signal on a thread that never ran PHP crashes or kills the process Under ZTS, a process signal can be delivered to a thread that has no TSRM storage, e.g. a Go runtime thread in FrankenPHP or any thread an embedder creates. pcntl_signal_handler() then read PCNTL_G() through a NULL pointer (--disable-zend-signals), and zend_signal_handler_defer() fell back to the original disposition, so SIG_DFL killed the process although the script registered a handler. On such a thread, for a signal PHP handles, add the signal to the mask saved in the interrupted context and send it to the process again. The kernel delivers it to a thread that does not block it, which converges on a PHP thread. Threads unblock re-routed signals when they start a request. Faults and abort() keep their default action on the thread they hit. Re-routing is enabled on Linux, macOS and FreeBSD, where it was verified. --- NEWS | 2 + Zend/zend.c | 3 + Zend/zend_signal.c | 167 +++++++++++++++++++++++- Zend/zend_signal.h | 8 ++ configure.ac | 1 + ext/pcntl/pcntl.c | 33 ++++- ext/pcntl/tests/gh25541.phpt | 28 ++++ ext/pcntl/tests/gh25541_default.phpt | 28 ++++ ext/pcntl/tests/gh25541_sent_fault.phpt | 26 ++++ ext/zend_test/test.c | 28 ++++ ext/zend_test/test.stub.php | 4 + ext/zend_test/test_arginfo.h | 14 +- 12 files changed, 338 insertions(+), 4 deletions(-) create mode 100644 ext/pcntl/tests/gh25541.phpt create mode 100644 ext/pcntl/tests/gh25541_default.phpt create mode 100644 ext/pcntl/tests/gh25541_sent_fault.phpt diff --git a/NEWS b/NEWS index d29b778cd7f4..e308128155b9 100644 --- a/NEWS +++ b/NEWS @@ -118,6 +118,8 @@ PHP NEWS - PCNTL: . Fixed pcntl_signal() raising a fatal error for SIGKILL and SIGSTOP. (lacatoire) + . Fixed bug GH-25541 (Signal delivered to a thread that never ran PHP + crashes or kills the process under ZTS). (phcorp) - PCRE: . Fixed \C being allowed in patterns enabling UTF mode with (*UTF) instead diff --git a/Zend/zend.c b/Zend/zend.c index 1b1dd9e12e05..30129e265525 100644 --- a/Zend/zend.c +++ b/Zend/zend.c @@ -1317,6 +1317,9 @@ ZEND_API void zend_activate(void) /* {{{ */ { #ifdef ZTS virtual_cwd_activate(); +# ifndef ZEND_WIN32 + zend_signal_unblock_rerouted(); +# endif #endif gc_reset(); init_compiler(); diff --git a/Zend/zend_signal.c b/Zend/zend_signal.c index 83f8aa0852db..1b94e1593544 100644 --- a/Zend/zend_signal.c +++ b/Zend/zend_signal.c @@ -37,6 +37,12 @@ #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_SYS_UCONTEXT_H +#include +#endif +#if defined(ZTS) && !defined(ZEND_WIN32) +#include +#endif #ifdef ZEND_SIGNALS @@ -77,6 +83,14 @@ static const int zend_sigs[] = { TIMEOUT_SIG, SIGHUP, SIGINT, SIGQUIT, SIGTERM, /* True globals, written only at process startup */ static zend_signal_entry_t global_orig_handlers[NSIG]; + +#ifdef ZTS +/* Signals for which a PHP thread registered a handler: only these are sent on + * from threads that never ran PHP, the others keep the original disposition. + * Never cleared: a PHP thread that no longer handles the signal applies the + * original disposition, as the thread that received it would have. */ +static zend_atomic_bool global_php_handled[NSIG]; +#endif static sigset_t global_sigmask; /* {{{ zend_signal_handler_defer @@ -88,9 +102,16 @@ static void zend_signal_handler_defer(int signo, siginfo_t *siginfo, void *conte #ifdef ZTS /* A signal could hit after TSRM shutdown, in this case globals are already freed. - * Or it could be delivered to a thread that didn't execute PHP yet. - * In the latter case we act as if SIGG(active) is false. */ + * Or it could be delivered to a thread that didn't execute PHP yet: if a PHP + * thread handles this signal and the embedder does not, send it on to the + * PHP threads. Otherwise act as if SIGG(active) is false. */ if (tsrm_is_shutdown() || !tsrm_is_managed_thread()) { + if (zend_atomic_bool_load_ex(&global_php_handled[signo-1]) + && (global_orig_handlers[signo-1].handler == (void *) SIG_DFL + || global_orig_handlers[signo-1].handler == (void *) SIG_IGN) + && zend_signal_reroute_from_foreign_thread(signo, siginfo, context)) { + return; + } /* Forward to default handler handler */ zend_signal_handler(signo, siginfo, context); return; @@ -245,6 +266,13 @@ ZEND_API void zend_sigaction(int signo, const struct sigaction *act, struct siga SIGG(handlers)[signo-1].handler = (void *) act->sa_handler; } +#ifdef ZTS + if (SIGG(handlers)[signo-1].handler != (void *) SIG_IGN + && SIGG(handlers)[signo-1].handler != (void *) SIG_DFL) { + zend_atomic_bool_store_ex(&global_php_handled[signo-1], true); + } +#endif + memset(&sa, 0, sizeof(sa)); if (SIGG(handlers)[signo-1].handler == (void *) SIG_IGN) { sa.sa_sigaction = (void *) SIG_IGN; @@ -446,3 +474,138 @@ ZEND_API void zend_signal_startup(void) #endif /* ZEND_SIGNALS */ + +#if defined(ZTS) && !defined(ZEND_WIN32) +/* Platforms whose kernel restores the thread's mask from uc_sigmask on return + * from a handler, and keeps a process signal that every thread blocks pending + * (see zend_signal_reroute_from_foreign_thread()). */ +# if defined(HAVE_SYS_UCONTEXT_H) && (defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)) +# define ZEND_SIGNAL_CAN_REROUTE 1 +# endif + +# ifdef NSIG +# define ZEND_SIGNAL_TRACKED NSIG +# else +# define ZEND_SIGNAL_TRACKED 65 +# endif + +/* Signals re-routed at least once, so some threads may block them. */ +static zend_atomic_bool zend_signal_rerouted[ZEND_SIGNAL_TRACKED]; + +/* {{{ zend_signal_is_synchronous + * Faults, and abort() on this thread, must take their default action on the + * thread they hit. The same signals sent with kill() are ordinary signals. */ +ZEND_API bool zend_signal_is_synchronous(int signo, const siginfo_t *siginfo) +{ + switch (signo) { + case SIGABRT: + return !siginfo || siginfo->si_pid == getpid(); + case SIGSEGV: + case SIGFPE: + case SIGILL: +# ifdef SIGBUS + case SIGBUS: +# endif +# ifdef SIGTRAP + case SIGTRAP: +# endif +# ifdef SIGSYS + case SIGSYS: +# endif + if (!siginfo) { + return true; + } + /* Raised by the kernel. SI_USER is 0 on Linux, where the codes of + * kill(), sigqueue() and tgkill() are not positive; it is 0x10001 on + * macOS and FreeBSD, above the codes of faults. macOS reports + * SIGSEGV and SIGBUS sent with kill() with a fault code, so those + * are treated as faults there. Elsewhere, assume a fault. */ +# if defined(__linux__) + return siginfo->si_code > 0; +# elif defined(__APPLE__) || defined(__FreeBSD__) + return siginfo->si_code > 0 && siginfo->si_code < SI_USER; +# else + return true; +# endif + } + return false; +} +/* }}} */ + +/* {{{ zend_signal_reroute_from_foreign_thread + * Called by a signal handler running on a thread that never ran PHP and has no + * TSRM storage, such as a thread of the Go runtime in FrankenPHP or any thread + * created by an embedding application. That thread can neither run the handler + * a script registered nor use the original disposition: SIG_DFL would kill the + * process although the script handles the signal. + * + * Instead, block the signal on this thread once the handler returns, by adding + * it to the mask saved in the interrupted context (Linux, macOS and FreeBSD + * restore the thread's mask from uc_sigmask on sigreturn), and send it to the + * process again. The kernel delivers it to a thread that does not block it. + * Threads without PHP converge to blocking it, so it ends up on a PHP thread. + * If every thread blocks it, it stays pending. There is no loop: the handler + * runs with the signal blocked, and this thread keeps it blocked afterwards. + * A thread that starts a request unblocks it again, see + * zend_signal_unblock_rerouted(); this covers threads created by a thread + * that blocked it. Like zend_sigaction(), this also unblocks it where the + * SAPI blocked it on purpose. + * + * Limits: the signal mask of a thread the embedder owns changes; the siginfo of + * the re-sent signal names this process as the sender; the PHP thread that + * receives it is not necessarily the one that registered the handler; macOS + * attaches a process signal that every thread blocks to the main thread, so if + * the main thread does not run PHP, a signal re-sent while all PHP threads + * block it (e.g. during pcntl_signal_dispatch()) is not delivered. + * + * Returns false when the caller must handle the signal itself: synchronous + * signals, after TSRM shutdown, and on platforms not listed above. */ +ZEND_API bool zend_signal_reroute_from_foreign_thread(int signo, const siginfo_t *siginfo, void *context) +{ +# ifdef ZEND_SIGNAL_CAN_REROUTE + int errno_save; + + if (signo <= 0 || signo >= ZEND_SIGNAL_TRACKED || !context || tsrm_is_shutdown() + || zend_signal_is_synchronous(signo, siginfo)) { + return false; + } + + errno_save = errno; + zend_atomic_bool_store_ex(&zend_signal_rerouted[signo], true); + sigaddset(&((ucontext_t *) context)->uc_sigmask, signo); + kill(getpid(), signo); + errno = errno_save; + return true; +# else + (void) signo; + (void) siginfo; + (void) context; + return false; +# endif +} +/* }}} */ + +/* {{{ zend_signal_unblock_rerouted + * Called when a thread starts a request: the thread, or the one that created + * it, may have blocked a signal while it did not run PHP. */ +ZEND_API void zend_signal_unblock_rerouted(void) +{ +# ifdef ZEND_SIGNAL_CAN_REROUTE + sigset_t set; + bool any = false; + int signo; + + sigemptyset(&set); + for (signo = 1; signo < ZEND_SIGNAL_TRACKED; signo++) { + if (zend_atomic_bool_load_ex(&zend_signal_rerouted[signo])) { + sigaddset(&set, signo); + any = true; + } + } + if (any) { + pthread_sigmask(SIG_UNBLOCK, &set, NULL); + } +# endif +} +/* }}} */ +#endif diff --git a/Zend/zend_signal.h b/Zend/zend_signal.h index e88892da35aa..233c991b8d1a 100644 --- a/Zend/zend_signal.h +++ b/Zend/zend_signal.h @@ -111,4 +111,12 @@ ZEND_API void zend_sigaction(int signo, const struct sigaction *act, struct siga #endif /* ZEND_SIGNALS */ +#if defined(ZTS) && !defined(ZEND_WIN32) +# include + +ZEND_API bool zend_signal_is_synchronous(int signo, const siginfo_t *siginfo); +ZEND_API bool zend_signal_reroute_from_foreign_thread(int signo, const siginfo_t *siginfo, void *context); +ZEND_API void zend_signal_unblock_rerouted(void); +#endif + #endif /* ZEND_SIGNAL_H */ diff --git a/configure.ac b/configure.ac index 5612a0696a1f..fac178816e1a 100644 --- a/configure.ac +++ b/configure.ac @@ -395,6 +395,7 @@ AC_CHECK_HEADERS(m4_normalize([ sys/vfs.h sys/sysexits.h sys/uio.h + sys/ucontext.h sys/wait.h sys/loadavg.h unistd.h diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 67b129a15096..777a6e925102 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -1297,7 +1297,38 @@ static void pcntl_signal_handler(int signo, siginfo_t *siginfo, void *context) static void pcntl_signal_handler(int signo) #endif { - struct php_pcntl_pending_signal *psig = PCNTL_G(spares); + struct php_pcntl_pending_signal *psig; + +#ifdef ZTS + /* Not a PHP thread: there is no PCNTL_G() here. */ + if (UNEXPECTED(tsrm_is_shutdown() || !tsrm_is_managed_thread())) { + const siginfo_t *info = NULL; + +# ifdef HAVE_STRUCT_SIGINFO_T + if (zend_signal_reroute_from_foreign_thread(signo, siginfo, context)) { + return; + } + info = siginfo; +# endif + /* A fault happens again on return, now with the default action. + * abort() raises SIGABRT again by itself, and the syscall that + * raised SIGSYS just fails. */ + if (zend_signal_is_synchronous(signo, info) && signo != SIGABRT +# ifdef SIGSYS + && signo != SIGSYS +# endif + ) { + struct sigaction sa; + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = SIG_DFL; + sigemptyset(&sa.sa_mask); + sigaction(signo, &sa, NULL); + } + return; + } +#endif + + psig = PCNTL_G(spares); if (!psig) { /* oops, too many signals for us to track, so we'll forget about this one */ return; diff --git a/ext/pcntl/tests/gh25541.phpt b/ext/pcntl/tests/gh25541.phpt new file mode 100644 index 000000000000..ab391618ca26 --- /dev/null +++ b/ext/pcntl/tests/gh25541.phpt @@ -0,0 +1,28 @@ +--TEST-- +GH-25541 (Signal handler not run for a signal delivered to a thread that never ran PHP) +--EXTENSIONS-- +pcntl +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +int(3) diff --git a/ext/pcntl/tests/gh25541_default.phpt b/ext/pcntl/tests/gh25541_default.phpt new file mode 100644 index 000000000000..e3e266b15465 --- /dev/null +++ b/ext/pcntl/tests/gh25541_default.phpt @@ -0,0 +1,28 @@ +--TEST-- +GH-25541 (Signal without a handler, delivered to a thread that never ran PHP, keeps its default action) +--EXTENSIONS-- +pcntl +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +bool(true) +bool(true) diff --git a/ext/pcntl/tests/gh25541_sent_fault.phpt b/ext/pcntl/tests/gh25541_sent_fault.phpt new file mode 100644 index 000000000000..799acee9990b --- /dev/null +++ b/ext/pcntl/tests/gh25541_sent_fault.phpt @@ -0,0 +1,26 @@ +--TEST-- +GH-25541 (SIGTRAP sent with kill() to a thread that never ran PHP is not a fault) +--EXTENSIONS-- +pcntl +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +bool(true) diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index 2d106337c5cb..b1e1d3037362 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -41,6 +41,9 @@ #include "zend_system_id.h" #include "zend_mm_custom_handlers.h" #include +#if defined(ZTS) && !defined(PHP_WIN32) +# include +#endif // `php.h` sets `NDEBUG` when not `PHP_DEBUG` which will make `assert()` from // assert.h a no-op. In order to have `assert()` working on NDEBUG builds, we @@ -783,6 +786,31 @@ static ZEND_FUNCTION(zend_test_raise_and_throw) zend_throw_exception(NULL, "Exception after raise()", 0); } +#if defined(ZTS) && !defined(PHP_WIN32) +static void *zend_test_raise_thread(void *arg) +{ + raise((int) (intptr_t) arg); + return NULL; +} + +/* Raises a signal on a thread that never ran PHP and has no TSRM storage. */ +static ZEND_FUNCTION(zend_test_raise_on_foreign_thread) +{ + zend_long signo; + pthread_t thread; + + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_LONG(signo) + ZEND_PARSE_PARAMETERS_END(); + + if (pthread_create(&thread, NULL, zend_test_raise_thread, (void *) (intptr_t) signo) != 0) { + zend_throw_error(NULL, "pthread_create() failed"); + RETURN_THROWS(); + } + pthread_join(thread, NULL); +} +#endif + static bool has_opline(zend_execute_data *execute_data) { return execute_data diff --git a/ext/zend_test/test.stub.php b/ext/zend_test/test.stub.php index 785390dd70cc..828a61883534 100644 --- a/ext/zend_test/test.stub.php +++ b/ext/zend_test/test.stub.php @@ -307,6 +307,10 @@ function zend_test_crash(?string $message = null): void {} function zend_test_raise_and_throw(int $signal): void {} +#if defined(ZTS) && !defined(PHP_WIN32) + function zend_test_raise_on_foreign_thread(int $signal): void {} +#endif + function zend_test_fill_packed_array(array &$array): void {} /** @return resource */ diff --git a/ext/zend_test/test_arginfo.h b/ext/zend_test/test_arginfo.h index 45545edf5db8..e75e73671b47 100644 --- a/ext/zend_test/test_arginfo.h +++ b/ext/zend_test/test_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 20856900222b20ff71d57bf575ad7034ffc34bf4 */ + * Stub hash: 84b5600c974d901618c660cedb2a0df2f854e618 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0) ZEND_END_ARG_INFO() @@ -155,6 +155,12 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_raise_and_throw, 0, 1, ZEND_ARG_TYPE_INFO(0, signal, IS_LONG, 0) ZEND_END_ARG_INFO() +#if defined(ZTS) && !defined(PHP_WIN32) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_raise_on_foreign_thread, 0, 1, IS_VOID, 0) + ZEND_ARG_TYPE_INFO(0, signal, IS_LONG, 0) +ZEND_END_ARG_INFO() +#endif + ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_fill_packed_array, 0, 1, IS_VOID, 0) ZEND_ARG_TYPE_INFO(1, array, IS_ARRAY, 0) ZEND_END_ARG_INFO() @@ -336,6 +342,9 @@ static ZEND_FUNCTION(zend_test_is_string_marked_as_valid_utf8); static ZEND_FUNCTION(zend_get_map_ptr_last); static ZEND_FUNCTION(zend_test_crash); static ZEND_FUNCTION(zend_test_raise_and_throw); +#if defined(ZTS) && !defined(PHP_WIN32) +static ZEND_FUNCTION(zend_test_raise_on_foreign_thread); +#endif static ZEND_FUNCTION(zend_test_fill_packed_array); static ZEND_FUNCTION(zend_test_create_throwing_resource); static ZEND_FUNCTION(get_open_basedir); @@ -452,6 +461,9 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(zend_get_map_ptr_last, arginfo_zend_get_map_ptr_last) ZEND_FE(zend_test_crash, arginfo_zend_test_crash) ZEND_FE(zend_test_raise_and_throw, arginfo_zend_test_raise_and_throw) +#if defined(ZTS) && !defined(PHP_WIN32) + ZEND_FE(zend_test_raise_on_foreign_thread, arginfo_zend_test_raise_on_foreign_thread) +#endif ZEND_FE(zend_test_fill_packed_array, arginfo_zend_test_fill_packed_array) ZEND_FE(zend_test_create_throwing_resource, arginfo_zend_test_create_throwing_resource) ZEND_FE(get_open_basedir, arginfo_get_open_basedir) From f84d03f3224653d261c0c2ca3018d276009ceff1 Mon Sep 17 00:00:00 2001 From: phcorp <725023+phcorp@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:38:26 +0100 Subject: [PATCH 2/5] Only re-route a signal while a PHP thread still handles it A signal whose handler was set back to SIG_DFL kept being sent on from a thread that never ran PHP. If every PHP thread blocked it, the signal stayed pending instead of taking its default action. Count the PHP threads whose handler is neither SIG_DFL nor SIG_IGN, and only re-route while that count is positive. The count follows every write to a thread's handlers and drops when a thread's storage is freed. zend_test_start_foreign_thread() starts a thread that never runs PHP and stays alive, so a test can block the signal on the PHP thread only. --- Zend/zend_signal.c | 69 ++++++++++++++++---- ext/pcntl/tests/gh25541_blocked.phpt | 29 ++++++++ ext/pcntl/tests/gh25541_handler_removed.phpt | 34 ++++++++++ ext/zend_test/test.c | 24 +++++++ ext/zend_test/test.stub.php | 2 + ext/zend_test/test_arginfo.h | 7 +- 6 files changed, 153 insertions(+), 12 deletions(-) create mode 100644 ext/pcntl/tests/gh25541_blocked.phpt create mode 100644 ext/pcntl/tests/gh25541_handler_removed.phpt diff --git a/Zend/zend_signal.c b/Zend/zend_signal.c index 1b94e1593544..37c1079726aa 100644 --- a/Zend/zend_signal.c +++ b/Zend/zend_signal.c @@ -85,11 +85,29 @@ static const int zend_sigs[] = { TIMEOUT_SIG, SIGHUP, SIGINT, SIGQUIT, SIGTERM, static zend_signal_entry_t global_orig_handlers[NSIG]; #ifdef ZTS -/* Signals for which a PHP thread registered a handler: only these are sent on - * from threads that never ran PHP, the others keep the original disposition. - * Never cleared: a PHP thread that no longer handles the signal applies the - * original disposition, as the thread that received it would have. */ -static zend_atomic_bool global_php_handled[NSIG]; +/* Number of PHP threads whose handler for the signal is neither SIG_DFL nor + * SIG_IGN. Only signals with a handler are sent on from threads that never ran + * PHP; the others keep the original disposition on the thread they hit. */ +static zend_atomic_int global_php_handlers[NSIG]; + +static bool zend_signal_entry_is_handler(const zend_signal_entry_t *entry) +{ + return entry->handler != (void *) SIG_DFL && entry->handler != (void *) SIG_IGN; +} + +/* Called by a PHP thread when its handler for signo changes. */ +static void zend_signal_count_handler(int signo, bool before, bool after) +{ + zend_atomic_int *count = &global_php_handlers[signo-1]; + int expected; + + if (before == after) { + return; + } + expected = zend_atomic_int_load_ex(count); + while (!zend_atomic_int_compare_exchange_ex(count, &expected, expected + (after ? 1 : -1))) { + } +} #endif static sigset_t global_sigmask; @@ -106,7 +124,7 @@ static void zend_signal_handler_defer(int signo, siginfo_t *siginfo, void *conte * thread handles this signal and the embedder does not, send it on to the * PHP threads. Otherwise act as if SIGG(active) is false. */ if (tsrm_is_shutdown() || !tsrm_is_managed_thread()) { - if (zend_atomic_bool_load_ex(&global_php_handled[signo-1]) + if (zend_atomic_int_load_ex(&global_php_handlers[signo-1]) > 0 && (global_orig_handlers[signo-1].handler == (void *) SIG_DFL || global_orig_handlers[signo-1].handler == (void *) SIG_IGN) && zend_signal_reroute_from_foreign_thread(signo, siginfo, context)) { @@ -236,6 +254,9 @@ static void zend_signal_handler(int signo, siginfo_t *siginfo, void *context) if (p_sig.flags & SA_RESETHAND) { SIGG(handlers)[signo-1].flags = 0; SIGG(handlers)[signo-1].handler = SIG_DFL; +#ifdef ZTS + zend_signal_count_handler(signo, true, false); +#endif } (*(void (*)(int, siginfo_t*, void*))p_sig.handler)(signo, siginfo, context); } else { @@ -259,6 +280,10 @@ ZEND_API void zend_sigaction(int signo, const struct sigaction *act, struct siga oldact->sa_mask = global_sigmask; } if (act != NULL) { +#ifdef ZTS + bool was_handler = zend_signal_entry_is_handler(&SIGG(handlers)[signo-1]); +#endif + SIGG(handlers)[signo-1].flags = act->sa_flags; if (act->sa_flags & SA_SIGINFO) { SIGG(handlers)[signo-1].handler = (void *) act->sa_sigaction; @@ -267,10 +292,7 @@ ZEND_API void zend_sigaction(int signo, const struct sigaction *act, struct siga } #ifdef ZTS - if (SIGG(handlers)[signo-1].handler != (void *) SIG_IGN - && SIGG(handlers)[signo-1].handler != (void *) SIG_DFL) { - zend_atomic_bool_store_ex(&global_php_handled[signo-1], true); - } + zend_signal_count_handler(signo, was_handler, zend_signal_entry_is_handler(&SIGG(handlers)[signo-1])); #endif memset(&sa, 0, sizeof(sa)); @@ -322,12 +344,19 @@ static zend_result zend_signal_register(int signo, void (*handler)(int, siginfo_ return FAILURE; } +#ifdef ZTS + bool was_handler = zend_signal_entry_is_handler(&SIGG(handlers)[signo-1]); +#endif + SIGG(handlers)[signo-1].flags = sa.sa_flags; if (sa.sa_flags & SA_SIGINFO) { SIGG(handlers)[signo-1].handler = (void *)sa.sa_sigaction; } else { SIGG(handlers)[signo-1].handler = (void *)sa.sa_handler; } +#ifdef ZTS + zend_signal_count_handler(signo, was_handler, zend_signal_entry_is_handler(&SIGG(handlers)[signo-1])); +#endif sa.sa_flags = SA_SIGINFO; /* we'll use a siginfo handler */ sa.sa_sigaction = handler; @@ -348,6 +377,12 @@ void zend_signal_activate(void) { size_t x; +#ifdef ZTS + for (x = 1; x < NSIG; x++) { + zend_signal_count_handler((int) x, zend_signal_entry_is_handler(&SIGG(handlers)[x-1]), + zend_signal_entry_is_handler(&global_orig_handlers[x-1])); + } +#endif memcpy(&SIGG(handlers), &global_orig_handlers, sizeof(global_orig_handlers)); if (SIGG(reset)) { @@ -415,6 +450,18 @@ static void zend_signal_globals_ctor(zend_signal_globals_t *zend_signal_globals) } /* }}} */ +#ifdef ZTS +static void zend_signal_globals_dtor(zend_signal_globals_t *zend_signal_globals) /* {{{ */ +{ + int signo; + + for (signo = 1; signo < NSIG; signo++) { + zend_signal_count_handler(signo, zend_signal_entry_is_handler(&zend_signal_globals->handlers[signo-1]), false); + } +} +/* }}} */ +#endif + void zend_signal_init(void) /* {{{ */ { int signo; @@ -441,7 +488,7 @@ ZEND_API void zend_signal_startup(void) { #ifdef ZTS - ts_allocate_fast_id(&zend_signal_globals_id, &zend_signal_globals_offset, sizeof(zend_signal_globals_t), (ts_allocate_ctor) zend_signal_globals_ctor, NULL); + ts_allocate_fast_id(&zend_signal_globals_id, &zend_signal_globals_offset, sizeof(zend_signal_globals_t), (ts_allocate_ctor) zend_signal_globals_ctor, (ts_allocate_dtor) zend_signal_globals_dtor); #else zend_signal_globals_ctor(&zend_signal_globals); #endif diff --git a/ext/pcntl/tests/gh25541_blocked.phpt b/ext/pcntl/tests/gh25541_blocked.phpt new file mode 100644 index 000000000000..798e63b5f873 --- /dev/null +++ b/ext/pcntl/tests/gh25541_blocked.phpt @@ -0,0 +1,29 @@ +--TEST-- +GH-25541 (Signal re-routed while the PHP thread blocks it is handled once unblocked) +--EXTENSIONS-- +pcntl +posix +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +bool(true) diff --git a/ext/pcntl/tests/gh25541_handler_removed.phpt b/ext/pcntl/tests/gh25541_handler_removed.phpt new file mode 100644 index 000000000000..ca1852becdc1 --- /dev/null +++ b/ext/pcntl/tests/gh25541_handler_removed.phpt @@ -0,0 +1,34 @@ +--TEST-- +GH-25541 (Signal whose handler was removed keeps its default action on a thread that never ran PHP) +--EXTENSIONS-- +pcntl +posix +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +bool(true) +bool(true) diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index b1e1d3037362..5ab51f392d49 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -809,6 +809,30 @@ static ZEND_FUNCTION(zend_test_raise_on_foreign_thread) } pthread_join(thread, NULL); } + +static void *zend_test_idle_thread(void *arg) +{ + (void) arg; + for (;;) { + pause(); + } + return NULL; +} + +/* Starts a thread that never runs PHP and stays alive until the process exits. + * It inherits the caller's signal mask. */ +static ZEND_FUNCTION(zend_test_start_foreign_thread) +{ + pthread_t thread; + + ZEND_PARSE_PARAMETERS_NONE(); + + if (pthread_create(&thread, NULL, zend_test_idle_thread, NULL) != 0) { + zend_throw_error(NULL, "pthread_create() failed"); + RETURN_THROWS(); + } + pthread_detach(thread); +} #endif static bool has_opline(zend_execute_data *execute_data) diff --git a/ext/zend_test/test.stub.php b/ext/zend_test/test.stub.php index 828a61883534..914b36e17c83 100644 --- a/ext/zend_test/test.stub.php +++ b/ext/zend_test/test.stub.php @@ -309,6 +309,8 @@ function zend_test_raise_and_throw(int $signal): void {} #if defined(ZTS) && !defined(PHP_WIN32) function zend_test_raise_on_foreign_thread(int $signal): void {} + + function zend_test_start_foreign_thread(): void {} #endif function zend_test_fill_packed_array(array &$array): void {} diff --git a/ext/zend_test/test_arginfo.h b/ext/zend_test/test_arginfo.h index e75e73671b47..28720e99c01a 100644 --- a/ext/zend_test/test_arginfo.h +++ b/ext/zend_test/test_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 84b5600c974d901618c660cedb2a0df2f854e618 */ + * Stub hash: 48ccd956ff33cb3abb8ed0a88329d6eff2a2ed5b */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0) ZEND_END_ARG_INFO() @@ -159,6 +159,9 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_raise_on_foreign_thread, 0, 1, IS_VOID, 0) ZEND_ARG_TYPE_INFO(0, signal, IS_LONG, 0) ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_start_foreign_thread, 0, 0, IS_VOID, 0) +ZEND_END_ARG_INFO() #endif ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_fill_packed_array, 0, 1, IS_VOID, 0) @@ -344,6 +347,7 @@ static ZEND_FUNCTION(zend_test_crash); static ZEND_FUNCTION(zend_test_raise_and_throw); #if defined(ZTS) && !defined(PHP_WIN32) static ZEND_FUNCTION(zend_test_raise_on_foreign_thread); +static ZEND_FUNCTION(zend_test_start_foreign_thread); #endif static ZEND_FUNCTION(zend_test_fill_packed_array); static ZEND_FUNCTION(zend_test_create_throwing_resource); @@ -463,6 +467,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(zend_test_raise_and_throw, arginfo_zend_test_raise_and_throw) #if defined(ZTS) && !defined(PHP_WIN32) ZEND_FE(zend_test_raise_on_foreign_thread, arginfo_zend_test_raise_on_foreign_thread) + ZEND_FE(zend_test_start_foreign_thread, arginfo_zend_test_start_foreign_thread) #endif ZEND_FE(zend_test_fill_packed_array, arginfo_zend_test_fill_packed_array) ZEND_FE(zend_test_create_throwing_resource, arginfo_zend_test_create_throwing_resource) From 045a31b88c05532d53628364734aed10684e2c36 Mon Sep 17 00:00:00 2001 From: phcorp <725023+phcorp@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:08:05 +0100 Subject: [PATCH 3/5] Don't reset SIGG(handlers) on a thread without TSRM On a thread without TSRM, or after TSRM shutdown, zend_signal_handler() calls the handler that was installed before PHP started. When that handler has SA_RESETHAND, it then wrote to SIGG(handlers), which such a thread has no storage for, and crashed. It also decremented the count of PHP threads handling the signal, although that thread was never counted. Only reset the entry when it comes from this thread's SIGG(handlers). macOS does not report SA_RESETHAND back from sigaction(), so it was not affected. --- Zend/zend_signal.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Zend/zend_signal.c b/Zend/zend_signal.c index 37c1079726aa..ebfc43c6393e 100644 --- a/Zend/zend_signal.c +++ b/Zend/zend_signal.c @@ -220,9 +220,13 @@ static void zend_signal_handler(int signo, siginfo_t *siginfo, void *context) struct sigaction sa; sigset_t sigset; zend_signal_entry_t p_sig; + /* Whether p_sig is this thread's entry in SIGG(handlers), which only then + * may be reset for SA_RESETHAND. */ + bool own_handler = true; #ifdef ZTS if (tsrm_is_shutdown() || !tsrm_is_managed_thread()) { p_sig = global_orig_handlers[signo-1]; + own_handler = false; } else #endif p_sig = SIGG(handlers)[signo-1]; @@ -251,7 +255,7 @@ static void zend_signal_handler(int signo, siginfo_t *siginfo, void *context) } } else if (p_sig.handler != SIG_IGN) { if (p_sig.flags & SA_SIGINFO) { - if (p_sig.flags & SA_RESETHAND) { + if ((p_sig.flags & SA_RESETHAND) && own_handler) { SIGG(handlers)[signo-1].flags = 0; SIGG(handlers)[signo-1].handler = SIG_DFL; #ifdef ZTS From c2b07bdd5be01e93ef1fef504884bd56cac7ba59 Mon Sep 17 00:00:00 2001 From: phcorp <725023+phcorp@users.noreply.github.com> Date: Sat, 21 Nov 2026 07:49:33 +0100 Subject: [PATCH 4/5] Re-route SIGABRT sent with kill() from a thread that never ran PHP zend_signal_is_synchronous() took a SIGABRT whose sender is this process for abort(). That is also true of kill(getpid(), SIGABRT) from a script, and of the signal the re-route itself sends, so with a pcntl handler set: - a SIGABRT the process sends itself, received by a thread without TSRM, killed the process (zend signals) or was dropped (without); - a SIGABRT from another process, re-sent by one such thread and received by a second one, did the same. abort() needs no exception: when a handler returns, it restores the default action and raises SIGABRT again, also when the handler left it blocked in uc_sigmask (measured on glibc, musl and macOS). macOS can't tell kill() from raise() within a process anyway. --- Zend/tests/arginfo_zpp_mismatch.inc | 1 + Zend/zend_signal.c | 11 +++--- ext/pcntl/pcntl.c | 5 ++- ext/pcntl/tests/gh25541_abort.phpt | 27 ++++++++++++++ ext/pcntl/tests/gh25541_abort_external.phpt | 40 +++++++++++++++++++++ ext/pcntl/tests/gh25541_abort_kill.phpt | 29 +++++++++++++++ ext/zend_test/test.c | 21 +++++++++++ ext/zend_test/test.stub.php | 2 ++ ext/zend_test/test_arginfo.h | 8 +++-- 9 files changed, 135 insertions(+), 9 deletions(-) create mode 100644 ext/pcntl/tests/gh25541_abort.phpt create mode 100644 ext/pcntl/tests/gh25541_abort_external.phpt create mode 100644 ext/pcntl/tests/gh25541_abort_kill.phpt diff --git a/Zend/tests/arginfo_zpp_mismatch.inc b/Zend/tests/arginfo_zpp_mismatch.inc index 0ac5df8ceffd..da721810a98c 100644 --- a/Zend/tests/arginfo_zpp_mismatch.inc +++ b/Zend/tests/arginfo_zpp_mismatch.inc @@ -13,6 +13,7 @@ function skipFunction($function): bool { || $function === 'zend_create_unterminated_string' || $function === 'zend_test_array_return' || $function === 'zend_test_crash' + || $function === 'zend_test_abort_on_foreign_thread' || $function === 'zend_leak_bytes' /* mess with output */ || (is_string($function) && str_starts_with($function, 'ob_')) diff --git a/Zend/zend_signal.c b/Zend/zend_signal.c index ebfc43c6393e..95ed37e53ac1 100644 --- a/Zend/zend_signal.c +++ b/Zend/zend_signal.c @@ -544,13 +544,16 @@ ZEND_API void zend_signal_startup(void) static zend_atomic_bool zend_signal_rerouted[ZEND_SIGNAL_TRACKED]; /* {{{ zend_signal_is_synchronous - * Faults, and abort() on this thread, must take their default action on the - * thread they hit. The same signals sent with kill() are ordinary signals. */ + * Faults must take their default action on the thread they hit. The same + * signals sent with kill() are ordinary signals. + * + * SIGABRT is not one of them: if a handler returns, abort() restores the + * default action and raises it again, even when the handler left it blocked. + * It also can't be told apart from kill(getpid(), SIGABRT), which the re-route + * itself sends, on every platform: macOS reports both alike. */ ZEND_API bool zend_signal_is_synchronous(int signo, const siginfo_t *siginfo) { switch (signo) { - case SIGABRT: - return !siginfo || siginfo->si_pid == getpid(); case SIGSEGV: case SIGFPE: case SIGILL: diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 777a6e925102..618b0ab56514 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -1311,9 +1311,8 @@ static void pcntl_signal_handler(int signo) info = siginfo; # endif /* A fault happens again on return, now with the default action. - * abort() raises SIGABRT again by itself, and the syscall that - * raised SIGSYS just fails. */ - if (zend_signal_is_synchronous(signo, info) && signo != SIGABRT + * The syscall that raised SIGSYS just fails. */ + if (zend_signal_is_synchronous(signo, info) # ifdef SIGSYS && signo != SIGSYS # endif diff --git a/ext/pcntl/tests/gh25541_abort.phpt b/ext/pcntl/tests/gh25541_abort.phpt new file mode 100644 index 000000000000..d4f593795266 --- /dev/null +++ b/ext/pcntl/tests/gh25541_abort.phpt @@ -0,0 +1,27 @@ +--TEST-- +GH-25541 (abort() on a thread that never ran PHP keeps its default action although SIGABRT is handled) +--EXTENSIONS-- +pcntl +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +bool(true) +bool(true) diff --git a/ext/pcntl/tests/gh25541_abort_external.phpt b/ext/pcntl/tests/gh25541_abort_external.phpt new file mode 100644 index 000000000000..89e520d7dc7a --- /dev/null +++ b/ext/pcntl/tests/gh25541_abort_external.phpt @@ -0,0 +1,40 @@ +--TEST-- +GH-25541 (SIGABRT from another process is re-routed across several threads that never ran PHP) +--EXTENSIONS-- +pcntl +posix +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +string(6) "exit 0" diff --git a/ext/pcntl/tests/gh25541_abort_kill.phpt b/ext/pcntl/tests/gh25541_abort_kill.phpt new file mode 100644 index 000000000000..1fea6210aafd --- /dev/null +++ b/ext/pcntl/tests/gh25541_abort_kill.phpt @@ -0,0 +1,29 @@ +--TEST-- +GH-25541 (SIGABRT sent with kill() by the process itself is re-routed from a thread that never ran PHP) +--EXTENSIONS-- +pcntl +posix +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +bool(true) diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index 5ab51f392d49..29a7c3a60e67 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -810,6 +810,27 @@ static ZEND_FUNCTION(zend_test_raise_on_foreign_thread) pthread_join(thread, NULL); } +static void *zend_test_abort_thread(void *arg) +{ + (void) arg; + abort(); + return NULL; +} + +/* Calls abort() on a thread that never ran PHP and has no TSRM storage. */ +static ZEND_FUNCTION(zend_test_abort_on_foreign_thread) +{ + pthread_t thread; + + ZEND_PARSE_PARAMETERS_NONE(); + + if (pthread_create(&thread, NULL, zend_test_abort_thread, NULL) != 0) { + zend_throw_error(NULL, "pthread_create() failed"); + RETURN_THROWS(); + } + pthread_join(thread, NULL); +} + static void *zend_test_idle_thread(void *arg) { (void) arg; diff --git a/ext/zend_test/test.stub.php b/ext/zend_test/test.stub.php index 914b36e17c83..f840b74bb577 100644 --- a/ext/zend_test/test.stub.php +++ b/ext/zend_test/test.stub.php @@ -310,6 +310,8 @@ function zend_test_raise_and_throw(int $signal): void {} #if defined(ZTS) && !defined(PHP_WIN32) function zend_test_raise_on_foreign_thread(int $signal): void {} + function zend_test_abort_on_foreign_thread(): void {} + function zend_test_start_foreign_thread(): void {} #endif diff --git a/ext/zend_test/test_arginfo.h b/ext/zend_test/test_arginfo.h index 28720e99c01a..d71fdf4263c9 100644 --- a/ext/zend_test/test_arginfo.h +++ b/ext/zend_test/test_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 48ccd956ff33cb3abb8ed0a88329d6eff2a2ed5b */ + * Stub hash: 8edb08faa803292b450ab11b2bb2c4f134aa37d3 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0) ZEND_END_ARG_INFO() @@ -160,8 +160,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_raise_on_foreign_threa ZEND_ARG_TYPE_INFO(0, signal, IS_LONG, 0) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_start_foreign_thread, 0, 0, IS_VOID, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_abort_on_foreign_thread, 0, 0, IS_VOID, 0) ZEND_END_ARG_INFO() + +#define arginfo_zend_test_start_foreign_thread arginfo_zend_test_abort_on_foreign_thread #endif ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_fill_packed_array, 0, 1, IS_VOID, 0) @@ -347,6 +349,7 @@ static ZEND_FUNCTION(zend_test_crash); static ZEND_FUNCTION(zend_test_raise_and_throw); #if defined(ZTS) && !defined(PHP_WIN32) static ZEND_FUNCTION(zend_test_raise_on_foreign_thread); +static ZEND_FUNCTION(zend_test_abort_on_foreign_thread); static ZEND_FUNCTION(zend_test_start_foreign_thread); #endif static ZEND_FUNCTION(zend_test_fill_packed_array); @@ -467,6 +470,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(zend_test_raise_and_throw, arginfo_zend_test_raise_and_throw) #if defined(ZTS) && !defined(PHP_WIN32) ZEND_FE(zend_test_raise_on_foreign_thread, arginfo_zend_test_raise_on_foreign_thread) + ZEND_FE(zend_test_abort_on_foreign_thread, arginfo_zend_test_abort_on_foreign_thread) ZEND_FE(zend_test_start_foreign_thread, arginfo_zend_test_start_foreign_thread) #endif ZEND_FE(zend_test_fill_packed_array, arginfo_zend_test_fill_packed_array) From d0984a2e5bec47960f020490052d1fea5dd5bccc Mon Sep 17 00:00:00 2001 From: phcorp <725023+phcorp@users.noreply.github.com> Date: Sat, 21 Nov 2026 10:48:27 +0100 Subject: [PATCH 5/5] Keep abort()'s default action on a thread that never ran PHP The previous commit re-routed every SIGABRT. When the handler leaves SIGABRT blocked on the thread that called abort(), glibc 2.39 and older can't raise it again and the process ends with another signal (SIGTRAP on aarch64, measured on glibc 2.35, 2.36 and 2.39). Count SIGABRT as abort() again when it was sent to this thread: SI_TKILL on Linux, SI_LWP on FreeBSD. kill(), including the re-route's own, reports SI_USER, so it is still re-routed. macOS reports both alike, and its abort() still ends with SIGABRT, so SIGABRT is re-routed there. Also poll pcntl_signal_dispatch() in the tests that block the signal, and skip the other foreign-thread helpers in arginfo_zpp_mismatch.inc. --- Zend/tests/arginfo_zpp_mismatch.inc | 2 ++ Zend/zend_signal.c | 27 ++++++++++++++++----- ext/pcntl/pcntl.c | 5 ++-- ext/pcntl/tests/gh25541_abort_external.phpt | 5 +++- ext/pcntl/tests/gh25541_abort_kill.phpt | 5 +++- ext/pcntl/tests/gh25541_blocked.phpt | 5 +++- 6 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Zend/tests/arginfo_zpp_mismatch.inc b/Zend/tests/arginfo_zpp_mismatch.inc index da721810a98c..a75eb4a57f6a 100644 --- a/Zend/tests/arginfo_zpp_mismatch.inc +++ b/Zend/tests/arginfo_zpp_mismatch.inc @@ -14,6 +14,8 @@ function skipFunction($function): bool { || $function === 'zend_test_array_return' || $function === 'zend_test_crash' || $function === 'zend_test_abort_on_foreign_thread' + || $function === 'zend_test_raise_on_foreign_thread' + || $function === 'zend_test_start_foreign_thread' || $function === 'zend_leak_bytes' /* mess with output */ || (is_string($function) && str_starts_with($function, 'ob_')) diff --git a/Zend/zend_signal.c b/Zend/zend_signal.c index 95ed37e53ac1..6c7f645a0d71 100644 --- a/Zend/zend_signal.c +++ b/Zend/zend_signal.c @@ -544,16 +544,31 @@ ZEND_API void zend_signal_startup(void) static zend_atomic_bool zend_signal_rerouted[ZEND_SIGNAL_TRACKED]; /* {{{ zend_signal_is_synchronous - * Faults must take their default action on the thread they hit. The same - * signals sent with kill() are ordinary signals. + * Faults, and abort() on this thread, must take their default action on the + * thread they hit. The same signals sent with kill() are ordinary signals. * - * SIGABRT is not one of them: if a handler returns, abort() restores the - * default action and raises it again, even when the handler left it blocked. - * It also can't be told apart from kill(getpid(), SIGABRT), which the re-route - * itself sends, on every platform: macOS reports both alike. */ + * abort() raises SIGABRT on the calling thread, which Linux reports as + * SI_TKILL and FreeBSD as SI_LWP, while kill(), also the one the re-route + * sends, reports SI_USER. Re-routing it would leave it blocked on this thread: + * glibc 2.39 and older then can't raise it again, and the process ends with + * another signal. macOS reports both alike, but its abort() still ends with + * SIGABRT, so it is re-routed there. */ ZEND_API bool zend_signal_is_synchronous(int signo, const siginfo_t *siginfo) { switch (signo) { + case SIGABRT: + if (!siginfo) { + return true; + } +# if defined(__linux__) + return siginfo->si_code == SI_TKILL; +# elif defined(__FreeBSD__) + return siginfo->si_code == SI_LWP; +# elif defined(__APPLE__) + return false; +# else + return true; +# endif case SIGSEGV: case SIGFPE: case SIGILL: diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 618b0ab56514..777a6e925102 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -1311,8 +1311,9 @@ static void pcntl_signal_handler(int signo) info = siginfo; # endif /* A fault happens again on return, now with the default action. - * The syscall that raised SIGSYS just fails. */ - if (zend_signal_is_synchronous(signo, info) + * abort() raises SIGABRT again by itself, and the syscall that + * raised SIGSYS just fails. */ + if (zend_signal_is_synchronous(signo, info) && signo != SIGABRT # ifdef SIGSYS && signo != SIGSYS # endif diff --git a/ext/pcntl/tests/gh25541_abort_external.phpt b/ext/pcntl/tests/gh25541_abort_external.phpt index 89e520d7dc7a..981137acb126 100644 --- a/ext/pcntl/tests/gh25541_abort_external.phpt +++ b/ext/pcntl/tests/gh25541_abort_external.phpt @@ -27,7 +27,10 @@ if ($pid === 0) { posix_kill(posix_getppid(), SIGUSR1); usleep(500000); pcntl_sigprocmask(SIG_UNBLOCK, [SIGABRT]); - pcntl_signal_dispatch(); + for ($wait = 0; !$handled && $wait < 500; $wait++) { + pcntl_signal_dispatch(); + usleep(10000); + } exit($handled ? 0 : 1); } diff --git a/ext/pcntl/tests/gh25541_abort_kill.phpt b/ext/pcntl/tests/gh25541_abort_kill.phpt index 1fea6210aafd..9a9b099fc1ad 100644 --- a/ext/pcntl/tests/gh25541_abort_kill.phpt +++ b/ext/pcntl/tests/gh25541_abort_kill.phpt @@ -22,7 +22,10 @@ pcntl_sigprocmask(SIG_BLOCK, [SIGABRT]); posix_kill(posix_getpid(), SIGABRT); usleep(100000); pcntl_sigprocmask(SIG_UNBLOCK, [SIGABRT]); -pcntl_signal_dispatch(); +for ($wait = 0; !$handled && $wait < 500; $wait++) { + pcntl_signal_dispatch(); + usleep(10000); +} var_dump($handled); ?> --EXPECT-- diff --git a/ext/pcntl/tests/gh25541_blocked.phpt b/ext/pcntl/tests/gh25541_blocked.phpt index 798e63b5f873..9c7531edba6b 100644 --- a/ext/pcntl/tests/gh25541_blocked.phpt +++ b/ext/pcntl/tests/gh25541_blocked.phpt @@ -22,7 +22,10 @@ pcntl_sigprocmask(SIG_BLOCK, [SIGUSR1]); posix_kill(posix_getpid(), SIGUSR1); usleep(100000); pcntl_sigprocmask(SIG_UNBLOCK, [SIGUSR1]); -pcntl_signal_dispatch(); +for ($wait = 0; !$handled && $wait < 500; $wait++) { + pcntl_signal_dispatch(); + usleep(10000); +} var_dump($handled); ?> --EXPECT--