Source-Changes-HG archive

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

[src/trunk]: src/tests/lib/libc/sys Move threads tests out of t_ptrace_wait.c...



details:   https://anonhg.NetBSD.org/src/rev/4b4bee665cd0
branches:  trunk
changeset: 971804:4b4bee665cd0
user:      kamil <kamil%NetBSD.org@localhost>
date:      Tue May 05 00:50:39 2020 +0000

description:
Move threads tests out of t_ptrace_wait.c to t_ptrace_threads_wait.h

The same tests are now included with the preprocessor in t_ptrace_wait.c.

No functional change intended.

diffstat:

 tests/lib/libc/sys/t_ptrace_threads_wait.h |  1069 ++++++++++++++++++++++++++++
 tests/lib/libc/sys/t_ptrace_wait.c         |  1057 +---------------------------
 tests/lib/libc/sys/t_ptrace_wait.h         |    14 +-
 3 files changed, 1086 insertions(+), 1054 deletions(-)

diffs (truncated from 2213 to 300 lines):

diff -r cde623f8c45c -r 4b4bee665cd0 tests/lib/libc/sys/t_ptrace_threads_wait.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/sys/t_ptrace_threads_wait.h        Tue May 05 00:50:39 2020 +0000
@@ -0,0 +1,1069 @@
+/*     $NetBSD: t_ptrace_threads_wait.h,v 1.1 2020/05/05 00:50:39 kamil Exp $  */
+
+/*-
+ * Copyright (c) 2016, 2017, 2018, 2019, 2020 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.
+ */
+
+
+#define TRACE_THREADS_NUM 100
+
+static volatile int done;
+pthread_mutex_t trace_threads_mtx = PTHREAD_MUTEX_INITIALIZER;
+
+static void *
+trace_threads_cb(void *arg __unused)
+{
+
+       pthread_mutex_lock(&trace_threads_mtx);
+       done++;
+       pthread_mutex_unlock(&trace_threads_mtx);
+
+       while (done < TRACE_THREADS_NUM)
+               sched_yield();
+
+       return NULL;
+}
+
+static void
+trace_threads(bool trace_create, bool trace_exit, bool masked)
+{
+       const int sigval = SIGSTOP;
+       pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+       int status;
+#endif
+       ptrace_state_t state;
+       const int slen = sizeof(state);
+       ptrace_event_t event;
+       const int elen = sizeof(event);
+       struct ptrace_siginfo info;
+
+       sigset_t intmask;
+
+       pthread_t t[TRACE_THREADS_NUM];
+       int rv;
+       size_t n;
+       lwpid_t lid;
+
+       /* Track created and exited threads */
+       struct lwp_event_count traced_lwps[__arraycount(t)] = {{0, 0}};
+
+       DPRINTF("Before forking process PID=%d\n", getpid());
+       SYSCALL_REQUIRE((child = fork()) != -1);
+       if (child == 0) {
+               DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
+               FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+               if (masked) {
+                       sigemptyset(&intmask);
+                       sigaddset(&intmask, SIGTRAP);
+                       sigprocmask(SIG_BLOCK, &intmask, NULL);
+               }
+
+               DPRINTF("Before raising %s from child\n", strsignal(sigval));
+               FORKEE_ASSERT(raise(sigval) == 0);
+
+               for (n = 0; n < __arraycount(t); n++) {
+                       rv = pthread_create(&t[n], NULL, trace_threads_cb,
+                           NULL);
+                       FORKEE_ASSERT(rv == 0);
+               }
+
+               for (n = 0; n < __arraycount(t); n++) {
+                       rv = pthread_join(t[n], NULL);
+                       FORKEE_ASSERT(rv == 0);
+               }
+
+               /*
+                * There is race between _exit() and pthread_join() detaching
+                * a thread. For simplicity kill the process after detecting
+                * LWP events.
+                */
+               while (true)
+                       continue;
+
+               FORKEE_ASSERT(0 && "Not reached");
+       }
+       DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+       DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+       validate_status_stopped(status, sigval);
+
+       DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+       SYSCALL_REQUIRE(
+           ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+       DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
+       DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
+           info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+           info.psi_siginfo.si_errno);
+
+       ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
+       ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
+
+       DPRINTF("Set LWP event mask for the child %d\n", child);
+       memset(&event, 0, sizeof(event));
+       if (trace_create)
+               event.pe_set_event |= PTRACE_LWP_CREATE;
+       if (trace_exit)
+               event.pe_set_event |= PTRACE_LWP_EXIT;
+       SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
+
+       DPRINTF("Before resuming the child process where it left off and "
+           "without signal to be sent\n");
+       SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+       for (n = 0; n < (trace_create ? __arraycount(t) : 0); n++) {
+               DPRINTF("Before calling %s() for the child - expected stopped "
+                   "SIGTRAP\n", TWAIT_FNAME);
+               TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
+                   child);
+
+               validate_status_stopped(status, SIGTRAP);
+
+               DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
+                   "child\n");
+               SYSCALL_REQUIRE(
+                   ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+               DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
+               DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
+                   "si_errno=%#x\n",
+                   info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+                   info.psi_siginfo.si_errno);
+
+               ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
+               ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_LWP);
+
+               SYSCALL_REQUIRE(
+                   ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
+
+               ATF_REQUIRE_EQ_MSG(state.pe_report_event, PTRACE_LWP_CREATE,
+                   "%d != %d", state.pe_report_event, PTRACE_LWP_CREATE);
+
+               lid = state.pe_lwp;
+               DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
+
+               *FIND_EVENT_COUNT(traced_lwps, lid) += 1;
+
+               DPRINTF("Before resuming the child process where it left off "
+                   "and without signal to be sent\n");
+               SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+       }
+
+       for (n = 0; n < (trace_exit ? __arraycount(t) : 0); n++) {
+               DPRINTF("Before calling %s() for the child - expected stopped "
+                   "SIGTRAP\n", TWAIT_FNAME);
+               TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
+                   child);
+
+               validate_status_stopped(status, SIGTRAP);
+
+               DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
+                   "child\n");
+               SYSCALL_REQUIRE(
+                   ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+               DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
+               DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
+                   "si_errno=%#x\n",
+                   info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+                   info.psi_siginfo.si_errno);
+
+               ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
+               ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_LWP);
+
+               SYSCALL_REQUIRE(
+                   ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
+
+               ATF_REQUIRE_EQ_MSG(state.pe_report_event, PTRACE_LWP_EXIT,
+                   "%d != %d", state.pe_report_event, PTRACE_LWP_EXIT);
+
+               lid = state.pe_lwp;
+               DPRINTF("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
+
+               if (trace_create) {
+                       int *count = FIND_EVENT_COUNT(traced_lwps, lid);
+                       ATF_REQUIRE_EQ(*count, 1);
+                       *count = 0;
+               }
+
+               DPRINTF("Before resuming the child process where it left off "
+                   "and without signal to be sent\n");
+               SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+       }
+
+       kill(child, SIGKILL);
+
+       DPRINTF("Before calling %s() for the child - expected exited\n",
+           TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+       validate_status_signaled(status, SIGKILL, 0);
+
+       DPRINTF("Before calling %s() for the child - expected no process\n",
+           TWAIT_FNAME);
+       TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+#define TRACE_THREADS(test, trace_create, trace_exit, mask)            \
+ATF_TC(test);                                                          \
+ATF_TC_HEAD(test, tc)                                                  \
+{                                                                      \
+        atf_tc_set_md_var(tc, "descr",                                 \
+            "Verify spawning threads with%s tracing LWP create and"    \
+           "with%s tracing LWP exit", trace_create ? "" : "out",       \
+           trace_exit ? "" : "out");                                   \
+}                                                                      \
+                                                                       \
+ATF_TC_BODY(test, tc)                                                  \
+{                                                                      \
+                                                                       \
+        trace_threads(trace_create, trace_exit, mask);                 \
+}
+
+TRACE_THREADS(trace_thread_nolwpevents, false, false, false)
+TRACE_THREADS(trace_thread_lwpexit, false, true, false)
+TRACE_THREADS(trace_thread_lwpcreate, true, false, false)
+TRACE_THREADS(trace_thread_lwpcreate_and_exit, true, true, false)
+
+TRACE_THREADS(trace_thread_lwpexit_masked_sigtrap, false, true, true)
+TRACE_THREADS(trace_thread_lwpcreate_masked_sigtrap, true, false, true)
+TRACE_THREADS(trace_thread_lwpcreate_and_exit_masked_sigtrap, true, true, true)
+
+/// ----------------------------------------------------------------------------
+
+static void *
+thread_and_exec_thread_cb(void *arg __unused)
+{
+
+       execlp("/bin/echo", "/bin/echo", NULL);
+
+       abort();
+}
+
+static void
+threads_and_exec(void)
+{
+       const int sigval = SIGSTOP;
+       pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+       int status;
+#endif
+       ptrace_state_t state;
+       const int slen = sizeof(state);
+       ptrace_event_t event;
+       const int elen = sizeof(event);
+       struct ptrace_siginfo info;
+
+       pthread_t t;
+       lwpid_t lid;
+
+       DPRINTF("Before forking process PID=%d\n", getpid());
+       SYSCALL_REQUIRE((child = fork()) != -1);
+       if (child == 0) {
+               DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
+               FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+               DPRINTF("Before raising %s from child\n", strsignal(sigval));
+               FORKEE_ASSERT(raise(sigval) == 0);
+
+               FORKEE_ASSERT(pthread_create(&t, NULL,
+                   thread_and_exec_thread_cb, NULL) == 0);



Home | Main Index | Thread Index | Old Index