xref: /freebsd/tests/sys/vm/mmap_test.c (revision 95eee0d40b83e71ef8e2bf9f9f7870e8eb97c567)
1 /*-
2  * Copyright (c) 2009	Simon L. Nielsen <simon@FreeBSD.org>,
3  * 			Bjoern A. Zeeb <bz@FreeBSD.org>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/mman.h>
31 #include <sys/sysctl.h>
32 
33 #include <atf-c.h>
34 #include <errno.h>
35 
36 static const struct {
37 	void	*addr;
38 	int	ok[2];	/* Depending on security.bsd.map_at_zero {0, !=0}. */
39 } map_at_zero_tests[] = {
40 	{ (void *)0,			{ 0, 1 } }, /* Test sysctl. */
41 	{ (void *)1,			{ 0, 0 } },
42 	{ (void *)(PAGE_SIZE - 1),	{ 0, 0 } },
43 	{ (void *)PAGE_SIZE,		{ 1, 1 } },
44 	{ (void *)-1,			{ 0, 0 } },
45 	{ (void *)(-PAGE_SIZE),		{ 0, 0 } },
46 	{ (void *)(-1 - PAGE_SIZE),	{ 0, 0 } },
47 	{ (void *)(-1 - PAGE_SIZE - 1),	{ 0, 0 } },
48 	{ (void *)(0x1000 * PAGE_SIZE),	{ 1, 1 } },
49 };
50 
51 #define	MAP_AT_ZERO	"security.bsd.map_at_zero"
52 
53 ATF_TC_WITHOUT_HEAD(mmap__map_at_zero);
54 ATF_TC_BODY(mmap__map_at_zero, tc)
55 {
56 	void *p;
57 	size_t len;
58 	unsigned int i;
59 	int map_at_zero;
60 
61 	len = sizeof(map_at_zero);
62 	if (sysctlbyname(MAP_AT_ZERO, &map_at_zero, &len, NULL, 0) == -1) {
63 		atf_tc_skip("sysctl for %s failed: %s\n", MAP_AT_ZERO,
64 		    strerror(errno));
65 		return;
66 	}
67 
68 	/* Normalize to 0 or 1 for array access. */
69 	map_at_zero = !!map_at_zero;
70 
71 	for (i = 0; i < nitems(map_at_zero_tests); i++) {
72 		p = mmap((void *)map_at_zero_tests[i].addr, PAGE_SIZE,
73 		    PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_FIXED,
74 		    -1, 0);
75 		if (p == MAP_FAILED) {
76 			ATF_CHECK_MSG(map_at_zero_tests[i].ok[map_at_zero] == 0,
77 			    "mmap(%p, ...) failed", map_at_zero_tests[i].addr);
78 		} else {
79 			ATF_CHECK_MSG(map_at_zero_tests[i].ok[map_at_zero] == 1,
80 			    "mmap(%p, ...) succeeded: p=%p\n",
81 			    map_at_zero_tests[i].addr, p);
82 		}
83 	}
84 }
85 
86 ATF_TP_ADD_TCS(tp)
87 {
88 
89 	ATF_TP_ADD_TC(tp, mmap__map_at_zero);
90 
91 	return (atf_no_error());
92 }
93