NetBSD Problem Report #57814

From www@netbsd.org  Wed Jan  3 18:50:39 2024
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 213C01A923D
	for <gnats-bugs@gnats.NetBSD.org>; Wed,  3 Jan 2024 18:50:39 +0000 (UTC)
Message-Id: <20240103185037.4E43D1A923E@mollari.NetBSD.org>
Date: Wed,  3 Jan 2024 18:50:37 +0000 (UTC)
From: brooks@freebsd.org
Reply-To: brooks@freebsd.org
To: gnats-bugs@NetBSD.org
Subject: mlock_test: use mmap/munmap for unmapped pointer
X-Send-Pr-Version: www-1.0

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

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.47 2022/09/11 19:34:41 kim Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2024 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.