Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ jobs:
with:
configurationParameters: >-
${{ matrix.asan && 'CFLAGS="-fsanitize=undefined,address -DZEND_TRACK_ARENA_ALLOC" LDFLAGS="-fsanitize=undefined,address"' || '' }}
${{ matrix.variation && '--disable-zend-signals' || '' }}
${{ matrix.variation && 'CFLAGS="-DZEND_RC_DEBUG=1 -DPROFITABILITY_CHECKS=0 -DZEND_VERIFY_FUNC_INFO=1 -DZEND_VERIFY_TYPE_INFERENCE"' || '' }}
${{ (matrix.variation && fromJson(inputs.branch).jobs.LINUX_X64.config.variation_enable_zend_max_execution_timers) && '--enable-zend-max-execution-timers' || '' }}
--${{ matrix.debug && 'enable' || 'disable' }}-debug
Expand Down
8 changes: 8 additions & 0 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,10 @@ void zend_startup(zend_utility_functions *utility_functions) /* {{{ */

zend_cpu_startup();

#if defined(ZTS) && !defined(ZEND_WIN32)
zend_signal_forward_startup();
#endif

#ifdef ZEND_WIN32
php_win32_cp_set_by_id(65001);
#endif
Expand Down Expand Up @@ -1160,6 +1164,10 @@ zend_result zend_post_startup(void) /* {{{ */

