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