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" |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 10 | #include "clang/Driver/Driver.h" |
| 11 | #include "clang/Driver/DriverDiagnostic.h" |
| 12 | #include "clang/Driver/Options.h" |
| 13 | #include "clang/Driver/ToolChain.h" |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringExtras.h" |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringSwitch.h" |
| 16 | #include "llvm/Support/FileSystem.h" |
| 17 | #include "llvm/Support/Path.h" |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 18 | #include "llvm/Support/SpecialCaseList.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 19 | #include <memory> |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang::driver; |
| 22 | using namespace llvm::opt; |
| 23 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | /// Assign ordinals to possible values of -fsanitize= flag. |
| 26 | /// We use the ordinal values as bit positions within \c SanitizeKind. |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 27 | enum SanitizeOrdinal : uint64_t { |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 28 | #define SANITIZER(NAME, ID) SO_##ID, |
| 29 | #define SANITIZER_GROUP(NAME, ID, ALIAS) SO_##ID##Group, |
| 30 | #include "clang/Basic/Sanitizers.def" |
| 31 | SO_Count |
| 32 | }; |
| 33 | |
| 34 | /// Represents a set of sanitizer kinds. It is also used to define: |
| 35 | /// 1) set of sanitizers each sanitizer group expands into. |
| 36 | /// 2) set of sanitizers sharing a specific property (e.g. |
| 37 | /// all sanitizers with zero-base shadow). |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 38 | enum SanitizeKind : uint64_t { |
| 39 | #define SANITIZER(NAME, ID) ID = 1ULL << SO_##ID, |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 40 | #define SANITIZER_GROUP(NAME, ID, ALIAS) \ |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 41 | ID = ALIAS, ID##Group = 1ULL << SO_##ID##Group, |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 42 | #include "clang/Basic/Sanitizers.def" |
| 43 | NeedsUbsanRt = Undefined | Integer, |
| 44 | NotAllowedWithTrap = Vptr, |
Dmitry Vyukov | 43419a7 | 2014-11-21 12:19:01 +0000 | [diff] [blame] | 45 | RequiresPIE = Memory | DataFlow, |
Kostya Serebryany | 2d88f3d | 2015-01-06 01:02:48 +0000 | [diff] [blame] | 46 | NeedsUnwindTables = Address | Thread | Memory | DataFlow, |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 47 | SupportsCoverage = Address | Memory | Leak | Undefined | Integer, |
| 48 | RecoverableByDefault = Undefined | Integer, |
| 49 | Unrecoverable = Address | Unreachable | Return, |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 50 | LegacyFsanitizeRecoverMask = Undefined | Integer, |
Peter Collingbourne | d2926c9 | 2015-03-14 02:42:25 +0000 | [diff] [blame^] | 51 | NeedsLTO = CFIDerivedCast | CFIUnrelatedCast | CFIVptr, |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 52 | }; |
| 53 | } |
| 54 | |
| 55 | /// Returns true if set of \p Sanitizers contain at least one sanitizer from |
| 56 | /// \p Kinds. |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 57 | static bool hasOneOf(const clang::SanitizerSet &Sanitizers, uint64_t Kinds) { |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 58 | #define SANITIZER(NAME, ID) \ |
| 59 | if (Sanitizers.has(clang::SanitizerKind::ID) && (Kinds & ID)) \ |
| 60 | return true; |
| 61 | #include "clang/Basic/Sanitizers.def" |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | /// Adds all sanitizers from \p Kinds to \p Sanitizers. |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 66 | static void addAllOf(clang::SanitizerSet &Sanitizers, uint64_t Kinds) { |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 67 | #define SANITIZER(NAME, ID) \ |
| 68 | if (Kinds & ID) \ |
| 69 | Sanitizers.set(clang::SanitizerKind::ID, true); |
| 70 | #include "clang/Basic/Sanitizers.def" |
| 71 | } |
| 72 | |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 73 | static uint64_t toSanitizeKind(clang::SanitizerKind K) { |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 74 | #define SANITIZER(NAME, ID) \ |
| 75 | if (K == clang::SanitizerKind::ID) \ |
| 76 | return ID; |
| 77 | #include "clang/Basic/Sanitizers.def" |
| 78 | llvm_unreachable("Invalid SanitizerKind!"); |
| 79 | } |
| 80 | |
| 81 | /// Parse a single value from a -fsanitize= or -fno-sanitize= value list. |
| 82 | /// Returns a member of the \c SanitizeKind enumeration, or \c 0 |
| 83 | /// if \p Value is not known. |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 84 | static uint64_t parseValue(const char *Value); |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 85 | |
| 86 | /// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any |
| 87 | /// invalid components. Returns OR of members of \c SanitizeKind enumeration. |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 88 | static uint64_t parseArgValues(const Driver &D, const llvm::opt::Arg *A, |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 89 | bool DiagnoseErrors); |
| 90 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 91 | /// Produce an argument string from ArgList \p Args, which shows how it |
| 92 | /// provides some sanitizer kind from \p Mask. For example, the argument list |
| 93 | /// "-fsanitize=thread,vptr -fsanitize=address" with mask \c NeedsUbsanRt |
| 94 | /// would produce "-fsanitize=vptr". |
| 95 | static std::string lastArgumentForMask(const Driver &D, |
| 96 | const llvm::opt::ArgList &Args, |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 97 | uint64_t Mask); |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 98 | |
| 99 | static std::string lastArgumentForKind(const Driver &D, |
| 100 | const llvm::opt::ArgList &Args, |
| 101 | clang::SanitizerKind K) { |
| 102 | return lastArgumentForMask(D, Args, toSanitizeKind(K)); |
| 103 | } |
| 104 | |
| 105 | /// Produce an argument string from argument \p A, which shows how it provides |
| 106 | /// a value in \p Mask. For instance, the argument |
| 107 | /// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce |
| 108 | /// "-fsanitize=alignment". |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 109 | static std::string describeSanitizeArg(const llvm::opt::Arg *A, uint64_t Mask); |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 110 | |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 111 | /// Produce a string containing comma-separated names of sanitizers in \p |
| 112 | /// Sanitizers set. |
| 113 | static std::string toString(const clang::SanitizerSet &Sanitizers); |
| 114 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 115 | /// For each sanitizer group bit set in \p Kinds, set the bits for sanitizers |
| 116 | /// this group enables. |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 117 | static uint64_t expandGroups(uint64_t Kinds); |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 118 | |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 119 | static uint64_t getToolchainUnsupportedKinds(const ToolChain &TC) { |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 120 | bool IsFreeBSD = TC.getTriple().getOS() == llvm::Triple::FreeBSD; |
| 121 | bool IsLinux = TC.getTriple().getOS() == llvm::Triple::Linux; |
| 122 | bool IsX86 = TC.getTriple().getArch() == llvm::Triple::x86; |
| 123 | bool IsX86_64 = TC.getTriple().getArch() == llvm::Triple::x86_64; |
Mohit K. Bhakkad | f4c47f6 | 2015-01-22 07:21:22 +0000 | [diff] [blame] | 124 | bool IsMIPS64 = TC.getTriple().getArch() == llvm::Triple::mips64 || |
| 125 | TC.getTriple().getArch() == llvm::Triple::mips64el; |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 126 | |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 127 | uint64_t Unsupported = 0; |
Mohit K. Bhakkad | f4c47f6 | 2015-01-22 07:21:22 +0000 | [diff] [blame] | 128 | if (!(IsLinux && (IsX86_64 || IsMIPS64))) { |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 129 | Unsupported |= Memory | DataFlow; |
| 130 | } |
Mohit K. Bhakkad | 69963e7 | 2015-02-23 09:32:35 +0000 | [diff] [blame] | 131 | if (!((IsLinux || IsFreeBSD) && (IsX86_64 || IsMIPS64))) { |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 132 | Unsupported |= Thread; |
| 133 | } |
| 134 | if (!(IsLinux && (IsX86 || IsX86_64))) { |
| 135 | Unsupported |= Function; |
| 136 | } |
| 137 | return Unsupported; |
| 138 | } |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 139 | |
| 140 | bool SanitizerArgs::needsUbsanRt() const { |
| 141 | return !UbsanTrapOnError && hasOneOf(Sanitizers, NeedsUbsanRt); |
| 142 | } |
| 143 | |
Dmitry Vyukov | 43419a7 | 2014-11-21 12:19:01 +0000 | [diff] [blame] | 144 | bool SanitizerArgs::requiresPIE() const { |
| 145 | return AsanZeroBaseShadow || hasOneOf(Sanitizers, RequiresPIE); |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | bool SanitizerArgs::needsUnwindTables() const { |
| 149 | return hasOneOf(Sanitizers, NeedsUnwindTables); |
| 150 | } |
| 151 | |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 152 | bool SanitizerArgs::needsLTO() const { |
Peter Collingbourne | d2926c9 | 2015-03-14 02:42:25 +0000 | [diff] [blame^] | 153 | return hasOneOf(Sanitizers, NeedsLTO); |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Alexey Samsonov | bb14f34 | 2013-08-08 11:32:17 +0000 | [diff] [blame] | 156 | void SanitizerArgs::clear() { |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 157 | Sanitizers.clear(); |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 158 | RecoverableSanitizers.clear(); |
Alexey Samsonov | a511cdd | 2015-02-04 17:40:08 +0000 | [diff] [blame] | 159 | BlacklistFiles.clear(); |
Kostya Serebryany | 75b4f9e | 2014-11-11 22:15:07 +0000 | [diff] [blame] | 160 | SanitizeCoverage = 0; |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 161 | MsanTrackOrigins = 0; |
Kostya Serebryany | aed71a8 | 2014-10-09 17:53:04 +0000 | [diff] [blame] | 162 | AsanFieldPadding = 0; |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 163 | AsanZeroBaseShadow = false; |
Alexey Samsonov | bb14f34 | 2013-08-08 11:32:17 +0000 | [diff] [blame] | 164 | UbsanTrapOnError = false; |
Alexey Samsonov | bdfa6c2 | 2014-04-01 13:31:10 +0000 | [diff] [blame] | 165 | AsanSharedRuntime = false; |
Alexey Samsonov | 90490af | 2014-08-08 22:47:17 +0000 | [diff] [blame] | 166 | LinkCXXRuntimes = false; |
Alexey Samsonov | bb14f34 | 2013-08-08 11:32:17 +0000 | [diff] [blame] | 167 | } |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 168 | |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 169 | SanitizerArgs::SanitizerArgs(const ToolChain &TC, |
| 170 | const llvm::opt::ArgList &Args) { |
Alexey Samsonov | bb14f34 | 2013-08-08 11:32:17 +0000 | [diff] [blame] | 171 | clear(); |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 172 | uint64_t AllRemove = 0; // During the loop below, the accumulated set of |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 173 | // sanitizers disabled by the current sanitizer |
| 174 | // argument or any argument after it. |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 175 | uint64_t DiagnosedKinds = 0; // All Kinds we have diagnosed up to now. |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 176 | // Used to deduplicate diagnostics. |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 177 | uint64_t Kinds = 0; |
| 178 | uint64_t NotSupported = getToolchainUnsupportedKinds(TC); |
Filipe Cabecinhas | ec5d0e6 | 2015-02-19 01:04:49 +0000 | [diff] [blame] | 179 | ToolChain::RTTIMode RTTIMode = TC.getRTTIMode(); |
| 180 | |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 181 | const Driver &D = TC.getDriver(); |
| 182 | for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend(); |
| 183 | I != E; ++I) { |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 184 | const auto *Arg = *I; |
| 185 | if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) { |
| 186 | Arg->claim(); |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 187 | uint64_t Add = parseArgValues(D, Arg, true); |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 188 | |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 189 | // Avoid diagnosing any sanitizer which is disabled later. |
| 190 | Add &= ~AllRemove; |
| 191 | // At this point we have not expanded groups, so any unsupported |
| 192 | // sanitizers in Add are those which have been explicitly enabled. |
| 193 | // Diagnose them. |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 194 | if (uint64_t KindsToDiagnose = Add & NotSupported & ~DiagnosedKinds) { |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 195 | // Only diagnose the new kinds. |
| 196 | std::string Desc = describeSanitizeArg(*I, KindsToDiagnose); |
| 197 | D.Diag(diag::err_drv_unsupported_opt_for_target) |
| 198 | << Desc << TC.getTriple().str(); |
| 199 | DiagnosedKinds |= KindsToDiagnose; |
| 200 | } |
| 201 | Add &= ~NotSupported; |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 202 | |
Filipe Cabecinhas | ec5d0e6 | 2015-02-19 01:04:49 +0000 | [diff] [blame] | 203 | // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups |
| 204 | // so we don't error out if -fno-rtti and -fsanitize=undefined were |
| 205 | // passed. |
| 206 | if (Add & SanitizeKind::Vptr && |
| 207 | (RTTIMode == ToolChain::RM_DisabledImplicitly || |
| 208 | RTTIMode == ToolChain::RM_DisabledExplicitly)) { |
| 209 | if (RTTIMode == ToolChain::RM_DisabledImplicitly) |
| 210 | // Warn about not having rtti enabled if the vptr sanitizer is |
| 211 | // explicitly enabled |
| 212 | D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default); |
| 213 | else { |
| 214 | const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg(); |
| 215 | assert(NoRTTIArg && |
| 216 | "RTTI disabled explicitly but we have no argument!"); |
| 217 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 218 | << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args); |
| 219 | } |
| 220 | |
| 221 | // Take out the Vptr sanitizer from the enabled sanitizers |
| 222 | AllRemove |= SanitizeKind::Vptr; |
| 223 | } |
| 224 | |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 225 | Add = expandGroups(Add); |
| 226 | // Group expansion may have enabled a sanitizer which is disabled later. |
| 227 | Add &= ~AllRemove; |
| 228 | // Silently discard any unsupported sanitizers implicitly enabled through |
| 229 | // group expansion. |
| 230 | Add &= ~NotSupported; |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 231 | |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 232 | Kinds |= Add; |
| 233 | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) { |
| 234 | Arg->claim(); |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 235 | uint64_t Remove = parseArgValues(D, Arg, true); |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 236 | AllRemove |= expandGroups(Remove); |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 237 | } |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 238 | } |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 239 | addAllOf(Sanitizers, Kinds); |
| 240 | |
Filipe Cabecinhas | ec5d0e6 | 2015-02-19 01:04:49 +0000 | [diff] [blame] | 241 | // We disable the vptr sanitizer if it was enabled by group expansion but RTTI |
| 242 | // is disabled. |
| 243 | if (Sanitizers.has(SanitizerKind::Vptr) && |
| 244 | (RTTIMode == ToolChain::RM_DisabledImplicitly || |
| 245 | RTTIMode == ToolChain::RM_DisabledExplicitly)) { |
| 246 | Kinds &= ~SanitizeKind::Vptr; |
| 247 | Sanitizers.set(SanitizerKind::Vptr, 0); |
| 248 | } |
| 249 | |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 250 | // Parse -f(no-)?sanitize-recover flags. |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 251 | uint64_t RecoverableKinds = RecoverableByDefault; |
| 252 | uint64_t DiagnosedUnrecoverableKinds = 0; |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 253 | for (const auto *Arg : Args) { |
Alexey Samsonov | ce2e77c | 2015-03-11 23:34:25 +0000 | [diff] [blame] | 254 | const char *DeprecatedReplacement = nullptr; |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 255 | if (Arg->getOption().matches(options::OPT_fsanitize_recover)) { |
Alexey Samsonov | ce2e77c | 2015-03-11 23:34:25 +0000 | [diff] [blame] | 256 | DeprecatedReplacement = "-fsanitize-recover=undefined,integer"; |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 257 | RecoverableKinds |= expandGroups(LegacyFsanitizeRecoverMask); |
| 258 | Arg->claim(); |
| 259 | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) { |
Alexey Samsonov | ce2e77c | 2015-03-11 23:34:25 +0000 | [diff] [blame] | 260 | DeprecatedReplacement = "-fno-sanitize-recover=undefined,integer"; |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 261 | RecoverableKinds &= ~expandGroups(LegacyFsanitizeRecoverMask); |
| 262 | Arg->claim(); |
| 263 | } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) { |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 264 | uint64_t Add = parseArgValues(D, Arg, true); |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 265 | // Report error if user explicitly tries to recover from unrecoverable |
| 266 | // sanitizer. |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 267 | if (uint64_t KindsToDiagnose = |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 268 | Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) { |
| 269 | SanitizerSet SetToDiagnose; |
| 270 | addAllOf(SetToDiagnose, KindsToDiagnose); |
| 271 | D.Diag(diag::err_drv_unsupported_option_argument) |
| 272 | << Arg->getOption().getName() << toString(SetToDiagnose); |
| 273 | DiagnosedUnrecoverableKinds |= KindsToDiagnose; |
| 274 | } |
| 275 | RecoverableKinds |= expandGroups(Add); |
| 276 | Arg->claim(); |
| 277 | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) { |
| 278 | RecoverableKinds &= ~expandGroups(parseArgValues(D, Arg, true)); |
| 279 | Arg->claim(); |
| 280 | } |
Alexey Samsonov | ce2e77c | 2015-03-11 23:34:25 +0000 | [diff] [blame] | 281 | if (DeprecatedReplacement) { |
| 282 | D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args) |
| 283 | << DeprecatedReplacement; |
| 284 | } |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 285 | } |
| 286 | RecoverableKinds &= Kinds; |
| 287 | RecoverableKinds &= ~Unrecoverable; |
| 288 | addAllOf(RecoverableSanitizers, RecoverableKinds); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 289 | |
| 290 | UbsanTrapOnError = |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 291 | Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error, |
| 292 | options::OPT_fno_sanitize_undefined_trap_on_error, false); |
| 293 | |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 294 | // Warn about undefined sanitizer options that require runtime support. |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 295 | if (UbsanTrapOnError && hasOneOf(Sanitizers, NotAllowedWithTrap)) { |
| 296 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 297 | << lastArgumentForMask(D, Args, NotAllowedWithTrap) |
Alexey Samsonov | cb3f812 | 2014-03-20 10:48:29 +0000 | [diff] [blame] | 298 | << "-fsanitize-undefined-trap-on-error"; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 301 | // Check for incompatible sanitizers. |
| 302 | bool NeedsAsan = Sanitizers.has(SanitizerKind::Address); |
| 303 | bool NeedsTsan = Sanitizers.has(SanitizerKind::Thread); |
| 304 | bool NeedsMsan = Sanitizers.has(SanitizerKind::Memory); |
| 305 | bool NeedsLsan = Sanitizers.has(SanitizerKind::Leak); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 306 | if (NeedsAsan && NeedsTsan) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 307 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 308 | << lastArgumentForKind(D, Args, SanitizerKind::Address) |
| 309 | << lastArgumentForKind(D, Args, SanitizerKind::Thread); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 310 | if (NeedsAsan && NeedsMsan) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 311 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 312 | << lastArgumentForKind(D, Args, SanitizerKind::Address) |
| 313 | << lastArgumentForKind(D, Args, SanitizerKind::Memory); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 314 | if (NeedsTsan && NeedsMsan) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 315 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 316 | << lastArgumentForKind(D, Args, SanitizerKind::Thread) |
| 317 | << lastArgumentForKind(D, Args, SanitizerKind::Memory); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 318 | if (NeedsLsan && NeedsTsan) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 319 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 320 | << lastArgumentForKind(D, Args, SanitizerKind::Leak) |
| 321 | << lastArgumentForKind(D, Args, SanitizerKind::Thread); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 322 | if (NeedsLsan && NeedsMsan) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 323 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 324 | << lastArgumentForKind(D, Args, SanitizerKind::Leak) |
| 325 | << lastArgumentForKind(D, Args, SanitizerKind::Memory); |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 326 | // FIXME: Currently -fsanitize=leak is silently ignored in the presence of |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 327 | // -fsanitize=address. Perhaps it should print an error, or perhaps |
| 328 | // -f(-no)sanitize=leak should change whether leak detection is enabled by |
| 329 | // default in ASan? |
| 330 | |
Alexey Samsonov | a511cdd | 2015-02-04 17:40:08 +0000 | [diff] [blame] | 331 | // Setup blacklist files. |
| 332 | // Add default blacklist from resource directory. |
| 333 | { |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 334 | std::string BLPath; |
Alexey Samsonov | 59f34bb | 2014-11-14 00:46:39 +0000 | [diff] [blame] | 335 | if (getDefaultBlacklist(D, BLPath) && llvm::sys::fs::exists(BLPath)) |
Alexey Samsonov | a511cdd | 2015-02-04 17:40:08 +0000 | [diff] [blame] | 336 | BlacklistFiles.push_back(BLPath); |
| 337 | } |
| 338 | // Parse -f(no-)sanitize-blacklist options. |
| 339 | for (const auto *Arg : Args) { |
| 340 | if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) { |
| 341 | Arg->claim(); |
| 342 | std::string BLPath = Arg->getValue(); |
| 343 | if (llvm::sys::fs::exists(BLPath)) |
| 344 | BlacklistFiles.push_back(BLPath); |
| 345 | else |
| 346 | D.Diag(clang::diag::err_drv_no_such_file) << BLPath; |
| 347 | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) { |
| 348 | Arg->claim(); |
| 349 | BlacklistFiles.clear(); |
| 350 | } |
| 351 | } |
| 352 | // Validate blacklists format. |
| 353 | { |
| 354 | std::string BLError; |
| 355 | std::unique_ptr<llvm::SpecialCaseList> SCL( |
| 356 | llvm::SpecialCaseList::create(BlacklistFiles, BLError)); |
| 357 | if (!SCL.get()) |
| 358 | D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 361 | // Parse -f[no-]sanitize-memory-track-origins[=level] options. |
| 362 | if (NeedsMsan) { |
| 363 | if (Arg *A = |
| 364 | Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ, |
| 365 | options::OPT_fsanitize_memory_track_origins, |
| 366 | options::OPT_fno_sanitize_memory_track_origins)) { |
| 367 | if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) { |
Evgeniy Stepanov | 6e09bca | 2015-02-26 15:59:30 +0000 | [diff] [blame] | 368 | MsanTrackOrigins = 2; |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 369 | } else if (A->getOption().matches( |
| 370 | options::OPT_fno_sanitize_memory_track_origins)) { |
| 371 | MsanTrackOrigins = 0; |
| 372 | } else { |
| 373 | StringRef S = A->getValue(); |
| 374 | if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 || |
| 375 | MsanTrackOrigins > 2) { |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 376 | D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S; |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
Kostya Serebryany | 78df9d0 | 2014-12-17 21:46:33 +0000 | [diff] [blame] | 382 | // Parse -fsanitize-coverage=N. Currently one of asan/msan/lsan is required. |
Kostya Serebryany | 2d88f3d | 2015-01-06 01:02:48 +0000 | [diff] [blame] | 383 | if (hasOneOf(Sanitizers, SupportsCoverage)) { |
Kostya Serebryany | 75b4f9e | 2014-11-11 22:15:07 +0000 | [diff] [blame] | 384 | if (Arg *A = Args.getLastArg(options::OPT_fsanitize_coverage)) { |
| 385 | StringRef S = A->getValue(); |
| 386 | // Legal values are 0..4. |
| 387 | if (S.getAsInteger(0, SanitizeCoverage) || SanitizeCoverage < 0 || |
| 388 | SanitizeCoverage > 4) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 389 | D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S; |
Kostya Serebryany | 75b4f9e | 2014-11-11 22:15:07 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Alexey Samsonov | bdfa6c2 | 2014-04-01 13:31:10 +0000 | [diff] [blame] | 393 | if (NeedsAsan) { |
| 394 | AsanSharedRuntime = |
Evgeniy Stepanov | 6f0ae18 | 2014-06-05 11:14:00 +0000 | [diff] [blame] | 395 | Args.hasArg(options::OPT_shared_libasan) || |
| 396 | (TC.getTriple().getEnvironment() == llvm::Triple::Android); |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 397 | AsanZeroBaseShadow = |
Evgeniy Stepanov | d04b861 | 2014-01-16 10:19:31 +0000 | [diff] [blame] | 398 | (TC.getTriple().getEnvironment() == llvm::Triple::Android); |
Kostya Serebryany | aed71a8 | 2014-10-09 17:53:04 +0000 | [diff] [blame] | 399 | if (Arg *A = |
| 400 | Args.getLastArg(options::OPT_fsanitize_address_field_padding)) { |
| 401 | StringRef S = A->getValue(); |
| 402 | // Legal values are 0 and 1, 2, but in future we may add more levels. |
| 403 | if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 || |
| 404 | AsanFieldPadding > 2) { |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 405 | D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S; |
Kostya Serebryany | aed71a8 | 2014-10-09 17:53:04 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
Ehsan Akhgari | e0db196 | 2014-10-14 23:15:44 +0000 | [diff] [blame] | 408 | |
| 409 | if (Arg *WindowsDebugRTArg = |
| 410 | Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT, |
| 411 | options::OPT__SLASH_MDd, options::OPT__SLASH_MD, |
| 412 | options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) { |
| 413 | switch (WindowsDebugRTArg->getOption().getID()) { |
| 414 | case options::OPT__SLASH_MTd: |
| 415 | case options::OPT__SLASH_MDd: |
| 416 | case options::OPT__SLASH_LDd: |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 417 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
Ehsan Akhgari | e0db196 | 2014-10-14 23:15:44 +0000 | [diff] [blame] | 418 | << WindowsDebugRTArg->getAsString(Args) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 419 | << lastArgumentForKind(D, Args, SanitizerKind::Address); |
| 420 | D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime); |
Ehsan Akhgari | e0db196 | 2014-10-14 23:15:44 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
Alexey Samsonov | bdfa6c2 | 2014-04-01 13:31:10 +0000 | [diff] [blame] | 423 | } |
Alexey Samsonov | 90490af | 2014-08-08 22:47:17 +0000 | [diff] [blame] | 424 | |
| 425 | // Parse -link-cxx-sanitizer flag. |
| 426 | LinkCXXRuntimes = |
| 427 | Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX(); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 430 | static std::string toString(const clang::SanitizerSet &Sanitizers) { |
| 431 | std::string Res; |
| 432 | #define SANITIZER(NAME, ID) \ |
| 433 | if (Sanitizers.has(clang::SanitizerKind::ID)) { \ |
| 434 | if (!Res.empty()) \ |
| 435 | Res += ","; \ |
| 436 | Res += NAME; \ |
| 437 | } |
| 438 | #include "clang/Basic/Sanitizers.def" |
| 439 | return Res; |
| 440 | } |
| 441 | |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 442 | void SanitizerArgs::addArgs(const llvm::opt::ArgList &Args, |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 443 | llvm::opt::ArgStringList &CmdArgs) const { |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 444 | if (Sanitizers.empty()) |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 445 | return; |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 446 | CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers))); |
| 447 | |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 448 | if (!RecoverableSanitizers.empty()) |
| 449 | CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" + |
| 450 | toString(RecoverableSanitizers))); |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 451 | |
| 452 | if (UbsanTrapOnError) |
| 453 | CmdArgs.push_back("-fsanitize-undefined-trap-on-error"); |
| 454 | |
Alexey Samsonov | a511cdd | 2015-02-04 17:40:08 +0000 | [diff] [blame] | 455 | for (const auto &BLPath : BlacklistFiles) { |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 456 | SmallString<64> BlacklistOpt("-fsanitize-blacklist="); |
Alexey Samsonov | a511cdd | 2015-02-04 17:40:08 +0000 | [diff] [blame] | 457 | BlacklistOpt += BLPath; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 458 | CmdArgs.push_back(Args.MakeArgString(BlacklistOpt)); |
| 459 | } |
| 460 | |
| 461 | if (MsanTrackOrigins) |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 462 | CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" + |
| 463 | llvm::utostr(MsanTrackOrigins))); |
Kostya Serebryany | aed71a8 | 2014-10-09 17:53:04 +0000 | [diff] [blame] | 464 | if (AsanFieldPadding) |
| 465 | CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" + |
| 466 | llvm::utostr(AsanFieldPadding))); |
Kostya Serebryany | 75b4f9e | 2014-11-11 22:15:07 +0000 | [diff] [blame] | 467 | if (SanitizeCoverage) |
| 468 | CmdArgs.push_back(Args.MakeArgString("-fsanitize-coverage=" + |
| 469 | llvm::utostr(SanitizeCoverage))); |
Sergey Matveev | 2ba8778 | 2015-02-17 15:09:33 +0000 | [diff] [blame] | 470 | // MSan: Workaround for PR16386. |
| 471 | // ASan: This is mainly to help LSan with cases such as |
| 472 | // https://code.google.com/p/address-sanitizer/issues/detail?id=373 |
| 473 | // We can't make this conditional on -fsanitize=leak, as that flag shouldn't |
| 474 | // affect compilation. |
| 475 | if (Sanitizers.has(SanitizerKind::Memory) || |
| 476 | Sanitizers.has(SanitizerKind::Address)) |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 477 | CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new")); |
| 478 | } |
| 479 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 480 | bool SanitizerArgs::getDefaultBlacklist(const Driver &D, std::string &BLPath) { |
| 481 | const char *BlacklistFile = nullptr; |
| 482 | if (Sanitizers.has(SanitizerKind::Address)) |
| 483 | BlacklistFile = "asan_blacklist.txt"; |
| 484 | else if (Sanitizers.has(SanitizerKind::Memory)) |
| 485 | BlacklistFile = "msan_blacklist.txt"; |
| 486 | else if (Sanitizers.has(SanitizerKind::Thread)) |
| 487 | BlacklistFile = "tsan_blacklist.txt"; |
| 488 | else if (Sanitizers.has(SanitizerKind::DataFlow)) |
| 489 | BlacklistFile = "dfsan_abilist.txt"; |
| 490 | |
| 491 | if (BlacklistFile) { |
| 492 | SmallString<64> Path(D.ResourceDir); |
| 493 | llvm::sys::path::append(Path, BlacklistFile); |
| 494 | BLPath = Path.str(); |
| 495 | return true; |
| 496 | } |
| 497 | return false; |
| 498 | } |
| 499 | |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 500 | uint64_t parseValue(const char *Value) { |
| 501 | uint64_t ParsedKind = llvm::StringSwitch<SanitizeKind>(Value) |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 502 | #define SANITIZER(NAME, ID) .Case(NAME, ID) |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 503 | #define SANITIZER_GROUP(NAME, ID, ALIAS) .Case(NAME, ID##Group) |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 504 | #include "clang/Basic/Sanitizers.def" |
| 505 | .Default(SanitizeKind()); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 506 | return ParsedKind; |
| 507 | } |
| 508 | |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 509 | uint64_t expandGroups(uint64_t Kinds) { |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 510 | #define SANITIZER(NAME, ID) |
| 511 | #define SANITIZER_GROUP(NAME, ID, ALIAS) if (Kinds & ID##Group) Kinds |= ID; |
| 512 | #include "clang/Basic/Sanitizers.def" |
| 513 | return Kinds; |
| 514 | } |
| 515 | |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 516 | uint64_t parseArgValues(const Driver &D, const llvm::opt::Arg *A, |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 517 | bool DiagnoseErrors) { |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 518 | assert((A->getOption().matches(options::OPT_fsanitize_EQ) || |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 519 | A->getOption().matches(options::OPT_fno_sanitize_EQ) || |
| 520 | A->getOption().matches(options::OPT_fsanitize_recover_EQ) || |
| 521 | A->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) && |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 522 | "Invalid argument in parseArgValues!"); |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 523 | uint64_t Kinds = 0; |
| 524 | for (int i = 0, n = A->getNumValues(); i != n; ++i) { |
| 525 | const char *Value = A->getValue(i); |
| 526 | uint64_t Kind; |
Alexey Samsonov | abd5bea | 2014-12-19 18:41:43 +0000 | [diff] [blame] | 527 | // Special case: don't accept -fsanitize=all. |
| 528 | if (A->getOption().matches(options::OPT_fsanitize_EQ) && |
| 529 | 0 == strcmp("all", Value)) |
| 530 | Kind = 0; |
| 531 | else |
| 532 | Kind = parseValue(Value); |
| 533 | |
| 534 | if (Kind) |
| 535 | Kinds |= Kind; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 536 | else if (DiagnoseErrors) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 537 | D.Diag(clang::diag::err_drv_unsupported_option_argument) |
Alexey Samsonov | abd5bea | 2014-12-19 18:41:43 +0000 | [diff] [blame] | 538 | << A->getOption().getName() << Value; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 539 | } |
Alexey Samsonov | abd5bea | 2014-12-19 18:41:43 +0000 | [diff] [blame] | 540 | return Kinds; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 543 | std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args, |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 544 | uint64_t Mask) { |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 545 | for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(), |
| 546 | E = Args.rend(); |
| 547 | I != E; ++I) { |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 548 | const auto *Arg = *I; |
| 549 | if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) { |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 550 | uint64_t AddKinds = expandGroups(parseArgValues(D, Arg, false)); |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 551 | if (AddKinds & Mask) |
| 552 | return describeSanitizeArg(Arg, Mask); |
| 553 | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) { |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 554 | uint64_t RemoveKinds = expandGroups(parseArgValues(D, Arg, false)); |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 555 | Mask &= ~RemoveKinds; |
| 556 | } |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 557 | } |
| 558 | llvm_unreachable("arg list didn't provide expected value"); |
| 559 | } |
| 560 | |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 561 | std::string describeSanitizeArg(const llvm::opt::Arg *A, uint64_t Mask) { |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 562 | assert(A->getOption().matches(options::OPT_fsanitize_EQ) |
| 563 | && "Invalid argument in describeSanitizerArg!"); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 564 | |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 565 | std::string Sanitizers; |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 566 | for (int i = 0, n = A->getNumValues(); i != n; ++i) { |
| 567 | if (expandGroups(parseValue(A->getValue(i))) & Mask) { |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 568 | if (!Sanitizers.empty()) |
| 569 | Sanitizers += ","; |
Alexey Samsonov | 83791e2 | 2015-03-03 22:15:32 +0000 | [diff] [blame] | 570 | Sanitizers += A->getValue(i); |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 571 | } |
| 572 | } |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 573 | |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 574 | assert(!Sanitizers.empty() && "arg didn't provide expected value"); |
| 575 | return "-fsanitize=" + Sanitizers; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 576 | } |