1 /* $NetBSD: t_mlock.c,v 1.5 2014/02/26 20:49:26 martin Exp $ */
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_mlock.c,v 1.5 2014/02/26 20:49:26 martin Exp $");
35 #include <sys/resource.h>
36 #include <sys/sysctl.h>
49 ATF_TC_HEAD(mlock_clip
, tc
)
51 atf_tc_set_md_var(tc
, "descr", "Test with mlock(2) that UVM only "
52 "clips if the clip address is within the entry (PR kern/44788)");
55 ATF_TC_BODY(mlock_clip
, tc
)
60 ATF_REQUIRE(buf
!= NULL
);
63 atf_tc_skip("page size too small");
65 for (size_t i
= page
; i
>= 1; i
= i
- 1024) {
66 (void)mlock(buf
, page
- i
);
67 (void)munlock(buf
, page
- i
);
74 ATF_TC_HEAD(mlock_err
, tc
)
76 atf_tc_set_md_var(tc
, "descr",
77 "Test error conditions in mlock(2) and munlock(2)");
80 ATF_TC_BODY(mlock_err
, tc
)
82 unsigned long vmin
= 0;
83 size_t len
= sizeof(vmin
);
85 int null_errno
= ENOMEM
; /* error expected for NULL */
87 if (sysctlbyname("vm.minaddress", &vmin
, &len
, NULL
, 0) != 0)
88 atf_tc_fail("failed to read vm.minaddress");
91 null_errno
= EINVAL
; /* NULL is not inside user VM */
94 ATF_REQUIRE_ERRNO(null_errno
, mlock(NULL
, page
) == -1);
97 ATF_REQUIRE_ERRNO(null_errno
, mlock((char *)0, page
) == -1);
100 ATF_REQUIRE_ERRNO(EINVAL
, mlock((char *)-1, page
) == -1);
103 ATF_REQUIRE_ERRNO(null_errno
, munlock(NULL
, page
) == -1);
106 ATF_REQUIRE_ERRNO(null_errno
, munlock((char *)0, page
) == -1);
109 ATF_REQUIRE_ERRNO(EINVAL
, munlock((char *)-1, page
) == -1);
112 * Try to create a pointer to an unmapped page - first after current
113 * brk will likely do.
115 invalid_ptr
= (void*)(((uintptr_t)sbrk(0)+page
) & ~(page
-1));
116 printf("testing with (hopefully) invalid pointer %p\n", invalid_ptr
);
119 ATF_REQUIRE_ERRNO(ENOMEM
, mlock(invalid_ptr
, page
) == -1);
122 ATF_REQUIRE_ERRNO(ENOMEM
, munlock(invalid_ptr
, page
) == -1);
125 ATF_TC(mlock_limits
);
126 ATF_TC_HEAD(mlock_limits
, tc
)
128 atf_tc_set_md_var(tc
, "descr", "Test system limits with mlock(2)");
131 ATF_TC_BODY(mlock_limits
, tc
)
139 ATF_REQUIRE(buf
!= NULL
);
142 ATF_REQUIRE(pid
>= 0);
146 for (ssize_t i
= page
; i
>= 2; i
-= 100) {
148 res
.rlim_cur
= i
- 1;
149 res
.rlim_max
= i
- 1;
151 (void)fprintf(stderr
, "trying to lock %zd bytes "
152 "with %zu byte limit\n", i
, (size_t)res
.rlim_cur
);
154 if (setrlimit(RLIMIT_MEMLOCK
, &res
) != 0)
159 if (mlock(buf
, i
) != -1 || errno
!= EAGAIN
) {
160 (void)munlock(buf
, i
);
170 if (WIFEXITED(sta
) == 0 || WEXITSTATUS(sta
) != EXIT_SUCCESS
)
171 atf_tc_fail("mlock(2) locked beyond system limits");
177 ATF_TC_HEAD(mlock_mmap
, tc
)
179 atf_tc_set_md_var(tc
, "descr", "Test mlock(2)-mmap(2) interaction");
182 ATF_TC_BODY(mlock_mmap
, tc
)
184 static const int flags
= MAP_ANON
| MAP_PRIVATE
| MAP_WIRED
;
188 * Make a wired RW mapping and check that mlock(2)
189 * does not fail for the (already locked) mapping.
191 buf
= mmap(NULL
, page
, PROT_READ
| PROT_WRITE
, flags
, -1, 0);
193 ATF_REQUIRE(buf
!= MAP_FAILED
);
194 ATF_REQUIRE(mlock(buf
, page
) == 0);
195 ATF_REQUIRE(munlock(buf
, page
) == 0);
196 ATF_REQUIRE(munmap(buf
, page
) == 0);
197 ATF_REQUIRE(munlock(buf
, page
) != 0);
200 * But it should be impossible to mlock(2) a PROT_NONE mapping.
202 buf
= mmap(NULL
, page
, PROT_NONE
, flags
, -1, 0);
204 ATF_REQUIRE(buf
!= MAP_FAILED
);
205 ATF_REQUIRE(mlock(buf
, page
) != 0);
206 ATF_REQUIRE(munmap(buf
, page
) == 0);
209 ATF_TC(mlock_nested
);
210 ATF_TC_HEAD(mlock_nested
, tc
)
212 atf_tc_set_md_var(tc
, "descr",
213 "Test that consecutive mlock(2) calls succeed");
216 ATF_TC_BODY(mlock_nested
, tc
)
218 const size_t maxiter
= 100;
222 ATF_REQUIRE(buf
!= NULL
);
224 for (size_t i
= 0; i
< maxiter
; i
++)
225 ATF_REQUIRE(mlock(buf
, page
) == 0);
227 ATF_REQUIRE(munlock(buf
, page
) == 0);
234 page
= sysconf(_SC_PAGESIZE
);
235 ATF_REQUIRE(page
>= 0);
237 ATF_TP_ADD_TC(tp
, mlock_clip
);
238 ATF_TP_ADD_TC(tp
, mlock_err
);
239 ATF_TP_ADD_TC(tp
, mlock_limits
);
240 ATF_TP_ADD_TC(tp
, mlock_mmap
);
241 ATF_TP_ADD_TC(tp
, mlock_nested
);
243 return atf_no_error();