]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Support mmap MAP_FIXED_NOREPLACE if defined
authorMark Wielaard <mark@klomp.org>
Tue, 15 Jul 2025 21:49:36 +0000 (23:49 +0200)
committerMark Wielaard <mark@klomp.org>
Tue, 15 Jul 2025 21:59:48 +0000 (23:59 +0200)
Define VKI_MAP_FIXED_NOREPLACE for amd64-linux, arm-linux,
arm64-linux, mips32-linux, mips64-linux, riscv64-linux and x86-linux.

If it is defined then ML_(generic_PRE_sys_mmap) will also interpret
VKI_MAP_FIXED_NOREPLACE as an MFixed hint. If the aspace manager
doesn't find a MAP_FIXED_NOREPLACE ok, then fail with EEXIST. If the
actual kernel mmap request fails and MAP_FIXED_NOREPLACE is set also
immediately fail with EEXIST without retrying.

This fixes the LTP mmap17 testcase.

https://bugs.kde.org/show_bug.cgi?id=418756

NEWS
coregrind/m_syswrap/syswrap-generic.c
include/vki/vki-amd64-linux.h
include/vki/vki-arm-linux.h
include/vki/vki-arm64-linux.h
include/vki/vki-mips32-linux.h
include/vki/vki-mips64-linux.h
include/vki/vki-riscv64-linux.h
include/vki/vki-x86-linux.h

