Date: Fri, 30 Sep 2022 02:04:17 GMT From: Konstantin Belousov <kib@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Subject: git: f52dbee828b3 - stable/13 - libthr: extract code to get main stack base and size into helpers Message-ID: <202209300204.28U24HA3099296@gitrepo.freebsd.org>
next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=f52dbee828b398864980c6085485e96c78ba97c8 commit f52dbee828b398864980c6085485e96c78ba97c8 Author: Konstantin Belousov <kib@FreeBSD.org> AuthorDate: 2022-09-13 22:14:24 +0000 Commit: Konstantin Belousov <kib@FreeBSD.org> CommitDate: 2022-09-30 00:29:10 +0000 libthr: extract code to get main stack base and size into helpers (cherry picked from commit e03c7f50054182549e097f9fbd9387d8c55ef521) --- lib/libthr/thread/thr_init.c | 56 ++++++++++++++++++++++++++++------------- lib/libthr/thread/thr_private.h | 2 ++ lib/libthr/thread/thr_stack.c | 24 +++++------------- 3 files changed, 46 insertions(+), 36 deletions(-) diff --git a/lib/libthr/thread/thr_init.c b/lib/libthr/thread/thr_init.c index b9d3265a05dc..1c4867cb8185 100644 --- a/lib/libthr/thread/thr_init.c +++ b/lib/libthr/thread/thr_init.c @@ -434,12 +434,43 @@ init_main_thread(struct pthread *thread) /* Others cleared to zero by thr_alloc() */ } -static void -init_private(void) +bool +__thr_get_main_stack_base(char **base) { - struct rlimit rlim; size_t len; int mib[2]; + + if (elf_aux_info(AT_USRSTACKBASE, base, sizeof(*base)) == 0) + return (true); + + mib[0] = CTL_KERN; + mib[1] = KERN_USRSTACK; + len = sizeof(*base); + if (sysctl(mib, nitems(mib), base, &len, NULL, 0) == 0) + return (true); + + return (false); +} + +bool +__thr_get_main_stack_lim(size_t *lim) +{ + struct rlimit rlim; + + if (elf_aux_info(AT_USRSTACKLIM, lim, sizeof(*lim)) == 0) + return (true); + + if (getrlimit(RLIMIT_STACK, &rlim) == 0) { + *lim = rlim.rlim_cur; + return (true); + } + + return (false); +} + +static void +init_private(void) +{ char *env, *env_bigstack, *env_splitstack; _thr_umutex_init(&_mutex_static_lock); @@ -465,24 +496,13 @@ init_private(void) __thr_malloc_init(); /* Find the stack top */ - if (elf_aux_info(AT_USRSTACKBASE, &_usrstack, - sizeof(_usrstack)) != 0) { - mib[0] = CTL_KERN; - mib[1] = KERN_USRSTACK; - len = sizeof (_usrstack); - if (sysctl(mib, nitems(mib), &_usrstack, &len, - NULL, 0) == -1) - PANIC("Cannot get kern.usrstack from sysctl"); - } + if (!__thr_get_main_stack_base(&_usrstack)) + PANIC("Cannot get kern.usrstack"); env_bigstack = getenv("LIBPTHREAD_BIGSTACK_MAIN"); env_splitstack = getenv("LIBPTHREAD_SPLITSTACK_MAIN"); if (env_bigstack != NULL || env_splitstack == NULL) { - if (elf_aux_info(AT_USRSTACKLIM, &_thr_stack_initial, - sizeof(_thr_stack_initial)) != 0) { - if (getrlimit(RLIMIT_STACK, &rlim) == -1) - PANIC("Cannot get stack rlimit"); - _thr_stack_initial = rlim.rlim_cur; - } + if (!__thr_get_main_stack_lim(&_thr_stack_initial)) + PANIC("Cannot get stack rlimit"); } _thr_is_smp = sysconf(_SC_NPROCESSORS_CONF); if (_thr_is_smp == -1) diff --git a/lib/libthr/thread/thr_private.h b/lib/libthr/thread/thr_private.h index a5bbc5997d30..0e88c6711817 100644 --- a/lib/libthr/thread/thr_private.h +++ b/lib/libthr/thread/thr_private.h @@ -1102,6 +1102,8 @@ int _thr_mutex_destroy(pthread_mutex_t *); int _thr_mutex_unlock(pthread_mutex_t *); int __Tthr_mutex_lock(pthread_mutex_t *); int __Tthr_mutex_trylock(pthread_mutex_t *); +bool __thr_get_main_stack_base(char **base); +bool __thr_get_main_stack_lim(size_t *lim); __END_DECLS __NULLABILITY_PRAGMA_POP diff --git a/lib/libthr/thread/thr_stack.c b/lib/libthr/thread/thr_stack.c index 34536ecfad2e..3befb740639e 100644 --- a/lib/libthr/thread/thr_stack.c +++ b/lib/libthr/thread/thr_stack.c @@ -148,25 +148,13 @@ _thr_stack_fix_protection(struct pthread *thrd) static void singlethread_map_stacks_exec(void) { - int mib[2]; - struct rlimit rlim; - u_long usrstack, stacksz; - size_t len; + char *usrstack; + size_t stacksz; - if (elf_aux_info(AT_USRSTACKBASE, &usrstack, sizeof(usrstack)) != 0) { - mib[0] = CTL_KERN; - mib[1] = KERN_USRSTACK; - len = sizeof(usrstack); - if (sysctl(mib, nitems(mib), &usrstack, &len, NULL, 0) == -1) - return; - } - if (elf_aux_info(AT_USRSTACKLIM, &stacksz, sizeof(stacksz)) != 0) { - if (getrlimit(RLIMIT_STACK, &rlim) == -1) - return; - stacksz = rlim.rlim_cur; - } - mprotect((void *)(uintptr_t)(usrstack - stacksz), stacksz, - _rtld_get_stack_prot()); + if (!__thr_get_main_stack_base(&usrstack) || + !__thr_get_main_stack_lim(&stacksz)) + return; + mprotect(usrstack - stacksz, stacksz, _rtld_get_stack_prot()); } void
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202209300204.28U24HA3099296>