Source-Changes-HG archive

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

[src/netbsd-9]: src Pull up following revision(s) (requested by kamil in tick...



details:   https://anonhg.NetBSD.org/src/rev/c79511291240
branches:  netbsd-9
changeset: 963168:c79511291240
user:      martin <martin%NetBSD.org@localhost>
date:      Wed Oct 23 19:25:39 2019 +0000

description:
Pull up following revision(s) (requested by kamil in ticket #366):

        tests/lib/libc/sys/t_ptrace_wait.c: revision 1.136
        sys/kern/kern_sig.c: revision 1.373
        tests/lib/libc/sys/t_ptrace_wait.c: revision 1.138
        tests/lib/libc/sys/t_ptrace_wait.c: revision 1.139
        sys/kern/kern_sig.c: revision 1.376
        tests/lib/libc/sys/t_ptrace_wait.c: revision 1.140
        sys/kern/sys_ptrace_common.c: revision 1.64

Fix typo in a comment

Enable TEST_LWP_ENABLED in t_ptrace_wait*
The LWP events (created, exited) are now reliable in my local tests.
PR kern/51420
PR kern/51995

Remove the short-circuit lwp_exit() path from sigswitch()

sigswitch() can be called from exit1() through:

   ttywait()->ttysleep()-> cv_timedwait_sig()->sleepq_block()->issignal()->sigswitch()

lwp_exit() called for the last LWP triggers exit1() and this causes a panic.
The debugger related signals have short-circuit demise paths in
eventswitch() and other functions, before calling sigswitch().

This change restores the original behavior, but there is an open question
whether the kernel crash is a red herring of misbehavior of ttywait().
This should fix PR kern/54618 by David H. Gutteridge

Fix a race condition when handling concurrent LWP signals and add a test

Fix a race condition that caused PT_GET_SIGINFO to return incorrect
information when multiple signals were delivered concurrently
to different LWPs.  Add a regression test that verifies that when 50
threads concurrently use pthread_kill() on themselves, the debugger
receives all signals with correct information.

The kernel uses separate signal queues for each LWP.  However,
the signal context used to implement PT_GET_SIGINFO is stored in 'struct
proc' and therefore common to all LWPs in the process.  Previously,
this member was filled in kpsignal2(), i.e. when the signal was sent.

This meant that if another LWP managed to send another signal
concurrently, the data was overwritten before the process was stopped.

As a result, PT_GET_SIGINFO did not report the correct LWP and signal
(it could even report a different signal than wait()).  This can be
quite reliably reproduced with the number of 20 LWPs, however it can
also occur with 10.

This patch moves setting of signal context to issignal(), just before
the process is actually stopped.  The data is taken from per-LWP
or per-process signal queue.  The added test confirms that the debugger
correctly receives all signals, and PT_GET_SIGINFO reports both correct
LWP and signal number.
Reviewed by kamil.

Remove preprocessor switch TEST_VFORK_ENABLED in t_ptrace_wait*
vfork(2) tests are now enabled always and confirmed to be stable.

Remove preprocessor switch TEST_LWP_ENABLED in t_ptrace_wait*
LWP tests are now enabled always and confirmed to be stable.

diffstat:

 sys/kern/kern_sig.c                |   49 ++++----
 sys/kern/sys_ptrace_common.c       |    6 +-
 tests/lib/libc/sys/t_ptrace_wait.c |  203 ++++++++++++++++++++++++++++--------
 3 files changed, 184 insertions(+), 74 deletions(-)

diffs (truncated from 532 to 300 lines):

diff -r e01b0a264bdc -r c79511291240 sys/kern/kern_sig.c
--- a/sys/kern/kern_sig.c       Wed Oct 23 19:17:15 2019 +0000
+++ b/sys/kern/kern_sig.c       Wed Oct 23 19:25:39 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_sig.c,v 1.364.2.7 2019/10/15 19:28:16 martin Exp $        */
+/*     $NetBSD: kern_sig.c,v 1.364.2.8 2019/10/23 19:25:39 martin Exp $        */
 
 /*-
  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.364.2.7 2019/10/15 19:28:16 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.364.2.8 2019/10/23 19:25:39 martin Exp $");
 
 #include "opt_ptrace.h"
 #include "opt_dtrace.h"
@@ -1318,10 +1318,6 @@
        if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
                return 0;
 
-       /* XXX for core dump/debugger */
-       p->p_sigctx.ps_lwp = ksi->ksi_lid;
-       p->p_sigctx.ps_info = ksi->ksi_info;
-
        /*
         * Notify any interested parties of the signal.
         */
@@ -1711,21 +1707,6 @@
        }
 
        /*
-        * If we are exiting, demise now.
-        *
-        * This avoids notifying tracer and deadlocking.
-        */
-       if (__predict_false(ISSET(p->p_sflag, PS_WEXIT))) {
-               mutex_exit(p->p_lock);
-               if (proc_lock_held) {
-                       mutex_exit(proc_lock);
-               }
-               lwp_exit(l);
-               panic("sigswitch");
-               /* NOTREACHED */
-       }
-
-       /*
         * On entry we know that the process needs to stop.  If it's
         * the result of a 'sideways' stop signal that has been sourced
         * through issignal(), then stop other LWPs in the process too.
@@ -1844,7 +1825,7 @@
 issignal(struct lwp *l)
 {
        struct proc *p;
-       int signo, prop;
+       int siglwp, signo, prop;
        sigpend_t *sp;
        sigset_t ss;
 
@@ -1886,6 +1867,7 @@
                if (signo == 0) {
                        sp = &l->l_sigpend;
                        ss = sp->sp_set;
+                       siglwp = l->l_lid;
                        if ((p->p_lflag & PL_PPWAIT) != 0)
                                sigminusset(&vforksigmask, &ss);
                        sigminusset(&l->l_sigmask, &ss);
@@ -1893,6 +1875,7 @@
                        if ((signo = firstsig(&ss)) == 0) {
                                sp = &p->p_sigpend;
                                ss = sp->sp_set;
+                               siglwp = 0;
                                if ((p->p_lflag & PL_PPWAIT) != 0)
                                        sigminusset(&vforksigmask, &ss);
                                sigminusset(&l->l_sigmask, &ss);
@@ -1911,6 +1894,28 @@
                        }
                }
 
+               if (sp) {
+                       /* Overwrite process' signal context to correspond
+                        * to the currently reported LWP.  This is necessary
+                        * for PT_GET_SIGINFO to report the correct signal when
+                        * multiple LWPs have pending signals.  We do this only
+                        * when the signal comes from the queue, for signals
+                        * created by the debugger we assume it set correct
+                        * siginfo.
+                        */
+                       ksiginfo_t *ksi = TAILQ_FIRST(&sp->sp_info);
+                       if (ksi) {
+                               p->p_sigctx.ps_lwp = ksi->ksi_lid;
+                               p->p_sigctx.ps_info = ksi->ksi_info;
+                       } else {
+                               p->p_sigctx.ps_lwp = siglwp;
+                               memset(&p->p_sigctx.ps_info, 0,
+                                   sizeof(p->p_sigctx.ps_info));
+                               p->p_sigctx.ps_info._signo = signo;
+                               p->p_sigctx.ps_info._code = SI_NOINFO;
+                       }
+               }
+
                /*
                 * We should see pending but ignored signals only if
                 * we are being traced.
diff -r e01b0a264bdc -r c79511291240 sys/kern/sys_ptrace_common.c
--- a/sys/kern/sys_ptrace_common.c      Wed Oct 23 19:17:15 2019 +0000
+++ b/sys/kern/sys_ptrace_common.c      Wed Oct 23 19:25:39 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sys_ptrace_common.c,v 1.58.2.8 2019/10/15 19:11:02 martin Exp $        */
+/*     $NetBSD: sys_ptrace_common.c,v 1.58.2.9 2019/10/23 19:25:39 martin Exp $        */
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -118,7 +118,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58.2.8 2019/10/15 19:11:02 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58.2.9 2019/10/23 19:25:39 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ptrace.h"
@@ -1275,7 +1275,7 @@
                }
 
                /*
-                * Reject setting program cunter to 0x0 if VA0 is disabled.
+                * Reject setting program counter to 0x0 if VA0 is disabled.
                 *
                 * Not all kernels implement this feature to set Program
                 * Counter in one go in PT_CONTINUE and similar operations.
diff -r e01b0a264bdc -r c79511291240 tests/lib/libc/sys/t_ptrace_wait.c
--- a/tests/lib/libc/sys/t_ptrace_wait.c        Wed Oct 23 19:17:15 2019 +0000
+++ b/tests/lib/libc/sys/t_ptrace_wait.c        Wed Oct 23 19:25:39 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: t_ptrace_wait.c,v 1.131.2.5 2019/10/15 18:47:03 martin Exp $   */
+/*     $NetBSD: t_ptrace_wait.c,v 1.131.2.6 2019/10/23 19:25:39 martin Exp $   */
 
 /*-
  * Copyright (c) 2016, 2017, 2018, 2019 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_ptrace_wait.c,v 1.131.2.5 2019/10/15 18:47:03 martin Exp $");
+__RCSID("$NetBSD: t_ptrace_wait.c,v 1.131.2.6 2019/10/23 19:25:39 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -121,14 +121,6 @@
        printf("%s() %s:%d " a, __func__, __FILE__, __LINE__,  ##__VA_ARGS__); \
     while (/*CONSTCOND*/0)
 
