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/types.h>
38 #include <sys/resource.h>
39 #include <sys/sysctl.h>
52 #include <machine/vmparam.h>
58 #define VM_MAX_WIRED "vm.max_wired"
61 vm_max_wired_sysctl(int *old_value, int *new_value)
64 size_t new_len = (new_value == NULL ? 0 : sizeof(int));
66 if (old_value == NULL)
67 printf("Setting the new value to %d\n", *new_value);
69 ATF_REQUIRE_MSG(sysctlbyname(VM_MAX_WIRED, NULL, &old_len,
70 new_value, new_len) == 0,
71 "sysctlbyname(%s) failed: %s", VM_MAX_WIRED, strerror(errno));
74 ATF_REQUIRE_MSG(sysctlbyname(VM_MAX_WIRED, old_value, &old_len,
75 new_value, new_len) == 0,
76 "sysctlbyname(%s) failed: %s", VM_MAX_WIRED, strerror(errno));
78 if (old_value != NULL)
79 printf("Saved the old value (%d)\n", *old_value);
83 set_vm_max_wired(int new_value)
88 fp = fopen(VM_MAX_WIRED, "w");
90 atf_tc_skip("could not open %s for writing: %s",
91 VM_MAX_WIRED, strerror(errno));
95 vm_max_wired_sysctl(&old_value, NULL);
97 ATF_REQUIRE_MSG(fprintf(fp, "%d", old_value) > 0,
98 "saving %s failed", VM_MAX_WIRED);
102 vm_max_wired_sysctl(NULL, &new_value);
106 restore_vm_max_wired(void)
111 fp = fopen(VM_MAX_WIRED, "r");
113 perror("fopen failed\n");
117 if (fscanf(fp, "%d", &saved_max_wired) != 1) {
118 perror("fscanf failed\n");
124 printf("old value in %s: %d\n", VM_MAX_WIRED, saved_max_wired);
126 if (saved_max_wired == 0) /* This will cripple the test host */
129 vm_max_wired_sysctl(NULL, &saved_max_wired);
134 ATF_TC_HEAD(mlock_clip, tc)
136 atf_tc_set_md_var(tc, "descr", "Test with mlock(2) that UVM only "
137 "clips if the clip address is within the entry (PR kern/44788)");
140 ATF_TC_BODY(mlock_clip, tc)
145 ATF_REQUIRE(buf != NULL);
148 atf_tc_skip("page size too small");
150 for (size_t i = page; i >= 1; i = i - 1024) {
151 (void)mlock(buf, page - i);
152 (void)munlock(buf, page - i);
159 ATF_TC_WITH_CLEANUP(mlock_err);
163 ATF_TC_HEAD(mlock_err, tc)
165 atf_tc_set_md_var(tc, "descr",
166 "Test error conditions in mlock(2) and munlock(2)");
168 atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");
169 atf_tc_set_md_var(tc, "require.user", "root");
173 ATF_TC_BODY(mlock_err, tc)
176 unsigned long vmin = 0;
177 size_t len = sizeof(vmin);
180 int null_errno = ENOMEM; /* error expected for NULL */
184 #ifdef VM_MIN_ADDRESS
185 if ((uintptr_t)VM_MIN_ADDRESS > 0)
186 null_errno = EINVAL; /* NULL is not inside user VM */
188 /* Set max_wired really really high to avoid EAGAIN */
189 set_vm_max_wired(INT_MAX);
191 if (sysctlbyname("vm.minaddress", &vmin, &len, NULL, 0) != 0)
192 atf_tc_fail("failed to read vm.minaddress");
194 * Any bad address must return ENOMEM (for lock & unlock)
197 ATF_REQUIRE_ERRNO(ENOMEM, mlock(NULL, page) == -1);
200 null_errno = EINVAL; /* NULL is not inside user VM */
204 ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)0, page) == -1);
207 ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)-1, page) == -1);
210 ATF_REQUIRE_ERRNO(ENOMEM, munlock(NULL, page) == -1);
213 ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)0, page) == -1);
216 ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)-1, page) == -1);
219 ATF_REQUIRE(buf != NULL);
222 * unlocking memory that is not locked is an error...
226 ATF_REQUIRE_ERRNO(ENOMEM, munlock(buf, page) == -1);
229 * These are permitted to fail (EINVAL) but do not on NetBSD
231 ATF_REQUIRE(mlock((void *)(((uintptr_t)buf) + page/3), page/5) == 0);
232 ATF_REQUIRE(munlock((void *)(((uintptr_t)buf) + page/3), page/5) == 0);
237 * Try to create a pointer to an unmapped page - first after current
238 * brk will likely do.
240 invalid_ptr = (void*)(((uintptr_t)sbrk(0)+page) & ~(page-1));
241 printf("testing with (hopefully) invalid pointer %p\n", invalid_ptr);
244 ATF_REQUIRE_ERRNO(ENOMEM, mlock(invalid_ptr, page) == -1);
247 ATF_REQUIRE_ERRNO(ENOMEM, munlock(invalid_ptr, page) == -1);
251 ATF_TC_CLEANUP(mlock_err, tc)
254 restore_vm_max_wired();
258 ATF_TC(mlock_limits);
259 ATF_TC_HEAD(mlock_limits, tc)
261 atf_tc_set_md_var(tc, "descr", "Test system limits with mlock(2)");
264 ATF_TC_BODY(mlock_limits, tc)
272 ATF_REQUIRE(buf != NULL);
275 ATF_REQUIRE(pid >= 0);
279 for (ssize_t i = page; i >= 2; i -= 100) {
281 res.rlim_cur = i - 1;
282 res.rlim_max = i - 1;
284 (void)fprintf(stderr, "trying to lock %zd bytes "
285 "with %zu byte limit\n", i, (size_t)res.rlim_cur);
287 if (setrlimit(RLIMIT_MEMLOCK, &res) != 0)
294 * NetBSD doesn't conform to POSIX with ENOMEM requirement;
297 * See: NetBSD PR # kern/48962 for more details.
299 if (mlock(buf, i) != -1 || errno != ENOMEM) {
301 if (mlock(buf, i) != -1 || errno != EAGAIN) {
303 (void)munlock(buf, i);
313 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
314 atf_tc_fail("mlock(2) locked beyond system limits");
320 ATF_TC_WITH_CLEANUP(mlock_mmap);
324 ATF_TC_HEAD(mlock_mmap, tc)
326 atf_tc_set_md_var(tc, "descr", "Test mlock(2)-mmap(2) interaction");
328 atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");
329 atf_tc_set_md_var(tc, "require.user", "root");
333 ATF_TC_BODY(mlock_mmap, tc)
336 static const int flags = MAP_ANON | MAP_PRIVATE | MAP_WIRED;
338 static const int flags = MAP_ANON | MAP_PRIVATE;
343 /* Set max_wired really really high to avoid EAGAIN */
344 set_vm_max_wired(INT_MAX);
348 * Make a wired RW mapping and check that mlock(2)
349 * does not fail for the (already locked) mapping.
351 buf = mmap(NULL, page, PROT_READ | PROT_WRITE, flags, -1, 0);
353 ATF_REQUIRE(buf != MAP_FAILED);
356 * The duplicate mlock call is added to ensure that the call works
357 * as described above without MAP_WIRED support.
359 ATF_REQUIRE(mlock(buf, page) == 0);
361 ATF_REQUIRE(mlock(buf, page) == 0);
362 ATF_REQUIRE(munlock(buf, page) == 0);
363 ATF_REQUIRE(munmap(buf, page) == 0);
364 ATF_REQUIRE(munlock(buf, page) != 0);
367 * But it should be impossible to mlock(2) a PROT_NONE mapping.
369 buf = mmap(NULL, page, PROT_NONE, flags, -1, 0);
371 ATF_REQUIRE(buf != MAP_FAILED);
373 ATF_REQUIRE_ERRNO(ENOMEM, mlock(buf, page) != 0);
375 ATF_REQUIRE(mlock(buf, page) != 0);
377 ATF_REQUIRE(munmap(buf, page) == 0);
381 ATF_TC_CLEANUP(mlock_mmap, tc)
384 restore_vm_max_wired();
389 ATF_TC_WITH_CLEANUP(mlock_nested);
391 ATF_TC(mlock_nested);
393 ATF_TC_HEAD(mlock_nested, tc)
395 atf_tc_set_md_var(tc, "descr",
396 "Test that consecutive mlock(2) calls succeed");
398 atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");
399 atf_tc_set_md_var(tc, "require.user", "root");
403 ATF_TC_BODY(mlock_nested, tc)
405 const size_t maxiter = 100;
409 /* Set max_wired really really high to avoid EAGAIN */
410 set_vm_max_wired(INT_MAX);
414 ATF_REQUIRE(buf != NULL);
416 for (size_t i = 0; i < maxiter; i++)
417 ATF_REQUIRE(mlock(buf, page) == 0);
419 ATF_REQUIRE(munlock(buf, page) == 0);
424 ATF_TC_CLEANUP(mlock_nested, tc)
427 restore_vm_max_wired();
434 page = sysconf(_SC_PAGESIZE);
435 ATF_REQUIRE(page >= 0);
437 ATF_TP_ADD_TC(tp, mlock_clip);
438 ATF_TP_ADD_TC(tp, mlock_err);
439 ATF_TP_ADD_TC(tp, mlock_limits);
440 ATF_TP_ADD_TC(tp, mlock_mmap);
441 ATF_TP_ADD_TC(tp, mlock_nested);
443 return atf_no_error();