[php-src] PR #19083: Fix GH-19065: Long match statement can segfault compiler during recursive SSA renaming
| From: | nielsdos | Date: | Wed, 09 Jul 2025 19:36:30 +0000 |
| Subject: | [php-src] PR #19083: Fix GH-19065: Long match statement can segfault compiler during recursive SSA renaming | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-32382@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/19083
Author: nielsdos
On some systems, like Alpine, the thread stack size is small by default. The last step of SSA
construction involves variable renaming that is recursive, and also makes copies of their version of
the renamed variables on the stack. This combination causes a stack overflow during compilation on
Alpine. Triggerable for example with very long match statements.
We previously ran into similar issues, where we also transformed to iterative algorithms, e.g. https://github.com/php/php-src/pull/14432
A stop-gap solution would be to use heap allocated arrays for the renamed variable list, but that
would only delay the error as increasing the number of match arms increases the depth of the
dominator tree, and will eventually run into the same issue.
Instead, this patch transforms the algorithm into an iterative one. There are two states stored in a
worklist stack: positive numbers indicate that the block still needs to undergo variable renaming.
Negative numbers indicate that the block and its dominated children are already renamed. Because 0
is also a valid block number, we bias the block numbers by adding 1.
To restore to the right variant when backtracking the "recursive" step, we index into an
array pointing to the different variable renaming variants.