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