tests: Add MAP_32BIT flag test

Reviewed by:		kib, markj
Differential Revision:	https://reviews.freebsd.org/D41236
MFC after:		1 month
This commit is contained in:
Dmitry Chagin 2023-08-01 23:23:15 +03:00
parent 344b5bf825
commit 5a7e48dddf
3 changed files with 96 additions and 0 deletions

View File

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

View File

@ -0,0 +1,51 @@
/*-
* Copyright (c) 2023 Dmitry Chagin <dchagin@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);
}

View File

@ -0,0 +1,37 @@
#
# Copyright (c) 2022 Dmitry Chagin <dchagin@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
}