void zend_shutdown(void) /* {{{ */
{
#if defined(ZTS) && !defined(ZEND_WIN32)
zend_signal_forward_shutdown();
#endif

zend_vm_dtor();

zend_destroy_rsrc_list(&EG(persistent_list));
Expand Down
9 changes: 9 additions & 0 deletions Zend/zend_atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@

#undef ZEND_GCC_PREREQ

/* Whether these atomics are lock-free, and therefore usable in a signal handler. */
#if (defined(HAVE_C11_ATOMICS) || defined(HAVE_GNUC_ATOMICS)) \
&& __GCC_ATOMIC_INT_LOCK_FREE == 2 && __GCC_ATOMIC_BOOL_LOCK_FREE == 2
# define ZEND_ATOMIC_SIGNAL_SAFE 1
#elif defined(HAVE_SYNC_ATOMICS) \
&& defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
# define ZEND_ATOMIC_SIGNAL_SAFE 1
#endif

/* Treat zend_atomic_* types as opaque. They have definitions only for size
* and alignment purposes.
*/
Expand Down
97 changes: 83 additions & 14 deletions Zend/zend_execute_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -1475,8 +1475,6 @@ static void zend_timeout_handler(int dummy, siginfo_t *si, void *uc) /* {{{ */
{
#ifdef ZTS
if (!tsrm_is_managed_thread()) {
fprintf(stderr, "zend_timeout_handler() called in a thread not managed by PHP. The expected signal handler will not be called. This is probably a bug.\n");

return;
}
#endif
Expand All @@ -1500,11 +1498,15 @@ static void zend_timeout_handler(int dummy) /* {{{ */
{
# endif
#ifdef ZTS
if (!tsrm_is_managed_thread()) {
fprintf(stderr, "zend_timeout_handler() called in a thread not managed by PHP. The expected signal handler will not be called. This is probably a bug.\n");
/* The timer is process-wide, so its signal may reach a thread that did not arm it. */
if (!tsrm_is_managed_thread() || tsrm_is_shutdown()) {
zend_signal_forward(dummy);

return;
}
if (!EG(timeout_seconds) && zend_signal_forward(dummy)) {
return;
}
#else
if (zend_atomic_bool_load_ex(&EG(timed_out))) {
/* Die on hard timeout */
Expand Down Expand Up @@ -1576,6 +1578,34 @@ VOID CALLBACK tq_timer_cb(PVOID arg, BOOLEAN timed_out)
#define SIGPROF 27
#endif

#if defined(ZTS) && !defined(ZEND_WIN32) && !defined(ZEND_MAX_EXECUTION_TIMERS) && defined(HAVE_SETITIMER)
/* A handler installed with pcntl_signal(), for example, replaces the timeout handler. */
static bool zend_timeout_handler_is_installed(int signo)
{
# ifdef ZEND_SIGNALS
return SIGG(handlers)[signo-1].handler == (void *) zend_timeout_handler;
# else
struct sigaction current;

return sigaction(signo, NULL, &current) == 0 && current.sa_handler == zend_timeout_handler;
# endif
}

/* The process-wide disposition that delivers the signal of the execution timer. */
static void zend_timeout_timer_action(struct sigaction *act)
{
# ifdef ZEND_SIGNALS
zend_signal_timer_action(act);
# else
memset(act, 0, sizeof(*act));
act->sa_handler = zend_timeout_handler;
sigemptyset(&act->sa_mask);
/* Without SA_RESETHAND: a signal forwarded from another thread must still find it. */
act->sa_flags = SA_ONSTACK | SA_NODEFER;
# endif
}
#endif

static void zend_set_timeout_ex(zend_long seconds, bool reset_signals) /* {{{ */
{
#ifdef ZEND_WIN32
Expand Down Expand Up @@ -1625,26 +1655,21 @@ static void zend_set_timeout_ex(zend_long seconds, bool reset_signals) /* {{{ */
{
struct itimerval t_r; /* timeout requested */
int signo;
int timer;

// Prevent EINVAL error
if (seconds < 0 || seconds > 999999999) {
seconds = 0;
}

if(seconds) {
t_r.it_value.tv_sec = seconds;
t_r.it_value.tv_usec = t_r.it_interval.tv_sec = t_r.it_interval.tv_usec = 0;

# if defined(__CYGWIN__) || defined(__PASE__) || (defined(__aarch64__) && defined(__APPLE__))
// ITIMER_PROF is broken in Apple Silicon system with MacOS >= 14
// See https://openradar.appspot.com/radar?id=5583058442911744.
setitimer(ITIMER_REAL, &t_r, NULL);
}
// ITIMER_PROF is broken in Apple Silicon system with MacOS >= 14
// See https://openradar.appspot.com/radar?id=5583058442911744.
signo = SIGALRM;
timer = ITIMER_REAL;
# else
setitimer(ITIMER_PROF, &t_r, NULL);
}
signo = SIGPROF;
timer = ITIMER_PROF;
# endif

if (reset_signals) {
Expand All @@ -1655,18 +1680,59 @@ static void zend_set_timeout_ex(zend_long seconds, bool reset_signals) /* {{{ */
# ifdef HAVE_SIGACTION
struct sigaction act;

# ifdef ZTS
/* The disposition is process-wide, and another thread may handle this signal. */
if (!zend_timeout_handler_is_installed(signo)) {
zend_timeout_timer_action(&act);
zend_signal_forward_timer_install(signo, &act);
}
# else
act.sa_handler = zend_timeout_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_ONSTACK | SA_RESETHAND | SA_NODEFER;
sigaction(signo, &act, NULL);
# endif
# else
signal(signo, zend_timeout_handler);
# endif /* HAVE_SIGACTION */
sigemptyset(&sigset);
sigaddset(&sigset, signo);
# ifdef ZTS
tsrm_sigmask(SIG_UNBLOCK, &sigset, NULL);
# else
sigprocmask(SIG_UNBLOCK, &sigset, NULL);
# endif
# endif /* ZEND_SIGNALS */
}

# ifdef ZTS
/* A previous timer must not expire on behalf of the one that is about to start. */
if (seconds) {
memset(&t_r, 0, sizeof(t_r));
setitimer(timer, &t_r, NULL);
}

/* The timer is still running while another thread's handler is installed. */
# ifdef ZEND_SIGNALS
if (seconds && (zend_timeout_handler_is_installed(signo) || zend_signal_forward_has_handler(signo))) {
# else
if (seconds) {
# endif
struct sigaction timer_action;

zend_timeout_timer_action(&timer_action);
zend_signal_forward_timer_arm(signo, &timer_action);
} else {
zend_signal_forward_timer_disarm();
}
# endif

/* Last, as a signal must find its handler and its owner. */
if (seconds) {
t_r.it_value.tv_sec = seconds;
t_r.it_value.tv_usec = t_r.it_interval.tv_sec = t_r.it_interval.tv_usec = 0;
setitimer(timer, &t_r, NULL);
}
}
#endif /* HAVE_SETITIMER */
}
Expand Down Expand Up @@ -1709,6 +1775,9 @@ void zend_unset_timeout(void) /* {{{ */
setitimer(ITIMER_PROF, &no_timeout, NULL);
# endif
}
# ifdef ZTS
zend_signal_forward_timer_disarm();
# endif
#endif
zend_atomic_bool_store_ex(&EG(timed_out), false);
}
Expand Down
87 changes: 68 additions & 19 deletions Zend/zend_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ static const int zend_sigs[] = { TIMEOUT_SIG, SIGHUP, SIGINT, SIGQUIT, SIGTERM,
static zend_signal_entry_t global_orig_handlers[NSIG];
static sigset_t global_sigmask;

#ifdef ZTS
/* A handler installed before PHP started keeps receiving the signals delivered
* to threads that never ran PHP. */
static zend_always_inline bool zend_signal_may_forward(int signo)
{
return signo < NSIG
&& (global_orig_handlers[signo-1].handler == (void *) SIG_DFL
|| global_orig_handlers[signo-1].handler == (void *) SIG_IGN);
}
#endif

/* {{{ zend_signal_handler_defer
* Blocks signals if in critical section */
static void zend_signal_handler_defer(int signo, siginfo_t *siginfo, void *context)
Expand All @@ -90,11 +101,23 @@ static void zend_signal_handler_defer(int signo, siginfo_t *siginfo, void *conte
/* 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. */
if (tsrm_is_shutdown() || !tsrm_is_managed_thread()) {
if (!tsrm_is_managed_thread() || tsrm_is_shutdown()) {
if (zend_signal_may_forward(signo) && zend_signal_forward(signo)) {
errno = errno_save;
return;
}
/* Forward to default handler handler */
zend_signal_handler(signo, siginfo, context);
return;
}

/* Another PHP thread may handle a signal that this one does not. */
if (zend_signal_may_forward(signo)
&& (SIGG(handlers)[signo-1].handler == (void *) SIG_DFL || SIGG(handlers)[signo-1].handler == (void *) SIG_IGN)
&& zend_signal_forward(signo)) {
errno = errno_save;
return;
}
#endif

if (EXPECTED(SIGG(active))) {
Expand Down Expand Up @@ -182,7 +205,10 @@ static void zend_signal_handler(int signo, siginfo_t *siginfo, void *context)
sigset_t sigset;
zend_signal_entry_t p_sig;
#ifdef ZTS
if (tsrm_is_shutdown() || !tsrm_is_managed_thread()) {
/* Only a thread with TSRM storage may use SIGG() */
bool has_globals = tsrm_is_managed_thread() && !tsrm_is_shutdown();

if (!has_globals) {
p_sig = global_orig_handlers[signo-1];
} else
#endif
Expand All @@ -199,20 +225,17 @@ static void zend_signal_handler(int signo, siginfo_t *siginfo, void *context)
if (sigaction(signo, &sa, NULL) == 0) {
/* throw away any blocked signals */
zend_sigprocmask(SIG_UNBLOCK, &sigset, NULL);
#ifdef ZTS
# define RAISE_ERROR "raise() failed\n"
if (raise(signo) != 0) {
/* On some systems raise() fails with errno 3: No such process */
kill(getpid(), signo);
}
#else
/* Not raise(), which is pthread_kill() on macOS and not safe in a handler there */
kill(getpid(), signo);
#endif
}
}
} else if (p_sig.handler != SIG_IGN) {
if (p_sig.flags & SA_SIGINFO) {
#ifdef ZTS
if ((p_sig.flags & SA_RESETHAND) && has_globals) {
#else
if (p_sig.flags & SA_RESETHAND) {
#endif
SIGG(handlers)[signo-1].flags = 0;
SIGG(handlers)[signo-1].handler = SIG_DFL;
}
Expand All @@ -238,23 +261,34 @@ ZEND_API void zend_sigaction(int signo, const struct sigaction *act, struct siga
oldact->sa_mask = global_sigmask;
}
if (act != NULL) {
SIGG(handlers)[signo-1].flags = act->sa_flags;
if (act->sa_flags & SA_SIGINFO) {
SIGG(handlers)[signo-1].handler = (void *) act->sa_sigaction;
} else {
SIGG(handlers)[signo-1].handler = (void *) act->sa_handler;
}
void *handler = (act->sa_flags & SA_SIGINFO) ? (void *) act->sa_sigaction : (void *) act->sa_handler;
int sigaction_result;

memset(&sa, 0, sizeof(sa));
if (SIGG(handlers)[signo-1].handler == (void *) SIG_IGN) {
if (handler == (void *) SIG_IGN) {
sa.sa_sigaction = (void *) SIG_IGN;
} else {
sa.sa_flags = SA_ONSTACK | SA_SIGINFO | (act->sa_flags & SA_FLAGS_MASK);
sa.sa_sigaction = zend_signal_handler_defer;
sa.sa_mask = global_sigmask;
}

if (sigaction(signo, &sa, NULL) < 0) {
#ifdef ZTS
/* While the handler is still there: a signal forwarded here afterwards
* would take the default action. */
if (handler == (void *) SIG_DFL || handler == (void *) SIG_IGN) {
sigaction_result = zend_signal_forward_release(signo, &sa);
SIGG(handlers)[signo-1].flags = act->sa_flags;
SIGG(handlers)[signo-1].handler = handler;
} else
#endif
{
SIGG(handlers)[signo-1].flags = act->sa_flags;
SIGG(handlers)[signo-1].handler = handler;
sigaction_result = sigaction(signo, &sa, NULL);
}

if (sigaction_result < 0) {
zend_error_noreturn(E_ERROR, "Error installing signal handler for %d", signo);
}

Expand All @@ -266,6 +300,17 @@ ZEND_API void zend_sigaction(int signo, const struct sigaction *act, struct siga
}
/* }}} */

#ifdef ZTS
/* The process-wide disposition of a signal that zend_signal() handles for a thread. */
void zend_signal_timer_action(struct sigaction *act)
{
memset(act, 0, sizeof(*act));
act->sa_flags = SA_ONSTACK | SA_SIGINFO;
act->sa_sigaction = zend_signal_handler_defer;
act->sa_mask = global_sigmask;
}
#endif

/* {{{ zend_signal
* Register a signal handler that will be deferred in critical sections */
ZEND_API void zend_signal(int signo, void (*handler)(int))
Expand Down Expand Up @@ -393,9 +438,13 @@ void zend_signal_init(void) /* {{{ */
struct sigaction sa;

/* Save previously registered signal handlers into orig_handlers */
memset(&global_orig_handlers, 0, sizeof(global_orig_handlers));
for (signo = 1; signo < NSIG; ++signo) {
if (sigaction(signo, NULL, &sa) == 0) {
/* Left installed by an earlier startup in this process, which already
* saved the original handler. */
if ((sa.sa_flags & SA_SIGINFO) && sa.sa_sigaction == zend_signal_handler_defer) {
continue;
}
global_orig_handlers[signo-1].flags = sa.sa_flags;
if (sa.sa_flags & SA_SIGINFO) {
global_orig_handlers[signo-1].handler = (void *) sa.sa_sigaction;
Expand Down
5 changes: 5 additions & 0 deletions Zend/zend_signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#ifndef ZEND_SIGNAL_H
#define ZEND_SIGNAL_H

#include "zend_signal_forward.h"

#ifdef ZEND_SIGNALS

#include <signal.h>
Expand Down Expand Up @@ -94,6 +96,9 @@ END_EXTERN_C()
void zend_signal_init(void);

ZEND_API void zend_signal(int signo, void (*handler)(int));
#ifdef ZTS
void zend_signal_timer_action(struct sigaction *act);
#endif
ZEND_API void zend_sigaction(int signo, const struct sigaction *act, struct sigaction *oldact);

#else /* ZEND_SIGNALS */
Expand Down
Loading
Loading