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 
78   bool needsSharedRt() const { return SharedRuntime; }
79   bool needsStableAbi() const { return StableABI; }
80 
81   bool needsMemProfRt() const { return NeedsMemProfRt; }
82   bool needsAsanRt() const { return Sanitizers.has(SanitizerKind::Address); }
83   bool needsHwasanRt() const {
84     return Sanitizers.has(SanitizerKind::HWAddress);
85   }
86   bool needsHwasanAliasesRt() const {
87     return needsHwasanRt() && HwasanUseAliases;
88   }
89   bool needsTsanRt() const { return Sanitizers.has(SanitizerKind::Thread); }
90   bool needsMsanRt() const { return Sanitizers.has(SanitizerKind::Memory); }
91   bool needsFuzzer() const { return Sanitizers.has(SanitizerKind::Fuzzer); }
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;
99   bool requiresMinimalRuntime() const { return MinimalRuntime; }
100   bool needsDfsanRt() const { return Sanitizers.has(SanitizerKind::DataFlow); }
101   bool needsSafeStackRt() const { return SafeStackRuntime; }
102   bool needsCfiRt() const;
103   bool needsCfiDiagRt() const;
104   bool needsStatsRt() const { return Stats; }
105   bool needsScudoRt() const { return Sanitizers.has(SanitizerKind::Scudo); }
106 
107   bool hasMemTag() const {
108     return hasMemtagHeap() || hasMemtagStack() || hasMemtagGlobals();
109   }
110   bool hasMemtagHeap() const {
111     return Sanitizers.has(SanitizerKind::MemtagHeap);
112   }
113   bool hasMemtagStack() const {
114     return Sanitizers.has(SanitizerKind::MemtagStack);
115   }
116   bool hasMemtagGlobals() const {
117     return Sanitizers.has(SanitizerKind::MemtagGlobals);
118   }
119   const std::string &getMemtagMode() const {
120     assert(!MemtagMode.empty());
121     return MemtagMode;
122   }
123 
124   bool hasShadowCallStack() const {
125     return Sanitizers.has(SanitizerKind::ShadowCallStack);
126   }
127 
128   bool requiresPIE() const;
129   bool needsUnwindTables() const;
130   bool needsLTO() const;
131   bool linkRuntimes() const { return LinkRuntimes; }
132   bool linkCXXRuntimes() const { return LinkCXXRuntimes; }
133   bool hasCrossDsoCfi() const { return CfiCrossDso; }
134   bool hasAnySanitizer() const { return !Sanitizers.empty(); }
135   void addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
136                llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const;
137 };
138 
139 }  // namespace driver
140 }  // namespace clang
141 
142 #endif
143