Dmitry V. Levin | 1c5fead | 2015-02-23 22:37:40 +0000 | [diff] [blame^] | 1 | #include <fcntl.h> |
Dmitry V. Levin | e96cb62 | 2015-02-15 15:52:02 +0000 | [diff] [blame] | 2 | #include <unistd.h> |
| 3 | #include <sys/mman.h> |
| 4 | #include <sys/wait.h> |
Dmitry V. Levin | 1c5fead | 2015-02-23 22:37:40 +0000 | [diff] [blame^] | 5 | #include <sys/sendfile.h> |
Dmitry V. Levin | e96cb62 | 2015-02-15 15:52:02 +0000 | [diff] [blame] | 6 | |
| 7 | int main(void) |
| 8 | { |
| 9 | const unsigned long size = sysconf(_SC_PAGESIZE); |
Dmitry V. Levin | e96cb62 | 2015-02-15 15:52:02 +0000 | [diff] [blame] | 10 | |
| 11 | /* write instruction pointer length to the log */ |
| 12 | if (write(-1, NULL, 2 * sizeof(void *)) >= 0) |
| 13 | return 77; |
Dmitry V. Levin | 1c5fead | 2015-02-23 22:37:40 +0000 | [diff] [blame^] | 14 | |
Dmitry V. Levin | e96cb62 | 2015-02-15 15:52:02 +0000 | [diff] [blame] | 15 | /* just a noticeable line in the log */ |
Dmitry V. Levin | 1c5fead | 2015-02-23 22:37:40 +0000 | [diff] [blame^] | 16 | if (munmap(&main, 0) >= 0) |
Dmitry V. Levin | e96cb62 | 2015-02-15 15:52:02 +0000 | [diff] [blame] | 17 | return 77; |
| 18 | |
| 19 | int pid = fork(); |
| 20 | if (pid < 0) |
| 21 | return 77; |
| 22 | |
| 23 | if (!pid) { |
Dmitry V. Levin | 1c5fead | 2015-02-23 22:37:40 +0000 | [diff] [blame^] | 24 | const unsigned long mask = ~(size - 1); |
| 25 | const unsigned long addr = (unsigned long) &main; |
| 26 | |
Dmitry V. Levin | e96cb62 | 2015-02-15 15:52:02 +0000 | [diff] [blame] | 27 | /* SIGSEGV is expected */ |
Dmitry V. Levin | 1c5fead | 2015-02-23 22:37:40 +0000 | [diff] [blame^] | 28 | (void) munmap((void *) ((addr & mask) - size * 2), size * 4); |
| 29 | (void) munmap((void *) ((addr & mask) - size * 2), size * 4); |
Dmitry V. Levin | e96cb62 | 2015-02-15 15:52:02 +0000 | [diff] [blame] | 30 | return 77; |
| 31 | } |
| 32 | |
| 33 | int status; |
| 34 | if (wait(&status) != pid || |
| 35 | !WIFSIGNALED(status) || |
| 36 | WTERMSIG(status) != SIGSEGV) |
| 37 | return 77; |
| 38 | |
Dmitry V. Levin | 1c5fead | 2015-02-23 22:37:40 +0000 | [diff] [blame^] | 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 | |
Dmitry V. Levin | e96cb62 | 2015-02-15 15:52:02 +0000 | [diff] [blame] | 44 | return 0; |
| 45 | } |