xref: /qemu/tests/tcg/multiarch/munmap-pthread.c (revision c3bef3b4)
1 /* Test that munmap() and thread creation do not race. */
2 #include <assert.h>
3 #include <pthread.h>
4 #include <stdbool.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/mman.h>
8 #include <unistd.h>
9 
10 #include "nop_func.h"
11 
12 static void *thread_mmap_munmap(void *arg)
13 {
14     volatile bool *run = arg;
15     char *p;
16     int ret;
17 
18     while (*run) {
19         p = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC,
20                  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
21         assert(p != MAP_FAILED);
22 
23         /* Create a small translation block.  */
24         memcpy(p, nop_func, sizeof(nop_func));
25         ((void(*)(void))p)();
26 
27         ret = munmap(p, getpagesize());
28         assert(ret == 0);
29     }
30 
31     return NULL;
32 }
33 
34 static void *thread_dummy(void *arg)
35 {
36     return NULL;
37 }
38 
39 int main(void)
40 {
41     pthread_t mmap_munmap, dummy;
42     volatile bool run = true;
43     int i, ret;
44 
45     /* Without a template, nothing to test. */
46     if (sizeof(nop_func) == 0) {
47         return EXIT_SUCCESS;
48     }
49 
50     ret = pthread_create(&mmap_munmap, NULL, thread_mmap_munmap, (void *)&run);
51     assert(ret == 0);
52 
53     for (i = 0; i < 1000; i++) {
54         ret = pthread_create(&dummy, NULL, thread_dummy, NULL);
55         assert(ret == 0);
56         ret = pthread_join(dummy, NULL);
57         assert(ret == 0);
58     }
59 
60     run = false;
61     ret = pthread_join(mmap_munmap, NULL);
62     assert(ret == 0);
63 
64     return EXIT_SUCCESS;
65 }
66