Skip to content
Open
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
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions Zend/tests/gh25548.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
GH-25548 (Execution timeout delivered to a thread that never ran PHP)
--EXTENSIONS--
zend_test
--SKIPIF--
<?php
if (!function_exists('zend_test_timeout_signal')) die('skip ZTS only');
if (zend_test_timeout_signal() === null) die('skip the execution timer is not process-wide');
if (!in_array(PHP_OS, ['Linux', 'Darwin', 'FreeBSD'], true)) die('skip signals are not re-routed on this platform');
?>
--FILE--
<?php
set_time_limit(30);
zend_test_raise_on_foreign_thread(zend_test_timeout_signal());
// Only the re-routed signal can end this loop: the timer itself fires after 30s.
$end = hrtime(true) + 3_000_000_000;
while (hrtime(true) < $end);
echo "no timeout\n";
?>
--EXPECTF--
Fatal error: Maximum execution time of 30 seconds exceeded in %s on line %d
20 changes: 20 additions & 0 deletions Zend/tests/gh25548_stale_timer.phpt
Original file line number Diff line number Diff line change
@@ -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--
<?php
if (!function_exists('zend_test_timeout_signal')) die('skip ZTS only');
if (zend_test_timeout_signal() === null) die('skip the execution timer is not process-wide');
if (!in_array(PHP_OS, ['Linux', 'Darwin', 'FreeBSD'], true)) die('skip signals are not re-routed on this platform');
?>
--FILE--
<?php
set_time_limit(60);
set_time_limit(0);
zend_test_raise_on_foreign_thread(zend_test_timeout_signal());
usleep(100000);
echo "survived\n";
?>
--EXPECT--
survived
3 changes: 3 additions & 0 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
44 changes: 44 additions & 0 deletions Zend/zend_execute_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading