asan_scariness_score.h revision 360784
1240116Smarcel//===-- asan_scariness_score.h ----------------------------------*- C++ -*-===//
2240116Smarcel//
3240116Smarcel// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4240116Smarcel// See https://llvm.org/LICENSE.txt for license information.
5240116Smarcel// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6240116Smarcel//
7240116Smarcel//===----------------------------------------------------------------------===//
8240116Smarcel//
9240116Smarcel// This file is a part of AddressSanitizer, an address sanity checker.
10240116Smarcel//
11240116Smarcel// Compute the level of scariness of the error message.
12240116Smarcel// Don't expect any deep science here, just a set of heuristics that suggest
13240116Smarcel// that e.g. 1-byte-read-global-buffer-overflow is less scary than
14240116Smarcel// 8-byte-write-stack-use-after-return.
15240116Smarcel//
16240116Smarcel// Every error report has one or more features, such as memory access size,
17240116Smarcel// type (read or write), type of accessed memory (e.g. free-d heap, or a global
18240116Smarcel// redzone), etc. Every such feature has an int score and a string description.
19240116Smarcel// The overall score is the sum of all feature scores and the description
20240116Smarcel// is a concatenation of feature descriptions.
21240116Smarcel// Examples:
22240116Smarcel//  17 (4-byte-read-heap-buffer-overflow)
23240116Smarcel//  65 (multi-byte-write-stack-use-after-return)
24240116Smarcel//  10 (null-deref)
25240116Smarcel//
26240116Smarcel//===----------------------------------------------------------------------===//
27240116Smarcel
28240116Smarcel#ifndef ASAN_SCARINESS_SCORE_H
29240116Smarcel#define ASAN_SCARINESS_SCORE_H
30240116Smarcel
31240116Smarcel#include "asan_flags.h"
32240116Smarcel#include "sanitizer_common/sanitizer_common.h"
33240116Smarcel#include "sanitizer_common/sanitizer_libc.h"
34240116Smarcel
35240116Smarcelnamespace __asan {
36240116Smarcelstruct ScarinessScoreBase {
37240116Smarcel  void Clear() {
38240116Smarcel    descr[0] = 0;
39240116Smarcel    score = 0;
40240116Smarcel  }
41240116Smarcel  void Scare(int add_to_score, const char *reason) {
42240116Smarcel    if (descr[0])
43240116Smarcel      internal_strlcat(descr, "-", sizeof(descr));
44240116Smarcel    internal_strlcat(descr, reason, sizeof(descr));
45240116Smarcel    score += add_to_score;
46240116Smarcel  }
47240116Smarcel  int GetScore() const { return score; }
48240116Smarcel  const char *GetDescription() const { return descr; }
49240116Smarcel  void Print() const {
50240116Smarcel    if (score && flags()->print_scariness)
51240116Smarcel      Printf("SCARINESS: %d (%s)\n", score, descr);
52240116Smarcel  }
53240116Smarcel  static void PrintSimple(int score, const char *descr) {
54240116Smarcel    ScarinessScoreBase SSB;
55240116Smarcel    SSB.Clear();
56240116Smarcel    SSB.Scare(score, descr);
57240116Smarcel    SSB.Print();
58240116Smarcel  }
59240116Smarcel
60240116Smarcel private:
61240116Smarcel  int score;
62240116Smarcel  char descr[1024];
63240116Smarcel};
64240116Smarcel
65240116Smarcelstruct ScarinessScore : ScarinessScoreBase {
66240116Smarcel  ScarinessScore() {
67240116Smarcel    Clear();
68240116Smarcel  }
69240116Smarcel};
70240116Smarcel
71240116Smarcel}  // namespace __asan
72240116Smarcel
73240116Smarcel#endif  // ASAN_SCARINESS_SCORE_H
74240116Smarcel