blob: d64ac53b69e97d7bdbc82bb048225f3bf2e2effc [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,
Peter Collingbourne3afb2662016-04-28 17:09:37 +000042 CFIClasses = CFIVCall | CFINVCall | CFIDerivedCast | CFIUnrelatedCast,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000043};
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000044
45enum CoverageFeature {
46 CoverageFunc = 1 << 0,
47 CoverageBB = 1 << 1,
48 CoverageEdge = 1 << 2,
49 CoverageIndirCall = 1 << 3,
50 CoverageTraceBB = 1 << 4,
51 CoverageTraceCmp = 1 << 5,
52 Coverage8bitCounters = 1 << 6,
Kostya Serebryanyd4590c72016-02-17 21:34:43 +000053 CoverageTracePC = 1 << 7,
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000054};
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000055
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000056/// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any
Peter Collingbourne3eea6772015-05-11 21:39:14 +000057/// invalid components. Returns a SanitizerMask.
58static SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
59 bool DiagnoseErrors);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000060
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000061/// Parse -f(no-)?sanitize-coverage= flag values, diagnosing any invalid
62/// components. Returns OR of members of \c CoverageFeature enumeration.
63static int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A);
64
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000065/// Produce an argument string from ArgList \p Args, which shows how it
66/// provides some sanitizer kind from \p Mask. For example, the argument list
67/// "-fsanitize=thread,vptr -fsanitize=address" with mask \c NeedsUbsanRt
68/// would produce "-fsanitize=vptr".
69static std::string lastArgumentForMask(const Driver &D,
70 const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +000071 SanitizerMask Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000072
73/// Produce an argument string from argument \p A, which shows how it provides
74/// a value in \p Mask. For instance, the argument
75/// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce
76/// "-fsanitize=alignment".
Peter Collingbourne3eea6772015-05-11 21:39:14 +000077static std::string describeSanitizeArg(const llvm::opt::Arg *A,
78 SanitizerMask Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000079
Alexey Samsonov1e715a62014-11-16 20:53:53 +000080/// Produce a string containing comma-separated names of sanitizers in \p
81/// Sanitizers set.
82static std::string toString(const clang::SanitizerSet &Sanitizers);
83
Peter Collingbourne3eea6772015-05-11 21:39:14 +000084static bool getDefaultBlacklist(const Driver &D, SanitizerMask Kinds,
Alexey Samsonovecf380e2015-03-20 18:45:06 +000085 std::string &BLPath) {
86 const char *BlacklistFile = nullptr;
Peter Collingbourne3eea6772015-05-11 21:39:14 +000087 if (Kinds & Address)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000088 BlacklistFile = "asan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000089 else if (Kinds & Memory)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000090 BlacklistFile = "msan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000091 else if (Kinds & Thread)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000092 BlacklistFile = "tsan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000093 else if (Kinds & DataFlow)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000094 BlacklistFile = "dfsan_abilist.txt";
Peter Collingbourne6fccf952015-07-15 12:15:56 +000095 else if (Kinds & CFI)
96 BlacklistFile = "cfi_blacklist.txt";
Alexey Samsonovecf380e2015-03-20 18:45:06 +000097
98 if (BlacklistFile) {
99 clang::SmallString<64> Path(D.ResourceDir);
100 llvm::sys::path::append(Path, BlacklistFile);
101 BLPath = Path.str();
102 return true;
103 }
104 return false;
105}
106
Peter Collingbourne9881b782015-06-18 23:59:22 +0000107/// Sets group bits for every group that has at least one representative already
108/// enabled in \p Kinds.
109static SanitizerMask setGroupBits(SanitizerMask Kinds) {
110#define SANITIZER(NAME, ID)
111#define SANITIZER_GROUP(NAME, ID, ALIAS) \
112 if (Kinds & SanitizerKind::ID) \
113 Kinds |= SanitizerKind::ID##Group;
114#include "clang/Basic/Sanitizers.def"
115 return Kinds;
116}
117
118static SanitizerMask parseSanitizeTrapArgs(const Driver &D,
119 const llvm::opt::ArgList &Args) {
120 SanitizerMask TrapRemove = 0; // During the loop below, the accumulated set of
121 // sanitizers disabled by the current sanitizer
122 // argument or any argument after it.
123 SanitizerMask TrappingKinds = 0;
124 SanitizerMask TrappingSupportedWithGroups = setGroupBits(TrappingSupported);
125
126 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
127 I != E; ++I) {
128 const auto *Arg = *I;
129 if (Arg->getOption().matches(options::OPT_fsanitize_trap_EQ)) {
130 Arg->claim();
131 SanitizerMask Add = parseArgValues(D, Arg, true);
132 Add &= ~TrapRemove;
133 if (SanitizerMask InvalidValues = Add & ~TrappingSupportedWithGroups) {
134 SanitizerSet S;
135 S.Mask = InvalidValues;
136 D.Diag(diag::err_drv_unsupported_option_argument) << "-fsanitize-trap"
137 << toString(S);
138 }
139 TrappingKinds |= expandSanitizerGroups(Add) & ~TrapRemove;
140 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) {
141 Arg->claim();
142 TrapRemove |= expandSanitizerGroups(parseArgValues(D, Arg, true));
143 } else if (Arg->getOption().matches(
144 options::OPT_fsanitize_undefined_trap_on_error)) {
145 Arg->claim();
146 TrappingKinds |=
147 expandSanitizerGroups(UndefinedGroup & ~TrapRemove) & ~TrapRemove;
148 } else if (Arg->getOption().matches(
149 options::OPT_fno_sanitize_undefined_trap_on_error)) {
150 Arg->claim();
151 TrapRemove |= expandSanitizerGroups(UndefinedGroup);
152 }
153 }
154
Peter Collingbourne6708c4a2015-06-19 01:51:54 +0000155 // Apply default trapping behavior.
156 TrappingKinds |= TrappingDefault & ~TrapRemove;
157
Peter Collingbourne9881b782015-06-18 23:59:22 +0000158 return TrappingKinds;
159}
160
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000161bool SanitizerArgs::needsUbsanRt() const {
Peter Collingbourne9881b782015-06-18 23:59:22 +0000162 return (Sanitizers.Mask & NeedsUbsanRt & ~TrapSanitizers.Mask) &&
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000163 !Sanitizers.has(Address) &&
164 !Sanitizers.has(Memory) &&
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000165 !Sanitizers.has(Thread) &&
166 !CfiCrossDso;
167}
168
169bool SanitizerArgs::needsCfiRt() const {
Evgeniy Stepanove3fb51c2015-12-16 00:38:42 +0000170 return !(Sanitizers.Mask & CFI & ~TrapSanitizers.Mask) && CfiCrossDso;
171}
172
173bool SanitizerArgs::needsCfiDiagRt() const {
174 return (Sanitizers.Mask & CFI & ~TrapSanitizers.Mask) && CfiCrossDso;
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000175}
176
Dmitry Vyukov43419a72014-11-21 12:19:01 +0000177bool SanitizerArgs::requiresPIE() const {
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +0000178 return NeedPIE || (Sanitizers.Mask & RequiresPIE);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000179}
180
181bool SanitizerArgs::needsUnwindTables() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000182 return Sanitizers.Mask & NeedsUnwindTables;
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000183}
184
Peter Collingbourne32701642013-11-01 18:16:25 +0000185SanitizerArgs::SanitizerArgs(const ToolChain &TC,
186 const llvm::opt::ArgList &Args) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000187 SanitizerMask AllRemove = 0; // During the loop below, the accumulated set of
188 // sanitizers disabled by the current sanitizer
189 // argument or any argument after it.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000190 SanitizerMask AllAddedKinds = 0; // Mask of all sanitizers ever enabled by
191 // -fsanitize= flags (directly or via group
192 // expansion), some of which may be disabled
193 // later. Used to carefully prune
194 // unused-argument diagnostics.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000195 SanitizerMask DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
196 // Used to deduplicate diagnostics.
197 SanitizerMask Kinds = 0;
Alexey Samsonov9b873092015-06-25 23:14:32 +0000198 const SanitizerMask Supported = setGroupBits(TC.getSupportedSanitizers());
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000199 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
200
Peter Collingbourne32701642013-11-01 18:16:25 +0000201 const Driver &D = TC.getDriver();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000202 SanitizerMask TrappingKinds = parseSanitizeTrapArgs(D, Args);
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000203 SanitizerMask InvalidTrappingKinds = TrappingKinds & NotAllowedWithTrap;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000204
Peter Collingbourne32701642013-11-01 18:16:25 +0000205 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
206 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000207 const auto *Arg = *I;
208 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
209 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000210 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000211 AllAddedKinds |= expandSanitizerGroups(Add);
Peter Collingbourne32701642013-11-01 18:16:25 +0000212
Alexey Samsonov799f7932014-12-19 02:35:16 +0000213 // Avoid diagnosing any sanitizer which is disabled later.
214 Add &= ~AllRemove;
215 // At this point we have not expanded groups, so any unsupported
216 // sanitizers in Add are those which have been explicitly enabled.
217 // Diagnose them.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000218 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000219 Add & InvalidTrappingKinds & ~DiagnosedKinds) {
Peter Collingbourne9881b782015-06-18 23:59:22 +0000220 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
221 D.Diag(diag::err_drv_argument_not_allowed_with)
222 << Desc << "-fsanitize-trap=undefined";
223 DiagnosedKinds |= KindsToDiagnose;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000224 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000225 Add &= ~InvalidTrappingKinds;
226 if (SanitizerMask KindsToDiagnose = Add & ~Supported & ~DiagnosedKinds) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000227 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
228 D.Diag(diag::err_drv_unsupported_opt_for_target)
229 << Desc << TC.getTriple().str();
230 DiagnosedKinds |= KindsToDiagnose;
231 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000232 Add &= Supported;
Peter Collingbourne32701642013-11-01 18:16:25 +0000233
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000234 // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups
235 // so we don't error out if -fno-rtti and -fsanitize=undefined were
236 // passed.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000237 if (Add & Vptr &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000238 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
239 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
240 if (RTTIMode == ToolChain::RM_DisabledImplicitly)
241 // Warn about not having rtti enabled if the vptr sanitizer is
242 // explicitly enabled
243 D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default);
244 else {
245 const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg();
246 assert(NoRTTIArg &&
247 "RTTI disabled explicitly but we have no argument!");
248 D.Diag(diag::err_drv_argument_not_allowed_with)
249 << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args);
250 }
251
252 // Take out the Vptr sanitizer from the enabled sanitizers
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000253 AllRemove |= Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000254 }
255
Peter Collingbournebf59c342015-05-11 21:39:20 +0000256 Add = expandSanitizerGroups(Add);
Alexey Samsonov799f7932014-12-19 02:35:16 +0000257 // Group expansion may have enabled a sanitizer which is disabled later.
258 Add &= ~AllRemove;
259 // Silently discard any unsupported sanitizers implicitly enabled through
260 // group expansion.
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000261 Add &= ~InvalidTrappingKinds;
262 Add &= Supported;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000263
Alexey Samsonov799f7932014-12-19 02:35:16 +0000264 Kinds |= Add;
265 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
266 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000267 SanitizerMask Remove = parseArgValues(D, Arg, true);
Peter Collingbournebf59c342015-05-11 21:39:20 +0000268 AllRemove |= expandSanitizerGroups(Remove);
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000269 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000270 }
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000271
Ed Schoutenfc79d2c2016-03-29 21:13:53 +0000272 // Enable toolchain specific default sanitizers if not explicitly disabled.
273 Kinds |= TC.getDefaultSanitizers() & ~AllRemove;
274
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000275 // We disable the vptr sanitizer if it was enabled by group expansion but RTTI
276 // is disabled.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000277 if ((Kinds & Vptr) &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000278 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
279 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000280 Kinds &= ~Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000281 }
282
Alexey Samsonov907880e2015-06-19 19:57:46 +0000283 // Check that LTO is enabled if we need it.
Teresa Johnson945bc502015-10-15 20:35:53 +0000284 if ((Kinds & NeedsLTO) && !D.isUsingLTO()) {
Alexey Samsonov907880e2015-06-19 19:57:46 +0000285 D.Diag(diag::err_drv_argument_only_allowed_with)
286 << lastArgumentForMask(D, Args, Kinds & NeedsLTO) << "-flto";
287 }
288
Alexey Samsonov9b873092015-06-25 23:14:32 +0000289 // Report error if there are non-trapping sanitizers that require
290 // c++abi-specific parts of UBSan runtime, and they are not provided by the
291 // toolchain. We don't have a good way to check the latter, so we just
292 // check if the toolchan supports vptr.
293 if (~Supported & Vptr) {
Peter Collingbourne4bdceaa2015-07-08 21:08:05 +0000294 SanitizerMask KindsToDiagnose = Kinds & ~TrappingKinds & NeedsUbsanCxxRt;
295 // The runtime library supports the Microsoft C++ ABI, but only well enough
296 // for CFI. FIXME: Remove this once we support vptr on Windows.
297 if (TC.getTriple().isOSWindows())
298 KindsToDiagnose &= ~CFI;
299 if (KindsToDiagnose) {
Alexey Samsonov9b873092015-06-25 23:14:32 +0000300 SanitizerSet S;
301 S.Mask = KindsToDiagnose;
302 D.Diag(diag::err_drv_unsupported_opt_for_target)
303 << ("-fno-sanitize-trap=" + toString(S)) << TC.getTriple().str();
304 Kinds &= ~KindsToDiagnose;
305 }
306 }
307
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000308 // Warn about incompatible groups of sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000309 std::pair<SanitizerMask, SanitizerMask> IncompatibleGroups[] = {
310 std::make_pair(Address, Thread), std::make_pair(Address, Memory),
311 std::make_pair(Thread, Memory), std::make_pair(Leak, Thread),
Alexander Potapenkob9b73ef2015-06-19 12:19:07 +0000312 std::make_pair(Leak, Memory), std::make_pair(KernelAddress, Address),
313 std::make_pair(KernelAddress, Leak),
314 std::make_pair(KernelAddress, Thread),
Derek Bruening256c2e12016-04-21 21:32:04 +0000315 std::make_pair(KernelAddress, Memory),
316 std::make_pair(Efficiency, Address),
317 std::make_pair(Efficiency, Leak),
318 std::make_pair(Efficiency, Thread),
319 std::make_pair(Efficiency, Memory),
320 std::make_pair(Efficiency, KernelAddress)};
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000321 for (auto G : IncompatibleGroups) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000322 SanitizerMask Group = G.first;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000323 if (Kinds & Group) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000324 if (SanitizerMask Incompatible = Kinds & G.second) {
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000325 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
326 << lastArgumentForMask(D, Args, Group)
327 << lastArgumentForMask(D, Args, Incompatible);
328 Kinds &= ~Incompatible;
329 }
330 }
331 }
332 // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
333 // -fsanitize=address. Perhaps it should print an error, or perhaps
334 // -f(-no)sanitize=leak should change whether leak detection is enabled by
335 // default in ASan?
336
Alexey Samsonov88459522015-01-12 22:39:12 +0000337 // Parse -f(no-)?sanitize-recover flags.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000338 SanitizerMask RecoverableKinds = RecoverableByDefault;
339 SanitizerMask DiagnosedUnrecoverableKinds = 0;
Alexey Samsonov88459522015-01-12 22:39:12 +0000340 for (const auto *Arg : Args) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000341 const char *DeprecatedReplacement = nullptr;
Alexey Samsonov88459522015-01-12 22:39:12 +0000342 if (Arg->getOption().matches(options::OPT_fsanitize_recover)) {
Kostya Serebryanyceb1add2016-05-04 20:21:47 +0000343 DeprecatedReplacement =
344 "-fsanitize-recover=undefined,integer' or '-fsanitize-recover=all";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000345 RecoverableKinds |= expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000346 Arg->claim();
347 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) {
Kostya Serebryanyceb1add2016-05-04 20:21:47 +0000348 DeprecatedReplacement = "-fno-sanitize-recover=undefined,integer' or "
349 "'-fno-sanitize-recover=all";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000350 RecoverableKinds &= ~expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000351 Arg->claim();
352 } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000353 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonov88459522015-01-12 22:39:12 +0000354 // Report error if user explicitly tries to recover from unrecoverable
355 // sanitizer.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000356 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov88459522015-01-12 22:39:12 +0000357 Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
358 SanitizerSet SetToDiagnose;
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000359 SetToDiagnose.Mask |= KindsToDiagnose;
Alexey Samsonov88459522015-01-12 22:39:12 +0000360 D.Diag(diag::err_drv_unsupported_option_argument)
361 << Arg->getOption().getName() << toString(SetToDiagnose);
362 DiagnosedUnrecoverableKinds |= KindsToDiagnose;
363 }
Peter Collingbournebf59c342015-05-11 21:39:20 +0000364 RecoverableKinds |= expandSanitizerGroups(Add);
Alexey Samsonov88459522015-01-12 22:39:12 +0000365 Arg->claim();
366 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000367 RecoverableKinds &= ~expandSanitizerGroups(parseArgValues(D, Arg, true));
Alexey Samsonov88459522015-01-12 22:39:12 +0000368 Arg->claim();
369 }
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000370 if (DeprecatedReplacement) {
371 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
372 << DeprecatedReplacement;
373 }
Alexey Samsonov88459522015-01-12 22:39:12 +0000374 }
375 RecoverableKinds &= Kinds;
376 RecoverableKinds &= ~Unrecoverable;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000377
Peter Collingbourne9881b782015-06-18 23:59:22 +0000378 TrappingKinds &= Kinds;
379
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000380 // Setup blacklist files.
381 // Add default blacklist from resource directory.
382 {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000383 std::string BLPath;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000384 if (getDefaultBlacklist(D, Kinds, BLPath) && llvm::sys::fs::exists(BLPath))
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000385 BlacklistFiles.push_back(BLPath);
386 }
387 // Parse -f(no-)sanitize-blacklist options.
388 for (const auto *Arg : Args) {
389 if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) {
390 Arg->claim();
391 std::string BLPath = Arg->getValue();
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000392 if (llvm::sys::fs::exists(BLPath)) {
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000393 BlacklistFiles.push_back(BLPath);
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000394 ExtraDeps.push_back(BLPath);
395 } else
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000396 D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000397
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000398 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) {
399 Arg->claim();
400 BlacklistFiles.clear();
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000401 ExtraDeps.clear();
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000402 }
403 }
404 // Validate blacklists format.
405 {
406 std::string BLError;
407 std::unique_ptr<llvm::SpecialCaseList> SCL(
408 llvm::SpecialCaseList::create(BlacklistFiles, BLError));
409 if (!SCL.get())
410 D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000411 }
412
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000413 // Parse -f[no-]sanitize-memory-track-origins[=level] options.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000414 if (AllAddedKinds & Memory) {
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000415 if (Arg *A =
416 Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
417 options::OPT_fsanitize_memory_track_origins,
418 options::OPT_fno_sanitize_memory_track_origins)) {
419 if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
Evgeniy Stepanov6e09bca2015-02-26 15:59:30 +0000420 MsanTrackOrigins = 2;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000421 } else if (A->getOption().matches(
422 options::OPT_fno_sanitize_memory_track_origins)) {
423 MsanTrackOrigins = 0;
424 } else {
425 StringRef S = A->getValue();
426 if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
427 MsanTrackOrigins > 2) {
Nico Weberf2a39a72015-04-13 20:03:03 +0000428 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000429 }
430 }
431 }
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +0000432 MsanUseAfterDtor =
433 Args.hasArg(options::OPT_fsanitize_memory_use_after_dtor);
434 NeedPIE |= !(TC.getTriple().isOSLinux() &&
435 TC.getTriple().getArch() == llvm::Triple::x86_64);
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000436 }
437
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000438 if (AllAddedKinds & CFI) {
439 CfiCrossDso = Args.hasFlag(options::OPT_fsanitize_cfi_cross_dso,
440 options::OPT_fno_sanitize_cfi_cross_dso, false);
441 // Without PIE, external function address may resolve to a PLT record, which
442 // can not be verified by the target module.
443 NeedPIE |= CfiCrossDso;
444 }
445
Peter Collingbournedc134532016-01-16 00:31:22 +0000446 Stats = Args.hasFlag(options::OPT_fsanitize_stats,
447 options::OPT_fno_sanitize_stats, false);
448
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000449 // Parse -f(no-)?sanitize-coverage flags if coverage is supported by the
450 // enabled sanitizers.
Kostya Serebryany52e86492016-02-18 00:49:23 +0000451 for (const auto *Arg : Args) {
452 if (Arg->getOption().matches(options::OPT_fsanitize_coverage)) {
453 int LegacySanitizeCoverage;
454 if (Arg->getNumValues() == 1 &&
455 !StringRef(Arg->getValue(0))
456 .getAsInteger(0, LegacySanitizeCoverage) &&
457 LegacySanitizeCoverage >= 0 && LegacySanitizeCoverage <= 4) {
Kostya Serebryany52e86492016-02-18 00:49:23 +0000458 switch (LegacySanitizeCoverage) {
459 case 0:
460 CoverageFeatures = 0;
Kostya Serebryany578787a2016-03-02 19:16:54 +0000461 Arg->claim();
Kostya Serebryany52e86492016-02-18 00:49:23 +0000462 break;
463 case 1:
Nico Weber4152f522016-02-18 19:32:54 +0000464 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
465 << "-fsanitize-coverage=func";
Kostya Serebryany52e86492016-02-18 00:49:23 +0000466 CoverageFeatures = CoverageFunc;
467 break;
468 case 2:
Nico Weber4152f522016-02-18 19:32:54 +0000469 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
470 << "-fsanitize-coverage=bb";
Kostya Serebryany52e86492016-02-18 00:49:23 +0000471 CoverageFeatures = CoverageBB;
472 break;
473 case 3:
Nico Weber4152f522016-02-18 19:32:54 +0000474 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
475 << "-fsanitize-coverage=edge";
Kostya Serebryany52e86492016-02-18 00:49:23 +0000476 CoverageFeatures = CoverageEdge;
477 break;
478 case 4:
Nico Weber4152f522016-02-18 19:32:54 +0000479 D.Diag(diag::warn_drv_deprecated_arg)
480 << Arg->getAsString(Args)
481 << "-fsanitize-coverage=edge,indirect-calls";
Kostya Serebryany52e86492016-02-18 00:49:23 +0000482 CoverageFeatures = CoverageEdge | CoverageIndirCall;
483 break;
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000484 }
Kostya Serebryany52e86492016-02-18 00:49:23 +0000485 continue;
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000486 }
Kostya Serebryany52e86492016-02-18 00:49:23 +0000487 CoverageFeatures |= parseCoverageFeatures(D, Arg);
488 // If there is trace-pc, allow it w/o any of the sanitizers.
489 // Otherwise, require that one of the supported sanitizers is present.
490 if ((CoverageFeatures & CoverageTracePC) ||
491 (AllAddedKinds & SupportsCoverage)) {
492 Arg->claim();
Kostya Serebryanyf5b25f82016-04-18 21:30:17 +0000493 } else {
494 CoverageFeatures = 0;
Kostya Serebryany52e86492016-02-18 00:49:23 +0000495 }
496 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_coverage)) {
497 Arg->claim();
498 CoverageFeatures &= ~parseCoverageFeatures(D, Arg);
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000499 }
500 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000501 // Choose at most one coverage type: function, bb, or edge.
502 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageBB))
503 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
504 << "-fsanitize-coverage=func"
505 << "-fsanitize-coverage=bb";
506 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageEdge))
507 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
508 << "-fsanitize-coverage=func"
509 << "-fsanitize-coverage=edge";
510 if ((CoverageFeatures & CoverageBB) && (CoverageFeatures & CoverageEdge))
511 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
512 << "-fsanitize-coverage=bb"
513 << "-fsanitize-coverage=edge";
514 // Basic block tracing and 8-bit counters require some type of coverage
515 // enabled.
516 int CoverageTypes = CoverageFunc | CoverageBB | CoverageEdge;
517 if ((CoverageFeatures & CoverageTraceBB) &&
518 !(CoverageFeatures & CoverageTypes))
519 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
520 << "-fsanitize-coverage=trace-bb"
521 << "-fsanitize-coverage=(func|bb|edge)";
522 if ((CoverageFeatures & Coverage8bitCounters) &&
523 !(CoverageFeatures & CoverageTypes))
524 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
525 << "-fsanitize-coverage=8bit-counters"
526 << "-fsanitize-coverage=(func|bb|edge)";
Kostya Serebryany52e86492016-02-18 00:49:23 +0000527 // trace-pc w/o func/bb/edge implies edge.
Kostya Serebryanyd4590c72016-02-17 21:34:43 +0000528 if ((CoverageFeatures & CoverageTracePC) &&
529 !(CoverageFeatures & CoverageTypes))
Kostya Serebryany52e86492016-02-18 00:49:23 +0000530 CoverageFeatures |= CoverageEdge;
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000531
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000532 if (AllAddedKinds & Address) {
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000533 AsanSharedRuntime =
Evgeniy Stepanov14deb7b2015-10-08 21:21:44 +0000534 Args.hasArg(options::OPT_shared_libasan) || TC.getTriple().isAndroid();
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +0000535 NeedPIE |= TC.getTriple().isAndroid();
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000536 if (Arg *A =
537 Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
538 StringRef S = A->getValue();
539 // Legal values are 0 and 1, 2, but in future we may add more levels.
540 if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
541 AsanFieldPadding > 2) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000542 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000543 }
544 }
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000545
546 if (Arg *WindowsDebugRTArg =
547 Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
548 options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
549 options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
550 switch (WindowsDebugRTArg->getOption().getID()) {
551 case options::OPT__SLASH_MTd:
552 case options::OPT__SLASH_MDd:
553 case options::OPT__SLASH_LDd:
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000554 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000555 << WindowsDebugRTArg->getAsString(Args)
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000556 << lastArgumentForMask(D, Args, Address);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000557 D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000558 }
559 }
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000560 }
Alexey Samsonov90490af2014-08-08 22:47:17 +0000561
Vitaly Buka9d4eb6f2016-06-02 00:24:20 +0000562 AsanUseAfterScope =
563 Args.hasArg(options::OPT_fsanitize_address_use_after_scope);
564 if (AsanUseAfterScope && !(AllAddedKinds & Address)) {
565 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
566 << "-fsanitize-address-use-after-scope"
567 << "-fsanitize=address";
568 }
569
Alexey Samsonov90490af2014-08-08 22:47:17 +0000570 // Parse -link-cxx-sanitizer flag.
571 LinkCXXRuntimes =
572 Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX();
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000573
574 // Finally, initialize the set of available and recoverable sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000575 Sanitizers.Mask |= Kinds;
576 RecoverableSanitizers.Mask |= RecoverableKinds;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000577 TrapSanitizers.Mask |= TrappingKinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000578}
579
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000580static std::string toString(const clang::SanitizerSet &Sanitizers) {
581 std::string Res;
582#define SANITIZER(NAME, ID) \
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000583 if (Sanitizers.has(ID)) { \
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000584 if (!Res.empty()) \
585 Res += ","; \
586 Res += NAME; \
587 }
588#include "clang/Basic/Sanitizers.def"
589 return Res;
590}
591
Peter Collingbournedc134532016-01-16 00:31:22 +0000592static void addIncludeLinkerOption(const ToolChain &TC,
593 const llvm::opt::ArgList &Args,
594 llvm::opt::ArgStringList &CmdArgs,
595 StringRef SymbolName) {
596 SmallString<64> LinkerOptionFlag;
597 LinkerOptionFlag = "--linker-option=/include:";
598 if (TC.getTriple().getArch() == llvm::Triple::x86) {
599 // Win32 mangles C function names with a '_' prefix.
600 LinkerOptionFlag += '_';
601 }
602 LinkerOptionFlag += SymbolName;
603 CmdArgs.push_back(Args.MakeArgString(LinkerOptionFlag));
604}
605
Peter Collingbourne581f4382015-07-02 01:48:12 +0000606void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
607 llvm::opt::ArgStringList &CmdArgs,
608 types::ID InputType) const {
Kostya Serebryany52e86492016-02-18 00:49:23 +0000609 // Translate available CoverageFeatures to corresponding clang-cc1 flags.
610 // Do it even if Sanitizers.empty() since some forms of coverage don't require
611 // sanitizers.
612 std::pair<int, const char *> CoverageFlags[] = {
613 std::make_pair(CoverageFunc, "-fsanitize-coverage-type=1"),
614 std::make_pair(CoverageBB, "-fsanitize-coverage-type=2"),
615 std::make_pair(CoverageEdge, "-fsanitize-coverage-type=3"),
616 std::make_pair(CoverageIndirCall, "-fsanitize-coverage-indirect-calls"),
617 std::make_pair(CoverageTraceBB, "-fsanitize-coverage-trace-bb"),
618 std::make_pair(CoverageTraceCmp, "-fsanitize-coverage-trace-cmp"),
619 std::make_pair(Coverage8bitCounters, "-fsanitize-coverage-8bit-counters"),
620 std::make_pair(CoverageTracePC, "-fsanitize-coverage-trace-pc")};
621 for (auto F : CoverageFlags) {
622 if (CoverageFeatures & F.first)
623 CmdArgs.push_back(Args.MakeArgString(F.second));
624 }
625
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000626 if (Sanitizers.empty())
Alexey Samsonovcf055962013-08-08 10:11:02 +0000627 return;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000628 CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
629
Alexey Samsonov88459522015-01-12 22:39:12 +0000630 if (!RecoverableSanitizers.empty())
631 CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
632 toString(RecoverableSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000633
Peter Collingbourne9881b782015-06-18 23:59:22 +0000634 if (!TrapSanitizers.empty())
635 CmdArgs.push_back(
636 Args.MakeArgString("-fsanitize-trap=" + toString(TrapSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000637
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000638 for (const auto &BLPath : BlacklistFiles) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000639 SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000640 BlacklistOpt += BLPath;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000641 CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
642 }
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000643 for (const auto &Dep : ExtraDeps) {
644 SmallString<64> ExtraDepOpt("-fdepfile-entry=");
645 ExtraDepOpt += Dep;
646 CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
647 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000648
649 if (MsanTrackOrigins)
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000650 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
651 llvm::utostr(MsanTrackOrigins)));
Evgeniy Stepanov45be9e02015-07-10 20:07:16 +0000652
653 if (MsanUseAfterDtor)
654 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-use-after-dtor"));
655
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000656 if (CfiCrossDso)
657 CmdArgs.push_back(Args.MakeArgString("-fsanitize-cfi-cross-dso"));
658
Peter Collingbournedc134532016-01-16 00:31:22 +0000659 if (Stats)
660 CmdArgs.push_back(Args.MakeArgString("-fsanitize-stats"));
661
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000662 if (AsanFieldPadding)
663 CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
664 llvm::utostr(AsanFieldPadding)));
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000665
Vitaly Buka9d4eb6f2016-06-02 00:24:20 +0000666 if (AsanUseAfterScope)
667 CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-use-after-scope"));
668
Sergey Matveev2ba87782015-02-17 15:09:33 +0000669 // MSan: Workaround for PR16386.
670 // ASan: This is mainly to help LSan with cases such as
671 // https://code.google.com/p/address-sanitizer/issues/detail?id=373
672 // We can't make this conditional on -fsanitize=leak, as that flag shouldn't
673 // affect compilation.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000674 if (Sanitizers.has(Memory) || Sanitizers.has(Address))
Alexey Samsonovcf055962013-08-08 10:11:02 +0000675 CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
Peter Collingbourne581f4382015-07-02 01:48:12 +0000676
677 if (TC.getTriple().isOSWindows() && needsUbsanRt()) {
678 // Instruct the code generator to embed linker directives in the object file
679 // that cause the required runtime libraries to be linked.
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000680 CmdArgs.push_back(Args.MakeArgString(
681 "--dependent-lib=" + TC.getCompilerRT(Args, "ubsan_standalone")));
Peter Collingbourne581f4382015-07-02 01:48:12 +0000682 if (types::isCXX(InputType))
Saleem Abdulrasoold44901f2015-09-26 03:26:44 +0000683 CmdArgs.push_back(Args.MakeArgString(
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000684 "--dependent-lib=" + TC.getCompilerRT(Args, "ubsan_standalone_cxx")));
Peter Collingbourne581f4382015-07-02 01:48:12 +0000685 }
Peter Collingbournedc134532016-01-16 00:31:22 +0000686 if (TC.getTriple().isOSWindows() && needsStatsRt()) {
687 CmdArgs.push_back(Args.MakeArgString("--dependent-lib=" +
688 TC.getCompilerRT(Args, "stats_client")));
689
690 // The main executable must export the stats runtime.
691 // FIXME: Only exporting from the main executable (e.g. based on whether the
692 // translation unit defines main()) would save a little space, but having
693 // multiple copies of the runtime shouldn't hurt.
694 CmdArgs.push_back(Args.MakeArgString("--dependent-lib=" +
695 TC.getCompilerRT(Args, "stats")));
696 addIncludeLinkerOption(TC, Args, CmdArgs, "__sanitizer_stats_register");
697 }
Peter Collingbourne3afb2662016-04-28 17:09:37 +0000698
699 // Require -fvisibility= flag on non-Windows when compiling if vptr CFI is
700 // enabled.
701 if (Sanitizers.hasOneOf(CFIClasses) && !TC.getTriple().isOSWindows() &&
702 !Args.hasArg(options::OPT_fvisibility_EQ)) {
703 TC.getDriver().Diag(clang::diag::err_drv_argument_only_allowed_with)
704 << lastArgumentForMask(TC.getDriver(), Args,
705 Sanitizers.Mask & CFIClasses)
706 << "-fvisibility=";
707 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000708}
709
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000710SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
711 bool DiagnoseErrors) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000712 assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
Alexey Samsonov88459522015-01-12 22:39:12 +0000713 A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
714 A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
Peter Collingbourne9881b782015-06-18 23:59:22 +0000715 A->getOption().matches(options::OPT_fno_sanitize_recover_EQ) ||
716 A->getOption().matches(options::OPT_fsanitize_trap_EQ) ||
717 A->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) &&
Alexey Samsonov799f7932014-12-19 02:35:16 +0000718 "Invalid argument in parseArgValues!");
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000719 SanitizerMask Kinds = 0;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000720 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
721 const char *Value = A->getValue(i);
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000722 SanitizerMask Kind;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000723 // Special case: don't accept -fsanitize=all.
724 if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
725 0 == strcmp("all", Value))
726 Kind = 0;
Derek Bruening256c2e12016-04-21 21:32:04 +0000727 // Similarly, don't accept -fsanitize=efficiency-all.
728 else if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
729 0 == strcmp("efficiency-all", Value))
730 Kind = 0;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000731 else
Peter Collingbournebf59c342015-05-11 21:39:20 +0000732 Kind = parseSanitizerValue(Value, /*AllowGroups=*/true);
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000733
734 if (Kind)
735 Kinds |= Kind;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000736 else if (DiagnoseErrors)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000737 D.Diag(clang::diag::err_drv_unsupported_option_argument)
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000738 << A->getOption().getName() << Value;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000739 }
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000740 return Kinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000741}
742
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000743int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A) {
744 assert(A->getOption().matches(options::OPT_fsanitize_coverage) ||
745 A->getOption().matches(options::OPT_fno_sanitize_coverage));
746 int Features = 0;
747 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
748 const char *Value = A->getValue(i);
749 int F = llvm::StringSwitch<int>(Value)
750 .Case("func", CoverageFunc)
751 .Case("bb", CoverageBB)
752 .Case("edge", CoverageEdge)
753 .Case("indirect-calls", CoverageIndirCall)
754 .Case("trace-bb", CoverageTraceBB)
755 .Case("trace-cmp", CoverageTraceCmp)
756 .Case("8bit-counters", Coverage8bitCounters)
Kostya Serebryanyd4590c72016-02-17 21:34:43 +0000757 .Case("trace-pc", CoverageTracePC)
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000758 .Default(0);
759 if (F == 0)
760 D.Diag(clang::diag::err_drv_unsupported_option_argument)
761 << A->getOption().getName() << Value;
762 Features |= F;
763 }
764 return Features;
765}
766
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000767std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000768 SanitizerMask Mask) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000769 for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
770 E = Args.rend();
771 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000772 const auto *Arg = *I;
773 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000774 SanitizerMask AddKinds =
775 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000776 if (AddKinds & Mask)
777 return describeSanitizeArg(Arg, Mask);
778 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000779 SanitizerMask RemoveKinds =
780 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000781 Mask &= ~RemoveKinds;
782 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000783 }
784 llvm_unreachable("arg list didn't provide expected value");
785}
786
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000787std::string describeSanitizeArg(const llvm::opt::Arg *A, SanitizerMask Mask) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000788 assert(A->getOption().matches(options::OPT_fsanitize_EQ)
789 && "Invalid argument in describeSanitizerArg!");
Alexey Samsonovcf055962013-08-08 10:11:02 +0000790
Peter Collingbourne32701642013-11-01 18:16:25 +0000791 std::string Sanitizers;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000792 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000793 if (expandSanitizerGroups(
794 parseSanitizerValue(A->getValue(i), /*AllowGroups=*/true)) &
795 Mask) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000796 if (!Sanitizers.empty())
797 Sanitizers += ",";
Alexey Samsonov83791e22015-03-03 22:15:32 +0000798 Sanitizers += A->getValue(i);
Peter Collingbourne32701642013-11-01 18:16:25 +0000799 }
800 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000801
Peter Collingbourne32701642013-11-01 18:16:25 +0000802 assert(!Sanitizers.empty() && "arg didn't provide expected value");
803 return "-fsanitize=" + Sanitizers;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000804}