blob: 2fded1c80da99420952ff6a36965c44390e969af [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"
Peter Collingbourne581f4382015-07-02 01:48:12 +000010#include "Tools.h"
Peter Collingbournebf59c342015-05-11 21:39:20 +000011#include "clang/Basic/Sanitizers.h"
Alexey Samsonovcf055962013-08-08 10:11:02 +000012#include "clang/Driver/Driver.h"
13#include "clang/Driver/DriverDiagnostic.h"
14#include "clang/Driver/Options.h"
15#include "clang/Driver/ToolChain.h"
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +000016#include "llvm/ADT/StringExtras.h"
Alexey Samsonovcf055962013-08-08 10:11:02 +000017#include "llvm/ADT/StringSwitch.h"
18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/Path.h"
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000020#include "llvm/Support/SpecialCaseList.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000021#include <memory>
Alexey Samsonovcf055962013-08-08 10:11:02 +000022
Peter Collingbourne3eea6772015-05-11 21:39:14 +000023using namespace clang;
24using namespace clang::SanitizerKind;
Alexey Samsonovcf055962013-08-08 10:11:02 +000025using namespace clang::driver;
26using namespace llvm::opt;
27
Peter Collingbourne3eea6772015-05-11 21:39:14 +000028enum : SanitizerMask {
Peter Collingbourne6708c4a2015-06-19 01:51:54 +000029 NeedsUbsanRt = Undefined | Integer | CFI,
Alexey Samsonov9b873092015-06-25 23:14:32 +000030 NeedsUbsanCxxRt = Vptr | CFI,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000031 NotAllowedWithTrap = Vptr,
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +000032 RequiresPIE = DataFlow,
Kostya Serebryany2d88f3d2015-01-06 01:02:48 +000033 NeedsUnwindTables = Address | Thread | Memory | DataFlow,
Kostya Serebryany6fe5fcb2015-03-20 00:06:52 +000034 SupportsCoverage = Address | Memory | Leak | Undefined | Integer | DataFlow,
Alexey Samsonov88459522015-01-12 22:39:12 +000035 RecoverableByDefault = Undefined | Integer,
Yury Gribov5bfeca12015-11-11 10:45:48 +000036 Unrecoverable = Unreachable | Return,
Peter Collingbournea4ccff32015-02-20 20:30:56 +000037 LegacyFsanitizeRecoverMask = Undefined | Integer,
Peter Collingbourne1a7488a2015-04-02 00:23:30 +000038 NeedsLTO = CFI,
Peter Collingbourne9881b782015-06-18 23:59:22 +000039 TrappingSupported =
Peter Collingbourne6708c4a2015-06-19 01:51:54 +000040 (Undefined & ~Vptr) | UnsignedIntegerOverflow | LocalBounds | CFI,
41 TrappingDefault = CFI,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000042};
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000043
44enum CoverageFeature {
45 CoverageFunc = 1 << 0,
46 CoverageBB = 1 << 1,
47 CoverageEdge = 1 << 2,
48 CoverageIndirCall = 1 << 3,
49 CoverageTraceBB = 1 << 4,
50 CoverageTraceCmp = 1 << 5,
51 Coverage8bitCounters = 1 << 6,
52};
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000053
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000054/// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any
Peter Collingbourne3eea6772015-05-11 21:39:14 +000055/// invalid components. Returns a SanitizerMask.
56static SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
57 bool DiagnoseErrors);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000058
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000059/// Parse -f(no-)?sanitize-coverage= flag values, diagnosing any invalid
60/// components. Returns OR of members of \c CoverageFeature enumeration.
61static int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A);
62
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000063/// Produce an argument string from ArgList \p Args, which shows how it
64/// provides some sanitizer kind from \p Mask. For example, the argument list
65/// "-fsanitize=thread,vptr -fsanitize=address" with mask \c NeedsUbsanRt
66/// would produce "-fsanitize=vptr".
67static std::string lastArgumentForMask(const Driver &D,
68 const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +000069 SanitizerMask Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000070
71/// Produce an argument string from argument \p A, which shows how it provides
72/// a value in \p Mask. For instance, the argument
73/// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce
74/// "-fsanitize=alignment".
Peter Collingbourne3eea6772015-05-11 21:39:14 +000075static std::string describeSanitizeArg(const llvm::opt::Arg *A,
76 SanitizerMask Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000077
Alexey Samsonov1e715a62014-11-16 20:53:53 +000078/// Produce a string containing comma-separated names of sanitizers in \p
79/// Sanitizers set.
80static std::string toString(const clang::SanitizerSet &Sanitizers);
81
Peter Collingbourne3eea6772015-05-11 21:39:14 +000082static bool getDefaultBlacklist(const Driver &D, SanitizerMask Kinds,
Alexey Samsonovecf380e2015-03-20 18:45:06 +000083 std::string &BLPath) {
84 const char *BlacklistFile = nullptr;
Peter Collingbourne3eea6772015-05-11 21:39:14 +000085 if (Kinds & Address)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000086 BlacklistFile = "asan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000087 else if (Kinds & Memory)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000088 BlacklistFile = "msan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000089 else if (Kinds & Thread)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000090 BlacklistFile = "tsan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000091 else if (Kinds & DataFlow)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000092 BlacklistFile = "dfsan_abilist.txt";
Peter Collingbourne6fccf952015-07-15 12:15:56 +000093 else if (Kinds & CFI)
94 BlacklistFile = "cfi_blacklist.txt";
Alexey Samsonovecf380e2015-03-20 18:45:06 +000095
96 if (BlacklistFile) {
97 clang::SmallString<64> Path(D.ResourceDir);
98 llvm::sys::path::append(Path, BlacklistFile);
99 BLPath = Path.str();
100 return true;
101 }
102 return false;
103}
104
Peter Collingbourne9881b782015-06-18 23:59:22 +0000105/// Sets group bits for every group that has at least one representative already
106/// enabled in \p Kinds.
107static SanitizerMask setGroupBits(SanitizerMask Kinds) {
108#define SANITIZER(NAME, ID)
109#define SANITIZER_GROUP(NAME, ID, ALIAS) \
110 if (Kinds & SanitizerKind::ID) \
111 Kinds |= SanitizerKind::ID##Group;
112#include "clang/Basic/Sanitizers.def"
113 return Kinds;
114}
115
116static SanitizerMask parseSanitizeTrapArgs(const Driver &D,
117 const llvm::opt::ArgList &Args) {
118 SanitizerMask TrapRemove = 0; // During the loop below, the accumulated set of
119 // sanitizers disabled by the current sanitizer
120 // argument or any argument after it.
121 SanitizerMask TrappingKinds = 0;
122 SanitizerMask TrappingSupportedWithGroups = setGroupBits(TrappingSupported);
123
124 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
125 I != E; ++I) {
126 const auto *Arg = *I;
127 if (Arg->getOption().matches(options::OPT_fsanitize_trap_EQ)) {
128 Arg->claim();
129 SanitizerMask Add = parseArgValues(D, Arg, true);
130 Add &= ~TrapRemove;
131 if (SanitizerMask InvalidValues = Add & ~TrappingSupportedWithGroups) {
132 SanitizerSet S;
133 S.Mask = InvalidValues;
134 D.Diag(diag::err_drv_unsupported_option_argument) << "-fsanitize-trap"
135 << toString(S);
136 }
137 TrappingKinds |= expandSanitizerGroups(Add) & ~TrapRemove;
138 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) {
139 Arg->claim();
140 TrapRemove |= expandSanitizerGroups(parseArgValues(D, Arg, true));
141 } else if (Arg->getOption().matches(
142 options::OPT_fsanitize_undefined_trap_on_error)) {
143 Arg->claim();
144 TrappingKinds |=
145 expandSanitizerGroups(UndefinedGroup & ~TrapRemove) & ~TrapRemove;
146 } else if (Arg->getOption().matches(
147 options::OPT_fno_sanitize_undefined_trap_on_error)) {
148 Arg->claim();
149 TrapRemove |= expandSanitizerGroups(UndefinedGroup);
150 }
151 }
152
Peter Collingbourne6708c4a2015-06-19 01:51:54 +0000153 // Apply default trapping behavior.
154 TrappingKinds |= TrappingDefault & ~TrapRemove;
155
Peter Collingbourne9881b782015-06-18 23:59:22 +0000156 return TrappingKinds;
157}
158
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000159bool SanitizerArgs::needsUbsanRt() const {
Peter Collingbourne9881b782015-06-18 23:59:22 +0000160 return (Sanitizers.Mask & NeedsUbsanRt & ~TrapSanitizers.Mask) &&
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000161 !Sanitizers.has(Address) &&
162 !Sanitizers.has(Memory) &&
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000163 !Sanitizers.has(Thread) &&
164 !CfiCrossDso;
165}
166
167bool SanitizerArgs::needsCfiRt() const {
Evgeniy Stepanove3fb51c2015-12-16 00:38:42 +0000168 return !(Sanitizers.Mask & CFI & ~TrapSanitizers.Mask) && CfiCrossDso;
169}
170
171bool SanitizerArgs::needsCfiDiagRt() const {
172 return (Sanitizers.Mask & CFI & ~TrapSanitizers.Mask) && CfiCrossDso;
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000173}
174
Dmitry Vyukov43419a72014-11-21 12:19:01 +0000175bool SanitizerArgs::requiresPIE() const {
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +0000176 return NeedPIE || (Sanitizers.Mask & RequiresPIE);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000177}
178
179bool SanitizerArgs::needsUnwindTables() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000180 return Sanitizers.Mask & NeedsUnwindTables;
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000181}
182
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000183void SanitizerArgs::clear() {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000184 Sanitizers.clear();
Alexey Samsonov88459522015-01-12 22:39:12 +0000185 RecoverableSanitizers.clear();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000186 TrapSanitizers.clear();
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000187 BlacklistFiles.clear();
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000188 ExtraDeps.clear();
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000189 CoverageFeatures = 0;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000190 MsanTrackOrigins = 0;
Evgeniy Stepanov45be9e02015-07-10 20:07:16 +0000191 MsanUseAfterDtor = false;
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +0000192 NeedPIE = false;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000193 AsanFieldPadding = 0;
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000194 AsanSharedRuntime = false;
Alexey Samsonov90490af2014-08-08 22:47:17 +0000195 LinkCXXRuntimes = false;
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000196 CfiCrossDso = false;
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000197}
Alexey Samsonovcf055962013-08-08 10:11:02 +0000198
Peter Collingbourne32701642013-11-01 18:16:25 +0000199SanitizerArgs::SanitizerArgs(const ToolChain &TC,
200 const llvm::opt::ArgList &Args) {
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000201 clear();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000202 SanitizerMask AllRemove = 0; // During the loop below, the accumulated set of
203 // sanitizers disabled by the current sanitizer
204 // argument or any argument after it.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000205 SanitizerMask AllAddedKinds = 0; // Mask of all sanitizers ever enabled by
206 // -fsanitize= flags (directly or via group
207 // expansion), some of which may be disabled
208 // later. Used to carefully prune
209 // unused-argument diagnostics.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000210 SanitizerMask DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
211 // Used to deduplicate diagnostics.
212 SanitizerMask Kinds = 0;
Alexey Samsonov9b873092015-06-25 23:14:32 +0000213 const SanitizerMask Supported = setGroupBits(TC.getSupportedSanitizers());
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000214 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
215
Peter Collingbourne32701642013-11-01 18:16:25 +0000216 const Driver &D = TC.getDriver();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000217 SanitizerMask TrappingKinds = parseSanitizeTrapArgs(D, Args);
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000218 SanitizerMask InvalidTrappingKinds = TrappingKinds & NotAllowedWithTrap;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000219
Peter Collingbourne32701642013-11-01 18:16:25 +0000220 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
221 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000222 const auto *Arg = *I;
223 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
224 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000225 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000226 AllAddedKinds |= expandSanitizerGroups(Add);
Peter Collingbourne32701642013-11-01 18:16:25 +0000227
Alexey Samsonov799f7932014-12-19 02:35:16 +0000228 // Avoid diagnosing any sanitizer which is disabled later.
229 Add &= ~AllRemove;
230 // At this point we have not expanded groups, so any unsupported
231 // sanitizers in Add are those which have been explicitly enabled.
232 // Diagnose them.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000233 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000234 Add & InvalidTrappingKinds & ~DiagnosedKinds) {
Peter Collingbourne9881b782015-06-18 23:59:22 +0000235 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
236 D.Diag(diag::err_drv_argument_not_allowed_with)
237 << Desc << "-fsanitize-trap=undefined";
238 DiagnosedKinds |= KindsToDiagnose;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000239 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000240 Add &= ~InvalidTrappingKinds;
241 if (SanitizerMask KindsToDiagnose = Add & ~Supported & ~DiagnosedKinds) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000242 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
243 D.Diag(diag::err_drv_unsupported_opt_for_target)
244 << Desc << TC.getTriple().str();
245 DiagnosedKinds |= KindsToDiagnose;
246 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000247 Add &= Supported;
Peter Collingbourne32701642013-11-01 18:16:25 +0000248
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000249 // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups
250 // so we don't error out if -fno-rtti and -fsanitize=undefined were
251 // passed.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000252 if (Add & Vptr &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000253 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
254 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
255 if (RTTIMode == ToolChain::RM_DisabledImplicitly)
256 // Warn about not having rtti enabled if the vptr sanitizer is
257 // explicitly enabled
258 D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default);
259 else {
260 const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg();
261 assert(NoRTTIArg &&
262 "RTTI disabled explicitly but we have no argument!");
263 D.Diag(diag::err_drv_argument_not_allowed_with)
264 << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args);
265 }
266
267 // Take out the Vptr sanitizer from the enabled sanitizers
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000268 AllRemove |= Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000269 }
270
Peter Collingbournebf59c342015-05-11 21:39:20 +0000271 Add = expandSanitizerGroups(Add);
Alexey Samsonov799f7932014-12-19 02:35:16 +0000272 // Group expansion may have enabled a sanitizer which is disabled later.
273 Add &= ~AllRemove;
274 // Silently discard any unsupported sanitizers implicitly enabled through
275 // group expansion.
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000276 Add &= ~InvalidTrappingKinds;
277 Add &= Supported;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000278
Alexey Samsonov799f7932014-12-19 02:35:16 +0000279 Kinds |= Add;
280 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
281 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000282 SanitizerMask Remove = parseArgValues(D, Arg, true);
Peter Collingbournebf59c342015-05-11 21:39:20 +0000283 AllRemove |= expandSanitizerGroups(Remove);
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000284 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000285 }
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000286
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000287 // We disable the vptr sanitizer if it was enabled by group expansion but RTTI
288 // is disabled.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000289 if ((Kinds & Vptr) &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000290 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
291 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000292 Kinds &= ~Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000293 }
294
Alexey Samsonov907880e2015-06-19 19:57:46 +0000295 // Check that LTO is enabled if we need it.
Teresa Johnson945bc502015-10-15 20:35:53 +0000296 if ((Kinds & NeedsLTO) && !D.isUsingLTO()) {
Alexey Samsonov907880e2015-06-19 19:57:46 +0000297 D.Diag(diag::err_drv_argument_only_allowed_with)
298 << lastArgumentForMask(D, Args, Kinds & NeedsLTO) << "-flto";
299 }
300
Alexey Samsonov9b873092015-06-25 23:14:32 +0000301 // Report error if there are non-trapping sanitizers that require
302 // c++abi-specific parts of UBSan runtime, and they are not provided by the
303 // toolchain. We don't have a good way to check the latter, so we just
304 // check if the toolchan supports vptr.
305 if (~Supported & Vptr) {
Peter Collingbourne4bdceaa2015-07-08 21:08:05 +0000306 SanitizerMask KindsToDiagnose = Kinds & ~TrappingKinds & NeedsUbsanCxxRt;
307 // The runtime library supports the Microsoft C++ ABI, but only well enough
308 // for CFI. FIXME: Remove this once we support vptr on Windows.
309 if (TC.getTriple().isOSWindows())
310 KindsToDiagnose &= ~CFI;
311 if (KindsToDiagnose) {
Alexey Samsonov9b873092015-06-25 23:14:32 +0000312 SanitizerSet S;
313 S.Mask = KindsToDiagnose;
314 D.Diag(diag::err_drv_unsupported_opt_for_target)
315 << ("-fno-sanitize-trap=" + toString(S)) << TC.getTriple().str();
316 Kinds &= ~KindsToDiagnose;
317 }
318 }
319
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000320 // Warn about incompatible groups of sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000321 std::pair<SanitizerMask, SanitizerMask> IncompatibleGroups[] = {
322 std::make_pair(Address, Thread), std::make_pair(Address, Memory),
323 std::make_pair(Thread, Memory), std::make_pair(Leak, Thread),
Alexander Potapenkob9b73ef2015-06-19 12:19:07 +0000324 std::make_pair(Leak, Memory), std::make_pair(KernelAddress, Address),
325 std::make_pair(KernelAddress, Leak),
326 std::make_pair(KernelAddress, Thread),
327 std::make_pair(KernelAddress, Memory)};
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000328 for (auto G : IncompatibleGroups) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000329 SanitizerMask Group = G.first;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000330 if (Kinds & Group) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000331 if (SanitizerMask Incompatible = Kinds & G.second) {
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000332 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
333 << lastArgumentForMask(D, Args, Group)
334 << lastArgumentForMask(D, Args, Incompatible);
335 Kinds &= ~Incompatible;
336 }
337 }
338 }
339 // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
340 // -fsanitize=address. Perhaps it should print an error, or perhaps
341 // -f(-no)sanitize=leak should change whether leak detection is enabled by
342 // default in ASan?
343
Alexey Samsonov88459522015-01-12 22:39:12 +0000344 // Parse -f(no-)?sanitize-recover flags.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000345 SanitizerMask RecoverableKinds = RecoverableByDefault;
346 SanitizerMask DiagnosedUnrecoverableKinds = 0;
Alexey Samsonov88459522015-01-12 22:39:12 +0000347 for (const auto *Arg : Args) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000348 const char *DeprecatedReplacement = nullptr;
Alexey Samsonov88459522015-01-12 22:39:12 +0000349 if (Arg->getOption().matches(options::OPT_fsanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000350 DeprecatedReplacement = "-fsanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000351 RecoverableKinds |= expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000352 Arg->claim();
353 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000354 DeprecatedReplacement = "-fno-sanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000355 RecoverableKinds &= ~expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000356 Arg->claim();
357 } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000358 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonov88459522015-01-12 22:39:12 +0000359 // Report error if user explicitly tries to recover from unrecoverable
360 // sanitizer.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000361 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov88459522015-01-12 22:39:12 +0000362 Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
363 SanitizerSet SetToDiagnose;
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000364 SetToDiagnose.Mask |= KindsToDiagnose;
Alexey Samsonov88459522015-01-12 22:39:12 +0000365 D.Diag(diag::err_drv_unsupported_option_argument)
366 << Arg->getOption().getName() << toString(SetToDiagnose);
367 DiagnosedUnrecoverableKinds |= KindsToDiagnose;
368 }
Peter Collingbournebf59c342015-05-11 21:39:20 +0000369 RecoverableKinds |= expandSanitizerGroups(Add);
Alexey Samsonov88459522015-01-12 22:39:12 +0000370 Arg->claim();
371 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000372 RecoverableKinds &= ~expandSanitizerGroups(parseArgValues(D, Arg, true));
Alexey Samsonov88459522015-01-12 22:39:12 +0000373 Arg->claim();
374 }
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000375 if (DeprecatedReplacement) {
376 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
377 << DeprecatedReplacement;
378 }
Alexey Samsonov88459522015-01-12 22:39:12 +0000379 }
380 RecoverableKinds &= Kinds;
381 RecoverableKinds &= ~Unrecoverable;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000382
Peter Collingbourne9881b782015-06-18 23:59:22 +0000383 TrappingKinds &= Kinds;
384
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000385 // Setup blacklist files.
386 // Add default blacklist from resource directory.
387 {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000388 std::string BLPath;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000389 if (getDefaultBlacklist(D, Kinds, BLPath) && llvm::sys::fs::exists(BLPath))
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000390 BlacklistFiles.push_back(BLPath);
391 }
392 // Parse -f(no-)sanitize-blacklist options.
393 for (const auto *Arg : Args) {
394 if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) {
395 Arg->claim();
396 std::string BLPath = Arg->getValue();
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000397 if (llvm::sys::fs::exists(BLPath)) {
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000398 BlacklistFiles.push_back(BLPath);
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000399 ExtraDeps.push_back(BLPath);
400 } else
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000401 D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000402
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000403 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) {
404 Arg->claim();
405 BlacklistFiles.clear();
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000406 ExtraDeps.clear();
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000407 }
408 }
409 // Validate blacklists format.
410 {
411 std::string BLError;
412 std::unique_ptr<llvm::SpecialCaseList> SCL(
413 llvm::SpecialCaseList::create(BlacklistFiles, BLError));
414 if (!SCL.get())
415 D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000416 }
417
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000418 // Parse -f[no-]sanitize-memory-track-origins[=level] options.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000419 if (AllAddedKinds & Memory) {
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000420 if (Arg *A =
421 Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
422 options::OPT_fsanitize_memory_track_origins,
423 options::OPT_fno_sanitize_memory_track_origins)) {
424 if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
Evgeniy Stepanov6e09bca2015-02-26 15:59:30 +0000425 MsanTrackOrigins = 2;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000426 } else if (A->getOption().matches(
427 options::OPT_fno_sanitize_memory_track_origins)) {
428 MsanTrackOrigins = 0;
429 } else {
430 StringRef S = A->getValue();
431 if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
432 MsanTrackOrigins > 2) {
Nico Weberf2a39a72015-04-13 20:03:03 +0000433 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000434 }
435 }
436 }
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +0000437 MsanUseAfterDtor =
438 Args.hasArg(options::OPT_fsanitize_memory_use_after_dtor);
439 NeedPIE |= !(TC.getTriple().isOSLinux() &&
440 TC.getTriple().getArch() == llvm::Triple::x86_64);
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000441 }
442
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000443 if (AllAddedKinds & CFI) {
444 CfiCrossDso = Args.hasFlag(options::OPT_fsanitize_cfi_cross_dso,
445 options::OPT_fno_sanitize_cfi_cross_dso, false);
446 // Without PIE, external function address may resolve to a PLT record, which
447 // can not be verified by the target module.
448 NeedPIE |= CfiCrossDso;
449 }
450
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000451 // Parse -f(no-)?sanitize-coverage flags if coverage is supported by the
452 // enabled sanitizers.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000453 if (AllAddedKinds & SupportsCoverage) {
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000454 for (const auto *Arg : Args) {
455 if (Arg->getOption().matches(options::OPT_fsanitize_coverage)) {
456 Arg->claim();
457 int LegacySanitizeCoverage;
458 if (Arg->getNumValues() == 1 &&
459 !StringRef(Arg->getValue(0))
460 .getAsInteger(0, LegacySanitizeCoverage) &&
461 LegacySanitizeCoverage >= 0 && LegacySanitizeCoverage <= 4) {
462 // TODO: Add deprecation notice for this form.
463 switch (LegacySanitizeCoverage) {
464 case 0:
465 CoverageFeatures = 0;
466 break;
467 case 1:
468 CoverageFeatures = CoverageFunc;
469 break;
470 case 2:
471 CoverageFeatures = CoverageBB;
472 break;
473 case 3:
474 CoverageFeatures = CoverageEdge;
475 break;
476 case 4:
477 CoverageFeatures = CoverageEdge | CoverageIndirCall;
478 break;
479 }
480 continue;
481 }
482 CoverageFeatures |= parseCoverageFeatures(D, Arg);
483 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_coverage)) {
484 Arg->claim();
485 CoverageFeatures &= ~parseCoverageFeatures(D, Arg);
486 }
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000487 }
488 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000489 // Choose at most one coverage type: function, bb, or edge.
490 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageBB))
491 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
492 << "-fsanitize-coverage=func"
493 << "-fsanitize-coverage=bb";
494 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageEdge))
495 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
496 << "-fsanitize-coverage=func"
497 << "-fsanitize-coverage=edge";
498 if ((CoverageFeatures & CoverageBB) && (CoverageFeatures & CoverageEdge))
499 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
500 << "-fsanitize-coverage=bb"
501 << "-fsanitize-coverage=edge";
502 // Basic block tracing and 8-bit counters require some type of coverage
503 // enabled.
504 int CoverageTypes = CoverageFunc | CoverageBB | CoverageEdge;
505 if ((CoverageFeatures & CoverageTraceBB) &&
506 !(CoverageFeatures & CoverageTypes))
507 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
508 << "-fsanitize-coverage=trace-bb"
509 << "-fsanitize-coverage=(func|bb|edge)";
510 if ((CoverageFeatures & Coverage8bitCounters) &&
511 !(CoverageFeatures & CoverageTypes))
512 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
513 << "-fsanitize-coverage=8bit-counters"
514 << "-fsanitize-coverage=(func|bb|edge)";
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000515
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000516 if (AllAddedKinds & Address) {
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000517 AsanSharedRuntime =
Evgeniy Stepanov14deb7b2015-10-08 21:21:44 +0000518 Args.hasArg(options::OPT_shared_libasan) || TC.getTriple().isAndroid();
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +0000519 NeedPIE |= TC.getTriple().isAndroid();
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000520 if (Arg *A =
521 Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
522 StringRef S = A->getValue();
523 // Legal values are 0 and 1, 2, but in future we may add more levels.
524 if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
525 AsanFieldPadding > 2) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000526 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000527 }
528 }
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000529
530 if (Arg *WindowsDebugRTArg =
531 Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
532 options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
533 options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
534 switch (WindowsDebugRTArg->getOption().getID()) {
535 case options::OPT__SLASH_MTd:
536 case options::OPT__SLASH_MDd:
537 case options::OPT__SLASH_LDd:
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000538 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000539 << WindowsDebugRTArg->getAsString(Args)
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000540 << lastArgumentForMask(D, Args, Address);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000541 D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000542 }
543 }
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000544 }
Alexey Samsonov90490af2014-08-08 22:47:17 +0000545
546 // Parse -link-cxx-sanitizer flag.
547 LinkCXXRuntimes =
548 Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX();
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000549
550 // Finally, initialize the set of available and recoverable sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000551 Sanitizers.Mask |= Kinds;
552 RecoverableSanitizers.Mask |= RecoverableKinds;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000553 TrapSanitizers.Mask |= TrappingKinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000554}
555
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000556static std::string toString(const clang::SanitizerSet &Sanitizers) {
557 std::string Res;
558#define SANITIZER(NAME, ID) \
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000559 if (Sanitizers.has(ID)) { \
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000560 if (!Res.empty()) \
561 Res += ","; \
562 Res += NAME; \
563 }
564#include "clang/Basic/Sanitizers.def"
565 return Res;
566}
567
Peter Collingbourne581f4382015-07-02 01:48:12 +0000568void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
569 llvm::opt::ArgStringList &CmdArgs,
570 types::ID InputType) const {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000571 if (Sanitizers.empty())
Alexey Samsonovcf055962013-08-08 10:11:02 +0000572 return;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000573 CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
574
Alexey Samsonov88459522015-01-12 22:39:12 +0000575 if (!RecoverableSanitizers.empty())
576 CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
577 toString(RecoverableSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000578
Peter Collingbourne9881b782015-06-18 23:59:22 +0000579 if (!TrapSanitizers.empty())
580 CmdArgs.push_back(
581 Args.MakeArgString("-fsanitize-trap=" + toString(TrapSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000582
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000583 for (const auto &BLPath : BlacklistFiles) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000584 SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000585 BlacklistOpt += BLPath;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000586 CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
587 }
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000588 for (const auto &Dep : ExtraDeps) {
589 SmallString<64> ExtraDepOpt("-fdepfile-entry=");
590 ExtraDepOpt += Dep;
591 CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
592 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000593
594 if (MsanTrackOrigins)
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000595 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
596 llvm::utostr(MsanTrackOrigins)));
Evgeniy Stepanov45be9e02015-07-10 20:07:16 +0000597
598 if (MsanUseAfterDtor)
599 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-use-after-dtor"));
600
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000601 if (CfiCrossDso)
602 CmdArgs.push_back(Args.MakeArgString("-fsanitize-cfi-cross-dso"));
603
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000604 if (AsanFieldPadding)
605 CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
606 llvm::utostr(AsanFieldPadding)));
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000607 // Translate available CoverageFeatures to corresponding clang-cc1 flags.
608 std::pair<int, const char *> CoverageFlags[] = {
609 std::make_pair(CoverageFunc, "-fsanitize-coverage-type=1"),
610 std::make_pair(CoverageBB, "-fsanitize-coverage-type=2"),
611 std::make_pair(CoverageEdge, "-fsanitize-coverage-type=3"),
612 std::make_pair(CoverageIndirCall, "-fsanitize-coverage-indirect-calls"),
613 std::make_pair(CoverageTraceBB, "-fsanitize-coverage-trace-bb"),
614 std::make_pair(CoverageTraceCmp, "-fsanitize-coverage-trace-cmp"),
615 std::make_pair(Coverage8bitCounters, "-fsanitize-coverage-8bit-counters")};
616 for (auto F : CoverageFlags) {
617 if (CoverageFeatures & F.first)
618 CmdArgs.push_back(Args.MakeArgString(F.second));
Alexey Samsonov3f3b3ab2015-05-07 18:31:29 +0000619 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000620
621
Sergey Matveev2ba87782015-02-17 15:09:33 +0000622 // MSan: Workaround for PR16386.
623 // ASan: This is mainly to help LSan with cases such as
624 // https://code.google.com/p/address-sanitizer/issues/detail?id=373
625 // We can't make this conditional on -fsanitize=leak, as that flag shouldn't
626 // affect compilation.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000627 if (Sanitizers.has(Memory) || Sanitizers.has(Address))
Alexey Samsonovcf055962013-08-08 10:11:02 +0000628 CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
Peter Collingbourne581f4382015-07-02 01:48:12 +0000629
630 if (TC.getTriple().isOSWindows() && needsUbsanRt()) {
631 // Instruct the code generator to embed linker directives in the object file
632 // that cause the required runtime libraries to be linked.
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000633 CmdArgs.push_back(Args.MakeArgString(
634 "--dependent-lib=" + TC.getCompilerRT(Args, "ubsan_standalone")));
Peter Collingbourne581f4382015-07-02 01:48:12 +0000635 if (types::isCXX(InputType))
Saleem Abdulrasoold44901f2015-09-26 03:26:44 +0000636 CmdArgs.push_back(Args.MakeArgString(
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000637 "--dependent-lib=" + TC.getCompilerRT(Args, "ubsan_standalone_cxx")));
Peter Collingbourne581f4382015-07-02 01:48:12 +0000638 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000639}
640
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000641SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
642 bool DiagnoseErrors) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000643 assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
Alexey Samsonov88459522015-01-12 22:39:12 +0000644 A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
645 A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
Peter Collingbourne9881b782015-06-18 23:59:22 +0000646 A->getOption().matches(options::OPT_fno_sanitize_recover_EQ) ||
647 A->getOption().matches(options::OPT_fsanitize_trap_EQ) ||
648 A->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) &&
Alexey Samsonov799f7932014-12-19 02:35:16 +0000649 "Invalid argument in parseArgValues!");
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000650 SanitizerMask Kinds = 0;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000651 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
652 const char *Value = A->getValue(i);
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000653 SanitizerMask Kind;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000654 // Special case: don't accept -fsanitize=all.
655 if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
656 0 == strcmp("all", Value))
657 Kind = 0;
658 else
Peter Collingbournebf59c342015-05-11 21:39:20 +0000659 Kind = parseSanitizerValue(Value, /*AllowGroups=*/true);
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000660
661 if (Kind)
662 Kinds |= Kind;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000663 else if (DiagnoseErrors)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000664 D.Diag(clang::diag::err_drv_unsupported_option_argument)
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000665 << A->getOption().getName() << Value;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000666 }
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000667 return Kinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000668}
669
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000670int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A) {
671 assert(A->getOption().matches(options::OPT_fsanitize_coverage) ||
672 A->getOption().matches(options::OPT_fno_sanitize_coverage));
673 int Features = 0;
674 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
675 const char *Value = A->getValue(i);
676 int F = llvm::StringSwitch<int>(Value)
677 .Case("func", CoverageFunc)
678 .Case("bb", CoverageBB)
679 .Case("edge", CoverageEdge)
680 .Case("indirect-calls", CoverageIndirCall)
681 .Case("trace-bb", CoverageTraceBB)
682 .Case("trace-cmp", CoverageTraceCmp)
683 .Case("8bit-counters", Coverage8bitCounters)
684 .Default(0);
685 if (F == 0)
686 D.Diag(clang::diag::err_drv_unsupported_option_argument)
687 << A->getOption().getName() << Value;
688 Features |= F;
689 }
690 return Features;
691}
692
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000693std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000694 SanitizerMask Mask) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000695 for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
696 E = Args.rend();
697 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000698 const auto *Arg = *I;
699 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000700 SanitizerMask AddKinds =
701 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000702 if (AddKinds & Mask)
703 return describeSanitizeArg(Arg, Mask);
704 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000705 SanitizerMask RemoveKinds =
706 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000707 Mask &= ~RemoveKinds;
708 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000709 }
710 llvm_unreachable("arg list didn't provide expected value");
711}
712
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000713std::string describeSanitizeArg(const llvm::opt::Arg *A, SanitizerMask Mask) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000714 assert(A->getOption().matches(options::OPT_fsanitize_EQ)
715 && "Invalid argument in describeSanitizerArg!");
Alexey Samsonovcf055962013-08-08 10:11:02 +0000716
Peter Collingbourne32701642013-11-01 18:16:25 +0000717 std::string Sanitizers;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000718 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000719 if (expandSanitizerGroups(
720 parseSanitizerValue(A->getValue(i), /*AllowGroups=*/true)) &
721 Mask) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000722 if (!Sanitizers.empty())
723 Sanitizers += ",";
Alexey Samsonov83791e22015-03-03 22:15:32 +0000724 Sanitizers += A->getValue(i);
Peter Collingbourne32701642013-11-01 18:16:25 +0000725 }
726 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000727
Peter Collingbourne32701642013-11-01 18:16:25 +0000728 assert(!Sanitizers.empty() && "arg didn't provide expected value");
729 return "-fsanitize=" + Sanitizers;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000730}