-#ifndef TEST_VFORK_ENABLED
-#define TEST_VFORK_ENABLED 1
-#endif
-
-#ifndef TEST_LWP_ENABLED
-#define TEST_LWP_ENABLED 0
-#endif
-
 /// ----------------------------------------------------------------------------
 
 static void
@@ -3269,7 +3261,6 @@
 FORK_TEST(fork16, "fork", true, true, true, true)
 #endif
 
-#if TEST_VFORK_ENABLED
 FORK_TEST(vfork1, "vfork", false, false, false, false)
 #if defined(TWAIT_HAVE_PID)
 FORK_TEST(vfork2, "vfork", false, true, false, false)
@@ -3294,7 +3285,6 @@
 FORK_TEST(vfork15, "vfork", true, false, true, true)
 FORK_TEST(vfork16, "vfork", true, true, true, true)
 #endif
-#endif
 
 FORK_TEST(posix_spawn1, "spawn", false, false, false, false)
 FORK_TEST(posix_spawn2, "spawn", false, true, false, false)
@@ -3497,22 +3487,17 @@
 
 FORK_DETACH_FORKER(posix_spawn_detach_spawner, "spawn", false)
 FORK_DETACH_FORKER(fork_detach_forker, "fork", false)
-#if TEST_VFORK_ENABLED
 FORK_DETACH_FORKER(vfork_detach_vforker, "vfork", false)
 FORK_DETACH_FORKER(vfork_detach_vforkerdone, "vforkdone", false)
