blob: c3ad8ef9c1effeb0ec0e428133976c00060da6ae [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";
93
94 if (BlacklistFile) {
95 clang::SmallString<64> Path(D.ResourceDir);
96 llvm::sys::path::append(Path, BlacklistFile);
97 BLPath = Path.str();
98 return true;
99 }
100 return false;
101}
102
Peter Collingbourne9881b782015-06-18 23:59:22 +0000103/// Sets group bits for every group that has at least one representative already
104/// enabled in \p Kinds.
105static SanitizerMask setGroupBits(SanitizerMask Kinds) {
106#define SANITIZER(NAME, ID)
107#define SANITIZER_GROUP(NAME, ID, ALIAS) \
108 if (Kinds & SanitizerKind::ID) \
109 Kinds |= SanitizerKind::ID##Group;
110#include "clang/Basic/Sanitizers.def"
111 return Kinds;
112}
113
114static SanitizerMask parseSanitizeTrapArgs(const Driver &D,
115 const llvm::opt::ArgList &Args) {
116 SanitizerMask TrapRemove = 0; // During the loop below, the accumulated set of
117 // sanitizers disabled by the current sanitizer
118 // argument or any argument after it.
119 SanitizerMask TrappingKinds = 0;
120 SanitizerMask TrappingSupportedWithGroups = setGroupBits(TrappingSupported);
121
122 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
123 I != E; ++I) {
124 const auto *Arg = *I;
125 if (Arg->getOption().matches(options::OPT_fsanitize_trap_EQ)) {
126 Arg->claim();
127 SanitizerMask Add = parseArgValues(D, Arg, true);
128 Add &= ~TrapRemove;
129 if (SanitizerMask InvalidValues = Add & ~TrappingSupportedWithGroups) {
130 SanitizerSet S;
131 S.Mask = InvalidValues;
132 D.Diag(diag::err_drv_unsupported_option_argument) << "-fsanitize-trap"
133 << toString(S);
134 }
135 TrappingKinds |= expandSanitizerGroups(Add) & ~TrapRemove;
136 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) {
137 Arg->claim();
138 TrapRemove |= expandSanitizerGroups(parseArgValues(D, Arg, true));
139 } else if (Arg->getOption().matches(
140 options::OPT_fsanitize_undefined_trap_on_error)) {
141 Arg->claim();
142 TrappingKinds |=
143 expandSanitizerGroups(UndefinedGroup & ~TrapRemove) & ~TrapRemove;
144 } else if (Arg->getOption().matches(
145 options::OPT_fno_sanitize_undefined_trap_on_error)) {
146 Arg->claim();
147 TrapRemove |= expandSanitizerGroups(UndefinedGroup);
148 }
149 }
150
Peter Collingbourne6708c4a2015-06-19 01:51:54 +0000151 // Apply default trapping behavior.
152 TrappingKinds |= TrappingDefault & ~TrapRemove;
153
Peter Collingbourne9881b782015-06-18 23:59:22 +0000154 return TrappingKinds;
155}
156
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000157bool SanitizerArgs::needsUbsanRt() const {
Peter Collingbourne9881b782015-06-18 23:59:22 +0000158 return (Sanitizers.Mask & NeedsUbsanRt & ~TrapSanitizers.Mask) &&
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000159 !Sanitizers.has(Address) &&
160 !Sanitizers.has(Memory) &&
161 !Sanitizers.has(Thread);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000162}
163
Dmitry Vyukov43419a72014-11-21 12:19:01 +0000164bool SanitizerArgs::requiresPIE() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000165 return AsanZeroBaseShadow || (Sanitizers.Mask & RequiresPIE);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000166}
167
168bool SanitizerArgs::needsUnwindTables() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000169 return Sanitizers.Mask & NeedsUnwindTables;
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000170}
171
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000172void SanitizerArgs::clear() {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000173 Sanitizers.clear();
Alexey Samsonov88459522015-01-12 22:39:12 +0000174 RecoverableSanitizers.clear();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000175 TrapSanitizers.clear();
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000176 BlacklistFiles.clear();
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000177 CoverageFeatures = 0;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000178 MsanTrackOrigins = 0;
Evgeniy Stepanov45be9e02015-07-10 20:07:16 +0000179 MsanUseAfterDtor = false;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000180 AsanFieldPadding = 0;
Peter Collingbourne32701642013-11-01 18:16:25 +0000181 AsanZeroBaseShadow = false;
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000182 AsanSharedRuntime = false;
Alexey Samsonov90490af2014-08-08 22:47:17 +0000183 LinkCXXRuntimes = false;
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000184}
Alexey Samsonovcf055962013-08-08 10:11:02 +0000185
Peter Collingbourne32701642013-11-01 18:16:25 +0000186SanitizerArgs::SanitizerArgs(const ToolChain &TC,
187 const llvm::opt::ArgList &Args) {
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000188 clear();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000189 SanitizerMask AllRemove = 0; // During the loop below, the accumulated set of
190 // sanitizers disabled by the current sanitizer
191 // argument or any argument after it.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000192 SanitizerMask AllAddedKinds = 0; // Mask of all sanitizers ever enabled by
193 // -fsanitize= flags (directly or via group
194 // expansion), some of which may be disabled
195 // later. Used to carefully prune
196 // unused-argument diagnostics.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000197 SanitizerMask DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
198 // Used to deduplicate diagnostics.
199 SanitizerMask Kinds = 0;
Alexey Samsonov9b873092015-06-25 23:14:32 +0000200 const SanitizerMask Supported = setGroupBits(TC.getSupportedSanitizers());
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000201 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
202
Peter Collingbourne32701642013-11-01 18:16:25 +0000203 const Driver &D = TC.getDriver();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000204 SanitizerMask TrappingKinds = parseSanitizeTrapArgs(D, Args);
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000205 SanitizerMask InvalidTrappingKinds = TrappingKinds & NotAllowedWithTrap;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000206
Peter Collingbourne32701642013-11-01 18:16:25 +0000207 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
208 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000209 const auto *Arg = *I;
210 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
211 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000212 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000213 AllAddedKinds |= expandSanitizerGroups(Add);
Peter Collingbourne32701642013-11-01 18:16:25 +0000214
Alexey Samsonov799f7932014-12-19 02:35:16 +0000215 // Avoid diagnosing any sanitizer which is disabled later.
216 Add &= ~AllRemove;
217 // At this point we have not expanded groups, so any unsupported
218 // sanitizers in Add are those which have been explicitly enabled.
219 // Diagnose them.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000220 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000221 Add & InvalidTrappingKinds & ~DiagnosedKinds) {
Peter Collingbourne9881b782015-06-18 23:59:22 +0000222 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
223 D.Diag(diag::err_drv_argument_not_allowed_with)
224 << Desc << "-fsanitize-trap=undefined";
225 DiagnosedKinds |= KindsToDiagnose;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000226 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000227 Add &= ~InvalidTrappingKinds;
228 if (SanitizerMask KindsToDiagnose = Add & ~Supported & ~DiagnosedKinds) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000229 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
230 D.Diag(diag::err_drv_unsupported_opt_for_target)
231 << Desc << TC.getTriple().str();
232 DiagnosedKinds |= KindsToDiagnose;
233 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000234 Add &= Supported;
Peter Collingbourne32701642013-11-01 18:16:25 +0000235
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000236 // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups
237 // so we don't error out if -fno-rtti and -fsanitize=undefined were
238 // passed.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000239 if (Add & Vptr &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000240 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
241 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
242 if (RTTIMode == ToolChain::RM_DisabledImplicitly)
243 // Warn about not having rtti enabled if the vptr sanitizer is
244 // explicitly enabled
245 D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default);
246 else {
247 const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg();
248 assert(NoRTTIArg &&
249 "RTTI disabled explicitly but we have no argument!");
250 D.Diag(diag::err_drv_argument_not_allowed_with)
251 << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args);
252 }
253
254 // Take out the Vptr sanitizer from the enabled sanitizers
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000255 AllRemove |= Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000256 }
257
Peter Collingbournebf59c342015-05-11 21:39:20 +0000258 Add = expandSanitizerGroups(Add);
Alexey Samsonov799f7932014-12-19 02:35:16 +0000259 // Group expansion may have enabled a sanitizer which is disabled later.
260 Add &= ~AllRemove;
261 // Silently discard any unsupported sanitizers implicitly enabled through
262 // group expansion.
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000263 Add &= ~InvalidTrappingKinds;
264 Add &= Supported;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000265
Alexey Samsonov799f7932014-12-19 02:35:16 +0000266 Kinds |= Add;
267 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
268 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000269 SanitizerMask Remove = parseArgValues(D, Arg, true);
Peter Collingbournebf59c342015-05-11 21:39:20 +0000270 AllRemove |= expandSanitizerGroups(Remove);
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000271 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000272 }
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000273
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000274 // We disable the vptr sanitizer if it was enabled by group expansion but RTTI
275 // is disabled.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000276 if ((Kinds & Vptr) &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000277 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
278 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000279 Kinds &= ~Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000280 }
281
Alexey Samsonov907880e2015-06-19 19:57:46 +0000282 // Check that LTO is enabled if we need it.
283 if ((Kinds & NeedsLTO) && !D.IsUsingLTO(Args)) {
284 D.Diag(diag::err_drv_argument_only_allowed_with)
285 << lastArgumentForMask(D, Args, Kinds & NeedsLTO) << "-flto";
286 }
287
Alexey Samsonov9b873092015-06-25 23:14:32 +0000288 // Report error if there are non-trapping sanitizers that require
289 // c++abi-specific parts of UBSan runtime, and they are not provided by the
290 // toolchain. We don't have a good way to check the latter, so we just
291 // check if the toolchan supports vptr.
292 if (~Supported & Vptr) {
Peter Collingbourne4bdceaa2015-07-08 21:08:05 +0000293 SanitizerMask KindsToDiagnose = Kinds & ~TrappingKinds & NeedsUbsanCxxRt;
294 // The runtime library supports the Microsoft C++ ABI, but only well enough
295 // for CFI. FIXME: Remove this once we support vptr on Windows.
296 if (TC.getTriple().isOSWindows())
297 KindsToDiagnose &= ~CFI;
298 if (KindsToDiagnose) {
Alexey Samsonov9b873092015-06-25 23:14:32 +0000299 SanitizerSet S;
300 S.Mask = KindsToDiagnose;
301 D.Diag(diag::err_drv_unsupported_opt_for_target)
302 << ("-fno-sanitize-trap=" + toString(S)) << TC.getTriple().str();
303 Kinds &= ~KindsToDiagnose;
304 }
305 }
306
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000307 // Warn about incompatible groups of sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000308 std::pair<SanitizerMask, SanitizerMask> IncompatibleGroups[] = {
309 std::make_pair(Address, Thread), std::make_pair(Address, Memory),
310 std::make_pair(Thread, Memory), std::make_pair(Leak, Thread),
Alexander Potapenkob9b73ef2015-06-19 12:19:07 +0000311 std::make_pair(Leak, Memory), std::make_pair(KernelAddress, Address),
312 std::make_pair(KernelAddress, Leak),
313 std::make_pair(KernelAddress, Thread),
314 std::make_pair(KernelAddress, Memory)};
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000315 for (auto G : IncompatibleGroups) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000316 SanitizerMask Group = G.first;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000317 if (Kinds & Group) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000318 if (SanitizerMask Incompatible = Kinds & G.second) {
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000319 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
320 << lastArgumentForMask(D, Args, Group)
321 << lastArgumentForMask(D, Args, Incompatible);
322 Kinds &= ~Incompatible;
323 }
324 }
325 }
326 // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
327 // -fsanitize=address. Perhaps it should print an error, or perhaps
328 // -f(-no)sanitize=leak should change whether leak detection is enabled by
329 // default in ASan?
330
Alexey Samsonov88459522015-01-12 22:39:12 +0000331 // Parse -f(no-)?sanitize-recover flags.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000332 SanitizerMask RecoverableKinds = RecoverableByDefault;
333 SanitizerMask DiagnosedUnrecoverableKinds = 0;
Alexey Samsonov88459522015-01-12 22:39:12 +0000334 for (const auto *Arg : Args) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000335 const char *DeprecatedReplacement = nullptr;
Alexey Samsonov88459522015-01-12 22:39:12 +0000336 if (Arg->getOption().matches(options::OPT_fsanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000337 DeprecatedReplacement = "-fsanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000338 RecoverableKinds |= expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000339 Arg->claim();
340 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000341 DeprecatedReplacement = "-fno-sanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000342 RecoverableKinds &= ~expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000343 Arg->claim();
344 } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000345 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonov88459522015-01-12 22:39:12 +0000346 // Report error if user explicitly tries to recover from unrecoverable
347 // sanitizer.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000348 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov88459522015-01-12 22:39:12 +0000349 Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
350 SanitizerSet SetToDiagnose;
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000351 SetToDiagnose.Mask |= KindsToDiagnose;
Alexey Samsonov88459522015-01-12 22:39:12 +0000352 D.Diag(diag::err_drv_unsupported_option_argument)
353 << Arg->getOption().getName() << toString(SetToDiagnose);
354 DiagnosedUnrecoverableKinds |= KindsToDiagnose;
355 }
Peter Collingbournebf59c342015-05-11 21:39:20 +0000356 RecoverableKinds |= expandSanitizerGroups(Add);
Alexey Samsonov88459522015-01-12 22:39:12 +0000357 Arg->claim();
358 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000359 RecoverableKinds &= ~expandSanitizerGroups(parseArgValues(D, Arg, true));
Alexey Samsonov88459522015-01-12 22:39:12 +0000360 Arg->claim();
361 }
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000362 if (DeprecatedReplacement) {
363 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
364 << DeprecatedReplacement;
365 }
Alexey Samsonov88459522015-01-12 22:39:12 +0000366 }
367 RecoverableKinds &= Kinds;
368 RecoverableKinds &= ~Unrecoverable;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000369
Peter Collingbourne9881b782015-06-18 23:59:22 +0000370 TrappingKinds &= Kinds;
371
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000372 // Setup blacklist files.
373 // Add default blacklist from resource directory.
374 {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000375 std::string BLPath;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000376 if (getDefaultBlacklist(D, Kinds, BLPath) && llvm::sys::fs::exists(BLPath))
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000377 BlacklistFiles.push_back(BLPath);
378 }
379 // Parse -f(no-)sanitize-blacklist options.
380 for (const auto *Arg : Args) {
381 if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) {
382 Arg->claim();
383 std::string BLPath = Arg->getValue();
384 if (llvm::sys::fs::exists(BLPath))
385 BlacklistFiles.push_back(BLPath);
386 else
387 D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
388 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) {
389 Arg->claim();
390 BlacklistFiles.clear();
391 }
392 }
393 // Validate blacklists format.
394 {
395 std::string BLError;
396 std::unique_ptr<llvm::SpecialCaseList> SCL(
397 llvm::SpecialCaseList::create(BlacklistFiles, BLError));
398 if (!SCL.get())
399 D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000400 }
401
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000402 // Parse -f[no-]sanitize-memory-track-origins[=level] options.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000403 if (AllAddedKinds & Memory) {
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000404 if (Arg *A =
405 Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
406 options::OPT_fsanitize_memory_track_origins,
407 options::OPT_fno_sanitize_memory_track_origins)) {
408 if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
Evgeniy Stepanov6e09bca2015-02-26 15:59:30 +0000409 MsanTrackOrigins = 2;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000410 } else if (A->getOption().matches(
411 options::OPT_fno_sanitize_memory_track_origins)) {
412 MsanTrackOrigins = 0;
413 } else {
414 StringRef S = A->getValue();
415 if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
416 MsanTrackOrigins > 2) {
Nico Weberf2a39a72015-04-13 20:03:03 +0000417 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000418 }
419 }
420 }
Evgeniy Stepanov45be9e02015-07-10 20:07:16 +0000421 MsanUseAfterDtor =
422 Args.hasArg(options::OPT_fsanitize_memory_use_after_dtor);
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000423 }
424
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000425 // Parse -f(no-)?sanitize-coverage flags if coverage is supported by the
426 // enabled sanitizers.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000427 if (AllAddedKinds & SupportsCoverage) {
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000428 for (const auto *Arg : Args) {
429 if (Arg->getOption().matches(options::OPT_fsanitize_coverage)) {
430 Arg->claim();
431 int LegacySanitizeCoverage;
432 if (Arg->getNumValues() == 1 &&
433 !StringRef(Arg->getValue(0))
434 .getAsInteger(0, LegacySanitizeCoverage) &&
435 LegacySanitizeCoverage >= 0 && LegacySanitizeCoverage <= 4) {
436 // TODO: Add deprecation notice for this form.
437 switch (LegacySanitizeCoverage) {
438 case 0:
439 CoverageFeatures = 0;
440 break;
441 case 1:
442 CoverageFeatures = CoverageFunc;
443 break;
444 case 2:
445 CoverageFeatures = CoverageBB;
446 break;
447 case 3:
448 CoverageFeatures = CoverageEdge;
449 break;
450 case 4:
451 CoverageFeatures = CoverageEdge | CoverageIndirCall;
452 break;
453 }
454 continue;
455 }
456 CoverageFeatures |= parseCoverageFeatures(D, Arg);
457 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_coverage)) {
458 Arg->claim();
459 CoverageFeatures &= ~parseCoverageFeatures(D, Arg);
460 }
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000461 }
462 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000463 // Choose at most one coverage type: function, bb, or edge.
464 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageBB))
465 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
466 << "-fsanitize-coverage=func"
467 << "-fsanitize-coverage=bb";
468 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageEdge))
469 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
470 << "-fsanitize-coverage=func"
471 << "-fsanitize-coverage=edge";
472 if ((CoverageFeatures & CoverageBB) && (CoverageFeatures & CoverageEdge))
473 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
474 << "-fsanitize-coverage=bb"
475 << "-fsanitize-coverage=edge";
476 // Basic block tracing and 8-bit counters require some type of coverage
477 // enabled.
478 int CoverageTypes = CoverageFunc | CoverageBB | CoverageEdge;
479 if ((CoverageFeatures & CoverageTraceBB) &&
480 !(CoverageFeatures & CoverageTypes))
481 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
482 << "-fsanitize-coverage=trace-bb"
483 << "-fsanitize-coverage=(func|bb|edge)";
484 if ((CoverageFeatures & Coverage8bitCounters) &&
485 !(CoverageFeatures & CoverageTypes))
486 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
487 << "-fsanitize-coverage=8bit-counters"
488 << "-fsanitize-coverage=(func|bb|edge)";
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000489
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000490 if (AllAddedKinds & Address) {
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000491 AsanSharedRuntime =
Evgeniy Stepanov6f0ae182014-06-05 11:14:00 +0000492 Args.hasArg(options::OPT_shared_libasan) ||
493 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Peter Collingbourne32701642013-11-01 18:16:25 +0000494 AsanZeroBaseShadow =
Evgeniy Stepanovd04b8612014-01-16 10:19:31 +0000495 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000496 if (Arg *A =
497 Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
498 StringRef S = A->getValue();
499 // Legal values are 0 and 1, 2, but in future we may add more levels.
500 if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
501 AsanFieldPadding > 2) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000502 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000503 }
504 }
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000505
506 if (Arg *WindowsDebugRTArg =
507 Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
508 options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
509 options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
510 switch (WindowsDebugRTArg->getOption().getID()) {
511 case options::OPT__SLASH_MTd:
512 case options::OPT__SLASH_MDd:
513 case options::OPT__SLASH_LDd:
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000514 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000515 << WindowsDebugRTArg->getAsString(Args)
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000516 << lastArgumentForMask(D, Args, Address);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000517 D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000518 }
519 }
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000520 }
Alexey Samsonov90490af2014-08-08 22:47:17 +0000521
522 // Parse -link-cxx-sanitizer flag.
523 LinkCXXRuntimes =
524 Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX();
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000525
526 // Finally, initialize the set of available and recoverable sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000527 Sanitizers.Mask |= Kinds;
528 RecoverableSanitizers.Mask |= RecoverableKinds;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000529 TrapSanitizers.Mask |= TrappingKinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000530}
531
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000532static std::string toString(const clang::SanitizerSet &Sanitizers) {
533 std::string Res;
534#define SANITIZER(NAME, ID) \
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000535 if (Sanitizers.has(ID)) { \
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000536 if (!Res.empty()) \
537 Res += ","; \
538 Res += NAME; \
539 }
540#include "clang/Basic/Sanitizers.def"
541 return Res;
542}
543
Peter Collingbourne581f4382015-07-02 01:48:12 +0000544void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
545 llvm::opt::ArgStringList &CmdArgs,
546 types::ID InputType) const {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000547 if (Sanitizers.empty())
Alexey Samsonovcf055962013-08-08 10:11:02 +0000548 return;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000549 CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
550
Alexey Samsonov88459522015-01-12 22:39:12 +0000551 if (!RecoverableSanitizers.empty())
552 CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
553 toString(RecoverableSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000554
Peter Collingbourne9881b782015-06-18 23:59:22 +0000555 if (!TrapSanitizers.empty())
556 CmdArgs.push_back(
557 Args.MakeArgString("-fsanitize-trap=" + toString(TrapSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000558
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000559 for (const auto &BLPath : BlacklistFiles) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000560 SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000561 BlacklistOpt += BLPath;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000562 CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
563 }
564
565 if (MsanTrackOrigins)
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000566 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
567 llvm::utostr(MsanTrackOrigins)));
Evgeniy Stepanov45be9e02015-07-10 20:07:16 +0000568
569 if (MsanUseAfterDtor)
570 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-use-after-dtor"));
571
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000572 if (AsanFieldPadding)
573 CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
574 llvm::utostr(AsanFieldPadding)));
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000575 // Translate available CoverageFeatures to corresponding clang-cc1 flags.
576 std::pair<int, const char *> CoverageFlags[] = {
577 std::make_pair(CoverageFunc, "-fsanitize-coverage-type=1"),
578 std::make_pair(CoverageBB, "-fsanitize-coverage-type=2"),
579 std::make_pair(CoverageEdge, "-fsanitize-coverage-type=3"),
580 std::make_pair(CoverageIndirCall, "-fsanitize-coverage-indirect-calls"),
581 std::make_pair(CoverageTraceBB, "-fsanitize-coverage-trace-bb"),
582 std::make_pair(CoverageTraceCmp, "-fsanitize-coverage-trace-cmp"),
583 std::make_pair(Coverage8bitCounters, "-fsanitize-coverage-8bit-counters")};
584 for (auto F : CoverageFlags) {
585 if (CoverageFeatures & F.first)
586 CmdArgs.push_back(Args.MakeArgString(F.second));
Alexey Samsonov3f3b3ab2015-05-07 18:31:29 +0000587 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000588
589
Sergey Matveev2ba87782015-02-17 15:09:33 +0000590 // MSan: Workaround for PR16386.
591 // ASan: This is mainly to help LSan with cases such as
592 // https://code.google.com/p/address-sanitizer/issues/detail?id=373
593 // We can't make this conditional on -fsanitize=leak, as that flag shouldn't
594 // affect compilation.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000595 if (Sanitizers.has(Memory) || Sanitizers.has(Address))
Alexey Samsonovcf055962013-08-08 10:11:02 +0000596 CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
Peter Collingbourne581f4382015-07-02 01:48:12 +0000597
598 if (TC.getTriple().isOSWindows() && needsUbsanRt()) {
599 // Instruct the code generator to embed linker directives in the object file
600 // that cause the required runtime libraries to be linked.
601 CmdArgs.push_back(Args.MakeArgString(
602 "--dependent-lib=" + tools::getCompilerRT(TC, "ubsan_standalone")));
603 if (types::isCXX(InputType))
604 CmdArgs.push_back(
605 Args.MakeArgString("--dependent-lib=" +
606 tools::getCompilerRT(TC, "ubsan_standalone_cxx")));
607 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000608}
609
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000610SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
611 bool DiagnoseErrors) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000612 assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
Alexey Samsonov88459522015-01-12 22:39:12 +0000613 A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
614 A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
Peter Collingbourne9881b782015-06-18 23:59:22 +0000615 A->getOption().matches(options::OPT_fno_sanitize_recover_EQ) ||
616 A->getOption().matches(options::OPT_fsanitize_trap_EQ) ||
617 A->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) &&
Alexey Samsonov799f7932014-12-19 02:35:16 +0000618 "Invalid argument in parseArgValues!");
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000619 SanitizerMask Kinds = 0;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000620 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
621 const char *Value = A->getValue(i);
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000622 SanitizerMask Kind;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000623 // Special case: don't accept -fsanitize=all.
624 if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
625 0 == strcmp("all", Value))
626 Kind = 0;
627 else
Peter Collingbournebf59c342015-05-11 21:39:20 +0000628 Kind = parseSanitizerValue(Value, /*AllowGroups=*/true);
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000629
630 if (Kind)
631 Kinds |= Kind;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000632 else if (DiagnoseErrors)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000633 D.Diag(clang::diag::err_drv_unsupported_option_argument)
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000634 << A->getOption().getName() << Value;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000635 }
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000636 return Kinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000637}
638
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000639int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A) {
640 assert(A->getOption().matches(options::OPT_fsanitize_coverage) ||
641 A->getOption().matches(options::OPT_fno_sanitize_coverage));
642 int Features = 0;
643 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
644 const char *Value = A->getValue(i);
645 int F = llvm::StringSwitch<int>(Value)
646 .Case("func", CoverageFunc)
647 .Case("bb", CoverageBB)
648 .Case("edge", CoverageEdge)
649 .Case("indirect-calls", CoverageIndirCall)
650 .Case("trace-bb", CoverageTraceBB)
651 .Case("trace-cmp", CoverageTraceCmp)
652 .Case("8bit-counters", Coverage8bitCounters)
653 .Default(0);
654 if (F == 0)
655 D.Diag(clang::diag::err_drv_unsupported_option_argument)
656 << A->getOption().getName() << Value;
657 Features |= F;
658 }
659 return Features;
660}
661
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000662std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000663 SanitizerMask Mask) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000664 for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
665 E = Args.rend();
666 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000667 const auto *Arg = *I;
668 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000669 SanitizerMask AddKinds =
670 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000671 if (AddKinds & Mask)
672 return describeSanitizeArg(Arg, Mask);
673 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000674 SanitizerMask RemoveKinds =
675 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000676 Mask &= ~RemoveKinds;
677 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000678 }
679 llvm_unreachable("arg list didn't provide expected value");
680}
681
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000682std::string describeSanitizeArg(const llvm::opt::Arg *A, SanitizerMask Mask) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000683 assert(A->getOption().matches(options::OPT_fsanitize_EQ)
684 && "Invalid argument in describeSanitizerArg!");
Alexey Samsonovcf055962013-08-08 10:11:02 +0000685
Peter Collingbourne32701642013-11-01 18:16:25 +0000686 std::string Sanitizers;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000687 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000688 if (expandSanitizerGroups(
689 parseSanitizerValue(A->getValue(i), /*AllowGroups=*/true)) &
690 Mask) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000691 if (!Sanitizers.empty())
692 Sanitizers += ",";
Alexey Samsonov83791e22015-03-03 22:15:32 +0000693 Sanitizers += A->getValue(i);
Peter Collingbourne32701642013-11-01 18:16:25 +0000694 }
695 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000696
Peter Collingbourne32701642013-11-01 18:16:25 +0000697 assert(!Sanitizers.empty() && "arg didn't provide expected value");
698 return "-fsanitize=" + Sanitizers;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000699}