Alexey Samsonov | 442c60a | 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 | //===----------------------------------------------------------------------===// |
| 9 | #include "SanitizerArgs.h" |
| 10 | |
| 11 | #include "clang/Driver/Driver.h" |
| 12 | #include "clang/Driver/DriverDiagnostic.h" |
| 13 | #include "clang/Driver/Options.h" |
| 14 | #include "clang/Driver/ToolChain.h" |
| 15 | #include "llvm/ADT/StringSwitch.h" |
| 16 | #include "llvm/Support/FileSystem.h" |
| 17 | #include "llvm/Support/Path.h" |
| 18 | |
| 19 | using namespace clang::driver; |
| 20 | using namespace llvm::opt; |
| 21 | |
Alexey Samsonov | 1e9fdc1 | 2013-08-08 11:32:17 +0000 | [diff] [blame] | 22 | void SanitizerArgs::clear() { |
| 23 | Kind = 0; |
| 24 | BlacklistFile = ""; |
| 25 | MsanTrackOrigins = false; |
Alexey Samsonov | 8bdc92c | 2013-08-09 07:42:13 +0000 | [diff] [blame] | 26 | AsanZeroBaseShadow = AZBSK_Default; |
Alexey Samsonov | 1e9fdc1 | 2013-08-08 11:32:17 +0000 | [diff] [blame] | 27 | UbsanTrapOnError = false; |
| 28 | } |
| 29 | |
| 30 | SanitizerArgs::SanitizerArgs() { |
| 31 | clear(); |
| 32 | } |
Alexey Samsonov | 442c60a | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 33 | |
Alexey Samsonov | 8bdc92c | 2013-08-09 07:42:13 +0000 | [diff] [blame] | 34 | SanitizerArgs::SanitizerArgs(const Driver &D, |
Alexey Samsonov | 1e9fdc1 | 2013-08-08 11:32:17 +0000 | [diff] [blame] | 35 | const llvm::opt::ArgList &Args) { |
| 36 | clear(); |
Alexey Samsonov | 8bdc92c | 2013-08-09 07:42:13 +0000 | [diff] [blame] | 37 | parse(D, Args); |
Alexey Samsonov | 1e9fdc1 | 2013-08-08 11:32:17 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Alexey Samsonov | 8bdc92c | 2013-08-09 07:42:13 +0000 | [diff] [blame] | 40 | void SanitizerArgs::parse(const Driver &D, |
Alexey Samsonov | 1e9fdc1 | 2013-08-08 11:32:17 +0000 | [diff] [blame] | 41 | const llvm::opt::ArgList &Args) { |
Alexey Samsonov | 442c60a | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 42 | unsigned AllKinds = 0; // All kinds of sanitizers that were turned on |
| 43 | // at least once (possibly, disabled further). |
Alexey Samsonov | 442c60a | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 44 | for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I) { |
| 45 | unsigned Add, Remove; |
| 46 | if (!parse(D, Args, *I, Add, Remove, true)) |
| 47 | continue; |
| 48 | (*I)->claim(); |
| 49 | Kind |= Add; |
| 50 | Kind &= ~Remove; |
| 51 | AllKinds |= Add; |
| 52 | } |
| 53 | |
| 54 | UbsanTrapOnError = |
| 55 | Args.hasArg(options::OPT_fcatch_undefined_behavior) || |
| 56 | Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error, |
| 57 | options::OPT_fno_sanitize_undefined_trap_on_error, false); |
| 58 | |
| 59 | if (Args.hasArg(options::OPT_fcatch_undefined_behavior) && |
| 60 | !Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error, |
| 61 | options::OPT_fno_sanitize_undefined_trap_on_error, true)) { |
| 62 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 63 | << "-fcatch-undefined-behavior" |
| 64 | << "-fno-sanitize-undefined-trap-on-error"; |
| 65 | } |
| 66 | |
| 67 | // Warn about undefined sanitizer options that require runtime support. |
| 68 | if (UbsanTrapOnError && notAllowedWithTrap()) { |
| 69 | if (Args.hasArg(options::OPT_fcatch_undefined_behavior)) |
| 70 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 71 | << lastArgumentForKind(D, Args, NotAllowedWithTrap) |
| 72 | << "-fcatch-undefined-behavior"; |
| 73 | else if (Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error, |
| 74 | options::OPT_fno_sanitize_undefined_trap_on_error, |
| 75 | false)) |
| 76 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 77 | << lastArgumentForKind(D, Args, NotAllowedWithTrap) |
| 78 | << "-fsanitize-undefined-trap-on-error"; |
| 79 | } |
| 80 | |
| 81 | // Only one runtime library can be used at once. |
| 82 | bool NeedsAsan = needsAsanRt(); |
| 83 | bool NeedsTsan = needsTsanRt(); |
| 84 | bool NeedsMsan = needsMsanRt(); |
| 85 | bool NeedsLsan = needsLeakDetection(); |
| 86 | if (NeedsAsan && NeedsTsan) |
| 87 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 88 | << lastArgumentForKind(D, Args, NeedsAsanRt) |
| 89 | << lastArgumentForKind(D, Args, NeedsTsanRt); |
| 90 | if (NeedsAsan && NeedsMsan) |
| 91 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 92 | << lastArgumentForKind(D, Args, NeedsAsanRt) |
| 93 | << lastArgumentForKind(D, Args, NeedsMsanRt); |
| 94 | if (NeedsTsan && NeedsMsan) |
| 95 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 96 | << lastArgumentForKind(D, Args, NeedsTsanRt) |
| 97 | << lastArgumentForKind(D, Args, NeedsMsanRt); |
| 98 | if (NeedsLsan && NeedsTsan) |
| 99 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 100 | << lastArgumentForKind(D, Args, NeedsLeakDetection) |
| 101 | << lastArgumentForKind(D, Args, NeedsTsanRt); |
| 102 | if (NeedsLsan && NeedsMsan) |
| 103 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 104 | << lastArgumentForKind(D, Args, NeedsLeakDetection) |
| 105 | << lastArgumentForKind(D, Args, NeedsMsanRt); |
| 106 | // FIXME: Currenly -fsanitize=leak is silently ignored in the presence of |
| 107 | // -fsanitize=address. Perhaps it should print an error, or perhaps |
| 108 | // -f(-no)sanitize=leak should change whether leak detection is enabled by |
| 109 | // default in ASan? |
| 110 | |
| 111 | // If -fsanitize contains extra features of ASan, it should also |
| 112 | // explicitly contain -fsanitize=address (probably, turned off later in the |
| 113 | // command line). |
| 114 | if ((Kind & AddressFull) != 0 && (AllKinds & Address) == 0) |
| 115 | D.Diag(diag::warn_drv_unused_sanitizer) |
| 116 | << lastArgumentForKind(D, Args, AddressFull) |
| 117 | << "-fsanitize=address"; |
| 118 | |
| 119 | // Parse -f(no-)sanitize-blacklist options. |
| 120 | if (Arg *BLArg = Args.getLastArg(options::OPT_fsanitize_blacklist, |
| 121 | options::OPT_fno_sanitize_blacklist)) { |
| 122 | if (BLArg->getOption().matches(options::OPT_fsanitize_blacklist)) { |
| 123 | std::string BLPath = BLArg->getValue(); |
| 124 | if (llvm::sys::fs::exists(BLPath)) |
| 125 | BlacklistFile = BLPath; |
| 126 | else |
| 127 | D.Diag(diag::err_drv_no_such_file) << BLPath; |
| 128 | } |
| 129 | } else { |
| 130 | // If no -fsanitize-blacklist option is specified, try to look up for |
| 131 | // blacklist in the resource directory. |
| 132 | std::string BLPath; |
| 133 | if (getDefaultBlacklistForKind(D, Kind, BLPath) && |
| 134 | llvm::sys::fs::exists(BLPath)) |
| 135 | BlacklistFile = BLPath; |
| 136 | } |
| 137 | |
| 138 | // Parse -f(no-)sanitize-memory-track-origins options. |
| 139 | if (NeedsMsan) |
| 140 | MsanTrackOrigins = |
| 141 | Args.hasFlag(options::OPT_fsanitize_memory_track_origins, |
| 142 | options::OPT_fno_sanitize_memory_track_origins, |
| 143 | /* Default */false); |
| 144 | |
| 145 | // Parse -f(no-)sanitize-address-zero-base-shadow options. |
| 146 | if (NeedsAsan) { |
Alexey Samsonov | 8bdc92c | 2013-08-09 07:42:13 +0000 | [diff] [blame] | 147 | if (Arg *A = Args.getLastArg( |
| 148 | options::OPT_fsanitize_address_zero_base_shadow, |
| 149 | options::OPT_fno_sanitize_address_zero_base_shadow)) |
| 150 | AsanZeroBaseShadow = A->getOption().matches( |
| 151 | options::OPT_fsanitize_address_zero_base_shadow) |
| 152 | ? AZBSK_On |
| 153 | : AZBSK_Off; |
Alexey Samsonov | 442c60a | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Alexey Samsonov | 8bdc92c | 2013-08-09 07:42:13 +0000 | [diff] [blame] | 157 | void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, |
Alexey Samsonov | 442c60a | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 158 | llvm::opt::ArgStringList &CmdArgs) const { |
| 159 | if (!Kind) |
| 160 | return; |
Alexey Samsonov | 8bdc92c | 2013-08-09 07:42:13 +0000 | [diff] [blame] | 161 | const Driver &D = TC.getDriver(); |
Alexey Samsonov | 442c60a | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 162 | SmallString<256> SanitizeOpt("-fsanitize="); |
| 163 | #define SANITIZER(NAME, ID) \ |
| 164 | if (Kind & ID) \ |
| 165 | SanitizeOpt += NAME ","; |
| 166 | #include "clang/Basic/Sanitizers.def" |
| 167 | SanitizeOpt.pop_back(); |
| 168 | CmdArgs.push_back(Args.MakeArgString(SanitizeOpt)); |
| 169 | if (!BlacklistFile.empty()) { |
| 170 | SmallString<64> BlacklistOpt("-fsanitize-blacklist="); |
| 171 | BlacklistOpt += BlacklistFile; |
| 172 | CmdArgs.push_back(Args.MakeArgString(BlacklistOpt)); |
| 173 | } |
| 174 | |
| 175 | if (MsanTrackOrigins) |
| 176 | CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins")); |
| 177 | |
Alexey Samsonov | 8bdc92c | 2013-08-09 07:42:13 +0000 | [diff] [blame] | 178 | if (needsAsanRt()) { |
| 179 | if (hasAsanZeroBaseShadow(TC)) { |
| 180 | CmdArgs.push_back( |
| 181 | Args.MakeArgString("-fsanitize-address-zero-base-shadow")); |
| 182 | } else if (TC.getTriple().getEnvironment() == llvm::Triple::Android) { |
| 183 | // Zero-base shadow is a requirement on Android. |
| 184 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 185 | << "-fno-sanitize-address-zero-base-shadow" |
| 186 | << lastArgumentForKind(D, Args, Address); |
| 187 | } |
| 188 | } |
Alexey Samsonov | 442c60a | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 189 | |
| 190 | // Workaround for PR16386. |
| 191 | if (needsMsanRt()) |
| 192 | CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new")); |
| 193 | } |
| 194 | |
Alexey Samsonov | 8bdc92c | 2013-08-09 07:42:13 +0000 | [diff] [blame] | 195 | bool SanitizerArgs::hasAsanZeroBaseShadow(const ToolChain &TC) const { |
Alexey Samsonov | 48d0f07 | 2013-08-09 10:56:42 +0000 | [diff] [blame] | 196 | if (!needsAsanRt()) |
| 197 | return false; |
Alexey Samsonov | 8bdc92c | 2013-08-09 07:42:13 +0000 | [diff] [blame] | 198 | if (AsanZeroBaseShadow != AZBSK_Default) |
| 199 | return AsanZeroBaseShadow == AZBSK_On; |
| 200 | // Zero-base shadow is used by default only on Android. |
| 201 | return TC.getTriple().getEnvironment() == llvm::Triple::Android; |
| 202 | } |
| 203 | |
Alexey Samsonov | 442c60a | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 204 | unsigned SanitizerArgs::parse(const char *Value) { |
| 205 | unsigned ParsedKind = llvm::StringSwitch<SanitizeKind>(Value) |
| 206 | #define SANITIZER(NAME, ID) .Case(NAME, ID) |
| 207 | #define SANITIZER_GROUP(NAME, ID, ALIAS) .Case(NAME, ID) |
| 208 | #include "clang/Basic/Sanitizers.def" |
| 209 | .Default(SanitizeKind()); |
| 210 | // Assume -fsanitize=address implies -fsanitize=init-order. |
| 211 | // FIXME: This should be either specified in Sanitizers.def, or go away when |
| 212 | // we get rid of "-fsanitize=init-order" flag at all. |
| 213 | if (ParsedKind & Address) |
| 214 | ParsedKind |= InitOrder; |
| 215 | return ParsedKind; |
| 216 | } |
| 217 | |
| 218 | unsigned SanitizerArgs::parse(const Driver &D, const llvm::opt::Arg *A, |
| 219 | bool DiagnoseErrors) { |
| 220 | unsigned Kind = 0; |
| 221 | for (unsigned I = 0, N = A->getNumValues(); I != N; ++I) { |
| 222 | if (unsigned K = parse(A->getValue(I))) |
| 223 | Kind |= K; |
| 224 | else if (DiagnoseErrors) |
| 225 | D.Diag(diag::err_drv_unsupported_option_argument) |
| 226 | << A->getOption().getName() << A->getValue(I); |
| 227 | } |
| 228 | return Kind; |
| 229 | } |
| 230 | |
| 231 | bool SanitizerArgs::parse(const Driver &D, const llvm::opt::ArgList &Args, |
| 232 | const llvm::opt::Arg *A, unsigned &Add, |
| 233 | unsigned &Remove, bool DiagnoseErrors) { |
| 234 | Add = 0; |
| 235 | Remove = 0; |
| 236 | const char *DeprecatedReplacement = 0; |
| 237 | if (A->getOption().matches(options::OPT_faddress_sanitizer)) { |
| 238 | Add = Address; |
| 239 | DeprecatedReplacement = "-fsanitize=address"; |
| 240 | } else if (A->getOption().matches(options::OPT_fno_address_sanitizer)) { |
| 241 | Remove = Address; |
| 242 | DeprecatedReplacement = "-fno-sanitize=address"; |
| 243 | } else if (A->getOption().matches(options::OPT_fthread_sanitizer)) { |
| 244 | Add = Thread; |
| 245 | DeprecatedReplacement = "-fsanitize=thread"; |
| 246 | } else if (A->getOption().matches(options::OPT_fno_thread_sanitizer)) { |
| 247 | Remove = Thread; |
| 248 | DeprecatedReplacement = "-fno-sanitize=thread"; |
| 249 | } else if (A->getOption().matches(options::OPT_fcatch_undefined_behavior)) { |
| 250 | Add = UndefinedTrap; |
| 251 | DeprecatedReplacement = |
| 252 | "-fsanitize=undefined-trap -fsanitize-undefined-trap-on-error"; |
| 253 | } else if (A->getOption().matches(options::OPT_fbounds_checking) || |
| 254 | A->getOption().matches(options::OPT_fbounds_checking_EQ)) { |
| 255 | Add = Bounds; |
| 256 | DeprecatedReplacement = "-fsanitize=bounds"; |
| 257 | } else if (A->getOption().matches(options::OPT_fsanitize_EQ)) { |
| 258 | Add = parse(D, A, DiagnoseErrors); |
| 259 | } else if (A->getOption().matches(options::OPT_fno_sanitize_EQ)) { |
| 260 | Remove = parse(D, A, DiagnoseErrors); |
| 261 | } else { |
| 262 | // Flag is not relevant to sanitizers. |
| 263 | return false; |
| 264 | } |
| 265 | // If this is a deprecated synonym, produce a warning directing users |
| 266 | // towards the new spelling. |
| 267 | if (DeprecatedReplacement && DiagnoseErrors) |
| 268 | D.Diag(diag::warn_drv_deprecated_arg) |
| 269 | << A->getAsString(Args) << DeprecatedReplacement; |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | std::string SanitizerArgs::lastArgumentForKind(const Driver &D, |
| 274 | const llvm::opt::ArgList &Args, |
| 275 | unsigned Kind) { |
| 276 | for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(), |
| 277 | E = Args.rend(); |
| 278 | I != E; ++I) { |
| 279 | unsigned Add, Remove; |
| 280 | if (parse(D, Args, *I, Add, Remove, false) && |
| 281 | (Add & Kind)) |
| 282 | return describeSanitizeArg(Args, *I, Kind); |
| 283 | Kind &= ~Remove; |
| 284 | } |
| 285 | llvm_unreachable("arg list didn't provide expected value"); |
| 286 | } |
| 287 | |
| 288 | std::string SanitizerArgs::describeSanitizeArg(const llvm::opt::ArgList &Args, |
| 289 | const llvm::opt::Arg *A, |
| 290 | unsigned Mask) { |
| 291 | if (!A->getOption().matches(options::OPT_fsanitize_EQ)) |
| 292 | return A->getAsString(Args); |
| 293 | |
| 294 | for (unsigned I = 0, N = A->getNumValues(); I != N; ++I) |
| 295 | if (parse(A->getValue(I)) & Mask) |
| 296 | return std::string("-fsanitize=") + A->getValue(I); |
| 297 | |
| 298 | llvm_unreachable("arg didn't provide expected value"); |
| 299 | } |
| 300 | |
| 301 | bool SanitizerArgs::getDefaultBlacklistForKind(const Driver &D, unsigned Kind, |
| 302 | std::string &BLPath) { |
| 303 | const char *BlacklistFile = 0; |
| 304 | if (Kind & NeedsAsanRt) |
| 305 | BlacklistFile = "asan_blacklist.txt"; |
| 306 | else if (Kind & NeedsMsanRt) |
| 307 | BlacklistFile = "msan_blacklist.txt"; |
| 308 | else if (Kind & NeedsTsanRt) |
| 309 | BlacklistFile = "tsan_blacklist.txt"; |
Peter Collingbourne | 5d27a51 | 2013-08-14 18:54:18 +0000 | [diff] [blame^] | 310 | else if (Kind & NeedsDfsanRt) |
| 311 | BlacklistFile = "dfsan_abilist.txt"; |
| 312 | |
Alexey Samsonov | 442c60a | 2013-08-08 10:11:02 +0000 | [diff] [blame] | 313 | if (BlacklistFile) { |
| 314 | SmallString<64> Path(D.ResourceDir); |
| 315 | llvm::sys::path::append(Path, BlacklistFile); |
| 316 | BLPath = Path.str(); |
| 317 | return true; |
| 318 | } |
| 319 | return false; |
| 320 | } |