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