-
Notifications
You must be signed in to change notification settings - Fork 4
/
single-meh.c
319 lines (283 loc) · 8.23 KB
/
single-meh.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <signal.h>
#include <x86intrin.h>
#include <unistd.h>
//void funcfoo(void) __attribute__((section(".funcfoo")));
//void funcbar(void) __attribute__((section(".funcbar")));
unsigned int junk=0;
// Put this in its own (4KB) page
uint8_t probe_buf[4096*256];
uint8_t some_other_buf[4096*256];
// This one will actually be called (to train BTB)
void alias_target_fn(void) __attribute__((section(".aliastarget")));
void alias_target_fn(void)
{
asm volatile ( "movb (%%rbx), %%al\n" :: "b"((uint8_t*)&some_other_buf[1*4096]) : "rax");
asm volatile ("mfence\n");
}
// This will never be called directly (but will happen speculatively)
void target_fn(void) __attribute__((section(".targetfn")));
void target_fn(void) {
//asm volatile ( "movb (%%rbx), %%al\n" :: "b"((uint8_t*)&probe_buf[100*4096]) : "rax");
}
// Keep stats
uint64_t cache_hits = 0; // Basically number of times target_fn was speculatively executed
uint64_t tot_runs = 0;
void test() {
uint64_t t0, t1;
asm volatile("mfence\n" :::);
t0 = _rdtscp(&junk);
asm volatile( "movb (%%rbx), %%al\n"
"mfence\n"
:: "b"(&probe_buf[100*4096]) : "rax");
t1 = _rdtscp(&junk);
if (t1-t0 < 140) {
cache_hits++;
//printf("# %lu\n", t1-t0);
}
tot_runs++;
// Clear probe_buf from cache
int i;
for (i=0; i<256; i++) {
_mm_clflush(&probe_buf[i*4096]);
}
}
void __attribute__((section(".fnptr"))) (*fn_ptr)(void); // we'll set this = test, and cflush it
void __attribute__((section(".aliasfnptr"))) (*alias_fn_ptr)(void); // we'll set this = test, and cflush it
uint64_t jmp_offset;
void alias_indirect(void) __attribute__((section(".aliasindirect")));
void alias_indirect(void) {
asm volatile (
"jmp call_get_ripa\n"
"get_ripa:\n"
"pop %%rax\n" // rax = rip
"push %%rax\n"
"ret\n"
"call_get_ripa:\n"
"call get_ripa\n"
// At this point, rip == rax
"add (%%rbx), %%rax\n"
"add $9, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
:
: "b"(&jmp_offset)
: "rax");
//*/
(*alias_fn_ptr)();
}
// Place this at the address of the function that will be doing an indirect call
// (measure)
void indirect(void) __attribute__((section(".indirect")));
void indirect(void) {
// Do indirect jump
asm volatile (
"jmp call_get_rip\n"
"get_rip:\n"
"pop %%rax\n" // rax = rip
"push %%rax\n"
"ret\n"
"call_get_rip:\n"
"call get_rip\n"
// At this point, rip == rax
"add (%%rbx), %%rax\n"
"add $9, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
"add $6, %%rax\n"
"jmpq *%%rax\n"
:
: "b"(&jmp_offset)
: "rax");
(*fn_ptr)();
}
void measure() {
int i, j;
while (1) {
alias_fn_ptr = alias_target_fn;
fn_ptr = test;
for (i=0; i<10000; i++) {
//_mm_clflush(&probe_buf[100*4096]);
for (j=0; j<256; j++) {
_mm_clflush(&probe_buf[j*4096]);
}
/*
for (j=0; j<100; j++) {
_mm_clflush(&jmp_offset);
_mm_clflush(fn_ptr);
_mm_clflush(alias_fn_ptr);
if (j == 95) {
indirect();
}
alias_indirect();
}
*/
/*
alias_indirect();
alias_indirect();
alias_indirect();
alias_indirect();
alias_indirect();
alias_indirect();
alias_indirect();*/
alias_indirect();
alias_indirect();
alias_indirect();
alias_indirect();
alias_indirect();
indirect();
//test();
//usleep(1);
}
printf("%lu / %lu = %0.4f%% hits\n", cache_hits, tot_runs, 100*((float)cache_hits)/((float)tot_runs));
cache_hits = 0;
tot_runs = 0;
}
}
void funcbar() {
printf("hello from funcbar\n");
}
int main()
{
uint64_t x;
//printf("funcfoo = %p\n", funcfoo);
//printf("funcfoo = %p\n", funcbar);
jmp_offset = 0x00;
fn_ptr = test;
printf("indirect jump ptr = %p\n", fn_ptr);
printf("indirect fn = %p\n", indirect);
printf("alias indir = %p\n", alias_indirect);
printf("target_fn = %p\n", target_fn);
printf("alias tar = %p\n", alias_target_fn);
printf("measuring...\n");
measure();
printf("[+] Success!\n");
//funcfoo();
//funcbar();
}