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