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 
100  bool CompileKernel;
101  bool Recover;
104 };
105 
106 /// Public interface to the address sanitizer pass for instrumenting code to
107 /// check for various memory errors at runtime.
108 ///
109 /// The sanitizer itself is a function pass that works by inserting various
110 /// calls to the ASan runtime library functions. The runtime library essentially
111 /// replaces malloc() and free() with custom implementations that allow regions
112 /// surrounding requested memory to be checked for invalid accesses.
113 class AddressSanitizerPass : public PassInfoMixin<AddressSanitizerPass> {
114 public:
116  : Options(Options){};
118  void printPipeline(raw_ostream &OS,
119  function_ref<StringRef(StringRef)> MapClassName2PassName);
120  static bool isRequired() { return true; }
121 
122 private:
123  AddressSanitizerOptions Options;
124 };
125 
126 /// Public interface to the address sanitizer module pass for instrumenting code
127 /// to check for various memory errors.
128 ///
129 /// This adds 'asan.module_ctor' to 'llvm.global_ctors'. This pass may also
130 /// run intependently of the function address sanitizer.
132  : public PassInfoMixin<ModuleAddressSanitizerPass> {
133 public:
135  bool CompileKernel = false, bool Recover = false, bool UseGlobalGC = true,
136  bool UseOdrIndicator = false,
137  AsanDtorKind DestructorKind = AsanDtorKind::Global);
139  void printPipeline(raw_ostream &OS,
140  function_ref<StringRef(StringRef)> MapClassName2PassName);
141  static bool isRequired() { return true; }
142 
143 private:
144  bool CompileKernel;
145  bool Recover;
146  bool UseGlobalGC;
147  bool UseOdrIndicator;
148  AsanDtorKind DestructorKind;
149 };
150 
151 // Insert AddressSanitizer (address sanity checking) instrumentation
153  bool CompileKernel = false, bool Recover = false,
154  bool UseAfterScope = false,
155  AsanDetectStackUseAfterReturnMode UseAfterReturn =
158  bool CompileKernel = false, bool Recover = false, bool UseGlobalsGC = true,
159  bool UseOdrIndicator = true,
160  AsanDtorKind DestructorKind = AsanDtorKind::Global);
161 
163  const int32_t Packed;
164  const uint8_t AccessSizeIndex;
165  const bool IsWrite;
166  const bool CompileKernel;
167 
168  explicit ASanAccessInfo(int32_t Packed);
170 };
171 
172 } // namespace llvm
173 
174 #endif
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
---------------------— PointerInfo ------------------------------------—
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::StringRef::empty
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
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:61
llvm::ASanGlobalsMetadataAnalysis::run
Result run(Module &, ModuleAnalysisManager &)
Definition: AddressSanitizer.cpp:1252
llvm::AddressSanitizerPass::isRequired
static bool isRequired()
Definition: AddressSanitizer.h:120
llvm::GlobalsMetadata::Entry::SourceLoc
LocationMetadata SourceLoc
Definition: AddressSanitizer.h:39
llvm::GlobalVariable
Definition: GlobalVariable.h:40
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:164
llvm::ModuleAddressSanitizerPass
Public interface to the address sanitizer module pass for instrumenting code to check for various mem...
Definition: AddressSanitizer.h:131
llvm::AddressSanitizerOptions::AddressSanitizerOptions
AddressSanitizerOptions(bool CompileKernel, bool Recover, bool UseAfterScope, AsanDetectStackUseAfterReturnMode UseAfterReturn)
Definition: AddressSanitizer.h:96
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:56
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:1286
false
Definition: StackSlotColoring.cpp:142
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:666
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:168
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::AddressSanitizerOptions::AddressSanitizerOptions
AddressSanitizerOptions()
Definition: AddressSanitizer.h:93
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:1257
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:163
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:67
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:101
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::ASanAccessInfo::ASanAccessInfo
ASanAccessInfo(int32_t Packed)
Definition: AddressSanitizer.cpp:584
llvm::ASanAccessInfo::IsWrite
const bool IsWrite
Definition: AddressSanitizer.h:165
llvm::ASanGlobalsMetadataAnalysis
The ASanGlobalsMetadataAnalysis initializes and returns a GlobalsMetadata object.
Definition: AddressSanitizer.h:80
llvm::ASanAccessInfo
Definition: AddressSanitizer.h:162
llvm::AsanDtorKind
AsanDtorKind
Types of ASan module destructors supported.
Definition: AddressSanitizerOptions.h:16
llvm::ASanAccessInfo::CompileKernel
const bool CompileKernel
Definition: AddressSanitizer.h:166
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:113
AddressSanitizerOptions.h
llvm::ModuleAddressSanitizerPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: AddressSanitizer.cpp:1302
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:102
llvm::ModuleAddressSanitizerPass::isRequired
static bool isRequired()
Definition: AddressSanitizer.h:141
llvm::LocationMetadata::parse
void parse(MDNode *MDN)
Definition: AddressSanitizer.cpp:1208
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::AddressSanitizerPass::AddressSanitizerPass
AddressSanitizerPass(AddressSanitizerOptions Options)
Definition: AddressSanitizer.h:115
llvm::AddressSanitizerOptions::UseAfterReturn
AsanDetectStackUseAfterReturnMode UseAfterReturn
Definition: AddressSanitizer.h:103
llvm::AddressSanitizerOptions::CompileKernel
bool CompileKernel
Definition: AddressSanitizer.h:99
llvm::AddressSanitizerPass::printPipeline
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition: AddressSanitizer.cpp:1276
llvm::AddressSanitizerOptions
Definition: AddressSanitizer.h:92
llvm::LocationMetadata::ColumnNo
int ColumnNo
Definition: AddressSanitizer.h:27
llvm::LocationMetadata::LocationMetadata
LocationMetadata()=default
llvm::ModuleAddressSanitizerPass::ModuleAddressSanitizerPass
ModuleAddressSanitizerPass(bool CompileKernel=false, bool Recover=false, bool UseGlobalGC=true, bool UseOdrIndicator=false, AsanDtorKind DestructorKind=AsanDtorKind::Global)
Definition: AddressSanitizer.cpp:1296
llvm::GlobalsMetadata::invalidate
bool invalidate(Function &, const PreservedAnalyses &, FunctionAnalysisManager::Invalidator &)
Definition: AddressSanitizer.h:65