xref: /llvm-project/clang/include/clang/Driver/SanitizerArgs.h (revision 76fac9c01736b1254e42427f8e0910c0f1d01fba)
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 
24 class SanitizerArgs {
25   SanitizerSet Sanitizers;
26   SanitizerSet RecoverableSanitizers;
27   SanitizerSet TrapSanitizers;
28   SanitizerSet MergeHandlers;
29   SanitizerMaskCutoffs SkipHotCutoffs;
30 
31   std::vector<std::string> UserIgnorelistFiles;
32   std::vector<std::string> SystemIgnorelistFiles;
33   std::vector<std::string> CoverageAllowlistFiles;
34   std::vector<std::string> CoverageIgnorelistFiles;
35   std::vector<std::string> BinaryMetadataIgnorelistFiles;
36   int CoverageFeatures = 0;
37   int BinaryMetadataFeatures = 0;
38   int OverflowPatternExclusions = 0;
39   int MsanTrackOrigins = 0;
40   bool MsanUseAfterDtor = true;
41   bool MsanParamRetval = true;
42   bool CfiCrossDso = false;
43   bool CfiICallGeneralizePointers = false;
44   bool CfiICallNormalizeIntegers = false;
45   bool CfiCanonicalJumpTables = false;
46   int AsanFieldPadding = 0;
47   bool SharedRuntime = false;
48   bool StableABI = false;
49   bool AsanUseAfterScope = true;
50   bool AsanPoisonCustomArrayCookie = false;
51   bool AsanGlobalsDeadStripping = false;
52   bool AsanUseOdrIndicator = false;
53   bool AsanInvalidPointerCmp = false;
54   bool AsanInvalidPointerSub = false;
55   bool AsanOutlineInstrumentation = false;
56   llvm::AsanDtorKind AsanDtorKind = llvm::AsanDtorKind::Invalid;
57   std::string HwasanAbi;
58   bool LinkRuntimes = true;
59   bool LinkCXXRuntimes = false;
60   bool NeedPIE = false;
61   bool SafeStackRuntime = false;
62   bool Stats = false;
63   bool TsanMemoryAccess = true;
64   bool TsanFuncEntryExit = true;
65   bool TsanAtomics = true;
66   bool MinimalRuntime = false;
67   // True if cross-dso CFI support if provided by the system (i.e. Android).
68   bool ImplicitCfiRuntime = false;
69   bool NeedsMemProfRt = false;
70   bool HwasanUseAliases = false;
71   llvm::AsanDetectStackUseAfterReturnMode AsanUseAfterReturn =
72       llvm::AsanDetectStackUseAfterReturnMode::Invalid;
73 
74   std::string MemtagMode;
75 
76 public:
77   /// Parses the sanitizer arguments from an argument list.
78   SanitizerArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
79                 bool DiagnoseErrors = true);
80 
81   bool needsSharedRt() const { return SharedRuntime; }
82   bool needsStableAbi() const { return StableABI; }
83 
84   bool needsMemProfRt() const { return NeedsMemProfRt; }
85   bool needsAsanRt() const { return Sanitizers.has(SanitizerKind::Address); }
86   bool needsHwasanRt() const {
87     return Sanitizers.has(SanitizerKind::HWAddress);
88   }
89   bool needsHwasanAliasesRt() const {
90     return needsHwasanRt() && HwasanUseAliases;
91   }
92   bool needsTysanRt() const { return Sanitizers.has(SanitizerKind::Type); }
93   bool needsTsanRt() const { return Sanitizers.has(SanitizerKind::Thread); }
94   bool needsMsanRt() const { return Sanitizers.has(SanitizerKind::Memory); }
95   bool needsFuzzer() const { return Sanitizers.has(SanitizerKind::Fuzzer); }
96   bool needsLsanRt() const {
97     return Sanitizers.has(SanitizerKind::Leak) &&
98            !Sanitizers.has(SanitizerKind::Address) &&
99            !Sanitizers.has(SanitizerKind::HWAddress);
100   }
101   bool needsFuzzerInterceptors() const;
102   bool needsUbsanRt() const;
103   bool needsUbsanCXXRt() const;
104   bool requiresMinimalRuntime() const { return MinimalRuntime; }
105   bool needsDfsanRt() const { return Sanitizers.has(SanitizerKind::DataFlow); }
106   bool needsSafeStackRt() const { return SafeStackRuntime; }
107   bool needsCfiRt() const;
108   bool needsCfiDiagRt() const;
109   bool needsStatsRt() const { return Stats; }
110   bool needsScudoRt() const { return Sanitizers.has(SanitizerKind::Scudo); }
111   bool needsNsanRt() const {
112     return Sanitizers.has(SanitizerKind::NumericalStability);
113   }
114   bool needsRtsanRt() const { return Sanitizers.has(SanitizerKind::Realtime); }
115 
116   bool hasMemTag() const {
117     return hasMemtagHeap() || hasMemtagStack() || hasMemtagGlobals();
118   }
119   bool hasMemtagHeap() const {
120     return Sanitizers.has(SanitizerKind::MemtagHeap);
121   }
122   bool hasMemtagStack() const {
123     return Sanitizers.has(SanitizerKind::MemtagStack);
124   }
125   bool hasMemtagGlobals() const {
126     return Sanitizers.has(SanitizerKind::MemtagGlobals);
127   }
128   const std::string &getMemtagMode() const {
129     assert(!MemtagMode.empty());
130     return MemtagMode;
131   }
132 
133   bool hasShadowCallStack() const {
134     return Sanitizers.has(SanitizerKind::ShadowCallStack);
135   }
136 
137   bool requiresPIE() const;
138   bool needsUnwindTables() const;
139   bool needsLTO() const;
140   bool linkRuntimes() const { return LinkRuntimes; }
141   bool linkCXXRuntimes() const { return LinkCXXRuntimes; }
142   bool hasCrossDsoCfi() const { return CfiCrossDso; }
143   bool hasAnySanitizer() const { return !Sanitizers.empty(); }
144   void addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
145                llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const;
146 };
147 
148 }  // namespace driver
149 }  // namespace clang
150 
151 #endif
152