Skip to content

Commit 3703025

Browse files
committed
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1: [ci skip] NEWS fix: support for timeouts with ZTS on Linux (#10141)
2 parents 3142829 + 8f92a07 commit 3703025

File tree

11 files changed

+245
-0
lines changed

11 files changed

+245
-0
lines changed

Zend/Zend.m4

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,28 @@ fi
272272
AC_MSG_CHECKING(whether to enable zend signal handling)
273273
AC_MSG_RESULT($ZEND_SIGNALS)
274274
275+
dnl Don't enable Zend Max Execution Timers by default until PHP 8.3 to not break the ABI
276+
AC_ARG_ENABLE([zend-max-execution-timers],
277+
[AS_HELP_STRING([--enable-zend-max-execution-timers],
278+
[whether to enable zend max execution timers])],
279+
[ZEND_MAX_EXECUTION_TIMERS=$enableval],
280+
[ZEND_MAX_EXECUTION_TIMERS='no'])
281+
282+
AS_CASE(["$host_alias"], [*linux*], [], [ZEND_MAX_EXECUTION_TIMERS='no'])
283+
284+
PHP_CHECK_FUNC(timer_create, rt)
285+
if test "$ac_cv_func_timer_create" != "yes"; then
286+
ZEND_MAX_EXECUTION_TIMERS='no'
287+
fi
288+
289+
if test "$ZEND_MAX_EXECUTION_TIMERS" = "yes"; then
290+
AC_DEFINE(ZEND_MAX_EXECUTION_TIMERS, 1, [Use zend max execution timers])
291+
CFLAGS="$CFLAGS -DZEND_MAX_EXECUTION_TIMERS"
292+
fi
293+
294+
AC_MSG_CHECKING(whether to enable zend max execution timers)
295+
AC_MSG_RESULT($ZEND_MAX_EXECUTION_TIMERS)
296+
275297
])
276298

277299
AC_MSG_CHECKING(whether /dev/urandom exists)

Zend/zend.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "zend_attributes.h"
3636
#include "zend_observer.h"
3737
#include "zend_fibers.h"
38+
#include "zend_max_execution_timer.h"
3839
#include "Optimizer/zend_optimizer.h"
3940

4041
static size_t global_map_ptr_last = 0;
@@ -795,6 +796,10 @@ static void executor_globals_ctor(zend_executor_globals *executor_globals) /* {{
795796
executor_globals->record_errors = false;
796797
executor_globals->num_errors = 0;
797798
executor_globals->errors = NULL;
799+
#ifdef ZEND_MAX_EXECUTION_TIMERS
800+
executor_globals->pid = 0;
801+
executor_globals->oldact = (struct sigaction){0};
802+
#endif
798803
}
799804
/* }}} */
800805

@@ -816,6 +821,7 @@ static void zend_new_thread_end_handler(THREAD_T thread_id) /* {{{ */
816821
{
817822
zend_copy_ini_directives();
818823
zend_ini_refresh_caches(ZEND_INI_STAGE_STARTUP);
824+
zend_max_execution_timer_init();
819825
}
820826
/* }}} */
821827
#endif
@@ -1593,6 +1599,18 @@ ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *
15931599
abort();
15941600
}
15951601

