-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
test-jit.c
205 lines (164 loc) · 4.63 KB
/
test-jit.c
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/user.h>
#include <unistd.h>
int fib(int n)
{
int first = 0, second = 1, next = 0, i = 0;
while(i <= n)
{
if(i < 2)
{
next = i;
}
else
{
next = first + second;
first = second;
second = next;
}
i++;
}
return next;
}
int pass_test()
{
return 0x42;
}
void fatal(char *msg)
{
fprintf(stderr, "*** FATAL ERROR: %s\n", (msg ? msg : "no message"));
fflush(stderr);
abort();
}
void test_shared()
{
static char filename[] = "/tmp/DoubleMapXXXXXX";
int fd = mkstemp(filename);
if(fd == -1)
{
fatal("mkstemp");
}
if(ftruncate(fd, PAGE_SIZE) == -1)
{
fatal("ftruncate");
}
uint8_t *const write_addr = mmap(0, 2 * PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
uint8_t *const exec_addr = mmap(write_addr+PAGE_SIZE, PAGE_SIZE,
PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED | MAP_FIXED, fd, 0);
if(write_addr == MAP_FAILED || exec_addr == MAP_FAILED)
{
fatal("mmap");
}
size_t size = PAGE_SIZE;
memcpy(write_addr, fib, size);
int (*fun_pointer)() = (void*)exec_addr;
// Give the JIT something to potentially cache
for(int i = 0; i < 15000; i++)
{
if(fun_pointer(20) != 6765)
{
fatal("fibonacci");
}
}
memcpy(write_addr, pass_test, size);
if(fun_pointer() == 0x42)
{
printf("test_shared passed\n");
}
munmap(write_addr, 2 * size);
munmap(exec_addr, size);
}
void test_consecutive()
{
uint8_t *const page0 = mmap(NULL,
2 * PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
// throwaway mmap to reduce likelhood of page0 and page1 mapping to consecutive physical frames
uint8_t *const throwaway = mmap(NULL,
PAGE_SIZE, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
uint8_t *const page1 = mmap(page0 + PAGE_SIZE, PAGE_SIZE,
PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
if(page0 == MAP_FAILED || throwaway == MAP_FAILED || page1 == MAP_FAILED)
{
fatal("mmap");
}
// Attempt to influence virtual to physical mapping - we want page0->page1 to not be contiguous
// physically
page0[0] = 0;
throwaway[0] = 0;
page1[0] = 0;
for(int32_t i = 0; i < 100; i++)
{
uint8_t* start = (uint8_t*)(page1 - i - 1);
memcpy(start, fib, PAGE_SIZE);
int (*fun_pointer)() = (void*)start;
for(int j = 0; j < 15000; j++)
{
if(fun_pointer(20) != 6765)
{
fatal("fibonacci");
}
}
}
printf("test_consecutive passed\n");
}
void test_consecutive_written()
{
uint8_t *const page0 = mmap(NULL,
2 * PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
// throwaway mmap to reduce likelhood of page0 and page1 mapping to consecutive physical frames
uint8_t *const throwaway = mmap(NULL,
PAGE_SIZE, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
uint8_t *const page1 = mmap(page0 + PAGE_SIZE, PAGE_SIZE,
PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
if(page0 == MAP_FAILED || throwaway == MAP_FAILED || page1 == MAP_FAILED)
{
fatal("mmap");
}
// Attempt to influence virtual to physical mapping - we want page0->page1 to not be contiguous
// physically
page0[0] = 0;
throwaway[0] = 0;
page1[0] = 0;
uint8_t* start = page1 - 8;
uint8_t* ptr = start;
const int32_t INC_COUNT = 16;
// xor eax, eax
*ptr++ = 0x31;
*ptr++ = 0xc0;
for(int i = 0; i < INC_COUNT; i++)
{
// inc eax
*ptr++ = 0x40;
}
// ret
*ptr++ = 0xC3;
int (*fun_pointer)() = (void*)start;
for(int i = 0; i < 15000; i++)
{
int32_t result = fun_pointer();
if(result != INC_COUNT)
{
fatal("test_consecutive_written");
}
}
// overwrite one INC at the start of the second page with a NOP
*page1 = 0x90;
int32_t result = fun_pointer();
if(result != INC_COUNT - 1)
{
fatal("test_consecutive_written after overwrite");
}
printf("test_consecutive_written passed\n");
}
int main()
{
test_shared();
// disabled for now, takes long and not sure if it actually catches bugs
//test_consecutive();
test_consecutive_written();
return 0;
}