From 09f17568acba4db9fc4d6273e6e17b16030a649c Mon Sep 17 00:00:00 2001 From: David Carlier Date: Fri, 7 Jul 2023 20:21:11 +0100 Subject: [PATCH 1/2] zend call stack, follow-up on 75e9980. user stack usable implementation for openbsd. --- Zend/zend_call_stack.c | 43 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/Zend/zend_call_stack.c b/Zend/zend_call_stack.c index 4b8f4106a1a6c..496b1bda4b4ca 100644 --- a/Zend/zend_call_stack.c +++ b/Zend/zend_call_stack.c @@ -46,6 +46,7 @@ #endif #ifdef __OpenBSD__ # include +# include #endif #ifdef __linux__ #include @@ -435,8 +436,9 @@ static bool zend_call_stack_get_macos(zend_call_stack *stack) } #endif /* defined(__APPLE__) && defined(HAVE_PTHREAD_GET_STACKADDR_NP) */ +#if defined(__OpenBSD__) #if defined(HAVE_PTHREAD_STACKSEG_NP) -static bool zend_call_stack_get_openbsd(zend_call_stack *stack) +static bool zend_call_stack_get_openbsd_pthread(zend_call_stack *stack) { stack_t ss; @@ -450,12 +452,49 @@ static bool zend_call_stack_get_openbsd(zend_call_stack *stack) return true; } #else -static bool zend_call_stack_get_openbsd(zend_call_stack *stack) +static bool zend_call_stack_get_openbsd_pthread(zend_call_stack *stack) { return false; } #endif /* defined(HAVE_PTHREAD_STACKSEG_NP) */ +static bool zend_call_stack_get_openbsd_vm(zend_call_stack *stack) +{ + void *stack_base; + struct rlimit rlim; + + + if (getrlimit(RLIMIT_STACK, &rlim) != 0) { + return false; + } + + if (rlim.rlim_cur == RLIM_INFINITY) { + return false; + } + + // arch dependent top user's stack + stack->base = USRSTACK - rlim.rlim_cur; + stack->max_size = rlim.rlim_cur - sysconf(_SC_PAGE_SIZE); + + return true; +} + +static bool zend_call_stack_get_openbsd(zend_call_stack *stack) +{ + if (getthrid() == getpid()) { + return zend_call_stack_get_openbsd_vm(stack); + } + + return zend_call_stack_get_openbsd_pthread(stack); +} + +#else +static bool zend_call_stack_get_openbsd(zend_call_stack *stack) +{ + return false; +} +#endif /* defined(__OpenBSD__) */ + /** Get the stack information for the calling thread */ ZEND_API bool zend_call_stack_get(zend_call_stack *stack) { From a368570d2925a9380a8baf7fef19f0c5a10f2d17 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 8 Jul 2023 11:26:42 +0100 Subject: [PATCH 2/2] changes from review --- Zend/zend_call_stack.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/Zend/zend_call_stack.c b/Zend/zend_call_stack.c index 496b1bda4b4ca..8c2b43ccdf921 100644 --- a/Zend/zend_call_stack.c +++ b/Zend/zend_call_stack.c @@ -45,8 +45,11 @@ # include #endif #ifdef __OpenBSD__ +typedef int boolean_t; +# include # include -# include +# include +# include #endif #ifdef __linux__ #include @@ -460,9 +463,14 @@ static bool zend_call_stack_get_openbsd_pthread(zend_call_stack *stack) static bool zend_call_stack_get_openbsd_vm(zend_call_stack *stack) { - void *stack_base; + struct _ps_strings ps; struct rlimit rlim; + int mib[2] = {CTL_VM, VM_PSSTRINGS }; + size_t len = sizeof(ps), pagesize; + if (sysctl(mib, 2, &ps, &len, NULL, 0) != 0) { + return false; + } if (getrlimit(RLIMIT_STACK, &rlim) != 0) { return false; @@ -472,16 +480,18 @@ static bool zend_call_stack_get_openbsd_vm(zend_call_stack *stack) return false; } - // arch dependent top user's stack - stack->base = USRSTACK - rlim.rlim_cur; - stack->max_size = rlim.rlim_cur - sysconf(_SC_PAGE_SIZE); + pagesize = sysconf(_SC_PAGE_SIZE); + + stack->base = (void *)((uintptr_t)ps.val + (pagesize - 1) & ~(pagesize - 1)); + stack->max_size = rlim.rlim_cur - pagesize; return true; } static bool zend_call_stack_get_openbsd(zend_call_stack *stack) { - if (getthrid() == getpid()) { + // TIB_THREAD_INITIAL_STACK is private and here we avoid using pthread's api (ie pthread_main_np) + if (!TIB_GET()->tib_thread || (TIB_GET()->tib_thread_flags & 0x002) != 0) { return zend_call_stack_get_openbsd_vm(stack); }