1 //===--------- Definition of the AddressSanitizer class ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the AddressSanitizer class which is a port of the legacy
11 // AddressSanitizer pass to use the new PassManager infrastructure.
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_ADDRESSSANITIZERPASS_H
15 #define LLVM_TRANSFORMS_INSTRUMENTATION_ADDRESSSANITIZERPASS_H
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/IR/PassManager.h"
23 /// Frontend-provided metadata for source location.
24 struct LocationMetadata
{
29 LocationMetadata() = default;
31 bool empty() const { return Filename
.empty(); }
32 void parse(MDNode
*MDN
);
35 /// Frontend-provided metadata for global variables.
36 class GlobalsMetadata
{
39 LocationMetadata SourceLoc
;
41 bool IsDynInit
= false;
42 bool IsBlacklisted
= false;
47 /// Create a default uninitialized GlobalsMetadata instance.
48 GlobalsMetadata() = default;
50 /// Create an initialized GlobalsMetadata instance.
51 GlobalsMetadata(Module
&M
);
53 /// Returns metadata entry for a given global.
54 Entry
get(GlobalVariable
*G
) const {
55 auto Pos
= Entries
.find(G
);
56 return (Pos
!= Entries
.end()) ? Pos
->second
: Entry();
59 /// Handle invalidation from the pass manager.
60 /// These results are never invalidated.
61 bool invalidate(Module
&, const PreservedAnalyses
&,
62 ModuleAnalysisManager::Invalidator
&) {
65 bool invalidate(Function
&, const PreservedAnalyses
&,
66 FunctionAnalysisManager::Invalidator
&) {
71 DenseMap
<GlobalVariable
*, Entry
> Entries
;
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.
80 class ASanGlobalsMetadataAnalysis
81 : public AnalysisInfoMixin
<ASanGlobalsMetadataAnalysis
> {
83 using Result
= GlobalsMetadata
;
85 Result
run(Module
&, ModuleAnalysisManager
&);
88 friend AnalysisInfoMixin
<ASanGlobalsMetadataAnalysis
>;
89 static AnalysisKey Key
;
92 /// Public interface to the address sanitizer pass for instrumenting code to
93 /// check for various memory errors at runtime.
95 /// The sanitizer itself is a function pass that works by inserting various
96 /// calls to the ASan runtime library functions. The runtime library essentially
97 /// replaces malloc() and free() with custom implementations that allow regions
98 /// surrounding requested memory to be checked for invalid accesses.
99 class AddressSanitizerPass
: public PassInfoMixin
<AddressSanitizerPass
> {
101 explicit AddressSanitizerPass(bool CompileKernel
= false,
102 bool Recover
= false,
103 bool UseAfterScope
= false);
104 PreservedAnalyses
run(Function
&F
, FunctionAnalysisManager
&AM
);
112 /// Public interface to the address sanitizer module pass for instrumenting code
113 /// to check for various memory errors.
115 /// This adds 'asan.module_ctor' to 'llvm.global_ctors'. This pass may also
116 /// run intependently of the function address sanitizer.
117 class ModuleAddressSanitizerPass
118 : public PassInfoMixin
<ModuleAddressSanitizerPass
> {
120 explicit ModuleAddressSanitizerPass(bool CompileKernel
= false,
121 bool Recover
= false,
122 bool UseGlobalGC
= true,
123 bool UseOdrIndicator
= false);
124 PreservedAnalyses
run(Module
&M
, ModuleAnalysisManager
&AM
);
130 bool UseOdrIndicator
;
133 // Insert AddressSanitizer (address sanity checking) instrumentation
134 FunctionPass
*createAddressSanitizerFunctionPass(bool CompileKernel
= false,
135 bool Recover
= false,
136 bool UseAfterScope
= false);
137 ModulePass
*createModuleAddressSanitizerLegacyPassPass(
138 bool CompileKernel
= false, bool Recover
= false, bool UseGlobalsGC
= true,
139 bool UseOdrIndicator
= true);