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

Message ID 20250208-fix-mremap-tst-v1-2-ad7ee617ec65@coelacanthus.name (mailing list archive)
State Under Review
Headers
Series linux: check mremap w/ MREMAP_DONTUNMAP correctly |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed

Commit Message

Celeste Liu Feb. 7, 2025, 8:17 p.m. UTC
  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(+)
  

Comments

Adhemerval Zanella Netto Feb. 11, 2025, 6:43 p.m. UTC | #1
On 07/02/25 17:17, Celeste Liu wrote:
> It should be tested since it's different code path, like the Linux
> selftests did.

The test looks ok, although the point of the tst-linux-mremap1.c is to 
check if the argument handling in the mremap implementation work
correctly [1]. This test seems to exercise the same path w.r.t to libc
wrapper.

[1] https://sourceware.org/bugzilla/show_bug.cgi?id=31968

> ---
>  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>
>
  

Patch

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>