Source-Changes-HG archive

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

[src/trunk]: src/tests/kernel Add tests for t_memfd_create and fix bug found ...



details:   https://anonhg.NetBSD.org/src/rev/95295424b02d
branches:  trunk
changeset: 378311:95295424b02d
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Jul 29 12:16:34 2023 +0000

description:
Add tests for t_memfd_create and fix bug found by tests

diffstat:

 distrib/sets/lists/debug/mi   |    3 +-
 distrib/sets/lists/tests/mi   |    3 +-
 sys/kern/sys_memfd.c          |    6 +-
 tests/kernel/Makefile         |    3 +-
 tests/kernel/t_fcntl.c        |   51 ++++-
 tests/kernel/t_memfd_create.c |  455 ++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 510 insertions(+), 11 deletions(-)

diffs (truncated from 634 to 300 lines):

diff -r 5be5c2b32127 -r 95295424b02d distrib/sets/lists/debug/mi
--- a/distrib/sets/lists/debug/mi       Sat Jul 29 11:58:53 2023 +0000
+++ b/distrib/sets/lists/debug/mi       Sat Jul 29 12:16:34 2023 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.407 2023/07/28 18:18:59 christos Exp $
+# $NetBSD: mi,v 1.408 2023/07/29 12:16:34 christos Exp $
 ./etc/mtree/set.debug                           comp-sys-root
 ./usr/lib                                      comp-sys-usr            compatdir
 ./usr/lib/i18n/libBIG5_g.a                     comp-c-debuglib         debuglib,compatfile
@@ -1800,6 +1800,7 @@
 ./usr/libdata/debug/usr/tests/kernel/t_lock.debug                      tests-kernel-tests      debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/t_lockf.debug                     tests-kernel-tests      debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/t_lwpctl.debug                    tests-obsolete          obsolete,compattestfile
+./usr/libdata/debug/usr/tests/kernel/t_memfd_create.debug              tests-kernel-tests      debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/t_mkdir.debug                     tests-obsolete          obsolete,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/t_mqueue.debug                    tests-kernel-tests      debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/t_open_pr_57260.debug             tests-kernel-tests      debug,atf,compattestfile
diff -r 5be5c2b32127 -r 95295424b02d distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Sat Jul 29 11:58:53 2023 +0000
+++ b/distrib/sets/lists/tests/mi       Sat Jul 29 12:16:34 2023 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1278 2023/07/28 18:19:00 christos Exp $
+# $NetBSD: mi,v 1.1279 2023/07/29 12:16:34 christos Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -2310,6 +2310,7 @@
 ./usr/tests/kernel/t_lockf                             tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/t_lwpctl                            tests-obsolete          obsolete
 ./usr/tests/kernel/t_magic_symlinks                    tests-kernel-tests      compattestfile,atf
+./usr/tests/kernel/t_memfd_create                      tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/t_mkdir                             tests-obsolete          obsolete
 ./usr/tests/kernel/t_mqueue                            tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/t_nointerpreter                     tests-kernel-tests      atf
