blob: 310f5e81f951e5548257189d61e8ca363dd59ec7 [file] [log] [blame]
Alexey Samsonovcf055962013-08-08 10:11:02 +00001//===--- 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 Samsonov609213f92013-08-19 09:14:21 +00009#include "clang/Driver/SanitizerArgs.h"
Alexey Samsonovcf055962013-08-08 10:11:02 +000010#include "clang/Driver/Driver.h"
11#include "clang/Driver/DriverDiagnostic.h"
12#include "clang/Driver/Options.h"
13#include "clang/Driver/ToolChain.h"
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +000014#include "llvm/ADT/StringExtras.h"
Alexey Samsonovcf055962013-08-08 10:11:02 +000015#include "llvm/ADT/StringSwitch.h"
16#include "llvm/Support/FileSystem.h"
17#include "llvm/Support/Path.h"
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000018#include "llvm/Support/SpecialCaseList.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000019#include <memory>
Alexey Samsonovcf055962013-08-08 10:11:02 +000020
21using namespace clang::driver;
22using namespace llvm::opt;
23
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000024namespace {
25/// Assign ordinals to possible values of -fsanitize= flag.
26/// We use the ordinal values as bit positions within \c SanitizeKind.
27enum 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).
38enum SanitizeKind {
39#define SANITIZER(NAME, ID) ID = 1 << SO_##ID,
40#define SANITIZER_GROUP(NAME, ID, ALIAS) \
41ID = ALIAS, ID##Group = 1 << SO_##ID##Group,
42#include "clang/Basic/Sanitizers.def"
43 NeedsUbsanRt = Undefined | Integer,
44 NotAllowedWithTrap = Vptr,
Dmitry Vyukov43419a72014-11-21 12:19:01 +000045 RequiresPIE = Memory | DataFlow,
Kostya Serebryany2d88f3d2015-01-06 01:02:48 +000046 NeedsUnwindTables = Address | Thread | Memory | DataFlow,
Alexey Samsonov88459522015-01-12 22:39:12 +000047 SupportsCoverage = Address | Memory | Leak | Undefined | Integer,
48 RecoverableByDefault = Undefined | Integer,
49 Unrecoverable = Address | Unreachable | Return,
50 LegacyFsanitizeRecoverMask = Undefined | Integer
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000051};
52}
53
54/// Returns true if set of \p Sanitizers contain at least one sanitizer from
55/// \p Kinds.
56static bool hasOneOf(const clang::SanitizerSet &Sanitizers, unsigned Kinds) {
57#define SANITIZER(NAME, ID) \
58 if (Sanitizers.has(clang::SanitizerKind::ID) && (Kinds & ID)) \
59 return true;
60#include "clang/Basic/Sanitizers.def"
61 return false;
62}
63
64/// Adds all sanitizers from \p Kinds to \p Sanitizers.
65static void addAllOf(clang::SanitizerSet &Sanitizers, unsigned Kinds) {
66#define SANITIZER(NAME, ID) \
67 if (Kinds & ID) \
68 Sanitizers.set(clang::SanitizerKind::ID, true);
69#include "clang/Basic/Sanitizers.def"
70}
71
72static unsigned toSanitizeKind(clang::SanitizerKind K) {
73#define SANITIZER(NAME, ID) \
74 if (K == clang::SanitizerKind::ID) \
75 return ID;
76#include "clang/Basic/Sanitizers.def"
77 llvm_unreachable("Invalid SanitizerKind!");
78}
79
80/// Parse a single value from a -fsanitize= or -fno-sanitize= value list.
81/// Returns a member of the \c SanitizeKind enumeration, or \c 0
82/// if \p Value is not known.
83static unsigned parseValue(const char *Value);
84
85/// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any
86/// invalid components. Returns OR of members of \c SanitizeKind enumeration.
87static unsigned parseArgValues(const Driver &D, const llvm::opt::Arg *A,
88 bool DiagnoseErrors);
89
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000090/// Produce an argument string from ArgList \p Args, which shows how it
91/// provides some sanitizer kind from \p Mask. For example, the argument list
92/// "-fsanitize=thread,vptr -fsanitize=address" with mask \c NeedsUbsanRt
93/// would produce "-fsanitize=vptr".
94static std::string lastArgumentForMask(const Driver &D,
95 const llvm::opt::ArgList &Args,
96 unsigned Mask);
97
98static std::string lastArgumentForKind(const Driver &D,
99 const llvm::opt::ArgList &Args,
100 clang::SanitizerKind K) {
101 return lastArgumentForMask(D, Args, toSanitizeKind(K));
102}
103
104/// Produce an argument string from argument \p A, which shows how it provides
105/// a value in \p Mask. For instance, the argument
106/// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce
107/// "-fsanitize=alignment".
108static std::string describeSanitizeArg(const llvm::opt::Arg *A, unsigned Mask);
109
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000110/// Produce a string containing comma-separated names of sanitizers in \p
111/// Sanitizers set.
112static std::string toString(const clang::SanitizerSet &Sanitizers);
113
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000114/// For each sanitizer group bit set in \p Kinds, set the bits for sanitizers
115/// this group enables.
116static unsigned expandGroups(unsigned Kinds);
117
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000118static unsigned getToolchainUnsupportedKinds(const ToolChain &TC) {
119 bool IsFreeBSD = TC.getTriple().getOS() == llvm::Triple::FreeBSD;
120 bool IsLinux = TC.getTriple().getOS() == llvm::Triple::Linux;
121 bool IsX86 = TC.getTriple().getArch() == llvm::Triple::x86;
122 bool IsX86_64 = TC.getTriple().getArch() == llvm::Triple::x86_64;
Mohit K. Bhakkadf4c47f62015-01-22 07:21:22 +0000123 bool IsMIPS64 = TC.getTriple().getArch() == llvm::Triple::mips64 ||
124 TC.getTriple().getArch() == llvm::Triple::mips64el;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000125
126 unsigned Unsupported = 0;
Mohit K. Bhakkadf4c47f62015-01-22 07:21:22 +0000127 if (!(IsLinux && (IsX86_64 || IsMIPS64))) {
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000128 Unsupported |= Memory | DataFlow;
129 }
130 if (!((IsLinux || IsFreeBSD) && IsX86_64)) {
131 Unsupported |= Thread;
132 }
133 if (!(IsLinux && (IsX86 || IsX86_64))) {
134 Unsupported |= Function;
135 }
136 return Unsupported;
137}
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000138
139bool SanitizerArgs::needsUbsanRt() const {
140 return !UbsanTrapOnError && hasOneOf(Sanitizers, NeedsUbsanRt);
141}
142
Dmitry Vyukov43419a72014-11-21 12:19:01 +0000143bool SanitizerArgs::requiresPIE() const {
144 return AsanZeroBaseShadow || hasOneOf(Sanitizers, RequiresPIE);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000145}
146
147bool SanitizerArgs::needsUnwindTables() const {
148 return hasOneOf(Sanitizers, NeedsUnwindTables);
149}
150
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000151void SanitizerArgs::clear() {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000152 Sanitizers.clear();
Alexey Samsonov88459522015-01-12 22:39:12 +0000153 RecoverableSanitizers.clear();
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000154 BlacklistFiles.clear();
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000155 SanitizeCoverage = 0;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000156 MsanTrackOrigins = 0;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000157 AsanFieldPadding = 0;
Peter Collingbourne32701642013-11-01 18:16:25 +0000158 AsanZeroBaseShadow = false;
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000159 UbsanTrapOnError = false;
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000160 AsanSharedRuntime = false;
Alexey Samsonov90490af2014-08-08 22:47:17 +0000161 LinkCXXRuntimes = false;
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000162}
Alexey Samsonovcf055962013-08-08 10:11:02 +0000163
Peter Collingbourne32701642013-11-01 18:16:25 +0000164SanitizerArgs::SanitizerArgs(const ToolChain &TC,
165 const llvm::opt::ArgList &Args) {
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000166 clear();
Peter Collingbourne32701642013-11-01 18:16:25 +0000167 unsigned AllRemove = 0; // During the loop below, the accumulated set of
168 // sanitizers disabled by the current sanitizer
169 // argument or any argument after it.
170 unsigned DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
171 // Used to deduplicate diagnostics.
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000172 unsigned Kinds = 0;
173 unsigned NotSupported = getToolchainUnsupportedKinds(TC);
Peter Collingbourne32701642013-11-01 18:16:25 +0000174 const Driver &D = TC.getDriver();
175 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
176 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000177 const auto *Arg = *I;
178 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
179 Arg->claim();
180 unsigned Add = parseArgValues(D, Arg, true);
Peter Collingbourne32701642013-11-01 18:16:25 +0000181
Alexey Samsonov799f7932014-12-19 02:35:16 +0000182 // Avoid diagnosing any sanitizer which is disabled later.
183 Add &= ~AllRemove;
184 // At this point we have not expanded groups, so any unsupported
185 // sanitizers in Add are those which have been explicitly enabled.
186 // Diagnose them.
187 if (unsigned KindsToDiagnose = Add & NotSupported & ~DiagnosedKinds) {
188 // Only diagnose the new kinds.
189 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
190 D.Diag(diag::err_drv_unsupported_opt_for_target)
191 << Desc << TC.getTriple().str();
192 DiagnosedKinds |= KindsToDiagnose;
193 }
194 Add &= ~NotSupported;
Peter Collingbourne32701642013-11-01 18:16:25 +0000195
Alexey Samsonov799f7932014-12-19 02:35:16 +0000196 Add = expandGroups(Add);
197 // Group expansion may have enabled a sanitizer which is disabled later.
198 Add &= ~AllRemove;
199 // Silently discard any unsupported sanitizers implicitly enabled through
200 // group expansion.
201 Add &= ~NotSupported;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000202
Alexey Samsonov799f7932014-12-19 02:35:16 +0000203 Kinds |= Add;
204 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
205 Arg->claim();
206 unsigned Remove = parseArgValues(D, Arg, true);
207 AllRemove |= expandGroups(Remove);
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000208 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000209 }
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000210 addAllOf(Sanitizers, Kinds);
211
Alexey Samsonov88459522015-01-12 22:39:12 +0000212 // Parse -f(no-)?sanitize-recover flags.
213 unsigned RecoverableKinds = RecoverableByDefault;
214 unsigned DiagnosedUnrecoverableKinds = 0;
215 for (const auto *Arg : Args) {
216 if (Arg->getOption().matches(options::OPT_fsanitize_recover)) {
217 // FIXME: Add deprecation notice, and then remove this flag.
218 RecoverableKinds |= expandGroups(LegacyFsanitizeRecoverMask);
219 Arg->claim();
220 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) {
221 // FIXME: Add deprecation notice, and then remove this flag.
222 RecoverableKinds &= ~expandGroups(LegacyFsanitizeRecoverMask);
223 Arg->claim();
224 } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
225 unsigned Add = parseArgValues(D, Arg, true);
226 // Report error if user explicitly tries to recover from unrecoverable
227 // sanitizer.
228 if (unsigned KindsToDiagnose =
229 Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
230 SanitizerSet SetToDiagnose;
231 addAllOf(SetToDiagnose, KindsToDiagnose);
232 D.Diag(diag::err_drv_unsupported_option_argument)
233 << Arg->getOption().getName() << toString(SetToDiagnose);
234 DiagnosedUnrecoverableKinds |= KindsToDiagnose;
235 }
236 RecoverableKinds |= expandGroups(Add);
237 Arg->claim();
238 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
239 RecoverableKinds &= ~expandGroups(parseArgValues(D, Arg, true));
240 Arg->claim();
241 }
242 }
243 RecoverableKinds &= Kinds;
244 RecoverableKinds &= ~Unrecoverable;
245 addAllOf(RecoverableSanitizers, RecoverableKinds);
Alexey Samsonovcf055962013-08-08 10:11:02 +0000246
247 UbsanTrapOnError =
Alexey Samsonovcf055962013-08-08 10:11:02 +0000248 Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
249 options::OPT_fno_sanitize_undefined_trap_on_error, false);
250
Alexey Samsonovcf055962013-08-08 10:11:02 +0000251 // Warn about undefined sanitizer options that require runtime support.
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000252 if (UbsanTrapOnError && hasOneOf(Sanitizers, NotAllowedWithTrap)) {
253 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
254 << lastArgumentForMask(D, Args, NotAllowedWithTrap)
Alexey Samsonovcb3f8122014-03-20 10:48:29 +0000255 << "-fsanitize-undefined-trap-on-error";
Alexey Samsonovcf055962013-08-08 10:11:02 +0000256 }
257
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000258 // Check for incompatible sanitizers.
259 bool NeedsAsan = Sanitizers.has(SanitizerKind::Address);
260 bool NeedsTsan = Sanitizers.has(SanitizerKind::Thread);
261 bool NeedsMsan = Sanitizers.has(SanitizerKind::Memory);
262 bool NeedsLsan = Sanitizers.has(SanitizerKind::Leak);
Alexey Samsonovcf055962013-08-08 10:11:02 +0000263 if (NeedsAsan && NeedsTsan)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000264 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
265 << lastArgumentForKind(D, Args, SanitizerKind::Address)
266 << lastArgumentForKind(D, Args, SanitizerKind::Thread);
Alexey Samsonovcf055962013-08-08 10:11:02 +0000267 if (NeedsAsan && NeedsMsan)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000268 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
269 << lastArgumentForKind(D, Args, SanitizerKind::Address)
270 << lastArgumentForKind(D, Args, SanitizerKind::Memory);
Alexey Samsonovcf055962013-08-08 10:11:02 +0000271 if (NeedsTsan && NeedsMsan)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000272 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
273 << lastArgumentForKind(D, Args, SanitizerKind::Thread)
274 << lastArgumentForKind(D, Args, SanitizerKind::Memory);
Alexey Samsonovcf055962013-08-08 10:11:02 +0000275 if (NeedsLsan && NeedsTsan)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000276 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
277 << lastArgumentForKind(D, Args, SanitizerKind::Leak)
278 << lastArgumentForKind(D, Args, SanitizerKind::Thread);
Alexey Samsonovcf055962013-08-08 10:11:02 +0000279 if (NeedsLsan && NeedsMsan)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000280 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
281 << lastArgumentForKind(D, Args, SanitizerKind::Leak)
282 << lastArgumentForKind(D, Args, SanitizerKind::Memory);
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000283 // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
Alexey Samsonovcf055962013-08-08 10:11:02 +0000284 // -fsanitize=address. Perhaps it should print an error, or perhaps
285 // -f(-no)sanitize=leak should change whether leak detection is enabled by
286 // default in ASan?
287
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000288 // Setup blacklist files.
289 // Add default blacklist from resource directory.
290 {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000291 std::string BLPath;
Alexey Samsonov59f34bb2014-11-14 00:46:39 +0000292 if (getDefaultBlacklist(D, BLPath) && llvm::sys::fs::exists(BLPath))
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000293 BlacklistFiles.push_back(BLPath);
294 }
295 // Parse -f(no-)sanitize-blacklist options.
296 for (const auto *Arg : Args) {
297 if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) {
298 Arg->claim();
299 std::string BLPath = Arg->getValue();
300 if (llvm::sys::fs::exists(BLPath))
301 BlacklistFiles.push_back(BLPath);
302 else
303 D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
304 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) {
305 Arg->claim();
306 BlacklistFiles.clear();
307 }
308 }
309 // Validate blacklists format.
310 {
311 std::string BLError;
312 std::unique_ptr<llvm::SpecialCaseList> SCL(
313 llvm::SpecialCaseList::create(BlacklistFiles, BLError));
314 if (!SCL.get())
315 D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000316 }
317
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000318 // Parse -f[no-]sanitize-memory-track-origins[=level] options.
319 if (NeedsMsan) {
320 if (Arg *A =
321 Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
322 options::OPT_fsanitize_memory_track_origins,
323 options::OPT_fno_sanitize_memory_track_origins)) {
324 if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
325 MsanTrackOrigins = 1;
326 } else if (A->getOption().matches(
327 options::OPT_fno_sanitize_memory_track_origins)) {
328 MsanTrackOrigins = 0;
329 } else {
330 StringRef S = A->getValue();
331 if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
332 MsanTrackOrigins > 2) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000333 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000334 }
335 }
336 }
337 }
338
Kostya Serebryany78df9d02014-12-17 21:46:33 +0000339 // Parse -fsanitize-coverage=N. Currently one of asan/msan/lsan is required.
Kostya Serebryany2d88f3d2015-01-06 01:02:48 +0000340 if (hasOneOf(Sanitizers, SupportsCoverage)) {
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000341 if (Arg *A = Args.getLastArg(options::OPT_fsanitize_coverage)) {
342 StringRef S = A->getValue();
343 // Legal values are 0..4.
344 if (S.getAsInteger(0, SanitizeCoverage) || SanitizeCoverage < 0 ||
345 SanitizeCoverage > 4)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000346 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000347 }
348 }
349
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000350 if (NeedsAsan) {
351 AsanSharedRuntime =
Evgeniy Stepanov6f0ae182014-06-05 11:14:00 +0000352 Args.hasArg(options::OPT_shared_libasan) ||
353 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Peter Collingbourne32701642013-11-01 18:16:25 +0000354 AsanZeroBaseShadow =
Evgeniy Stepanovd04b8612014-01-16 10:19:31 +0000355 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000356 if (Arg *A =
357 Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
358 StringRef S = A->getValue();
359 // Legal values are 0 and 1, 2, but in future we may add more levels.
360 if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
361 AsanFieldPadding > 2) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000362 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000363 }
364 }
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000365
366 if (Arg *WindowsDebugRTArg =
367 Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
368 options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
369 options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
370 switch (WindowsDebugRTArg->getOption().getID()) {
371 case options::OPT__SLASH_MTd:
372 case options::OPT__SLASH_MDd:
373 case options::OPT__SLASH_LDd:
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000374 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000375 << WindowsDebugRTArg->getAsString(Args)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000376 << lastArgumentForKind(D, Args, SanitizerKind::Address);
377 D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000378 }
379 }
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000380 }
Alexey Samsonov90490af2014-08-08 22:47:17 +0000381
382 // Parse -link-cxx-sanitizer flag.
383 LinkCXXRuntimes =
384 Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX();
Alexey Samsonovcf055962013-08-08 10:11:02 +0000385}
386
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000387static std::string toString(const clang::SanitizerSet &Sanitizers) {
388 std::string Res;
389#define SANITIZER(NAME, ID) \
390 if (Sanitizers.has(clang::SanitizerKind::ID)) { \
391 if (!Res.empty()) \
392 Res += ","; \
393 Res += NAME; \
394 }
395#include "clang/Basic/Sanitizers.def"
396 return Res;
397}
398
Peter Collingbourne32701642013-11-01 18:16:25 +0000399void SanitizerArgs::addArgs(const llvm::opt::ArgList &Args,
Alexey Samsonovcf055962013-08-08 10:11:02 +0000400 llvm::opt::ArgStringList &CmdArgs) const {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000401 if (Sanitizers.empty())
Alexey Samsonovcf055962013-08-08 10:11:02 +0000402 return;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000403 CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
404
Alexey Samsonov88459522015-01-12 22:39:12 +0000405 if (!RecoverableSanitizers.empty())
406 CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
407 toString(RecoverableSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000408
409 if (UbsanTrapOnError)
410 CmdArgs.push_back("-fsanitize-undefined-trap-on-error");
411
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000412 for (const auto &BLPath : BlacklistFiles) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000413 SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000414 BlacklistOpt += BLPath;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000415 CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
416 }
417
418 if (MsanTrackOrigins)
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000419 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
420 llvm::utostr(MsanTrackOrigins)));
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000421 if (AsanFieldPadding)
422 CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
423 llvm::utostr(AsanFieldPadding)));
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000424 if (SanitizeCoverage)
425 CmdArgs.push_back(Args.MakeArgString("-fsanitize-coverage=" +
426 llvm::utostr(SanitizeCoverage)));
Sergey Matveev2ba87782015-02-17 15:09:33 +0000427 // MSan: Workaround for PR16386.
428 // ASan: This is mainly to help LSan with cases such as
429 // https://code.google.com/p/address-sanitizer/issues/detail?id=373
430 // We can't make this conditional on -fsanitize=leak, as that flag shouldn't
431 // affect compilation.
432 if (Sanitizers.has(SanitizerKind::Memory) ||
433 Sanitizers.has(SanitizerKind::Address))
Alexey Samsonovcf055962013-08-08 10:11:02 +0000434 CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
435}
436
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000437bool SanitizerArgs::getDefaultBlacklist(const Driver &D, std::string &BLPath) {
438 const char *BlacklistFile = nullptr;
439 if (Sanitizers.has(SanitizerKind::Address))
440 BlacklistFile = "asan_blacklist.txt";
441 else if (Sanitizers.has(SanitizerKind::Memory))
442 BlacklistFile = "msan_blacklist.txt";
443 else if (Sanitizers.has(SanitizerKind::Thread))
444 BlacklistFile = "tsan_blacklist.txt";
445 else if (Sanitizers.has(SanitizerKind::DataFlow))
446 BlacklistFile = "dfsan_abilist.txt";
447
448 if (BlacklistFile) {
449 SmallString<64> Path(D.ResourceDir);
450 llvm::sys::path::append(Path, BlacklistFile);
451 BLPath = Path.str();
452 return true;
453 }
454 return false;
455}
456
457unsigned parseValue(const char *Value) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000458 unsigned ParsedKind = llvm::StringSwitch<SanitizeKind>(Value)
459#define SANITIZER(NAME, ID) .Case(NAME, ID)
Peter Collingbourne32701642013-11-01 18:16:25 +0000460#define SANITIZER_GROUP(NAME, ID, ALIAS) .Case(NAME, ID##Group)
Alexey Samsonovcf055962013-08-08 10:11:02 +0000461#include "clang/Basic/Sanitizers.def"
462 .Default(SanitizeKind());
Alexey Samsonovcf055962013-08-08 10:11:02 +0000463 return ParsedKind;
464}
465
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000466unsigned expandGroups(unsigned Kinds) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000467#define SANITIZER(NAME, ID)
468#define SANITIZER_GROUP(NAME, ID, ALIAS) if (Kinds & ID##Group) Kinds |= ID;
469#include "clang/Basic/Sanitizers.def"
470 return Kinds;
471}
472
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000473unsigned parseArgValues(const Driver &D, const llvm::opt::Arg *A,
474 bool DiagnoseErrors) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000475 assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
Alexey Samsonov88459522015-01-12 22:39:12 +0000476 A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
477 A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
478 A->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) &&
Alexey Samsonov799f7932014-12-19 02:35:16 +0000479 "Invalid argument in parseArgValues!");
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000480 unsigned Kinds = 0;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000481 for (unsigned I = 0, N = A->getNumValues(); I != N; ++I) {
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000482 const char *Value = A->getValue(I);
483 unsigned Kind;
484 // Special case: don't accept -fsanitize=all.
485 if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
486 0 == strcmp("all", Value))
487 Kind = 0;
488 else
489 Kind = parseValue(Value);
490
491 if (Kind)
492 Kinds |= Kind;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000493 else if (DiagnoseErrors)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000494 D.Diag(clang::diag::err_drv_unsupported_option_argument)
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000495 << A->getOption().getName() << Value;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000496 }
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000497 return Kinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000498}
499
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000500std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
501 unsigned Mask) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000502 for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
503 E = Args.rend();
504 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000505 const auto *Arg = *I;
506 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
507 unsigned AddKinds = expandGroups(parseArgValues(D, Arg, false));
508 if (AddKinds & Mask)
509 return describeSanitizeArg(Arg, Mask);
510 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
511 unsigned RemoveKinds = expandGroups(parseArgValues(D, Arg, false));
512 Mask &= ~RemoveKinds;
513 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000514 }
515 llvm_unreachable("arg list didn't provide expected value");
516}
517
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000518std::string describeSanitizeArg(const llvm::opt::Arg *A, unsigned Mask) {
519 assert(A->getOption().matches(options::OPT_fsanitize_EQ)
520 && "Invalid argument in describeSanitizerArg!");
Alexey Samsonovcf055962013-08-08 10:11:02 +0000521
Peter Collingbourne32701642013-11-01 18:16:25 +0000522 std::string Sanitizers;
523 for (unsigned I = 0, N = A->getNumValues(); I != N; ++I) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000524 if (expandGroups(parseValue(A->getValue(I))) & Mask) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000525 if (!Sanitizers.empty())
526 Sanitizers += ",";
527 Sanitizers += A->getValue(I);
528 }
529 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000530
Peter Collingbourne32701642013-11-01 18:16:25 +0000531 assert(!Sanitizers.empty() && "arg didn't provide expected value");
532 return "-fsanitize=" + Sanitizers;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000533}