-
Notifications
You must be signed in to change notification settings - Fork 1
/
globalContext.cpp
130 lines (98 loc) · 3.1 KB
/
globalContext.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
#include "config.h"
#include "debug.h"
#include "ihpp.h"
using namespace std;
#include "benchmark.h"
#include "output.h"
#include "tracingFuncs.h"
const string& ThreadContext::getCurrentFunctionName() const {
return globalSharedContext->allFuncs[currentFunction]->functionName();
}
void GlobalContext::destroyThreadContext(PIN_THREAD_UID tid)
{
// We have just reset the TLS for this thread ID and in *no case*
// delete the threadContext object, as we'll need it later in output.cpp.
PIN_SetThreadData(_tls_key, nullptr, tid);
}
void GlobalContext::createThreadContext(PIN_THREAD_UID tid)
{
if (EMPTY_ANALYSIS) {
if (threadContexts.size() > 0)
return;
}
ThreadContext *threadCtx = new ThreadContext(tid, startFuncAddr, stopFuncAddr);
PIN_SetThreadData(_tls_key, threadCtx, tid);
threadContexts.push_back(threadCtx);
WorkingModeType wMode = globalSharedContext->WorkingMode();
if (globalSharedContext->funcsToTrace.empty())
return;
if (wMode == WM_InterProcMode) {
traceObject(
globalSharedContext->allBlocks[(ADDRINT)-1],
threadCtx,
threadCtx->treeTop,
threadCtx->treeBottom
);
return;
}
// wMode is WM_FuncMode or WM_IntraMode.
ihppNode *t = nullptr;
ihppNode *b = nullptr;
traceObject(globalSharedContext->allFuncs[(ADDRINT)-1], threadCtx, t, b);
#if ENABLE_KEEP_STACK_PTR
threadCtx->shadowStack.push(ShadowStackItemType(t,b,(ADDRINT)-1));
#else
threadCtx->shadowStack.push(ShadowStackItemType(t,b));
#endif
if (wMode == WM_IntraMode) {
IntraModeContext *intraCtx;
threadCtx->setCurrentFunction((ADDRINT)-1);
intraCtx = threadCtx->getCurrentFunctionCtx();
t = nullptr;
b = nullptr;
traceObject(globalSharedContext->allBlocks[(ADDRINT)-1], intraCtx, t, b);
#if ENABLE_KEEP_STACK_PTR
intraCtx->shadowStack.push(ShadowStackItemType(t,b,(ADDRINT)-1));
#else
intraCtx->shadowStack.push(ShadowStackItemType(t,b));
#endif
}
}
ThreadContext *GlobalContext::getThreadCtx(PIN_THREAD_UID tid)
{
static bool already_called;
if (!already_called) {
double tmp = getMilliseconds();
double diff = tmp - timer;
fprintf(stderr, "[ IHPP ] Instrumentation time: %.3f sec\n", diff/1000.0);
timer = tmp;
already_called = true;
}
if (EMPTY_ANALYSIS)
return nullptr;
return (ThreadContext *)PIN_GetThreadData(_tls_key, tid);
}
GlobalContext::GlobalContext(WorkingModeType wm, unsigned kval, optionsClass options)
: _K_CCF_VAL(kval)
, _WorkingMode(wm)
, exitPassed(false)
#if DEBUG
, showDebug(true)
#endif
, options(options)
, startFuncAddr(0)
, stopFuncAddr(0)
{
_tls_key = PIN_CreateThreadDataKey(nullptr);
if (_tls_key == INVALID_TLS_KEY) {
cerr << "Unable to create TLS KEY" << endl;
PIN_ExitProcess(1);
}
}
GlobalContext::~GlobalContext() {
PIN_DeleteThreadDataKey(_tls_key);
for (auto& p : allBlocks)
delete p.second;
for (auto& p : allFuncs)
delete p.second;
}