Home | History | Annotate | Line # | Download | only in sys
t_mlock.c revision 1.2
      1 /* $NetBSD: t_mlock.c,v 1.2 2012/04/21 01:15:13 jruoho 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 2012/04/21 01:15:13 jruoho Exp $");
     33 
     34 #include <sys/mman.h>
     35 #include <sys/resource.h>
     36 #include <sys/wait.h>
     37 
     38 #include <errno.h>
     39 #include <atf-c.h>
     40 #include <stdio.h>
     41 #include <stdlib.h>
     42 #include <unistd.h>
     43 
     44 static long page = 0;
     45 
     46 ATF_TC(mlock_clip);
     47 ATF_TC_HEAD(mlock_clip, tc)
     48 {
     49 	atf_tc_set_md_var(tc, "descr", "Test with mlock(2) that UVM only "
     50 	    "clips if the clip address is within the entry (PR kern/44788)");
     51 }
     52 
     53 ATF_TC_BODY(mlock_clip, tc)
     54 {
     55 	void *buf;
     56 
     57 	buf = malloc(page);
     58 	ATF_REQUIRE(buf != NULL);
     59 
     60 	if (page < 1024)
     61 		atf_tc_skip("page size too small");
     62 
     63 	for (size_t i = page; i >= 1; i = i - 1024) {
     64 		(void)mlock(buf, page - i);
     65 		(void)munlock(buf, page - i);
     66 	}
     67 
     68 	free(buf);
     69 }
     70 
     71 ATF_TC(mlock_err);
     72 ATF_TC_HEAD(mlock_err, tc)
     73 {
     74 	atf_tc_set_md_var(tc, "descr",
     75 	    "Test error conditions in mlock(2) and munlock(2)");
     76 }
     77 
     78 ATF_TC_BODY(mlock_err, tc)
     79 {
     80 
     81 	errno = 0;
     82 	ATF_REQUIRE_ERRNO(ENOMEM, mlock(NULL, page) == -1);
     83 
     84 	errno = 0;
     85 	ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)0, page) == -1);
     86 
     87 	errno = 0;
     88 	ATF_REQUIRE_ERRNO(EINVAL, mlock((char *)-1, page) == -1);
     89 
     90 	errno = 0;
     91 	ATF_REQUIRE_ERRNO(ENOMEM, munlock(NULL, page) == -1);
     92 
     93 	errno = 0;
     94 	ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)0, page) == -1);
     95 
     96 	errno = 0;
     97 	ATF_REQUIRE_ERRNO(EINVAL, munlock((char *)-1, page) == -1);
     98 }
     99 
    100 ATF_TC(mlock_limits);
    101 ATF_TC_HEAD(mlock_limits, tc)
    102 {
    103 	atf_tc_set_md_var(tc, "descr", "Test system limits with mlock(2)");
    104 }
    105 
    106 ATF_TC_BODY(mlock_limits, tc)
    107 {
    108 	struct rlimit res;
    109 	void *buf;
    110 	pid_t pid;
    111 	int sta;
    112 
    113 	buf = malloc(page);
    114 	ATF_REQUIRE(buf != NULL);
    115 
    116 	pid = fork();
    117 	ATF_REQUIRE(pid >= 0);
    118 
    119 	if (pid == 0) {
    120 
    121 		for (ssize_t i = page; i >= 2; i -= 100) {
    122 
    123 			res.rlim_cur = i - 1;
    124 			res.rlim_max = i - 1;
    125 
    126 			(void)fprintf(stderr, "trying to lock %zd bytes "
    127 			    "with %zu byte limit\n", i, (size_t)res.rlim_cur);
    128 
    129 			if (setrlimit(RLIMIT_MEMLOCK, &res) != 0)
    130 				_exit(EXIT_FAILURE);
    131 
    132 			errno = 0;
    133 
    134 			if (mlock(buf, i) != -1 || errno != EAGAIN) {
    135 				(void)munlock(buf, i);
    136 				_exit(EXIT_FAILURE);
    137 			}
    138 		}
    139 
    140 		_exit(EXIT_SUCCESS);
    141 	}
    142 
    143 	(void)wait(&sta);
    144 
    145 	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
    146 		atf_tc_fail("mlock(2) locked beyond system limits");
    147 
    148 	free(buf);
    149 }
    150 
    151 ATF_TC(mlock_mmap);
    152 ATF_TC_HEAD(mlock_mmap, tc)
    153 {
    154 	atf_tc_set_md_var(tc, "descr", "Test mlock(2)-mmap(2) interaction");
    155 }
    156 
    157 ATF_TC_BODY(mlock_mmap, tc)
    158 {
    159 	static const int flags = MAP_ANON | MAP_PRIVATE | MAP_WIRED;
    160 	void *buf;
    161 
    162 	/*
    163 	 * Make a wired RW mapping and check that mlock(2)
    164 	 * does not fail for the (already locked) mapping.
    165 	 */
    166 	buf = mmap(NULL, page, PROT_READ | PROT_WRITE, flags, -1, 0);
    167 
    168 	ATF_REQUIRE(buf != MAP_FAILED);
    169 	ATF_REQUIRE(mlock(buf, page) == 0);
    170 	ATF_REQUIRE(munlock(buf, page) == 0);
    171 	ATF_REQUIRE(munmap(buf, page) == 0);
    172 	ATF_REQUIRE(munlock(buf, page) != 0);
    173 
    174 	/*
    175 	 * But it should be impossible to mlock(2) a PROT_NONE mapping.
    176 	 */
    177 	buf = mmap(NULL, page, PROT_NONE, flags, -1, 0);
    178 
    179 	ATF_REQUIRE(buf != MAP_FAILED);
    180 	ATF_REQUIRE(mlock(buf, page) != 0);
    181 	ATF_REQUIRE(munmap(buf, page) == 0);
    182 }
    183 
    184 ATF_TC(mlock_nested);
    185 ATF_TC_HEAD(mlock_nested, tc)
    186 {
    187 	atf_tc_set_md_var(tc, "descr",
    188 	    "Test that consecutive mlock(2) calls succeed");
    189 }
    190 
    191 ATF_TC_BODY(mlock_nested, tc)
    192 {
    193 	const size_t maxiter = 100;
    194 	void *buf;
    195 
    196 	buf = malloc(page);
    197 	ATF_REQUIRE(buf != NULL);
    198 
    199 	for (size_t i = 0; i < maxiter; i++)
    200 		ATF_REQUIRE(mlock(buf, page) == 0);
    201 
    202 	ATF_REQUIRE(munlock(buf, page) == 0);
    203 	free(buf);
    204 }
    205 
    206 ATF_TP_ADD_TCS(tp)
    207 {
    208 
    209 	page = sysconf(_SC_PAGESIZE);
    210 	ATF_REQUIRE(page >= 0);
    211 
    212 	ATF_TP_ADD_TC(tp, mlock_clip);
    213 	ATF_TP_ADD_TC(tp, mlock_err);
    214 	ATF_TP_ADD_TC(tp, mlock_limits);
    215 	ATF_TP_ADD_TC(tp, mlock_mmap);
    216 	ATF_TP_ADD_TC(tp, mlock_nested);
    217 
    218 	return atf_no_error();
    219 }
    220