aboutsummaryrefslogtreecommitdiff
path: root/tests/tcg/multiarch/munmap-pthread.c
blob: d7143b00d5fd2acdc79666da7ef667afec5231ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* Test that munmap() and thread creation do not race. */
#include <assert.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>

static const char nop_func[] = {
#if defined(__aarch64__)
    0xc0, 0x03, 0x5f, 0xd6,     /* ret */
#elif defined(__alpha__)
    0x01, 0x80, 0xFA, 0x6B,     /* ret */
#elif defined(__arm__)
    0x1e, 0xff, 0x2f, 0xe1,     /* bx lr */
#elif defined(__riscv)
    0x67, 0x80, 0x00, 0x00,     /* ret */
#elif defined(__s390__)
    0x07, 0xfe,                 /* br %r14 */
#elif defined(__i386__) || defined(__x86_64__)
    0xc3,                       /* ret */
#endif
};

static void *thread_mmap_munmap(void *arg)
{
    volatile bool *run = arg;
    char *p;
    int ret;

    while (*run) {
        p = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC,
                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
        assert(p != MAP_FAILED);

        /* Create a small translation block.  */
        memcpy(p, nop_func, sizeof(nop_func));
        ((void(*)(void))p)();

        ret = munmap(p, getpagesize());
        assert(ret == 0);
    }

    return NULL;
}

static void *thread_dummy(void *arg)
{
    return NULL;
}

int main(void)
{
    pthread_t mmap_munmap, dummy;
    volatile bool run = true;
    int i, ret;

    /* Without a template, nothing to test. */
    if (sizeof(nop_func) == 0) {
        return EXIT_SUCCESS;
    }

    ret = pthread_create(&mmap_munmap, NULL, thread_mmap_munmap, (void *)&run);
    assert(ret == 0);

    for (i = 0; i < 1000; i++) {
        ret = pthread_create(&dummy, NULL, thread_dummy, NULL);
        assert(ret == 0);
        ret = pthread_join(dummy, NULL);
        assert(ret == 0);
    }

    run = false;
    ret = pthread_join(mmap_munmap, NULL);
    assert(ret == 0);

    return EXIT_SUCCESS;
}