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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ PHP NEWS
. Improved performance of integer to string conversion, used by string casts,
echo, implode(), json_encode(), serialize() and other places, by emitting
two digits per step. (Alexander Danilov)
. Zend max execution timers are now reused across requests on Linux
instead of being recreated for every request. (henderkes)

- Curl:
. Negative values for CURLOPT_POSTREDIR are now rejected, causing
Expand Down
3 changes: 3 additions & 0 deletions TSRM/TSRM_atexit.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ typedef struct _tsrm_tls_entry tsrm_tls_entry;

void tsrm_atexit_init(void);
void tsrm_atexit_set_data(tsrm_tls_entry *thread_resources);
#if !defined(TSRM_WIN32) && !defined(__APPLE__)
bool tsrm_atexit_is_registered(void);
#endif
void tsrm_atexit_disarm(void);
void tsrm_free_thread_at_exit(tsrm_tls_entry *thread_resources);

Expand Down
5 changes: 5 additions & 0 deletions TSRM/TSRM_atexit_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ void tsrm_atexit_set_data(tsrm_tls_entry *thread_resources)
pthread_setspecific(tsrm_exit_key, thread_resources);
}

bool tsrm_atexit_is_registered(void)
{
return tsrm_thread_exit_armed && pthread_getspecific(tsrm_exit_key) != NULL;
}

