-
Notifications
You must be signed in to change notification settings - Fork 1
/
funcMode.cpp
224 lines (150 loc) · 4.72 KB
/
funcMode.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
#include "config.h"
#include "debug.h"
#include "ihpp.h"
using namespace std;
#include "benchmark.h"
#include "output.h"
#include "tracingFuncs.h"
#include "win32_hacks.h"
#define FUNCMODE_TOP_STACKPTR() \
(ctx->shadowStack.size() ?ctx->shadowStack.top().stackPtr : (ADDRINT)-1)
#if ENABLE_RELY_ON_SP_CHECK
static void funcMode_sp_check(ThreadContext *ctx, ADDRINT reg_sp)
{
#if ENABLE_INS_TRACING
if (globalSharedContext->options.insTracing)
return;
#endif
if (reg_sp >= FUNCMODE_TOP_STACKPTR()) {
dbg_functr_regsp_gt();
while (ctx->shadowStack.size() > 1 && reg_sp >= FUNCMODE_TOP_STACKPTR()) {
dbg_functr_pop();
if (!ctx->popShadowStack())
break;
}
}
}
#endif
void FunctionObjTrace(FunctionObj *fc
#if ENABLE_KEEP_STACK_PTR
, ADDRINT reg_sp
#endif
) {
ihppNode *treeTop = nullptr;
ihppNode *treeBottom = nullptr;
ThreadContext *ctx;
GlobalContext *globalCtx = globalSharedContext;
#if !ENABLE_KEEP_STACK_PTR
const ADDRINT reg_sp = (ADDRINT)-1;
#endif
ctx = globalCtx->getThreadCtx(PIN_ThreadUid());
#if EMPTY_ANALYSIS
return;
#endif
if (fc->functionAddress() == ctx->startFuncAddr)
ctx->haveToTrace=true;
if (!ctx->haveToTrace)
return;
if (fc->functionAddress() == ctx->stopFuncAddr)
ctx->haveToTrace=false;
ctx->setCurrentFunction(fc->functionAddress());
fc->incSimpleCounter();
if (!globalCtx->exitPassed && fc->functionAddress() == globalSharedContext->spAttrs.exit_addr)
globalCtx->exitPassed=true;
#if defined(_WIN32) && ENABLE_WIN32_MAIN_ALIGNMENT
win32_main_alignment(ctx, fc);
#endif
dbg_functr_funcname();
dbg_functr_stackptr();
#if ENABLE_RELY_ON_SP_CHECK
funcMode_sp_check(ctx, reg_sp);
#endif
if (ctx->shadowStack.size()) {
treeTop = ctx->shadowStack.top().treeTop;
treeBottom = ctx->shadowStack.top().treeBottom;
}
assert(ctx->shadowStack.size() || !ctx->rootKey);
if (globalCtx->options.showCalls)
funcTraceDebugDump(globalCtx, fc, ctx, reg_sp, treeTop, treeBottom);
traceObject(fc, ctx, treeTop, treeBottom);
#if ENABLE_KEEP_STACK_PTR
ctx->shadowStack.push(ShadowStackItemType(treeTop,treeBottom,reg_sp));
#else
ctx->shadowStack.push(ShadowStackItemType(treeTop,treeBottom));
#endif
#if ENABLE_INS_FORWARD_JMP_RECOGNITION
if (ctx->forwardJmpHappened && ctx->fjmpsFuncAddr == fc->functionAddress()) {
ctx->forwardJmpHappened=false;
ctx->shadowStack.top().fjmps = ctx->lastfjmps+1;
dbg_functr_fjmps_set();
}
#endif
dbg_functr_ssize_after();
}
void funcMode_ret()
{
GlobalContext *globalCtx = globalSharedContext;
ThreadContext *ctx;
bool fjmps_happened=false;
#if EMPTY_ANALYSIS
return;
#endif
ctx = globalCtx->getThreadCtx(PIN_ThreadUid());
if (!ctx->haveToTrace)
return;
dbg_funcret_name();
assert( globalCtx->funcsToTrace.size() || (ctx->shadowStack.size() || globalCtx->exitPassed) );
#if ENABLE_INS_FORWARD_JMP_RECOGNITION
dbg_funcret_fjmps();
if (ctx->shadowStack.top().fjmps)
fjmps_happened=true;
while (ctx->shadowStack.top().fjmps--)
{
if (ctx->shadowStack.size() <= 1)
break;
if (!ctx->canPopStack())
break;
dbg_functr_pop();
ctx->shadowStack.pop();
//if (globalCtx->options.unrollRec) {
//
// ctx->counter = ctx->counter ? ctx->counter-1 : 0;
//
//} else {
//
// if (ctx->shadowStack.size() > 1)
// if (ctx->shadowStack.top().treeTop->getKey() !=
// ctx->shadowStack.top(1).treeTop->getKey())
// ctx->counter--;
//}
if (globalCtx->WorkingMode() == WM_IntraMode)
intraMode_ret();
}
#endif
if (ctx->shadowStack.size() == 1) {
dbg_funcret_pop_err();
goto function_end;
}
dbg_funcret_pop();
if (ctx->canPopStack() && !fjmps_happened) {
dbg_functr_pop();
ctx->shadowStack.pop();
//if (globalCtx->options.unrollRec) {
//
// ctx->counter = ctx->counter ? ctx->counter-1 : 0;
//
//} else {
//
// if (ctx->shadowStack.size() > 1)
// if (ctx->shadowStack.top().treeTop->getKey() !=
// ctx->shadowStack.top(1).treeTop->getKey())
// ctx->counter--;
//}
}
dbg_funcret_stack_after_pop();
function_end:
if (globalCtx->WorkingMode() == WM_IntraMode)
intraMode_ret();
if (ctx->shadowStack.size())
ctx->setCurrentFunction(ctx->shadowStack.top().treeTop->getValue()->getKey());
}