xref: /netbsd-src/tests/lib/libc/sys/t_ptrace_wait.h (revision fc5f862a3e9cd75845ddf32398d9397f0bf1597a)
1 /*	$NetBSD: t_ptrace_wait.h,v 1.36 2024/05/14 16:06:20 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2016, 2017, 2018, 2019 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 /* Detect plain wait(2) use-case */
30 #if !defined(TWAIT_WAITPID) && \
31     !defined(TWAIT_WAITID) && \
32     !defined(TWAIT_WAIT3) && \
33     !defined(TWAIT_WAIT4) && \
34     !defined(TWAIT_WAIT6)
35 #define TWAIT_WAIT
36 #endif
37 
38 /*
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
42  *
43  * The TWAIT_FNAME value is to be used for convenience in debug messages.
44  *
45  * The TWAIT_GENERIC() macro is designed to reuse the same unmodified
46  * code with as many wait(2)-like functions as possible.
47  *
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
52  * WEXITED|WTRUNCATED.
53  *
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
62  *
63  */
64 
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 #	define TWAIT_HAVE_OPTIONS		1
77 #elif defined(TWAIT_WAITID)
78 #	define TWAIT_FNAME			"waitid"
79 #	define TWAIT_GENERIC(a,b,c)		\
80 		waitid(P_PID,(a),NULL,(c)|WEXITED|WTRAPPED)
81 #	define TWAIT_WAIT6TYPE(a,b,c,d,e,f)	waitid((a),(b),(f),(d))
82 #	define TWAIT_HAVE_PID			1
83 #	define TWAIT_HAVE_OPTIONS		1
84 #elif defined(TWAIT_WAIT3)
85 #	define TWAIT_FNAME			"wait3"
86 #	define TWAIT_WAIT4TYPE(a,b,c,d)		wait3((b),(c),(d))
87 #	define TWAIT_GENERIC(a,b,c)		wait3((b),(c),NULL)
88 #	define TWAIT_HAVE_STATUS		1
89 #	define TWAIT_HAVE_RUSAGE		1
90 #	define TWAIT_HAVE_OPTIONS		1
91 #elif defined(TWAIT_WAIT4)
92 #	define TWAIT_FNAME			"wait4"
93 #	define TWAIT_WAIT4TYPE(a,b,c,d)		wait4((a),(b),(c),(d))
94 #	define TWAIT_GENERIC(a,b,c)		wait4((a),(b),(c),NULL)
95 #	define TWAIT_HAVE_PID			1
96 #	define TWAIT_HAVE_STATUS		1
97 #	define TWAIT_HAVE_RUSAGE		1
98 #	define TWAIT_HAVE_OPTIONS		1
99 #elif defined(TWAIT_WAIT6)
100 #	define TWAIT_FNAME			"wait6"
101 #	define TWAIT_WAIT6TYPE(a,b,c,d,e,f)	wait6((a),(b),(c),(d),(e),(f))
102 #	define TWAIT_GENERIC(a,b,c)		\
103 		wait6(P_PID,(a),(b),(c)|WEXITED|WTRAPPED,NULL,NULL)
104 #	define TWAIT_HAVE_PID			1
105 #	define TWAIT_HAVE_STATUS		1
106 #	define TWAIT_HAVE_OPTIONS		1
107 #endif
108 
109 /*
110  * There are 3 groups of tests:
111  * - TWAIT_GENERIC()	(wait, wait2, waitpid, wait3, wait4, wait6)
112  * - TWAIT_WAIT4TYPE()	(wait2, waitpid, wait3, wait4)
113  * - TWAIT_WAIT6TYPE()	(waitid, wait6)
114  *
115  * Tests only in the above categories are allowed. However some tests are not
116  * possible in the context requested functionality to be verified, therefore
117  * there are helper macros:
118  * - TWAIT_HAVE_PID	(wait2, waitpid, waitid, wait4, wait6)
119  * - TWAIT_HAVE_STATUS	(wait, wait2, waitpid, wait3, wait4, wait6)
120  * - TWAIT_HAVE_RUSAGE	(wait3, wait4)
121  * - TWAIT_HAVE_RETPID	(wait, wait2, waitpid, wait3, wait4, wait6)
122  *
123  * If there is an intention to test e.g. wait6(2) specific features in the
124  * ptrace(2) context, find the most matching group and with #ifdefs reduce
125  * functionality of less featured than wait6(2) interface (TWAIT_WAIT6TYPE).
126  *
127  * For clarity never use negative preprocessor checks, like:
128  *     #if !defined(TWAIT_WAIT4)
129  * always refer to checks for positive values.
130  */
131 
132 #define TEST_REQUIRE_EQ(x, y)						\
133 do {									\
134 	uintmax_t vx = (x);						\
135 	uintmax_t vy = (y);						\
136 	int ret = vx == vy;						\
137 	if (!ret)							\
138 		ATF_REQUIRE_EQ_MSG(vx, vy, "%s(%ju) == %s(%ju)", 	\
139 		    #x, vx, #y, vy);					\
140 } while (/*CONSTCOND*/0)
141 
142 /*
143  * A child process cannot call atf functions and expect them to magically
144  * work like in the parent.
145  * The printf(3) messaging from a child will not work out of the box as well
146  * without establishing a communication protocol with its parent. To not
147  * overcomplicate the tests - do not log from a child and use err(3)/errx(3)
148  * wrapped with FORKEE_ASSERT()/FORKEE_ASSERTX() as that is guaranteed to work.
149  */
150 #define FORKEE_ASSERT_EQ(x, y)						\
151 do {									\
152 	uintmax_t vx = (x);						\
153 	uintmax_t vy = (y);						\
154 	int ret = vx == vy;						\
155 	if (!ret)							\
156 		errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: "	\
157 		    "%s(%ju) == %s(%ju)", __FILE__, __LINE__, __func__,	\
158 		    #x, vx, #y, vy);					\
159 } while (/*CONSTCOND*/0)
160 
161 #define FORKEE_ASSERT_NEQ(x, y)						\
162 do {									\
163 	uintmax_t vx = (x);						\
164 	uintmax_t vy = (y);						\
165 	int ret = vx != vy;						\
166 	if (!ret)							\
167 		errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: "	\
168 		    "%s(%ju) != %s(%ju)", __FILE__, __LINE__, __func__,	\
169 		    #x, vx, #y, vy);					\
170 } while (/*CONSTCOND*/0)
171 
172 #define FORKEE_ASSERTX(x)						\
173 do {									\
174 	int ret = (x);							\
175 	if (!ret)							\
176 		errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s",\
177 		     __FILE__, __LINE__, __func__, #x);			\
178 } while (/*CONSTCOND*/0)
179 
180 #define FORKEE_ASSERT(x)						\
181 do {									\
182 	int ret = (x);							\
183 	if (!ret)							\
184 		err(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s",\
185 		     __FILE__, __LINE__, __func__, #x);			\
186 } while (/*CONSTCOND*/0)
187 
188 /*
189  * Simplify logic for functions using general purpose registers add HAVE_GPREGS
190  *
191  * For platforms that do not implement all needed calls for simplicity assume
192  * that they are unsupported at all.
193  */
194 #if defined(PT_GETREGS)			\
195     && defined(PT_SETREGS)		\
196     && defined(PTRACE_REG_PC)		\
197     && defined(PTRACE_REG_SET_PC)	\
198     && defined(PTRACE_REG_SP)		\
199     && defined(PTRACE_REG_INTRV)
200 #define HAVE_GPREGS
201 #endif
202 
203 /* Add guards for floating point registers */
204 #if defined(PT_GETFPREGS)		\
205     && defined(PT_SETFPREGS)
206 #define HAVE_FPREGS
207 #endif
208 
209 /* Add guards for cpu debug registers */
210 #if defined(PT_GETDBREGS)		\
211     && defined(PT_SETDBREGS)
212 #define HAVE_DBREGS
213 #endif
214 
215 /*
216  * If waitid(2) returns because one or more processes have a state change to
217  * report, 0 is returned.  If an error is detected, a value of -1 is returned
218  * and errno is set to indicate the error. If WNOHANG is specified and there
219  * are no stopped, continued or exited children, 0 is returned.
220  */
221 #if defined(TWAIT_WAITID)
222 #define TWAIT_REQUIRE_SUCCESS(a,b)	TEST_REQUIRE_EQ((a), 0)
223 #define TWAIT_REQUIRE_FAILURE(a,b)	ATF_REQUIRE_ERRNO((a),(b) == -1)
224 #define FORKEE_REQUIRE_SUCCESS(a,b)	FORKEE_ASSERT_EQ(a, 0)
225 #define FORKEE_REQUIRE_FAILURE(a,b)	\
226 	FORKEE_ASSERTX(((a) == errno) && ((b) == -1))
227 #else
228 #define TWAIT_REQUIRE_SUCCESS(a,b)	TEST_REQUIRE_EQ((a), (b))
229 #define TWAIT_REQUIRE_FAILURE(a,b)	ATF_REQUIRE_ERRNO((a),(b) == -1)
230 #define FORKEE_REQUIRE_SUCCESS(a,b)	FORKEE_ASSERT_EQ(a, b)
231 #define FORKEE_REQUIRE_FAILURE(a,b)	\
232 	FORKEE_ASSERTX(((a) == errno) && ((b) == -1))
233 #endif
234 
235 /*
236  * Helper tools to verify whether status reports exited value
237  */
238 #if TWAIT_HAVE_STATUS
239 static void __used
validate_status_exited(int status,int expected)240 validate_status_exited(int status, int expected)
241 {
242         ATF_REQUIRE_MSG(WIFEXITED(status), "Reported !exited process");
243         ATF_REQUIRE_MSG(!WIFCONTINUED(status), "Reported continued process");
244         ATF_REQUIRE_MSG(!WIFSIGNALED(status), "Reported signaled process");
245         ATF_REQUIRE_MSG(!WIFSTOPPED(status), "Reported stopped process");
246 
247 	ATF_REQUIRE_EQ_MSG(WEXITSTATUS(status), expected,
248 	    "The process has exited with invalid value %d != %d",
249 	    WEXITSTATUS(status), expected);
250 }
251 
252 static void __used
forkee_status_exited(int status,int expected)253 forkee_status_exited(int status, int expected)
254 {
255 	FORKEE_ASSERTX(WIFEXITED(status));
256 	FORKEE_ASSERTX(!WIFCONTINUED(status));
257 	FORKEE_ASSERTX(!WIFSIGNALED(status));
258 	FORKEE_ASSERTX(!WIFSTOPPED(status));
259 
260 	FORKEE_ASSERT_EQ(WEXITSTATUS(status), expected);
261 }
262 
263 static void __used
validate_status_continued(int status)264 validate_status_continued(int status)
265 {
266 	ATF_REQUIRE_MSG(!WIFEXITED(status), "Reported exited process");
267 	ATF_REQUIRE_MSG(WIFCONTINUED(status), "Reported !continued process");
268 	ATF_REQUIRE_MSG(!WIFSIGNALED(status), "Reported signaled process");
269 	ATF_REQUIRE_MSG(!WIFSTOPPED(status), "Reported stopped process");
270 }
271 
272 static void __used
forkee_status_continued(int status)273 forkee_status_continued(int status)
274 {
275 	FORKEE_ASSERTX(!WIFEXITED(status));
276 	FORKEE_ASSERTX(WIFCONTINUED(status));
277 	FORKEE_ASSERTX(!WIFSIGNALED(status));
278 	FORKEE_ASSERTX(!WIFSTOPPED(status));
279 }
280 
281 static void __used
validate_status_signaled(int status,int expected_termsig,int expected_core)282 validate_status_signaled(int status, int expected_termsig, int expected_core)
283 {
284 	ATF_REQUIRE_MSG(!WIFEXITED(status), "Reported exited process");
285 	ATF_REQUIRE_MSG(!WIFCONTINUED(status), "Reported continued process");
286 	ATF_REQUIRE_MSG(WIFSIGNALED(status), "Reported !signaled process");
287 	ATF_REQUIRE_MSG(!WIFSTOPPED(status), "Reported stopped process");
288 
289 	ATF_REQUIRE_EQ_MSG(WTERMSIG(status), expected_termsig,
290 	    "Unexpected signal received");
291 
292 	ATF_REQUIRE_EQ_MSG(!!WCOREDUMP(status), expected_core,
293 	    "Unexpectedly core file %s generated", expected_core ? "not" : "");
294 }
295 
296 static void __used
forkee_status_signaled(int status,int expected_termsig,int expected_core)297 forkee_status_signaled(int status, int expected_termsig, int expected_core)
298 {
299 	FORKEE_ASSERTX(!WIFEXITED(status));
300 	FORKEE_ASSERTX(!WIFCONTINUED(status));
301 	FORKEE_ASSERTX(WIFSIGNALED(status));
302 	FORKEE_ASSERTX(!WIFSTOPPED(status));
303 
304 	FORKEE_ASSERT_EQ(WTERMSIG(status), expected_termsig);
305 	FORKEE_ASSERT_EQ(!!WCOREDUMP(status), expected_core);
306 }
307 
308 static void __used
validate_status_stopped(int status,int expected)309 validate_status_stopped(int status, int expected)
310 {
311 	ATF_REQUIRE_MSG(!WIFEXITED(status), "Reported exited process");
312 	ATF_REQUIRE_MSG(!WIFCONTINUED(status), "Reported continued process");
313 	ATF_REQUIRE_MSG(!WIFSIGNALED(status), "Reported signaled process");
314 	ATF_REQUIRE_MSG(WIFSTOPPED(status), "Reported !stopped process");
315 
316 	char st[128], ex[128];
317 	strlcpy(st, strsignal(WSTOPSIG(status)), sizeof(st));
318 	strlcpy(ex, strsignal(expected), sizeof(ex));
319 
320 	ATF_REQUIRE_EQ_MSG(WSTOPSIG(status), expected,
321 	    "Unexpected stop signal received [%s] != [%s]", st, ex);
322 }
323 
324 static void __used
forkee_status_stopped(int status,int expected)325 forkee_status_stopped(int status, int expected)
326 {
327 	FORKEE_ASSERTX(!WIFEXITED(status));
328 	FORKEE_ASSERTX(!WIFCONTINUED(status));
329 	FORKEE_ASSERTX(!WIFSIGNALED(status));
330 	FORKEE_ASSERTX(WIFSTOPPED(status));
331 
332 	FORKEE_ASSERT_EQ(WSTOPSIG(status), expected);
333 }
334 #else
335 #define validate_status_exited(a,b)
336 #define forkee_status_exited(a,b)
337 #define validate_status_continued(a,b)
338 #define forkee_status_continued(a,b)
339 #define validate_status_signaled(a,b,c)
340 #define forkee_status_signaled(a,b,c)
341 #define validate_status_stopped(a,b)
342 #define forkee_status_stopped(a,b)
343 #endif
344 
345 /* This function is currently designed to be run in the main/parent process */
346 static void __used
await_zombie_raw(pid_t process,useconds_t ms)347 await_zombie_raw(pid_t process, useconds_t ms)
348 {
349 	struct kinfo_proc2 p;
350 	size_t len = sizeof(p);
351 
352 	const int name[] = {
353 		[0] = CTL_KERN,
354 		[1] = KERN_PROC2,
355 		[2] = KERN_PROC_PID,
356 		[3] = process,
357 		[4] = sizeof(p),
358 		[5] = 1
359 	};
360 
361 	const size_t namelen = __arraycount(name);
362 
363 	/* Await the process becoming a zombie */
364 	while(1) {
365 		ATF_REQUIRE(sysctl(name, namelen, &p, &len, NULL, 0) == 0);
366 
367 		if (p.p_stat == LSZOMB)
368 			break;
369 
370 		if (ms > 0) {
371 			ATF_REQUIRE(usleep(ms) == 0);
372 		}
373 	}
374 }
375 
376 static void __used
await_zombie(pid_t process)377 await_zombie(pid_t process)
378 {
379 
380 	await_zombie_raw(process, 1000);
381 }
382 
383 static void __used
await_stopped(pid_t process)384 await_stopped(pid_t process)
385 {
386 	struct kinfo_proc2 p;
387 	size_t len = sizeof(p);
388 
389 	const int name[] = {
390 		[0] = CTL_KERN,
391 		[1] = KERN_PROC2,
392 		[2] = KERN_PROC_PID,
393 		[3] = process,
394 		[4] = sizeof(p),
395 		[5] = 1
396 	};
397 
398 	const size_t namelen = __arraycount(name);
399 
400 	/* Await the process becoming a zombie */
401 	while(1) {
402 		ATF_REQUIRE(sysctl(name, namelen, &p, &len, NULL, 0) == 0);
403 
404 		if (p.p_stat == LSSTOP)
405 			break;
406 
407 		ATF_REQUIRE(usleep(1000) == 0);
408 	}
409 }
410 
411 static pid_t __used
await_stopped_child(pid_t process)412 await_stopped_child(pid_t process)
413 {
414 	struct kinfo_proc2 *p = NULL;
415 	size_t i, len;
416 	pid_t child = -1;
417 
418 	int name[] = {
419 		[0] = CTL_KERN,
420 		[1] = KERN_PROC2,
421 		[2] = KERN_PROC_ALL,
422 		[3] = 0,
423 		[4] = sizeof(struct kinfo_proc2),
424 		[5] = 0
425 	};
426 
427 	const size_t namelen = __arraycount(name);
428 
429 	/* Await the process becoming a zombie */
430 	while(1) {
431 		name[5] = 0;
432 
433 		FORKEE_ASSERT_EQ(sysctl(name, namelen, 0, &len, NULL, 0), 0);
434 
435 		FORKEE_ASSERT_EQ(reallocarr(&p,
436 		                            len,
437 		                            sizeof(struct kinfo_proc2)), 0);
438 
439 		name[5] = len;
440 
441 		FORKEE_ASSERT_EQ(sysctl(name, namelen, p, &len, NULL, 0), 0);
442 
443 		for (i = 0; i < len/sizeof(struct kinfo_proc2); i++) {
444 			if (p[i].p_pid == getpid())
445 				continue;
446 			if (p[i].p_ppid != process)
447 				continue;
448 			if (p[i].p_stat != LSSTOP)
449 				continue;
450 			child = p[i].p_pid;
451 			break;
452 		}
453 
454 		if (child != -1)
455 			break;
456 
457 		FORKEE_ASSERT_EQ(usleep(1000), 0);
458 	}
459 
460 	/* Free the buffer */
461 	FORKEE_ASSERT_EQ(reallocarr(&p, 0, sizeof(struct kinfo_proc2)), 0);
462 
463 	return child;
464 }
465 
466 static void __used
await_collected(pid_t process)467 await_collected(pid_t process)
468 {
469 	struct kinfo_proc2 p;
470 	size_t len = sizeof(p);
471 
472 	const int name[] = {
473 		[0] = CTL_KERN,
474 		[1] = KERN_PROC2,
475 		[2] = KERN_PROC_PID,
476 		[3] = process,
477 		[4] = sizeof(p),
478 		[5] = 1
479 	};
480 
481 	const size_t namelen = __arraycount(name);
482 
483 	/* Await the process to disappear */
484 	while(1) {
485 		FORKEE_ASSERT_EQ(sysctl(name, namelen, &p, &len, NULL, 0), 0);
486 		if (len == 0)
487 			break;
488 
489 		ATF_REQUIRE(usleep(1000) == 0);
490 	}
491 }
492 
493 /* Happy number sequence -- this function is used to just consume cpu cycles */
494 #define	HAPPY_NUMBER	1
495 
496 /* If n is not happy then its sequence ends in the cycle:
497  * 4, 16, 37, 58, 89, 145, 42, 20, 4, ... */
498 #define	SAD_NUMBER	4
499 
500 /* Calculate the sum of the squares of the digits of n */
501 static unsigned __used
dsum(unsigned n)502 dsum(unsigned n)
503 {
504 	unsigned sum, x;
505 	for (sum = 0; n; n /= 10) {
506 		x = n % 10;
507 		sum += x * x;
508 	}
509 	return sum;
510 }
511 
512 /*
513  * XXX: Disabled optimization is required to make tests for hardware assisted
514  * traps in .text functional
515  *
516  * Tested with GCC 5.4 on NetBSD 7.99.47 amd64
517  */
518 static int __used
519 #ifdef __clang__
520 __attribute__((__optnone__))
521 #else
522 __attribute__((__optimize__("O0")))
523 #endif
check_happy(unsigned n)524 check_happy(unsigned n)
525 {
526 	for (;;) {
527 		unsigned total = dsum(n);
528 
529 		if (total == HAPPY_NUMBER)
530 			return 1;
531 		if (total == SAD_NUMBER)
532 			return 0;
533 
534 		n = total;
535 	}
536 }
537 
538 static void * __used
infinite_thread(void * arg __unused)539 infinite_thread(void *arg __unused)
540 {
541 
542         while (true)
543                 continue;
544 
545         __unreachable();
546 }
547 
548 static int __used
clone_func(void * arg)549 clone_func(void *arg)
550 {
551 	int ret;
552 
553 	ret = (int)(intptr_t)arg;
554 
555 	return ret;
556 }
557 
558 #if defined(HAVE_DBREGS)
559 static bool __used
can_we_set_dbregs(void)560 can_we_set_dbregs(void)
561 {
562 	static long euid = -1;
563 	static int user_set_dbregs  = -1;
564 	size_t user_set_dbregs_len = sizeof(user_set_dbregs);
565 
566 	if (euid == -1)
567 		euid = geteuid();
568 
569 	if (euid == 0)
570 		return true;
571 
572 	if (user_set_dbregs == -1) {
573 		if (sysctlbyname("security.models.extensions.user_set_dbregs",
574 			&user_set_dbregs, &user_set_dbregs_len, NULL, 0)
575 			== -1) {
576 			return false;
577 		}
578 	}
579 
580 	if (user_set_dbregs > 0)
581 		return true;
582 	else
583 		return false;
584 }
585 #endif
586 
587 static bool __used
get_user_va0_disable(void)588 get_user_va0_disable(void)
589 {
590 	static int user_va0_disable = -1;
591 	size_t user_va0_disable_len = sizeof(user_va0_disable);
592 
593 	if (user_va0_disable == -1) {
594 		if (sysctlbyname("vm.user_va0_disable",
595 			&user_va0_disable, &user_va0_disable_len, NULL, 0)
596 			== -1) {
597 			return true;
598 		}
599 	}
600 
601 	if (user_va0_disable > 0)
602 		return true;
603 	else
604 		return false;
605 }
606 
607 static bool __used
can_we_write_to_text(pid_t pid)608 can_we_write_to_text(pid_t pid)
609 {
610 	int mib[3];
611 	int paxflags;
612 	size_t len = sizeof(int);
613 
614 	mib[0] = CTL_PROC;
615 	mib[1] = pid;
616 	mib[2] = PROC_PID_PAXFLAGS;
617 
618 	if (sysctl(mib, 3, &paxflags, &len, NULL, 0) == -1)
619 		return false;
620 
621 	return !(paxflags & CTL_PROC_PAXFLAGS_MPROTECT);
622 }
623 
624 static void __used
trigger_trap(void)625 trigger_trap(void)
626 {
627 
628 	/* Software breakpoint causes CPU trap, translated to SIGTRAP */
629 #ifdef PTRACE_BREAKPOINT_ASM
630 	PTRACE_BREAKPOINT_ASM;
631 #else
632 	/* port me */
633 #endif
634 }
635 
636 static void __used
trigger_segv(void)637 trigger_segv(void)
638 {
639 	static volatile char *ptr = NULL;
640 
641 	/* Access to unmapped memory causes CPU trap, translated to SIGSEGV */
642 	*ptr = 1;
643 }
644 
645 static void __used
trigger_ill(void)646 trigger_ill(void)
647 {
648 
649 	/* Illegal instruction causes CPU trap, translated to SIGILL */
650 #ifdef PTRACE_ILLEGAL_ASM
651 #ifndef __mips__ /* To avoid GXemul crash */
652 	PTRACE_ILLEGAL_ASM;
653 #endif
654 #else
655 	/* port me */
656 #endif
657 }
658 
659 #include <fenv.h>
660 
661 #if (__arm__ && !__SOFTFP__) || __aarch64__
662 #include <ieeefp.h> /* only need for ARM Cortex/Neon hack */
663 
664 static bool __used
are_fpu_exceptions_supported(void)665 are_fpu_exceptions_supported(void)
666 {
667 	/*
668 	 * Some NEON fpus do not trap on IEEE 754 FP exceptions.
669 	 * Skip these tests if running on them and compiled for
670 	 * hard float.
671 	 */
672 	if (0 == fpsetmask(fpsetmask(FP_X_INV)))
673 		return false;
674 	return true;
675 }
676 #elif defined __riscv__
677 #define are_fpu_exceptions_supported() 0
678 #else
679 #define are_fpu_exceptions_supported() 1
680 #endif
681 
682 volatile double ignore_result;
683 
684 static void __used
trigger_fpe(void)685 trigger_fpe(void)
686 {
687 #if __i386__ || __x86_64__
688 	/*
689 	 * XXX
690 	 * Hack for QEMU bug #1668041, by which floating-point division by
691 	 * zero is not trapped correctly. Also, assertions for si_code in
692 	 * ptrace_signal_wait.h are commented out. Clean them up after the
693 	 * bug is fixed.
694 	 */
695 	volatile int a, b;
696 #else
697 	volatile double a, b;
698 #endif
699 
700 	a = getpid();
701 	b = atoi("0");
702 
703 #ifdef __HAVE_FENV
704 	feenableexcept(FE_ALL_EXCEPT);
705 #endif
706 
707 	/* Division by zero causes CPU trap, translated to SIGFPE */
708 	ignore_result = (int)(a / b);
709 }
710 
711 static void __used
trigger_bus(void)712 trigger_bus(void)
713 {
714 	FILE *fp;
715 	char *p;
716 
717 	/* Open an empty file for writing. */
718 	fp = tmpfile();
719 	FORKEE_ASSERT_NEQ((uintptr_t)fp, (uintptr_t)NULL);
720 
721 	/*
722 	 * Map an empty file with mmap(2) to a pointer.
723 	 *
724 	 * PROT_READ handles read-modify-write sequences emitted for
725 	 * certain combinations of CPUs and compilers (e.g. Alpha AXP).
726 	 */
727 	p = mmap(0, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(fp), 0);
728 	FORKEE_ASSERT_NEQ((uintptr_t)p, (uintptr_t)MAP_FAILED);
729 
730 	/* Invalid memory access causes CPU trap, translated to SIGBUS */
731 	*p = 'a';
732 }
733 
734 struct lwp_event_count {
735 	lwpid_t lec_lwp;
736 	int lec_count;
737 };
738 
739 static int * __used
find_event_count(struct lwp_event_count list[],lwpid_t lwp,size_t max_lwps)740 find_event_count(struct lwp_event_count list[], lwpid_t lwp, size_t max_lwps)
741 {
742 	size_t i;
743 
744 	for (i = 0; i < max_lwps; i++) {
745 		if (list[i].lec_lwp == 0)
746 			list[i].lec_lwp = lwp;
747 		if (list[i].lec_lwp == lwp)
748 			return &list[i].lec_count;
749 	}
750 
751 	atf_tc_fail("More LWPs reported than expected");
752 }
753 
754 #define FIND_EVENT_COUNT(list, lwp)			\
755 	find_event_count(list, lwp, __arraycount(list))
756 
757 
758 #if defined(TWAIT_HAVE_PID)
759 #define ATF_TP_ADD_TC_HAVE_PID(a,b)	ATF_TP_ADD_TC(a,b)
760 #else
761 #define ATF_TP_ADD_TC_HAVE_PID(a,b)
762 #endif
763 
764 #if defined(TWAIT_HAVE_STATUS)
765 #define ATF_TP_ADD_TC_HAVE_STATUS(a,b)	ATF_TP_ADD_TC(a,b)
766 #else
767 #define ATF_TP_ADD_TC_HAVE_STATUS(a,b)
768 #endif
769 
770 #if defined(TWAIT_HAVE_STATUS) && (defined(__i386__) || defined(__x86_64__))
771 #define ATF_TP_ADD_TC_HAVE_STATUS_X86(a,b)	ATF_TP_ADD_TC(a,b)
772 #else
773 #define ATF_TP_ADD_TC_HAVE_STATUS_X86(a,b)
774 #endif
775 
776 #if defined(HAVE_GPREGS)
777 #define ATF_TP_ADD_TC_HAVE_GPREGS(a,b)	ATF_TP_ADD_TC(a,b)
778 #else
779 #define ATF_TP_ADD_TC_HAVE_GPREGS(a,b)
780 #endif
781 
782 #if defined(HAVE_FPREGS)
783 #define ATF_TP_ADD_TC_HAVE_FPREGS(a,b)	ATF_TP_ADD_TC(a,b)
784 #else
785 #define ATF_TP_ADD_TC_HAVE_FPREGS(a,b)
786 #endif
787 
788 #if defined(HAVE_DBREGS)
789 #define ATF_TP_ADD_TC_HAVE_DBREGS(a,b) ATF_TP_ADD_TC(a,b)
790 #else
791 #define ATF_TP_ADD_TC_HAVE_DBREGS(a,b)
792 #endif
793 
794 #if defined(PT_STEP)
795 #define ATF_TP_ADD_TC_PT_STEP(a,b)	ATF_TP_ADD_TC(a,b)
796 #else
797 #define ATF_TP_ADD_TC_PT_STEP(a,b)
798 #endif
799