|
2 | 2 | #include "util.h" |
3 | 3 |
|
4 | 4 | #include <errno.h> |
| 5 | +#include <sched.h> |
5 | 6 | #include <setjmp.h> |
6 | 7 | #include <signal.h> |
7 | 8 | #include <stdint.h> |
@@ -124,6 +125,62 @@ TEST_ADD(munmap) { |
124 | 125 | return 0; |
125 | 126 | } |
126 | 127 |
|
| 128 | +static volatile int sigcont_handled = 0; |
| 129 | + |
| 130 | +static void sigcont_handler(int signo) { |
| 131 | + printf("sigusr1 handled!\n"); |
| 132 | + sigcont_handled = 1; |
| 133 | +} |
| 134 | + |
| 135 | +TEST_ADD(mmap_private) { |
| 136 | + signal(SIGCONT, sigcont_handler); |
| 137 | + char *addr; |
| 138 | + int ppid = getpid(); |
| 139 | + size_t pgsz = getpagesize(); |
| 140 | + |
| 141 | + /* mmap & munmap one page */ |
| 142 | + addr = mmap_anon_prw(NULL, pgsz); |
| 143 | + assert(addr != (char *)MAP_FAILED); |
| 144 | + |
| 145 | + pid_t pid = fork(); |
| 146 | + assert(pid >= 0); |
| 147 | + |
| 148 | + sprintf(addr, "parent"); |
| 149 | + |
| 150 | + if (pid == 0) { |
| 151 | + /* child */ |
| 152 | + printf("Child read: '%s'\n", addr); |
| 153 | + /* Check and modify. */ |
| 154 | + string_eq(addr, "parent"); |
| 155 | + sprintf(addr, "child"); |
| 156 | + printf("Child written: '%s'\n", addr); |
| 157 | + |
| 158 | + /* Wait for parent to check and modify its memory. */ |
| 159 | + kill(ppid, SIGCONT); |
| 160 | + while (!sigcont_handled) |
| 161 | + sched_yield(); |
| 162 | + |
| 163 | + printf("Child read again: '%s'\n", addr); |
| 164 | + string_eq(addr, "child"); |
| 165 | + return 0; |
| 166 | + } |
| 167 | + |
| 168 | + /* Wait for child to check and modify its memory. */ |
| 169 | + while (!sigcont_handled) |
| 170 | + sched_yield(); |
| 171 | + |
| 172 | + printf("Parent read: '%s'\n", addr); |
| 173 | + /* Check and modify. */ |
| 174 | + string_eq(addr, "parent"); |
| 175 | + sprintf(addr, "parent again"); |
| 176 | + |
| 177 | + /* Resume child. */ |
| 178 | + kill(pid, SIGCONT); |
| 179 | + |
| 180 | + wait_for_child_exit(pid, 0); |
| 181 | + return 0; |
| 182 | +} |
| 183 | + |
127 | 184 | #define NPAGES 8 |
128 | 185 |
|
129 | 186 | TEST_ADD(mmap_prot_none) { |
|
0 commit comments