Static Value-Flow Analysis
Loading...
Searching...
No Matches
SVFBugReport.cpp
Go to the documentation of this file.
1//===- SVFBugReport.cpp -- Base class for statistics---------------------------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-> <Yulei Sui>
6//
7
8// This program is free software: you can redistribute it and/or modify
9// it under the terms of the GNU Affero General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU Affero General Public License for more details.
17
18// You should have received a copy of the GNU Affero General Public License
19// along with this program. If not, see <http://www.gnu.org/licenses/>.
20//
21//===----------------------------------------------------------------------===//
22
23
24//
25// Created by JoelYang on 2023/4/19.
26//
27
28#include <cassert>
29#include <sstream>
30#include <fstream>
31
32#include "Graphs/ICFGNode.h"
33#include "Util/cJSON.h"
34#include "Util/SVFBugReport.h"
35#include "Util/SVFUtil.h"
37
38
39using namespace std;
40using namespace SVF;
41
42const std::map<GenericBug::BugType, std::string> GenericBug::BugType2Str =
43{
44 {GenericBug::FULLBUFOVERFLOW, "Full Buffer Overflow"},
45 {GenericBug::PARTIALBUFOVERFLOW, "Partial Buffer Overflow"},
46 {GenericBug::NEVERFREE, "Never Free"},
47 {GenericBug::PARTIALLEAK, "Partial Leak"},
48 {GenericBug::FILENEVERCLOSE, "File Never Close"},
49 {GenericBug::FILEPARTIALCLOSE, "File Partial Close"},
50 {GenericBug::DOUBLEFREE, "Double Free"},
51 {GenericBug::FULLNULLPTRDEREFERENCE, "Full Null Ptr Dereference"},
52 {GenericBug::PARTIALNULLPTRDEREFERENCE, "Partial Null Ptr Dereference"}
53};
54
55const std::string GenericBug::getLoc() const
56{
57 const SVFBugEvent&sourceInstEvent = bugEventStack.at(bugEventStack.size() -1);
58 return sourceInstEvent.getEventLoc();
59}
60
61const std::string GenericBug::getFuncName() const
62{
63 const SVFBugEvent&sourceInstEvent = bugEventStack.at(bugEventStack.size() -1);
64 return sourceInstEvent.getFuncName();
65}
66
68{
69 const auto endpoint = [](const BoundedInt& value)
70 {
71 if (value.is_infinity())
72 return cJSON_CreateString(value.is_plus_infinity() ? "+oo" : "-oo");
73 return cJSON_CreateNumber(value.getNumeral());
74 };
75 cJSON *bugDescription = cJSON_CreateObject();
76 cJSON *allocLB = endpoint(allocLowerBound);
77 cJSON *allocUB = endpoint(allocUpperBound);
78 cJSON *accessLB = endpoint(accessLowerBound);
79 cJSON *accessUB = endpoint(accessUpperBound);
80
81 cJSON_AddItemToObject(bugDescription, "AllocLowerBound", allocLB);
82 cJSON_AddItemToObject(bugDescription, "AllocUpperBound", allocUB);
83 cJSON_AddItemToObject(bugDescription, "AccessLowerBound", accessLB);
84 cJSON_AddItemToObject(bugDescription, "AccessUpperBound", accessUB);
85
86 return bugDescription;
87}
88
90{
91 const auto endpoint = [](const BoundedInt& value) -> std::string
92 {
93 if (value.is_infinity()) return value.is_plus_infinity() ? "+oo" : "-oo";
94 return std::to_string(value.getNumeral());
95 };
96 stringstream bugInfo;
98 {
99 SVFUtil::errs() << SVFUtil::bugMsg1("\t Full Overflow :") << " accessing at : ("
100 << GenericBug::getLoc() << ")\n";
101
102 }
103 else
104 {
105 SVFUtil::errs() << SVFUtil::bugMsg1("\t Partial Overflow :") << " accessing at : ("
106 << GenericBug::getLoc() << ")\n";
107 }
108 bugInfo << "\t\t allocate size : [" << endpoint(allocLowerBound) << ", " << endpoint(allocUpperBound) << "], ";
109 bugInfo << "access size : [" << endpoint(accessLowerBound) << ", " << endpoint(accessUpperBound) << "]\n";
110 SVFUtil::errs() << "\t\t Info : \n" << bugInfo.str();
111 SVFUtil::errs() << "\t\t Events : \n";
112
113 for(auto event : bugEventStack)
114 {
115 switch(event.getEventType())
116 {
118 {
119 SVFUtil::errs() << "\t\t callsite at : ( " << event.getEventLoc() << " )\n";
120 break;
121 }
122 default:
123 {
124 // TODO: implement more events when needed
125 break;
126 }
127 }
128 }
129}
130
132{
133 cJSON *bugDescription = cJSON_CreateObject();
134 return bugDescription;
135}
136
138{
139 SVFUtil::errs() << SVFUtil::bugMsg1("\t NeverFree :") << " memory allocation at : ("
140 << GenericBug::getLoc() << ")\n";
141}
142
144{
145 cJSON *bugDescription = cJSON_CreateObject();
146 cJSON *pathInfo = cJSON_CreateArray();
147 auto lastBranchEventIt = bugEventStack.end() - 1;
148 for(auto eventIt = bugEventStack.begin(); eventIt != lastBranchEventIt; eventIt++)
149 {
150 cJSON *newBranch = cJSON_CreateObject();
151 cJSON *branchLoc = cJSON_Parse((*eventIt).getEventLoc().c_str());
152 if(branchLoc == nullptr) branchLoc = cJSON_CreateObject();
153
154 cJSON *branchCondition = cJSON_CreateString((*eventIt).getEventDescription().c_str());
155
156 cJSON_AddItemToObject(newBranch, "BranchLoc", branchLoc);
157 cJSON_AddItemToObject(newBranch, "BranchCond", branchCondition);
158
159 cJSON_AddItemToArray(pathInfo, newBranch);
160 }
161
162 cJSON_AddItemToObject(bugDescription, "ConditionalFreePath", pathInfo);
163
164 return bugDescription;
165}
166
168{
169 SVFUtil::errs() << SVFUtil::bugMsg2("\t PartialLeak :") << " memory allocation at : ("
170 << GenericBug::getLoc() << ")\n";
171
172 SVFUtil::errs() << "\t\t conditional free path: \n";
173 auto lastBranchEventIt = bugEventStack.end() - 1;
174 for(auto eventIt = bugEventStack.begin(); eventIt != lastBranchEventIt; eventIt++)
175 {
176 SVFUtil::errs() << "\t\t --> (" << (*eventIt).getEventLoc() << "|" << (*eventIt).getEventDescription() << ") \n";
177 }
178 SVFUtil::errs() << "\n";
179}
180
182{
183 cJSON *bugDescription = cJSON_CreateObject();
184
185 cJSON *pathInfo = cJSON_CreateArray();
186 auto lastBranchEventIt = bugEventStack.end() - 1;
187 for(auto eventIt = bugEventStack.begin(); eventIt != lastBranchEventIt; eventIt++)
188 {
189 cJSON *newBranch = cJSON_CreateObject();
190 cJSON *branchLoc = cJSON_Parse((*eventIt).getEventLoc().c_str());
191 if(branchLoc == nullptr) branchLoc = cJSON_CreateObject();
192 cJSON *branchCondition = cJSON_CreateString((*eventIt).getEventDescription().c_str());
193
194 cJSON_AddItemToObject(newBranch, "BranchLoc", branchLoc);
195 cJSON_AddItemToObject(newBranch, "BranchCond", branchCondition);
196
197 cJSON_AddItemToArray(pathInfo, newBranch);
198 }
199 cJSON_AddItemToObject(bugDescription, "DoubleFreePath", pathInfo);
200
201 return bugDescription;
202}
203
205{
206 SVFUtil::errs() << SVFUtil::bugMsg2("\t Double Free :") << " memory allocation at : ("
207 << GenericBug::getLoc() << ")\n";
208
209 SVFUtil::errs() << "\t\t double free path: \n";
210 auto lastBranchEventIt = bugEventStack.end() - 1;
211 for(auto eventIt = bugEventStack.begin(); eventIt != lastBranchEventIt; eventIt++)
212 {
213 SVFUtil::errs() << "\t\t --> (" << (*eventIt).getEventLoc() << "|" << (*eventIt).getEventDescription() << ") \n";
214 }
215 SVFUtil::errs() << "\n";
216}
217
219{
220 cJSON *bugDescription = cJSON_CreateObject();
221 return bugDescription;
222}
223
225{
226 SVFUtil::errs() << SVFUtil::bugMsg1("\t FileNeverClose :") << " file open location at : ("
227 << GenericBug::getLoc() << ")\n";
228}
229
231{
232 cJSON *bugDescription = cJSON_CreateObject();
233
234 cJSON *pathInfo = cJSON_CreateArray();
235
236 auto lastBranchEventIt = bugEventStack.end() - 1;
237 for(auto eventIt = bugEventStack.begin(); eventIt != lastBranchEventIt; eventIt++)
238 {
239 cJSON *newBranch = cJSON_CreateObject();
240 cJSON *branchLoc = cJSON_Parse((*eventIt).getEventLoc().c_str());
241 if(branchLoc == nullptr) branchLoc = cJSON_CreateObject();
242 cJSON *branchCondition = cJSON_CreateString((*eventIt).getEventDescription().c_str());
243
244 cJSON_AddItemToObject(newBranch, "BranchLoc", branchLoc);
245 cJSON_AddItemToObject(newBranch, "BranchCond", branchCondition);
246
247 cJSON_AddItemToArray(pathInfo, newBranch);
248 }
249 cJSON_AddItemToObject(bugDescription, "ConditionalFileClosePath", pathInfo);
250
251 return bugDescription;
252}
253
255{
256 SVFUtil::errs() << SVFUtil::bugMsg2("\t PartialFileClose :") << " file open location at : ("
257 << GenericBug::getLoc() << ")\n";
258
259 SVFUtil::errs() << "\t\t conditional file close path: \n";
260 auto lastBranchEventIt = bugEventStack.end() - 1;
261 for(auto eventIt = bugEventStack.begin(); eventIt != lastBranchEventIt; eventIt++)
262 {
263 SVFUtil::errs() << "\t\t --> (" << (*eventIt).getEventLoc() << "|" << (*eventIt).getEventDescription() << ") \n";
264 }
265 SVFUtil::errs() << "\n";
266}
267
269{
270 cJSON *bugDescription = cJSON_CreateObject();
271 return bugDescription;
272}
273
275{
276 SVFUtil::errs() << SVFUtil::bugMsg2("\t FullNullPtrDereference :") << " dereference at : ("
277 << GenericBug::getLoc() << ")\n";
278}
279
281{
282 cJSON *bugDescription = cJSON_CreateObject();
283 return bugDescription;
284}
285
287{
288 SVFUtil::errs() << SVFUtil::bugMsg2("\t PartialNullPtrDereference :") << " dereference at : ("
289 << GenericBug::getLoc() << ")\n";
290}
291
292const std::string SVFBugEvent::getFuncName() const
293{
294 return eventInst->getFun()->getName();
295}
296
297const std::string SVFBugEvent::getEventLoc() const
298{
299 return eventInst->getSourceLoc();
300}
301
302const std::string SVFBugEvent::getEventDescription() const
303{
304 switch(getEventType())
305 {
307 {
309 {
310 return "True";
311 }
312 else
313 {
314 return "False";
315 }
316 break;
317 }
319 {
320 std::string description("calls ");
321 assert(SVFUtil::isa<CallICFGNode>(eventInst) && "not a call ICFGNode?");
322 const FunObjVar *callee = SVFUtil::cast<CallICFGNode>(eventInst)->getCalledFunction();
323 if(callee == nullptr)
324 {
325 description += "<unknown>";
326 }
327 else
328 {
329 description += callee->getName();
330 }
331 return description;
332 break;
333 }
335 {
336 return "None";
337 }
338 default:
339 {
340 assert(false && "No such type of event!");
341 abort();
342 }
343 }
344}
345
347{
348 for(auto bugIt: bugSet)
349 {
350 delete bugIt;
351 }
352}
353
354void SVFBugReport::dumpToJsonFile(const std::string& filePath) const
355{
356 std::map<u32_t, std::string> eventType2Str =
357 {
358 {SVFBugEvent::CallSite, "call site"},
359 {SVFBugEvent::Caller, "caller"},
360 {SVFBugEvent::Loop, "loop"},
361 {SVFBugEvent::Branch, "branch"}
362 };
363
364 ofstream jsonFile(filePath, ios::out);
365
366 jsonFile << "{\n";
367
369 jsonFile << "\"Defects\": [\n";
370 size_t commaCounter = bugSet.size() - 1;
371 for (auto bugPtr : bugSet)
372 {
373 cJSON *singleDefect = cJSON_CreateObject();
374
376 cJSON *bugType = cJSON_CreateString(
377 GenericBug::BugType2Str.at(bugPtr->getBugType()).c_str());
378 cJSON_AddItemToObject(singleDefect, "DefectType", bugType);
379
380 cJSON *bugLoc = cJSON_Parse(bugPtr->getLoc().c_str());
381 if (bugLoc == nullptr)
382 {
383 bugLoc = cJSON_CreateObject();
384 }
385 cJSON_AddItemToObject(singleDefect, "Location", bugLoc);
386
387 cJSON *bugFunction = cJSON_CreateString(
388 bugPtr->getFuncName().c_str());
389 cJSON_AddItemToObject(singleDefect, "Function", bugFunction);
390
391 cJSON_AddItemToObject(singleDefect, "Description",
392 bugPtr->getBugDescription());
393
395 cJSON *eventList = cJSON_CreateArray();
396 const GenericBug::EventStack &bugEventStack = bugPtr->getEventStack();
397 if (BufferOverflowBug::classof(bugPtr))
398 {
399 // Add only when bug is context sensitive
400 for (const SVFBugEvent &event : bugEventStack)
401 {
402 if (event.getEventType() == SVFBugEvent::SourceInst)
403 {
404 continue;
405 }
406
407 cJSON *singleEvent = cJSON_CreateObject();
408 // Event type
409 cJSON *eventType = cJSON_CreateString(
410 eventType2Str[event.getEventType()].c_str());
411 cJSON_AddItemToObject(singleEvent, "EventType", eventType);
412 // Function name
413 cJSON *eventFunc = cJSON_CreateString(
414 event.getFuncName().c_str());
415 cJSON_AddItemToObject(singleEvent, "Function", eventFunc);
416 // Event loc
417 cJSON *eventLoc = cJSON_Parse(event.getEventLoc().c_str());
418 if (eventLoc == nullptr)
419 {
420 eventLoc = cJSON_CreateObject();
421 }
422 cJSON_AddItemToObject(singleEvent, "Location", eventLoc);
423 // Event description
424 cJSON *eventDescription = cJSON_CreateString(
425 event.getEventDescription().c_str());
426 cJSON_AddItemToObject(singleEvent, "Description", eventDescription);
427
428 cJSON_AddItemToArray(eventList, singleEvent);
429 }
430 }
431 cJSON_AddItemToObject(singleDefect, "Events", eventList);
432
434 char *singleDefectStr = cJSON_Print(singleDefect);
435 jsonFile << singleDefectStr;
436 if (commaCounter != 0)
437 {
438 jsonFile << ",\n";
439 }
440 commaCounter--;
441
443 cJSON_Delete(singleDefect);
444 }
445 jsonFile << "\n],\n";
446
448 jsonFile << "\"Time\": " << time << ",\n";
449 jsonFile << "\"Memory\": \"" << mem << "\",\n";
450 jsonFile << "\"Coverage\": " << coverage << "\n";
451
452 jsonFile << "}";
453 jsonFile.close();
454}
#define BRANCHFLAGMASK
cJSON_Delete(null)
A class representing a bounded 64-bit integer.
cJSON * getBugDescription() const
static bool classof(const GenericBug *bug)
ClassOf.
void printBugToTerminal() const
cJSON * getBugDescription() const
void printBugToTerminal() const
cJSON * getBugDescription() const
void printBugToTerminal() const
cJSON * getBugDescription() const
static bool classof(const GenericBug *bug)
ClassOf.
static const OrderedMap< GenericBug::BugType, std::string > BugType2Str
const std::string getLoc() const
returns bug location as json format string
const EventStack bugEventStack
std::vector< SVFBugEvent > EventStack
const std::string getFuncName() const
return bug source function name
cJSON * getBugDescription() const
void printBugToTerminal() const
void printBugToTerminal() const
cJSON * getBugDescription() const
virtual const std::string getEventLoc() const
virtual const std::string getEventDescription() const
u32_t getEventType() const
const ICFGNode * eventInst
virtual const std::string getFuncName() const
void dumpToJsonFile(const std::string &filePath) const
virtual const std::string & getName() const
Definition SVFValue.h:184
std::string bugMsg1(const std::string &msg)
Definition SVFUtil.cpp:103
std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > cast(const Y &Val)
Definition Casting.h:360
LLVM_NODISCARD bool isa(const Y &Val)
Definition Casting.h:241
std::ostream & errs()
Overwrite llvm::errs().
Definition SVFUtil.h:64
std::string bugMsg2(const std::string &msg)
Definition SVFUtil.cpp:108
for isBitcode
Definition BasicTypes.h:70
Definition cJSON.h:104