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