vm: use arch_map2str to print pagefault info, to properly display code addrs
[minix.git] / test / test42.c
blob39787821b795f587975c44a50d8cd9c1cecdf466
1 /* Tests for MINIX3 ptrace(2) - by D.C. van Moolenbroek */
2 #define _POSIX_SOURCE 1
3 #include <setjmp.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <signal.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <sys/wait.h>
11 #include <sys/select.h>
12 #include <sys/ptrace.h>
14 #define ITERATIONS 3
15 #define MAX_ERROR 4
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;
78 static pid_t ppid;
79 static int pfd[4];
80 static int sigs, caught;
82 int main(argc, argv)
83 int argc;
84 char **argv;
86 int i, m = 0xFFFFFF, n = 0xF;
88 if (strcmp(argv[0], "DO CHECK") == 0) {
89 exit(42);
92 printf("Test 42 ");
93 fflush(stdout);
95 executable = argv[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);
107 quit();
108 return(-1); /* impossible */
111 void test(m, a)
112 int m;
113 int a;
115 attach = a;
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);
143 e(700);
144 quit();
145 exit(-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; }
154 alarm(60);
155 func();
156 alarm(0);
158 else
160 /* report timeout as error */
161 printf("timeout in %s\n", s);
162 e(702);
166 pid_t traced_fork(c)
167 _PROTOTYPE(void (*c), (void));
169 pid_t pid;
170 int r, status;
172 if (pipe(pfd) != 0) e(200);
173 if (pipe(&pfd[2]) != 0) e(201);
175 switch (attach) {
176 case 0: /* let child volunteer to be traced */
177 pid = fork();
179 if (pid < 0) e(202);
181 if (pid == 0) {
182 child = 1;
184 if (ptrace(T_OK, 0, 0, 0) != 0) e(203);
186 WRITE(0);
188 c();
190 e(204);
193 if (READ() != 0) e(205);
195 break;
197 case 1: /* attach to child process */
198 pid = fork();
200 if (pid < 0) e(206);
202 if (pid == 0) {
203 child = 1;
205 if (READ() != 0) e(207);
207 c();
209 e(208);
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);
220 WRITE(0);
222 break;
224 case 2: /* attach to non-child process */
225 ppid = fork();
227 if (ppid < 0) e(214);
229 if (ppid == 0) {
230 pid = fork();
232 if (pid < 0) exit(215);
234 if (pid == 0) {
235 child = 1;
237 if (READ() != 0) e(216);
239 c();
241 e(217);
244 child = 1;
246 WRITE(pid);
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);
252 exit(0);
255 pid = READ();
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);
265 WRITE(0);
267 break;
269 case 3: /* attach by forking from child */
270 ppid = fork();
272 if (ppid < 0) e(225);
274 if (ppid == 0) {
275 child = 1;
277 if (ptrace(T_OK, 0, 0, 0) != 0) e(226);
279 WRITE(0);
281 if (READ() != 0) e(227);
283 pid = fork();
285 if (pid < 0) e(228);
287 if (pid == 0) {
288 c();
290 e(229);
293 WRITE(pid);
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);
299 exit(0);
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);
314 WRITE(0);
316 pid = READ();
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);
327 break;
330 return pid;
333 pid_t traced_pfork(c)
334 _PROTOTYPE(void (*c), (void));
336 pid_t pid;
338 if (pipe(pfd) != 0) e(300);
339 if (pipe(&pfd[2]) != 0) e(301);
341 pid = fork();
343 if (pid < 0) e(302);
345 if (pid == 0) {
346 child = 1;
348 c();
350 e(303);
353 return pid;
356 void WRITE(value)
357 int value;
359 if (write(pfd[child*2+1], &value, sizeof(value)) != sizeof(value)) e(400);
362 int READ()
364 int value;
366 if (read(pfd[2-child*2], &value, sizeof(value)) != sizeof(value)) e(401);
368 return value;
371 void traced_wait()
373 int r, status;
375 if (attach == 2) {
376 if (waitpid(ppid, &status, 0) != ppid) e(500);
377 if (!_WIFEXITED(status)) e(501);
378 if ((r = WEXITSTATUS(status)) != 0) e(r);
380 else {
381 /* Quick hack to clean up detached children */
382 waitpid(-1, NULL, WNOHANG);
385 close(pfd[0]);
386 close(pfd[1]);
387 close(pfd[2]);
388 close(pfd[3]);
391 void detach_running(pid)
392 pid_t 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
402 * will not happen.
404 int status;
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);
414 return;
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. */
426 e(606);
429 void dummy_handler(sig)
430 int sig;
434 void exit_handler(sig)
435 int sig;
437 exit(42);
440 void count_handler(sig)
441 int sig;
443 sigs++;
446 void catch_handler(sig)
447 int sig;
449 sigset_t set;
450 int bit;
452 switch (sig) {
453 case SIGUSR1: bit = 1; break;
454 case SIGUSR2: bit = 2; break;
455 case SIGTERM: bit = 4; break;
456 default: e(100);
459 sigfillset(&set);
460 sigprocmask(SIG_SETMASK, &set, NULL);
462 if (caught & bit) e(101);
463 caught |= bit;
466 void test_wait_child()
468 exit(42);
471 void test_wait()
473 pid_t pid;
474 int status;
476 subtest = 1;
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);
484 traced_wait();
487 void test_exec_child()
489 if (READ() != 0) e(100);
491 execl(executable, "DO CHECK", NULL);
493 e(101);
496 void test_exec()
498 pid_t pid;
499 int r, status;
501 /* This test covers the T_OK case. */
502 if (attach != 0) return;
504 subtest = 2;
506 pid = traced_fork(test_exec_child);
508 WRITE(0);
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);
521 traced_wait();
524 void test_step_child()
526 sigset_t set;
528 signal(SIGUSR1, SIG_IGN);
530 WRITE(0);
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);
536 sigfillset(&set);
537 sigprocmask(SIG_SETMASK, &set, NULL);
539 exit(42);
542 void test_step()
544 pid_t pid;
545 int r, status, count;
547 subtest = 3;
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);
560 WRITE(0);
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 */
581 traced_wait();
584 void test_sig_child()
586 signal(SIGUSR1, exit_handler);
588 if (READ() != 0) e(100);
590 pause();
592 e(101);
595 void test_sig()
597 pid_t pid;
598 int r, sig, status;
600 subtest = 4;
602 pid = traced_fork(test_sig_child);
604 WRITE(0);
606 /* allow the child to enter the pause */
607 sleep(1);
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 */
645 sleep(1);
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);
662 traced_wait();
665 void test_exit_child()
667 WRITE(0);
669 for(;;);
672 void test_exit()
674 pid_t pid;
675 int r, status;
677 subtest = 5;
679 pid = traced_fork(test_exit_child);
681 if (READ() != 0) e(1);
683 sleep(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);
701 traced_wait();
704 void test_term_child()
706 signal(SIGUSR1, SIG_DFL);
707 signal(SIGUSR2, dummy_handler);
709 WRITE(0);
711 pause();
713 e(100);
716 void test_term()
718 pid_t pid;
719 int status;
721 subtest = 6;
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);
750 traced_wait();
753 void test_catch_child()
755 struct sigaction sa;
756 sigset_t set, oset;
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);
766 sigfillset(&set);
767 sigprocmask(SIG_SETMASK, &set, &oset);
769 caught = 0;
771 WRITE(0);
773 while (caught != 7) sigsuspend(&oset);
775 exit(42);
778 void test_catch()
780 pid_t pid;
781 int r, sig, status;
783 subtest = 7;
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);
828 traced_wait();
831 void test_kill_child()
833 sigset_t set;
835 signal(SIGKILL, SIG_IGN);
836 sigfillset(&set);
837 sigprocmask(SIG_SETMASK, &set, NULL);
839 WRITE(0);
841 pause();
843 e(100);
846 void test_kill()
848 pid_t pid;
849 int status;
851 subtest = 8;
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);
868 traced_wait();
871 void test_attach_child()
873 if (ptrace(T_OK, 0, 0, 0) != -1) e(100);
874 if (errno != EBUSY) e(101);
876 WRITE(0);
878 if (READ() != 0) e(102);
880 exit(42);
883 void test_attach()
885 pid_t pid;
886 int r, status;
888 subtest = 9;
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. */
899 #if 0
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);
903 #endif
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);
917 detach_running(pid);
919 WRITE(0);
921 traced_wait();
924 void test_detach_child()
926 struct sigaction sa;
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);
937 sigfillset(&set);
938 sigprocmask(SIG_SETMASK, &set, &oset);
940 sigfillset(&sset);
941 sigdelset(&sset, SIGUSR1);
943 caught = 0;
945 WRITE(0);
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);
956 exit(42);
959 void test_detach()
961 pid_t pid;
962 int r, status;
964 /* Can't use traced_fork(), so simplify a bit */
965 if (attach != 0) return;
967 subtest = 10;
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);
996 WRITE(0);
998 if (waitpid(pid, &status, 0) != pid) e(14);
999 if (!_WIFEXITED(status)) e(15);
1000 if ((r = WEXITSTATUS(status)) != 42) e(r);
1002 traced_wait();
1005 void test_death_child()
1007 pid_t pid;
1009 pid = fork();
1011 if (pid < 0) e(100);
1013 if (pid == 0) {
1014 ptrace(T_OK, 0, 0, 0);
1016 WRITE(getpid());
1018 for (;;) pause();
1021 if (READ() != 0) e(101);
1023 kill(getpid(), SIGKILL);
1025 e(102);
1028 void test_death()
1030 pid_t pid, cpid;
1031 int status;
1033 subtest = 11;
1035 pid = traced_fork(test_death_child);
1037 cpid = READ();
1039 if (kill(cpid, 0) != 0) e(1);
1041 WRITE(0);
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);
1051 traced_wait();
1054 void test_zdeath_child()
1056 if (READ() != 0) e(100);
1058 exit(42);
1061 void test_zdeath()
1063 pid_t pid, tpid;
1064 int r, status;
1066 /* Can't use traced_fork(), so simplify a bit */
1067 if (attach != 0) return;
1069 subtest = 12;
1071 pid = traced_pfork(test_zdeath_child);
1073 tpid = fork();
1075 if (tpid < 0) e(1);
1077 if (tpid == 0) {
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);
1086 WRITE(0);
1088 /* Unwaited-for traced zombies should be passed to their parent. */
1089 sleep(2);
1091 exit(84);
1094 sleep(1);
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);
1107 traced_wait();
1110 void test_syscall_child()
1112 signal(SIGUSR1, count_handler);
1113 signal(SIGUSR2, count_handler);
1115 sigs = 0;
1117 WRITE(0);
1119 if (READ() != 0) e(100);
1121 /* Three calls (may fail) */
1122 setuid(0);
1123 close(123);
1124 getpid();
1126 if (sigs != 2) e(101);
1128 exit(42);
1131 void test_syscall()
1133 pid_t pid;
1134 int i, r, sig, status;
1136 subtest = 13;
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);
1148 WRITE(0);
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;
1161 else sig = 0;
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);
1177 traced_wait();
1180 void test_tracefork_child()
1182 pid_t pid;
1184 signal(SIGHUP, SIG_IGN);
1186 pid = setsid();
1188 WRITE(pid);
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;
1202 subtest = 14;
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);
1216 WRITE(0);
1218 if (ptrace(T_SYSCALL, ppid, 0, 0) != 0) e(7);
1220 cpid = -1;
1221 gotstop = -1;
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) {
1229 cpid = wpid;
1230 gotstop = 0;
1232 if (wpid != ppid && wpid != cpid) e(9);
1234 if (_WIFEXITED(status)) {
1235 if (wpid == ppid) {
1236 if ((r = WEXITSTATUS(status)) != 42) e(r);
1237 ppid = 0;
1239 else {
1240 if ((r = WEXITSTATUS(status)) != 84) e(r);
1241 cpid = 0;
1244 else {
1245 if (!_WIFSTOPPED(status)) e(10);
1247 switch (WSTOPSIG(status)) {
1248 case SIGCHLD:
1249 case SIGHUP:
1250 break;
1251 case SIGSTOP:
1252 if (wpid != cpid) e(11);
1253 if (gotstop) e(12);
1254 gotstop = 1;
1255 break;
1256 case SIGTRAP:
1257 if (wpid == ppid) ptraps++;
1258 else ctraps++;
1259 break;
1260 default:
1261 e(13);
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
1277 * its exit().
1279 if (ctraps < 2) e(17);
1280 if (ctraps % 2) e(18);
1282 traced_wait();
1285 void sigexec(setflag, opt, traps, stop)
1286 int setflag;
1287 int opt;
1288 int *traps;
1289 int *stop;
1291 pid_t pid;
1292 int r, status;
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);
1304 WRITE(0);
1306 if (ptrace(T_SYSCALL, pid, 0, 0) != 0) e(6);
1308 *traps = 0;
1309 *stop = -1;
1311 for (;;) {
1312 if (waitpid(pid, &status, 0) != pid) e(7);
1314 if (_WIFEXITED(status)) break;
1316 if (!_WIFSTOPPED(status)) e(8);
1318 switch (WSTOPSIG(status)) {
1319 case SIGTRAP:
1320 (*traps)++;
1321 break;
1322 case SIGSTOP:
1323 if (*stop >= 0) e(9);
1324 *stop = *traps;
1325 break;
1326 default:
1327 e(10);
1330 if (ptrace(T_SYSCALL, pid, 0, 0) != 0) e(11);
1333 if ((r = WEXITSTATUS(status)) != 42) e(r);
1335 traced_wait();
1338 void test_trapexec()
1340 int traps, stop;
1342 subtest = 15;
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);
1356 void test_altexec()
1358 int traps, stop;
1360 subtest = 16;
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);
1374 void test_noexec()
1376 int traps, stop;
1378 subtest = 17;
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);
1388 void test_defexec()
1390 int traps, stop;
1392 /* We want to test the default of T_OK (0) and T_ATTACH (TO_NOEXEC). */
1393 if (attach != 0 && attach != 1) return;
1395 subtest = 18;
1397 /* Do not set any options this time. */
1398 sigexec(0, 0, &traps, &stop);
1400 /* See above. */
1401 if (attach == 0) {
1402 if (traps < 4) e(12);
1403 if (traps % 2) e(13);
1404 if (stop >= 0) e(14);
1406 else {
1407 if (traps < 3) e(15);
1408 if (!(traps % 2)) e(16);
1409 if (stop >= 0) e(17);
1413 void test_reattach_child()
1415 struct timeval tv;
1417 if (READ() != 0) e(100);
1419 tv.tv_sec = 2;
1420 tv.tv_usec = 0;
1421 if (select(0, NULL, NULL, NULL, &tv) != 0) e(101);
1423 exit(42);
1426 void test_reattach()
1428 pid_t pid;
1429 int r, status, count;
1431 subtest = 19;
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);
1441 WRITE(0);
1443 signal(SIGALRM, dummy_handler);
1444 alarm(1);
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);
1497 traced_wait();
1500 void e(n)
1501 int n;
1504 if (child) exit(n);
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");
1511 exit(1);
1515 void quit()
1517 if (errct == 0) {
1518 printf("ok\n");
1519 exit(0);
1520 } else {
1521 printf("%d errors\n", errct);
1522 exit(1);