-
Notifications
You must be signed in to change notification settings - Fork 71
/
BugDetectorDriver.cpp
203 lines (182 loc) · 9.14 KB
/
BugDetectorDriver.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
//
// Created by machiry on 12/28/16.
//
#include "bug_detectors/BugDetectorDriver.h"
#include "bug_detectors/TaintedPointerDereference.h"
#include "bug_detectors/KernelUninitMemoryLeakDetector.h"
#include "bug_detectors/TaintedSizeDetector.h"
#include "bug_detectors/InvalidCastDetector.h"
#include "bug_detectors/IntegerOverflowDetector.h"
#include "bug_detectors/ImproperTaintedDataUseDetector.h"
#include "bug_detectors/TaintedLoopBoundDetector.h"
#include "bug_detectors/GlobalVariableRaceDetector.h"
using namespace llvm;
namespace DRCHECKER {
//#define DISABLE_INVALIDCASTDETECTOR
//#define DISABLE_INTEGEROVERFLOWDETECTOR
//#define DISABLE_TAINTEDPOINTERDEREFERENCE
//#define DISABLE_KERNELUNITMEMORYLEAKDETECTOR
//#define DISABLE_TAINEDSIZEDETECTOR
//#define DISABLE_IMPROPERTAINTEDDATAUSEDETECTOR
//#define DISABLE_TAINTEDLOOPBOUNDDETECTOR
//#define DISABLE_GLOBALVARIABLERACEDETECTOR
void BugDetectorDriver::addPreAnalysisBugDetectors(GlobalState &targetState,
Function *toAnalyze,
std::vector<Instruction *> *srcCallSites,
std::vector<VisitorCallback *> *allCallbacks,
FunctionChecker *targetChecker) {
#ifndef DISABLE_INVALIDCASTDETECTOR
VisitorCallback *invalidCastDetector = new InvalidCastDetector(targetState,
toAnalyze,
srcCallSites, targetChecker);
allCallbacks->push_back(invalidCastDetector);
#endif
#ifndef DISABLE_INTEGEROVERFLOWDETECTOR
VisitorCallback *integerOverflowDetector = new IntegerOverflowDetector(targetState,
toAnalyze,
srcCallSites, targetChecker);
allCallbacks->push_back(integerOverflowDetector);
#endif
}
void BugDetectorDriver::addPostAnalysisBugDetectors(GlobalState &targetState,
Function *toAnalyze,
std::vector<Instruction *> *srcCallSites,
std::vector<VisitorCallback *> *allCallbacks,
FunctionChecker *targetChecker) {
#ifndef DISABLE_TAINTEDPOINTERDEREFERENCE
VisitorCallback *currTaintDetector = new TaintedPointerDereference(targetState,
toAnalyze,
srcCallSites, targetChecker);
allCallbacks->push_back(currTaintDetector);
#endif
#ifndef DISABLE_KERNELUNITMEMORYLEAKDETECTOR
VisitorCallback *currLeakDetector = new KernelUninitMemoryLeakDetector(targetState,
toAnalyze,
srcCallSites, targetChecker);
allCallbacks->push_back(currLeakDetector);
#endif
#ifndef DISABLE_TAINEDSIZEDETECTOR
VisitorCallback *currTaintSizeDetector = new TaintedSizeDetector(targetState,
toAnalyze,
srcCallSites, targetChecker);
allCallbacks->push_back(currTaintSizeDetector);
#endif
#ifndef DISABLE_IMPROPERTAINTEDDATAUSEDETECTOR
VisitorCallback *currImproperDataUseDetector = new ImproperTaintedDataUseDetector(targetState,
toAnalyze,
srcCallSites, targetChecker);
allCallbacks->push_back(currImproperDataUseDetector);
#endif
#ifndef DISABLE_TAINTEDLOOPBOUNDDETECTOR
VisitorCallback *taintedCondDetector = new TaintedLoopBoundDetector(targetState,
toAnalyze,
srcCallSites, targetChecker);
allCallbacks->push_back(taintedCondDetector);
#endif
#ifndef DISABLE_GLOBALVARIABLERACEDETECTOR
VisitorCallback *globalVarRaceDetector = new GlobalVariableRaceDetector(targetState,
toAnalyze,
srcCallSites, targetChecker);
allCallbacks->push_back(globalVarRaceDetector);
#endif
}
void BugDetectorDriver::printAllWarnings(GlobalState &targetState, llvm::raw_ostream& O) {
O << "{\"num_contexts\":";
if(targetState.allVulnWarnings.size() == 0) {
O << "0";
//O << "No Warnings. Everything looks good\n";
} else {
O << targetState.allVulnWarnings.size() << ",\n";
bool addout_comma = false;
O << "\"all_contexts\":[\n";
for (auto warn_iter = targetState.allVulnWarnings.begin(); warn_iter != targetState.allVulnWarnings.end();
warn_iter++) {
bool addin_comma = false;
if(addout_comma) {
O << ",\n";
}
O << "{";
AnalysisContext *targetContext = warn_iter->first;
std::set<VulnerabilityWarning *> *allWarnings = warn_iter->second;
O << "\"num_warnings\":" << allWarnings->size() << ",\n";
// O << "At Calling Context:";
targetContext->printContext(O);
O << ",";
//O << "Found:" << allWarnings->size() << " warnings.\n";
long currWarningNo = 1;
O << "\"warnings\":[\n";
for (VulnerabilityWarning *currWarning:*(allWarnings)) {
if(addin_comma) {
O << ",\n";
}
O << "{";
O << "\"warn_no\":" << currWarningNo << ",";
currWarning->printWarning(O);
currWarningNo++;
addin_comma = true;
O << "}";
}
O << "\n]";
addout_comma = true;
O << "}";
}
O << "]\n";
}
O << "\n}";
}
void BugDetectorDriver::printWarningsByInstr(GlobalState &targetState, llvm::raw_ostream& O) {
O << "{\"num_instructions\":";
if(targetState.warningsByInstr.size() == 0) {
O << "0";
//O << "No Warnings. Everything looks good\n";
} else {
O << targetState.warningsByInstr.size() << ",\n";
bool addout_comma = false;
O << "\"all_instrs\":[\n";
for (auto warn_iter = targetState.warningsByInstr.begin(); warn_iter != targetState.warningsByInstr.end();
warn_iter++) {
bool addin_comma = false;
if(addout_comma) {
O << ",\n";
}
O << "{";
Instruction *currInstr = warn_iter->first;
std::set<VulnerabilityWarning *> *allWarnings = warn_iter->second;
O << "\"num_warnings\":" << allWarnings->size() << ",\n";
O << "\"at\":\"";
O << InstructionUtils::escapeValueString(currInstr) << "\",";
O << "\"at_line\":";
DILocation *instrLoc = nullptr;
//instrLoc = this->target_instr->getDebugLoc().get();
instrLoc = InstructionUtils::getCorrectInstrLocation(currInstr);
if(instrLoc != nullptr) {
//O << ", src line:" << instrLoc->getLine() << " file:" << instrLoc->getFilename();
O << instrLoc->getLine() << ",\"at_file\":\"" << InstructionUtils::escapeJsonString(instrLoc->getFilename()) << "\",";
} else {
//O << ", No line";
O << "-1,";
}
O << "\"at_func\":\"" << InstructionUtils::escapeJsonString(currInstr->getFunction()->getName()) << "\",";
//O << "Found:" << allWarnings->size() << " warnings.\n";
long currWarningNo = 1;
O << "\"warnings\":[\n";
for (VulnerabilityWarning *currWarning:*(allWarnings)) {
if(addin_comma) {
O << ",\n";
}
O << "{";
O << "\"warn_no\":" << currWarningNo << ",";
currWarning->printWarning(O);
currWarningNo++;
addin_comma = true;
O << "}";
}
O << "\n]";
addout_comma = true;
O << "}";
}
O << "]\n";
}
O << "\n}";
}
}