Skip to content

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

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

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
Contributor

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.

@iliaal iliaal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a NEWS entry for PHP-8.6.

try {
map_int();
} catch (TypeError $e) {
echo $e->getMessage(), "\n";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: please print the class too (echo $e::class, ": ", $e->getMessage(), "\n";) so the test pins the TypeError.

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.
@pawell67
pawell67 force-pushed the fix-type-assert-const-operand branch from 240713f to 88f6fe3 Compare December 2, 2026 07:40
@pawell67

pawell67 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Drafted with the help of an AI assistant (Claude), quoted per CONTRIBUTING.md:

Thanks! Added the NEWS entry (under 8.6.2, since 8.6.1RC1 was already branched), the test now prints the exception class, and I rebased onto current PHP-8.6. The test still fails there without the fix.

@iliaal iliaal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM.

@iliaal iliaal closed this in 2dbb28e Dec 2, 2026
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.

2 participants