diff --git a/NEWS b/NEWS
index 49403da0132b89ff0734d532b390d349badc3d32..796d9716e5c102e3a0d760092d35f577b61d8992 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -29,8 +29,8 @@ bugzilla (https://bugs.kde.org/enter_bug.cgi?product=valgrind) rather
 than mailing the developers (or mailing lists) directly -- bugs that
 are not entered into bugzilla tend to get forgotten about or ignored.
 
-506076  unimplemented fcntl command: 1028 (F_CREATED_QUERY)
 338803  Handling of dwz debug alt files or cross-CU is broken
+418756  MAP_FIXED_NOREPLACE mmap flag unsupported
 493434  Add --track-fds=bad mode (no "leak" tracking)
 503098  Incorrect NAN-boxing for float registers in RISC-V
 503641  close_range syscalls started failing with 3.25.0
@@ -52,6 +52,7 @@ are not entered into bugzilla tend to get forgotten about or ignored.
         AMD64_GET_TLSBASE
 505228  Wrap linux specific mseal syscall
 502968  Wrap linux specific syscalls 457 (listmount) and 458 (statmount)
+506076  unimplemented fcntl command: 1028 (F_CREATED_QUERY)
 506499  Unhandled syscall 592 (exterrctl - FreeBSD
 506795  Better report which clone flags are problematic
 506930  valgrind allows SIGKILL being reset to SIG_DFL
index 50deb1e7641f8ed5f421d98507f0cc3671e615a4..50415a2faa518eaf1ae610a84d23ceab78390462 100644 (file)
@@ -2678,7 +2678,12 @@ ML_(generic_PRE_sys_mmap) ( ThreadId tid,
       (fixed/hint/any), and ask aspacem what we should do. */
    mreq.start = arg1;
    mreq.len   = arg2;
-   if (arg4 & VKI_MAP_FIXED) {
+   if ((arg4 & VKI_MAP_FIXED)
+#if defined(VKI_MAP_FIXED_NOREPLACE)
+       || (arg4 & VKI_MAP_FIXED_NOREPLACE)
+#endif
+      )
+   {
       mreq.rkind = MFixed;
    } else
 #if defined(VGO_solaris) && defined(VKI_MAP_ALIGN)
@@ -2710,6 +2715,11 @@ ML_(generic_PRE_sys_mmap) ( ThreadId tid,
    advised = VG_(am_get_advisory)( &mreq, True/*client*/, &mreq_ok );
    if (!mreq_ok) {
       /* Our request was bounced, so we'd better fail. */
+#if defined(VKI_MAP_FIXED_NOREPLACE)
+      if (arg4 & VKI_MAP_FIXED_NOREPLACE) {
+         return VG_(mk_SysRes_Error)( VKI_EEXIST );
+      }
+#endif
       return VG_(mk_SysRes_Error)( VKI_EINVAL );
    }
 
@@ -2744,6 +2754,13 @@ ML_(generic_PRE_sys_mmap) ( ThreadId tid,
    }
 #  endif
 
+#  if defined(VKI_MAP_FIXED_NOREPLACE)
+   /* FIXED_NOREPLACE is fatal, no retries. */
+   if ((arg4 & VKI_MAP_FIXED_NOREPLACE) && sr_isError(sres)) {
+      return VG_(mk_SysRes_Error)( VKI_EEXIST );
+   }
+#  endif
+
    /* A refinement: it may be that the kernel refused aspacem's choice
       of address.  If we were originally asked for a hinted mapping,
       there is still a last chance: try again at any address.
index 12cd65ac7c3207a1fba1fab33fc276e02aa9a7ff..bbcf4ab4e9402ee20274f2d31dfbfd99f4793bb1 100644 (file)
@@ -236,6 +236,7 @@ struct vki_sigcontext {
 #define VKI_MAP_ANONYMOUS      0x20    /* don't use a file */
 #define VKI_MAP_32BIT  0x40            /* only give out 32bit addresses */
 #define VKI_MAP_NORESERVE       0x4000  /* don't check for reservations */
+#define VKI_MAP_FIXED_NOREPLACE        0x100000 /* fail EEXIST if fixed map fails */
 
 //----------------------------------------------------------------------
 // From linux-2.6.9/include/asm-x86_64/fcntl.h
index 7e0001c0cf47fc2b07b047ff7be75059f86fe81c..a72268ca4320499b0c0df2689a0ac4c79c153330 100644 (file)
@@ -233,6 +233,7 @@ struct vki_sigcontext {
 #define VKI_MAP_FIXED  0x10            /* Interpret addr exactly */
 #define VKI_MAP_ANONYMOUS      0x20    /* don't use a file */
 #define VKI_MAP_NORESERVE      0x4000          /* don't check for reservations */
+#define VKI_MAP_FIXED_NOREPLACE        0x100000 /* fail EEXIST if fixed map fails */
 
 //----------------------------------------------------------------------
 // From linux-2.6.8.1/include/asm-i386/fcntl.h
index 2fc97e614f6ff93a06eaca7543a0241244f182d7..1b005c7750e47a35a5fc89af7fd69d782e158a2b 100644 (file)
@@ -215,6 +215,7 @@ struct vki_sigcontext {
 #define VKI_MAP_FIXED  0x10            /* Interpret addr exactly */
 #define VKI_MAP_ANONYMOUS      0x20    /* don't use a file */
 #define VKI_MAP_NORESERVE       0x4000  /* don't check for reservations */
+#define VKI_MAP_FIXED_NOREPLACE        0x100000 /* fail EEXIST if fixed map fails */
 
 //----------------------------------------------------------------------
 // From linux-3.10.5/uapi/include/asm-generic/fcntl.h
index 2d752e2ccbb75c108168ac9dd81b2f7e7c7eb825..584b5dd727959135f5b6d78560484e4a77b71743 100644 (file)
@@ -300,6 +300,7 @@ struct vki_sigcontext {
 #define VKI_MAP_LOCKED      0x8000          /* pages are locked */
 #define VKI_MAP_POPULATE    0x10000         /* populate (prefault) pagetables */
 #define VKI_MAP_NONBLOCK    0x20000         /* do not block on IO */
+#define VKI_MAP_FIXED_NOREPLACE        0x100000    /* fail EEXIST if fixed map fails */
 
 
 //----------------------------------------------------------------------
index 527b0dae63067bf5e0ccb2ef6502cf8000a4d6cd..9171b6fb0e266a6cce50f3e3970947107682c8a4 100644 (file)
@@ -306,6 +306,7 @@ struct vki_sigcontext {
 #define VKI_MAP_LOCKED      0x8000          /* pages are locked */
 #define VKI_MAP_POPULATE    0x10000         /* populate (prefault) pagetables */
 #define VKI_MAP_NONBLOCK    0x20000         /* do not block on IO */
+#define VKI_MAP_FIXED_NOREPLACE        0x100000    /* fail EEXIST if fixed map fails */
 
 //----------------------------------------------------------------------
 // From linux-2.6.35.9/include/asm-mips/fcntl.h
index 5cc98b6ab12283cde17d8450c28911b855992726..0ad826c02ff5aac90e22068b1a980a5d135bc6ed 100644 (file)
@@ -186,6 +186,7 @@ typedef struct vki_sigaltstack {
 //----------------------------------------------------------------------
 
 #define VKI_MAP_NORESERVE       0x4000  /* don't check for reservations */
+#define VKI_MAP_FIXED_NOREPLACE        0x100000 /* fail EEXIST if fixed map fails */
 
 //----------------------------------------------------------------------
 // From linux-6.0/include/uapi/linux/mman.h
index 5a5f9e5d82ec262aa7238140565c84ef7220543b..d00de22b41b485e6a7d45836c6a4f5c2b91d0933 100644 (file)
@@ -271,6 +271,7 @@ struct vki_sigcontext {
 #define VKI_MAP_FIXED  0x10            /* Interpret addr exactly */
 #define VKI_MAP_ANONYMOUS      0x20    /* don't use a file */
 #define VKI_MAP_NORESERVE      0x4000          /* don't check for reservations */
+#define VKI_MAP_FIXED_NOREPLACE        0x100000 /* fail EEXIST if fixed map fails */
 
 //----------------------------------------------------------------------
 // From linux-2.6.8.1/include/asm-i386/fcntl.h