• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- asan_scariness_score.h ----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of AddressSanitizer, an address sanity checker.
11 //
12 // Compute the level of scariness of the error message.
13 // Don't expect any deep science here, just a set of heuristics that suggest
14 // that e.g. 1-byte-read-global-buffer-overflow is less scary than
15 // 8-byte-write-stack-use-after-return.
16 //
17 // Every error report has one or more features, such as memory access size,
18 // type (read or write), type of accessed memory (e.g. free-d heap, or a global
19 // redzone), etc. Every such feature has an int score and a string description.
20 // The overall score is the sum of all feature scores and the description
21 // is a concatenation of feature descriptions.
22 // Examples:
23 //  17 (4-byte-read-heap-buffer-overflow)
24 //  65 (multi-byte-write-stack-use-after-return)
25 //  10 (null-deref)
26 //
27 //===----------------------------------------------------------------------===//
28 
29 #ifndef ASAN_SCARINESS_SCORE_H
30 #define ASAN_SCARINESS_SCORE_H
31 
32 #include "asan_flags.h"
33 #include "sanitizer_common/sanitizer_common.h"
34 #include "sanitizer_common/sanitizer_libc.h"
35 
36 namespace __asan {
37 class ScarinessScore {
38  public:
ScarinessScore()39   ScarinessScore() {
40     descr[0] = 0;
41   }
Scare(int add_to_score,const char * reason)42   void Scare(int add_to_score, const char *reason) {
43     if (descr[0])
44       internal_strlcat(descr, "-", sizeof(descr));
45     internal_strlcat(descr, reason, sizeof(descr));
46     score += add_to_score;
47   };
GetScore()48   int GetScore() const { return score; }
GetDescription()49   const char *GetDescription() const { return descr; }
Print()50   void Print() {
51     if (score && flags()->print_scariness)
52       Printf("SCARINESS: %d (%s)\n", score, descr);
53   }
PrintSimple(int score,const char * descr)54   static void PrintSimple(int score, const char *descr) {
55     ScarinessScore SS;
56     SS.Scare(score, descr);
57     SS.Print();
58   }
59 
60  private:
61   int score = 0;
62   char descr[1024];
63 };
64 
65 }  // namespace __asan
66 
67 #endif  // ASAN_SCARINESS_SCORE_H
68