-#endif
 
 FORK_DETACH_FORKER(posix_spawn_kill_spawner, "spawn", true)
 FORK_DETACH_FORKER(fork_kill_forker, "fork", true)
-#if TEST_VFORK_ENABLED
 FORK_DETACH_FORKER(vfork_kill_vforker, "vfork", true)
 FORK_DETACH_FORKER(vfork_kill_vforkerdone, "vforkdone", true)
 #endif
-#endif
 
 /// ----------------------------------------------------------------------------
 
-#if TEST_VFORK_ENABLED
 static void
 traceme_vfork_fork_body(pid_t (*fn)(void))
 {
@@ -3571,7 +3556,6 @@
 
 TRACEME_VFORK_FORK_TEST(traceme_vfork_fork, fork)
 TRACEME_VFORK_FORK_TEST(traceme_vfork_vfork, vfork)
-#endif
 
 /// ----------------------------------------------------------------------------
 
@@ -5496,11 +5480,6 @@
        /* Track created and exited threads */
        bool traced_lwps[__arraycount(t)];
 
-#if !TEST_LWP_ENABLED
-       if (trace_create || trace_exit)
-               atf_tc_skip("PR kern/51995");
-#endif
-
        DPRINTF("Before forking process PID=%d\n", getpid());
        SYSCALL_REQUIRE((child = fork()) != -1);
        if (child == 0) {
@@ -6126,13 +6105,11 @@
 FORK2_TEST(posix_spawn_singalignored, "spawn", false, true)
 FORK2_TEST(fork_singalmasked, "fork", true, false)
 FORK2_TEST(fork_singalignored, "fork", false, true)
-#if TEST_VFORK_ENABLED
 FORK2_TEST(vfork_singalmasked, "vfork", true, false)
 FORK2_TEST(vfork_singalignored, "vfork", false, true)
 FORK2_TEST(vforkdone_singalmasked, "vforkdone", true, false)
 FORK2_TEST(vforkdone_singalignored, "vforkdone", false, true)
 #endif
-#endif
 
 /// ----------------------------------------------------------------------------
 
@@ -6991,7 +6968,6 @@
 //CLONE_TEST(clone_sighand8, CLONE_SIGHAND, true, true, true)
 #endif
 
-#if TEST_VFORK_ENABLED
 CLONE_TEST(clone_vfork1, CLONE_VFORK, false, false, false)
 #if defined(TWAIT_HAVE_PID)
 CLONE_TEST(clone_vfork2, CLONE_VFORK, true, false, false)
@@ -7004,7 +6980,6 @@
 CLONE_TEST(clone_vfork7, CLONE_VFORK, false, true, true)
 CLONE_TEST(clone_vfork8, CLONE_VFORK, true, true, true)
 #endif
-#endif
 
 /// ----------------------------------------------------------------------------
 
@@ -7367,15 +7342,12 @@
 CLONE_TEST2(clone_files_signalmasked, CLONE_FILES, false, true)
 //CLONE_TEST2(clone_sighand_signalignored, CLONE_SIGHAND, true, false) // XXX
 //CLONE_TEST2(clone_sighand_signalmasked, CLONE_SIGHAND, false, true)  // XXX
-#if TEST_VFORK_ENABLED
 CLONE_TEST2(clone_vfork_signalignored, CLONE_VFORK, true, false)
 CLONE_TEST2(clone_vfork_signalmasked, CLONE_VFORK, false, true)
 #endif
-#endif
 
 /// ----------------------------------------------------------------------------
 
-#if TEST_VFORK_ENABLED
 #if defined(TWAIT_HAVE_PID)
 static void
 traceme_vfork_clone_body(int flags)
@@ -7456,7 +7428,6 @@
 //TRACEME_VFORK_CLONE_TEST(traceme_vfork_clone_sighand, CLONE_SIGHAND)  // XXX
 TRACEME_VFORK_CLONE_TEST(traceme_vfork_clone_vfork, CLONE_VFORK)
 #endif
-#endif
 
 /// ----------------------------------------------------------------------------
 
@@ -7722,6 +7693,152 @@
 
 /// ----------------------------------------------------------------------------
 
+#if defined(TWAIT_HAVE_STATUS)
+
+ATF_TC(thread_concurrent_signals);
+ATF_TC_HEAD(thread_concurrent_signals, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Verify that concurrent signals issued to a single thread "
+           "are reported correctly");
+}
+
+/* List of signals to use for the test */
+const int thread_concurrent_signals_list[] = {
+       SIGIO,
+       SIGXCPU,
+       SIGXFSZ,
+       SIGVTALRM,
+       SIGPROF,
+       SIGWINCH,



Home | Main Index | Thread Index | Old Index