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