1 /* $NetBSD: t_ptrace_wait.h,v 1.7 2017/01/09 22:09:20 kamil Exp $ */
4 * Copyright (c) 2016 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 /* Detect plain wait(2) use-case */
30 #if !defined(TWAIT_WAITPID) && \
31 !defined(TWAIT_WAITID) && \
32 !defined(TWAIT_WAIT3) && \
33 !defined(TWAIT_WAIT4) && \
39 * There are two classes of wait(2)-like functions:
40 * - wait4(2)-like accepting pid_t, optional options parameter, struct rusage*
41 * - wait6(2)-like accepting idtype_t, id_t, struct wrusage, mandatory options
43 * The TWAIT_FNAME value is to be used for convenience in debug messages.
45 * The TWAIT_GENERIC() macro is designed to reuse the same unmodified
46 * code with as many wait(2)-like functions as possible.
48 * In a common use-case wait4(2) and wait6(2)-like function can work the almost
49 * the same way, however there are few important differences:
50 * wait6(2) must specify P_PID for idtype to match wpid from wait4(2).
51 * To behave like wait4(2), wait6(2) the 'options' to wait must include
54 * There are two helper macros (they purpose it to mach more than one
55 * wait(2)-like function):
56 * The TWAIT_HAVE_STATUS - specifies whether a function can retrieve
57 * status (as integer value).
58 * The TWAIT_HAVE_PID - specifies whether a function can request
59 * exact process identifier
60 * The TWAIT_HAVE_RUSAGE - specifies whether a function can request
61 * the struct rusage value
65 #if defined(TWAIT_WAIT)
66 # define TWAIT_FNAME "wait"
67 # define TWAIT_WAIT4TYPE(a,b,c,d) wait((b))
68 # define TWAIT_GENERIC(a,b,c) wait((b))
69 # define TWAIT_HAVE_STATUS 1
70 #elif defined(TWAIT_WAITPID)
71 # define TWAIT_FNAME "waitpid"
72 # define TWAIT_WAIT4TYPE(a,b,c,d) waitpid((a),(b),(c))
73 # define TWAIT_GENERIC(a,b,c) waitpid((a),(b),(c))
74 # define TWAIT_HAVE_PID 1
75 # define TWAIT_HAVE_STATUS 1
76 #elif defined(TWAIT_WAITID)
77 # define TWAIT_FNAME "waitid"
78 # define TWAIT_GENERIC(a,b,c) \
79 waitid(P_PID,(a),NULL,(c)|WEXITED|WTRAPPED)
80 # define TWAIT_WAIT6TYPE(a,b,c,d,e,f) waitid((a),(b),(f),(d))
81 # define TWAIT_HAVE_PID 1
82 #elif defined(TWAIT_WAIT3)
83 # define TWAIT_FNAME "wait3"
84 # define TWAIT_WAIT4TYPE(a,b,c,d) wait3((b),(c),(d))
85 # define TWAIT_GENERIC(a,b,c) wait3((b),(c),NULL)
86 # define TWAIT_HAVE_STATUS 1
87 # define TWAIT_HAVE_RUSAGE 1
88 #elif defined(TWAIT_WAIT4)
89 # define TWAIT_FNAME "wait4"
90 # define TWAIT_WAIT4TYPE(a,b,c,d) wait4((a),(b),(c),(d))
91 # define TWAIT_GENERIC(a,b,c) wait4((a),(b),(c),NULL)
92 # define TWAIT_HAVE_PID 1
93 # define TWAIT_HAVE_STATUS 1
94 # define TWAIT_HAVE_RUSAGE 1
95 #elif defined(TWAIT_WAIT6)
96 # define TWAIT_FNAME "wait6"
97 # define TWAIT_WAIT6TYPE(a,b,c,d,e,f) wait6((a),(b),(c),(d),(e),(f))
98 # define TWAIT_GENERIC(a,b,c) \
99 wait6(P_PID,(a),(b),(c)|WEXITED|WTRAPPED,NULL,NULL)
100 # define TWAIT_HAVE_PID 1
101 # define TWAIT_HAVE_STATUS 1
105 * There are 3 groups of tests:
106 * - TWAIT_GENERIC() (wait, wait2, waitpid, wait3, wait4, wait6)
107 * - TWAIT_WAIT4TYPE() (wait2, waitpid, wait3, wait4)
108 * - TWAIT_WAIT6TYPE() (waitid, wait6)
110 * Tests only in the above categories are allowed. However some tests are not
111 * possible in the context requested functionality to be verified, therefore
112 * there are helper macros:
113 * - TWAIT_HAVE_PID (wait2, waitpid, waitid, wait4, wait6)
114 * - TWAIT_HAVE_STATUS (wait, wait2, waitpid, wait3, wait4, wait6)
115 * - TWAIT_HAVE_RUSAGE (wait3, wait4)
116 * - TWAIT_HAVE_RETPID (wait, wait2, waitpid, wait3, wait4, wait6)
118 * If there is an intention to test e.g. wait6(2) specific features in the
119 * ptrace(2) context, find the most matching group and with #ifdefs reduce
120 * functionality of less featured than wait6(2) interface (TWAIT_WAIT6TYPE).
122 * For clarity never use negative preprocessor checks, like:
123 * #if !defined(TWAIT_WAIT4)
124 * always refer to checks for positive values.
127 #define TEST_REQUIRE_EQ(x, y) \
129 uintmax_t vx = (x); \
130 uintmax_t vy = (y); \
131 int ret = vx == vy; \
133 ATF_REQUIRE_EQ_MSG(vx, vy, "%s(%ju) == %s(%ju)", \
135 } while (/*CONSTCOND*/0)
138 * A child process cannot call atf functions and expect them to magically
139 * work like in the parent.
140 * The printf(3) messaging from a child will not work out of the box as well
141 * without estabilishing a communication protocol with its parent. To not
142 * overcomplicate the tests - do not log from a child and use err(3)/errx(3)
143 * wrapped with FORKEE_ASSERT()/FORKEE_ASSERTX() as that is guaranteed to work.
145 #define FORKEE_ASSERT_EQ(x, y) \
147 uintmax_t vx = (x); \
148 uintmax_t vy = (y); \
149 int ret = vx == vy; \
151 errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: " \
152 "%s(%ju) == %s(%ju)", __FILE__, __LINE__, __func__, \
154 } while (/*CONSTCOND*/0)
156 #define FORKEE_ASSERTX(x) \
160 errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s",\
161 __FILE__, __LINE__, __func__, #x); \
162 } while (/*CONSTCOND*/0)
164 #define FORKEE_ASSERT(x) \
168 err(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s",\
169 __FILE__, __LINE__, __func__, #x); \
170 } while (/*CONSTCOND*/0)
173 * Simplify logic for functions using general purpose registers add HAVE_GPREGS
175 * For platforms that do not implement all needed calls for simplicity assume
176 * that they are unsupported at all.
178 #if defined(PT_GETREGS) \
179 && defined(PT_SETREGS) \
180 && defined(PTRACE_REG_PC) \
181 && defined(PTRACE_REG_SET_PC) \
182 && defined(PTRACE_REG_SP) \
183 && defined(PTRACE_REG_INTRV)
187 /* Add guards for floating point registers */
188 #if defined(PT_GETFPREGS) \
189 && defined(PT_SETFPREGS)
193 /* Add guards for cpu debug registers */
194 #if defined(PT_GETDBREGS) \
195 && defined(PT_SETDBREGS)
200 * If waitid(2) returns because one or more processes have a state change to
201 * report, 0 is returned. If an error is detected, a value of -1 is returned
202 * and errno is set to indicate the error. If WNOHANG is specified and there
203 * are no stopped, continued or exited children, 0 is returned.
205 #if defined(TWAIT_WAITID)
206 #define TWAIT_REQUIRE_SUCCESS(a,b) TEST_REQUIRE_EQ((a), 0)
207 #define TWAIT_REQUIRE_FAILURE(a,b) ATF_REQUIRE_ERRNO((a),(b) == -1)
208 #define FORKEE_REQUIRE_SUCCESS(a,b) FORKEE_ASSERT_EQ(a, 0)
209 #define FORKEE_REQUIRE_FAILURE(a,b) \
210 FORKEE_ASSERTX(((a) == errno) && ((b) == -1))
212 #define TWAIT_REQUIRE_SUCCESS(a,b) TEST_REQUIRE_EQ((a), (b))
213 #define TWAIT_REQUIRE_FAILURE(a,b) ATF_REQUIRE_ERRNO((a),(b) == -1)
214 #define FORKEE_REQUIRE_SUCCESS(a,b) FORKEE_ASSERT_EQ(a, b)
215 #define FORKEE_REQUIRE_FAILURE(a,b) \
216 FORKEE_ASSERTX(((a) == errno) && ((b) == -1))
220 * Helper tools to verify whether status reports exited value
222 #if TWAIT_HAVE_STATUS
224 validate_status_exited(int status, int expected)
226 ATF_REQUIRE_MSG(WIFEXITED(status), "Reported !exited process");
227 ATF_REQUIRE_MSG(!WIFCONTINUED(status), "Reported continued process");
228 ATF_REQUIRE_MSG(!WIFSIGNALED(status), "Reported signaled process");
229 ATF_REQUIRE_MSG(!WIFSTOPPED(status), "Reported stopped process");
231 ATF_REQUIRE_EQ_MSG(WEXITSTATUS(status), expected,
232 "The process has exited with invalid value %d != %d",
233 WEXITSTATUS(status), expected);
237 forkee_status_exited(int status, int expected)
239 FORKEE_ASSERTX(WIFEXITED(status));
240 FORKEE_ASSERTX(!WIFCONTINUED(status));
241 FORKEE_ASSERTX(!WIFSIGNALED(status));
242 FORKEE_ASSERTX(!WIFSTOPPED(status));
244 FORKEE_ASSERT_EQ(WEXITSTATUS(status), expected);
248 validate_status_continued(int status)
250 ATF_REQUIRE_MSG(!WIFEXITED(status), "Reported exited process");
251 ATF_REQUIRE_MSG(WIFCONTINUED(status), "Reported !continued process");
252 ATF_REQUIRE_MSG(!WIFSIGNALED(status), "Reported signaled process");
253 ATF_REQUIRE_MSG(!WIFSTOPPED(status), "Reported stopped process");
257 forkee_status_continued(int status)
259 FORKEE_ASSERTX(!WIFEXITED(status));
260 FORKEE_ASSERTX(WIFCONTINUED(status));
261 FORKEE_ASSERTX(!WIFSIGNALED(status));
262 FORKEE_ASSERTX(!WIFSTOPPED(status));
266 validate_status_signaled(int status, int expected_termsig, int expected_core)
268 ATF_REQUIRE_MSG(!WIFEXITED(status), "Reported exited process");
269 ATF_REQUIRE_MSG(!WIFCONTINUED(status), "Reported continued process");
270 ATF_REQUIRE_MSG(WIFSIGNALED(status), "Reported !signaled process");
271 ATF_REQUIRE_MSG(!WIFSTOPPED(status), "Reported stopped process");
273 ATF_REQUIRE_EQ_MSG(WTERMSIG(status), expected_termsig,
274 "Unexpected signal received");
276 ATF_REQUIRE_EQ_MSG(WCOREDUMP(status), expected_core,
277 "Unexpectedly core file %s generated", expected_core ? "not" : "");
281 forkee_status_signaled(int status, int expected_termsig, int expected_core)
283 FORKEE_ASSERTX(!WIFEXITED(status));
284 FORKEE_ASSERTX(!WIFCONTINUED(status));
285 FORKEE_ASSERTX(WIFSIGNALED(status));
286 FORKEE_ASSERTX(!WIFSTOPPED(status));
288 FORKEE_ASSERT_EQ(WTERMSIG(status), expected_termsig);
289 FORKEE_ASSERT_EQ(WCOREDUMP(status), expected_core);
293 validate_status_stopped(int status, int expected)
295 ATF_REQUIRE_MSG(!WIFEXITED(status), "Reported exited process");
296 ATF_REQUIRE_MSG(!WIFCONTINUED(status), "Reported continued process");
297 ATF_REQUIRE_MSG(!WIFSIGNALED(status), "Reported signaled process");
298 ATF_REQUIRE_MSG(WIFSTOPPED(status), "Reported !stopped process");
300 char st[128], ex[128];
301 strlcpy(st, strsignal(WSTOPSIG(status)), sizeof(st));
302 strlcpy(ex, strsignal(expected), sizeof(ex));
304 ATF_REQUIRE_EQ_MSG(WSTOPSIG(status), expected,
305 "Unexpected stop signal received [%s] != [%s]", st, ex);
309 forkee_status_stopped(int status, int expected)
311 FORKEE_ASSERTX(!WIFEXITED(status));
312 FORKEE_ASSERTX(!WIFCONTINUED(status));
313 FORKEE_ASSERTX(!WIFSIGNALED(status));
314 FORKEE_ASSERTX(WIFSTOPPED(status));
316 FORKEE_ASSERT_EQ(WSTOPSIG(status), expected);
319 #define validate_status_exited(a,b)
320 #define forkee_status_exited(a,b)
321 #define validate_status_continued(a,b)
322 #define forkee_status_continued(a,b)
323 #define validate_status_signaled(a,b,c)
324 #define forkee_status_signaled(a,b,c)
325 #define validate_status_stopped(a,b)
326 #define forkee_status_stopped(a,b)
329 /* This function is currently designed to be run in the main/parent process */
331 await_zombie(pid_t process)
333 struct kinfo_proc2 p;
334 size_t len = sizeof(p);
345 const size_t namelen = __arraycount(name);
347 /* Await the process becoming a zombie */
349 ATF_REQUIRE(sysctl(name, namelen, &p, &len, NULL, 0) == 0);
351 if (p.p_stat == LSZOMB)
354 ATF_REQUIRE(usleep(1000) == 0);
358 /* Happy number sequence -- this function is used to just consume cpu cycles */
359 #define HAPPY_NUMBER 1
361 /* If n is not happy then its sequence ends in the cycle:
362 * 4, 16, 37, 58, 89, 145, 42, 20, 4, ... */
365 /* Calculate the sum of the squares of the digits of n */
366 static unsigned __used
370 for (sum = 0; n; n /= 10) {
378 * XXX: Disabled optimization is required to make tests for hardware assisted
379 * traps in .text functional
381 * Tested with GCC 5.4 on NetBSD 7.99.47 amd64
385 __attribute__((__optnone__))
387 __attribute__((__optimize__("O0")))
389 check_happy(unsigned n)
392 unsigned total = dsum(n);
394 if (total == HAPPY_NUMBER)
396 if (total == SAD_NUMBER)
403 #if defined(TWAIT_HAVE_PID)
404 #define ATF_TP_ADD_TC_HAVE_PID(a,b) ATF_TP_ADD_TC(a,b)
406 #define ATF_TP_ADD_TC_HAVE_PID(a,b)
409 #if defined(HAVE_GPREGS)
410 #define ATF_TP_ADD_TC_HAVE_GPREGS(a,b) ATF_TP_ADD_TC(a,b)
412 #define ATF_TP_ADD_TC_HAVE_GPREGS(a,b)
415 #if defined(HAVE_FPREGS)
416 #define ATF_TP_ADD_TC_HAVE_FPREGS(a,b) ATF_TP_ADD_TC(a,b)
418 #define ATF_TP_ADD_TC_HAVE_FPREGS(a,b)
422 #define ATF_TP_ADD_TC_PT_STEP(a,b) ATF_TP_ADD_TC(a,b)
424 #define ATF_TP_ADD_TC_PT_STEP(a,b)
427 #if defined(__HAVE_PTRACE_WATCHPOINTS)
428 #define ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(a,b) ATF_TP_ADD_TC(a,b)
430 #define ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(a,b)