AddressSanitizer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--------- Definition of the AddressSanitizer class ---------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares the AddressSanitizer class which is a port of the legacy
  15. // AddressSanitizer pass to use the new PassManager infrastructure.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_ADDRESSSANITIZER_H
  19. #define LLVM_TRANSFORMS_INSTRUMENTATION_ADDRESSSANITIZER_H
  20. #include "llvm/IR/Function.h"
  21. #include "llvm/IR/Module.h"
  22. #include "llvm/IR/PassManager.h"
  23. #include "llvm/Pass.h"
  24. #include "llvm/Transforms/Instrumentation/AddressSanitizerOptions.h"
  25. namespace llvm {
  26. /// Frontend-provided metadata for source location.
  27. struct LocationMetadata {
  28. StringRef Filename;
  29. int LineNo = 0;
  30. int ColumnNo = 0;
  31. LocationMetadata() = default;
  32. bool empty() const { return Filename.empty(); }
  33. void parse(MDNode *MDN);
  34. };
  35. /// Frontend-provided metadata for global variables.
  36. class GlobalsMetadata {
  37. public:
  38. struct Entry {
  39. LocationMetadata SourceLoc;
  40. StringRef Name;
  41. bool IsDynInit = false;
  42. bool IsExcluded = false;
  43. Entry() = default;
  44. };
  45. /// Create a default uninitialized GlobalsMetadata instance.
  46. GlobalsMetadata() = default;
  47. /// Create an initialized GlobalsMetadata instance.
  48. GlobalsMetadata(Module &M);
  49. /// Returns metadata entry for a given global.
  50. Entry get(GlobalVariable *G) const {
  51. auto Pos = Entries.find(G);
  52. return (Pos != Entries.end()) ? Pos->second : Entry();
  53. }
  54. /// Handle invalidation from the pass manager.
  55. /// These results are never invalidated.
  56. bool invalidate(Module &, const PreservedAnalyses &,
  57. ModuleAnalysisManager::Invalidator &) {
  58. return false;
  59. }
  60. bool invalidate(Function &, const PreservedAnalyses &,
  61. FunctionAnalysisManager::Invalidator &) {
  62. return false;
  63. }
  64. private:
  65. DenseMap<GlobalVariable *, Entry> Entries;
  66. };
  67. /// The ASanGlobalsMetadataAnalysis initializes and returns a GlobalsMetadata
  68. /// object. More specifically, ASan requires looking at all globals registered
  69. /// in 'llvm.asan.globals' before running, which only depends on reading module
  70. /// level metadata. This analysis is required to run before running the
  71. /// AddressSanitizerPass since it collects that metadata.
  72. /// The legacy pass manager equivalent of this is ASanGlobalsMetadataLegacyPass.
  73. class ASanGlobalsMetadataAnalysis
  74. : public AnalysisInfoMixin<ASanGlobalsMetadataAnalysis> {
  75. public:
  76. using Result = GlobalsMetadata;
  77. Result run(Module &, ModuleAnalysisManager &);
  78. private:
  79. friend AnalysisInfoMixin<ASanGlobalsMetadataAnalysis>;
  80. static AnalysisKey Key;
  81. };
  82. struct AddressSanitizerOptions {
  83. bool CompileKernel = false;
  84. bool Recover = false;
  85. bool UseAfterScope = false;
  86. AsanDetectStackUseAfterReturnMode UseAfterReturn =
  87. AsanDetectStackUseAfterReturnMode::Runtime;
  88. };
  89. /// Public interface to the address sanitizer pass for instrumenting code to
  90. /// check for various memory errors at runtime.
  91. ///
  92. /// The sanitizer itself is a function pass that works by inserting various
  93. /// calls to the ASan runtime library functions. The runtime library essentially
  94. /// replaces malloc() and free() with custom implementations that allow regions
  95. /// surrounding requested memory to be checked for invalid accesses.
  96. class AddressSanitizerPass : public PassInfoMixin<AddressSanitizerPass> {
  97. public:
  98. AddressSanitizerPass(const AddressSanitizerOptions &Options)
  99. : Options(Options){};
  100. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  101. void printPipeline(raw_ostream &OS,
  102. function_ref<StringRef(StringRef)> MapClassName2PassName);
  103. static bool isRequired() { return true; }
  104. private:
  105. AddressSanitizerOptions Options;
  106. };
  107. /// Public interface to the address sanitizer module pass for instrumenting code
  108. /// to check for various memory errors.
  109. ///
  110. /// This adds 'asan.module_ctor' to 'llvm.global_ctors'. This pass may also
  111. /// run intependently of the function address sanitizer.
  112. class ModuleAddressSanitizerPass
  113. : public PassInfoMixin<ModuleAddressSanitizerPass> {
  114. public:
  115. ModuleAddressSanitizerPass(
  116. const AddressSanitizerOptions &Options, bool UseGlobalGC = true,
  117. bool UseOdrIndicator = false,
  118. AsanDtorKind DestructorKind = AsanDtorKind::Global);
  119. PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
  120. void printPipeline(raw_ostream &OS,
  121. function_ref<StringRef(StringRef)> MapClassName2PassName);
  122. static bool isRequired() { return true; }
  123. private:
  124. AddressSanitizerOptions Options;
  125. bool UseGlobalGC;
  126. bool UseOdrIndicator;
  127. AsanDtorKind DestructorKind;
  128. };
  129. // Insert AddressSanitizer (address basic correctness checking) instrumentation
  130. FunctionPass *createAddressSanitizerFunctionPass(
  131. bool CompileKernel = false, bool Recover = false,
  132. bool UseAfterScope = false,
  133. AsanDetectStackUseAfterReturnMode UseAfterReturn =
  134. AsanDetectStackUseAfterReturnMode::Runtime);
  135. ModulePass *createModuleAddressSanitizerLegacyPassPass(
  136. bool CompileKernel = false, bool Recover = false, bool UseGlobalsGC = true,
  137. bool UseOdrIndicator = true,
  138. AsanDtorKind DestructorKind = AsanDtorKind::Global);
  139. struct ASanAccessInfo {
  140. const int32_t Packed;
  141. const uint8_t AccessSizeIndex;
  142. const bool IsWrite;
  143. const bool CompileKernel;
  144. explicit ASanAccessInfo(int32_t Packed);
  145. ASanAccessInfo(bool IsWrite, bool CompileKernel, uint8_t AccessSizeIndex);
  146. };
  147. } // namespace llvm
  148. #endif
  149. #ifdef __GNUC__
  150. #pragma GCC diagnostic pop
  151. #endif