From 3af16637fc581e5a3b3607fe9d38d4803cfb953b Mon Sep 17 00:00:00 2001 From: Kurt Miller Date: Sun, 8 Jul 2007 01:53:47 +0000 Subject: Report the correct stack size and top for the primordial thread in pthread_stackseg_np(). With input and okay marc@ --- lib/libpthread/uthread/uthread_stackseg_np.c | 33 ++++++++++++++----------- lib/librthread/rthread_np.c | 37 ++++++++++++++++------------ 2 files changed, 39 insertions(+), 31 deletions(-) (limited to 'lib') diff --git a/lib/libpthread/uthread/uthread_stackseg_np.c b/lib/libpthread/uthread/uthread_stackseg_np.c index cd423aeaf9a..c2a981c3a57 100644 --- a/lib/libpthread/uthread/uthread_stackseg_np.c +++ b/lib/libpthread/uthread/uthread_stackseg_np.c @@ -1,9 +1,11 @@ -/* $OpenBSD: uthread_stackseg_np.c,v 1.5 2007/05/18 19:28:50 kurt Exp $ */ +/* $OpenBSD: uthread_stackseg_np.c,v 1.6 2007/07/08 01:53:46 kurt Exp $ */ /* PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman */ #include +#include #include +#include #include #include @@ -27,6 +29,7 @@ pthread_stackseg_np(pthread_t thread, stack_t *sinfo) char *base; size_t pgsz; int ret; + struct rlimit rl; if (thread->stack) { base = thread->stack->base; @@ -38,22 +41,22 @@ pthread_stackseg_np(pthread_t thread, stack_t *sinfo) sinfo->ss_flags = 0; ret = 0; } else if (thread == _thread_initial) { + if (getrlimit(RLIMIT_STACK, &rl) != 0) + return (EAGAIN); pgsz = (size_t)sysconf(_SC_PAGESIZE); if (pgsz == (size_t)-1) - ret = EAGAIN; - else { -#if defined(MACHINE_STACK_GROWS_UP) - base = (caddr_t) USRSTACK; -#else - base = (caddr_t) ((USRSTACK - DFLSSIZ) & ~(pgsz - 1)); - base += DFLSSIZ; -#endif - sinfo->ss_sp = base; - sinfo->ss_size = DFLSSIZ; - sinfo->ss_flags = 0; - ret = 0; - } - + return (EAGAIN); + /* + * round_page() stack rlim_cur and + * trunc_page() USRSTACK to be consistent with + * the way the kernel sets up the stack. + */ + sinfo->ss_size = (size_t)rl.rlim_cur; + sinfo->ss_size += (pgsz - 1); + sinfo->ss_size &= ~(pgsz - 1); + sinfo->ss_sp = (caddr_t) (USRSTACK & ~(pgsz - 1)); + sinfo->ss_flags = 0; + ret = 0; } else ret = EAGAIN; diff --git a/lib/librthread/rthread_np.c b/lib/librthread/rthread_np.c index d4388fff479..cb36095a917 100644 --- a/lib/librthread/rthread_np.c +++ b/lib/librthread/rthread_np.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rthread_np.c,v 1.4 2006/04/12 13:34:12 henning Exp $ */ +/* $OpenBSD: rthread_np.c,v 1.5 2007/07/08 01:53:46 kurt Exp $ */ /* * Copyright (c) 2004,2005 Ted Unangst * Copyright (c) 2005 Otto Moerbeek @@ -18,12 +18,15 @@ */ #include +#include #include +#include #include #include #include #include +#include #include #include @@ -58,32 +61,34 @@ pthread_stackseg_np(pthread_t thread, stack_t *sinfo) char *base; size_t pgsz; int ret; + struct rlimit rl; if (thread->stack) { base = thread->stack->base; #if !defined(MACHINE_STACK_GROWS_UP) - base += thread->stack->len; + base += (ptrdiff_t)thread->stack->len; #endif sinfo->ss_sp = base; sinfo->ss_size = thread->stack->len; sinfo->ss_flags = 0; ret = 0; } else if (thread == &_initial_thread) { - pgsz = sysconf(_SC_PAGESIZE); + if (getrlimit(RLIMIT_STACK, &rl) != 0) + return (EAGAIN); + pgsz = (size_t)sysconf(_SC_PAGESIZE); if (pgsz == (size_t)-1) - ret = EAGAIN; - else { -#if defined(MACHINE_STACK_GROWS_UP) - base = (caddr_t) USRSTACK; -#else - base = (caddr_t) ((USRSTACK - DFLSSIZ) & ~(pgsz - 1)); - base += DFLSSIZ; -#endif - sinfo->ss_sp = base; - sinfo->ss_size = DFLSSIZ; - sinfo->ss_flags = 0; - ret = 0; - } + return (EAGAIN); + /* + * round_page() stack rlim_cur and + * trunc_page() USRSTACK to be consistent with + * the way the kernel sets up the stack. + */ + sinfo->ss_size = (size_t)rl.rlim_cur; + sinfo->ss_size += (pgsz - 1); + sinfo->ss_size &= ~(pgsz - 1); + sinfo->ss_sp = (caddr_t) (USRSTACK & ~(pgsz - 1)); + sinfo->ss_flags = 0; + ret = 0; } else ret = EAGAIN; -- cgit v1.2.3