xref: /minix3/tests/lib/libc/sys/t_mincore.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1 /* $NetBSD: t_mincore.c,v 1.8 2012/06/08 07:18:58 martin Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*-
33  * Copyright (c) 1999 The NetBSD Foundation, Inc.
34  * All rights reserved.
35  *
36  * This code is derived from software contributed to The NetBSD Foundation
37  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
38  * NASA Ames Research Center.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59  * POSSIBILITY OF SUCH DAMAGE.
60  */
61 #include <sys/cdefs.h>
62 __RCSID("$NetBSD: t_mincore.c,v 1.8 2012/06/08 07:18:58 martin Exp $");
63 
64 #include <sys/mman.h>
65 #include <sys/shm.h>
66 
67 #include <atf-c.h>
68 #include <errno.h>
69 #include <fcntl.h>
70 #include <kvm.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <unistd.h>
75 #include <sys/resource.h>
76 
77 static long		page = 0;
78 static const char	path[] = "mincore";
79 static size_t		check_residency(void *, size_t);
80 
81 static size_t
check_residency(void * addr,size_t npgs)82 check_residency(void *addr, size_t npgs)
83 {
84 	size_t i, resident;
85 	char *vec;
86 
87 	vec = malloc(npgs);
88 
89 	ATF_REQUIRE(vec != NULL);
90 	ATF_REQUIRE(mincore(addr, npgs * page, vec) == 0);
91 
92 	for (i = resident = 0; i < npgs; i++) {
93 
94 		if (vec[i] != 0)
95 			resident++;
96 
97 #if 0
98 		(void)fprintf(stderr, "page 0x%p is %sresident\n",
99 		    (char *)addr + (i * page), vec[i] ? "" : "not ");
100 #endif
101 	}
102 
103 	free(vec);
104 
105 	return resident;
106 }
107 
108 ATF_TC(mincore_err);
ATF_TC_HEAD(mincore_err,tc)109 ATF_TC_HEAD(mincore_err, tc)
110 {
111 	atf_tc_set_md_var(tc, "descr", "Test errors from mincore(2)");
112 }
113 
ATF_TC_BODY(mincore_err,tc)114 ATF_TC_BODY(mincore_err, tc)
115 {
116 	char *map, *vec;
117 
118 	map = mmap(NULL, page, PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
119 	vec = malloc(page);
120 
121 	ATF_REQUIRE(vec != NULL);
122 	ATF_REQUIRE(map != MAP_FAILED);
123 
124 	errno = 0;
125 	ATF_REQUIRE_ERRNO(EINVAL, mincore(map, 0, vec) == -1);
126 
127 	errno = 0;
128 	ATF_REQUIRE_ERRNO(ENOMEM, mincore(0, page, vec) == -1);
129 
130 	errno = 0;
131 	ATF_REQUIRE_ERRNO(EFAULT, mincore(map, page, (void *)-1) == -1);
132 
133 	free(vec);
134 	ATF_REQUIRE(munmap(map, page) == 0);
135 }
136 
137 ATF_TC_WITH_CLEANUP(mincore_resid);
ATF_TC_HEAD(mincore_resid,tc)138 ATF_TC_HEAD(mincore_resid, tc)
139 {
140 	atf_tc_set_md_var(tc, "descr", "Test page residency with mincore(2)");
141 }
142 
ATF_TC_BODY(mincore_resid,tc)143 ATF_TC_BODY(mincore_resid, tc)
144 {
145 	void *addr, *addr2, *addr3, *buf;
146 	size_t npgs = 0, resident;
147 	struct stat st;
148 	int fd, rv;
149 	struct rlimit rlim;
150 
151 	ATF_REQUIRE(getrlimit(RLIMIT_MEMLOCK, &rlim) == 0);
152 	rlim.rlim_cur = rlim.rlim_max;
153 	ATF_REQUIRE(setrlimit(RLIMIT_MEMLOCK, &rlim) == 0);
154 
155 	(void)memset(&st, 0, sizeof(struct stat));
156 
157 	fd = open(path, O_RDWR | O_CREAT, 0700);
158 	buf = malloc(page * 5);
159 
160 	ATF_REQUIRE(fd >= 0);
161 	ATF_REQUIRE(buf != NULL);
162 
163 	rv = write(fd, buf, page * 5);
164 	ATF_REQUIRE(rv >= 0);
165 
166 	ATF_REQUIRE(fd >= 0);
167 	ATF_REQUIRE(fstat(fd, &st) == 0);
168 
169 	addr = mmap(NULL, (size_t)st.st_size, PROT_READ,
170 	    MAP_FILE | MAP_SHARED, fd, (off_t) 0);
171 
172 	ATF_REQUIRE(addr != MAP_FAILED);
173 
174 	(void)close(fd);
175 
176 	npgs = st.st_size / page;
177 
178 	if (st.st_size % page != 0)
179 		npgs++;
180 
181 	(void)check_residency(addr, npgs);
182 
183 	rv = mlock(addr, npgs * page);
184 	if (rv == -1 && errno == EAGAIN)
185 		atf_tc_skip("hit process resource limits");
186 	ATF_REQUIRE(munmap(addr, st.st_size) == 0);
187 
188 	npgs = 128;
189 
190 	addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE,
191 	    MAP_ANON | MAP_PRIVATE | MAP_WIRED, -1, (off_t)0);
192 
193 	if (addr == MAP_FAILED)
194 		atf_tc_skip("could not mmap wired anonymous test area, system "
195 		    "might be low on memory");
196 
197 	ATF_REQUIRE(check_residency(addr, npgs) == npgs);
198 	ATF_REQUIRE(munmap(addr, npgs * page) == 0);
199 
200 	npgs = 128;
201 
202 	addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE,
203 	    MAP_ANON | MAP_PRIVATE, -1, (off_t)0);
204 
205 	ATF_REQUIRE(addr != MAP_FAILED);
206 
207 	/*
208 	 * Check that the in-core pages match the locked pages.
209 	 */
210 	ATF_REQUIRE(check_residency(addr, npgs) == 0);
211 
212 	errno = 0;
213 	if (mlockall(MCL_CURRENT|MCL_FUTURE) != 0 && errno != ENOMEM)
214 		atf_tc_fail("mlockall(2) failed");
215 	if (errno == ENOMEM)
216 		atf_tc_skip("mlockall() exceeded process resource limits");
217 
218 	resident = check_residency(addr, npgs);
219 	if (resident < npgs)
220 		atf_tc_fail("mlockall(MCL_FUTURE) succeeded, still only "
221 		    "%zu pages of the newly mapped %zu pages are resident",
222 		    resident, npgs);
223 
224 	addr2 = mmap(NULL, npgs * page, PROT_READ, MAP_ANON, -1, (off_t)0);
225 	addr3 = mmap(NULL, npgs * page, PROT_NONE, MAP_ANON, -1, (off_t)0);
226 
227 	if (addr2 == MAP_FAILED || addr3 == MAP_FAILED)
228 		atf_tc_skip("could not mmap more anonymous test pages with "
229 		    "mlockall(MCL_FUTURE) in effect, system "
230 		    "might be low on memory");
231 
232 	ATF_REQUIRE(check_residency(addr2, npgs) == npgs);
233 	ATF_REQUIRE(check_residency(addr3, npgs) == 0);
234 	ATF_REQUIRE(mprotect(addr3, npgs * page, PROT_READ) == 0);
235 	ATF_REQUIRE(check_residency(addr, npgs) == npgs);
236 	ATF_REQUIRE(check_residency(addr2, npgs) == npgs);
237 
238 	(void)munlockall();
239 
240 	ATF_REQUIRE(madvise(addr2, npgs * page, MADV_FREE) == 0);
241 	ATF_REQUIRE(check_residency(addr2, npgs) == 0);
242 
243 	(void)memset(addr, 0, npgs * page);
244 
245 	ATF_REQUIRE(madvise(addr, npgs * page, MADV_FREE) == 0);
246 	ATF_REQUIRE(check_residency(addr, npgs) == 0);
247 
248 	(void)munmap(addr, npgs * page);
249 	(void)munmap(addr2, npgs * page);
250 	(void)munmap(addr3, npgs * page);
251 	(void)unlink(path);
252 }
253 
ATF_TC_CLEANUP(mincore_resid,tc)254 ATF_TC_CLEANUP(mincore_resid, tc)
255 {
256 	(void)unlink(path);
257 }
258 
259 ATF_TC(mincore_shmseg);
ATF_TC_HEAD(mincore_shmseg,tc)260 ATF_TC_HEAD(mincore_shmseg, tc)
261 {
262 	atf_tc_set_md_var(tc, "descr", "residency of shared memory");
263 }
264 
ATF_TC_BODY(mincore_shmseg,tc)265 ATF_TC_BODY(mincore_shmseg, tc)
266 {
267 	size_t npgs = 128;
268 	void *addr = NULL;
269 	int shmid;
270 
271 	shmid = shmget(IPC_PRIVATE, npgs * page,
272 	    IPC_CREAT | S_IRUSR | S_IWUSR);
273 
274 	ATF_REQUIRE(shmid != -1);
275 
276 	addr = shmat(shmid, NULL, 0);
277 
278 	ATF_REQUIRE(addr != NULL);
279 	ATF_REQUIRE(check_residency(addr, npgs) == 0);
280 
281 	(void)memset(addr, 0xff, npgs * page);
282 
283 	ATF_REQUIRE(check_residency(addr, npgs) == npgs);
284 	ATF_REQUIRE(madvise(addr, npgs * page, MADV_FREE) == 0);
285 
286 	/*
287 	 * NOTE!  Even though we have MADV_FREE'd the range,
288 	 * there is another reference (the kernel's) to the
289 	 * object which owns the pages.  In this case, the
290 	 * kernel does not simply free the pages, as haphazardly
291 	 * freeing pages when there are still references to
292 	 * an object can cause data corruption (say, the other
293 	 * referencer doesn't expect the pages to be freed,
294 	 * and is surprised by the subsequent ZFOD).
295 	 *
296 	 * Because of this, we simply report the number of
297 	 * pages still resident, for information only.
298 	 */
299 	npgs = check_residency(addr, npgs);
300 
301 	(void)fprintf(stderr, "%zu pages still resident\n", npgs);
302 
303 	ATF_REQUIRE(shmdt(addr) == 0);
304 	ATF_REQUIRE(shmctl(shmid, IPC_RMID, NULL) == 0);
305 }
306 
ATF_TP_ADD_TCS(tp)307 ATF_TP_ADD_TCS(tp)
308 {
309 
310 	page = sysconf(_SC_PAGESIZE);
311 	ATF_REQUIRE(page >= 0);
312 
313 	ATF_TP_ADD_TC(tp, mincore_err);
314 	ATF_TP_ADD_TC(tp, mincore_resid);
315 	ATF_TP_ADD_TC(tp, mincore_shmseg);
316 
317 	return atf_no_error();
318 }
319