Skip to content

Guard AST conversion and teardown against C stack overflow - #265

Closed
iliaal wants to merge 1 commit into
nikic:masterfrom
iliaal:fix/ast-recursion-stack-limit
Closed

iliaal wants to merge 1 commit into
nikic:masterfrom
iliaal:fix/ast-recursion-stack-limit

Conversation

@iliaal

@iliaal iliaal commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Before

ast_to_zval() and ast_fill_children_ht() recursed without bound on the C call stack when converting deeply nested zend_ast trees, such as left-recursive expression chains ($a+1+1+..., $a->f()->f()...) that bypass Bison's YYMAXDEPTH, or right-deep chains. PHP_FUNCTION(parse_file) and PHP_FUNCTION(parse_code) also called recursive zend_ast_destroy(), which can overflow the stack during teardown on PHP 7.2 or on right-deep trees on PHP 7.3+.

After

  • Guarded ast_to_zval() with ast_check_stack_limit(), using zend_call_stack_overflowed(EG(stack_limit)) when ZEND_CHECK_STACK_LIMIT is defined (PHP 8.3+) and a fallback AST_MAX_RECURSION_DEPTH limit (256U) on older PHP versions or platforms without ZEND_CHECK_STACK_LIMIT.
  • Propagated int (SUCCESS/FAILURE) through ast_to_zval(), ast_fill_children_ht(), ast_create_virtual_node(), and ast_name_to_zval(), throwing Error and freeing partially constructed nodes cleanly on unwind.
  • Replaced recursive ZEND_AST_STMT_LIST flattening in ast_fill_children_ht() with iterative ast_fill_stmt_list_ht().
  • Replaced recursive zend_ast_destroy(ast) in parse_file() and parse_code() with iterative ast_destroy_ast(), delegating leaf and child-cleared declaration payload cleanup to zend_ast_destroy().

Scope rationale

  • Uses native Zend stack-limit checks on all actively supported PHP versions (8.3+) instead of rewriting all version-branched conversion logic in ast_to_zval() into a heap state machine.
  • Does not modify package.xml, matching repository convention for PRs.

Test plan

  • Added tests/deep_recursion.phpt covering left-deep binary expressions, method-call chains, right-deep ?? chains, parse_file(), deeply nested {...} statement lists, and post-error recovery (verified ASan stack-overflow failure on master)
  • Built with -Wall -Wextra -Wno-unused-parameter -Werror and passed full test suite on PHP 7.4 debug, 8.4 ASan NTS/ZTS, 8.5 debug/release, 8.5 fallback (-UZEND_CHECK_STACK_LIMIT), and 8.6 debug
  • Ran Valgrind (run-tests.php -m) on tests/deep_recursion.phpt, tests/stmt_list.phpt, tests/php81_first_class_callable_support.phpt, and tests/class.phpt: 4/4 passed, 0 leaks
  • Verified pecl package succeeds

ast_to_zval(), ast_fill_children_ht(), and zend_ast_destroy() recursed without bound on deeply nested ASTs (such as left-recursive expression chains that bypass Bison's YYMAXDEPTH or right-deep chains), overflowing the C call stack and crashing with SIGSEGV even on PHP 8.3+ with zend.max_allowed_stack_size configured.

Check zend_call_stack_overflowed(EG(stack_limit)) when ZEND_CHECK_STACK_LIMIT is available (PHP 8.3+), with a fallback AST_MAX_RECURSION_DEPTH of 256 levels on older PHP versions or platforms without ZEND_CHECK_STACK_LIMIT, throwing Error and unwinding partially constructed nodes cleanly on failure. Flatten nested ZEND_AST_STMT_LIST nodes iteratively in ast_fill_stmt_list_ht() and replace recursive zend_ast_destroy() calls in parse_file() and parse_code() with iterative ast_destroy_ast().
@nikic nikic closed this Nov 29, 2026
@nikic

nikic commented Nov 29, 2026

Copy link
Copy Markdown
Owner

Not interested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants