t_ptrace_register_wait.h revision 1.5 1 /* $NetBSD: t_ptrace_register_wait.h,v 1.5 2025/05/02 02:24:32 riastradh 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 #if defined(HAVE_GPREGS) || defined(HAVE_FPREGS)
30 static void
31 access_regs(const char *regset, const char *aux)
32 {
33 const int exitval = 5;
34 const int sigval = SIGSTOP;
35 pid_t child, wpid;
36 #if defined(TWAIT_HAVE_STATUS)
37 int status;
38 #endif
39 #if defined(HAVE_GPREGS)
40 struct reg gpr;
41 register_t rgstr;
42 #endif
43 #if defined(HAVE_FPREGS)
44 struct fpreg fpr;
45 #endif
46
47 #if !defined(HAVE_GPREGS)
48 if (strcmp(regset, "regs") == 0)
49 atf_tc_fail("Impossible test scenario!");
50 #endif
51
52 #if !defined(HAVE_FPREGS)
53 if (strcmp(regset, "fpregs") == 0)
54 atf_tc_fail("Impossible test scenario!");
55 #endif
56
57 DPRINTF("Before forking process PID=%d\n", getpid());
58 SYSCALL_REQUIRE((child = fork()) != -1);
59 if (child == 0) {
60 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
61 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
62
63 DPRINTF("Before raising %s from child\n", strsignal(sigval));
64 FORKEE_ASSERT(raise(sigval) == 0);
65
66 DPRINTF("Before exiting of the child process\n");
67 _exit(exitval);
68 }
69 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
70
71 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
72 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
73
74 validate_status_stopped(status, sigval);
75
76 #if defined(HAVE_GPREGS)
77 if (strcmp(regset, "regs") == 0) {
78 DPRINTF("Call GETREGS for the child process\n");
79 SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &gpr, 0) != -1);
80
81 if (strcmp(aux, "none") == 0) {
82 DPRINTF("Retrieved registers\n");
83 } else if (strcmp(aux, "pc") == 0) {
84 rgstr = PTRACE_REG_PC(&gpr);
85 DPRINTF("Retrieved %" PRIxREGISTER "\n", rgstr);
86 } else if (strstr(aux, "set_pc") != NULL) {
87 rgstr = PTRACE_REG_PC(&gpr);
88 DPRINTF("Retrieved PC %" PRIxREGISTER "\n", rgstr);
89 if (strstr(aux, "0x1") != NULL) {
90 rgstr |= 0x1;
91 } else if (strstr(aux, "0x3") != NULL) {
92 rgstr |= 0x3;
93 } else if (strstr(aux, "0x7") != NULL) {
94 rgstr |= 0x7;
95 }
96 DPRINTF("Set PC %" PRIxREGISTER "\n", rgstr);
97 PTRACE_REG_SET_PC(&gpr, rgstr);
98 if (strcmp(aux, "set_pc") != 0) {
99 /* This call can fail with EINVAL or similar. */
100 ptrace(PT_SETREGS, child, &gpr, 0);
101 }
102 } else if (strcmp(aux, "sp") == 0) {
103 rgstr = PTRACE_REG_SP(&gpr);
104 DPRINTF("Retrieved %" PRIxREGISTER "\n", rgstr);
105 } else if (strcmp(aux, "intrv") == 0) {
106 rgstr = PTRACE_REG_INTRV(&gpr);
107 DPRINTF("Retrieved %" PRIxREGISTER "\n", rgstr);
108 } else if (strcmp(aux, "setregs") == 0) {
109 DPRINTF("Call SETREGS for the child process\n");
110 SYSCALL_REQUIRE(
111 ptrace(PT_SETREGS, child, &gpr, 0) != -1);
112 }
113 }
114 #endif
115
116 #if defined(HAVE_FPREGS)
117 if (strcmp(regset, "fpregs") == 0) {
118 DPRINTF("Call GETFPREGS for the child process\n");
119 SYSCALL_REQUIRE(ptrace(PT_GETFPREGS, child, &fpr, 0) != -1);
120
121 if (strcmp(aux, "getfpregs") == 0) {
122 DPRINTF("Retrieved FP registers\n");
123 } else if (strcmp(aux, "setfpregs") == 0) {
124 DPRINTF("Call SETFPREGS for the child\n");
125 SYSCALL_REQUIRE(
126 ptrace(PT_SETFPREGS, child, &fpr, 0) != -1);
127 }
128 }
129 #endif
130
131 DPRINTF("Before resuming the child process where it left off and "
132 "without signal to be sent\n");
133 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
134
135 if (strstr(aux, "unaligned") != NULL) {
136 DPRINTF("Before resuming the child process where it left off "
137 "and without signal to be sent\n");
138
139 ptrace(PT_KILL, child, NULL, 0);
140
141 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
142 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
143 child);
144
145 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
146 TWAIT_REQUIRE_FAILURE(ECHILD,
147 wpid = TWAIT_GENERIC(child, &status, 0));
148 } else {
149 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
150 TWAIT_REQUIRE_SUCCESS(
151 wpid = TWAIT_GENERIC(child, &status, 0), child);
152
153 validate_status_exited(status, exitval);
154
155 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
156 TWAIT_REQUIRE_FAILURE(ECHILD,
157 wpid = TWAIT_GENERIC(child, &status, 0));
158 }
159 }
160
161 #define ACCESS_REGS(test, regset, aux) \
162 ATF_TC(test); \
163 ATF_TC_HEAD(test, tc) \
164 { \
165 atf_tc_set_md_var(tc, "descr", \
166 "Verify " regset " with auxiliary operation: " aux); \
167 } \
168 \
169 ATF_TC_BODY(test, tc) \
170 { \
171 \
172 access_regs(regset, aux); \
173 }
174 #endif
175
176 #if defined(HAVE_GPREGS)
177 ACCESS_REGS(access_regs1, "regs", "none")
178 ACCESS_REGS(access_regs2, "regs", "pc")
179 ACCESS_REGS(access_regs3, "regs", "set_pc")
180 ACCESS_REGS(access_regs4, "regs", "sp")
181 ACCESS_REGS(access_regs5, "regs", "intrv")
182 ACCESS_REGS(access_regs6, "regs", "setregs")
183 ACCESS_REGS(access_regs_set_unaligned_pc_0x1, "regs", "set_pc+unaligned+0x1")
184 ACCESS_REGS(access_regs_set_unaligned_pc_0x3, "regs", "set_pc+unaligned+0x3")
185 ACCESS_REGS(access_regs_set_unaligned_pc_0x7, "regs", "set_pc+unaligned+0x7")
186 #endif
187 #if defined(HAVE_FPREGS)
188 ACCESS_REGS(access_fpregs1, "fpregs", "getfpregs")
189 ACCESS_REGS(access_fpregs2, "fpregs", "setfpregs")
190 #endif
191
192 #define ATF_TP_ADD_TCS_PTRACE_WAIT_REGISTER() \
193 ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs1); \
194 ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs2); \
195 ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs3); \
196 ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs4); \
197 ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs5); \
198 ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs6); \
199 ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs_set_unaligned_pc_0x1); \
200 ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs_set_unaligned_pc_0x3); \
201 ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs_set_unaligned_pc_0x7); \
202 ATF_TP_ADD_TC_HAVE_FPREGS(tp, access_fpregs1); \
203 ATF_TP_ADD_TC_HAVE_FPREGS(tp, access_fpregs2);
204