LLVM  15.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/PassManager.h"
18 
19 namespace llvm {
20 class Function;
21 class FunctionPass;
22 class GlobalVariable;
23 class MDNode;
24 class Module;
25 class ModulePass;
26 class raw_ostream;
27 
28 /// Frontend-provided metadata for source location.
31  int LineNo = 0;
32  int ColumnNo = 0;
33 
34  LocationMetadata() = default;
35 
36  bool empty() const { return Filename.empty(); }
37  void parse(MDNode *MDN);
38 };
39 
40 /// Frontend-provided metadata for global variables.
42 public:
43  struct Entry {
46  bool IsDynInit = false;
47  bool IsExcluded = false;
48 
49  Entry() = default;
50  };
51 
52  /// Create a default uninitialized GlobalsMetadata instance.
53  GlobalsMetadata() = default;
54 
55  /// Create an initialized GlobalsMetadata instance.
57 
58  /// Returns metadata entry for a given global.
60  auto Pos = Entries.find(G);
61  return (Pos != Entries.end()) ? Pos->second : Entry();
62  }
63 
64  /// Handle invalidation from the pass manager.
65  /// These results are never invalidated.
68  return false;
69  }
72  return false;
73  }
74 
75 private:
77 };
78 
79 /// The ASanGlobalsMetadataAnalysis initializes and returns a GlobalsMetadata
80 /// object. More specifically, ASan requires looking at all globals registered
81 /// in 'llvm.asan.globals' before running, which only depends on reading module
82 /// level metadata. This analysis is required to run before running the
83 /// AddressSanitizerPass since it collects that metadata.
84 /// The legacy pass manager equivalent of this is ASanGlobalsMetadataLegacyPass.
86  : public AnalysisInfoMixin<ASanGlobalsMetadataAnalysis> {
87 public:
89 
91 
92 private:
94  static AnalysisKey Key;
95 };
96 
98  bool CompileKernel = false;
99  bool Recover = false;
100  bool UseAfterScope = false;
103 };
104 
105 /// Public interface to the address sanitizer module pass for instrumenting code
106 /// to check for various memory errors.
107 ///
108 /// This adds 'asan.module_ctor' to 'llvm.global_ctors'. This pass may also
109 /// run intependently of the function address sanitizer.
111  : public PassInfoMixin<ModuleAddressSanitizerPass> {
112 public:
114  const AddressSanitizerOptions &Options, bool UseGlobalGC = true,
115  bool UseOdrIndicator = false,
116  AsanDtorKind DestructorKind = AsanDtorKind::Global);
118  void printPipeline(raw_ostream &OS,
119  function_ref<StringRef(StringRef)> MapClassName2PassName);
120  static bool isRequired() { return true; }
121 
122 private:
124  bool UseGlobalGC;
125  bool UseOdrIndicator;
126  AsanDtorKind DestructorKind;
127 };
128 
130  const int32_t Packed;
131  const uint8_t AccessSizeIndex;
132  const bool IsWrite;
133  const bool CompileKernel;
134 
135  explicit ASanAccessInfo(int32_t Packed);
137 };
138 
139 } // namespace llvm
140 
141 #endif
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
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:45
llvm::GlobalsMetadata::Entry
Definition: AddressSanitizer.h:43
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:371
llvm::Function
Definition: Function.h:60
llvm::ASanGlobalsMetadataAnalysis::run
Result run(Module &, ModuleAnalysisManager &)
Definition: AddressSanitizer.cpp:1153
llvm::GlobalsMetadata::Entry::SourceLoc
LocationMetadata SourceLoc
Definition: AddressSanitizer.h:44
llvm::GlobalVariable
Definition: GlobalVariable.h:39
llvm::AsanDetectStackUseAfterReturnMode::Runtime
@ Runtime
Detect stack use after return if not disabled runtime with (ASAN_OPTIONS=detect_stack_use_after_retur...
llvm::ASanAccessInfo::AccessSizeIndex
const uint8_t AccessSizeIndex
Definition: AddressSanitizer.h:131
llvm::ModuleAddressSanitizerPass
Public interface to the address sanitizer module pass for instrumenting code to check for various mem...
Definition: AddressSanitizer.h:110
llvm::GlobalsMetadata::Entry::IsExcluded
bool IsExcluded
Definition: AddressSanitizer.h:47
llvm::LocationMetadata::empty
bool empty() const
Definition: AddressSanitizer.h:36
llvm::LocationMetadata::Filename
StringRef Filename
Definition: AddressSanitizer.h:30
llvm::GlobalsMetadata::invalidate
bool invalidate(Module &, const PreservedAnalyses &, ModuleAnalysisManager::Invalidator &)
Handle invalidation from the pass manager.
Definition: AddressSanitizer.h:66
llvm::ModuleAddressSanitizerPass::printPipeline
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition: AddressSanitizer.cpp:1158
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:54
llvm::AnalysisManager::Invalidator
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:667
llvm::LocationMetadata
Frontend-provided metadata for source location.
Definition: AddressSanitizer.h:29
llvm::GlobalsMetadata::get
Entry get(GlobalVariable *G) const
Returns metadata entry for a given global.
Definition: AddressSanitizer.h:59
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
llvm::GlobalsMetadata::Entry::IsDynInit
bool IsDynInit
Definition: AddressSanitizer.h:46
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:200
llvm::GlobalsMetadata
Frontend-provided metadata for global variables.
Definition: AddressSanitizer.h:41
llvm::StringRef::empty
constexpr LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
llvm::GlobalsMetadata::GlobalsMetadata
GlobalsMetadata()=default
Create a default uninitialized GlobalsMetadata instance.
llvm::DenseMap
Definition: DenseMap.h:716
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:69
llvm::AsanDetectStackUseAfterReturnMode
AsanDetectStackUseAfterReturnMode
Mode of ASan detect stack use after return.
Definition: AddressSanitizerOptions.h:23
llvm::ASanAccessInfo::Packed
const int32_t Packed
Definition: AddressSanitizer.h:130
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:926
llvm::AnalysisInfoMixin
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:394
llvm::GlobalsMetadata::Entry::Entry
Entry()=default
llvm::AddressSanitizerOptions::Recover
bool Recover
Definition: AddressSanitizer.h:99
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
llvm::ASanAccessInfo::ASanAccessInfo
ASanAccessInfo(int32_t Packed)
Definition: AddressSanitizer.cpp:592
llvm::ASanAccessInfo::IsWrite
const bool IsWrite
Definition: AddressSanitizer.h:132
llvm::ASanGlobalsMetadataAnalysis
The ASanGlobalsMetadataAnalysis initializes and returns a GlobalsMetadata object.
Definition: AddressSanitizer.h:85
llvm::ASanAccessInfo
Definition: AddressSanitizer.h:129
llvm::AsanDtorKind
AsanDtorKind
Types of ASan module destructors supported.
Definition: AddressSanitizerOptions.h:16
llvm::ASanAccessInfo::CompileKernel
const bool CompileKernel
Definition: AddressSanitizer.h:133
llvm::AsanDtorKind::Global
@ Global
Append to llvm.global_dtors.
PassManager.h
AddressSanitizerOptions.h
llvm::ModuleAddressSanitizerPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: AddressSanitizer.cpp:1174
llvm::AddressSanitizerOptions::UseAfterScope
bool UseAfterScope
Definition: AddressSanitizer.h:100
llvm::ModuleAddressSanitizerPass::isRequired
static bool isRequired()
Definition: AddressSanitizer.h:120
llvm::LocationMetadata::parse
void parse(MDNode *MDN)
Definition: AddressSanitizer.cpp:1109
llvm::LocationMetadata::LineNo
int LineNo
Definition: AddressSanitizer.h:31
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::AddressSanitizerOptions::UseAfterReturn
AsanDetectStackUseAfterReturnMode UseAfterReturn
Definition: AddressSanitizer.h:101
llvm::ModuleAddressSanitizerPass::ModuleAddressSanitizerPass
ModuleAddressSanitizerPass(const AddressSanitizerOptions &Options, bool UseGlobalGC=true, bool UseOdrIndicator=false, AsanDtorKind DestructorKind=AsanDtorKind::Global)
Definition: AddressSanitizer.cpp:1168
llvm::AddressSanitizerOptions::CompileKernel
bool CompileKernel
Definition: AddressSanitizer.h:98
llvm::AddressSanitizerOptions
Definition: AddressSanitizer.h:97
llvm::LocationMetadata::ColumnNo
int ColumnNo
Definition: AddressSanitizer.h:32
llvm::codeview::PublicSymFlags::Function
@ Function
llvm::LocationMetadata::LocationMetadata
LocationMetadata()=default
llvm::GlobalsMetadata::invalidate
bool invalidate(Function &, const PreservedAnalyses &, FunctionAnalysisManager::Invalidator &)
Definition: AddressSanitizer.h:70