1 /* Tests for MINIX3 ptrace(2) - by D.C. van Moolenbroek */
2 #define _POSIX_SOURCE 1
11 #include <sys/select.h>
12 #include <sys/ptrace.h>
17 #define _WIFSTOPPED(s) (WIFSTOPPED(s) && !WIFSIGNALED(s) && !WIFEXITED(s))
18 #define _WIFSIGNALED(s) (!WIFSTOPPED(s) && WIFSIGNALED(s) && !WIFEXITED(s))
19 #define _WIFEXITED(s) (!WIFSTOPPED(s) && !WIFSIGNALED(s) && WIFEXITED(s))
21 #define timed_test(func) (timed_test_func(#func, func));
23 _PROTOTYPE(int main
, (int argc
, char **argv
));
24 _PROTOTYPE(void test
, (int m
, int a
));
25 _PROTOTYPE(void timed_test_func
, (const char *s
, void (* func
)(void)));
26 _PROTOTYPE(void timed_test_timeout
, (int signum
));
27 _PROTOTYPE(pid_t traced_fork
, (_PROTOTYPE(void (*c
), (void))));
28 _PROTOTYPE(pid_t traced_pfork
, (_PROTOTYPE(void (*c
), (void))));
29 _PROTOTYPE(void WRITE
, (int value
));
30 _PROTOTYPE(int READ
, (void));
31 _PROTOTYPE(void traced_wait
, (void));
32 _PROTOTYPE(void detach_running
, (pid_t pid
));
33 _PROTOTYPE(void dummy_handler
, (int sig
));
34 _PROTOTYPE(void exit_handler
, (int sig
));
35 _PROTOTYPE(void count_handler
, (int sig
));
36 _PROTOTYPE(void catch_handler
, (int sig
));
37 _PROTOTYPE(void test_wait_child
, (void));
38 _PROTOTYPE(void test_wait
, (void));
39 _PROTOTYPE(void test_exec_child
, (void));
40 _PROTOTYPE(void test_exec
, (void));
41 _PROTOTYPE(void test_step_child
, (void));
42 _PROTOTYPE(void test_step
, (void));
43 _PROTOTYPE(void test_sig_child
, (void));
44 _PROTOTYPE(void test_sig
, (void));
45 _PROTOTYPE(void test_exit_child
, (void));
46 _PROTOTYPE(void test_exit
, (void));
47 _PROTOTYPE(void test_term_child
, (void));
48 _PROTOTYPE(void test_term
, (void));
49 _PROTOTYPE(void test_catch_child
, (void));
50 _PROTOTYPE(void test_catch
, (void));
51 _PROTOTYPE(void test_kill_child
, (void));
52 _PROTOTYPE(void test_kill
, (void));
53 _PROTOTYPE(void test_attach_child
, (void));
54 _PROTOTYPE(void test_attach
, (void));
55 _PROTOTYPE(void test_detach_child
, (void));
56 _PROTOTYPE(void test_detach
, (void));
57 _PROTOTYPE(void test_death_child
, (void));
58 _PROTOTYPE(void test_death
, (void));
59 _PROTOTYPE(void test_zdeath_child
, (void));
60 _PROTOTYPE(void test_zdeath
, (void));
61 _PROTOTYPE(void test_syscall_child
, (void));
62 _PROTOTYPE(void test_syscall
, (void));
63 _PROTOTYPE(void test_tracefork_child
, (void));
64 _PROTOTYPE(void test_tracefork
, (void));
65 _PROTOTYPE(void sigexec
, (int setflag
, int opt
, int *traps
, int *stop
));
66 _PROTOTYPE(void test_trapexec
, (void));
67 _PROTOTYPE(void test_altexec
, (void));
68 _PROTOTYPE(void test_noexec
, (void));
69 _PROTOTYPE(void test_defexec
, (void));
70 _PROTOTYPE(void test_reattach_child
, (void));
71 _PROTOTYPE(void test_reattach
, (void));
72 _PROTOTYPE(void e
, (int n
));
73 _PROTOTYPE(void quit
, (void));
75 static char *executable
;
76 static int errct
= 0, subtest
;
77 static int child
= 0, attach
;
80 static int sigs
, caught
;
86 int i
, m
= 0xFFFFFF, n
= 0xF;
88 if (strcmp(argv
[0], "DO CHECK") == 0) {
97 if (argc
>= 2) m
= atoi(argv
[1]);
98 if (argc
>= 3) n
= atoi(argv
[2]);
100 for (i
= 0; i
< ITERATIONS
; i
++) {
101 if (n
& 001) test(m
, 0);
102 if (n
& 002) test(m
, 1);
103 if (n
& 004) test(m
, 2);
104 if (n
& 010) test(m
, 3);
108 return(-1); /* impossible */
117 if (m
& 00000001) timed_test(test_wait
);
118 if (m
& 00000002) timed_test(test_exec
);
119 if (m
& 00000004) timed_test(test_step
);
120 if (m
& 00000010) timed_test(test_sig
);
121 if (m
& 00000020) timed_test(test_exit
);
122 if (m
& 00000040) timed_test(test_term
);
123 if (m
& 00000100) timed_test(test_catch
);
124 if (m
& 00000200) timed_test(test_kill
);
125 if (m
& 00000400) timed_test(test_attach
);
126 if (m
& 00001000) timed_test(test_detach
);
127 if (m
& 00002000) timed_test(test_death
);
128 if (m
& 00004000) timed_test(test_zdeath
);
129 if (m
& 00010000) timed_test(test_syscall
);
130 if (m
& 00020000) timed_test(test_tracefork
);
131 if (m
& 00040000) timed_test(test_trapexec
);
132 if (m
& 00100000) timed_test(test_altexec
);
133 if (m
& 00200000) timed_test(test_noexec
);
134 if (m
& 00400000) timed_test(test_defexec
);
135 if (m
& 01000000) test_reattach(); /* not timed, catches SIGALRM */
138 static jmp_buf timed_test_context
;
140 void timed_test_timeout(int signum
)
142 longjmp(timed_test_context
, -1);
148 void timed_test_func(const char *s
, void (* func
)(void))
150 if (setjmp(timed_test_context
) == 0)
152 /* the function gets 60 seconds to complete */
153 if (signal(SIGALRM
, timed_test_timeout
) == SIG_ERR
) { e(701); return; }
160 /* report timeout as error */
161 printf("timeout in %s\n", s
);
167 _PROTOTYPE(void (*c
), (void));
172 if (pipe(pfd
) != 0) e(200);
173 if (pipe(&pfd
[2]) != 0) e(201);
176 case 0: /* let child volunteer to be traced */
184 if (ptrace(T_OK
, 0, 0, 0) != 0) e(203);
193 if (READ() != 0) e(205);
197 case 1: /* attach to child process */
205 if (READ() != 0) e(207);
212 if (ptrace(T_ATTACH
, pid
, 0, 0) != 0) e(209);
214 if (waitpid(pid
, &status
, 0) != pid
) e(210);
215 if (!_WIFSTOPPED(status
)) e(211);
216 if (WSTOPSIG(status
) != SIGSTOP
) e(212);
218 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) e(213);
224 case 2: /* attach to non-child process */
227 if (ppid
< 0) e(214);
232 if (pid
< 0) exit(215);
237 if (READ() != 0) e(216);
248 if (waitpid(pid
, &status
, 0) != pid
) e(218);
249 if (_WIFSTOPPED(status
)) e(219);
250 if (_WIFEXITED(status
) && (r
= WEXITSTATUS(status
)) != 42) e(r
);
257 if (ptrace(T_ATTACH
, pid
, 0, 0) != 0) e(220);
259 if (waitpid(pid
, &status
, 0) != pid
) e(221);
260 if (!_WIFSTOPPED(status
)) e(222);
261 if (WSTOPSIG(status
) != SIGSTOP
) e(223);
263 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) e(224);
269 case 3: /* attach by forking from child */
272 if (ppid
< 0) e(225);
277 if (ptrace(T_OK
, 0, 0, 0) != 0) e(226);
281 if (READ() != 0) e(227);
295 if (waitpid(pid
, &status
, 0) != pid
) e(230);
296 if (_WIFSTOPPED(status
)) e(231);
297 if (_WIFEXITED(status
) && (r
= WEXITSTATUS(status
)) != 42) e(r
);
302 if (READ() != 0) e(232);
304 if (kill(ppid
, SIGSTOP
) != 0) e(233);
306 if (waitpid(ppid
, &status
, 0) != ppid
) e(234);
307 if (!_WIFSTOPPED(status
)) e(235);
308 if (WSTOPSIG(status
) != SIGSTOP
) e(236);
310 if (ptrace(T_SETOPT
, ppid
, 0, TO_TRACEFORK
) != 0) e(237);
312 if (ptrace(T_RESUME
, ppid
, 0, 0) != 0) e(238);
318 if (waitpid(pid
, &status
, 0) != pid
) e(239);
319 if (!_WIFSTOPPED(status
)) e(240);
320 if (WSTOPSIG(status
) != SIGSTOP
) e(241);
322 if (ptrace(T_SETOPT
, pid
, 0, 0) != 0) e(242);
323 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) e(243);
325 detach_running(ppid
);
333 pid_t
traced_pfork(c
)
334 _PROTOTYPE(void (*c
), (void));
338 if (pipe(pfd
) != 0) e(300);
339 if (pipe(&pfd
[2]) != 0) e(301);
359 if (write(pfd
[child
*2+1], &value
, sizeof(value
)) != sizeof(value
)) e(400);
366 if (read(pfd
[2-child
*2], &value
, sizeof(value
)) != sizeof(value
)) e(401);
376 if (waitpid(ppid
, &status
, 0) != ppid
) e(500);
377 if (!_WIFEXITED(status
)) e(501);
378 if ((r
= WEXITSTATUS(status
)) != 0) e(r
);
381 /* Quick hack to clean up detached children */
382 waitpid(-1, NULL
, WNOHANG
);
391 void detach_running(pid
)
394 /* Detach from a process that is not already stopped. This is the way to do it.
395 * We have to stop the child in order to detach from it, but as the child may
396 * have other signals pending for the tracer, we cannot assume we get our own
397 * signal back immediately. However, because we know that the kill is instant
398 * and resuming with pending signals will only stop the process immediately
399 * again, we can use T_RESUME for all the signals until we get our own signal,
400 * and then detach. A complicating factor is that anywhere during this
401 * procedure, the child may die (e.g. by getting a SIGKILL). In our tests, this
406 if (kill(pid
, SIGSTOP
) != 0) e(600);
408 if (waitpid(pid
, &status
, 0) != pid
) e(601);
410 while (_WIFSTOPPED(status
)) {
411 if (WSTOPSIG(status
) == SIGSTOP
) {
412 if (ptrace(T_DETACH
, pid
, 0, 0) != 0) e(602);
417 if (ptrace(T_RESUME
, pid
, 0, WSTOPSIG(status
)) != 0) e(603);
419 if (waitpid(pid
, &status
, 0) != pid
) e(604);
422 /* Apparently the process exited. */
423 if (!_WIFEXITED(status
) && !_WIFSIGNALED(status
)) e(605);
425 /* In our tests, that should not happen. */
429 void dummy_handler(sig
)
434 void exit_handler(sig
)
440 void count_handler(sig
)
446 void catch_handler(sig
)
453 case SIGUSR1
: bit
= 1; break;
454 case SIGUSR2
: bit
= 2; break;
455 case SIGTERM
: bit
= 4; break;
460 sigprocmask(SIG_SETMASK
, &set
, NULL
);
462 if (caught
& bit
) e(101);
466 void test_wait_child()
478 pid
= traced_fork(test_wait_child
);
480 if (waitpid(pid
, &status
, 0) != pid
) e(1);
481 if (!_WIFEXITED(status
)) e(2);
482 if (WEXITSTATUS(status
) != 42) e(3);
487 void test_exec_child()
489 if (READ() != 0) e(100);
491 execl(executable
, "DO CHECK", NULL
);
501 /* This test covers the T_OK case. */
502 if (attach
!= 0) return;
506 pid
= traced_fork(test_exec_child
);
510 /* An exec() should result in a trap signal. */
511 if (waitpid(pid
, &status
, 0) != pid
) e(1);
512 if (!_WIFSTOPPED(status
)) e(2);
513 if (WSTOPSIG(status
) != SIGTRAP
) e(3);
515 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) e(4);
517 if (waitpid(pid
, &status
, 0) != pid
) e(5);
518 if (!_WIFEXITED(status
)) e(6);
519 if ((r
= WEXITSTATUS(status
)) != 42) e(r
);
524 void test_step_child()
528 signal(SIGUSR1
, SIG_IGN
);
532 if (READ() != 0) e(100);
534 /* It must not be possible for the child to stop the single-step signal. */
535 signal(SIGTRAP
, SIG_IGN
);
537 sigprocmask(SIG_SETMASK
, &set
, NULL
);
545 int r
, status
, count
;
549 pid
= traced_fork(test_step_child
);
551 if (READ() != 0) e(1);
553 /* While the child is running, neither waitpid() nor ptrace() should work. */
554 if (waitpid(pid
, &status
, WNOHANG
) != 0) e(2);
555 if (ptrace(T_RESUME
, pid
, 0, 0) != -1) e(3);
556 if (errno
!= EBUSY
) e(4);
558 if (kill(pid
, SIGUSR1
) != 0) e(5);
562 /* A kill() signal (other than SIGKILL) should be delivered to the tracer. */
563 if (waitpid(pid
, &status
, 0) != pid
) e(6);
564 if (!_WIFSTOPPED(status
)) e(7);
565 if (WSTOPSIG(status
) != SIGUSR1
) e(8);
567 /* ptrace(T_STEP) should result in instruction-wise progress. */
568 for (count
= 0; ; count
++) {
569 if (ptrace(T_STEP
, pid
, 0, 0) != 0) e(9);
571 if (waitpid(pid
, &status
, 0) != pid
) e(10);
572 if (_WIFEXITED(status
)) break;
573 if (!_WIFSTOPPED(status
)) e(11);
574 if (WSTOPSIG(status
) != SIGTRAP
) e(12);
577 if ((r
= WEXITSTATUS(status
)) != 42) e(r
);
579 if (count
< 10) e(13); /* in practice: hundreds */
584 void test_sig_child()
586 signal(SIGUSR1
, exit_handler
);
588 if (READ() != 0) e(100);
602 pid
= traced_fork(test_sig_child
);
606 /* allow the child to enter the pause */
609 if (kill(pid
, SIGUSR1
) != 0) e(1);
610 if (kill(pid
, SIGUSR2
) != 0) e(2);
612 /* All signals should arrive at the tracer, although in "random" order. */
613 if (waitpid(pid
, &status
, 0) != pid
) e(3);
614 if (!_WIFSTOPPED(status
)) e(4);
615 if (WSTOPSIG(status
) != SIGUSR1
&& WSTOPSIG(status
) != SIGUSR2
) e(5);
617 /* The tracer should see kills arriving while the tracee is stopped. */
618 if (kill(pid
, WSTOPSIG(status
)) != 0) e(6);
620 if (waitpid(pid
, &status
, WNOHANG
) != pid
) e(7);
621 if (!_WIFSTOPPED(status
)) e(8);
622 if (WSTOPSIG(status
) != SIGUSR1
&& WSTOPSIG(status
) != SIGUSR2
) e(9);
623 sig
= (WSTOPSIG(status
) == SIGUSR1
) ? SIGUSR2
: SIGUSR1
;
625 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) e(10);
627 if (waitpid(pid
, &status
, 0) != pid
) e(11);
628 if (!_WIFSTOPPED(status
)) e(12);
629 if (WSTOPSIG(status
) != sig
) e(13);
631 if (waitpid(pid
, &status
, WNOHANG
) != 0) e(14);
633 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) e(15);
635 /* Ignored signals passed via ptrace() should be ignored. */
636 if (kill(pid
, SIGUSR1
) != 0) e(16);
638 if (waitpid(pid
, &status
, 0) != pid
) e(17);
639 if (!_WIFSTOPPED(status
)) e(18);
640 if (WSTOPSIG(status
) != SIGUSR1
) e(19);
642 if (ptrace(T_RESUME
, pid
, 0, SIGCHLD
) != 0) e(20);
644 /* if the pause has been aborted (shouldn't happen!), let the child exit */
647 if (waitpid(pid
, &status
, WNOHANG
) != 0) e(21);
649 /* Caught signals passed via ptrace() should invoke their signal handlers. */
650 if (kill(pid
, SIGUSR1
) != 0) e(22);
652 if (waitpid(pid
, &status
, 0) != pid
) e(23);
653 if (!_WIFSTOPPED(status
)) e(24);
654 if (WSTOPSIG(status
) != SIGUSR1
) e(25);
656 if (ptrace(T_RESUME
, pid
, 0, SIGUSR1
) != 0) e(26);
658 if (waitpid(pid
, &status
, 0) != pid
) e(27);
659 if (!_WIFEXITED(status
)) e(28);
660 if ((r
= WEXITSTATUS(status
)) != 42) e(29);
665 void test_exit_child()
679 pid
= traced_fork(test_exit_child
);
681 if (READ() != 0) e(1);
685 if (kill(pid
, SIGSTOP
) != 0) e(2);
687 if (waitpid(pid
, &status
, 0) != pid
) e(3);
688 if (!_WIFSTOPPED(status
)) e(4);
689 if (WSTOPSIG(status
) != SIGSTOP
) e(5);
691 /* There should be no more signals pending for the tracer now. */
692 if (waitpid(pid
, &status
, WNOHANG
) != 0) e(6);
694 /* ptrace(T_EXIT) should terminate the process with the given exit value. */
695 if (ptrace(T_EXIT
, pid
, 0, 42) != 0) e(7);
697 if (waitpid(pid
, &status
, 0) != pid
) e(8);
698 if (!_WIFEXITED(status
)) e(9);
699 if ((r
= WEXITSTATUS(status
)) != 42) e(r
);
704 void test_term_child()
706 signal(SIGUSR1
, SIG_DFL
);
707 signal(SIGUSR2
, dummy_handler
);
723 pid
= traced_fork(test_term_child
);
725 if (READ() != 0) e(1);
727 /* If the first of two signals terminates the traced child, the second signal
728 * may or may not be delivered to the tracer - this is merely a policy issue.
729 * However, nothing unexpected should happen.
731 if (kill(pid
, SIGUSR1
) != 0) e(2);
732 if (kill(pid
, SIGUSR2
) != 0) e(3);
734 if (waitpid(pid
, &status
, 0) != pid
) e(4);
735 if (!_WIFSTOPPED(status
)) e(5);
737 if (ptrace(T_RESUME
, pid
, 0, SIGUSR1
) != 0) e(6);
739 if (waitpid(pid
, &status
, 0) != pid
) e(7);
741 if (_WIFSTOPPED(status
)) {
742 if (ptrace(T_RESUME
, pid
, 0, SIGUSR1
) != 0) e(8);
744 if (waitpid(pid
, &status
, 0) != pid
) e(9);
747 if (!_WIFSIGNALED(status
)) e(10);
748 if (WTERMSIG(status
) != SIGUSR1
) e(11);
753 void test_catch_child()
758 sa
.sa_handler
= catch_handler
;
759 sigemptyset(&sa
.sa_mask
);
760 sa
.sa_flags
= SA_NODEFER
;
762 sigaction(SIGUSR1
, &sa
, NULL
);
763 sigaction(SIGUSR2
, &sa
, NULL
);
764 sigaction(SIGTERM
, &sa
, NULL
);
767 sigprocmask(SIG_SETMASK
, &set
, &oset
);
773 while (caught
!= 7) sigsuspend(&oset
);
785 pid
= traced_fork(test_catch_child
);
787 if (READ() != 0) e(1);
789 if (kill(pid
, SIGUSR1
) != 0) e(2);
790 if (kill(pid
, SIGUSR2
) != 0) e(3);
792 if (waitpid(pid
, &status
, 0) != pid
) e(4);
793 if (!_WIFSTOPPED(status
)) e(5);
794 if (WSTOPSIG(status
) != SIGUSR1
&& WSTOPSIG(status
) != SIGUSR2
) e(6);
795 sig
= (WSTOPSIG(status
) == SIGUSR1
) ? SIGUSR2
: SIGUSR1
;
797 if (ptrace(T_RESUME
, pid
, 0, WSTOPSIG(status
)) != 0) e(7);
799 if (kill(pid
, SIGTERM
) != 0) e(8);
801 if (waitpid(pid
, &status
, 0) != pid
) e(9);
802 if (!_WIFSTOPPED(status
)) e(10);
803 if (WSTOPSIG(status
) != sig
&& WSTOPSIG(status
) != SIGTERM
) e(11);
804 if (WSTOPSIG(status
) == sig
) sig
= SIGTERM
;
806 if (ptrace(T_RESUME
, pid
, 0, WSTOPSIG(status
)) != 0) e(12);
808 if (kill(pid
, SIGBUS
) != 0) e(13);
810 if (waitpid(pid
, &status
, 0) != pid
) e(14);
811 if (!_WIFSTOPPED(status
)) e(15);
812 if (WSTOPSIG(status
) != sig
&& WSTOPSIG(status
) != SIGBUS
) e(16);
814 if (ptrace(T_RESUME
, pid
, 0, sig
) != 0) e(17);
816 if (WSTOPSIG(status
) == sig
) sig
= SIGBUS
;
818 if (waitpid(pid
, &status
, 0) != pid
) e(18);
819 if (!_WIFSTOPPED(status
)) e(19);
820 if (WSTOPSIG(status
) != sig
) e(20);
822 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) e(21);
824 if (waitpid(pid
, &status
, 0) != pid
) e(22);
825 if (!_WIFEXITED(status
)) e(23);
826 if ((r
= WEXITSTATUS(status
)) != 42) e(r
);
831 void test_kill_child()
835 signal(SIGKILL
, SIG_IGN
);
837 sigprocmask(SIG_SETMASK
, &set
, NULL
);
853 pid
= traced_fork(test_kill_child
);
855 if (READ() != 0) e(1);
857 /* SIGKILL must be unstoppable in every way. */
858 if (kill(pid
, SIGKILL
) != 0) e(2);
860 if (waitpid(pid
, &status
, 0) != pid
) e(3);
861 if (!_WIFSIGNALED(status
)) e(4);
862 if (WTERMSIG(status
) != SIGKILL
) e(5);
864 /* After termination, the child must no longer be visible to the tracer. */
865 if (waitpid(pid
, &status
, WNOHANG
) != -1) e(6);
866 if (errno
!= ECHILD
) e(7);
871 void test_attach_child()
873 if (ptrace(T_OK
, 0, 0, 0) != -1) e(100);
874 if (errno
!= EBUSY
) e(101);
878 if (READ() != 0) e(102);
890 /* Attaching to kernel processes is not allowed. */
891 if (ptrace(T_ATTACH
, -1, 0, 0) != -1) e(1);
892 if (errno
!= ESRCH
) e(2);
894 /* Attaching to self is not allowed. */
895 if (ptrace(T_ATTACH
, getpid(), 0, 0) != -1) e(3);
896 if (errno
!= EPERM
) e(4);
898 /* Attaching to PM is not allowed. */
900 /* FIXME: disabled until we can reliably determine PM's pid */
901 if (ptrace(T_ATTACH
, 0, 0, 0) != -1) e(5);
902 if (errno
!= EPERM
) e(6);
905 pid
= traced_fork(test_attach_child
);
907 /* Attaching more than once is not allowed. */
908 if (ptrace(T_ATTACH
, pid
, 0, 0) != -1) e(7);
909 if (errno
!= EBUSY
) e(8);
911 if (READ() != 0) e(9);
913 /* Detaching a running child should not succeed. */
914 if (ptrace(T_DETACH
, pid
, 0, 0) == 0) e(10);
915 if (errno
!= EBUSY
) e(11);
924 void test_detach_child()
927 sigset_t set
, sset
, oset
;
929 sa
.sa_handler
= catch_handler
;
930 sigemptyset(&sa
.sa_mask
);
931 sa
.sa_flags
= SA_NODEFER
;
933 sigaction(SIGUSR1
, &sa
, NULL
);
934 sigaction(SIGUSR2
, &sa
, NULL
);
935 sigaction(SIGTERM
, &sa
, NULL
);
938 sigprocmask(SIG_SETMASK
, &set
, &oset
);
941 sigdelset(&sset
, SIGUSR1
);
947 if (sigsuspend(&sset
) != -1) e(102);
948 if (errno
!= EINTR
) e(103);
950 if (caught
!= 1) e(104);
952 if (READ() != 0) e(105);
954 while (caught
!= 7) sigsuspend(&oset
);
964 /* Can't use traced_fork(), so simplify a bit */
965 if (attach
!= 0) return;
969 pid
= traced_pfork(test_detach_child
);
971 if (READ() != 0) e(1);
973 /* The tracer should not see signals sent to the process before attaching. */
974 if (kill(pid
, SIGUSR2
) != 0) e(2);
976 if (ptrace(T_ATTACH
, pid
, 0, 0) != 0) e(3);
978 if (waitpid(pid
, &status
, 0) != pid
) e(4);
979 if (!_WIFSTOPPED(status
)) e(5);
980 if (WSTOPSIG(status
) != SIGSTOP
) e(6);
982 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) e(7);
984 if (kill(pid
, SIGUSR1
) != 0) e(8);
986 if (waitpid(pid
, &status
, 0) != pid
) e(9);
987 if (!_WIFSTOPPED(status
)) e(10);
988 if (WSTOPSIG(status
) != SIGUSR1
) e(11);
990 /* Signals pending at the tracer should be passed on after detaching. */
991 if (kill(pid
, SIGTERM
) != 0) e(12);
993 /* A signal may be passed with the detach request. */
994 if (ptrace(T_DETACH
, pid
, 0, SIGUSR1
) != 0) e(13);
998 if (waitpid(pid
, &status
, 0) != pid
) e(14);
999 if (!_WIFEXITED(status
)) e(15);
1000 if ((r
= WEXITSTATUS(status
)) != 42) e(r
);
1005 void test_death_child()
1011 if (pid
< 0) e(100);
1014 ptrace(T_OK
, 0, 0, 0);
1021 if (READ() != 0) e(101);
1023 kill(getpid(), SIGKILL
);
1035 pid
= traced_fork(test_death_child
);
1039 if (kill(cpid
, 0) != 0) e(1);
1043 if (waitpid(pid
, &status
, 0) != pid
) e(2);
1044 if (!_WIFSIGNALED(status
)) e(3);
1045 if (WTERMSIG(status
) != SIGKILL
) e(4);
1047 /* The children of killed tracers should be terminated. */
1048 while (kill(cpid
, 0) == 0) sleep(1);
1049 if (errno
!= ESRCH
) e(5);
1054 void test_zdeath_child()
1056 if (READ() != 0) e(100);
1066 /* Can't use traced_fork(), so simplify a bit */
1067 if (attach
!= 0) return;
1071 pid
= traced_pfork(test_zdeath_child
);
1078 if (ptrace(T_ATTACH
, pid
, 0, 0) != 0) exit(101);
1080 if (waitpid(pid
, &status
, 0) != pid
) exit(102);
1081 if (!_WIFSTOPPED(status
)) exit(103);
1082 if (WSTOPSIG(status
) != SIGSTOP
) exit(104);
1084 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) exit(105);
1088 /* Unwaited-for traced zombies should be passed to their parent. */
1096 /* However, that should only happen once the tracer has actually died. */
1097 if (waitpid(pid
, &status
, WNOHANG
) != 0) e(2);
1099 if (waitpid(tpid
, &status
, 0) != tpid
) e(3);
1100 if (!_WIFEXITED(status
)) e(4);
1101 if ((r
= WEXITSTATUS(status
)) != 84) e(r
);
1103 if (waitpid(pid
, &status
, 0) != pid
) e(5);
1104 if (!_WIFEXITED(status
)) e(6);
1105 if ((r
= WEXITSTATUS(status
)) != 42) e(r
);
1110 void test_syscall_child()
1112 signal(SIGUSR1
, count_handler
);
1113 signal(SIGUSR2
, count_handler
);
1119 if (READ() != 0) e(100);
1121 /* Three calls (may fail) */
1126 if (sigs
!= 2) e(101);
1134 int i
, r
, sig
, status
;
1138 pid
= traced_fork(test_syscall_child
);
1140 if (READ() != 0) e(1);
1142 if (kill(pid
, SIGSTOP
) != 0) e(2);
1144 if (waitpid(pid
, &status
, 0) != pid
) e(3);
1145 if (!_WIFSTOPPED(status
)) e(4);
1146 if (WSTOPSIG(status
) != SIGSTOP
) e(5);
1150 /* Upon resuming a first system call, no syscall leave event must be sent. */
1151 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) e(6);
1153 if (waitpid(pid
, &status
, 0) != pid
) e(7);
1155 for (i
= 0; _WIFSTOPPED(status
); i
++) {
1156 if (WSTOPSIG(status
) != SIGTRAP
) e(8);
1158 /* Signals passed via T_SYSCALL should arrive, on enter and exit. */
1159 if (i
== 3) sig
= SIGUSR1
;
1160 else if (i
== 6) sig
= SIGUSR2
;
1163 if (ptrace(T_SYSCALL
, pid
, 0, sig
) != 0) e(9);
1165 if (waitpid(pid
, &status
, 0) != pid
) e(10);
1168 if (!_WIFEXITED(status
)) e(11);
1169 if ((r
= WEXITSTATUS(status
)) != 42) e(r
);
1171 /* The number of events seen is deterministic but libc-dependent. */
1172 if (i
< 10 || i
> 100) e(12);
1174 /* The last system call event must be for entering exit(). */
1175 if (!(i
% 2)) e(13);
1180 void test_tracefork_child()
1184 signal(SIGHUP
, SIG_IGN
);
1190 if (READ() != 0) e(100);
1192 if ((pid
= fork()) < 0) e(101);
1194 exit(pid
> 0 ? 42 : 84);
1197 void test_tracefork()
1199 pid_t pgrp
, ppid
, cpid
, wpid
;
1200 int r
, status
, gotstop
, ptraps
, ctraps
;
1204 ppid
= traced_fork(test_tracefork_child
);
1206 if ((pgrp
= READ()) <= 0) e(1);
1208 if (kill(ppid
, SIGSTOP
) != 0) e(2);
1210 if (waitpid(ppid
, &status
, 0) != ppid
) e(3);
1211 if (!_WIFSTOPPED(status
)) e(4);
1212 if (WSTOPSIG(status
) != SIGSTOP
) e(5);
1214 if (ptrace(T_SETOPT
, ppid
, 0, TO_TRACEFORK
) != 0) e(6);
1218 if (ptrace(T_SYSCALL
, ppid
, 0, 0) != 0) e(7);
1223 /* Count how many traps we get for parent and child, until they both exit. */
1224 for (ptraps
= ctraps
= 0; ppid
|| cpid
; ) {
1225 wpid
= waitpid(-pgrp
, &status
, 0);
1227 if (wpid
<= 0) e(8);
1228 if (cpid
< 0 && wpid
!= ppid
) {
1232 if (wpid
!= ppid
&& wpid
!= cpid
) e(9);
1234 if (_WIFEXITED(status
)) {
1236 if ((r
= WEXITSTATUS(status
)) != 42) e(r
);
1240 if ((r
= WEXITSTATUS(status
)) != 84) e(r
);
1245 if (!_WIFSTOPPED(status
)) e(10);
1247 switch (WSTOPSIG(status
)) {
1252 if (wpid
!= cpid
) e(11);
1257 if (wpid
== ppid
) ptraps
++;
1264 if (ptrace(T_SYSCALL
, wpid
, 0, 0) != 0) e(14);
1268 /* The parent should get an odd number of traps: the first one is a syscall
1269 * enter trap (typically for the fork()), the last one is the syscall enter
1270 * trap for its exit().
1272 if (ptraps
< 3) e(15);
1273 if (!(ptraps
% 2)) e(16);
1275 /* The child should get an even number of traps: the first one is a syscall
1276 * leave trap from the fork(), the last one is the syscall enter trap for
1279 if (ctraps
< 2) e(17);
1280 if (ctraps
% 2) e(18);
1285 void sigexec(setflag
, opt
, traps
, stop
)
1294 pid
= traced_fork(test_exec_child
);
1296 if (kill(pid
, SIGSTOP
) != 0) e(1);
1298 if (waitpid(pid
, &status
, 0) != pid
) e(2);
1299 if (!_WIFSTOPPED(status
)) e(3);
1300 if (WSTOPSIG(status
) != SIGSTOP
) e(4);
1302 if (setflag
&& ptrace(T_SETOPT
, pid
, 0, opt
) != 0) e(5);
1306 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) e(6);
1312 if (waitpid(pid
, &status
, 0) != pid
) e(7);
1314 if (_WIFEXITED(status
)) break;
1316 if (!_WIFSTOPPED(status
)) e(8);
1318 switch (WSTOPSIG(status
)) {
1323 if (*stop
>= 0) e(9);
1330 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) e(11);
1333 if ((r
= WEXITSTATUS(status
)) != 42) e(r
);
1338 void test_trapexec()
1344 sigexec(1, 0, &traps
, &stop
);
1346 /* The exec does not cause a SIGSTOP. This gives us an even number of traps;
1347 * as above, but plus the exec()'s extra SIGTRAP. This trap is
1348 * indistinguishable from a syscall trap, especially when considering failed
1349 * exec() calls and immediately following signal handler invocations.
1351 if (traps
< 4) e(12);
1352 if (traps
% 2) e(13);
1353 if (stop
>= 0) e(14);
1362 sigexec(1, TO_ALTEXEC
, &traps
, &stop
);
1364 /* The exec causes a SIGSTOP. This gives us an odd number of traps: a pair
1365 * for each system call, plus one for the final exit(). The stop must have
1366 * taken place after a syscall enter event, i.e. must be odd as well.
1368 if (traps
< 3) e(12);
1369 if (!(traps
% 2)) e(13);
1370 if (stop
< 0) e(14);
1371 if (!(stop
% 2)) e(15);
1380 sigexec(1, TO_NOEXEC
, &traps
, &stop
);
1382 /* The exec causes no signal at all. As above, but without the SIGSTOPs. */
1383 if (traps
< 3) e(12);
1384 if (!(traps
% 2)) e(13);
1385 if (stop
>= 0) e(14);
1392 /* We want to test the default of T_OK (0) and T_ATTACH (TO_NOEXEC). */
1393 if (attach
!= 0 && attach
!= 1) return;
1397 /* Do not set any options this time. */
1398 sigexec(0, 0, &traps
, &stop
);
1402 if (traps
< 4) e(12);
1403 if (traps
% 2) e(13);
1404 if (stop
>= 0) e(14);
1407 if (traps
< 3) e(15);
1408 if (!(traps
% 2)) e(16);
1409 if (stop
>= 0) e(17);
1413 void test_reattach_child()
1417 if (READ() != 0) e(100);
1421 if (select(0, NULL
, NULL
, NULL
, &tv
) != 0) e(101);
1426 void test_reattach()
1429 int r
, status
, count
;
1433 pid
= traced_fork(test_reattach_child
);
1435 if (kill(pid
, SIGSTOP
) != 0) e(1);
1437 if (waitpid(pid
, &status
, 0) != pid
) e(2);
1438 if (!_WIFSTOPPED(status
)) e(3);
1439 if (WSTOPSIG(status
) != SIGSTOP
) e(4);
1443 signal(SIGALRM
, dummy_handler
);
1446 /* Start tracing system calls. We don't know how many there will be until
1447 * we reach the child's select(), so we have to interrupt ourselves.
1448 * The hard assumption here is that the child is able to enter the select()
1449 * within a second, despite being traced. If this is not the case, the test
1450 * may hang or fail, and the child may die from a SIGTRAP.
1452 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) e(5);
1454 for (count
= 0; (r
= waitpid(pid
, &status
, 0)) == pid
; count
++) {
1455 if (!_WIFSTOPPED(status
)) e(6);
1456 if (WSTOPSIG(status
) != SIGTRAP
) e(7);
1458 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) e(8);
1461 if (r
!= -1 || errno
!= EINTR
) e(9);
1463 /* We always start with syscall enter event; the last event we should have
1464 * seen before the alarm was entering the select() call.
1466 if (!(count
% 2)) e(10);
1468 /* Detach, and immediately attach again. */
1469 detach_running(pid
);
1471 if (ptrace(T_ATTACH
, pid
, 0, 0) != 0) e(11);
1473 if (waitpid(pid
, &status
, 0) != pid
) e(12);
1474 if (!_WIFSTOPPED(status
)) e(13);
1475 if (WSTOPSIG(status
) != SIGSTOP
) e(14);
1477 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) e(15);
1479 if (waitpid(pid
, &status
, 0) != pid
) e(16);
1481 for (count
= 0; _WIFSTOPPED(status
); count
++) {
1482 if (WSTOPSIG(status
) != SIGTRAP
) e(17);
1484 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) e(18);
1486 if (waitpid(pid
, &status
, 0) != pid
) e(19);
1489 if (!_WIFEXITED(status
)) e(20);
1490 if ((r
= WEXITSTATUS(status
)) != 42) e(r
);
1492 /* We must not have seen the select()'s syscall leave event, and the last
1493 * event will be the syscall enter for the exit().
1495 if (!(count
% 2)) e(21);
1506 printf("Subtest %d, attach type %d, error %d, errno %d: %s\n",
1507 subtest
, attach
, n
, errno
, strerror(errno
));
1509 if (errct
++ > MAX_ERROR
) {
1510 printf("Too many errors; test aborted\n");
1521 printf("%d errors\n", errct
);