-
Notifications
You must be signed in to change notification settings - Fork 1
/
intraMode.cpp
265 lines (177 loc) · 6.15 KB
/
intraMode.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
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
#include "config.h"
#include "debug.h"
#include "ihpp.h"
#include "benchmark.h"
#include "output.h"
#include "tracingFuncs.h"
using namespace std;
#define INTRAMODE_LOAD_TOP_BOTTOM() { \
treeTop = intraCtx->shadowStack.top().treeTop; \
treeBottom = intraCtx->shadowStack.top().treeBottom; \
}
#if ENABLE_KEEP_STACK_PTR
#define INTRAMODE_STORE_TOP_BOTTOM() { \
intraCtx->shadowStack.push(ShadowStackItemType(treeTop,treeBottom,reg_sp)); \
}
#define INTRAMODE_REPLACE_TOP_BOTTOM() { \
intraCtx->shadowStack.pop(); \
intraCtx->shadowStack.push(ShadowStackItemType(treeTop,treeBottom,oldStackPtr)); \
}
#define TOP_STACKPTR() \
(intraCtx->shadowStack.size()?intraCtx->shadowStack.top().stackPtr:(ADDRINT)-1)
#else
#define INTRAMODE_STORE_TOP_BOTTOM() \
intraCtx->shadowStack.push(ShadowStackItemType(treeTop,treeBottom));
#define INTRAMODE_REPLACE_TOP_BOTTOM() { \
intraCtx->shadowStack.pop(); \
intraCtx->shadowStack.push(ShadowStackItemType(treeTop,treeBottom)); \
}
#endif
#define INTRAMODE_SET_TOP_BOTTOM_TO_ROOT() { \
intraCtx->counter=1; \
treeTop = intraCtx->kSlabForest.getTreeRef(intraCtx->rootKey); \
treeBottom = nullptr; \
}
#define INTRAMODE_TOP_BOTTOM_ARE_POINTING_TO_ROOT() \
(treeTop == intraCtx->kSlabForest.getTreeRef(intraCtx->rootKey) && !treeBottom)
void intraModeBlockTrace(BasicBlock *bb
#if ENABLE_KEEP_STACK_PTR
, ADDRINT reg_sp
#endif
) {
GlobalContext *globalCtx = globalSharedContext;
ThreadContext *ctx;
IntraModeContext *intraCtx;
ihppNode *treeTop;
ihppNode *treeBottom;
ctx = globalCtx->getThreadCtx(PIN_ThreadUid());
#if EMPTY_ANALYSIS
return;
#endif
bb->incSimpleCounter();
if (!ctx->haveToTrace)
return;
if ( FUNC_IS_TEXT(bb->functionAddr()) )
return;
#ifdef _WIN32
if (IS_WIN32_NLG_NOTIFY(bb->functionAddr())) {
dbg_intratr_nlog_skip();
return;
}
#endif
dbg_intratr_begin();
#if ENABLE_RELY_ON_SP_CHECK
#if ENABLE_INS_TRACING
if (!globalCtx->options.insTracing)
#endif
ctx->setCurrentFunction(bb->functionAddr());
#else
if ( ctx->getCurrentFunction() && ctx->getCurrentFunction() != bb->functionAddr() ) {
dbg_intratr_longjmp();
intraMode_ret();
ctx->setCurrentFunction(bb->functionAddr());
}
#endif
intraCtx = ctx->getCurrentFunctionCtx();
dbg_intratr_begin_sp();
if (!bb->isFirstBlock()) {
//If this function wasn't traced before (due to startFunc), don't it trace now..
if (!intraCtx->shadowStack.size())
return;
dbg_intratr_normal_trace();
INTRAMODE_LOAD_TOP_BOTTOM();
#if ENABLE_KEEP_STACK_PTR
ADDRINT oldStackPtr = TOP_STACKPTR();
#endif
if (!globalCtx->options.rollLoops) {
traceObject(bb, intraCtx, treeTop, treeBottom);
} else {
bool found=false;
ihppNode *parent = treeTop->getParentRef();
while (parent) {
if (parent->getKey() == bb->getKey()) {
treeTop = parent;
treeBottom = nullptr;
//in rollLoops mode, k=inf is automatic
//intraCtx->counter=1;
found=true;
treeTop->incCounter();
break;
}
parent = parent->getParentRef();
}
if (!found) {
traceObject(bb, intraCtx, treeTop, treeBottom);
}
}
INTRAMODE_REPLACE_TOP_BOTTOM();
dbg_intratr_end_sp();
return;
}
//The first basic block of a function is met:
//it has the same address of the function which belongs to
dbg_intratr_first_block();
if (!intraCtx->rootKey) {
//Rootkey is null, so this is the first time
//the function is called (in this thread): everything is very simple.
dbg_intratr_first_call();
treeTop = nullptr;
treeBottom = nullptr;
traceObject(bb, intraCtx, treeTop, treeBottom);
assert(intraCtx->rootKey);
assert(treeTop);
INTRAMODE_STORE_TOP_BOTTOM();
return;
}
//The BBL is a first block but this is NOT the first time
//this function is called (in this thread)
if (intraCtx->shadowStack.size())
INTRAMODE_LOAD_TOP_BOTTOM();
if (!INTRAMODE_TOP_BOTTOM_ARE_POINTING_TO_ROOT())
{
//Move top,bottom pointers to the root of the function's kSF
INTRAMODE_SET_TOP_BOTTOM_TO_ROOT();
//Push them on the shadow stack
INTRAMODE_STORE_TOP_BOTTOM();
}
treeTop->incCounter();
dbg_intratr_end_sp();
}
void intraMode_ret()
{
ThreadContext *ctx;
IntraModeContext *intraCtx;
GlobalContext *globalCtx = globalSharedContext;
ctx = globalCtx->getThreadCtx(PIN_ThreadUid());
#if EMPTY_ANALYSIS
return;
#endif
intraCtx = ctx->getCurrentFunctionCtx();
#ifdef _WIN32
if (IS_WIN32_NLG_NOTIFY(ctx->getCurrentFunction()))
return;
#endif
dbg_intraret_begin();
if (!intraCtx->shadowStack.size()) {
dbg_intraret_cantpop();
return;
}
dbg_intraret_stackpop();
intraCtx->shadowStack.pop();
if (!intraCtx->shadowStack.size()) {
ihppNode *treeTop;
ihppNode *treeBottom;
#if ENABLE_KEEP_STACK_PTR
ADDRINT reg_sp = (ADDRINT)-1;
#endif
dbg_intraret_lastret();
/*
Since now shadow stack is empty, on the next activation of the function
top and bottom pointers MUST point to the root of the k slab forest
*/
//Move top,bottom pointers to the root of the function's kSF
INTRAMODE_SET_TOP_BOTTOM_TO_ROOT();
//Push them on the shadow stack
INTRAMODE_STORE_TOP_BOTTOM();
}
}