Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 1 | //===--- SanitizerArgs.cpp - Arguments for sanitizer tools ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Alexey Samsonov | 609213f9 | 2013-08-19 09:14:21 +0000 | [diff] [blame] | 9 | #include "clang/Driver/SanitizerArgs.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 10 | #include "ToolChains/CommonArgs.h" |
Peter Collingbourne | bf59c34 | 2015-05-11 21:39:20 +0000 | [diff] [blame] | 11 | #include "clang/Basic/Sanitizers.h" |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 12 | #include "clang/Driver/Driver.h" |
| 13 | #include "clang/Driver/DriverDiagnostic.h" |
| 14 | #include "clang/Driver/Options.h" |
| 15 | #include "clang/Driver/ToolChain.h" |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringSwitch.h" |
| 18 | #include "llvm/Support/FileSystem.h" |
| 19 | #include "llvm/Support/Path.h" |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 20 | #include "llvm/Support/SpecialCaseList.h" |
Peter Collingbourne | f11eb3e | 2018-04-04 21:55:44 +0000 | [diff] [blame] | 21 | #include "llvm/Support/TargetParser.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 22 | #include <memory> |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 23 | |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace clang::SanitizerKind; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 26 | using namespace clang::driver; |
| 27 | using namespace llvm::opt; |
| 28 | |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 29 | enum : SanitizerMask { |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 30 | NeedsUbsanRt = Undefined | Integer | Nullability | CFI, |
Alexey Samsonov | 9b87309 | 2015-06-25 23:14:32 +0000 | [diff] [blame] | 31 | NeedsUbsanCxxRt = Vptr | CFI, |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 32 | NotAllowedWithTrap = Vptr, |
Evgeniy Stepanov | 6d2b6f0 | 2017-08-29 20:03:51 +0000 | [diff] [blame] | 33 | NotAllowedWithMinimalRuntime = Vptr, |
Alex Shlyapnikov | 0a20cef | 2018-03-23 19:47:45 +0000 | [diff] [blame] | 34 | RequiresPIE = DataFlow | HWAddress | Scudo, |
Evgeniy Stepanov | 12817e5 | 2017-12-09 01:32:07 +0000 | [diff] [blame] | 35 | NeedsUnwindTables = Address | HWAddress | Thread | Memory | DataFlow, |
Andrey Konovalov | 1ba9d9c | 2018-04-13 18:05:21 +0000 | [diff] [blame^] | 36 | SupportsCoverage = Address | HWAddress | KernelAddress | KernelHWAddress | |
| 37 | Memory | Leak | Undefined | Integer | Nullability | |
| 38 | DataFlow | Fuzzer | FuzzerNoLink, |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 39 | RecoverableByDefault = Undefined | Integer | Nullability, |
Yury Gribov | 5bfeca1 | 2015-11-11 10:45:48 +0000 | [diff] [blame] | 40 | Unrecoverable = Unreachable | Return, |
Andrey Konovalov | 1ba9d9c | 2018-04-13 18:05:21 +0000 | [diff] [blame^] | 41 | AlwaysRecoverable = KernelAddress | KernelHWAddress, |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 42 | LegacyFsanitizeRecoverMask = Undefined | Integer, |
Peter Collingbourne | 1a7488a | 2015-04-02 00:23:30 +0000 | [diff] [blame] | 43 | NeedsLTO = CFI, |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 44 | TrappingSupported = (Undefined & ~Vptr) | UnsignedIntegerOverflow | |
| 45 | Nullability | LocalBounds | CFI, |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 46 | TrappingDefault = CFI, |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 47 | CFIClasses = CFIVCall | CFINVCall | CFIDerivedCast | CFIUnrelatedCast, |
Evgeniy Stepanov | 6d2b6f0 | 2017-08-29 20:03:51 +0000 | [diff] [blame] | 48 | CompatibleWithMinimalRuntime = TrappingSupported, |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 49 | }; |
Alexey Samsonov | dfa908c | 2015-05-07 22:34:06 +0000 | [diff] [blame] | 50 | |
| 51 | enum CoverageFeature { |
| 52 | CoverageFunc = 1 << 0, |
| 53 | CoverageBB = 1 << 1, |
| 54 | CoverageEdge = 1 << 2, |
| 55 | CoverageIndirCall = 1 << 3, |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 56 | CoverageTraceBB = 1 << 4, // Deprecated. |
Alexey Samsonov | dfa908c | 2015-05-07 22:34:06 +0000 | [diff] [blame] | 57 | CoverageTraceCmp = 1 << 5, |
Kostya Serebryany | 3b41971 | 2016-08-30 01:27:03 +0000 | [diff] [blame] | 58 | CoverageTraceDiv = 1 << 6, |
| 59 | CoverageTraceGep = 1 << 7, |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 60 | Coverage8bitCounters = 1 << 8, // Deprecated. |
Kostya Serebryany | 3b41971 | 2016-08-30 01:27:03 +0000 | [diff] [blame] | 61 | CoverageTracePC = 1 << 9, |
Kostya Serebryany | 60cdd61 | 2016-09-14 01:39:49 +0000 | [diff] [blame] | 62 | CoverageTracePCGuard = 1 << 10, |
Kostya Serebryany | 50fb618 | 2017-05-05 23:28:18 +0000 | [diff] [blame] | 63 | CoverageNoPrune = 1 << 11, |
Kostya Serebryany | 6145776 | 2017-07-28 00:10:10 +0000 | [diff] [blame] | 64 | CoverageInline8bitCounters = 1 << 12, |
| 65 | CoveragePCTable = 1 << 13, |
Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 66 | CoverageStackDepth = 1 << 14, |
Alexey Samsonov | dfa908c | 2015-05-07 22:34:06 +0000 | [diff] [blame] | 67 | }; |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 68 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 69 | /// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 70 | /// invalid components. Returns a SanitizerMask. |
| 71 | static SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A, |
| 72 | bool DiagnoseErrors); |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 73 | |
Alexey Samsonov | dfa908c | 2015-05-07 22:34:06 +0000 | [diff] [blame] | 74 | /// Parse -f(no-)?sanitize-coverage= flag values, diagnosing any invalid |
| 75 | /// components. Returns OR of members of \c CoverageFeature enumeration. |
| 76 | static int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A); |
| 77 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 78 | /// Produce an argument string from ArgList \p Args, which shows how it |
| 79 | /// provides some sanitizer kind from \p Mask. For example, the argument list |
| 80 | /// "-fsanitize=thread,vptr -fsanitize=address" with mask \c NeedsUbsanRt |
| 81 | /// would produce "-fsanitize=vptr". |
| 82 | static std::string lastArgumentForMask(const Driver &D, |
| 83 | const llvm::opt::ArgList &Args, |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 84 | SanitizerMask Mask); |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 85 | |
| 86 | /// Produce an argument string from argument \p A, which shows how it provides |
| 87 | /// a value in \p Mask. For instance, the argument |
| 88 | /// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce |
| 89 | /// "-fsanitize=alignment". |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 90 | static std::string describeSanitizeArg(const llvm::opt::Arg *A, |
| 91 | SanitizerMask Mask); |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 92 | |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 93 | /// Produce a string containing comma-separated names of sanitizers in \p |
| 94 | /// Sanitizers set. |
| 95 | static std::string toString(const clang::SanitizerSet &Sanitizers); |
| 96 | |
Richard Smith | 6cd4861 | 2018-02-20 23:17:41 +0000 | [diff] [blame] | 97 | static void addDefaultBlacklists(const Driver &D, SanitizerMask Kinds, |
| 98 | std::vector<std::string> &BlacklistFiles) { |
| 99 | struct Blacklist { |
| 100 | const char *File; |
| 101 | SanitizerMask Mask; |
| 102 | } Blacklists[] = {{"asan_blacklist.txt", Address}, |
| 103 | {"hwasan_blacklist.txt", HWAddress}, |
| 104 | {"msan_blacklist.txt", Memory}, |
| 105 | {"tsan_blacklist.txt", Thread}, |
| 106 | {"dfsan_abilist.txt", DataFlow}, |
| 107 | {"cfi_blacklist.txt", CFI}, |
| 108 | {"ubsan_blacklist.txt", Undefined | Integer | Nullability}}; |
Alexey Samsonov | ecf380e | 2015-03-20 18:45:06 +0000 | [diff] [blame] | 109 | |
Richard Smith | 6cd4861 | 2018-02-20 23:17:41 +0000 | [diff] [blame] | 110 | for (auto BL : Blacklists) { |
| 111 | if (!(Kinds & BL.Mask)) |
| 112 | continue; |
| 113 | |
Alexey Samsonov | ecf380e | 2015-03-20 18:45:06 +0000 | [diff] [blame] | 114 | clang::SmallString<64> Path(D.ResourceDir); |
Richard Smith | 6cd4861 | 2018-02-20 23:17:41 +0000 | [diff] [blame] | 115 | llvm::sys::path::append(Path, "share", BL.File); |
| 116 | if (llvm::sys::fs::exists(Path)) |
| 117 | BlacklistFiles.push_back(Path.str()); |
Alexey Samsonov | ecf380e | 2015-03-20 18:45:06 +0000 | [diff] [blame] | 118 | } |
Alexey Samsonov | ecf380e | 2015-03-20 18:45:06 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 121 | /// Sets group bits for every group that has at least one representative already |
| 122 | /// enabled in \p Kinds. |
| 123 | static SanitizerMask setGroupBits(SanitizerMask Kinds) { |
| 124 | #define SANITIZER(NAME, ID) |
| 125 | #define SANITIZER_GROUP(NAME, ID, ALIAS) \ |
| 126 | if (Kinds & SanitizerKind::ID) \ |
| 127 | Kinds |= SanitizerKind::ID##Group; |
| 128 | #include "clang/Basic/Sanitizers.def" |
| 129 | return Kinds; |
| 130 | } |
| 131 | |
| 132 | static SanitizerMask parseSanitizeTrapArgs(const Driver &D, |
| 133 | const llvm::opt::ArgList &Args) { |
| 134 | SanitizerMask TrapRemove = 0; // During the loop below, the accumulated set of |
| 135 | // sanitizers disabled by the current sanitizer |
| 136 | // argument or any argument after it. |
| 137 | SanitizerMask TrappingKinds = 0; |
| 138 | SanitizerMask TrappingSupportedWithGroups = setGroupBits(TrappingSupported); |
| 139 | |
| 140 | for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend(); |
| 141 | I != E; ++I) { |
| 142 | const auto *Arg = *I; |
| 143 | if (Arg->getOption().matches(options::OPT_fsanitize_trap_EQ)) { |
| 144 | Arg->claim(); |
| 145 | SanitizerMask Add = parseArgValues(D, Arg, true); |
| 146 | Add &= ~TrapRemove; |
| 147 | if (SanitizerMask InvalidValues = Add & ~TrappingSupportedWithGroups) { |
| 148 | SanitizerSet S; |
| 149 | S.Mask = InvalidValues; |
| 150 | D.Diag(diag::err_drv_unsupported_option_argument) << "-fsanitize-trap" |
| 151 | << toString(S); |
| 152 | } |
| 153 | TrappingKinds |= expandSanitizerGroups(Add) & ~TrapRemove; |
| 154 | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) { |
| 155 | Arg->claim(); |
| 156 | TrapRemove |= expandSanitizerGroups(parseArgValues(D, Arg, true)); |
| 157 | } else if (Arg->getOption().matches( |
| 158 | options::OPT_fsanitize_undefined_trap_on_error)) { |
| 159 | Arg->claim(); |
| 160 | TrappingKinds |= |
| 161 | expandSanitizerGroups(UndefinedGroup & ~TrapRemove) & ~TrapRemove; |
| 162 | } else if (Arg->getOption().matches( |
| 163 | options::OPT_fno_sanitize_undefined_trap_on_error)) { |
| 164 | Arg->claim(); |
| 165 | TrapRemove |= expandSanitizerGroups(UndefinedGroup); |
| 166 | } |
| 167 | } |
| 168 | |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 169 | // Apply default trapping behavior. |
| 170 | TrappingKinds |= TrappingDefault & ~TrapRemove; |
| 171 | |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 172 | return TrappingKinds; |
| 173 | } |
| 174 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 175 | bool SanitizerArgs::needsUbsanRt() const { |
Evgeniy Stepanov | d5d36a1 | 2017-10-16 18:02:57 +0000 | [diff] [blame] | 176 | // All of these include ubsan. |
Evgeniy Stepanov | 12817e5 | 2017-12-09 01:32:07 +0000 | [diff] [blame] | 177 | if (needsAsanRt() || needsMsanRt() || needsHwasanRt() || needsTsanRt() || |
| 178 | needsDfsanRt() || needsLsanRt() || needsCfiDiagRt() || needsScudoRt()) |
Evgeniy Stepanov | d5d36a1 | 2017-10-16 18:02:57 +0000 | [diff] [blame] | 179 | return false; |
| 180 | |
| 181 | return (Sanitizers.Mask & NeedsUbsanRt & ~TrapSanitizers.Mask) || |
| 182 | CoverageFeatures; |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | bool SanitizerArgs::needsCfiRt() const { |
Evgeniy Stepanov | d5d36a1 | 2017-10-16 18:02:57 +0000 | [diff] [blame] | 186 | return !(Sanitizers.Mask & CFI & ~TrapSanitizers.Mask) && CfiCrossDso && |
| 187 | !ImplicitCfiRuntime; |
Evgeniy Stepanov | e3fb51c | 2015-12-16 00:38:42 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | bool SanitizerArgs::needsCfiDiagRt() const { |
Evgeniy Stepanov | d5d36a1 | 2017-10-16 18:02:57 +0000 | [diff] [blame] | 191 | return (Sanitizers.Mask & CFI & ~TrapSanitizers.Mask) && CfiCrossDso && |
| 192 | !ImplicitCfiRuntime; |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Dmitry Vyukov | 43419a7 | 2014-11-21 12:19:01 +0000 | [diff] [blame] | 195 | bool SanitizerArgs::requiresPIE() const { |
Evgeniy Stepanov | 71554fe | 2015-10-21 21:28:49 +0000 | [diff] [blame] | 196 | return NeedPIE || (Sanitizers.Mask & RequiresPIE); |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | bool SanitizerArgs::needsUnwindTables() const { |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 200 | return Sanitizers.Mask & NeedsUnwindTables; |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 203 | SanitizerArgs::SanitizerArgs(const ToolChain &TC, |
| 204 | const llvm::opt::ArgList &Args) { |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 205 | SanitizerMask AllRemove = 0; // During the loop below, the accumulated set of |
| 206 | // sanitizers disabled by the current sanitizer |
| 207 | // argument or any argument after it. |
Alexey Samsonov | de0aff3 | 2015-05-21 01:07:52 +0000 | [diff] [blame] | 208 | SanitizerMask AllAddedKinds = 0; // Mask of all sanitizers ever enabled by |
| 209 | // -fsanitize= flags (directly or via group |
| 210 | // expansion), some of which may be disabled |
| 211 | // later. Used to carefully prune |
| 212 | // unused-argument diagnostics. |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 213 | SanitizerMask DiagnosedKinds = 0; // All Kinds we have diagnosed up to now. |
| 214 | // Used to deduplicate diagnostics. |
| 215 | SanitizerMask Kinds = 0; |
Alexey Samsonov | 9b87309 | 2015-06-25 23:14:32 +0000 | [diff] [blame] | 216 | const SanitizerMask Supported = setGroupBits(TC.getSupportedSanitizers()); |
Filipe Cabecinhas | ec5d0e6 | 2015-02-19 01:04:49 +0000 | [diff] [blame] | 217 | ToolChain::RTTIMode RTTIMode = TC.getRTTIMode(); |
| 218 | |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 219 | const Driver &D = TC.getDriver(); |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 220 | SanitizerMask TrappingKinds = parseSanitizeTrapArgs(D, Args); |
Alexey Samsonov | 7f2a0d2 | 2015-06-19 21:36:47 +0000 | [diff] [blame] | 221 | SanitizerMask InvalidTrappingKinds = TrappingKinds & NotAllowedWithTrap; |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 222 | |
Evgeniy Stepanov | 6d2b6f0 | 2017-08-29 20:03:51 +0000 | [diff] [blame] | 223 | MinimalRuntime = |
| 224 | Args.hasFlag(options::OPT_fsanitize_minimal_runtime, |
| 225 | options::OPT_fno_sanitize_minimal_runtime, MinimalRuntime); |
| 226 | |
Vedant Kumar | 7aacb65 | 2017-06-23 23:15:24 +0000 | [diff] [blame] | 227 | // The object size sanitizer should not be enabled at -O0. |
| 228 | Arg *OptLevel = Args.getLastArg(options::OPT_O_Group); |
| 229 | bool RemoveObjectSizeAtO0 = |
| 230 | !OptLevel || OptLevel->getOption().matches(options::OPT_O0); |
| 231 | |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 232 | for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend(); |
| 233 | I != E; ++I) { |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 234 | const auto *Arg = *I; |
| 235 | if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) { |
| 236 | Arg->claim(); |
Vedant Kumar | 7aacb65 | 2017-06-23 23:15:24 +0000 | [diff] [blame] | 237 | SanitizerMask Add = parseArgValues(D, Arg, /*AllowGroups=*/true); |
| 238 | |
| 239 | if (RemoveObjectSizeAtO0) { |
| 240 | AllRemove |= SanitizerKind::ObjectSize; |
| 241 | |
| 242 | // The user explicitly enabled the object size sanitizer. Warn that |
| 243 | // that this does nothing at -O0. |
| 244 | if (Add & SanitizerKind::ObjectSize) |
| 245 | D.Diag(diag::warn_drv_object_size_disabled_O0) |
| 246 | << Arg->getAsString(Args); |
| 247 | } |
| 248 | |
Alexey Samsonov | de0aff3 | 2015-05-21 01:07:52 +0000 | [diff] [blame] | 249 | AllAddedKinds |= expandSanitizerGroups(Add); |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 250 | |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 251 | // Avoid diagnosing any sanitizer which is disabled later. |
| 252 | Add &= ~AllRemove; |
| 253 | // At this point we have not expanded groups, so any unsupported |
| 254 | // sanitizers in Add are those which have been explicitly enabled. |
| 255 | // Diagnose them. |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 256 | if (SanitizerMask KindsToDiagnose = |
Alexey Samsonov | 7f2a0d2 | 2015-06-19 21:36:47 +0000 | [diff] [blame] | 257 | Add & InvalidTrappingKinds & ~DiagnosedKinds) { |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 258 | std::string Desc = describeSanitizeArg(*I, KindsToDiagnose); |
| 259 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 260 | << Desc << "-fsanitize-trap=undefined"; |
| 261 | DiagnosedKinds |= KindsToDiagnose; |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 262 | } |
Alexey Samsonov | 7f2a0d2 | 2015-06-19 21:36:47 +0000 | [diff] [blame] | 263 | Add &= ~InvalidTrappingKinds; |
Evgeniy Stepanov | 6d2b6f0 | 2017-08-29 20:03:51 +0000 | [diff] [blame] | 264 | |
| 265 | if (MinimalRuntime) { |
| 266 | if (SanitizerMask KindsToDiagnose = |
| 267 | Add & NotAllowedWithMinimalRuntime & ~DiagnosedKinds) { |
| 268 | std::string Desc = describeSanitizeArg(*I, KindsToDiagnose); |
| 269 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 270 | << Desc << "-fsanitize-minimal-runtime"; |
| 271 | DiagnosedKinds |= KindsToDiagnose; |
| 272 | } |
| 273 | Add &= ~NotAllowedWithMinimalRuntime; |
| 274 | } |
| 275 | |
Alexey Samsonov | 7f2a0d2 | 2015-06-19 21:36:47 +0000 | [diff] [blame] | 276 | if (SanitizerMask KindsToDiagnose = Add & ~Supported & ~DiagnosedKinds) { |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 277 | std::string Desc = describeSanitizeArg(*I, KindsToDiagnose); |
| 278 | D.Diag(diag::err_drv_unsupported_opt_for_target) |
| 279 | << Desc << TC.getTriple().str(); |
| 280 | DiagnosedKinds |= KindsToDiagnose; |
| 281 | } |
Alexey Samsonov | 7f2a0d2 | 2015-06-19 21:36:47 +0000 | [diff] [blame] | 282 | Add &= Supported; |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 283 | |
Filipe Cabecinhas | ec5d0e6 | 2015-02-19 01:04:49 +0000 | [diff] [blame] | 284 | // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups |
| 285 | // so we don't error out if -fno-rtti and -fsanitize=undefined were |
| 286 | // passed. |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 287 | if (Add & Vptr && |
Filipe Cabecinhas | ec5d0e6 | 2015-02-19 01:04:49 +0000 | [diff] [blame] | 288 | (RTTIMode == ToolChain::RM_DisabledImplicitly || |
| 289 | RTTIMode == ToolChain::RM_DisabledExplicitly)) { |
| 290 | if (RTTIMode == ToolChain::RM_DisabledImplicitly) |
| 291 | // Warn about not having rtti enabled if the vptr sanitizer is |
| 292 | // explicitly enabled |
| 293 | D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default); |
| 294 | else { |
| 295 | const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg(); |
| 296 | assert(NoRTTIArg && |
| 297 | "RTTI disabled explicitly but we have no argument!"); |
| 298 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 299 | << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args); |
| 300 | } |
| 301 | |
| 302 | // Take out the Vptr sanitizer from the enabled sanitizers |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 303 | AllRemove |= Vptr; |
Filipe Cabecinhas | ec5d0e6 | 2015-02-19 01:04:49 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Peter Collingbourne | bf59c34 | 2015-05-11 21:39:20 +0000 | [diff] [blame] | 306 | Add = expandSanitizerGroups(Add); |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 307 | // Group expansion may have enabled a sanitizer which is disabled later. |
| 308 | Add &= ~AllRemove; |
| 309 | // Silently discard any unsupported sanitizers implicitly enabled through |
| 310 | // group expansion. |
Alexey Samsonov | 7f2a0d2 | 2015-06-19 21:36:47 +0000 | [diff] [blame] | 311 | Add &= ~InvalidTrappingKinds; |
Evgeniy Stepanov | 6d2b6f0 | 2017-08-29 20:03:51 +0000 | [diff] [blame] | 312 | if (MinimalRuntime) { |
| 313 | Add &= ~NotAllowedWithMinimalRuntime; |
| 314 | } |
Alexey Samsonov | 7f2a0d2 | 2015-06-19 21:36:47 +0000 | [diff] [blame] | 315 | Add &= Supported; |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 316 | |
George Karpenkov | f2fc5b0 | 2017-04-24 18:23:24 +0000 | [diff] [blame] | 317 | if (Add & Fuzzer) |
George Karpenkov | 33613f6 | 2017-08-11 17:22:58 +0000 | [diff] [blame] | 318 | Add |= FuzzerNoLink; |
| 319 | |
Matt Morehouse | 6ec7595 | 2017-08-25 22:01:21 +0000 | [diff] [blame] | 320 | // Enable coverage if the fuzzing flag is set. |
Matt Morehouse | 034126e | 2017-08-30 22:49:31 +0000 | [diff] [blame] | 321 | if (Add & FuzzerNoLink) { |
Kostya Serebryany | a523135 | 2017-09-01 18:34:36 +0000 | [diff] [blame] | 322 | CoverageFeatures |= CoverageInline8bitCounters | CoverageIndirCall | |
Matt Morehouse | 6ec7595 | 2017-08-25 22:01:21 +0000 | [diff] [blame] | 323 | CoverageTraceCmp | CoveragePCTable; |
Matt Morehouse | 034126e | 2017-08-30 22:49:31 +0000 | [diff] [blame] | 324 | // Due to TLS differences, stack depth tracking is only enabled on Linux |
| 325 | if (TC.getTriple().isOSLinux()) |
| 326 | CoverageFeatures |= CoverageStackDepth; |
| 327 | } |
George Karpenkov | f2fc5b0 | 2017-04-24 18:23:24 +0000 | [diff] [blame] | 328 | |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 329 | Kinds |= Add; |
| 330 | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) { |
| 331 | Arg->claim(); |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 332 | SanitizerMask Remove = parseArgValues(D, Arg, true); |
Peter Collingbourne | bf59c34 | 2015-05-11 21:39:20 +0000 | [diff] [blame] | 333 | AllRemove |= expandSanitizerGroups(Remove); |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 334 | } |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 335 | } |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 336 | |
Petr Hosek | a14b460 | 2018-03-07 01:27:03 +0000 | [diff] [blame] | 337 | std::pair<SanitizerMask, SanitizerMask> IncompatibleGroups[] = { |
| 338 | std::make_pair(Address, Thread | Memory), |
| 339 | std::make_pair(Thread, Memory), |
| 340 | std::make_pair(Leak, Thread | Memory), |
| 341 | std::make_pair(KernelAddress, Address | Leak | Thread | Memory), |
| 342 | std::make_pair(HWAddress, Address | Thread | Memory | KernelAddress), |
| 343 | std::make_pair(Efficiency, Address | HWAddress | Leak | Thread | Memory | |
| 344 | KernelAddress), |
| 345 | std::make_pair(Scudo, Address | HWAddress | Leak | Thread | Memory | |
| 346 | KernelAddress | Efficiency), |
| 347 | std::make_pair(SafeStack, Address | HWAddress | Leak | Thread | Memory | |
Vlad Tsyrklevich | e55aa03 | 2018-04-03 22:33:53 +0000 | [diff] [blame] | 348 | KernelAddress | Efficiency), |
| 349 | std::make_pair(ShadowCallStack, Address | HWAddress | Leak | Thread | |
| 350 | Memory | KernelAddress | Efficiency | |
Andrey Konovalov | 1ba9d9c | 2018-04-13 18:05:21 +0000 | [diff] [blame^] | 351 | SafeStack), |
| 352 | std::make_pair(KernelHWAddress, Address | HWAddress | Leak | Thread | |
| 353 | Memory | KernelAddress | Efficiency | |
| 354 | SafeStack | ShadowCallStack)}; |
Petr Hosek | a14b460 | 2018-03-07 01:27:03 +0000 | [diff] [blame] | 355 | |
Ed Schouten | fc79d2c | 2016-03-29 21:13:53 +0000 | [diff] [blame] | 356 | // Enable toolchain specific default sanitizers if not explicitly disabled. |
Petr Hosek | a14b460 | 2018-03-07 01:27:03 +0000 | [diff] [blame] | 357 | SanitizerMask Default = TC.getDefaultSanitizers() & ~AllRemove; |
| 358 | |
Petr Hosek | 8b8d6bf | 2018-03-12 00:23:37 +0000 | [diff] [blame] | 359 | // Disable default sanitizers that are incompatible with explicitly requested |
| 360 | // ones. |
Petr Hosek | a14b460 | 2018-03-07 01:27:03 +0000 | [diff] [blame] | 361 | for (auto G : IncompatibleGroups) { |
| 362 | SanitizerMask Group = G.first; |
| 363 | if ((Default & Group) && (Kinds & G.second)) |
| 364 | Default &= ~Group; |
| 365 | } |
| 366 | |
| 367 | Kinds |= Default; |
Ed Schouten | fc79d2c | 2016-03-29 21:13:53 +0000 | [diff] [blame] | 368 | |
Filipe Cabecinhas | ec5d0e6 | 2015-02-19 01:04:49 +0000 | [diff] [blame] | 369 | // We disable the vptr sanitizer if it was enabled by group expansion but RTTI |
| 370 | // is disabled. |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 371 | if ((Kinds & Vptr) && |
Filipe Cabecinhas | ec5d0e6 | 2015-02-19 01:04:49 +0000 | [diff] [blame] | 372 | (RTTIMode == ToolChain::RM_DisabledImplicitly || |
| 373 | RTTIMode == ToolChain::RM_DisabledExplicitly)) { |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 374 | Kinds &= ~Vptr; |
Filipe Cabecinhas | ec5d0e6 | 2015-02-19 01:04:49 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Alexey Samsonov | 907880e | 2015-06-19 19:57:46 +0000 | [diff] [blame] | 377 | // Check that LTO is enabled if we need it. |
Teresa Johnson | 945bc50 | 2015-10-15 20:35:53 +0000 | [diff] [blame] | 378 | if ((Kinds & NeedsLTO) && !D.isUsingLTO()) { |
Alexey Samsonov | 907880e | 2015-06-19 19:57:46 +0000 | [diff] [blame] | 379 | D.Diag(diag::err_drv_argument_only_allowed_with) |
| 380 | << lastArgumentForMask(D, Args, Kinds & NeedsLTO) << "-flto"; |
| 381 | } |
| 382 | |
Peter Collingbourne | f11eb3e | 2018-04-04 21:55:44 +0000 | [diff] [blame] | 383 | if ((Kinds & ShadowCallStack) && |
| 384 | TC.getTriple().getArch() == llvm::Triple::aarch64 && |
| 385 | !llvm::AArch64::isX18ReservedByDefault(TC.getTriple()) && |
| 386 | !Args.hasArg(options::OPT_ffixed_x18)) { |
| 387 | D.Diag(diag::err_drv_argument_only_allowed_with) |
| 388 | << lastArgumentForMask(D, Args, Kinds & ShadowCallStack) |
| 389 | << "-ffixed-x18"; |
| 390 | } |
| 391 | |
Alexey Samsonov | 9b87309 | 2015-06-25 23:14:32 +0000 | [diff] [blame] | 392 | // Report error if there are non-trapping sanitizers that require |
| 393 | // c++abi-specific parts of UBSan runtime, and they are not provided by the |
| 394 | // toolchain. We don't have a good way to check the latter, so we just |
| 395 | // check if the toolchan supports vptr. |
| 396 | if (~Supported & Vptr) { |
Peter Collingbourne | 4bdceaa | 2015-07-08 21:08:05 +0000 | [diff] [blame] | 397 | SanitizerMask KindsToDiagnose = Kinds & ~TrappingKinds & NeedsUbsanCxxRt; |
| 398 | // The runtime library supports the Microsoft C++ ABI, but only well enough |
| 399 | // for CFI. FIXME: Remove this once we support vptr on Windows. |
| 400 | if (TC.getTriple().isOSWindows()) |
| 401 | KindsToDiagnose &= ~CFI; |
| 402 | if (KindsToDiagnose) { |
Alexey Samsonov | 9b87309 | 2015-06-25 23:14:32 +0000 | [diff] [blame] | 403 | SanitizerSet S; |
| 404 | S.Mask = KindsToDiagnose; |
| 405 | D.Diag(diag::err_drv_unsupported_opt_for_target) |
| 406 | << ("-fno-sanitize-trap=" + toString(S)) << TC.getTriple().str(); |
| 407 | Kinds &= ~KindsToDiagnose; |
| 408 | } |
| 409 | } |
| 410 | |
Alexey Samsonov | ecf380e | 2015-03-20 18:45:06 +0000 | [diff] [blame] | 411 | // Warn about incompatible groups of sanitizers. |
Alexey Samsonov | ecf380e | 2015-03-20 18:45:06 +0000 | [diff] [blame] | 412 | for (auto G : IncompatibleGroups) { |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 413 | SanitizerMask Group = G.first; |
Alexey Samsonov | ecf380e | 2015-03-20 18:45:06 +0000 | [diff] [blame] | 414 | if (Kinds & Group) { |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 415 | if (SanitizerMask Incompatible = Kinds & G.second) { |
Alexey Samsonov | ecf380e | 2015-03-20 18:45:06 +0000 | [diff] [blame] | 416 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 417 | << lastArgumentForMask(D, Args, Group) |
| 418 | << lastArgumentForMask(D, Args, Incompatible); |
| 419 | Kinds &= ~Incompatible; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | // FIXME: Currently -fsanitize=leak is silently ignored in the presence of |
| 424 | // -fsanitize=address. Perhaps it should print an error, or perhaps |
| 425 | // -f(-no)sanitize=leak should change whether leak detection is enabled by |
| 426 | // default in ASan? |
| 427 | |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 428 | // Parse -f(no-)?sanitize-recover flags. |
Andrey Konovalov | 1ba9d9c | 2018-04-13 18:05:21 +0000 | [diff] [blame^] | 429 | SanitizerMask RecoverableKinds = RecoverableByDefault | AlwaysRecoverable; |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 430 | SanitizerMask DiagnosedUnrecoverableKinds = 0; |
Andrey Konovalov | 1ba9d9c | 2018-04-13 18:05:21 +0000 | [diff] [blame^] | 431 | SanitizerMask DiagnosedAlwaysRecoverableKinds = 0; |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 432 | for (const auto *Arg : Args) { |
Alexey Samsonov | ce2e77c | 2015-03-11 23:34:25 +0000 | [diff] [blame] | 433 | const char *DeprecatedReplacement = nullptr; |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 434 | if (Arg->getOption().matches(options::OPT_fsanitize_recover)) { |
Kostya Serebryany | ceb1add | 2016-05-04 20:21:47 +0000 | [diff] [blame] | 435 | DeprecatedReplacement = |
| 436 | "-fsanitize-recover=undefined,integer' or '-fsanitize-recover=all"; |
Peter Collingbourne | bf59c34 | 2015-05-11 21:39:20 +0000 | [diff] [blame] | 437 | RecoverableKinds |= expandSanitizerGroups(LegacyFsanitizeRecoverMask); |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 438 | Arg->claim(); |
| 439 | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) { |
Kostya Serebryany | ceb1add | 2016-05-04 20:21:47 +0000 | [diff] [blame] | 440 | DeprecatedReplacement = "-fno-sanitize-recover=undefined,integer' or " |
| 441 | "'-fno-sanitize-recover=all"; |
Peter Collingbourne | bf59c34 | 2015-05-11 21:39:20 +0000 | [diff] [blame] | 442 | RecoverableKinds &= ~expandSanitizerGroups(LegacyFsanitizeRecoverMask); |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 443 | Arg->claim(); |
| 444 | } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) { |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 445 | SanitizerMask Add = parseArgValues(D, Arg, true); |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 446 | // Report error if user explicitly tries to recover from unrecoverable |
| 447 | // sanitizer. |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 448 | if (SanitizerMask KindsToDiagnose = |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 449 | Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) { |
| 450 | SanitizerSet SetToDiagnose; |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 451 | SetToDiagnose.Mask |= KindsToDiagnose; |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 452 | D.Diag(diag::err_drv_unsupported_option_argument) |
| 453 | << Arg->getOption().getName() << toString(SetToDiagnose); |
| 454 | DiagnosedUnrecoverableKinds |= KindsToDiagnose; |
| 455 | } |
Peter Collingbourne | bf59c34 | 2015-05-11 21:39:20 +0000 | [diff] [blame] | 456 | RecoverableKinds |= expandSanitizerGroups(Add); |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 457 | Arg->claim(); |
| 458 | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) { |
Andrey Konovalov | 1ba9d9c | 2018-04-13 18:05:21 +0000 | [diff] [blame^] | 459 | SanitizerMask Remove = parseArgValues(D, Arg, true); |
| 460 | // Report error if user explicitly tries to disable recovery from |
| 461 | // always recoverable sanitizer. |
| 462 | if (SanitizerMask KindsToDiagnose = |
| 463 | Remove & AlwaysRecoverable & ~DiagnosedAlwaysRecoverableKinds) { |
| 464 | SanitizerSet SetToDiagnose; |
| 465 | SetToDiagnose.Mask |= KindsToDiagnose; |
| 466 | D.Diag(diag::err_drv_unsupported_option_argument) |
| 467 | << Arg->getOption().getName() << toString(SetToDiagnose); |
| 468 | DiagnosedAlwaysRecoverableKinds |= KindsToDiagnose; |
| 469 | } |
| 470 | RecoverableKinds &= ~expandSanitizerGroups(Remove); |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 471 | Arg->claim(); |
| 472 | } |
Alexey Samsonov | ce2e77c | 2015-03-11 23:34:25 +0000 | [diff] [blame] | 473 | if (DeprecatedReplacement) { |
| 474 | D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args) |
| 475 | << DeprecatedReplacement; |
| 476 | } |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 477 | } |
| 478 | RecoverableKinds &= Kinds; |
| 479 | RecoverableKinds &= ~Unrecoverable; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 480 | |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 481 | TrappingKinds &= Kinds; |
Vedant Kumar | fae4f7c | 2017-12-21 00:10:24 +0000 | [diff] [blame] | 482 | RecoverableKinds &= ~TrappingKinds; |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 483 | |
Alexey Samsonov | a511cdd | 2015-02-04 17:40:08 +0000 | [diff] [blame] | 484 | // Setup blacklist files. |
| 485 | // Add default blacklist from resource directory. |
Richard Smith | 6cd4861 | 2018-02-20 23:17:41 +0000 | [diff] [blame] | 486 | addDefaultBlacklists(D, Kinds, BlacklistFiles); |
Alexey Samsonov | a511cdd | 2015-02-04 17:40:08 +0000 | [diff] [blame] | 487 | // Parse -f(no-)sanitize-blacklist options. |
| 488 | for (const auto *Arg : Args) { |
| 489 | if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) { |
| 490 | Arg->claim(); |
| 491 | std::string BLPath = Arg->getValue(); |
Ivan Krasin | 4c3f237 | 2015-09-02 20:02:38 +0000 | [diff] [blame] | 492 | if (llvm::sys::fs::exists(BLPath)) { |
Alexey Samsonov | a511cdd | 2015-02-04 17:40:08 +0000 | [diff] [blame] | 493 | BlacklistFiles.push_back(BLPath); |
Ivan Krasin | 4c3f237 | 2015-09-02 20:02:38 +0000 | [diff] [blame] | 494 | ExtraDeps.push_back(BLPath); |
Richard Smith | 6cd4861 | 2018-02-20 23:17:41 +0000 | [diff] [blame] | 495 | } else { |
Alexey Samsonov | a511cdd | 2015-02-04 17:40:08 +0000 | [diff] [blame] | 496 | D.Diag(clang::diag::err_drv_no_such_file) << BLPath; |
Richard Smith | 6cd4861 | 2018-02-20 23:17:41 +0000 | [diff] [blame] | 497 | } |
Alexey Samsonov | a511cdd | 2015-02-04 17:40:08 +0000 | [diff] [blame] | 498 | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) { |
| 499 | Arg->claim(); |
| 500 | BlacklistFiles.clear(); |
Ivan Krasin | 4c3f237 | 2015-09-02 20:02:38 +0000 | [diff] [blame] | 501 | ExtraDeps.clear(); |
Alexey Samsonov | a511cdd | 2015-02-04 17:40:08 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | // Validate blacklists format. |
| 505 | { |
| 506 | std::string BLError; |
| 507 | std::unique_ptr<llvm::SpecialCaseList> SCL( |
| 508 | llvm::SpecialCaseList::create(BlacklistFiles, BLError)); |
| 509 | if (!SCL.get()) |
| 510 | D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 513 | // Parse -f[no-]sanitize-memory-track-origins[=level] options. |
Alexey Samsonov | de0aff3 | 2015-05-21 01:07:52 +0000 | [diff] [blame] | 514 | if (AllAddedKinds & Memory) { |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 515 | if (Arg *A = |
| 516 | Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ, |
| 517 | options::OPT_fsanitize_memory_track_origins, |
| 518 | options::OPT_fno_sanitize_memory_track_origins)) { |
| 519 | if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) { |
Evgeniy Stepanov | 6e09bca | 2015-02-26 15:59:30 +0000 | [diff] [blame] | 520 | MsanTrackOrigins = 2; |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 521 | } else if (A->getOption().matches( |
| 522 | options::OPT_fno_sanitize_memory_track_origins)) { |
| 523 | MsanTrackOrigins = 0; |
| 524 | } else { |
| 525 | StringRef S = A->getValue(); |
| 526 | if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 || |
| 527 | MsanTrackOrigins > 2) { |
Nico Weber | f2a39a7 | 2015-04-13 20:03:03 +0000 | [diff] [blame] | 528 | D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S; |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | } |
Evgeniy Stepanov | 71554fe | 2015-10-21 21:28:49 +0000 | [diff] [blame] | 532 | MsanUseAfterDtor = |
Matt Morehouse | bb26f86 | 2017-09-14 23:14:37 +0000 | [diff] [blame] | 533 | Args.hasFlag(options::OPT_fsanitize_memory_use_after_dtor, |
| 534 | options::OPT_fno_sanitize_memory_use_after_dtor, |
Matt Morehouse | 175625c | 2017-09-14 23:53:56 +0000 | [diff] [blame] | 535 | MsanUseAfterDtor); |
Evgeniy Stepanov | 71554fe | 2015-10-21 21:28:49 +0000 | [diff] [blame] | 536 | NeedPIE |= !(TC.getTriple().isOSLinux() && |
| 537 | TC.getTriple().getArch() == llvm::Triple::x86_64); |
Matt Morehouse | 175625c | 2017-09-14 23:53:56 +0000 | [diff] [blame] | 538 | } else { |
| 539 | MsanUseAfterDtor = false; |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Evgeniy Stepanov | 8a2bedb | 2016-11-11 23:17:36 +0000 | [diff] [blame] | 542 | if (AllAddedKinds & Thread) { |
| 543 | TsanMemoryAccess = Args.hasFlag(options::OPT_fsanitize_thread_memory_access, |
| 544 | options::OPT_fno_sanitize_thread_memory_access, |
| 545 | TsanMemoryAccess); |
| 546 | TsanFuncEntryExit = Args.hasFlag(options::OPT_fsanitize_thread_func_entry_exit, |
| 547 | options::OPT_fno_sanitize_thread_func_entry_exit, |
| 548 | TsanFuncEntryExit); |
| 549 | TsanAtomics = Args.hasFlag(options::OPT_fsanitize_thread_atomics, |
| 550 | options::OPT_fno_sanitize_thread_atomics, |
| 551 | TsanAtomics); |
| 552 | } |
| 553 | |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 554 | if (AllAddedKinds & CFI) { |
| 555 | CfiCrossDso = Args.hasFlag(options::OPT_fsanitize_cfi_cross_dso, |
| 556 | options::OPT_fno_sanitize_cfi_cross_dso, false); |
| 557 | // Without PIE, external function address may resolve to a PLT record, which |
| 558 | // can not be verified by the target module. |
| 559 | NeedPIE |= CfiCrossDso; |
Vlad Tsyrklevich | 634c601 | 2017-10-31 22:39:44 +0000 | [diff] [blame] | 560 | CfiICallGeneralizePointers = |
| 561 | Args.hasArg(options::OPT_fsanitize_cfi_icall_generalize_pointers); |
| 562 | |
| 563 | if (CfiCrossDso && CfiICallGeneralizePointers) |
| 564 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 565 | << "-fsanitize-cfi-cross-dso" |
| 566 | << "-fsanitize-cfi-icall-generalize-pointers"; |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 569 | Stats = Args.hasFlag(options::OPT_fsanitize_stats, |
| 570 | options::OPT_fno_sanitize_stats, false); |
| 571 | |
Evgeniy Stepanov | 6d2b6f0 | 2017-08-29 20:03:51 +0000 | [diff] [blame] | 572 | if (MinimalRuntime) { |
| 573 | SanitizerMask IncompatibleMask = |
| 574 | Kinds & ~setGroupBits(CompatibleWithMinimalRuntime); |
| 575 | if (IncompatibleMask) |
| 576 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 577 | << "-fsanitize-minimal-runtime" |
| 578 | << lastArgumentForMask(D, Args, IncompatibleMask); |
| 579 | |
| 580 | SanitizerMask NonTrappingCfi = Kinds & CFI & ~TrappingKinds; |
| 581 | if (NonTrappingCfi) |
| 582 | D.Diag(clang::diag::err_drv_argument_only_allowed_with) |
| 583 | << "fsanitize-minimal-runtime" |
| 584 | << "fsanitize-trap=cfi"; |
| 585 | } |
| 586 | |
Alexey Samsonov | dfa908c | 2015-05-07 22:34:06 +0000 | [diff] [blame] | 587 | // Parse -f(no-)?sanitize-coverage flags if coverage is supported by the |
| 588 | // enabled sanitizers. |
Kostya Serebryany | 52e8649 | 2016-02-18 00:49:23 +0000 | [diff] [blame] | 589 | for (const auto *Arg : Args) { |
| 590 | if (Arg->getOption().matches(options::OPT_fsanitize_coverage)) { |
| 591 | int LegacySanitizeCoverage; |
| 592 | if (Arg->getNumValues() == 1 && |
| 593 | !StringRef(Arg->getValue(0)) |
Kostya Serebryany | 9d1ed13 | 2017-04-19 19:57:16 +0000 | [diff] [blame] | 594 | .getAsInteger(0, LegacySanitizeCoverage)) { |
| 595 | CoverageFeatures = 0; |
| 596 | Arg->claim(); |
| 597 | if (LegacySanitizeCoverage != 0) { |
Nico Weber | 4152f52 | 2016-02-18 19:32:54 +0000 | [diff] [blame] | 598 | D.Diag(diag::warn_drv_deprecated_arg) |
Kostya Serebryany | 9d1ed13 | 2017-04-19 19:57:16 +0000 | [diff] [blame] | 599 | << Arg->getAsString(Args) << "-fsanitize-coverage=trace-pc-guard"; |
Alexey Samsonov | dfa908c | 2015-05-07 22:34:06 +0000 | [diff] [blame] | 600 | } |
Kostya Serebryany | 52e8649 | 2016-02-18 00:49:23 +0000 | [diff] [blame] | 601 | continue; |
Alexey Samsonov | dfa908c | 2015-05-07 22:34:06 +0000 | [diff] [blame] | 602 | } |
Kostya Serebryany | 52e8649 | 2016-02-18 00:49:23 +0000 | [diff] [blame] | 603 | CoverageFeatures |= parseCoverageFeatures(D, Arg); |
Evgeniy Stepanov | 5b49eb4 | 2016-06-14 21:33:40 +0000 | [diff] [blame] | 604 | |
| 605 | // Disable coverage and not claim the flags if there is at least one |
| 606 | // non-supporting sanitizer. |
Petr Hosek | eb4127f | 2017-07-21 01:17:49 +0000 | [diff] [blame] | 607 | if (!(AllAddedKinds & ~AllRemove & ~setGroupBits(SupportsCoverage))) { |
Kostya Serebryany | 52e8649 | 2016-02-18 00:49:23 +0000 | [diff] [blame] | 608 | Arg->claim(); |
Kostya Serebryany | f5b25f8 | 2016-04-18 21:30:17 +0000 | [diff] [blame] | 609 | } else { |
| 610 | CoverageFeatures = 0; |
Kostya Serebryany | 52e8649 | 2016-02-18 00:49:23 +0000 | [diff] [blame] | 611 | } |
| 612 | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_coverage)) { |
| 613 | Arg->claim(); |
| 614 | CoverageFeatures &= ~parseCoverageFeatures(D, Arg); |
Kostya Serebryany | 75b4f9e | 2014-11-11 22:15:07 +0000 | [diff] [blame] | 615 | } |
| 616 | } |
Alexey Samsonov | dfa908c | 2015-05-07 22:34:06 +0000 | [diff] [blame] | 617 | // Choose at most one coverage type: function, bb, or edge. |
| 618 | if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageBB)) |
| 619 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 620 | << "-fsanitize-coverage=func" |
| 621 | << "-fsanitize-coverage=bb"; |
| 622 | if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageEdge)) |
| 623 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 624 | << "-fsanitize-coverage=func" |
| 625 | << "-fsanitize-coverage=edge"; |
| 626 | if ((CoverageFeatures & CoverageBB) && (CoverageFeatures & CoverageEdge)) |
| 627 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 628 | << "-fsanitize-coverage=bb" |
| 629 | << "-fsanitize-coverage=edge"; |
| 630 | // Basic block tracing and 8-bit counters require some type of coverage |
| 631 | // enabled. |
Kostya Serebryany | 1c0e9e9 | 2017-04-19 21:31:11 +0000 | [diff] [blame] | 632 | if (CoverageFeatures & CoverageTraceBB) |
| 633 | D.Diag(clang::diag::warn_drv_deprecated_arg) |
Alexey Samsonov | dfa908c | 2015-05-07 22:34:06 +0000 | [diff] [blame] | 634 | << "-fsanitize-coverage=trace-bb" |
Kostya Serebryany | 1c0e9e9 | 2017-04-19 21:31:11 +0000 | [diff] [blame] | 635 | << "-fsanitize-coverage=trace-pc-guard"; |
| 636 | if (CoverageFeatures & Coverage8bitCounters) |
Kostya Serebryany | 1a02d8b | 2017-04-19 20:15:58 +0000 | [diff] [blame] | 637 | D.Diag(clang::diag::warn_drv_deprecated_arg) |
Alexey Samsonov | dfa908c | 2015-05-07 22:34:06 +0000 | [diff] [blame] | 638 | << "-fsanitize-coverage=8bit-counters" |
Kostya Serebryany | 1a02d8b | 2017-04-19 20:15:58 +0000 | [diff] [blame] | 639 | << "-fsanitize-coverage=trace-pc-guard"; |
Kostya Serebryany | 8955efc | 2017-05-03 01:27:28 +0000 | [diff] [blame] | 640 | |
| 641 | int InsertionPointTypes = CoverageFunc | CoverageBB | CoverageEdge; |
Kostya Serebryany | 9f338dc | 2017-08-08 20:20:40 +0000 | [diff] [blame] | 642 | int InstrumentationTypes = |
| 643 | CoverageTracePC | CoverageTracePCGuard | CoverageInline8bitCounters; |
Kostya Serebryany | 8955efc | 2017-05-03 01:27:28 +0000 | [diff] [blame] | 644 | if ((CoverageFeatures & InsertionPointTypes) && |
Kostya Serebryany | 9f338dc | 2017-08-08 20:20:40 +0000 | [diff] [blame] | 645 | !(CoverageFeatures & InstrumentationTypes)) { |
Kostya Serebryany | 8955efc | 2017-05-03 01:27:28 +0000 | [diff] [blame] | 646 | D.Diag(clang::diag::warn_drv_deprecated_arg) |
| 647 | << "-fsanitize-coverage=[func|bb|edge]" |
| 648 | << "-fsanitize-coverage=[func|bb|edge],[trace-pc-guard|trace-pc]"; |
| 649 | } |
| 650 | |
Kostya Serebryany | 52e8649 | 2016-02-18 00:49:23 +0000 | [diff] [blame] | 651 | // trace-pc w/o func/bb/edge implies edge. |
Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 652 | if (!(CoverageFeatures & InsertionPointTypes)) { |
| 653 | if (CoverageFeatures & |
| 654 | (CoverageTracePC | CoverageTracePCGuard | CoverageInline8bitCounters)) |
| 655 | CoverageFeatures |= CoverageEdge; |
| 656 | |
| 657 | if (CoverageFeatures & CoverageStackDepth) |
| 658 | CoverageFeatures |= CoverageFunc; |
| 659 | } |
Kostya Serebryany | 75b4f9e | 2014-11-11 22:15:07 +0000 | [diff] [blame] | 660 | |
Evgeniy Stepanov | 0876cfb | 2017-10-05 20:14:00 +0000 | [diff] [blame] | 661 | SharedRuntime = |
| 662 | Args.hasFlag(options::OPT_shared_libsan, options::OPT_static_libsan, |
Vedant Kumar | 358d642 | 2017-10-07 01:42:09 +0000 | [diff] [blame] | 663 | TC.getTriple().isAndroid() || TC.getTriple().isOSFuchsia() || |
| 664 | TC.getTriple().isOSDarwin()); |
Evgeniy Stepanov | 0876cfb | 2017-10-05 20:14:00 +0000 | [diff] [blame] | 665 | |
Evgeniy Stepanov | d5d36a1 | 2017-10-16 18:02:57 +0000 | [diff] [blame] | 666 | ImplicitCfiRuntime = TC.getTriple().isAndroid(); |
| 667 | |
Alexey Samsonov | de0aff3 | 2015-05-21 01:07:52 +0000 | [diff] [blame] | 668 | if (AllAddedKinds & Address) { |
Evgeniy Stepanov | 117627c | 2017-10-25 20:39:22 +0000 | [diff] [blame] | 669 | NeedPIE |= TC.getTriple().isOSFuchsia(); |
Kostya Serebryany | aed71a8 | 2014-10-09 17:53:04 +0000 | [diff] [blame] | 670 | if (Arg *A = |
| 671 | Args.getLastArg(options::OPT_fsanitize_address_field_padding)) { |
| 672 | StringRef S = A->getValue(); |
| 673 | // Legal values are 0 and 1, 2, but in future we may add more levels. |
| 674 | if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 || |
| 675 | AsanFieldPadding > 2) { |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 676 | D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S; |
Kostya Serebryany | aed71a8 | 2014-10-09 17:53:04 +0000 | [diff] [blame] | 677 | } |
| 678 | } |
Ehsan Akhgari | e0db196 | 2014-10-14 23:15:44 +0000 | [diff] [blame] | 679 | |
| 680 | if (Arg *WindowsDebugRTArg = |
| 681 | Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT, |
| 682 | options::OPT__SLASH_MDd, options::OPT__SLASH_MD, |
| 683 | options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) { |
| 684 | switch (WindowsDebugRTArg->getOption().getID()) { |
| 685 | case options::OPT__SLASH_MTd: |
| 686 | case options::OPT__SLASH_MDd: |
| 687 | case options::OPT__SLASH_LDd: |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 688 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
Ehsan Akhgari | e0db196 | 2014-10-14 23:15:44 +0000 | [diff] [blame] | 689 | << WindowsDebugRTArg->getAsString(Args) |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 690 | << lastArgumentForMask(D, Args, Address); |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 691 | D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime); |
Ehsan Akhgari | e0db196 | 2014-10-14 23:15:44 +0000 | [diff] [blame] | 692 | } |
| 693 | } |
Alexey Samsonov | 90490af | 2014-08-08 22:47:17 +0000 | [diff] [blame] | 694 | |
Vedant Kumar | a9d8be6 | 2017-05-08 21:11:55 +0000 | [diff] [blame] | 695 | AsanUseAfterScope = Args.hasFlag( |
| 696 | options::OPT_fsanitize_address_use_after_scope, |
| 697 | options::OPT_fno_sanitize_address_use_after_scope, AsanUseAfterScope); |
Evgeniy Stepanov | d991cdd | 2017-05-09 21:57:43 +0000 | [diff] [blame] | 698 | |
| 699 | // As a workaround for a bug in gold 2.26 and earlier, dead stripping of |
| 700 | // globals in ASan is disabled by default on ELF targets. |
| 701 | // See https://sourceware.org/bugzilla/show_bug.cgi?id=19002 |
| 702 | AsanGlobalsDeadStripping = |
Petr Hosek | b31582b | 2017-08-03 23:02:22 +0000 | [diff] [blame] | 703 | !TC.getTriple().isOSBinFormatELF() || TC.getTriple().isOSFuchsia() || |
Evgeniy Stepanov | d991cdd | 2017-05-09 21:57:43 +0000 | [diff] [blame] | 704 | Args.hasArg(options::OPT_fsanitize_address_globals_dead_stripping); |
Vedant Kumar | a9d8be6 | 2017-05-08 21:11:55 +0000 | [diff] [blame] | 705 | } else { |
| 706 | AsanUseAfterScope = false; |
Vitaly Buka | 9d4eb6f | 2016-06-02 00:24:20 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Petr Hosek | 766288b | 2017-08-16 19:06:05 +0000 | [diff] [blame] | 709 | if (AllAddedKinds & SafeStack) { |
| 710 | // SafeStack runtime is built into the system on Fuchsia. |
| 711 | SafeStackRuntime = !TC.getTriple().isOSFuchsia(); |
| 712 | } |
| 713 | |
Alexey Samsonov | 90490af | 2014-08-08 22:47:17 +0000 | [diff] [blame] | 714 | // Parse -link-cxx-sanitizer flag. |
| 715 | LinkCXXRuntimes = |
| 716 | Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX(); |
Alexey Samsonov | ecf380e | 2015-03-20 18:45:06 +0000 | [diff] [blame] | 717 | |
| 718 | // Finally, initialize the set of available and recoverable sanitizers. |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 719 | Sanitizers.Mask |= Kinds; |
| 720 | RecoverableSanitizers.Mask |= RecoverableKinds; |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 721 | TrapSanitizers.Mask |= TrappingKinds; |
Vedant Kumar | fae4f7c | 2017-12-21 00:10:24 +0000 | [diff] [blame] | 722 | assert(!(RecoverableKinds & TrappingKinds) && |
| 723 | "Overlap between recoverable and trapping sanitizers"); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 726 | static std::string toString(const clang::SanitizerSet &Sanitizers) { |
| 727 | std::string Res; |
| 728 | #define SANITIZER(NAME, ID) \ |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 729 | if (Sanitizers.has(ID)) { \ |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 730 | if (!Res.empty()) \ |
| 731 | Res += ","; \ |
| 732 | Res += NAME; \ |
| 733 | } |
| 734 | #include "clang/Basic/Sanitizers.def" |
| 735 | return Res; |
| 736 | } |
| 737 | |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 738 | static void addIncludeLinkerOption(const ToolChain &TC, |
| 739 | const llvm::opt::ArgList &Args, |
| 740 | llvm::opt::ArgStringList &CmdArgs, |
| 741 | StringRef SymbolName) { |
| 742 | SmallString<64> LinkerOptionFlag; |
| 743 | LinkerOptionFlag = "--linker-option=/include:"; |
| 744 | if (TC.getTriple().getArch() == llvm::Triple::x86) { |
| 745 | // Win32 mangles C function names with a '_' prefix. |
| 746 | LinkerOptionFlag += '_'; |
| 747 | } |
| 748 | LinkerOptionFlag += SymbolName; |
| 749 | CmdArgs.push_back(Args.MakeArgString(LinkerOptionFlag)); |
| 750 | } |
| 751 | |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 752 | void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, |
Peter Collingbourne | 581f438 | 2015-07-02 01:48:12 +0000 | [diff] [blame] | 753 | llvm::opt::ArgStringList &CmdArgs, |
| 754 | types::ID InputType) const { |
Justin Lebar | 6efbc73 | 2016-09-15 23:44:13 +0000 | [diff] [blame] | 755 | // NVPTX doesn't currently support sanitizers. Bailing out here means that |
| 756 | // e.g. -fsanitize=address applies only to host code, which is what we want |
| 757 | // for now. |
| 758 | if (TC.getTriple().isNVPTX()) |
| 759 | return; |
| 760 | |
Kostya Serebryany | 52e8649 | 2016-02-18 00:49:23 +0000 | [diff] [blame] | 761 | // Translate available CoverageFeatures to corresponding clang-cc1 flags. |
| 762 | // Do it even if Sanitizers.empty() since some forms of coverage don't require |
| 763 | // sanitizers. |
| 764 | std::pair<int, const char *> CoverageFlags[] = { |
| 765 | std::make_pair(CoverageFunc, "-fsanitize-coverage-type=1"), |
| 766 | std::make_pair(CoverageBB, "-fsanitize-coverage-type=2"), |
| 767 | std::make_pair(CoverageEdge, "-fsanitize-coverage-type=3"), |
| 768 | std::make_pair(CoverageIndirCall, "-fsanitize-coverage-indirect-calls"), |
| 769 | std::make_pair(CoverageTraceBB, "-fsanitize-coverage-trace-bb"), |
| 770 | std::make_pair(CoverageTraceCmp, "-fsanitize-coverage-trace-cmp"), |
Kostya Serebryany | 3b41971 | 2016-08-30 01:27:03 +0000 | [diff] [blame] | 771 | std::make_pair(CoverageTraceDiv, "-fsanitize-coverage-trace-div"), |
| 772 | std::make_pair(CoverageTraceGep, "-fsanitize-coverage-trace-gep"), |
Kostya Serebryany | 52e8649 | 2016-02-18 00:49:23 +0000 | [diff] [blame] | 773 | std::make_pair(Coverage8bitCounters, "-fsanitize-coverage-8bit-counters"), |
Kostya Serebryany | 60cdd61 | 2016-09-14 01:39:49 +0000 | [diff] [blame] | 774 | std::make_pair(CoverageTracePC, "-fsanitize-coverage-trace-pc"), |
Kostya Serebryany | 50fb618 | 2017-05-05 23:28:18 +0000 | [diff] [blame] | 775 | std::make_pair(CoverageTracePCGuard, "-fsanitize-coverage-trace-pc-guard"), |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 776 | std::make_pair(CoverageInline8bitCounters, "-fsanitize-coverage-inline-8bit-counters"), |
Kostya Serebryany | 6145776 | 2017-07-28 00:10:10 +0000 | [diff] [blame] | 777 | std::make_pair(CoveragePCTable, "-fsanitize-coverage-pc-table"), |
Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 778 | std::make_pair(CoverageNoPrune, "-fsanitize-coverage-no-prune"), |
| 779 | std::make_pair(CoverageStackDepth, "-fsanitize-coverage-stack-depth")}; |
Kostya Serebryany | 52e8649 | 2016-02-18 00:49:23 +0000 | [diff] [blame] | 780 | for (auto F : CoverageFlags) { |
| 781 | if (CoverageFeatures & F.first) |
Evgeniy Stepanov | b86ca11 | 2017-05-09 21:57:39 +0000 | [diff] [blame] | 782 | CmdArgs.push_back(F.second); |
Kostya Serebryany | 52e8649 | 2016-02-18 00:49:23 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Evgeniy Stepanov | 5c063c1 | 2016-06-14 23:21:19 +0000 | [diff] [blame] | 785 | if (TC.getTriple().isOSWindows() && needsUbsanRt()) { |
| 786 | // Instruct the code generator to embed linker directives in the object file |
| 787 | // that cause the required runtime libraries to be linked. |
| 788 | CmdArgs.push_back(Args.MakeArgString( |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 789 | "--dependent-lib=" + TC.getCompilerRT(Args, "ubsan_standalone"))); |
Evgeniy Stepanov | 5c063c1 | 2016-06-14 23:21:19 +0000 | [diff] [blame] | 790 | if (types::isCXX(InputType)) |
| 791 | CmdArgs.push_back(Args.MakeArgString( |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 792 | "--dependent-lib=" + TC.getCompilerRT(Args, "ubsan_standalone_cxx"))); |
Evgeniy Stepanov | 5c063c1 | 2016-06-14 23:21:19 +0000 | [diff] [blame] | 793 | } |
| 794 | if (TC.getTriple().isOSWindows() && needsStatsRt()) { |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 795 | CmdArgs.push_back(Args.MakeArgString("--dependent-lib=" + |
| 796 | TC.getCompilerRT(Args, "stats_client"))); |
Evgeniy Stepanov | 5c063c1 | 2016-06-14 23:21:19 +0000 | [diff] [blame] | 797 | |
| 798 | // The main executable must export the stats runtime. |
| 799 | // FIXME: Only exporting from the main executable (e.g. based on whether the |
| 800 | // translation unit defines main()) would save a little space, but having |
| 801 | // multiple copies of the runtime shouldn't hurt. |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 802 | CmdArgs.push_back(Args.MakeArgString("--dependent-lib=" + |
| 803 | TC.getCompilerRT(Args, "stats"))); |
Evgeniy Stepanov | 5c063c1 | 2016-06-14 23:21:19 +0000 | [diff] [blame] | 804 | addIncludeLinkerOption(TC, Args, CmdArgs, "__sanitizer_stats_register"); |
| 805 | } |
| 806 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 807 | if (Sanitizers.empty()) |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 808 | return; |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 809 | CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers))); |
| 810 | |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 811 | if (!RecoverableSanitizers.empty()) |
| 812 | CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" + |
| 813 | toString(RecoverableSanitizers))); |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 814 | |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 815 | if (!TrapSanitizers.empty()) |
| 816 | CmdArgs.push_back( |
| 817 | Args.MakeArgString("-fsanitize-trap=" + toString(TrapSanitizers))); |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 818 | |
Alexey Samsonov | a511cdd | 2015-02-04 17:40:08 +0000 | [diff] [blame] | 819 | for (const auto &BLPath : BlacklistFiles) { |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 820 | SmallString<64> BlacklistOpt("-fsanitize-blacklist="); |
Alexey Samsonov | a511cdd | 2015-02-04 17:40:08 +0000 | [diff] [blame] | 821 | BlacklistOpt += BLPath; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 822 | CmdArgs.push_back(Args.MakeArgString(BlacklistOpt)); |
| 823 | } |
Ivan Krasin | 4c3f237 | 2015-09-02 20:02:38 +0000 | [diff] [blame] | 824 | for (const auto &Dep : ExtraDeps) { |
| 825 | SmallString<64> ExtraDepOpt("-fdepfile-entry="); |
| 826 | ExtraDepOpt += Dep; |
| 827 | CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt)); |
| 828 | } |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 829 | |
| 830 | if (MsanTrackOrigins) |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 831 | CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" + |
Benjamin Kramer | 3a13ed6 | 2017-12-28 16:58:54 +0000 | [diff] [blame] | 832 | Twine(MsanTrackOrigins))); |
Evgeniy Stepanov | 45be9e0 | 2015-07-10 20:07:16 +0000 | [diff] [blame] | 833 | |
| 834 | if (MsanUseAfterDtor) |
Evgeniy Stepanov | b86ca11 | 2017-05-09 21:57:39 +0000 | [diff] [blame] | 835 | CmdArgs.push_back("-fsanitize-memory-use-after-dtor"); |
Evgeniy Stepanov | 45be9e0 | 2015-07-10 20:07:16 +0000 | [diff] [blame] | 836 | |
Evgeniy Stepanov | 8a2bedb | 2016-11-11 23:17:36 +0000 | [diff] [blame] | 837 | // FIXME: Pass these parameters as function attributes, not as -llvm flags. |
| 838 | if (!TsanMemoryAccess) { |
| 839 | CmdArgs.push_back("-mllvm"); |
| 840 | CmdArgs.push_back("-tsan-instrument-memory-accesses=0"); |
| 841 | CmdArgs.push_back("-mllvm"); |
| 842 | CmdArgs.push_back("-tsan-instrument-memintrinsics=0"); |
| 843 | } |
| 844 | if (!TsanFuncEntryExit) { |
| 845 | CmdArgs.push_back("-mllvm"); |
| 846 | CmdArgs.push_back("-tsan-instrument-func-entry-exit=0"); |
| 847 | } |
| 848 | if (!TsanAtomics) { |
| 849 | CmdArgs.push_back("-mllvm"); |
| 850 | CmdArgs.push_back("-tsan-instrument-atomics=0"); |
| 851 | } |
| 852 | |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 853 | if (CfiCrossDso) |
Evgeniy Stepanov | b86ca11 | 2017-05-09 21:57:39 +0000 | [diff] [blame] | 854 | CmdArgs.push_back("-fsanitize-cfi-cross-dso"); |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 855 | |
Vlad Tsyrklevich | 634c601 | 2017-10-31 22:39:44 +0000 | [diff] [blame] | 856 | if (CfiICallGeneralizePointers) |
| 857 | CmdArgs.push_back("-fsanitize-cfi-icall-generalize-pointers"); |
| 858 | |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 859 | if (Stats) |
Evgeniy Stepanov | b86ca11 | 2017-05-09 21:57:39 +0000 | [diff] [blame] | 860 | CmdArgs.push_back("-fsanitize-stats"); |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 861 | |
Evgeniy Stepanov | 6d2b6f0 | 2017-08-29 20:03:51 +0000 | [diff] [blame] | 862 | if (MinimalRuntime) |
| 863 | CmdArgs.push_back("-fsanitize-minimal-runtime"); |
| 864 | |
Kostya Serebryany | aed71a8 | 2014-10-09 17:53:04 +0000 | [diff] [blame] | 865 | if (AsanFieldPadding) |
| 866 | CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" + |
Benjamin Kramer | 3a13ed6 | 2017-12-28 16:58:54 +0000 | [diff] [blame] | 867 | Twine(AsanFieldPadding))); |
Alexey Samsonov | dfa908c | 2015-05-07 22:34:06 +0000 | [diff] [blame] | 868 | |
Vitaly Buka | 9d4eb6f | 2016-06-02 00:24:20 +0000 | [diff] [blame] | 869 | if (AsanUseAfterScope) |
Evgeniy Stepanov | b86ca11 | 2017-05-09 21:57:39 +0000 | [diff] [blame] | 870 | CmdArgs.push_back("-fsanitize-address-use-after-scope"); |
Vitaly Buka | 9d4eb6f | 2016-06-02 00:24:20 +0000 | [diff] [blame] | 871 | |
Evgeniy Stepanov | d991cdd | 2017-05-09 21:57:43 +0000 | [diff] [blame] | 872 | if (AsanGlobalsDeadStripping) |
| 873 | CmdArgs.push_back("-fsanitize-address-globals-dead-stripping"); |
| 874 | |
Sergey Matveev | 2ba8778 | 2015-02-17 15:09:33 +0000 | [diff] [blame] | 875 | // MSan: Workaround for PR16386. |
| 876 | // ASan: This is mainly to help LSan with cases such as |
Hans Wennborg | 5fd1f17 | 2017-11-13 23:27:54 +0000 | [diff] [blame] | 877 | // https://github.com/google/sanitizers/issues/373 |
Sergey Matveev | 2ba8778 | 2015-02-17 15:09:33 +0000 | [diff] [blame] | 878 | // We can't make this conditional on -fsanitize=leak, as that flag shouldn't |
| 879 | // affect compilation. |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 880 | if (Sanitizers.has(Memory) || Sanitizers.has(Address)) |
Evgeniy Stepanov | b86ca11 | 2017-05-09 21:57:39 +0000 | [diff] [blame] | 881 | CmdArgs.push_back("-fno-assume-sane-operator-new"); |
Peter Collingbourne | 581f438 | 2015-07-02 01:48:12 +0000 | [diff] [blame] | 882 | |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 883 | // Require -fvisibility= flag on non-Windows when compiling if vptr CFI is |
| 884 | // enabled. |
| 885 | if (Sanitizers.hasOneOf(CFIClasses) && !TC.getTriple().isOSWindows() && |
| 886 | !Args.hasArg(options::OPT_fvisibility_EQ)) { |
| 887 | TC.getDriver().Diag(clang::diag::err_drv_argument_only_allowed_with) |
| 888 | << lastArgumentForMask(TC.getDriver(), Args, |
| 889 | Sanitizers.Mask & CFIClasses) |
| 890 | << "-fvisibility="; |
| 891 | } |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 894 | SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A, |
| 895 | bool DiagnoseErrors) { |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 896 | assert((A->getOption().matches(options::OPT_fsanitize_EQ) || |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 897 | A->getOption().matches(options::OPT_fno_sanitize_EQ) || |
| 898 | A->getOption().matches(options::OPT_fsanitize_recover_EQ) || |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 899 | A->getOption().matches(options::OPT_fno_sanitize_recover_EQ) || |
| 900 | A->getOption().matches(options::OPT_fsanitize_trap_EQ) || |
| 901 | A->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) && |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 902 | "Invalid argument in parseArgValues!"); |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 903 | SanitizerMask Kinds = 0; |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 904 | for (int i = 0, n = A->getNumValues(); i != n; ++i) { |
| 905 | const char *Value = A->getValue(i); |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 906 | SanitizerMask Kind; |
Alexey Samsonov | abd5bea | 2014-12-19 18:41:43 +0000 | [diff] [blame] | 907 | // Special case: don't accept -fsanitize=all. |
| 908 | if (A->getOption().matches(options::OPT_fsanitize_EQ) && |
| 909 | 0 == strcmp("all", Value)) |
| 910 | Kind = 0; |
Derek Bruening | 256c2e1 | 2016-04-21 21:32:04 +0000 | [diff] [blame] | 911 | // Similarly, don't accept -fsanitize=efficiency-all. |
| 912 | else if (A->getOption().matches(options::OPT_fsanitize_EQ) && |
| 913 | 0 == strcmp("efficiency-all", Value)) |
| 914 | Kind = 0; |
Alexey Samsonov | abd5bea | 2014-12-19 18:41:43 +0000 | [diff] [blame] | 915 | else |
Peter Collingbourne | bf59c34 | 2015-05-11 21:39:20 +0000 | [diff] [blame] | 916 | Kind = parseSanitizerValue(Value, /*AllowGroups=*/true); |
Alexey Samsonov | abd5bea | 2014-12-19 18:41:43 +0000 | [diff] [blame] | 917 | |
| 918 | if (Kind) |
| 919 | Kinds |= Kind; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 920 | else if (DiagnoseErrors) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 921 | D.Diag(clang::diag::err_drv_unsupported_option_argument) |
Alexey Samsonov | abd5bea | 2014-12-19 18:41:43 +0000 | [diff] [blame] | 922 | << A->getOption().getName() << Value; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 923 | } |
Alexey Samsonov | abd5bea | 2014-12-19 18:41:43 +0000 | [diff] [blame] | 924 | return Kinds; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Alexey Samsonov | dfa908c | 2015-05-07 22:34:06 +0000 | [diff] [blame] | 927 | int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A) { |
| 928 | assert(A->getOption().matches(options::OPT_fsanitize_coverage) || |
| 929 | A->getOption().matches(options::OPT_fno_sanitize_coverage)); |
| 930 | int Features = 0; |
| 931 | for (int i = 0, n = A->getNumValues(); i != n; ++i) { |
| 932 | const char *Value = A->getValue(i); |
| 933 | int F = llvm::StringSwitch<int>(Value) |
| 934 | .Case("func", CoverageFunc) |
| 935 | .Case("bb", CoverageBB) |
| 936 | .Case("edge", CoverageEdge) |
| 937 | .Case("indirect-calls", CoverageIndirCall) |
| 938 | .Case("trace-bb", CoverageTraceBB) |
| 939 | .Case("trace-cmp", CoverageTraceCmp) |
Kostya Serebryany | 3b41971 | 2016-08-30 01:27:03 +0000 | [diff] [blame] | 940 | .Case("trace-div", CoverageTraceDiv) |
| 941 | .Case("trace-gep", CoverageTraceGep) |
Alexey Samsonov | dfa908c | 2015-05-07 22:34:06 +0000 | [diff] [blame] | 942 | .Case("8bit-counters", Coverage8bitCounters) |
Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 943 | .Case("trace-pc", CoverageTracePC) |
Kostya Serebryany | 60cdd61 | 2016-09-14 01:39:49 +0000 | [diff] [blame] | 944 | .Case("trace-pc-guard", CoverageTracePCGuard) |
Kostya Serebryany | 50fb618 | 2017-05-05 23:28:18 +0000 | [diff] [blame] | 945 | .Case("no-prune", CoverageNoPrune) |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 946 | .Case("inline-8bit-counters", CoverageInline8bitCounters) |
Kostya Serebryany | 6145776 | 2017-07-28 00:10:10 +0000 | [diff] [blame] | 947 | .Case("pc-table", CoveragePCTable) |
Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 948 | .Case("stack-depth", CoverageStackDepth) |
Alexey Samsonov | dfa908c | 2015-05-07 22:34:06 +0000 | [diff] [blame] | 949 | .Default(0); |
| 950 | if (F == 0) |
| 951 | D.Diag(clang::diag::err_drv_unsupported_option_argument) |
| 952 | << A->getOption().getName() << Value; |
| 953 | Features |= F; |
| 954 | } |
| 955 | return Features; |
| 956 | } |
| 957 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 958 | std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args, |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 959 | SanitizerMask Mask) { |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 960 | for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(), |
| 961 | E = Args.rend(); |
| 962 | I != E; ++I) { |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 963 | const auto *Arg = *I; |
| 964 | if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) { |
Peter Collingbourne | bf59c34 | 2015-05-11 21:39:20 +0000 | [diff] [blame] | 965 | SanitizerMask AddKinds = |
| 966 | expandSanitizerGroups(parseArgValues(D, Arg, false)); |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 967 | if (AddKinds & Mask) |
| 968 | return describeSanitizeArg(Arg, Mask); |
| 969 | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) { |
Peter Collingbourne | bf59c34 | 2015-05-11 21:39:20 +0000 | [diff] [blame] | 970 | SanitizerMask RemoveKinds = |
| 971 | expandSanitizerGroups(parseArgValues(D, Arg, false)); |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 972 | Mask &= ~RemoveKinds; |
| 973 | } |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 974 | } |
| 975 | llvm_unreachable("arg list didn't provide expected value"); |
| 976 | } |
| 977 | |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 978 | std::string describeSanitizeArg(const llvm::opt::Arg *A, SanitizerMask Mask) { |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 979 | assert(A->getOption().matches(options::OPT_fsanitize_EQ) |
| 980 | && "Invalid argument in describeSanitizerArg!"); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 981 | |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 982 | std::string Sanitizers; |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 983 | for (int i = 0, n = A->getNumValues(); i != n; ++i) { |
Peter Collingbourne | bf59c34 | 2015-05-11 21:39:20 +0000 | [diff] [blame] | 984 | if (expandSanitizerGroups( |
| 985 | parseSanitizerValue(A->getValue(i), /*AllowGroups=*/true)) & |
| 986 | Mask) { |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 987 | if (!Sanitizers.empty()) |
| 988 | Sanitizers += ","; |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 989 | Sanitizers += A->getValue(i); |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 990 | } |
| 991 | } |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 992 | |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 993 | assert(!Sanitizers.empty() && "arg didn't provide expected value"); |
| 994 | return "-fsanitize=" + Sanitizers; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 995 | } |