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/tests/arginfo_zpp_mismatch.inc b/Zend/tests/arginfo_zpp_mismatch.inc index 0ac5df8ceffd..a75eb4a57f6a 100644 --- a/Zend/tests/arginfo_zpp_mismatch.inc +++ b/Zend/tests/arginfo_zpp_mismatch.inc @@ -13,6 +13,9 @@ 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_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.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..cedabdca921f 100644 --- a/Zend/zend_signal.c +++ b/Zend/zend_signal.c @@ -37,6 +37,15 @@ #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_SYS_UCONTEXT_H +#include +#endif +#if defined(ZTS) && !defined(ZEND_WIN32) +#include +#endif +#if defined(ZTS) && defined(__linux__) +#include +#endif #ifdef ZEND_SIGNALS @@ -77,6 +86,48 @@ 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 +/* 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))) { + } +} + +/* {{{ zend_signal_fork_child + * Called in the child of fork(). Only the calling thread exists there, so only + * its handlers count: a handler of another PHP thread must not have a signal + * re-routed in the child. */ +ZEND_API void zend_signal_fork_child(void) +{ + bool managed = !tsrm_is_shutdown() && tsrm_is_managed_thread(); + int signo; + + for (signo = 1; signo < NSIG; signo++) { + zend_atomic_int_store_ex(&global_php_handlers[signo-1], + managed && zend_signal_entry_is_handler(&SIGG(handlers)[signo-1]) ? 1 : 0); + } +} +/* }}} */ +#endif static sigset_t global_sigmask; /* {{{ zend_signal_handler_defer @@ -88,9 +139,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_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)) { + return; + } /* Forward to default handler handler */ zend_signal_handler(signo, siginfo, context); return; @@ -181,9 +239,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]; @@ -212,9 +274,12 @@ 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 + zend_signal_count_handler(signo, true, false); +#endif } (*(void (*)(int, siginfo_t*, void*))p_sig.handler)(signo, siginfo, context); } else { @@ -238,6 +303,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; @@ -245,6 +314,10 @@ ZEND_API void zend_sigaction(int signo, const struct sigaction *act, struct siga SIGG(handlers)[signo-1].handler = (void *) act->sa_handler; } +#ifdef ZTS + zend_signal_count_handler(signo, was_handler, zend_signal_entry_is_handler(&SIGG(handlers)[signo-1])); +#endif + memset(&sa, 0, sizeof(sa)); if (SIGG(handlers)[signo-1].handler == (void *) SIG_IGN) { sa.sa_sigaction = (void *) SIG_IGN; @@ -294,12 +367,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; @@ -320,6 +400,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)) { @@ -387,6 +473,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; @@ -413,7 +511,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 @@ -446,3 +544,168 @@ 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. + * + * abort() raises SIGABRT on the calling thread, which Linux reports as + * SI_TKILL and FreeBSD as SI_LWP, while kill() reports SI_USER. The re-route + * never sends these codes: it sends SI_USER, or on Linux the code of the + * signal it re-routes. Re-routing abort()'s SIGABRT 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: +# 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. + * + * Linux keeps the original siginfo, such as the child of a SIGCHLD: a thread may + * send any siginfo to its own process with rt_sigqueueinfo() when it names + * itself, and the signal is still sent to the process. + * + * Limits: the signal mask of a thread the embedder owns changes; elsewhere 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, and if it has none, the signal takes its default action there, as + * it does when sent to that thread without a re-route; 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); +# if defined(__linux__) && defined(SYS_rt_sigqueueinfo) && defined(SYS_gettid) + if (!siginfo || syscall(SYS_rt_sigqueueinfo, (pid_t) syscall(SYS_gettid), signo, siginfo) != 0) +# endif + { + 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..60d88b1b682e 100644 --- a/Zend/zend_signal.h +++ b/Zend/zend_signal.h @@ -96,6 +96,12 @@ void zend_signal_init(void); ZEND_API void zend_signal(int signo, void (*handler)(int)); ZEND_API void zend_sigaction(int signo, const struct sigaction *act, struct sigaction *oldact); +# ifdef ZTS +ZEND_API void zend_signal_fork_child(void); +# else +# define zend_signal_fork_child() +# endif + #else /* ZEND_SIGNALS */ # define ZEND_SIGNAL_BLOCK_INTERRUPTIONS() @@ -105,10 +111,19 @@ ZEND_API void zend_sigaction(int signo, const struct sigaction *act, struct siga # define zend_signal_deactivate() # define zend_signal_startup() # define zend_signal_init() +# define zend_signal_fork_child() # define zend_signal(signo, handler) signal(signo, handler) # define zend_sigaction(signo, act, oldact) sigaction(signo, act, oldact) #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..08dda9782356 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -294,6 +294,7 @@ PHP_FUNCTION(pcntl_fork) } } else if (id == 0) { + zend_signal_fork_child(); zend_max_execution_timer_init(); php_clear_stat_cache(true, NULL, 0); } @@ -1297,7 +1298,48 @@ 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 + /* Take the default action here, as zend signals do: faults and abort() + * end the process on this thread. Not every fault happens again on + * return (a breakpoint, or a SIGSEGV that macOS reports as a fault), + * so raise it instead of relying on that, and leave the script's + * handler installed. The syscall that raised SIGSYS just fails. */ + if (zend_signal_is_synchronous(signo, info) +# ifdef SIGSYS + && signo != SIGSYS +# endif + ) { + struct sigaction sa, old; + sigset_t set; + + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = SIG_DFL; + sigemptyset(&sa.sa_mask); + sigemptyset(&set); + sigaddset(&set, signo); + if (sigaction(signo, &sa, &old) == 0) { + pthread_sigmask(SIG_UNBLOCK, &set, NULL); + raise(signo); + sigaction(signo, &old, 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; @@ -1582,6 +1624,7 @@ PHP_FUNCTION(pcntl_rfork) php_error_docref(NULL, E_WARNING, "Error %d", errno); } } else if (pid == 0) { + zend_signal_fork_child(); zend_max_execution_timer_init(); php_clear_stat_cache(true, NULL, 0); } @@ -1628,6 +1671,7 @@ PHP_FUNCTION(pcntl_forkx) php_error_docref(NULL, E_WARNING, "Error %d", errno); } } else if (pid == 0) { + zend_signal_fork_child(); zend_max_execution_timer_init(); php_clear_stat_cache(true, NULL, 0); } 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_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..981137acb126 --- /dev/null +++ b/ext/pcntl/tests/gh25541_abort_external.phpt @@ -0,0 +1,43 @@ +--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..9a9b099fc1ad --- /dev/null +++ b/ext/pcntl/tests/gh25541_abort_kill.phpt @@ -0,0 +1,32 @@ +--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/pcntl/tests/gh25541_blocked.phpt b/ext/pcntl/tests/gh25541_blocked.phpt new file mode 100644 index 000000000000..9c7531edba6b --- /dev/null +++ b/ext/pcntl/tests/gh25541_blocked.phpt @@ -0,0 +1,32 @@ +--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_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_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/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/pcntl/tests/gh25541_sent_segv.phpt b/ext/pcntl/tests/gh25541_sent_segv.phpt new file mode 100644 index 000000000000..6a7d9788880f --- /dev/null +++ b/ext/pcntl/tests/gh25541_sent_segv.phpt @@ -0,0 +1,35 @@ +--TEST-- +GH-25541 (SIGSEGV sent to a thread that never ran PHP is handled or kills the process, never dropped) +--EXTENSIONS-- +pcntl +posix +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +bool(true) diff --git a/ext/pcntl/tests/gh25541_sigchld.phpt b/ext/pcntl/tests/gh25541_sigchld.phpt new file mode 100644 index 000000000000..50d3e03a5ceb --- /dev/null +++ b/ext/pcntl/tests/gh25541_sigchld.phpt @@ -0,0 +1,46 @@ +--TEST-- +GH-25541 (SIGCHLD re-routed from a thread that never ran PHP keeps its siginfo on Linux) +--EXTENSIONS-- +pcntl +posix +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true) +int(7) diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index 2d106337c5cb..29a7c3a60e67 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,76 @@ 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); +} + +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; + 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) { return execute_data diff --git a/ext/zend_test/test.stub.php b/ext/zend_test/test.stub.php index 785390dd70cc..f840b74bb577 100644 --- a/ext/zend_test/test.stub.php +++ b/ext/zend_test/test.stub.php @@ -307,6 +307,14 @@ 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 {} + + function zend_test_abort_on_foreign_thread(): void {} + + function zend_test_start_foreign_thread(): 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..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: 20856900222b20ff71d57bf575ad7034ffc34bf4 */ + * 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() @@ -155,6 +155,17 @@ 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() + +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) ZEND_ARG_TYPE_INFO(1, array, IS_ARRAY, 0) ZEND_END_ARG_INFO() @@ -336,6 +347,11 @@ 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); +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); static ZEND_FUNCTION(zend_test_create_throwing_resource); static ZEND_FUNCTION(get_open_basedir); @@ -452,6 +468,11 @@ 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) + 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) ZEND_FE(zend_test_create_throwing_resource, arginfo_zend_test_create_throwing_resource) ZEND_FE(get_open_basedir, arginfo_get_open_basedir)