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