t_mincore.c revision 1.2 1 /* $NetBSD: t_mincore.c,v 1.2 2011/07/14 08:09:48 jruoho 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.2 2011/07/14 08:09:48 jruoho 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 <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <unistd.h>
74
75 static long page = 0;
76 static const char path[] = "mincore";
77 static size_t check_residency(void *, size_t);
78
79 static size_t
80 check_residency(void *addr, size_t npgs)
81 {
82 size_t i, resident;
83 char *vec;
84
85 vec = malloc(npgs);
86
87 ATF_REQUIRE(vec != NULL);
88 ATF_REQUIRE(mincore(addr, npgs * page, vec) == 0);
89
90 for (i = resident = 0; i < npgs; i++) {
91
92 if (vec[i] != 0)
93 resident++;
94
95 (void)fprintf(stderr, "page 0x%p is %sresident\n",
96 (char *)addr + (i * page), vec[i] ? "" : "not ");
97 }
98
99 free(vec);
100
101 return resident;
102 }
103
104 ATF_TC(mincore_err);
105 ATF_TC_HEAD(mincore_err, tc)
106 {
107 atf_tc_set_md_var(tc, "descr", "Test errors from mincore(2)");
108 }
109
110 ATF_TC_BODY(mincore_err, tc)
111 {
112 char *map, *vec;
113
114 map = mmap(NULL, page, PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
115 vec = malloc(page);
116
117 ATF_REQUIRE(vec != NULL);
118 ATF_REQUIRE(map != MAP_FAILED);
119
120 errno = 0;
121 ATF_REQUIRE_ERRNO(EINVAL, mincore(map, 0, vec) == -1);
122
123 errno = 0;
124 ATF_REQUIRE_ERRNO(ENOMEM, mincore(0, page, vec) == -1);
125
126 errno = 0;
127 ATF_REQUIRE_ERRNO(EFAULT, mincore(map, page, (void *)-1) == -1);
128
129 free(vec);
130 ATF_REQUIRE(munmap(map, page) == 0);
131 }
132
133 ATF_TC_WITH_CLEANUP(mincore_incore);
134 ATF_TC_HEAD(mincore_incore, tc)
135 {
136 atf_tc_set_md_var(tc, "descr", "Test that mincore(2) works");
137 }
138
139 ATF_TC_BODY(mincore_incore, tc)
140 {
141 char *buf, *vec, *map = MAP_FAILED;
142 const char *str = NULL;
143 const size_t n = 3;
144 ssize_t tot;
145 int fd, rv;
146 size_t i, j;
147
148 /*
149 * Create a temporary file, write
150 * few pages to it, and map the file.
151 */
152 buf = calloc(n, page);
153 vec = calloc(n, page);
154
155 if (buf == NULL || vec == NULL)
156 return;
157
158 for (i = 0; i < (size_t)page * n; i++)
159 buf[i] = 'x';
160
161 fd = open(path, O_RDWR | O_CREAT, 0600);
162
163 if (fd < 0) {
164 str = "failed to open";
165 goto out;
166 }
167
168 tot = 0;
169
170 while (tot < page * (long)n) {
171
172 rv = write(fd, buf, sizeof(buf));
173
174 if (rv < 0) {
175 str = "failed to write";
176 goto out;
177 }
178
179 tot += rv;
180 }
181
182 map = mmap(NULL, page * n, PROT_READ | PROT_WRITE,
183 MAP_FILE | MAP_PRIVATE, fd, 0);
184
185 if (map == MAP_FAILED) {
186 str = "failed to map";
187 goto out;
188 }
189
190 /*
191 * Lock the mapping such that only
192 * in-core page status is returned.
193 */
194 if (mlock(map, page * n) != 0) {
195 str = "failed to lock";
196 goto out;
197 }
198
199 if (mincore(map, page * n, vec) != 0) {
200 str = "mincore failed";
201 goto out;
202 }
203
204 /*
205 * Check that the in-core pages
206 * match the locked pages.
207 */
208 for (i = j = 0; i < (size_t)page * n; i++) {
209
210 if (vec[i] != 0)
211 j++;
212 }
213
214 if (j != n)
215 str = "mismatch of in-core pages";
216
217 out:
218 free(buf);
219 free(vec);
220
221 (void)close(fd);
222 (void)unlink(path);
223
224 if (map != MAP_FAILED) {
225 (void)munlock(map, page);
226 (void)munmap(map, page);
227 }
228
229 if (str != NULL)
230 atf_tc_fail("%s", str);
231 }
232
233 ATF_TC_CLEANUP(mincore_incore, tc)
234 {
235 (void)unlink(path);
236 }
237
238 ATF_TC_WITH_CLEANUP(mincore_residency);
239 ATF_TC_HEAD(mincore_residency, tc)
240 {
241 atf_tc_set_md_var(tc, "descr", "mmap(2) residency with mincore(2)");
242 }
243
244 ATF_TC_BODY(mincore_residency, tc)
245 {
246 void *addr, *addr2, *addr3, *buf;
247 size_t npgs = 0;
248 struct stat st;
249 ssize_t tot;
250 int fd, rv;
251
252 (void)memset(&st, 0, sizeof(struct stat));
253
254 fd = open(path, O_RDWR | O_CREAT, 0700);
255 buf = malloc(page * 5);
256
257 ATF_REQUIRE(fd >= 0);
258 ATF_REQUIRE(buf != NULL);
259
260 tot = 0;
261
262 while (tot < page) {
263
264 rv = write(fd, buf, sizeof(buf));
265 ATF_REQUIRE(rv >= 0);
266
267 tot += rv;
268 }
269
270 ATF_REQUIRE(fd >= 0);
271 ATF_REQUIRE(fstat(fd, &st) == 0);
272
273 addr = mmap(NULL, (size_t)st.st_size, PROT_READ,
274 MAP_FILE | MAP_SHARED, fd, (off_t) 0);
275
276 ATF_REQUIRE(addr != MAP_FAILED);
277
278 (void)close(fd);
279
280 npgs = st.st_size / page;
281
282 if (st.st_size % page != 0)
283 npgs++;
284
285 (void)check_residency(addr, npgs);
286
287 ATF_REQUIRE(mlock(addr, npgs * page) == 0);
288 ATF_REQUIRE(munmap(addr, st.st_size) == 0);
289
290 npgs = 128;
291
292 addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE,
293 MAP_ANON | MAP_PRIVATE | MAP_WIRED, -1, (off_t)0);
294
295 if (addr == MAP_FAILED)
296 return;
297
298 ATF_REQUIRE(check_residency(addr, npgs) == npgs);
299 ATF_REQUIRE(munmap(addr, npgs * page) == 0);
300
301 npgs = 128;
302
303 addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE,
304 MAP_ANON | MAP_PRIVATE, -1, (off_t)0);
305
306 if (addr == MAP_FAILED)
307 return;
308
309 ATF_REQUIRE(check_residency(addr, npgs) == 0);
310 ATF_REQUIRE(mlockall(MCL_CURRENT|MCL_FUTURE) == 0);
311 ATF_REQUIRE(check_residency(addr, npgs) == npgs);
312
313 addr2 = mmap(NULL, npgs * page, PROT_READ, MAP_ANON, -1, (off_t)0);
314 addr3 = mmap(NULL, npgs * page, PROT_NONE, MAP_ANON, -1, (off_t)0);
315
316 if (addr2 == MAP_FAILED || addr3 == MAP_FAILED)
317 return;
318
319 ATF_REQUIRE(check_residency(addr2, npgs) == npgs);
320 ATF_REQUIRE(check_residency(addr3, npgs) == 0);
321 ATF_REQUIRE(mprotect(addr3, npgs * page, PROT_READ) == 0);
322 ATF_REQUIRE(check_residency(addr, npgs) == npgs);
323 ATF_REQUIRE(check_residency(addr2, npgs) == npgs);
324
325 (void)munlockall();
326
327 ATF_REQUIRE(madvise(addr2, npgs * page, MADV_FREE) == 0);
328 ATF_REQUIRE(check_residency(addr2, npgs) == 0);
329
330 (void)memset(addr, 0, npgs * page);
331
332 ATF_REQUIRE(madvise(addr, npgs * page, MADV_FREE) == 0);
333 ATF_REQUIRE(check_residency(addr, npgs) == 0);
334
335 (void)munmap(addr, npgs * page);
336 (void)munmap(addr2, npgs * page);
337 (void)munmap(addr3, npgs * page);
338 (void)unlink(path);
339 }
340
341 ATF_TC_CLEANUP(mincore_residency, tc)
342 {
343 (void)unlink(path);
344 }
345
346 ATF_TC(mincore_shmseg);
347 ATF_TC_HEAD(mincore_shmseg, tc)
348 {
349 atf_tc_set_md_var(tc, "descr", "residency of shared memory");
350 }
351
352 ATF_TC_BODY(mincore_shmseg, tc)
353 {
354 size_t npgs = 128;
355 void *addr = NULL;
356 int shmid;
357
358 shmid = shmget(IPC_PRIVATE, npgs * page,
359 IPC_CREAT | S_IRUSR | S_IWUSR);
360
361 ATF_REQUIRE(shmid != -1);
362
363 addr = shmat(shmid, NULL, 0);
364
365 ATF_REQUIRE(addr != NULL);
366 ATF_REQUIRE(check_residency(addr, npgs) == 0);
367
368 (void)memset(addr, 0xff, npgs * page);
369
370 ATF_REQUIRE(check_residency(addr, npgs) == npgs);
371 ATF_REQUIRE(madvise(addr, npgs * page, MADV_FREE) == 0);
372
373 /*
374 * NOTE! Even though we have MADV_FREE'd the range,
375 * there is another reference (the kernel's) to the
376 * object which owns the pages. In this case, the
377 * kernel does not simply free the pages, as haphazardly
378 * freeing pages when there are still references to
379 * an object can cause data corruption (say, the other
380 * referencer doesn't expect the pages to be freed,
381 * and is surprised by the subsequent ZFOD).
382 *
383 * Because of this, we simply report the number of
384 * pages still resident, for information only.
385 */
386 npgs = check_residency(addr, npgs);
387
388 (void)fprintf(stderr, "%zu pages still resident\n", npgs);
389
390 ATF_REQUIRE(shmdt(addr) == 0);
391 ATF_REQUIRE(shmctl(shmid, IPC_RMID, NULL) == 0);
392 }
393
394 ATF_TP_ADD_TCS(tp)
395 {
396
397 page = sysconf(_SC_PAGESIZE);
398 ATF_REQUIRE(page >= 0);
399
400 ATF_TP_ADD_TC(tp, mincore_err);
401 ATF_TP_ADD_TC(tp, mincore_incore);
402 ATF_TP_ADD_TC(tp, mincore_residency);
403 ATF_TP_ADD_TC(tp, mincore_shmseg);
404
405 return atf_no_error();
406 }
407