Commit 2dbb28edfa0 for php
commit 2dbb28edfa061863274ca12bc53a0b984587eab7
Author: pawell67 <20501059+pawell67@users.noreply.github.com>
Date: Wed Dec 2 08:36:59 2026 +0100
Zend/Optimizer: Keep TYPE_ASSERT operand when propagating a constant
TYPE_ASSERT checks its operand and leaves it alive for a later use, such
as the FE_RESET_R of the loop emitted for array_map() by
zend_compile_func_array_map(). When the operand was found to be constant,
pass 1 and the block pass replaced only the TYPE_ASSERT operand and
removed its definition, leaving the later use without one: a segfault
with opcache, and an SSA integrity failure in debug builds, e.g. for
array_map(strlen(...), constant('PHP_EOL') ? ['a', 'bb'] : 1)
Keep looking for later uses after replacing the operand of TYPE_ASSERT.
Closes GH-25730
diff --git a/NEWS b/NEWS
index fcd9356f864..1f7daeae86f 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,10 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.6.2
+- Opcache:
+ . Fixed segfault when array_map() is called with a first-class callable and
+ an array that becomes constant only after optimization. (Pawel Wankiewicz)
+
03 Dec 2026, PHP 8.6.1RC1
diff --git a/Zend/Optimizer/block_pass.c b/Zend/Optimizer/block_pass.c
index f790c3bf620..b8f6b123e45 100644
--- a/Zend/Optimizer/block_pass.c
+++ b/Zend/Optimizer/block_pass.c
@@ -199,7 +199,14 @@ static void zend_optimize_block(zend_basic_block *block, zend_op_array *op_array
zval c;
ZVAL_COPY(&c, &ZEND_OP1_LITERAL(src));
- if (zend_optimizer_update_op2_const(op_array, opline, &c)) {
+ if (opline->opcode == ZEND_TYPE_ASSERT) {
+ /* Does not consume its operand, which is used again later. */
+ if (zend_optimizer_update_op2_const(op_array, opline, &c)) {
+ ++(*opt_count);
+ } else {
+ zval_ptr_dtor_nogc(&c);
+ }
+ } else if (zend_optimizer_update_op2_const(op_array, opline, &c)) {
VAR_SOURCE(op2) = NULL;
if (!zend_bitset_in(used_ext, VAR_NUM(src->result.var))) {
literal_dtor(&ZEND_OP1_LITERAL(src));
diff --git a/Zend/Optimizer/zend_optimizer.c b/Zend/Optimizer/zend_optimizer.c
index 93706a8a0d4..17e4acf7740 100644
--- a/Zend/Optimizer/zend_optimizer.c
+++ b/Zend/Optimizer/zend_optimizer.c
@@ -679,6 +679,16 @@ bool zend_optimizer_replace_by_const(zend_op_array *op_array,
if (opline->op2_type == type &&
opline->op2.var == var) {
+ if (opline->opcode == ZEND_TYPE_ASSERT) {
+ /* Does not consume its operand, which is used again later. */
+ Z_TRY_ADDREF_P(val);
+ if (!zend_optimizer_update_op2_const(op_array, opline, val)) {
+ zval_ptr_dtor(val);
+ return false;
+ }
+ opline++;
+ continue;
+ }
return zend_optimizer_update_op2_const(op_array, opline, val);
}
opline++;
diff --git a/ext/opcache/tests/opt/type_assert_const_operand.phpt b/ext/opcache/tests/opt/type_assert_const_operand.phpt
new file mode 100644
index 00000000000..ac7d00d73a4
--- /dev/null
+++ b/ext/opcache/tests/opt/type_assert_const_operand.phpt
@@ -0,0 +1,35 @@
+--TEST--
+TYPE_ASSERT does not consume its operand, which may be replaced by a constant
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.optimization_level=-1
+opcache.file_update_protection=0
+--EXTENSIONS--
+opcache
+--FILE--
+<?php
+/* The array_map() loop uses the array in TYPE_ASSERT and then in FE_RESET_R.
+ * The array is only known to be constant after optimization. */
+function map_array() {
+ return array_map(strlen(...), constant('PHP_EOL') ? ['a', 'bb'] : 1);
+}
+function map_int() {
+ return array_map(strlen(...), constant('PHP_EOL') ? 1 : []);
+}
+
+var_dump(map_array());
+try {
+ map_int();
+} catch (TypeError $e) {
+ echo $e::class, ": ", $e->getMessage(), "\n";
+}
+?>
+--EXPECT--
+array(2) {
+ [0]=>
+ int(1)
+ [1]=>
+ int(2)
+}
+TypeError: array_map(): Argument #2 ($array) must be of type array, int given