-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreceiver.cpp
More file actions
115 lines (88 loc) · 2.74 KB
/
receiver.cpp
File metadata and controls
115 lines (88 loc) · 2.74 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
#include "util.hpp"
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
// helper function that prepares the channel
// for communication with the sender.
// To do this we fill the whole L3 cache with our dataset.
void prepare_channel(char *channel, uint32_t *sets);
// array shuflle function from:https://stackoverflow.com/questions/6127503/shuffle-array-in-c
void shuffle (uint64_t *array, uint64_t n);
int main(int argc, char **argv) {
// Put your covert channel setup code here
char *channel;
uint32_t sets[L3_SETS];
// Allocate enough memory to cover the whole L3
channel = (char *)malloc(L3_SIZE);
if (channel == NULL) {
cerr << "Failed to allocate memory of size " << L3_SIZE << " bytes\n";
return -1;
}
prepare_channel(channel, sets);
printf("Please press enter.\n");
char text_buf[2];
// fgets(text_buf, sizeof(text_buf), stdin);
printf("Receiver now listening.\n");
uint64_t int_array[10];
uint64_t addr;
bool listening = true;
// while (listening) {
// for (int i = 0; i < 10; i++) {
// addr = (uint64_t)&int_array[i];
// uint32_t time_passed = measure_one_block_access_time(addr);
// cout << "time passed = " << time_passed << endl;
// }
// Put your covert channel code here
//}
printf("Receiver finished.\n");
return 0;
}
void prepare_channel(char *channel, uint32_t *sets) {
uint32_t time_passed=0, addrs=0;
uint64_t addr=(uint64_t)&channel[0];
uint64_t access[L3_SETS];
// initiliaze the sets miss/hit history
memset(sets, 0, L3_SETS * sizeof(uint32_t));
srand(time(NULL));
// generate all the address that will be probed
for (uint64_t i=0; i < L3_SETS; i++){
access[i]=addr;
addr += CACHE_LINE_SIZE;
}
// shuffle the array to avoid prefetching effects
shuffle(access, L3_SETS);
for (uint64_t reps = 0; reps < REPETITION_NUM; reps++) {
// initialize the channel
memset(channel, 1, L3_SIZE);
// grab the beggining of the array
addr = (uint64_t)&channel[0];
// sleep to give time to the sender to write
sleep(SLEEP_TIME);
for (uint64_t i = 0; i < L3_SETS; i++) {
// measure the access time for each line
// indirection based access to avoid prefetching effects
time_passed = measure_one_block_access_time(access[i]);
// and mark the misses
sets[i] += (time_passed > HIT_TIME ? 1 : 0);
}
}
for (uint64_t i = 0; i < L3_SETS; i++) {
cout << sets[i] << "\t";
}
// cout << "\n";
}
void shuffle(uint64_t *array, uint64_t n)
{
if (n > 1)
{
uint64_t i;
for (i = 0; i < n - 1; i++)
{
uint64_t j = i + rand() / (RAND_MAX / (n - i) + 1);
uint64_t t = array[j];
array[j] = array[i];
array[i] = t;
}
}
}