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