diff --git a/NEWS b/NEWS index 01788956353b..e032b3544920 100644 --- a/NEWS +++ b/NEWS @@ -29,6 +29,8 @@ PHP NEWS . Fixed object_properties_load() adding integer keys to property tables, which made the properties unreadable and clone hit an assertion. (Nicolas Grekas) + . Fixed bug GH-25548 (Execution timeout delivered to a thread that never ran + PHP is lost and kills the process under ZTS). (phcorp) - Curl: . Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire) @@ -103,6 +105,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/gh25548.phpt b/Zend/tests/gh25548.phpt new file mode 100644 index 000000000000..eb0ffd1dd7b2 --- /dev/null +++ b/Zend/tests/gh25548.phpt @@ -0,0 +1,21 @@ +--TEST-- +GH-25548 (Execution timeout delivered to a thread that never ran PHP) +--EXTENSIONS-- +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +Fatal error: Maximum execution time of 30 seconds exceeded in %s on line %d diff --git a/Zend/tests/gh25548_stale_timer.phpt b/Zend/tests/gh25548_stale_timer.phpt new file mode 100644 index 000000000000..b25b95365f8e --- /dev/null +++ b/Zend/tests/gh25548_stale_timer.phpt @@ -0,0 +1,20 @@ +--TEST-- +GH-25548 (Signal of a disarmed execution timer delivered to a thread that never ran PHP) +--EXTENSIONS-- +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +survived 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_execute_API.c b/Zend/zend_execute_API.c index aeb68bdd6be5..21d3e96a1611 100644 --- a/Zend/zend_execute_API.c +++ b/Zend/zend_execute_API.c @@ -1505,6 +1505,13 @@ static void zend_timeout_handler(int dummy) /* {{{ */ return; } +# ifndef ZEND_MAX_EXECUTION_TIMERS + /* The timer is process-wide. If this thread has no timeout armed, the signal + * is stale (the timer was disarmed) or was meant for another thread. */ + if (!EG(timeout_seconds)) { + return; + } +# endif #else if (zend_atomic_bool_load_ex(&EG(timed_out))) { /* Die on hard timeout */ @@ -1554,6 +1561,27 @@ static void zend_timeout_handler(int dummy) /* {{{ */ /* }}} */ #endif +#if defined(ZTS) && !defined(ZEND_WIN32) && !defined(ZEND_MAX_EXECUTION_TIMERS) \ + && defined(HAVE_SETITIMER) && !defined(ZEND_SIGNALS) && defined(HAVE_SIGACTION) +# define ZEND_TIMEOUT_SIGNAL_REROUTE 1 +/* Whether the process-wide timer is armed. */ +static zend_atomic_bool zend_timer_armed; + +/* The timer signal may reach a thread that never ran PHP: send it on to a PHP + * thread while the timer is armed, and ignore it otherwise. */ +static void zend_timeout_signal_handler(int signo, siginfo_t *si, void *uc) /* {{{ */ +{ + if (!tsrm_is_managed_thread()) { + if (!zend_atomic_bool_load_ex(&zend_timer_armed) + || zend_signal_reroute_from_foreign_thread(signo, si, uc)) { + return; + } + } + zend_timeout_handler(signo); +} +/* }}} */ +#endif + #ifdef ZEND_WIN32 VOID CALLBACK tq_timer_cb(PVOID arg, BOOLEAN timed_out) { @@ -1646,6 +1674,11 @@ static void zend_set_timeout_ex(zend_long seconds, bool reset_signals) /* {{{ */ } signo = SIGPROF; # endif +# ifdef ZEND_TIMEOUT_SIGNAL_REROUTE + if (seconds) { + zend_atomic_bool_store_ex(&zend_timer_armed, true); + } +# endif if (reset_signals) { # ifdef ZEND_SIGNALS @@ -1655,9 +1688,17 @@ static void zend_set_timeout_ex(zend_long seconds, bool reset_signals) /* {{{ */ # ifdef HAVE_SIGACTION struct sigaction act; +# ifdef ZTS + /* No SA_RESETHAND: the disposition is process-wide, and a signal + * re-routed from another thread must still find the handler. */ + act.sa_sigaction = zend_timeout_signal_handler; + sigemptyset(&act.sa_mask); + act.sa_flags = SA_ONSTACK | SA_SIGINFO; +# else act.sa_handler = zend_timeout_handler; sigemptyset(&act.sa_mask); act.sa_flags = SA_ONSTACK | SA_RESETHAND | SA_NODEFER; +# endif sigaction(signo, &act, NULL); # else signal(signo, zend_timeout_handler); @@ -1707,6 +1748,9 @@ void zend_unset_timeout(void) /* {{{ */ setitimer(ITIMER_REAL, &no_timeout, NULL); # else setitimer(ITIMER_PROF, &no_timeout, NULL); +# endif +# ifdef ZEND_TIMEOUT_SIGNAL_REROUTE + zend_atomic_bool_store_ex(&zend_timer_armed, false); # endif } #endif diff --git a/Zend/zend_signal.c b/Zend/zend_signal.c index 83f8aa0852db..ebfc43c6393e 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,32 @@ 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))) { + } +} +#endif static sigset_t global_sigmask; /* {{{ zend_signal_handler_defer @@ -88,9 +120,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 +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]; @@ -212,9 +255,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 +284,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 +295,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 +348,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 +381,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 +454,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 +492,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 +525,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_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_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/zend_test/test.c b/ext/zend_test/test.c index 4bd96976a965..e7e1365a3229 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,69 @@ 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_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); +} + +/* The signal of the execution timer if it is process-wide, null otherwise. */ +static ZEND_FUNCTION(zend_test_timeout_signal) +{ + ZEND_PARSE_PARAMETERS_NONE(); + +#if defined(ZEND_MAX_EXECUTION_TIMERS) || !defined(HAVE_SETITIMER) + RETURN_NULL(); +#elif defined(__CYGWIN__) || defined(__PASE__) || (defined(__aarch64__) && defined(__APPLE__)) + RETURN_LONG(SIGALRM); +#else + RETURN_LONG(SIGPROF); +#endif +} +#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..ac498325c6af 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_start_foreign_thread(): void {} + + function zend_test_timeout_signal(): ?int {} +#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..f41319e6388d 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: 8d9bda09350e35211527847e5bc41c9b9f9ddbce */ 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,18 @@ 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_start_foreign_thread, 0, 0, IS_VOID, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_timeout_signal, 0, 0, IS_LONG, 1) +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 +348,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_start_foreign_thread); +static ZEND_FUNCTION(zend_test_timeout_signal); +#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 +469,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_start_foreign_thread, arginfo_zend_test_start_foreign_thread) + ZEND_FE(zend_test_timeout_signal, arginfo_zend_test_timeout_signal) +#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)