This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGetEntryExit.cpp
391 lines (352 loc) · 12 KB
/
GetEntryExit.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
/*
* GetEntryPoint.cpp
*
* Created on: 2014/06/03
* Author: yoshimura
*/
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
#include "clang/AST/StmtVisitor.h"
#include "llvm/ADT/SetVector.h"
using namespace clang;
using namespace ento;
namespace {
typedef llvm::SmallString<64> FuncString;
typedef FuncString FieldString;
typedef FuncString StructString;
typedef llvm::SmallSetVector<FuncString, 100> FuncSet;
typedef llvm::SmallSetVector<std::tuple<FuncString, FuncString>, 100> FuncPairSet;
typedef llvm::SmallSetVector<std::tuple<StructString, FieldString, FuncString>, 100> StructFuncSet;
typedef llvm::StringMap<FuncSet> FuncExitMap;
class SearchEntryCallInFunction : public StmtVisitor<SearchEntryCallInFunction> {
private:
FuncPairSet funcArgs;
FuncSet rhsFuncs;
StructFuncSet structFuncs;
public:
void VisitChildren(Stmt *stmt) {
for (auto i = stmt->child_begin(), e = stmt->child_end(); i != e; ++i)
if (Stmt *child = *i)
Visit(child);
}
/* for cases like:
* static int g(int arg) {
* ...
* }
* static void f() {
* int (*open)(int arg);
* ...
* open = g; // open may be passed to global functions
* ...
* }
*/
void VisitBinaryOperator(BinaryOperator *op) {
VisitChildren(op);
if (!op->isAssignmentOp())
return;
StringRef funcName;
// right of =
if (const DeclRefExpr *declExpr = dyn_cast_or_null<DeclRefExpr>(op->getRHS()->IgnoreParenCasts())) {
if (const FunctionDecl *funcDecl = dyn_cast_or_null<FunctionDecl>(declExpr->getDecl())) {
funcName = funcDecl->getIdentifier()->getName();
}
}
if (funcName.empty())
return;
// left of =
if (const MemberExpr *declExpr = dyn_cast_or_null<MemberExpr>(op->getLHS()->IgnoreParenCasts())) {
if (const FieldDecl *fieldDecl = dyn_cast_or_null<FieldDecl>(declExpr->getMemberDecl())) {
StringRef fieldName = fieldDecl->getIdentifier()->getName();
RecordDecl *recordDecl = const_cast<RecordDecl*>(fieldDecl->getParent());
IdentifierInfo * structIdentifier = recordDecl->getIdentifier();
while (!structIdentifier) {
recordDecl = const_cast<RecordDecl*>(dyn_cast_or_null<RecordDecl>(recordDecl->getParent()));
if (!recordDecl)
break;
structIdentifier = recordDecl->getIdentifier();
}
StringRef structName;
if (structIdentifier)
structName = structIdentifier->getName();
else
structName = "(no-name)";
structFuncs.insert(std::make_tuple(structName, fieldName, funcName));
return;
}
}
rhsFuncs.insert(funcName);
}
/* for cases like:
* static int h(int arg) {
* ...
* }
* static int g(int (*open)(int arg)) {
* ... // open may be passed to global functions
* }
* static void f() {
* ...
* g(h);
* ...
* }
*/
void VisitCallExpr(CallExpr *call) {
VisitChildren(call);
const FunctionDecl * calleeDecl = call->getDirectCallee();
if (!calleeDecl || !calleeDecl->isGlobal())
return;
StringRef calleeName = calleeDecl->getIdentifier()->getName();
int i, e = call->getNumArgs();
for (i = 0; i < e; i++) {
if (DeclRefExpr *argExpr = dyn_cast<DeclRefExpr>(call->getArg(i)->IgnoreParenCasts()))
if (const FunctionDecl *decl = dyn_cast_or_null<FunctionDecl>(argExpr->getDecl()))
funcArgs.insert(std::make_tuple(calleeName, decl->getIdentifier()->getName()));
}
}
void VisitStmt(Stmt *S) {
VisitChildren(S);
}
void getFuncArgs(FuncPairSet &result) {
result.insert(funcArgs.begin(), funcArgs.end());
}
void getRhsFuncs(FuncSet &result) {
result.insert(rhsFuncs.begin(), rhsFuncs.end());
}
void getStructFuncs(StructFuncSet &result) {
result.insert(structFuncs.begin(), structFuncs.end());
}
};
//for getting exit points
class WalkCall : public StmtVisitor<WalkCall> {
FuncSet walked;
FuncSet passed;
public:
void VisitChildren(Stmt *stmt) {
for (auto i = stmt->child_begin(), e = stmt->child_end(); i != e; ++i)
if (Stmt *child = *i)
Visit(child);
}
void VisitCallExpr(CallExpr *call) {
VisitChildren(call);
if (const FunctionDecl * funcDecl = call->getDirectCallee()) {
StringRef funcName = funcDecl->getIdentifier()->getName();
if (!funcDecl->hasBody()) {
walked.insert(funcName);
}
else if (passed.count(funcName) == 0) {
passed.insert(funcName);
Visit(funcDecl->getBody());
}
}
}
void VisitStmt(Stmt *S) {
VisitChildren(S);
}
FuncSet & getWalkResult() {
return walked;
}
};
class GetEntryExit : public Checker<check::ASTDecl<FunctionDecl>,
check::ASTDecl<VarDecl>, check::EndAnalysis> {
public:
GetEntryExit() : noCall("(no-calls)") {}
void checkASTDecl(const FunctionDecl *fd, AnalysisManager &mgr, BugReporter &reporter) const;
void checkASTDecl(const VarDecl *var, AnalysisManager &mgr, BugReporter &reporter) const;
void checkEndAnalysis(ExplodedGraph &unused, BugReporter &unused2, ExprEngine &eng) const;
private:
void traverseListExpr(const Decl* left, Stmt * right) const;
void printExitPoints(llvm::raw_fd_ostream &output, StringRef funcName) const;
mutable StructFuncSet structFuncs;
mutable FuncSet initFuncs;
mutable FuncSet globalFuncs;
mutable FuncPairSet funcArgs;
mutable FuncSet rhsFuncs;
mutable FuncExitMap exitPoints;
const StringRef noCall;
};
} // end of anonymous namespace
void GetEntryExit::traverseListExpr(const Decl* left, Stmt * right) const {
const FieldDecl * fieldDecl = dyn_cast<FieldDecl>(left);
if (!fieldDecl)
return;
if (const DeclRefExpr *refExpr = dyn_cast<DeclRefExpr>(right->IgnoreImplicit())) {
if (const FunctionDecl *declFunc = dyn_cast<FunctionDecl>(refExpr->getDecl())) {
StringRef fieldName = fieldDecl->getIdentifier()->getName();
RecordDecl *recordDecl = const_cast<RecordDecl*>(fieldDecl->getParent());
IdentifierInfo * structIdentifier = recordDecl->getIdentifier();
while (!structIdentifier) {
recordDecl = const_cast<RecordDecl*>(dyn_cast_or_null<RecordDecl>(recordDecl->getParent()));
if (!recordDecl)
break;
structIdentifier = recordDecl->getIdentifier();
}
StringRef structName;
if (structIdentifier)
structName = structIdentifier->getName();
else
structName = "(no-name)";
structFuncs.insert(std::make_tuple(structName, fieldName, declFunc->getIdentifier()->getName()));
}
}
else if (const InitListExpr *listExpr = dyn_cast<InitListExpr>(right->IgnoreImplicit())) {
/* for cases like:
* const static struct x_ops op = { // op may be passed to global functions
* .open = f,
* .pm = { .suspend = g, .... }, // some nested structs are included
* .close = ...
* };
*/
if (const RecordType * type = listExpr->getType()->getAsStructureType()) {
const RecordDecl *nextStructDecl = type->getDecl();
DeclContext::decl_iterator i = nextStructDecl->decls_begin(), e = nextStructDecl->decls_end();
InitListExpr::const_iterator i2 = listExpr->begin(), e2 = listExpr->end();
for (; i != e && i2 != e2; i2++) {
bool increment = true;
if (const RecordDecl *recordDecl = dyn_cast<RecordDecl>(*i)) {
if (recordDecl->getIdentifier() == nullptr) {
increment = false;
}
}
traverseListExpr(*i, *i2);
if (increment)
i++;
}
}
}
}
/* for cases like:
* static void f() {
* ...
* }
* const static struct x_ops op = { // op may be passed to global functions
* .open = f,
* .close = ...
* };
* static void (*glob)(void) = f; // glob may be passed to global functions
*/
void GetEntryExit::checkASTDecl(const VarDecl *varDecl, AnalysisManager &mgr, BugReporter &reporter) const {
if (!mgr.getSourceManager().isInMainFile(varDecl->getLocation()))
return;
const Expr *init = varDecl->getInit();
if (!init)
return;
if (const RecordType * type = varDecl->getType()->getAsStructureType()) {
const RecordDecl *structDecl = type->getDecl();
if (structDecl->getTagKind() == RecordDecl::TagKind::TTK_Struct) {
if (const InitListExpr *declExpr = dyn_cast<InitListExpr>(init->IgnoreParenCasts())) {
DeclContext::decl_iterator i = structDecl->decls_begin(), e = structDecl->decls_end();
InitListExpr::const_iterator i2 = declExpr->begin(), e2 = declExpr->end();
for (; i != e && i2 != e2; i2++) {
bool increment = true;
if (const RecordDecl *recordDecl = dyn_cast<RecordDecl>(*i)) {
if (recordDecl->getIdentifier() == nullptr) {
increment = false;
}
}
traverseListExpr(*i, *i2);
if (increment)
i++;
}
}
}
}
else if (const DeclRefExpr *refExpr = dyn_cast<DeclRefExpr>(init->IgnoreParenCasts())) {
if (const FunctionDecl * funcDecl = dyn_cast<FunctionDecl>(refExpr->getDecl()))
initFuncs.insert(funcDecl->getIdentifier()->getName());
}
}
/* for cases like:
* void f() { // any functions can call
* ...
* }
*/
void GetEntryExit::checkASTDecl(const FunctionDecl *fd, AnalysisManager &mgr, BugReporter &reporter) const {
if (!fd->hasBody())
return;
StringRef funcName = fd->getIdentifier()->getName();
if (mgr.getSourceManager().isInMainFile(fd->getLocation())) {
SearchEntryCallInFunction search;
search.Visit(fd->getBody());
search.getFuncArgs(funcArgs);
search.getRhsFuncs(rhsFuncs);
search.getStructFuncs(structFuncs);
if (fd->getStorageClass() == StorageClass::SC_None) {
globalFuncs.insert(funcName);
}
}
//for getting exit points
WalkCall walk;
walk.Visit(fd->getBody());
FuncSet exitFuncs = walk.getWalkResult();
if (!exitFuncs.empty()) {
exitPoints[funcName].insert(exitFuncs.begin(), exitFuncs.end());
}
else {
exitPoints[funcName].insert(noCall);
}
}
//for checkEndAnalysis
void GetEntryExit::printExitPoints(llvm::raw_fd_ostream &output, StringRef funcName) const {
output << "\t";
if (exitPoints.count(funcName) > 0) {
FuncSet exitPointsForThisFunc = exitPoints[funcName];
auto i2 = exitPointsForThisFunc.begin(), e2 = exitPointsForThisFunc.end();
output << *(i2++);
for (; i2 != e2; i2++)
output << " " << *i2;
}
else {
output << "(has-no-body)";
}
}
//output results in .entry files
void GetEntryExit::checkEndAnalysis(ExplodedGraph &unused, BugReporter &unused2, ExprEngine &eng) const {
SourceManager &mgr = eng.getAnalysisManager().getSourceManager();
StringRef mainFileName = mgr.getFileEntryForID(mgr.getMainFileID())->getName();
size_t cpos = mainFileName.rfind(".c");
if (cpos == std::string::npos) {
llvm::errs() << "[GetEntryExit] Not .c file: " << mainFileName << "\n";
}
SmallString<128> fileName = mainFileName.substr(0, cpos);
fileName.append(".entry");
SmallString<128> prefix = mainFileName.substr(0, cpos);
prefix.append("/");
std::error_code err;
llvm::raw_fd_ostream output(fileName, err, llvm::sys::fs::F_Text);
if (output.has_error()) {
llvm::errs() << "[GetEntryExit] failed to open file: " << fileName << "\n\t:" << err.message() << "\n";
return;
}
for (auto i = structFuncs.begin(), e = structFuncs.end(); i != e; i++) {
output << std::get<0>(*i) << "::" << std::get<1>(*i)
<< "\t" << prefix << std::get<2>(*i);
printExitPoints(output, std::get<2>(*i));
output << "\n";
}
for (auto i = funcArgs.begin(), e = funcArgs.end(); i != e; i++) {
output << "argof(" << std::get<0>(*i) << ")\t" << prefix << std::get<1>(*i);
printExitPoints(output, std::get<1>(*i));
output << "\n";
}
for (auto i = initFuncs.begin(), e = initFuncs.end(); i != e; i++) {
output << "init" << '\t' << *i;
printExitPoints(output, *i);
output << "\n";
}
for (auto i = globalFuncs.begin(), e = globalFuncs.end(); i != e; i++) {
output << "global" << '\t' << *i;
printExitPoints(output, *i);
output << "\n";
}
for (auto i = rhsFuncs.begin(), e = rhsFuncs.end(); i != e; i++) {
output << "rhs" << '\t' << *i;
printExitPoints(output, *i);
output << "\n";
}
}
// register this checker
void registerGetEntryExit(CheckerRegistry ®istry) {
registry.addChecker<GetEntryExit>("linux.GetEntryExit",
"Shows exit point functions for each entry point function in a translation unit");
}