Skip to content

Commit 0c7fc35

Browse files
committed
Merge branch 'PHP-8.2'
* PHP-8.2: [ci skip] NEWS [ci skip] NEWS fix: support for timeouts with ZTS on Linux (#10141)
2 parents ccc16b4 + ae845cc commit 0c7fc35

File tree

11 files changed

+253
-1
lines changed

11 files changed

+253
-1
lines changed

Zend/Zend.m4

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,28 @@ fi
302302
AC_MSG_CHECKING(whether to enable zend signal handling)
303303
AC_MSG_RESULT($ZEND_SIGNALS)
304304
305+
dnl Don't enable Zend Max Execution Timers by default until PHP 8.3 to not break the ABI
306+
AC_ARG_ENABLE([zend-max-execution-timers],
307+
[AS_HELP_STRING([--enable-zend-max-execution-timers],
308+
[whether to enable zend max execution timers])],
309+
[ZEND_MAX_EXECUTION_TIMERS=$enableval],
310+
[ZEND_MAX_EXECUTION_TIMERS='no'])
311+
312+
AS_CASE(["$host_alias"], [*linux*], [], [ZEND_MAX_EXECUTION_TIMERS='no'])
313+
314+
PHP_CHECK_FUNC(timer_create, rt)
315+
if test "$ac_cv_func_timer_create" != "yes"; then
316+
ZEND_MAX_EXECUTION_TIMERS='no'
317+
fi
318+
319+
if test "$ZEND_MAX_EXECUTION_TIMERS" = "yes"; then
320+
AC_DEFINE(ZEND_MAX_EXECUTION_TIMERS, 1, [Use zend max execution timers])
321+
CFLAGS="$CFLAGS -DZEND_MAX_EXECUTION_TIMERS"
322+
fi
323+
324+
AC_MSG_CHECKING(whether to enable zend max execution timers)
325+
AC_MSG_RESULT($ZEND_MAX_EXECUTION_TIMERS)
326+
305327
])
306328

307329
AC_ARG_ENABLE([gcc-global-regs],

Zend/zend.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "zend_observer.h"
3737
#include "zend_fibers.h"
3838
#include "zend_call_stack.h"
39+
#include "zend_max_execution_timer.h"
3940
#include "Optimizer/zend_optimizer.h"
4041

4142
static size_t global_map_ptr_last = 0;
@@ -802,6 +803,10 @@ static void executor_globals_ctor(zend_executor_globals *executor_globals) /* {{
802803
executor_globals->stack_limit = (void*)0;
803804
executor_globals->stack_base = (void*)0;
804805
#endif
806+
#ifdef ZEND_MAX_EXECUTION_TIMERS
807+
executor_globals->pid = 0;
808+
executor_globals->oldact = (struct sigaction){0};
809+
#endif
805810
}
806811
/* }}} */
807812

@@ -826,6 +831,7 @@ static void zend_new_thread_end_handler(THREAD_T thread_id) /* {{{ */
826831
#ifdef ZEND_CHECK_STACK_LIMIT
827832
zend_call_stack_init();
828833
#endif
834+
zend_max_execution_timer_init();
829835
}
830836
/* }}} */
831837
#endif
@@ -1607,6 +1613,18 @@ ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *
16071613
abort();
16081614
}
16091615

