git: b904f197ced4 - stable/13 - libc, libthr: use AT_USRSTACK{BASE,LIM} instead of sysctl("kern.usrstack") and get_rlimit(RLIMIT_STACK)

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Fri, 30 Sep 2022 02:04:16 UTC
The branch stable/13 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=b904f197ced4f98aa6f105eab937167e59314950

commit b904f197ced4f98aa6f105eab937167e59314950
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2022-09-12 19:45:21 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-09-30 00:29:10 +0000

    libc, libthr: use AT_USRSTACK{BASE,LIM} instead of sysctl("kern.usrstack") and get_rlimit(RLIMIT_STACK)
    
    (cherry picked from commit e2879ece4314eed2d22fe484bd4adfcbb1009685)
---
 lib/libc/gen/elf_utils.c      | 27 ++++++++++++++++-----------
 lib/libthr/thread/thr_init.c  | 25 +++++++++++++++++--------
 lib/libthr/thread/thr_stack.c | 26 ++++++++++++++++----------
 3 files changed, 49 insertions(+), 29 deletions(-)

diff --git a/lib/libc/gen/elf_utils.c b/lib/libc/gen/elf_utils.c
index ea5cc25f8601..ed3ac843f7c3 100644
--- a/lib/libc/gen/elf_utils.c
+++ b/lib/libc/gen/elf_utils.c
@@ -29,6 +29,7 @@
  */
 
 #include <sys/param.h>
+#include <sys/auxv.h>
 #include <sys/mman.h>
 #include <sys/resource.h>
 #include <sys/sysctl.h>
@@ -77,19 +78,23 @@ __libc_map_stacks_exec(void)
 {
 	int mib[2];
 	struct rlimit rlim;
-	u_long usrstack;
+	u_long usrstack, stacksz;
 	size_t len;
 	
-	mib[0] = CTL_KERN;
-	mib[1] = KERN_USRSTACK;
-	len = sizeof(usrstack);
-	if (sysctl(mib, nitems(mib), &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());
+	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());
 }
 
 #pragma weak __pthread_map_stacks_exec
diff --git a/lib/libthr/thread/thr_init.c b/lib/libthr/thread/thr_init.c
index 0ab051e57994..b9d3265a05dc 100644
--- a/lib/libthr/thread/thr_init.c
+++ b/lib/libthr/thread/thr_init.c
@@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$");
 
 #include "namespace.h"
 #include <sys/param.h>
+#include <sys/auxv.h>
 #include <sys/signalvar.h>
 #include <sys/ioctl.h>
 #include <sys/link_elf.h>
@@ -462,18 +463,26 @@ init_private(void)
 	if (init_once == 0) {
 		__thr_pshared_init();
 		__thr_malloc_init();
+
 		/* Find the stack top */
-		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 (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");
+		}
 		env_bigstack = getenv("LIBPTHREAD_BIGSTACK_MAIN");
 		env_splitstack = getenv("LIBPTHREAD_SPLITSTACK_MAIN");
 		if (env_bigstack != NULL || env_splitstack == NULL) {
-			if (getrlimit(RLIMIT_STACK, &rlim) == -1)
-				PANIC("Cannot get stack rlimit");
-			_thr_stack_initial = rlim.rlim_cur;
+			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;
+			}
 		}
 		_thr_is_smp = sysconf(_SC_NPROCESSORS_CONF);
 		if (_thr_is_smp == -1)
diff --git a/lib/libthr/thread/thr_stack.c b/lib/libthr/thread/thr_stack.c
index af396fe2ba93..34536ecfad2e 100644
--- a/lib/libthr/thread/thr_stack.c
+++ b/lib/libthr/thread/thr_stack.c
@@ -31,6 +31,7 @@
 __FBSDID("$FreeBSD$");
 
 #include <sys/param.h>
+#include <sys/auxv.h>
 #include <sys/mman.h>
 #include <sys/queue.h>
 #include <sys/resource.h>
@@ -149,18 +150,23 @@ singlethread_map_stacks_exec(void)
 {
 	int mib[2];
 	struct rlimit rlim;
-	u_long usrstack;
+	u_long usrstack, stacksz;
 	size_t len;
 
-	mib[0] = CTL_KERN;
-	mib[1] = KERN_USRSTACK;
-	len = sizeof(usrstack);
-	if (sysctl(mib, nitems(mib), &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());
+	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());
 }
 
 void