1 /* $NetBSD: t_mlock.c,v 1.6 2016/08/09 12:02:44 kre Exp $ */
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_mlock.c,v 1.6 2016/08/09 12:02:44 kre Exp $");
35 #include <sys/param.h> /* NetBSD requires sys/param.h for sysctl(3), unlike FreeBSD */
38 #include <sys/resource.h>
39 #include <sys/sysctl.h>
52 #include <machine/vmparam.h>
54 void set_vm_max_wired(int);
55 void restore_vm_max_wired(void);
61 ATF_TC_HEAD(mlock_clip, tc)
63 atf_tc_set_md_var(tc, "descr", "Test with mlock(2) that UVM only "
64 "clips if the clip address is within the entry (PR kern/44788)");
67 ATF_TC_BODY(mlock_clip, tc)
72 ATF_REQUIRE(buf != NULL);
75 atf_tc_skip("page size too small");
77 for (size_t i = page; i >= 1; i = i - 1024) {
78 (void)mlock(buf, page - i);
79 (void)munlock(buf, page - i);
86 ATF_TC_WITH_CLEANUP(mlock_err);
90 ATF_TC_HEAD(mlock_err, tc)
92 atf_tc_set_md_var(tc, "descr",
93 "Test error conditions in mlock(2) and munlock(2)");
95 atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");
96 atf_tc_set_md_var(tc, "require.user", "root");
100 ATF_TC_BODY(mlock_err, tc)
103 unsigned long vmin = 0;
104 size_t len = sizeof(vmin);
106 #if !defined(__aarch64__) && !defined(__riscv)
109 int null_errno = ENOMEM; /* error expected for NULL */
113 #ifdef VM_MIN_ADDRESS
114 if ((uintptr_t)VM_MIN_ADDRESS > 0)
115 null_errno = EINVAL; /* NULL is not inside user VM */
117 /* Set max_wired really really high to avoid EAGAIN */
118 set_vm_max_wired(INT_MAX);
120 if (sysctlbyname("vm.minaddress", &vmin, &len, NULL, 0) != 0)
121 atf_tc_fail("failed to read vm.minaddress");
123 * Any bad address must return ENOMEM (for lock & unlock)
126 ATF_REQUIRE_ERRNO(ENOMEM, mlock(NULL, page) == -1);
129 null_errno = EINVAL; /* NULL is not inside user VM */
133 ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)0, page) == -1);
136 ATF_REQUIRE_ERRNO(ENOMEM, munlock(NULL, page) == -1);
139 ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)0, page) == -1);
142 /* Wrap around should return EINVAL */
144 ATF_REQUIRE_ERRNO(EINVAL, mlock((char *)-1, page) == -1);
146 ATF_REQUIRE_ERRNO(EINVAL, munlock((char *)-1, page) == -1);
149 ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)-1, page) == -1);
151 ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)-1, page) == -1);
154 buf = malloc(page); /* Get a valid address */
155 ATF_REQUIRE(buf != NULL);
158 /* Wrap around should return EINVAL */
159 ATF_REQUIRE_ERRNO(EINVAL, mlock(buf, -page) == -1);
161 ATF_REQUIRE_ERRNO(EINVAL, munlock(buf, -page) == -1);
164 ATF_REQUIRE_ERRNO(ENOMEM, mlock(buf, -page) == -1);
166 ATF_REQUIRE_ERRNO(ENOMEM, munlock(buf, -page) == -1);
170 /* There is no sbrk on AArch64 and RISC-V */
171 #if !defined(__aarch64__) && !defined(__riscv)
173 * Try to create a pointer to an unmapped page - first after current
174 * brk will likely do.
176 invalid_ptr = (void*)(((uintptr_t)sbrk(0)+page) & ~(page-1));
177 printf("testing with (hopefully) invalid pointer %p\n", invalid_ptr);
180 ATF_REQUIRE_ERRNO(ENOMEM, mlock(invalid_ptr, page) == -1);
183 ATF_REQUIRE_ERRNO(ENOMEM, munlock(invalid_ptr, page) == -1);
188 ATF_TC_CLEANUP(mlock_err, tc)
191 restore_vm_max_wired();
195 ATF_TC(mlock_limits);
196 ATF_TC_HEAD(mlock_limits, tc)
198 atf_tc_set_md_var(tc, "descr", "Test system limits with mlock(2)");
201 ATF_TC_BODY(mlock_limits, tc)
209 ATF_REQUIRE(buf != NULL);
212 ATF_REQUIRE(pid >= 0);
216 for (ssize_t i = page; i >= 2; i -= 100) {
218 res.rlim_cur = i - 1;
219 res.rlim_max = i - 1;
221 (void)fprintf(stderr, "trying to lock %zd bytes "
222 "with %zu byte limit\n", i, (size_t)res.rlim_cur);
224 if (setrlimit(RLIMIT_MEMLOCK, &res) != 0)
231 * NetBSD doesn't conform to POSIX with ENOMEM requirement;
234 * See: NetBSD PR # kern/48962 for more details.
236 if (mlock(buf, i) != -1 || errno != ENOMEM) {
238 if (mlock(buf, i) != -1 || errno != EAGAIN) {
240 (void)munlock(buf, i);
250 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
251 atf_tc_fail("mlock(2) locked beyond system limits");
257 ATF_TC_WITH_CLEANUP(mlock_mmap);
261 ATF_TC_HEAD(mlock_mmap, tc)
263 atf_tc_set_md_var(tc, "descr", "Test mlock(2)-mmap(2) interaction");
265 atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");
266 atf_tc_set_md_var(tc, "require.user", "root");
270 ATF_TC_BODY(mlock_mmap, tc)
273 static const int flags = MAP_ANON | MAP_PRIVATE | MAP_WIRED;
275 static const int flags = MAP_ANON | MAP_PRIVATE;
280 /* Set max_wired really really high to avoid EAGAIN */
281 set_vm_max_wired(INT_MAX);
285 * Make a wired RW mapping and check that mlock(2)
286 * does not fail for the (already locked) mapping.
288 buf = mmap(NULL, page, PROT_READ | PROT_WRITE, flags, -1, 0);
290 ATF_REQUIRE(buf != MAP_FAILED);
293 * The duplicate mlock call is added to ensure that the call works
294 * as described above without MAP_WIRED support.
296 ATF_REQUIRE(mlock(buf, page) == 0);
298 ATF_REQUIRE(mlock(buf, page) == 0);
299 ATF_REQUIRE(munlock(buf, page) == 0);
300 ATF_REQUIRE(munmap(buf, page) == 0);
301 ATF_REQUIRE(munlock(buf, page) != 0);
304 * But it should be impossible to mlock(2) a PROT_NONE mapping.
306 buf = mmap(NULL, page, PROT_NONE, flags, -1, 0);
308 ATF_REQUIRE(buf != MAP_FAILED);
310 ATF_REQUIRE_ERRNO(ENOMEM, mlock(buf, page) != 0);
312 ATF_REQUIRE(mlock(buf, page) != 0);
314 ATF_REQUIRE(munmap(buf, page) == 0);
318 ATF_TC_CLEANUP(mlock_mmap, tc)
321 restore_vm_max_wired();
326 ATF_TC_WITH_CLEANUP(mlock_nested);
328 ATF_TC(mlock_nested);
330 ATF_TC_HEAD(mlock_nested, tc)
332 atf_tc_set_md_var(tc, "descr",
333 "Test that consecutive mlock(2) calls succeed");
335 atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");
336 atf_tc_set_md_var(tc, "require.user", "root");
340 ATF_TC_BODY(mlock_nested, tc)
342 const size_t maxiter = 100;
346 /* Set max_wired really really high to avoid EAGAIN */
347 set_vm_max_wired(INT_MAX);
351 ATF_REQUIRE(buf != NULL);
353 for (size_t i = 0; i < maxiter; i++)
354 ATF_REQUIRE(mlock(buf, page) == 0);
356 ATF_REQUIRE(munlock(buf, page) == 0);
361 ATF_TC_CLEANUP(mlock_nested, tc)
364 restore_vm_max_wired();
369 ATF_TC_WITH_CLEANUP(mlock_unaligned);
371 ATF_TC(mlock_unaligned);
373 ATF_TC_HEAD(mlock_unaligned, tc)
375 atf_tc_set_md_var(tc, "descr",
376 "Test that mlock(2) can lock page-unaligned memory");
378 atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");
379 atf_tc_set_md_var(tc, "require.user", "root");
383 ATF_TC_BODY(mlock_unaligned, tc)
388 /* Set max_wired really really high to avoid EAGAIN */
389 set_vm_max_wired(INT_MAX);
393 ATF_REQUIRE(buf != NULL);
395 if ((uintptr_t)buf & ((uintptr_t)page - 1))
398 addr = (void *)(((uintptr_t)buf) + page/3);
400 ATF_REQUIRE_EQ(mlock(addr, page/5), 0);
401 ATF_REQUIRE_EQ(munlock(addr, page/5), 0);
407 ATF_TC_CLEANUP(mlock_unaligned, tc)
410 restore_vm_max_wired();
414 ATF_TC(munlock_unlocked);
415 ATF_TC_HEAD(munlock_unlocked, tc)
417 atf_tc_set_md_var(tc, "descr",
419 "munlock(2) accepts unlocked memory");
421 "munlock(2) of unlocked memory is an error");
423 atf_tc_set_md_var(tc, "require.user", "root");
426 ATF_TC_BODY(munlock_unlocked, tc)
431 ATF_REQUIRE(buf != NULL);
434 ATF_REQUIRE_EQ(munlock(buf, page), 0);
437 ATF_REQUIRE_ERRNO(ENOMEM, munlock(buf, page) == -1);
445 page = sysconf(_SC_PAGESIZE);
446 ATF_REQUIRE(page >= 0);
448 ATF_TP_ADD_TC(tp, mlock_clip);
449 ATF_TP_ADD_TC(tp, mlock_err);
450 ATF_TP_ADD_TC(tp, mlock_limits);
451 ATF_TP_ADD_TC(tp, mlock_mmap);
452 ATF_TP_ADD_TC(tp, mlock_nested);
453 ATF_TP_ADD_TC(tp, mlock_unaligned);
454 ATF_TP_ADD_TC(tp, munlock_unlocked);
456 return atf_no_error();