Rewrite zend_eval_const_expr to use a stack on the heap instead of recursion where possible?
Description
This is the same as https://bugs.php.net/bug.php?id=79307 with a proposed solution for ZEND_BINARY_OP. It was brought up in https://github.com/php/php-src/pull/7940 . Filing this approach to track refactoring zend_eval_const_expr (or the reason why we don't do this) for future reference.
This would help with binary operators, but not with ast kinds that call different recursive methods, e.g. ZEND_AST_ARRAY
The following code:
php > $x = eval('return ' . var_export(str_repeat("\0", 100000), true) . ';');
Resulted in this output:
[1] 16259 segmentation fault (core dumped) php -a
But I expected this output instead:
$x is assigned.
An alternative approach would be to rewrite zend_eval_const_expr with a C stack (pointer, capacity, size) on the heap (with emalloc/erealloc) in cases where it calls itself. (https://softwareengineering.stackexchange.com/questions/279004/general-way-to-convert-a-loop-while-for-to-recursion-or-from-a-recursion-to-a)
I'm not sure if there'd be objections to that for readability/maintainability/performance, though, and it doesn't help with deeply nested arrays - the stack allocation can be avoided if nothing is pushed. @nikic are there reasons to not do that?
Could be addressed by converting concat into a list node with some special handling, not sure if that's worth the bother.
(Incomplete) Pseudocode is written as a comment alongside the unmodified original code.
static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
{
zend_ast *ast = *ast_ptr;
zval result;
if (!ast) {
return;
}
// Stack would be:
// 1. Child nodes to zend_eval_const_expr inside of type != ZEND_AST_ZVAL
// 2. State - whether this is starting or being completed (0, 1)
//
// Pseudocode
// node_state = UNPROCESSED;
// do {
// switch(ast->kind) {
// case ZEND_AST_BINARY_OP:
// if (node_state == UNPROCESSED) {
//
// push_stack(stack, ast->child[i], PROCESSED) // unless there's nothing needed and all are already ZEND_AST_ZVAL
// for child nodes needed where kind != ZEND_AST_ZVAL
// push_stack(stack, ast->child[i], UNPROCESSED)
// break; // break, unless there's nothing needed
// }
// finish evaluating this ast
// break;
// case OTHERS....:
// }
// ast_ptr, node_state = pop_stack(stack, ast); // e.g. use low bit for node_state
// ast = *ast_ptr;
// }
switch (ast->kind) {
case ZEND_AST_BINARY_OP:
zend_eval_const_expr(&ast->child[0]);
zend_eval_const_expr(&ast->child[1]);
if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
return;
}
if (!zend_try_ct_eval_binary_op(&result, ast->attr,
zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]))
) {
return;
}
break;
PHP Version
8.1/any
Operating System
Linux Mint
I don't think this makes sense. If it's just a matter of dealing with "x" . "y" style cases, we can fold directly during parsing. If it's a matter of supporting long $x . $y chains, then we should add a concat list node.
If it's a matter of supporting long $x . $y chains, then we should add a concat list node.
That. The concat operator is likely the only binary operator where such super-deep nesting might happen otherwise.
That. The concat operator is likely the only binary operator where such super-deep nesting might happen otherwise.
I expect that to be the case in real-world usage, other kinds/flags would only show up in obscure edge cases in fuzzing, or code generation/translation tools.
Changing expressions such as a . b . c . d to a 4-value AST_CONCAT_LIST (min size 2) seems straightforward, should be translatable to external projects such as php-parser (depending on target php version?) and php-ast (new version number for parse_code/parse_file)
The output of token_get_all wouldn't change, so existing code wouldn't be affected meaningfully. The line number of errors may need to be preserved for useful error messages for users, but it's available on the child nodes (and the compiler may already use the line of the child nodes, actually)
$x = 'a' .
'b' .
new stdClass() .
'c';
I don't think this makes sense. If it's just a matter of dealing with "x" . "y" style cases, we can fold directly during parsing. If it's a matter of supporting long $x . $y chains, then we should add a concat list node.
I missed the first part about folding during parsing. zend_compile_create_balanced_binary_op_tree in that PR could be refactored to act on the unbalanced tree instead, with a loop, or concatenate directly in a loop instead
Folding should probably also handle cases like return '$prefix . ' . var_export($value); where literals are mixed with non-literals (($prefix . 'literal') . 'literal2'), but opcache also optimizes consecutive concats, so that doesn't seem as urgent.
For assert output, is there a need to keep the original AST? Also, assert seems to ignore operator precedence and not add parentheses.
php > assert("a" . '' == 123);
Warning: assert(): assert('a' . '' == 123) failed in php shell code on line 1
php > assert('x='.PHP_VERSION == $var);
Warning: assert(): assert('x=' . PHP_VERSION == $var) failed in php shell code on line 1
Unrelatedly, assert isn't escaping null bytes in string literals
php > assert("\0" . '' == 123);
Warning: assert(): assert(' failed in php shell code on line 1