blob: 528b7243c5f2f631291b995a887878107af52aca [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
21using namespace clang::driver;
22using namespace llvm::opt;
23
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000024namespace {
25/// Assign ordinals to possible values of -fsanitize= flag.
26/// We use the ordinal values as bit positions within \c SanitizeKind.
Alexey Samsonov83791e22015-03-03 22:15:32 +000027enum SanitizeOrdinal : uint64_t {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000028#define SANITIZER(NAME, ID) SO_##ID,
29#define SANITIZER_GROUP(NAME, ID, ALIAS) SO_##ID##Group,
30#include "clang/Basic/Sanitizers.def"
31 SO_Count
32};
33
34/// Represents a set of sanitizer kinds. It is also used to define:
35/// 1) set of sanitizers each sanitizer group expands into.
36/// 2) set of sanitizers sharing a specific property (e.g.
37/// all sanitizers with zero-base shadow).
Alexey Samsonov83791e22015-03-03 22:15:32 +000038enum SanitizeKind : uint64_t {
39#define SANITIZER(NAME, ID) ID = 1ULL << SO_##ID,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000040#define SANITIZER_GROUP(NAME, ID, ALIAS) \
Alexey Samsonov83791e22015-03-03 22:15:32 +000041 ID = ALIAS, ID##Group = 1ULL << SO_##ID##Group,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000042#include "clang/Basic/Sanitizers.def"
43 NeedsUbsanRt = Undefined | Integer,
44 NotAllowedWithTrap = Vptr,
Dmitry Vyukov43419a72014-11-21 12:19:01 +000045 RequiresPIE = Memory | DataFlow,
Kostya Serebryany2d88f3d2015-01-06 01:02:48 +000046 NeedsUnwindTables = Address | Thread | Memory | DataFlow,
Kostya Serebryany6fe5fcb2015-03-20 00:06:52 +000047 SupportsCoverage = Address | Memory | Leak | Undefined | Integer | DataFlow,
Alexey Samsonov88459522015-01-12 22:39:12 +000048 RecoverableByDefault = Undefined | Integer,
49 Unrecoverable = Address | Unreachable | Return,
Peter Collingbournea4ccff32015-02-20 20:30:56 +000050 LegacyFsanitizeRecoverMask = Undefined | Integer,
Peter Collingbourne1a7488a2015-04-02 00:23:30 +000051 NeedsLTO = CFI,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000052};
53}
54
55/// Returns true if set of \p Sanitizers contain at least one sanitizer from
56/// \p Kinds.
Alexey Samsonov83791e22015-03-03 22:15:32 +000057static bool hasOneOf(const clang::SanitizerSet &Sanitizers, uint64_t Kinds) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000058#define SANITIZER(NAME, ID) \
59 if (Sanitizers.has(clang::SanitizerKind::ID) && (Kinds & ID)) \
60 return true;
61#include "clang/Basic/Sanitizers.def"
62 return false;
63}
64
65/// Adds all sanitizers from \p Kinds to \p Sanitizers.
Alexey Samsonov83791e22015-03-03 22:15:32 +000066static void addAllOf(clang::SanitizerSet &Sanitizers, uint64_t Kinds) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000067#define SANITIZER(NAME, ID) \
68 if (Kinds & ID) \
69 Sanitizers.set(clang::SanitizerKind::ID, true);
70#include "clang/Basic/Sanitizers.def"
71}
72
Alexey Samsonov83791e22015-03-03 22:15:32 +000073static uint64_t toSanitizeKind(clang::SanitizerKind K) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000074#define SANITIZER(NAME, ID) \
75 if (K == clang::SanitizerKind::ID) \
76 return ID;
77#include "clang/Basic/Sanitizers.def"
78 llvm_unreachable("Invalid SanitizerKind!");
79}
80
81/// Parse a single value from a -fsanitize= or -fno-sanitize= value list.
82/// Returns a member of the \c SanitizeKind enumeration, or \c 0
83/// if \p Value is not known.
Alexey Samsonov83791e22015-03-03 22:15:32 +000084static uint64_t parseValue(const char *Value);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000085
86/// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any
87/// invalid components. Returns OR of members of \c SanitizeKind enumeration.
Alexey Samsonov83791e22015-03-03 22:15:32 +000088static uint64_t parseArgValues(const Driver &D, const llvm::opt::Arg *A,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000089 bool DiagnoseErrors);
90
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000091/// Produce an argument string from ArgList \p Args, which shows how it
92/// provides some sanitizer kind from \p Mask. For example, the argument list
93/// "-fsanitize=thread,vptr -fsanitize=address" with mask \c NeedsUbsanRt
94/// would produce "-fsanitize=vptr".
95static std::string lastArgumentForMask(const Driver &D,
96 const llvm::opt::ArgList &Args,
Alexey Samsonov83791e22015-03-03 22:15:32 +000097 uint64_t Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000098
99static std::string lastArgumentForKind(const Driver &D,
100 const llvm::opt::ArgList &Args,
101 clang::SanitizerKind K) {
102 return lastArgumentForMask(D, Args, toSanitizeKind(K));
103}
104
105/// Produce an argument string from argument \p A, which shows how it provides
106/// a value in \p Mask. For instance, the argument
107/// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce
108/// "-fsanitize=alignment".
Alexey Samsonov83791e22015-03-03 22:15:32 +0000109static std::string describeSanitizeArg(const llvm::opt::Arg *A, uint64_t Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000110
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000111/// Produce a string containing comma-separated names of sanitizers in \p
112/// Sanitizers set.
113static std::string toString(const clang::SanitizerSet &Sanitizers);
114
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000115/// For each sanitizer group bit set in \p Kinds, set the bits for sanitizers
116/// this group enables.
Alexey Samsonov83791e22015-03-03 22:15:32 +0000117static uint64_t expandGroups(uint64_t Kinds);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000118
Alexey Samsonov83791e22015-03-03 22:15:32 +0000119static uint64_t getToolchainUnsupportedKinds(const ToolChain &TC) {
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000120 bool IsFreeBSD = TC.getTriple().getOS() == llvm::Triple::FreeBSD;
121 bool IsLinux = TC.getTriple().getOS() == llvm::Triple::Linux;
122 bool IsX86 = TC.getTriple().getArch() == llvm::Triple::x86;
123 bool IsX86_64 = TC.getTriple().getArch() == llvm::Triple::x86_64;
Mohit K. Bhakkadf4c47f62015-01-22 07:21:22 +0000124 bool IsMIPS64 = TC.getTriple().getArch() == llvm::Triple::mips64 ||
125 TC.getTriple().getArch() == llvm::Triple::mips64el;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000126
Alexey Samsonov83791e22015-03-03 22:15:32 +0000127 uint64_t Unsupported = 0;
Mohit K. Bhakkadf4c47f62015-01-22 07:21:22 +0000128 if (!(IsLinux && (IsX86_64 || IsMIPS64))) {
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000129 Unsupported |= Memory | DataFlow;
130 }
Mohit K. Bhakkad69963e72015-02-23 09:32:35 +0000131 if (!((IsLinux || IsFreeBSD) && (IsX86_64 || IsMIPS64))) {
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000132 Unsupported |= Thread;
133 }
134 if (!(IsLinux && (IsX86 || IsX86_64))) {
135 Unsupported |= Function;
136 }
137 return Unsupported;
138}
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000139
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000140static bool getDefaultBlacklist(const Driver &D, uint64_t Kinds,
141 std::string &BLPath) {
142 const char *BlacklistFile = nullptr;
143 if (Kinds & SanitizeKind::Address)
144 BlacklistFile = "asan_blacklist.txt";
145 else if (Kinds & SanitizeKind::Memory)
146 BlacklistFile = "msan_blacklist.txt";
147 else if (Kinds & SanitizeKind::Thread)
148 BlacklistFile = "tsan_blacklist.txt";
149 else if (Kinds & SanitizeKind::DataFlow)
150 BlacklistFile = "dfsan_abilist.txt";
151
152 if (BlacklistFile) {
153 clang::SmallString<64> Path(D.ResourceDir);
154 llvm::sys::path::append(Path, BlacklistFile);
155 BLPath = Path.str();
156 return true;
157 }
158 return false;
159}
160
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000161bool SanitizerArgs::needsUbsanRt() const {
Alexey Samsonov31095462015-04-01 22:42:25 +0000162 return !UbsanTrapOnError && hasOneOf(Sanitizers, NeedsUbsanRt) &&
Alexey Samsonovd60832e2015-04-28 00:56:36 +0000163 !Sanitizers.has(SanitizerKind::Address) &&
164 !Sanitizers.has(SanitizerKind::Memory) &&
165 !Sanitizers.has(SanitizerKind::Thread);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000166}
167
Dmitry Vyukov43419a72014-11-21 12:19:01 +0000168bool SanitizerArgs::requiresPIE() const {
169 return AsanZeroBaseShadow || hasOneOf(Sanitizers, RequiresPIE);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000170}
171
172bool SanitizerArgs::needsUnwindTables() const {
173 return hasOneOf(Sanitizers, NeedsUnwindTables);
174}
175
Peter Collingbournea4ccff32015-02-20 20:30:56 +0000176bool SanitizerArgs::needsLTO() const {
Peter Collingbourned2926c92015-03-14 02:42:25 +0000177 return hasOneOf(Sanitizers, NeedsLTO);
Peter Collingbournea4ccff32015-02-20 20:30:56 +0000178}
179
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000180void SanitizerArgs::clear() {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000181 Sanitizers.clear();
Alexey Samsonov88459522015-01-12 22:39:12 +0000182 RecoverableSanitizers.clear();
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000183 BlacklistFiles.clear();
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000184 SanitizeCoverage = 0;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000185 MsanTrackOrigins = 0;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000186 AsanFieldPadding = 0;
Peter Collingbourne32701642013-11-01 18:16:25 +0000187 AsanZeroBaseShadow = false;
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000188 UbsanTrapOnError = false;
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000189 AsanSharedRuntime = false;
Alexey Samsonov90490af2014-08-08 22:47:17 +0000190 LinkCXXRuntimes = false;
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000191}
Alexey Samsonovcf055962013-08-08 10:11:02 +0000192
Peter Collingbourne32701642013-11-01 18:16:25 +0000193SanitizerArgs::SanitizerArgs(const ToolChain &TC,
194 const llvm::opt::ArgList &Args) {
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000195 clear();
Alexey Samsonov83791e22015-03-03 22:15:32 +0000196 uint64_t AllRemove = 0; // During the loop below, the accumulated set of
Peter Collingbourne32701642013-11-01 18:16:25 +0000197 // sanitizers disabled by the current sanitizer
198 // argument or any argument after it.
Alexey Samsonov83791e22015-03-03 22:15:32 +0000199 uint64_t DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
Peter Collingbourne32701642013-11-01 18:16:25 +0000200 // Used to deduplicate diagnostics.
Alexey Samsonov83791e22015-03-03 22:15:32 +0000201 uint64_t Kinds = 0;
202 uint64_t NotSupported = getToolchainUnsupportedKinds(TC);
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000203 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
204
Peter Collingbourne32701642013-11-01 18:16:25 +0000205 const Driver &D = TC.getDriver();
206 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
207 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000208 const auto *Arg = *I;
209 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
210 Arg->claim();
Alexey Samsonov83791e22015-03-03 22:15:32 +0000211 uint64_t Add = parseArgValues(D, Arg, true);
Peter Collingbourne32701642013-11-01 18:16:25 +0000212
Alexey Samsonov799f7932014-12-19 02:35:16 +0000213 // Avoid diagnosing any sanitizer which is disabled later.
214 Add &= ~AllRemove;
215 // At this point we have not expanded groups, so any unsupported
216 // sanitizers in Add are those which have been explicitly enabled.
217 // Diagnose them.
Alexey Samsonov83791e22015-03-03 22:15:32 +0000218 if (uint64_t KindsToDiagnose = Add & NotSupported & ~DiagnosedKinds) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000219 // Only diagnose the new kinds.
220 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
221 D.Diag(diag::err_drv_unsupported_opt_for_target)
222 << Desc << TC.getTriple().str();
223 DiagnosedKinds |= KindsToDiagnose;
224 }
225 Add &= ~NotSupported;
Peter Collingbourne32701642013-11-01 18:16:25 +0000226
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000227 // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups
228 // so we don't error out if -fno-rtti and -fsanitize=undefined were
229 // passed.
230 if (Add & SanitizeKind::Vptr &&
231 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
232 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
233 if (RTTIMode == ToolChain::RM_DisabledImplicitly)
234 // Warn about not having rtti enabled if the vptr sanitizer is
235 // explicitly enabled
236 D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default);
237 else {
238 const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg();
239 assert(NoRTTIArg &&
240 "RTTI disabled explicitly but we have no argument!");
241 D.Diag(diag::err_drv_argument_not_allowed_with)
242 << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args);
243 }
244
245 // Take out the Vptr sanitizer from the enabled sanitizers
246 AllRemove |= SanitizeKind::Vptr;
247 }
248
Alexey Samsonov799f7932014-12-19 02:35:16 +0000249 Add = expandGroups(Add);
250 // Group expansion may have enabled a sanitizer which is disabled later.
251 Add &= ~AllRemove;
252 // Silently discard any unsupported sanitizers implicitly enabled through
253 // group expansion.
254 Add &= ~NotSupported;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000255
Alexey Samsonov799f7932014-12-19 02:35:16 +0000256 Kinds |= Add;
257 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
258 Arg->claim();
Alexey Samsonov83791e22015-03-03 22:15:32 +0000259 uint64_t Remove = parseArgValues(D, Arg, true);
Alexey Samsonov799f7932014-12-19 02:35:16 +0000260 AllRemove |= expandGroups(Remove);
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000261 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000262 }
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000263
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000264 // We disable the vptr sanitizer if it was enabled by group expansion but RTTI
265 // is disabled.
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000266 if ((Kinds & SanitizeKind::Vptr) &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000267 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
268 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
269 Kinds &= ~SanitizeKind::Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000270 }
271
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000272 // Warn about undefined sanitizer options that require runtime support.
273 UbsanTrapOnError =
274 Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
275 options::OPT_fno_sanitize_undefined_trap_on_error, false);
276 if (UbsanTrapOnError && (Kinds & SanitizeKind::NotAllowedWithTrap)) {
277 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
278 << lastArgumentForMask(D, Args, NotAllowedWithTrap)
279 << "-fsanitize-undefined-trap-on-error";
280 Kinds &= ~SanitizeKind::NotAllowedWithTrap;
281 }
282
283 // Warn about incompatible groups of sanitizers.
284 std::pair<uint64_t, uint64_t> IncompatibleGroups[] = {
285 std::make_pair(SanitizeKind::Address, SanitizeKind::Thread),
286 std::make_pair(SanitizeKind::Address, SanitizeKind::Memory),
287 std::make_pair(SanitizeKind::Thread, SanitizeKind::Memory),
288 std::make_pair(SanitizeKind::Leak, SanitizeKind::Thread),
Alexey Samsonovd60832e2015-04-28 00:56:36 +0000289 std::make_pair(SanitizeKind::Leak, SanitizeKind::Memory)};
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000290 for (auto G : IncompatibleGroups) {
291 uint64_t Group = G.first;
292 if (Kinds & Group) {
293 if (uint64_t Incompatible = Kinds & G.second) {
294 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
295 << lastArgumentForMask(D, Args, Group)
296 << lastArgumentForMask(D, Args, Incompatible);
297 Kinds &= ~Incompatible;
298 }
299 }
300 }
301 // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
302 // -fsanitize=address. Perhaps it should print an error, or perhaps
303 // -f(-no)sanitize=leak should change whether leak detection is enabled by
304 // default in ASan?
305
Alexey Samsonov88459522015-01-12 22:39:12 +0000306 // Parse -f(no-)?sanitize-recover flags.
Alexey Samsonov83791e22015-03-03 22:15:32 +0000307 uint64_t RecoverableKinds = RecoverableByDefault;
308 uint64_t DiagnosedUnrecoverableKinds = 0;
Alexey Samsonov88459522015-01-12 22:39:12 +0000309 for (const auto *Arg : Args) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000310 const char *DeprecatedReplacement = nullptr;
Alexey Samsonov88459522015-01-12 22:39:12 +0000311 if (Arg->getOption().matches(options::OPT_fsanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000312 DeprecatedReplacement = "-fsanitize-recover=undefined,integer";
Alexey Samsonov88459522015-01-12 22:39:12 +0000313 RecoverableKinds |= expandGroups(LegacyFsanitizeRecoverMask);
314 Arg->claim();
315 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000316 DeprecatedReplacement = "-fno-sanitize-recover=undefined,integer";
Alexey Samsonov88459522015-01-12 22:39:12 +0000317 RecoverableKinds &= ~expandGroups(LegacyFsanitizeRecoverMask);
318 Arg->claim();
319 } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
Alexey Samsonov83791e22015-03-03 22:15:32 +0000320 uint64_t Add = parseArgValues(D, Arg, true);
Alexey Samsonov88459522015-01-12 22:39:12 +0000321 // Report error if user explicitly tries to recover from unrecoverable
322 // sanitizer.
Alexey Samsonov83791e22015-03-03 22:15:32 +0000323 if (uint64_t KindsToDiagnose =
Alexey Samsonov88459522015-01-12 22:39:12 +0000324 Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
325 SanitizerSet SetToDiagnose;
326 addAllOf(SetToDiagnose, KindsToDiagnose);
327 D.Diag(diag::err_drv_unsupported_option_argument)
328 << Arg->getOption().getName() << toString(SetToDiagnose);
329 DiagnosedUnrecoverableKinds |= KindsToDiagnose;
330 }
331 RecoverableKinds |= expandGroups(Add);
332 Arg->claim();
333 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
334 RecoverableKinds &= ~expandGroups(parseArgValues(D, Arg, true));
335 Arg->claim();
336 }
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000337 if (DeprecatedReplacement) {
338 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
339 << DeprecatedReplacement;
340 }
Alexey Samsonov88459522015-01-12 22:39:12 +0000341 }
342 RecoverableKinds &= Kinds;
343 RecoverableKinds &= ~Unrecoverable;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000344
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000345 // Setup blacklist files.
346 // Add default blacklist from resource directory.
347 {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000348 std::string BLPath;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000349 if (getDefaultBlacklist(D, Kinds, BLPath) && llvm::sys::fs::exists(BLPath))
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000350 BlacklistFiles.push_back(BLPath);
351 }
352 // Parse -f(no-)sanitize-blacklist options.
353 for (const auto *Arg : Args) {
354 if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) {
355 Arg->claim();
356 std::string BLPath = Arg->getValue();
357 if (llvm::sys::fs::exists(BLPath))
358 BlacklistFiles.push_back(BLPath);
359 else
360 D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
361 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) {
362 Arg->claim();
363 BlacklistFiles.clear();
364 }
365 }
366 // Validate blacklists format.
367 {
368 std::string BLError;
369 std::unique_ptr<llvm::SpecialCaseList> SCL(
370 llvm::SpecialCaseList::create(BlacklistFiles, BLError));
371 if (!SCL.get())
372 D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000373 }
374
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000375 // Parse -f[no-]sanitize-memory-track-origins[=level] options.
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000376 if (Kinds & SanitizeKind::Memory) {
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000377 if (Arg *A =
378 Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
379 options::OPT_fsanitize_memory_track_origins,
380 options::OPT_fno_sanitize_memory_track_origins)) {
381 if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
Evgeniy Stepanov6e09bca2015-02-26 15:59:30 +0000382 MsanTrackOrigins = 2;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000383 } else if (A->getOption().matches(
384 options::OPT_fno_sanitize_memory_track_origins)) {
385 MsanTrackOrigins = 0;
386 } else {
387 StringRef S = A->getValue();
388 if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
389 MsanTrackOrigins > 2) {
Nico Weberf2a39a72015-04-13 20:03:03 +0000390 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000391 }
392 }
393 }
394 }
395
Kostya Serebryany78df9d02014-12-17 21:46:33 +0000396 // Parse -fsanitize-coverage=N. Currently one of asan/msan/lsan is required.
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000397 if (Kinds & SanitizeKind::SupportsCoverage) {
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000398 if (Arg *A = Args.getLastArg(options::OPT_fsanitize_coverage)) {
399 StringRef S = A->getValue();
400 // Legal values are 0..4.
401 if (S.getAsInteger(0, SanitizeCoverage) || SanitizeCoverage < 0 ||
402 SanitizeCoverage > 4)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000403 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000404 }
405 }
406
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000407 if (Kinds & SanitizeKind::Address) {
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000408 AsanSharedRuntime =
Evgeniy Stepanov6f0ae182014-06-05 11:14:00 +0000409 Args.hasArg(options::OPT_shared_libasan) ||
410 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Peter Collingbourne32701642013-11-01 18:16:25 +0000411 AsanZeroBaseShadow =
Evgeniy Stepanovd04b8612014-01-16 10:19:31 +0000412 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000413 if (Arg *A =
414 Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
415 StringRef S = A->getValue();
416 // Legal values are 0 and 1, 2, but in future we may add more levels.
417 if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
418 AsanFieldPadding > 2) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000419 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000420 }
421 }
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000422
423 if (Arg *WindowsDebugRTArg =
424 Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
425 options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
426 options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
427 switch (WindowsDebugRTArg->getOption().getID()) {
428 case options::OPT__SLASH_MTd:
429 case options::OPT__SLASH_MDd:
430 case options::OPT__SLASH_LDd:
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000431 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000432 << WindowsDebugRTArg->getAsString(Args)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000433 << lastArgumentForKind(D, Args, SanitizerKind::Address);
434 D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000435 }
436 }
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000437 }
Alexey Samsonov90490af2014-08-08 22:47:17 +0000438
439 // Parse -link-cxx-sanitizer flag.
440 LinkCXXRuntimes =
441 Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX();
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000442
443 // Finally, initialize the set of available and recoverable sanitizers.
444 addAllOf(Sanitizers, Kinds);
445 addAllOf(RecoverableSanitizers, RecoverableKinds);
Alexey Samsonovcf055962013-08-08 10:11:02 +0000446}
447
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000448static std::string toString(const clang::SanitizerSet &Sanitizers) {
449 std::string Res;
450#define SANITIZER(NAME, ID) \
451 if (Sanitizers.has(clang::SanitizerKind::ID)) { \
452 if (!Res.empty()) \
453 Res += ","; \
454 Res += NAME; \
455 }
456#include "clang/Basic/Sanitizers.def"
457 return Res;
458}
459
Peter Collingbourne32701642013-11-01 18:16:25 +0000460void SanitizerArgs::addArgs(const llvm::opt::ArgList &Args,
Alexey Samsonovcf055962013-08-08 10:11:02 +0000461 llvm::opt::ArgStringList &CmdArgs) const {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000462 if (Sanitizers.empty())
Alexey Samsonovcf055962013-08-08 10:11:02 +0000463 return;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000464 CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
465
Alexey Samsonov88459522015-01-12 22:39:12 +0000466 if (!RecoverableSanitizers.empty())
467 CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
468 toString(RecoverableSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000469
470 if (UbsanTrapOnError)
471 CmdArgs.push_back("-fsanitize-undefined-trap-on-error");
472
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000473 for (const auto &BLPath : BlacklistFiles) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000474 SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000475 BlacklistOpt += BLPath;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000476 CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
477 }
478
479 if (MsanTrackOrigins)
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000480 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
481 llvm::utostr(MsanTrackOrigins)));
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000482 if (AsanFieldPadding)
483 CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
484 llvm::utostr(AsanFieldPadding)));
Alexey Samsonov3f3b3ab2015-05-07 18:31:29 +0000485 if (SanitizeCoverage) {
486 int CoverageType = std::min(SanitizeCoverage, 3);
487 CmdArgs.push_back(Args.MakeArgString("-fsanitize-coverage-type=" +
488 llvm::utostr(CoverageType)));
489 if (SanitizeCoverage == 4)
490 CmdArgs.push_back(
491 Args.MakeArgString("-fsanitize-coverage-indirect-calls"));
492 }
Sergey Matveev2ba87782015-02-17 15:09:33 +0000493 // MSan: Workaround for PR16386.
494 // ASan: This is mainly to help LSan with cases such as
495 // https://code.google.com/p/address-sanitizer/issues/detail?id=373
496 // We can't make this conditional on -fsanitize=leak, as that flag shouldn't
497 // affect compilation.
498 if (Sanitizers.has(SanitizerKind::Memory) ||
499 Sanitizers.has(SanitizerKind::Address))
Alexey Samsonovcf055962013-08-08 10:11:02 +0000500 CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
501}
502
Alexey Samsonov83791e22015-03-03 22:15:32 +0000503uint64_t parseValue(const char *Value) {
504 uint64_t ParsedKind = llvm::StringSwitch<SanitizeKind>(Value)
Alexey Samsonovcf055962013-08-08 10:11:02 +0000505#define SANITIZER(NAME, ID) .Case(NAME, ID)
Peter Collingbourne32701642013-11-01 18:16:25 +0000506#define SANITIZER_GROUP(NAME, ID, ALIAS) .Case(NAME, ID##Group)
Alexey Samsonovcf055962013-08-08 10:11:02 +0000507#include "clang/Basic/Sanitizers.def"
508 .Default(SanitizeKind());
Alexey Samsonovcf055962013-08-08 10:11:02 +0000509 return ParsedKind;
510}
511
Alexey Samsonov83791e22015-03-03 22:15:32 +0000512uint64_t expandGroups(uint64_t Kinds) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000513#define SANITIZER(NAME, ID)
514#define SANITIZER_GROUP(NAME, ID, ALIAS) if (Kinds & ID##Group) Kinds |= ID;
515#include "clang/Basic/Sanitizers.def"
516 return Kinds;
517}
518
Alexey Samsonov83791e22015-03-03 22:15:32 +0000519uint64_t parseArgValues(const Driver &D, const llvm::opt::Arg *A,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000520 bool DiagnoseErrors) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000521 assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
Alexey Samsonov88459522015-01-12 22:39:12 +0000522 A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
523 A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
524 A->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) &&
Alexey Samsonov799f7932014-12-19 02:35:16 +0000525 "Invalid argument in parseArgValues!");
Alexey Samsonov83791e22015-03-03 22:15:32 +0000526 uint64_t Kinds = 0;
527 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
528 const char *Value = A->getValue(i);
529 uint64_t Kind;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000530 // Special case: don't accept -fsanitize=all.
531 if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
532 0 == strcmp("all", Value))
533 Kind = 0;
534 else
535 Kind = parseValue(Value);
536
537 if (Kind)
538 Kinds |= Kind;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000539 else if (DiagnoseErrors)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000540 D.Diag(clang::diag::err_drv_unsupported_option_argument)
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000541 << A->getOption().getName() << Value;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000542 }
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000543 return Kinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000544}
545
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000546std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
Alexey Samsonov83791e22015-03-03 22:15:32 +0000547 uint64_t Mask) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000548 for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
549 E = Args.rend();
550 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000551 const auto *Arg = *I;
552 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
Alexey Samsonov83791e22015-03-03 22:15:32 +0000553 uint64_t AddKinds = expandGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000554 if (AddKinds & Mask)
555 return describeSanitizeArg(Arg, Mask);
556 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
Alexey Samsonov83791e22015-03-03 22:15:32 +0000557 uint64_t RemoveKinds = expandGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000558 Mask &= ~RemoveKinds;
559 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000560 }
561 llvm_unreachable("arg list didn't provide expected value");
562}
563
Alexey Samsonov83791e22015-03-03 22:15:32 +0000564std::string describeSanitizeArg(const llvm::opt::Arg *A, uint64_t Mask) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000565 assert(A->getOption().matches(options::OPT_fsanitize_EQ)
566 && "Invalid argument in describeSanitizerArg!");
Alexey Samsonovcf055962013-08-08 10:11:02 +0000567
Peter Collingbourne32701642013-11-01 18:16:25 +0000568 std::string Sanitizers;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000569 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
570 if (expandGroups(parseValue(A->getValue(i))) & Mask) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000571 if (!Sanitizers.empty())
572 Sanitizers += ",";
Alexey Samsonov83791e22015-03-03 22:15:32 +0000573 Sanitizers += A->getValue(i);
Peter Collingbourne32701642013-11-01 18:16:25 +0000574 }
575 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000576
Peter Collingbourne32701642013-11-01 18:16:25 +0000577 assert(!Sanitizers.empty() && "arg didn't provide expected value");
578 return "-fsanitize=" + Sanitizers;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000579}