1*83d92559SIlya Leoshkevich /* Test that munmap() and thread creation do not race. */ 2*83d92559SIlya Leoshkevich #include <assert.h> 3*83d92559SIlya Leoshkevich #include <pthread.h> 4*83d92559SIlya Leoshkevich #include <stdbool.h> 5*83d92559SIlya Leoshkevich #include <stdlib.h> 6*83d92559SIlya Leoshkevich #include <string.h> 7*83d92559SIlya Leoshkevich #include <sys/mman.h> 8*83d92559SIlya Leoshkevich #include <unistd.h> 9*83d92559SIlya Leoshkevich 10*83d92559SIlya Leoshkevich static const char nop_func[] = { 11*83d92559SIlya Leoshkevich #if defined(__aarch64__) 12*83d92559SIlya Leoshkevich 0xc0, 0x03, 0x5f, 0xd6, /* ret */ 13*83d92559SIlya Leoshkevich #elif defined(__alpha__) 14*83d92559SIlya Leoshkevich 0x01, 0x80, 0xFA, 0x6B, /* ret */ 15*83d92559SIlya Leoshkevich #elif defined(__arm__) 16*83d92559SIlya Leoshkevich 0x1e, 0xff, 0x2f, 0xe1, /* bx lr */ 17*83d92559SIlya Leoshkevich #elif defined(__riscv) 18*83d92559SIlya Leoshkevich 0x67, 0x80, 0x00, 0x00, /* ret */ 19*83d92559SIlya Leoshkevich #elif defined(__s390__) 20*83d92559SIlya Leoshkevich 0x07, 0xfe, /* br %r14 */ 21*83d92559SIlya Leoshkevich #elif defined(__i386__) || defined(__x86_64__) 22*83d92559SIlya Leoshkevich 0xc3, /* ret */ 23*83d92559SIlya Leoshkevich #endif 24*83d92559SIlya Leoshkevich }; 25*83d92559SIlya Leoshkevich 26*83d92559SIlya Leoshkevich static void *thread_mmap_munmap(void *arg) 27*83d92559SIlya Leoshkevich { 28*83d92559SIlya Leoshkevich volatile bool *run = arg; 29*83d92559SIlya Leoshkevich char *p; 30*83d92559SIlya Leoshkevich int ret; 31*83d92559SIlya Leoshkevich 32*83d92559SIlya Leoshkevich while (*run) { 33*83d92559SIlya Leoshkevich p = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC, 34*83d92559SIlya Leoshkevich MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 35*83d92559SIlya Leoshkevich assert(p != MAP_FAILED); 36*83d92559SIlya Leoshkevich 37*83d92559SIlya Leoshkevich /* Create a small translation block. */ 38*83d92559SIlya Leoshkevich memcpy(p, nop_func, sizeof(nop_func)); 39*83d92559SIlya Leoshkevich ((void(*)(void))p)(); 40*83d92559SIlya Leoshkevich 41*83d92559SIlya Leoshkevich ret = munmap(p, getpagesize()); 42*83d92559SIlya Leoshkevich assert(ret == 0); 43*83d92559SIlya Leoshkevich } 44*83d92559SIlya Leoshkevich 45*83d92559SIlya Leoshkevich return NULL; 46*83d92559SIlya Leoshkevich } 47*83d92559SIlya Leoshkevich 48*83d92559SIlya Leoshkevich static void *thread_dummy(void *arg) 49*83d92559SIlya Leoshkevich { 50*83d92559SIlya Leoshkevich return NULL; 51*83d92559SIlya Leoshkevich } 52*83d92559SIlya Leoshkevich 53*83d92559SIlya Leoshkevich int main(void) 54*83d92559SIlya Leoshkevich { 55*83d92559SIlya Leoshkevich pthread_t mmap_munmap, dummy; 56*83d92559SIlya Leoshkevich volatile bool run = true; 57*83d92559SIlya Leoshkevich int i, ret; 58*83d92559SIlya Leoshkevich 59*83d92559SIlya Leoshkevich /* Without a template, nothing to test. */ 60*83d92559SIlya Leoshkevich if (sizeof(nop_func) == 0) { 61*83d92559SIlya Leoshkevich return EXIT_SUCCESS; 62*83d92559SIlya Leoshkevich } 63*83d92559SIlya Leoshkevich 64*83d92559SIlya Leoshkevich ret = pthread_create(&mmap_munmap, NULL, thread_mmap_munmap, (void *)&run); 65*83d92559SIlya Leoshkevich assert(ret == 0); 66*83d92559SIlya Leoshkevich 67*83d92559SIlya Leoshkevich for (i = 0; i < 1000; i++) { 68*83d92559SIlya Leoshkevich ret = pthread_create(&dummy, NULL, thread_dummy, NULL); 69*83d92559SIlya Leoshkevich assert(ret == 0); 70*83d92559SIlya Leoshkevich ret = pthread_join(dummy, NULL); 71*83d92559SIlya Leoshkevich assert(ret == 0); 72*83d92559SIlya Leoshkevich } 73*83d92559SIlya Leoshkevich 74*83d92559SIlya Leoshkevich run = false; 75*83d92559SIlya Leoshkevich ret = pthread_join(mmap_munmap, NULL); 76*83d92559SIlya Leoshkevich assert(ret == 0); 77*83d92559SIlya Leoshkevich 78*83d92559SIlya Leoshkevich return EXIT_SUCCESS; 79*83d92559SIlya Leoshkevich } 80