Re: AMD64 14.0-CURRENT memory layout changes

From: Mark Johnston <markj_at_freebsd.org>
Date: Tue, 18 Oct 2022 15:36:07 UTC
On Tue, Oct 18, 2022 at 02:13:46PM +0200, Paul Floyd wrote:
> 
> > 
> > How is 14.0 working out what address to use for the stack?
> > (The above is with ASLR all off)
> 
> 
> Answering my own question:
> 
> it's in auxv (from __thr_get_main_stack_base)
> 
> 
> /usr/include/sys/elf_common.h:#define   AT_USRSTACKBASE 35      /* Top 
> of user stack */
> 
> I haven't yet added this (or AT_USRSTACKLIM) to the client auxv that 
> Valgrind synthesizes.
> 
> 
> I'm still not certain that will fix it - I would have expected 
> __thr_get_main_stack_base to fallback to using sysctl.

I think this is a compatibility bug in elf_aux_info().  The values of
AT_USRSTACKBASE and AT_USRSTACKLIM can never legitimately be zero, I
think, so we can use that to test.

diff --git a/lib/libc/gen/auxv.c b/lib/libc/gen/auxv.c
index af59a2dda90a..2f043f8814cf 100644
--- a/lib/libc/gen/auxv.c
+++ b/lib/libc/gen/auxv.c
@@ -381,15 +381,21 @@ _elf_aux_info(int aux, void *buf, int buflen)
 		break;
 	case AT_USRSTACKBASE:
 		if (buflen == sizeof(u_long)) {
-			*(u_long *)buf = usrstackbase;
-			res = 0;
+			if (usrstackbase != 0) {
+				*(u_long *)buf = usrstackbase;
+				res = 0;
+			} else
+				res = ENOENT;
 		} else
 			res = EINVAL;
 		break;
 	case AT_USRSTACKLIM:
 		if (buflen == sizeof(u_long)) {
-			*(u_long *)buf = usrstacklim;
-			res = 0;
+			if (usrstacklim != 0) {
+				*(u_long *)buf = usrstacklim;
+				res = 0;
+			} else
+				res = ENOENT;
 		} else
 			res = EINVAL;
 		break;