test42: skip single step test
[minix3.git] / test / test42.c
blobc61ce0d6a3eb384ad3c6d48e68fe87c39db16c3b
1 /* Tests for MINIX3 ptrace(2) - by D.C. van Moolenbroek */
2 #include <setjmp.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <signal.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <sys/wait.h>
10 #include <sys/select.h>
11 #include <sys/ptrace.h>
13 #define ITERATIONS 3
14 int max_error = 4;
15 #include "common.h"
17 #define my_e(n) { \
18 if (child) exit(n); printf("Attach type %d, ", attach); e(n); }
21 #define _WIFSTOPPED(s) (WIFSTOPPED(s) && !WIFSIGNALED(s) && !WIFEXITED(s))
22 #define _WIFSIGNALED(s) (!WIFSTOPPED(s) && WIFSIGNALED(s) && !WIFEXITED(s))
23 #define _WIFEXITED(s) (!WIFSTOPPED(s) && !WIFSIGNALED(s) && WIFEXITED(s))
25 #define timed_test(func) (timed_test_func(#func, func));
27 int main(int argc, char **argv);
28 void test(int m, int a);
29 void timed_test_func(const char *s, void (* func)(void));
30 void timed_test_timeout(int signum);
31 pid_t traced_fork(void (*c) (void));
32 pid_t traced_pfork(void (*c) (void));
33 void WRITE(int value);
34 int READ(void);
35 void traced_wait(void);
36 void detach_running(pid_t pid);
37 void dummy_handler(int sig);
38 void exit_handler(int sig);
39 void count_handler(int sig);
40 void catch_handler(int sig);
41 void test_wait_child(void);
42 void test_wait(void);
43 void test_exec_child(void);
44 void test_exec(void);
45 void test_step_child(void);
46 void test_step(void);
47 void test_sig_child(void);
48 void test_sig(void);
49 void test_exit_child(void);
50 void test_exit(void);
51 void test_term_child(void);
52 void test_term(void);
53 void test_catch_child(void);
54 void test_catch(void);
55 void test_kill_child(void);
56 void test_kill(void);
57 void test_attach_child(void);
58 void test_attach(void);
59 void test_detach_child(void);
60 void test_detach(void);
61 void test_death_child(void);
62 void test_death(void);
63 void test_zdeath_child(void);
64 void test_zdeath(void);
65 void test_syscall_child(void);
66 void test_syscall(void);
67 void test_tracefork_child(void);
68 void test_tracefork(void);
69 void sigexec(int setflag, int opt, int *traps, int *stop);
70 void test_trapexec(void);
71 void test_altexec(void);
72 void test_noexec(void);
73 void test_defexec(void);
74 void test_reattach_child(void);
75 void test_reattach(void);
77 static char *executable;
78 static int child = 0, attach;
79 static pid_t ppid;
80 static int pfd[4];
81 static int sigs, caught;
83 int main(argc, argv)
84 int argc;
85 char **argv;
87 int i, m = 0xFFFFFF, n = 0xF;
88 char cp_cmd[NAME_MAX + 10];
90 if (strcmp(argv[0], "DO CHECK") == 0) {
91 exit(42);
94 start(42);
96 executable = argv[0];
98 snprintf(cp_cmd, sizeof(cp_cmd), "cp ../%s .", executable);
99 system(cp_cmd);
101 if (argc >= 2) m = atoi(argv[1]);
102 if (argc >= 3) n = atoi(argv[2]);
104 for (i = 0; i < ITERATIONS; i++) {
105 if (n & 001) test(m, 0);
106 if (n & 002) test(m, 1);
107 if (n & 004) test(m, 2);
108 if (n & 010) test(m, 3);
111 quit();
112 return(-1); /* impossible */
115 void test(m, a)
116 int m;
117 int a;
119 attach = a;
121 if (m & 00000001) timed_test(test_wait);
122 if (m & 00000002) timed_test(test_exec);
123 #if !defined(__arm__)
124 /* BJG: single-stepping isn't implemented on ARM */
125 if (m & 00000004) timed_test(test_step);
126 #endif
127 if (m & 00000010) timed_test(test_sig);
128 if (m & 00000020) timed_test(test_exit);
129 if (m & 00000040) timed_test(test_term);
130 if (m & 00000100) timed_test(test_catch);
131 if (m & 00000200) timed_test(test_kill);
132 if (m & 00000400) timed_test(test_attach);
133 if (m & 00001000) timed_test(test_detach);
134 if (m & 00002000) timed_test(test_death);
135 if (m & 00004000) timed_test(test_zdeath);
136 if (m & 00010000) timed_test(test_syscall);
137 if (m & 00020000) timed_test(test_tracefork);
138 if (m & 00040000) timed_test(test_trapexec);
139 if (m & 00100000) timed_test(test_altexec);
140 if (m & 00200000) timed_test(test_noexec);
141 if (m & 00400000) timed_test(test_defexec);
142 if (m & 01000000) test_reattach(); /* not timed, catches SIGALRM */
145 static jmp_buf timed_test_context;
147 void timed_test_timeout(int signum)
149 longjmp(timed_test_context, -1);
150 my_e(700);
151 quit();
152 exit(-1);
155 void timed_test_func(const char *s, void (* func)(void))
157 if (setjmp(timed_test_context) == 0)
159 /* the function gets 60 seconds to complete */
160 if (signal(SIGALRM, timed_test_timeout) == SIG_ERR) { my_e(701); return; }
161 alarm(60);
162 func();
163 alarm(0);
165 else
167 /* report timeout as error */
168 printf("timeout in %s\n", s);
169 my_e(702);
173 pid_t traced_fork(c)
174 void(*c) (void);
176 pid_t pid;
177 int r, status;
179 if (pipe(pfd) != 0) my_e(200);
180 if (pipe(&pfd[2]) != 0) my_e(201);
182 switch (attach) {
183 case 0: /* let child volunteer to be traced */
184 pid = fork();
186 if (pid < 0) my_e(202);
188 if (pid == 0) {
189 child = 1;
191 if (ptrace(T_OK, 0, 0, 0) != 0) my_e(203);
193 WRITE(0);
195 c();
197 my_e(204);
200 if (READ() != 0) my_e(205);
202 break;
204 case 1: /* attach to child process */
205 pid = fork();
207 if (pid < 0) my_e(206);
209 if (pid == 0) {
210 child = 1;
212 if (READ() != 0) my_e(207);
214 c();
216 my_e(208);
219 if (ptrace(T_ATTACH, pid, 0, 0) != 0) my_e(209);
221 if (waitpid(pid, &status, 0) != pid) my_e(210);
222 if (!_WIFSTOPPED(status)) my_e(211);
223 if (WSTOPSIG(status) != SIGSTOP) my_e(212);
225 if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(213);
227 WRITE(0);
229 break;
231 case 2: /* attach to non-child process */
232 ppid = fork();
234 if (ppid < 0) my_e(214);
236 if (ppid == 0) {
237 pid = fork();
239 if (pid < 0) exit(215);
241 if (pid == 0) {
242 child = 1;
244 if (READ() != 0) my_e(216);
246 c();
248 my_e(217);
251 child = 1;
253 WRITE(pid);
255 if (waitpid(pid, &status, 0) != pid) my_e(218);
256 if (_WIFSTOPPED(status)) my_e(219);
257 if (_WIFEXITED(status) && (r = WEXITSTATUS(status)) != 42) my_e(r);
259 exit(0);
262 pid = READ();
264 if (ptrace(T_ATTACH, pid, 0, 0) != 0) my_e(220);
266 if (waitpid(pid, &status, 0) != pid) my_e(221);
267 if (!_WIFSTOPPED(status)) my_e(222);
268 if (WSTOPSIG(status) != SIGSTOP) my_e(223);
270 if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(224);
272 WRITE(0);
274 break;
276 case 3: /* attach by forking from child */
277 ppid = fork();
279 if (ppid < 0) my_e(225);
281 if (ppid == 0) {
282 child = 1;
284 if (ptrace(T_OK, 0, 0, 0) != 0) my_e(226);
286 WRITE(0);
288 if (READ() != 0) my_e(227);
290 pid = fork();
292 if (pid < 0) my_e(228);
294 if (pid == 0) {
295 c();
297 my_e(229);
300 WRITE(pid);
302 if (waitpid(pid, &status, 0) != pid) my_e(230);
303 if (_WIFSTOPPED(status)) my_e(231);
304 if (_WIFEXITED(status) && (r = WEXITSTATUS(status)) != 42) my_e(r);
306 exit(0);
309 if (READ() != 0) my_e(232);
311 if (kill(ppid, SIGSTOP) != 0) my_e(233);
313 if (waitpid(ppid, &status, 0) != ppid) my_e(234);
314 if (!_WIFSTOPPED(status)) my_e(235);
315 if (WSTOPSIG(status) != SIGSTOP) my_e(236);
317 if (ptrace(T_SETOPT, ppid, 0, TO_TRACEFORK) != 0) my_e(237);
319 if (ptrace(T_RESUME, ppid, 0, 0) != 0) my_e(238);
321 WRITE(0);
323 pid = READ();
325 if (waitpid(pid, &status, 0) != pid) my_e(239);
326 if (!_WIFSTOPPED(status)) my_e(240);
327 if (WSTOPSIG(status) != SIGSTOP) my_e(241);
329 if (ptrace(T_SETOPT, pid, 0, 0) != 0) my_e(242);
330 if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(243);
332 detach_running(ppid);
334 break;
337 return pid;
340 pid_t traced_pfork(c)
341 void(*c) (void);
343 pid_t pid;
345 if (pipe(pfd) != 0) my_e(300);
346 if (pipe(&pfd[2]) != 0) my_e(301);
348 pid = fork();
350 if (pid < 0) my_e(302);
352 if (pid == 0) {
353 child = 1;
355 c();
357 my_e(303);
360 return pid;
363 void WRITE(value)
364 int value;
366 if (write(pfd[child*2+1], &value, sizeof(value)) != sizeof(value)) my_e(400);
369 int READ()
371 int value;
373 if (read(pfd[2-child*2], &value, sizeof(value)) != sizeof(value)) my_e(401);
375 return value;
378 void traced_wait()
380 int r, status;
382 if (attach == 2) {
383 if (waitpid(ppid, &status, 0) != ppid) my_e(500);
384 if (!_WIFEXITED(status)) my_e(501);
385 if ((r = WEXITSTATUS(status)) != 0) my_e(r);
387 else {
388 /* Quick hack to clean up detached children */
389 waitpid(-1, NULL, WNOHANG);
392 close(pfd[0]);
393 close(pfd[1]);
394 close(pfd[2]);
395 close(pfd[3]);
398 void detach_running(pid)
399 pid_t pid;
401 /* Detach from a process that is not already stopped. This is the way to do it.
402 * We have to stop the child in order to detach from it, but as the child may
403 * have other signals pending for the tracer, we cannot assume we get our own
404 * signal back immediately. However, because we know that the kill is instant
405 * and resuming with pending signals will only stop the process immediately
406 * again, we can use T_RESUME for all the signals until we get our own signal,
407 * and then detach. A complicating factor is that anywhere during this
408 * procedure, the child may die (e.g. by getting a SIGKILL). In our tests, this
409 * will not happen.
411 int status;
413 if (kill(pid, SIGSTOP) != 0) my_e(600);
415 if (waitpid(pid, &status, 0) != pid) my_e(601);
417 while (_WIFSTOPPED(status)) {
418 if (WSTOPSIG(status) == SIGSTOP) {
419 if (ptrace(T_DETACH, pid, 0, 0) != 0) my_e(602);
421 return;
424 if (ptrace(T_RESUME, pid, 0, WSTOPSIG(status)) != 0) my_e(603);
426 if (waitpid(pid, &status, 0) != pid) my_e(604);
429 /* Apparently the process exited. */
430 if (!_WIFEXITED(status) && !_WIFSIGNALED(status)) my_e(605);
432 /* In our tests, that should not happen. */
433 my_e(606);
436 void dummy_handler(sig)
437 int sig;
441 void exit_handler(sig)
442 int sig;
444 exit(42);
447 void count_handler(sig)
448 int sig;
450 sigs++;
453 void catch_handler(sig)
454 int sig;
456 sigset_t set;
457 int bit;
459 switch (sig) {
460 case SIGUSR1: bit = 1; break;
461 case SIGUSR2: bit = 2; break;
462 case SIGTERM: bit = 4; break;
463 default: my_e(100);
466 sigfillset(&set);
467 sigprocmask(SIG_SETMASK, &set, NULL);
469 if (caught & bit) my_e(101);
470 caught |= bit;
473 void test_wait_child()
475 exit(42);
478 void test_wait()
480 pid_t pid;
481 int status;
483 subtest = 1;
485 pid = traced_fork(test_wait_child);
487 if (waitpid(pid, &status, 0) != pid) my_e(1);
488 if (!_WIFEXITED(status)) my_e(2);
489 if (WEXITSTATUS(status) != 42) my_e(3);
491 traced_wait();
494 void test_exec_child()
496 if (READ() != 0) my_e(100);
498 execl(executable, "DO CHECK", NULL);
500 my_e(101);
503 void test_exec()
505 pid_t pid;
506 int r, status;
508 /* This test covers the T_OK case. */
509 if (attach != 0) return;
511 subtest = 2;
513 pid = traced_fork(test_exec_child);
515 WRITE(0);
517 /* An exec() should result in a trap signal. */
518 if (waitpid(pid, &status, 0) != pid) my_e(1);
519 if (!_WIFSTOPPED(status)) my_e(2);
520 if (WSTOPSIG(status) != SIGTRAP) my_e(3);
522 if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(4);
524 if (waitpid(pid, &status, 0) != pid) my_e(5);
525 if (!_WIFEXITED(status)) my_e(6);
526 if ((r = WEXITSTATUS(status)) != 42) my_e(r);
528 traced_wait();
531 void test_step_child()
533 sigset_t set;
535 signal(SIGUSR1, SIG_IGN);
537 WRITE(0);
539 if (READ() != 0) my_e(100);
541 /* It must not be possible for the child to stop the single-step signal. */
542 signal(SIGTRAP, SIG_IGN);
543 sigfillset(&set);
544 sigprocmask(SIG_SETMASK, &set, NULL);
546 exit(42);
549 void test_step()
551 pid_t pid;
552 int r, status, count;
554 subtest = 3;
556 pid = traced_fork(test_step_child);
558 if (READ() != 0) my_e(1);
560 /* While the child is running, neither waitpid() nor ptrace() should work. */
561 if (waitpid(pid, &status, WNOHANG) != 0) my_e(2);
562 if (ptrace(T_RESUME, pid, 0, 0) != -1) my_e(3);
563 if (errno != EBUSY) my_e(4);
565 if (kill(pid, SIGUSR1) != 0) my_e(5);
567 WRITE(0);
569 /* A kill() signal (other than SIGKILL) should be delivered to the tracer. */
570 if (waitpid(pid, &status, 0) != pid) my_e(6);
571 if (!_WIFSTOPPED(status)) my_e(7);
572 if (WSTOPSIG(status) != SIGUSR1) my_e(8);
574 /* ptrace(T_STEP) should result in instruction-wise progress. */
575 for (count = 0; ; count++) {
576 if (ptrace(T_STEP, pid, 0, 0) != 0) my_e(9);
578 if (waitpid(pid, &status, 0) != pid) my_e(10);
579 if (_WIFEXITED(status)) break;
580 if (!_WIFSTOPPED(status)) my_e(11);
581 if (WSTOPSIG(status) != SIGTRAP) my_e(12);
584 if ((r = WEXITSTATUS(status)) != 42) my_e(r);
586 if (count < 10) my_e(13); /* in practice: hundreds */
588 traced_wait();
591 void test_sig_child()
593 signal(SIGUSR1, exit_handler);
595 if (READ() != 0) my_e(100);
597 pause();
599 my_e(101);
602 void test_sig()
604 pid_t pid;
605 int r, sig, status;
607 subtest = 4;
609 pid = traced_fork(test_sig_child);
611 WRITE(0);
613 /* allow the child to enter the pause */
614 sleep(1);
616 if (kill(pid, SIGUSR1) != 0) my_e(1);
617 if (kill(pid, SIGUSR2) != 0) my_e(2);
619 /* All signals should arrive at the tracer, although in "random" order. */
620 if (waitpid(pid, &status, 0) != pid) my_e(3);
621 if (!_WIFSTOPPED(status)) my_e(4);
622 if (WSTOPSIG(status) != SIGUSR1 && WSTOPSIG(status) != SIGUSR2) my_e(5);
624 /* The tracer should see kills arriving while the tracee is stopped. */
625 if (kill(pid, WSTOPSIG(status)) != 0) my_e(6);
627 if (waitpid(pid, &status, WNOHANG) != pid) my_e(7);
628 if (!_WIFSTOPPED(status)) my_e(8);
629 if (WSTOPSIG(status) != SIGUSR1 && WSTOPSIG(status) != SIGUSR2) my_e(9);
630 sig = (WSTOPSIG(status) == SIGUSR1) ? SIGUSR2 : SIGUSR1;
632 if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(10);
634 if (waitpid(pid, &status, 0) != pid) my_e(11);
635 if (!_WIFSTOPPED(status)) my_e(12);
636 if (WSTOPSIG(status) != sig) my_e(13);
638 if (waitpid(pid, &status, WNOHANG) != 0) my_e(14);
640 if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(15);
642 /* Ignored signals passed via ptrace() should be ignored. */
643 if (kill(pid, SIGUSR1) != 0) my_e(16);
645 if (waitpid(pid, &status, 0) != pid) my_e(17);
646 if (!_WIFSTOPPED(status)) my_e(18);
647 if (WSTOPSIG(status) != SIGUSR1) my_e(19);
649 if (ptrace(T_RESUME, pid, 0, SIGCHLD) != 0) my_e(20);
651 /* if the pause has been aborted (shouldn't happen!), let the child exit */
652 sleep(1);
654 if (waitpid(pid, &status, WNOHANG) != 0) my_e(21);
656 /* Caught signals passed via ptrace() should invoke their signal handlers. */
657 if (kill(pid, SIGUSR1) != 0) my_e(22);
659 if (waitpid(pid, &status, 0) != pid) my_e(23);
660 if (!_WIFSTOPPED(status)) my_e(24);
661 if (WSTOPSIG(status) != SIGUSR1) my_e(25);
663 if (ptrace(T_RESUME, pid, 0, SIGUSR1) != 0) my_e(26);
665 if (waitpid(pid, &status, 0) != pid) my_e(27);
666 if (!_WIFEXITED(status)) my_e(28);
667 if ((r = WEXITSTATUS(status)) != 42) my_e(29);
669 traced_wait();
672 void test_exit_child()
674 WRITE(0);
676 for(;;);
679 void test_exit()
681 pid_t pid;
682 int r, status;
684 subtest = 5;
686 pid = traced_fork(test_exit_child);
688 if (READ() != 0) my_e(1);
690 sleep(1);
692 if (kill(pid, SIGSTOP) != 0) my_e(2);
694 if (waitpid(pid, &status, 0) != pid) my_e(3);
695 if (!_WIFSTOPPED(status)) my_e(4);
696 if (WSTOPSIG(status) != SIGSTOP) my_e(5);
698 /* There should be no more signals pending for the tracer now. */
699 if (waitpid(pid, &status, WNOHANG) != 0) my_e(6);
701 /* ptrace(T_EXIT) should terminate the process with the given exit value. */
702 if (ptrace(T_EXIT, pid, 0, 42) != 0) my_e(7);
704 if (waitpid(pid, &status, 0) != pid) my_e(8);
705 if (!_WIFEXITED(status)) my_e(9);
706 if ((r = WEXITSTATUS(status)) != 42) my_e(r);
708 traced_wait();
711 void test_term_child()
713 signal(SIGUSR1, SIG_DFL);
714 signal(SIGUSR2, dummy_handler);
716 WRITE(0);
718 pause();
720 my_e(100);
723 void test_term()
725 pid_t pid;
726 int status;
728 subtest = 6;
730 pid = traced_fork(test_term_child);
732 if (READ() != 0) my_e(1);
734 /* If the first of two signals terminates the traced child, the second signal
735 * may or may not be delivered to the tracer - this is merely a policy issue.
736 * However, nothing unexpected should happen.
738 if (kill(pid, SIGUSR1) != 0) my_e(2);
739 if (kill(pid, SIGUSR2) != 0) my_e(3);
741 if (waitpid(pid, &status, 0) != pid) my_e(4);
742 if (!_WIFSTOPPED(status)) my_e(5);
744 if (ptrace(T_RESUME, pid, 0, SIGUSR1) != 0) my_e(6);
746 if (waitpid(pid, &status, 0) != pid) my_e(7);
748 if (_WIFSTOPPED(status)) {
749 if (ptrace(T_RESUME, pid, 0, SIGUSR1) != 0) my_e(8);
751 if (waitpid(pid, &status, 0) != pid) my_e(9);
754 if (!_WIFSIGNALED(status)) my_e(10);
755 if (WTERMSIG(status) != SIGUSR1) my_e(11);
757 traced_wait();
760 void test_catch_child()
762 struct sigaction sa;
763 sigset_t set, oset;
765 sa.sa_handler = catch_handler;
766 sigemptyset(&sa.sa_mask);
767 sa.sa_flags = SA_NODEFER;
769 sigaction(SIGUSR1, &sa, NULL);
770 sigaction(SIGUSR2, &sa, NULL);
771 sigaction(SIGTERM, &sa, NULL);
773 sigfillset(&set);
774 sigprocmask(SIG_SETMASK, &set, &oset);
776 caught = 0;
778 WRITE(0);
780 while (caught != 7) sigsuspend(&oset);
782 exit(42);
785 void test_catch()
787 pid_t pid;
788 int r, sig, status;
790 subtest = 7;
792 pid = traced_fork(test_catch_child);
794 if (READ() != 0) my_e(1);
796 if (kill(pid, SIGUSR1) != 0) my_e(2);
797 if (kill(pid, SIGUSR2) != 0) my_e(3);
799 if (waitpid(pid, &status, 0) != pid) my_e(4);
800 if (!_WIFSTOPPED(status)) my_e(5);
801 if (WSTOPSIG(status) != SIGUSR1 && WSTOPSIG(status) != SIGUSR2) my_e(6);
802 sig = (WSTOPSIG(status) == SIGUSR1) ? SIGUSR2 : SIGUSR1;
804 if (ptrace(T_RESUME, pid, 0, WSTOPSIG(status)) != 0) my_e(7);
806 if (kill(pid, SIGTERM) != 0) my_e(8);
808 if (waitpid(pid, &status, 0) != pid) my_e(9);
809 if (!_WIFSTOPPED(status)) my_e(10);
810 if (WSTOPSIG(status) != sig && WSTOPSIG(status) != SIGTERM) my_e(11);
811 if (WSTOPSIG(status) == sig) sig = SIGTERM;
813 if (ptrace(T_RESUME, pid, 0, WSTOPSIG(status)) != 0) my_e(12);
815 if (kill(pid, SIGBUS) != 0) my_e(13);
817 if (waitpid(pid, &status, 0) != pid) my_e(14);
818 if (!_WIFSTOPPED(status)) my_e(15);
819 if (WSTOPSIG(status) != sig && WSTOPSIG(status) != SIGBUS) my_e(16);
821 if (ptrace(T_RESUME, pid, 0, sig) != 0) my_e(17);
823 if (WSTOPSIG(status) == sig) sig = SIGBUS;
825 if (waitpid(pid, &status, 0) != pid) my_e(18);
826 if (!_WIFSTOPPED(status)) my_e(19);
827 if (WSTOPSIG(status) != sig) my_e(20);
829 if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(21);
831 if (waitpid(pid, &status, 0) != pid) my_e(22);
832 if (!_WIFEXITED(status)) my_e(23);
833 if ((r = WEXITSTATUS(status)) != 42) my_e(r);
835 traced_wait();
838 void test_kill_child()
840 sigset_t set;
842 signal(SIGKILL, SIG_IGN);
843 sigfillset(&set);
844 sigprocmask(SIG_SETMASK, &set, NULL);
846 WRITE(0);
848 pause();
850 my_e(100);
853 void test_kill()
855 pid_t pid;
856 int status;
858 subtest = 8;
860 pid = traced_fork(test_kill_child);
862 if (READ() != 0) my_e(1);
864 /* SIGKILL must be unstoppable in every way. */
865 if (kill(pid, SIGKILL) != 0) my_e(2);
867 if (waitpid(pid, &status, 0) != pid) my_e(3);
868 if (!_WIFSIGNALED(status)) my_e(4);
869 if (WTERMSIG(status) != SIGKILL) my_e(5);
871 /* After termination, the child must no longer be visible to the tracer. */
872 if (waitpid(pid, &status, WNOHANG) != -1) my_e(6);
873 if (errno != ECHILD) my_e(7);
875 traced_wait();
878 void test_attach_child()
880 if (ptrace(T_OK, 0, 0, 0) != -1) my_e(100);
881 if (errno != EBUSY) my_e(101);
883 WRITE(0);
885 if (READ() != 0) my_e(102);
887 exit(42);
890 void test_attach()
892 pid_t pid;
894 subtest = 9;
896 /* Attaching to kernel processes is not allowed. */
897 if (ptrace(T_ATTACH, -1, 0, 0) != -1) my_e(1);
898 if (errno != ESRCH) my_e(2);
900 /* Attaching to self is not allowed. */
901 if (ptrace(T_ATTACH, getpid(), 0, 0) != -1) my_e(3);
902 if (errno != EPERM) my_e(4);
904 /* Attaching to PM is not allowed. */
905 #if 0
906 /* FIXME: disabled until we can reliably determine PM's pid */
907 if (ptrace(T_ATTACH, 0, 0, 0) != -1) my_e(5);
908 if (errno != EPERM) my_e(6);
909 #endif
911 pid = traced_fork(test_attach_child);
913 /* Attaching more than once is not allowed. */
914 if (ptrace(T_ATTACH, pid, 0, 0) != -1) my_e(7);
915 if (errno != EBUSY) my_e(8);
917 if (READ() != 0) my_e(9);
919 /* Detaching a running child should not succeed. */
920 if (ptrace(T_DETACH, pid, 0, 0) == 0) my_e(10);
921 if (errno != EBUSY) my_e(11);
923 detach_running(pid);
925 WRITE(0);
927 traced_wait();
930 void test_detach_child()
932 struct sigaction sa;
933 sigset_t set, sset, oset;
935 sa.sa_handler = catch_handler;
936 sigemptyset(&sa.sa_mask);
937 sa.sa_flags = SA_NODEFER;
939 sigaction(SIGUSR1, &sa, NULL);
940 sigaction(SIGUSR2, &sa, NULL);
941 sigaction(SIGTERM, &sa, NULL);
943 sigfillset(&set);
944 sigprocmask(SIG_SETMASK, &set, &oset);
946 sigfillset(&sset);
947 sigdelset(&sset, SIGUSR1);
949 caught = 0;
951 WRITE(0);
953 if (sigsuspend(&sset) != -1) my_e(102);
954 if (errno != EINTR) my_e(103);
956 if (caught != 1) my_e(104);
958 if (READ() != 0) my_e(105);
960 while (caught != 7) sigsuspend(&oset);
962 exit(42);
965 void test_detach()
967 pid_t pid;
968 int r, status;
970 /* Can't use traced_fork(), so simplify a bit */
971 if (attach != 0) return;
973 subtest = 10;
975 pid = traced_pfork(test_detach_child);
977 if (READ() != 0) my_e(1);
979 /* The tracer should not see signals sent to the process before attaching. */
980 if (kill(pid, SIGUSR2) != 0) my_e(2);
982 if (ptrace(T_ATTACH, pid, 0, 0) != 0) my_e(3);
984 if (waitpid(pid, &status, 0) != pid) my_e(4);
985 if (!_WIFSTOPPED(status)) my_e(5);
986 if (WSTOPSIG(status) != SIGSTOP) my_e(6);
988 if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(7);
990 if (kill(pid, SIGUSR1) != 0) my_e(8);
992 if (waitpid(pid, &status, 0) != pid) my_e(9);
993 if (!_WIFSTOPPED(status)) my_e(10);
994 if (WSTOPSIG(status) != SIGUSR1) my_e(11);
996 /* Signals pending at the tracer should be passed on after detaching. */
997 if (kill(pid, SIGTERM) != 0) my_e(12);
999 /* A signal may be passed with the detach request. */
1000 if (ptrace(T_DETACH, pid, 0, SIGUSR1) != 0) my_e(13);
1002 WRITE(0);
1004 if (waitpid(pid, &status, 0) != pid) my_e(14);
1005 if (!_WIFEXITED(status)) my_e(15);
1006 if ((r = WEXITSTATUS(status)) != 42) my_e(r);
1008 traced_wait();
1011 void test_death_child()
1013 pid_t pid;
1015 pid = fork();
1017 if (pid < 0) my_e(100);
1019 if (pid == 0) {
1020 ptrace(T_OK, 0, 0, 0);
1022 WRITE(getpid());
1024 for (;;) pause();
1027 if (READ() != 0) my_e(101);
1029 kill(getpid(), SIGKILL);
1031 my_e(102);
1034 void test_death()
1036 pid_t pid, cpid;
1037 int status;
1039 subtest = 11;
1041 pid = traced_fork(test_death_child);
1043 cpid = READ();
1045 if (kill(cpid, 0) != 0) my_e(1);
1047 WRITE(0);
1049 if (waitpid(pid, &status, 0) != pid) my_e(2);
1050 if (!_WIFSIGNALED(status)) my_e(3);
1051 if (WTERMSIG(status) != SIGKILL) my_e(4);
1053 /* The children of killed tracers should be terminated. */
1054 while (kill(cpid, 0) == 0) sleep(1);
1055 if (errno != ESRCH) my_e(5);
1057 traced_wait();
1060 void test_zdeath_child()
1062 if (READ() != 0) my_e(100);
1064 exit(42);
1067 void test_zdeath()
1069 pid_t pid, tpid;
1070 int r, status;
1072 /* Can't use traced_fork(), so simplify a bit */
1073 if (attach != 0) return;
1075 subtest = 12;
1077 pid = traced_pfork(test_zdeath_child);
1079 tpid = fork();
1081 if (tpid < 0) my_e(1);
1083 if (tpid == 0) {
1084 if (ptrace(T_ATTACH, pid, 0, 0) != 0) exit(101);
1086 if (waitpid(pid, &status, 0) != pid) exit(102);
1087 if (!_WIFSTOPPED(status)) exit(103);
1088 if (WSTOPSIG(status) != SIGSTOP) exit(104);
1090 if (ptrace(T_RESUME, pid, 0, 0) != 0) exit(105);
1092 WRITE(0);
1094 /* Unwaited-for traced zombies should be passed to their parent. */
1095 sleep(2);
1097 exit(84);
1100 sleep(1);
1102 /* However, that should only happen once the tracer has actually died. */
1103 if (waitpid(pid, &status, WNOHANG) != 0) my_e(2);
1105 if (waitpid(tpid, &status, 0) != tpid) my_e(3);
1106 if (!_WIFEXITED(status)) my_e(4);
1107 if ((r = WEXITSTATUS(status)) != 84) my_e(r);
1109 if (waitpid(pid, &status, 0) != pid) my_e(5);
1110 if (!_WIFEXITED(status)) my_e(6);
1111 if ((r = WEXITSTATUS(status)) != 42) my_e(r);
1113 traced_wait();
1116 void test_syscall_child()
1118 signal(SIGUSR1, count_handler);
1119 signal(SIGUSR2, count_handler);
1121 sigs = 0;
1123 WRITE(0);
1125 if (READ() != 0) my_e(100);
1127 /* Three calls (may fail) */
1128 setuid(0);
1129 close(123);
1130 getpid();
1132 if (sigs != 2) my_e(101);
1134 exit(42);
1137 void test_syscall()
1139 pid_t pid;
1140 int i, r, sig, status;
1142 subtest = 13;
1144 pid = traced_fork(test_syscall_child);
1146 if (READ() != 0) my_e(1);
1148 if (kill(pid, SIGSTOP) != 0) my_e(2);
1150 if (waitpid(pid, &status, 0) != pid) my_e(3);
1151 if (!_WIFSTOPPED(status)) my_e(4);
1152 if (WSTOPSIG(status) != SIGSTOP) my_e(5);
1154 WRITE(0);
1156 /* Upon resuming a first system call, no syscall leave event must be sent. */
1157 if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(6);
1159 if (waitpid(pid, &status, 0) != pid) my_e(7);
1161 for (i = 0; _WIFSTOPPED(status); i++) {
1162 if (WSTOPSIG(status) != SIGTRAP) my_e(8);
1164 /* Signals passed via T_SYSCALL should arrive, on enter and exit. */
1165 if (i == 3) sig = SIGUSR1;
1166 else if (i == 6) sig = SIGUSR2;
1167 else sig = 0;
1169 if (ptrace(T_SYSCALL, pid, 0, sig) != 0) my_e(9);
1171 if (waitpid(pid, &status, 0) != pid) my_e(10);
1174 if (!_WIFEXITED(status)) my_e(11);
1175 if ((r = WEXITSTATUS(status)) != 42) my_e(r);
1177 /* The number of events seen is deterministic but libc-dependent. */
1178 if (i < 10 || i > 100) my_e(12);
1180 /* The last system call event must be for entering exit(). */
1181 if (!(i % 2)) my_e(13);
1183 traced_wait();
1186 void test_tracefork_child()
1188 pid_t pid;
1190 signal(SIGHUP, SIG_IGN);
1192 pid = setsid();
1194 WRITE(pid);
1196 if (READ() != 0) my_e(100);
1198 if ((pid = fork()) < 0) my_e(101);
1200 exit(pid > 0 ? 42 : 84);
1203 void test_tracefork()
1205 pid_t pgrp, ppid, cpid, wpid;
1206 int r, status, gotstop, ptraps, ctraps;
1208 subtest = 14;
1210 ppid = traced_fork(test_tracefork_child);
1212 if ((pgrp = READ()) <= 0) my_e(1);
1214 if (kill(ppid, SIGSTOP) != 0) my_e(2);
1216 if (waitpid(ppid, &status, 0) != ppid) my_e(3);
1217 if (!_WIFSTOPPED(status)) my_e(4);
1218 if (WSTOPSIG(status) != SIGSTOP) my_e(5);
1220 if (ptrace(T_SETOPT, ppid, 0, TO_TRACEFORK) != 0) my_e(6);
1222 WRITE(0);
1224 if (ptrace(T_SYSCALL, ppid, 0, 0) != 0) my_e(7);
1226 cpid = -1;
1227 gotstop = -1;
1229 /* Count how many traps we get for parent and child, until they both exit. */
1230 for (ptraps = ctraps = 0; ppid || cpid; ) {
1231 wpid = waitpid(-pgrp, &status, 0);
1233 if (wpid <= 0) my_e(8);
1234 if (cpid < 0 && wpid != ppid) {
1235 cpid = wpid;
1236 gotstop = 0;
1238 if (wpid != ppid && wpid != cpid) my_e(9);
1240 if (_WIFEXITED(status)) {
1241 if (wpid == ppid) {
1242 if ((r = WEXITSTATUS(status)) != 42) my_e(r);
1243 ppid = 0;
1245 else {
1246 if ((r = WEXITSTATUS(status)) != 84) my_e(r);
1247 cpid = 0;
1250 else {
1251 if (!_WIFSTOPPED(status)) my_e(10);
1253 switch (WSTOPSIG(status)) {
1254 case SIGCHLD:
1255 case SIGHUP:
1256 break;
1257 case SIGSTOP:
1258 if (wpid != cpid) my_e(11);
1259 if (gotstop) my_e(12);
1260 gotstop = 1;
1261 break;
1262 case SIGTRAP:
1263 if (wpid == ppid) ptraps++;
1264 else ctraps++;
1265 break;
1266 default:
1267 my_e(13);
1270 if (ptrace(T_SYSCALL, wpid, 0, 0) != 0) my_e(14);
1274 /* The parent should get an odd number of traps: the first one is a syscall
1275 * enter trap (typically for the fork()), the last one is the syscall enter
1276 * trap for its exit().
1278 if (ptraps < 3) my_e(15);
1279 if (!(ptraps % 2)) my_e(16);
1281 /* The child should get an even number of traps: the first one is a syscall
1282 * leave trap from the fork(), the last one is the syscall enter trap for
1283 * its exit().
1285 if (ctraps < 2) my_e(17);
1286 if (ctraps % 2) my_e(18);
1288 traced_wait();
1291 void sigexec(setflag, opt, traps, stop)
1292 int setflag;
1293 int opt;
1294 int *traps;
1295 int *stop;
1297 pid_t pid;
1298 int r, status;
1300 pid = traced_fork(test_exec_child);
1302 if (kill(pid, SIGSTOP) != 0) my_e(1);
1304 if (waitpid(pid, &status, 0) != pid) my_e(2);
1305 if (!_WIFSTOPPED(status)) my_e(3);
1306 if (WSTOPSIG(status) != SIGSTOP) my_e(4);
1308 if (setflag && ptrace(T_SETOPT, pid, 0, opt) != 0) my_e(5);
1310 WRITE(0);
1312 if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(6);
1314 *traps = 0;
1315 *stop = -1;
1317 for (;;) {
1318 if (waitpid(pid, &status, 0) != pid) my_e(7);
1320 if (_WIFEXITED(status)) break;
1322 if (!_WIFSTOPPED(status)) my_e(8);
1324 switch (WSTOPSIG(status)) {
1325 case SIGTRAP:
1326 (*traps)++;
1327 break;
1328 case SIGSTOP:
1329 if (*stop >= 0) my_e(9);
1330 *stop = *traps;
1331 break;
1332 default:
1333 my_e(10);
1336 if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(11);
1339 if ((r = WEXITSTATUS(status)) != 42) my_e(r);
1341 traced_wait();
1344 void test_trapexec()
1346 int traps, stop;
1348 subtest = 15;
1350 sigexec(1, 0, &traps, &stop);
1352 /* The exec does not cause a SIGSTOP. This gives us an even number of traps;
1353 * as above, but plus the exec()'s extra SIGTRAP. This trap is
1354 * indistinguishable from a syscall trap, especially when considering failed
1355 * exec() calls and immediately following signal handler invocations.
1357 if (traps < 4) my_e(12);
1358 if (traps % 2) my_e(13);
1359 if (stop >= 0) my_e(14);
1362 void test_altexec()
1364 int traps, stop;
1366 subtest = 16;
1368 sigexec(1, TO_ALTEXEC, &traps, &stop);
1370 /* The exec causes a SIGSTOP. This gives us an odd number of traps: a pair
1371 * for each system call, plus one for the final exit(). The stop must have
1372 * taken place after a syscall enter event, i.e. must be odd as well.
1374 if (traps < 3) my_e(12);
1375 if (!(traps % 2)) my_e(13);
1376 if (stop < 0) my_e(14);
1377 if (!(stop % 2)) my_e(15);
1380 void test_noexec()
1382 int traps, stop;
1384 subtest = 17;
1386 sigexec(1, TO_NOEXEC, &traps, &stop);
1388 /* The exec causes no signal at all. As above, but without the SIGSTOPs. */
1389 if (traps < 3) my_e(12);
1390 if (!(traps % 2)) my_e(13);
1391 if (stop >= 0) my_e(14);
1394 void test_defexec()
1396 int traps, stop;
1398 /* We want to test the default of T_OK (0) and T_ATTACH (TO_NOEXEC). */
1399 if (attach != 0 && attach != 1) return;
1401 subtest = 18;
1403 /* Do not set any options this time. */
1404 sigexec(0, 0, &traps, &stop);
1406 /* See above. */
1407 if (attach == 0) {
1408 if (traps < 4) my_e(12);
1409 if (traps % 2) my_e(13);
1410 if (stop >= 0) my_e(14);
1412 else {
1413 if (traps < 3) my_e(15);
1414 if (!(traps % 2)) my_e(16);
1415 if (stop >= 0) my_e(17);
1419 void test_reattach_child()
1421 struct timeval tv;
1423 if (READ() != 0) my_e(100);
1425 tv.tv_sec = 2;
1426 tv.tv_usec = 0;
1427 if (select(0, NULL, NULL, NULL, &tv) != 0) my_e(101);
1429 exit(42);
1432 void test_reattach()
1434 pid_t pid;
1435 int r, status, count;
1437 subtest = 19;
1439 pid = traced_fork(test_reattach_child);
1441 if (kill(pid, SIGSTOP) != 0) my_e(1);
1443 if (waitpid(pid, &status, 0) != pid) my_e(2);
1444 if (!_WIFSTOPPED(status)) my_e(3);
1445 if (WSTOPSIG(status) != SIGSTOP) my_e(4);
1447 WRITE(0);
1449 signal(SIGALRM, dummy_handler);
1450 alarm(1);
1452 /* Start tracing system calls. We don't know how many there will be until
1453 * we reach the child's select(), so we have to interrupt ourselves.
1454 * The hard assumption here is that the child is able to enter the select()
1455 * within a second, despite being traced. If this is not the case, the test
1456 * may hang or fail, and the child may die from a SIGTRAP.
1458 if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(5);
1460 for (count = 0; (r = waitpid(pid, &status, 0)) == pid; count++) {
1461 if (!_WIFSTOPPED(status)) my_e(6);
1462 if (WSTOPSIG(status) != SIGTRAP) my_e(7);
1464 if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(8);
1467 if (r != -1 || errno != EINTR) my_e(9);
1469 /* We always start with syscall enter event; the last event we should have
1470 * seen before the alarm was entering the select() call.
1472 if (!(count % 2)) my_e(10);
1474 /* Detach, and immediately attach again. */
1475 detach_running(pid);
1477 if (ptrace(T_ATTACH, pid, 0, 0) != 0) my_e(11);
1479 if (waitpid(pid, &status, 0) != pid) my_e(12);
1480 if (!_WIFSTOPPED(status)) my_e(13);
1481 if (WSTOPSIG(status) != SIGSTOP) my_e(14);
1483 if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(15);
1485 if (waitpid(pid, &status, 0) != pid) my_e(16);
1487 for (count = 0; _WIFSTOPPED(status); count++) {
1488 if (WSTOPSIG(status) != SIGTRAP) my_e(17);
1490 if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(18);
1492 if (waitpid(pid, &status, 0) != pid) my_e(19);
1495 if (!_WIFEXITED(status)) my_e(20);
1496 if ((r = WEXITSTATUS(status)) != 42) my_e(r);
1498 /* We must not have seen the select()'s syscall leave event, and the last
1499 * event will be the syscall enter for the exit().
1501 if (!(count % 2)) my_e(21);
1503 traced_wait();