From 240713f26009c1d4e2d2c3778d7a05c56835d731 Mon Sep 17 00:00:00 2001 From: pawell67 <20501059+pawell67@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:43:57 +0100 Subject: [PATCH] Optimizer: TYPE_ASSERT does not consume its operand 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. --- Zend/Optimizer/block_pass.c | 9 ++++- Zend/Optimizer/zend_optimizer.c | 10 ++++++ .../tests/opt/type_assert_const_operand.phpt | 35 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 ext/opcache/tests/opt/type_assert_const_operand.phpt diff --git a/Zend/Optimizer/block_pass.c b/Zend/Optimizer/block_pass.c index 0601994479e6..4a1387a2c8bf 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 93706a8a0d4c..17e4acf77401 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 000000000000..e3c2f77ffb4e --- /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-- +getMessage(), "\n"; +} +?> +--EXPECT-- +array(2) { + [0]=> + int(1) + [1]=> + int(2) +} +array_map(): Argument #2 ($array) must be of type array, int given