1 /* $NetBSD: t_ptrace_wait.c,v 1.192 2024/04/01 18:33:23 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 /*
30 * XXX Hack: Force the use of sys/exec_elf.h, not elfdefinitions.h.
31 * Why?
32 *
33 * - libelf.h and gelf.h are needed for parsing core files in
34 * t_ptrace_core_wait.h.
35 *
36 * - sys/exec_elf.h is needed for struct netbsd_elfcore_procinfo also
37 * in t_ptrace_core_wait.h.
38 *
39 * libelf.h and gelf.h pull in elfdefinitions.h, but that conflicts
40 * with sys/exec_elf.h on most basic ELF definitions.
41 */
42 #define _SYS_ELFDEFINITIONS_H_
43
44 #include <sys/cdefs.h>
45 __RCSID("$NetBSD: t_ptrace_wait.c,v 1.192 2024/04/01 18:33:23 riastradh Exp $");
46
47 #define __LEGACY_PT_LWPINFO
48
49 #include <sys/param.h>
50 #include <sys/types.h>
51 #include <sys/exec_elf.h>
52 #include <sys/mman.h>
53 #include <sys/ptrace.h>
54 #include <sys/resource.h>
55 #include <sys/stat.h>
56 #include <sys/syscall.h>
57 #include <sys/sysctl.h>
58 #include <sys/uio.h>
59 #include <sys/wait.h>
60 #include <machine/reg.h>
61 #include <assert.h>
62 #include <elf.h>
63 #include <err.h>
64 #include <errno.h>
65 #include <fcntl.h>
66 #include <lwp.h>
67 #include <pthread.h>
68 #include <sched.h>
69 #include <signal.h>
70 #include <spawn.h>
71 #include <stdint.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <strings.h>
75 #include <time.h>
76 #include <unistd.h>
77
78 #if defined(__i386__) || defined(__x86_64__)
79 #include <cpuid.h>
80 #include <x86/cpu_extended_state.h>
81 #include <x86/specialreg.h>
82 #endif
83
84 #include <libelf.h>
85 #include <gelf.h>
86
87 #include <atf-c.h>
88
89 #ifdef ENABLE_TESTS
90
91 /* Assumptions in the kernel code that must be kept. */
92 __CTASSERT(sizeof(((struct ptrace_state *)0)->pe_report_event) ==
93 sizeof(((siginfo_t *)0)->si_pe_report_event));
94 __CTASSERT(sizeof(((struct ptrace_state *)0)->pe_other_pid) ==
95 sizeof(((siginfo_t *)0)->si_pe_other_pid));
96 __CTASSERT(sizeof(((struct ptrace_state *)0)->pe_lwp) ==
97 sizeof(((siginfo_t *)0)->si_pe_lwp));
98 __CTASSERT(sizeof(((struct ptrace_state *)0)->pe_other_pid) ==
99 sizeof(((struct ptrace_state *)0)->pe_lwp));
100
101 #include "h_macros.h"
102
103 #include "t_ptrace_wait.h"
104 #include "msg.h"
105
106 #define SYSCALL_REQUIRE(expr) ATF_REQUIRE_MSG(expr, "%s: %s", # expr, \
107 strerror(errno))
108 #define SYSCALL_REQUIRE_ERRNO(res, exp) ATF_REQUIRE_MSG(res == exp, \
109 "%d(%s) != %d", res, strerror(res), exp)
110
111 static int debug = 0;
112
113 #define DPRINTF(a, ...) do \
114 if (debug) \
115 printf("%s() %d.%d %s:%d " a, \
116 __func__, getpid(), _lwp_self(), __FILE__, __LINE__, ##__VA_ARGS__); \
117 while (/*CONSTCOND*/0)
118
119 /// ----------------------------------------------------------------------------
120
121 #include "t_ptrace_register_wait.h"
122 #include "t_ptrace_syscall_wait.h"
123 #include "t_ptrace_step_wait.h"
124 #include "t_ptrace_kill_wait.h"
125 #include "t_ptrace_bytetransfer_wait.h"
126 #include "t_ptrace_clone_wait.h"
127 #include "t_ptrace_fork_wait.h"
128 #include "t_ptrace_signal_wait.h"
129 #include "t_ptrace_eventmask_wait.h"
130 #include "t_ptrace_lwp_wait.h"
131 #include "t_ptrace_exec_wait.h"
132 #include "t_ptrace_topology_wait.h"
133 #include "t_ptrace_threads_wait.h"
134 #include "t_ptrace_siginfo_wait.h"
135 #include "t_ptrace_core_wait.h"
136 #include "t_ptrace_misc_wait.h"
137
138 /// ----------------------------------------------------------------------------
139
140 #include "t_ptrace_amd64_wait.h"
141 #include "t_ptrace_i386_wait.h"
142 #include "t_ptrace_x86_wait.h"
143
144 /// ----------------------------------------------------------------------------
145
146 #else
147 ATF_TC(dummy);
ATF_TC_HEAD(dummy,tc)148 ATF_TC_HEAD(dummy, tc)
149 {
150 atf_tc_set_md_var(tc, "descr", "A dummy test");
151 }
152
ATF_TC_BODY(dummy,tc)153 ATF_TC_BODY(dummy, tc)
154 {
155
156 // Dummy, skipped
157 // The ATF framework requires at least a single defined test.
158 }
159 #endif
160
ATF_TP_ADD_TCS(tp)161 ATF_TP_ADD_TCS(tp)
162 {
163 setvbuf(stdout, NULL, _IONBF, 0);
164 setvbuf(stderr, NULL, _IONBF, 0);
165
166 #ifdef ENABLE_TESTS
167 ATF_TP_ADD_TCS_PTRACE_WAIT_REGISTER();
168 ATF_TP_ADD_TCS_PTRACE_WAIT_SYSCALL();
169 ATF_TP_ADD_TCS_PTRACE_WAIT_STEP();
170 ATF_TP_ADD_TCS_PTRACE_WAIT_KILL();
171 ATF_TP_ADD_TCS_PTRACE_WAIT_BYTETRANSFER();
172 ATF_TP_ADD_TCS_PTRACE_WAIT_CLONE();
173 ATF_TP_ADD_TCS_PTRACE_WAIT_FORK();
174 ATF_TP_ADD_TCS_PTRACE_WAIT_SIGNAL();
175 ATF_TP_ADD_TCS_PTRACE_WAIT_EVENTMASK();
176 ATF_TP_ADD_TCS_PTRACE_WAIT_LWP();
177 ATF_TP_ADD_TCS_PTRACE_WAIT_EXEC();
178 ATF_TP_ADD_TCS_PTRACE_WAIT_TOPOLOGY();
179 ATF_TP_ADD_TCS_PTRACE_WAIT_THREADS();
180 ATF_TP_ADD_TCS_PTRACE_WAIT_SIGINFO();
181 ATF_TP_ADD_TCS_PTRACE_WAIT_CORE();
182 ATF_TP_ADD_TCS_PTRACE_WAIT_MISC();
183
184 ATF_TP_ADD_TCS_PTRACE_WAIT_AMD64();
185 ATF_TP_ADD_TCS_PTRACE_WAIT_I386();
186 ATF_TP_ADD_TCS_PTRACE_WAIT_X86();
187
188 #else
189 ATF_TP_ADD_TC(tp, dummy);
190 #endif
191
192 return atf_no_error();
193 }
194