1 /* $NetBSD: t_ptrace_siginfo_wait.h,v 1.1 2020/05/05 00:57:34 kamil Exp $ */
2
3 /*-
4 * Copyright (c) 2016, 2017, 2018, 2019, 2020 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29
30 static void
ptrace_siginfo(bool faked,void (* sah)(int a,siginfo_t * b,void * c),int * signal_caught)31 ptrace_siginfo(bool faked, void (*sah)(int a, siginfo_t *b, void *c), int *signal_caught)
32 {
33 const int exitval = 5;
34 const int sigval = SIGINT;
35 const int sigfaked = SIGTRAP;
36 const int sicodefaked = TRAP_BRKPT;
37 pid_t child, wpid;
38 struct sigaction sa;
39 #if defined(TWAIT_HAVE_STATUS)
40 int status;
41 #endif
42 struct ptrace_siginfo info;
43 memset(&info, 0, sizeof(info));
44
45 DPRINTF("Before forking process PID=%d\n", getpid());
46 SYSCALL_REQUIRE((child = fork()) != -1);
47 if (child == 0) {
48 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
49 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
50
51 sa.sa_sigaction = sah;
52 sa.sa_flags = SA_SIGINFO;
53 sigemptyset(&sa.sa_mask);
54
55 FORKEE_ASSERT(sigaction(faked ? sigfaked : sigval, &sa, NULL)
56 != -1);
57
58 DPRINTF("Before raising %s from child\n", strsignal(sigval));
59 FORKEE_ASSERT(raise(sigval) == 0);
60
61 FORKEE_ASSERT_EQ(*signal_caught, 1);
62
63 DPRINTF("Before exiting of the child process\n");
64 _exit(exitval);
65 }
66 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
67
68 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
69 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
70
71 validate_status_stopped(status, sigval);
72
73 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
74 SYSCALL_REQUIRE(
75 ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
76
77 DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
78 DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
79 info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
80 info.psi_siginfo.si_errno);
81
82 if (faked) {
83 DPRINTF("Before setting new faked signal to signo=%d "
84 "si_code=%d\n", sigfaked, sicodefaked);
85 info.psi_siginfo.si_signo = sigfaked;
86 info.psi_siginfo.si_code = sicodefaked;
87 }
88
89 DPRINTF("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
90 SYSCALL_REQUIRE(
91 ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
92
93 if (faked) {
94 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
95 "child\n");
96 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info,
97 sizeof(info)) != -1);
98
99 DPRINTF("Before checking siginfo_t\n");
100 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigfaked);
101 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, sicodefaked);
102 }
103
104 DPRINTF("Before resuming the child process where it left off and "
105 "without signal to be sent\n");
106 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1,
107 faked ? sigfaked : sigval) != -1);
108
109 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
110 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
111
112 validate_status_exited(status, exitval);
113
114 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
115 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
116 }
117
118 #define PTRACE_SIGINFO(test, faked) \
119 ATF_TC(test); \
120 ATF_TC_HEAD(test, tc) \
121 { \
122 atf_tc_set_md_var(tc, "descr", \
123 "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls" \
124 "with%s setting signal to new value", faked ? "" : "out"); \
125 } \
126 \
127 static int test##_caught = 0; \
128 \
129 static void \
130 test##_sighandler(int sig, siginfo_t *info, void *ctx) \
131 { \
132 if (faked) { \
133 FORKEE_ASSERT_EQ(sig, SIGTRAP); \
134 FORKEE_ASSERT_EQ(info->si_signo, SIGTRAP); \
135 FORKEE_ASSERT_EQ(info->si_code, TRAP_BRKPT); \
136 } else { \
137 FORKEE_ASSERT_EQ(sig, SIGINT); \
138 FORKEE_ASSERT_EQ(info->si_signo, SIGINT); \
139 FORKEE_ASSERT_EQ(info->si_code, SI_LWP); \
140 } \
141 \
142 ++ test##_caught; \
143 } \
144 \
145 ATF_TC_BODY(test, tc) \
146 { \
147 \
148 ptrace_siginfo(faked, test##_sighandler, & test##_caught); \
149 }
150
151 PTRACE_SIGINFO(siginfo_set_unmodified, false)
152 PTRACE_SIGINFO(siginfo_set_faked, true)
153
154 #define ATF_TP_ADD_TCS_PTRACE_WAIT_SIGINFO() \
155 ATF_TP_ADD_TC(tp, siginfo_set_unmodified); \
156 ATF_TP_ADD_TC(tp, siginfo_set_faked);
157