diff --git a/lib/libthr/thread/thr_stack.c b/lib/libthr/thread/thr_stack.c index cb390cc..500c340 100644 --- a/lib/libthr/thread/thr_stack.c +++ b/lib/libthr/thread/thr_stack.c @@ -30,6 +30,8 @@ #include #include #include +#include +#include #include #include #include @@ -139,6 +141,26 @@ _thr_stack_fix_protection(struct pthread *thrd) _rtld_get_stack_prot()); } +static void +singlethr_map_stacks_exec(void) +{ + int mib[2]; + struct rlimit rlim; + u_long usrstack; + size_t len; + + mib[0] = CTL_KERN; + mib[1] = KERN_USRSTACK; + len = sizeof(usrstack); + if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), &usrstack, &len, NULL, 0) + == -1) + return; + if (getrlimit(RLIMIT_STACK, &rlim) == -1) + return; + mprotect((void *)(uintptr_t)(usrstack - rlim.rlim_cur), + rlim.rlim_cur, _rtld_get_stack_prot()); +} + void __pthread_map_stacks_exec(void); void __pthread_map_stacks_exec(void) @@ -146,6 +168,10 @@ __pthread_map_stacks_exec(void) struct pthread *curthread, *thrd; struct stack *st; + if (!_thr_isthreaded()) { + singlethr_map_stacks_exec(); + return; + } curthread = _get_curthread(); THREAD_LIST_RDLOCK(curthread); LIST_FOREACH(st, &mstackq, qe)