-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtracingObjects.h
176 lines (130 loc) · 4.27 KB
/
tracingObjects.h
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
#pragma once
#include <sstream>
#include <cstring>
#include "dataStructures.h"
#include "util.h"
enum class TracingObjType {
Other,
Func,
BasicBlock,
};
template <typename keyT>
class TracingObject : public ObjectWithKey<keyT> {
protected:
TracingObjType type;
obj_counter_t simpleCounter;
keyT key;
public:
TracingObject(TracingObjType t, keyT k)
: type(t)
, simpleCounter(0)
, key(k)
{ }
virtual ~TracingObject() = default;
TracingObjType getType() const { return type; }
virtual keyT getKey() const { return key; }
virtual obj_counter_t getSimpleCounter() { return simpleCounter; }
virtual void incSimpleCounter() { simpleCounter++; }
virtual operator std::string() const = 0;
};
class insInfo {
public:
std::string ins_text;
ADDRINT targetAddr;
ADDRINT targetFuncAddr;
char *externFuncName;
bool isCall;
bool isDirectBranchOrCall() const { return targetAddr != 0; }
insInfo()
: targetAddr(0)
, targetFuncAddr(0)
, externFuncName(0)
, isCall(false)
{ }
insInfo(std::string ins, ADDRINT tAddr, ADDRINT tfuncAddr, char *extFuncName = nullptr)
: ins_text(ins)
, targetAddr(tAddr)
, targetFuncAddr(tfuncAddr)
, externFuncName(extFuncName)
, isCall(ins_text.compare(0, 4, "call") == 0)
{ }
};
class FunctionObj : public TracingObject<ADDRINT> {
std::string _functionName;
std::string _fileName;
public:
std::map<ADDRINT, insInfo> instructions;
FunctionObj(ADDRINT ptr, std::string funcName, std::string fileName)
: TracingObject<ADDRINT>(TracingObjType::Func, ptr)
, _functionName(funcName)
, _fileName(fileName)
{ }
ADDRINT functionAddress() const { return key; }
const std::string& functionName() const { return _functionName; }
const std::string& fileName() const { return _fileName; }
operator std::string() const { return functionName()+"()"; }
};
class BasicBlock : public TracingObject<ADDRINT> {
ADDRINT lastInsAddr;
INT32 _firstLine;
INT32 _firstCh;
public:
FunctionObj *functionPtr;
BasicBlock(ADDRINT ptr, FunctionObj *funcPtr, ADDRINT lastInsAddress, INT32 line, INT32 col)
: TracingObject<ADDRINT>(TracingObjType::BasicBlock, ptr)
, lastInsAddr(lastInsAddress)
, _firstLine(line)
, _firstCh(col)
, functionPtr(funcPtr)
{
assert(functionPtr);
}
ADDRINT blockAddress() const { return key; }
ADDRINT blockEndAddress() const { return lastInsAddr; }
ADDRINT functionAddr() const { return functionPtr->functionAddress(); }
std::string functionName() const { return functionPtr->functionName(); }
INT32 firstLine() const { return _firstLine; }
INT32 firstCh() const { return _firstCh; }
bool isFirstBlock() const { return blockAddress() == functionPtr->functionAddress(); }
operator std::string() const {
std::stringstream ss;
ss << "{";
ss << functionName();
ss << "+";
ss << (blockAddress()-functionAddr());
ss << " at ";
ss << _firstLine;
ss << ",";
ss << _firstCh;
ss << "}";
return ss.str();
}
};
inline std::ostream& operator << (std::ostream& s, BasicBlock& bb) {
return s << static_cast<std::string>(bb);
}
inline std::ostream& operator << (std::ostream& s, FunctionObj& fc) {
return s << static_cast<std::string>(fc);
}
typedef node<ADDRINT> ihppNode;
typedef forest<ADDRINT> ihppForest;
typedef std::map<ADDRINT, ihppNode*> ihppNodeMap;
typedef std::map<ADDRINT, FunctionObj*> FuncsMap;
typedef std::map<ADDRINT, BasicBlock*> BlocksMap;
typedef FuncsMap::iterator FuncsMapIt;
typedef BlocksMap::iterator BlocksMapIt;
inline std::ostream& operator<<(std::ostream& s, ihppNode &n) {
TracingObject<ADDRINT> *obj =
static_cast<TracingObject<ADDRINT> *>(n.getValue());
assert(obj != nullptr);
return s << static_cast<std::string>(*obj);
}
inline std::ostream& operator << (std::ostream& s, ihppNode *n) {
if (n) {
TracingObject<ADDRINT> *obj =
static_cast<TracingObject<ADDRINT> *>(n->getValue());
assert(obj != nullptr);
return s << static_cast<std::string>(*obj);
}
return s << "(null)";
}