forked from yuchi1989/ErrDoc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ErrDocAllPath.cpp
222 lines (183 loc) · 5.44 KB
/
ErrDocAllPath.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
#include "ClangSACheckers.h"
#include "clang/AST/ParentMap.h"
#include "clang/AST/StmtObjC.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include "llvm/Support/raw_ostream.h"
#include <unistd.h>
#include <string.h>
#include <iostream>
#include <vector>
using namespace clang;
using namespace ento;
using namespace std;
namespace {
/* CallRef structure stores each function call information. */
class CallRef: public llvm::FoldingSetNode {
public:
CallRef(string name):func_name(name),func_depth(0){}
CallRef(string name, unsigned depth):func_name(name),func_depth(depth){}
string func_name;
unsigned func_depth;
bool operator==(const CallRef &X) const
{
return func_name==X.func_name&&func_depth==X.func_depth;
}
bool operator<(const CallRef &X) const
{
if(func_name!=X.func_name){
return func_name < X.func_name;
}
else{
return func_depth < X.func_depth;
}
}
void Profile(llvm::FoldingSetNodeID &ID) const
{
ID.AddString(func_name);
ID.AddInteger(func_depth);
}
};
/* PathState structure stores path information for each CallRef */
struct PathState {
private:
/* the name of the caller function */
std::string func_name;
unsigned func_depth;
bool error_path;
public:
vector<string> path;
/* InFuncName: the name of the caller function */
PathState(std::string InFuncName, unsigned depth) : func_name(InFuncName),func_depth(depth),
error_path(false){
}
PathState(std::string InFuncName) : func_name(InFuncName),func_depth(0),
error_path(false){
}
PathState(const PathState *ps)
{
vector<string> p = ps->path;
for(size_t i = 0;i<p.size();i++){
path.push_back(p[i]);
}
error_path = ps->isErrorPath();
func_name = ps->getFuncName();
func_depth = ps->getDepth();
}
PathState(const PathState& ps)
{
vector<string> p = ps.path;
for(size_t i = 0;i<p.size();i++){
path.push_back(p[i]);
}
error_path = ps.isErrorPath();
func_name = ps.getFuncName();
func_depth = ps.getDepth();
}
std::string getFuncName() const
{
return func_name;
}
unsigned getDepth() const
{
return func_depth;
}
bool isErrorPath() const
{
return error_path;
}
void setError(){
error_path = true;
}
bool operator==(const PathState &X) const
{
return func_name==X.getFuncName();
}
void Profile(llvm::FoldingSetNodeID &ID) const
{
ID.AddString(func_name);
ID.AddInteger(func_depth);
ID.AddBoolean(error_path);
for(size_t i = 0;i<path.size();i++){
ID.AddString(path[i]);
}
}
};
class ErrDocAllPath : public Checker< check::PreCall,
check::PostCall > {
public:
void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
std::string getCaller(CheckerContext &C) const;
unsigned getStackDepth(CheckerContext &C) const;
void printMsg(std::string str) const;
};
}
/* It registers a map from CallRef to PathState. */
REGISTER_MAP_WITH_PROGRAMSTATE(PathMap, CallRef, PathState)
/* Add a function call to the currernt path */
void ErrDocAllPath::checkPreCall(const CallEvent &Call, CheckerContext &C) const {
if (Call.getCalleeIdentifier() == NULL) {
return;
}
StringRef name = Call.getCalleeIdentifier()->getName();
ProgramStateRef State = C.getState();
if(State){
unsigned depth = getStackDepth(C);
State = State->set<PathMap>(CallRef(name.str(),depth), PathState(name.str()));
string Callername = getCaller(C);
if(!Callername.empty()){
const PathState *ps = State->get<PathMap>(CallRef(Callername,depth-1));
if(ps!=nullptr){
PathState ps2 = PathState(ps);
ps2.path.push_back(name.str());
State = State->set<PathMap>(CallRef(Callername,depth-1), ps2);
}
}
C.addTransition(State);
}
}
void ErrDocAllPath::printMsg(std::string str) const {
llvm::errs() << "ErrDoc: " << str << "\n";
}
/* get caller name*/
std::string ErrDocAllPath::getCaller(CheckerContext &C) const
{
const clang::Decl *DC = C.getCurrentAnalysisDeclContext()->getDecl();
return DC->getAsFunction()->getNameInfo().getAsString();
}
/* get call stack depth*/
unsigned ErrDocAllPath::getStackDepth(CheckerContext &C) const
{
unsigned stack_depth = 0;
for (const LocationContext *LCtx = C.getLocationContext(); LCtx;
LCtx = LCtx->getParent()) {
if (LCtx->getKind() ==
LocationContext::ContextKind::StackFrame) {
stack_depth++;
}
}
return stack_depth;
}
/*Print a path*/
void ErrDocAllPath::checkPostCall(const CallEvent &Call, CheckerContext &C) const {
ProgramStateRef state = C.getState();
if(state){
StringRef name = Call.getCalleeIdentifier()->getName();
unsigned depth = getStackDepth(C);
const PathState *ps = state->get<PathMap>(CallRef(name.str(),depth));
if(ps!=nullptr){
if(ps->path.size()>0){
printMsg("Path in function " + Call.getCalleeIdentifier()->getName().str());
for(size_t i = 0; i<ps->path.size();i++ ){
printMsg("P: " + ps->path[i]);
}
}
}
}
}
void ento::registerErrDocAllPath(CheckerManager &mgr) {
mgr.registerChecker<ErrDocAllPath>();
}