Skip to content

Commit a04a72f

Browse files
committed
tests: add mmap_private test
1 parent affe470 commit a04a72f

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

bin/utest/mmap.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "util.h"
33

44
#include <errno.h>
5+
#include <sched.h>
56
#include <setjmp.h>
67
#include <signal.h>
78
#include <stdint.h>
@@ -124,6 +125,62 @@ TEST_ADD(munmap) {
124125
return 0;
125126
}
126127

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+
127184
#define NPAGES 8
128185

129186
TEST_ADD(mmap_prot_none) {

sys/tests/utest.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ UTEST_ADD(vmmap_rodata_access);
6868

6969
UTEST_ADD(mmap);
7070
UTEST_ADD(munmap);
71+
UTEST_ADD(mmap_private);
7172
UTEST_ADD(munmap_sigsegv);
7273
UTEST_ADD(munmap_many_1);
7374
UTEST_ADD(munmap_many_2);

0 commit comments

Comments
 (0)