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