xref: /minix3/tests/lib/libc/sys/t_mlock.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_mlock.c,v 1.5 2014/02/26 20:49:26 martin Exp $ */
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc  * Copyright (c) 2012 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc  * by Jukka Ruohonen.
911be35a1SLionel Sambuc  *
1011be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc  * are met:
1311be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc  *
1911be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc  */
3111be35a1SLionel Sambuc #include <sys/cdefs.h>
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_mlock.c,v 1.5 2014/02/26 20:49:26 martin Exp $");
3311be35a1SLionel Sambuc 
3411be35a1SLionel Sambuc #include <sys/mman.h>
3511be35a1SLionel Sambuc #include <sys/resource.h>
36*0a6a1f1dSLionel Sambuc #include <sys/sysctl.h>
3711be35a1SLionel Sambuc #include <sys/wait.h>
3811be35a1SLionel Sambuc 
3911be35a1SLionel Sambuc #include <errno.h>
4011be35a1SLionel Sambuc #include <atf-c.h>
4111be35a1SLionel Sambuc #include <stdint.h>
4211be35a1SLionel Sambuc #include <stdio.h>
4311be35a1SLionel Sambuc #include <stdlib.h>
4411be35a1SLionel Sambuc #include <unistd.h>
4511be35a1SLionel Sambuc 
4611be35a1SLionel Sambuc static long page = 0;
4711be35a1SLionel Sambuc 
4811be35a1SLionel Sambuc ATF_TC(mlock_clip);
ATF_TC_HEAD(mlock_clip,tc)4911be35a1SLionel Sambuc ATF_TC_HEAD(mlock_clip, tc)
5011be35a1SLionel Sambuc {
5111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test with mlock(2) that UVM only "
5211be35a1SLionel Sambuc 	    "clips if the clip address is within the entry (PR kern/44788)");
5311be35a1SLionel Sambuc }
5411be35a1SLionel Sambuc 
ATF_TC_BODY(mlock_clip,tc)5511be35a1SLionel Sambuc ATF_TC_BODY(mlock_clip, tc)
5611be35a1SLionel Sambuc {
5711be35a1SLionel Sambuc 	void *buf;
5811be35a1SLionel Sambuc 
5911be35a1SLionel Sambuc 	buf = malloc(page);
6011be35a1SLionel Sambuc 	ATF_REQUIRE(buf != NULL);
6111be35a1SLionel Sambuc 
6211be35a1SLionel Sambuc 	if (page < 1024)
6311be35a1SLionel Sambuc 		atf_tc_skip("page size too small");
6411be35a1SLionel Sambuc 
6511be35a1SLionel Sambuc 	for (size_t i = page; i >= 1; i = i - 1024) {
6611be35a1SLionel Sambuc 		(void)mlock(buf, page - i);
6711be35a1SLionel Sambuc 		(void)munlock(buf, page - i);
6811be35a1SLionel Sambuc 	}
6911be35a1SLionel Sambuc 
7011be35a1SLionel Sambuc 	free(buf);
7111be35a1SLionel Sambuc }
7211be35a1SLionel Sambuc 
7311be35a1SLionel Sambuc ATF_TC(mlock_err);
ATF_TC_HEAD(mlock_err,tc)7411be35a1SLionel Sambuc ATF_TC_HEAD(mlock_err, tc)
7511be35a1SLionel Sambuc {
7611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
7711be35a1SLionel Sambuc 	    "Test error conditions in mlock(2) and munlock(2)");
7811be35a1SLionel Sambuc }
7911be35a1SLionel Sambuc 
ATF_TC_BODY(mlock_err,tc)8011be35a1SLionel Sambuc ATF_TC_BODY(mlock_err, tc)
8111be35a1SLionel Sambuc {
82*0a6a1f1dSLionel Sambuc 	unsigned long vmin = 0;
83*0a6a1f1dSLionel Sambuc 	size_t len = sizeof(vmin);
8411be35a1SLionel Sambuc 	void *invalid_ptr;
8511be35a1SLionel Sambuc 	int null_errno = ENOMEM;	/* error expected for NULL */
8611be35a1SLionel Sambuc 
87*0a6a1f1dSLionel Sambuc 	if (sysctlbyname("vm.minaddress", &vmin, &len, NULL, 0) != 0)
88*0a6a1f1dSLionel Sambuc 		atf_tc_fail("failed to read vm.minaddress");
89*0a6a1f1dSLionel Sambuc 
90*0a6a1f1dSLionel Sambuc 	if (vmin > 0)
9111be35a1SLionel Sambuc 		null_errno = EINVAL;	/* NULL is not inside user VM */
9211be35a1SLionel Sambuc 
9311be35a1SLionel Sambuc 	errno = 0;
9411be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(null_errno, mlock(NULL, page) == -1);
9511be35a1SLionel Sambuc 
9611be35a1SLionel Sambuc 	errno = 0;
9711be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(null_errno, mlock((char *)0, page) == -1);
9811be35a1SLionel Sambuc 
9911be35a1SLionel Sambuc 	errno = 0;
10011be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EINVAL, mlock((char *)-1, page) == -1);
10111be35a1SLionel Sambuc 
10211be35a1SLionel Sambuc 	errno = 0;
10311be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(null_errno, munlock(NULL, page) == -1);
10411be35a1SLionel Sambuc 
10511be35a1SLionel Sambuc 	errno = 0;
10611be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(null_errno, munlock((char *)0, page) == -1);
10711be35a1SLionel Sambuc 
10811be35a1SLionel Sambuc 	errno = 0;
10911be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EINVAL, munlock((char *)-1, page) == -1);
11011be35a1SLionel Sambuc 
11111be35a1SLionel Sambuc 	/*
11211be35a1SLionel Sambuc 	 * Try to create a pointer to an unmapped page - first after current
11311be35a1SLionel Sambuc 	 * brk will likely do.
11411be35a1SLionel Sambuc 	 */
11511be35a1SLionel Sambuc 	invalid_ptr = (void*)(((uintptr_t)sbrk(0)+page) & ~(page-1));
11611be35a1SLionel Sambuc 	printf("testing with (hopefully) invalid pointer %p\n", invalid_ptr);
11711be35a1SLionel Sambuc 
11811be35a1SLionel Sambuc 	errno = 0;
11911be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(ENOMEM, mlock(invalid_ptr, page) == -1);
12011be35a1SLionel Sambuc 
12111be35a1SLionel Sambuc 	errno = 0;
12211be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(ENOMEM, munlock(invalid_ptr, page) == -1);
12311be35a1SLionel Sambuc }
12411be35a1SLionel Sambuc 
12511be35a1SLionel Sambuc ATF_TC(mlock_limits);
ATF_TC_HEAD(mlock_limits,tc)12611be35a1SLionel Sambuc ATF_TC_HEAD(mlock_limits, tc)
12711be35a1SLionel Sambuc {
12811be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test system limits with mlock(2)");
12911be35a1SLionel Sambuc }
13011be35a1SLionel Sambuc 
ATF_TC_BODY(mlock_limits,tc)13111be35a1SLionel Sambuc ATF_TC_BODY(mlock_limits, tc)
13211be35a1SLionel Sambuc {
13311be35a1SLionel Sambuc 	struct rlimit res;
13411be35a1SLionel Sambuc 	void *buf;
13511be35a1SLionel Sambuc 	pid_t pid;
13611be35a1SLionel Sambuc 	int sta;
13711be35a1SLionel Sambuc 
13811be35a1SLionel Sambuc 	buf = malloc(page);
13911be35a1SLionel Sambuc 	ATF_REQUIRE(buf != NULL);
14011be35a1SLionel Sambuc 
14111be35a1SLionel Sambuc 	pid = fork();
14211be35a1SLionel Sambuc 	ATF_REQUIRE(pid >= 0);
14311be35a1SLionel Sambuc 
14411be35a1SLionel Sambuc 	if (pid == 0) {
14511be35a1SLionel Sambuc 
14611be35a1SLionel Sambuc 		for (ssize_t i = page; i >= 2; i -= 100) {
14711be35a1SLionel Sambuc 
14811be35a1SLionel Sambuc 			res.rlim_cur = i - 1;
14911be35a1SLionel Sambuc 			res.rlim_max = i - 1;
15011be35a1SLionel Sambuc 
15111be35a1SLionel Sambuc 			(void)fprintf(stderr, "trying to lock %zd bytes "
15211be35a1SLionel Sambuc 			    "with %zu byte limit\n", i, (size_t)res.rlim_cur);
15311be35a1SLionel Sambuc 
15411be35a1SLionel Sambuc 			if (setrlimit(RLIMIT_MEMLOCK, &res) != 0)
15511be35a1SLionel Sambuc 				_exit(EXIT_FAILURE);
15611be35a1SLionel Sambuc 
15711be35a1SLionel Sambuc 			errno = 0;
15811be35a1SLionel Sambuc 
15911be35a1SLionel Sambuc 			if (mlock(buf, i) != -1 || errno != EAGAIN) {
16011be35a1SLionel Sambuc 				(void)munlock(buf, i);
16111be35a1SLionel Sambuc 				_exit(EXIT_FAILURE);
16211be35a1SLionel Sambuc 			}
16311be35a1SLionel Sambuc 		}
16411be35a1SLionel Sambuc 
16511be35a1SLionel Sambuc 		_exit(EXIT_SUCCESS);
16611be35a1SLionel Sambuc 	}
16711be35a1SLionel Sambuc 
16811be35a1SLionel Sambuc 	(void)wait(&sta);
16911be35a1SLionel Sambuc 
17011be35a1SLionel Sambuc 	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
17111be35a1SLionel Sambuc 		atf_tc_fail("mlock(2) locked beyond system limits");
17211be35a1SLionel Sambuc 
17311be35a1SLionel Sambuc 	free(buf);
17411be35a1SLionel Sambuc }
17511be35a1SLionel Sambuc 
17611be35a1SLionel Sambuc ATF_TC(mlock_mmap);
ATF_TC_HEAD(mlock_mmap,tc)17711be35a1SLionel Sambuc ATF_TC_HEAD(mlock_mmap, tc)
17811be35a1SLionel Sambuc {
17911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test mlock(2)-mmap(2) interaction");
18011be35a1SLionel Sambuc }
18111be35a1SLionel Sambuc 
ATF_TC_BODY(mlock_mmap,tc)18211be35a1SLionel Sambuc ATF_TC_BODY(mlock_mmap, tc)
18311be35a1SLionel Sambuc {
18411be35a1SLionel Sambuc 	static const int flags = MAP_ANON | MAP_PRIVATE | MAP_WIRED;
18511be35a1SLionel Sambuc 	void *buf;
18611be35a1SLionel Sambuc 
18711be35a1SLionel Sambuc 	/*
18811be35a1SLionel Sambuc 	 * Make a wired RW mapping and check that mlock(2)
18911be35a1SLionel Sambuc 	 * does not fail for the (already locked) mapping.
19011be35a1SLionel Sambuc 	 */
19111be35a1SLionel Sambuc 	buf = mmap(NULL, page, PROT_READ | PROT_WRITE, flags, -1, 0);
19211be35a1SLionel Sambuc 
19311be35a1SLionel Sambuc 	ATF_REQUIRE(buf != MAP_FAILED);
19411be35a1SLionel Sambuc 	ATF_REQUIRE(mlock(buf, page) == 0);
19511be35a1SLionel Sambuc 	ATF_REQUIRE(munlock(buf, page) == 0);
19611be35a1SLionel Sambuc 	ATF_REQUIRE(munmap(buf, page) == 0);
19711be35a1SLionel Sambuc 	ATF_REQUIRE(munlock(buf, page) != 0);
19811be35a1SLionel Sambuc 
19911be35a1SLionel Sambuc 	/*
20011be35a1SLionel Sambuc 	 * But it should be impossible to mlock(2) a PROT_NONE mapping.
20111be35a1SLionel Sambuc 	 */
20211be35a1SLionel Sambuc 	buf = mmap(NULL, page, PROT_NONE, flags, -1, 0);
20311be35a1SLionel Sambuc 
20411be35a1SLionel Sambuc 	ATF_REQUIRE(buf != MAP_FAILED);
20511be35a1SLionel Sambuc 	ATF_REQUIRE(mlock(buf, page) != 0);
20611be35a1SLionel Sambuc 	ATF_REQUIRE(munmap(buf, page) == 0);
20711be35a1SLionel Sambuc }
20811be35a1SLionel Sambuc 
20911be35a1SLionel Sambuc ATF_TC(mlock_nested);
ATF_TC_HEAD(mlock_nested,tc)21011be35a1SLionel Sambuc ATF_TC_HEAD(mlock_nested, tc)
21111be35a1SLionel Sambuc {
21211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
21311be35a1SLionel Sambuc 	    "Test that consecutive mlock(2) calls succeed");
21411be35a1SLionel Sambuc }
21511be35a1SLionel Sambuc 
ATF_TC_BODY(mlock_nested,tc)21611be35a1SLionel Sambuc ATF_TC_BODY(mlock_nested, tc)
21711be35a1SLionel Sambuc {
21811be35a1SLionel Sambuc 	const size_t maxiter = 100;
21911be35a1SLionel Sambuc 	void *buf;
22011be35a1SLionel Sambuc 
22111be35a1SLionel Sambuc 	buf = malloc(page);
22211be35a1SLionel Sambuc 	ATF_REQUIRE(buf != NULL);
22311be35a1SLionel Sambuc 
22411be35a1SLionel Sambuc 	for (size_t i = 0; i < maxiter; i++)
22511be35a1SLionel Sambuc 		ATF_REQUIRE(mlock(buf, page) == 0);
22611be35a1SLionel Sambuc 
22711be35a1SLionel Sambuc 	ATF_REQUIRE(munlock(buf, page) == 0);
22811be35a1SLionel Sambuc 	free(buf);
22911be35a1SLionel Sambuc }
23011be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)23111be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
23211be35a1SLionel Sambuc {
23311be35a1SLionel Sambuc 
23411be35a1SLionel Sambuc 	page = sysconf(_SC_PAGESIZE);
23511be35a1SLionel Sambuc 	ATF_REQUIRE(page >= 0);
23611be35a1SLionel Sambuc 
23711be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, mlock_clip);
23811be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, mlock_err);
23911be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, mlock_limits);
24011be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, mlock_mmap);
24111be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, mlock_nested);
24211be35a1SLionel Sambuc 
24311be35a1SLionel Sambuc 	return atf_no_error();
24411be35a1SLionel Sambuc }
245