1314817Sngie/* $NetBSD: t_mincore.c,v 1.10 2017/01/14 20:51:13 christos Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code is derived from software contributed to The NetBSD Foundation
8272343Sngie * by Jukka Ruohonen.
9272343Sngie *
10272343Sngie * Redistribution and use in source and binary forms, with or without
11272343Sngie * modification, are permitted provided that the following conditions
12272343Sngie * are met:
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in the
17272343Sngie *    documentation and/or other materials provided with the distribution.
18272343Sngie *
19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29272343Sngie * POSSIBILITY OF SUCH DAMAGE.
30272343Sngie */
31272343Sngie
32272343Sngie/*-
33272343Sngie * Copyright (c) 1999 The NetBSD Foundation, Inc.
34272343Sngie * All rights reserved.
35272343Sngie *
36272343Sngie * This code is derived from software contributed to The NetBSD Foundation
37272343Sngie * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
38272343Sngie * NASA Ames Research Center.
39272343Sngie *
40272343Sngie * Redistribution and use in source and binary forms, with or without
41272343Sngie * modification, are permitted provided that the following conditions
42272343Sngie * are met:
43272343Sngie * 1. Redistributions of source code must retain the above copyright
44272343Sngie *    notice, this list of conditions and the following disclaimer.
45272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
46272343Sngie *    notice, this list of conditions and the following disclaimer in the
47272343Sngie *    documentation and/or other materials provided with the distribution.
48272343Sngie *
49272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59272343Sngie * POSSIBILITY OF SUCH DAMAGE.
60272343Sngie */
61272343Sngie#include <sys/cdefs.h>
62314817Sngie__RCSID("$NetBSD: t_mincore.c,v 1.10 2017/01/14 20:51:13 christos Exp $");
63272343Sngie
64272343Sngie#include <sys/mman.h>
65313535Sngie#include <sys/stat.h>
66272343Sngie#include <sys/shm.h>
67272343Sngie
68272343Sngie#include <atf-c.h>
69272343Sngie#include <errno.h>
70272343Sngie#include <fcntl.h>
71272343Sngie#include <kvm.h>
72272343Sngie#include <stdio.h>
73272343Sngie#include <stdlib.h>
74272343Sngie#include <string.h>
75272343Sngie#include <unistd.h>
76272343Sngie#include <sys/resource.h>
77272343Sngie
78272343Sngiestatic long		page = 0;
79272343Sngiestatic const char	path[] = "mincore";
80272343Sngiestatic size_t		check_residency(void *, size_t);
81272343Sngie
82272343Sngiestatic size_t
83272343Sngiecheck_residency(void *addr, size_t npgs)
84272343Sngie{
85272343Sngie	size_t i, resident;
86272343Sngie	char *vec;
87272343Sngie
88272343Sngie	vec = malloc(npgs);
89272343Sngie
90272343Sngie	ATF_REQUIRE(vec != NULL);
91272343Sngie	ATF_REQUIRE(mincore(addr, npgs * page, vec) == 0);
92272343Sngie
93272343Sngie	for (i = resident = 0; i < npgs; i++) {
94272343Sngie
95272343Sngie		if (vec[i] != 0)
96272343Sngie			resident++;
97272343Sngie
98272343Sngie#if 0
99272343Sngie		(void)fprintf(stderr, "page 0x%p is %sresident\n",
100272343Sngie		    (char *)addr + (i * page), vec[i] ? "" : "not ");
101272343Sngie#endif
102272343Sngie	}
103272343Sngie
104272343Sngie	free(vec);
105272343Sngie
106272343Sngie	return resident;
107272343Sngie}
108272343Sngie
109272343SngieATF_TC(mincore_err);
110272343SngieATF_TC_HEAD(mincore_err, tc)
111272343Sngie{
112272343Sngie	atf_tc_set_md_var(tc, "descr", "Test errors from mincore(2)");
113272343Sngie}
114272343Sngie
115272343SngieATF_TC_BODY(mincore_err, tc)
116272343Sngie{
117272343Sngie	char *map, *vec;
118272343Sngie
119272343Sngie	map = mmap(NULL, page, PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
120272343Sngie	vec = malloc(page);
121272343Sngie
122272343Sngie	ATF_REQUIRE(vec != NULL);
123272343Sngie	ATF_REQUIRE(map != MAP_FAILED);
124272343Sngie
125276478Sngie#ifdef __NetBSD__
126272343Sngie	errno = 0;
127272343Sngie	ATF_REQUIRE_ERRNO(EINVAL, mincore(map, 0, vec) == -1);
128276478Sngie#endif
129272343Sngie
130272343Sngie	errno = 0;
131272343Sngie	ATF_REQUIRE_ERRNO(ENOMEM, mincore(0, page, vec) == -1);
132272343Sngie
133272343Sngie	errno = 0;
134272343Sngie	ATF_REQUIRE_ERRNO(EFAULT, mincore(map, page, (void *)-1) == -1);
135272343Sngie
136272343Sngie	free(vec);
137272343Sngie	ATF_REQUIRE(munmap(map, page) == 0);
138272343Sngie}
139272343Sngie
140272343SngieATF_TC_WITH_CLEANUP(mincore_resid);
141272343SngieATF_TC_HEAD(mincore_resid, tc)
142272343Sngie{
143272343Sngie	atf_tc_set_md_var(tc, "descr", "Test page residency with mincore(2)");
144310507Sngie	atf_tc_set_md_var(tc, "require.user", "root");
145272343Sngie}
146272343Sngie
147272343SngieATF_TC_BODY(mincore_resid, tc)
148272343Sngie{
149272343Sngie	void *addr, *addr2, *addr3, *buf;
150272343Sngie	size_t npgs = 0, resident;
151272343Sngie	struct stat st;
152272343Sngie	int fd, rv;
153272343Sngie	struct rlimit rlim;
154272343Sngie
155272343Sngie	ATF_REQUIRE(getrlimit(RLIMIT_MEMLOCK, &rlim) == 0);
156310507Sngie	/*
157310507Sngie	 * Bump the mlock limit to unlimited so the rest of the testcase
158310507Sngie	 * passes instead of failing on the mlock call.
159310507Sngie	 */
160310507Sngie	rlim.rlim_max = RLIM_INFINITY;
161272343Sngie	rlim.rlim_cur = rlim.rlim_max;
162272343Sngie	ATF_REQUIRE(setrlimit(RLIMIT_MEMLOCK, &rlim) == 0);
163272343Sngie
164272343Sngie	(void)memset(&st, 0, sizeof(struct stat));
165272343Sngie
166272343Sngie	fd = open(path, O_RDWR | O_CREAT, 0700);
167272343Sngie	buf = malloc(page * 5);
168272343Sngie
169272343Sngie	ATF_REQUIRE(fd >= 0);
170272343Sngie	ATF_REQUIRE(buf != NULL);
171272343Sngie
172272343Sngie	rv = write(fd, buf, page * 5);
173272343Sngie	ATF_REQUIRE(rv >= 0);
174272343Sngie
175272343Sngie	ATF_REQUIRE(fd >= 0);
176272343Sngie	ATF_REQUIRE(fstat(fd, &st) == 0);
177272343Sngie
178272343Sngie	addr = mmap(NULL, (size_t)st.st_size, PROT_READ,
179272343Sngie	    MAP_FILE | MAP_SHARED, fd, (off_t) 0);
180272343Sngie
181272343Sngie	ATF_REQUIRE(addr != MAP_FAILED);
182272343Sngie
183272343Sngie	(void)close(fd);
184272343Sngie
185272343Sngie	npgs = st.st_size / page;
186272343Sngie
187272343Sngie	if (st.st_size % page != 0)
188272343Sngie		npgs++;
189272343Sngie
190272343Sngie	(void)check_residency(addr, npgs);
191272343Sngie
192272343Sngie	rv = mlock(addr, npgs * page);
193272343Sngie	if (rv == -1 && errno == EAGAIN)
194272343Sngie		atf_tc_skip("hit process resource limits");
195272343Sngie	ATF_REQUIRE(munmap(addr, st.st_size) == 0);
196272343Sngie
197272343Sngie	npgs = 128;
198272343Sngie
199276478Sngie#ifdef __FreeBSD__
200272343Sngie	addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE,
201276478Sngie	    MAP_ANON | MAP_PRIVATE, -1, (off_t)0);
202276478Sngie#else
203276478Sngie	addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE,
204272343Sngie	    MAP_ANON | MAP_PRIVATE | MAP_WIRED, -1, (off_t)0);
205276478Sngie#endif
206272343Sngie
207272343Sngie	if (addr == MAP_FAILED)
208272343Sngie		atf_tc_skip("could not mmap wired anonymous test area, system "
209272343Sngie		    "might be low on memory");
210272343Sngie
211276478Sngie#ifdef __FreeBSD__
212310507Sngie	if (mlock(addr, npgs * page) == -1 && errno != ENOMEM)
213310507Sngie		atf_tc_skip("could not wire anonymous test area, system might "
214310507Sngie		    "be low on memory");
215276478Sngie#endif
216272343Sngie	ATF_REQUIRE(check_residency(addr, npgs) == npgs);
217272343Sngie	ATF_REQUIRE(munmap(addr, npgs * page) == 0);
218272343Sngie
219272343Sngie	npgs = 128;
220272343Sngie
221272343Sngie	addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE,
222272343Sngie	    MAP_ANON | MAP_PRIVATE, -1, (off_t)0);
223272343Sngie
224272343Sngie	ATF_REQUIRE(addr != MAP_FAILED);
225272343Sngie
226272343Sngie	/*
227272343Sngie	 * Check that the in-core pages match the locked pages.
228272343Sngie	 */
229272343Sngie	ATF_REQUIRE(check_residency(addr, npgs) == 0);
230272343Sngie
231272343Sngie	errno = 0;
232272343Sngie	if (mlockall(MCL_CURRENT|MCL_FUTURE) != 0 && errno != ENOMEM)
233272343Sngie		atf_tc_fail("mlockall(2) failed");
234272343Sngie	if (errno == ENOMEM)
235272343Sngie		atf_tc_skip("mlockall() exceeded process resource limits");
236272343Sngie
237272343Sngie	resident = check_residency(addr, npgs);
238272343Sngie	if (resident < npgs)
239272343Sngie		atf_tc_fail("mlockall(MCL_FUTURE) succeeded, still only "
240272343Sngie		    "%zu pages of the newly mapped %zu pages are resident",
241272343Sngie		    resident, npgs);
242272343Sngie
243272343Sngie	addr2 = mmap(NULL, npgs * page, PROT_READ, MAP_ANON, -1, (off_t)0);
244272343Sngie	addr3 = mmap(NULL, npgs * page, PROT_NONE, MAP_ANON, -1, (off_t)0);
245272343Sngie
246272343Sngie	if (addr2 == MAP_FAILED || addr3 == MAP_FAILED)
247272343Sngie		atf_tc_skip("could not mmap more anonymous test pages with "
248272343Sngie		    "mlockall(MCL_FUTURE) in effect, system "
249272343Sngie		    "might be low on memory");
250272343Sngie
251272343Sngie	ATF_REQUIRE(check_residency(addr2, npgs) == npgs);
252272343Sngie	ATF_REQUIRE(check_residency(addr3, npgs) == 0);
253272343Sngie	ATF_REQUIRE(mprotect(addr3, npgs * page, PROT_READ) == 0);
254272343Sngie	ATF_REQUIRE(check_residency(addr, npgs) == npgs);
255272343Sngie	ATF_REQUIRE(check_residency(addr2, npgs) == npgs);
256272343Sngie
257272343Sngie	(void)munlockall();
258272343Sngie
259272343Sngie	ATF_REQUIRE(madvise(addr2, npgs * page, MADV_FREE) == 0);
260276478Sngie#ifdef __NetBSD__
261272343Sngie	ATF_REQUIRE(check_residency(addr2, npgs) == 0);
262276478Sngie#endif
263272343Sngie
264272343Sngie	(void)memset(addr, 0, npgs * page);
265272343Sngie
266272343Sngie	ATF_REQUIRE(madvise(addr, npgs * page, MADV_FREE) == 0);
267276478Sngie#ifdef __NetBSD__
268272343Sngie	ATF_REQUIRE(check_residency(addr, npgs) == 0);
269276478Sngie#endif
270272343Sngie
271272343Sngie	(void)munmap(addr, npgs * page);
272272343Sngie	(void)munmap(addr2, npgs * page);
273272343Sngie	(void)munmap(addr3, npgs * page);
274272343Sngie	(void)unlink(path);
275311619Sngie	free(buf);
276272343Sngie}
277272343Sngie
278272343SngieATF_TC_CLEANUP(mincore_resid, tc)
279272343Sngie{
280272343Sngie	(void)unlink(path);
281272343Sngie}
282272343Sngie
283272343SngieATF_TC(mincore_shmseg);
284272343SngieATF_TC_HEAD(mincore_shmseg, tc)
285272343Sngie{
286272343Sngie	atf_tc_set_md_var(tc, "descr", "residency of shared memory");
287272343Sngie}
288272343Sngie
289272343SngieATF_TC_BODY(mincore_shmseg, tc)
290272343Sngie{
291272343Sngie	size_t npgs = 128;
292272343Sngie	void *addr = NULL;
293272343Sngie	int shmid;
294272343Sngie
295272343Sngie	shmid = shmget(IPC_PRIVATE, npgs * page,
296272343Sngie	    IPC_CREAT | S_IRUSR | S_IWUSR);
297272343Sngie
298272343Sngie	ATF_REQUIRE(shmid != -1);
299272343Sngie
300272343Sngie	addr = shmat(shmid, NULL, 0);
301272343Sngie
302272343Sngie	ATF_REQUIRE(addr != NULL);
303272343Sngie	ATF_REQUIRE(check_residency(addr, npgs) == 0);
304272343Sngie
305272343Sngie	(void)memset(addr, 0xff, npgs * page);
306272343Sngie
307272343Sngie	ATF_REQUIRE(check_residency(addr, npgs) == npgs);
308272343Sngie	ATF_REQUIRE(madvise(addr, npgs * page, MADV_FREE) == 0);
309272343Sngie
310272343Sngie	/*
311272343Sngie	 * NOTE!  Even though we have MADV_FREE'd the range,
312272343Sngie	 * there is another reference (the kernel's) to the
313272343Sngie	 * object which owns the pages.  In this case, the
314272343Sngie	 * kernel does not simply free the pages, as haphazardly
315272343Sngie	 * freeing pages when there are still references to
316272343Sngie	 * an object can cause data corruption (say, the other
317272343Sngie	 * referencer doesn't expect the pages to be freed,
318272343Sngie	 * and is surprised by the subsequent ZFOD).
319272343Sngie	 *
320272343Sngie	 * Because of this, we simply report the number of
321272343Sngie	 * pages still resident, for information only.
322272343Sngie	 */
323272343Sngie	npgs = check_residency(addr, npgs);
324272343Sngie
325272343Sngie	(void)fprintf(stderr, "%zu pages still resident\n", npgs);
326272343Sngie
327272343Sngie	ATF_REQUIRE(shmdt(addr) == 0);
328272343Sngie	ATF_REQUIRE(shmctl(shmid, IPC_RMID, NULL) == 0);
329272343Sngie}
330272343Sngie
331272343SngieATF_TP_ADD_TCS(tp)
332272343Sngie{
333272343Sngie
334272343Sngie	page = sysconf(_SC_PAGESIZE);
335272343Sngie	ATF_REQUIRE(page >= 0);
336272343Sngie
337272343Sngie	ATF_TP_ADD_TC(tp, mincore_err);
338272343Sngie	ATF_TP_ADD_TC(tp, mincore_resid);
339272343Sngie	ATF_TP_ADD_TC(tp, mincore_shmseg);
340272343Sngie
341272343Sngie	return atf_no_error();
342272343Sngie}
343