1 //===--------- Definition of the HWAddressSanitizer class -------*- 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 declares the Hardware AddressSanitizer class which is a port of the
10 // legacy HWAddressSanitizer pass to use the new PassManager infrastructure.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_HWADDRESSSANITIZER_H
14 #define LLVM_TRANSFORMS_INSTRUMENTATION_HWADDRESSSANITIZER_H
15 
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/PassManager.h"
18 #include "llvm/Pass.h"
19 
20 namespace llvm {
21 
22 struct HWAddressSanitizerOptions {
23   HWAddressSanitizerOptions()
24       : HWAddressSanitizerOptions(false, false, false){};
25   HWAddressSanitizerOptions(bool CompileKernel, bool Recover,
26                             bool DisableOptimization)
27       : CompileKernel(CompileKernel), Recover(Recover),
28         DisableOptimization(DisableOptimization){};
29   bool CompileKernel;
30   bool Recover;
31   bool DisableOptimization;
32 };
33 
34 /// This is a public interface to the hardware address sanitizer pass for
35 /// instrumenting code to check for various memory errors at runtime, similar to
36 /// AddressSanitizer but based on partial hardware assistance.
37 class HWAddressSanitizerPass : public PassInfoMixin<HWAddressSanitizerPass> {
38 public:
39   explicit HWAddressSanitizerPass(HWAddressSanitizerOptions Options)
40       : Options(Options){};
41   PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
42   static bool isRequired() { return true; }
43   void printPipeline(raw_ostream &OS,
44                      function_ref<StringRef(StringRef)> MapClassName2PassName);
45 
46 private:
47   HWAddressSanitizerOptions Options;
48 };
49 
50 FunctionPass *
51 createHWAddressSanitizerLegacyPassPass(bool CompileKernel = false,
52                                        bool Recover = false,
53                                        bool DisableOptimization = false);
54 
55 namespace HWASanAccessInfo {
56 
57 // Bit field positions for the accessinfo parameter to
58 // llvm.hwasan.check.memaccess. Shared between the pass and the backend. Bits
59 // 0-15 are also used by the runtime.
60 enum {
61   AccessSizeShift = 0, // 4 bits
62   IsWriteShift = 4,
63   RecoverShift = 5,
64   MatchAllShift = 16, // 8 bits
65   HasMatchAllShift = 24,
66   CompileKernelShift = 25,
67 };
68 
69 enum { RuntimeMask = 0xffff };
70 
71 } // namespace HWASanAccessInfo
72 
73 } // namespace llvm
74 
75 #endif
76