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