Skip to content

Fix stack limit on OpenBSD threads other than the main one - #25635

Closed
osbre wants to merge 1 commit into
php:PHP-8.4from
osbre:openbsd-stack-base
Closed

osbre wants to merge 1 commit into
php:PHP-8.4from
osbre:openbsd-stack-base

Conversation

@osbre

@osbre osbre commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

On OpenBSD, pthread_stackseg_np() returns the top of the stack in ss_sp, not the bottom (lib/librthread/rthread_np.c):

sinfo->ss_sp = (char *)thread->stack->base +
    thread->stack->len;

zend_call_stack_get_openbsd_pthread() subtracts ss_size from it, so stack->base ends up one stack size below the real stack. The limit then sits below the stack and is never reached.

The main thread isn't affected. zend_call_stack_get_openbsd_vm() computes the same value libc returns for it (VM_PSSTRINGS rounded up to a page) and already uses it as the base.

A small test program on a thread with a 512 KB stack shows a local variable just below ss_sp:

thread: ss_sp=0x1bf1689000 ss_size=512 KB  &local=0x1bf1688f7c

So deep recursion on any thread but the main one crashes the process with SIGILL instead of throwing Error: Maximum call stack size of ... reached. I found this with FrankenPHP, which runs PHP in threads: one request recursing 1000 levels through array_map() took down the whole server. With this change it throws the Error, as the CLI already does.

The code came in with #11578. I didn't add a test: run-tests only uses the CLI, where PHP runs on the main thread, and CI has no OpenBSD job.

@osbre
osbre requested a review from arnaud-lb as a code owner November 20, 2026 20:03
@osbre
osbre changed the base branch from master to PHP-8.4 November 20, 2026 20:07
@osbre
osbre marked this pull request as draft November 20, 2026 20:08
Comment thread Zend/zend_call_stack.c
}

stack->base = (char *)ss.ss_sp - ss.ss_size;
stack->base = ss.ss_sp;

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.

unlikely indeed but only one machine/arch exception. I guess we can go with it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Looks like configure only defines ZEND_CHECK_STACK_LIMIT when the stack grows down (Zend.m4), so this code isn't built on hppa

@osbre
osbre marked this pull request as ready for review November 20, 2026 21:08

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

lgtm

@devnexen devnexen closed this in e7e5bb1 Nov 20, 2026
@devnexen

Copy link
Copy Markdown
Member

Thanks !

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