clang  16.0.0git
SanitizerArgs.h
Go to the documentation of this file.
1 //===--- SanitizerArgs.h - Arguments for sanitizer tools -------*- 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 #ifndef LLVM_CLANG_DRIVER_SANITIZERARGS_H
9 #define LLVM_CLANG_DRIVER_SANITIZERARGS_H
10 
11 #include "clang/Basic/Sanitizers.h"
12 #include "clang/Driver/Types.h"
13 #include "llvm/Option/Arg.h"
14 #include "llvm/Option/ArgList.h"
15 #include "llvm/Transforms/Instrumentation/AddressSanitizerOptions.h"
16 #include <string>
17 #include <vector>
18 
19 namespace clang {
20 namespace driver {
21 
22 class ToolChain;
23 
25  SanitizerSet Sanitizers;
26  SanitizerSet RecoverableSanitizers;
27  SanitizerSet TrapSanitizers;
28 
29  std::vector<std::string> UserIgnorelistFiles;
30  std::vector<std::string> SystemIgnorelistFiles;
31  std::vector<std::string> CoverageAllowlistFiles;
32  std::vector<std::string> CoverageIgnorelistFiles;
33  int CoverageFeatures = 0;
34  int BinaryMetadataFeatures = 0;
35  int MsanTrackOrigins = 0;
36  bool MsanUseAfterDtor = true;
37  bool MsanParamRetval = true;
38  bool CfiCrossDso = false;
39  bool CfiICallGeneralizePointers = false;
40  bool CfiCanonicalJumpTables = false;
41  int AsanFieldPadding = 0;
42  bool SharedRuntime = false;
43  bool AsanUseAfterScope = true;
44  bool AsanPoisonCustomArrayCookie = false;
45  bool AsanGlobalsDeadStripping = false;
46  bool AsanUseOdrIndicator = false;
47  bool AsanInvalidPointerCmp = false;
48  bool AsanInvalidPointerSub = false;
49  bool AsanOutlineInstrumentation = false;
50  llvm::AsanDtorKind AsanDtorKind = llvm::AsanDtorKind::Invalid;
51  std::string HwasanAbi;
52  bool LinkRuntimes = true;
53  bool LinkCXXRuntimes = false;
54  bool NeedPIE = false;
55  bool SafeStackRuntime = false;
56  bool Stats = false;
57  bool TsanMemoryAccess = true;
58  bool TsanFuncEntryExit = true;
59  bool TsanAtomics = true;
60  bool MinimalRuntime = false;
61  // True if cross-dso CFI support if provided by the system (i.e. Android).
62  bool ImplicitCfiRuntime = false;
63  bool NeedsMemProfRt = false;
64  bool HwasanUseAliases = false;
65  llvm::AsanDetectStackUseAfterReturnMode AsanUseAfterReturn =
67 
68  std::string MemtagMode;
69 
70 public:
71  /// Parses the sanitizer arguments from an argument list.
72  SanitizerArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
73  bool DiagnoseErrors = true);
74 
75  bool needsSharedRt() const { return SharedRuntime; }
76 
77  bool needsMemProfRt() const { return NeedsMemProfRt; }
78  bool needsAsanRt() const { return Sanitizers.has(SanitizerKind::Address); }
79  bool needsHwasanRt() const {
80  return Sanitizers.has(SanitizerKind::HWAddress);
81  }
82  bool needsHwasanAliasesRt() const {
83  return needsHwasanRt() && HwasanUseAliases;
84  }
85  bool needsTsanRt() const { return Sanitizers.has(SanitizerKind::Thread); }
86  bool needsMsanRt() const { return Sanitizers.has(SanitizerKind::Memory); }
87  bool needsFuzzer() const { return Sanitizers.has(SanitizerKind::Fuzzer); }
88  bool needsLsanRt() const {
89  return Sanitizers.has(SanitizerKind::Leak) &&
90  !Sanitizers.has(SanitizerKind::Address) &&
91  !Sanitizers.has(SanitizerKind::HWAddress);
92  }
93  bool needsFuzzerInterceptors() const;
94  bool needsUbsanRt() const;
95  bool requiresMinimalRuntime() const { return MinimalRuntime; }
96  bool needsDfsanRt() const { return Sanitizers.has(SanitizerKind::DataFlow); }
97  bool needsSafeStackRt() const { return SafeStackRuntime; }
98  bool needsCfiRt() const;
99  bool needsCfiDiagRt() const;
100  bool needsStatsRt() const { return Stats; }
101  bool needsScudoRt() const { return Sanitizers.has(SanitizerKind::Scudo); }
102 
103  bool hasMemTag() const {
104  return hasMemtagHeap() || hasMemtagStack() || hasMemtagGlobals();
105  }
106  bool hasMemtagHeap() const {
107  return Sanitizers.has(SanitizerKind::MemtagHeap);
108  }
109  bool hasMemtagStack() const {
110  return Sanitizers.has(SanitizerKind::MemtagStack);
111  }
112  bool hasMemtagGlobals() const {
113  return Sanitizers.has(SanitizerKind::MemtagGlobals);
114  }
115  const std::string &getMemtagMode() const {
116  assert(!MemtagMode.empty());
117  return MemtagMode;
118  }
119 
120  bool requiresPIE() const;
121  bool needsUnwindTables() const;
122  bool needsLTO() const;
123  bool linkRuntimes() const { return LinkRuntimes; }
124  bool linkCXXRuntimes() const { return LinkCXXRuntimes; }
125  bool hasCrossDsoCfi() const { return CfiCrossDso; }
126  bool hasAnySanitizer() const { return !Sanitizers.empty(); }
127  void addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
128  llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const;
129 };
130 
131 } // namespace driver
132 } // namespace clang
133 
134 #endif
clang::driver::SanitizerArgs::getMemtagMode
const std::string & getMemtagMode() const
Definition: SanitizerArgs.h:115
clang::driver::SanitizerArgs::needsUnwindTables
bool needsUnwindTables() const
Definition: SanitizerArgs.cpp:297
clang::driver::SanitizerArgs::hasCrossDsoCfi
bool hasCrossDsoCfi() const
Definition: SanitizerArgs.h:125
clang::driver::SanitizerArgs::SanitizerArgs
SanitizerArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, bool DiagnoseErrors=true)
Parses the sanitizer arguments from an argument list.
Definition: SanitizerArgs.cpp:305
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::driver::SanitizerArgs::hasMemtagStack
bool hasMemtagStack() const
Definition: SanitizerArgs.h:109
clang::driver::SanitizerArgs::needsSafeStackRt
bool needsSafeStackRt() const
Definition: SanitizerArgs.h:97
clang::SanitizerSet::has
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.
Definition: Sanitizers.h:155
clang::driver::SanitizerArgs::needsTsanRt
bool needsTsanRt() const
Definition: SanitizerArgs.h:85
clang::driver::SanitizerArgs::needsHwasanRt
bool needsHwasanRt() const
Definition: SanitizerArgs.h:79
clang::driver::SanitizerArgs::requiresMinimalRuntime
bool requiresMinimalRuntime() const
Definition: SanitizerArgs.h:95
clang::driver::SanitizerArgs::requiresPIE
bool requiresPIE() const
Definition: SanitizerArgs.cpp:293
clang::driver::SanitizerArgs::linkRuntimes
bool linkRuntimes() const
Definition: SanitizerArgs.h:123
clang::SanitizerSet
Definition: Sanitizers.h:153
clang::driver::SanitizerArgs::addArgs
void addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const
Definition: SanitizerArgs.cpp:1083
clang::driver::SanitizerArgs::needsFuzzer
bool needsFuzzer() const
Definition: SanitizerArgs.h:87
clang::driver::SanitizerArgs::hasAnySanitizer
bool hasAnySanitizer() const
Definition: SanitizerArgs.h:126
clang::driver::SanitizerArgs::hasMemtagGlobals
bool hasMemtagGlobals() const
Definition: SanitizerArgs.h:112
clang::driver::SanitizerArgs::hasMemTag
bool hasMemTag() const
Definition: SanitizerArgs.h:103
clang::driver::SanitizerArgs::needsAsanRt
bool needsAsanRt() const
Definition: SanitizerArgs.h:78
clang::driver::SanitizerArgs::hasMemtagHeap
bool hasMemtagHeap() const
Definition: SanitizerArgs.h:106
clang::driver::SanitizerArgs::needsScudoRt
bool needsScudoRt() const
Definition: SanitizerArgs.h:101
clang::driver::SanitizerArgs::needsFuzzerInterceptors
bool needsFuzzerInterceptors() const
Definition: SanitizerArgs.cpp:268
clang::driver::SanitizerArgs
Definition: SanitizerArgs.h:24
clang::driver::SanitizerArgs::needsLTO
bool needsLTO() const
Definition: SanitizerArgs.cpp:301
clang::driver::SanitizerArgs::needsLsanRt
bool needsLsanRt() const
Definition: SanitizerArgs.h:88
clang::driver::SanitizerArgs::needsHwasanAliasesRt
bool needsHwasanAliasesRt() const
Definition: SanitizerArgs.h:82
clang::driver::ToolChain
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:91
clang::driver::SanitizerArgs::needsCfiRt
bool needsCfiRt() const
Definition: SanitizerArgs.cpp:283
clang::driver::SanitizerArgs::needsMemProfRt
bool needsMemProfRt() const
Definition: SanitizerArgs.h:77
clang::driver::SanitizerArgs::needsCfiDiagRt
bool needsCfiDiagRt() const
Definition: SanitizerArgs.cpp:288
clang::driver::types::ID
ID
Definition: Types.h:23
clang::SanitizerSet::empty
bool empty() const
Returns true if no sanitizers are enabled.
Definition: Sanitizers.h:173
Sanitizers.h
clang
Definition: CalledOnceCheck.h:17
clang::driver::SanitizerArgs::linkCXXRuntimes
bool linkCXXRuntimes() const
Definition: SanitizerArgs.h:124
clang::driver::SanitizerArgs::needsUbsanRt
bool needsUbsanRt() const
Definition: SanitizerArgs.cpp:272
Types.h
clang::driver::SanitizerArgs::needsDfsanRt
bool needsDfsanRt() const
Definition: SanitizerArgs.h:96
clang::driver::SanitizerArgs::needsSharedRt
bool needsSharedRt() const
Definition: SanitizerArgs.h:75
clang::driver::SanitizerArgs::needsStatsRt
bool needsStatsRt() const
Definition: SanitizerArgs.h:100
clang::RISCV::Invalid
@ Invalid
Definition: RISCVVIntrinsicUtils.h:171
clang::driver::SanitizerArgs::needsMsanRt
bool needsMsanRt() const
Definition: SanitizerArgs.h:86