Static Value-Flow Analysis
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 
43 namespace SVF
44 {
45 
52 {
53 public:
54  enum EventType
55  {
56  Branch = 0x1,
57  Caller = 0x2,
58  CallSite = 0x3,
59  Loop = 0x4,
60  SourceInst = 0x5
61  };
62 
63 protected:
66 
67 public:
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 {
82 public:
83  typedef std::vector<SVFBugEvent> EventStack;
84 
85 public:
87  static const std::map<GenericBug::BugType, std::string> BugType2Str;
88 
89 protected:
92 
93 public:
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 
122 {
123 protected:
125 
126 public:
133 
134  cJSON *getBugDescription() const;
135  void printBugToTerminal() const;
136 
138  static inline bool classof(const GenericBug *bug)
139  {
141  }
142 };
143 
145 {
146 public:
152 
154  static inline bool classof(const GenericBug *bug)
155  {
156  return bug->getBugType() == GenericBug::FULLBUFOVERFLOW;
157  }
158 };
159 
161 {
162 public:
168 
170  static inline bool classof(const GenericBug *bug)
171  {
173  }
174 };
175 
176 class NeverFreeBug : public GenericBug
177 {
178 public:
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 {
194 public:
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 
208 class DoubleFreeBug : public GenericBug
209 {
210 public:
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 {
226 public:
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 {
242 public:
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 {
258 public:
261 
262  cJSON *getBugDescription() const;
263  void printBugToTerminal() const;
264 
266  static inline bool classof(const GenericBug *bug)
267  {
269  }
270 };
271 
273 {
274 public:
277 
278  cJSON *getBugDescription() const;
279  void printBugToTerminal() const;
280 
282  static inline bool classof(const GenericBug *bug)
283  {
285  }
286 };
287 
289 {
290 public:
291  SVFBugReport() = default;
292  ~SVFBugReport();
294 
295 protected:
296  BugSet bugSet; // maintain bugs
297  double time; // time (sec)
298  std::string mem; // string memory (KB)
299  double coverage; // coverage (%)
300 
301 public:
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  */
315  void addSaberBug(GenericBug::BugType bugType, const GenericBug::EventStack &eventStack)
316  {
318  GenericBug *newBug = nullptr;
319  switch(bugType)
320  {
322  {
323  newBug = new NeverFreeBug(eventStack);
324  bugSet.insert(newBug);
325  break;
326  }
328  {
329  newBug = new PartialLeakBug(eventStack);
330  bugSet.insert(newBug);
331  break;
332  }
334  {
335  newBug = new DoubleFreeBug(eventStack);
336  bugSet.insert(newBug);
337  break;
338  }
340  {
341  newBug = new FileNeverCloseBug(eventStack);
342  bugSet.insert(newBug);
343  break;
344  }
346  {
347  newBug = new FilePartialCloseBug(eventStack);
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  {
388  newBug = new FullNullPtrDereferenceBug(eventStack);
389  bugSet.insert(newBug);
390  break;
391  }
393  {
394  newBug = new PartialNullPtrDereferenceBug(eventStack);
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
Definition: SVFBugReport.h:41
const char *const string
Definition: cJSON.h:172
cJSON * getBugDescription() const
static bool classof(const GenericBug *bug)
ClassOf.
Definition: SVFBugReport.h:138
BufferOverflowBug(GenericBug::BugType bugType, const EventStack &eventStack, s64_t allocLowerBound, s64_t allocUpperBound, s64_t accessLowerBound, s64_t accessUpperBound)
Definition: SVFBugReport.h:127
void printBugToTerminal() const
static bool classof(const GenericBug *bug)
ClassOf.
Definition: SVFBugReport.h:218
cJSON * getBugDescription() const
DoubleFreeBug(const EventStack &bugEventStack)
Definition: SVFBugReport.h:211
void printBugToTerminal() const
cJSON * getBugDescription() const
static bool classof(const GenericBug *bug)
ClassOf.
Definition: SVFBugReport.h:234
FileNeverCloseBug(const EventStack &bugEventStack)
Definition: SVFBugReport.h:227
void printBugToTerminal() const
cJSON * getBugDescription() const
FilePartialCloseBug(const EventStack &bugEventStack)
Definition: SVFBugReport.h:243
void printBugToTerminal() const
static bool classof(const GenericBug *bug)
ClassOf.
Definition: SVFBugReport.h:250
static bool classof(const GenericBug *bug)
ClassOf.
Definition: SVFBugReport.h:154
FullBufferOverflowBug(const EventStack &eventStack, s64_t allocLowerBound, s64_t allocUpperBound, s64_t accessLowerBound, s64_t accessUpperBound)
Definition: SVFBugReport.h:147
static bool classof(const GenericBug *bug)
ClassOf.
Definition: SVFBugReport.h:266
FullNullPtrDereferenceBug(const EventStack &bugEventStack)
Definition: SVFBugReport.h:259
GenericBug(BugType bugType, const EventStack &bugEventStack)
note: should be initialized with a bugEventStack
Definition: SVFBugReport.h:95
static const std::map< GenericBug::BugType, std::string > BugType2Str
Definition: SVFBugReport.h:87
BugType getBugType() const
returns bug type
Definition: SVFBugReport.h:103
virtual ~GenericBug()=default
const EventStack & getEventStack() const
Definition: SVFBugReport.h:112
virtual void printBugToTerminal() const =0
const std::string getLoc() const
returns bug location as json format string
const EventStack bugEventStack
Definition: SVFBugReport.h:91
std::vector< SVFBugEvent > EventStack
Definition: SVFBugReport.h:83
virtual cJSON * getBugDescription() const =0
const std::string getFuncName() const
return bug source function name
cJSON * getBugDescription() const
NeverFreeBug(const EventStack &bugEventStack)
Definition: SVFBugReport.h:179
static bool classof(const GenericBug *bug)
ClassOf.
Definition: SVFBugReport.h:186
void printBugToTerminal() const
PartialBufferOverflowBug(const EventStack &eventStack, s64_t allocLowerBound, s64_t allocUpperBound, s64_t accessLowerBound, s64_t accessUpperBound)
Definition: SVFBugReport.h:163
static bool classof(const GenericBug *bug)
ClassOf.
Definition: SVFBugReport.h:170
void printBugToTerminal() const
cJSON * getBugDescription() const
PartialLeakBug(const EventStack &bugEventStack)
Definition: SVFBugReport.h:195
static bool classof(const GenericBug *bug)
ClassOf.
Definition: SVFBugReport.h:202
static bool classof(const GenericBug *bug)
ClassOf.
Definition: SVFBugReport.h:282
PartialNullPtrDereferenceBug(const EventStack &bugEventStack)
Definition: SVFBugReport.h:275
virtual const std::string getEventLoc() const
virtual const std::string getEventDescription() const
SVFBugEvent(u32_t typeAndInfoFlag, const ICFGNode *eventInst)
Definition: SVFBugReport.h:68
u32_t getEventType() const
Definition: SVFBugReport.h:71
const ICFGNode * eventInst
Definition: SVFBugReport.h:65
virtual const std::string getFuncName() const
virtual ~SVFBugEvent()=default
void addAbsExecBug(GenericBug::BugType bugType, const GenericBug::EventStack &eventStack, s64_t allocLowerBound, s64_t allocUpperBound, s64_t accessLowerBound, s64_t accessUpperBound)
Definition: SVFBugReport.h:367
SVF::Set< const GenericBug * > BugSet
Definition: SVFBugReport.h:293
const BugSet & getBugSet() const
Definition: SVFBugReport.h:419
void setStat(double time, std::string mem, double coverage)
Definition: SVFBugReport.h:303
void dumpToJsonFile(const std::string &filePath) const
void addSaberBug(GenericBug::BugType bugType, const GenericBug::EventStack &eventStack)
Definition: SVFBugReport.h:315
std::string mem
Definition: SVFBugReport.h:298
SVFBugReport()=default
for isBitcode
Definition: BasicTypes.h:68
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
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition: GeneralType.h:96
Definition: cJSON.h:104