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