[php-src] PHP-8.5: Merge branch 'PHP-8.4' into PHP-8.5

From: Date: Mon, 02 Nov 2026 18:35:42 +0000
Subject: [php-src] PHP-8.5: Merge branch 'PHP-8.4' into PHP-8.5
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-143230@lists.php.net to get a copy of this message
Author: David Carlier (devnexen) Date: 2026-11-02T18:32:47Z Commit: https://github.com/php/php-src/commit/273f8dbe27579ae60a318694a3a20d94b4b8f502 Raw diff: https://github.com/php/php-src/commit/273f8dbe27579ae60a318694a3a20d94b4b8f502.diff Merge branch 'PHP-8.4' into PHP-8.5 * PHP-8.4: Fix GH-24480: finally block running twice when a VM interrupt throws in FAST_RET Changed paths: A Zend/tests/try/finally_interrupt_after_return.phpt A Zend/tests/try/finally_interrupt_after_return_generator.phpt A Zend/tests/try/finally_interrupt_after_return_nested.phpt M Zend/zend_vm_def.h M Zend/zend_vm_execute.h M ext/zend_test/observer.c M ext/zend_test/php_test.h M ext/zend_test/test.c Diff: diff --git a/Zend/tests/try/finally_interrupt_after_return.phpt b/Zend/tests/try/finally_interrupt_after_return.phpt new file mode 100644 index 000000000000..1dfd3db00590 --- /dev/null +++ b/Zend/tests/try/finally_interrupt_after_return.phpt @@ -0,0 +1,42 @@ +--TEST-- +VM interrupt throwing at the end of a finally block after "return" in the try block +--DESCRIPTION-- +"return" inside a try block runs the finally block with FAST_CALL, and FAST_RET +then jumps back to the RETURN, which is inside the try block. An interrupt that +throws as the finally block ends belongs to the finally block: it must not run +the finally block again, and it discards the pending return value. +--EXTENSIONS-- +zend_test +--INI-- +zend_test.observer.enabled=1 +zend_test.observer.show_output=0 +zend_test.observer.observe_function_names=release +zend_test.observer.set_vm_interrupt_on_end=1 +--FILE-- +<?php +$runs = 0; + +function release() { + global $runs; + $runs++; +} + +function test() { + try { + return str_repeat('a', 3); + } finally { + release(); + } +} + +try { + $result = test(); + echo "test() returned $result\n"; +} catch (Exception $e) { + echo get_class($e), ': ', $e->getMessage(), "\n"; +} +echo "finally ran $runs time(s)\n"; +?> +--EXPECT-- +Exception: VM interrupt +finally ran 1 time(s) diff --git a/Zend/tests/try/finally_interrupt_after_return_generator.phpt b/Zend/tests/try/finally_interrupt_after_return_generator.phpt new file mode 100644 index 000000000000..da5c64bae3c6 --- /dev/null +++ b/Zend/tests/try/finally_interrupt_after_return_generator.phpt @@ -0,0 +1,43 @@ +--TEST-- +VM interrupt throwing at the end of a generator's finally block after "return" in the try block +--EXTENSIONS-- +zend_test +--INI-- +zend_test.observer.enabled=1 +zend_test.observer.show_output=0 +zend_test.observer.observe_function_names=release +zend_test.observer.set_vm_interrupt_on_end=1 +--FILE-- +<?php +$runs = 0; + +function release() { + global $runs; + $runs++; +} + +function gen() { + try { + yield 1; + return str_repeat('a', 3); + } finally { + release(); + } +} + +$gen = gen(); +try { + echo "yielded ", $gen->current(), "\n"; + $gen->next(); + echo "gen() returned ", $gen->getReturn(), "\n"; +} catch (Exception $e) { + echo get_class($e), ': ', $e->getMessage(), "\n"; +} +var_dump($gen->valid()); +echo "finally ran $runs time(s)\n"; +?> +--EXPECT-- +yielded 1 +Exception: VM interrupt +bool(false) +finally ran 1 time(s) diff --git a/Zend/tests/try/finally_interrupt_after_return_nested.phpt b/Zend/tests/try/finally_interrupt_after_return_nested.phpt new file mode 100644 index 000000000000..f4a2d09c0771 --- /dev/null +++ b/Zend/tests/try/finally_interrupt_after_return_nested.phpt @@ -0,0 +1,47 @@ +--TEST-- +VM interrupt throwing at the end of nested finally blocks after "return" in the try block +--DESCRIPTION-- +The inner finally block's FAST_RET jumps to the outer FAST_CALL, which is inside +the inner try block. An interrupt that throws there must not run the inner +finally block again, but the outer finally block still runs. +--EXTENSIONS-- +zend_test +--INI-- +zend_test.observer.enabled=1 +zend_test.observer.show_output=0 +zend_test.observer.observe_function_names=release +zend_test.observer.set_vm_interrupt_on_end=1 +--FILE-- +<?php +$runs = []; + +function release($name) { + global $runs; + $runs[] = $name; +} + +function test() { + try { + try { + return str_repeat('a', 3); + } finally { + release('inner'); + } + } finally { + release('outer'); + } +} + +try { + $result = test(); + echo "test() returned $result\n"; +} catch (Exception $e) { + echo get_class($e), ': ', $e->getMessage(), "\n"; + echo 'previous: ', get_class($e->getPrevious()), ': ', $e->getPrevious()->getMessage(), "\n"; +} +echo implode(', ', $runs), "\n"; +?> +--EXPECT-- +Exception: VM interrupt +previous: Exception: VM interrupt +inner, outer diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index 12a6345d9671..0a96afdb3dcf 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -8131,7 +8131,9 @@ ZEND_VM_HELPER(zend_dispatch_try_catch_finally_helper, ANY, ANY, uint32_t try_ca Z_OPLINE_NUM_P(fast_call) = (uint32_t)-1; ZEND_VM_JMP_EX(&EX(func)->op_array.opcodes[try_catch->finally_op], 0); - } else if (op_num < try_catch->finally_end) { + } else if (op_num <= try_catch->finally_end) { + /* Exception thrown in the finally block, or at its FAST_RET + * (by an interrupt) */ zval *fast_call = EX_VAR(EX(func)->op_array.opcodes[try_catch->finally_end].op1.var); /* cleanup incomplete RETURN statement */ @@ -8211,7 +8213,7 @@ ZEND_VM_HANDLER(149, ZEND_HANDLE_EXCEPTION, ANY, ANY) /* further blocks will not be relevant... */ break; } - if (throw_op_num < try_catch->catch_op || throw_op_num < try_catch->finally_end) { + if (throw_op_num < try_catch->catch_op || throw_op_num <= try_catch->finally_end) { current_try_catch_offset = i; } } @@ -8672,7 +8674,13 @@ ZEND_VM_HANDLER(163, ZEND_FAST_RET, ANY, TRY_CATCH) if (Z_OPLINE_NUM_P(fast_call) != (uint32_t)-1) { const zend_op *fast_ret = EX(func)->op_array.opcodes + Z_OPLINE_NUM_P(fast_call); - ZEND_VM_JMP_EX(fast_ret + 1, 0); + /* Handle interrupts before jumping: fast_ret + 1 is inside the try + * block, so an exception thrown there would run the finally block + * again. An exception thrown at FAST_RET belongs to the finally + * block. */ + ZEND_VM_INTERRUPT_CHECK(); + ZEND_VM_SET_OPCODE_NO_INTERRUPT(fast_ret + 1); + ZEND_VM_CONTINUE(); } /* special case for unhandled exceptions */ diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index 8978eebca552..dd00d04f6bcb 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -3339,7 +3339,9 @@ static zend_never_inline ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV_ Z_OPLINE_NUM_P(fast_call) = (uint32_t)-1; ZEND_VM_JMP_EX(&EX(func)->op_array.opcodes[try_catch->finally_op], 0); - } else if (op_num < try_catch->finally_end) { + } else if (op_num <= try_catch->finally_end) { + /* Exception thrown in the finally block, or at its FAST_RET + * (by an interrupt) */ zval *fast_call = EX_VAR(EX(func)->op_array.opcodes[try_catch->finally_end].op1.var); /* cleanup incomplete RETURN statement */ @@ -3419,7 +3421,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_HANDLE_EXCEPT /* further blocks will not be relevant... */ break; } - if (throw_op_num < try_catch->catch_op || throw_op_num < try_catch->finally_end) { + if (throw_op_num < try_catch->catch_op || throw_op_num <= try_catch->finally_end) { current_try_catch_offset = i; } } @@ -3541,7 +3543,13 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_FAST_RET_SPEC if (Z_OPLINE_NUM_P(fast_call) != (uint32_t)-1) { const zend_op *fast_ret = EX(func)->op_array.opcodes + Z_OPLINE_NUM_P(fast_call); - ZEND_VM_JMP_EX(fast_ret + 1, 0); + /* Handle interrupts before jumping: fast_ret + 1 is inside the try + * block, so an exception thrown there would run the finally block + * again. An exception thrown at FAST_RET belongs to the finally + * block. */ + ZEND_VM_INTERRUPT_CHECK(); + ZEND_VM_SET_OPCODE_NO_INTERRUPT(fast_ret + 1); + ZEND_VM_CONTINUE(); } /* special case for unhandled exceptions */ @@ -59151,7 +59159,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_HANDLE_EXCEPTION_S /* further blocks will not be relevant... */ break; } - if (throw_op_num < try_catch->catch_op || throw_op_num < try_catch->finally_end) { + if (throw_op_num < try_catch->catch_op || throw_op_num <= try_catch->finally_end) { current_try_catch_offset = i; } } @@ -59273,7 +59281,13 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_FAST_RET_SPEC_TAIL if (Z_OPLINE_NUM_P(fast_call) != (uint32_t)-1) { const zend_op *fast_ret = EX(func)->op_array.opcodes + Z_OPLINE_NUM_P(fast_call); - ZEND_VM_JMP_EX(fast_ret + 1, 0); + /* Handle interrupts before jumping: fast_ret + 1 is inside the try + * block, so an exception thrown there would run the finally block + * again. An exception thrown at FAST_RET belongs to the finally + * block. */ + ZEND_VM_INTERRUPT_CHECK(); + ZEND_VM_SET_OPCODE_NO_INTERRUPT(fast_ret + 1); + ZEND_VM_CONTINUE(); } /* special case for unhandled exceptions */ @@ -112400,7 +112414,9 @@ static zend_never_inline ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV_EX z Z_OPLINE_NUM_P(fast_call) = (uint32_t)-1; ZEND_VM_JMP_EX(&EX(func)->op_array.opcodes[try_catch->finally_op], 0); - } else if (op_num < try_catch->finally_end) { + } else if (op_num <= try_catch->finally_end) { + /* Exception thrown in the finally block, or at its FAST_RET + * (by an interrupt) */ zval *fast_call = EX_VAR(EX(func)->op_array.opcodes[try_catch->finally_end].op1.var); /* cleanup incomplete RETURN statement */ diff --git a/ext/zend_test/observer.c b/ext/zend_test/observer.c index 6f3950e87fa6..98bbce214eea 100644 --- a/ext/zend_test/observer.c +++ b/ext/zend_test/observer.c @@ -22,6 +22,7 @@ #include "zend_smart_str.h" #include "ext/standard/php_var.h" #include "zend_generators.h" +#include "zend_exceptions.h" static zend_observer_fcall_handlers observer_fcall_init(zend_execute_data *execute_data); @@ -129,6 +130,11 @@ static void observer_end(zend_execute_data *execute_data, zval *retval) { assert_observer_opline(execute_data); + if (ZT_G(observer_set_vm_interrupt_on_end) && !EG(exception)) { + ZT_G(observer_throw_on_vm_interrupt) = true; + zend_atomic_bool_store_ex(&EG(vm_interrupt), true); + } + if (!ZT_G(observer_show_output)) { return; } @@ -158,6 +164,11 @@ static void zend_test_interrupt_function(zend_execute_data *execute_data) if (zend_test_prev_interrupt_function) { zend_test_prev_interrupt_function(execute_data); } + + if (ZT_G(observer_throw_on_vm_interrupt)) { + ZT_G(observer_throw_on_vm_interrupt) = false; + zend_throw_exception(NULL, "VM interrupt", 0); + } } static void observer_show_init(zend_function *fbc) @@ -377,6 +388,7 @@ PHP_INI_BEGIN() STD_PHP_INI_ENTRY("zend_test.observer.show_opcode_in_user_handler", "", PHP_INI_SYSTEM, OnUpdateString, observer_show_opcode_in_user_handler, zend_zend_test_globals, zend_test_globals) STD_PHP_INI_BOOLEAN("zend_test.observer.set_vm_interrupt_on_begin", "0", PHP_INI_SYSTEM, OnUpdateBool, observer_set_vm_interrupt_on_begin, zend_zend_test_globals, zend_test_globals) STD_PHP_INI_BOOLEAN("zend_test.observer.set_vm_interrupt_on_begin_without_callback", "0", PHP_INI_SYSTEM, OnUpdateBool, observer_set_vm_interrupt_on_begin_without_callback, zend_zend_test_globals, zend_test_globals) + STD_PHP_INI_BOOLEAN("zend_test.observer.set_vm_interrupt_on_end", "0", PHP_INI_SYSTEM, OnUpdateBool, observer_set_vm_interrupt_on_end, zend_zend_test_globals, zend_test_globals) STD_PHP_INI_BOOLEAN("zend_test.observer.fiber_init", "0", PHP_INI_SYSTEM, OnUpdateBool, observer_fiber_init, zend_zend_test_globals, zend_test_globals) STD_PHP_INI_BOOLEAN("zend_test.observer.fiber_switch", "0", PHP_INI_SYSTEM, OnUpdateBool, observer_fiber_switch, zend_zend_test_globals, zend_test_globals) STD_PHP_INI_BOOLEAN("zend_test.observer.fiber_destroy", "0", PHP_INI_SYSTEM, OnUpdateBool, observer_fiber_destroy, zend_zend_test_globals, zend_test_globals) @@ -419,7 +431,7 @@ void zend_test_observer_init(INIT_FUNC_ARGS) zend_execute_internal = zend_test_execute_internal; } - if (ZT_G(observer_set_vm_interrupt_on_begin)) { + if (ZT_G(observer_set_vm_interrupt_on_begin) || ZT_G(observer_set_vm_interrupt_on_end)) { zend_test_prev_interrupt_function = zend_interrupt_function; zend_interrupt_function = zend_test_interrupt_function; } diff --git a/ext/zend_test/php_test.h b/ext/zend_test/php_test.h index ee2e334fb55c..ea567169d255 100644 --- a/ext/zend_test/php_test.h +++ b/ext/zend_test/php_test.h @@ -47,6 +47,8 @@ ZEND_BEGIN_MODULE_GLOBALS(zend_test) char *observer_show_opcode_in_user_handler; int observer_set_vm_interrupt_on_begin; int observer_set_vm_interrupt_on_begin_without_callback; + int observer_set_vm_interrupt_on_end; + bool observer_throw_on_vm_interrupt; int observer_nesting_depth; int observer_fiber_init; int observer_fiber_switch; diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index 7ed311a5ddf5..b67815f11ed9 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -1806,6 +1806,7 @@ PHP_RINIT_FUNCTION(zend_test) ALLOC_HASHTABLE(ZT_G(global_weakmap)); zend_hash_init(ZT_G(global_weakmap), 8, NULL, ZVAL_PTR_DTOR, 0); ZT_G(observer_nesting_depth) = 0; + ZT_G(observer_throw_on_vm_interrupt) = false; zend_test_mm_custom_handlers_rinit(); if (ZT_G(rearm_vm_interrupt)) {

« previous php.cvs (#143230) next »