Static Value-Flow Analysis
Loading...
Searching...
No Matches
SVFBugReport.h
Go to the documentation of this file.
1//===- SVFBugReport.h -- 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// Created by JoelYang on 2023/4/19.
25//
26
27#ifndef SVF_BUGRECODER_H
28#define SVF_BUGRECODER_H
29
30#include <vector>
31#include "SVFIR/SVFValue.h"
32#include "SVFIR/SVFVariables.h"
33#include "SVFIR/SVFStatements.h"
34#include "Graphs/ICFGNode.h"
35#include <string>
36#include <map>
37#include "Util/cJSON.h"
38#include <set>
39
40#define BRANCHFLAGMASK 0x00000010
41#define EVENTTYPEMASK 0x0000000f
42
43namespace SVF
44{
45
52{
53public:
55 {
56 Branch = 0x1,
57 Caller = 0x2,
58 CallSite = 0x3,
59 Loop = 0x4,
60 SourceInst = 0x5
61 };
62
63protected:
66
67public:
69 virtual ~SVFBugEvent() = default;
70
71 inline u32_t getEventType() const
72 {
74 }
75 virtual const std::string getEventDescription() const;
76 virtual const std::string getFuncName() const;
77 virtual const std::string getEventLoc() const;
78};
79
81{
82public:
83 typedef std::vector<SVFBugEvent> EventStack;
84
85public:
87 static const std::map<GenericBug::BugType, std::string> BugType2Str;
88
89protected:
92
93public:
97 {
98 assert(bugEventStack.size() != 0 && "bugEventStack should NOT be empty!");
99 }
100 virtual ~GenericBug() = default;
101 //GenericBug(const GenericBug &) = delete;
103 inline BugType getBugType() const
104 {
105 return bugType;
106 }
108 const std::string getLoc() const;
110 const std::string getFuncName() const;
111
112 inline const EventStack& getEventStack() const
113 {
114 return bugEventStack;
115 }
116
117 virtual cJSON *getBugDescription() const = 0;
118 virtual void printBugToTerminal() const = 0;
119};
120
143
159
175
177{
178public:
181
182 cJSON *getBugDescription() const;
183 void printBugToTerminal() const;
184
186 static inline bool classof(const GenericBug *bug)
187 {
188 return bug->getBugType() == GenericBug::NEVERFREE;
189 }
190};
191
193{
194public:
197
198 cJSON *getBugDescription() const;
199 void printBugToTerminal() const;
200
202 static inline bool classof(const GenericBug *bug)
203 {
204 return bug->getBugType() == GenericBug::PARTIALLEAK;
205 }
206};
207
209{
210public:
213
214 cJSON *getBugDescription() const;
215 void printBugToTerminal() const;
216
218 static inline bool classof(const GenericBug *bug)
219 {
220 return bug->getBugType() == GenericBug::DOUBLEFREE;
221 }
222};
223
225{
226public:
229
230 cJSON *getBugDescription() const;
231 void printBugToTerminal() const;
232
234 static inline bool classof(const GenericBug *bug)
235 {
236 return bug->getBugType() == GenericBug::FILENEVERCLOSE;
237 }
238};
239
241{
242public:
245
246 cJSON *getBugDescription() const;
247 void printBugToTerminal() const;
248
250 static inline bool classof(const GenericBug *bug)
251 {
252 return bug->getBugType() == GenericBug::FILEPARTIALCLOSE;
253 }
254};
255
257{
258public:
261
262 cJSON *getBugDescription() const;
263 void printBugToTerminal() const;
264
266 static inline bool classof(const GenericBug *bug)
267 {
268 return bug->getBugType() == GenericBug::FULLNULLPTRDEREFERENCE;
269 }
270};
271
273{
274public:
277
278 cJSON *getBugDescription() const;
279 void printBugToTerminal() const;
280
282 static inline bool classof(const GenericBug *bug)
283 {
284 return bug->getBugType() == GenericBug::PARTIALNULLPTRDEREFERENCE;
285 }
286};
287
289{
290public:
291 SVFBugReport() = default;
294
295protected:
296 BugSet bugSet; // maintain bugs
297 double time; // time (sec)
298 std::string mem; // string memory (KB)
299 double coverage; // coverage (%)
300
301public:
302 // set time, mem and coverage
303 void setStat(double time, std::string mem, double coverage)
304 {
305 this->time = time;
306 this->mem = mem;
307 this->coverage = coverage;
308 }
309
310 /*
311 * function: pass bug type (i.e., GenericBug::NEVERFREE) and eventStack as parameter,
312 * it will add the bug into bugQueue.
313 * usage: addSaberBug(GenericBug::NEVERFREE, eventStack)
314 */
316 {
318 GenericBug *newBug = nullptr;
319 switch(bugType)
320 {
322 {
324 bugSet.insert(newBug);
325 break;
326 }
328 {
330 bugSet.insert(newBug);
331 break;
332 }
334 {
336 bugSet.insert(newBug);
337 break;
338 }
340 {
342 bugSet.insert(newBug);
343 break;
344 }
346 {
348 bugSet.insert(newBug);
349 break;
350 }
351 default:
352 {
353 assert(false && "saber does NOT have this bug type!");
354 break;
355 }
356 }
357
358 // when add a bug, also print it to terminal
359 newBug->printBugToTerminal();
360 }
361
362 /*
363 * function: pass bug type (i.e., GenericBug::FULLBUFOVERFLOW) and eventStack as parameter,
364 * it will add the bug into bugQueue.
365 * usage: addAbsExecBug(GenericBug::FULLBUFOVERFLOW, eventStack, 0, 10, 11, 11)
366 */
368 s64_t allocLowerBound, s64_t allocUpperBound, s64_t accessLowerBound, s64_t accessUpperBound)
369 {
371 GenericBug *newBug = nullptr;
372 switch(bugType)
373 {
375 {
376 newBug = new FullBufferOverflowBug(eventStack, allocLowerBound, allocUpperBound, accessLowerBound, accessUpperBound);
377 bugSet.insert(newBug);
378 break;
379 }
381 {
382 newBug = new PartialBufferOverflowBug(eventStack, allocLowerBound, allocUpperBound, accessLowerBound, accessUpperBound);
383 bugSet.insert(newBug);
384 break;
385 }
387 {
389 bugSet.insert(newBug);
390 break;
391 }
393 {
395 bugSet.insert(newBug);
396 break;
397 }
398 default:
399 {
400 assert(false && "Abstract Execution does NOT have this bug type!");
401 break;
402 }
403 }
404
405 // when add a bug, also print it to terminal
406 //newBug->printBugToTerminal();
407 }
408
409 /*
410 * function: pass file path, open the file and dump bug report as JSON format
411 * usage: dumpToFile("/path/to/file")
412 */
413 void dumpToJsonFile(const std::string& filePath) const;
414
415 /*
416 * function: get underlying bugset
417 * usage: getBugSet()
418 */
419 const BugSet &getBugSet() const
420 {
421 return bugSet;
422 }
423
424};
425}
426
427#endif
#define EVENTTYPEMASK
cJSON * getBugDescription() const
static bool classof(const GenericBug *bug)
ClassOf.
BufferOverflowBug(GenericBug::BugType bugType, const EventStack &eventStack, s64_t allocLowerBound, s64_t allocUpperBound, s64_t accessLowerBound, s64_t accessUpperBound)
void printBugToTerminal() const
static bool classof(const GenericBug *bug)
ClassOf.
cJSON * getBugDescription() const
DoubleFreeBug(const EventStack &bugEventStack)
void printBugToTerminal() const
cJSON * getBugDescription() const
static bool classof(const GenericBug *bug)
ClassOf.
FileNeverCloseBug(const EventStack &bugEventStack)
void printBugToTerminal() const
cJSON * getBugDescription() const
FilePartialCloseBug(const EventStack &bugEventStack)
static bool classof(const GenericBug *bug)
ClassOf.
static bool classof(const GenericBug *bug)
ClassOf.
FullBufferOverflowBug(const EventStack &eventStack, s64_t allocLowerBound, s64_t allocUpperBound, s64_t accessLowerBound, s64_t accessUpperBound)
static bool classof(const GenericBug *bug)
ClassOf.
FullNullPtrDereferenceBug(const EventStack &bugEventStack)
GenericBug(BugType bugType, const EventStack &bugEventStack)
note: should be initialized with a bugEventStack
static const std::map< GenericBug::BugType, std::string > BugType2Str
const EventStack & getEventStack() const
BugType getBugType() const
returns bug type
virtual ~GenericBug()=default
virtual void printBugToTerminal() const =0
const std::string getLoc() const
returns bug location as json format string
const EventStack bugEventStack
std::vector< SVFBugEvent > EventStack
virtual cJSON * getBugDescription() const =0
const std::string getFuncName() const
return bug source function name
cJSON * getBugDescription() const
NeverFreeBug(const EventStack &bugEventStack)
static bool classof(const GenericBug *bug)
ClassOf.
void printBugToTerminal() const
PartialBufferOverflowBug(const EventStack &eventStack, s64_t allocLowerBound, s64_t allocUpperBound, s64_t accessLowerBound, s64_t accessUpperBound)
static bool classof(const GenericBug *bug)
ClassOf.
void printBugToTerminal() const
cJSON * getBugDescription() const
PartialLeakBug(const EventStack &bugEventStack)
static bool classof(const GenericBug *bug)
ClassOf.
static bool classof(const GenericBug *bug)
ClassOf.
PartialNullPtrDereferenceBug(const EventStack &bugEventStack)
virtual const std::string getEventLoc() const
virtual const std::string getEventDescription() const
SVFBugEvent(u32_t typeAndInfoFlag, const ICFGNode *eventInst)
u32_t getEventType() const
const ICFGNode * eventInst
virtual const std::string getFuncName() const
virtual ~SVFBugEvent()=default
const BugSet & getBugSet() const
void addAbsExecBug(GenericBug::BugType bugType, const GenericBug::EventStack &eventStack, s64_t allocLowerBound, s64_t allocUpperBound, s64_t accessLowerBound, s64_t accessUpperBound)
SVF::Set< const GenericBug * > BugSet
void setStat(double time, std::string mem, double coverage)
void dumpToJsonFile(const std::string &filePath) const
void addSaberBug(GenericBug::BugType bugType, const GenericBug::EventStack &eventStack)
SVFBugReport()=default
for isBitcode
Definition BasicTypes.h:68
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
llvm::Loop Loop
LLVM Loop.
Definition BasicTypes.h:140
unsigned u32_t
Definition GeneralType.h:46
signed long long s64_t
Definition GeneralType.h:49
Definition cJSON.h:104