void tsrm_atexit_disarm(void)
{
if (!tsrm_thread_exit_armed) {
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,6 @@ static void executor_globals_ctor(zend_executor_globals *executor_globals) /* {{
#endif
#ifdef ZEND_MAX_EXECUTION_TIMERS
executor_globals->pid = 0;
executor_globals->oldact = (struct sigaction){0};
#endif
memset(executor_globals->strtod_state.freelist, 0,
sizeof(executor_globals->strtod_state.freelist));
Expand Down Expand Up @@ -1090,6 +1089,7 @@ void zend_startup(zend_utility_functions *utility_functions) /* {{{ */
zend_optimizer_startup();

#ifdef ZTS
zend_max_execution_timer_cache_startup();
tsrm_set_new_thread_end_handler(zend_new_thread_end_handler);
tsrm_set_thread_free_handler(zend_thread_free_handler);
tsrm_set_shutdown_handler(zend_interned_strings_dtor);
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -2595,6 +2595,7 @@ ZEND_API void zend_destroy_modules(void) /* {{{ */
free(module_request_startup_handlers);
module_request_startup_handlers = NULL;
zend_hash_graceful_reverse_destroy(&module_registry);
zend_max_execution_timer_cache_shutdown();
}
/* }}} */

Expand Down
27 changes: 22 additions & 5 deletions Zend/zend_execute_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,10 @@ ZEND_NORETURN ZEND_API void ZEND_FASTCALL zend_timeout(void) /* {{{ */

#ifndef ZEND_WIN32
# ifdef ZEND_MAX_EXECUTION_TIMERS
/* The handler we displaced is process-wide. A null sigaction selects the one-argument handler. */
static _Atomic(void (*)(int, siginfo_t *, void *)) previous_sigaction;
static _Atomic(void (*)(int)) previous_handler;

static void zend_timeout_handler(int dummy, siginfo_t *si, void *uc) /* {{{ */
{
#ifdef ZTS
Expand All @@ -1535,12 +1539,16 @@ static void zend_timeout_handler(int dummy, siginfo_t *si, void *uc) /* {{{ */
fprintf(stderr, "Executing previous handler (if set) for unexpected signal SIGRTMIN received on thread %d\n", (pid_t) syscall(SYS_gettid));
#endif

if (EG(oldact).sa_sigaction) {
EG(oldact).sa_sigaction(dummy, si, uc);
void (*action)(int, siginfo_t *, void *) = atomic_load(&previous_sigaction);
if (action) {
action(dummy, si, uc);

return;
}
if (EG(oldact).sa_handler) EG(oldact).sa_handler(dummy);
void (*handler)(int) = atomic_load(&previous_handler);
if (handler != SIG_IGN && handler != SIG_DFL) {
handler(dummy);
}

return;
}
Expand Down Expand Up @@ -1658,12 +1666,21 @@ static void zend_set_timeout_ex(zend_long seconds, bool reset_signals) /* {{{ */

if (reset_signals) {
sigset_t sigset;
struct sigaction act;
struct sigaction act, old;

act.sa_sigaction = zend_timeout_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_ONSTACK | SA_SIGINFO;
sigaction(SIGRTMIN, &act, NULL);
if (sigaction(SIGRTMIN, NULL, &old) == 0
&& !((old.sa_flags & SA_SIGINFO) && old.sa_sigaction == zend_timeout_handler)) {
if ((old.sa_flags & SA_SIGINFO) && old.sa_handler != SIG_DFL && old.sa_handler != SIG_IGN) {
atomic_store(&previous_sigaction, old.sa_sigaction);
} else {
atomic_store(&previous_handler, old.sa_handler);
atomic_store(&previous_sigaction, NULL);
}
sigaction(SIGRTMIN, &act, NULL);
}
sigemptyset(&sigset);
sigaddset(&sigset, SIGRTMIN);
sigprocmask(SIG_UNBLOCK, &sigset, NULL);
Expand Down
1 change: 0 additions & 1 deletion Zend/zend_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ struct _zend_executor_globals {
#ifdef ZEND_MAX_EXECUTION_TIMERS
timer_t max_execution_timer_timer;
pid_t pid;
struct sigaction oldact;
#endif

zend_strtod_state strtod_state;
Expand Down
89 changes: 87 additions & 2 deletions Zend/zend_max_execution_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#include "zend_globals.h"
#include "zend_portability.h"

#if defined(ZEND_MAX_EXECUTION_TIMER_CACHE) && defined(ZTS)
# include "TSRM/TSRM_atexit.h"
#endif

#if __has_feature(memory_sanitizer)
# include <sanitizer/msan_interface.h>
#endif
Expand All @@ -46,6 +50,56 @@
# define ZEND_MAX_EXECUTION_TIMERS_CLOCK CLOCK_BOOTTIME
# endif

#ifdef ZEND_MAX_EXECUTION_TIMER_CACHE
/* Owns the timer from creation until deletion, including during a request. */
typedef struct {
pid_t pid;
timer_t timer;
} zend_cached_timer;

static void timer_cache_dtor(void *ptr)
{
zend_cached_timer *cache = ptr;
if (cache->pid == getpid()) {
timer_delete(cache->timer);
}
cache->pid = 0;
}

# ifdef ZTS
static ts_rsrc_id timer_cache_id;
# define TIMER_CACHE (timer_cache_id ? TSRMG_BULK_STATIC(timer_cache_id, zend_cached_timer *) : NULL)
# define CAN_CACHE_TIMER() tsrm_atexit_is_registered()

static void timer_cache_ctor(void *ptr)
{
((zend_cached_timer *) ptr)->pid = 0;
}

void zend_max_execution_timer_cache_startup(void)
{
ts_allocate_id(&timer_cache_id, sizeof(zend_cached_timer), timer_cache_ctor, timer_cache_dtor);
}

# else
static zend_cached_timer process_timer_cache;
# define TIMER_CACHE (&process_timer_cache)
# define CAN_CACHE_TIMER() true
# endif

void zend_max_execution_timer_cache_shutdown(void)
{
# ifdef ZTS
if (timer_cache_id) {
ts_free_id(timer_cache_id);
timer_cache_id = 0;
}
# else
timer_cache_dtor(&process_timer_cache);
# endif
}
#endif

ZEND_API void zend_max_execution_timer_init(void) /* {{{ */
{
pid_t pid = getpid();
Expand All @@ -54,6 +108,18 @@ ZEND_API void zend_max_execution_timer_init(void) /* {{{ */
return;
}

#ifdef ZEND_MAX_EXECUTION_TIMER_CACHE
zend_cached_timer *cache = TIMER_CACHE;
if (cache && cache->pid == pid) {
EG(max_execution_timer_timer) = cache->timer;
EG(pid) = pid;
# ifdef MAX_EXECUTION_TIMERS_DEBUG
fprintf(stderr, "Timer %#jx reused on thread %d\n", (uintmax_t) EG(max_execution_timer_timer), (pid_t) syscall(SYS_gettid));
# endif
return;
}
#endif

struct sigevent sev;
sev.sigev_notify = SIGEV_THREAD_ID;
sev.sigev_value.sival_ptr = &EG(max_execution_timer_timer);
Expand All @@ -76,12 +142,15 @@ ZEND_API void zend_max_execution_timer_init(void) /* {{{ */
}

EG(pid) = pid;
#ifdef ZEND_MAX_EXECUTION_TIMER_CACHE
if (cache) {
*cache = (zend_cached_timer) {pid, EG(max_execution_timer_timer)};
}
#endif

# ifdef MAX_EXECUTION_TIMERS_DEBUG
fprintf(stderr, "Timer %#jx created on thread %d\n", (uintmax_t) EG(max_execution_timer_timer), sev.sigev_notify_thread_id);
# endif

sigaction(sev.sigev_signo, NULL, &EG(oldact));
}
/* }}} */

Expand Down Expand Up @@ -120,6 +189,22 @@ void zend_max_execution_timer_shutdown(void) /* {{{ */
return;
}

#ifdef ZEND_MAX_EXECUTION_TIMER_CACHE
static const struct itimerspec disarmed = {0};
zend_cached_timer *cache = TIMER_CACHE;
if (cache && cache->pid == EG(pid) && CAN_CACHE_TIMER()
&& timer_settime(EG(max_execution_timer_timer), 0, &disarmed, NULL) == 0) {
EG(pid) = 0;
# ifdef MAX_EXECUTION_TIMERS_DEBUG
fprintf(stderr, "Timer %#jx disarmed and cached on thread %d\n", (uintmax_t) cache->timer, (pid_t) syscall(SYS_gettid));
# endif
return;
}
if (cache) {
cache->pid = 0;
}
#endif

EG(pid) = 0;

timer_t timer = EG(max_execution_timer_timer);
Expand Down
16 changes: 16 additions & 0 deletions Zend/zend_max_execution_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

#include "zend_long.h"

/* Linux reuses each thread's timer across requests instead of recreating it. */
# ifdef __linux__
# define ZEND_MAX_EXECUTION_TIMER_CACHE
# endif

BEGIN_EXTERN_C()
/* Must be called after calls to fork() */
ZEND_API void zend_max_execution_timer_init(void);
Expand All @@ -33,4 +38,15 @@ void zend_max_execution_timer_shutdown(void);
#define zend_max_execution_timer_shutdown()

# endif

# ifdef ZEND_MAX_EXECUTION_TIMER_CACHE
# ifdef ZTS
void zend_max_execution_timer_cache_startup(void);
# endif
void zend_max_execution_timer_cache_shutdown(void);
# else
# define zend_max_execution_timer_cache_startup()
# define zend_max_execution_timer_cache_shutdown()
# endif

#endif
45 changes: 45 additions & 0 deletions sapi/cli/tests/max_execution_timer_reuse.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--TEST--
Zend max execution timers keep firing when reused across requests on one thread
--INI--
allow_url_fopen=1
--SKIPIF--
<?php
include "skipif.inc";
if (PHP_OS !== 'Linux') {
die("skip Linux only");
}
ob_start();
phpinfo(INFO_GENERAL);
if (!str_contains(ob_get_clean(), 'Zend Max Execution Timers => enabled')) {
die("skip Zend max execution timers are disabled");
}
?>
--FILE--
<?php
include "php_cli_server.inc";
php_cli_server_start(<<<'PHP'
set_time_limit(1);
if (isset($_GET['busy'])) {
while (true) {}
}
echo "ok\n";
PHP);

/* A broken timer would leave the busy loop spinning; fail instead of hanging. */
$context = stream_context_create(['http' => ['ignore_errors' => true, 'timeout' => 10]]);
foreach (['busy', 'idle', 'busy'] as $request) {
$query = $request === 'busy' ? '?busy=1' : '';
$body = @file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS . "/$query", false, $context);
if ($body === false) {
echo "$request: no response\n";
} elseif (preg_match('/Maximum execution time of 1 second exceeded/', $body)) {
echo "$request: timed out\n";
} else {
echo "$request: ", trim($body), "\n";
}
}
?>
--EXPECT--
busy: timed out
idle: ok
busy: timed out
Loading