[1/2] linux: prevent kernel choose addr by itself in mremap test

Message ID 20250208-fix-mremap-tst-v1-1-ad7ee617ec65@coelacanthus.name (mailing list archive)
State Accepted
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
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build 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's only guaranteed when MREMAP_FIXED was specified.
---
 sysdeps/unix/sysv/linux/tst-linux-mremap1.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
  

Comments

Adhemerval Zanella Netto Feb. 11, 2025, 6:28 p.m. UTC | #1
On 07/02/25 17:17, Celeste Liu wrote:
> It's only guaranteed when MREMAP_FIXED was specified.

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
>  sysdeps/unix/sysv/linux/tst-linux-mremap1.c | 19 ++++++++++---------
>  1 file changed, 10 insertions(+), 9 deletions(-)
> 
> diff --git a/sysdeps/unix/sysv/linux/tst-linux-mremap1.c b/sysdeps/unix/sysv/linux/tst-linux-mremap1.c
> index a5dce5aee4104b5c4a594daab4e5fda1c4080ab4..334ce2a30be1362233f6c20139f7a80213585d10 100644
> --- a/sysdeps/unix/sysv/linux/tst-linux-mremap1.c
> +++ b/sysdeps/unix/sysv/linux/tst-linux-mremap1.c
> @@ -40,20 +40,21 @@ do_test (void)
>  			    MAP_PRIVATE | MAP_ANONYMOUS, -1);
>    xmunmap (fixed_addr, fixed_size);
>  
> -  /* Add 3 * pagesize.  */
> -  fixed_size += 3 * old_size;
> -
> -  /* Test MREMAP_DONTUNMAP.  It should return FIXED_ADDR created above.  */
> +  /* Test MREMAP_DONTUNMAP and MREMAP_FIXED. It should return address we
> +     specified and the old address is still available  */
>    char *new_addr = mremap (old_addr, old_size, new_size,
> -			   MREMAP_DONTUNMAP | MREMAP_MAYMOVE,
> +			   MREMAP_DONTUNMAP | MREMAP_MAYMOVE | MREMAP_FIXED,
>  			   fixed_addr);
>    if (new_addr == MAP_FAILED)
>      return mremap_failure_exit (errno);
> +  /* mremap should use the hint address. */
>    TEST_VERIFY_EXIT (fixed_addr == new_addr);
> -  old_addr[0] = 3;
> -  old_addr[old_size - 1] = 4;
> -  new_addr[0] = 1;
> -  new_addr[new_size - 1] = 2;
> +  /* 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);
>  
>
  

Patch

diff --git a/sysdeps/unix/sysv/linux/tst-linux-mremap1.c b/sysdeps/unix/sysv/linux/tst-linux-mremap1.c
index a5dce5aee4104b5c4a594daab4e5fda1c4080ab4..334ce2a30be1362233f6c20139f7a80213585d10 100644
--- a/sysdeps/unix/sysv/linux/tst-linux-mremap1.c
+++ b/sysdeps/unix/sysv/linux/tst-linux-mremap1.c
@@ -40,20 +40,21 @@  do_test (void)
 			    MAP_PRIVATE | MAP_ANONYMOUS, -1);
   xmunmap (fixed_addr, fixed_size);
 
-  /* Add 3 * pagesize.  */
-  fixed_size += 3 * old_size;
-
-  /* Test MREMAP_DONTUNMAP.  It should return FIXED_ADDR created above.  */
+  /* Test MREMAP_DONTUNMAP and MREMAP_FIXED. It should return address we
+     specified and the old address is still available  */
   char *new_addr = mremap (old_addr, old_size, new_size,
-			   MREMAP_DONTUNMAP | MREMAP_MAYMOVE,
+			   MREMAP_DONTUNMAP | MREMAP_MAYMOVE | MREMAP_FIXED,
 			   fixed_addr);
   if (new_addr == MAP_FAILED)
     return mremap_failure_exit (errno);
+  /* mremap should use the hint address. */
   TEST_VERIFY_EXIT (fixed_addr == new_addr);
-  old_addr[0] = 3;
-  old_addr[old_size - 1] = 4;
-  new_addr[0] = 1;
-  new_addr[new_size - 1] = 2;
+  /* 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);