summaryrefslogtreecommitdiffstats
path: root/tests/tcg/aarch64/bti-2.c
blob: 65e8e857ddac0f03da2544751b532c3e348a71b4 (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Branch target identification, basic notskip cases.
 */

#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>

#ifndef PROT_BTI
#define PROT_BTI  0x10
#endif

static void skip2_sigill(int sig, siginfo_t *info, void *vuc)
{
    ucontext_t *uc = vuc;
    uc->uc_mcontext.pc += 8;
    uc->uc_mcontext.pstate = 1;
}

#define NOP       "nop"
#define BTI_N     "hint #32"
#define BTI_C     "hint #34"
#define BTI_J     "hint #36"
#define BTI_JC    "hint #38"

#define BTYPE_1(DEST)    \
    "mov x1, #1\n\t"     \
    "adr x16, 1f\n\t"    \
    "br x16\n"           \
"1: " DEST "\n\t"        \
    "mov x1, #0"

#define BTYPE_2(DEST)    \
    "mov x1, #1\n\t"     \
    "adr x16, 1f\n\t"    \
    "blr x16\n"          \
"1: " DEST "\n\t"        \
    "mov x1, #0"

#define BTYPE_3(DEST)    \
    "mov x1, #1\n\t"     \
    "adr x15, 1f\n\t"    \
    "br x15\n"           \
"1: " DEST "\n\t"        \
    "mov x1, #0"

#define TEST(WHICH, DEST, EXPECT) \
    WHICH(DEST) "\n"              \
    ".if " #EXPECT "\n\t"         \
    "eor x1, x1," #EXPECT "\n"    \
    ".endif\n\t"                  \
    "add x0, x0, x1\n\t"

asm("\n"
"test_begin:\n\t"
    BTI_C "\n\t"
    "mov x2, x30\n\t"
    "mov x0, #0\n\t"

    TEST(BTYPE_1, NOP, 1)
    TEST(BTYPE_1, BTI_N, 1)
    TEST(BTYPE_1, BTI_C, 0)
    TEST(BTYPE_1, BTI_J, 0)
    TEST(BTYPE_1, BTI_JC, 0)

    TEST(BTYPE_2, NOP, 1)
    TEST(BTYPE_2, BTI_N, 1)
    TEST(BTYPE_2, BTI_C, 0)
    TEST(BTYPE_2, BTI_J, 1)
    TEST(BTYPE_2, BTI_JC, 0)

    TEST(BTYPE_3, NOP, 1)
    TEST(BTYPE_3, BTI_N, 1)
    TEST(BTYPE_3, BTI_C, 1)
    TEST(BTYPE_3, BTI_J, 0)
    TEST(BTYPE_3, BTI_JC, 0)

    "ret x2\n"
"test_end:"
);

int main()
{
    struct sigaction sa;
    void *tb, *te;

    void *p = mmap(0, getpagesize(),
                   PROT_EXEC | PROT_READ | PROT_WRITE | PROT_BTI,
                   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (p == MAP_FAILED) {
        perror("mmap");
        return 1;
    }

    memset(&sa, 0, sizeof(sa));
    sa.sa_sigaction = skip2_sigill;
    sa.sa_flags = SA_SIGINFO;
    if (sigaction(SIGILL, &sa, NULL) < 0) {
        perror("sigaction");
        return 1;
    }

    /*
     * ??? With "extern char test_begin[]", some compiler versions
     * will use :got references, and some linker versions will
     * resolve this reference to a static symbol incorrectly.
     * Bypass this error by using a pc-relative reference directly.
     */
    asm("adr %0, test_begin; adr %1, test_end" : "=r"(tb), "=r"(te));

    memcpy(p, tb, te - tb);

    return ((int (*)(void))p)();
}