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