blob: 3b3984c7197b6def461320b96499c593c97d331a [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,
Dmitry Vyukov43419a72014-11-21 12:19:01 +000032 RequiresPIE = Memory | 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,
36 Unrecoverable = Address | 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) &&
163 !Sanitizers.has(Thread);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000164}
165
Dmitry Vyukov43419a72014-11-21 12:19:01 +0000166bool SanitizerArgs::requiresPIE() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000167 return AsanZeroBaseShadow || (Sanitizers.Mask & RequiresPIE);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000168}
169
170bool SanitizerArgs::needsUnwindTables() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000171 return Sanitizers.Mask & NeedsUnwindTables;
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000172}
173
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000174void SanitizerArgs::clear() {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000175 Sanitizers.clear();
Alexey Samsonov88459522015-01-12 22:39:12 +0000176 RecoverableSanitizers.clear();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000177 TrapSanitizers.clear();
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000178 BlacklistFiles.clear();
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000179 CoverageFeatures = 0;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000180 MsanTrackOrigins = 0;
Evgeniy Stepanov45be9e02015-07-10 20:07:16 +0000181 MsanUseAfterDtor = false;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000182 AsanFieldPadding = 0;
Peter Collingbourne32701642013-11-01 18:16:25 +0000183 AsanZeroBaseShadow = false;
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000184 AsanSharedRuntime = false;
Alexey Samsonov90490af2014-08-08 22:47:17 +0000185 LinkCXXRuntimes = false;
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000186}
Alexey Samsonovcf055962013-08-08 10:11:02 +0000187
Peter Collingbourne32701642013-11-01 18:16:25 +0000188SanitizerArgs::SanitizerArgs(const ToolChain &TC,
189 const llvm::opt::ArgList &Args) {
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000190 clear();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000191 SanitizerMask AllRemove = 0; // During the loop below, the accumulated set of
192 // sanitizers disabled by the current sanitizer
193 // argument or any argument after it.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000194 SanitizerMask AllAddedKinds = 0; // Mask of all sanitizers ever enabled by
195 // -fsanitize= flags (directly or via group
196 // expansion), some of which may be disabled
197 // later. Used to carefully prune
198 // unused-argument diagnostics.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000199 SanitizerMask DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
200 // Used to deduplicate diagnostics.
201 SanitizerMask Kinds = 0;
Alexey Samsonov9b873092015-06-25 23:14:32 +0000202 const SanitizerMask Supported = setGroupBits(TC.getSupportedSanitizers());
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000203 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
204
Peter Collingbourne32701642013-11-01 18:16:25 +0000205 const Driver &D = TC.getDriver();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000206 SanitizerMask TrappingKinds = parseSanitizeTrapArgs(D, Args);
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000207 SanitizerMask InvalidTrappingKinds = TrappingKinds & NotAllowedWithTrap;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000208
Peter Collingbourne32701642013-11-01 18:16:25 +0000209 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
210 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000211 const auto *Arg = *I;
212 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
213 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000214 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000215 AllAddedKinds |= expandSanitizerGroups(Add);
Peter Collingbourne32701642013-11-01 18:16:25 +0000216
Alexey Samsonov799f7932014-12-19 02:35:16 +0000217 // Avoid diagnosing any sanitizer which is disabled later.
218 Add &= ~AllRemove;
219 // At this point we have not expanded groups, so any unsupported
220 // sanitizers in Add are those which have been explicitly enabled.
221 // Diagnose them.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000222 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000223 Add & InvalidTrappingKinds & ~DiagnosedKinds) {
Peter Collingbourne9881b782015-06-18 23:59:22 +0000224 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
225 D.Diag(diag::err_drv_argument_not_allowed_with)
226 << Desc << "-fsanitize-trap=undefined";
227 DiagnosedKinds |= KindsToDiagnose;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000228 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000229 Add &= ~InvalidTrappingKinds;
230 if (SanitizerMask KindsToDiagnose = Add & ~Supported & ~DiagnosedKinds) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000231 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
232 D.Diag(diag::err_drv_unsupported_opt_for_target)
233 << Desc << TC.getTriple().str();
234 DiagnosedKinds |= KindsToDiagnose;
235 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000236 Add &= Supported;
Peter Collingbourne32701642013-11-01 18:16:25 +0000237
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000238 // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups
239 // so we don't error out if -fno-rtti and -fsanitize=undefined were
240 // passed.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000241 if (Add & Vptr &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000242 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
243 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
244 if (RTTIMode == ToolChain::RM_DisabledImplicitly)
245 // Warn about not having rtti enabled if the vptr sanitizer is
246 // explicitly enabled
247 D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default);
248 else {
249 const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg();
250 assert(NoRTTIArg &&
251 "RTTI disabled explicitly but we have no argument!");
252 D.Diag(diag::err_drv_argument_not_allowed_with)
253 << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args);
254 }
255
256 // Take out the Vptr sanitizer from the enabled sanitizers
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000257 AllRemove |= Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000258 }
259
Peter Collingbournebf59c342015-05-11 21:39:20 +0000260 Add = expandSanitizerGroups(Add);
Alexey Samsonov799f7932014-12-19 02:35:16 +0000261 // Group expansion may have enabled a sanitizer which is disabled later.
262 Add &= ~AllRemove;
263 // Silently discard any unsupported sanitizers implicitly enabled through
264 // group expansion.
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000265 Add &= ~InvalidTrappingKinds;
266 Add &= Supported;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000267
Alexey Samsonov799f7932014-12-19 02:35:16 +0000268 Kinds |= Add;
269 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
270 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000271 SanitizerMask Remove = parseArgValues(D, Arg, true);
Peter Collingbournebf59c342015-05-11 21:39:20 +0000272 AllRemove |= expandSanitizerGroups(Remove);
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000273 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000274 }
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000275
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000276 // We disable the vptr sanitizer if it was enabled by group expansion but RTTI
277 // is disabled.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000278 if ((Kinds & Vptr) &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000279 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
280 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000281 Kinds &= ~Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000282 }
283
Alexey Samsonov907880e2015-06-19 19:57:46 +0000284 // Check that LTO is enabled if we need it.
285 if ((Kinds & NeedsLTO) && !D.IsUsingLTO(Args)) {
286 D.Diag(diag::err_drv_argument_only_allowed_with)
287 << lastArgumentForMask(D, Args, Kinds & NeedsLTO) << "-flto";
288 }
289
Alexey Samsonov9b873092015-06-25 23:14:32 +0000290 // Report error if there are non-trapping sanitizers that require
291 // c++abi-specific parts of UBSan runtime, and they are not provided by the
292 // toolchain. We don't have a good way to check the latter, so we just
293 // check if the toolchan supports vptr.
294 if (~Supported & Vptr) {
Peter Collingbourne4bdceaa2015-07-08 21:08:05 +0000295 SanitizerMask KindsToDiagnose = Kinds & ~TrappingKinds & NeedsUbsanCxxRt;
296 // The runtime library supports the Microsoft C++ ABI, but only well enough
297 // for CFI. FIXME: Remove this once we support vptr on Windows.
298 if (TC.getTriple().isOSWindows())
299 KindsToDiagnose &= ~CFI;
300 if (KindsToDiagnose) {
Alexey Samsonov9b873092015-06-25 23:14:32 +0000301 SanitizerSet S;
302 S.Mask = KindsToDiagnose;
303 D.Diag(diag::err_drv_unsupported_opt_for_target)
304 << ("-fno-sanitize-trap=" + toString(S)) << TC.getTriple().str();
305 Kinds &= ~KindsToDiagnose;
306 }
307 }
308
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000309 // Warn about incompatible groups of sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000310 std::pair<SanitizerMask, SanitizerMask> IncompatibleGroups[] = {
311 std::make_pair(Address, Thread), std::make_pair(Address, Memory),
312 std::make_pair(Thread, Memory), std::make_pair(Leak, Thread),
Alexander Potapenkob9b73ef2015-06-19 12:19:07 +0000313 std::make_pair(Leak, Memory), std::make_pair(KernelAddress, Address),
314 std::make_pair(KernelAddress, Leak),
315 std::make_pair(KernelAddress, Thread),
316 std::make_pair(KernelAddress, Memory)};
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000317 for (auto G : IncompatibleGroups) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000318 SanitizerMask Group = G.first;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000319 if (Kinds & Group) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000320 if (SanitizerMask Incompatible = Kinds & G.second) {
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000321 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
322 << lastArgumentForMask(D, Args, Group)
323 << lastArgumentForMask(D, Args, Incompatible);
324 Kinds &= ~Incompatible;
325 }
326 }
327 }
328 // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
329 // -fsanitize=address. Perhaps it should print an error, or perhaps
330 // -f(-no)sanitize=leak should change whether leak detection is enabled by
331 // default in ASan?
332
Alexey Samsonov88459522015-01-12 22:39:12 +0000333 // Parse -f(no-)?sanitize-recover flags.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000334 SanitizerMask RecoverableKinds = RecoverableByDefault;
335 SanitizerMask DiagnosedUnrecoverableKinds = 0;
Alexey Samsonov88459522015-01-12 22:39:12 +0000336 for (const auto *Arg : Args) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000337 const char *DeprecatedReplacement = nullptr;
Alexey Samsonov88459522015-01-12 22:39:12 +0000338 if (Arg->getOption().matches(options::OPT_fsanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000339 DeprecatedReplacement = "-fsanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000340 RecoverableKinds |= expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000341 Arg->claim();
342 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000343 DeprecatedReplacement = "-fno-sanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000344 RecoverableKinds &= ~expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000345 Arg->claim();
346 } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000347 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonov88459522015-01-12 22:39:12 +0000348 // Report error if user explicitly tries to recover from unrecoverable
349 // sanitizer.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000350 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov88459522015-01-12 22:39:12 +0000351 Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
352 SanitizerSet SetToDiagnose;
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000353 SetToDiagnose.Mask |= KindsToDiagnose;
Alexey Samsonov88459522015-01-12 22:39:12 +0000354 D.Diag(diag::err_drv_unsupported_option_argument)
355 << Arg->getOption().getName() << toString(SetToDiagnose);
356 DiagnosedUnrecoverableKinds |= KindsToDiagnose;
357 }
Peter Collingbournebf59c342015-05-11 21:39:20 +0000358 RecoverableKinds |= expandSanitizerGroups(Add);
Alexey Samsonov88459522015-01-12 22:39:12 +0000359 Arg->claim();
360 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000361 RecoverableKinds &= ~expandSanitizerGroups(parseArgValues(D, Arg, true));
Alexey Samsonov88459522015-01-12 22:39:12 +0000362 Arg->claim();
363 }
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000364 if (DeprecatedReplacement) {
365 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
366 << DeprecatedReplacement;
367 }
Alexey Samsonov88459522015-01-12 22:39:12 +0000368 }
369 RecoverableKinds &= Kinds;
370 RecoverableKinds &= ~Unrecoverable;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000371
Peter Collingbourne9881b782015-06-18 23:59:22 +0000372 TrappingKinds &= Kinds;
373
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000374 // Setup blacklist files.
375 // Add default blacklist from resource directory.
376 {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000377 std::string BLPath;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000378 if (getDefaultBlacklist(D, Kinds, BLPath) && llvm::sys::fs::exists(BLPath))
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000379 BlacklistFiles.push_back(BLPath);
380 }
381 // Parse -f(no-)sanitize-blacklist options.
382 for (const auto *Arg : Args) {
383 if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) {
384 Arg->claim();
385 std::string BLPath = Arg->getValue();
386 if (llvm::sys::fs::exists(BLPath))
387 BlacklistFiles.push_back(BLPath);
388 else
389 D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
390 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) {
391 Arg->claim();
392 BlacklistFiles.clear();
393 }
394 }
395 // Validate blacklists format.
396 {
397 std::string BLError;
398 std::unique_ptr<llvm::SpecialCaseList> SCL(
399 llvm::SpecialCaseList::create(BlacklistFiles, BLError));
400 if (!SCL.get())
401 D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000402 }
403
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000404 // Parse -f[no-]sanitize-memory-track-origins[=level] options.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000405 if (AllAddedKinds & Memory) {
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000406 if (Arg *A =
407 Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
408 options::OPT_fsanitize_memory_track_origins,
409 options::OPT_fno_sanitize_memory_track_origins)) {
410 if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
Evgeniy Stepanov6e09bca2015-02-26 15:59:30 +0000411 MsanTrackOrigins = 2;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000412 } else if (A->getOption().matches(
413 options::OPT_fno_sanitize_memory_track_origins)) {
414 MsanTrackOrigins = 0;
415 } else {
416 StringRef S = A->getValue();
417 if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
418 MsanTrackOrigins > 2) {
Nico Weberf2a39a72015-04-13 20:03:03 +0000419 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000420 }
421 }
422 }
Evgeniy Stepanov45be9e02015-07-10 20:07:16 +0000423 MsanUseAfterDtor =
424 Args.hasArg(options::OPT_fsanitize_memory_use_after_dtor);
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000425 }
426
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000427 // Parse -f(no-)?sanitize-coverage flags if coverage is supported by the
428 // enabled sanitizers.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000429 if (AllAddedKinds & SupportsCoverage) {
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000430 for (const auto *Arg : Args) {
431 if (Arg->getOption().matches(options::OPT_fsanitize_coverage)) {
432 Arg->claim();
433 int LegacySanitizeCoverage;
434 if (Arg->getNumValues() == 1 &&
435 !StringRef(Arg->getValue(0))
436 .getAsInteger(0, LegacySanitizeCoverage) &&
437 LegacySanitizeCoverage >= 0 && LegacySanitizeCoverage <= 4) {
438 // TODO: Add deprecation notice for this form.
439 switch (LegacySanitizeCoverage) {
440 case 0:
441 CoverageFeatures = 0;
442 break;
443 case 1:
444 CoverageFeatures = CoverageFunc;
445 break;
446 case 2:
447 CoverageFeatures = CoverageBB;
448 break;
449 case 3:
450 CoverageFeatures = CoverageEdge;
451 break;
452 case 4:
453 CoverageFeatures = CoverageEdge | CoverageIndirCall;
454 break;
455 }
456 continue;
457 }
458 CoverageFeatures |= parseCoverageFeatures(D, Arg);
459 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_coverage)) {
460 Arg->claim();
461 CoverageFeatures &= ~parseCoverageFeatures(D, Arg);
462 }
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000463 }
464 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000465 // Choose at most one coverage type: function, bb, or edge.
466 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageBB))
467 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
468 << "-fsanitize-coverage=func"
469 << "-fsanitize-coverage=bb";
470 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageEdge))
471 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
472 << "-fsanitize-coverage=func"
473 << "-fsanitize-coverage=edge";
474 if ((CoverageFeatures & CoverageBB) && (CoverageFeatures & CoverageEdge))
475 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
476 << "-fsanitize-coverage=bb"
477 << "-fsanitize-coverage=edge";
478 // Basic block tracing and 8-bit counters require some type of coverage
479 // enabled.
480 int CoverageTypes = CoverageFunc | CoverageBB | CoverageEdge;
481 if ((CoverageFeatures & CoverageTraceBB) &&
482 !(CoverageFeatures & CoverageTypes))
483 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
484 << "-fsanitize-coverage=trace-bb"
485 << "-fsanitize-coverage=(func|bb|edge)";
486 if ((CoverageFeatures & Coverage8bitCounters) &&
487 !(CoverageFeatures & CoverageTypes))
488 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
489 << "-fsanitize-coverage=8bit-counters"
490 << "-fsanitize-coverage=(func|bb|edge)";
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000491
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000492 if (AllAddedKinds & Address) {
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000493 AsanSharedRuntime =
Evgeniy Stepanov6f0ae182014-06-05 11:14:00 +0000494 Args.hasArg(options::OPT_shared_libasan) ||
495 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Peter Collingbourne32701642013-11-01 18:16:25 +0000496 AsanZeroBaseShadow =
Evgeniy Stepanovd04b8612014-01-16 10:19:31 +0000497 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000498 if (Arg *A =
499 Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
500 StringRef S = A->getValue();
501 // Legal values are 0 and 1, 2, but in future we may add more levels.
502 if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
503 AsanFieldPadding > 2) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000504 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000505 }
506 }
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000507
508 if (Arg *WindowsDebugRTArg =
509 Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
510 options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
511 options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
512 switch (WindowsDebugRTArg->getOption().getID()) {
513 case options::OPT__SLASH_MTd:
514 case options::OPT__SLASH_MDd:
515 case options::OPT__SLASH_LDd:
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000516 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000517 << WindowsDebugRTArg->getAsString(Args)
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000518 << lastArgumentForMask(D, Args, Address);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000519 D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000520 }
521 }
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000522 }
Alexey Samsonov90490af2014-08-08 22:47:17 +0000523
524 // Parse -link-cxx-sanitizer flag.
525 LinkCXXRuntimes =
526 Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX();
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000527
528 // Finally, initialize the set of available and recoverable sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000529 Sanitizers.Mask |= Kinds;
530 RecoverableSanitizers.Mask |= RecoverableKinds;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000531 TrapSanitizers.Mask |= TrappingKinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000532}
533
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000534static std::string toString(const clang::SanitizerSet &Sanitizers) {
535 std::string Res;
536#define SANITIZER(NAME, ID) \
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000537 if (Sanitizers.has(ID)) { \
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000538 if (!Res.empty()) \
539 Res += ","; \
540 Res += NAME; \
541 }
542#include "clang/Basic/Sanitizers.def"
543 return Res;
544}
545
Peter Collingbourne581f4382015-07-02 01:48:12 +0000546void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
547 llvm::opt::ArgStringList &CmdArgs,
548 types::ID InputType) const {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000549 if (Sanitizers.empty())
Alexey Samsonovcf055962013-08-08 10:11:02 +0000550 return;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000551 CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
552
Alexey Samsonov88459522015-01-12 22:39:12 +0000553 if (!RecoverableSanitizers.empty())
554 CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
555 toString(RecoverableSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000556
Peter Collingbourne9881b782015-06-18 23:59:22 +0000557 if (!TrapSanitizers.empty())
558 CmdArgs.push_back(
559 Args.MakeArgString("-fsanitize-trap=" + toString(TrapSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000560
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000561 for (const auto &BLPath : BlacklistFiles) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000562 SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000563 BlacklistOpt += BLPath;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000564 CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
565 }
566
567 if (MsanTrackOrigins)
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000568 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
569 llvm::utostr(MsanTrackOrigins)));
Evgeniy Stepanov45be9e02015-07-10 20:07:16 +0000570
571 if (MsanUseAfterDtor)
572 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-use-after-dtor"));
573
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000574 if (AsanFieldPadding)
575 CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
576 llvm::utostr(AsanFieldPadding)));
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000577 // Translate available CoverageFeatures to corresponding clang-cc1 flags.
578 std::pair<int, const char *> CoverageFlags[] = {
579 std::make_pair(CoverageFunc, "-fsanitize-coverage-type=1"),
580 std::make_pair(CoverageBB, "-fsanitize-coverage-type=2"),
581 std::make_pair(CoverageEdge, "-fsanitize-coverage-type=3"),
582 std::make_pair(CoverageIndirCall, "-fsanitize-coverage-indirect-calls"),
583 std::make_pair(CoverageTraceBB, "-fsanitize-coverage-trace-bb"),
584 std::make_pair(CoverageTraceCmp, "-fsanitize-coverage-trace-cmp"),
585 std::make_pair(Coverage8bitCounters, "-fsanitize-coverage-8bit-counters")};
586 for (auto F : CoverageFlags) {
587 if (CoverageFeatures & F.first)
588 CmdArgs.push_back(Args.MakeArgString(F.second));
Alexey Samsonov3f3b3ab2015-05-07 18:31:29 +0000589 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000590
591
Sergey Matveev2ba87782015-02-17 15:09:33 +0000592 // MSan: Workaround for PR16386.
593 // ASan: This is mainly to help LSan with cases such as
594 // https://code.google.com/p/address-sanitizer/issues/detail?id=373
595 // We can't make this conditional on -fsanitize=leak, as that flag shouldn't
596 // affect compilation.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000597 if (Sanitizers.has(Memory) || Sanitizers.has(Address))
Alexey Samsonovcf055962013-08-08 10:11:02 +0000598 CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
Peter Collingbourne581f4382015-07-02 01:48:12 +0000599
600 if (TC.getTriple().isOSWindows() && needsUbsanRt()) {
601 // Instruct the code generator to embed linker directives in the object file
602 // that cause the required runtime libraries to be linked.
603 CmdArgs.push_back(Args.MakeArgString(
604 "--dependent-lib=" + tools::getCompilerRT(TC, "ubsan_standalone")));
605 if (types::isCXX(InputType))
606 CmdArgs.push_back(
607 Args.MakeArgString("--dependent-lib=" +
608 tools::getCompilerRT(TC, "ubsan_standalone_cxx")));
609 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000610}
611
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000612SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
613 bool DiagnoseErrors) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000614 assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
Alexey Samsonov88459522015-01-12 22:39:12 +0000615 A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
616 A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
Peter Collingbourne9881b782015-06-18 23:59:22 +0000617 A->getOption().matches(options::OPT_fno_sanitize_recover_EQ) ||
618 A->getOption().matches(options::OPT_fsanitize_trap_EQ) ||
619 A->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) &&
Alexey Samsonov799f7932014-12-19 02:35:16 +0000620 "Invalid argument in parseArgValues!");
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000621 SanitizerMask Kinds = 0;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000622 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
623 const char *Value = A->getValue(i);
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000624 SanitizerMask Kind;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000625 // Special case: don't accept -fsanitize=all.
626 if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
627 0 == strcmp("all", Value))
628 Kind = 0;
629 else
Peter Collingbournebf59c342015-05-11 21:39:20 +0000630 Kind = parseSanitizerValue(Value, /*AllowGroups=*/true);
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000631
632 if (Kind)
633 Kinds |= Kind;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000634 else if (DiagnoseErrors)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000635 D.Diag(clang::diag::err_drv_unsupported_option_argument)
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000636 << A->getOption().getName() << Value;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000637 }
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000638 return Kinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000639}
640
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000641int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A) {
642 assert(A->getOption().matches(options::OPT_fsanitize_coverage) ||
643 A->getOption().matches(options::OPT_fno_sanitize_coverage));
644 int Features = 0;
645 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
646 const char *Value = A->getValue(i);
647 int F = llvm::StringSwitch<int>(Value)
648 .Case("func", CoverageFunc)
649 .Case("bb", CoverageBB)
650 .Case("edge", CoverageEdge)
651 .Case("indirect-calls", CoverageIndirCall)
652 .Case("trace-bb", CoverageTraceBB)
653 .Case("trace-cmp", CoverageTraceCmp)
654 .Case("8bit-counters", Coverage8bitCounters)
655 .Default(0);
656 if (F == 0)
657 D.Diag(clang::diag::err_drv_unsupported_option_argument)
658 << A->getOption().getName() << Value;
659 Features |= F;
660 }
661 return Features;
662}
663
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000664std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000665 SanitizerMask Mask) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000666 for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
667 E = Args.rend();
668 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000669 const auto *Arg = *I;
670 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000671 SanitizerMask AddKinds =
672 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000673 if (AddKinds & Mask)
674 return describeSanitizeArg(Arg, Mask);
675 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000676 SanitizerMask RemoveKinds =
677 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000678 Mask &= ~RemoveKinds;
679 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000680 }
681 llvm_unreachable("arg list didn't provide expected value");
682}
683
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000684std::string describeSanitizeArg(const llvm::opt::Arg *A, SanitizerMask Mask) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000685 assert(A->getOption().matches(options::OPT_fsanitize_EQ)
686 && "Invalid argument in describeSanitizerArg!");
Alexey Samsonovcf055962013-08-08 10:11:02 +0000687
Peter Collingbourne32701642013-11-01 18:16:25 +0000688 std::string Sanitizers;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000689 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000690 if (expandSanitizerGroups(
691 parseSanitizerValue(A->getValue(i), /*AllowGroups=*/true)) &
692 Mask) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000693 if (!Sanitizers.empty())
694 Sanitizers += ",";
Alexey Samsonov83791e22015-03-03 22:15:32 +0000695 Sanitizers += A->getValue(i);
Peter Collingbourne32701642013-11-01 18:16:25 +0000696 }
697 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000698
Peter Collingbourne32701642013-11-01 18:16:25 +0000699 assert(!Sanitizers.empty() && "arg didn't provide expected value");
700 return "-fsanitize=" + Sanitizers;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000701}