Module Name: src Committed By: kamil Date: Tue Feb 11 00:41:37 UTC 2020
Modified Files: src/tests/lib/libc/sys: t_ptrace_wait.c t_ptrace_wait.h Log Message: Rewrite the t_ptrace_wait resume1 test Switch to pthread functions. Rename to 'resume'. Synchronize the threads with pthread barriers. Avoid race in the test. Test passes correctly. Fixes PR bin/54893 by Andreas Gustafsson To generate a diff of this commit: cvs rdiff -u -r1.154 -r1.155 src/tests/lib/libc/sys/t_ptrace_wait.c cvs rdiff -u -r1.18 -r1.19 src/tests/lib/libc/sys/t_ptrace_wait.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/lib/libc/sys/t_ptrace_wait.c diff -u src/tests/lib/libc/sys/t_ptrace_wait.c:1.154 src/tests/lib/libc/sys/t_ptrace_wait.c:1.155 --- src/tests/lib/libc/sys/t_ptrace_wait.c:1.154 Mon Feb 10 11:42:41 2020 +++ src/tests/lib/libc/sys/t_ptrace_wait.c Tue Feb 11 00:41:37 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: t_ptrace_wait.c,v 1.154 2020/02/10 11:42:41 kamil Exp $ */ +/* $NetBSD: t_ptrace_wait.c,v 1.155 2020/02/11 00:41:37 kamil 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.154 2020/02/10 11:42:41 kamil Exp $"); +__RCSID("$NetBSD: t_ptrace_wait.c,v 1.155 2020/02/11 00:41:37 kamil Exp $"); #define __LEGACY_PT_LWPINFO @@ -7317,44 +7317,44 @@ ATF_TC_BODY(suspend_no_deadlock, tc) /// ---------------------------------------------------------------------------- -volatile lwpid_t the_lwp_id = 0; +static pthread_barrier_t barrier1_resume; +static pthread_barrier_t barrier2_resume; -static void -lwp_main_stop(void *arg) +static void * +resume_thread(void *arg) { - the_lwp_id = _lwp_self(); - raise(SIGTRAP); + raise(SIGUSR1); + + pthread_barrier_wait(&barrier1_resume); + + /* Debugger will suspend the process here */ + + pthread_barrier_wait(&barrier2_resume); + + raise(SIGUSR2); - _lwp_exit(); + return infinite_thread(arg); } -ATF_TC(resume1); -ATF_TC_HEAD(resume1, tc) +ATF_TC(resume); +ATF_TC_HEAD(resume, tc) { atf_tc_set_md_var(tc, "descr", "Verify that a thread can be suspended by a debugger and later " "resumed by the debugger"); } -ATF_TC_BODY(resume1, tc) +ATF_TC_BODY(resume, tc) { - struct msg_fds fds; - const int exitval = 5; const int sigval = SIGSTOP; pid_t child, wpid; - uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */ #if defined(TWAIT_HAVE_STATUS) int status; #endif - ucontext_t uc; lwpid_t lid; - static const size_t ssize = 16*1024; - void *stack; - struct ptrace_lwpinfo pl; struct ptrace_siginfo psi; - - SYSCALL_REQUIRE(msg_open(&fds) == 0); + pthread_t t; DPRINTF("Before forking process PID=%d\n", getpid()); SYSCALL_REQUIRE((child = fork()) != -1); @@ -7362,31 +7362,20 @@ ATF_TC_BODY(resume1, tc) DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); + pthread_barrier_init(&barrier1_resume, NULL, 2); + pthread_barrier_init(&barrier2_resume, NULL, 2); + DPRINTF("Before raising %s from child\n", strsignal(sigval)); FORKEE_ASSERT(raise(sigval) == 0); - DPRINTF("Before allocating memory for stack in child\n"); - FORKEE_ASSERT((stack = malloc(ssize)) != NULL); - - DPRINTF("Before making context for new lwp in child\n"); - _lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize); + DPRINTF("Before creating new thread in child\n"); + FORKEE_ASSERT(pthread_create(&t, NULL, resume_thread, NULL) == 0); - DPRINTF("Before creating new in child\n"); - FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0); + pthread_barrier_wait(&barrier1_resume); - CHILD_TO_PARENT("Message", fds, msg); + pthread_barrier_wait(&barrier2_resume); - raise(SIGINT); - - DPRINTF("Before waiting for lwp %d to exit\n", lid); - FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0); - - DPRINTF("Before verifying that reported %d and running lid %d " - "are the same\n", lid, the_lwp_id); - FORKEE_ASSERT_EQ(lid, the_lwp_id); - - DPRINTF("Before exiting of the child process\n"); - _exit(exitval); + infinite_thread(NULL); } DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); @@ -7400,10 +7389,10 @@ ATF_TC_BODY(resume1, tc) SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); DPRINTF("Before calling %s() for the child - expected stopped " - "SIGTRAP\n", TWAIT_FNAME); + "SIGUSR1\n", TWAIT_FNAME); TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); - validate_status_stopped(status, SIGTRAP); + validate_status_stopped(status, SIGUSR1); DPRINTF("Before reading siginfo and lwpid_t\n"); SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1); @@ -7411,53 +7400,61 @@ ATF_TC_BODY(resume1, tc) DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid); SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1); - PARENT_FROM_CHILD("Message", fds, msg); + lid = psi.psi_lwpid; 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); + DPRINTF("Before suspending the parent for 1 second, we expect no signals\n"); + SYSCALL_REQUIRE(sleep(1) == 0); + +#if defined(TWAIT_HAVE_OPTIONS) + DPRINTF("Before calling %s() for the child - expected no status\n", + TWAIT_FNAME); + TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, WNOHANG), 0); +#endif + + DPRINTF("Before resuming the child process where it left off and " + "without signal to be sent\n"); + SYSCALL_REQUIRE(ptrace(PT_STOP, child, NULL, 0) != -1); + DPRINTF("Before calling %s() for the child - expected stopped " - "SIGINT\n", TWAIT_FNAME); + "SIGSTOP\n", TWAIT_FNAME); TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); - validate_status_stopped(status, SIGINT); + validate_status_stopped(status, SIGSTOP); - pl.pl_lwpid = 0; + DPRINTF("Before resuming LWP %d\n", lid); + SYSCALL_REQUIRE(ptrace(PT_RESUME, child, NULL, lid) != -1); - SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1); - while (pl.pl_lwpid != 0) { - SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1); - switch (pl.pl_lwpid) { - case 1: - ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL); - break; - case 2: - ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED); - break; - } - } + 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); - DPRINTF("Before resuming LWP %d\n", psi.psi_lwpid); - SYSCALL_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1); + DPRINTF("Before calling %s() for the child - expected stopped " + "SIGUSR2\n", TWAIT_FNAME); + TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); + + validate_status_stopped(status, SIGUSR2); 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); + SYSCALL_REQUIRE(ptrace(PT_KILL, child, (void *)1, 0) != -1); DPRINTF("Before calling %s() for the child - expected exited\n", TWAIT_FNAME); TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); - validate_status_exited(status, exitval); + 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)); - - msg_close(&fds); } +/// ---------------------------------------------------------------------------- + ATF_TC(syscall1); ATF_TC_HEAD(syscall1, tc) { @@ -9261,7 +9258,7 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, suspend_no_deadlock); - ATF_TP_ADD_TC(tp, resume1); + ATF_TP_ADD_TC(tp, resume); ATF_TP_ADD_TC(tp, syscall1); Index: src/tests/lib/libc/sys/t_ptrace_wait.h diff -u src/tests/lib/libc/sys/t_ptrace_wait.h:1.18 src/tests/lib/libc/sys/t_ptrace_wait.h:1.19 --- src/tests/lib/libc/sys/t_ptrace_wait.h:1.18 Tue Nov 12 18:18:04 2019 +++ src/tests/lib/libc/sys/t_ptrace_wait.h Tue Feb 11 00:41:37 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: t_ptrace_wait.h,v 1.18 2019/11/12 18:18:04 kamil Exp $ */ +/* $NetBSD: t_ptrace_wait.h,v 1.19 2020/02/11 00:41:37 kamil Exp $ */ /*- * Copyright (c) 2016, 2017, 2018, 2019 The NetBSD Foundation, Inc. @@ -73,18 +73,21 @@ # define TWAIT_GENERIC(a,b,c) waitpid((a),(b),(c)) # define TWAIT_HAVE_PID 1 # define TWAIT_HAVE_STATUS 1 +# define TWAIT_HAVE_OPTIONS 1 #elif defined(TWAIT_WAITID) # define TWAIT_FNAME "waitid" # define TWAIT_GENERIC(a,b,c) \ waitid(P_PID,(a),NULL,(c)|WEXITED|WTRAPPED) # define TWAIT_WAIT6TYPE(a,b,c,d,e,f) waitid((a),(b),(f),(d)) # define TWAIT_HAVE_PID 1 +# define TWAIT_HAVE_OPTIONS 1 #elif defined(TWAIT_WAIT3) # define TWAIT_FNAME "wait3" # define TWAIT_WAIT4TYPE(a,b,c,d) wait3((b),(c),(d)) # define TWAIT_GENERIC(a,b,c) wait3((b),(c),NULL) # define TWAIT_HAVE_STATUS 1 # define TWAIT_HAVE_RUSAGE 1 +# define TWAIT_HAVE_OPTIONS 1 #elif defined(TWAIT_WAIT4) # define TWAIT_FNAME "wait4" # define TWAIT_WAIT4TYPE(a,b,c,d) wait4((a),(b),(c),(d)) @@ -92,6 +95,7 @@ # define TWAIT_HAVE_PID 1 # define TWAIT_HAVE_STATUS 1 # define TWAIT_HAVE_RUSAGE 1 +# define TWAIT_HAVE_OPTIONS 1 #elif defined(TWAIT_WAIT6) # define TWAIT_FNAME "wait6" # define TWAIT_WAIT6TYPE(a,b,c,d,e,f) wait6((a),(b),(c),(d),(e),(f)) @@ -99,6 +103,7 @@ wait6(P_PID,(a),(b),(c)|WEXITED|WTRAPPED,NULL,NULL) # define TWAIT_HAVE_PID 1 # define TWAIT_HAVE_STATUS 1 +# define TWAIT_HAVE_OPTIONS 1 #endif /*