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 <string>
31#include <vector>
32
33#include "Util/cJSON.h"
34#include "Util/GeneralType.h"
36
37#define BRANCHFLAGMASK 0x00000010
38#define EVENTTYPEMASK 0x0000000f
39
40namespace SVF
41{
42
43class ICFGNode;
44
48
49
51{
52public:
54 {
55 Branch = 0x1,
56 Caller = 0x2,
57 CallSite = 0x3,
58 Loop = 0x4,
60 };
61
62protected:
65
66public:
68 virtual ~SVFBugEvent() = default;
69
70 inline u32_t getEventType() const
71 {
73 }
74 virtual const std::string getEventDescription() const;
75 virtual const std::string getFuncName() const;
76 virtual const std::string getEventLoc() const;
77};
78
80{
81public:
82 typedef std::vector<SVFBugEvent> EventStack;
83
84public:
87
88protected:
91
92public:
96 {
97 assert(bugEventStack.size() != 0 && "bugEventStack should NOT be empty!");
98 }
99 virtual ~GenericBug() = default;
100 //GenericBug(const GenericBug &) = delete;
102 inline BugType getBugType() const
103 {
104 return bugType;
105 }
106
107 const std::string getLoc() const;
109 const std::string getFuncName() const;
110
111 inline const EventStack& getEventStack() const
112 {
113 return bugEventStack;
114 }
115
116 virtual cJSON *getBugDescription() const = 0;
117 virtual void printBugToTerminal() const = 0;
118};
119
142
158
174
176{
177public:
180
181 cJSON *getBugDescription() const;
182 void printBugToTerminal() const;
183
185 static inline bool classof(const GenericBug *bug)
186 {
187 return bug->getBugType() == GenericBug::NEVERFREE;
188 }
189};
190
192{
193public:
196
197 cJSON *getBugDescription() const;
198 void printBugToTerminal() const;
199
201 static inline bool classof(const GenericBug *bug)
202 {
203 return bug->getBugType() == GenericBug::PARTIALLEAK;
204 }
205};
206
208{
209public:
212
213 cJSON *getBugDescription() const;
214 void printBugToTerminal() const;
215
217 static inline bool classof(const GenericBug *bug)
218 {
219 return bug->getBugType() == GenericBug::DOUBLEFREE;
220 }
221};
222
224{
225public:
228
229 cJSON *getBugDescription() const;
230 void printBugToTerminal() const;
231
233 static inline bool classof(const GenericBug *bug)
234 {
235 return bug->getBugType() == GenericBug::FILENEVERCLOSE;
236 }
237};
238
240{
241public:
244
245 cJSON *getBugDescription() const;
246 void printBugToTerminal() const;
247
249 static inline bool classof(const GenericBug *bug)
250 {
252 }
253};
254
256{
257public:
260
261 cJSON *getBugDescription() const;
262 void printBugToTerminal() const;
263
265 static inline bool classof(const GenericBug *bug)
266 {
268 }
269};
270
272{
273public:
276
277 cJSON *getBugDescription() const;
278 void printBugToTerminal() const;
279
281 static inline bool classof(const GenericBug *bug)
282 {
284 }
285};
286
288{
289public:
290 SVFBugReport() = default;
293
294protected:
295 BugSet bugSet; // maintain bugs
296 double time; // time (sec)
297 std::string mem; // string memory (KB)
298 double coverage; // coverage (%)
299
300public:
301 // set time, mem and coverage
302 void setStat(double time, std::string mem, double coverage)
303 {
304 this->time = time;
305 this->mem = mem;
306 this->coverage = coverage;
307 }
308
309 /*
310 * function: pass bug type (i.e., GenericBug::NEVERFREE) and eventStack as parameter,
311 * it will add the bug into bugQueue.
312 * usage: addSaberBug(GenericBug::NEVERFREE, eventStack)
313 */
315 {
317 GenericBug *newBug = nullptr;
318 switch(bugType)
319 {
321 {
322 newBug = new NeverFreeBug(eventStack);
323 bugSet.insert(newBug);
324 break;
325 }
327 {
328 newBug = new PartialLeakBug(eventStack);
329 bugSet.insert(newBug);
330 break;
331 }
333 {
334 newBug = new DoubleFreeBug(eventStack);
335 bugSet.insert(newBug);
336 break;
337 }
339 {
340 newBug = new FileNeverCloseBug(eventStack);
341 bugSet.insert(newBug);
342 break;
343 }
345 {
346 newBug = new FilePartialCloseBug(eventStack);
347 bugSet.insert(newBug);
348 break;
349 }
350 default:
351 {
352 assert(false && "saber does NOT have this bug type!");
353 break;
354 }
355 }
356
357 // when add a bug, also print it to terminal
358 newBug->printBugToTerminal();
359 }
360
361 /*
362 * function: pass bug type (i.e., GenericBug::FULLBUFOVERFLOW) and eventStack as parameter,
363 * it will add the bug into bugQueue.
364 * usage: addAbsExecBug(GenericBug::FULLBUFOVERFLOW, eventStack, 0, 10, 11, 11)
365 */
367 BoundedInt allocLowerBound, BoundedInt allocUpperBound, BoundedInt accessLowerBound, BoundedInt accessUpperBound)
368 {
370 GenericBug *newBug = nullptr;
371 switch(bugType)
372 {
374 {
375 newBug = new FullBufferOverflowBug(eventStack, allocLowerBound, allocUpperBound, accessLowerBound, accessUpperBound);
376 bugSet.insert(newBug);
377 break;
378 }
380 {
381 newBug = new PartialBufferOverflowBug(eventStack, allocLowerBound, allocUpperBound, accessLowerBound, accessUpperBound);
382 bugSet.insert(newBug);
383 break;
384 }
386 {
387 newBug = new FullNullPtrDereferenceBug(eventStack);
388 bugSet.insert(newBug);
389 break;
390 }
392 {
393 newBug = new PartialNullPtrDereferenceBug(eventStack);
394 bugSet.insert(newBug);
395 break;
396 }
397 default:
398 {
399 assert(false && "Abstract Execution does NOT have this bug type!");
400 break;
401 }
402 }
403
404 // when add a bug, also print it to terminal
405 newBug->printBugToTerminal();
406 }
407
408 /*
409 * function: pass file path, open the file and dump bug report as JSON format
410 * usage: dumpToFile("/path/to/file")
411 */
412 void dumpToJsonFile(const std::string& filePath) const;
413
414 /*
415 * function: get underlying bugset
416 * usage: getBugSet()
417 */
418 const BugSet &getBugSet() const
419 {
420 return bugSet;
421 }
422
423};
424}
425
426#endif
#define EVENTTYPEMASK
A class representing a bounded 64-bit integer.
cJSON * getBugDescription() const
static bool classof(const GenericBug *bug)
ClassOf.
BufferOverflowBug(GenericBug::BugType bugType, const EventStack &eventStack, BoundedInt allocLowerBound, BoundedInt allocUpperBound, BoundedInt accessLowerBound, BoundedInt 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.
FullBufferOverflowBug(const EventStack &eventStack, BoundedInt allocLowerBound, BoundedInt allocUpperBound, BoundedInt accessLowerBound, BoundedInt accessUpperBound)
static bool classof(const GenericBug *bug)
ClassOf.
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 OrderedMap< 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
static bool classof(const GenericBug *bug)
ClassOf.
PartialBufferOverflowBug(const EventStack &eventStack, BoundedInt allocLowerBound, BoundedInt allocUpperBound, BoundedInt accessLowerBound, BoundedInt accessUpperBound)
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
SVF::Set< const GenericBug * > BugSet
void addAbsExecBug(GenericBug::BugType bugType, const GenericBug::EventStack &eventStack, BoundedInt allocLowerBound, BoundedInt allocUpperBound, BoundedInt accessLowerBound, BoundedInt accessUpperBound)
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:70
std::map< Key, Value, Compare, Allocator > OrderedMap
Definition GeneralType.h:64
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition GeneralType.h:51
unsigned u32_t
Definition GeneralType.h:67
Definition cJSON.h:104