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