t_mlock.c revision 1.2.2.3 1 /* $NetBSD: t_mlock.c,v 1.2.2.3 2012/10/30 19:00:01 yamt 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.2.2.3 2012/10/30 19:00:01 yamt Exp $");
33
34 #include <sys/mman.h>
35 #include <sys/resource.h>
36 #include <sys/wait.h>
37 #define _KMEMUSER
38 #include <machine/vmparam.h>
39
40 #include <errno.h>
41 #include <atf-c.h>
42 #include <stdint.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46
47 static long page = 0;
48
49 ATF_TC(mlock_clip);
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
56 ATF_TC_BODY(mlock_clip, tc)
57 {
58 void *buf;
59
60 buf = malloc(page);
61 ATF_REQUIRE(buf != NULL);
62
63 if (page < 1024)
64 atf_tc_skip("page size too small");
65
66 for (size_t i = page; i >= 1; i = i - 1024) {
67 (void)mlock(buf, page - i);
68 (void)munlock(buf, page - i);
69 }
70
71 free(buf);
72 }
73
74 ATF_TC(mlock_err);
75 ATF_TC_HEAD(mlock_err, tc)
76 {
77 atf_tc_set_md_var(tc, "descr",
78 "Test error conditions in mlock(2) and munlock(2)");
79 }
80
81 ATF_TC_BODY(mlock_err, tc)
82 {
83 void *invalid_ptr;
84 int null_errno = ENOMEM; /* error expected for NULL */
85
86 #ifdef VM_MIN_ADDRESS
87 if ((uintptr_t)VM_MIN_ADDRESS > 0)
88 null_errno = EINVAL; /* NULL is not inside user VM */
89 #endif
90
91 errno = 0;
92 ATF_REQUIRE_ERRNO(null_errno, mlock(NULL, page) == -1);
93
94 errno = 0;
95 ATF_REQUIRE_ERRNO(null_errno, mlock((char *)0, page) == -1);
96
97 errno = 0;
98 ATF_REQUIRE_ERRNO(EINVAL, mlock((char *)-1, page) == -1);
99
100 errno = 0;
101 ATF_REQUIRE_ERRNO(null_errno, munlock(NULL, page) == -1);
102
103 errno = 0;
104 ATF_REQUIRE_ERRNO(null_errno, munlock((char *)0, page) == -1);
105
106 errno = 0;
107 ATF_REQUIRE_ERRNO(EINVAL, munlock((char *)-1, page) == -1);
108
109 /*
110 * Try to create a pointer to an unmapped page - first after current
111 * brk will likely do.
112 */
113 invalid_ptr = (void*)(((uintptr_t)sbrk(0)+page) & ~(page-1));
114 printf("testing with (hopefully) invalid pointer %p\n", invalid_ptr);
115
116 errno = 0;
117 ATF_REQUIRE_ERRNO(ENOMEM, mlock(invalid_ptr, page) == -1);
118
119 errno = 0;
120 ATF_REQUIRE_ERRNO(ENOMEM, munlock(invalid_ptr, page) == -1);
121 }
122
123 ATF_TC(mlock_limits);
124 ATF_TC_HEAD(mlock_limits, tc)
125 {
126 atf_tc_set_md_var(tc, "descr", "Test system limits with mlock(2)");
127 }
128
129 ATF_TC_BODY(mlock_limits, tc)
130 {
131 struct rlimit res;
132 void *buf;
133 pid_t pid;
134 int sta;
135
136 buf = malloc(page);
137 ATF_REQUIRE(buf != NULL);
138
139 pid = fork();
140 ATF_REQUIRE(pid >= 0);
141
142 if (pid == 0) {
143
144 for (ssize_t i = page; i >= 2; i -= 100) {
145
146 res.rlim_cur = i - 1;
147 res.rlim_max = i - 1;
148
149 (void)fprintf(stderr, "trying to lock %zd bytes "
150 "with %zu byte limit\n", i, (size_t)res.rlim_cur);
151
152 if (setrlimit(RLIMIT_MEMLOCK, &res) != 0)
153 _exit(EXIT_FAILURE);
154
155 errno = 0;
156
157 if (mlock(buf, i) != -1 || errno != EAGAIN) {
158 (void)munlock(buf, i);
159 _exit(EXIT_FAILURE);
160 }
161 }
162
163 _exit(EXIT_SUCCESS);
164 }
165
166 (void)wait(&sta);
167
168 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
169 atf_tc_fail("mlock(2) locked beyond system limits");
170
171 free(buf);
172 }
173
174 ATF_TC(mlock_mmap);
175 ATF_TC_HEAD(mlock_mmap, tc)
176 {
177 atf_tc_set_md_var(tc, "descr", "Test mlock(2)-mmap(2) interaction");
178 }
179
180 ATF_TC_BODY(mlock_mmap, tc)
181 {
182 static const int flags = MAP_ANON | MAP_PRIVATE | MAP_WIRED;
183 void *buf;
184
185 /*
186 * Make a wired RW mapping and check that mlock(2)
187 * does not fail for the (already locked) mapping.
188 */
189 buf = mmap(NULL, page, PROT_READ | PROT_WRITE, flags, -1, 0);
190
191 ATF_REQUIRE(buf != MAP_FAILED);
192 ATF_REQUIRE(mlock(buf, page) == 0);
193 ATF_REQUIRE(munlock(buf, page) == 0);
194 ATF_REQUIRE(munmap(buf, page) == 0);
195 ATF_REQUIRE(munlock(buf, page) != 0);
196
197 /*
198 * But it should be impossible to mlock(2) a PROT_NONE mapping.
199 */
200 buf = mmap(NULL, page, PROT_NONE, flags, -1, 0);
201
202 ATF_REQUIRE(buf != MAP_FAILED);
203 ATF_REQUIRE(mlock(buf, page) != 0);
204 ATF_REQUIRE(munmap(buf, page) == 0);
205 }
206
207 ATF_TC(mlock_nested);
208 ATF_TC_HEAD(mlock_nested, tc)
209 {
210 atf_tc_set_md_var(tc, "descr",
211 "Test that consecutive mlock(2) calls succeed");
212 }
213
214 ATF_TC_BODY(mlock_nested, tc)
215 {
216 const size_t maxiter = 100;
217 void *buf;
218
219 buf = malloc(page);
220 ATF_REQUIRE(buf != NULL);
221
222 for (size_t i = 0; i < maxiter; i++)
223 ATF_REQUIRE(mlock(buf, page) == 0);
224
225 ATF_REQUIRE(munlock(buf, page) == 0);
226 free(buf);
227 }
228
229 ATF_TP_ADD_TCS(tp)
230 {
231
232 page = sysconf(_SC_PAGESIZE);
233 ATF_REQUIRE(page >= 0);
234
235 ATF_TP_ADD_TC(tp, mlock_clip);
236 ATF_TP_ADD_TC(tp, mlock_err);
237 ATF_TP_ADD_TC(tp, mlock_limits);
238 ATF_TP_ADD_TC(tp, mlock_mmap);
239 ATF_TP_ADD_TC(tp, mlock_nested);
240
241 return atf_no_error();
242 }
243