git: 16c60a70c99b - stable/13 - posixshm tests: Fix occasional largepage_mprotect failures

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 10 Jan 2022 14:05:30 UTC
The branch stable/13 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=16c60a70c99b82e2b645c334f0cc3e89ad3c2be5

commit 16c60a70c99b82e2b645c334f0cc3e89ad3c2be5
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2022-01-03 16:15:46 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-01-10 14:05:12 +0000

    posixshm tests: Fix occasional largepage_mprotect failures
    
    largepage_mprotect maps a superpage and later extends the mapping.  This
    occasionally fails with ASLR disabled.  To fix this, first try to
    reserve a sufficiently large virtual address region.
    
    Reported by:    Jenkins
    Sponsored by:   The FreeBSD Foundation
    
    (cherry picked from commit 321e586e46115c69e7e833d592703d997a4ec477)
---
 tests/sys/posixshm/posixshm_test.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/tests/sys/posixshm/posixshm_test.c b/tests/sys/posixshm/posixshm_test.c
index d1c1b14aef65..2f2c6eb5fc80 100644
--- a/tests/sys/posixshm/posixshm_test.c
+++ b/tests/sys/posixshm/posixshm_test.c
@@ -1434,9 +1434,19 @@ ATF_TC_BODY(largepage_mprotect, tc)
 
 	pscnt = pagesizes(ps);
 	for (int i = 1; i < pscnt; i++) {
+		/*
+		 * Reserve a contiguous region in the address space to avoid
+		 * spurious failures in the face of ASLR.
+		 */
+		addr = mmap(NULL, ps[i] * 2, PROT_NONE,
+		    MAP_ANON | MAP_ALIGNED(ffsl(ps[i]) - 1), -1, 0);
+		ATF_REQUIRE_MSG(addr != MAP_FAILED,
+		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
+		ATF_REQUIRE(munmap(addr, ps[i] * 2) == 0);
+
 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
-		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
-		    0);
+		addr = mmap(addr, ps[i], PROT_READ | PROT_WRITE,
+		    MAP_SHARED | MAP_FIXED, fd, 0);
 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);