xref: /freebsd/tests/sys/vm/mmap_test.c (revision bd81e07d2761cf1c13063eb49a5c0cb4a6951318)
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 #include <fcntl.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 
40 static const struct {
41 	void	*addr;
42 	int	ok[2];	/* Depending on security.bsd.map_at_zero {0, !=0}. */
43 } map_at_zero_tests[] = {
44 	{ (void *)0,			{ 0, 1 } }, /* Test sysctl. */
45 	{ (void *)1,			{ 0, 0 } },
46 	{ (void *)(PAGE_SIZE - 1),	{ 0, 0 } },
47 	{ (void *)PAGE_SIZE,		{ 1, 1 } },
48 	{ (void *)-1,			{ 0, 0 } },
49 	{ (void *)(-PAGE_SIZE),		{ 0, 0 } },
50 	{ (void *)(-1 - PAGE_SIZE),	{ 0, 0 } },
51 	{ (void *)(-1 - PAGE_SIZE - 1),	{ 0, 0 } },
52 	{ (void *)(0x1000 * PAGE_SIZE),	{ 1, 1 } },
53 };
54 
55 #define	MAP_AT_ZERO	"security.bsd.map_at_zero"
56 
57 ATF_TC_WITHOUT_HEAD(mmap__map_at_zero);
58 ATF_TC_BODY(mmap__map_at_zero, tc)
59 {
60 	void *p;
61 	size_t len;
62 	unsigned int i;
63 	int map_at_zero;
64 
65 	len = sizeof(map_at_zero);
66 	if (sysctlbyname(MAP_AT_ZERO, &map_at_zero, &len, NULL, 0) == -1) {
67 		atf_tc_skip("sysctl for %s failed: %s\n", MAP_AT_ZERO,
68 		    strerror(errno));
69 		return;
70 	}
71 
72 	/* Normalize to 0 or 1 for array access. */
73 	map_at_zero = !!map_at_zero;
74 
75 	for (i = 0; i < nitems(map_at_zero_tests); i++) {
76 		p = mmap((void *)map_at_zero_tests[i].addr, PAGE_SIZE,
77 		    PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_FIXED,
78 		    -1, 0);
79 		if (p == MAP_FAILED) {
80 			ATF_CHECK_MSG(map_at_zero_tests[i].ok[map_at_zero] == 0,
81 			    "mmap(%p, ...) failed", map_at_zero_tests[i].addr);
82 		} else {
83 			ATF_CHECK_MSG(map_at_zero_tests[i].ok[map_at_zero] == 1,
84 			    "mmap(%p, ...) succeeded: p=%p\n",
85 			    map_at_zero_tests[i].addr, p);
86 		}
87 	}
88 }
89 
90 static void
91 checked_mmap(int prot, int flags, int fd, int error, const char *msg)
92 {
93 	void *p;
94 
95 	p = mmap(NULL, getpagesize(), prot, flags, fd, 0);
96 	if (p == MAP_FAILED) {
97 		if (error == 0)
98 			ATF_CHECK_MSG(0, "%s failed with errno %d", msg,
99 			    errno);
100 		else
101 			ATF_CHECK_EQ_MSG(error, errno,
102 			    "%s failed with wrong errno %d (expected %d)", msg,
103 			    errno, error);
104 	} else {
105 		ATF_CHECK_MSG(error == 0, "%s succeeded", msg);
106 		munmap(p, getpagesize());
107 	}
108 }
109 
110 ATF_TC_WITHOUT_HEAD(mmap__bad_arguments);
111 ATF_TC_BODY(mmap__bad_arguments, tc)
112 {
113 	int fd;
114 
115 	ATF_REQUIRE((fd = shm_open(SHM_ANON, O_RDWR, 0644)) >= 0);
116 	ATF_REQUIRE(ftruncate(fd, getpagesize()) == 0);
117 
118 	/* These should work. */
119 	checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON, -1, 0,
120 	    "simple MAP_ANON");
121 	checked_mmap(PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0,
122 	    "simple shm fd shared");
123 	checked_mmap(PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0,
124 	    "simple shm fd private");
125 
126 	/* Extra PROT flags. */
127 	checked_mmap(PROT_READ | PROT_WRITE | 0x100000, MAP_ANON, -1, EINVAL,
128 	    "MAP_ANON with extra PROT flags");
129 	checked_mmap(0xffff, MAP_SHARED, fd, EINVAL,
130 	    "shm fd with garbage PROT");
131 
132 	/* Undefined flag. */
133 	checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_RESERVED0080, -1,
134 	    EINVAL, "Undefined flag");
135 
136 	/* Both MAP_SHARED and MAP_PRIVATE */
137 	checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE |
138 	    MAP_SHARED, -1, EINVAL, "MAP_ANON with both SHARED and PRIVATE");
139 	checked_mmap(PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_SHARED, fd,
140 	    EINVAL, "shm fd with both SHARED and PRIVATE");
141 
142 	/* At least one of MAP_SHARED or MAP_PRIVATE without ANON */
143 	checked_mmap(PROT_READ | PROT_WRITE, 0, fd, EINVAL,
144 	    "shm fd without sharing flag");
145 
146 	/* MAP_ANON with either sharing flag (impacts fork). */
147 	checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0,
148 	    "shared MAP_ANON");
149 	checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0,
150 	    "private MAP_ANON");
151 
152 	/* MAP_ANON should require an fd of -1. */
153 	checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, EINVAL,
154 	    "MAP_ANON with fd != -1");
155 }
156 
157 ATF_TP_ADD_TCS(tp)
158 {
159 
160 	ATF_TP_ADD_TC(tp, mmap__map_at_zero);
161 	ATF_TP_ADD_TC(tp, mmap__bad_arguments);
162 
163 	return (atf_no_error());
164 }
165