xref: /freebsd/contrib/netbsd-tests/lib/libc/sys/t_mlock.c (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
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 #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 #include <limits.h>
51 #define _KMEMUSER
52 #include <machine/vmparam.h>
53 #endif
54 
55 static long page = 0;
56 
57 #ifdef __FreeBSD__
58 #define	VM_MAX_WIRED "vm.max_wired"
59 
60 static void
61 vm_max_wired_sysctl(int *old_value, int *new_value)
62 {
63 	size_t old_len;
64 	size_t new_len = (new_value == NULL ? 0 : sizeof(int));
65 
66 	if (old_value == NULL)
67 		printf("Setting the new value to %d\n", *new_value);
68 	else {
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));
72 	}
73 
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));
77 
78 	if (old_value != NULL)
79 		printf("Saved the old value (%d)\n", *old_value);
80 }
81 
82 static void
83 set_vm_max_wired(int new_value)
84 {
85 	FILE *fp;
86 	int old_value;
87 
88 	fp = fopen(VM_MAX_WIRED, "w");
89 	if (fp == NULL) {
90 		atf_tc_skip("could not open %s for writing: %s",
91 		    VM_MAX_WIRED, strerror(errno));
92 		return;
93 	}
94 
95 	vm_max_wired_sysctl(&old_value, NULL);
96 
97 	ATF_REQUIRE_MSG(fprintf(fp, "%d", old_value) > 0,
98 	    "saving %s failed", VM_MAX_WIRED);
99 
100 	fclose(fp);
101 
102 	vm_max_wired_sysctl(NULL, &new_value);
103 }
104 
105 static void
106 restore_vm_max_wired(void)
107 {
108 	FILE *fp;
109 	int saved_max_wired;
110 
111 	fp = fopen(VM_MAX_WIRED, "r");
112 	if (fp == NULL) {
113 		perror("fopen failed\n");
114 		return;
115 	}
116 
117 	if (fscanf(fp, "%d", &saved_max_wired) != 1) {
118 		perror("fscanf failed\n");
119 		fclose(fp);
120 		return;
121 	}
122 
123 	fclose(fp);
124 	printf("old value in %s: %d\n", VM_MAX_WIRED, saved_max_wired);
125 
126 	if (saved_max_wired == 0) /* This will cripple the test host */
127 		return;
128 
129 	vm_max_wired_sysctl(NULL, &saved_max_wired);
130 }
131 #endif
132 
133 ATF_TC(mlock_clip);
134 ATF_TC_HEAD(mlock_clip, tc)
135 {
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)");
138 }
139 
140 ATF_TC_BODY(mlock_clip, tc)
141 {
142 	void *buf;
143 
144 	buf = malloc(page);
145 	ATF_REQUIRE(buf != NULL);
146 
147 	if (page < 1024)
148 		atf_tc_skip("page size too small");
149 
150 	for (size_t i = page; i >= 1; i = i - 1024) {
151 		(void)mlock(buf, page - i);
152 		(void)munlock(buf, page - i);
153 	}
154 
155 	free(buf);
156 }
157 
158 #ifdef __FreeBSD__
159 ATF_TC_WITH_CLEANUP(mlock_err);
160 #else
161 ATF_TC(mlock_err);
162 #endif
163 ATF_TC_HEAD(mlock_err, tc)
164 {
165 	atf_tc_set_md_var(tc, "descr",
166 	    "Test error conditions in mlock(2) and munlock(2)");
167 #ifdef __FreeBSD__
168 	atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");
169 	atf_tc_set_md_var(tc, "require.user", "root");
170 #endif
171 }
172 
173 ATF_TC_BODY(mlock_err, tc)
174 {
175 #ifdef __NetBSD__
176 	unsigned long vmin = 0;
177 	size_t len = sizeof(vmin);
178 #endif
179 #if !defined(__aarch64__) && !defined(__riscv__)
180 	void *invalid_ptr;
181 #endif
182 	int null_errno = ENOMEM;	/* error expected for NULL */
183 	void *buf;
184 
185 #ifdef __FreeBSD__
186 #ifdef VM_MIN_ADDRESS
187 	if ((uintptr_t)VM_MIN_ADDRESS > 0)
188 		null_errno = EINVAL;	/* NULL is not inside user VM */
189 #endif
190 	/* Set max_wired really really high to avoid EAGAIN */
191 	set_vm_max_wired(INT_MAX);
192 #else
193 	if (sysctlbyname("vm.minaddress", &vmin, &len, NULL, 0) != 0)
194 		atf_tc_fail("failed to read vm.minaddress");
195 	/*
196 	 * Any bad address must return ENOMEM (for lock & unlock)
197 	 */
198 	errno = 0;
199 	ATF_REQUIRE_ERRNO(ENOMEM, mlock(NULL, page) == -1);
200 
201 	if (vmin > 0)
202 		null_errno = EINVAL;	/* NULL is not inside user VM */
203 #endif
204 
205 	errno = 0;
206 	ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)0, page) == -1);
207 
208 	errno = 0;
209 	ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)-1, page) == -1);
210 
211 	errno = 0;
212 	ATF_REQUIRE_ERRNO(ENOMEM, munlock(NULL, page) == -1);
213 
214 	errno = 0;
215 	ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)0, page) == -1);
216 
217 	errno = 0;
218 	ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)-1, page) == -1);
219 
220 	buf = malloc(page);
221 	ATF_REQUIRE(buf != NULL);
222 
223 	/*
224 	 * unlocking memory that is not locked is an error...
225 	 */
226 
227 	errno = 0;
228 	ATF_REQUIRE_ERRNO(ENOMEM, munlock(buf, page) == -1);
229 
230 /* There is no sbrk on AArch64 and RISC-V */
231 #if !defined(__aarch64__) && !defined(__riscv__)
232 	/*
233 	 * These are permitted to fail (EINVAL) but do not on NetBSD
234 	 */
235 	ATF_REQUIRE(mlock((void *)(((uintptr_t)buf) + page/3), page/5) == 0);
236 	ATF_REQUIRE(munlock((void *)(((uintptr_t)buf) + page/3), page/5) == 0);
237 
238 	(void)free(buf);
239 
240 	/*
241 	 * Try to create a pointer to an unmapped page - first after current
242 	 * brk will likely do.
243 	 */
244 	invalid_ptr = (void*)(((uintptr_t)sbrk(0)+page) & ~(page-1));
245 	printf("testing with (hopefully) invalid pointer %p\n", invalid_ptr);
246 
247 	errno = 0;
248 	ATF_REQUIRE_ERRNO(ENOMEM, mlock(invalid_ptr, page) == -1);
249 
250 	errno = 0;
251 	ATF_REQUIRE_ERRNO(ENOMEM, munlock(invalid_ptr, page) == -1);
252 #endif
253 }
254 
255 #ifdef __FreeBSD__
256 ATF_TC_CLEANUP(mlock_err, tc)
257 {
258 
259 	restore_vm_max_wired();
260 }
261 #endif
262 
263 ATF_TC(mlock_limits);
264 ATF_TC_HEAD(mlock_limits, tc)
265 {
266 	atf_tc_set_md_var(tc, "descr", "Test system limits with mlock(2)");
267 }
268 
269 ATF_TC_BODY(mlock_limits, tc)
270 {
271 	struct rlimit res;
272 	void *buf;
273 	pid_t pid;
274 	int sta;
275 
276 	buf = malloc(page);
277 	ATF_REQUIRE(buf != NULL);
278 
279 	pid = fork();
280 	ATF_REQUIRE(pid >= 0);
281 
282 	if (pid == 0) {
283 
284 		for (ssize_t i = page; i >= 2; i -= 100) {
285 
286 			res.rlim_cur = i - 1;
287 			res.rlim_max = i - 1;
288 
289 			(void)fprintf(stderr, "trying to lock %zd bytes "
290 			    "with %zu byte limit\n", i, (size_t)res.rlim_cur);
291 
292 			if (setrlimit(RLIMIT_MEMLOCK, &res) != 0)
293 				_exit(EXIT_FAILURE);
294 
295 			errno = 0;
296 
297 #ifdef __FreeBSD__
298 			/*
299 			 * NetBSD doesn't conform to POSIX with ENOMEM requirement;
300 			 * FreeBSD does.
301 			 *
302 			 * See: NetBSD PR # kern/48962 for more details.
303 			 */
304 			if (mlock(buf, i) != -1 || errno != ENOMEM) {
305 #else
306 			if (mlock(buf, i) != -1 || errno != EAGAIN) {
307 #endif
308 				(void)munlock(buf, i);
309 				_exit(EXIT_FAILURE);
310 			}
311 		}
312 
313 		_exit(EXIT_SUCCESS);
314 	}
315 
316 	(void)wait(&sta);
317 
318 	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
319 		atf_tc_fail("mlock(2) locked beyond system limits");
320 
321 	free(buf);
322 }
323 
324 #ifdef __FreeBSD__
325 ATF_TC_WITH_CLEANUP(mlock_mmap);
326 #else
327 ATF_TC(mlock_mmap);
328 #endif
329 ATF_TC_HEAD(mlock_mmap, tc)
330 {
331 	atf_tc_set_md_var(tc, "descr", "Test mlock(2)-mmap(2) interaction");
332 #ifdef __FreeBSD__
333 	atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");
334 	atf_tc_set_md_var(tc, "require.user", "root");
335 #endif
336 }
337 
338 ATF_TC_BODY(mlock_mmap, tc)
339 {
340 #ifdef __NetBSD__
341 	static const int flags = MAP_ANON | MAP_PRIVATE | MAP_WIRED;
342 #else
343 	static const int flags = MAP_ANON | MAP_PRIVATE;
344 #endif
345 	void *buf;
346 
347 #ifdef __FreeBSD__
348 	/* Set max_wired really really high to avoid EAGAIN */
349 	set_vm_max_wired(INT_MAX);
350 #endif
351 
352 	/*
353 	 * Make a wired RW mapping and check that mlock(2)
354 	 * does not fail for the (already locked) mapping.
355 	 */
356 	buf = mmap(NULL, page, PROT_READ | PROT_WRITE, flags, -1, 0);
357 
358 	ATF_REQUIRE(buf != MAP_FAILED);
359 #ifdef __FreeBSD__
360 	/*
361 	 * The duplicate mlock call is added to ensure that the call works
362 	 * as described above without MAP_WIRED support.
363 	 */
364 	ATF_REQUIRE(mlock(buf, page) == 0);
365 #endif
366 	ATF_REQUIRE(mlock(buf, page) == 0);
367 	ATF_REQUIRE(munlock(buf, page) == 0);
368 	ATF_REQUIRE(munmap(buf, page) == 0);
369 	ATF_REQUIRE(munlock(buf, page) != 0);
370 
371 	/*
372 	 * But it should be impossible to mlock(2) a PROT_NONE mapping.
373 	 */
374 	buf = mmap(NULL, page, PROT_NONE, flags, -1, 0);
375 
376 	ATF_REQUIRE(buf != MAP_FAILED);
377 #ifdef __FreeBSD__
378 	ATF_REQUIRE_ERRNO(ENOMEM, mlock(buf, page) != 0);
379 #else
380 	ATF_REQUIRE(mlock(buf, page) != 0);
381 #endif
382 	ATF_REQUIRE(munmap(buf, page) == 0);
383 }
384 
385 #ifdef __FreeBSD__
386 ATF_TC_CLEANUP(mlock_mmap, tc)
387 {
388 
389 	restore_vm_max_wired();
390 }
391 #endif
392 
393 #ifdef __FreeBSD__
394 ATF_TC_WITH_CLEANUP(mlock_nested);
395 #else
396 ATF_TC(mlock_nested);
397 #endif
398 ATF_TC_HEAD(mlock_nested, tc)
399 {
400 	atf_tc_set_md_var(tc, "descr",
401 	    "Test that consecutive mlock(2) calls succeed");
402 #ifdef __FreeBSD__
403 	atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");
404 	atf_tc_set_md_var(tc, "require.user", "root");
405 #endif
406 }
407 
408 ATF_TC_BODY(mlock_nested, tc)
409 {
410 	const size_t maxiter = 100;
411 	void *buf;
412 
413 #ifdef __FreeBSD__
414 	/* Set max_wired really really high to avoid EAGAIN */
415 	set_vm_max_wired(INT_MAX);
416 #endif
417 
418 	buf = malloc(page);
419 	ATF_REQUIRE(buf != NULL);
420 
421 	for (size_t i = 0; i < maxiter; i++)
422 		ATF_REQUIRE(mlock(buf, page) == 0);
423 
424 	ATF_REQUIRE(munlock(buf, page) == 0);
425 	free(buf);
426 }
427 
428 #ifdef __FreeBSD__
429 ATF_TC_CLEANUP(mlock_nested, tc)
430 {
431 
432 	restore_vm_max_wired();
433 }
434 #endif
435 
436 ATF_TP_ADD_TCS(tp)
437 {
438 
439 	page = sysconf(_SC_PAGESIZE);
440 	ATF_REQUIRE(page >= 0);
441 
442 	ATF_TP_ADD_TC(tp, mlock_clip);
443 	ATF_TP_ADD_TC(tp, mlock_err);
444 	ATF_TP_ADD_TC(tp, mlock_limits);
445 	ATF_TP_ADD_TC(tp, mlock_mmap);
446 	ATF_TP_ADD_TC(tp, mlock_nested);
447 
448 	return atf_no_error();
449 }
450