diff --git a/Zend/Zend.m4 b/Zend/Zend.m4 index 1a73cd6f8dca9..475355a78db13 100644 --- a/Zend/Zend.m4 +++ b/Zend/Zend.m4 @@ -269,6 +269,28 @@ fi AC_MSG_CHECKING(whether to enable zend signal handling) AC_MSG_RESULT($ZEND_SIGNALS) +dnl Don't enable Zend Max Execution Timers by default until PHP 8.3 to not break the ABI +AC_ARG_ENABLE([zend-max-execution-timers], + [AS_HELP_STRING([--enable-zend-max-execution-timers], + [whether to enable zend max execution timers])], + [ZEND_MAX_EXECUTION_TIMERS=$enableval], + [ZEND_MAX_EXECUTION_TIMERS='no']) + +AS_CASE(["$host_alias"], [*linux*], [], [ZEND_MAX_EXECUTION_TIMERS='no']) + +PHP_CHECK_FUNC(timer_create, rt) +if test "$ac_cv_func_timer_create" != "yes"; then + ZEND_MAX_EXECUTION_TIMERS='no' +fi + +if test "$ZEND_MAX_EXECUTION_TIMERS" = "yes"; then + AC_DEFINE(ZEND_MAX_EXECUTION_TIMERS, 1, [Use zend max execution timers]) + CFLAGS="$CFLAGS -DZEND_MAX_EXECUTION_TIMERS" +fi + +AC_MSG_CHECKING(whether to enable zend max execution timers) +AC_MSG_RESULT($ZEND_MAX_EXECUTION_TIMERS) + ]) AC_MSG_CHECKING(whether /dev/urandom exists) diff --git a/Zend/zend.c b/Zend/zend.c index be7fa210fff70..bd2a04e2ba9d8 100644 --- a/Zend/zend.c +++ b/Zend/zend.c @@ -35,6 +35,7 @@ #include "zend_attributes.h" #include "zend_observer.h" #include "zend_fibers.h" +#include "zend_max_execution_timer.h" #include "Optimizer/zend_optimizer.h" static size_t global_map_ptr_last = 0; @@ -800,6 +801,10 @@ static void executor_globals_ctor(zend_executor_globals *executor_globals) /* {{ executor_globals->record_errors = false; executor_globals->num_errors = 0; executor_globals->errors = NULL; +#ifdef ZEND_MAX_EXECUTION_TIMERS + executor_globals->pid = 0; + executor_globals->oldact = (struct sigaction){0}; +#endif } /* }}} */ @@ -821,6 +826,7 @@ static void zend_new_thread_end_handler(THREAD_T thread_id) /* {{{ */ { zend_copy_ini_directives(); zend_ini_refresh_caches(ZEND_INI_STAGE_STARTUP); + zend_max_execution_timer_init(); } /* }}} */ #endif @@ -1612,6 +1618,18 @@ ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char * abort(); } +ZEND_API ZEND_COLD ZEND_NORETURN void zend_strerror_noreturn(int type, int errn, const char *message) +{ +#ifdef HAVE_STR_ERROR_R + char buf[1024]; + strerror_r(errn, buf, sizeof(buf)); +#else + char *buf = strerror(errn); +#endif + + zend_error_noreturn(type, "%s: %s (%d)", message, buf, errn); +} + ZEND_API ZEND_COLD void zend_error_zstr(int type, zend_string *message) { zend_string *filename; uint32_t lineno; diff --git a/Zend/zend.h b/Zend/zend.h index 18e605c5bc27a..0b054b6477a53 100644 --- a/Zend/zend.h +++ b/Zend/zend.h @@ -39,6 +39,7 @@ #include "zend_smart_str_public.h" #include "zend_smart_string_public.h" #include "zend_signal.h" +#include "zend_max_execution_timer.h" #define zend_sprintf sprintf @@ -353,6 +354,9 @@ ZEND_API ZEND_COLD void zend_value_error(const char *format, ...) ZEND_ATTRIBUTE ZEND_COLD void zenderror(const char *error); +/* For internal C errors */ +ZEND_API ZEND_COLD ZEND_NORETURN void zend_strerror_noreturn(int type, int errn, const char *message); + /* The following #define is used for code duality in PHP for Engine 1 & 2 */ #define ZEND_STANDARD_CLASS_DEF_PTR zend_standard_class_def extern ZEND_API zend_class_entry *zend_standard_class_def; diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c index 3ef2dec1036c2..a85a11e77308c 100644 --- a/Zend/zend_execute_API.c +++ b/Zend/zend_execute_API.c @@ -43,6 +43,9 @@ #ifdef HAVE_UNISTD_H #include #endif +#ifdef ZEND_MAX_EXECUTION_TIMERS +#include +#endif ZEND_API void (*zend_execute_ex)(zend_execute_data *execute_data); ZEND_API void (*zend_execute_internal)(zend_execute_data *execute_data, zval *return_value); @@ -192,6 +195,7 @@ void init_executor(void) /* {{{ */ EG(num_errors) = 0; EG(errors) = NULL; + zend_max_execution_timer_init(); zend_fiber_init(); zend_weakrefs_init(); @@ -403,6 +407,7 @@ void shutdown_executor(void) /* {{{ */ zend_shutdown_executor_values(fast_shutdown); zend_weakrefs_shutdown(); + zend_max_execution_timer_shutdown(); zend_fiber_shutdown(); zend_try { @@ -1314,8 +1319,27 @@ ZEND_API ZEND_NORETURN void ZEND_FASTCALL zend_timeout(void) /* {{{ */ /* }}} */ #ifndef ZEND_WIN32 +# ifdef ZEND_MAX_EXECUTION_TIMERS +static void zend_timeout_handler(int dummy, siginfo_t *si, void *uc) /* {{{ */ +{ + if (si->si_value.sival_ptr != &EG(max_execution_timer_timer)) { +#ifdef MAX_EXECUTION_TIMERS_DEBUG + 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); + + return; + } + if (EG(oldact).sa_handler) EG(oldact).sa_handler(dummy); + + return; + } +# else static void zend_timeout_handler(int dummy) /* {{{ */ { +# endif #ifndef ZTS if (EG(timed_out)) { /* Die on hard timeout */ @@ -1415,6 +1439,21 @@ static void zend_set_timeout_ex(zend_long seconds, bool reset_signals) /* {{{ */ zend_error_noreturn(E_ERROR, "Could not queue new timer"); return; } +#elif defined(ZEND_MAX_EXECUTION_TIMERS) + zend_max_execution_timer_settime(seconds); + + if (reset_signals) { + sigset_t sigset; + struct sigaction act; + + act.sa_sigaction = zend_timeout_handler; + sigemptyset(&act.sa_mask); + act.sa_flags = SA_ONSTACK | SA_SIGINFO; + sigaction(SIGRTMIN, &act, NULL); + sigemptyset(&sigset); + sigaddset(&sigset, SIGRTMIN); + sigprocmask(SIG_UNBLOCK, &sigset, NULL); + } #elif defined(HAVE_SETITIMER) { struct itimerval t_r; /* timeout requested */ @@ -1480,6 +1519,8 @@ void zend_unset_timeout(void) /* {{{ */ } tq_timer = NULL; } +#elif ZEND_MAX_EXECUTION_TIMERS + zend_max_execution_timer_settime(0); #elif defined(HAVE_SETITIMER) if (EG(timeout_seconds)) { struct itimerval no_timeout; diff --git a/Zend/zend_globals.h b/Zend/zend_globals.h index 1726dee9ac12a..4612198b44ca5 100644 --- a/Zend/zend_globals.h +++ b/Zend/zend_globals.h @@ -22,6 +22,7 @@ #include +#include #include "zend_globals_macros.h" @@ -36,6 +37,7 @@ #include "zend_multibyte.h" #include "zend_multiply.h" #include "zend_arena.h" +#include "zend_max_execution_timer.h" /* Define ZTS if you want a thread-safe Zend */ /*#undef ZTS*/ @@ -266,6 +268,12 @@ struct _zend_executor_globals { uint32_t num_errors; zend_error_info **errors; +#ifdef ZEND_MAX_EXECUTION_TIMERS + timer_t max_execution_timer_timer; + pid_t pid; + struct sigaction oldact; +#endif + void *reserved[ZEND_MAX_RESERVED_RESOURCES]; }; diff --git a/Zend/zend_max_execution_timer.c b/Zend/zend_max_execution_timer.c new file mode 100644 index 0000000000000..b1c83e9cbb3a6 --- /dev/null +++ b/Zend/zend_max_execution_timer.c @@ -0,0 +1,103 @@ +/* + +----------------------------------------------------------------------+ + | Copyright (c) The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | https://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Kévin Dunglas | + +----------------------------------------------------------------------+ + */ + +#ifdef ZEND_MAX_EXECUTION_TIMERS + +#include +#include +#include +#include +#include +#include +#include + +#include "zend.h" +#include "zend_globals.h" + +// Musl Libc defines this macro, glibc does not +// 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 +# ifndef sigev_notify_thread_id +# define sigev_notify_thread_id _sigev_un._tid +# endif + +ZEND_API void zend_max_execution_timer_init(void) /* {{{ */ +{ + struct sigevent sev; + sev.sigev_notify = SIGEV_THREAD_ID; + sev.sigev_value.sival_ptr = &EG(max_execution_timer_timer); + sev.sigev_signo = SIGRTMIN; + sev.sigev_notify_thread_id = (pid_t) syscall(SYS_gettid); + + EG(pid) = getpid(); + // 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 + if (timer_create(CLOCK_BOOTTIME, &sev, &EG(max_execution_timer_timer)) != 0) { + zend_strerror_noreturn(E_ERROR, errno, "Could not create timer"); + } + +# 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)); +} +/* }}} */ + +void zend_max_execution_timer_settime(zend_long seconds) /* {{{ }*/ +{ + /* Timer not initialized or shutdown. */ + if (!EG(pid)) { + return; + } + + timer_t timer = EG(max_execution_timer_timer); + + struct itimerspec its; + its.it_value.tv_sec = seconds; + its.it_value.tv_nsec = its.it_interval.tv_sec = its.it_interval.tv_nsec = 0; + +# ifdef MAX_EXECUTION_TIMERS_DEBUG + fprintf(stderr, "Setting timer %#jx on thread %d (%ld seconds)...\n", (uintmax_t) timer, (pid_t) syscall(SYS_gettid), seconds); +# endif + + if (timer_settime(timer, 0, &its, NULL) != 0) { + zend_strerror_noreturn(E_ERROR, errno, "Could not set timer"); + } +} +/* }}} */ + +void zend_max_execution_timer_shutdown(void) /* {{{ */ +{ + /* Don't try to delete a timer created before a call to fork() */ + if (EG(pid) != getpid()) { + return; + } + + EG(pid) = 0; + + timer_t timer = EG(max_execution_timer_timer); + +# ifdef MAX_EXECUTION_TIMERS_DEBUG + fprintf(stderr, "Deleting timer %#jx on thread %d...\n", (uintmax_t) timer, (pid_t) syscall(SYS_gettid)); +# endif + + int err = timer_delete(timer); + if (err != 0) { + zend_strerror_noreturn(E_ERROR, errno, "Could not delete timer"); + } +} +/* }}}} */ + +#endif diff --git a/Zend/zend_max_execution_timer.h b/Zend/zend_max_execution_timer.h new file mode 100644 index 0000000000000..789f50e6b7dcd --- /dev/null +++ b/Zend/zend_max_execution_timer.h @@ -0,0 +1,36 @@ +/* + +----------------------------------------------------------------------+ + | Copyright (c) The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | https://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Kévin Dunglas | + +----------------------------------------------------------------------+ + */ + +#ifndef ZEND_MAX_EXECUTION_TIMER_H +#define ZEND_MAX_EXECUTION_TIMER_H + +# ifdef ZEND_MAX_EXECUTION_TIMERS + +#include "zend_long.h" + +/* Must be called after calls to fork() */ +ZEND_API void zend_max_execution_timer_init(void); +void zend_max_execution_timer_settime(zend_long seconds); +void zend_max_execution_timer_shutdown(void); + +# else + +#define zend_max_execution_timer_init() +#define zend_max_execution_timer_settime(seconds) +#define zend_max_execution_timer_shutdown() + +# endif +#endif diff --git a/configure.ac b/configure.ac index 9aa8c5caf2e2d..197c49a5fcfb0 100644 --- a/configure.ac +++ b/configure.ac @@ -609,6 +609,7 @@ vasprintf \ asprintf \ nanosleep \ memmem \ +strerror_r \ ) AX_FUNC_WHICH_GETHOSTBYNAME_R @@ -1633,7 +1634,7 @@ PHP_ADD_SOURCES(Zend, \ zend_closures.c zend_weakrefs.c zend_float.c zend_string.c zend_signal.c zend_generators.c \ zend_virtual_cwd.c zend_ast.c zend_objects.c zend_object_handlers.c zend_objects_API.c \ zend_default_classes.c zend_inheritance.c zend_smart_str.c zend_cpuinfo.c zend_gdb.c \ - zend_observer.c zend_system_id.c zend_enum.c zend_fibers.c \ + zend_observer.c zend_system_id.c zend_enum.c zend_fibers.c zend_max_execution_timer.c \ Optimizer/zend_optimizer.c \ Optimizer/pass1.c \ Optimizer/pass3.c \ diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 2ed577af5e341..c691d32239e36 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -50,6 +50,8 @@ # define NSIG 32 #endif +#include "Zend/zend_max_execution_timer.h" + ZEND_DECLARE_MODULE_GLOBALS(pcntl) static PHP_GINIT_FUNCTION(pcntl); @@ -531,6 +533,8 @@ PHP_FUNCTION(pcntl_fork) if (id == -1) { PCNTL_G(last_error) = errno; php_error_docref(NULL, E_WARNING, "Error %d", errno); + } else if (id == 0) { + zend_max_execution_timer_init(); } RETURN_LONG((zend_long) id); diff --git a/ext/standard/info.c b/ext/standard/info.c index 651bf3ae3f6d7..58e2eee6d49cb 100644 --- a/ext/standard/info.c +++ b/ext/standard/info.c @@ -891,6 +891,12 @@ PHPAPI ZEND_COLD void php_print_info(int flag) efree(descr); } +#ifdef ZEND_MAX_EXECUTION_TIMERS + php_info_print_table_row(2, "Zend Max Execution Timers", "enabled" ); +#else + php_info_print_table_row(2, "Zend Max Execution Timers", "disabled" ); +#endif + #if HAVE_IPV6 php_info_print_table_row(2, "IPv6 Support", "enabled" ); #else diff --git a/ext/standard/tests/general_functions/phpinfo.phpt b/ext/standard/tests/general_functions/phpinfo.phpt index 5f4d99ffbaf2e..57ba13bb7bf7c 100644 --- a/ext/standard/tests/general_functions/phpinfo.phpt +++ b/ext/standard/tests/general_functions/phpinfo.phpt @@ -34,6 +34,7 @@ Thread Safety => %s%A Zend Signal Handling => %s Zend Memory Manager => %s Zend Multibyte Support => %s +Zend Max Execution Timers => %s IPv6 Support => %s DTrace Support => %s