Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Add some basic tests for mmap(2), including one for the vm.u...



details:   https://anonhg.NetBSD.org/src/rev/fd58981ae70e
branches:  trunk
changeset: 763710:fd58981ae70e
user:      jruoho <jruoho%NetBSD.org@localhost>
date:      Thu Mar 31 13:07:20 2011 +0000

description:
Add some basic tests for mmap(2), including one for the vm.user_va0_disable.

diffstat:

 distrib/sets/lists/tests/mi |    4 +-
 tests/syscall/Makefile      |    4 +-
 tests/syscall/t_mmap.c      |  321 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 326 insertions(+), 3 deletions(-)

diffs (truncated from 366 to 300 lines):

diff -r da917011281f -r fd58981ae70e distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Thu Mar 31 12:56:03 2011 +0000
+++ b/distrib/sets/lists/tests/mi       Thu Mar 31 13:07:20 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.281 2011/03/30 17:02:17 jruoho Exp $
+# $NetBSD: mi,v 1.282 2011/03/31 13:07:20 jruoho Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -563,6 +563,7 @@
 ./usr/libdata/debug/usr/tests/syscall                                  tests-syscall-debug
 ./usr/libdata/debug/usr/tests/syscall/t_cmsg.debug                     tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_fsync.debug                    tests-syscall-debug     debug,atf
+./usr/libdata/debug/usr/tests/syscall/t_mmap.debug                     tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_timer.debug                    tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/usr.bin                                  tests-sbin-tests
 ./usr/libdata/debug/usr/tests/usr.sbin                                 tests-sbin-tests
@@ -2096,6 +2097,7 @@
 ./usr/tests/syscall/Atffile                    tests-syscall-tests     atf
 ./usr/tests/syscall/t_cmsg                     tests-syscall-tests     atf
 ./usr/tests/syscall/t_fsync                    tests-syscall-tests     atf
+./usr/tests/syscall/t_mmap                     tests-syscall-tests     atf
 ./usr/tests/syscall/t_timer                    tests-syscall-tests     atf
 ./usr/tests/toolchain  tests-syscall-tests     atf
 ./usr/tests/toolchain/Atffile  tests-syscall-tests     atf
diff -r da917011281f -r fd58981ae70e tests/syscall/Makefile
--- a/tests/syscall/Makefile    Thu Mar 31 12:56:03 2011 +0000
+++ b/tests/syscall/Makefile    Thu Mar 31 13:07:20 2011 +0000
@@ -1,10 +1,10 @@
-# $NetBSD: Makefile,v 1.10 2011/03/30 09:43:21 jruoho Exp $
+# $NetBSD: Makefile,v 1.11 2011/03/31 13:07:20 jruoho Exp $
 
 .include <bsd.own.mk>
 
 TESTSDIR=      ${TESTSBASE}/syscall
 
-TESTS_C+=      t_cmsg t_fsync t_timer
+TESTS_C+=      t_cmsg t_fsync t_mmap t_timer
 
 LDADD.t_cmsg+= -lrumpnet_local -lrumpnet_net -lrumpnet
 LDADD.t_cmsg+= -lrumpvfs -lrump -lrumpuser -lpthread
