The branch main has been updated by dchagin:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=5a7e48dddfb5a668ded4742b79e6f6f88b647e6a

commit 5a7e48dddfb5a668ded4742b79e6f6f88b647e6a
Author:     Dmitry Chagin <dcha...@freebsd.org>
AuthorDate: 2023-08-01 20:23:15 +0000
Commit:     Dmitry Chagin <dcha...@freebsd.org>
CommitDate: 2023-08-01 20:23:15 +0000

    tests: Add MAP_32BIT flag test
    
    Reviewed by:            kib, markj
    Differential Revision:  https://reviews.freebsd.org/D41236
    MFC after:              1 month
---
 tests/sys/vm/Makefile                |  8 ++++++
 tests/sys/vm/mmap_map_32bit_helper.c | 51 ++++++++++++++++++++++++++++++++++++
 tests/sys/vm/mmap_map_32bit_test.sh  | 37 ++++++++++++++++++++++++++
 3 files changed, 96 insertions(+)

diff --git a/tests/sys/vm/Makefile b/tests/sys/vm/Makefile
index 8ef8a45e5f39..9aac49bc5320 100644
--- a/tests/sys/vm/Makefile
+++ b/tests/sys/vm/Makefile
@@ -9,4 +9,12 @@ ATF_TESTS_C+=  mlock_test \
                page_fault_signal \
                shared_shadow_inval_test
 
+.if ${MACHINE_ARCH} != "i386" && ${MACHINE} != "arm" && \
+       (${MACHINE} != "powerpc" || ${MACHINE_ARCH} != "powerpc")
+       # MAP_32BIT is only available on 64-bit platforms
+BINDIR=                ${TESTSDIR}
+ATF_TESTS_SH+= mmap_map_32bit_test
+PROGS+=                mmap_map_32bit_helper
+.endif
+
 .include <bsd.test.mk>
diff --git a/tests/sys/vm/mmap_map_32bit_helper.c 
b/tests/sys/vm/mmap_map_32bit_helper.c
new file mode 100644
index 000000000000..47d4b2c53c20
--- /dev/null
+++ b/tests/sys/vm/mmap_map_32bit_helper.c
@@ -0,0 +1,51 @@
+/*-
+ * Copyright (c) 2023 Dmitry Chagin <dcha...@freebsd.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <sys/mman.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+_Static_assert(sizeof(vm_offset_t) >= 8, "Test is not intended for ILP32");
+#define MAP_32BIT_MAX_ADDR      ((vm_offset_t)1 << 31)
+
+int
+main(void)
+{
+       size_t pagesize;
+       void *s32;
+       int fd;
+
+       if ((pagesize = getpagesize()) <= 0)
+               err(1, "getpagesize");
+
+       fd = open("/dev/zero", O_RDONLY);
+       if (fd <= 0)
+               err(1, "open failed");
+
+       s32 = mmap(NULL, pagesize, PROT_READ, MAP_32BIT | MAP_PRIVATE, fd, 0);
+       if (s32 == MAP_FAILED)
+               err(1, "mmap MAP_32BIT | MAP_PRIVATE failed");
+       if (((vm_offset_t)s32 + pagesize) > MAP_32BIT_MAX_ADDR)
+               errx(1, "mmap invalid result %p", s32);
+
+       close(fd);
+       if (munmap(s32, pagesize) != 0)
+               err(1, "munmap failed");
+
+       s32 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE,
+           MAP_32BIT | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+       if (s32 == MAP_FAILED)
+               err(1, "mmap MAP_32BIT | MAP_ANONYMOUS | MAP_PRIVATE failed");
+       if (((vm_offset_t)s32 + pagesize) > MAP_32BIT_MAX_ADDR)
+               errx(1, "mmap invalid result %p", s32);
+
+       if (munmap(s32, pagesize) != 0)
+               err(1, "munmap failed");
+       exit(0);
+}
diff --git a/tests/sys/vm/mmap_map_32bit_test.sh 
b/tests/sys/vm/mmap_map_32bit_test.sh
new file mode 100644
index 000000000000..2df53e78f1b7
--- /dev/null
+++ b/tests/sys/vm/mmap_map_32bit_test.sh
@@ -0,0 +1,37 @@
+#
+# Copyright (c) 2022 Dmitry Chagin <dcha...@freebsd.org>
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Simple test of MAP_32BIT flag w/wo ASLR
+
+map_32bit_w_aslr_head()
+{
+       atf_set descr "MAP_32BIT with ASLR"
+       atf_set require.progs proccontrol
+}
+
+map_32bit_w_aslr_body()
+{
+       atf_check -s exit:0 -x proccontrol -m aslr -s enable \
+           $(atf_get_srcdir)/mmap_map_32bit_helper
+}
+
+map_32bit_wo_aslr_head()
+{
+       atf_set descr "MAP_32BIT without ASLR"
+       atf_set require.progs proccontrol
+}
+
+map_32bit_wo_aslr_body()
+{
+       atf_check -s exit:0 -x proccontrol -m aslr -s disable \
+           $(atf_get_srcdir)/mmap_map_32bit_helper
+}
+
+
+atf_init_test_cases()
+{
+       atf_add_test_case map_32bit_w_aslr
+       atf_add_test_case map_32bit_wo_aslr
+}

Reply via email to