NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

kern/57814: mlock_test: use mmap/munmap for unmapped pointer



>Number:         57814
>Category:       kern
>Synopsis:       mlock_test: use mmap/munmap for unmapped pointer
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    kern-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Wed Jan 03 18:55:00 +0000 2024
>Originator:     Brooks Davis
>Release:        all
>Organization:
>Environment:
>Description:
The mlock_test:mlock_err test uses sbrk(0) to attempt to find an unmapped page.  This works fine, but on FreeBSD we don't implement sbrk on aarch64 and riscv64 and I hope stop exposing it in libc on all architectures in FreeBSD 15. This change switches to using mmap+munmap to locate an unmapped page instead.
>How-To-Repeat:

>Fix:
Author: Brooks Davis <brooks%freebsd.org@localhost>
Date:   Wed Jan 3 18:38:54 2024 +0000

    mlock_test: use mmap/munmap for unmapped pointer
    
    Instead of hoping that the page after sbrk(0) is unmapped, map a page
    with mmap and then munmap it.

diff --git a/tests/lib/libc/sys/t_mlock.c b/tests/lib/libc/sys/t_mlock.c
index 0232631a3a15..1ca57e6a99fc 100644
--- a/tests/lib/libc/sys/t_mlock.c
+++ b/tests/lib/libc/sys/t_mlock.c
@@ -145,10 +145,13 @@ ATF_TC_BODY(mlock_err, tc)
        (void)free(buf);
 
        /*
-        * Try to create a pointer to an unmapped page - first after current
-        * brk will likely do.
+        * Try to create a pointer to an unmapped page - mmap and then
+        * munmap a page.
         */
-       invalid_ptr = (void*)(((uintptr_t)sbrk(0)+page) & ~(page-1));
+       invalid_ptr = mmap(NULL, page, PROT_READ | PROT_WRITE,
+           MAP_ANON | MAP_PRIVATE, -1, 0);
+       ATF_REQUIRE(invalid_ptr != MAP_FAILED);
+       ATF_REQUIRE(munmap(invalid_ptr, page) == 0);
        printf("testing with (hopefully) invalid pointer %p\n", invalid_ptr);
 
        errno = 0;



Home | Main Index | Thread Index | Old Index