1 //===- Transforms/Instrumentation/MemorySanitizer.h - MSan Pass -----------===//
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 defines the memoy sanitizer pass.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_MEMORYSANITIZER_H
14 #define LLVM_TRANSFORMS_INSTRUMENTATION_MEMORYSANITIZER_H
15 
16 #include "llvm/ADT/STLFunctionalExtras.h"
17 #include "llvm/IR/PassManager.h"
18 
19 namespace llvm {
20 class Function;
21 class Module;
22 class StringRef;
23 class raw_ostream;
24 
25 struct MemorySanitizerOptions {
26   MemorySanitizerOptions() : MemorySanitizerOptions(0, false, false, false){};
27   MemorySanitizerOptions(int TrackOrigins, bool Recover, bool Kernel)
28       : MemorySanitizerOptions(TrackOrigins, Recover, Kernel, false) {}
29   MemorySanitizerOptions(int TrackOrigins, bool Recover, bool Kernel,
30                          bool EagerChecks);
31   bool Kernel;
32   int TrackOrigins;
33   bool Recover;
34   bool EagerChecks;
35 };
36 
37 /// A function pass for msan instrumentation.
38 ///
39 /// Instruments functions to detect unitialized reads. This function pass
40 /// inserts calls to runtime library functions. If the functions aren't declared
41 /// yet, the pass inserts the declarations. Otherwise the existing globals are
42 /// used.
43 struct MemorySanitizerPass : public PassInfoMixin<MemorySanitizerPass> {
44   MemorySanitizerPass(MemorySanitizerOptions Options) : Options(Options) {}
45 
46   PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
47   void printPipeline(raw_ostream &OS,
48                      function_ref<StringRef(StringRef)> MapClassName2PassName);
49   static bool isRequired() { return true; }
50 
51 private:
52   MemorySanitizerOptions Options;
53 };
54 
55 /// A module pass for msan instrumentation.
56 ///
57 /// Instruments functions to detect unitialized reads. This function pass
58 /// inserts calls to runtime library functions. If the functions aren't declared
59 /// yet, the pass inserts the declarations. Otherwise the existing globals are
60 /// used.
61 struct ModuleMemorySanitizerPass : public PassInfoMixin<ModuleMemorySanitizerPass> {
62   ModuleMemorySanitizerPass(MemorySanitizerOptions Options) : Options(Options) {}
63 
64   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
65   static bool isRequired() { return true; }
66 
67 private:
68   MemorySanitizerOptions Options;
69 };
70 }
71 
72 #endif /* LLVM_TRANSFORMS_INSTRUMENTATION_MEMORYSANITIZER_H */
73