1 /* $NetBSD: t_mlock.c,v 1.8 2020/01/24 08:45:16 skrll 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.8 2020/01/24 08:45:16 skrll Exp $");
33 
34 #include <sys/mman.h>
35 #include <sys/resource.h>
36 #include <sys/sysctl.h>
37 #include <sys/wait.h>
38 
39 #include <errno.h>
40 #include <atf-c.h>
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 static long page = 0;
48 
49 ATF_TC(mlock_clip);
ATF_TC_HEAD(mlock_clip,tc)50 ATF_TC_HEAD(mlock_clip, tc)
51 {
52           atf_tc_set_md_var(tc, "descr", "Test with mlock(2) that UVM only "
53               "clips if the clip address is within the entry (PR kern/44788)");
54 }
55 
ATF_TC_BODY(mlock_clip,tc)56 ATF_TC_BODY(mlock_clip, tc)
57 {
58           void *buf;
59           int err1, err2;
60 
61           buf = malloc(page);
62           ATF_REQUIRE(buf != NULL);
63           fprintf(stderr, "mlock_clip: buf = %p (page=%ld)\n", buf, page);
64 
65           if (page < 1024)
66                     atf_tc_skip("page size too small");
67 
68           for (size_t i = page; i >= 1; i = i - 1024) {
69                     err1 = mlock(buf, page - i);
70                     if (err1 != 0)
71                               fprintf(stderr, "mlock_clip: page=%ld i=%zu,"
72                                   " mlock(%p, %ld): %s\n", page, i, buf, page - i,
73                                   strerror(errno));
74                     err2 = munlock(buf, page - i);
75                     if (err2 != 0)
76                               fprintf(stderr, "mlock_clip: page=%ld i=%zu,"
77                                   " munlock(%p, %ld): %s (mlock %s)\n", page, i,
78                                   buf, page - i, strerror(errno), err1?"failed":"ok");
79           }
80 
81           free(buf);
82 }
83 
84 ATF_TC(mlock_err);
ATF_TC_HEAD(mlock_err,tc)85 ATF_TC_HEAD(mlock_err, tc)
86 {
87           atf_tc_set_md_var(tc, "descr",
88               "Test error conditions in mlock(2) and munlock(2)");
89 }
90 
ATF_TC_BODY(mlock_err,tc)91 ATF_TC_BODY(mlock_err, tc)
92 {
93           void *invalid_ptr;
94           void *buf;
95           int mlock_err, munlock_err;
96 
97           /*
98            * Any bad address must return ENOMEM (for lock & unlock)
99            */
100           errno = 0;
101           ATF_REQUIRE_ERRNO(ENOMEM, mlock(NULL, page) == -1);
102 
103           errno = 0;
104           ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)0, page) == -1);
105 
106           errno = 0;
107           ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)-1, page) == -1);
108 
109           errno = 0;
110           ATF_REQUIRE_ERRNO(ENOMEM, munlock(NULL, page) == -1);
111 
112           errno = 0;
113           ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)0, page) == -1);
114 
115           errno = 0;
116           ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)-1, page) == -1);
117 
118           buf = malloc(page);
119           ATF_REQUIRE(buf != NULL);
120           fprintf(stderr, "mlock_err: buf = %p (page=%ld)\n", buf, page);
121 
122           /*
123            * unlocking memory that is not locked is an error...
124            */
125 
126           errno = 0;
127           ATF_REQUIRE_ERRNO(ENOMEM, munlock(buf, page) == -1);
128 
129           /*
130            * These are permitted to fail (EINVAL) but do not on NetBSD
131            */
132           mlock_err = mlock((void *)(((uintptr_t)buf) + page/3), page/5);
133           if (mlock_err != 0)
134               fprintf(stderr, "mlock_err: mlock(%p, %ld): %d [%d] %s\n",
135                     (void *)(((uintptr_t)buf) + page/3), page/5, mlock_err,
136                     errno, strerror(errno));
137           ATF_REQUIRE(mlock_err == 0);
138           munlock_err= munlock((void *)(((uintptr_t)buf) + page/3), page/5);
139           if (munlock_err != 0)
140               fprintf(stderr, "mlock_err: munlock(%p, %ld): %d [%d] %s\n",
141                     (void *)(((uintptr_t)buf) + page/3), page/5, munlock_err,
142                     errno, strerror(errno));
143           ATF_REQUIRE(munlock_err == 0);
144 
145           (void)free(buf);
146 
147           /*
148            * Try to create a pointer to an unmapped page - first after current
149            * brk will likely do.
150            */
151           invalid_ptr = (void*)(((uintptr_t)sbrk(0)+page) & ~(page-1));
152           printf("testing with (hopefully) invalid pointer %p\n", invalid_ptr);
153 
154           errno = 0;
155           ATF_REQUIRE_ERRNO(ENOMEM, mlock(invalid_ptr, page) == -1);
156 
157           errno = 0;
158           ATF_REQUIRE_ERRNO(ENOMEM, munlock(invalid_ptr, page) == -1);
159 }
160 
161 ATF_TC(mlock_limits);
ATF_TC_HEAD(mlock_limits,tc)162 ATF_TC_HEAD(mlock_limits, tc)
163 {
164           atf_tc_set_md_var(tc, "descr", "Test system limits with mlock(2)");
165 }
166 
ATF_TC_BODY(mlock_limits,tc)167 ATF_TC_BODY(mlock_limits, tc)
168 {
169           struct rlimit res;
170           void *buf;
171           pid_t pid;
172           int sta;
173 
174           buf = malloc(page);
175           ATF_REQUIRE(buf != NULL);
176           fprintf(stderr, "mlock_limits: buf = %p (page=%ld)\n", buf, page);
177 
178           pid = fork();
179           ATF_REQUIRE(pid >= 0);
180 
181           if (pid == 0) {
182 
183                     for (ssize_t i = page; i >= 2; i -= 100) {
184 
185                               res.rlim_cur = i - 1;
186                               res.rlim_max = i - 1;
187 
188                               (void)fprintf(stderr, "trying to lock %zu bytes "
189                                   "with %zu byte limit\n", i, (size_t)res.rlim_cur);
190 
191                               if (setrlimit(RLIMIT_MEMLOCK, &res) != 0)
192                                         _exit(EXIT_FAILURE);
193 
194                               errno = 0;
195 
196                               if ((sta = mlock(buf, i)) != -1 || errno != EAGAIN) {
197                                         fprintf(stderr, "mlock(%p, %zu): %d [%d] %s\n",
198                                             buf, i, sta, errno, strerror(errno));
199                                         (void)munlock(buf, i);
200                                         _exit(EXIT_FAILURE);
201                               }
202                     }
203 
204                     _exit(EXIT_SUCCESS);
205           }
206 
207           (void)wait(&sta);
208 
209           if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
210                     atf_tc_fail("mlock(2) locked beyond system limits");
211 
212           free(buf);
213 }
214 
215 ATF_TC(mlock_mmap);
ATF_TC_HEAD(mlock_mmap,tc)216 ATF_TC_HEAD(mlock_mmap, tc)
217 {
218           atf_tc_set_md_var(tc, "descr", "Test mlock(2)-mmap(2) interaction");
219 }
220 
ATF_TC_BODY(mlock_mmap,tc)221 ATF_TC_BODY(mlock_mmap, tc)
222 {
223           static const int flags = MAP_ANON | MAP_PRIVATE | MAP_WIRED;
224           void *buf;
225 
226           /*
227            * Make a wired RW mapping and check that mlock(2)
228            * does not fail for the (already locked) mapping.
229            */
230           buf = mmap(NULL, page, PROT_READ | PROT_WRITE, flags, -1, 0);
231 
232           if (buf == MAP_FAILED)
233                     fprintf(stderr,
234                         "mlock_mmap: mmap(NULL, %ld, %#x, %#x, -1, 0): MAP_FAILED"
235                         " [%d] %s\n", page, PROT_READ | PROT_WRITE, flags, errno,
236                         strerror(errno));
237 
238           ATF_REQUIRE(buf != MAP_FAILED);
239 
240           fprintf(stderr, "mlock_mmap: buf=%p, page=%ld\n", buf, page);
241 
242           ATF_REQUIRE(mlock(buf, page) == 0);
243           ATF_REQUIRE(munlock(buf, page) == 0);
244           ATF_REQUIRE(munmap(buf, page) == 0);
245           ATF_REQUIRE(munlock(buf, page) != 0);
246 
247           fprintf(stderr, "mlock_mmap: first test succeeded\n");
248 
249           /*
250            * But it should be impossible to mlock(2) a PROT_NONE mapping.
251            */
252           buf = mmap(NULL, page, PROT_NONE, flags, -1, 0);
253 
254           if (buf == MAP_FAILED)
255                     fprintf(stderr,
256                         "mlock_mmap: mmap(NULL, %ld, %#x, %#x, -1, 0): MAP_FAILED"
257                         " [%d] %s\n", page, PROT_NONE, flags, errno,
258                         strerror(errno));
259 
260           ATF_REQUIRE(buf != MAP_FAILED);
261           ATF_REQUIRE(mlock(buf, page) != 0);
262           ATF_REQUIRE(munmap(buf, page) == 0);
263 
264           fprintf(stderr, "mlock_mmap: second test succeeded\n");
265 }
266 
267 ATF_TC(mlock_nested);
ATF_TC_HEAD(mlock_nested,tc)268 ATF_TC_HEAD(mlock_nested, tc)
269 {
270           atf_tc_set_md_var(tc, "descr",
271               "Test that consecutive mlock(2) calls succeed");
272 }
273 
ATF_TC_BODY(mlock_nested,tc)274 ATF_TC_BODY(mlock_nested, tc)
275 {
276           const size_t maxiter = 100;
277           void *buf;
278           int err;
279 
280           buf = malloc(page);
281           ATF_REQUIRE(buf != NULL);
282           fprintf(stderr, "mlock_nested: buf = %p (page=%ld)\n", buf, page);
283 
284           for (size_t i = 0; i < maxiter; i++) {
285                     err = mlock(buf, page);
286                     if (err != 0)
287                         fprintf(stderr,
288                         "mlock_nested: i=%zu (of %zu) mlock(%p, %ld): %d [%d] %s\n",
289                               i, maxiter, buf, page, err, errno, strerror(errno));
290                     ATF_REQUIRE(err == 0);
291           }
292 
293           err = munlock(buf, page);
294           if (err != 0)
295                     fprintf(stderr, "mlock_nested: munlock(%p, %ld): %d [%d] %s\n",
296                         buf, page, err, errno, strerror(errno));
297           ATF_REQUIRE(err == 0);
298           free(buf);
299 }
300 
ATF_TP_ADD_TCS(tp)301 ATF_TP_ADD_TCS(tp)
302 {
303 
304           page = sysconf(_SC_PAGESIZE);
305           ATF_REQUIRE(page >= 0);
306 
307           ATF_TP_ADD_TC(tp, mlock_clip);
308           ATF_TP_ADD_TC(tp, mlock_err);
309           ATF_TP_ADD_TC(tp, mlock_limits);
310           ATF_TP_ADD_TC(tp, mlock_mmap);
311           ATF_TP_ADD_TC(tp, mlock_nested);
312 
313           return atf_no_error();
314 }
315