blob: 060530d70fb763e987d1926895e5b363d368e6b5 [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"
Alexey Samsonovcf055962013-08-08 10:11:02 +000010#include "clang/Driver/Driver.h"
11#include "clang/Driver/DriverDiagnostic.h"
12#include "clang/Driver/Options.h"
13#include "clang/Driver/ToolChain.h"
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +000014#include "llvm/ADT/StringExtras.h"
Alexey Samsonovcf055962013-08-08 10:11:02 +000015#include "llvm/ADT/StringSwitch.h"
16#include "llvm/Support/FileSystem.h"
17#include "llvm/Support/Path.h"
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000018#include "llvm/Support/SpecialCaseList.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000019#include <memory>
Alexey Samsonovcf055962013-08-08 10:11:02 +000020
Peter Collingbourne3eea6772015-05-11 21:39:14 +000021using namespace clang;
22using namespace clang::SanitizerKind;
Alexey Samsonovcf055962013-08-08 10:11:02 +000023using namespace clang::driver;
24using namespace llvm::opt;
25
Peter Collingbourne3eea6772015-05-11 21:39:14 +000026enum : SanitizerMask {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000027 NeedsUbsanRt = Undefined | Integer,
28 NotAllowedWithTrap = Vptr,
Dmitry Vyukov43419a72014-11-21 12:19:01 +000029 RequiresPIE = Memory | DataFlow,
Kostya Serebryany2d88f3d2015-01-06 01:02:48 +000030 NeedsUnwindTables = Address | Thread | Memory | DataFlow,
Kostya Serebryany6fe5fcb2015-03-20 00:06:52 +000031 SupportsCoverage = Address | Memory | Leak | Undefined | Integer | DataFlow,
Alexey Samsonov88459522015-01-12 22:39:12 +000032 RecoverableByDefault = Undefined | Integer,
33 Unrecoverable = Address | Unreachable | Return,
Peter Collingbournea4ccff32015-02-20 20:30:56 +000034 LegacyFsanitizeRecoverMask = Undefined | Integer,
Peter Collingbourne1a7488a2015-04-02 00:23:30 +000035 NeedsLTO = CFI,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000036};
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000037
38enum CoverageFeature {
39 CoverageFunc = 1 << 0,
40 CoverageBB = 1 << 1,
41 CoverageEdge = 1 << 2,
42 CoverageIndirCall = 1 << 3,
43 CoverageTraceBB = 1 << 4,
44 CoverageTraceCmp = 1 << 5,
45 Coverage8bitCounters = 1 << 6,
46};
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000047
48/// Parse a single value from a -fsanitize= or -fno-sanitize= value list.
Peter Collingbourne3eea6772015-05-11 21:39:14 +000049/// Returns a non-zero SanitizerMask, or \c 0 if \p Value is not known.
50static SanitizerMask parseValue(const char *Value);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000051
52/// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any
Peter Collingbourne3eea6772015-05-11 21:39:14 +000053/// invalid components. Returns a SanitizerMask.
54static SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
55 bool DiagnoseErrors);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000056
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000057/// Parse -f(no-)?sanitize-coverage= flag values, diagnosing any invalid
58/// components. Returns OR of members of \c CoverageFeature enumeration.
59static int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A);
60
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000061/// Produce an argument string from ArgList \p Args, which shows how it
62/// provides some sanitizer kind from \p Mask. For example, the argument list
63/// "-fsanitize=thread,vptr -fsanitize=address" with mask \c NeedsUbsanRt
64/// would produce "-fsanitize=vptr".
65static std::string lastArgumentForMask(const Driver &D,
66 const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +000067 SanitizerMask Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000068
69/// Produce an argument string from argument \p A, which shows how it provides
70/// a value in \p Mask. For instance, the argument
71/// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce
72/// "-fsanitize=alignment".
Peter Collingbourne3eea6772015-05-11 21:39:14 +000073static std::string describeSanitizeArg(const llvm::opt::Arg *A,
74 SanitizerMask Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000075
Alexey Samsonov1e715a62014-11-16 20:53:53 +000076/// Produce a string containing comma-separated names of sanitizers in \p
77/// Sanitizers set.
78static std::string toString(const clang::SanitizerSet &Sanitizers);
79
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000080/// For each sanitizer group bit set in \p Kinds, set the bits for sanitizers
81/// this group enables.
Peter Collingbourne3eea6772015-05-11 21:39:14 +000082static SanitizerMask expandGroups(SanitizerMask Kinds);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000083
Peter Collingbourne3eea6772015-05-11 21:39:14 +000084static SanitizerMask getToolchainUnsupportedKinds(const ToolChain &TC) {
Alexey Samsonov1e715a62014-11-16 20:53:53 +000085 bool IsFreeBSD = TC.getTriple().getOS() == llvm::Triple::FreeBSD;
86 bool IsLinux = TC.getTriple().getOS() == llvm::Triple::Linux;
87 bool IsX86 = TC.getTriple().getArch() == llvm::Triple::x86;
88 bool IsX86_64 = TC.getTriple().getArch() == llvm::Triple::x86_64;
Mohit K. Bhakkadf4c47f62015-01-22 07:21:22 +000089 bool IsMIPS64 = TC.getTriple().getArch() == llvm::Triple::mips64 ||
90 TC.getTriple().getArch() == llvm::Triple::mips64el;
Alexey Samsonov1e715a62014-11-16 20:53:53 +000091
Peter Collingbourne3eea6772015-05-11 21:39:14 +000092 SanitizerMask Unsupported = 0;
Mohit K. Bhakkadf4c47f62015-01-22 07:21:22 +000093 if (!(IsLinux && (IsX86_64 || IsMIPS64))) {
Alexey Samsonov1e715a62014-11-16 20:53:53 +000094 Unsupported |= Memory | DataFlow;
95 }
Mohit K. Bhakkad69963e72015-02-23 09:32:35 +000096 if (!((IsLinux || IsFreeBSD) && (IsX86_64 || IsMIPS64))) {
Alexey Samsonov1e715a62014-11-16 20:53:53 +000097 Unsupported |= Thread;
98 }
99 if (!(IsLinux && (IsX86 || IsX86_64))) {
100 Unsupported |= Function;
101 }
102 return Unsupported;
103}
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000104
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000105static bool getDefaultBlacklist(const Driver &D, SanitizerMask Kinds,
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000106 std::string &BLPath) {
107 const char *BlacklistFile = nullptr;
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000108 if (Kinds & Address)
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000109 BlacklistFile = "asan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000110 else if (Kinds & Memory)
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000111 BlacklistFile = "msan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000112 else if (Kinds & Thread)
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000113 BlacklistFile = "tsan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000114 else if (Kinds & DataFlow)
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000115 BlacklistFile = "dfsan_abilist.txt";
116
117 if (BlacklistFile) {
118 clang::SmallString<64> Path(D.ResourceDir);
119 llvm::sys::path::append(Path, BlacklistFile);
120 BLPath = Path.str();
121 return true;
122 }
123 return false;
124}
125
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000126bool SanitizerArgs::needsUbsanRt() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000127 return !UbsanTrapOnError && (Sanitizers.Mask & NeedsUbsanRt) &&
128 !Sanitizers.has(Address) &&
129 !Sanitizers.has(Memory) &&
130 !Sanitizers.has(Thread);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000131}
132
Dmitry Vyukov43419a72014-11-21 12:19:01 +0000133bool SanitizerArgs::requiresPIE() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000134 return AsanZeroBaseShadow || (Sanitizers.Mask & RequiresPIE);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000135}
136
137bool SanitizerArgs::needsUnwindTables() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000138 return Sanitizers.Mask & NeedsUnwindTables;
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000139}
140
Peter Collingbournea4ccff32015-02-20 20:30:56 +0000141bool SanitizerArgs::needsLTO() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000142 return Sanitizers.Mask & NeedsLTO;
Peter Collingbournea4ccff32015-02-20 20:30:56 +0000143}
144
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000145void SanitizerArgs::clear() {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000146 Sanitizers.clear();
Alexey Samsonov88459522015-01-12 22:39:12 +0000147 RecoverableSanitizers.clear();
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000148 BlacklistFiles.clear();
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000149 CoverageFeatures = 0;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000150 MsanTrackOrigins = 0;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000151 AsanFieldPadding = 0;
Peter Collingbourne32701642013-11-01 18:16:25 +0000152 AsanZeroBaseShadow = false;
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000153 UbsanTrapOnError = false;
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000154 AsanSharedRuntime = false;
Alexey Samsonov90490af2014-08-08 22:47:17 +0000155 LinkCXXRuntimes = false;
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000156}
Alexey Samsonovcf055962013-08-08 10:11:02 +0000157
Peter Collingbourne32701642013-11-01 18:16:25 +0000158SanitizerArgs::SanitizerArgs(const ToolChain &TC,
159 const llvm::opt::ArgList &Args) {
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000160 clear();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000161 SanitizerMask AllRemove = 0; // During the loop below, the accumulated set of
162 // sanitizers disabled by the current sanitizer
163 // argument or any argument after it.
164 SanitizerMask DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
165 // Used to deduplicate diagnostics.
166 SanitizerMask Kinds = 0;
167 SanitizerMask NotSupported = getToolchainUnsupportedKinds(TC);
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000168 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
169
Peter Collingbourne32701642013-11-01 18:16:25 +0000170 const Driver &D = TC.getDriver();
171 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
172 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000173 const auto *Arg = *I;
174 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
175 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000176 SanitizerMask Add = parseArgValues(D, Arg, true);
Peter Collingbourne32701642013-11-01 18:16:25 +0000177
Alexey Samsonov799f7932014-12-19 02:35:16 +0000178 // Avoid diagnosing any sanitizer which is disabled later.
179 Add &= ~AllRemove;
180 // At this point we have not expanded groups, so any unsupported
181 // sanitizers in Add are those which have been explicitly enabled.
182 // Diagnose them.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000183 if (SanitizerMask KindsToDiagnose =
184 Add & NotSupported & ~DiagnosedKinds) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000185 // Only diagnose the new kinds.
186 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
187 D.Diag(diag::err_drv_unsupported_opt_for_target)
188 << Desc << TC.getTriple().str();
189 DiagnosedKinds |= KindsToDiagnose;
190 }
191 Add &= ~NotSupported;
Peter Collingbourne32701642013-11-01 18:16:25 +0000192
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000193 // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups
194 // so we don't error out if -fno-rtti and -fsanitize=undefined were
195 // passed.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000196 if (Add & Vptr &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000197 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
198 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
199 if (RTTIMode == ToolChain::RM_DisabledImplicitly)
200 // Warn about not having rtti enabled if the vptr sanitizer is
201 // explicitly enabled
202 D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default);
203 else {
204 const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg();
205 assert(NoRTTIArg &&
206 "RTTI disabled explicitly but we have no argument!");
207 D.Diag(diag::err_drv_argument_not_allowed_with)
208 << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args);
209 }
210
211 // Take out the Vptr sanitizer from the enabled sanitizers
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000212 AllRemove |= Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000213 }
214
Alexey Samsonov799f7932014-12-19 02:35:16 +0000215 Add = expandGroups(Add);
216 // Group expansion may have enabled a sanitizer which is disabled later.
217 Add &= ~AllRemove;
218 // Silently discard any unsupported sanitizers implicitly enabled through
219 // group expansion.
220 Add &= ~NotSupported;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000221
Alexey Samsonov799f7932014-12-19 02:35:16 +0000222 Kinds |= Add;
223 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
224 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000225 SanitizerMask Remove = parseArgValues(D, Arg, true);
Alexey Samsonov799f7932014-12-19 02:35:16 +0000226 AllRemove |= expandGroups(Remove);
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000227 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000228 }
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000229
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000230 // We disable the vptr sanitizer if it was enabled by group expansion but RTTI
231 // is disabled.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000232 if ((Kinds & Vptr) &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000233 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
234 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000235 Kinds &= ~Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000236 }
237
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000238 // Warn about undefined sanitizer options that require runtime support.
239 UbsanTrapOnError =
240 Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
241 options::OPT_fno_sanitize_undefined_trap_on_error, false);
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000242 if (UbsanTrapOnError && (Kinds & NotAllowedWithTrap)) {
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000243 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
244 << lastArgumentForMask(D, Args, NotAllowedWithTrap)
245 << "-fsanitize-undefined-trap-on-error";
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000246 Kinds &= ~NotAllowedWithTrap;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000247 }
248
249 // Warn about incompatible groups of sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000250 std::pair<SanitizerMask, SanitizerMask> IncompatibleGroups[] = {
251 std::make_pair(Address, Thread), std::make_pair(Address, Memory),
252 std::make_pair(Thread, Memory), std::make_pair(Leak, Thread),
253 std::make_pair(Leak, Memory)};
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000254 for (auto G : IncompatibleGroups) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000255 SanitizerMask Group = G.first;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000256 if (Kinds & Group) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000257 if (SanitizerMask Incompatible = Kinds & G.second) {
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000258 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
259 << lastArgumentForMask(D, Args, Group)
260 << lastArgumentForMask(D, Args, Incompatible);
261 Kinds &= ~Incompatible;
262 }
263 }
264 }
265 // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
266 // -fsanitize=address. Perhaps it should print an error, or perhaps
267 // -f(-no)sanitize=leak should change whether leak detection is enabled by
268 // default in ASan?
269
Alexey Samsonov88459522015-01-12 22:39:12 +0000270 // Parse -f(no-)?sanitize-recover flags.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000271 SanitizerMask RecoverableKinds = RecoverableByDefault;
272 SanitizerMask DiagnosedUnrecoverableKinds = 0;
Alexey Samsonov88459522015-01-12 22:39:12 +0000273 for (const auto *Arg : Args) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000274 const char *DeprecatedReplacement = nullptr;
Alexey Samsonov88459522015-01-12 22:39:12 +0000275 if (Arg->getOption().matches(options::OPT_fsanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000276 DeprecatedReplacement = "-fsanitize-recover=undefined,integer";
Alexey Samsonov88459522015-01-12 22:39:12 +0000277 RecoverableKinds |= expandGroups(LegacyFsanitizeRecoverMask);
278 Arg->claim();
279 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000280 DeprecatedReplacement = "-fno-sanitize-recover=undefined,integer";
Alexey Samsonov88459522015-01-12 22:39:12 +0000281 RecoverableKinds &= ~expandGroups(LegacyFsanitizeRecoverMask);
282 Arg->claim();
283 } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000284 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonov88459522015-01-12 22:39:12 +0000285 // Report error if user explicitly tries to recover from unrecoverable
286 // sanitizer.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000287 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov88459522015-01-12 22:39:12 +0000288 Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
289 SanitizerSet SetToDiagnose;
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000290 SetToDiagnose.Mask |= KindsToDiagnose;
Alexey Samsonov88459522015-01-12 22:39:12 +0000291 D.Diag(diag::err_drv_unsupported_option_argument)
292 << Arg->getOption().getName() << toString(SetToDiagnose);
293 DiagnosedUnrecoverableKinds |= KindsToDiagnose;
294 }
295 RecoverableKinds |= expandGroups(Add);
296 Arg->claim();
297 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
298 RecoverableKinds &= ~expandGroups(parseArgValues(D, Arg, true));
299 Arg->claim();
300 }
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000301 if (DeprecatedReplacement) {
302 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
303 << DeprecatedReplacement;
304 }
Alexey Samsonov88459522015-01-12 22:39:12 +0000305 }
306 RecoverableKinds &= Kinds;
307 RecoverableKinds &= ~Unrecoverable;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000308
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000309 // Setup blacklist files.
310 // Add default blacklist from resource directory.
311 {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000312 std::string BLPath;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000313 if (getDefaultBlacklist(D, Kinds, BLPath) && llvm::sys::fs::exists(BLPath))
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000314 BlacklistFiles.push_back(BLPath);
315 }
316 // Parse -f(no-)sanitize-blacklist options.
317 for (const auto *Arg : Args) {
318 if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) {
319 Arg->claim();
320 std::string BLPath = Arg->getValue();
321 if (llvm::sys::fs::exists(BLPath))
322 BlacklistFiles.push_back(BLPath);
323 else
324 D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
325 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) {
326 Arg->claim();
327 BlacklistFiles.clear();
328 }
329 }
330 // Validate blacklists format.
331 {
332 std::string BLError;
333 std::unique_ptr<llvm::SpecialCaseList> SCL(
334 llvm::SpecialCaseList::create(BlacklistFiles, BLError));
335 if (!SCL.get())
336 D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000337 }
338
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000339 // Parse -f[no-]sanitize-memory-track-origins[=level] options.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000340 if (Kinds & Memory) {
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000341 if (Arg *A =
342 Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
343 options::OPT_fsanitize_memory_track_origins,
344 options::OPT_fno_sanitize_memory_track_origins)) {
345 if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
Evgeniy Stepanov6e09bca2015-02-26 15:59:30 +0000346 MsanTrackOrigins = 2;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000347 } else if (A->getOption().matches(
348 options::OPT_fno_sanitize_memory_track_origins)) {
349 MsanTrackOrigins = 0;
350 } else {
351 StringRef S = A->getValue();
352 if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
353 MsanTrackOrigins > 2) {
Nico Weberf2a39a72015-04-13 20:03:03 +0000354 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000355 }
356 }
357 }
358 }
359
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000360 // Parse -f(no-)?sanitize-coverage flags if coverage is supported by the
361 // enabled sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000362 if (Kinds & SupportsCoverage) {
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000363 for (const auto *Arg : Args) {
364 if (Arg->getOption().matches(options::OPT_fsanitize_coverage)) {
365 Arg->claim();
366 int LegacySanitizeCoverage;
367 if (Arg->getNumValues() == 1 &&
368 !StringRef(Arg->getValue(0))
369 .getAsInteger(0, LegacySanitizeCoverage) &&
370 LegacySanitizeCoverage >= 0 && LegacySanitizeCoverage <= 4) {
371 // TODO: Add deprecation notice for this form.
372 switch (LegacySanitizeCoverage) {
373 case 0:
374 CoverageFeatures = 0;
375 break;
376 case 1:
377 CoverageFeatures = CoverageFunc;
378 break;
379 case 2:
380 CoverageFeatures = CoverageBB;
381 break;
382 case 3:
383 CoverageFeatures = CoverageEdge;
384 break;
385 case 4:
386 CoverageFeatures = CoverageEdge | CoverageIndirCall;
387 break;
388 }
389 continue;
390 }
391 CoverageFeatures |= parseCoverageFeatures(D, Arg);
392 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_coverage)) {
393 Arg->claim();
394 CoverageFeatures &= ~parseCoverageFeatures(D, Arg);
395 }
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000396 }
397 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000398 // Choose at most one coverage type: function, bb, or edge.
399 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageBB))
400 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
401 << "-fsanitize-coverage=func"
402 << "-fsanitize-coverage=bb";
403 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageEdge))
404 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
405 << "-fsanitize-coverage=func"
406 << "-fsanitize-coverage=edge";
407 if ((CoverageFeatures & CoverageBB) && (CoverageFeatures & CoverageEdge))
408 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
409 << "-fsanitize-coverage=bb"
410 << "-fsanitize-coverage=edge";
411 // Basic block tracing and 8-bit counters require some type of coverage
412 // enabled.
413 int CoverageTypes = CoverageFunc | CoverageBB | CoverageEdge;
414 if ((CoverageFeatures & CoverageTraceBB) &&
415 !(CoverageFeatures & CoverageTypes))
416 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
417 << "-fsanitize-coverage=trace-bb"
418 << "-fsanitize-coverage=(func|bb|edge)";
419 if ((CoverageFeatures & Coverage8bitCounters) &&
420 !(CoverageFeatures & CoverageTypes))
421 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
422 << "-fsanitize-coverage=8bit-counters"
423 << "-fsanitize-coverage=(func|bb|edge)";
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000424
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000425 if (Kinds & Address) {
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000426 AsanSharedRuntime =
Evgeniy Stepanov6f0ae182014-06-05 11:14:00 +0000427 Args.hasArg(options::OPT_shared_libasan) ||
428 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Peter Collingbourne32701642013-11-01 18:16:25 +0000429 AsanZeroBaseShadow =
Evgeniy Stepanovd04b8612014-01-16 10:19:31 +0000430 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000431 if (Arg *A =
432 Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
433 StringRef S = A->getValue();
434 // Legal values are 0 and 1, 2, but in future we may add more levels.
435 if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
436 AsanFieldPadding > 2) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000437 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000438 }
439 }
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000440
441 if (Arg *WindowsDebugRTArg =
442 Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
443 options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
444 options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
445 switch (WindowsDebugRTArg->getOption().getID()) {
446 case options::OPT__SLASH_MTd:
447 case options::OPT__SLASH_MDd:
448 case options::OPT__SLASH_LDd:
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000449 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000450 << WindowsDebugRTArg->getAsString(Args)
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000451 << lastArgumentForMask(D, Args, Address);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000452 D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000453 }
454 }
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000455 }
Alexey Samsonov90490af2014-08-08 22:47:17 +0000456
457 // Parse -link-cxx-sanitizer flag.
458 LinkCXXRuntimes =
459 Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX();
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000460
461 // Finally, initialize the set of available and recoverable sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000462 Sanitizers.Mask |= Kinds;
463 RecoverableSanitizers.Mask |= RecoverableKinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000464}
465
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000466static std::string toString(const clang::SanitizerSet &Sanitizers) {
467 std::string Res;
468#define SANITIZER(NAME, ID) \
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000469 if (Sanitizers.has(ID)) { \
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000470 if (!Res.empty()) \
471 Res += ","; \
472 Res += NAME; \
473 }
474#include "clang/Basic/Sanitizers.def"
475 return Res;
476}
477
Peter Collingbourne32701642013-11-01 18:16:25 +0000478void SanitizerArgs::addArgs(const llvm::opt::ArgList &Args,
Alexey Samsonovcf055962013-08-08 10:11:02 +0000479 llvm::opt::ArgStringList &CmdArgs) const {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000480 if (Sanitizers.empty())
Alexey Samsonovcf055962013-08-08 10:11:02 +0000481 return;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000482 CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
483
Alexey Samsonov88459522015-01-12 22:39:12 +0000484 if (!RecoverableSanitizers.empty())
485 CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
486 toString(RecoverableSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000487
488 if (UbsanTrapOnError)
489 CmdArgs.push_back("-fsanitize-undefined-trap-on-error");
490
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000491 for (const auto &BLPath : BlacklistFiles) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000492 SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000493 BlacklistOpt += BLPath;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000494 CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
495 }
496
497 if (MsanTrackOrigins)
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000498 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
499 llvm::utostr(MsanTrackOrigins)));
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000500 if (AsanFieldPadding)
501 CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
502 llvm::utostr(AsanFieldPadding)));
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000503 // Translate available CoverageFeatures to corresponding clang-cc1 flags.
504 std::pair<int, const char *> CoverageFlags[] = {
505 std::make_pair(CoverageFunc, "-fsanitize-coverage-type=1"),
506 std::make_pair(CoverageBB, "-fsanitize-coverage-type=2"),
507 std::make_pair(CoverageEdge, "-fsanitize-coverage-type=3"),
508 std::make_pair(CoverageIndirCall, "-fsanitize-coverage-indirect-calls"),
509 std::make_pair(CoverageTraceBB, "-fsanitize-coverage-trace-bb"),
510 std::make_pair(CoverageTraceCmp, "-fsanitize-coverage-trace-cmp"),
511 std::make_pair(Coverage8bitCounters, "-fsanitize-coverage-8bit-counters")};
512 for (auto F : CoverageFlags) {
513 if (CoverageFeatures & F.first)
514 CmdArgs.push_back(Args.MakeArgString(F.second));
Alexey Samsonov3f3b3ab2015-05-07 18:31:29 +0000515 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000516
517
Sergey Matveev2ba87782015-02-17 15:09:33 +0000518 // MSan: Workaround for PR16386.
519 // ASan: This is mainly to help LSan with cases such as
520 // https://code.google.com/p/address-sanitizer/issues/detail?id=373
521 // We can't make this conditional on -fsanitize=leak, as that flag shouldn't
522 // affect compilation.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000523 if (Sanitizers.has(Memory) || Sanitizers.has(Address))
Alexey Samsonovcf055962013-08-08 10:11:02 +0000524 CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
525}
526
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000527SanitizerMask parseValue(const char *Value) {
528 SanitizerMask ParsedKind = llvm::StringSwitch<SanitizerMask>(Value)
Alexey Samsonovcf055962013-08-08 10:11:02 +0000529#define SANITIZER(NAME, ID) .Case(NAME, ID)
Peter Collingbourne32701642013-11-01 18:16:25 +0000530#define SANITIZER_GROUP(NAME, ID, ALIAS) .Case(NAME, ID##Group)
Alexey Samsonovcf055962013-08-08 10:11:02 +0000531#include "clang/Basic/Sanitizers.def"
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000532 .Default(0);
Alexey Samsonovcf055962013-08-08 10:11:02 +0000533 return ParsedKind;
534}
535
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000536SanitizerMask expandGroups(SanitizerMask Kinds) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000537#define SANITIZER(NAME, ID)
538#define SANITIZER_GROUP(NAME, ID, ALIAS) if (Kinds & ID##Group) Kinds |= ID;
539#include "clang/Basic/Sanitizers.def"
540 return Kinds;
541}
542
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000543SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
544 bool DiagnoseErrors) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000545 assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
Alexey Samsonov88459522015-01-12 22:39:12 +0000546 A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
547 A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
548 A->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) &&
Alexey Samsonov799f7932014-12-19 02:35:16 +0000549 "Invalid argument in parseArgValues!");
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000550 SanitizerMask Kinds = 0;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000551 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
552 const char *Value = A->getValue(i);
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000553 SanitizerMask Kind;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000554 // Special case: don't accept -fsanitize=all.
555 if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
556 0 == strcmp("all", Value))
557 Kind = 0;
558 else
559 Kind = parseValue(Value);
560
561 if (Kind)
562 Kinds |= Kind;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000563 else if (DiagnoseErrors)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000564 D.Diag(clang::diag::err_drv_unsupported_option_argument)
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000565 << A->getOption().getName() << Value;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000566 }
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000567 return Kinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000568}
569
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000570int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A) {
571 assert(A->getOption().matches(options::OPT_fsanitize_coverage) ||
572 A->getOption().matches(options::OPT_fno_sanitize_coverage));
573 int Features = 0;
574 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
575 const char *Value = A->getValue(i);
576 int F = llvm::StringSwitch<int>(Value)
577 .Case("func", CoverageFunc)
578 .Case("bb", CoverageBB)
579 .Case("edge", CoverageEdge)
580 .Case("indirect-calls", CoverageIndirCall)
581 .Case("trace-bb", CoverageTraceBB)
582 .Case("trace-cmp", CoverageTraceCmp)
583 .Case("8bit-counters", Coverage8bitCounters)
584 .Default(0);
585 if (F == 0)
586 D.Diag(clang::diag::err_drv_unsupported_option_argument)
587 << A->getOption().getName() << Value;
588 Features |= F;
589 }
590 return Features;
591}
592
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000593std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000594 SanitizerMask Mask) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000595 for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
596 E = Args.rend();
597 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000598 const auto *Arg = *I;
599 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000600 SanitizerMask AddKinds = expandGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000601 if (AddKinds & Mask)
602 return describeSanitizeArg(Arg, Mask);
603 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000604 SanitizerMask RemoveKinds = expandGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000605 Mask &= ~RemoveKinds;
606 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000607 }
608 llvm_unreachable("arg list didn't provide expected value");
609}
610
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000611std::string describeSanitizeArg(const llvm::opt::Arg *A, SanitizerMask Mask) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000612 assert(A->getOption().matches(options::OPT_fsanitize_EQ)
613 && "Invalid argument in describeSanitizerArg!");
Alexey Samsonovcf055962013-08-08 10:11:02 +0000614
Peter Collingbourne32701642013-11-01 18:16:25 +0000615 std::string Sanitizers;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000616 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
617 if (expandGroups(parseValue(A->getValue(i))) & Mask) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000618 if (!Sanitizers.empty())
619 Sanitizers += ",";
Alexey Samsonov83791e22015-03-03 22:15:32 +0000620 Sanitizers += A->getValue(i);
Peter Collingbourne32701642013-11-01 18:16:25 +0000621 }
622 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000623
Peter Collingbourne32701642013-11-01 18:16:25 +0000624 assert(!Sanitizers.empty() && "arg didn't provide expected value");
625 return "-fsanitize=" + Sanitizers;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000626}