LLVM  14.0.0git
AddressSanitizer.h
Go to the documentation of this file.
1 //===--------- Definition of the AddressSanitizer 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 AddressSanitizer class which is a port of the legacy
10 // AddressSanitizer pass to use the new PassManager infrastructure.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_ADDRESSSANITIZER_H
14 #define LLVM_TRANSFORMS_INSTRUMENTATION_ADDRESSSANITIZER_H
15 
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/PassManager.h"
20 
21 namespace llvm {
22 
23 /// Frontend-provided metadata for source location.
26  int LineNo = 0;
27  int ColumnNo = 0;
28 
29  LocationMetadata() = default;
30 
31  bool empty() const { return Filename.empty(); }
32  void parse(MDNode *MDN);
33 };
34 
35 /// Frontend-provided metadata for global variables.
37 public:
38  struct Entry {
41  bool IsDynInit = false;
42  bool IsExcluded = false;
43 
44  Entry() = default;
45  };
46 
47  /// Create a default uninitialized GlobalsMetadata instance.
48  GlobalsMetadata() = default;
49 
50  /// Create an initialized GlobalsMetadata instance.
52 
53  /// Returns metadata entry for a given global.
55  auto Pos = Entries.find(G);
56  return (Pos != Entries.end()) ? Pos->second : Entry();
57  }
58 
59  /// Handle invalidation from the pass manager.
60  /// These results are never invalidated.
63  return false;
64  }
67  return false;
68  }
69 
70 private:
72 };
73 
74 /// The ASanGlobalsMetadataAnalysis initializes and returns a GlobalsMetadata
75 /// object. More specifically, ASan requires looking at all globals registered
76 /// in 'llvm.asan.globals' before running, which only depends on reading module
77 /// level metadata. This analysis is required to run before running the
78 /// AddressSanitizerPass since it collects that metadata.
79 /// The legacy pass manager equivalent of this is ASanGlobalsMetadataLegacyPass.
81  : public AnalysisInfoMixin<ASanGlobalsMetadataAnalysis> {
82 public:
84 
86 
87 private:
89  static AnalysisKey Key;
90 };
91 
93  bool CompileKernel = false;
94  bool Recover = false;
95  bool UseAfterScope = false;
98 };
99 
100 /// Public interface to the address sanitizer pass for instrumenting code to
101 /// check for various memory errors at runtime.
102 ///
103 /// The sanitizer itself is a function pass that works by inserting various
104 /// calls to the ASan runtime library functions. The runtime library essentially
105 /// replaces malloc() and free() with custom implementations that allow regions
106 /// surrounding requested memory to be checked for invalid accesses.
107 class AddressSanitizerPass : public PassInfoMixin<AddressSanitizerPass> {
108 public:
110  : Options(Options){};
112  void printPipeline(raw_ostream &OS,
113  function_ref<StringRef(StringRef)> MapClassName2PassName);
114  static bool isRequired() { return true; }
115 
116 private:
117  AddressSanitizerOptions Options;
118 };
119 
120 /// Public interface to the address sanitizer module pass for instrumenting code
121 /// to check for various memory errors.
122 ///
123 /// This adds 'asan.module_ctor' to 'llvm.global_ctors'. This pass may also
124 /// run intependently of the function address sanitizer.
126  : public PassInfoMixin<ModuleAddressSanitizerPass> {
127 public:
129  const AddressSanitizerOptions &Options, bool UseGlobalGC = true,
130  bool UseOdrIndicator = false,
131  AsanDtorKind DestructorKind = AsanDtorKind::Global);
133  void printPipeline(raw_ostream &OS,
134  function_ref<StringRef(StringRef)> MapClassName2PassName);
135  static bool isRequired() { return true; }
136 
137 private:
139  bool UseGlobalGC;
140  bool UseOdrIndicator;
141  AsanDtorKind DestructorKind;
142 };
143 
144 // Insert AddressSanitizer (address basic correctness checking) instrumentation
146  bool CompileKernel = false, bool Recover = false,
147  bool UseAfterScope = false,
148  AsanDetectStackUseAfterReturnMode UseAfterReturn =
151  bool CompileKernel = false, bool Recover = false, bool UseGlobalsGC = true,
152  bool UseOdrIndicator = true,
153  AsanDtorKind DestructorKind = AsanDtorKind::Global);
154 
156  const int32_t Packed;
157  const uint8_t AccessSizeIndex;
158  const bool IsWrite;
159  const bool CompileKernel;
160 
161  explicit ASanAccessInfo(int32_t Packed);
163 };
164 
165 } // namespace llvm
166 
167 #endif
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::GlobalsMetadata::Entry::Name
StringRef Name
Definition: AddressSanitizer.h:40
llvm::GlobalsMetadata::Entry
Definition: AddressSanitizer.h:38
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:374
llvm::Function
Definition: Function.h:62
llvm::ASanGlobalsMetadataAnalysis::run
Result run(Module &, ModuleAnalysisManager &)
Definition: AddressSanitizer.cpp:1268
llvm::AddressSanitizerPass::isRequired
static bool isRequired()
Definition: AddressSanitizer.h:114
llvm::GlobalsMetadata::Entry::SourceLoc
LocationMetadata SourceLoc
Definition: AddressSanitizer.h:39
llvm::GlobalVariable
Definition: GlobalVariable.h:39
llvm::AsanDetectStackUseAfterReturnMode::Runtime
@ Runtime
Detect stack use after return if runtime flag is enabled (ASAN_OPTIONS=detect_stack_use_after_return=...
Module.h
llvm::ASanAccessInfo::AccessSizeIndex
const uint8_t AccessSizeIndex
Definition: AddressSanitizer.h:157
llvm::ModuleAddressSanitizerPass
Public interface to the address sanitizer module pass for instrumenting code to check for various mem...
Definition: AddressSanitizer.h:125
llvm::GlobalsMetadata::Entry::IsExcluded
bool IsExcluded
Definition: AddressSanitizer.h:42
llvm::LocationMetadata::empty
bool empty() const
Definition: AddressSanitizer.h:31
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::LocationMetadata::Filename
StringRef Filename
Definition: AddressSanitizer.h:25
llvm::GlobalsMetadata::invalidate
bool invalidate(Module &, const PreservedAnalyses &, ModuleAnalysisManager::Invalidator &)
Handle invalidation from the pass manager.
Definition: AddressSanitizer.h:61
llvm::ModuleAddressSanitizerPass::printPipeline
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition: AddressSanitizer.cpp:1303
Options
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
Definition: PassBuilderBindings.cpp:48
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::AnalysisManager::Invalidator
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:670
llvm::LocationMetadata
Frontend-provided metadata for source location.
Definition: AddressSanitizer.h:24
llvm::GlobalsMetadata::get
Entry get(GlobalVariable *G) const
Returns metadata entry for a given global.
Definition: AddressSanitizer.h:54
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:223
llvm::GlobalsMetadata::Entry::IsDynInit
bool IsDynInit
Definition: AddressSanitizer.h:41
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
llvm::GlobalsMetadata
Frontend-provided metadata for global variables.
Definition: AddressSanitizer.h:36
llvm::StringRef::empty
constexpr LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:152
llvm::GlobalsMetadata::GlobalsMetadata
GlobalsMetadata()=default
Create a default uninitialized GlobalsMetadata instance.
llvm::DenseMap
Definition: DenseMap.h:714
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:72
llvm::AddressSanitizerPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: AddressSanitizer.cpp:1273
llvm::AsanDetectStackUseAfterReturnMode
AsanDetectStackUseAfterReturnMode
Mode of ASan detect stack use after return.
Definition: AddressSanitizerOptions.h:24
llvm::ASanAccessInfo::Packed
const int32_t Packed
Definition: AddressSanitizer.h:156
llvm::createAddressSanitizerFunctionPass
FunctionPass * createAddressSanitizerFunctionPass(bool CompileKernel=false, bool Recover=false, bool UseAfterScope=false, AsanDetectStackUseAfterReturnMode UseAfterReturn=AsanDetectStackUseAfterReturnMode::Runtime)
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::MDNode
Metadata node.
Definition: Metadata.h:906
llvm::AnalysisInfoMixin
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:397
llvm::GlobalsMetadata::Entry::Entry
Entry()=default
llvm::AddressSanitizerOptions::Recover
bool Recover
Definition: AddressSanitizer.h:94
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::ASanAccessInfo::ASanAccessInfo
ASanAccessInfo(int32_t Packed)
Definition: AddressSanitizer.cpp:592
llvm::ASanAccessInfo::IsWrite
const bool IsWrite
Definition: AddressSanitizer.h:158
llvm::ASanGlobalsMetadataAnalysis
The ASanGlobalsMetadataAnalysis initializes and returns a GlobalsMetadata object.
Definition: AddressSanitizer.h:80
llvm::ASanAccessInfo
Definition: AddressSanitizer.h:155
llvm::AsanDtorKind
AsanDtorKind
Types of ASan module destructors supported.
Definition: AddressSanitizerOptions.h:16
llvm::ASanAccessInfo::CompileKernel
const bool CompileKernel
Definition: AddressSanitizer.h:159
Function.h
llvm::AsanDtorKind::Global
@ Global
Append to llvm.global_dtors.
PassManager.h
llvm::AddressSanitizerPass
Public interface to the address sanitizer pass for instrumenting code to check for various memory err...
Definition: AddressSanitizer.h:107
AddressSanitizerOptions.h
llvm::ModuleAddressSanitizerPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: AddressSanitizer.cpp:1319
llvm::createModuleAddressSanitizerLegacyPassPass
ModulePass * createModuleAddressSanitizerLegacyPassPass(bool CompileKernel=false, bool Recover=false, bool UseGlobalsGC=true, bool UseOdrIndicator=true, AsanDtorKind DestructorKind=AsanDtorKind::Global)
llvm::AddressSanitizerOptions::UseAfterScope
bool UseAfterScope
Definition: AddressSanitizer.h:95
llvm::ModuleAddressSanitizerPass::isRequired
static bool isRequired()
Definition: AddressSanitizer.h:135
llvm::LocationMetadata::parse
void parse(MDNode *MDN)
Definition: AddressSanitizer.cpp:1224
llvm::AddressSanitizerPass::AddressSanitizerPass
AddressSanitizerPass(const AddressSanitizerOptions &Options)
Definition: AddressSanitizer.h:109
llvm::LocationMetadata::LineNo
int LineNo
Definition: AddressSanitizer.h:26
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::AddressSanitizerOptions::UseAfterReturn
AsanDetectStackUseAfterReturnMode UseAfterReturn
Definition: AddressSanitizer.h:96
llvm::ModuleAddressSanitizerPass::ModuleAddressSanitizerPass
ModuleAddressSanitizerPass(const AddressSanitizerOptions &Options, bool UseGlobalGC=true, bool UseOdrIndicator=false, AsanDtorKind DestructorKind=AsanDtorKind::Global)
Definition: AddressSanitizer.cpp:1313
llvm::AddressSanitizerOptions::CompileKernel
bool CompileKernel
Definition: AddressSanitizer.h:93
llvm::AddressSanitizerPass::printPipeline
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition: AddressSanitizer.cpp:1293
llvm::AddressSanitizerOptions
Definition: AddressSanitizer.h:92
llvm::LocationMetadata::ColumnNo
int ColumnNo
Definition: AddressSanitizer.h:27
llvm::LocationMetadata::LocationMetadata
LocationMetadata()=default
llvm::GlobalsMetadata::invalidate
bool invalidate(Function &, const PreservedAnalyses &, FunctionAnalysisManager::Invalidator &)
Definition: AddressSanitizer.h:65