diff -r da917011281f -r fd58981ae70e tests/syscall/t_mmap.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/syscall/t_mmap.c    Thu Mar 31 13:07:20 2011 +0000
@@ -0,0 +1,321 @@
+/* $NetBSD: t_mmap.c,v 1.1 2011/03/31 13:07:20 jruoho Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jukka Ruohonen.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_mmap.c,v 1.1 2011/03/31 13:07:20 jruoho Exp $");
+
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+static long    page = 0;
+static char    path[] = "/tmp/mmap";
+static void    map_check(void *, int);
+static void    map_sighandler(int);
+
+static void
+map_check(void *map, int flag)
+{
+
+       if (flag != 0) {
+               ATF_REQUIRE(map == MAP_FAILED);
+               return;
+       }
+
+       ATF_REQUIRE(map != MAP_FAILED);
+       ATF_REQUIRE(munmap(map, page) == 0);
+}
+
+static void
+map_sighandler(int signo)
+{
+       exit(signo);
+}
+
+ATF_TC(mmap_err);
+ATF_TC_HEAD(mmap_err, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test error conditions of mmap(2)");
+}
+
+ATF_TC_BODY(mmap_err, tc)
+{
+       size_t addr = SIZE_MAX;
+       void *map;
+
+       errno = 0;
+       map = mmap(NULL, 3, PROT_READ, MAP_FILE, -1, 0);
+
+       ATF_REQUIRE(map == MAP_FAILED);
+       ATF_REQUIRE(errno == EBADF);
+
+       errno = 0;
+       map = mmap(&addr, page, PROT_READ, MAP_FIXED, -1, 0);
+
+       ATF_REQUIRE(map == MAP_FAILED);
+       ATF_REQUIRE(errno == EINVAL);
+
+       errno = 0;
+       map = mmap(NULL, page, PROT_READ, MAP_ANON, INT_MAX, 0);
+
+       ATF_REQUIRE(map == MAP_FAILED);
+       ATF_REQUIRE(errno == EINVAL);
+}
+
+ATF_TC_WITH_CLEANUP(mmap_prot_1);
+ATF_TC_HEAD(mmap_prot_1, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test mmap(2) protections, #1");
+}
+
+ATF_TC_BODY(mmap_prot_1, tc)
+{
+       void *map;
+       int fd;
+
+       /*
+        * Open a file write-only and try to
+        * map it read-only. This should fail.
+        */
+       fd = open(path, O_WRONLY | O_CREAT, 0700);
+
+       if (fd < 0)
+               return;
+
+       ATF_REQUIRE(write(fd, "XXX", 3) == 3);
+
+       map = mmap(NULL, 3, PROT_READ, MAP_FILE, fd, 0);
+       map_check(map, 1);
+
+       map = mmap(NULL, 3, PROT_WRITE, MAP_FILE, fd, 0);
+       map_check(map, 0);
+
+       ATF_REQUIRE(close(fd) == 0);
+}
+
+ATF_TC_CLEANUP(mmap_prot_1, tc)
+{
+       (void)unlink(path);
+}
+
+ATF_TC(mmap_prot_2);
+ATF_TC_HEAD(mmap_prot_2, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test mmap(2) protections, #2");
+}
+
+ATF_TC_BODY(mmap_prot_2, tc)
+{
+       char buf[2];
+       void *map;
+       pid_t pid;
+       int sta;
+
+       /*
+        * Make a PROT_NONE mapping and try to access it.
+        * If we catch a SIGSEGV, all works as expected.
+        */
+       map = mmap(NULL, page, PROT_NONE, MAP_ANON, -1, 0);
+       ATF_REQUIRE(map != MAP_FAILED);
+
+       pid = fork();
+       ATF_REQUIRE(pid >= 0);
+
+       if (pid == 0) {
+               ATF_REQUIRE(signal(SIGSEGV, map_sighandler) != SIG_ERR);
+               ATF_REQUIRE(strlcpy(buf, map, sizeof(buf)) != 0);
+       }
+
+       (void)wait(&sta);
+
+       ATF_REQUIRE(WIFEXITED(sta) != 0);
+       ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV);
+       ATF_REQUIRE(munmap(map, page) == 0);
+}
+
+ATF_TC_WITH_CLEANUP(mmap_prot_3);
+ATF_TC_HEAD(mmap_prot_3, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test mmap(2) protections, #3");
+}
+
+ATF_TC_BODY(mmap_prot_3, tc)
+{
+       char buf[2];
+       int fd, sta;
+       void *map;
+       pid_t pid;
+
+       /*
+        * Open a file, change the permissions
+        * to read-only, and try to map it as
+        * PROT_NONE. This should succeed, but
+        * the access should generate SIGSEGV.
+        */
+       fd = open(path, O_RDWR | O_CREAT, 0700);
+
+       if (fd < 0)
+               return;
+
+       ATF_REQUIRE(write(fd, "XXX", 3) == 3);
+       ATF_REQUIRE(close(fd) == 0);
+       ATF_REQUIRE(chmod(path, 0444) == 0);
+
+       fd = open(path, O_RDONLY);
+       ATF_REQUIRE(fd != -1);
+
+       map = mmap(NULL, 3, PROT_NONE, MAP_FILE | MAP_SHARED, fd, 0);
+       ATF_REQUIRE(map != MAP_FAILED);
+
+       pid = fork();
+
+       ATF_REQUIRE(pid >= 0);
+
+       if (pid == 0) {
+               ATF_REQUIRE(signal(SIGSEGV, map_sighandler) != SIG_ERR);
+               ATF_REQUIRE(strlcpy(buf, map, sizeof(buf)) != 0);
+       }
+
+       (void)wait(&sta);
+
+       ATF_REQUIRE(WIFEXITED(sta) != 0);
+       ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV);
+       ATF_REQUIRE(munmap(map, 3) == 0);
+}
+
+ATF_TC_CLEANUP(mmap_prot_3, tc)
+{
+       (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(mmap_truncate);
+ATF_TC_HEAD(mmap_truncate, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test mmap(2) and ftruncate(2)");
+}
+
+ATF_TC_BODY(mmap_truncate, tc)
+{
+       char *map;
+       long i;
+       int fd;
+
+       fd = open(path, O_RDWR | O_CREAT, 0700);
+
+       if (fd < 0)
+               return;
+
+       /*
+        * See that ftruncate(2) works
+        * while the file is mapped.
+        */
+       ATF_REQUIRE(ftruncate(fd, page) == 0);
+
+       map = mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_FILE, fd, 0);
+       ATF_REQUIRE(map != MAP_FAILED);
+
+       for (i = 0; i < page; i++)
+               map[i] = 'x';



Home | Main Index | Thread Index | Old Index