From: Paul Floyd Date: Fri, 18 Jul 2025 11:21:26 +0000 (+0200) Subject: FreeBSD: fix check for mmap flags X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=baac076edfde2870ee6cc69390f3bca9d4f7d974;p=thirdparty%2Fvalgrind.git FreeBSD: fix check for mmap flags On FreeBSD, mmap also has MAP_STACK and MAP_GUARD that can be mapped without a backing file referred to by fd. As a result during ld.so startup and thread creation mmap for stacks was failing. So no guest could be load and execute, with errors like ld-elf.so.1: /home/paulf/scratch/valgrind_nightly/nightly/valgrind-new/.in_place/vgpreload_core-amd64-freebsd.so: mmap of entire address space failed: Bad file descriptor --- diff --git a/coregrind/m_syswrap/syswrap-generic.c b/coregrind/m_syswrap/syswrap-generic.c index 7ad280980..1b3b5e80d 100644 --- a/coregrind/m_syswrap/syswrap-generic.c +++ b/coregrind/m_syswrap/syswrap-generic.c @@ -2653,12 +2653,26 @@ ML_(generic_PRE_sys_mmap) ( ThreadId tid, VG_(core_panic)("can't use ML_(generic_PRE_sys_mmap) on Darwin"); # endif - /* fd (arg4) is only used when flags (arg4) does not contain - MAP_ANONYMOUS. ML_(fd_allowed) might just warn (with --track-fds) +#if !defined(VKI_MAP_GUARD) +// on platforms without MAP_GUARD the compiler should optimise away +// the term using it below as it will always be true +#define VKI_MAP_GUARD 0 +#endif + +#if !defined(VKI_MAP_STACK) +// as above +#define VKI_MAP_STACK 0 +#endif + + /* fd (arg5) is only used when flags (arg4) does not contain + MAP_ANONYMOUS (or, on FreeBSD, MAP_GUARD and MAP_STACK). + ML_(fd_allowed) might just warn (with --track-fds) and not fail, unless it is a Valgrind owned file descriptor. So also check with fcntl (F_GETFD) to know if it really is a bad fd. Fail early in that case with EBADF. */ if (!(arg4 & VKI_MAP_ANONYMOUS) + && !(arg4 & VKI_MAP_GUARD) + && !(arg4 & VKI_MAP_STACK) && (!ML_(fd_allowed)(arg5, "mmap", tid, False) || VG_(fcntl) (arg5, VKI_F_GETFD, 0) < 0)) { return VG_(mk_SysRes_Error)( VKI_EBADF ); diff --git a/include/vki/vki-freebsd.h b/include/vki/vki-freebsd.h index 63ffbe7e5..1a371245f 100644 --- a/include/vki/vki-freebsd.h +++ b/include/vki/vki-freebsd.h @@ -1526,6 +1526,7 @@ typedef enum vki_idtype { #define VKI_MAP_STACK 0x400 #define VKI_MAP_ANON 0x1000 /* don't use a file */ #define VKI_MAP_ANONYMOUS VKI_MAP_ANON +#define VKI_MAP_GUARD 0x00002000 #define VKI_MAP_ALIGNED(n) ((n) << VKI_MAP_ALIGNMENT_SHIFT) #define VKI_MAP_ALIGNMENT_SHIFT 24