blob: 50a93a8d431c0723739262a4d8c661fbe922020d [file] [log] [blame]
Alexey Samsonovcf055962013-08-08 10:11:02 +00001//===--- SanitizerArgs.cpp - Arguments for sanitizer tools ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Alexey Samsonov609213f92013-08-19 09:14:21 +00009#include "clang/Driver/SanitizerArgs.h"
Peter Collingbourne581f4382015-07-02 01:48:12 +000010#include "Tools.h"
Peter Collingbournebf59c342015-05-11 21:39:20 +000011#include "clang/Basic/Sanitizers.h"
Alexey Samsonovcf055962013-08-08 10:11:02 +000012#include "clang/Driver/Driver.h"
13#include "clang/Driver/DriverDiagnostic.h"
14#include "clang/Driver/Options.h"
15#include "clang/Driver/ToolChain.h"
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +000016#include "llvm/ADT/StringExtras.h"
Alexey Samsonovcf055962013-08-08 10:11:02 +000017#include "llvm/ADT/StringSwitch.h"
18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/Path.h"
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000020#include "llvm/Support/SpecialCaseList.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000021#include <memory>
Alexey Samsonovcf055962013-08-08 10:11:02 +000022
Peter Collingbourne3eea6772015-05-11 21:39:14 +000023using namespace clang;
24using namespace clang::SanitizerKind;
Alexey Samsonovcf055962013-08-08 10:11:02 +000025using namespace clang::driver;
26using namespace llvm::opt;
27
Peter Collingbourne3eea6772015-05-11 21:39:14 +000028enum : SanitizerMask {
Peter Collingbourne6708c4a2015-06-19 01:51:54 +000029 NeedsUbsanRt = Undefined | Integer | CFI,
Alexey Samsonov9b873092015-06-25 23:14:32 +000030 NeedsUbsanCxxRt = Vptr | CFI,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000031 NotAllowedWithTrap = Vptr,
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +000032 RequiresPIE = DataFlow,
Kostya Serebryany2d88f3d2015-01-06 01:02:48 +000033 NeedsUnwindTables = Address | Thread | Memory | DataFlow,
Kostya Serebryany6fe5fcb2015-03-20 00:06:52 +000034 SupportsCoverage = Address | Memory | Leak | Undefined | Integer | DataFlow,
Alexey Samsonov88459522015-01-12 22:39:12 +000035 RecoverableByDefault = Undefined | Integer,
Yury Gribov5bfeca12015-11-11 10:45:48 +000036 Unrecoverable = Unreachable | Return,
Peter Collingbournea4ccff32015-02-20 20:30:56 +000037 LegacyFsanitizeRecoverMask = Undefined | Integer,
Peter Collingbourne1a7488a2015-04-02 00:23:30 +000038 NeedsLTO = CFI,
Peter Collingbourne9881b782015-06-18 23:59:22 +000039 TrappingSupported =
Peter Collingbourne6708c4a2015-06-19 01:51:54 +000040 (Undefined & ~Vptr) | UnsignedIntegerOverflow | LocalBounds | CFI,
41 TrappingDefault = CFI,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000042};
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000043
44enum CoverageFeature {
45 CoverageFunc = 1 << 0,
46 CoverageBB = 1 << 1,
47 CoverageEdge = 1 << 2,
48 CoverageIndirCall = 1 << 3,
49 CoverageTraceBB = 1 << 4,
50 CoverageTraceCmp = 1 << 5,
51 Coverage8bitCounters = 1 << 6,
Kostya Serebryanyd4590c72016-02-17 21:34:43 +000052 CoverageTracePC = 1 << 7,
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000053};
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000054
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000055/// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any
Peter Collingbourne3eea6772015-05-11 21:39:14 +000056/// invalid components. Returns a SanitizerMask.
57static SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
58 bool DiagnoseErrors);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000059
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000060/// Parse -f(no-)?sanitize-coverage= flag values, diagnosing any invalid
61/// components. Returns OR of members of \c CoverageFeature enumeration.
62static int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A);
63
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000064/// Produce an argument string from ArgList \p Args, which shows how it
65/// provides some sanitizer kind from \p Mask. For example, the argument list
66/// "-fsanitize=thread,vptr -fsanitize=address" with mask \c NeedsUbsanRt
67/// would produce "-fsanitize=vptr".
68static std::string lastArgumentForMask(const Driver &D,
69 const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +000070 SanitizerMask Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000071
72/// Produce an argument string from argument \p A, which shows how it provides
73/// a value in \p Mask. For instance, the argument
74/// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce
75/// "-fsanitize=alignment".
Peter Collingbourne3eea6772015-05-11 21:39:14 +000076static std::string describeSanitizeArg(const llvm::opt::Arg *A,
77 SanitizerMask Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000078
Alexey Samsonov1e715a62014-11-16 20:53:53 +000079/// Produce a string containing comma-separated names of sanitizers in \p
80/// Sanitizers set.
81static std::string toString(const clang::SanitizerSet &Sanitizers);
82
Peter Collingbourne3eea6772015-05-11 21:39:14 +000083static bool getDefaultBlacklist(const Driver &D, SanitizerMask Kinds,
Alexey Samsonovecf380e2015-03-20 18:45:06 +000084 std::string &BLPath) {
85 const char *BlacklistFile = nullptr;
Peter Collingbourne3eea6772015-05-11 21:39:14 +000086 if (Kinds & Address)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000087 BlacklistFile = "asan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000088 else if (Kinds & Memory)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000089 BlacklistFile = "msan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000090 else if (Kinds & Thread)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000091 BlacklistFile = "tsan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000092 else if (Kinds & DataFlow)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000093 BlacklistFile = "dfsan_abilist.txt";
Peter Collingbourne6fccf952015-07-15 12:15:56 +000094 else if (Kinds & CFI)
95 BlacklistFile = "cfi_blacklist.txt";
Alexey Samsonovecf380e2015-03-20 18:45:06 +000096
97 if (BlacklistFile) {
98 clang::SmallString<64> Path(D.ResourceDir);
99 llvm::sys::path::append(Path, BlacklistFile);
100 BLPath = Path.str();
101 return true;
102 }
103 return false;
104}
105
Peter Collingbourne9881b782015-06-18 23:59:22 +0000106/// Sets group bits for every group that has at least one representative already
107/// enabled in \p Kinds.
108static SanitizerMask setGroupBits(SanitizerMask Kinds) {
109#define SANITIZER(NAME, ID)
110#define SANITIZER_GROUP(NAME, ID, ALIAS) \
111 if (Kinds & SanitizerKind::ID) \
112 Kinds |= SanitizerKind::ID##Group;
113#include "clang/Basic/Sanitizers.def"
114 return Kinds;
115}
116
117static SanitizerMask parseSanitizeTrapArgs(const Driver &D,
118 const llvm::opt::ArgList &Args) {
119 SanitizerMask TrapRemove = 0; // During the loop below, the accumulated set of
120 // sanitizers disabled by the current sanitizer
121 // argument or any argument after it.
122 SanitizerMask TrappingKinds = 0;
123 SanitizerMask TrappingSupportedWithGroups = setGroupBits(TrappingSupported);
124
125 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
126 I != E; ++I) {
127 const auto *Arg = *I;
128 if (Arg->getOption().matches(options::OPT_fsanitize_trap_EQ)) {
129 Arg->claim();
130 SanitizerMask Add = parseArgValues(D, Arg, true);
131 Add &= ~TrapRemove;
132 if (SanitizerMask InvalidValues = Add & ~TrappingSupportedWithGroups) {
133 SanitizerSet S;
134 S.Mask = InvalidValues;
135 D.Diag(diag::err_drv_unsupported_option_argument) << "-fsanitize-trap"
136 << toString(S);
137 }
138 TrappingKinds |= expandSanitizerGroups(Add) & ~TrapRemove;
139 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) {
140 Arg->claim();
141 TrapRemove |= expandSanitizerGroups(parseArgValues(D, Arg, true));
142 } else if (Arg->getOption().matches(
143 options::OPT_fsanitize_undefined_trap_on_error)) {
144 Arg->claim();
145 TrappingKinds |=
146 expandSanitizerGroups(UndefinedGroup & ~TrapRemove) & ~TrapRemove;
147 } else if (Arg->getOption().matches(
148 options::OPT_fno_sanitize_undefined_trap_on_error)) {
149 Arg->claim();
150 TrapRemove |= expandSanitizerGroups(UndefinedGroup);
151 }
152 }
153
Peter Collingbourne6708c4a2015-06-19 01:51:54 +0000154 // Apply default trapping behavior.
155 TrappingKinds |= TrappingDefault & ~TrapRemove;
156
Peter Collingbourne9881b782015-06-18 23:59:22 +0000157 return TrappingKinds;
158}
159
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000160bool SanitizerArgs::needsUbsanRt() const {
Peter Collingbourne9881b782015-06-18 23:59:22 +0000161 return (Sanitizers.Mask & NeedsUbsanRt & ~TrapSanitizers.Mask) &&
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000162 !Sanitizers.has(Address) &&
163 !Sanitizers.has(Memory) &&
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000164 !Sanitizers.has(Thread) &&
165 !CfiCrossDso;
166}
167
168bool SanitizerArgs::needsCfiRt() const {
Evgeniy Stepanove3fb51c2015-12-16 00:38:42 +0000169 return !(Sanitizers.Mask & CFI & ~TrapSanitizers.Mask) && CfiCrossDso;
170}
171
172bool SanitizerArgs::needsCfiDiagRt() const {
173 return (Sanitizers.Mask & CFI & ~TrapSanitizers.Mask) && CfiCrossDso;
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000174}
175
Dmitry Vyukov43419a72014-11-21 12:19:01 +0000176bool SanitizerArgs::requiresPIE() const {
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +0000177 return NeedPIE || (Sanitizers.Mask & RequiresPIE);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000178}
179
180bool SanitizerArgs::needsUnwindTables() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000181 return Sanitizers.Mask & NeedsUnwindTables;
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000182}
183
Peter Collingbourne32701642013-11-01 18:16:25 +0000184SanitizerArgs::SanitizerArgs(const ToolChain &TC,
185 const llvm::opt::ArgList &Args) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000186 SanitizerMask AllRemove = 0; // During the loop below, the accumulated set of
187 // sanitizers disabled by the current sanitizer
188 // argument or any argument after it.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000189 SanitizerMask AllAddedKinds = 0; // Mask of all sanitizers ever enabled by
190 // -fsanitize= flags (directly or via group
191 // expansion), some of which may be disabled
192 // later. Used to carefully prune
193 // unused-argument diagnostics.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000194 SanitizerMask DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
195 // Used to deduplicate diagnostics.
196 SanitizerMask Kinds = 0;
Alexey Samsonov9b873092015-06-25 23:14:32 +0000197 const SanitizerMask Supported = setGroupBits(TC.getSupportedSanitizers());
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000198 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
199
Peter Collingbourne32701642013-11-01 18:16:25 +0000200 const Driver &D = TC.getDriver();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000201 SanitizerMask TrappingKinds = parseSanitizeTrapArgs(D, Args);
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000202 SanitizerMask InvalidTrappingKinds = TrappingKinds & NotAllowedWithTrap;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000203
Peter Collingbourne32701642013-11-01 18:16:25 +0000204 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
205 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000206 const auto *Arg = *I;
207 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
208 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000209 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000210 AllAddedKinds |= expandSanitizerGroups(Add);
Peter Collingbourne32701642013-11-01 18:16:25 +0000211
Alexey Samsonov799f7932014-12-19 02:35:16 +0000212 // Avoid diagnosing any sanitizer which is disabled later.
213 Add &= ~AllRemove;
214 // At this point we have not expanded groups, so any unsupported
215 // sanitizers in Add are those which have been explicitly enabled.
216 // Diagnose them.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000217 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000218 Add & InvalidTrappingKinds & ~DiagnosedKinds) {
Peter Collingbourne9881b782015-06-18 23:59:22 +0000219 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
220 D.Diag(diag::err_drv_argument_not_allowed_with)
221 << Desc << "-fsanitize-trap=undefined";
222 DiagnosedKinds |= KindsToDiagnose;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000223 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000224 Add &= ~InvalidTrappingKinds;
225 if (SanitizerMask KindsToDiagnose = Add & ~Supported & ~DiagnosedKinds) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000226 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
227 D.Diag(diag::err_drv_unsupported_opt_for_target)
228 << Desc << TC.getTriple().str();
229 DiagnosedKinds |= KindsToDiagnose;
230 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000231 Add &= Supported;
Peter Collingbourne32701642013-11-01 18:16:25 +0000232
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000233 // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups
234 // so we don't error out if -fno-rtti and -fsanitize=undefined were
235 // passed.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000236 if (Add & Vptr &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000237 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
238 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
239 if (RTTIMode == ToolChain::RM_DisabledImplicitly)
240 // Warn about not having rtti enabled if the vptr sanitizer is
241 // explicitly enabled
242 D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default);
243 else {
244 const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg();
245 assert(NoRTTIArg &&
246 "RTTI disabled explicitly but we have no argument!");
247 D.Diag(diag::err_drv_argument_not_allowed_with)
248 << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args);
249 }
250
251 // Take out the Vptr sanitizer from the enabled sanitizers
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000252 AllRemove |= Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000253 }
254
Peter Collingbournebf59c342015-05-11 21:39:20 +0000255 Add = expandSanitizerGroups(Add);
Alexey Samsonov799f7932014-12-19 02:35:16 +0000256 // Group expansion may have enabled a sanitizer which is disabled later.
257 Add &= ~AllRemove;
258 // Silently discard any unsupported sanitizers implicitly enabled through
259 // group expansion.
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000260 Add &= ~InvalidTrappingKinds;
261 Add &= Supported;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000262
Alexey Samsonov799f7932014-12-19 02:35:16 +0000263 Kinds |= Add;
264 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
265 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000266 SanitizerMask Remove = parseArgValues(D, Arg, true);
Peter Collingbournebf59c342015-05-11 21:39:20 +0000267 AllRemove |= expandSanitizerGroups(Remove);
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000268 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000269 }
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000270
Ed Schoutenfc79d2c2016-03-29 21:13:53 +0000271 // Enable toolchain specific default sanitizers if not explicitly disabled.
272 Kinds |= TC.getDefaultSanitizers() & ~AllRemove;
273
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.
Teresa Johnson945bc502015-10-15 20:35:53 +0000283 if ((Kinds & NeedsLTO) && !D.isUsingLTO()) {
Alexey Samsonov907880e2015-06-19 19:57:46 +0000284 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),
Derek Bruening256c2e12016-04-21 21:32:04 +0000314 std::make_pair(KernelAddress, Memory),
315 std::make_pair(Efficiency, Address),
316 std::make_pair(Efficiency, Leak),
317 std::make_pair(Efficiency, Thread),
318 std::make_pair(Efficiency, Memory),
319 std::make_pair(Efficiency, KernelAddress)};
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000320 for (auto G : IncompatibleGroups) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000321 SanitizerMask Group = G.first;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000322 if (Kinds & Group) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000323 if (SanitizerMask Incompatible = Kinds & G.second) {
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000324 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
325 << lastArgumentForMask(D, Args, Group)
326 << lastArgumentForMask(D, Args, Incompatible);
327 Kinds &= ~Incompatible;
328 }
329 }
330 }
331 // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
332 // -fsanitize=address. Perhaps it should print an error, or perhaps
333 // -f(-no)sanitize=leak should change whether leak detection is enabled by
334 // default in ASan?
335
Alexey Samsonov88459522015-01-12 22:39:12 +0000336 // Parse -f(no-)?sanitize-recover flags.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000337 SanitizerMask RecoverableKinds = RecoverableByDefault;
338 SanitizerMask DiagnosedUnrecoverableKinds = 0;
Alexey Samsonov88459522015-01-12 22:39:12 +0000339 for (const auto *Arg : Args) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000340 const char *DeprecatedReplacement = nullptr;
Alexey Samsonov88459522015-01-12 22:39:12 +0000341 if (Arg->getOption().matches(options::OPT_fsanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000342 DeprecatedReplacement = "-fsanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000343 RecoverableKinds |= expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000344 Arg->claim();
345 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000346 DeprecatedReplacement = "-fno-sanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000347 RecoverableKinds &= ~expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000348 Arg->claim();
349 } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000350 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonov88459522015-01-12 22:39:12 +0000351 // Report error if user explicitly tries to recover from unrecoverable
352 // sanitizer.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000353 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov88459522015-01-12 22:39:12 +0000354 Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
355 SanitizerSet SetToDiagnose;
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000356 SetToDiagnose.Mask |= KindsToDiagnose;
Alexey Samsonov88459522015-01-12 22:39:12 +0000357 D.Diag(diag::err_drv_unsupported_option_argument)
358 << Arg->getOption().getName() << toString(SetToDiagnose);
359 DiagnosedUnrecoverableKinds |= KindsToDiagnose;
360 }
Peter Collingbournebf59c342015-05-11 21:39:20 +0000361 RecoverableKinds |= expandSanitizerGroups(Add);
Alexey Samsonov88459522015-01-12 22:39:12 +0000362 Arg->claim();
363 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000364 RecoverableKinds &= ~expandSanitizerGroups(parseArgValues(D, Arg, true));
Alexey Samsonov88459522015-01-12 22:39:12 +0000365 Arg->claim();
366 }
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000367 if (DeprecatedReplacement) {
368 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
369 << DeprecatedReplacement;
370 }
Alexey Samsonov88459522015-01-12 22:39:12 +0000371 }
372 RecoverableKinds &= Kinds;
373 RecoverableKinds &= ~Unrecoverable;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000374
Peter Collingbourne9881b782015-06-18 23:59:22 +0000375 TrappingKinds &= Kinds;
376
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000377 // Setup blacklist files.
378 // Add default blacklist from resource directory.
379 {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000380 std::string BLPath;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000381 if (getDefaultBlacklist(D, Kinds, BLPath) && llvm::sys::fs::exists(BLPath))
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000382 BlacklistFiles.push_back(BLPath);
383 }
384 // Parse -f(no-)sanitize-blacklist options.
385 for (const auto *Arg : Args) {
386 if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) {
387 Arg->claim();
388 std::string BLPath = Arg->getValue();
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000389 if (llvm::sys::fs::exists(BLPath)) {
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000390 BlacklistFiles.push_back(BLPath);
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000391 ExtraDeps.push_back(BLPath);
392 } else
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000393 D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000394
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000395 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) {
396 Arg->claim();
397 BlacklistFiles.clear();
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000398 ExtraDeps.clear();
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000399 }
400 }
401 // Validate blacklists format.
402 {
403 std::string BLError;
404 std::unique_ptr<llvm::SpecialCaseList> SCL(
405 llvm::SpecialCaseList::create(BlacklistFiles, BLError));
406 if (!SCL.get())
407 D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000408 }
409
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000410 // Parse -f[no-]sanitize-memory-track-origins[=level] options.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000411 if (AllAddedKinds & Memory) {
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000412 if (Arg *A =
413 Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
414 options::OPT_fsanitize_memory_track_origins,
415 options::OPT_fno_sanitize_memory_track_origins)) {
416 if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
Evgeniy Stepanov6e09bca2015-02-26 15:59:30 +0000417 MsanTrackOrigins = 2;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000418 } else if (A->getOption().matches(
419 options::OPT_fno_sanitize_memory_track_origins)) {
420 MsanTrackOrigins = 0;
421 } else {
422 StringRef S = A->getValue();
423 if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
424 MsanTrackOrigins > 2) {
Nico Weberf2a39a72015-04-13 20:03:03 +0000425 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000426 }
427 }
428 }
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +0000429 MsanUseAfterDtor =
430 Args.hasArg(options::OPT_fsanitize_memory_use_after_dtor);
431 NeedPIE |= !(TC.getTriple().isOSLinux() &&
432 TC.getTriple().getArch() == llvm::Triple::x86_64);
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000433 }
434
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000435 if (AllAddedKinds & CFI) {
436 CfiCrossDso = Args.hasFlag(options::OPT_fsanitize_cfi_cross_dso,
437 options::OPT_fno_sanitize_cfi_cross_dso, false);
438 // Without PIE, external function address may resolve to a PLT record, which
439 // can not be verified by the target module.
440 NeedPIE |= CfiCrossDso;
441 }
442
Peter Collingbournedc134532016-01-16 00:31:22 +0000443 Stats = Args.hasFlag(options::OPT_fsanitize_stats,
444 options::OPT_fno_sanitize_stats, false);
445
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000446 // Parse -f(no-)?sanitize-coverage flags if coverage is supported by the
447 // enabled sanitizers.
Kostya Serebryany52e86492016-02-18 00:49:23 +0000448 for (const auto *Arg : Args) {
449 if (Arg->getOption().matches(options::OPT_fsanitize_coverage)) {
450 int LegacySanitizeCoverage;
451 if (Arg->getNumValues() == 1 &&
452 !StringRef(Arg->getValue(0))
453 .getAsInteger(0, LegacySanitizeCoverage) &&
454 LegacySanitizeCoverage >= 0 && LegacySanitizeCoverage <= 4) {
Kostya Serebryany52e86492016-02-18 00:49:23 +0000455 switch (LegacySanitizeCoverage) {
456 case 0:
457 CoverageFeatures = 0;
Kostya Serebryany578787a2016-03-02 19:16:54 +0000458 Arg->claim();
Kostya Serebryany52e86492016-02-18 00:49:23 +0000459 break;
460 case 1:
Nico Weber4152f522016-02-18 19:32:54 +0000461 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
462 << "-fsanitize-coverage=func";
Kostya Serebryany52e86492016-02-18 00:49:23 +0000463 CoverageFeatures = CoverageFunc;
464 break;
465 case 2:
Nico Weber4152f522016-02-18 19:32:54 +0000466 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
467 << "-fsanitize-coverage=bb";
Kostya Serebryany52e86492016-02-18 00:49:23 +0000468 CoverageFeatures = CoverageBB;
469 break;
470 case 3:
Nico Weber4152f522016-02-18 19:32:54 +0000471 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
472 << "-fsanitize-coverage=edge";
Kostya Serebryany52e86492016-02-18 00:49:23 +0000473 CoverageFeatures = CoverageEdge;
474 break;
475 case 4:
Nico Weber4152f522016-02-18 19:32:54 +0000476 D.Diag(diag::warn_drv_deprecated_arg)
477 << Arg->getAsString(Args)
478 << "-fsanitize-coverage=edge,indirect-calls";
Kostya Serebryany52e86492016-02-18 00:49:23 +0000479 CoverageFeatures = CoverageEdge | CoverageIndirCall;
480 break;
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000481 }
Kostya Serebryany52e86492016-02-18 00:49:23 +0000482 continue;
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000483 }
Kostya Serebryany52e86492016-02-18 00:49:23 +0000484 CoverageFeatures |= parseCoverageFeatures(D, Arg);
485 // If there is trace-pc, allow it w/o any of the sanitizers.
486 // Otherwise, require that one of the supported sanitizers is present.
487 if ((CoverageFeatures & CoverageTracePC) ||
488 (AllAddedKinds & SupportsCoverage)) {
489 Arg->claim();
Kostya Serebryanyf5b25f82016-04-18 21:30:17 +0000490 } else {
491 CoverageFeatures = 0;
Kostya Serebryany52e86492016-02-18 00:49:23 +0000492 }
493 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_coverage)) {
494 Arg->claim();
495 CoverageFeatures &= ~parseCoverageFeatures(D, Arg);
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000496 }
497 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000498 // Choose at most one coverage type: function, bb, or edge.
499 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageBB))
500 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
501 << "-fsanitize-coverage=func"
502 << "-fsanitize-coverage=bb";
503 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageEdge))
504 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
505 << "-fsanitize-coverage=func"
506 << "-fsanitize-coverage=edge";
507 if ((CoverageFeatures & CoverageBB) && (CoverageFeatures & CoverageEdge))
508 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
509 << "-fsanitize-coverage=bb"
510 << "-fsanitize-coverage=edge";
511 // Basic block tracing and 8-bit counters require some type of coverage
512 // enabled.
513 int CoverageTypes = CoverageFunc | CoverageBB | CoverageEdge;
514 if ((CoverageFeatures & CoverageTraceBB) &&
515 !(CoverageFeatures & CoverageTypes))
516 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
517 << "-fsanitize-coverage=trace-bb"
518 << "-fsanitize-coverage=(func|bb|edge)";
519 if ((CoverageFeatures & Coverage8bitCounters) &&
520 !(CoverageFeatures & CoverageTypes))
521 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
522 << "-fsanitize-coverage=8bit-counters"
523 << "-fsanitize-coverage=(func|bb|edge)";
Kostya Serebryany52e86492016-02-18 00:49:23 +0000524 // trace-pc w/o func/bb/edge implies edge.
Kostya Serebryanyd4590c72016-02-17 21:34:43 +0000525 if ((CoverageFeatures & CoverageTracePC) &&
526 !(CoverageFeatures & CoverageTypes))
Kostya Serebryany52e86492016-02-18 00:49:23 +0000527 CoverageFeatures |= CoverageEdge;
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000528
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000529 if (AllAddedKinds & Address) {
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000530 AsanSharedRuntime =
Evgeniy Stepanov14deb7b2015-10-08 21:21:44 +0000531 Args.hasArg(options::OPT_shared_libasan) || TC.getTriple().isAndroid();
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +0000532 NeedPIE |= TC.getTriple().isAndroid();
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000533 if (Arg *A =
534 Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
535 StringRef S = A->getValue();
536 // Legal values are 0 and 1, 2, but in future we may add more levels.
537 if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
538 AsanFieldPadding > 2) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000539 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000540 }
541 }
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000542
543 if (Arg *WindowsDebugRTArg =
544 Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
545 options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
546 options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
547 switch (WindowsDebugRTArg->getOption().getID()) {
548 case options::OPT__SLASH_MTd:
549 case options::OPT__SLASH_MDd:
550 case options::OPT__SLASH_LDd:
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000551 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000552 << WindowsDebugRTArg->getAsString(Args)
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000553 << lastArgumentForMask(D, Args, Address);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000554 D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000555 }
556 }
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000557 }
Alexey Samsonov90490af2014-08-08 22:47:17 +0000558
559 // Parse -link-cxx-sanitizer flag.
560 LinkCXXRuntimes =
561 Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX();
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000562
563 // Finally, initialize the set of available and recoverable sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000564 Sanitizers.Mask |= Kinds;
565 RecoverableSanitizers.Mask |= RecoverableKinds;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000566 TrapSanitizers.Mask |= TrappingKinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000567}
568
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000569static std::string toString(const clang::SanitizerSet &Sanitizers) {
570 std::string Res;
571#define SANITIZER(NAME, ID) \
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000572 if (Sanitizers.has(ID)) { \
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000573 if (!Res.empty()) \
574 Res += ","; \
575 Res += NAME; \
576 }
577#include "clang/Basic/Sanitizers.def"
578 return Res;
579}
580
Peter Collingbournedc134532016-01-16 00:31:22 +0000581static void addIncludeLinkerOption(const ToolChain &TC,
582 const llvm::opt::ArgList &Args,
583 llvm::opt::ArgStringList &CmdArgs,
584 StringRef SymbolName) {
585 SmallString<64> LinkerOptionFlag;
586 LinkerOptionFlag = "--linker-option=/include:";
587 if (TC.getTriple().getArch() == llvm::Triple::x86) {
588 // Win32 mangles C function names with a '_' prefix.
589 LinkerOptionFlag += '_';
590 }
591 LinkerOptionFlag += SymbolName;
592 CmdArgs.push_back(Args.MakeArgString(LinkerOptionFlag));
593}
594
Peter Collingbourne581f4382015-07-02 01:48:12 +0000595void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
596 llvm::opt::ArgStringList &CmdArgs,
597 types::ID InputType) const {
Kostya Serebryany52e86492016-02-18 00:49:23 +0000598 // Translate available CoverageFeatures to corresponding clang-cc1 flags.
599 // Do it even if Sanitizers.empty() since some forms of coverage don't require
600 // sanitizers.
601 std::pair<int, const char *> CoverageFlags[] = {
602 std::make_pair(CoverageFunc, "-fsanitize-coverage-type=1"),
603 std::make_pair(CoverageBB, "-fsanitize-coverage-type=2"),
604 std::make_pair(CoverageEdge, "-fsanitize-coverage-type=3"),
605 std::make_pair(CoverageIndirCall, "-fsanitize-coverage-indirect-calls"),
606 std::make_pair(CoverageTraceBB, "-fsanitize-coverage-trace-bb"),
607 std::make_pair(CoverageTraceCmp, "-fsanitize-coverage-trace-cmp"),
608 std::make_pair(Coverage8bitCounters, "-fsanitize-coverage-8bit-counters"),
609 std::make_pair(CoverageTracePC, "-fsanitize-coverage-trace-pc")};
610 for (auto F : CoverageFlags) {
611 if (CoverageFeatures & F.first)
612 CmdArgs.push_back(Args.MakeArgString(F.second));
613 }
614
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000615 if (Sanitizers.empty())
Alexey Samsonovcf055962013-08-08 10:11:02 +0000616 return;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000617 CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
618
Alexey Samsonov88459522015-01-12 22:39:12 +0000619 if (!RecoverableSanitizers.empty())
620 CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
621 toString(RecoverableSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000622
Peter Collingbourne9881b782015-06-18 23:59:22 +0000623 if (!TrapSanitizers.empty())
624 CmdArgs.push_back(
625 Args.MakeArgString("-fsanitize-trap=" + toString(TrapSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000626
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000627 for (const auto &BLPath : BlacklistFiles) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000628 SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000629 BlacklistOpt += BLPath;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000630 CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
631 }
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000632 for (const auto &Dep : ExtraDeps) {
633 SmallString<64> ExtraDepOpt("-fdepfile-entry=");
634 ExtraDepOpt += Dep;
635 CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
636 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000637
638 if (MsanTrackOrigins)
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000639 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
640 llvm::utostr(MsanTrackOrigins)));
Evgeniy Stepanov45be9e02015-07-10 20:07:16 +0000641
642 if (MsanUseAfterDtor)
643 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-use-after-dtor"));
644
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000645 if (CfiCrossDso)
646 CmdArgs.push_back(Args.MakeArgString("-fsanitize-cfi-cross-dso"));
647
Peter Collingbournedc134532016-01-16 00:31:22 +0000648 if (Stats)
649 CmdArgs.push_back(Args.MakeArgString("-fsanitize-stats"));
650
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000651 if (AsanFieldPadding)
652 CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
653 llvm::utostr(AsanFieldPadding)));
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000654
Sergey Matveev2ba87782015-02-17 15:09:33 +0000655 // MSan: Workaround for PR16386.
656 // ASan: This is mainly to help LSan with cases such as
657 // https://code.google.com/p/address-sanitizer/issues/detail?id=373
658 // We can't make this conditional on -fsanitize=leak, as that flag shouldn't
659 // affect compilation.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000660 if (Sanitizers.has(Memory) || Sanitizers.has(Address))
Alexey Samsonovcf055962013-08-08 10:11:02 +0000661 CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
Peter Collingbourne581f4382015-07-02 01:48:12 +0000662
663 if (TC.getTriple().isOSWindows() && needsUbsanRt()) {
664 // Instruct the code generator to embed linker directives in the object file
665 // that cause the required runtime libraries to be linked.
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000666 CmdArgs.push_back(Args.MakeArgString(
667 "--dependent-lib=" + TC.getCompilerRT(Args, "ubsan_standalone")));
Peter Collingbourne581f4382015-07-02 01:48:12 +0000668 if (types::isCXX(InputType))
Saleem Abdulrasoold44901f2015-09-26 03:26:44 +0000669 CmdArgs.push_back(Args.MakeArgString(
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000670 "--dependent-lib=" + TC.getCompilerRT(Args, "ubsan_standalone_cxx")));
Peter Collingbourne581f4382015-07-02 01:48:12 +0000671 }
Peter Collingbournedc134532016-01-16 00:31:22 +0000672 if (TC.getTriple().isOSWindows() && needsStatsRt()) {
673 CmdArgs.push_back(Args.MakeArgString("--dependent-lib=" +
674 TC.getCompilerRT(Args, "stats_client")));
675
676 // The main executable must export the stats runtime.
677 // FIXME: Only exporting from the main executable (e.g. based on whether the
678 // translation unit defines main()) would save a little space, but having
679 // multiple copies of the runtime shouldn't hurt.
680 CmdArgs.push_back(Args.MakeArgString("--dependent-lib=" +
681 TC.getCompilerRT(Args, "stats")));
682 addIncludeLinkerOption(TC, Args, CmdArgs, "__sanitizer_stats_register");
683 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000684}
685
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000686SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
687 bool DiagnoseErrors) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000688 assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
Alexey Samsonov88459522015-01-12 22:39:12 +0000689 A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
690 A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
Peter Collingbourne9881b782015-06-18 23:59:22 +0000691 A->getOption().matches(options::OPT_fno_sanitize_recover_EQ) ||
692 A->getOption().matches(options::OPT_fsanitize_trap_EQ) ||
693 A->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) &&
Alexey Samsonov799f7932014-12-19 02:35:16 +0000694 "Invalid argument in parseArgValues!");
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000695 SanitizerMask Kinds = 0;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000696 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
697 const char *Value = A->getValue(i);
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000698 SanitizerMask Kind;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000699 // Special case: don't accept -fsanitize=all.
700 if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
701 0 == strcmp("all", Value))
702 Kind = 0;
Derek Bruening256c2e12016-04-21 21:32:04 +0000703 // Similarly, don't accept -fsanitize=efficiency-all.
704 else if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
705 0 == strcmp("efficiency-all", Value))
706 Kind = 0;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000707 else
Peter Collingbournebf59c342015-05-11 21:39:20 +0000708 Kind = parseSanitizerValue(Value, /*AllowGroups=*/true);
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000709
710 if (Kind)
711 Kinds |= Kind;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000712 else if (DiagnoseErrors)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000713 D.Diag(clang::diag::err_drv_unsupported_option_argument)
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000714 << A->getOption().getName() << Value;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000715 }
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000716 return Kinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000717}
718
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000719int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A) {
720 assert(A->getOption().matches(options::OPT_fsanitize_coverage) ||
721 A->getOption().matches(options::OPT_fno_sanitize_coverage));
722 int Features = 0;
723 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
724 const char *Value = A->getValue(i);
725 int F = llvm::StringSwitch<int>(Value)
726 .Case("func", CoverageFunc)
727 .Case("bb", CoverageBB)
728 .Case("edge", CoverageEdge)
729 .Case("indirect-calls", CoverageIndirCall)
730 .Case("trace-bb", CoverageTraceBB)
731 .Case("trace-cmp", CoverageTraceCmp)
732 .Case("8bit-counters", Coverage8bitCounters)
Kostya Serebryanyd4590c72016-02-17 21:34:43 +0000733 .Case("trace-pc", CoverageTracePC)
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000734 .Default(0);
735 if (F == 0)
736 D.Diag(clang::diag::err_drv_unsupported_option_argument)
737 << A->getOption().getName() << Value;
738 Features |= F;
739 }
740 return Features;
741}
742
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000743std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000744 SanitizerMask Mask) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000745 for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
746 E = Args.rend();
747 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000748 const auto *Arg = *I;
749 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000750 SanitizerMask AddKinds =
751 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000752 if (AddKinds & Mask)
753 return describeSanitizeArg(Arg, Mask);
754 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000755 SanitizerMask RemoveKinds =
756 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000757 Mask &= ~RemoveKinds;
758 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000759 }
760 llvm_unreachable("arg list didn't provide expected value");
761}
762
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000763std::string describeSanitizeArg(const llvm::opt::Arg *A, SanitizerMask Mask) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000764 assert(A->getOption().matches(options::OPT_fsanitize_EQ)
765 && "Invalid argument in describeSanitizerArg!");
Alexey Samsonovcf055962013-08-08 10:11:02 +0000766
Peter Collingbourne32701642013-11-01 18:16:25 +0000767 std::string Sanitizers;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000768 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000769 if (expandSanitizerGroups(
770 parseSanitizerValue(A->getValue(i), /*AllowGroups=*/true)) &
771 Mask) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000772 if (!Sanitizers.empty())
773 Sanitizers += ",";
Alexey Samsonov83791e22015-03-03 22:15:32 +0000774 Sanitizers += A->getValue(i);
Peter Collingbourne32701642013-11-01 18:16:25 +0000775 }
776 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000777
Peter Collingbourne32701642013-11-01 18:16:25 +0000778 assert(!Sanitizers.empty() && "arg didn't provide expected value");
779 return "-fsanitize=" + Sanitizers;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000780}