Module Name:    src
Committed By:   riastradh
Date:           Sat Jun  4 23:09:18 UTC 2022

Modified Files:
        src/tests/lib/libc/sys: t_mmap.c

Log Message:
tests/lib/libc: Test mmap(2) with bad hints.


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.18 src/tests/lib/libc/sys/t_mmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/lib/libc/sys/t_mmap.c
diff -u src/tests/lib/libc/sys/t_mmap.c:1.17 src/tests/lib/libc/sys/t_mmap.c:1.18
--- src/tests/lib/libc/sys/t_mmap.c:1.17	Wed Apr  6 10:02:55 2022
+++ src/tests/lib/libc/sys/t_mmap.c	Sat Jun  4 23:09:18 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: t_mmap.c,v 1.17 2022/04/06 10:02:55 gson Exp $ */
+/* $NetBSD: t_mmap.c,v 1.18 2022/06/04 23:09:18 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -55,7 +55,7 @@
  * SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_mmap.c,v 1.17 2022/04/06 10:02:55 gson Exp $");
+__RCSID("$NetBSD: t_mmap.c,v 1.18 2022/06/04 23:09:18 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/disklabel.h>
@@ -619,6 +619,71 @@ ATF_TC_BODY(mmap_va0, tc)
 	map_check(map, val);
 }
 
+static void
+test_mmap_hint(uintptr_t hintaddr)
+{
+	void *hint = (void *)hintaddr;
+	void *map1 = MAP_FAILED, *map2 = MAP_FAILED, *map3 = MAP_FAILED;
+
+	map1 = mmap(hint, page, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
+	if (map1 == MAP_FAILED) {
+		atf_tc_fail_nonfatal("mmap1 hint=%p: errno=%d", hint, errno);
+		goto out;
+	}
+	map2 = mmap(map1, page, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
+	if (map2 == MAP_FAILED) {
+		atf_tc_fail_nonfatal("mmap2 hint=%p map1=%p failed: errno=%d",
+		    hint, map1, errno);
+		goto out;
+	}
+	map3 = mmap(hint, page, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
+	if (map3 == MAP_FAILED) {
+		atf_tc_fail_nonfatal("mmap3 hint=%p map1=%p failed: errno=%d",
+		    hint, map1, errno);
+		goto out;
+	}
+out:
+	if (map3 != MAP_FAILED) {
+		ATF_CHECK_MSG(munmap(map3, page) == 0, "munmap3 %p hint=%p",
+		    map3, hint);
+	}
+	if (map2 != MAP_FAILED) {
+		ATF_CHECK_MSG(munmap(map2, page) == 0, "munmap2 %p hint=%p",
+		    map2, hint);
+	}
+	if (map1 != MAP_FAILED) {
+		ATF_CHECK_MSG(munmap(map1, page) == 0, "munmap1 %p hint=%p",
+		    map1, hint);
+	}
+}
+
+ATF_TC(mmap_hint);
+ATF_TC_HEAD(mmap_hint, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test mmap with hints");
+}
+ATF_TC_BODY(mmap_hint, tc)
+{
+	static const int minaddress_mib[] = { CTL_VM, VM_MINADDRESS };
+	static const int maxaddress_mib[] = { CTL_VM, VM_MAXADDRESS };
+	long minaddress, maxaddress;
+	size_t minaddresssz = sizeof(minaddress);
+	size_t maxaddresssz = sizeof(maxaddress);
+
+	ATF_REQUIRE_MSG(sysctl(minaddress_mib, __arraycount(minaddress_mib),
+		&minaddress, &minaddresssz, NULL, 0) == 0,
+	    "sysctl vm.minaddress: errno=%d", errno);
+	ATF_REQUIRE_MSG(sysctl(maxaddress_mib, __arraycount(maxaddress_mib),
+		&maxaddress, &maxaddresssz, NULL, 0) == 0,
+	    "sysctl vm.maxaddress: errno=%d", errno);
+
+	test_mmap_hint(0);
+	test_mmap_hint(-1);
+	test_mmap_hint(page);
+	test_mmap_hint(minaddress);
+	test_mmap_hint(maxaddress);
+}
+
 ATF_TP_ADD_TCS(tp)
 {
 	page = sysconf(_SC_PAGESIZE);
@@ -634,6 +699,7 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, mmap_truncate);
 	ATF_TP_ADD_TC(tp, mmap_truncate_signal);
 	ATF_TP_ADD_TC(tp, mmap_va0);
+	ATF_TP_ADD_TC(tp, mmap_hint);
 
 	return atf_no_error();
 }

Reply via email to