Source-Changes-HG archive

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

[src/trunk]: src/tests/lib/libc/sys tests/lib/libc: Test mmap(2) with bad hints.



details:   https://anonhg.NetBSD.org/src/rev/c06d53d91c43
branches:  trunk
changeset: 366652:c06d53d91c43
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Sat Jun 04 23:09:18 2022 +0000

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

diffstat:

 tests/lib/libc/sys/t_mmap.c |  70 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 68 insertions(+), 2 deletions(-)

diffs (98 lines):

diff -r 92b349763a2b -r c06d53d91c43 tests/lib/libc/sys/t_mmap.c
--- a/tests/lib/libc/sys/t_mmap.c       Sat Jun 04 22:32:20 2022 +0000
+++ b/tests/lib/libc/sys/t_mmap.c       Sat Jun 04 23:09:18 2022 +0000
@@ -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 @@
        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_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();
 }



Home | Main Index | Thread Index | Old Index