-
Notifications
You must be signed in to change notification settings - Fork 2
/
DSAGenerator.cpp
415 lines (387 loc) · 15.5 KB
/
DSAGenerator.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//
// This file is distributed under the MIT License. See LICENSE for details.
//
#include "dsaGenerator/DSAGenerator.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ValueSymbolTable.h"
#include "llvm/Support/Debug.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/Support/FileSystem.h"
#include <fstream>
#include <iterator>
#include <algorithm>
#include <unordered_set>
namespace dsa {
using namespace llvm;
typedef std::map<unsigned, std::pair<std::string, DIType *>> offsetNames;
DITypeIdentifierMap TypeIdentifierMap;
void offsetPrinter(const DSNode &node, std::ofstream &file, StringRef op,
offsetNames &of, std::string, std::string);
void getAllNames(DIType *Ty, offsetNames &of, unsigned prev_off,
std::string baseName, std::string indent, StringRef argName, std::string&);
/// Prints it in console. Solely for debugging purposes
void dumpOffsetNames(offsetNames &of) {
errs() << "-------------------------------------------\n";
for (auto off : of) {
errs() << "\toffset : " << off.first << "\t";
errs() << " | name : " << std::get<0>(off.second) << "\n";
}
errs() << "-------------------------------------------\n";
}
void printOffsets(DSNode *node, std::string indentation, std::ofstream *file,
std::vector<DSNode *> *visitedNodes, offsetNames &of2,
Type* elementType, StringRef elementName, std::string& structName, std::string functionName) {
if(structName != "") {
//arg.dump();
//errs() << "\nis pointer: " << isa<PointerType>(arg.getType()) << "\n";
//errs() << "\nis struct: " << dyn_cast<StructType>(dyn_cast<PointerType>(arg.getType())->getElementType())->getName().str() << "\n";
//if(dyn_cast<PointerType>(arg.getType())->getElementType()->getStructName() != NULL)
//*file << indentation << "\nprojection <struct " << arg
//.getType()
//->getStructName()
// .str() << "> " << structName << " {";
//else
Type* argType = elementType;
while(!isa<StructType>(argType)) {
argType = dyn_cast<PointerType>(argType)->getElementType();
}
*file << indentation << "\nprojection <struct " << dyn_cast<StructType>(argType)->getName().substr(7).str() << "> " << functionName << "." << elementName.str() << " {\n";
//*file << (node->isCollapsedNode() ? "collapsed\n" : "not collapsed\n");
offsetPrinter(*node, *file, "read", of2, functionName, indentation);
offsetPrinter(*node, *file, "write", of2, functionName, indentation);
*file << indentation << "}\n\n";
}
//else
//*file << "\n" << indentation << "{\n";
visitedNodes->push_back(node);
for (DSNode::const_edge_iterator ei = node->edge_begin();
ei != node->edge_end(); ei++) {
if (std::find(visitedNodes->begin(), visitedNodes->end(),
(*ei).second.getNode()) != visitedNodes->end())
continue;
offsetNames of3;
std::string ind2 = indentation;
ind2.append("\t");
unsigned offset = (*ei).first;
std::string offset_name;
offsetNames ofInner;
// FIXME: exception handling may be removed once dsa gives back the correct
// offsets.
// Have this now to avoid segfault
try {
offset_name = of2.at(offset).first;
//*file << "\n" << ind2 << "{\n";
//*file << ind2 << "offset: " << offset << " | name: " << offset_name
//<< "\n";
//*file << "collapsed: " << (*ei).second.getNode()->isCollapsedNode() << "\n";
//*file << "forward: " << (*ei).second.getNode()->isForwarding() << "\n";
} catch (std::out_of_range &e) {
errs() << "OUT of range exception " << e.what() << "\n";
errs() << "offset: " << offset << "\n";
continue;
}
DIType *Ty = of2.at(offset).second;
//errs() << Ty->getTag() << "\n";
structName = "";
// Previous offset is zero. No continuation
getAllNames(Ty, ofInner, 0, offset_name, " ", elementName, structName);
dumpOffsetNames(ofInner);
//assuming that we will have to print this projection only once
std::string emptyString("");
printOffsets((*ei).second.getNode(), ind2, file, visitedNodes, ofInner, elementType, elementName, emptyString, emptyString);
}
}
DIType* getLowestDINode(DIType* Ty) {
if (Ty->getTag() == dwarf::DW_TAG_pointer_type ||
Ty->getTag() == dwarf::DW_TAG_member || Ty->getTag() == dwarf::DW_TAG_typedef) {
DIType *baseTy =
dyn_cast<DIDerivedType>(Ty)->getBaseType().resolve(TypeIdentifierMap);
if (!baseTy) {
errs() << "Type : NULL - Nothing more to do\n";
return NULL;
}
//Skip all the DINodes with DW_TAG_typedef tag
while ((baseTy->getTag() == dwarf::DW_TAG_typedef || baseTy->getTag() == dwarf::DW_TAG_const_type
|| baseTy->getTag() == dwarf::DW_TAG_pointer_type)) {
if (DITypeRef temp = dyn_cast<DIDerivedType>(baseTy)->getBaseType())
baseTy = temp.resolve(TypeIdentifierMap);
else
break;
}
return baseTy;
}
return Ty;
}
void getAllNames(DIType *Ty, offsetNames &of, unsigned prev_off,
std::string baseName, std::string indent, StringRef argName, std::string& structName) {
//if(prev_off >= 1024) return;
// Handle Pointer type
DIType* baseTy = getLowestDINode(Ty);
if (!baseTy)
return;
// If that pointer is a struct
if (baseTy->getTag() == dwarf::DW_TAG_structure_type) {
//*file << "projection <struct " << arg->getType()->getStructName().str() << "" ;
structName = baseTy->getName().str();
DICompositeType *compType = dyn_cast<DICompositeType>(baseTy);
// Go thro struct elements and print them all
for (DINode *Op : compType->getElements()) {
DIDerivedType *der = dyn_cast<DIDerivedType>(Op);
unsigned offset = der->getOffsetInBits() >> 3;
std::string new_name(baseName);
if (new_name != "")
new_name.append(".");
new_name.append(der->getName().str());
of[offset + prev_off] = std::pair<std::string, DIType *>(
new_name, der->getBaseType().resolve(TypeIdentifierMap));
/// XXX: crude assumption that we want to peek only into those members
/// whose sizes are greater than 8 bytes
if (((der->getSizeInBits() >> 3) > 8)
&& der->getBaseType().resolve(TypeIdentifierMap)->getTag()) {
std::string tempStructName("");
getAllNames(dyn_cast<DIType>(der), of, prev_off + offset,
new_name, indent, argName, tempStructName);
}
//errs() << "--------------- " << der->getName().str() << "\n";
}
} else if (DIBasicType *bas = dyn_cast<DIBasicType>(baseTy)) {
structName = "";
//if type tag for the parameter is of pointer_type and DI type is DIBasicType
//then treat it as a pointer of native type
of[0] = std::pair<std::string, DIType *>(
argName.str(), bas);
} else {
structName = "";
}
//}
}
offsetNames getArgFieldNames(Function &F, unsigned argNumber, StringRef argName, std::string& structName) {
offsetNames offNames;
assert((argNumber == 0) &&
"Request for return type information. Not supported");
if (argNumber > F.arg_size()) {
errs() << "### WARN : requested data for non-existent element\n";
return offNames;
}
errs() << "## Function ## : " << F.getName().str()
<< " | argsize: " << F.arg_size() << " :: Requested " << argNumber
<< "\n";
SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
F.getAllMetadata(MDs);
for (auto &MD : MDs) {
if (MDNode *N = MD.second) {
if (auto *subRoutine = dyn_cast<DISubprogram>(N)->getType()) {
if (!subRoutine->getTypeArray()[0]) {
errs() << "return type \"void\" for Function : " << F.getName().str()
<< "\n";
}
const auto &TypeRef = subRoutine->getTypeArray();
/// XXX: When function arguments are coerced in IR, the corresponding
/// debugInfo extracted for that function from the source code will
/// not have the same number of arguments. Check the indexes to
/// prevent array out of bounds exception (segfault)
if (argNumber >= TypeRef.size()) {
errs() << "TypeArray request out of bounds. Are parameters coerced??\n";
goto done;
}
if (const auto &ArgTypeRef = TypeRef[argNumber]) {
// Resolve the type
DIType *Ty = ArgTypeRef.resolve(TypeIdentifierMap);
// Handle Pointer type
getAllNames(Ty, offNames, 0, "", " ", argName, structName);
}
}
}
}
done:
return offNames;
}
std::string getTypeNameFromDINode(DIType* dt) {
while(dt->getName().str() == "") {
dt = dyn_cast<DIDerivedType>(dt)->getBaseType().resolve(TypeIdentifierMap);
}
return dt->getName().str();
}
std::string getTypeName(DIType* dt, std::string function, std::string fieldName) {
if (DIBasicType* bt = dyn_cast<DIBasicType>(dt)) {
return bt->getName().str() + " " + fieldName;
} else if (DICompositeType* ct = dyn_cast<DICompositeType>(dt)) {
if (ct->getTag() == dwarf::DW_TAG_union_type) {
return "union " + fieldName;
} else {
return "projection " + function + "." + ct->getName().str() + " *" + fieldName;
}
} else if (DISubroutineType* sr = dyn_cast<DISubroutineType>(dt)) {
//if (!sr->getType()) {
//return "rpc function " + "(*" + fieldName + ")()";
//} else {
std::string name("rpc ");
const DITypeRefArray &types = sr->getTypeArray();
if (!types[0]) {
//return type void
name += "void (*" + fieldName + ") (";
} else {
DIType *ty = types[0].resolve(TypeIdentifierMap);
//DIType *baseTy = getLowestDINode(ty);
name += getTypeNameFromDINode(ty) + " (*" + fieldName + ") (";
}
unsigned int paramIndex = 1;
while (paramIndex < types.size()) {
DIType *ty = types[paramIndex].resolve(TypeIdentifierMap);
ty->dump();
//name += getTypeNameFromDINode(ty) + ", ";
name += getTypeName(ty, function, "") + ", ";
paramIndex++;
}
name += ");";
return name;
//}
} else {
if (DITypeRef temp = dyn_cast<DIDerivedType>(dt)->getBaseType()) {
DIType *baseTy = temp.resolve(TypeIdentifierMap);
return getTypeName(baseTy, function, fieldName);
}
/*DIType *baseTy =
dyn_cast<DIDerivedType>(dt)->getBaseType().resolve(TypeIdentifierMap);
while ((baseTy->getTag() == dwarf::DW_TAG_typedef || baseTy->getTag() == dwarf::DW_TAG_const_type
|| baseTy->getTag() == dwarf::DW_TAG_pointer_type || baseTy->getTag() == dwarf::DW_TAG_union_type)) {
if (DITypeRef temp = dyn_cast<DIDerivedType>(baseTy)->getBaseType())
baseTy = temp.resolve(TypeIdentifierMap);
else
break;
}
return baseTy->getName().str();*/
}
}
void offsetPrinter(const DSNode &node, std::ofstream &file, StringRef op,
offsetNames &of, std::string functionName, std::string indent) {
DSNode::const_offset_iterator ii, ei;
//int sz, i=0;
if (op.str() == "read") {
file << "\n" << indent << "Read: \n";
ii = node.read_offset_begin();
ei = node.read_offset_end();
//sz = node.read_offset_sz();
} else if (op.str() == "write") {
file << "\n" << indent << "Write: \n";
ii = node.write_offset_begin();
ei = node.write_offset_end();
//sz = node.write_offset_sz();
}
//errs() << "size: " << sz << "\n";
for (; ii != ei; ii++) {
unsigned offset = *ii;
std::string Name("????");
if (of.find(offset) != of.end()) {
Name = of.at(offset).first;
}
errs() << indent << " offset: " << *ii << "\t\t" << Name << "\n";
of.at(offset).second->dump();
file << indent << " offset: " << *ii << "\t\t" << getTypeName(of.at(offset).second, functionName, Name) << "\n";
}
}
bool DSAGenerator::runOnModule(Module &m) {
BU = &getAnalysis<BUDataStructures>();
std::error_code EC;
llvm::raw_fd_ostream F("bu", EC, sys::fs::OpenFlags::F_None);
BU->print(F, &m);
if (NamedMDNode *CU_Nodes = m.getNamedMetadata("llvm.dbg.cu")) {
TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
}
//std::error_code EC;
//StringRef b("bu");
//llvm::raw_fd_ostream F1(b, EC, sys::fs::OpenFlags::F_None);
//std::ofstream F1("bu");
//BU->print(F1, &m);
std::ofstream file(m.getName().str() + ".idl");
std::ofstream undefinedFunctionsFile(m.getName().str() + "_undefined_functions.txt");
std::string functionsListFile = getFunctionsList();
std::unordered_set<std::string> functions;
if(!functionsListFile.empty()) {
std::ifstream functionFile(functionsListFile);
std::copy(std::istream_iterator<std::string>(functionFile),
std::istream_iterator<std::string>(),
std::inserter(functions, functions.begin()));
}
for (auto &F : m) {
if (F.getName().find("llvm") == std::string::npos
&& (functions.empty() || functions.find(F.getName()) != functions.end())) {
if (F.isDeclaration()) {
undefinedFunctionsFile << F.getName().str() << "\n";
continue;
}
DSGraph *graph = BU->getDSGraph(F);
std::vector<DSNode *> argumentNodes;
//errs() << "has metadata: " << F.hasMetadata() << "\n";
for (auto &arg : F.args()) {
if (arg.hasName()) {
errs() << arg.getArgNo() << " = arg -->" << arg.getName().str() << "\n";
}
// XXX: What about non-pointer variables ??
if (arg.getType()->isPointerTy()) {
DSNodeHandle &nodeHandle = graph->getNodeForValue(&arg);
DSNode *node = nodeHandle.getNode();
errs() << "isglobal: " << node->isGlobalNode() << "-------------------\n";
if (node->isGlobalNode())
continue;
std::string structName;
offsetNames of = getArgFieldNames(F, arg.getArgNo() + 1, arg.getName(), structName);
dumpOffsetNames(of);
//file << "collapsed: " << node->isCollapsedNode() << "\n";
//file << "forward: " << node->isForwarding() << "\n";
std::vector<DSNode *> visitedNodes;
visitedNodes.push_back(node);
printOffsets(node, "", &file, &visitedNodes, of, arg.getType(), arg.getName(), structName, F.getName().str());
}
file << "\n";
}
}
}
//errs() << "global names\n";
//errs() << "size: " << m.getGlobalList().size() << "\n";
//for(llvm::Module::global_iterator gi = m.global_begin(); gi != m.global_end(); gi++) {
/*for(auto &gi : m.globals()) {
gi.dump();
errs() << gi.getName() << "\n";
gi.getType()->dump();
}*/
/*for(auto& nmd: m.getNamedMDList()) {
nmd.dump();
}*/
NamedMDNode* nmd = m.getNamedMetadata("llvm.dbg.cu");
if(DICompileUnit* cu = dyn_cast<DICompileUnit>(nmd->getOperand(0))) {
DIGlobalVariableArray globalVariables = cu->getGlobalVariables();
DSGraph* graph = BU->getGlobalsGraph();
for(unsigned int i=0; i<globalVariables.size(); i++) {
//globalVariables[i]->dump();
//errs() << globalVariables[i]->getDisplayName().str() << "\n";
Constant* var = globalVariables[i]->getVariable();
//var->dump();
DIType* diType = globalVariables[i]->getType().resolve(TypeIdentifierMap);
std::string structName;
offsetNames of;
getAllNames(diType, of, 0, "", " ", globalVariables[i]->getDisplayName(), structName);
//offsetNames of = getArgFieldNames(F, arg.getArgNo() + 1, arg.getName(), structName);
dumpOffsetNames(of);
DSNodeHandle &nodeHandle = graph->getNodeForValue(var);
DSNode *node = nodeHandle.getNode();
errs() << "name: " << globalVariables[i]->getDisplayName() << "-------------\n";
errs() << "is global: " << node->isGlobalNode() << "---------------\n";
//file << "collapsed: " << node->isCollapsedNode() << "\n";
//file << "forward: " << node->isForwarding() << "\n";
std::vector<DSNode *> visitedNodes;
visitedNodes.push_back(node);
printOffsets(node, "", &file, &visitedNodes, of, var->getType(), globalVariables[i]->getDisplayName(), structName, "bu.global");
file << "\n";
}
}
return false;
}
// Pass ID variable
char DSAGenerator::ID = 0;
}