1 /* $NetBSD: t_mlock.c,v 1.5 2014/02/26 20:49:26 martin 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.5 2014/02/26 20:49:26 martin Exp $"); 33 34 #ifdef __FreeBSD__ 35 #include <sys/types.h> 36 #endif 37 #include <sys/mman.h> 38 #include <sys/resource.h> 39 #include <sys/sysctl.h> 40 #include <sys/wait.h> 41 42 #include <errno.h> 43 #include <atf-c.h> 44 #include <stdint.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <unistd.h> 48 49 #ifdef __FreeBSD__ 50 #define _KMEMUSER 51 #include <machine/vmparam.h> 52 #endif 53 54 static long page = 0; 55 56 ATF_TC(mlock_clip); 57 ATF_TC_HEAD(mlock_clip, tc) 58 { 59 atf_tc_set_md_var(tc, "descr", "Test with mlock(2) that UVM only " 60 "clips if the clip address is within the entry (PR kern/44788)"); 61 } 62 63 ATF_TC_BODY(mlock_clip, tc) 64 { 65 void *buf; 66 67 buf = malloc(page); 68 ATF_REQUIRE(buf != NULL); 69 70 if (page < 1024) 71 atf_tc_skip("page size too small"); 72 73 for (size_t i = page; i >= 1; i = i - 1024) { 74 (void)mlock(buf, page - i); 75 (void)munlock(buf, page - i); 76 } 77 78 free(buf); 79 } 80 81 ATF_TC(mlock_err); 82 ATF_TC_HEAD(mlock_err, tc) 83 { 84 atf_tc_set_md_var(tc, "descr", 85 "Test error conditions in mlock(2) and munlock(2)"); 86 } 87 88 ATF_TC_BODY(mlock_err, tc) 89 { 90 #ifdef __NetBSD__ 91 unsigned long vmin = 0; 92 size_t len = sizeof(vmin); 93 #endif 94 void *invalid_ptr; 95 int null_errno = ENOMEM; /* error expected for NULL */ 96 97 #ifdef __FreeBSD__ 98 #ifdef VM_MIN_ADDRESS 99 if ((uintptr_t)VM_MIN_ADDRESS > 0) 100 null_errno = EINVAL; /* NULL is not inside user VM */ 101 #endif 102 #else 103 if (sysctlbyname("vm.minaddress", &vmin, &len, NULL, 0) != 0) 104 atf_tc_fail("failed to read vm.minaddress"); 105 106 if (vmin > 0) 107 null_errno = EINVAL; /* NULL is not inside user VM */ 108 #endif 109 110 errno = 0; 111 ATF_REQUIRE_ERRNO(null_errno, mlock(NULL, page) == -1); 112 113 errno = 0; 114 ATF_REQUIRE_ERRNO(null_errno, mlock((char *)0, page) == -1); 115 116 errno = 0; 117 ATF_REQUIRE_ERRNO(EINVAL, mlock((char *)-1, page) == -1); 118 119 errno = 0; 120 ATF_REQUIRE_ERRNO(null_errno, munlock(NULL, page) == -1); 121 122 errno = 0; 123 ATF_REQUIRE_ERRNO(null_errno, munlock((char *)0, page) == -1); 124 125 errno = 0; 126 ATF_REQUIRE_ERRNO(EINVAL, munlock((char *)-1, page) == -1); 127 128 /* 129 * Try to create a pointer to an unmapped page - first after current 130 * brk will likely do. 131 */ 132 invalid_ptr = (void*)(((uintptr_t)sbrk(0)+page) & ~(page-1)); 133 printf("testing with (hopefully) invalid pointer %p\n", invalid_ptr); 134 135 errno = 0; 136 ATF_REQUIRE_ERRNO(ENOMEM, mlock(invalid_ptr, page) == -1); 137 138 errno = 0; 139 ATF_REQUIRE_ERRNO(ENOMEM, munlock(invalid_ptr, page) == -1); 140 } 141 142 ATF_TC(mlock_limits); 143 ATF_TC_HEAD(mlock_limits, tc) 144 { 145 atf_tc_set_md_var(tc, "descr", "Test system limits with mlock(2)"); 146 } 147 148 ATF_TC_BODY(mlock_limits, tc) 149 { 150 struct rlimit res; 151 void *buf; 152 pid_t pid; 153 int sta; 154 155 buf = malloc(page); 156 ATF_REQUIRE(buf != NULL); 157 158 pid = fork(); 159 ATF_REQUIRE(pid >= 0); 160 161 if (pid == 0) { 162 163 for (ssize_t i = page; i >= 2; i -= 100) { 164 165 res.rlim_cur = i - 1; 166 res.rlim_max = i - 1; 167 168 (void)fprintf(stderr, "trying to lock %zd bytes " 169 "with %zu byte limit\n", i, (size_t)res.rlim_cur); 170 171 if (setrlimit(RLIMIT_MEMLOCK, &res) != 0) 172 _exit(EXIT_FAILURE); 173 174 errno = 0; 175 176 #ifdef __FreeBSD__ 177 /* 178 * NetBSD doesn't conform to POSIX with ENOMEM requirement; 179 * FreeBSD does. 180 * 181 * See: NetBSD PR # kern/48962 for more details. 182 */ 183 if (mlock(buf, i) != -1 || errno != ENOMEM) { 184 #else 185 if (mlock(buf, i) != -1 || errno != EAGAIN) { 186 #endif 187 (void)munlock(buf, i); 188 _exit(EXIT_FAILURE); 189 } 190 } 191 192 _exit(EXIT_SUCCESS); 193 } 194 195 (void)wait(&sta); 196 197 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) 198 atf_tc_fail("mlock(2) locked beyond system limits"); 199 200 free(buf); 201 } 202 203 ATF_TC(mlock_mmap); 204 ATF_TC_HEAD(mlock_mmap, tc) 205 { 206 atf_tc_set_md_var(tc, "descr", "Test mlock(2)-mmap(2) interaction"); 207 } 208 209 ATF_TC_BODY(mlock_mmap, tc) 210 { 211 #ifdef __NetBSD__ 212 static const int flags = MAP_ANON | MAP_PRIVATE | MAP_WIRED; 213 #else 214 static const int flags = MAP_ANON | MAP_PRIVATE; 215 #endif 216 void *buf; 217 218 /* 219 * Make a wired RW mapping and check that mlock(2) 220 * does not fail for the (already locked) mapping. 221 */ 222 buf = mmap(NULL, page, PROT_READ | PROT_WRITE, flags, -1, 0); 223 224 ATF_REQUIRE(buf != MAP_FAILED); 225 #ifdef __FreeBSD__ 226 /* 227 * The duplicate mlock call is added to ensure that the call works 228 * as described above without MAP_WIRED support. 229 */ 230 ATF_REQUIRE(mlock(buf, page) == 0); 231 #endif 232 ATF_REQUIRE(mlock(buf, page) == 0); 233 ATF_REQUIRE(munlock(buf, page) == 0); 234 ATF_REQUIRE(munmap(buf, page) == 0); 235 ATF_REQUIRE(munlock(buf, page) != 0); 236 237 /* 238 * But it should be impossible to mlock(2) a PROT_NONE mapping. 239 */ 240 buf = mmap(NULL, page, PROT_NONE, flags, -1, 0); 241 242 ATF_REQUIRE(buf != MAP_FAILED); 243 #ifdef __FreeBSD__ 244 ATF_REQUIRE_ERRNO(ENOMEM, mlock(buf, page) != 0); 245 #else 246 ATF_REQUIRE(mlock(buf, page) != 0); 247 #endif 248 ATF_REQUIRE(munmap(buf, page) == 0); 249 } 250 251 ATF_TC(mlock_nested); 252 ATF_TC_HEAD(mlock_nested, tc) 253 { 254 atf_tc_set_md_var(tc, "descr", 255 "Test that consecutive mlock(2) calls succeed"); 256 } 257 258 ATF_TC_BODY(mlock_nested, tc) 259 { 260 const size_t maxiter = 100; 261 void *buf; 262 263 buf = malloc(page); 264 ATF_REQUIRE(buf != NULL); 265 266 for (size_t i = 0; i < maxiter; i++) 267 ATF_REQUIRE(mlock(buf, page) == 0); 268 269 ATF_REQUIRE(munlock(buf, page) == 0); 270 free(buf); 271 } 272 273 ATF_TP_ADD_TCS(tp) 274 { 275 276 page = sysconf(_SC_PAGESIZE); 277 ATF_REQUIRE(page >= 0); 278 279 ATF_TP_ADD_TC(tp, mlock_clip); 280 ATF_TP_ADD_TC(tp, mlock_err); 281 ATF_TP_ADD_TC(tp, mlock_limits); 282 ATF_TP_ADD_TC(tp, mlock_mmap); 283 ATF_TP_ADD_TC(tp, mlock_nested); 284 285 return atf_no_error(); 286 } 287