-
Notifications
You must be signed in to change notification settings - Fork 3
/
TGDispatcherBuilder.h
208 lines (160 loc) · 5.88 KB
/
TGDispatcherBuilder.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
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
#ifndef LLVM_TYPEGRAPHS_TGDISPATCHERBUILDER_H
#define LLVM_TYPEGRAPHS_TGDISPATCHERBUILDER_H
#ifndef WITHOUT_LLVM
#include <llvm/IR/Function.h>
#include <llvm/IR/InstrTypes.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/Typegraph/TypegraphSettings.h>
#endif
#include <llvm/Typegraph/StringContainer.h>
#include <set>
#include <string>
#include <vector>
namespace typegraph {
constexpr long NOID = -1;
template <class FunctionRef, class UseRef> struct TGDFunction {
long ID;
bool Leaking;
FunctionRef Ref;
// std::vector<UseRef> Uses;
bool Removed = false;
bool InCluster = false;
TGDFunction(long Id, FunctionRef Ref) : ID(Id), Ref(Ref) {}
};
enum TGDResolvePointType { CALL, EXTERNAL_FUNCTION };
/**
* A resolve point is one of:
* - an indirect call (converted to switch case)
* - a call to stdlib with function pointer argument (needs raw pointer)
* @tparam CallRef
*/
template <class FunctionRef, class UseRef, class CallRef> struct TGDResolvePoint {
TGDResolvePointType Type;
const std::string *CallName;
bool IsExternal = false;
std::vector<TGDFunction<FunctionRef, UseRef> *> Targets;
CallRef CallInst;
int ResolveArgnum;
long VertexID = -1;
const std::string *Hash = nullptr;
TGDResolvePoint(TGDResolvePointType Type, const std::string *CallName, CallRef CallInst)
: Type(Type), CallName(CallName), CallInst(CallInst) {}
};
template <class FunctionRef, class UseRef, class CallRef> class TGDispatcherBuilder {
public:
typedef TGDFunction<FunctionRef, UseRef> DFunction;
typedef TGDResolvePoint<FunctionRef, UseRef, CallRef> DResolvePoint;
protected:
std::map<FunctionRef, std::unique_ptr<DFunction>> Functions;
std::vector<DResolvePoint> ResolvePoints;
long NextId = 1;
public:
/**
* Add a call-typed resolve point.
* @return A reference to the new resolvepoint. Rendered invalid when the next add occurs.
*/
DResolvePoint &addCall(const std::string *CallName, CallRef Ref) {
ResolvePoints.emplace_back(CALL, CallName, Ref);
return ResolvePoints.back();
}
DResolvePoint &addResolvePoint(const std::string *RPName, CallRef Ref, int ArgIndex) {
ResolvePoints.emplace_back(EXTERNAL_FUNCTION, RPName, Ref);
ResolvePoints.back().ResolveArgnum = ArgIndex;
return ResolvePoints.back();
}
DFunction *getFunction(FunctionRef Ref, long ID = NOID) {
auto It = Functions.find(Ref);
if (It == Functions.end()) {
DFunction *F = (Functions[Ref] = std::make_unique<DFunction>(ID, Ref)).get();
F->Leaking = false;
return F;
}
return It->second.get();
}
DFunction *getFunctionOpt(FunctionRef Ref) {
auto It = Functions.find(Ref);
if (It == Functions.end()) {
return nullptr;
}
return It->second.get();
}
/*
void purgeUnusedFunctions() {
for (auto &F : Functions) {
if (F.Uses.empty())
F.Removed = true;
}
for (auto &RP : ResolvePoints) {
// TODO remove F from RP.Targets if F.Removed
}
}*/
};
#ifndef WITHOUT_LLVM
class LLVMDispatcherBuilder : public TGDispatcherBuilder<llvm::Function *, llvm::User *, llvm::CallBase *> {
std::shared_ptr<StringContainer> Symbols;
llvm::LLVMContext &Context;
llvm::Module &M;
llvm::IntegerType *IntPtr;
std::map<llvm::CallBase *, const std::string *> DynamicSymbolCalls;
public:
size_t ModuleID = 1;
llvm::DenseMap<const std::string *, llvm::GlobalVariable *> ExternalCallDispatchers;
llvm::DenseMap<const std::string *, llvm::GlobalVariable *> ExternalDynamicSymbolInfos;
llvm::DenseMap<const std::string *, std::set<std::string>> DynamicSymbolTargets;
std::map<const std::string *, long> CountPerTargetHash;
inline LLVMDispatcherBuilder(llvm::LLVMContext &Context, llvm::Module &M, std::shared_ptr<StringContainer> Symbols)
: Symbols(Symbols), Context(Context), M(M) {
NextId = Settings.enforce_min_id;
IntPtr = M.getDataLayout().getIntPtrType(M.getContext());
}
inline void addDynamicSymbolCall(llvm::CallBase *DynSymbolCall, const std::string *DlSymName) {
DynamicSymbolCalls[DynSymbolCall] = DlSymName;
}
void initialize();
void assignOptimalIDs();
void assignMissingIDs();
void debugIDAssignment();
void replaceFunctionsWithIDs();
void generateCodeForResolvePoint(DResolvePoint &RP);
inline void generateCodeForAll() {
for (auto &RP: ResolvePoints) {
generateCodeForResolvePoint(RP);
}
}
void generateCodeForDynamicSymbols();
void printFunctionIDs();
void writeFunctionIDsToFile();
inline std::map<llvm::Function *, std::unique_ptr<DFunction>> &getFunctions() {
return Functions;
}
inline void setModuleID(size_t NewModuleID) {
this->ModuleID = NewModuleID;
if (ModuleID > typegraph::Settings.enforce_min_id) {
NextId = ModuleID;
} else {
NextId = typegraph::Settings.enforce_min_id;
}
}
void allTrampolinesGenerated();
private:
bool typesafeReplaceAllUsesWith(llvm::User *OldValue, llvm::Constant *NewValue);
bool typesafeReplaceUseWith(llvm::User *User, llvm::Value *Old, llvm::Constant *New);
/**
* @param Result pairs (User, OldValue)
* @param F
*/
void collectFunctionUsages(std::vector<std::pair<llvm::User *, llvm::Value *>> &Result, llvm::Value *F);
bool canReplaceAllFunctionUsages(llvm::Value *V);
void collectCluster(std::set<DFunction *> &Cluster, std::set<DResolvePoint *> &RPs);
long assignClusterIDs(std::set<DFunction *> &Cluster, std::set<DResolvePoint *> &RPs, long MinNumber);
// generate code that translates a function ID back to a pointer
llvm::Value *generateBackTranslation(DResolvePoint &RP, llvm::Value *OldValue, llvm::Instruction *SplitBefore);
// generate code that errors
void generateErrorCase(llvm::IRBuilder<> &Builder, DResolvePoint &RP, llvm::Value *FunctionID);
void newTrampoline(llvm::GlobalVariable *GV);
};
#endif
} // namespace typegraph
#endif // LLVM_TYPEGRAPHS_TGDISPATCHERBUILDER_H