]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
FreeBSD: fix check for mmap flags
authorPaul Floyd <pjfloyd@wanadoo.fr>
Fri, 18 Jul 2025 11:21:26 +0000 (13:21 +0200)
committerPaul Floyd <pjfloyd@wanadoo.fr>
Fri, 18 Jul 2025 11:24:28 +0000 (13:24 +0200)
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

coregrind/m_syswrap/syswrap-generic.c
include/vki/vki-freebsd.h

index 7ad280980777629250446abfaff176dae6d61404..1b3b5e80d6785727bf06b4fb481620d07d2fe39b 100644 (file)
@@ -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 );
index 63ffbe7e5ee0a4463dd15ee87ca5c40390b0dfe5..1a371245f605cd57c2044ea4b56af389860e0af8 100644 (file)
@@ -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