[php-src] PHP-8.5: Fix block pass dropping ZEND_FAST_CALL OP2 definition
| From: | Mrmaxmeier via ndossche | Date: | Wed, 28 Oct 2026 22:12:26 +0000 |
| Subject: | [php-src] PHP-8.5: Fix block pass dropping ZEND_FAST_CALL OP2 definition | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-142900@lists.php.net to get a copy of this message | ||
Author: Mrmaxmeier (Mrmaxmeier)
Committer: ndossche (ndossche)
Date: 2026-10-28T23:11:19+01:00
Commit: https://github.com/php/php-src/commit/139c84bc2da215820b83e439e328e8c8f069b80a
Raw diff: https://github.com/php/php-src/commit/139c84bc2da215820b83e439e328e8c8f069b80a.diff
Fix block pass dropping ZEND_FAST_CALL OP2 definition
For a return from inside a try block that has a finally, ZEND_FAST_CALL names
the temporary holding the pending return value in its OP2. A TMP normally has a
single consumer, but FAST_CALL reads this one without consuming it: the RETURN
behind the FAST_CALL is the consumer, and
zend_dispatch_try_catch_finally_helper() destroys the value through OP2 when
the finally block throws.
The block pass tracked only a single source per temporary and treated the
RETURN as its sole use, so constant propagation removed the QM_ASSIGN that
defines it:
```php
$x = 1;
try {
try {
return true ? 1.5 : 2.5;
} finally {
undef_fn();
}
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
```
The optimized op_array kept "FAST_CALL finally T3" with T3 no longer defined
anywhere, and unwinding through the finally destroyed an uninitialized
temporary.
Clear the recorded source for the OP2 operand of ZEND_FAST_CALL, the same way
the pass already does for the operands that ZEND_FETCH_LIST_R and friends keep
alive.
Assisted-By: Claude <noreply@anthropic.com>
Closes GH-23508.
Changed paths:
A ext/opcache/tests/opt/block_pass_fast_call_op2.phpt
M NEWS
M Zend/Optimizer/block_pass.c
Diff:
diff --git a/NEWS b/NEWS
index 2b7a26cb8bb7..c1c0c03b7117 100644
--- a/NEWS
+++ b/NEWS
@@ -169,6 +169,7 @@ PHP NEWS
non-string value. (jorgsowa)
. Fixed AArch64 JIT encoding of invalid logical immediates. (jorgsowa)
. Fixed unsound shift_left_overflows range when shift wraps. (mkrishna793)
+ . Fix block pass dropping ZEND_FAST_CALL OP2 definition. (Mrmaxmeier)
- OpenSSL:
. Fixed bug GH-24784 (openssl_pkcs7_read() and openssl_cms_read() overwrite
diff --git a/Zend/Optimizer/block_pass.c b/Zend/Optimizer/block_pass.c
index ee70d021f4a9..6e14b8ee76da 100644
--- a/Zend/Optimizer/block_pass.c
+++ b/Zend/Optimizer/block_pass.c
@@ -398,6 +398,18 @@ static void zend_optimize_block(zend_basic_block *block, zend_op_array
*op_array
}
break;
+ case ZEND_FAST_CALL:
+ if (opline->op2_type & (IS_TMP_VAR|IS_VAR)) {
+ /* OP2 holds the pending return value. A TMP normally has a single
+ * consumer, but FAST_CALL reads does not consume it:
+ * the RETURN behind it is the consumer, and
+ * zend_dispatch_try_catch_finally_helper() destroys the value
+ * through OP2 if the finally block throws. Propagating the source
+ * into the RETURN therefore must not remove it. */
+ Tsource[VAR_NUM(opline->op2.var)] = NULL;
+ }
+ break;
+
case ZEND_SWITCH_LONG:
case ZEND_SWITCH_STRING:
case ZEND_MATCH:
diff --git a/ext/opcache/tests/opt/block_pass_fast_call_op2.phpt
b/ext/opcache/tests/opt/block_pass_fast_call_op2.phpt
new file mode 100644
index 000000000000..a3222bcf4636
--- /dev/null
+++ b/ext/opcache/tests/opt/block_pass_fast_call_op2.phpt
@@ -0,0 +1,45 @@
+--TEST--
+Block pass must not remove the source of the ZEND_FAST_CALL OP2 return value
+--EXTENSIONS--
+opcache
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+--FILE--
+<?php
+function test_scalar() {
+ $x = 1;
+ try {
+ try {
+ return true ? 1.5 : 2.5;
+ } finally {
+ undef_fn();
+ }
+ } catch (Throwable $e) {
+ echo $e::class, ': ', $e->getMessage(), "\n";
+ }
+ return "fallback";
+}
+
+function test_refcounted() {
+ $x = 1;
+ try {
+ try {
+ return true ? "returned" : "other";
+ } finally {
+ undef_fn();
+ }
+ } catch (Throwable $e) {
+ echo $e::class, ': ', $e->getMessage(), "\n";
+ }
+ return "fallback";
+}
+
+var_dump(test_scalar());
+var_dump(test_refcounted());
+?>
+--EXPECT--
+Error: Call to undefined function undef_fn()
+string(8) "fallback"
+Error: Call to undefined function undef_fn()
+string(8) "fallback"