blob: 9918fcbd5e1f8f9324f64e218c1724384923cc2f [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 {
Peter Collingbourne6708c4a2015-06-19 01:51:54 +000028 NeedsUbsanRt = Undefined | Integer | CFI,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000029 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,
Peter Collingbourne9881b782015-06-18 23:59:22 +000037 TrappingSupported =
Peter Collingbourne6708c4a2015-06-19 01:51:54 +000038 (Undefined & ~Vptr) | UnsignedIntegerOverflow | LocalBounds | CFI,
39 TrappingDefault = CFI,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000040};
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000041
42enum CoverageFeature {
43 CoverageFunc = 1 << 0,
44 CoverageBB = 1 << 1,
45 CoverageEdge = 1 << 2,
46 CoverageIndirCall = 1 << 3,
47 CoverageTraceBB = 1 << 4,
48 CoverageTraceCmp = 1 << 5,
49 Coverage8bitCounters = 1 << 6,
50};
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000051
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000052/// 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 Samsonov9bc2ad52015-06-17 22:27:32 +000080static SanitizerMask getToolchainUnsupportedKinds(const ToolChain &TC) {
81 bool IsFreeBSD = TC.getTriple().getOS() == llvm::Triple::FreeBSD;
82 bool IsLinux = TC.getTriple().getOS() == llvm::Triple::Linux;
83 bool IsX86 = TC.getTriple().getArch() == llvm::Triple::x86;
84 bool IsX86_64 = TC.getTriple().getArch() == llvm::Triple::x86_64;
85 bool IsMIPS64 = TC.getTriple().getArch() == llvm::Triple::mips64 ||
86 TC.getTriple().getArch() == llvm::Triple::mips64el;
87
88 SanitizerMask Unsupported = 0;
89 if (!(IsLinux && (IsX86_64 || IsMIPS64))) {
90 Unsupported |= Memory | DataFlow;
91 }
92 if (!((IsLinux || IsFreeBSD) && (IsX86_64 || IsMIPS64))) {
93 Unsupported |= Thread;
94 }
95 if (!(IsLinux && (IsX86 || IsX86_64))) {
96 Unsupported |= Function;
97 }
98 return Unsupported;
99}
100
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000101static bool getDefaultBlacklist(const Driver &D, SanitizerMask Kinds,
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000102 std::string &BLPath) {
103 const char *BlacklistFile = nullptr;
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000104 if (Kinds & Address)
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000105 BlacklistFile = "asan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000106 else if (Kinds & Memory)
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000107 BlacklistFile = "msan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000108 else if (Kinds & Thread)
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000109 BlacklistFile = "tsan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000110 else if (Kinds & DataFlow)
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000111 BlacklistFile = "dfsan_abilist.txt";
112
113 if (BlacklistFile) {
114 clang::SmallString<64> Path(D.ResourceDir);
115 llvm::sys::path::append(Path, BlacklistFile);
116 BLPath = Path.str();
117 return true;
118 }
119 return false;
120}
121
Peter Collingbourne9881b782015-06-18 23:59:22 +0000122/// Sets group bits for every group that has at least one representative already
123/// enabled in \p Kinds.
124static SanitizerMask setGroupBits(SanitizerMask Kinds) {
125#define SANITIZER(NAME, ID)
126#define SANITIZER_GROUP(NAME, ID, ALIAS) \
127 if (Kinds & SanitizerKind::ID) \
128 Kinds |= SanitizerKind::ID##Group;
129#include "clang/Basic/Sanitizers.def"
130 return Kinds;
131}
132
133static SanitizerMask parseSanitizeTrapArgs(const Driver &D,
134 const llvm::opt::ArgList &Args) {
135 SanitizerMask TrapRemove = 0; // During the loop below, the accumulated set of
136 // sanitizers disabled by the current sanitizer
137 // argument or any argument after it.
138 SanitizerMask TrappingKinds = 0;
139 SanitizerMask TrappingSupportedWithGroups = setGroupBits(TrappingSupported);
140
141 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
142 I != E; ++I) {
143 const auto *Arg = *I;
144 if (Arg->getOption().matches(options::OPT_fsanitize_trap_EQ)) {
145 Arg->claim();
146 SanitizerMask Add = parseArgValues(D, Arg, true);
147 Add &= ~TrapRemove;
148 if (SanitizerMask InvalidValues = Add & ~TrappingSupportedWithGroups) {
149 SanitizerSet S;
150 S.Mask = InvalidValues;
151 D.Diag(diag::err_drv_unsupported_option_argument) << "-fsanitize-trap"
152 << toString(S);
153 }
154 TrappingKinds |= expandSanitizerGroups(Add) & ~TrapRemove;
155 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) {
156 Arg->claim();
157 TrapRemove |= expandSanitizerGroups(parseArgValues(D, Arg, true));
158 } else if (Arg->getOption().matches(
159 options::OPT_fsanitize_undefined_trap_on_error)) {
160 Arg->claim();
161 TrappingKinds |=
162 expandSanitizerGroups(UndefinedGroup & ~TrapRemove) & ~TrapRemove;
163 } else if (Arg->getOption().matches(
164 options::OPT_fno_sanitize_undefined_trap_on_error)) {
165 Arg->claim();
166 TrapRemove |= expandSanitizerGroups(UndefinedGroup);
167 }
168 }
169
Peter Collingbourne6708c4a2015-06-19 01:51:54 +0000170 // Apply default trapping behavior.
171 TrappingKinds |= TrappingDefault & ~TrapRemove;
172
Peter Collingbourne9881b782015-06-18 23:59:22 +0000173 return TrappingKinds;
174}
175
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000176bool SanitizerArgs::needsUbsanRt() const {
Peter Collingbourne9881b782015-06-18 23:59:22 +0000177 return (Sanitizers.Mask & NeedsUbsanRt & ~TrapSanitizers.Mask) &&
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000178 !Sanitizers.has(Address) &&
179 !Sanitizers.has(Memory) &&
180 !Sanitizers.has(Thread);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000181}
182
Dmitry Vyukov43419a72014-11-21 12:19:01 +0000183bool SanitizerArgs::requiresPIE() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000184 return AsanZeroBaseShadow || (Sanitizers.Mask & RequiresPIE);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000185}
186
187bool SanitizerArgs::needsUnwindTables() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000188 return Sanitizers.Mask & NeedsUnwindTables;
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000189}
190
Peter Collingbournea4ccff32015-02-20 20:30:56 +0000191bool SanitizerArgs::needsLTO() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000192 return Sanitizers.Mask & NeedsLTO;
Peter Collingbournea4ccff32015-02-20 20:30:56 +0000193}
194
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000195void SanitizerArgs::clear() {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000196 Sanitizers.clear();
Alexey Samsonov88459522015-01-12 22:39:12 +0000197 RecoverableSanitizers.clear();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000198 TrapSanitizers.clear();
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000199 BlacklistFiles.clear();
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000200 CoverageFeatures = 0;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000201 MsanTrackOrigins = 0;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000202 AsanFieldPadding = 0;
Peter Collingbourne32701642013-11-01 18:16:25 +0000203 AsanZeroBaseShadow = false;
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000204 AsanSharedRuntime = false;
Alexey Samsonov90490af2014-08-08 22:47:17 +0000205 LinkCXXRuntimes = false;
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000206}
Alexey Samsonovcf055962013-08-08 10:11:02 +0000207
Peter Collingbourne32701642013-11-01 18:16:25 +0000208SanitizerArgs::SanitizerArgs(const ToolChain &TC,
209 const llvm::opt::ArgList &Args) {
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000210 clear();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000211 SanitizerMask AllRemove = 0; // During the loop below, the accumulated set of
212 // sanitizers disabled by the current sanitizer
213 // argument or any argument after it.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000214 SanitizerMask AllAddedKinds = 0; // Mask of all sanitizers ever enabled by
215 // -fsanitize= flags (directly or via group
216 // expansion), some of which may be disabled
217 // later. Used to carefully prune
218 // unused-argument diagnostics.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000219 SanitizerMask DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
220 // Used to deduplicate diagnostics.
221 SanitizerMask Kinds = 0;
Alexey Samsonov9bc2ad52015-06-17 22:27:32 +0000222 SanitizerMask NotSupported = getToolchainUnsupportedKinds(TC);
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000223 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
224
Peter Collingbourne32701642013-11-01 18:16:25 +0000225 const Driver &D = TC.getDriver();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000226 SanitizerMask TrappingKinds = parseSanitizeTrapArgs(D, Args);
227 NotSupported |= TrappingKinds & NotAllowedWithTrap;
228
Peter Collingbourne32701642013-11-01 18:16:25 +0000229 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
230 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000231 const auto *Arg = *I;
232 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
233 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000234 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000235 AllAddedKinds |= expandSanitizerGroups(Add);
Peter Collingbourne32701642013-11-01 18:16:25 +0000236
Alexey Samsonov799f7932014-12-19 02:35:16 +0000237 // Avoid diagnosing any sanitizer which is disabled later.
238 Add &= ~AllRemove;
239 // At this point we have not expanded groups, so any unsupported
240 // sanitizers in Add are those which have been explicitly enabled.
241 // Diagnose them.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000242 if (SanitizerMask KindsToDiagnose =
Peter Collingbourne9881b782015-06-18 23:59:22 +0000243 Add & TrappingKinds & NotAllowedWithTrap & ~DiagnosedKinds) {
244 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
245 D.Diag(diag::err_drv_argument_not_allowed_with)
246 << Desc << "-fsanitize-trap=undefined";
247 DiagnosedKinds |= KindsToDiagnose;
248 Add &= ~KindsToDiagnose;
249 }
250 if (SanitizerMask KindsToDiagnose = Add & NotSupported & ~DiagnosedKinds) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000251 // Only diagnose the new kinds.
252 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
253 D.Diag(diag::err_drv_unsupported_opt_for_target)
254 << Desc << TC.getTriple().str();
255 DiagnosedKinds |= KindsToDiagnose;
256 }
Alexey Samsonov9bc2ad52015-06-17 22:27:32 +0000257 Add &= ~NotSupported;
Peter Collingbourne32701642013-11-01 18:16:25 +0000258
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000259 // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups
260 // so we don't error out if -fno-rtti and -fsanitize=undefined were
261 // passed.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000262 if (Add & Vptr &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000263 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
264 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
265 if (RTTIMode == ToolChain::RM_DisabledImplicitly)
266 // Warn about not having rtti enabled if the vptr sanitizer is
267 // explicitly enabled
268 D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default);
269 else {
270 const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg();
271 assert(NoRTTIArg &&
272 "RTTI disabled explicitly but we have no argument!");
273 D.Diag(diag::err_drv_argument_not_allowed_with)
274 << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args);
275 }
276
277 // Take out the Vptr sanitizer from the enabled sanitizers
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000278 AllRemove |= Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000279 }
280
Peter Collingbournebf59c342015-05-11 21:39:20 +0000281 Add = expandSanitizerGroups(Add);
Alexey Samsonov799f7932014-12-19 02:35:16 +0000282 // Group expansion may have enabled a sanitizer which is disabled later.
283 Add &= ~AllRemove;
284 // Silently discard any unsupported sanitizers implicitly enabled through
285 // group expansion.
Alexey Samsonov9bc2ad52015-06-17 22:27:32 +0000286 Add &= ~NotSupported;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000287
Alexey Samsonov799f7932014-12-19 02:35:16 +0000288 Kinds |= Add;
289 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
290 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000291 SanitizerMask Remove = parseArgValues(D, Arg, true);
Peter Collingbournebf59c342015-05-11 21:39:20 +0000292 AllRemove |= expandSanitizerGroups(Remove);
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000293 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000294 }
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000295
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000296 // We disable the vptr sanitizer if it was enabled by group expansion but RTTI
297 // is disabled.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000298 if ((Kinds & Vptr) &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000299 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
300 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000301 Kinds &= ~Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000302 }
303
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000304 // Warn about incompatible groups of sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000305 std::pair<SanitizerMask, SanitizerMask> IncompatibleGroups[] = {
306 std::make_pair(Address, Thread), std::make_pair(Address, Memory),
307 std::make_pair(Thread, Memory), std::make_pair(Leak, Thread),
308 std::make_pair(Leak, Memory)};
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000309 for (auto G : IncompatibleGroups) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000310 SanitizerMask Group = G.first;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000311 if (Kinds & Group) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000312 if (SanitizerMask Incompatible = Kinds & G.second) {
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000313 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
314 << lastArgumentForMask(D, Args, Group)
315 << lastArgumentForMask(D, Args, Incompatible);
316 Kinds &= ~Incompatible;
317 }
318 }
319 }
320 // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
321 // -fsanitize=address. Perhaps it should print an error, or perhaps
322 // -f(-no)sanitize=leak should change whether leak detection is enabled by
323 // default in ASan?
324
Alexey Samsonov88459522015-01-12 22:39:12 +0000325 // Parse -f(no-)?sanitize-recover flags.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000326 SanitizerMask RecoverableKinds = RecoverableByDefault;
327 SanitizerMask DiagnosedUnrecoverableKinds = 0;
Alexey Samsonov88459522015-01-12 22:39:12 +0000328 for (const auto *Arg : Args) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000329 const char *DeprecatedReplacement = nullptr;
Alexey Samsonov88459522015-01-12 22:39:12 +0000330 if (Arg->getOption().matches(options::OPT_fsanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000331 DeprecatedReplacement = "-fsanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000332 RecoverableKinds |= expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000333 Arg->claim();
334 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000335 DeprecatedReplacement = "-fno-sanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000336 RecoverableKinds &= ~expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000337 Arg->claim();
338 } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000339 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonov88459522015-01-12 22:39:12 +0000340 // Report error if user explicitly tries to recover from unrecoverable
341 // sanitizer.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000342 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov88459522015-01-12 22:39:12 +0000343 Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
344 SanitizerSet SetToDiagnose;
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000345 SetToDiagnose.Mask |= KindsToDiagnose;
Alexey Samsonov88459522015-01-12 22:39:12 +0000346 D.Diag(diag::err_drv_unsupported_option_argument)
347 << Arg->getOption().getName() << toString(SetToDiagnose);
348 DiagnosedUnrecoverableKinds |= KindsToDiagnose;
349 }
Peter Collingbournebf59c342015-05-11 21:39:20 +0000350 RecoverableKinds |= expandSanitizerGroups(Add);
Alexey Samsonov88459522015-01-12 22:39:12 +0000351 Arg->claim();
352 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000353 RecoverableKinds &= ~expandSanitizerGroups(parseArgValues(D, Arg, true));
Alexey Samsonov88459522015-01-12 22:39:12 +0000354 Arg->claim();
355 }
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000356 if (DeprecatedReplacement) {
357 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
358 << DeprecatedReplacement;
359 }
Alexey Samsonov88459522015-01-12 22:39:12 +0000360 }
361 RecoverableKinds &= Kinds;
362 RecoverableKinds &= ~Unrecoverable;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000363
Peter Collingbourne9881b782015-06-18 23:59:22 +0000364 TrappingKinds &= Kinds;
365
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000366 // Setup blacklist files.
367 // Add default blacklist from resource directory.
368 {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000369 std::string BLPath;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000370 if (getDefaultBlacklist(D, Kinds, BLPath) && llvm::sys::fs::exists(BLPath))
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000371 BlacklistFiles.push_back(BLPath);
372 }
373 // Parse -f(no-)sanitize-blacklist options.
374 for (const auto *Arg : Args) {
375 if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) {
376 Arg->claim();
377 std::string BLPath = Arg->getValue();
378 if (llvm::sys::fs::exists(BLPath))
379 BlacklistFiles.push_back(BLPath);
380 else
381 D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
382 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) {
383 Arg->claim();
384 BlacklistFiles.clear();
385 }
386 }
387 // Validate blacklists format.
388 {
389 std::string BLError;
390 std::unique_ptr<llvm::SpecialCaseList> SCL(
391 llvm::SpecialCaseList::create(BlacklistFiles, BLError));
392 if (!SCL.get())
393 D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000394 }
395
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000396 // Parse -f[no-]sanitize-memory-track-origins[=level] options.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000397 if (AllAddedKinds & Memory) {
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000398 if (Arg *A =
399 Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
400 options::OPT_fsanitize_memory_track_origins,
401 options::OPT_fno_sanitize_memory_track_origins)) {
402 if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
Evgeniy Stepanov6e09bca2015-02-26 15:59:30 +0000403 MsanTrackOrigins = 2;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000404 } else if (A->getOption().matches(
405 options::OPT_fno_sanitize_memory_track_origins)) {
406 MsanTrackOrigins = 0;
407 } else {
408 StringRef S = A->getValue();
409 if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
410 MsanTrackOrigins > 2) {
Nico Weberf2a39a72015-04-13 20:03:03 +0000411 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000412 }
413 }
414 }
415 }
416
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000417 // Parse -f(no-)?sanitize-coverage flags if coverage is supported by the
418 // enabled sanitizers.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000419 if (AllAddedKinds & SupportsCoverage) {
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000420 for (const auto *Arg : Args) {
421 if (Arg->getOption().matches(options::OPT_fsanitize_coverage)) {
422 Arg->claim();
423 int LegacySanitizeCoverage;
424 if (Arg->getNumValues() == 1 &&
425 !StringRef(Arg->getValue(0))
426 .getAsInteger(0, LegacySanitizeCoverage) &&
427 LegacySanitizeCoverage >= 0 && LegacySanitizeCoverage <= 4) {
428 // TODO: Add deprecation notice for this form.
429 switch (LegacySanitizeCoverage) {
430 case 0:
431 CoverageFeatures = 0;
432 break;
433 case 1:
434 CoverageFeatures = CoverageFunc;
435 break;
436 case 2:
437 CoverageFeatures = CoverageBB;
438 break;
439 case 3:
440 CoverageFeatures = CoverageEdge;
441 break;
442 case 4:
443 CoverageFeatures = CoverageEdge | CoverageIndirCall;
444 break;
445 }
446 continue;
447 }
448 CoverageFeatures |= parseCoverageFeatures(D, Arg);
449 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_coverage)) {
450 Arg->claim();
451 CoverageFeatures &= ~parseCoverageFeatures(D, Arg);
452 }
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000453 }
454 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000455 // Choose at most one coverage type: function, bb, or edge.
456 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageBB))
457 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
458 << "-fsanitize-coverage=func"
459 << "-fsanitize-coverage=bb";
460 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageEdge))
461 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
462 << "-fsanitize-coverage=func"
463 << "-fsanitize-coverage=edge";
464 if ((CoverageFeatures & CoverageBB) && (CoverageFeatures & CoverageEdge))
465 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
466 << "-fsanitize-coverage=bb"
467 << "-fsanitize-coverage=edge";
468 // Basic block tracing and 8-bit counters require some type of coverage
469 // enabled.
470 int CoverageTypes = CoverageFunc | CoverageBB | CoverageEdge;
471 if ((CoverageFeatures & CoverageTraceBB) &&
472 !(CoverageFeatures & CoverageTypes))
473 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
474 << "-fsanitize-coverage=trace-bb"
475 << "-fsanitize-coverage=(func|bb|edge)";
476 if ((CoverageFeatures & Coverage8bitCounters) &&
477 !(CoverageFeatures & CoverageTypes))
478 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
479 << "-fsanitize-coverage=8bit-counters"
480 << "-fsanitize-coverage=(func|bb|edge)";
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000481
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000482 if (AllAddedKinds & Address) {
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000483 AsanSharedRuntime =
Evgeniy Stepanov6f0ae182014-06-05 11:14:00 +0000484 Args.hasArg(options::OPT_shared_libasan) ||
485 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Peter Collingbourne32701642013-11-01 18:16:25 +0000486 AsanZeroBaseShadow =
Evgeniy Stepanovd04b8612014-01-16 10:19:31 +0000487 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000488 if (Arg *A =
489 Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
490 StringRef S = A->getValue();
491 // Legal values are 0 and 1, 2, but in future we may add more levels.
492 if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
493 AsanFieldPadding > 2) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000494 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000495 }
496 }
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000497
498 if (Arg *WindowsDebugRTArg =
499 Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
500 options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
501 options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
502 switch (WindowsDebugRTArg->getOption().getID()) {
503 case options::OPT__SLASH_MTd:
504 case options::OPT__SLASH_MDd:
505 case options::OPT__SLASH_LDd:
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000506 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000507 << WindowsDebugRTArg->getAsString(Args)
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000508 << lastArgumentForMask(D, Args, Address);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000509 D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000510 }
511 }
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000512 }
Alexey Samsonov90490af2014-08-08 22:47:17 +0000513
514 // Parse -link-cxx-sanitizer flag.
515 LinkCXXRuntimes =
516 Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX();
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000517
518 // Finally, initialize the set of available and recoverable sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000519 Sanitizers.Mask |= Kinds;
520 RecoverableSanitizers.Mask |= RecoverableKinds;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000521 TrapSanitizers.Mask |= TrappingKinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000522}
523
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000524static std::string toString(const clang::SanitizerSet &Sanitizers) {
525 std::string Res;
526#define SANITIZER(NAME, ID) \
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000527 if (Sanitizers.has(ID)) { \
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000528 if (!Res.empty()) \
529 Res += ","; \
530 Res += NAME; \
531 }
532#include "clang/Basic/Sanitizers.def"
533 return Res;
534}
535
Peter Collingbourne32701642013-11-01 18:16:25 +0000536void SanitizerArgs::addArgs(const llvm::opt::ArgList &Args,
Alexey Samsonovcf055962013-08-08 10:11:02 +0000537 llvm::opt::ArgStringList &CmdArgs) const {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000538 if (Sanitizers.empty())
Alexey Samsonovcf055962013-08-08 10:11:02 +0000539 return;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000540 CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
541
Alexey Samsonov88459522015-01-12 22:39:12 +0000542 if (!RecoverableSanitizers.empty())
543 CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
544 toString(RecoverableSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000545
Peter Collingbourne9881b782015-06-18 23:59:22 +0000546 if (!TrapSanitizers.empty())
547 CmdArgs.push_back(
548 Args.MakeArgString("-fsanitize-trap=" + toString(TrapSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000549
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000550 for (const auto &BLPath : BlacklistFiles) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000551 SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000552 BlacklistOpt += BLPath;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000553 CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
554 }
555
556 if (MsanTrackOrigins)
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000557 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
558 llvm::utostr(MsanTrackOrigins)));
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000559 if (AsanFieldPadding)
560 CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
561 llvm::utostr(AsanFieldPadding)));
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000562 // Translate available CoverageFeatures to corresponding clang-cc1 flags.
563 std::pair<int, const char *> CoverageFlags[] = {
564 std::make_pair(CoverageFunc, "-fsanitize-coverage-type=1"),
565 std::make_pair(CoverageBB, "-fsanitize-coverage-type=2"),
566 std::make_pair(CoverageEdge, "-fsanitize-coverage-type=3"),
567 std::make_pair(CoverageIndirCall, "-fsanitize-coverage-indirect-calls"),
568 std::make_pair(CoverageTraceBB, "-fsanitize-coverage-trace-bb"),
569 std::make_pair(CoverageTraceCmp, "-fsanitize-coverage-trace-cmp"),
570 std::make_pair(Coverage8bitCounters, "-fsanitize-coverage-8bit-counters")};
571 for (auto F : CoverageFlags) {
572 if (CoverageFeatures & F.first)
573 CmdArgs.push_back(Args.MakeArgString(F.second));
Alexey Samsonov3f3b3ab2015-05-07 18:31:29 +0000574 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000575
576
Sergey Matveev2ba87782015-02-17 15:09:33 +0000577 // MSan: Workaround for PR16386.
578 // ASan: This is mainly to help LSan with cases such as
579 // https://code.google.com/p/address-sanitizer/issues/detail?id=373
580 // We can't make this conditional on -fsanitize=leak, as that flag shouldn't
581 // affect compilation.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000582 if (Sanitizers.has(Memory) || Sanitizers.has(Address))
Alexey Samsonovcf055962013-08-08 10:11:02 +0000583 CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
584}
585
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000586SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
587 bool DiagnoseErrors) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000588 assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
Alexey Samsonov88459522015-01-12 22:39:12 +0000589 A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
590 A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
Peter Collingbourne9881b782015-06-18 23:59:22 +0000591 A->getOption().matches(options::OPT_fno_sanitize_recover_EQ) ||
592 A->getOption().matches(options::OPT_fsanitize_trap_EQ) ||
593 A->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) &&
Alexey Samsonov799f7932014-12-19 02:35:16 +0000594 "Invalid argument in parseArgValues!");
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000595 SanitizerMask Kinds = 0;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000596 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
597 const char *Value = A->getValue(i);
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000598 SanitizerMask Kind;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000599 // Special case: don't accept -fsanitize=all.
600 if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
601 0 == strcmp("all", Value))
602 Kind = 0;
603 else
Peter Collingbournebf59c342015-05-11 21:39:20 +0000604 Kind = parseSanitizerValue(Value, /*AllowGroups=*/true);
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000605
606 if (Kind)
607 Kinds |= Kind;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000608 else if (DiagnoseErrors)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000609 D.Diag(clang::diag::err_drv_unsupported_option_argument)
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000610 << A->getOption().getName() << Value;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000611 }
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000612 return Kinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000613}
614
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000615int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A) {
616 assert(A->getOption().matches(options::OPT_fsanitize_coverage) ||
617 A->getOption().matches(options::OPT_fno_sanitize_coverage));
618 int Features = 0;
619 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
620 const char *Value = A->getValue(i);
621 int F = llvm::StringSwitch<int>(Value)
622 .Case("func", CoverageFunc)
623 .Case("bb", CoverageBB)
624 .Case("edge", CoverageEdge)
625 .Case("indirect-calls", CoverageIndirCall)
626 .Case("trace-bb", CoverageTraceBB)
627 .Case("trace-cmp", CoverageTraceCmp)
628 .Case("8bit-counters", Coverage8bitCounters)
629 .Default(0);
630 if (F == 0)
631 D.Diag(clang::diag::err_drv_unsupported_option_argument)
632 << A->getOption().getName() << Value;
633 Features |= F;
634 }
635 return Features;
636}
637
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000638std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000639 SanitizerMask Mask) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000640 for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
641 E = Args.rend();
642 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000643 const auto *Arg = *I;
644 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000645 SanitizerMask AddKinds =
646 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000647 if (AddKinds & Mask)
648 return describeSanitizeArg(Arg, Mask);
649 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000650 SanitizerMask RemoveKinds =
651 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000652 Mask &= ~RemoveKinds;
653 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000654 }
655 llvm_unreachable("arg list didn't provide expected value");
656}
657
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000658std::string describeSanitizeArg(const llvm::opt::Arg *A, SanitizerMask Mask) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000659 assert(A->getOption().matches(options::OPT_fsanitize_EQ)
660 && "Invalid argument in describeSanitizerArg!");
Alexey Samsonovcf055962013-08-08 10:11:02 +0000661
Peter Collingbourne32701642013-11-01 18:16:25 +0000662 std::string Sanitizers;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000663 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000664 if (expandSanitizerGroups(
665 parseSanitizerValue(A->getValue(i), /*AllowGroups=*/true)) &
666 Mask) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000667 if (!Sanitizers.empty())
668 Sanitizers += ",";
Alexey Samsonov83791e22015-03-03 22:15:32 +0000669 Sanitizers += A->getValue(i);
Peter Collingbourne32701642013-11-01 18:16:25 +0000670 }
671 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000672
Peter Collingbourne32701642013-11-01 18:16:25 +0000673 assert(!Sanitizers.empty() && "arg didn't provide expected value");
674 return "-fsanitize=" + Sanitizers;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000675}