t_mincore.c revision 1.3
1/* $NetBSD: t_mincore.c,v 1.3 2011/07/14 10:24:56 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.3 2011/07/14 10:24:56 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 75static long page = 0; 76static const char path[] = "mincore"; 77static size_t check_residency(void *, size_t); 78 79static size_t 80check_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 104ATF_TC(mincore_err); 105ATF_TC_HEAD(mincore_err, tc) 106{ 107 atf_tc_set_md_var(tc, "descr", "Test errors from mincore(2)"); 108} 109 110ATF_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 133ATF_TC_WITH_CLEANUP(mincore_resid); 134ATF_TC_HEAD(mincore_resid, tc) 135{ 136 atf_tc_set_md_var(tc, "descr", "Test page residency with mincore(2)"); 137} 138 139ATF_TC_BODY(mincore_resid, tc) 140{ 141 void *addr, *addr2, *addr3, *buf; 142 size_t npgs = 0; 143 struct stat st; 144 ssize_t tot; 145 int fd, rv; 146 147 (void)memset(&st, 0, sizeof(struct stat)); 148 149 fd = open(path, O_RDWR | O_CREAT, 0700); 150 buf = malloc(page * 5); 151 152 ATF_REQUIRE(fd >= 0); 153 ATF_REQUIRE(buf != NULL); 154 155 tot = 0; 156 157 while (tot < page) { 158 159 rv = write(fd, buf, sizeof(buf)); 160 ATF_REQUIRE(rv >= 0); 161 162 tot += rv; 163 } 164 165 ATF_REQUIRE(fd >= 0); 166 ATF_REQUIRE(fstat(fd, &st) == 0); 167 168 addr = mmap(NULL, (size_t)st.st_size, PROT_READ, 169 MAP_FILE | MAP_SHARED, fd, (off_t) 0); 170 171 ATF_REQUIRE(addr != MAP_FAILED); 172 173 (void)close(fd); 174 175 npgs = st.st_size / page; 176 177 if (st.st_size % page != 0) 178 npgs++; 179 180 (void)check_residency(addr, npgs); 181 182 ATF_REQUIRE(mlock(addr, npgs * page) == 0); 183 ATF_REQUIRE(munmap(addr, st.st_size) == 0); 184 185 npgs = 128; 186 187 addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE, 188 MAP_ANON | MAP_PRIVATE | MAP_WIRED, -1, (off_t)0); 189 190 if (addr == MAP_FAILED) 191 return; 192 193 ATF_REQUIRE(check_residency(addr, npgs) == npgs); 194 ATF_REQUIRE(munmap(addr, npgs * page) == 0); 195 196 npgs = 128; 197 198 addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE, 199 MAP_ANON | MAP_PRIVATE, -1, (off_t)0); 200 201 if (addr == MAP_FAILED) 202 return; 203 204 /* 205 * Check that the in-core pages match the locked pages. 206 */ 207 ATF_REQUIRE(check_residency(addr, npgs) == 0); 208 ATF_REQUIRE(mlockall(MCL_CURRENT|MCL_FUTURE) == 0); 209 ATF_REQUIRE(check_residency(addr, npgs) == npgs); 210 211 addr2 = mmap(NULL, npgs * page, PROT_READ, MAP_ANON, -1, (off_t)0); 212 addr3 = mmap(NULL, npgs * page, PROT_NONE, MAP_ANON, -1, (off_t)0); 213 214 if (addr2 == MAP_FAILED || addr3 == MAP_FAILED) 215 return; 216 217 ATF_REQUIRE(check_residency(addr2, npgs) == npgs); 218 ATF_REQUIRE(check_residency(addr3, npgs) == 0); 219 ATF_REQUIRE(mprotect(addr3, npgs * page, PROT_READ) == 0); 220 ATF_REQUIRE(check_residency(addr, npgs) == npgs); 221 ATF_REQUIRE(check_residency(addr2, npgs) == npgs); 222 223 (void)munlockall(); 224 225 ATF_REQUIRE(madvise(addr2, npgs * page, MADV_FREE) == 0); 226 ATF_REQUIRE(check_residency(addr2, npgs) == 0); 227 228 (void)memset(addr, 0, npgs * page); 229 230 ATF_REQUIRE(madvise(addr, npgs * page, MADV_FREE) == 0); 231 ATF_REQUIRE(check_residency(addr, npgs) == 0); 232 233 (void)munmap(addr, npgs * page); 234 (void)munmap(addr2, npgs * page); 235 (void)munmap(addr3, npgs * page); 236 (void)unlink(path); 237} 238 239ATF_TC_CLEANUP(mincore_resid, tc) 240{ 241 (void)unlink(path); 242} 243 244ATF_TC(mincore_shmseg); 245ATF_TC_HEAD(mincore_shmseg, tc) 246{ 247 atf_tc_set_md_var(tc, "descr", "residency of shared memory"); 248} 249 250ATF_TC_BODY(mincore_shmseg, tc) 251{ 252 size_t npgs = 128; 253 void *addr = NULL; 254 int shmid; 255 256 shmid = shmget(IPC_PRIVATE, npgs * page, 257 IPC_CREAT | S_IRUSR | S_IWUSR); 258 259 ATF_REQUIRE(shmid != -1); 260 261 addr = shmat(shmid, NULL, 0); 262 263 ATF_REQUIRE(addr != NULL); 264 ATF_REQUIRE(check_residency(addr, npgs) == 0); 265 266 (void)memset(addr, 0xff, npgs * page); 267 268 ATF_REQUIRE(check_residency(addr, npgs) == npgs); 269 ATF_REQUIRE(madvise(addr, npgs * page, MADV_FREE) == 0); 270 271 /* 272 * NOTE! Even though we have MADV_FREE'd the range, 273 * there is another reference (the kernel's) to the 274 * object which owns the pages. In this case, the 275 * kernel does not simply free the pages, as haphazardly 276 * freeing pages when there are still references to 277 * an object can cause data corruption (say, the other 278 * referencer doesn't expect the pages to be freed, 279 * and is surprised by the subsequent ZFOD). 280 * 281 * Because of this, we simply report the number of 282 * pages still resident, for information only. 283 */ 284 npgs = check_residency(addr, npgs); 285 286 (void)fprintf(stderr, "%zu pages still resident\n", npgs); 287 288 ATF_REQUIRE(shmdt(addr) == 0); 289 ATF_REQUIRE(shmctl(shmid, IPC_RMID, NULL) == 0); 290} 291 292ATF_TP_ADD_TCS(tp) 293{ 294 295 page = sysconf(_SC_PAGESIZE); 296 ATF_REQUIRE(page >= 0); 297 298 ATF_TP_ADD_TC(tp, mincore_err); 299 ATF_TP_ADD_TC(tp, mincore_resid); 300 ATF_TP_ADD_TC(tp, mincore_shmseg); 301 302 return atf_no_error(); 303} 304