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   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 =
66       llvm::AsanDetectStackUseAfterReturnMode::Invalid;
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 
needsSharedRt()75   bool needsSharedRt() const { return SharedRuntime; }
76 
needsMemProfRt()77   bool needsMemProfRt() const { return NeedsMemProfRt; }
needsAsanRt()78   bool needsAsanRt() const { return Sanitizers.has(SanitizerKind::Address); }
needsHwasanRt()79   bool needsHwasanRt() const {
80     return Sanitizers.has(SanitizerKind::HWAddress);
81   }
needsHwasanAliasesRt()82   bool needsHwasanAliasesRt() const {
83     return needsHwasanRt() && HwasanUseAliases;
84   }
needsTsanRt()85   bool needsTsanRt() const { return Sanitizers.has(SanitizerKind::Thread); }
needsMsanRt()86   bool needsMsanRt() const { return Sanitizers.has(SanitizerKind::Memory); }
needsFuzzer()87   bool needsFuzzer() const { return Sanitizers.has(SanitizerKind::Fuzzer); }
needsLsanRt()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;
requiresMinimalRuntime()95   bool requiresMinimalRuntime() const { return MinimalRuntime; }
needsDfsanRt()96   bool needsDfsanRt() const { return Sanitizers.has(SanitizerKind::DataFlow); }
needsSafeStackRt()97   bool needsSafeStackRt() const { return SafeStackRuntime; }
98   bool needsCfiRt() const;
99   bool needsCfiDiagRt() const;
needsStatsRt()100   bool needsStatsRt() const { return Stats; }
needsScudoRt()101   bool needsScudoRt() const { return Sanitizers.has(SanitizerKind::Scudo); }
102 
hasMemTag()103   bool hasMemTag() const {
104     return hasMemtagHeap() || hasMemtagStack() || hasMemtagGlobals();
105   }
hasMemtagHeap()106   bool hasMemtagHeap() const {
107     return Sanitizers.has(SanitizerKind::MemtagHeap);
108   }
hasMemtagStack()109   bool hasMemtagStack() const {
110     return Sanitizers.has(SanitizerKind::MemtagStack);
111   }
hasMemtagGlobals()112   bool hasMemtagGlobals() const {
113     return Sanitizers.has(SanitizerKind::MemtagGlobals);
114   }
getMemtagMode()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;
linkRuntimes()123   bool linkRuntimes() const { return LinkRuntimes; }
linkCXXRuntimes()124   bool linkCXXRuntimes() const { return LinkCXXRuntimes; }
hasCrossDsoCfi()125   bool hasCrossDsoCfi() const { return CfiCrossDso; }
hasAnySanitizer()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
135