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. |
| 27 | enum SanitizeOrdinal { |
| 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). |
| 38 | enum SanitizeKind { |
| 39 | #define SANITIZER(NAME, ID) ID = 1 << SO_##ID, |
| 40 | #define SANITIZER_GROUP(NAME, ID, ALIAS) \ |
| 41 | ID = ALIAS, ID##Group = 1 << SO_##ID##Group, |
| 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, |
| 47 | SupportsCoverage = Address | Memory | Leak | Undefined | Integer |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 48 | }; |
| 49 | } |
| 50 | |
| 51 | /// Returns true if set of \p Sanitizers contain at least one sanitizer from |
| 52 | /// \p Kinds. |
| 53 | static bool hasOneOf(const clang::SanitizerSet &Sanitizers, unsigned Kinds) { |
| 54 | #define SANITIZER(NAME, ID) \ |
| 55 | if (Sanitizers.has(clang::SanitizerKind::ID) && (Kinds & ID)) \ |
| 56 | return true; |
| 57 | #include "clang/Basic/Sanitizers.def" |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | /// Adds all sanitizers from \p Kinds to \p Sanitizers. |
| 62 | static void addAllOf(clang::SanitizerSet &Sanitizers, unsigned Kinds) { |
| 63 | #define SANITIZER(NAME, ID) \ |
| 64 | if (Kinds & ID) \ |
| 65 | Sanitizers.set(clang::SanitizerKind::ID, true); |
| 66 | #include "clang/Basic/Sanitizers.def" |
| 67 | } |
| 68 | |
| 69 | static unsigned toSanitizeKind(clang::SanitizerKind K) { |
| 70 | #define SANITIZER(NAME, ID) \ |
| 71 | if (K == clang::SanitizerKind::ID) \ |
| 72 | return ID; |
| 73 | #include "clang/Basic/Sanitizers.def" |
| 74 | llvm_unreachable("Invalid SanitizerKind!"); |
| 75 | } |
| 76 | |
| 77 | /// Parse a single value from a -fsanitize= or -fno-sanitize= value list. |
| 78 | /// Returns a member of the \c SanitizeKind enumeration, or \c 0 |
| 79 | /// if \p Value is not known. |
| 80 | static unsigned parseValue(const char *Value); |
| 81 | |
| 82 | /// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any |
| 83 | /// invalid components. Returns OR of members of \c SanitizeKind enumeration. |
| 84 | static unsigned parseArgValues(const Driver &D, const llvm::opt::Arg *A, |
| 85 | bool DiagnoseErrors); |
| 86 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 87 | /// Produce an argument string from ArgList \p Args, which shows how it |
| 88 | /// provides some sanitizer kind from \p Mask. For example, the argument list |
| 89 | /// "-fsanitize=thread,vptr -fsanitize=address" with mask \c NeedsUbsanRt |
| 90 | /// would produce "-fsanitize=vptr". |
| 91 | static std::string lastArgumentForMask(const Driver &D, |
| 92 | const llvm::opt::ArgList &Args, |
| 93 | unsigned Mask); |
| 94 | |
| 95 | static std::string lastArgumentForKind(const Driver &D, |
| 96 | const llvm::opt::ArgList &Args, |
| 97 | clang::SanitizerKind K) { |
| 98 | return lastArgumentForMask(D, Args, toSanitizeKind(K)); |
| 99 | } |
| 100 | |
| 101 | /// Produce an argument string from argument \p A, which shows how it provides |
| 102 | /// a value in \p Mask. For instance, the argument |
| 103 | /// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce |
| 104 | /// "-fsanitize=alignment". |
| 105 | static std::string describeSanitizeArg(const llvm::opt::Arg *A, unsigned Mask); |
| 106 | |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 107 | /// Produce a string containing comma-separated names of sanitizers in \p |
| 108 | /// Sanitizers set. |
| 109 | static std::string toString(const clang::SanitizerSet &Sanitizers); |
| 110 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 111 | /// For each sanitizer group bit set in \p Kinds, set the bits for sanitizers |
| 112 | /// this group enables. |
| 113 | static unsigned expandGroups(unsigned Kinds); |
| 114 | |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 115 | static unsigned getToolchainUnsupportedKinds(const ToolChain &TC) { |
| 116 | bool IsFreeBSD = TC.getTriple().getOS() == llvm::Triple::FreeBSD; |
| 117 | bool IsLinux = TC.getTriple().getOS() == llvm::Triple::Linux; |
| 118 | bool IsX86 = TC.getTriple().getArch() == llvm::Triple::x86; |
| 119 | bool IsX86_64 = TC.getTriple().getArch() == llvm::Triple::x86_64; |
| 120 | |
| 121 | unsigned Unsupported = 0; |
| 122 | if (!(IsLinux && IsX86_64)) { |
| 123 | Unsupported |= Memory | DataFlow; |
| 124 | } |
| 125 | if (!((IsLinux || IsFreeBSD) && IsX86_64)) { |
| 126 | Unsupported |= Thread; |
| 127 | } |
| 128 | if (!(IsLinux && (IsX86 || IsX86_64))) { |
| 129 | Unsupported |= Function; |
| 130 | } |
| 131 | return Unsupported; |
| 132 | } |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 133 | |
| 134 | bool SanitizerArgs::needsUbsanRt() const { |
| 135 | return !UbsanTrapOnError && hasOneOf(Sanitizers, NeedsUbsanRt); |
| 136 | } |
| 137 | |
Dmitry Vyukov | 43419a7 | 2014-11-21 12:19:01 +0000 | [diff] [blame] | 138 | bool SanitizerArgs::requiresPIE() const { |
| 139 | return AsanZeroBaseShadow || hasOneOf(Sanitizers, RequiresPIE); |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | bool SanitizerArgs::needsUnwindTables() const { |
| 143 | return hasOneOf(Sanitizers, NeedsUnwindTables); |
| 144 | } |
| 145 | |
Alexey Samsonov | bb14f34 | 2013-08-08 11:32:17 +0000 | [diff] [blame] | 146 | void SanitizerArgs::clear() { |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 147 | Sanitizers.clear(); |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 148 | SanitizeRecover = false; |
Alexey Samsonov | bb14f34 | 2013-08-08 11:32:17 +0000 | [diff] [blame] | 149 | BlacklistFile = ""; |
Kostya Serebryany | 75b4f9e | 2014-11-11 22:15:07 +0000 | [diff] [blame] | 150 | SanitizeCoverage = 0; |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 151 | MsanTrackOrigins = 0; |
Kostya Serebryany | aed71a8 | 2014-10-09 17:53:04 +0000 | [diff] [blame] | 152 | AsanFieldPadding = 0; |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 153 | AsanZeroBaseShadow = false; |
Alexey Samsonov | bb14f34 | 2013-08-08 11:32:17 +0000 | [diff] [blame] | 154 | UbsanTrapOnError = false; |
Alexey Samsonov | bdfa6c2 | 2014-04-01 13:31:10 +0000 | [diff] [blame] | 155 | AsanSharedRuntime = false; |
Alexey Samsonov | 90490af | 2014-08-08 22:47:17 +0000 | [diff] [blame] | 156 | LinkCXXRuntimes = false; |
Alexey Samsonov | bb14f34 | 2013-08-08 11:32:17 +0000 | [diff] [blame] | 157 | } |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 158 | |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 159 | SanitizerArgs::SanitizerArgs(const ToolChain &TC, |
| 160 | const llvm::opt::ArgList &Args) { |
Alexey Samsonov | bb14f34 | 2013-08-08 11:32:17 +0000 | [diff] [blame] | 161 | clear(); |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 162 | unsigned AllRemove = 0; // During the loop below, the accumulated set of |
| 163 | // sanitizers disabled by the current sanitizer |
| 164 | // argument or any argument after it. |
| 165 | unsigned DiagnosedKinds = 0; // All Kinds we have diagnosed up to now. |
| 166 | // Used to deduplicate diagnostics. |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 167 | unsigned Kinds = 0; |
| 168 | unsigned NotSupported = getToolchainUnsupportedKinds(TC); |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 169 | const Driver &D = TC.getDriver(); |
| 170 | for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend(); |
| 171 | I != E; ++I) { |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 172 | const auto *Arg = *I; |
| 173 | if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) { |
| 174 | Arg->claim(); |
| 175 | unsigned Add = parseArgValues(D, Arg, true); |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 176 | |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 177 | // Avoid diagnosing any sanitizer which is disabled later. |
| 178 | Add &= ~AllRemove; |
| 179 | // At this point we have not expanded groups, so any unsupported |
| 180 | // sanitizers in Add are those which have been explicitly enabled. |
| 181 | // Diagnose them. |
| 182 | if (unsigned KindsToDiagnose = Add & NotSupported & ~DiagnosedKinds) { |
| 183 | // Only diagnose the new kinds. |
| 184 | std::string Desc = describeSanitizeArg(*I, KindsToDiagnose); |
| 185 | D.Diag(diag::err_drv_unsupported_opt_for_target) |
| 186 | << Desc << TC.getTriple().str(); |
| 187 | DiagnosedKinds |= KindsToDiagnose; |
| 188 | } |
| 189 | Add &= ~NotSupported; |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 190 | |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 191 | Add = expandGroups(Add); |
| 192 | // Group expansion may have enabled a sanitizer which is disabled later. |
| 193 | Add &= ~AllRemove; |
| 194 | // Silently discard any unsupported sanitizers implicitly enabled through |
| 195 | // group expansion. |
| 196 | Add &= ~NotSupported; |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 197 | |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 198 | Kinds |= Add; |
| 199 | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) { |
| 200 | Arg->claim(); |
| 201 | unsigned Remove = parseArgValues(D, Arg, true); |
| 202 | AllRemove |= expandGroups(Remove); |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 203 | } |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 204 | } |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 205 | addAllOf(Sanitizers, Kinds); |
| 206 | |
| 207 | SanitizeRecover = Args.hasFlag(options::OPT_fsanitize_recover, |
| 208 | options::OPT_fno_sanitize_recover, true); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 209 | |
| 210 | UbsanTrapOnError = |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 211 | Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error, |
| 212 | options::OPT_fno_sanitize_undefined_trap_on_error, false); |
| 213 | |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 214 | // Warn about undefined sanitizer options that require runtime support. |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 215 | if (UbsanTrapOnError && hasOneOf(Sanitizers, NotAllowedWithTrap)) { |
| 216 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 217 | << lastArgumentForMask(D, Args, NotAllowedWithTrap) |
Alexey Samsonov | cb3f812 | 2014-03-20 10:48:29 +0000 | [diff] [blame] | 218 | << "-fsanitize-undefined-trap-on-error"; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 221 | // Check for incompatible sanitizers. |
| 222 | bool NeedsAsan = Sanitizers.has(SanitizerKind::Address); |
| 223 | bool NeedsTsan = Sanitizers.has(SanitizerKind::Thread); |
| 224 | bool NeedsMsan = Sanitizers.has(SanitizerKind::Memory); |
| 225 | bool NeedsLsan = Sanitizers.has(SanitizerKind::Leak); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 226 | if (NeedsAsan && NeedsTsan) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 227 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 228 | << lastArgumentForKind(D, Args, SanitizerKind::Address) |
| 229 | << lastArgumentForKind(D, Args, SanitizerKind::Thread); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 230 | if (NeedsAsan && NeedsMsan) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 231 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 232 | << lastArgumentForKind(D, Args, SanitizerKind::Address) |
| 233 | << lastArgumentForKind(D, Args, SanitizerKind::Memory); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 234 | if (NeedsTsan && NeedsMsan) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 235 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 236 | << lastArgumentForKind(D, Args, SanitizerKind::Thread) |
| 237 | << lastArgumentForKind(D, Args, SanitizerKind::Memory); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 238 | if (NeedsLsan && NeedsTsan) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 239 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 240 | << lastArgumentForKind(D, Args, SanitizerKind::Leak) |
| 241 | << lastArgumentForKind(D, Args, SanitizerKind::Thread); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 242 | if (NeedsLsan && NeedsMsan) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 243 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 244 | << lastArgumentForKind(D, Args, SanitizerKind::Leak) |
| 245 | << lastArgumentForKind(D, Args, SanitizerKind::Memory); |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 246 | // FIXME: Currently -fsanitize=leak is silently ignored in the presence of |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 247 | // -fsanitize=address. Perhaps it should print an error, or perhaps |
| 248 | // -f(-no)sanitize=leak should change whether leak detection is enabled by |
| 249 | // default in ASan? |
| 250 | |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 251 | // Parse -f(no-)sanitize-blacklist options. |
| 252 | if (Arg *BLArg = Args.getLastArg(options::OPT_fsanitize_blacklist, |
| 253 | options::OPT_fno_sanitize_blacklist)) { |
| 254 | if (BLArg->getOption().matches(options::OPT_fsanitize_blacklist)) { |
| 255 | std::string BLPath = BLArg->getValue(); |
Alexey Samsonov | 0c127d7 | 2013-08-19 13:59:22 +0000 | [diff] [blame] | 256 | if (llvm::sys::fs::exists(BLPath)) { |
| 257 | // Validate the blacklist format. |
| 258 | std::string BLError; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 259 | std::unique_ptr<llvm::SpecialCaseList> SCL( |
Alexey Samsonov | 0c127d7 | 2013-08-19 13:59:22 +0000 | [diff] [blame] | 260 | llvm::SpecialCaseList::create(BLPath, BLError)); |
| 261 | if (!SCL.get()) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 262 | D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError; |
Alexey Samsonov | 0c127d7 | 2013-08-19 13:59:22 +0000 | [diff] [blame] | 263 | else |
| 264 | BlacklistFile = BLPath; |
| 265 | } else { |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 266 | D.Diag(clang::diag::err_drv_no_such_file) << BLPath; |
Alexey Samsonov | 0c127d7 | 2013-08-19 13:59:22 +0000 | [diff] [blame] | 267 | } |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 268 | } |
| 269 | } else { |
| 270 | // If no -fsanitize-blacklist option is specified, try to look up for |
| 271 | // blacklist in the resource directory. |
| 272 | std::string BLPath; |
Alexey Samsonov | 59f34bb | 2014-11-14 00:46:39 +0000 | [diff] [blame] | 273 | if (getDefaultBlacklist(D, BLPath) && llvm::sys::fs::exists(BLPath)) |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 274 | BlacklistFile = BLPath; |
| 275 | } |
| 276 | |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 277 | // Parse -f[no-]sanitize-memory-track-origins[=level] options. |
| 278 | if (NeedsMsan) { |
| 279 | if (Arg *A = |
| 280 | Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ, |
| 281 | options::OPT_fsanitize_memory_track_origins, |
| 282 | options::OPT_fno_sanitize_memory_track_origins)) { |
| 283 | if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) { |
| 284 | MsanTrackOrigins = 1; |
| 285 | } else if (A->getOption().matches( |
| 286 | options::OPT_fno_sanitize_memory_track_origins)) { |
| 287 | MsanTrackOrigins = 0; |
| 288 | } else { |
| 289 | StringRef S = A->getValue(); |
| 290 | if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 || |
| 291 | MsanTrackOrigins > 2) { |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 292 | D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S; |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
Kostya Serebryany | 78df9d0 | 2014-12-17 21:46:33 +0000 | [diff] [blame] | 298 | // Parse -fsanitize-coverage=N. Currently one of asan/msan/lsan is required. |
Kostya Serebryany | 2d88f3d | 2015-01-06 01:02:48 +0000 | [diff] [blame^] | 299 | if (hasOneOf(Sanitizers, SupportsCoverage)) { |
Kostya Serebryany | 75b4f9e | 2014-11-11 22:15:07 +0000 | [diff] [blame] | 300 | if (Arg *A = Args.getLastArg(options::OPT_fsanitize_coverage)) { |
| 301 | StringRef S = A->getValue(); |
| 302 | // Legal values are 0..4. |
| 303 | if (S.getAsInteger(0, SanitizeCoverage) || SanitizeCoverage < 0 || |
| 304 | SanitizeCoverage > 4) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 305 | D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S; |
Kostya Serebryany | 75b4f9e | 2014-11-11 22:15:07 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Alexey Samsonov | bdfa6c2 | 2014-04-01 13:31:10 +0000 | [diff] [blame] | 309 | if (NeedsAsan) { |
| 310 | AsanSharedRuntime = |
Evgeniy Stepanov | 6f0ae18 | 2014-06-05 11:14:00 +0000 | [diff] [blame] | 311 | Args.hasArg(options::OPT_shared_libasan) || |
| 312 | (TC.getTriple().getEnvironment() == llvm::Triple::Android); |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 313 | AsanZeroBaseShadow = |
Evgeniy Stepanov | d04b861 | 2014-01-16 10:19:31 +0000 | [diff] [blame] | 314 | (TC.getTriple().getEnvironment() == llvm::Triple::Android); |
Kostya Serebryany | aed71a8 | 2014-10-09 17:53:04 +0000 | [diff] [blame] | 315 | if (Arg *A = |
| 316 | Args.getLastArg(options::OPT_fsanitize_address_field_padding)) { |
| 317 | StringRef S = A->getValue(); |
| 318 | // Legal values are 0 and 1, 2, but in future we may add more levels. |
| 319 | if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 || |
| 320 | AsanFieldPadding > 2) { |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 321 | D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S; |
Kostya Serebryany | aed71a8 | 2014-10-09 17:53:04 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
Ehsan Akhgari | e0db196 | 2014-10-14 23:15:44 +0000 | [diff] [blame] | 324 | |
| 325 | if (Arg *WindowsDebugRTArg = |
| 326 | Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT, |
| 327 | options::OPT__SLASH_MDd, options::OPT__SLASH_MD, |
| 328 | options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) { |
| 329 | switch (WindowsDebugRTArg->getOption().getID()) { |
| 330 | case options::OPT__SLASH_MTd: |
| 331 | case options::OPT__SLASH_MDd: |
| 332 | case options::OPT__SLASH_LDd: |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 333 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
Ehsan Akhgari | e0db196 | 2014-10-14 23:15:44 +0000 | [diff] [blame] | 334 | << WindowsDebugRTArg->getAsString(Args) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 335 | << lastArgumentForKind(D, Args, SanitizerKind::Address); |
| 336 | D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime); |
Ehsan Akhgari | e0db196 | 2014-10-14 23:15:44 +0000 | [diff] [blame] | 337 | } |
| 338 | } |
Alexey Samsonov | bdfa6c2 | 2014-04-01 13:31:10 +0000 | [diff] [blame] | 339 | } |
Alexey Samsonov | 90490af | 2014-08-08 22:47:17 +0000 | [diff] [blame] | 340 | |
| 341 | // Parse -link-cxx-sanitizer flag. |
| 342 | LinkCXXRuntimes = |
| 343 | Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX(); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 346 | static std::string toString(const clang::SanitizerSet &Sanitizers) { |
| 347 | std::string Res; |
| 348 | #define SANITIZER(NAME, ID) \ |
| 349 | if (Sanitizers.has(clang::SanitizerKind::ID)) { \ |
| 350 | if (!Res.empty()) \ |
| 351 | Res += ","; \ |
| 352 | Res += NAME; \ |
| 353 | } |
| 354 | #include "clang/Basic/Sanitizers.def" |
| 355 | return Res; |
| 356 | } |
| 357 | |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 358 | void SanitizerArgs::addArgs(const llvm::opt::ArgList &Args, |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 359 | llvm::opt::ArgStringList &CmdArgs) const { |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 360 | if (Sanitizers.empty()) |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 361 | return; |
Alexey Samsonov | 1e715a6 | 2014-11-16 20:53:53 +0000 | [diff] [blame] | 362 | CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers))); |
| 363 | |
| 364 | if (!SanitizeRecover) |
| 365 | CmdArgs.push_back("-fno-sanitize-recover"); |
| 366 | |
| 367 | if (UbsanTrapOnError) |
| 368 | CmdArgs.push_back("-fsanitize-undefined-trap-on-error"); |
| 369 | |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 370 | if (!BlacklistFile.empty()) { |
| 371 | SmallString<64> BlacklistOpt("-fsanitize-blacklist="); |
| 372 | BlacklistOpt += BlacklistFile; |
| 373 | CmdArgs.push_back(Args.MakeArgString(BlacklistOpt)); |
| 374 | } |
| 375 | |
| 376 | if (MsanTrackOrigins) |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 377 | CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" + |
| 378 | llvm::utostr(MsanTrackOrigins))); |
Kostya Serebryany | aed71a8 | 2014-10-09 17:53:04 +0000 | [diff] [blame] | 379 | if (AsanFieldPadding) |
| 380 | CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" + |
| 381 | llvm::utostr(AsanFieldPadding))); |
Kostya Serebryany | 75b4f9e | 2014-11-11 22:15:07 +0000 | [diff] [blame] | 382 | if (SanitizeCoverage) |
| 383 | CmdArgs.push_back(Args.MakeArgString("-fsanitize-coverage=" + |
| 384 | llvm::utostr(SanitizeCoverage))); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 385 | // Workaround for PR16386. |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 386 | if (Sanitizers.has(SanitizerKind::Memory)) |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 387 | CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new")); |
| 388 | } |
| 389 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 390 | bool SanitizerArgs::getDefaultBlacklist(const Driver &D, std::string &BLPath) { |
| 391 | const char *BlacklistFile = nullptr; |
| 392 | if (Sanitizers.has(SanitizerKind::Address)) |
| 393 | BlacklistFile = "asan_blacklist.txt"; |
| 394 | else if (Sanitizers.has(SanitizerKind::Memory)) |
| 395 | BlacklistFile = "msan_blacklist.txt"; |
| 396 | else if (Sanitizers.has(SanitizerKind::Thread)) |
| 397 | BlacklistFile = "tsan_blacklist.txt"; |
| 398 | else if (Sanitizers.has(SanitizerKind::DataFlow)) |
| 399 | BlacklistFile = "dfsan_abilist.txt"; |
| 400 | |
| 401 | if (BlacklistFile) { |
| 402 | SmallString<64> Path(D.ResourceDir); |
| 403 | llvm::sys::path::append(Path, BlacklistFile); |
| 404 | BLPath = Path.str(); |
| 405 | return true; |
| 406 | } |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | unsigned parseValue(const char *Value) { |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 411 | unsigned ParsedKind = llvm::StringSwitch<SanitizeKind>(Value) |
| 412 | #define SANITIZER(NAME, ID) .Case(NAME, ID) |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 413 | #define SANITIZER_GROUP(NAME, ID, ALIAS) .Case(NAME, ID##Group) |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 414 | #include "clang/Basic/Sanitizers.def" |
| 415 | .Default(SanitizeKind()); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 416 | return ParsedKind; |
| 417 | } |
| 418 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 419 | unsigned expandGroups(unsigned Kinds) { |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 420 | #define SANITIZER(NAME, ID) |
| 421 | #define SANITIZER_GROUP(NAME, ID, ALIAS) if (Kinds & ID##Group) Kinds |= ID; |
| 422 | #include "clang/Basic/Sanitizers.def" |
| 423 | return Kinds; |
| 424 | } |
| 425 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 426 | unsigned parseArgValues(const Driver &D, const llvm::opt::Arg *A, |
| 427 | bool DiagnoseErrors) { |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 428 | assert((A->getOption().matches(options::OPT_fsanitize_EQ) || |
| 429 | A->getOption().matches(options::OPT_fno_sanitize_EQ)) && |
| 430 | "Invalid argument in parseArgValues!"); |
Alexey Samsonov | abd5bea | 2014-12-19 18:41:43 +0000 | [diff] [blame] | 431 | unsigned Kinds = 0; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 432 | for (unsigned I = 0, N = A->getNumValues(); I != N; ++I) { |
Alexey Samsonov | abd5bea | 2014-12-19 18:41:43 +0000 | [diff] [blame] | 433 | const char *Value = A->getValue(I); |
| 434 | unsigned Kind; |
| 435 | // Special case: don't accept -fsanitize=all. |
| 436 | if (A->getOption().matches(options::OPT_fsanitize_EQ) && |
| 437 | 0 == strcmp("all", Value)) |
| 438 | Kind = 0; |
| 439 | else |
| 440 | Kind = parseValue(Value); |
| 441 | |
| 442 | if (Kind) |
| 443 | Kinds |= Kind; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 444 | else if (DiagnoseErrors) |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 445 | D.Diag(clang::diag::err_drv_unsupported_option_argument) |
Alexey Samsonov | abd5bea | 2014-12-19 18:41:43 +0000 | [diff] [blame] | 446 | << A->getOption().getName() << Value; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 447 | } |
Alexey Samsonov | abd5bea | 2014-12-19 18:41:43 +0000 | [diff] [blame] | 448 | return Kinds; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 451 | std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args, |
| 452 | unsigned Mask) { |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 453 | for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(), |
| 454 | E = Args.rend(); |
| 455 | I != E; ++I) { |
Alexey Samsonov | 799f793 | 2014-12-19 02:35:16 +0000 | [diff] [blame] | 456 | const auto *Arg = *I; |
| 457 | if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) { |
| 458 | unsigned AddKinds = expandGroups(parseArgValues(D, Arg, false)); |
| 459 | if (AddKinds & Mask) |
| 460 | return describeSanitizeArg(Arg, Mask); |
| 461 | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) { |
| 462 | unsigned RemoveKinds = expandGroups(parseArgValues(D, Arg, false)); |
| 463 | Mask &= ~RemoveKinds; |
| 464 | } |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 465 | } |
| 466 | llvm_unreachable("arg list didn't provide expected value"); |
| 467 | } |
| 468 | |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 469 | std::string describeSanitizeArg(const llvm::opt::Arg *A, unsigned Mask) { |
| 470 | assert(A->getOption().matches(options::OPT_fsanitize_EQ) |
| 471 | && "Invalid argument in describeSanitizerArg!"); |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 472 | |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 473 | std::string Sanitizers; |
| 474 | for (unsigned I = 0, N = A->getNumValues(); I != N; ++I) { |
Alexey Samsonov | 4c12c6c | 2014-11-14 02:59:20 +0000 | [diff] [blame] | 475 | if (expandGroups(parseValue(A->getValue(I))) & Mask) { |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 476 | if (!Sanitizers.empty()) |
| 477 | Sanitizers += ","; |
| 478 | Sanitizers += A->getValue(I); |
| 479 | } |
| 480 | } |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 481 | |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 482 | assert(!Sanitizers.empty() && "arg didn't provide expected value"); |
| 483 | return "-fsanitize=" + Sanitizers; |
Alexey Samsonov | cf05596 | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 484 | } |