blob: c298302c477c8e2bdb417a1a2464e056d5a3ced1 [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"
David L. Jonesf561aba2017-03-08 01:02:16 +000010#include "ToolChains/CommonArgs.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 {
Vedant Kumar42c17ec2017-03-14 01:56:34 +000029 NeedsUbsanRt = Undefined | Integer | Nullability | 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,
Vedant Kumar42c17ec2017-03-14 01:56:34 +000034 SupportsCoverage =
35 Address | Memory | Leak | Undefined | Integer | Nullability | DataFlow,
36 RecoverableByDefault = Undefined | Integer | Nullability,
Yury Gribov5bfeca12015-11-11 10:45:48 +000037 Unrecoverable = Unreachable | Return,
Peter Collingbournea4ccff32015-02-20 20:30:56 +000038 LegacyFsanitizeRecoverMask = Undefined | Integer,
Peter Collingbourne1a7488a2015-04-02 00:23:30 +000039 NeedsLTO = CFI,
Vedant Kumar42c17ec2017-03-14 01:56:34 +000040 TrappingSupported = (Undefined & ~Vptr) | UnsignedIntegerOverflow |
41 Nullability | LocalBounds | CFI,
Peter Collingbourne6708c4a2015-06-19 01:51:54 +000042 TrappingDefault = CFI,
Peter Collingbourne3afb2662016-04-28 17:09:37 +000043 CFIClasses = CFIVCall | CFINVCall | CFIDerivedCast | CFIUnrelatedCast,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000044};
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000045
46enum CoverageFeature {
47 CoverageFunc = 1 << 0,
48 CoverageBB = 1 << 1,
49 CoverageEdge = 1 << 2,
50 CoverageIndirCall = 1 << 3,
51 CoverageTraceBB = 1 << 4,
52 CoverageTraceCmp = 1 << 5,
Kostya Serebryany3b419712016-08-30 01:27:03 +000053 CoverageTraceDiv = 1 << 6,
54 CoverageTraceGep = 1 << 7,
55 Coverage8bitCounters = 1 << 8,
56 CoverageTracePC = 1 << 9,
Kostya Serebryany60cdd612016-09-14 01:39:49 +000057 CoverageTracePCGuard = 1 << 10,
Kostya Serebryany50fb6182017-05-05 23:28:18 +000058 CoverageNoPrune = 1 << 11,
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000059};
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000060
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000061/// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any
Peter Collingbourne3eea6772015-05-11 21:39:14 +000062/// invalid components. Returns a SanitizerMask.
63static SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
64 bool DiagnoseErrors);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000065
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000066/// Parse -f(no-)?sanitize-coverage= flag values, diagnosing any invalid
67/// components. Returns OR of members of \c CoverageFeature enumeration.
68static int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A);
69
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000070/// Produce an argument string from ArgList \p Args, which shows how it
71/// provides some sanitizer kind from \p Mask. For example, the argument list
72/// "-fsanitize=thread,vptr -fsanitize=address" with mask \c NeedsUbsanRt
73/// would produce "-fsanitize=vptr".
74static std::string lastArgumentForMask(const Driver &D,
75 const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +000076 SanitizerMask Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000077
78/// Produce an argument string from argument \p A, which shows how it provides
79/// a value in \p Mask. For instance, the argument
80/// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce
81/// "-fsanitize=alignment".
Peter Collingbourne3eea6772015-05-11 21:39:14 +000082static std::string describeSanitizeArg(const llvm::opt::Arg *A,
83 SanitizerMask Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000084
Alexey Samsonov1e715a62014-11-16 20:53:53 +000085/// Produce a string containing comma-separated names of sanitizers in \p
86/// Sanitizers set.
87static std::string toString(const clang::SanitizerSet &Sanitizers);
88
Peter Collingbourne3eea6772015-05-11 21:39:14 +000089static bool getDefaultBlacklist(const Driver &D, SanitizerMask Kinds,
Alexey Samsonovecf380e2015-03-20 18:45:06 +000090 std::string &BLPath) {
91 const char *BlacklistFile = nullptr;
Peter Collingbourne3eea6772015-05-11 21:39:14 +000092 if (Kinds & Address)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000093 BlacklistFile = "asan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000094 else if (Kinds & Memory)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000095 BlacklistFile = "msan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000096 else if (Kinds & Thread)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000097 BlacklistFile = "tsan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000098 else if (Kinds & DataFlow)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000099 BlacklistFile = "dfsan_abilist.txt";
Peter Collingbourne6fccf952015-07-15 12:15:56 +0000100 else if (Kinds & CFI)
101 BlacklistFile = "cfi_blacklist.txt";
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000102
103 if (BlacklistFile) {
104 clang::SmallString<64> Path(D.ResourceDir);
105 llvm::sys::path::append(Path, BlacklistFile);
106 BLPath = Path.str();
107 return true;
108 }
109 return false;
110}
111
Peter Collingbourne9881b782015-06-18 23:59:22 +0000112/// Sets group bits for every group that has at least one representative already
113/// enabled in \p Kinds.
114static SanitizerMask setGroupBits(SanitizerMask Kinds) {
115#define SANITIZER(NAME, ID)
116#define SANITIZER_GROUP(NAME, ID, ALIAS) \
117 if (Kinds & SanitizerKind::ID) \
118 Kinds |= SanitizerKind::ID##Group;
119#include "clang/Basic/Sanitizers.def"
120 return Kinds;
121}
122
123static SanitizerMask parseSanitizeTrapArgs(const Driver &D,
124 const llvm::opt::ArgList &Args) {
125 SanitizerMask TrapRemove = 0; // During the loop below, the accumulated set of
126 // sanitizers disabled by the current sanitizer
127 // argument or any argument after it.
128 SanitizerMask TrappingKinds = 0;
129 SanitizerMask TrappingSupportedWithGroups = setGroupBits(TrappingSupported);
130
131 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
132 I != E; ++I) {
133 const auto *Arg = *I;
134 if (Arg->getOption().matches(options::OPT_fsanitize_trap_EQ)) {
135 Arg->claim();
136 SanitizerMask Add = parseArgValues(D, Arg, true);
137 Add &= ~TrapRemove;
138 if (SanitizerMask InvalidValues = Add & ~TrappingSupportedWithGroups) {
139 SanitizerSet S;
140 S.Mask = InvalidValues;
141 D.Diag(diag::err_drv_unsupported_option_argument) << "-fsanitize-trap"
142 << toString(S);
143 }
144 TrappingKinds |= expandSanitizerGroups(Add) & ~TrapRemove;
145 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) {
146 Arg->claim();
147 TrapRemove |= expandSanitizerGroups(parseArgValues(D, Arg, true));
148 } else if (Arg->getOption().matches(
149 options::OPT_fsanitize_undefined_trap_on_error)) {
150 Arg->claim();
151 TrappingKinds |=
152 expandSanitizerGroups(UndefinedGroup & ~TrapRemove) & ~TrapRemove;
153 } else if (Arg->getOption().matches(
154 options::OPT_fno_sanitize_undefined_trap_on_error)) {
155 Arg->claim();
156 TrapRemove |= expandSanitizerGroups(UndefinedGroup);
157 }
158 }
159
Peter Collingbourne6708c4a2015-06-19 01:51:54 +0000160 // Apply default trapping behavior.
161 TrappingKinds |= TrappingDefault & ~TrapRemove;
162
Peter Collingbourne9881b782015-06-18 23:59:22 +0000163 return TrappingKinds;
164}
165
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000166bool SanitizerArgs::needsUbsanRt() const {
Evgeniy Stepanov5b49eb42016-06-14 21:33:40 +0000167 return ((Sanitizers.Mask & NeedsUbsanRt & ~TrapSanitizers.Mask) ||
168 CoverageFeatures) &&
169 !Sanitizers.has(Address) && !Sanitizers.has(Memory) &&
Mike Aizatskyd1033f52016-12-08 22:25:01 +0000170 !Sanitizers.has(Thread) && !Sanitizers.has(DataFlow) &&
171 !Sanitizers.has(Leak) && !CfiCrossDso;
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000172}
173
174bool SanitizerArgs::needsCfiRt() const {
Evgeniy Stepanove3fb51c2015-12-16 00:38:42 +0000175 return !(Sanitizers.Mask & CFI & ~TrapSanitizers.Mask) && CfiCrossDso;
176}
177
178bool SanitizerArgs::needsCfiDiagRt() const {
179 return (Sanitizers.Mask & CFI & ~TrapSanitizers.Mask) && CfiCrossDso;
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000180}
181
Dmitry Vyukov43419a72014-11-21 12:19:01 +0000182bool SanitizerArgs::requiresPIE() const {
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +0000183 return NeedPIE || (Sanitizers.Mask & RequiresPIE);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000184}
185
186bool SanitizerArgs::needsUnwindTables() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000187 return Sanitizers.Mask & NeedsUnwindTables;
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000188}
189
Peter Collingbourne32701642013-11-01 18:16:25 +0000190SanitizerArgs::SanitizerArgs(const ToolChain &TC,
191 const llvm::opt::ArgList &Args) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000192 SanitizerMask AllRemove = 0; // During the loop below, the accumulated set of
193 // sanitizers disabled by the current sanitizer
194 // argument or any argument after it.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000195 SanitizerMask AllAddedKinds = 0; // Mask of all sanitizers ever enabled by
196 // -fsanitize= flags (directly or via group
197 // expansion), some of which may be disabled
198 // later. Used to carefully prune
199 // unused-argument diagnostics.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000200 SanitizerMask DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
201 // Used to deduplicate diagnostics.
202 SanitizerMask Kinds = 0;
Alexey Samsonov9b873092015-06-25 23:14:32 +0000203 const SanitizerMask Supported = setGroupBits(TC.getSupportedSanitizers());
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000204 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
205
Peter Collingbourne32701642013-11-01 18:16:25 +0000206 const Driver &D = TC.getDriver();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000207 SanitizerMask TrappingKinds = parseSanitizeTrapArgs(D, Args);
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000208 SanitizerMask InvalidTrappingKinds = TrappingKinds & NotAllowedWithTrap;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000209
Peter Collingbourne32701642013-11-01 18:16:25 +0000210 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
211 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000212 const auto *Arg = *I;
213 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
214 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000215 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000216 AllAddedKinds |= expandSanitizerGroups(Add);
Peter Collingbourne32701642013-11-01 18:16:25 +0000217
Alexey Samsonov799f7932014-12-19 02:35:16 +0000218 // Avoid diagnosing any sanitizer which is disabled later.
219 Add &= ~AllRemove;
220 // At this point we have not expanded groups, so any unsupported
221 // sanitizers in Add are those which have been explicitly enabled.
222 // Diagnose them.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000223 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000224 Add & InvalidTrappingKinds & ~DiagnosedKinds) {
Peter Collingbourne9881b782015-06-18 23:59:22 +0000225 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
226 D.Diag(diag::err_drv_argument_not_allowed_with)
227 << Desc << "-fsanitize-trap=undefined";
228 DiagnosedKinds |= KindsToDiagnose;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000229 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000230 Add &= ~InvalidTrappingKinds;
231 if (SanitizerMask KindsToDiagnose = Add & ~Supported & ~DiagnosedKinds) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000232 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
233 D.Diag(diag::err_drv_unsupported_opt_for_target)
234 << Desc << TC.getTriple().str();
235 DiagnosedKinds |= KindsToDiagnose;
236 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000237 Add &= Supported;
Peter Collingbourne32701642013-11-01 18:16:25 +0000238
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000239 // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups
240 // so we don't error out if -fno-rtti and -fsanitize=undefined were
241 // passed.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000242 if (Add & Vptr &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000243 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
244 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
245 if (RTTIMode == ToolChain::RM_DisabledImplicitly)
246 // Warn about not having rtti enabled if the vptr sanitizer is
247 // explicitly enabled
248 D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default);
249 else {
250 const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg();
251 assert(NoRTTIArg &&
252 "RTTI disabled explicitly but we have no argument!");
253 D.Diag(diag::err_drv_argument_not_allowed_with)
254 << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args);
255 }
256
257 // Take out the Vptr sanitizer from the enabled sanitizers
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000258 AllRemove |= Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000259 }
260
Peter Collingbournebf59c342015-05-11 21:39:20 +0000261 Add = expandSanitizerGroups(Add);
Alexey Samsonov799f7932014-12-19 02:35:16 +0000262 // Group expansion may have enabled a sanitizer which is disabled later.
263 Add &= ~AllRemove;
264 // Silently discard any unsupported sanitizers implicitly enabled through
265 // group expansion.
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000266 Add &= ~InvalidTrappingKinds;
267 Add &= Supported;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000268
George Karpenkovf2fc5b02017-04-24 18:23:24 +0000269 // Enable coverage if the fuzzing flag is set.
270 if (Add & Fuzzer)
271 CoverageFeatures |= CoverageTracePCGuard | CoverageIndirCall | CoverageTraceCmp;
272
Alexey Samsonov799f7932014-12-19 02:35:16 +0000273 Kinds |= Add;
274 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
275 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000276 SanitizerMask Remove = parseArgValues(D, Arg, true);
Peter Collingbournebf59c342015-05-11 21:39:20 +0000277 AllRemove |= expandSanitizerGroups(Remove);
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000278 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000279 }
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000280
Ed Schoutenfc79d2c2016-03-29 21:13:53 +0000281 // Enable toolchain specific default sanitizers if not explicitly disabled.
282 Kinds |= TC.getDefaultSanitizers() & ~AllRemove;
283
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000284 // We disable the vptr sanitizer if it was enabled by group expansion but RTTI
285 // is disabled.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000286 if ((Kinds & Vptr) &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000287 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
288 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000289 Kinds &= ~Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000290 }
291
Alexey Samsonov907880e2015-06-19 19:57:46 +0000292 // Check that LTO is enabled if we need it.
Teresa Johnson945bc502015-10-15 20:35:53 +0000293 if ((Kinds & NeedsLTO) && !D.isUsingLTO()) {
Alexey Samsonov907880e2015-06-19 19:57:46 +0000294 D.Diag(diag::err_drv_argument_only_allowed_with)
295 << lastArgumentForMask(D, Args, Kinds & NeedsLTO) << "-flto";
296 }
297
Alexey Samsonov9b873092015-06-25 23:14:32 +0000298 // Report error if there are non-trapping sanitizers that require
299 // c++abi-specific parts of UBSan runtime, and they are not provided by the
300 // toolchain. We don't have a good way to check the latter, so we just
301 // check if the toolchan supports vptr.
302 if (~Supported & Vptr) {
Peter Collingbourne4bdceaa2015-07-08 21:08:05 +0000303 SanitizerMask KindsToDiagnose = Kinds & ~TrappingKinds & NeedsUbsanCxxRt;
304 // The runtime library supports the Microsoft C++ ABI, but only well enough
305 // for CFI. FIXME: Remove this once we support vptr on Windows.
306 if (TC.getTriple().isOSWindows())
307 KindsToDiagnose &= ~CFI;
308 if (KindsToDiagnose) {
Alexey Samsonov9b873092015-06-25 23:14:32 +0000309 SanitizerSet S;
310 S.Mask = KindsToDiagnose;
311 D.Diag(diag::err_drv_unsupported_opt_for_target)
312 << ("-fno-sanitize-trap=" + toString(S)) << TC.getTriple().str();
313 Kinds &= ~KindsToDiagnose;
314 }
315 }
316
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000317 // Warn about incompatible groups of sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000318 std::pair<SanitizerMask, SanitizerMask> IncompatibleGroups[] = {
319 std::make_pair(Address, Thread), std::make_pair(Address, Memory),
320 std::make_pair(Thread, Memory), std::make_pair(Leak, Thread),
Alexander Potapenkob9b73ef2015-06-19 12:19:07 +0000321 std::make_pair(Leak, Memory), std::make_pair(KernelAddress, Address),
322 std::make_pair(KernelAddress, Leak),
323 std::make_pair(KernelAddress, Thread),
Derek Bruening256c2e12016-04-21 21:32:04 +0000324 std::make_pair(KernelAddress, Memory),
325 std::make_pair(Efficiency, Address),
326 std::make_pair(Efficiency, Leak),
327 std::make_pair(Efficiency, Thread),
328 std::make_pair(Efficiency, Memory),
329 std::make_pair(Efficiency, KernelAddress)};
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000330 for (auto G : IncompatibleGroups) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000331 SanitizerMask Group = G.first;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000332 if (Kinds & Group) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000333 if (SanitizerMask Incompatible = Kinds & G.second) {
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000334 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
335 << lastArgumentForMask(D, Args, Group)
336 << lastArgumentForMask(D, Args, Incompatible);
337 Kinds &= ~Incompatible;
338 }
339 }
340 }
341 // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
342 // -fsanitize=address. Perhaps it should print an error, or perhaps
343 // -f(-no)sanitize=leak should change whether leak detection is enabled by
344 // default in ASan?
345
Alexey Samsonov88459522015-01-12 22:39:12 +0000346 // Parse -f(no-)?sanitize-recover flags.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000347 SanitizerMask RecoverableKinds = RecoverableByDefault;
348 SanitizerMask DiagnosedUnrecoverableKinds = 0;
Alexey Samsonov88459522015-01-12 22:39:12 +0000349 for (const auto *Arg : Args) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000350 const char *DeprecatedReplacement = nullptr;
Alexey Samsonov88459522015-01-12 22:39:12 +0000351 if (Arg->getOption().matches(options::OPT_fsanitize_recover)) {
Kostya Serebryanyceb1add2016-05-04 20:21:47 +0000352 DeprecatedReplacement =
353 "-fsanitize-recover=undefined,integer' or '-fsanitize-recover=all";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000354 RecoverableKinds |= expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000355 Arg->claim();
356 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) {
Kostya Serebryanyceb1add2016-05-04 20:21:47 +0000357 DeprecatedReplacement = "-fno-sanitize-recover=undefined,integer' or "
358 "'-fno-sanitize-recover=all";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000359 RecoverableKinds &= ~expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000360 Arg->claim();
361 } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000362 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonov88459522015-01-12 22:39:12 +0000363 // Report error if user explicitly tries to recover from unrecoverable
364 // sanitizer.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000365 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov88459522015-01-12 22:39:12 +0000366 Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
367 SanitizerSet SetToDiagnose;
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000368 SetToDiagnose.Mask |= KindsToDiagnose;
Alexey Samsonov88459522015-01-12 22:39:12 +0000369 D.Diag(diag::err_drv_unsupported_option_argument)
370 << Arg->getOption().getName() << toString(SetToDiagnose);
371 DiagnosedUnrecoverableKinds |= KindsToDiagnose;
372 }
Peter Collingbournebf59c342015-05-11 21:39:20 +0000373 RecoverableKinds |= expandSanitizerGroups(Add);
Alexey Samsonov88459522015-01-12 22:39:12 +0000374 Arg->claim();
375 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000376 RecoverableKinds &= ~expandSanitizerGroups(parseArgValues(D, Arg, true));
Alexey Samsonov88459522015-01-12 22:39:12 +0000377 Arg->claim();
378 }
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000379 if (DeprecatedReplacement) {
380 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
381 << DeprecatedReplacement;
382 }
Alexey Samsonov88459522015-01-12 22:39:12 +0000383 }
384 RecoverableKinds &= Kinds;
385 RecoverableKinds &= ~Unrecoverable;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000386
Peter Collingbourne9881b782015-06-18 23:59:22 +0000387 TrappingKinds &= Kinds;
388
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000389 // Setup blacklist files.
390 // Add default blacklist from resource directory.
391 {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000392 std::string BLPath;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000393 if (getDefaultBlacklist(D, Kinds, BLPath) && llvm::sys::fs::exists(BLPath))
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000394 BlacklistFiles.push_back(BLPath);
395 }
396 // Parse -f(no-)sanitize-blacklist options.
397 for (const auto *Arg : Args) {
398 if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) {
399 Arg->claim();
400 std::string BLPath = Arg->getValue();
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000401 if (llvm::sys::fs::exists(BLPath)) {
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000402 BlacklistFiles.push_back(BLPath);
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000403 ExtraDeps.push_back(BLPath);
404 } else
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000405 D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000406
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000407 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) {
408 Arg->claim();
409 BlacklistFiles.clear();
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000410 ExtraDeps.clear();
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000411 }
412 }
413 // Validate blacklists format.
414 {
415 std::string BLError;
416 std::unique_ptr<llvm::SpecialCaseList> SCL(
417 llvm::SpecialCaseList::create(BlacklistFiles, BLError));
418 if (!SCL.get())
419 D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000420 }
421
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000422 // Parse -f[no-]sanitize-memory-track-origins[=level] options.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000423 if (AllAddedKinds & Memory) {
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000424 if (Arg *A =
425 Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
426 options::OPT_fsanitize_memory_track_origins,
427 options::OPT_fno_sanitize_memory_track_origins)) {
428 if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
Evgeniy Stepanov6e09bca2015-02-26 15:59:30 +0000429 MsanTrackOrigins = 2;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000430 } else if (A->getOption().matches(
431 options::OPT_fno_sanitize_memory_track_origins)) {
432 MsanTrackOrigins = 0;
433 } else {
434 StringRef S = A->getValue();
435 if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
436 MsanTrackOrigins > 2) {
Nico Weberf2a39a72015-04-13 20:03:03 +0000437 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000438 }
439 }
440 }
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +0000441 MsanUseAfterDtor =
442 Args.hasArg(options::OPT_fsanitize_memory_use_after_dtor);
443 NeedPIE |= !(TC.getTriple().isOSLinux() &&
444 TC.getTriple().getArch() == llvm::Triple::x86_64);
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000445 }
446
Evgeniy Stepanov8a2bedb2016-11-11 23:17:36 +0000447 if (AllAddedKinds & Thread) {
448 TsanMemoryAccess = Args.hasFlag(options::OPT_fsanitize_thread_memory_access,
449 options::OPT_fno_sanitize_thread_memory_access,
450 TsanMemoryAccess);
451 TsanFuncEntryExit = Args.hasFlag(options::OPT_fsanitize_thread_func_entry_exit,
452 options::OPT_fno_sanitize_thread_func_entry_exit,
453 TsanFuncEntryExit);
454 TsanAtomics = Args.hasFlag(options::OPT_fsanitize_thread_atomics,
455 options::OPT_fno_sanitize_thread_atomics,
456 TsanAtomics);
457 }
458
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000459 if (AllAddedKinds & CFI) {
460 CfiCrossDso = Args.hasFlag(options::OPT_fsanitize_cfi_cross_dso,
461 options::OPT_fno_sanitize_cfi_cross_dso, false);
462 // Without PIE, external function address may resolve to a PLT record, which
463 // can not be verified by the target module.
464 NeedPIE |= CfiCrossDso;
465 }
466
Peter Collingbournedc134532016-01-16 00:31:22 +0000467 Stats = Args.hasFlag(options::OPT_fsanitize_stats,
468 options::OPT_fno_sanitize_stats, false);
469
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000470 // Parse -f(no-)?sanitize-coverage flags if coverage is supported by the
471 // enabled sanitizers.
Kostya Serebryany52e86492016-02-18 00:49:23 +0000472 for (const auto *Arg : Args) {
473 if (Arg->getOption().matches(options::OPT_fsanitize_coverage)) {
474 int LegacySanitizeCoverage;
475 if (Arg->getNumValues() == 1 &&
476 !StringRef(Arg->getValue(0))
Kostya Serebryany9d1ed132017-04-19 19:57:16 +0000477 .getAsInteger(0, LegacySanitizeCoverage)) {
478 CoverageFeatures = 0;
479 Arg->claim();
480 if (LegacySanitizeCoverage != 0) {
Nico Weber4152f522016-02-18 19:32:54 +0000481 D.Diag(diag::warn_drv_deprecated_arg)
Kostya Serebryany9d1ed132017-04-19 19:57:16 +0000482 << Arg->getAsString(Args) << "-fsanitize-coverage=trace-pc-guard";
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000483 }
Kostya Serebryany52e86492016-02-18 00:49:23 +0000484 continue;
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000485 }
Kostya Serebryany52e86492016-02-18 00:49:23 +0000486 CoverageFeatures |= parseCoverageFeatures(D, Arg);
Evgeniy Stepanov5b49eb42016-06-14 21:33:40 +0000487
488 // Disable coverage and not claim the flags if there is at least one
489 // non-supporting sanitizer.
490 if (!(AllAddedKinds & ~setGroupBits(SupportsCoverage))) {
Kostya Serebryany52e86492016-02-18 00:49:23 +0000491 Arg->claim();
Kostya Serebryanyf5b25f82016-04-18 21:30:17 +0000492 } else {
493 CoverageFeatures = 0;
Kostya Serebryany52e86492016-02-18 00:49:23 +0000494 }
495 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_coverage)) {
496 Arg->claim();
497 CoverageFeatures &= ~parseCoverageFeatures(D, Arg);
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000498 }
499 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000500 // Choose at most one coverage type: function, bb, or edge.
501 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageBB))
502 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
503 << "-fsanitize-coverage=func"
504 << "-fsanitize-coverage=bb";
505 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageEdge))
506 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
507 << "-fsanitize-coverage=func"
508 << "-fsanitize-coverage=edge";
509 if ((CoverageFeatures & CoverageBB) && (CoverageFeatures & CoverageEdge))
510 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
511 << "-fsanitize-coverage=bb"
512 << "-fsanitize-coverage=edge";
513 // Basic block tracing and 8-bit counters require some type of coverage
514 // enabled.
Kostya Serebryany1c0e9e92017-04-19 21:31:11 +0000515 if (CoverageFeatures & CoverageTraceBB)
516 D.Diag(clang::diag::warn_drv_deprecated_arg)
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000517 << "-fsanitize-coverage=trace-bb"
Kostya Serebryany1c0e9e92017-04-19 21:31:11 +0000518 << "-fsanitize-coverage=trace-pc-guard";
519 if (CoverageFeatures & Coverage8bitCounters)
Kostya Serebryany1a02d8b2017-04-19 20:15:58 +0000520 D.Diag(clang::diag::warn_drv_deprecated_arg)
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000521 << "-fsanitize-coverage=8bit-counters"
Kostya Serebryany1a02d8b2017-04-19 20:15:58 +0000522 << "-fsanitize-coverage=trace-pc-guard";
Kostya Serebryany8955efc2017-05-03 01:27:28 +0000523
524 int InsertionPointTypes = CoverageFunc | CoverageBB | CoverageEdge;
525 if ((CoverageFeatures & InsertionPointTypes) &&
526 !(CoverageFeatures &(CoverageTracePC | CoverageTracePCGuard))) {
527 D.Diag(clang::diag::warn_drv_deprecated_arg)
528 << "-fsanitize-coverage=[func|bb|edge]"
529 << "-fsanitize-coverage=[func|bb|edge],[trace-pc-guard|trace-pc]";
530 }
531
Kostya Serebryany52e86492016-02-18 00:49:23 +0000532 // trace-pc w/o func/bb/edge implies edge.
Kostya Serebryany60cdd612016-09-14 01:39:49 +0000533 if ((CoverageFeatures & (CoverageTracePC | CoverageTracePCGuard)) &&
Kostya Serebryany8955efc2017-05-03 01:27:28 +0000534 !(CoverageFeatures & InsertionPointTypes))
Kostya Serebryany52e86492016-02-18 00:49:23 +0000535 CoverageFeatures |= CoverageEdge;
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000536
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000537 if (AllAddedKinds & Address) {
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000538 AsanSharedRuntime =
Evgeniy Stepanov14deb7b2015-10-08 21:21:44 +0000539 Args.hasArg(options::OPT_shared_libasan) || TC.getTriple().isAndroid();
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +0000540 NeedPIE |= TC.getTriple().isAndroid();
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000541 if (Arg *A =
542 Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
543 StringRef S = A->getValue();
544 // Legal values are 0 and 1, 2, but in future we may add more levels.
545 if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
546 AsanFieldPadding > 2) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000547 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000548 }
549 }
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000550
551 if (Arg *WindowsDebugRTArg =
552 Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
553 options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
554 options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
555 switch (WindowsDebugRTArg->getOption().getID()) {
556 case options::OPT__SLASH_MTd:
557 case options::OPT__SLASH_MDd:
558 case options::OPT__SLASH_LDd:
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000559 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000560 << WindowsDebugRTArg->getAsString(Args)
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000561 << lastArgumentForMask(D, Args, Address);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000562 D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000563 }
564 }
Alexey Samsonov90490af2014-08-08 22:47:17 +0000565
Vitaly Bukaa77ac1b2016-10-10 21:31:50 +0000566 if (Arg *A = Args.getLastArg(
567 options::OPT_fsanitize_address_use_after_scope,
568 options::OPT_fno_sanitize_address_use_after_scope)) {
569 AsanUseAfterScope = A->getOption().getID() ==
570 options::OPT_fsanitize_address_use_after_scope;
571 }
Vitaly Buka9d4eb6f2016-06-02 00:24:20 +0000572 }
573
Alexey Samsonov90490af2014-08-08 22:47:17 +0000574 // Parse -link-cxx-sanitizer flag.
575 LinkCXXRuntimes =
576 Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX();
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000577
578 // Finally, initialize the set of available and recoverable sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000579 Sanitizers.Mask |= Kinds;
580 RecoverableSanitizers.Mask |= RecoverableKinds;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000581 TrapSanitizers.Mask |= TrappingKinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000582}
583
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000584static std::string toString(const clang::SanitizerSet &Sanitizers) {
585 std::string Res;
586#define SANITIZER(NAME, ID) \
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000587 if (Sanitizers.has(ID)) { \
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000588 if (!Res.empty()) \
589 Res += ","; \
590 Res += NAME; \
591 }
592#include "clang/Basic/Sanitizers.def"
593 return Res;
594}
595
Peter Collingbournedc134532016-01-16 00:31:22 +0000596static void addIncludeLinkerOption(const ToolChain &TC,
597 const llvm::opt::ArgList &Args,
598 llvm::opt::ArgStringList &CmdArgs,
599 StringRef SymbolName) {
600 SmallString<64> LinkerOptionFlag;
601 LinkerOptionFlag = "--linker-option=/include:";
602 if (TC.getTriple().getArch() == llvm::Triple::x86) {
603 // Win32 mangles C function names with a '_' prefix.
604 LinkerOptionFlag += '_';
605 }
606 LinkerOptionFlag += SymbolName;
607 CmdArgs.push_back(Args.MakeArgString(LinkerOptionFlag));
608}
609
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000610void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
Peter Collingbourne581f4382015-07-02 01:48:12 +0000611 llvm::opt::ArgStringList &CmdArgs,
612 types::ID InputType) const {
Justin Lebar6efbc732016-09-15 23:44:13 +0000613 // NVPTX doesn't currently support sanitizers. Bailing out here means that
614 // e.g. -fsanitize=address applies only to host code, which is what we want
615 // for now.
616 if (TC.getTriple().isNVPTX())
617 return;
618
Kostya Serebryany52e86492016-02-18 00:49:23 +0000619 // Translate available CoverageFeatures to corresponding clang-cc1 flags.
620 // Do it even if Sanitizers.empty() since some forms of coverage don't require
621 // sanitizers.
622 std::pair<int, const char *> CoverageFlags[] = {
623 std::make_pair(CoverageFunc, "-fsanitize-coverage-type=1"),
624 std::make_pair(CoverageBB, "-fsanitize-coverage-type=2"),
625 std::make_pair(CoverageEdge, "-fsanitize-coverage-type=3"),
626 std::make_pair(CoverageIndirCall, "-fsanitize-coverage-indirect-calls"),
627 std::make_pair(CoverageTraceBB, "-fsanitize-coverage-trace-bb"),
628 std::make_pair(CoverageTraceCmp, "-fsanitize-coverage-trace-cmp"),
Kostya Serebryany3b419712016-08-30 01:27:03 +0000629 std::make_pair(CoverageTraceDiv, "-fsanitize-coverage-trace-div"),
630 std::make_pair(CoverageTraceGep, "-fsanitize-coverage-trace-gep"),
Kostya Serebryany52e86492016-02-18 00:49:23 +0000631 std::make_pair(Coverage8bitCounters, "-fsanitize-coverage-8bit-counters"),
Kostya Serebryany60cdd612016-09-14 01:39:49 +0000632 std::make_pair(CoverageTracePC, "-fsanitize-coverage-trace-pc"),
Kostya Serebryany50fb6182017-05-05 23:28:18 +0000633 std::make_pair(CoverageTracePCGuard, "-fsanitize-coverage-trace-pc-guard"),
634 std::make_pair(CoverageNoPrune, "-fsanitize-coverage-no-prune")};
Kostya Serebryany52e86492016-02-18 00:49:23 +0000635 for (auto F : CoverageFlags) {
636 if (CoverageFeatures & F.first)
637 CmdArgs.push_back(Args.MakeArgString(F.second));
638 }
639
Evgeniy Stepanov5c063c12016-06-14 23:21:19 +0000640 if (TC.getTriple().isOSWindows() && needsUbsanRt()) {
641 // Instruct the code generator to embed linker directives in the object file
642 // that cause the required runtime libraries to be linked.
643 CmdArgs.push_back(Args.MakeArgString(
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000644 "--dependent-lib=" + TC.getCompilerRT(Args, "ubsan_standalone")));
Evgeniy Stepanov5c063c12016-06-14 23:21:19 +0000645 if (types::isCXX(InputType))
646 CmdArgs.push_back(Args.MakeArgString(
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000647 "--dependent-lib=" + TC.getCompilerRT(Args, "ubsan_standalone_cxx")));
Evgeniy Stepanov5c063c12016-06-14 23:21:19 +0000648 }
649 if (TC.getTriple().isOSWindows() && needsStatsRt()) {
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000650 CmdArgs.push_back(Args.MakeArgString("--dependent-lib=" +
651 TC.getCompilerRT(Args, "stats_client")));
Evgeniy Stepanov5c063c12016-06-14 23:21:19 +0000652
653 // The main executable must export the stats runtime.
654 // FIXME: Only exporting from the main executable (e.g. based on whether the
655 // translation unit defines main()) would save a little space, but having
656 // multiple copies of the runtime shouldn't hurt.
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000657 CmdArgs.push_back(Args.MakeArgString("--dependent-lib=" +
658 TC.getCompilerRT(Args, "stats")));
Evgeniy Stepanov5c063c12016-06-14 23:21:19 +0000659 addIncludeLinkerOption(TC, Args, CmdArgs, "__sanitizer_stats_register");
660 }
661
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000662 if (Sanitizers.empty())
Alexey Samsonovcf055962013-08-08 10:11:02 +0000663 return;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000664 CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
665
Alexey Samsonov88459522015-01-12 22:39:12 +0000666 if (!RecoverableSanitizers.empty())
667 CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
668 toString(RecoverableSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000669
Peter Collingbourne9881b782015-06-18 23:59:22 +0000670 if (!TrapSanitizers.empty())
671 CmdArgs.push_back(
672 Args.MakeArgString("-fsanitize-trap=" + toString(TrapSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000673
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000674 for (const auto &BLPath : BlacklistFiles) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000675 SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000676 BlacklistOpt += BLPath;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000677 CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
678 }
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000679 for (const auto &Dep : ExtraDeps) {
680 SmallString<64> ExtraDepOpt("-fdepfile-entry=");
681 ExtraDepOpt += Dep;
682 CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
683 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000684
685 if (MsanTrackOrigins)
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000686 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
687 llvm::utostr(MsanTrackOrigins)));
Evgeniy Stepanov45be9e02015-07-10 20:07:16 +0000688
689 if (MsanUseAfterDtor)
690 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-use-after-dtor"));
691
Evgeniy Stepanov8a2bedb2016-11-11 23:17:36 +0000692 // FIXME: Pass these parameters as function attributes, not as -llvm flags.
693 if (!TsanMemoryAccess) {
694 CmdArgs.push_back("-mllvm");
695 CmdArgs.push_back("-tsan-instrument-memory-accesses=0");
696 CmdArgs.push_back("-mllvm");
697 CmdArgs.push_back("-tsan-instrument-memintrinsics=0");
698 }
699 if (!TsanFuncEntryExit) {
700 CmdArgs.push_back("-mllvm");
701 CmdArgs.push_back("-tsan-instrument-func-entry-exit=0");
702 }
703 if (!TsanAtomics) {
704 CmdArgs.push_back("-mllvm");
705 CmdArgs.push_back("-tsan-instrument-atomics=0");
706 }
707
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000708 if (CfiCrossDso)
709 CmdArgs.push_back(Args.MakeArgString("-fsanitize-cfi-cross-dso"));
710
Peter Collingbournedc134532016-01-16 00:31:22 +0000711 if (Stats)
712 CmdArgs.push_back(Args.MakeArgString("-fsanitize-stats"));
713
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000714 if (AsanFieldPadding)
715 CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
716 llvm::utostr(AsanFieldPadding)));
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000717
Vitaly Buka9d4eb6f2016-06-02 00:24:20 +0000718 if (AsanUseAfterScope)
719 CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-use-after-scope"));
720
Sergey Matveev2ba87782015-02-17 15:09:33 +0000721 // MSan: Workaround for PR16386.
722 // ASan: This is mainly to help LSan with cases such as
723 // https://code.google.com/p/address-sanitizer/issues/detail?id=373
724 // We can't make this conditional on -fsanitize=leak, as that flag shouldn't
725 // affect compilation.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000726 if (Sanitizers.has(Memory) || Sanitizers.has(Address))
Alexey Samsonovcf055962013-08-08 10:11:02 +0000727 CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
Peter Collingbourne581f4382015-07-02 01:48:12 +0000728
Peter Collingbourne3afb2662016-04-28 17:09:37 +0000729 // Require -fvisibility= flag on non-Windows when compiling if vptr CFI is
730 // enabled.
731 if (Sanitizers.hasOneOf(CFIClasses) && !TC.getTriple().isOSWindows() &&
732 !Args.hasArg(options::OPT_fvisibility_EQ)) {
733 TC.getDriver().Diag(clang::diag::err_drv_argument_only_allowed_with)
734 << lastArgumentForMask(TC.getDriver(), Args,
735 Sanitizers.Mask & CFIClasses)
736 << "-fvisibility=";
737 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000738}
739
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000740SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
741 bool DiagnoseErrors) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000742 assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
Alexey Samsonov88459522015-01-12 22:39:12 +0000743 A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
744 A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
Peter Collingbourne9881b782015-06-18 23:59:22 +0000745 A->getOption().matches(options::OPT_fno_sanitize_recover_EQ) ||
746 A->getOption().matches(options::OPT_fsanitize_trap_EQ) ||
747 A->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) &&
Alexey Samsonov799f7932014-12-19 02:35:16 +0000748 "Invalid argument in parseArgValues!");
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000749 SanitizerMask Kinds = 0;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000750 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
751 const char *Value = A->getValue(i);
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000752 SanitizerMask Kind;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000753 // Special case: don't accept -fsanitize=all.
754 if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
755 0 == strcmp("all", Value))
756 Kind = 0;
Derek Bruening256c2e12016-04-21 21:32:04 +0000757 // Similarly, don't accept -fsanitize=efficiency-all.
758 else if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
759 0 == strcmp("efficiency-all", Value))
760 Kind = 0;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000761 else
Peter Collingbournebf59c342015-05-11 21:39:20 +0000762 Kind = parseSanitizerValue(Value, /*AllowGroups=*/true);
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000763
764 if (Kind)
765 Kinds |= Kind;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000766 else if (DiagnoseErrors)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000767 D.Diag(clang::diag::err_drv_unsupported_option_argument)
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000768 << A->getOption().getName() << Value;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000769 }
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000770 return Kinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000771}
772
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000773int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A) {
774 assert(A->getOption().matches(options::OPT_fsanitize_coverage) ||
775 A->getOption().matches(options::OPT_fno_sanitize_coverage));
776 int Features = 0;
777 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
778 const char *Value = A->getValue(i);
779 int F = llvm::StringSwitch<int>(Value)
780 .Case("func", CoverageFunc)
781 .Case("bb", CoverageBB)
782 .Case("edge", CoverageEdge)
783 .Case("indirect-calls", CoverageIndirCall)
784 .Case("trace-bb", CoverageTraceBB)
785 .Case("trace-cmp", CoverageTraceCmp)
Kostya Serebryany3b419712016-08-30 01:27:03 +0000786 .Case("trace-div", CoverageTraceDiv)
787 .Case("trace-gep", CoverageTraceGep)
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000788 .Case("8bit-counters", Coverage8bitCounters)
Kostya Serebryanyd4590c72016-02-17 21:34:43 +0000789 .Case("trace-pc", CoverageTracePC)
Kostya Serebryany60cdd612016-09-14 01:39:49 +0000790 .Case("trace-pc-guard", CoverageTracePCGuard)
Kostya Serebryany50fb6182017-05-05 23:28:18 +0000791 .Case("no-prune", CoverageNoPrune)
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000792 .Default(0);
793 if (F == 0)
794 D.Diag(clang::diag::err_drv_unsupported_option_argument)
795 << A->getOption().getName() << Value;
796 Features |= F;
797 }
798 return Features;
799}
800
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000801std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000802 SanitizerMask Mask) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000803 for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
804 E = Args.rend();
805 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000806 const auto *Arg = *I;
807 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000808 SanitizerMask AddKinds =
809 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000810 if (AddKinds & Mask)
811 return describeSanitizeArg(Arg, Mask);
812 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000813 SanitizerMask RemoveKinds =
814 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000815 Mask &= ~RemoveKinds;
816 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000817 }
818 llvm_unreachable("arg list didn't provide expected value");
819}
820
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000821std::string describeSanitizeArg(const llvm::opt::Arg *A, SanitizerMask Mask) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000822 assert(A->getOption().matches(options::OPT_fsanitize_EQ)
823 && "Invalid argument in describeSanitizerArg!");
Alexey Samsonovcf055962013-08-08 10:11:02 +0000824
Peter Collingbourne32701642013-11-01 18:16:25 +0000825 std::string Sanitizers;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000826 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000827 if (expandSanitizerGroups(
828 parseSanitizerValue(A->getValue(i), /*AllowGroups=*/true)) &
829 Mask) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000830 if (!Sanitizers.empty())
831 Sanitizers += ",";
Alexey Samsonov83791e22015-03-03 22:15:32 +0000832 Sanitizers += A->getValue(i);
Peter Collingbourne32701642013-11-01 18:16:25 +0000833 }
834 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000835
Peter Collingbourne32701642013-11-01 18:16:25 +0000836 assert(!Sanitizers.empty() && "arg didn't provide expected value");
837 return "-fsanitize=" + Sanitizers;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000838}