blob: 1c31b532ff29dae7258ba5ca27afed1461417a63 [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 Collingbournebf59c342015-05-11 21:39:20 +000010#include "clang/Basic/Sanitizers.h"
Alexey Samsonovcf055962013-08-08 10:11:02 +000011#include "clang/Driver/Driver.h"
12#include "clang/Driver/DriverDiagnostic.h"
13#include "clang/Driver/Options.h"
14#include "clang/Driver/ToolChain.h"
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +000015#include "llvm/ADT/StringExtras.h"
Alexey Samsonovcf055962013-08-08 10:11:02 +000016#include "llvm/ADT/StringSwitch.h"
17#include "llvm/Support/FileSystem.h"
18#include "llvm/Support/Path.h"
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000019#include "llvm/Support/SpecialCaseList.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000020#include <memory>
Alexey Samsonovcf055962013-08-08 10:11:02 +000021
Peter Collingbourne3eea6772015-05-11 21:39:14 +000022using namespace clang;
23using namespace clang::SanitizerKind;
Alexey Samsonovcf055962013-08-08 10:11:02 +000024using namespace clang::driver;
25using namespace llvm::opt;
26
Peter Collingbourne3eea6772015-05-11 21:39:14 +000027enum : SanitizerMask {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000028 NeedsUbsanRt = Undefined | Integer,
29 NotAllowedWithTrap = Vptr,
Dmitry Vyukov43419a72014-11-21 12:19:01 +000030 RequiresPIE = Memory | DataFlow,
Kostya Serebryany2d88f3d2015-01-06 01:02:48 +000031 NeedsUnwindTables = Address | Thread | Memory | DataFlow,
Kostya Serebryany6fe5fcb2015-03-20 00:06:52 +000032 SupportsCoverage = Address | Memory | Leak | Undefined | Integer | DataFlow,
Alexey Samsonov88459522015-01-12 22:39:12 +000033 RecoverableByDefault = Undefined | Integer,
34 Unrecoverable = Address | Unreachable | Return,
Peter Collingbournea4ccff32015-02-20 20:30:56 +000035 LegacyFsanitizeRecoverMask = Undefined | Integer,
Peter Collingbourne1a7488a2015-04-02 00:23:30 +000036 NeedsLTO = CFI,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000037};
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000038
39enum CoverageFeature {
40 CoverageFunc = 1 << 0,
41 CoverageBB = 1 << 1,
42 CoverageEdge = 1 << 2,
43 CoverageIndirCall = 1 << 3,
44 CoverageTraceBB = 1 << 4,
45 CoverageTraceCmp = 1 << 5,
46 Coverage8bitCounters = 1 << 6,
47};
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000048
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000049/// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any
Peter Collingbourne3eea6772015-05-11 21:39:14 +000050/// invalid components. Returns a SanitizerMask.
51static SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
52 bool DiagnoseErrors);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000053
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000054/// Parse -f(no-)?sanitize-coverage= flag values, diagnosing any invalid
55/// components. Returns OR of members of \c CoverageFeature enumeration.
56static int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A);
57
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000058/// Produce an argument string from ArgList \p Args, which shows how it
59/// provides some sanitizer kind from \p Mask. For example, the argument list
60/// "-fsanitize=thread,vptr -fsanitize=address" with mask \c NeedsUbsanRt
61/// would produce "-fsanitize=vptr".
62static std::string lastArgumentForMask(const Driver &D,
63 const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +000064 SanitizerMask Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000065
66/// Produce an argument string from argument \p A, which shows how it provides
67/// a value in \p Mask. For instance, the argument
68/// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce
69/// "-fsanitize=alignment".
Peter Collingbourne3eea6772015-05-11 21:39:14 +000070static std::string describeSanitizeArg(const llvm::opt::Arg *A,
71 SanitizerMask Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000072
Alexey Samsonov1e715a62014-11-16 20:53:53 +000073/// Produce a string containing comma-separated names of sanitizers in \p
74/// Sanitizers set.
75static std::string toString(const clang::SanitizerSet &Sanitizers);
76
Peter Collingbourne3eea6772015-05-11 21:39:14 +000077static SanitizerMask getToolchainUnsupportedKinds(const ToolChain &TC) {
Alexey Samsonov1e715a62014-11-16 20:53:53 +000078 bool IsFreeBSD = TC.getTriple().getOS() == llvm::Triple::FreeBSD;
79 bool IsLinux = TC.getTriple().getOS() == llvm::Triple::Linux;
80 bool IsX86 = TC.getTriple().getArch() == llvm::Triple::x86;
81 bool IsX86_64 = TC.getTriple().getArch() == llvm::Triple::x86_64;
Mohit K. Bhakkadf4c47f62015-01-22 07:21:22 +000082 bool IsMIPS64 = TC.getTriple().getArch() == llvm::Triple::mips64 ||
83 TC.getTriple().getArch() == llvm::Triple::mips64el;
Alexey Samsonov1e715a62014-11-16 20:53:53 +000084
Peter Collingbourne3eea6772015-05-11 21:39:14 +000085 SanitizerMask Unsupported = 0;
Mohit K. Bhakkadf4c47f62015-01-22 07:21:22 +000086 if (!(IsLinux && (IsX86_64 || IsMIPS64))) {
Alexey Samsonov1e715a62014-11-16 20:53:53 +000087 Unsupported |= Memory | DataFlow;
88 }
Mohit K. Bhakkad69963e72015-02-23 09:32:35 +000089 if (!((IsLinux || IsFreeBSD) && (IsX86_64 || IsMIPS64))) {
Alexey Samsonov1e715a62014-11-16 20:53:53 +000090 Unsupported |= Thread;
91 }
92 if (!(IsLinux && (IsX86 || IsX86_64))) {
93 Unsupported |= Function;
94 }
95 return Unsupported;
96}
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000097
Peter Collingbourne3eea6772015-05-11 21:39:14 +000098static bool getDefaultBlacklist(const Driver &D, SanitizerMask Kinds,
Alexey Samsonovecf380e2015-03-20 18:45:06 +000099 std::string &BLPath) {
100 const char *BlacklistFile = nullptr;
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000101 if (Kinds & Address)
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000102 BlacklistFile = "asan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000103 else if (Kinds & Memory)
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000104 BlacklistFile = "msan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000105 else if (Kinds & Thread)
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000106 BlacklistFile = "tsan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000107 else if (Kinds & DataFlow)
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000108 BlacklistFile = "dfsan_abilist.txt";
109
110 if (BlacklistFile) {
111 clang::SmallString<64> Path(D.ResourceDir);
112 llvm::sys::path::append(Path, BlacklistFile);
113 BLPath = Path.str();
114 return true;
115 }
116 return false;
117}
118
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000119bool SanitizerArgs::needsUbsanRt() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000120 return !UbsanTrapOnError && (Sanitizers.Mask & NeedsUbsanRt) &&
121 !Sanitizers.has(Address) &&
122 !Sanitizers.has(Memory) &&
123 !Sanitizers.has(Thread);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000124}
125
Dmitry Vyukov43419a72014-11-21 12:19:01 +0000126bool SanitizerArgs::requiresPIE() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000127 return AsanZeroBaseShadow || (Sanitizers.Mask & RequiresPIE);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000128}
129
130bool SanitizerArgs::needsUnwindTables() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000131 return Sanitizers.Mask & NeedsUnwindTables;
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000132}
133
Peter Collingbournea4ccff32015-02-20 20:30:56 +0000134bool SanitizerArgs::needsLTO() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000135 return Sanitizers.Mask & NeedsLTO;
Peter Collingbournea4ccff32015-02-20 20:30:56 +0000136}
137
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000138void SanitizerArgs::clear() {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000139 Sanitizers.clear();
Alexey Samsonov88459522015-01-12 22:39:12 +0000140 RecoverableSanitizers.clear();
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000141 BlacklistFiles.clear();
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000142 CoverageFeatures = 0;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000143 MsanTrackOrigins = 0;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000144 AsanFieldPadding = 0;
Peter Collingbourne32701642013-11-01 18:16:25 +0000145 AsanZeroBaseShadow = false;
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000146 UbsanTrapOnError = false;
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000147 AsanSharedRuntime = false;
Alexey Samsonov90490af2014-08-08 22:47:17 +0000148 LinkCXXRuntimes = false;
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000149}
Alexey Samsonovcf055962013-08-08 10:11:02 +0000150
Peter Collingbourne32701642013-11-01 18:16:25 +0000151SanitizerArgs::SanitizerArgs(const ToolChain &TC,
152 const llvm::opt::ArgList &Args) {
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000153 clear();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000154 SanitizerMask AllRemove = 0; // During the loop below, the accumulated set of
155 // sanitizers disabled by the current sanitizer
156 // argument or any argument after it.
157 SanitizerMask DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
158 // Used to deduplicate diagnostics.
159 SanitizerMask Kinds = 0;
160 SanitizerMask NotSupported = getToolchainUnsupportedKinds(TC);
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000161 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
162
Peter Collingbourne32701642013-11-01 18:16:25 +0000163 const Driver &D = TC.getDriver();
164 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
165 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000166 const auto *Arg = *I;
167 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
168 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000169 SanitizerMask Add = parseArgValues(D, Arg, true);
Peter Collingbourne32701642013-11-01 18:16:25 +0000170
Alexey Samsonov799f7932014-12-19 02:35:16 +0000171 // Avoid diagnosing any sanitizer which is disabled later.
172 Add &= ~AllRemove;
173 // At this point we have not expanded groups, so any unsupported
174 // sanitizers in Add are those which have been explicitly enabled.
175 // Diagnose them.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000176 if (SanitizerMask KindsToDiagnose =
177 Add & NotSupported & ~DiagnosedKinds) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000178 // Only diagnose the new kinds.
179 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
180 D.Diag(diag::err_drv_unsupported_opt_for_target)
181 << Desc << TC.getTriple().str();
182 DiagnosedKinds |= KindsToDiagnose;
183 }
184 Add &= ~NotSupported;
Peter Collingbourne32701642013-11-01 18:16:25 +0000185
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000186 // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups
187 // so we don't error out if -fno-rtti and -fsanitize=undefined were
188 // passed.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000189 if (Add & Vptr &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000190 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
191 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
192 if (RTTIMode == ToolChain::RM_DisabledImplicitly)
193 // Warn about not having rtti enabled if the vptr sanitizer is
194 // explicitly enabled
195 D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default);
196 else {
197 const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg();
198 assert(NoRTTIArg &&
199 "RTTI disabled explicitly but we have no argument!");
200 D.Diag(diag::err_drv_argument_not_allowed_with)
201 << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args);
202 }
203
204 // Take out the Vptr sanitizer from the enabled sanitizers
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000205 AllRemove |= Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000206 }
207
Peter Collingbournebf59c342015-05-11 21:39:20 +0000208 Add = expandSanitizerGroups(Add);
Alexey Samsonov799f7932014-12-19 02:35:16 +0000209 // Group expansion may have enabled a sanitizer which is disabled later.
210 Add &= ~AllRemove;
211 // Silently discard any unsupported sanitizers implicitly enabled through
212 // group expansion.
213 Add &= ~NotSupported;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000214
Alexey Samsonov799f7932014-12-19 02:35:16 +0000215 Kinds |= Add;
216 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
217 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000218 SanitizerMask Remove = parseArgValues(D, Arg, true);
Peter Collingbournebf59c342015-05-11 21:39:20 +0000219 AllRemove |= expandSanitizerGroups(Remove);
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000220 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000221 }
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000222
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000223 // We disable the vptr sanitizer if it was enabled by group expansion but RTTI
224 // is disabled.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000225 if ((Kinds & Vptr) &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000226 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
227 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000228 Kinds &= ~Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000229 }
230
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000231 // Warn about undefined sanitizer options that require runtime support.
232 UbsanTrapOnError =
233 Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
234 options::OPT_fno_sanitize_undefined_trap_on_error, false);
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000235 if (UbsanTrapOnError && (Kinds & NotAllowedWithTrap)) {
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000236 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
237 << lastArgumentForMask(D, Args, NotAllowedWithTrap)
238 << "-fsanitize-undefined-trap-on-error";
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000239 Kinds &= ~NotAllowedWithTrap;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000240 }
241
242 // Warn about incompatible groups of sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000243 std::pair<SanitizerMask, SanitizerMask> IncompatibleGroups[] = {
244 std::make_pair(Address, Thread), std::make_pair(Address, Memory),
245 std::make_pair(Thread, Memory), std::make_pair(Leak, Thread),
246 std::make_pair(Leak, Memory)};
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000247 for (auto G : IncompatibleGroups) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000248 SanitizerMask Group = G.first;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000249 if (Kinds & Group) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000250 if (SanitizerMask Incompatible = Kinds & G.second) {
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000251 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
252 << lastArgumentForMask(D, Args, Group)
253 << lastArgumentForMask(D, Args, Incompatible);
254 Kinds &= ~Incompatible;
255 }
256 }
257 }
258 // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
259 // -fsanitize=address. Perhaps it should print an error, or perhaps
260 // -f(-no)sanitize=leak should change whether leak detection is enabled by
261 // default in ASan?
262
Alexey Samsonov88459522015-01-12 22:39:12 +0000263 // Parse -f(no-)?sanitize-recover flags.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000264 SanitizerMask RecoverableKinds = RecoverableByDefault;
265 SanitizerMask DiagnosedUnrecoverableKinds = 0;
Alexey Samsonov88459522015-01-12 22:39:12 +0000266 for (const auto *Arg : Args) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000267 const char *DeprecatedReplacement = nullptr;
Alexey Samsonov88459522015-01-12 22:39:12 +0000268 if (Arg->getOption().matches(options::OPT_fsanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000269 DeprecatedReplacement = "-fsanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000270 RecoverableKinds |= expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000271 Arg->claim();
272 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000273 DeprecatedReplacement = "-fno-sanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000274 RecoverableKinds &= ~expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000275 Arg->claim();
276 } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000277 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonov88459522015-01-12 22:39:12 +0000278 // Report error if user explicitly tries to recover from unrecoverable
279 // sanitizer.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000280 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov88459522015-01-12 22:39:12 +0000281 Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
282 SanitizerSet SetToDiagnose;
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000283 SetToDiagnose.Mask |= KindsToDiagnose;
Alexey Samsonov88459522015-01-12 22:39:12 +0000284 D.Diag(diag::err_drv_unsupported_option_argument)
285 << Arg->getOption().getName() << toString(SetToDiagnose);
286 DiagnosedUnrecoverableKinds |= KindsToDiagnose;
287 }
Peter Collingbournebf59c342015-05-11 21:39:20 +0000288 RecoverableKinds |= expandSanitizerGroups(Add);
Alexey Samsonov88459522015-01-12 22:39:12 +0000289 Arg->claim();
290 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000291 RecoverableKinds &= ~expandSanitizerGroups(parseArgValues(D, Arg, true));
Alexey Samsonov88459522015-01-12 22:39:12 +0000292 Arg->claim();
293 }
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000294 if (DeprecatedReplacement) {
295 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
296 << DeprecatedReplacement;
297 }
Alexey Samsonov88459522015-01-12 22:39:12 +0000298 }
299 RecoverableKinds &= Kinds;
300 RecoverableKinds &= ~Unrecoverable;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000301
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000302 // Setup blacklist files.
303 // Add default blacklist from resource directory.
304 {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000305 std::string BLPath;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000306 if (getDefaultBlacklist(D, Kinds, BLPath) && llvm::sys::fs::exists(BLPath))
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000307 BlacklistFiles.push_back(BLPath);
308 }
309 // Parse -f(no-)sanitize-blacklist options.
310 for (const auto *Arg : Args) {
311 if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) {
312 Arg->claim();
313 std::string BLPath = Arg->getValue();
314 if (llvm::sys::fs::exists(BLPath))
315 BlacklistFiles.push_back(BLPath);
316 else
317 D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
318 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) {
319 Arg->claim();
320 BlacklistFiles.clear();
321 }
322 }
323 // Validate blacklists format.
324 {
325 std::string BLError;
326 std::unique_ptr<llvm::SpecialCaseList> SCL(
327 llvm::SpecialCaseList::create(BlacklistFiles, BLError));
328 if (!SCL.get())
329 D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000330 }
331
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000332 // Parse -f[no-]sanitize-memory-track-origins[=level] options.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000333 if (Kinds & Memory) {
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000334 if (Arg *A =
335 Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
336 options::OPT_fsanitize_memory_track_origins,
337 options::OPT_fno_sanitize_memory_track_origins)) {
338 if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
Evgeniy Stepanov6e09bca2015-02-26 15:59:30 +0000339 MsanTrackOrigins = 2;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000340 } else if (A->getOption().matches(
341 options::OPT_fno_sanitize_memory_track_origins)) {
342 MsanTrackOrigins = 0;
343 } else {
344 StringRef S = A->getValue();
345 if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
346 MsanTrackOrigins > 2) {
Nico Weberf2a39a72015-04-13 20:03:03 +0000347 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000348 }
349 }
350 }
351 }
352
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000353 // Parse -f(no-)?sanitize-coverage flags if coverage is supported by the
354 // enabled sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000355 if (Kinds & SupportsCoverage) {
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000356 for (const auto *Arg : Args) {
357 if (Arg->getOption().matches(options::OPT_fsanitize_coverage)) {
358 Arg->claim();
359 int LegacySanitizeCoverage;
360 if (Arg->getNumValues() == 1 &&
361 !StringRef(Arg->getValue(0))
362 .getAsInteger(0, LegacySanitizeCoverage) &&
363 LegacySanitizeCoverage >= 0 && LegacySanitizeCoverage <= 4) {
364 // TODO: Add deprecation notice for this form.
365 switch (LegacySanitizeCoverage) {
366 case 0:
367 CoverageFeatures = 0;
368 break;
369 case 1:
370 CoverageFeatures = CoverageFunc;
371 break;
372 case 2:
373 CoverageFeatures = CoverageBB;
374 break;
375 case 3:
376 CoverageFeatures = CoverageEdge;
377 break;
378 case 4:
379 CoverageFeatures = CoverageEdge | CoverageIndirCall;
380 break;
381 }
382 continue;
383 }
384 CoverageFeatures |= parseCoverageFeatures(D, Arg);
385 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_coverage)) {
386 Arg->claim();
387 CoverageFeatures &= ~parseCoverageFeatures(D, Arg);
388 }
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000389 }
390 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000391 // Choose at most one coverage type: function, bb, or edge.
392 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageBB))
393 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
394 << "-fsanitize-coverage=func"
395 << "-fsanitize-coverage=bb";
396 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageEdge))
397 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
398 << "-fsanitize-coverage=func"
399 << "-fsanitize-coverage=edge";
400 if ((CoverageFeatures & CoverageBB) && (CoverageFeatures & CoverageEdge))
401 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
402 << "-fsanitize-coverage=bb"
403 << "-fsanitize-coverage=edge";
404 // Basic block tracing and 8-bit counters require some type of coverage
405 // enabled.
406 int CoverageTypes = CoverageFunc | CoverageBB | CoverageEdge;
407 if ((CoverageFeatures & CoverageTraceBB) &&
408 !(CoverageFeatures & CoverageTypes))
409 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
410 << "-fsanitize-coverage=trace-bb"
411 << "-fsanitize-coverage=(func|bb|edge)";
412 if ((CoverageFeatures & Coverage8bitCounters) &&
413 !(CoverageFeatures & CoverageTypes))
414 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
415 << "-fsanitize-coverage=8bit-counters"
416 << "-fsanitize-coverage=(func|bb|edge)";
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000417
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000418 if (Kinds & Address) {
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000419 AsanSharedRuntime =
Evgeniy Stepanov6f0ae182014-06-05 11:14:00 +0000420 Args.hasArg(options::OPT_shared_libasan) ||
421 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Peter Collingbourne32701642013-11-01 18:16:25 +0000422 AsanZeroBaseShadow =
Evgeniy Stepanovd04b8612014-01-16 10:19:31 +0000423 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000424 if (Arg *A =
425 Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
426 StringRef S = A->getValue();
427 // Legal values are 0 and 1, 2, but in future we may add more levels.
428 if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
429 AsanFieldPadding > 2) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000430 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000431 }
432 }
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000433
434 if (Arg *WindowsDebugRTArg =
435 Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
436 options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
437 options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
438 switch (WindowsDebugRTArg->getOption().getID()) {
439 case options::OPT__SLASH_MTd:
440 case options::OPT__SLASH_MDd:
441 case options::OPT__SLASH_LDd:
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000442 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000443 << WindowsDebugRTArg->getAsString(Args)
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000444 << lastArgumentForMask(D, Args, Address);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000445 D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000446 }
447 }
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000448 }
Alexey Samsonov90490af2014-08-08 22:47:17 +0000449
450 // Parse -link-cxx-sanitizer flag.
451 LinkCXXRuntimes =
452 Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX();
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000453
454 // Finally, initialize the set of available and recoverable sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000455 Sanitizers.Mask |= Kinds;
456 RecoverableSanitizers.Mask |= RecoverableKinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000457}
458
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000459static std::string toString(const clang::SanitizerSet &Sanitizers) {
460 std::string Res;
461#define SANITIZER(NAME, ID) \
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000462 if (Sanitizers.has(ID)) { \
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000463 if (!Res.empty()) \
464 Res += ","; \
465 Res += NAME; \
466 }
467#include "clang/Basic/Sanitizers.def"
468 return Res;
469}
470
Peter Collingbourne32701642013-11-01 18:16:25 +0000471void SanitizerArgs::addArgs(const llvm::opt::ArgList &Args,
Alexey Samsonovcf055962013-08-08 10:11:02 +0000472 llvm::opt::ArgStringList &CmdArgs) const {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000473 if (Sanitizers.empty())
Alexey Samsonovcf055962013-08-08 10:11:02 +0000474 return;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000475 CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
476
Alexey Samsonov88459522015-01-12 22:39:12 +0000477 if (!RecoverableSanitizers.empty())
478 CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
479 toString(RecoverableSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000480
481 if (UbsanTrapOnError)
482 CmdArgs.push_back("-fsanitize-undefined-trap-on-error");
483
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000484 for (const auto &BLPath : BlacklistFiles) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000485 SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000486 BlacklistOpt += BLPath;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000487 CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
488 }
489
490 if (MsanTrackOrigins)
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000491 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
492 llvm::utostr(MsanTrackOrigins)));
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000493 if (AsanFieldPadding)
494 CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
495 llvm::utostr(AsanFieldPadding)));
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000496 // Translate available CoverageFeatures to corresponding clang-cc1 flags.
497 std::pair<int, const char *> CoverageFlags[] = {
498 std::make_pair(CoverageFunc, "-fsanitize-coverage-type=1"),
499 std::make_pair(CoverageBB, "-fsanitize-coverage-type=2"),
500 std::make_pair(CoverageEdge, "-fsanitize-coverage-type=3"),
501 std::make_pair(CoverageIndirCall, "-fsanitize-coverage-indirect-calls"),
502 std::make_pair(CoverageTraceBB, "-fsanitize-coverage-trace-bb"),
503 std::make_pair(CoverageTraceCmp, "-fsanitize-coverage-trace-cmp"),
504 std::make_pair(Coverage8bitCounters, "-fsanitize-coverage-8bit-counters")};
505 for (auto F : CoverageFlags) {
506 if (CoverageFeatures & F.first)
507 CmdArgs.push_back(Args.MakeArgString(F.second));
Alexey Samsonov3f3b3ab2015-05-07 18:31:29 +0000508 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000509
510
Sergey Matveev2ba87782015-02-17 15:09:33 +0000511 // MSan: Workaround for PR16386.
512 // ASan: This is mainly to help LSan with cases such as
513 // https://code.google.com/p/address-sanitizer/issues/detail?id=373
514 // We can't make this conditional on -fsanitize=leak, as that flag shouldn't
515 // affect compilation.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000516 if (Sanitizers.has(Memory) || Sanitizers.has(Address))
Alexey Samsonovcf055962013-08-08 10:11:02 +0000517 CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
518}
519
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000520SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
521 bool DiagnoseErrors) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000522 assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
Alexey Samsonov88459522015-01-12 22:39:12 +0000523 A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
524 A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
525 A->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) &&
Alexey Samsonov799f7932014-12-19 02:35:16 +0000526 "Invalid argument in parseArgValues!");
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000527 SanitizerMask Kinds = 0;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000528 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
529 const char *Value = A->getValue(i);
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000530 SanitizerMask Kind;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000531 // Special case: don't accept -fsanitize=all.
532 if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
533 0 == strcmp("all", Value))
534 Kind = 0;
535 else
Peter Collingbournebf59c342015-05-11 21:39:20 +0000536 Kind = parseSanitizerValue(Value, /*AllowGroups=*/true);
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000537
538 if (Kind)
539 Kinds |= Kind;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000540 else if (DiagnoseErrors)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000541 D.Diag(clang::diag::err_drv_unsupported_option_argument)
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000542 << A->getOption().getName() << Value;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000543 }
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000544 return Kinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000545}
546
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000547int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A) {
548 assert(A->getOption().matches(options::OPT_fsanitize_coverage) ||
549 A->getOption().matches(options::OPT_fno_sanitize_coverage));
550 int Features = 0;
551 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
552 const char *Value = A->getValue(i);
553 int F = llvm::StringSwitch<int>(Value)
554 .Case("func", CoverageFunc)
555 .Case("bb", CoverageBB)
556 .Case("edge", CoverageEdge)
557 .Case("indirect-calls", CoverageIndirCall)
558 .Case("trace-bb", CoverageTraceBB)
559 .Case("trace-cmp", CoverageTraceCmp)
560 .Case("8bit-counters", Coverage8bitCounters)
561 .Default(0);
562 if (F == 0)
563 D.Diag(clang::diag::err_drv_unsupported_option_argument)
564 << A->getOption().getName() << Value;
565 Features |= F;
566 }
567 return Features;
568}
569
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000570std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000571 SanitizerMask Mask) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000572 for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
573 E = Args.rend();
574 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000575 const auto *Arg = *I;
576 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000577 SanitizerMask AddKinds =
578 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000579 if (AddKinds & Mask)
580 return describeSanitizeArg(Arg, Mask);
581 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000582 SanitizerMask RemoveKinds =
583 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000584 Mask &= ~RemoveKinds;
585 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000586 }
587 llvm_unreachable("arg list didn't provide expected value");
588}
589
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000590std::string describeSanitizeArg(const llvm::opt::Arg *A, SanitizerMask Mask) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000591 assert(A->getOption().matches(options::OPT_fsanitize_EQ)
592 && "Invalid argument in describeSanitizerArg!");
Alexey Samsonovcf055962013-08-08 10:11:02 +0000593
Peter Collingbourne32701642013-11-01 18:16:25 +0000594 std::string Sanitizers;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000595 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000596 if (expandSanitizerGroups(
597 parseSanitizerValue(A->getValue(i), /*AllowGroups=*/true)) &
598 Mask) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000599 if (!Sanitizers.empty())
600 Sanitizers += ",";
Alexey Samsonov83791e22015-03-03 22:15:32 +0000601 Sanitizers += A->getValue(i);
Peter Collingbourne32701642013-11-01 18:16:25 +0000602 }
603 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000604
Peter Collingbourne32701642013-11-01 18:16:25 +0000605 assert(!Sanitizers.empty() && "arg didn't provide expected value");
606 return "-fsanitize=" + Sanitizers;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000607}