1616+
ZEND_API ZEND_COLD ZEND_NORETURN void zend_strerror_noreturn(int type, int errn, const char *message)
1617+
{
1618+
#ifdef HAVE_STR_ERROR_R
1619+
char buf[1024];
1620+
strerror_r(errn, buf, sizeof(buf));
1621+
#else
1622+
char *buf = strerror(errn);
1623+
#endif
1624+
1625+
zend_error_noreturn(type, "%s: %s (%d)", message, buf, errn);
1626+
}
1627+
16101628
ZEND_API ZEND_COLD void zend_error_zstr(int type, zend_string *message) {
16111629
zend_string *filename;
16121630
uint32_t lineno;

Zend/zend.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "zend_smart_string_public.h"
4141
#include "zend_signal.h"
4242
#include "zend_type_code.h"
43+
#include "zend_max_execution_timer.h"
4344

4445
#define zend_sprintf sprintf
4546

@@ -360,6 +361,9 @@ ZEND_API ZEND_COLD void zend_value_error(const char *format, ...) ZEND_ATTRIBUTE
360361

361362
ZEND_COLD void zenderror(const char *error);
362363

364+
/* For internal C errors */
365+
ZEND_API ZEND_COLD ZEND_NORETURN void zend_strerror_noreturn(int type, int errn, const char *message);
366+
363367
/* The following #define is used for code duality in PHP for Engine 1 & 2 */
364368
#define ZEND_STANDARD_CLASS_DEF_PTR zend_standard_class_def
365369
extern ZEND_API zend_class_entry *zend_standard_class_def;

Zend/zend_execute_API.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
#ifdef HAVE_UNISTD_H
4545
#include <unistd.h>
4646
#endif
47+
#ifdef ZEND_MAX_EXECUTION_TIMERS
48+
#include <sys/syscall.h>
49+
#endif
4750

4851
ZEND_API void (*zend_execute_ex)(zend_execute_data *execute_data);
4952
ZEND_API void (*zend_execute_internal)(zend_execute_data *execute_data, zval *return_value);
@@ -196,6 +199,7 @@ void init_executor(void) /* {{{ */
196199
EG(filename_override) = NULL;
197200
EG(lineno_override) = -1;
198201

202+
zend_max_execution_timer_init();
199203
zend_fiber_init();
200204
zend_weakrefs_init();
201205

@@ -413,6 +417,7 @@ void shutdown_executor(void) /* {{{ */
413417
zend_shutdown_executor_values(fast_shutdown);
414418

415419
zend_weakrefs_shutdown();
420+
zend_max_execution_timer_shutdown();
416421
zend_fiber_shutdown();
417422

418423
zend_try {
@@ -1367,8 +1372,35 @@ ZEND_API ZEND_NORETURN void ZEND_FASTCALL zend_timeout(void) /* {{{ */
13671372
/* }}} */
13681373

13691374
#ifndef ZEND_WIN32
1375+
# ifdef ZEND_MAX_EXECUTION_TIMERS
1376+
static void zend_timeout_handler(int dummy, siginfo_t *si, void *uc) /* {{{ */
1377+
{
1378+
#ifdef ZTS
1379+
if (!tsrm_is_managed_thread()) {
1380+
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");
1381+
1382+
return;
1383+
}
1384+
#endif
1385+
1386+
if (si->si_value.sival_ptr != &EG(max_execution_timer_timer)) {
1387+
#ifdef MAX_EXECUTION_TIMERS_DEBUG
1388+
fprintf(stderr, "Executing previous handler (if set) for unexpected signal SIGRTMIN received on thread %d\n", (pid_t) syscall(SYS_gettid));
1389+
#endif
1390+
1391+
if (EG(oldact).sa_sigaction) {
1392+
EG(oldact).sa_sigaction(dummy, si, uc);
1393+
1394+
return;
1395+
}
1396+
if (EG(oldact).sa_handler) EG(oldact).sa_handler(dummy);
1397+
1398+
return;
1399+
}
1400+
# else
13701401
static void zend_timeout_handler(int dummy) /* {{{ */
13711402
{
1403+
# endif
13721404
#ifdef ZTS
13731405
if (!tsrm_is_managed_thread()) {
13741406
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");
@@ -1474,6 +1506,21 @@ static void zend_set_timeout_ex(zend_long seconds, bool reset_signals) /* {{{ */
14741506
zend_error_noreturn(E_ERROR, "Could not queue new timer");
14751507
return;
14761508
}
1509+
#elif defined(ZEND_MAX_EXECUTION_TIMERS)
1510+
zend_max_execution_timer_settime(seconds);
1511+
1512+
if (reset_signals) {
1513+
sigset_t sigset;
1514+
struct sigaction act;
1515+
1516+
act.sa_sigaction = zend_timeout_handler;
1517+
sigemptyset(&act.sa_mask);
1518+
act.sa_flags = SA_ONSTACK | SA_SIGINFO;
1519+
sigaction(SIGRTMIN, &act, NULL);
1520+
sigemptyset(&sigset);
1521+
sigaddset(&sigset, SIGRTMIN);
1522+
sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1523+
}
14771524
#elif defined(HAVE_SETITIMER)
14781525
{
14791526
struct itimerval t_r; /* timeout requested */
@@ -1539,6 +1586,8 @@ void zend_unset_timeout(void) /* {{{ */
15391586
}
15401587
tq_timer = NULL;
15411588
}
1589+
#elif ZEND_MAX_EXECUTION_TIMERS
1590+
zend_max_execution_timer_settime(0);
15421591
#elif defined(HAVE_SETITIMER)
15431592
if (EG(timeout_seconds)) {
15441593
struct itimerval no_timeout;

Zend/zend_globals.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <setjmp.h>
2525
#include <stdint.h>
26+
#include <sys/types.h>
2627

2728
#include "zend_globals_macros.h"
2829

@@ -39,6 +40,7 @@
3940
#include "zend_multiply.h"
4041
#include "zend_arena.h"
4142
#include "zend_call_stack.h"
43+
#include "zend_max_execution_timer.h"
4244

4345
/* Define ZTS if you want a thread-safe Zend */
4446
/*#undef ZTS*/
@@ -294,6 +296,12 @@ struct _zend_executor_globals {
294296
zend_ulong reserved_stack_size;
295297
#endif
296298

299+
#ifdef ZEND_MAX_EXECUTION_TIMERS
300+
timer_t max_execution_timer_timer;
301+
pid_t pid;
302+
struct sigaction oldact;
303+
#endif
304+
297305
void *reserved[ZEND_MAX_RESERVED_RESOURCES];
298306
};
299307

Zend/zend_max_execution_timer.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| [email protected] so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Author: Kévin Dunglas <[email protected]> |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#ifdef ZEND_MAX_EXECUTION_TIMERS
18+
19+
#include <stdio.h>
20+
#include <signal.h>
21+
#include <time.h>
22+
#include <unistd.h>
23+
#include <errno.h>
24+
#include <sys/syscall.h>
25+
#include <sys/types.h>
26+
27+
#include "zend.h"
28+
#include "zend_globals.h"
29+
30+
// Musl Libc defines this macro, glibc does not
31+
// According to "man 2 timer_create" this field should always be available, but it's not: https://sourceware.org/bugzilla/show_bug.cgi?id=27417
32+
# ifndef sigev_notify_thread_id
33+
# define sigev_notify_thread_id _sigev_un._tid
34+
# endif
35+
36+
ZEND_API void zend_max_execution_timer_init(void) /* {{{ */
37+
{
38+
struct sigevent sev;
39+
sev.sigev_notify = SIGEV_THREAD_ID;
40+
sev.sigev_value.sival_ptr = &EG(max_execution_timer_timer);
41+
sev.sigev_signo = SIGRTMIN;
42+
sev.sigev_notify_thread_id = (pid_t) syscall(SYS_gettid);
43+
44+
EG(pid) = getpid();
45+
// Measure wall time instead of CPU time as originally planned now that it is possible https://github.com/php/php-src/pull/6504#issuecomment-1370303727
46+
if (timer_create(CLOCK_BOOTTIME, &sev, &EG(max_execution_timer_timer)) != 0) {
47+
zend_strerror_noreturn(E_ERROR, errno, "Could not create timer");
48+
}
49+
50+
# ifdef MAX_EXECUTION_TIMERS_DEBUG
51+
fprintf(stderr, "Timer %#jx created on thread %d\n", (uintmax_t) EG(max_execution_timer_timer), sev.sigev_notify_thread_id);
52+
# endif
53+
54+
sigaction(sev.sigev_signo, NULL, &EG(oldact));
55+
}
56+
/* }}} */
57+
58+
void zend_max_execution_timer_settime(zend_long seconds) /* {{{ }*/
59+
{
60+
/* Timer not initialized or shutdown. */
61+
if (!EG(pid)) {
62+
return;
63+
}
64+
65+
timer_t timer = EG(max_execution_timer_timer);
66+
67+
struct itimerspec its;
68+
its.it_value.tv_sec = seconds;
69+
its.it_value.tv_nsec = its.it_interval.tv_sec = its.it_interval.tv_nsec = 0;
70+
71+
# ifdef MAX_EXECUTION_TIMERS_DEBUG
72+
fprintf(stderr, "Setting timer %#jx on thread %d (%ld seconds)...\n", (uintmax_t) timer, (pid_t) syscall(SYS_gettid), seconds);
73+
# endif
74+
75+
if (timer_settime(timer, 0, &its, NULL) != 0) {
76+
zend_strerror_noreturn(E_ERROR, errno, "Could not set timer");
77+
}
78+
}
79+
/* }}} */
80+
81+
void zend_max_execution_timer_shutdown(void) /* {{{ */
82+
{
83+
/* Don't try to delete a timer created before a call to fork() */
84+
if (EG(pid) != getpid()) {
85+
return;
86+
}
87+
88+
EG(pid) = 0;
89+
90+
timer_t timer = EG(max_execution_timer_timer);
91+
92+
# ifdef MAX_EXECUTION_TIMERS_DEBUG
93+
fprintf(stderr, "Deleting timer %#jx on thread %d...\n", (uintmax_t) timer, (pid_t) syscall(SYS_gettid));
94+
# endif
95+
96+
int err = timer_delete(timer);
97+
if (err != 0) {
98+
zend_strerror_noreturn(E_ERROR, errno, "Could not delete timer");
99+
}
100+
}
101+
/* }}}} */
102+
103+
#endif

Zend/zend_max_execution_timer.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| [email protected] so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Author: Kévin Dunglas <[email protected]> |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#ifndef ZEND_MAX_EXECUTION_TIMER_H
18+
#define ZEND_MAX_EXECUTION_TIMER_H
19+
20+
# ifdef ZEND_MAX_EXECUTION_TIMERS
21+
22+
#include "zend_long.h"
23+
24+
/* Must be called after calls to fork() */
25+
ZEND_API void zend_max_execution_timer_init(void);
26+
void zend_max_execution_timer_settime(zend_long seconds);
27+
void zend_max_execution_timer_shutdown(void);
28+
29+
# else
30+
31+
#define zend_max_execution_timer_init()
32+
#define zend_max_execution_timer_settime(seconds)
33+
#define zend_max_execution_timer_shutdown()
34+
35+
# endif
36+
#endif

configure.ac

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ asprintf \
636636
nanosleep \
637637
memmem \
638638
memrchr \
639+
strerror_r \
639640
)
640641

641642
AX_FUNC_WHICH_GETHOSTBYNAME_R
@@ -1722,7 +1723,7 @@ PHP_ADD_SOURCES(Zend, \
17221723
zend_virtual_cwd.c zend_ast.c zend_objects.c zend_object_handlers.c zend_objects_API.c \
17231724
zend_default_classes.c zend_inheritance.c zend_smart_str.c zend_cpuinfo.c zend_gdb.c \
17241725
zend_observer.c zend_system_id.c zend_enum.c zend_fibers.c zend_atomic.c \
1725-
zend_rc_debug.c \
1726+
zend_rc_debug.c zend_max_execution_timer.c \
17261727
Optimizer/zend_optimizer.c \
17271728
Optimizer/pass1.c \
17281729
Optimizer/pass3.c \

ext/pcntl/pcntl.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858

5959
#include "pcntl_arginfo.h"
6060

61+
#include "Zend/zend_max_execution_timer.h"
62+
6163
ZEND_DECLARE_MODULE_GLOBALS(pcntl)
6264
static PHP_GINIT_FUNCTION(pcntl);
6365

@@ -184,6 +186,8 @@ PHP_FUNCTION(pcntl_fork)
184186
if (id == -1) {
185187
PCNTL_G(last_error) = errno;
186188
php_error_docref(NULL, E_WARNING, "Error %d", errno);
189+
} else if (id == 0) {
190+
zend_max_execution_timer_init();
187191
}
188192

189193
RETURN_LONG((zend_long) id);

0 commit comments

Comments
 (0)