From b9fc2739c8f1857157e6d544ad3abd9f5f210253 Mon Sep 17 00:00:00 2001 From: Alex Dowad Date: Wed, 6 May 2020 22:02:57 +0200 Subject: [PATCH 1/2] Honor script time limit when calling shutdown functions A time limit can be set on PHP script execution via `set_time_limit` (or .ini file). When the time limit is reached, the OS will notify PHP and `timed_out` and `vm_interrupt` flags are set. While these flags are regularly checked when executing PHP code, once the end of the script is reached, they are not checked while invoking shutdown functions (registered via `register_shutdown_function`). Of course, if the shutdown functions are implemented *in* PHP, then the interrupt flag will be checked while the VM is running PHP bytecode and the timeout will take effect. But if the shutdown functions are built-in (implemented in C), it will not. Since the shutdown functions are invoked through `zend_call_function`, add a check of the `vm_interrupt` flag there. Then, the script time limit will be respected when *entering* each shutdown function. The fact still remains that if a shutdown function is built-in and runs for a long time, script execution will not time out until it finishes and the interpreter tries to invoke the next one. Still, the behavior of scripts with execution time limits will be more consistent after this patch. To make the execution time-out feature work even more precisely, it would be necessary to scrutinize all the built-in functions and add checks of the `vm_interrupt` flag in any which can run for a long time. That might not be worth the effort, though. It should be mentioned that this patch does not solely affect shutdown functions, neither does it solely allow for interruption of running code due to script execution timeout. Anything else which causes `vm_interrupt` to be set, such as the PHP interpreter receiving a signal, will take effect when exiting from an internal function. And not just internal functions which are called because they were registered to run at shutdown; there are other cases where a series of internal functions might run in the midst of a script. In all such cases, it will be possible to interrupt the interpreter now. --- Zend/zend_execute_API.c | 11 +++++++++++ tests/basic/timeout_variation_10.phpt | 3 +-- tests/basic/timeout_variation_9.phpt | 2 -- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c index e8b70840b7831..0c3917163d80d 100644 --- a/Zend/zend_execute_API.c +++ b/Zend/zend_execute_API.c @@ -830,6 +830,17 @@ int zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache) / /* We must re-initialize function again */ fci_cache->function_handler = NULL; } + + /* This flag is regularly checked while running user functions, but not internal + * So see whether interrupt flag was set while the function was running... */ + if (EG(vm_interrupt)) { + EG(vm_interrupt) = 0; + if (EG(timed_out)) { + zend_timeout(); + } else if (zend_interrupt_function) { + zend_interrupt_function(EG(current_execute_data)); + } + } } zend_vm_stack_free_call_frame(call); diff --git a/tests/basic/timeout_variation_10.phpt b/tests/basic/timeout_variation_10.phpt index b067238db500e..7680c96adf781 100644 --- a/tests/basic/timeout_variation_10.phpt +++ b/tests/basic/timeout_variation_10.phpt @@ -5,8 +5,6 @@ Timeout within shutdown function, variation if (getenv("SKIP_SLOW_TESTS")) die("skip slow test"); if (PHP_OS_FAMILY !== "Windows") die("skip Windows only test"); ?> ---XFAIL-- -Missing timeout check in call_user_function --FILE-- ---XFAIL-- -Missing timeout check in call_user_function --FILE-- Date: Wed, 13 May 2020 10:13:28 +0200 Subject: [PATCH 2/2] Add pcntl signals test --- ext/pcntl/tests/async_signals_2.phpt | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 ext/pcntl/tests/async_signals_2.phpt diff --git a/ext/pcntl/tests/async_signals_2.phpt b/ext/pcntl/tests/async_signals_2.phpt new file mode 100644 index 0000000000000..be631bab5ef35 --- /dev/null +++ b/ext/pcntl/tests/async_signals_2.phpt @@ -0,0 +1,29 @@ +--TEST-- +Async signals in zend_call_function +--SKIPIF-- + +--FILE-- +getMessage(), "\n"; +} + +?> +--EXPECT-- +Alarm!