]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/asan/asan_scariness_score.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / asan / asan_scariness_score.h
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 struct ScarinessScoreBase {
38   void Clear() {
39     descr[0] = 0;
40     score = 0;
41   }
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   };
48   int GetScore() const { return score; }
49   const char *GetDescription() const { return descr; }
50   void Print() const {
51     if (score && flags()->print_scariness)
52       Printf("SCARINESS: %d (%s)\n", score, descr);
53   }
54   static void PrintSimple(int score, const char *descr) {
55     ScarinessScoreBase SSB;
56     SSB.Clear();
57     SSB.Scare(score, descr);
58     SSB.Print();
59   }
60
61  private:
62   int score;
63   char descr[1024];
64 };
65
66 struct ScarinessScore : ScarinessScoreBase {
67   ScarinessScore() {
68     Clear();
69   }
70 };
71
72 }  // namespace __asan
73
74 #endif  // ASAN_SCARINESS_SCORE_H