blob: 72530b442f52f2ca43314d523f9c1a3130532230 [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
Alexey Samsonov9bc2ad52015-06-17 22:27:32 +000077static SanitizerMask getToolchainUnsupportedKinds(const ToolChain &TC) {
78 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;
82 bool IsMIPS64 = TC.getTriple().getArch() == llvm::Triple::mips64 ||
83 TC.getTriple().getArch() == llvm::Triple::mips64el;
84
85 SanitizerMask Unsupported = 0;
86 if (!(IsLinux && (IsX86_64 || IsMIPS64))) {
87 Unsupported |= Memory | DataFlow;
88 }
89 if (!((IsLinux || IsFreeBSD) && (IsX86_64 || IsMIPS64))) {
90 Unsupported |= Thread;
91 }
92 if (!(IsLinux && (IsX86 || IsX86_64))) {
93 Unsupported |= Function;
94 }
95 return Unsupported;
96}
97
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.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000157 SanitizerMask AllAddedKinds = 0; // Mask of all sanitizers ever enabled by
158 // -fsanitize= flags (directly or via group
159 // expansion), some of which may be disabled
160 // later. Used to carefully prune
161 // unused-argument diagnostics.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000162 SanitizerMask DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
163 // Used to deduplicate diagnostics.
164 SanitizerMask Kinds = 0;
Alexey Samsonov9bc2ad52015-06-17 22:27:32 +0000165 SanitizerMask NotSupported = getToolchainUnsupportedKinds(TC);
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000166 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
167
Peter Collingbourne32701642013-11-01 18:16:25 +0000168 const Driver &D = TC.getDriver();
169 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
170 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000171 const auto *Arg = *I;
172 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
173 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000174 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000175 AllAddedKinds |= expandSanitizerGroups(Add);
Peter Collingbourne32701642013-11-01 18:16:25 +0000176
Alexey Samsonov799f7932014-12-19 02:35:16 +0000177 // Avoid diagnosing any sanitizer which is disabled later.
178 Add &= ~AllRemove;
179 // At this point we have not expanded groups, so any unsupported
180 // sanitizers in Add are those which have been explicitly enabled.
181 // Diagnose them.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000182 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov9bc2ad52015-06-17 22:27:32 +0000183 Add & NotSupported & ~DiagnosedKinds) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000184 // Only diagnose the new kinds.
185 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
186 D.Diag(diag::err_drv_unsupported_opt_for_target)
187 << Desc << TC.getTriple().str();
188 DiagnosedKinds |= KindsToDiagnose;
189 }
Alexey Samsonov9bc2ad52015-06-17 22:27:32 +0000190 Add &= ~NotSupported;
Peter Collingbourne32701642013-11-01 18:16:25 +0000191
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000192 // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups
193 // so we don't error out if -fno-rtti and -fsanitize=undefined were
194 // passed.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000195 if (Add & Vptr &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000196 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
197 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
198 if (RTTIMode == ToolChain::RM_DisabledImplicitly)
199 // Warn about not having rtti enabled if the vptr sanitizer is
200 // explicitly enabled
201 D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default);
202 else {
203 const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg();
204 assert(NoRTTIArg &&
205 "RTTI disabled explicitly but we have no argument!");
206 D.Diag(diag::err_drv_argument_not_allowed_with)
207 << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args);
208 }
209
210 // Take out the Vptr sanitizer from the enabled sanitizers
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000211 AllRemove |= Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000212 }
213
Peter Collingbournebf59c342015-05-11 21:39:20 +0000214 Add = expandSanitizerGroups(Add);
Alexey Samsonov799f7932014-12-19 02:35:16 +0000215 // Group expansion may have enabled a sanitizer which is disabled later.
216 Add &= ~AllRemove;
217 // Silently discard any unsupported sanitizers implicitly enabled through
218 // group expansion.
Alexey Samsonov9bc2ad52015-06-17 22:27:32 +0000219 Add &= ~NotSupported;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000220
Alexey Samsonov799f7932014-12-19 02:35:16 +0000221 Kinds |= Add;
222 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
223 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000224 SanitizerMask Remove = parseArgValues(D, Arg, true);
Peter Collingbournebf59c342015-05-11 21:39:20 +0000225 AllRemove |= expandSanitizerGroups(Remove);
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000226 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000227 }
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000228
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000229 // We disable the vptr sanitizer if it was enabled by group expansion but RTTI
230 // is disabled.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000231 if ((Kinds & Vptr) &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000232 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
233 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000234 Kinds &= ~Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000235 }
236
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000237 // Warn about undefined sanitizer options that require runtime support.
238 UbsanTrapOnError =
239 Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
240 options::OPT_fno_sanitize_undefined_trap_on_error, false);
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000241 if (UbsanTrapOnError && (Kinds & NotAllowedWithTrap)) {
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000242 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
243 << lastArgumentForMask(D, Args, NotAllowedWithTrap)
244 << "-fsanitize-undefined-trap-on-error";
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000245 Kinds &= ~NotAllowedWithTrap;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000246 }
247
248 // Warn about incompatible groups of sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000249 std::pair<SanitizerMask, SanitizerMask> IncompatibleGroups[] = {
250 std::make_pair(Address, Thread), std::make_pair(Address, Memory),
251 std::make_pair(Thread, Memory), std::make_pair(Leak, Thread),
252 std::make_pair(Leak, Memory)};
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000253 for (auto G : IncompatibleGroups) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000254 SanitizerMask Group = G.first;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000255 if (Kinds & Group) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000256 if (SanitizerMask Incompatible = Kinds & G.second) {
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000257 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
258 << lastArgumentForMask(D, Args, Group)
259 << lastArgumentForMask(D, Args, Incompatible);
260 Kinds &= ~Incompatible;
261 }
262 }
263 }
264 // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
265 // -fsanitize=address. Perhaps it should print an error, or perhaps
266 // -f(-no)sanitize=leak should change whether leak detection is enabled by
267 // default in ASan?
268
Alexey Samsonov88459522015-01-12 22:39:12 +0000269 // Parse -f(no-)?sanitize-recover flags.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000270 SanitizerMask RecoverableKinds = RecoverableByDefault;
271 SanitizerMask DiagnosedUnrecoverableKinds = 0;
Alexey Samsonov88459522015-01-12 22:39:12 +0000272 for (const auto *Arg : Args) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000273 const char *DeprecatedReplacement = nullptr;
Alexey Samsonov88459522015-01-12 22:39:12 +0000274 if (Arg->getOption().matches(options::OPT_fsanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000275 DeprecatedReplacement = "-fsanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000276 RecoverableKinds |= expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000277 Arg->claim();
278 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000279 DeprecatedReplacement = "-fno-sanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000280 RecoverableKinds &= ~expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000281 Arg->claim();
282 } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000283 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonov88459522015-01-12 22:39:12 +0000284 // Report error if user explicitly tries to recover from unrecoverable
285 // sanitizer.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000286 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov88459522015-01-12 22:39:12 +0000287 Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
288 SanitizerSet SetToDiagnose;
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000289 SetToDiagnose.Mask |= KindsToDiagnose;
Alexey Samsonov88459522015-01-12 22:39:12 +0000290 D.Diag(diag::err_drv_unsupported_option_argument)
291 << Arg->getOption().getName() << toString(SetToDiagnose);
292 DiagnosedUnrecoverableKinds |= KindsToDiagnose;
293 }
Peter Collingbournebf59c342015-05-11 21:39:20 +0000294 RecoverableKinds |= expandSanitizerGroups(Add);
Alexey Samsonov88459522015-01-12 22:39:12 +0000295 Arg->claim();
296 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000297 RecoverableKinds &= ~expandSanitizerGroups(parseArgValues(D, Arg, true));
Alexey Samsonov88459522015-01-12 22:39:12 +0000298 Arg->claim();
299 }
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000300 if (DeprecatedReplacement) {
301 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
302 << DeprecatedReplacement;
303 }
Alexey Samsonov88459522015-01-12 22:39:12 +0000304 }
305 RecoverableKinds &= Kinds;
306 RecoverableKinds &= ~Unrecoverable;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000307
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000308 // Setup blacklist files.
309 // Add default blacklist from resource directory.
310 {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000311 std::string BLPath;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000312 if (getDefaultBlacklist(D, Kinds, BLPath) && llvm::sys::fs::exists(BLPath))
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000313 BlacklistFiles.push_back(BLPath);
314 }
315 // Parse -f(no-)sanitize-blacklist options.
316 for (const auto *Arg : Args) {
317 if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) {
318 Arg->claim();
319 std::string BLPath = Arg->getValue();
320 if (llvm::sys::fs::exists(BLPath))
321 BlacklistFiles.push_back(BLPath);
322 else
323 D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
324 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) {
325 Arg->claim();
326 BlacklistFiles.clear();
327 }
328 }
329 // Validate blacklists format.
330 {
331 std::string BLError;
332 std::unique_ptr<llvm::SpecialCaseList> SCL(
333 llvm::SpecialCaseList::create(BlacklistFiles, BLError));
334 if (!SCL.get())
335 D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000336 }
337
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000338 // Parse -f[no-]sanitize-memory-track-origins[=level] options.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000339 if (AllAddedKinds & Memory) {
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000340 if (Arg *A =
341 Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
342 options::OPT_fsanitize_memory_track_origins,
343 options::OPT_fno_sanitize_memory_track_origins)) {
344 if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
Evgeniy Stepanov6e09bca2015-02-26 15:59:30 +0000345 MsanTrackOrigins = 2;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000346 } else if (A->getOption().matches(
347 options::OPT_fno_sanitize_memory_track_origins)) {
348 MsanTrackOrigins = 0;
349 } else {
350 StringRef S = A->getValue();
351 if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
352 MsanTrackOrigins > 2) {
Nico Weberf2a39a72015-04-13 20:03:03 +0000353 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000354 }
355 }
356 }
357 }
358
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000359 // Parse -f(no-)?sanitize-coverage flags if coverage is supported by the
360 // enabled sanitizers.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000361 if (AllAddedKinds & SupportsCoverage) {
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000362 for (const auto *Arg : Args) {
363 if (Arg->getOption().matches(options::OPT_fsanitize_coverage)) {
364 Arg->claim();
365 int LegacySanitizeCoverage;
366 if (Arg->getNumValues() == 1 &&
367 !StringRef(Arg->getValue(0))
368 .getAsInteger(0, LegacySanitizeCoverage) &&
369 LegacySanitizeCoverage >= 0 && LegacySanitizeCoverage <= 4) {
370 // TODO: Add deprecation notice for this form.
371 switch (LegacySanitizeCoverage) {
372 case 0:
373 CoverageFeatures = 0;
374 break;
375 case 1:
376 CoverageFeatures = CoverageFunc;
377 break;
378 case 2:
379 CoverageFeatures = CoverageBB;
380 break;
381 case 3:
382 CoverageFeatures = CoverageEdge;
383 break;
384 case 4:
385 CoverageFeatures = CoverageEdge | CoverageIndirCall;
386 break;
387 }
388 continue;
389 }
390 CoverageFeatures |= parseCoverageFeatures(D, Arg);
391 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_coverage)) {
392 Arg->claim();
393 CoverageFeatures &= ~parseCoverageFeatures(D, Arg);
394 }
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000395 }
396 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000397 // Choose at most one coverage type: function, bb, or edge.
398 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageBB))
399 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
400 << "-fsanitize-coverage=func"
401 << "-fsanitize-coverage=bb";
402 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageEdge))
403 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
404 << "-fsanitize-coverage=func"
405 << "-fsanitize-coverage=edge";
406 if ((CoverageFeatures & CoverageBB) && (CoverageFeatures & CoverageEdge))
407 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
408 << "-fsanitize-coverage=bb"
409 << "-fsanitize-coverage=edge";
410 // Basic block tracing and 8-bit counters require some type of coverage
411 // enabled.
412 int CoverageTypes = CoverageFunc | CoverageBB | CoverageEdge;
413 if ((CoverageFeatures & CoverageTraceBB) &&
414 !(CoverageFeatures & CoverageTypes))
415 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
416 << "-fsanitize-coverage=trace-bb"
417 << "-fsanitize-coverage=(func|bb|edge)";
418 if ((CoverageFeatures & Coverage8bitCounters) &&
419 !(CoverageFeatures & CoverageTypes))
420 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
421 << "-fsanitize-coverage=8bit-counters"
422 << "-fsanitize-coverage=(func|bb|edge)";
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000423
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000424 if (AllAddedKinds & Address) {
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000425 AsanSharedRuntime =
Evgeniy Stepanov6f0ae182014-06-05 11:14:00 +0000426 Args.hasArg(options::OPT_shared_libasan) ||
427 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Peter Collingbourne32701642013-11-01 18:16:25 +0000428 AsanZeroBaseShadow =
Evgeniy Stepanovd04b8612014-01-16 10:19:31 +0000429 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000430 if (Arg *A =
431 Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
432 StringRef S = A->getValue();
433 // Legal values are 0 and 1, 2, but in future we may add more levels.
434 if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
435 AsanFieldPadding > 2) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000436 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000437 }
438 }
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000439
440 if (Arg *WindowsDebugRTArg =
441 Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
442 options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
443 options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
444 switch (WindowsDebugRTArg->getOption().getID()) {
445 case options::OPT__SLASH_MTd:
446 case options::OPT__SLASH_MDd:
447 case options::OPT__SLASH_LDd:
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000448 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000449 << WindowsDebugRTArg->getAsString(Args)
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000450 << lastArgumentForMask(D, Args, Address);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000451 D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000452 }
453 }
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000454 }
Alexey Samsonov90490af2014-08-08 22:47:17 +0000455
456 // Parse -link-cxx-sanitizer flag.
457 LinkCXXRuntimes =
458 Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX();
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000459
460 // Finally, initialize the set of available and recoverable sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000461 Sanitizers.Mask |= Kinds;
462 RecoverableSanitizers.Mask |= RecoverableKinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000463}
464
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000465static std::string toString(const clang::SanitizerSet &Sanitizers) {
466 std::string Res;
467#define SANITIZER(NAME, ID) \
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000468 if (Sanitizers.has(ID)) { \
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000469 if (!Res.empty()) \
470 Res += ","; \
471 Res += NAME; \
472 }
473#include "clang/Basic/Sanitizers.def"
474 return Res;
475}
476
Peter Collingbourne32701642013-11-01 18:16:25 +0000477void SanitizerArgs::addArgs(const llvm::opt::ArgList &Args,
Alexey Samsonovcf055962013-08-08 10:11:02 +0000478 llvm::opt::ArgStringList &CmdArgs) const {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000479 if (Sanitizers.empty())
Alexey Samsonovcf055962013-08-08 10:11:02 +0000480 return;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000481 CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
482
Alexey Samsonov88459522015-01-12 22:39:12 +0000483 if (!RecoverableSanitizers.empty())
484 CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
485 toString(RecoverableSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000486
487 if (UbsanTrapOnError)
488 CmdArgs.push_back("-fsanitize-undefined-trap-on-error");
489
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000490 for (const auto &BLPath : BlacklistFiles) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000491 SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000492 BlacklistOpt += BLPath;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000493 CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
494 }
495
496 if (MsanTrackOrigins)
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000497 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
498 llvm::utostr(MsanTrackOrigins)));
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000499 if (AsanFieldPadding)
500 CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
501 llvm::utostr(AsanFieldPadding)));
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000502 // Translate available CoverageFeatures to corresponding clang-cc1 flags.
503 std::pair<int, const char *> CoverageFlags[] = {
504 std::make_pair(CoverageFunc, "-fsanitize-coverage-type=1"),
505 std::make_pair(CoverageBB, "-fsanitize-coverage-type=2"),
506 std::make_pair(CoverageEdge, "-fsanitize-coverage-type=3"),
507 std::make_pair(CoverageIndirCall, "-fsanitize-coverage-indirect-calls"),
508 std::make_pair(CoverageTraceBB, "-fsanitize-coverage-trace-bb"),
509 std::make_pair(CoverageTraceCmp, "-fsanitize-coverage-trace-cmp"),
510 std::make_pair(Coverage8bitCounters, "-fsanitize-coverage-8bit-counters")};
511 for (auto F : CoverageFlags) {
512 if (CoverageFeatures & F.first)
513 CmdArgs.push_back(Args.MakeArgString(F.second));
Alexey Samsonov3f3b3ab2015-05-07 18:31:29 +0000514 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000515
516
Sergey Matveev2ba87782015-02-17 15:09:33 +0000517 // MSan: Workaround for PR16386.
518 // ASan: This is mainly to help LSan with cases such as
519 // https://code.google.com/p/address-sanitizer/issues/detail?id=373
520 // We can't make this conditional on -fsanitize=leak, as that flag shouldn't
521 // affect compilation.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000522 if (Sanitizers.has(Memory) || Sanitizers.has(Address))
Alexey Samsonovcf055962013-08-08 10:11:02 +0000523 CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
524}
525
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000526SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
527 bool DiagnoseErrors) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000528 assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
Alexey Samsonov88459522015-01-12 22:39:12 +0000529 A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
530 A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
531 A->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) &&
Alexey Samsonov799f7932014-12-19 02:35:16 +0000532 "Invalid argument in parseArgValues!");
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000533 SanitizerMask Kinds = 0;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000534 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
535 const char *Value = A->getValue(i);
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000536 SanitizerMask Kind;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000537 // Special case: don't accept -fsanitize=all.
538 if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
539 0 == strcmp("all", Value))
540 Kind = 0;
541 else
Peter Collingbournebf59c342015-05-11 21:39:20 +0000542 Kind = parseSanitizerValue(Value, /*AllowGroups=*/true);
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000543
544 if (Kind)
545 Kinds |= Kind;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000546 else if (DiagnoseErrors)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000547 D.Diag(clang::diag::err_drv_unsupported_option_argument)
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000548 << A->getOption().getName() << Value;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000549 }
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000550 return Kinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000551}
552
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000553int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A) {
554 assert(A->getOption().matches(options::OPT_fsanitize_coverage) ||
555 A->getOption().matches(options::OPT_fno_sanitize_coverage));
556 int Features = 0;
557 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
558 const char *Value = A->getValue(i);
559 int F = llvm::StringSwitch<int>(Value)
560 .Case("func", CoverageFunc)
561 .Case("bb", CoverageBB)
562 .Case("edge", CoverageEdge)
563 .Case("indirect-calls", CoverageIndirCall)
564 .Case("trace-bb", CoverageTraceBB)
565 .Case("trace-cmp", CoverageTraceCmp)
566 .Case("8bit-counters", Coverage8bitCounters)
567 .Default(0);
568 if (F == 0)
569 D.Diag(clang::diag::err_drv_unsupported_option_argument)
570 << A->getOption().getName() << Value;
571 Features |= F;
572 }
573 return Features;
574}
575
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000576std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000577 SanitizerMask Mask) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000578 for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
579 E = Args.rend();
580 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000581 const auto *Arg = *I;
582 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000583 SanitizerMask AddKinds =
584 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000585 if (AddKinds & Mask)
586 return describeSanitizeArg(Arg, Mask);
587 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000588 SanitizerMask RemoveKinds =
589 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000590 Mask &= ~RemoveKinds;
591 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000592 }
593 llvm_unreachable("arg list didn't provide expected value");
594}
595
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000596std::string describeSanitizeArg(const llvm::opt::Arg *A, SanitizerMask Mask) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000597 assert(A->getOption().matches(options::OPT_fsanitize_EQ)
598 && "Invalid argument in describeSanitizerArg!");
Alexey Samsonovcf055962013-08-08 10:11:02 +0000599
Peter Collingbourne32701642013-11-01 18:16:25 +0000600 std::string Sanitizers;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000601 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000602 if (expandSanitizerGroups(
603 parseSanitizerValue(A->getValue(i), /*AllowGroups=*/true)) &
604 Mask) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000605 if (!Sanitizers.empty())
606 Sanitizers += ",";
Alexey Samsonov83791e22015-03-03 22:15:32 +0000607 Sanitizers += A->getValue(i);
Peter Collingbourne32701642013-11-01 18:16:25 +0000608 }
609 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000610
Peter Collingbourne32701642013-11-01 18:16:25 +0000611 assert(!Sanitizers.empty() && "arg didn't provide expected value");
612 return "-fsanitize=" + Sanitizers;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000613}