Skip to content

Commit 09f1756

Browse files
committed
zend call stack, follow-up on 75e9980.
user stack usable implementation for openbsd.
1 parent c6b9db2 commit 09f1756

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

Zend/zend_call_stack.c

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#endif
4747
#ifdef __OpenBSD__
4848
# include <pthread_np.h>
49+
# include <machine/vmparam.h>
4950
#endif
5051
#ifdef __linux__
5152
#include <sys/syscall.h>
@@ -435,8 +436,9 @@ static bool zend_call_stack_get_macos(zend_call_stack *stack)
435436
}
436437
#endif /* defined(__APPLE__) && defined(HAVE_PTHREAD_GET_STACKADDR_NP) */
437438

439+
#if defined(__OpenBSD__)
438440
#if defined(HAVE_PTHREAD_STACKSEG_NP)
439-
static bool zend_call_stack_get_openbsd(zend_call_stack *stack)
441+
static bool zend_call_stack_get_openbsd_pthread(zend_call_stack *stack)
440442
{
441443
stack_t ss;
442444

@@ -450,12 +452,49 @@ static bool zend_call_stack_get_openbsd(zend_call_stack *stack)
450452
return true;
451453
}
452454
#else
453-
static bool zend_call_stack_get_openbsd(zend_call_stack *stack)
455+
static bool zend_call_stack_get_openbsd_pthread(zend_call_stack *stack)
454456
{
455457
return false;
456458
}
457459
#endif /* defined(HAVE_PTHREAD_STACKSEG_NP) */
458460

461+
static bool zend_call_stack_get_openbsd_vm(zend_call_stack *stack)
462+
{
463+
void *stack_base;
464+
struct rlimit rlim;
465+
466+
467+
if (getrlimit(RLIMIT_STACK, &rlim) != 0) {
468+
return false;
469+
}
470+
471+
if (rlim.rlim_cur == RLIM_INFINITY) {
472+
return false;
473+
}
474+
475+
// arch dependent top user's stack
476+
stack->base = USRSTACK - rlim.rlim_cur;
477+
stack->max_size = rlim.rlim_cur - sysconf(_SC_PAGE_SIZE);
478+
479+
return true;
480+
}
481+
482+
static bool zend_call_stack_get_openbsd(zend_call_stack *stack)
483+
{
484+
if (getthrid() == getpid()) {
485+
return zend_call_stack_get_openbsd_vm(stack);
486+
}
487+
488+
return zend_call_stack_get_openbsd_pthread(stack);
489+
}
490+
491+
#else
492+
static bool zend_call_stack_get_openbsd(zend_call_stack *stack)
493+
{
494+
return false;
495+
}
496+
#endif /* defined(__OpenBSD__) */
497+
459498
/** Get the stack information for the calling thread */
460499
ZEND_API bool zend_call_stack_get(zend_call_stack *stack)
461500
{

0 commit comments

Comments
 (0)