xref: /minix3/tests/lib/libc/sys/t_mmap.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_mmap.c,v 1.9 2015/02/28 13:57:08 martin Exp $ */
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc  * Copyright (c) 2011 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 
3211be35a1SLionel Sambuc /*-
3311be35a1SLionel Sambuc  * Copyright (c)2004 YAMAMOTO Takashi,
3411be35a1SLionel Sambuc  * All rights reserved.
3511be35a1SLionel Sambuc  *
3611be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
3711be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
3811be35a1SLionel Sambuc  * are met:
3911be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
4011be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
4111be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
4211be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
4311be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
4411be35a1SLionel Sambuc  *
4511be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
4611be35a1SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4711be35a1SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4811be35a1SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4911be35a1SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5011be35a1SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5111be35a1SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5211be35a1SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5311be35a1SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5411be35a1SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5511be35a1SLionel Sambuc  * SUCH DAMAGE.
5611be35a1SLionel Sambuc  */
5711be35a1SLionel Sambuc #include <sys/cdefs.h>
58*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_mmap.c,v 1.9 2015/02/28 13:57:08 martin Exp $");
5911be35a1SLionel Sambuc 
6011be35a1SLionel Sambuc #include <sys/param.h>
6111be35a1SLionel Sambuc #include <sys/mman.h>
6211be35a1SLionel Sambuc #include <sys/socket.h>
6311be35a1SLionel Sambuc #include <sys/sysctl.h>
6411be35a1SLionel Sambuc #include <sys/wait.h>
6511be35a1SLionel Sambuc 
6611be35a1SLionel Sambuc #include <atf-c.h>
6711be35a1SLionel Sambuc #include <errno.h>
6811be35a1SLionel Sambuc #include <fcntl.h>
6911be35a1SLionel Sambuc #include <signal.h>
7011be35a1SLionel Sambuc #include <stdio.h>
7111be35a1SLionel Sambuc #include <stdlib.h>
7211be35a1SLionel Sambuc #include <string.h>
7311be35a1SLionel Sambuc #include <unistd.h>
7411be35a1SLionel Sambuc #include <paths.h>
7511be35a1SLionel Sambuc #include <machine/disklabel.h>
7611be35a1SLionel Sambuc 
7711be35a1SLionel Sambuc static long	page = 0;
7811be35a1SLionel Sambuc static char	path[] = "mmap";
7911be35a1SLionel Sambuc static void	map_check(void *, int);
8011be35a1SLionel Sambuc static void	map_sighandler(int);
8111be35a1SLionel Sambuc static void	testloan(void *, void *, char, int);
8211be35a1SLionel Sambuc 
8311be35a1SLionel Sambuc #define	BUFSIZE	(32 * 1024)	/* enough size to trigger sosend_loan */
8411be35a1SLionel Sambuc 
8511be35a1SLionel Sambuc static void
map_check(void * map,int flag)8611be35a1SLionel Sambuc map_check(void *map, int flag)
8711be35a1SLionel Sambuc {
8811be35a1SLionel Sambuc 
8911be35a1SLionel Sambuc 	if (flag != 0) {
9011be35a1SLionel Sambuc 		ATF_REQUIRE(map == MAP_FAILED);
9111be35a1SLionel Sambuc 		return;
9211be35a1SLionel Sambuc 	}
9311be35a1SLionel Sambuc 
9411be35a1SLionel Sambuc 	ATF_REQUIRE(map != MAP_FAILED);
9511be35a1SLionel Sambuc 	ATF_REQUIRE(munmap(map, page) == 0);
9611be35a1SLionel Sambuc }
9711be35a1SLionel Sambuc 
9811be35a1SLionel Sambuc void
testloan(void * vp,void * vp2,char pat,int docheck)9911be35a1SLionel Sambuc testloan(void *vp, void *vp2, char pat, int docheck)
10011be35a1SLionel Sambuc {
10111be35a1SLionel Sambuc 	char buf[BUFSIZE];
10211be35a1SLionel Sambuc 	char backup[BUFSIZE];
10311be35a1SLionel Sambuc 	ssize_t nwritten;
10411be35a1SLionel Sambuc 	ssize_t nread;
10511be35a1SLionel Sambuc 	int fds[2];
10611be35a1SLionel Sambuc 	int val;
10711be35a1SLionel Sambuc 
10811be35a1SLionel Sambuc 	val = BUFSIZE;
10911be35a1SLionel Sambuc 
11011be35a1SLionel Sambuc 	if (docheck != 0)
11111be35a1SLionel Sambuc 		(void)memcpy(backup, vp, BUFSIZE);
11211be35a1SLionel Sambuc 
11311be35a1SLionel Sambuc 	if (socketpair(AF_LOCAL, SOCK_STREAM, PF_UNSPEC, fds) != 0)
11411be35a1SLionel Sambuc 		atf_tc_fail("socketpair() failed");
11511be35a1SLionel Sambuc 
11611be35a1SLionel Sambuc 	val = BUFSIZE;
11711be35a1SLionel Sambuc 
11811be35a1SLionel Sambuc 	if (setsockopt(fds[1], SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)) != 0)
11911be35a1SLionel Sambuc 		atf_tc_fail("setsockopt() failed, SO_RCVBUF");
12011be35a1SLionel Sambuc 
12111be35a1SLionel Sambuc 	val = BUFSIZE;
12211be35a1SLionel Sambuc 
12311be35a1SLionel Sambuc 	if (setsockopt(fds[0], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)) != 0)
12411be35a1SLionel Sambuc 		atf_tc_fail("setsockopt() failed, SO_SNDBUF");
12511be35a1SLionel Sambuc 
12611be35a1SLionel Sambuc 	if (fcntl(fds[0], F_SETFL, O_NONBLOCK) != 0)
12711be35a1SLionel Sambuc 		atf_tc_fail("fcntl() failed");
12811be35a1SLionel Sambuc 
12911be35a1SLionel Sambuc 	nwritten = write(fds[0], (char *)vp + page, BUFSIZE - page);
13011be35a1SLionel Sambuc 
13111be35a1SLionel Sambuc 	if (nwritten == -1)
13211be35a1SLionel Sambuc 		atf_tc_fail("write() failed");
13311be35a1SLionel Sambuc 
13411be35a1SLionel Sambuc 	/* Break loan. */
13511be35a1SLionel Sambuc 	(void)memset(vp2, pat, BUFSIZE);
13611be35a1SLionel Sambuc 
13711be35a1SLionel Sambuc 	nread = read(fds[1], buf + page, BUFSIZE - page);
13811be35a1SLionel Sambuc 
13911be35a1SLionel Sambuc 	if (nread == -1)
14011be35a1SLionel Sambuc 		atf_tc_fail("read() failed");
14111be35a1SLionel Sambuc 
14211be35a1SLionel Sambuc 	if (nread != nwritten)
14311be35a1SLionel Sambuc 		atf_tc_fail("too short read");
14411be35a1SLionel Sambuc 
14511be35a1SLionel Sambuc 	if (docheck != 0 && memcmp(backup, buf + page, nread) != 0)
14611be35a1SLionel Sambuc 		atf_tc_fail("data mismatch");
14711be35a1SLionel Sambuc 
14811be35a1SLionel Sambuc 	ATF_REQUIRE(close(fds[0]) == 0);
14911be35a1SLionel Sambuc 	ATF_REQUIRE(close(fds[1]) == 0);
15011be35a1SLionel Sambuc }
15111be35a1SLionel Sambuc 
15211be35a1SLionel Sambuc static void
map_sighandler(int signo)15311be35a1SLionel Sambuc map_sighandler(int signo)
15411be35a1SLionel Sambuc {
15511be35a1SLionel Sambuc 	_exit(signo);
15611be35a1SLionel Sambuc }
15711be35a1SLionel Sambuc 
15811be35a1SLionel Sambuc ATF_TC(mmap_block);
ATF_TC_HEAD(mmap_block,tc)15911be35a1SLionel Sambuc ATF_TC_HEAD(mmap_block, tc)
16011be35a1SLionel Sambuc {
16111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test mmap(2) with a block device");
16211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "require.user", "root");
16311be35a1SLionel Sambuc }
16411be35a1SLionel Sambuc 
ATF_TC_BODY(mmap_block,tc)16511be35a1SLionel Sambuc ATF_TC_BODY(mmap_block, tc)
16611be35a1SLionel Sambuc {
16711be35a1SLionel Sambuc 	static const int mib[] = { CTL_HW, HW_DISKNAMES };
16811be35a1SLionel Sambuc 	static const unsigned int miblen = __arraycount(mib);
16911be35a1SLionel Sambuc 	char *map, *dk, *drives, dev[PATH_MAX];
17011be35a1SLionel Sambuc 	size_t len;
17111be35a1SLionel Sambuc 	int fd = -1;
17211be35a1SLionel Sambuc 
17311be35a1SLionel Sambuc 	atf_tc_skip("The test case causes a panic (PR kern/38889, kern/46592)");
17411be35a1SLionel Sambuc 
17511be35a1SLionel Sambuc 	ATF_REQUIRE(sysctl(mib, miblen, NULL, &len, NULL, 0) == 0);
17611be35a1SLionel Sambuc 	drives = malloc(len);
17711be35a1SLionel Sambuc 	ATF_REQUIRE(drives != NULL);
17811be35a1SLionel Sambuc 	ATF_REQUIRE(sysctl(mib, miblen, drives, &len, NULL, 0) == 0);
17911be35a1SLionel Sambuc 	for (dk = strtok(drives, " "); dk != NULL; dk = strtok(NULL, " ")) {
18011be35a1SLionel Sambuc 		sprintf(dev, _PATH_DEV "%s%c", dk, 'a'+RAW_PART);
18111be35a1SLionel Sambuc 		fprintf(stderr, "trying: %s\n", dev);
18211be35a1SLionel Sambuc 
18311be35a1SLionel Sambuc 		if ((fd = open(dev, O_RDONLY)) >= 0) {
18411be35a1SLionel Sambuc 			(void)fprintf(stderr, "using %s\n", dev);
18511be35a1SLionel Sambuc 			break;
18611be35a1SLionel Sambuc 		}
18711be35a1SLionel Sambuc 	}
18811be35a1SLionel Sambuc 	free(drives);
18911be35a1SLionel Sambuc 
19011be35a1SLionel Sambuc 	if (fd < 0)
19111be35a1SLionel Sambuc 		atf_tc_skip("failed to find suitable block device");
19211be35a1SLionel Sambuc 
19311be35a1SLionel Sambuc 	map = mmap(NULL, 4096, PROT_READ, MAP_FILE, fd, 0);
19411be35a1SLionel Sambuc 	ATF_REQUIRE(map != MAP_FAILED);
19511be35a1SLionel Sambuc 
19611be35a1SLionel Sambuc 	(void)fprintf(stderr, "first byte %x\n", *map);
19711be35a1SLionel Sambuc 	ATF_REQUIRE(close(fd) == 0);
19811be35a1SLionel Sambuc 	(void)fprintf(stderr, "first byte %x\n", *map);
19911be35a1SLionel Sambuc 
20011be35a1SLionel Sambuc 	ATF_REQUIRE(munmap(map, 4096) == 0);
20111be35a1SLionel Sambuc }
20211be35a1SLionel Sambuc 
20311be35a1SLionel Sambuc ATF_TC(mmap_err);
ATF_TC_HEAD(mmap_err,tc)20411be35a1SLionel Sambuc ATF_TC_HEAD(mmap_err, tc)
20511be35a1SLionel Sambuc {
20611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test error conditions of mmap(2)");
20711be35a1SLionel Sambuc }
20811be35a1SLionel Sambuc 
ATF_TC_BODY(mmap_err,tc)20911be35a1SLionel Sambuc ATF_TC_BODY(mmap_err, tc)
21011be35a1SLionel Sambuc {
21111be35a1SLionel Sambuc 	size_t addr = SIZE_MAX;
21211be35a1SLionel Sambuc 	void *map;
21311be35a1SLionel Sambuc 
21411be35a1SLionel Sambuc 	errno = 0;
21511be35a1SLionel Sambuc 	map = mmap(NULL, 3, PROT_READ, MAP_FILE|MAP_PRIVATE, -1, 0);
21611be35a1SLionel Sambuc 
21711be35a1SLionel Sambuc 	ATF_REQUIRE(map == MAP_FAILED);
21811be35a1SLionel Sambuc 	ATF_REQUIRE(errno == EBADF);
21911be35a1SLionel Sambuc 
22011be35a1SLionel Sambuc 	errno = 0;
22111be35a1SLionel Sambuc 	map = mmap(&addr, page, PROT_READ, MAP_FIXED|MAP_PRIVATE, -1, 0);
22211be35a1SLionel Sambuc 
22311be35a1SLionel Sambuc 	ATF_REQUIRE(map == MAP_FAILED);
22411be35a1SLionel Sambuc 	ATF_REQUIRE(errno == EINVAL);
22511be35a1SLionel Sambuc 
22611be35a1SLionel Sambuc 	errno = 0;
22711be35a1SLionel Sambuc 	map = mmap(NULL, page, PROT_READ, MAP_ANON|MAP_PRIVATE, INT_MAX, 0);
22811be35a1SLionel Sambuc 
22911be35a1SLionel Sambuc 	ATF_REQUIRE(map == MAP_FAILED);
23011be35a1SLionel Sambuc 	ATF_REQUIRE(errno == EINVAL);
23111be35a1SLionel Sambuc }
23211be35a1SLionel Sambuc 
23311be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(mmap_loan);
ATF_TC_HEAD(mmap_loan,tc)23411be35a1SLionel Sambuc ATF_TC_HEAD(mmap_loan, tc)
23511be35a1SLionel Sambuc {
23611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test uvm page loanout with mmap(2)");
23711be35a1SLionel Sambuc }
23811be35a1SLionel Sambuc 
ATF_TC_BODY(mmap_loan,tc)23911be35a1SLionel Sambuc ATF_TC_BODY(mmap_loan, tc)
24011be35a1SLionel Sambuc {
24111be35a1SLionel Sambuc 	char buf[BUFSIZE];
24211be35a1SLionel Sambuc 	char *vp, *vp2;
24311be35a1SLionel Sambuc 	int fd;
24411be35a1SLionel Sambuc 
24511be35a1SLionel Sambuc 	fd = open(path, O_RDWR | O_CREAT, 0600);
24611be35a1SLionel Sambuc 	ATF_REQUIRE(fd >= 0);
24711be35a1SLionel Sambuc 
24811be35a1SLionel Sambuc 	(void)memset(buf, 'x', sizeof(buf));
24911be35a1SLionel Sambuc 	(void)write(fd, buf, sizeof(buf));
25011be35a1SLionel Sambuc 
25111be35a1SLionel Sambuc 	vp = mmap(NULL, BUFSIZE, PROT_READ | PROT_WRITE,
25211be35a1SLionel Sambuc 	    MAP_FILE | MAP_PRIVATE, fd, 0);
25311be35a1SLionel Sambuc 
25411be35a1SLionel Sambuc 	ATF_REQUIRE(vp != MAP_FAILED);
25511be35a1SLionel Sambuc 
25611be35a1SLionel Sambuc 	vp2 = vp;
25711be35a1SLionel Sambuc 
25811be35a1SLionel Sambuc 	testloan(vp, vp2, 'A', 0);
25911be35a1SLionel Sambuc 	testloan(vp, vp2, 'B', 1);
26011be35a1SLionel Sambuc 
26111be35a1SLionel Sambuc 	ATF_REQUIRE(munmap(vp, BUFSIZE) == 0);
26211be35a1SLionel Sambuc 
26311be35a1SLionel Sambuc 	vp = mmap(NULL, BUFSIZE, PROT_READ | PROT_WRITE,
26411be35a1SLionel Sambuc 	    MAP_FILE | MAP_SHARED, fd, 0);
26511be35a1SLionel Sambuc 
26611be35a1SLionel Sambuc 	vp2 = mmap(NULL, BUFSIZE, PROT_READ | PROT_WRITE,
26711be35a1SLionel Sambuc 	    MAP_FILE | MAP_SHARED, fd, 0);
26811be35a1SLionel Sambuc 
26911be35a1SLionel Sambuc 	ATF_REQUIRE(vp != MAP_FAILED);
27011be35a1SLionel Sambuc 	ATF_REQUIRE(vp2 != MAP_FAILED);
27111be35a1SLionel Sambuc 
27211be35a1SLionel Sambuc 	testloan(vp, vp2, 'E', 1);
27311be35a1SLionel Sambuc 
27411be35a1SLionel Sambuc 	ATF_REQUIRE(munmap(vp, BUFSIZE) == 0);
27511be35a1SLionel Sambuc 	ATF_REQUIRE(munmap(vp2, BUFSIZE) == 0);
27611be35a1SLionel Sambuc }
27711be35a1SLionel Sambuc 
ATF_TC_CLEANUP(mmap_loan,tc)27811be35a1SLionel Sambuc ATF_TC_CLEANUP(mmap_loan, tc)
27911be35a1SLionel Sambuc {
28011be35a1SLionel Sambuc 	(void)unlink(path);
28111be35a1SLionel Sambuc }
28211be35a1SLionel Sambuc 
28311be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(mmap_prot_1);
ATF_TC_HEAD(mmap_prot_1,tc)28411be35a1SLionel Sambuc ATF_TC_HEAD(mmap_prot_1, tc)
28511be35a1SLionel Sambuc {
28611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test mmap(2) protections, #1");
28711be35a1SLionel Sambuc }
28811be35a1SLionel Sambuc 
ATF_TC_BODY(mmap_prot_1,tc)28911be35a1SLionel Sambuc ATF_TC_BODY(mmap_prot_1, tc)
29011be35a1SLionel Sambuc {
29111be35a1SLionel Sambuc 	void *map;
29211be35a1SLionel Sambuc 	int fd;
29311be35a1SLionel Sambuc 
29411be35a1SLionel Sambuc 	/*
29511be35a1SLionel Sambuc 	 * Open a file write-only and try to
29611be35a1SLionel Sambuc 	 * map it read-only. This should fail.
29711be35a1SLionel Sambuc 	 */
29811be35a1SLionel Sambuc 	fd = open(path, O_WRONLY | O_CREAT, 0700);
29911be35a1SLionel Sambuc 
30011be35a1SLionel Sambuc 	if (fd < 0)
30111be35a1SLionel Sambuc 		return;
30211be35a1SLionel Sambuc 
30311be35a1SLionel Sambuc 	ATF_REQUIRE(write(fd, "XXX", 3) == 3);
30411be35a1SLionel Sambuc 
30511be35a1SLionel Sambuc 	map = mmap(NULL, 3, PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0);
30611be35a1SLionel Sambuc 	map_check(map, 1);
30711be35a1SLionel Sambuc 
30811be35a1SLionel Sambuc 	map = mmap(NULL, 3, PROT_WRITE, MAP_FILE|MAP_PRIVATE, fd, 0);
30911be35a1SLionel Sambuc 	map_check(map, 0);
31011be35a1SLionel Sambuc 
31111be35a1SLionel Sambuc 	ATF_REQUIRE(close(fd) == 0);
31211be35a1SLionel Sambuc }
31311be35a1SLionel Sambuc 
ATF_TC_CLEANUP(mmap_prot_1,tc)31411be35a1SLionel Sambuc ATF_TC_CLEANUP(mmap_prot_1, tc)
31511be35a1SLionel Sambuc {
31611be35a1SLionel Sambuc 	(void)unlink(path);
31711be35a1SLionel Sambuc }
31811be35a1SLionel Sambuc 
31911be35a1SLionel Sambuc ATF_TC(mmap_prot_2);
ATF_TC_HEAD(mmap_prot_2,tc)32011be35a1SLionel Sambuc ATF_TC_HEAD(mmap_prot_2, tc)
32111be35a1SLionel Sambuc {
32211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test mmap(2) protections, #2");
32311be35a1SLionel Sambuc }
32411be35a1SLionel Sambuc 
ATF_TC_BODY(mmap_prot_2,tc)32511be35a1SLionel Sambuc ATF_TC_BODY(mmap_prot_2, tc)
32611be35a1SLionel Sambuc {
32711be35a1SLionel Sambuc 	char buf[2];
32811be35a1SLionel Sambuc 	void *map;
32911be35a1SLionel Sambuc 	pid_t pid;
33011be35a1SLionel Sambuc 	int sta;
33111be35a1SLionel Sambuc 
33211be35a1SLionel Sambuc 	/*
33311be35a1SLionel Sambuc 	 * Make a PROT_NONE mapping and try to access it.
33411be35a1SLionel Sambuc 	 * If we catch a SIGSEGV, all works as expected.
33511be35a1SLionel Sambuc 	 */
33611be35a1SLionel Sambuc 	map = mmap(NULL, page, PROT_NONE, MAP_ANON|MAP_PRIVATE, -1, 0);
33711be35a1SLionel Sambuc 	ATF_REQUIRE(map != MAP_FAILED);
33811be35a1SLionel Sambuc 
33911be35a1SLionel Sambuc 	pid = fork();
34011be35a1SLionel Sambuc 	ATF_REQUIRE(pid >= 0);
34111be35a1SLionel Sambuc 
34211be35a1SLionel Sambuc 	if (pid == 0) {
34311be35a1SLionel Sambuc 		ATF_REQUIRE(signal(SIGSEGV, map_sighandler) != SIG_ERR);
34411be35a1SLionel Sambuc 		ATF_REQUIRE(strlcpy(buf, map, sizeof(buf)) != 0);
34511be35a1SLionel Sambuc 	}
34611be35a1SLionel Sambuc 
34711be35a1SLionel Sambuc 	(void)wait(&sta);
34811be35a1SLionel Sambuc 
34911be35a1SLionel Sambuc 	ATF_REQUIRE(WIFEXITED(sta) != 0);
35011be35a1SLionel Sambuc 	ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV);
35111be35a1SLionel Sambuc 	ATF_REQUIRE(munmap(map, page) == 0);
35211be35a1SLionel Sambuc }
35311be35a1SLionel Sambuc 
35411be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(mmap_prot_3);
ATF_TC_HEAD(mmap_prot_3,tc)35511be35a1SLionel Sambuc ATF_TC_HEAD(mmap_prot_3, tc)
35611be35a1SLionel Sambuc {
35711be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test mmap(2) protections, #3");
35811be35a1SLionel Sambuc }
35911be35a1SLionel Sambuc 
ATF_TC_BODY(mmap_prot_3,tc)36011be35a1SLionel Sambuc ATF_TC_BODY(mmap_prot_3, tc)
36111be35a1SLionel Sambuc {
36211be35a1SLionel Sambuc 	char buf[2];
36311be35a1SLionel Sambuc 	int fd, sta;
36411be35a1SLionel Sambuc 	void *map;
36511be35a1SLionel Sambuc 	pid_t pid;
36611be35a1SLionel Sambuc 
36711be35a1SLionel Sambuc 	/*
36811be35a1SLionel Sambuc 	 * Open a file, change the permissions
36911be35a1SLionel Sambuc 	 * to read-only, and try to map it as
37011be35a1SLionel Sambuc 	 * PROT_NONE. This should succeed, but
37111be35a1SLionel Sambuc 	 * the access should generate SIGSEGV.
37211be35a1SLionel Sambuc 	 */
37311be35a1SLionel Sambuc 	fd = open(path, O_RDWR | O_CREAT, 0700);
37411be35a1SLionel Sambuc 
37511be35a1SLionel Sambuc 	if (fd < 0)
37611be35a1SLionel Sambuc 		return;
37711be35a1SLionel Sambuc 
37811be35a1SLionel Sambuc 	ATF_REQUIRE(write(fd, "XXX", 3) == 3);
37911be35a1SLionel Sambuc 	ATF_REQUIRE(close(fd) == 0);
38011be35a1SLionel Sambuc 	ATF_REQUIRE(chmod(path, 0444) == 0);
38111be35a1SLionel Sambuc 
38211be35a1SLionel Sambuc 	fd = open(path, O_RDONLY);
38311be35a1SLionel Sambuc 	ATF_REQUIRE(fd != -1);
38411be35a1SLionel Sambuc 
38511be35a1SLionel Sambuc 	map = mmap(NULL, 3, PROT_NONE, MAP_FILE | MAP_SHARED, fd, 0);
38611be35a1SLionel Sambuc 	ATF_REQUIRE(map != MAP_FAILED);
38711be35a1SLionel Sambuc 
38811be35a1SLionel Sambuc 	pid = fork();
38911be35a1SLionel Sambuc 
39011be35a1SLionel Sambuc 	ATF_REQUIRE(pid >= 0);
39111be35a1SLionel Sambuc 
39211be35a1SLionel Sambuc 	if (pid == 0) {
39311be35a1SLionel Sambuc 		ATF_REQUIRE(signal(SIGSEGV, map_sighandler) != SIG_ERR);
39411be35a1SLionel Sambuc 		ATF_REQUIRE(strlcpy(buf, map, sizeof(buf)) != 0);
39511be35a1SLionel Sambuc 	}
39611be35a1SLionel Sambuc 
39711be35a1SLionel Sambuc 	(void)wait(&sta);
39811be35a1SLionel Sambuc 
39911be35a1SLionel Sambuc 	ATF_REQUIRE(WIFEXITED(sta) != 0);
40011be35a1SLionel Sambuc 	ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV);
40111be35a1SLionel Sambuc 	ATF_REQUIRE(munmap(map, 3) == 0);
40211be35a1SLionel Sambuc }
40311be35a1SLionel Sambuc 
ATF_TC_CLEANUP(mmap_prot_3,tc)40411be35a1SLionel Sambuc ATF_TC_CLEANUP(mmap_prot_3, tc)
40511be35a1SLionel Sambuc {
40611be35a1SLionel Sambuc 	(void)unlink(path);
40711be35a1SLionel Sambuc }
40811be35a1SLionel Sambuc 
40911be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(mmap_truncate);
ATF_TC_HEAD(mmap_truncate,tc)41011be35a1SLionel Sambuc ATF_TC_HEAD(mmap_truncate, tc)
41111be35a1SLionel Sambuc {
41211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test mmap(2) and ftruncate(2)");
41311be35a1SLionel Sambuc }
41411be35a1SLionel Sambuc 
ATF_TC_BODY(mmap_truncate,tc)41511be35a1SLionel Sambuc ATF_TC_BODY(mmap_truncate, tc)
41611be35a1SLionel Sambuc {
41711be35a1SLionel Sambuc 	char *map;
41811be35a1SLionel Sambuc 	long i;
41911be35a1SLionel Sambuc 	int fd;
42011be35a1SLionel Sambuc 
42111be35a1SLionel Sambuc 	fd = open(path, O_RDWR | O_CREAT, 0700);
42211be35a1SLionel Sambuc 
42311be35a1SLionel Sambuc 	if (fd < 0)
42411be35a1SLionel Sambuc 		return;
42511be35a1SLionel Sambuc 
42611be35a1SLionel Sambuc 	/*
42711be35a1SLionel Sambuc 	 * See that ftruncate(2) works
42811be35a1SLionel Sambuc 	 * while the file is mapped.
42911be35a1SLionel Sambuc 	 */
43011be35a1SLionel Sambuc 	ATF_REQUIRE(ftruncate(fd, page) == 0);
43111be35a1SLionel Sambuc 
43211be35a1SLionel Sambuc 	map = mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_FILE|MAP_PRIVATE,
43311be35a1SLionel Sambuc 	     fd, 0);
43411be35a1SLionel Sambuc 	ATF_REQUIRE(map != MAP_FAILED);
43511be35a1SLionel Sambuc 
43611be35a1SLionel Sambuc 	for (i = 0; i < page; i++)
43711be35a1SLionel Sambuc 		map[i] = 'x';
43811be35a1SLionel Sambuc 
43911be35a1SLionel Sambuc 	ATF_REQUIRE(ftruncate(fd, 0) == 0);
44011be35a1SLionel Sambuc 	ATF_REQUIRE(ftruncate(fd, page / 8) == 0);
44111be35a1SLionel Sambuc 	ATF_REQUIRE(ftruncate(fd, page / 4) == 0);
44211be35a1SLionel Sambuc 	ATF_REQUIRE(ftruncate(fd, page / 2) == 0);
44311be35a1SLionel Sambuc 	ATF_REQUIRE(ftruncate(fd, page / 12) == 0);
44411be35a1SLionel Sambuc 	ATF_REQUIRE(ftruncate(fd, page / 64) == 0);
44511be35a1SLionel Sambuc 
44611be35a1SLionel Sambuc 	ATF_REQUIRE(close(fd) == 0);
44711be35a1SLionel Sambuc }
44811be35a1SLionel Sambuc 
ATF_TC_CLEANUP(mmap_truncate,tc)44911be35a1SLionel Sambuc ATF_TC_CLEANUP(mmap_truncate, tc)
45011be35a1SLionel Sambuc {
45111be35a1SLionel Sambuc 	(void)unlink(path);
45211be35a1SLionel Sambuc }
45311be35a1SLionel Sambuc 
454*0a6a1f1dSLionel Sambuc ATF_TC_WITH_CLEANUP(mmap_truncate_signal);
ATF_TC_HEAD(mmap_truncate_signal,tc)455*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(mmap_truncate_signal, tc)
456*0a6a1f1dSLionel Sambuc {
457*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
458*0a6a1f1dSLionel Sambuc 	    "Test mmap(2) ftruncate(2) causing signal");
459*0a6a1f1dSLionel Sambuc }
460*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(mmap_truncate_signal,tc)461*0a6a1f1dSLionel Sambuc ATF_TC_BODY(mmap_truncate_signal, tc)
462*0a6a1f1dSLionel Sambuc {
463*0a6a1f1dSLionel Sambuc 	char *map;
464*0a6a1f1dSLionel Sambuc 	long i;
465*0a6a1f1dSLionel Sambuc 	int fd, sta;
466*0a6a1f1dSLionel Sambuc 	pid_t pid;
467*0a6a1f1dSLionel Sambuc 
468*0a6a1f1dSLionel Sambuc 	fd = open(path, O_RDWR | O_CREAT, 0700);
469*0a6a1f1dSLionel Sambuc 
470*0a6a1f1dSLionel Sambuc 	if (fd < 0)
471*0a6a1f1dSLionel Sambuc 		return;
472*0a6a1f1dSLionel Sambuc 
473*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(write(fd, "foo\n", 5) == 5);
474*0a6a1f1dSLionel Sambuc 
475*0a6a1f1dSLionel Sambuc 	map = mmap(NULL, page, PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0);
476*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(map != MAP_FAILED);
477*0a6a1f1dSLionel Sambuc 
478*0a6a1f1dSLionel Sambuc 	sta = 0;
479*0a6a1f1dSLionel Sambuc 	for (i = 0; i < 5; i++)
480*0a6a1f1dSLionel Sambuc 		sta += map[i];
481*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(sta == 334);
482*0a6a1f1dSLionel Sambuc 
483*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(ftruncate(fd, 0) == 0);
484*0a6a1f1dSLionel Sambuc 	pid = fork();
485*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(pid >= 0);
486*0a6a1f1dSLionel Sambuc 
487*0a6a1f1dSLionel Sambuc 	if (pid == 0) {
488*0a6a1f1dSLionel Sambuc 		ATF_REQUIRE(signal(SIGBUS, map_sighandler) != SIG_ERR);
489*0a6a1f1dSLionel Sambuc 		ATF_REQUIRE(signal(SIGSEGV, map_sighandler) != SIG_ERR);
490*0a6a1f1dSLionel Sambuc 		sta = 0;
491*0a6a1f1dSLionel Sambuc 		for (i = 0; i < page; i++)
492*0a6a1f1dSLionel Sambuc 			sta += map[i];
493*0a6a1f1dSLionel Sambuc 		/* child never will get this far, but the compiler will
494*0a6a1f1dSLionel Sambuc 		   not know, so better use the values calculated to
495*0a6a1f1dSLionel Sambuc 		   prevent the access to be optimized out */
496*0a6a1f1dSLionel Sambuc 		ATF_REQUIRE(i == 0);
497*0a6a1f1dSLionel Sambuc 		ATF_REQUIRE(sta == 0);
498*0a6a1f1dSLionel Sambuc 		return;
499*0a6a1f1dSLionel Sambuc 	}
500*0a6a1f1dSLionel Sambuc 
501*0a6a1f1dSLionel Sambuc 	(void)wait(&sta);
502*0a6a1f1dSLionel Sambuc 
503*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(WIFEXITED(sta) != 0);
504*0a6a1f1dSLionel Sambuc 	if (WEXITSTATUS(sta) == SIGSEGV)
505*0a6a1f1dSLionel Sambuc 		atf_tc_fail("child process got SIGSEGV instead of SIGBUS");
506*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(WEXITSTATUS(sta) == SIGBUS);
507*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(munmap(map, page) == 0);
508*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(close(fd) == 0);
509*0a6a1f1dSLionel Sambuc }
510*0a6a1f1dSLionel Sambuc 
ATF_TC_CLEANUP(mmap_truncate_signal,tc)511*0a6a1f1dSLionel Sambuc ATF_TC_CLEANUP(mmap_truncate_signal, tc)
512*0a6a1f1dSLionel Sambuc {
513*0a6a1f1dSLionel Sambuc 	(void)unlink(path);
514*0a6a1f1dSLionel Sambuc }
515*0a6a1f1dSLionel Sambuc 
51611be35a1SLionel Sambuc ATF_TC(mmap_va0);
ATF_TC_HEAD(mmap_va0,tc)51711be35a1SLionel Sambuc ATF_TC_HEAD(mmap_va0, tc)
51811be35a1SLionel Sambuc {
51911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test mmap(2) and vm.user_va0_disable");
52011be35a1SLionel Sambuc }
52111be35a1SLionel Sambuc 
ATF_TC_BODY(mmap_va0,tc)52211be35a1SLionel Sambuc ATF_TC_BODY(mmap_va0, tc)
52311be35a1SLionel Sambuc {
52411be35a1SLionel Sambuc 	int flags = MAP_ANON | MAP_FIXED | MAP_PRIVATE;
52511be35a1SLionel Sambuc 	size_t len = sizeof(int);
52611be35a1SLionel Sambuc 	void *map;
52711be35a1SLionel Sambuc 	int val;
52811be35a1SLionel Sambuc 
52911be35a1SLionel Sambuc 	/*
53011be35a1SLionel Sambuc 	 * Make an anonymous fixed mapping at zero address. If the address
53111be35a1SLionel Sambuc 	 * is restricted as noted in security(7), the syscall should fail.
53211be35a1SLionel Sambuc 	 */
53311be35a1SLionel Sambuc 	if (sysctlbyname("vm.user_va0_disable", &val, &len, NULL, 0) != 0)
53411be35a1SLionel Sambuc 		atf_tc_fail("failed to read vm.user_va0_disable");
53511be35a1SLionel Sambuc 
53611be35a1SLionel Sambuc 	map = mmap(NULL, page, PROT_EXEC, flags, -1, 0);
53711be35a1SLionel Sambuc 	map_check(map, val);
53811be35a1SLionel Sambuc 
53911be35a1SLionel Sambuc 	map = mmap(NULL, page, PROT_READ, flags, -1, 0);
54011be35a1SLionel Sambuc 	map_check(map, val);
54111be35a1SLionel Sambuc 
54211be35a1SLionel Sambuc 	map = mmap(NULL, page, PROT_WRITE, flags, -1, 0);
54311be35a1SLionel Sambuc 	map_check(map, val);
54411be35a1SLionel Sambuc 
54511be35a1SLionel Sambuc 	map = mmap(NULL, page, PROT_READ|PROT_WRITE, flags, -1, 0);
54611be35a1SLionel Sambuc 	map_check(map, val);
54711be35a1SLionel Sambuc 
54811be35a1SLionel Sambuc 	map = mmap(NULL, page, PROT_EXEC|PROT_READ|PROT_WRITE, flags, -1, 0);
54911be35a1SLionel Sambuc 	map_check(map, val);
55011be35a1SLionel Sambuc }
55111be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)55211be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
55311be35a1SLionel Sambuc {
55411be35a1SLionel Sambuc 	page = sysconf(_SC_PAGESIZE);
55511be35a1SLionel Sambuc 	ATF_REQUIRE(page >= 0);
55611be35a1SLionel Sambuc 
55711be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, mmap_block);
55811be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, mmap_err);
55911be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, mmap_loan);
56011be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, mmap_prot_1);
56111be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, mmap_prot_2);
56211be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, mmap_prot_3);
56311be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, mmap_truncate);
564*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, mmap_truncate_signal);
56511be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, mmap_va0);
56611be35a1SLionel Sambuc 
56711be35a1SLionel Sambuc 	return atf_no_error();
56811be35a1SLionel Sambuc }
569