From 15108d76b80c1009c6583cb26b27b1fd8388e501 Mon Sep 17 00:00:00 2001 From: Pratik Bhujel Date: Thu, 26 Nov 2026 18:14:51 +0545 Subject: [PATCH] Fix GH-25756: Do not fold IS_VAR QM_ASSIGN into ZEND_RETURN Since the VAR|TMP overhaul (GH-20628, 6173a9a1098), ZEND_RETURN only handles CONST|TMP|CV operands, and by-reference assignments (such as `return $a = &$b`) rely on ZEND_QM_ASSIGN to unwrap the IS_VAR reference into an IS_TMP_VAR. The block pass folded `T = QM_ASSIGN(X); RETURN(T)` into `RETURN(X)` without checking `src->op1_type != IS_VAR` (unlike the ZEND_JMPZ/ZEND_JMPZ_EX folds in the same pass), emitting an invalid `ZEND_RETURN` with an `IS_VAR` operand ("Invalid opcode 62/4/0"). Closes GH-25756 --- Zend/Optimizer/block_pass.c | 2 +- ext/opcache/tests/gh25756.phpt | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 ext/opcache/tests/gh25756.phpt diff --git a/Zend/Optimizer/block_pass.c b/Zend/Optimizer/block_pass.c index 0601994479e6..f790c3bf620d 100644 --- a/Zend/Optimizer/block_pass.c +++ b/Zend/Optimizer/block_pass.c @@ -910,7 +910,7 @@ static void zend_optimize_block(zend_basic_block *block, zend_op_array *op_array case ZEND_RETURN: if (opline->op1_type == IS_TMP_VAR) { src = VAR_SOURCE(opline->op1); - if (src && src->opcode == ZEND_QM_ASSIGN) { + if (src && src->opcode == ZEND_QM_ASSIGN && src->op1_type != IS_VAR) { zend_op *op = src + 1; bool optimize = true; diff --git a/ext/opcache/tests/gh25756.phpt b/ext/opcache/tests/gh25756.phpt new file mode 100644 index 000000000000..edf5ba028ffe --- /dev/null +++ b/ext/opcache/tests/gh25756.phpt @@ -0,0 +1,49 @@ +--TEST-- +GH-25756: OPcache "Invalid opcode 62/4/0" when assigning a reference in a return +--EXTENSIONS-- +opcache +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +--FILE-- + $variable_1 = &$variable_2; + return $fn(); +} + +function test_match_ref_assign() { + $variable_2 = 'match'; + return match (1) { + 1 => $variable_1 = &$variable_2, + }; +} + +function test_return_unwraps_ref(&$out) { + $out = 'before'; + return $local = &$out; +} + +echo test_return_ref_assign(), "\n"; +echo test_arrow_fn_ref_assign(), "\n"; +echo test_match_ref_assign(), "\n"; + +$ref = null; +$val = test_return_unwraps_ref($ref); +$ref = 'after'; +var_dump($val, $ref); + +?> +--EXPECT-- +success +arrow +match +string(6) "before" +string(5) "after"