1602+
ZEND_API ZEND_COLD ZEND_NORETURN void zend_strerror_noreturn(int type, int errn, const char *message)
1603+
{
1604+
#ifdef HAVE_STR_ERROR_R
1605+
char buf[1024];
1606+
strerror_r(errn, buf, sizeof(buf));
1607+
#else
1608+
char *buf = strerror(errn);
1609+
#endif
1610+
1611+
zend_error_noreturn(type, "%s: %s (%d)", message, buf, errn);
1612+
}
1613+
15961614
ZEND_API ZEND_COLD void zend_error_zstr(int type, zend_string *message) {
15971615
zend_string *filename;
15981616
uint32_t lineno;

Zend/zend.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "zend_smart_str_public.h"
4040
#include "zend_smart_string_public.h"
4141
#include "zend_signal.h"
42+
#include "zend_max_execution_timer.h"
4243

4344
#define zend_sprintf sprintf
4445

@@ -357,6 +358,9 @@ ZEND_API ZEND_COLD void zend_value_error(const char *format, ...) ZEND_ATTRIBUTE
357358

358359
ZEND_COLD void zenderror(const char *error);
359360

361+
/* For internal C errors */
362+
ZEND_API ZEND_COLD ZEND_NORETURN void zend_strerror_noreturn(int type, int errn, const char *message);
363+
360364
/* The following #define is used for code duality in PHP for Engine 1 & 2 */
361365
#define ZEND_STANDARD_CLASS_DEF_PTR zend_standard_class_def
362366
extern ZEND_API zend_class_entry *zend_standard_class_def;

Zend/zend_execute_API.c

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

4750
ZEND_API void (*zend_execute_ex)(zend_execute_data *execute_data);
4851
ZEND_API void (*zend_execute_internal)(zend_execute_data *execute_data, zval *return_value);
@@ -195,6 +198,7 @@ void init_executor(void) /* {{{ */
195198
EG(filename_override) = NULL;
196199
EG(lineno_override) = -1;
197200

201+
zend_max_execution_timer_init();
198202
zend_fiber_init();
199203
zend_weakrefs_init();
200204

@@ -412,6 +416,7 @@ void shutdown_executor(void) /* {{{ */
412416
zend_shutdown_executor_values(fast_shutdown);
413417

414418
zend_weakrefs_shutdown();
419+
zend_max_execution_timer_shutdown();
415420
zend_fiber_shutdown();
416421

417422
zend_try {
@@ -1359,8 +1364,27 @@ ZEND_API ZEND_NORETURN void ZEND_FASTCALL zend_timeout(void) /* {{{ */
13591364
/* }}} */
13601365

13611366
#ifndef ZEND_WIN32
1367+
# ifdef ZEND_MAX_EXECUTION_TIMERS
1368+
static void zend_timeout_handler(int dummy, siginfo_t *si, void *uc) /* {{{ */
1369+
{
1370+
if (si->si_value.sival_ptr != &EG(max_execution_timer_timer)) {
1371+
#ifdef MAX_EXECUTION_TIMERS_DEBUG
1372+
fprintf(stderr, "Executing previous handler (if set) for unexpected signal SIGRTMIN received on thread %d\n", (pid_t) syscall(SYS_gettid));
1373+
#endif
1374+
1375+
if (EG(oldact).sa_sigaction) {
1376+
EG(oldact).sa_sigaction(dummy, si, uc);
1377+
1378+
return;
1379+
}
1380+
if (EG(oldact).sa_handler) EG(oldact).sa_handler(dummy);
1381+
1382+
return;
1383+
}
1384+
# else
13621385
static void zend_timeout_handler(int dummy) /* {{{ */
13631386
{
1387+
# endif
13641388
#ifndef ZTS
13651389
if (zend_atomic_bool_load_ex(&EG(timed_out))) {
13661390
/* Die on hard timeout */
@@ -1460,6 +1484,21 @@ static void zend_set_timeout_ex(zend_long seconds, bool reset_signals) /* {{{ */
14601484
zend_error_noreturn(E_ERROR, "Could not queue new timer");
14611485
return;
14621486
}
1487+
#elif defined(ZEND_MAX_EXECUTION_TIMERS)
1488+
zend_max_execution_timer_settime(seconds);
1489+
1490+
if (reset_signals) {
1491+
sigset_t sigset;
1492+
struct sigaction act;
1493+
1494+
act.sa_sigaction = zend_timeout_handler;
1495+
sigemptyset(&act.sa_mask);
1496+
act.sa_flags = SA_ONSTACK | SA_SIGINFO;
1497+
sigaction(SIGRTMIN, &act, NULL);
1498+
sigemptyset(&sigset);
1499+
sigaddset(&sigset, SIGRTMIN);
1500+
sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1501+
}
14631502
#elif defined(HAVE_SETITIMER)
14641503
{
14651504
struct itimerval t_r; /* timeout requested */
@@ -1525,6 +1564,8 @@ void zend_unset_timeout(void) /* {{{ */
15251564
}
15261565
tq_timer = NULL;
15271566
}
1567+
#elif ZEND_MAX_EXECUTION_TIMERS
1568+
zend_max_execution_timer_settime(0);
15281569
#elif defined(HAVE_SETITIMER)
15291570
if (EG(timeout_seconds)) {
15301571
struct itimerval no_timeout;

Zend/zend_globals.h

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

2323

2424
#include <setjmp.h>
25+
#include <sys/types.h>
2526

2627
#include "zend_globals_macros.h"
2728

@@ -37,6 +38,7 @@
3738
#include "zend_multibyte.h"
3839
#include "zend_multiply.h"
3940
#include "zend_arena.h"
41+
#include "zend_max_execution_timer.h"
4042

4143
/* Define ZTS if you want a thread-safe Zend */
4244
/*#undef ZTS*/
@@ -271,6 +273,12 @@ struct _zend_executor_globals {
271273
zend_string *filename_override;
272274
zend_long lineno_override;
273275

276+
#ifdef ZEND_MAX_EXECUTION_TIMERS
277+
timer_t max_execution_timer_timer;
278+
pid_t pid;
279+
struct sigaction oldact;
280+
#endif
281+
274282
void *reserved[ZEND_MAX_RESERVED_RESOURCES];
275283
};
276284

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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ asprintf \
621621
nanosleep \
622622
memmem \
623623
memrchr \
624+
strerror_r \
624625
)
625626

626627
AX_FUNC_WHICH_GETHOSTBYNAME_R
@@ -1682,6 +1683,7 @@ PHP_ADD_SOURCES(Zend, \
16821683
zend_virtual_cwd.c zend_ast.c zend_objects.c zend_object_handlers.c zend_objects_API.c \
16831684
zend_default_classes.c zend_inheritance.c zend_smart_str.c zend_cpuinfo.c zend_gdb.c \
16841685
zend_observer.c zend_system_id.c zend_enum.c zend_fibers.c zend_atomic.c \
1686+
zend_max_execution_timer.c \
16851687
Optimizer/zend_optimizer.c \
16861688
Optimizer/pass1.c \
16871689
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);

ext/standard/info.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,12 @@ PHPAPI ZEND_COLD void php_print_info(int flag)
898898
efree(descr);
899899
}
900900

901+
#ifdef ZEND_MAX_EXECUTION_TIMERS
902+
php_info_print_table_row(2, "Zend Max Execution Timers", "enabled" );
903+
#else
904+
php_info_print_table_row(2, "Zend Max Execution Timers", "disabled" );
905+
#endif
906+
901907
#ifdef HAVE_IPV6
902908
php_info_print_table_row(2, "IPv6 Support", "enabled" );
903909
#else

0 commit comments

Comments
 (0)