-
Notifications
You must be signed in to change notification settings - Fork 1
/
ORAMTrace.cpp
236 lines (200 loc) · 6.78 KB
/
ORAMTrace.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
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
#include <stdio.h>
#include <string.h>
#include <unordered_map>
#include <iostream>
#include <fstream>
#include <list>
#include "pin.H"
static std::unordered_map<ADDRINT, std::string> str_of_ins_at;
FILE * trace;
// just want a hash table
static std::unordered_map<uint64_t, uint64_t> addrs_list;
static int8_t cache_state[64] = {0};
int timer = 0;
uint64_t func_start = 0x0;
uint64_t func_end = 0x0;
/* ===================================================================== */
// Command line switches
/* ===================================================================== */
KNOB<std::string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
"o", "", "specify file name for MyPinTool output");
KNOB<std::string> KnobAddrsFile(KNOB_MODE_WRITEONCE, "pintool",
"addrs_file", "0x422860", "file path");
KNOB<uint64_t> KnobFuncStart(KNOB_MODE_WRITEONCE, "pintool",
"func_start", "", "target func start addr");
KNOB<uint64_t> KnobFuncEnd(KNOB_MODE_WRITEONCE, "pintool",
"func_end", "", "target func end addr");
/* ===================================================================== */
// Utilities
/* ===================================================================== */
int debug_count = 0;
int stop_flag = 0;
VOID StopFlag(VOID * ip)
{
if ((uint64_t)ip == func_end){
stop_flag = 1;
fprintf(trace, "stop at %p\n", ip);
}
}
VOID RecordAddr(VOID * ip, VOID * mem_addr, USIZE mem_size)
{
if (stop_flag)
return;
// debug
// if (str_of_ins_at[(ADDRINT)ip].find("movaps") != std::string::npos && debug_count < 20){
// printf("%p: %s\n%p, %ld\n", ip, str_of_ins_at[(ADDRINT)ip].c_str(), mem_addr, mem_size);
// debug_count ++;
// }
uint32_t cache_idx = ((long)mem_addr & 0x0FC0ll) >> 6;
cache_state [cache_idx] = 1;
// TODO: recheck this for glow binary
// uint32_t cache_end_idx = ( ((long)mem_addr+mem_size-1) & 0x0FC0ll) >> 6;
// if (cache_idx != cache_end_idx){
// printf("%p, %ld\n", mem_addr, mem_size);
// }
timer ++;
if (timer == 100){ // used to be 260
timer = 0;
for (int i=0; i<64; i++){
fprintf(trace, "%d ", cache_state[i]);
}
fprintf(trace, "\n");
memset(cache_state, 0, 64);
}
// fprintf(trace,"%p\n", ip);
// fprintf(trace,"%llx\n", (uint64_t)mem_addr&0x0FC0ll);
//std::string ins_str = str_of_ins_at[(ADDRINT)ip];
//fprintf(trace,"%p:\t%s\n", ip, ins_str.c_str());
//fprintf(trace,"R:\t%p:\t%lu\n", mem_addr, mem_size);
//fprintf(trace,"%p: R %p\n", ip, addr);
}
VOID RecordReadAddr(VOID * ip, VOID * mem_addr, USIZE mem_size)
{
if (stop_flag)
return;
uint32_t cache_idx = ((long)mem_addr & 0x0FC0ll) >> 6;
if (timer >= 100000){
return;
}
timer ++;
fprintf(trace, "R %d\n", cache_idx);
}
VOID RecordWriteAddr(VOID * ip, VOID * mem_addr, USIZE mem_size)
{
if (stop_flag)
return;
uint32_t cache_idx = ((long)mem_addr & 0x0FC0ll) >> 6;
if (timer >= 100000){
return;
}
timer ++;
fprintf(trace, "W %d\n", cache_idx);
}
// Is called for every instruction and instruments reads and writes
VOID Instruction(INS ins, VOID *v)
{
ADDRINT ins_addr = INS_Address(ins);
if (func_start > ins_addr || ins_addr > func_end){
return;
}
str_of_ins_at[INS_Address(ins)] = INS_Disassemble(ins);
std::string ins_asm = INS_Disassemble(ins);
UINT32 memOperands = INS_MemoryOperandCount(ins);
// Iterate over each memory operand of the instruction.
UINT32 memOp = 0;
USIZE mem_size = 0;
for (; memOp < memOperands; memOp++)
{
if (INS_MemoryOperandIsRead(ins, memOp))
{
// USIZE mem_size = INS_MemoryReadSize(ins); // DEPRECATED
mem_size = INS_MemoryOperandSize(ins, memOp);
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)RecordReadAddr,
IARG_INST_PTR,
IARG_MEMORYOP_EA, memOp,
IARG_UINT64, mem_size,
IARG_END);
}
// Note that in some architectures a single memory operand can be
// both read and written (for instance incl (%eax) on IA-32)
// In that case we instrument it once for read and once for write.
if (INS_MemoryOperandIsWritten(ins, memOp))
{
// USIZE mem_size = INS_MemoryWriteSize(ins); // DEPRECATED
mem_size = INS_MemoryOperandSize(ins, memOp);
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)RecordWriteAddr,
IARG_INST_PTR,
IARG_MEMORYOP_EA, memOp,
IARG_UINT64, mem_size,
IARG_END);
}
}
if (ins_addr == func_end){
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)StopFlag,
IARG_INST_PTR,
IARG_END);
}
}
VOID Fini(INT32 code, VOID *v)
{
fprintf(trace, "#eof\n");
fclose(trace);
}
/* ===================================================================== */
/* Print Help Message */
/* ===================================================================== */
INT32 Usage()
{
PIN_ERROR( "This Pintool prints a trace of memory addresses\n"
+ KNOB_BASE::StringKnobSummary() + "\n");
return -1;
}
int ReadAddrList(){
std::string addrs_file = KnobAddrsFile.Value();
FILE *fp = fopen(addrs_file.c_str(),"r");
//int count = 0;
while(!feof(fp)){
uint64_t current_addr;
fscanf(fp, "%lx\n", ¤t_addr);
addrs_list[current_addr] = current_addr;
//printf("insert 0x%lx\n", current_addr); // debug
//count += 1;
//printf("%d\n", count);
}
return 0;
}
/* ===================================================================== */
/* Main */
/* ===================================================================== */
int main(int argc, char *argv[])
{
if (PIN_Init(argc, argv)) return Usage();
std::string fileName = KnobOutputFile.Value();
trace = fopen(fileName.c_str(), "w");
//trace = fopen("pinatrace.out", "w");
func_start = KnobFuncStart.Value();
func_end = KnobFuncEnd.Value();
// ReadAddrList();
// debug
//printf("output: %s, start: %p, end: %p\n", fileName.c_str(), (void *)start_addr, (void *)end_addr);
/*
std::unordered_map<uint64_t, uint64_t>::iterator iter;
iter = addrs_list.begin();
int count = 0;
while(iter != addrs_list.end()) {
printf("0x%lx\n", iter->second);
iter++;
count += 1;
printf("%d\n", count);
}
return 0;
*/
INS_AddInstrumentFunction(Instruction, 0);
PIN_AddFiniFunction(Fini, 0);
// Never returns
PIN_StartProgram();
return 0;
}