t_mincore.c revision 1.12
1/* $NetBSD: t_mincore.c,v 1.12 2017/09/01 16:27:02 kre 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.12 2017/09/01 16:27:02 kre Exp $"); 63 64#include <sys/mman.h> 65#include <sys/stat.h> 66#include <sys/shm.h> 67 68#include <atf-c.h> 69#include <errno.h> 70#include <fcntl.h> 71#include <kvm.h> 72#include <stdio.h> 73#include <stdlib.h> 74#include <string.h> 75#include <unistd.h> 76#include <sys/resource.h> 77 78static long page = 0; 79static const char path[] = "mincore"; 80static size_t check_residency(void *, size_t); 81 82#define ATF_REQUIRE_STRERROR(a) ATF_REQUIRE_MSG(a, " (%s)", strerror(errno)) 83 84static size_t 85check_residency(void *addr, size_t npgs) 86{ 87 size_t i, resident; 88 char *vec; 89 90 vec = malloc(npgs); 91 92 ATF_REQUIRE(vec != NULL); 93 ATF_REQUIRE(mincore(addr, npgs * page, vec) == 0); 94 95 for (i = resident = 0; i < npgs; i++) { 96 97 if (vec[i] != 0) 98 resident++; 99 100#if 0 101 (void)fprintf(stderr, "page 0x%p is %sresident\n", 102 (char *)addr + (i * page), vec[i] ? "" : "not "); 103#endif 104 } 105 106 free(vec); 107 108 return resident; 109} 110 111ATF_TC(mincore_err); 112ATF_TC_HEAD(mincore_err, tc) 113{ 114 atf_tc_set_md_var(tc, "descr", "Test errors from mincore(2)"); 115} 116 117ATF_TC_BODY(mincore_err, tc) 118{ 119 char *map, *vec; 120 121 map = mmap(NULL, page, PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); 122 vec = malloc(page); 123 124 ATF_REQUIRE(vec != NULL); 125 ATF_REQUIRE(map != MAP_FAILED); 126 127 errno = 0; 128 ATF_REQUIRE_ERRNO(EINVAL, mincore(map, 0, vec) == -1); 129 130 errno = 0; 131 ATF_REQUIRE_ERRNO(ENOMEM, mincore(0, page, vec) == -1); 132 133 errno = 0; 134 ATF_REQUIRE_ERRNO(EFAULT, mincore(map, page, (void *)-1) == -1); 135 136 free(vec); 137 ATF_REQUIRE(munmap(map, page) == 0); 138} 139 140ATF_TC_WITH_CLEANUP(mincore_resid); 141ATF_TC_HEAD(mincore_resid, tc) 142{ 143 atf_tc_set_md_var(tc, "descr", "Test page residency with mincore(2)"); 144 atf_tc_set_md_var(tc, "require.user", "root"); 145} 146 147ATF_TC_BODY(mincore_resid, tc) 148{ 149 void *addr, *addr2, *addr3, *buf; 150 size_t npgs = 0, resident; 151 struct stat st; 152 int fd, rv; 153 struct rlimit rlim; 154 155 ATF_REQUIRE(getrlimit(RLIMIT_MEMLOCK, &rlim) == 0); 156 /* 157 * Bump the mlock limit to unlimited so the rest of the testcase 158 * passes instead of failing on the mlock call. 159 */ 160 rlim.rlim_max = RLIM_INFINITY; 161 rlim.rlim_cur = rlim.rlim_max; 162 ATF_REQUIRE(setrlimit(RLIMIT_MEMLOCK, &rlim) == 0); 163 164 (void)memset(&st, 0, sizeof(struct stat)); 165 166 fd = open(path, O_RDWR | O_CREAT, 0700); 167 buf = malloc(page * 5); 168 169 ATF_REQUIRE(fd >= 0); 170 ATF_REQUIRE(buf != NULL); 171 172 rv = write(fd, buf, page * 5); 173 ATF_REQUIRE(rv >= 0); 174 175 ATF_REQUIRE(fd >= 0); 176 ATF_REQUIRE(fstat(fd, &st) == 0); 177 178 addr = mmap(NULL, (size_t)st.st_size, PROT_READ, 179 MAP_FILE | MAP_SHARED, fd, (off_t) 0); 180 181 ATF_REQUIRE(addr != MAP_FAILED); 182 183 (void)close(fd); 184 185 npgs = st.st_size / page; 186 187 if (st.st_size % page != 0) 188 npgs++; 189 190 (void)check_residency(addr, npgs); 191 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); 196 197 npgs = 128; 198 199 addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE, 200 MAP_ANON | MAP_PRIVATE | MAP_WIRED, -1, (off_t)0); 201 202 if (addr == MAP_FAILED) 203 atf_tc_skip("could not mmap wired anonymous test area, system " 204 "might be low on memory"); 205 206 ATF_REQUIRE(check_residency(addr, npgs) == npgs); 207 ATF_REQUIRE(munmap(addr, npgs * page) == 0); 208 209 npgs = 128; 210 211 addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE, 212 MAP_ANON | MAP_PRIVATE, -1, (off_t)0); 213 214 ATF_REQUIRE(addr != MAP_FAILED); 215 216 /* 217 * Check that the in-core pages match the locked pages. 218 */ 219 ATF_REQUIRE(check_residency(addr, npgs) == 0); 220 221 errno = 0; 222 if (mlockall(MCL_CURRENT|MCL_FUTURE) != 0 && errno != ENOMEM) 223 atf_tc_fail("mlockall(2) failed"); 224 if (errno == ENOMEM) 225 atf_tc_skip("mlockall() exceeded process resource limits"); 226 227 resident = check_residency(addr, npgs); 228 if (resident < npgs) 229 atf_tc_fail("mlockall(MCL_FUTURE) succeeded, still only " 230 "%zu pages of the newly mapped %zu pages are resident", 231 resident, npgs); 232 233 addr2 = mmap(NULL, npgs * page, PROT_READ, MAP_ANON, -1, (off_t)0); 234 addr3 = mmap(NULL, npgs * page, PROT_MPROTECT(PROT_READ) | PROT_NONE, 235 MAP_ANON, -1, (off_t)0); 236 237 if (addr2 == MAP_FAILED || addr3 == MAP_FAILED) 238 atf_tc_skip("could not mmap more anonymous test pages with " 239 "mlockall(MCL_FUTURE) in effect, system " 240 "might be low on memory"); 241 242 ATF_REQUIRE(check_residency(addr2, npgs) == npgs); 243 ATF_REQUIRE(check_residency(addr3, npgs) == 0); 244 ATF_REQUIRE_STRERROR(mprotect(addr3, npgs * page, PROT_READ) == 0); 245 ATF_REQUIRE(check_residency(addr, npgs) == npgs); 246 ATF_REQUIRE(check_residency(addr2, npgs) == npgs); 247 248 (void)munlockall(); 249 250 ATF_REQUIRE_STRERROR(madvise(addr2, npgs * page, MADV_FREE) == 0); 251 ATF_REQUIRE(check_residency(addr2, npgs) == 0); 252 253 (void)memset(addr, 0, npgs * page); 254 255 ATF_REQUIRE_STRERROR(madvise(addr, npgs * page, MADV_FREE) == 0); 256 ATF_REQUIRE(check_residency(addr, npgs) == 0); 257 258 (void)munmap(addr, npgs * page); 259 (void)munmap(addr2, npgs * page); 260 (void)munmap(addr3, npgs * page); 261 (void)unlink(path); 262 free(buf); 263} 264 265ATF_TC_CLEANUP(mincore_resid, tc) 266{ 267 (void)unlink(path); 268} 269 270ATF_TC(mincore_shmseg); 271ATF_TC_HEAD(mincore_shmseg, tc) 272{ 273 atf_tc_set_md_var(tc, "descr", "residency of shared memory"); 274} 275 276ATF_TC_BODY(mincore_shmseg, tc) 277{ 278 size_t npgs = 128; 279 void *addr = NULL; 280 int shmid; 281 282 shmid = shmget(IPC_PRIVATE, npgs * page, 283 IPC_CREAT | S_IRUSR | S_IWUSR); 284 285 ATF_REQUIRE_STRERROR(shmid != -1); 286 287 addr = shmat(shmid, NULL, 0); 288 289 ATF_REQUIRE_STRERROR(addr != NULL); 290 ATF_REQUIRE(check_residency(addr, npgs) == 0); 291 292 (void)memset(addr, 0xff, npgs * page); 293 294 ATF_REQUIRE(check_residency(addr, npgs) == npgs); 295 ATF_REQUIRE_STRERROR(madvise(addr, npgs * page, MADV_FREE) == 0); 296 297 /* 298 * NOTE! Even though we have MADV_FREE'd the range, 299 * there is another reference (the kernel's) to the 300 * object which owns the pages. In this case, the 301 * kernel does not simply free the pages, as haphazardly 302 * freeing pages when there are still references to 303 * an object can cause data corruption (say, the other 304 * referencer doesn't expect the pages to be freed, 305 * and is surprised by the subsequent ZFOD). 306 * 307 * Because of this, we simply report the number of 308 * pages still resident, for information only. 309 */ 310 npgs = check_residency(addr, npgs); 311 312 (void)fprintf(stderr, "%zu pages still resident\n", npgs); 313 314 ATF_REQUIRE_STRERROR(shmdt(addr) == 0); 315 ATF_REQUIRE_STRERROR(shmctl(shmid, IPC_RMID, NULL) == 0); 316} 317 318ATF_TP_ADD_TCS(tp) 319{ 320 321 page = sysconf(_SC_PAGESIZE); 322 ATF_REQUIRE(page >= 0); 323 324 ATF_TP_ADD_TC(tp, mincore_err); 325 ATF_TP_ADD_TC(tp, mincore_resid); 326 ATF_TP_ADD_TC(tp, mincore_shmseg); 327 328 return atf_no_error(); 329} 330