diff -r 5be5c2b32127 -r 95295424b02d sys/kern/sys_memfd.c
--- a/sys/kern/sys_memfd.c      Sat Jul 29 11:58:53 2023 +0000
+++ b/sys/kern/sys_memfd.c      Sat Jul 29 12:16:34 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sys_memfd.c,v 1.4 2023/07/29 08:46:47 riastradh Exp $  */
+/*     $NetBSD: sys_memfd.c,v 1.5 2023/07/29 12:16:34 christos Exp $   */
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_memfd.c,v 1.4 2023/07/29 08:46:47 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_memfd.c,v 1.5 2023/07/29 12:16:34 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -165,6 +165,7 @@ memfd_read(file_t *fp, off_t *offp, stru
        todo = MIN(uio->uio_resid, mfd->mfd_size - *offp);
        error = ubc_uiomove(mfd->mfd_uobj, uio, todo, UVM_ADV_SEQUENTIAL,
            UBC_READ|UBC_PARTIALOK);
+       *offp = uio->uio_offset;
 
 leave:
        if (offp == &fp->f_offset)
@@ -215,6 +216,7 @@ memfd_write(file_t *fp, off_t *offp, str
 
        error = ubc_uiomove(mfd->mfd_uobj, uio, todo, UVM_ADV_SEQUENTIAL,
            UBC_WRITE|UBC_PARTIALOK);
+       *offp = uio->uio_offset;
 
        getnanotime(&mfd->mfd_mtime);
 
diff -r 5be5c2b32127 -r 95295424b02d tests/kernel/Makefile
--- a/tests/kernel/Makefile     Sat Jul 29 11:58:53 2023 +0000
+++ b/tests/kernel/Makefile     Sat Jul 29 12:16:34 2023 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.73 2023/07/28 18:19:01 christos Exp $
+# $NetBSD: Makefile,v 1.74 2023/07/29 12:16:34 christos Exp $
 
 NOMAN=         # defined
 
@@ -12,6 +12,7 @@ TESTS_C+=     t_fcntl
 TESTS_C+=      t_lock
 TESTS_C+=      t_lockf
 TESTS_C+=      t_pty
+TESTS_C+=      t_memfd_create
 TESTS_C+=      t_mqueue
 TESTS_C+=      t_proccwd
 TESTS_C+=      t_sysv
diff -r 5be5c2b32127 -r 95295424b02d tests/kernel/t_fcntl.c
--- a/tests/kernel/t_fcntl.c    Sat Jul 29 11:58:53 2023 +0000
+++ b/tests/kernel/t_fcntl.c    Sat Jul 29 12:16:34 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: t_fcntl.c,v 1.2 2019/10/20 16:02:11 christos Exp $     */
+/*     $NetBSD: t_fcntl.c,v 1.3 2023/07/29 12:16:34 christos Exp $     */
 
 /*-
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -31,6 +31,7 @@
 
 #include <sys/param.h>
 #include <sys/types.h>
+#include <sys/mman.h>
 #include <atf-c.h>
 #include <fcntl.h>
 #include <errno.h>
@@ -38,11 +39,11 @@
 #include <string.h>
 #include <unistd.h>
 
-ATF_TC(getpath);
-ATF_TC_HEAD(getpath, tc)
+ATF_TC(getpath_vnode);
+ATF_TC_HEAD(getpath_vnode, tc)
 {  
 
-       atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) F_GETPATH");
+       atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) F_GETPATH for vnodes");
 }
 
 static const struct {
@@ -57,7 +58,7 @@ static const struct {
        { "/", ENOENT },
 };
 
-ATF_TC_BODY(getpath, tc)
+ATF_TC_BODY(getpath_vnode, tc)
 {
        char path[MAXPATHLEN];
        int fd, rv;
@@ -81,9 +82,47 @@ ATF_TC_BODY(getpath, tc)
        }
 }
 
+ATF_TC(getpath_memfd);
+ATF_TC_HEAD(getpath_memfd, tc)
+{
+
+       atf_tc_set_md_var(tc, "descr",
+           "Checks fcntl(2) F_GETPATH for fds created by memfd_create");
+}
+
+#define        MEMFD_NAME(name)        { name, "memfd:" name }
+static const struct {
+       const char *bare;
+       const char *prefixed;
+} memfd_names[] = {
+       MEMFD_NAME(""),
+       MEMFD_NAME("some text"),
+       MEMFD_NAME("memfd:"),
+       MEMFD_NAME("../\\"),
+};
+
+ATF_TC_BODY(getpath_memfd, tc)
+{
+       char path[MAXPATHLEN];
+       int fd, rv;
+
+       for (size_t i = 0; i < __arraycount(memfd_names); i++) {
+               fd = memfd_create(memfd_names[i].bare, 0);
+               ATF_REQUIRE_MSG(fd != -1, "Failed to create memfd (%s)",
+                   strerror(errno));
+               rv = fcntl(fd, F_GETPATH, path);
+               ATF_REQUIRE_MSG(rv != -1, "Can't get path `%s' (%s)",
+                   memfd_names[i].bare, strerror(errno));
+               ATF_REQUIRE_MSG(strcmp(memfd_names[i].prefixed, path) == 0,
+                   "Bad name `%s' != `%s'", path, memfd_names[i].prefixed);
+               close(fd);
+       }
+}
+
 ATF_TP_ADD_TCS(tp)
 {
-       ATF_TP_ADD_TC(tp, getpath); 
+       ATF_TP_ADD_TC(tp, getpath_vnode);
+       ATF_TP_ADD_TC(tp, getpath_memfd);
 
        return atf_no_error();
 }
diff -r 5be5c2b32127 -r 95295424b02d tests/kernel/t_memfd_create.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/kernel/t_memfd_create.c     Sat Jul 29 12:16:34 2023 +0000
@@ -0,0 +1,455 @@
+/*     $NetBSD: t_memfd_create.c,v 1.1 2023/07/29 12:16:34 christos Exp $      */
+
+/*-
+ * Copyright (c) 2023 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Theodore Preduta.
+ *
+ * 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_memfd_create.c,v 1.1 2023/07/29 12:16:34 christos Exp $");
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+char name_buf[NAME_MAX];
+char write_buf[8192];
+char read_buf[8192];
+
+ATF_TC(create_null_name);
+ATF_TC_HEAD(create_null_name, tc)
+{
+
+       atf_tc_set_md_var(tc, "descr",
+           "Checks memfd_create fails with EFAULT when invalid memory"
+           " is provided");
+}
+ATF_TC_BODY(create_null_name, tc)
+{
+       int fd;
+
+       ATF_REQUIRE_EQ_MSG(fd = memfd_create(NULL, 0), -1,
+           "Unexpected success");
+       ATF_REQUIRE_ERRNO(EFAULT, true);
+}
+
+ATF_TC(create_long_name);
+ATF_TC_HEAD(create_long_name, tc)
+{
+
+       atf_tc_set_md_var(tc, "descr",
+           "Checks memfd_create fails for names longer than NAME_MAX-6");
+}
+ATF_TC_BODY(create_long_name, tc)
+{
+       int fd;
+
+        memset(name_buf, 'A', sizeof(name_buf));
+       name_buf[NAME_MAX-6] = '\0';
+
+       ATF_REQUIRE_EQ_MSG(fd = memfd_create(name_buf, 0), -1,
+           "Unexpected success");
+       ATF_REQUIRE_ERRNO(ENAMETOOLONG, true);
+
+       name_buf[NAME_MAX-7] = '\0';
+
+       RL(fd = memfd_create(name_buf, 0));
+}
+
+ATF_TC(read_write);
+ATF_TC_HEAD(read_write, tc)
+{
+
+       atf_tc_set_md_var(tc, "descr",
+           "Checks that data can be written to/read from a memfd");
+}
+ATF_TC_BODY(read_write, tc)
+{
+       int fd;
+       off_t offset;
+
+       RL(fd = memfd_create("", 0));
+
+       tests_makegarbage(write_buf, sizeof(write_buf));
+       memset(read_buf, 0, sizeof(read_buf));
+
+       RL(write(fd, write_buf, sizeof(write_buf)));
+       offset = lseek(fd, 0, SEEK_CUR);
+       ATF_REQUIRE_EQ_MSG(offset, sizeof(write_buf),
+           "File offset not set after write (%ld != %ld)", offset,
+           sizeof(write_buf));
+
+       RZ(lseek(fd, 0, SEEK_SET));
+
+       RL(read(fd, read_buf, sizeof(read_buf)));
+       offset = lseek(fd, 0, SEEK_CUR);
+       ATF_REQUIRE_EQ_MSG(offset, sizeof(read_buf),
+           "File offset not set after read (%ld != %ld)", offset,
+           sizeof(read_buf));
+
+       for (size_t i = 0; i < sizeof(read_buf); i++)
+               ATF_REQUIRE_EQ_MSG(read_buf[i], write_buf[i],



Home | Main Index | Thread Index | Old Index