Home | History | Annotate | Line # | Download | only in sys
t_mlock.c revision 1.6
      1 /* $NetBSD: t_mlock.c,v 1.6 2016/08/09 12:02:44 kre Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2012 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 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: t_mlock.c,v 1.6 2016/08/09 12:02:44 kre Exp $");
     33 
     34 #include <sys/mman.h>
     35 #include <sys/resource.h>
     36 #include <sys/sysctl.h>
     37 #include <sys/wait.h>
     38 
     39 #include <errno.h>
     40 #include <atf-c.h>
     41 #include <stdint.h>
     42 #include <stdio.h>
     43 #include <stdlib.h>
     44 #include <unistd.h>
     45 
     46 static long page = 0;
     47 
     48 ATF_TC(mlock_clip);
     49 ATF_TC_HEAD(mlock_clip, tc)
     50 {
     51 	atf_tc_set_md_var(tc, "descr", "Test with mlock(2) that UVM only "
     52 	    "clips if the clip address is within the entry (PR kern/44788)");
     53 }
     54 
     55 ATF_TC_BODY(mlock_clip, tc)
     56 {
     57 	void *buf;
     58 
     59 	buf = malloc(page);
     60 	ATF_REQUIRE(buf != NULL);
     61 
     62 	if (page < 1024)
     63 		atf_tc_skip("page size too small");
     64 
     65 	for (size_t i = page; i >= 1; i = i - 1024) {
     66 		(void)mlock(buf, page - i);
     67 		(void)munlock(buf, page - i);
     68 	}
     69 
     70 	free(buf);
     71 }
     72 
     73 ATF_TC(mlock_err);
     74 ATF_TC_HEAD(mlock_err, tc)
     75 {
     76 	atf_tc_set_md_var(tc, "descr",
     77 	    "Test error conditions in mlock(2) and munlock(2)");
     78 }
     79 
     80 ATF_TC_BODY(mlock_err, tc)
     81 {
     82 	void *invalid_ptr;
     83 	void *buf;
     84 
     85 	/*
     86 	 * Any bad address must return ENOMEM (for lock & unlock)
     87 	 */
     88 	errno = 0;
     89 	ATF_REQUIRE_ERRNO(ENOMEM, mlock(NULL, page) == -1);
     90 
     91 	errno = 0;
     92 	ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)0, page) == -1);
     93 
     94 	errno = 0;
     95 	ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)-1, page) == -1);
     96 
     97 	errno = 0;
     98 	ATF_REQUIRE_ERRNO(ENOMEM, munlock(NULL, page) == -1);
     99 
    100 	errno = 0;
    101 	ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)0, page) == -1);
    102 
    103 	errno = 0;
    104 	ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)-1, page) == -1);
    105 
    106 	buf = malloc(page);
    107 	ATF_REQUIRE(buf != NULL);
    108 
    109 	/*
    110 	 * unlocking memory that is not locked is an error...
    111 	 */
    112 
    113 	errno = 0;
    114 	ATF_REQUIRE_ERRNO(ENOMEM, munlock(buf, page) == -1);
    115 
    116 	/*
    117 	 * These are permitted to fail (EINVAL) but do not on NetBSD
    118 	 */
    119 	ATF_REQUIRE(mlock((void *)(((uintptr_t)buf) + page/3), page/5) == 0);
    120 	ATF_REQUIRE(munlock((void *)(((uintptr_t)buf) + page/3), page/5) == 0);
    121 
    122 	(void)free(buf);
    123 
    124 	/*
    125 	 * Try to create a pointer to an unmapped page - first after current
    126 	 * brk will likely do.
    127 	 */
    128 	invalid_ptr = (void*)(((uintptr_t)sbrk(0)+page) & ~(page-1));
    129 	printf("testing with (hopefully) invalid pointer %p\n", invalid_ptr);
    130 
    131 	errno = 0;
    132 	ATF_REQUIRE_ERRNO(ENOMEM, mlock(invalid_ptr, page) == -1);
    133 
    134 	errno = 0;
    135 	ATF_REQUIRE_ERRNO(ENOMEM, munlock(invalid_ptr, page) == -1);
    136 }
    137 
    138 ATF_TC(mlock_limits);
    139 ATF_TC_HEAD(mlock_limits, tc)
    140 {
    141 	atf_tc_set_md_var(tc, "descr", "Test system limits with mlock(2)");
    142 }
    143 
    144 ATF_TC_BODY(mlock_limits, tc)
    145 {
    146 	struct rlimit res;
    147 	void *buf;
    148 	pid_t pid;
    149 	int sta;
    150 
    151 	buf = malloc(page);
    152 	ATF_REQUIRE(buf != NULL);
    153 
    154 	pid = fork();
    155 	ATF_REQUIRE(pid >= 0);
    156 
    157 	if (pid == 0) {
    158 
    159 		for (ssize_t i = page; i >= 2; i -= 100) {
    160 
    161 			res.rlim_cur = i - 1;
    162 			res.rlim_max = i - 1;
    163 
    164 			(void)fprintf(stderr, "trying to lock %zd bytes "
    165 			    "with %zu byte limit\n", i, (size_t)res.rlim_cur);
    166 
    167 			if (setrlimit(RLIMIT_MEMLOCK, &res) != 0)
    168 				_exit(EXIT_FAILURE);
    169 
    170 			errno = 0;
    171 
    172 			if (mlock(buf, i) != -1 || errno != EAGAIN) {
    173 				(void)munlock(buf, i);
    174 				_exit(EXIT_FAILURE);
    175 			}
    176 		}
    177 
    178 		_exit(EXIT_SUCCESS);
    179 	}
    180 
    181 	(void)wait(&sta);
    182 
    183 	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
    184 		atf_tc_fail("mlock(2) locked beyond system limits");
    185 
    186 	free(buf);
    187 }
    188 
    189 ATF_TC(mlock_mmap);
    190 ATF_TC_HEAD(mlock_mmap, tc)
    191 {
    192 	atf_tc_set_md_var(tc, "descr", "Test mlock(2)-mmap(2) interaction");
    193 }
    194 
    195 ATF_TC_BODY(mlock_mmap, tc)
    196 {
    197 	static const int flags = MAP_ANON | MAP_PRIVATE | MAP_WIRED;
    198 	void *buf;
    199 
    200 	/*
    201 	 * Make a wired RW mapping and check that mlock(2)
    202 	 * does not fail for the (already locked) mapping.
    203 	 */
    204 	buf = mmap(NULL, page, PROT_READ | PROT_WRITE, flags, -1, 0);
    205 
    206 	ATF_REQUIRE(buf != MAP_FAILED);
    207 	ATF_REQUIRE(mlock(buf, page) == 0);
    208 	ATF_REQUIRE(munlock(buf, page) == 0);
    209 	ATF_REQUIRE(munmap(buf, page) == 0);
    210 	ATF_REQUIRE(munlock(buf, page) != 0);
    211 
    212 	/*
    213 	 * But it should be impossible to mlock(2) a PROT_NONE mapping.
    214 	 */
    215 	buf = mmap(NULL, page, PROT_NONE, flags, -1, 0);
    216 
    217 	ATF_REQUIRE(buf != MAP_FAILED);
    218 	ATF_REQUIRE(mlock(buf, page) != 0);
    219 	ATF_REQUIRE(munmap(buf, page) == 0);
    220 }
    221 
    222 ATF_TC(mlock_nested);
    223 ATF_TC_HEAD(mlock_nested, tc)
    224 {
    225 	atf_tc_set_md_var(tc, "descr",
    226 	    "Test that consecutive mlock(2) calls succeed");
    227 }
    228 
    229 ATF_TC_BODY(mlock_nested, tc)
    230 {
    231 	const size_t maxiter = 100;
    232 	void *buf;
    233 
    234 	buf = malloc(page);
    235 	ATF_REQUIRE(buf != NULL);
    236 
    237 	for (size_t i = 0; i < maxiter; i++)
    238 		ATF_REQUIRE(mlock(buf, page) == 0);
    239 
    240 	ATF_REQUIRE(munlock(buf, page) == 0);
    241 	free(buf);
    242 }
    243 
    244 ATF_TP_ADD_TCS(tp)
    245 {
    246 
    247 	page = sysconf(_SC_PAGESIZE);
    248 	ATF_REQUIRE(page >= 0);
    249 
    250 	ATF_TP_ADD_TC(tp, mlock_clip);
    251 	ATF_TP_ADD_TC(tp, mlock_err);
    252 	ATF_TP_ADD_TC(tp, mlock_limits);
    253 	ATF_TP_ADD_TC(tp, mlock_mmap);
    254 	ATF_TP_ADD_TC(tp, mlock_nested);
    255 
    256 	return atf_no_error();
    257 }
    258