xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Transforms/Utils/SanitizerStats.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- SanitizerStats.h - Sanitizer statistics gathering  -------*- 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 // Declares functions and data structures for sanitizer statistics gathering.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_TRANSFORMS_UTILS_SANITIZERSTATS_H
14 #define LLVM_TRANSFORMS_UTILS_SANITIZERSTATS_H
15 
16 #include "llvm/IR/IRBuilder.h"
17 #include "llvm/Support/Compiler.h"
18 
19 namespace llvm {
20 
21 // Number of bits in data that are used for the sanitizer kind. Needs to match
22 // __sanitizer::kKindBits in compiler-rt/lib/stats/stats.h
23 enum { kSanitizerStatKindBits = 3 };
24 
25 enum SanitizerStatKind {
26   SanStat_CFI_VCall,
27   SanStat_CFI_NVCall,
28   SanStat_CFI_DerivedCast,
29   SanStat_CFI_UnrelatedCast,
30   SanStat_CFI_ICall,
31 };
32 
33 struct SanitizerStatReport {
34   LLVM_ABI SanitizerStatReport(Module *M);
35 
36   /// Generates code into B that increments a location-specific counter tagged
37   /// with the given sanitizer kind SK.
38   LLVM_ABI void create(IRBuilder<> &B, SanitizerStatKind SK);
39 
40   /// Finalize module stats array and add global constructor to register it.
41   LLVM_ABI void finish();
42 
43 private:
44   Module *M;
45   GlobalVariable *ModuleStatsGV;
46   ArrayType *StatTy;
47   StructType *EmptyModuleStatsTy;
48 
49   std::vector<Constant *> Inits;
50   ArrayType *makeModuleStatsArrayTy();
51   StructType *makeModuleStatsTy();
52 };
53 
54 }
55 
56 #endif
57