Source-Changes-HG archive

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

[src/trunk]: src/tests/kernel/kqueue Add a test case for the race condition i...



details:   https://anonhg.NetBSD.org/src/rev/8544fafa9249
branches:  trunk
changeset: 1024117:8544fafa9249
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Sun Oct 10 17:47:38 2021 +0000

description:
Add a test case for the race condition in PR kern/50094, modeled after
the Go run-time scenario described in the PR.

diffstat:

 distrib/sets/lists/debug/mi  |    3 +-
 distrib/sets/lists/tests/mi  |    3 +-
 tests/kernel/kqueue/Makefile |    6 +-
 tests/kernel/kqueue/t_scan.c |  117 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 126 insertions(+), 3 deletions(-)

diffs (176 lines):

diff -r 1da1de1a6ace -r 8544fafa9249 distrib/sets/lists/debug/mi
--- a/distrib/sets/lists/debug/mi       Sun Oct 10 17:43:15 2021 +0000
+++ b/distrib/sets/lists/debug/mi       Sun Oct 10 17:47:38 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.363 2021/10/10 17:43:15 thorpej Exp $
+# $NetBSD: mi,v 1.364 2021/10/10 17:47:38 thorpej 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
@@ -1765,6 +1765,7 @@
 ./usr/libdata/debug/usr/tests/kernel/kqueue/t_proc2.debug              tests-kernel-tests      debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/kqueue/t_proc3.debug              tests-kernel-tests      debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/kqueue/t_proc4.debug              tests-kernel-tests      debug,atf,compattestfile
+./usr/libdata/debug/usr/tests/kernel/kqueue/t_scan.debug               tests-kernel-tests      debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/kqueue/t_sig.debug                        tests-kernel-tests      debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/kqueue/t_vnode.debug              tests-kernel-tests      debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/kqueue/write/t_fifo.debug         tests-kernel-tests      debug,atf,compattestfile
diff -r 1da1de1a6ace -r 8544fafa9249 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Sun Oct 10 17:43:15 2021 +0000
+++ b/distrib/sets/lists/tests/mi       Sun Oct 10 17:47:38 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1134 2021/10/10 17:43:15 thorpej Exp $
+# $NetBSD: mi,v 1.1135 2021/10/10 17:47:38 thorpej Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -2185,6 +2185,7 @@
 ./usr/tests/kernel/kqueue/t_proc2                      tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/kqueue/t_proc3                      tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/kqueue/t_proc4                      tests-kernel-tests      compattestfile,atf
+./usr/tests/kernel/kqueue/t_scan                       tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/kqueue/t_sig                                tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/kqueue/t_vnode                      tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/kqueue/write                                tests-kernel-tests      compattestfile,atf
diff -r 1da1de1a6ace -r 8544fafa9249 tests/kernel/kqueue/Makefile
--- a/tests/kernel/kqueue/Makefile      Sun Oct 10 17:43:15 2021 +0000
+++ b/tests/kernel/kqueue/Makefile      Sun Oct 10 17:47:38 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.5 2016/04/29 07:12:34 ryoon Exp $
+# $NetBSD: Makefile,v 1.6 2021/10/10 17:47:39 thorpej Exp $
 
 WARNS?=6
 NOMAN=         # defined
@@ -14,7 +14,11 @@
 TESTS_C+=      t_proc1
 TESTS_C+=      t_proc2
 TESTS_C+=      t_proc3
+TESTS_C+=      t_proc4
+TESTS_C+=      t_scan
 TESTS_C+=      t_sig
 TESTS_C+=      t_vnode
 
+LDADD.t_scan+= -lpthread
+
 .include <bsd.test.mk>
diff -r 1da1de1a6ace -r 8544fafa9249 tests/kernel/kqueue/t_scan.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/kernel/kqueue/t_scan.c      Sun Oct 10 17:47:38 2021 +0000
@@ -0,0 +1,117 @@
+/* $NetBSD: t_scan.c,v 1.1 2021/10/10 17:47:39 thorpej Exp $ */
+
+/*-
+ * Copyright (c) 2021 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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_scan.c,v 1.1 2021/10/10 17:47:39 thorpej Exp $");
+
+#include <sys/event.h>
+#include <sys/time.h>
+#include <sys/types.h>
+
+#include <err.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+/*
+ * Each kevent thread will make this many kevent() calls, and if it
+ * achieves this mark, we assume the race condition has not occurred
+ * the delcare the test passes.
+ */
+#define        NKEVENT_CALLS           10000
+
+static int kq;
+
+static void *
+kevent_thread(void *arg)
+{
+       struct timespec ts = { 0, 0 };
+       struct kevent event;
+       int rv, i;
+
+       for (i = 0; i < NKEVENT_CALLS; i++) {
+               rv = kevent(kq, NULL, 0, &event, 1, &ts);
+               if (rv < 0) {
+                       i = rv;
+                       break;
+               }
+               if (rv != 1) {
+                       break;
+               }
+       }
+
+       return (void *)(intptr_t)i;
+}
+
+ATF_TC(scan1);
+ATF_TC_HEAD(scan1, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Exercises multi-threaded kevent() calls (PR kern/50094)");
+}
+
+ATF_TC_BODY(scan1, tc)
+{
+       pthread_t t1, t2;
+       void *v1, *v2;
+       intptr_t r1, r2;
+       struct kevent event;
+       int p[2];
+
+       ATF_REQUIRE(pipe(p) == 0);
+       ATF_REQUIRE((kq = kqueue()) >= 0);
+
+       EV_SET(&event, p[0], EVFILT_READ, EV_ADD, 0, 0, 0);
+       ATF_REQUIRE(kevent(kq, &event, 1, NULL, 0, NULL) == 0);
+       ATF_REQUIRE(write(p[1], p, 1) == 1);
+
+       ATF_REQUIRE(pthread_create(&t1, NULL, kevent_thread, NULL) == 0);
+       ATF_REQUIRE(pthread_create(&t2, NULL, kevent_thread, NULL) == 0);
+
+       ATF_REQUIRE(pthread_join(t1, &v1) == 0);
+       r1 = (intptr_t)v1;
+
+       ATF_REQUIRE(pthread_join(t2, &v2) == 0);
+       r2 = (intptr_t)v2;
+
+       ATF_REQUIRE(r1 >= 0);
+       ATF_REQUIRE(r2 >= 0);
+
+       ATF_REQUIRE(r1 == NKEVENT_CALLS);
+       ATF_REQUIRE(r2 == NKEVENT_CALLS);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+       ATF_TP_ADD_TC(tp, scan1);
+
+       return atf_no_error();
+}



Home | Main Index | Thread Index | Old Index