[PATCH 2/2] linux: add test for mremap w/ MREMAP_DONTUNMAP but w/o MREMAP_FIXED

Celeste Liu uwu@coelacanthus.name
Fri Feb 7 20:17:51 GMT 2025


It should be tested since it's different code path, like the Linux
selftests did.
---
 sysdeps/unix/sysv/linux/Makefile            |  1 +
 sysdeps/unix/sysv/linux/tst-linux-mremap2.c | 54 +++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 395d2d65938d33de6747264a63b73915003281e6..fc10611cf979971d83f55dde4bc43771886ac8df 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -209,6 +209,7 @@ tests += \
   tst-gettid \
   tst-gettid-kill \
   tst-linux-mremap1 \
+  tst-linux-mremap2 \
   tst-memfd_create \
   tst-misalign-clone \
   tst-mlock2 \
diff --git a/sysdeps/unix/sysv/linux/tst-linux-mremap2.c b/sysdeps/unix/sysv/linux/tst-linux-mremap2.c
new file mode 100644
index 0000000000000000000000000000000000000000..be4e59223db840add8596ed30b0dcd91c248b96f
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-linux-mremap2.c
@@ -0,0 +1,54 @@
+/* Test mremap with MREMAP_DONTUNMAP.
+   Copyright (C) 2025 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <errno.h>
+#include <sys/mman.h>
+#include <support/xstdlib.h>
+#include <support/xunistd.h>
+#include <support/check.h>
+#include <support/test-driver.h>
+#include <mremap-failure.h>
+
+static int
+do_test (void)
+{
+  size_t old_size = getpagesize ();
+  size_t new_size = old_size;
+  char *old_addr = xmmap (NULL, old_size, PROT_READ | PROT_WRITE,
+			  MAP_PRIVATE | MAP_ANONYMOUS, -1);
+  old_addr[0] = 1;
+  old_addr[old_size - 1] = 2;
+
+  /* Test MREMAP_DONTUNMAP w/o MREMAP_FIXED. Old address is still available. */
+  char *new_addr = mremap (old_addr, old_size, new_size,
+			   MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL);
+  if (new_addr == MAP_FAILED)
+    return mremap_failure_exit (errno);
+  /* Old range should be zero-filled. */
+  TEST_VERIFY_EXIT (old_addr[0] == 0);
+  TEST_VERIFY_EXIT (old_addr[old_size - 1] == 0);
+  /* New range should have the same bytes as the old one. */
+  TEST_VERIFY_EXIT (new_addr[0] == 1);
+  TEST_VERIFY_EXIT (new_addr[new_size - 1] == 2);
+  xmunmap (new_addr, new_size);
+  xmunmap (old_addr, old_size);
+
+  return 0;
+}
+
+#include <support/test-driver.c>

-- 
2.48.1



More information about the Libc-alpha mailing list