]> granicus.if.org Git - strace/blob - tests/pc.c
tests: robustify pc.test
[strace] / tests / pc.c
1 #include <fcntl.h>
2 #include <unistd.h>
3 #include <sys/mman.h>
4 #include <sys/wait.h>
5 #include <sys/sendfile.h>
6
7 int main(void)
8 {
9         const unsigned long size = sysconf(_SC_PAGESIZE);
10
11         /* write instruction pointer length to the log */
12         if (write(-1, NULL, 2 * sizeof(void *)) >= 0)
13                 return 77;
14
15         /* just a noticeable line in the log */
16         if (munmap(&main, 0) >= 0)
17                 return 77;
18
19         int pid = fork();
20         if (pid < 0)
21                 return 77;
22
23         if (!pid) {
24                 const unsigned long mask = ~(size - 1);
25                 const unsigned long addr = (unsigned long) &main;
26
27                 /* SIGSEGV is expected */
28                 (void) munmap((void *) ((addr & mask) - size * 2), size * 4);
29                 (void) munmap((void *) ((addr & mask) - size * 2), size * 4);
30                 return 77;
31         }
32
33         int status;
34         if (wait(&status) != pid ||
35             !WIFSIGNALED(status) ||
36             WTERMSIG(status) != SIGSEGV)
37                 return 77;
38
39         /* dump process map for debug purposes */
40         close(0);
41         if (!open("/proc/self/maps", O_RDONLY))
42                 (void) sendfile(1, 0, NULL, size);
43
44         return 0;
45 }