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