xref: /openbsd/regress/lib/libc/sys/t_mlock.c (revision 55cc5ba3)
1 /*	$OpenBSD: t_mlock.c,v 1.2 2020/11/09 23:18:51 bluhm Exp $	*/
2 /* $NetBSD: t_mlock.c,v 1.8 2020/01/24 08:45:16 skrll Exp $ */
3 
4 /*-
5  * Copyright (c) 2012 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jukka Ruohonen.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "macros.h"
34 
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: t_mlock.c,v 1.8 2020/01/24 08:45:16 skrll Exp $");
37 
38 #include <sys/mman.h>
39 #include <sys/resource.h>
40 #include <sys/sysctl.h>
41 #include <sys/wait.h>
42 
43 #include <errno.h>
44 #include "atf-c.h"
45 #include <stdint.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 
51 static long page = 0;
52 
53 ATF_TC(mlock_clip);
54 ATF_TC_HEAD(mlock_clip, tc)
55 {
56 	atf_tc_set_md_var(tc, "descr", "Test with mlock(2) that UVM only "
57 	    "clips if the clip address is within the entry (PR kern/44788)");
58 }
59 
60 ATF_TC_BODY(mlock_clip, tc)
61 {
62 	void *buf;
63 	int err1, err2;
64 
65 	buf = malloc(page);
66 	ATF_REQUIRE(buf != NULL);
67 	fprintf(stderr, "mlock_clip: buf = %p (page=%ld)\n", buf, page);
68 
69 	if (page < 1024)
70 		atf_tc_skip("page size too small");
71 
72 	for (size_t i = page; i >= 1; i = i - 1024) {
73 		err1 = mlock(buf, page - i);
74 		if (err1 != 0)
75 			fprintf(stderr, "mlock_clip: page=%ld i=%zu,"
76 			    " mlock(%p, %ld): %s\n", page, i, buf, page - i,
77 			    strerror(errno));
78 		err2 = munlock(buf, page - i);
79 		if (err2 != 0)
80 			fprintf(stderr, "mlock_clip: page=%ld i=%zu,"
81 			    " munlock(%p, %ld): %s (mlock %s)\n", page, i,
82 			    buf, page - i, strerror(errno), err1?"failed":"ok");
83 	}
84 
85 	free(buf);
86 }
87 
88 ATF_TC(mlock_err);
89 ATF_TC_HEAD(mlock_err, tc)
90 {
91 	atf_tc_set_md_var(tc, "descr",
92 	    "Test error conditions in mlock(2) and munlock(2)");
93 }
94 
95 ATF_TC_BODY(mlock_err, tc)
96 {
97 	void *invalid_ptr;
98 	void *buf;
99 	int mlock_err, munlock_err;
100 
101 	/*
102 	 * Any bad address must return ENOMEM (for lock & unlock)
103 	 */
104 	errno = 0;
105 	ATF_REQUIRE_ERRNO(ENOMEM, mlock(NULL, page) == -1);
106 
107 	errno = 0;
108 	ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)0, page) == -1);
109 
110 	errno = 0;
111 #ifdef __OpenBSD__
112 	ATF_REQUIRE_ERRNO(EINVAL, mlock((char *)-1, page) == -1);
113 #else
114 	ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)-1, page) == -1);
115 #endif
116 
117 	errno = 0;
118 	ATF_REQUIRE_ERRNO(ENOMEM, munlock(NULL, page) == -1);
119 
120 	errno = 0;
121 	ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)0, page) == -1);
122 
123 	errno = 0;
124 #ifdef __OpenBSD__
125 	ATF_REQUIRE_ERRNO(EINVAL, munlock((char *)-1, page) == -1);
126 #else
127 	ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)-1, page) == -1);
128 #endif
129 
130 	buf = malloc(page);
131 	ATF_REQUIRE(buf != NULL);
132 	fprintf(stderr, "mlock_err: buf = %p (page=%ld)\n", buf, page);
133 
134 	/*
135 	 * unlocking memory that is not locked is an error...
136 	 */
137 
138 #ifndef __OpenBSD__
139 	errno = 0;
140 	ATF_REQUIRE_ERRNO(ENOMEM, munlock(buf, page) == -1);
141 #endif
142 
143 	/*
144 	 * These are permitted to fail (EINVAL) but do not on NetBSD
145 	 */
146 	mlock_err = mlock((void *)(((uintptr_t)buf) + page/3), page/5);
147 	if (mlock_err != 0)
148 	    fprintf(stderr, "mlock_err: mlock(%p, %ld): %d [%d] %s\n",
149 		(void *)(((uintptr_t)buf) + page/3), page/5, mlock_err,
150 		errno, strerror(errno));
151 	ATF_REQUIRE(mlock_err == 0);
152 	munlock_err= munlock((void *)(((uintptr_t)buf) + page/3), page/5);
153 	if (munlock_err != 0)
154 	    fprintf(stderr, "mlock_err: munlock(%p, %ld): %d [%d] %s\n",
155 		(void *)(((uintptr_t)buf) + page/3), page/5, munlock_err,
156 		errno, strerror(errno));
157 	ATF_REQUIRE(munlock_err == 0);
158 
159 	(void)free(buf);
160 
161 	/*
162 	 * Try to create a pointer to an unmapped page - first after current
163 	 * brk will likely do.
164 	 */
165 	invalid_ptr = (void*)(((uintptr_t)sbrk(0)+page) & ~(page-1));
166 	printf("testing with (hopefully) invalid pointer %p\n", invalid_ptr);
167 
168 	errno = 0;
169 	ATF_REQUIRE_ERRNO(ENOMEM, mlock(invalid_ptr, page) == -1);
170 
171 	errno = 0;
172 	ATF_REQUIRE_ERRNO(ENOMEM, munlock(invalid_ptr, page) == -1);
173 }
174 
175 ATF_TC(mlock_limits);
176 ATF_TC_HEAD(mlock_limits, tc)
177 {
178 	atf_tc_set_md_var(tc, "descr", "Test system limits with mlock(2)");
179 }
180 
181 ATF_TC_BODY(mlock_limits, tc)
182 {
183 	struct rlimit res;
184 	void *buf;
185 	pid_t pid;
186 	int sta;
187 
188 	buf = malloc(page);
189 	ATF_REQUIRE(buf != NULL);
190 	fprintf(stderr, "mlock_limits: buf = %p (page=%ld)\n", buf, page);
191 
192 	pid = fork();
193 	ATF_REQUIRE(pid >= 0);
194 
195 	if (pid == 0) {
196 
197 		for (ssize_t i = page; i >= 2; i -= 100) {
198 
199 			res.rlim_cur = i - 1;
200 			res.rlim_max = i - 1;
201 
202 			(void)fprintf(stderr, "trying to lock %zu bytes "
203 			    "with %zu byte limit\n", i, (size_t)res.rlim_cur);
204 
205 			if (setrlimit(RLIMIT_MEMLOCK, &res) != 0)
206 				_exit(EXIT_FAILURE);
207 
208 			errno = 0;
209 
210 			if ((sta = mlock(buf, i)) != -1 || errno != EAGAIN) {
211 				fprintf(stderr, "mlock(%p, %zu): %d [%d] %s\n",
212 				    buf, i, sta, errno, strerror(errno));
213 				(void)munlock(buf, i);
214 				_exit(EXIT_FAILURE);
215 			}
216 		}
217 
218 		_exit(EXIT_SUCCESS);
219 	}
220 
221 	(void)wait(&sta);
222 
223 	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
224 		atf_tc_fail("mlock(2) locked beyond system limits");
225 
226 	free(buf);
227 }
228 
229 ATF_TC(mlock_mmap);
230 ATF_TC_HEAD(mlock_mmap, tc)
231 {
232 	atf_tc_set_md_var(tc, "descr", "Test mlock(2)-mmap(2) interaction");
233 }
234 
235 ATF_TC_BODY(mlock_mmap, tc)
236 {
237 #ifdef __OpenBSD__
238 	static const int flags = MAP_ANON | MAP_PRIVATE;
239 #else
240 	static const int flags = MAP_ANON | MAP_PRIVATE | MAP_WIRED;
241 #endif
242 	void *buf;
243 
244 	/*
245 	 * Make a wired RW mapping and check that mlock(2)
246 	 * does not fail for the (already locked) mapping.
247 	 */
248 	buf = mmap(NULL, page, PROT_READ | PROT_WRITE, flags, -1, 0);
249 
250 	if (buf == MAP_FAILED)
251 		fprintf(stderr,
252 		    "mlock_mmap: mmap(NULL, %ld, %#x, %#x, -1, 0): MAP_FAILED"
253 		    " [%d] %s\n", page, PROT_READ | PROT_WRITE, flags, errno,
254 		    strerror(errno));
255 
256 	ATF_REQUIRE(buf != MAP_FAILED);
257 
258 	fprintf(stderr, "mlock_mmap: buf=%p, page=%ld\n", buf, page);
259 
260 	ATF_REQUIRE(mlock(buf, page) == 0);
261 	ATF_REQUIRE(munlock(buf, page) == 0);
262 	ATF_REQUIRE(munmap(buf, page) == 0);
263 	ATF_REQUIRE(munlock(buf, page) != 0);
264 
265 	fprintf(stderr, "mlock_mmap: first test succeeded\n");
266 
267 	/*
268 	 * But it should be impossible to mlock(2) a PROT_NONE mapping.
269 	 */
270 	buf = mmap(NULL, page, PROT_NONE, flags, -1, 0);
271 
272 	if (buf == MAP_FAILED)
273 		fprintf(stderr,
274 		    "mlock_mmap: mmap(NULL, %ld, %#x, %#x, -1, 0): MAP_FAILED"
275 		    " [%d] %s\n", page, PROT_NONE, flags, errno,
276 		    strerror(errno));
277 
278 	ATF_REQUIRE(buf != MAP_FAILED);
279 	ATF_REQUIRE(mlock(buf, page) != 0);
280 	ATF_REQUIRE(munmap(buf, page) == 0);
281 
282 	fprintf(stderr, "mlock_mmap: second test succeeded\n");
283 }
284 
285 ATF_TC(mlock_nested);
286 ATF_TC_HEAD(mlock_nested, tc)
287 {
288 	atf_tc_set_md_var(tc, "descr",
289 	    "Test that consecutive mlock(2) calls succeed");
290 }
291 
292 ATF_TC_BODY(mlock_nested, tc)
293 {
294 	const size_t maxiter = 100;
295 	void *buf;
296 	int err;
297 
298 	buf = malloc(page);
299 	ATF_REQUIRE(buf != NULL);
300 	fprintf(stderr, "mlock_nested: buf = %p (page=%ld)\n", buf, page);
301 
302 	for (size_t i = 0; i < maxiter; i++) {
303 		err = mlock(buf, page);
304 		if (err != 0)
305 		    fprintf(stderr,
306 		    "mlock_nested: i=%zu (of %zu) mlock(%p, %ld): %d [%d] %s\n",
307 			i, maxiter, buf, page, err, errno, strerror(errno));
308 		ATF_REQUIRE(err == 0);
309 	}
310 
311 	err = munlock(buf, page);
312 	if (err != 0)
313 		fprintf(stderr, "mlock_nested: munlock(%p, %ld): %d [%d] %s\n",
314 		    buf, page, err, errno, strerror(errno));
315 	ATF_REQUIRE(err == 0);
316 	free(buf);
317 }
318 
319 ATF_TP_ADD_TCS(tp)
320 {
321 
322 	page = sysconf(_SC_PAGESIZE);
323 	ATF_REQUIRE(page >= 0);
324 
325 	ATF_TP_ADD_TC(tp, mlock_clip);
326 	ATF_TP_ADD_TC(tp, mlock_err);
327 	ATF_TP_ADD_TC(tp, mlock_limits);
328 	ATF_TP_ADD_TC(tp, mlock_mmap);
329 	ATF_TP_ADD_TC(tp, mlock_nested);
330 
331 	return atf_no_error();
332 }
333