1 /* $NetBSD: t_mincore.c,v 1.10 2017/01/14 20:51:13 christos Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 * Copyright (c) 1999 The NetBSD Foundation, Inc.
34 * All rights reserved.
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.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
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.
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.
61 #include <sys/cdefs.h>
62 __RCSID("$NetBSD: t_mincore.c,v 1.10 2017/01/14 20:51:13 christos Exp $");
76 #include <sys/resource.h>
79 static const char path[] = "mincore";
80 static size_t check_residency(void *, size_t);
83 check_residency(void *addr, size_t npgs)
90 ATF_REQUIRE(vec != NULL);
91 ATF_REQUIRE(mincore(addr, npgs * page, vec) == 0);
93 for (i = resident = 0; i < npgs; i++) {
99 (void)fprintf(stderr, "page 0x%p is %sresident\n",
100 (char *)addr + (i * page), vec[i] ? "" : "not ");
110 ATF_TC_HEAD(mincore_err, tc)
112 atf_tc_set_md_var(tc, "descr", "Test errors from mincore(2)");
115 ATF_TC_BODY(mincore_err, tc)
119 map = mmap(NULL, page, PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
122 ATF_REQUIRE(vec != NULL);
123 ATF_REQUIRE(map != MAP_FAILED);
127 ATF_REQUIRE_ERRNO(EINVAL, mincore(map, 0, vec) == -1);
131 ATF_REQUIRE_ERRNO(ENOMEM, mincore(0, page, vec) == -1);
134 ATF_REQUIRE_ERRNO(EFAULT, mincore(map, page, (void *)-1) == -1);
137 ATF_REQUIRE(munmap(map, page) == 0);
140 ATF_TC_WITH_CLEANUP(mincore_resid);
141 ATF_TC_HEAD(mincore_resid, tc)
143 atf_tc_set_md_var(tc, "descr", "Test page residency with mincore(2)");
144 atf_tc_set_md_var(tc, "require.user", "root");
147 ATF_TC_BODY(mincore_resid, tc)
149 void *addr, *addr2, *addr3, *buf;
150 size_t npgs = 0, resident;
155 ATF_REQUIRE(getrlimit(RLIMIT_MEMLOCK, &rlim) == 0);
157 * Bump the mlock limit to unlimited so the rest of the testcase
158 * passes instead of failing on the mlock call.
160 rlim.rlim_max = RLIM_INFINITY;
161 rlim.rlim_cur = rlim.rlim_max;
162 ATF_REQUIRE(setrlimit(RLIMIT_MEMLOCK, &rlim) == 0);
164 (void)memset(&st, 0, sizeof(struct stat));
166 fd = open(path, O_RDWR | O_CREAT, 0700);
167 buf = malloc(page * 5);
169 ATF_REQUIRE(fd >= 0);
170 ATF_REQUIRE(buf != NULL);
172 rv = write(fd, buf, page * 5);
173 ATF_REQUIRE(rv >= 0);
175 ATF_REQUIRE(fd >= 0);
176 ATF_REQUIRE(fstat(fd, &st) == 0);
178 addr = mmap(NULL, (size_t)st.st_size, PROT_READ,
179 MAP_FILE | MAP_SHARED, fd, (off_t) 0);
181 ATF_REQUIRE(addr != MAP_FAILED);
185 npgs = st.st_size / page;
187 if (st.st_size % page != 0)
190 (void)check_residency(addr, npgs);
192 rv = mlock(addr, npgs * page);
193 if (rv == -1 && errno == EAGAIN)
194 atf_tc_skip("hit process resource limits");
195 ATF_REQUIRE(munmap(addr, st.st_size) == 0);
200 addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE,
201 MAP_ANON | MAP_PRIVATE, -1, (off_t)0);
203 addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE,
204 MAP_ANON | MAP_PRIVATE | MAP_WIRED, -1, (off_t)0);
207 if (addr == MAP_FAILED)
208 atf_tc_skip("could not mmap wired anonymous test area, system "
209 "might be low on memory");
212 if (mlock(addr, npgs * page) == -1 && errno != ENOMEM)
213 atf_tc_skip("could not wire anonymous test area, system might "
216 ATF_REQUIRE(check_residency(addr, npgs) == npgs);
217 ATF_REQUIRE(munmap(addr, npgs * page) == 0);
221 addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE,
222 MAP_ANON | MAP_PRIVATE, -1, (off_t)0);
224 ATF_REQUIRE(addr != MAP_FAILED);
227 * Check that the in-core pages match the locked pages.
229 ATF_REQUIRE(check_residency(addr, npgs) == 0);
232 if (mlockall(MCL_CURRENT|MCL_FUTURE) != 0 && errno != ENOMEM)
233 atf_tc_fail("mlockall(2) failed");
235 atf_tc_skip("mlockall() exceeded process resource limits");
237 resident = check_residency(addr, npgs);
239 atf_tc_fail("mlockall(MCL_FUTURE) succeeded, still only "
240 "%zu pages of the newly mapped %zu pages are resident",
243 addr2 = mmap(NULL, npgs * page, PROT_READ, MAP_ANON, -1, (off_t)0);
244 addr3 = mmap(NULL, npgs * page, PROT_NONE, MAP_ANON, -1, (off_t)0);
246 if (addr2 == MAP_FAILED || addr3 == MAP_FAILED)
247 atf_tc_skip("could not mmap more anonymous test pages with "
248 "mlockall(MCL_FUTURE) in effect, system "
249 "might be low on memory");
251 ATF_REQUIRE(check_residency(addr2, npgs) == npgs);
252 ATF_REQUIRE(check_residency(addr3, npgs) == 0);
253 ATF_REQUIRE(mprotect(addr3, npgs * page, PROT_READ) == 0);
254 ATF_REQUIRE(check_residency(addr, npgs) == npgs);
255 ATF_REQUIRE(check_residency(addr2, npgs) == npgs);
259 ATF_REQUIRE(madvise(addr2, npgs * page, MADV_FREE) == 0);
261 ATF_REQUIRE(check_residency(addr2, npgs) == 0);
264 (void)memset(addr, 0, npgs * page);
266 ATF_REQUIRE(madvise(addr, npgs * page, MADV_FREE) == 0);
268 ATF_REQUIRE(check_residency(addr, npgs) == 0);
271 (void)munmap(addr, npgs * page);
272 (void)munmap(addr2, npgs * page);
273 (void)munmap(addr3, npgs * page);
278 ATF_TC_CLEANUP(mincore_resid, tc)
283 ATF_TC(mincore_shmseg);
284 ATF_TC_HEAD(mincore_shmseg, tc)
286 atf_tc_set_md_var(tc, "descr", "residency of shared memory");
289 ATF_TC_BODY(mincore_shmseg, tc)
295 shmid = shmget(IPC_PRIVATE, npgs * page,
296 IPC_CREAT | S_IRUSR | S_IWUSR);
298 ATF_REQUIRE(shmid != -1);
300 addr = shmat(shmid, NULL, 0);
302 ATF_REQUIRE(addr != NULL);
303 ATF_REQUIRE(check_residency(addr, npgs) == 0);
305 (void)memset(addr, 0xff, npgs * page);
307 ATF_REQUIRE(check_residency(addr, npgs) == npgs);
308 ATF_REQUIRE(madvise(addr, npgs * page, MADV_FREE) == 0);
311 * NOTE! Even though we have MADV_FREE'd the range,
312 * there is another reference (the kernel's) to the
313 * object which owns the pages. In this case, the
314 * kernel does not simply free the pages, as haphazardly
315 * freeing pages when there are still references to
316 * an object can cause data corruption (say, the other
317 * referencer doesn't expect the pages to be freed,
318 * and is surprised by the subsequent ZFOD).
320 * Because of this, we simply report the number of
321 * pages still resident, for information only.
323 npgs = check_residency(addr, npgs);
325 (void)fprintf(stderr, "%zu pages still resident\n", npgs);
327 ATF_REQUIRE(shmdt(addr) == 0);
328 ATF_REQUIRE(shmctl(shmid, IPC_RMID, NULL) == 0);
334 page = sysconf(_SC_PAGESIZE);
335 ATF_REQUIRE(page >= 0);
337 ATF_TP_ADD_TC(tp, mincore_err);
338 ATF_TP_ADD_TC(tp, mincore_resid);
339 ATF_TP_ADD_TC(tp, mincore_shmseg);
341 return atf_no_error();