Skip to content

Fix segfault with a constant array in optimized array_map() - #25730

Open
pawell67 wants to merge 1 commit into
php:PHP-8.6from
pawell67:fix-type-assert-const-operand
Open

pawell67 wants to merge 1 commit into
php:PHP-8.6from
pawell67:fix-type-assert-const-operand

Conversation

@pawell67

@pawell67 pawell67 commented Nov 25, 2026 •

Copy link
Copy Markdown

The fix and this description were written with the help of an AI assistant (Claude), so the description is quoted below as requested in CONTRIBUTING.md. I have reviewed it and tested the fix.

With opcache, this segfaults on PHP-8.6 (also on 8.6.0) and master:

<?php
function f() {
    return array_map(strlen(...), constant('PHP_EOL') ? ['a', 'bb'] : 1);
}
var_dump(f());

Debug builds fail the SSA integrity check instead:

var op1 of 2 (ZEND_FE_RESET_R) does not use/def an ssa var

The loop emitted by zend_compile_func_array_map() uses the array TMP twice: as op2 of TYPE_ASSERT and as op1 of FE_RESET_R. TYPE_ASSERT does not consume its operand. When the optimizer finds that the TMP is constant (here, after constant('PHP_EOL') ? ... : ... is evaluated), zend_optimizer_replace_by_const() (pass 1) and the constant propagation of the block pass replace the TYPE_ASSERT operand, treat it as the only use, and remove the definition. FE_RESET_R is left with an undefined TMP.

This keeps looking for later uses after replacing the operand of TYPE_ASSERT, in both places. Both are needed: with only the pass 1 change, the block pass still removes the definition for the test case.

The test fails without the fix and passes with it. Zend/tests, ext/opcache/tests, ext/standard/tests/array and ext/spl/tests pass without opcache, with opcache and with the tracing JIT.

This is not covered by GH-25379 or by #25546, which keeps using the same TMP for TYPE_ASSERT and FE_RESET_R.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant