Skip to content

Fix stack limit fallback on FreeBSD threads - #25643

Closed
osbre wants to merge 1 commit into
php:PHP-8.4from
osbre:freebsd-thread-stack-default
Closed

osbre wants to merge 1 commit into
php:PHP-8.4from
osbre:freebsd-thread-stack-default

Conversation

@osbre

@osbre osbre commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

When configure doesn't find pthread_attr_getstack(), zend_call_stack_init() falls back to the current stack pointer plus zend_call_stack_default_size(), which is 4 MiB for every FreeBSD thread. But libthr gives new threads 2 MiB on 64-bit and 1 MiB on 32-bit (THR_STACK_DEFAULT), so the limit lies past the end of the stack. Deep recursion through internal functions then crashes with SIGSEGV instead of throwing Error: Maximum call stack size of ... reached.

This PR uses THR_STACK_DEFAULT for threads other than the main one, like the macOS branch does. The main thread keeps 4 MiB.

I hit this with FrankenPHP built against FreeBSD's lang/php8x ports: their ZTS builds miss pthread_attr_getstack() (a port bug, reported separately), and one request recursing through array_map() took down the whole server.

Tested on FreeBSD 15.1/arm64 with a ZTS build of PHP-8.4 configured with ac_cv_func_pthread_attr_getstack=no, under FrankenPHP: before, the server crashes; after, the request gets the Error and the server keeps serving. Builds that detect pthread_attr_getstack() and the main thread (the CLI) are unchanged, so there's no phpt.

libthr gives threads a 2 MiB stack on 64-bit (1 MiB on 32-bit), not
4 MiB, therefore the fallback limit was past the end of the stack.

@devnexen devnexen 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.

is consistent with other platforms (i.e. could also be concealed in zend_call_stack.c to avoid header inclusion such as pthread_np.h all over the place)

@devnexen devnexen closed this in 0d7a572 Nov 21, 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