blob: e203aac582aa957ad199817fdb8bc11660f92964 [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 Samsonov9b873092015-06-25 23:14:32 +000029 NeedsUbsanCxxRt = Vptr | CFI,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000030 NotAllowedWithTrap = Vptr,
Dmitry Vyukov43419a72014-11-21 12:19:01 +000031 RequiresPIE = Memory | DataFlow,
Kostya Serebryany2d88f3d2015-01-06 01:02:48 +000032 NeedsUnwindTables = Address | Thread | Memory | DataFlow,
Kostya Serebryany6fe5fcb2015-03-20 00:06:52 +000033 SupportsCoverage = Address | Memory | Leak | Undefined | Integer | DataFlow,
Alexey Samsonov88459522015-01-12 22:39:12 +000034 RecoverableByDefault = Undefined | Integer,
35 Unrecoverable = Address | Unreachable | Return,
Peter Collingbournea4ccff32015-02-20 20:30:56 +000036 LegacyFsanitizeRecoverMask = Undefined | Integer,
Peter Collingbourne1a7488a2015-04-02 00:23:30 +000037 NeedsLTO = CFI,
Peter Collingbourne9881b782015-06-18 23:59:22 +000038 TrappingSupported =
Peter Collingbourne6708c4a2015-06-19 01:51:54 +000039 (Undefined & ~Vptr) | UnsignedIntegerOverflow | LocalBounds | CFI,
40 TrappingDefault = CFI,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000041};
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000042
43enum CoverageFeature {
44 CoverageFunc = 1 << 0,
45 CoverageBB = 1 << 1,
46 CoverageEdge = 1 << 2,
47 CoverageIndirCall = 1 << 3,
48 CoverageTraceBB = 1 << 4,
49 CoverageTraceCmp = 1 << 5,
50 Coverage8bitCounters = 1 << 6,
51};
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000052
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000053/// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any
Peter Collingbourne3eea6772015-05-11 21:39:14 +000054/// invalid components. Returns a SanitizerMask.
55static SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
56 bool DiagnoseErrors);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000057
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000058/// Parse -f(no-)?sanitize-coverage= flag values, diagnosing any invalid
59/// components. Returns OR of members of \c CoverageFeature enumeration.
60static int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A);
61
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000062/// Produce an argument string from ArgList \p Args, which shows how it
63/// provides some sanitizer kind from \p Mask. For example, the argument list
64/// "-fsanitize=thread,vptr -fsanitize=address" with mask \c NeedsUbsanRt
65/// would produce "-fsanitize=vptr".
66static std::string lastArgumentForMask(const Driver &D,
67 const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +000068 SanitizerMask Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000069
70/// Produce an argument string from argument \p A, which shows how it provides
71/// a value in \p Mask. For instance, the argument
72/// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce
73/// "-fsanitize=alignment".
Peter Collingbourne3eea6772015-05-11 21:39:14 +000074static std::string describeSanitizeArg(const llvm::opt::Arg *A,
75 SanitizerMask Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000076
Alexey Samsonov1e715a62014-11-16 20:53:53 +000077/// Produce a string containing comma-separated names of sanitizers in \p
78/// Sanitizers set.
79static std::string toString(const clang::SanitizerSet &Sanitizers);
80
Peter Collingbourne3eea6772015-05-11 21:39:14 +000081static bool getDefaultBlacklist(const Driver &D, SanitizerMask Kinds,
Alexey Samsonovecf380e2015-03-20 18:45:06 +000082 std::string &BLPath) {
83 const char *BlacklistFile = nullptr;
Peter Collingbourne3eea6772015-05-11 21:39:14 +000084 if (Kinds & Address)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000085 BlacklistFile = "asan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000086 else if (Kinds & Memory)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000087 BlacklistFile = "msan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000088 else if (Kinds & Thread)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000089 BlacklistFile = "tsan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000090 else if (Kinds & DataFlow)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000091 BlacklistFile = "dfsan_abilist.txt";
92
93 if (BlacklistFile) {
94 clang::SmallString<64> Path(D.ResourceDir);
95 llvm::sys::path::append(Path, BlacklistFile);
96 BLPath = Path.str();
97 return true;
98 }
99 return false;
100}
101
Peter Collingbourne9881b782015-06-18 23:59:22 +0000102/// Sets group bits for every group that has at least one representative already
103/// enabled in \p Kinds.
104static SanitizerMask setGroupBits(SanitizerMask Kinds) {
105#define SANITIZER(NAME, ID)
106#define SANITIZER_GROUP(NAME, ID, ALIAS) \
107 if (Kinds & SanitizerKind::ID) \
108 Kinds |= SanitizerKind::ID##Group;
109#include "clang/Basic/Sanitizers.def"
110 return Kinds;
111}
112
113static SanitizerMask parseSanitizeTrapArgs(const Driver &D,
114 const llvm::opt::ArgList &Args) {
115 SanitizerMask TrapRemove = 0; // During the loop below, the accumulated set of
116 // sanitizers disabled by the current sanitizer
117 // argument or any argument after it.
118 SanitizerMask TrappingKinds = 0;
119 SanitizerMask TrappingSupportedWithGroups = setGroupBits(TrappingSupported);
120
121 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
122 I != E; ++I) {
123 const auto *Arg = *I;
124 if (Arg->getOption().matches(options::OPT_fsanitize_trap_EQ)) {
125 Arg->claim();
126 SanitizerMask Add = parseArgValues(D, Arg, true);
127 Add &= ~TrapRemove;
128 if (SanitizerMask InvalidValues = Add & ~TrappingSupportedWithGroups) {
129 SanitizerSet S;
130 S.Mask = InvalidValues;
131 D.Diag(diag::err_drv_unsupported_option_argument) << "-fsanitize-trap"
132 << toString(S);
133 }
134 TrappingKinds |= expandSanitizerGroups(Add) & ~TrapRemove;
135 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) {
136 Arg->claim();
137 TrapRemove |= expandSanitizerGroups(parseArgValues(D, Arg, true));
138 } else if (Arg->getOption().matches(
139 options::OPT_fsanitize_undefined_trap_on_error)) {
140 Arg->claim();
141 TrappingKinds |=
142 expandSanitizerGroups(UndefinedGroup & ~TrapRemove) & ~TrapRemove;
143 } else if (Arg->getOption().matches(
144 options::OPT_fno_sanitize_undefined_trap_on_error)) {
145 Arg->claim();
146 TrapRemove |= expandSanitizerGroups(UndefinedGroup);
147 }
148 }
149
Peter Collingbourne6708c4a2015-06-19 01:51:54 +0000150 // Apply default trapping behavior.
151 TrappingKinds |= TrappingDefault & ~TrapRemove;
152
Peter Collingbourne9881b782015-06-18 23:59:22 +0000153 return TrappingKinds;
154}
155
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000156bool SanitizerArgs::needsUbsanRt() const {
Peter Collingbourne9881b782015-06-18 23:59:22 +0000157 return (Sanitizers.Mask & NeedsUbsanRt & ~TrapSanitizers.Mask) &&
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000158 !Sanitizers.has(Address) &&
159 !Sanitizers.has(Memory) &&
160 !Sanitizers.has(Thread);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000161}
162
Dmitry Vyukov43419a72014-11-21 12:19:01 +0000163bool SanitizerArgs::requiresPIE() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000164 return AsanZeroBaseShadow || (Sanitizers.Mask & RequiresPIE);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000165}
166
167bool SanitizerArgs::needsUnwindTables() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000168 return Sanitizers.Mask & NeedsUnwindTables;
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000169}
170
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000171void SanitizerArgs::clear() {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000172 Sanitizers.clear();
Alexey Samsonov88459522015-01-12 22:39:12 +0000173 RecoverableSanitizers.clear();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000174 TrapSanitizers.clear();
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000175 BlacklistFiles.clear();
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000176 CoverageFeatures = 0;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000177 MsanTrackOrigins = 0;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000178 AsanFieldPadding = 0;
Peter Collingbourne32701642013-11-01 18:16:25 +0000179 AsanZeroBaseShadow = false;
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000180 AsanSharedRuntime = false;
Alexey Samsonov90490af2014-08-08 22:47:17 +0000181 LinkCXXRuntimes = false;
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000182}
Alexey Samsonovcf055962013-08-08 10:11:02 +0000183
Peter Collingbourne32701642013-11-01 18:16:25 +0000184SanitizerArgs::SanitizerArgs(const ToolChain &TC,
185 const llvm::opt::ArgList &Args) {
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000186 clear();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000187 SanitizerMask AllRemove = 0; // During the loop below, the accumulated set of
188 // sanitizers disabled by the current sanitizer
189 // argument or any argument after it.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000190 SanitizerMask AllAddedKinds = 0; // Mask of all sanitizers ever enabled by
191 // -fsanitize= flags (directly or via group
192 // expansion), some of which may be disabled
193 // later. Used to carefully prune
194 // unused-argument diagnostics.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000195 SanitizerMask DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
196 // Used to deduplicate diagnostics.
197 SanitizerMask Kinds = 0;
Alexey Samsonov9b873092015-06-25 23:14:32 +0000198 const SanitizerMask Supported = setGroupBits(TC.getSupportedSanitizers());
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000199 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
200
Peter Collingbourne32701642013-11-01 18:16:25 +0000201 const Driver &D = TC.getDriver();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000202 SanitizerMask TrappingKinds = parseSanitizeTrapArgs(D, Args);
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000203 SanitizerMask InvalidTrappingKinds = TrappingKinds & NotAllowedWithTrap;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000204
Peter Collingbourne32701642013-11-01 18:16:25 +0000205 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
206 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000207 const auto *Arg = *I;
208 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
209 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000210 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000211 AllAddedKinds |= expandSanitizerGroups(Add);
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.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000218 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000219 Add & InvalidTrappingKinds & ~DiagnosedKinds) {
Peter Collingbourne9881b782015-06-18 23:59:22 +0000220 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
221 D.Diag(diag::err_drv_argument_not_allowed_with)
222 << Desc << "-fsanitize-trap=undefined";
223 DiagnosedKinds |= KindsToDiagnose;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000224 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000225 Add &= ~InvalidTrappingKinds;
226 if (SanitizerMask KindsToDiagnose = Add & ~Supported & ~DiagnosedKinds) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000227 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
228 D.Diag(diag::err_drv_unsupported_opt_for_target)
229 << Desc << TC.getTriple().str();
230 DiagnosedKinds |= KindsToDiagnose;
231 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000232 Add &= Supported;
Peter Collingbourne32701642013-11-01 18:16:25 +0000233
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000234 // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups
235 // so we don't error out if -fno-rtti and -fsanitize=undefined were
236 // passed.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000237 if (Add & Vptr &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000238 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
239 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
240 if (RTTIMode == ToolChain::RM_DisabledImplicitly)
241 // Warn about not having rtti enabled if the vptr sanitizer is
242 // explicitly enabled
243 D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default);
244 else {
245 const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg();
246 assert(NoRTTIArg &&
247 "RTTI disabled explicitly but we have no argument!");
248 D.Diag(diag::err_drv_argument_not_allowed_with)
249 << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args);
250 }
251
252 // Take out the Vptr sanitizer from the enabled sanitizers
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000253 AllRemove |= Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000254 }
255
Peter Collingbournebf59c342015-05-11 21:39:20 +0000256 Add = expandSanitizerGroups(Add);
Alexey Samsonov799f7932014-12-19 02:35:16 +0000257 // Group expansion may have enabled a sanitizer which is disabled later.
258 Add &= ~AllRemove;
259 // Silently discard any unsupported sanitizers implicitly enabled through
260 // group expansion.
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000261 Add &= ~InvalidTrappingKinds;
262 Add &= Supported;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000263
Alexey Samsonov799f7932014-12-19 02:35:16 +0000264 Kinds |= Add;
265 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
266 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000267 SanitizerMask Remove = parseArgValues(D, Arg, true);
Peter Collingbournebf59c342015-05-11 21:39:20 +0000268 AllRemove |= expandSanitizerGroups(Remove);
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000269 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000270 }
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000271
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000272 // We disable the vptr sanitizer if it was enabled by group expansion but RTTI
273 // is disabled.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000274 if ((Kinds & Vptr) &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000275 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
276 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000277 Kinds &= ~Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000278 }
279
Alexey Samsonov907880e2015-06-19 19:57:46 +0000280 // Check that LTO is enabled if we need it.
281 if ((Kinds & NeedsLTO) && !D.IsUsingLTO(Args)) {
282 D.Diag(diag::err_drv_argument_only_allowed_with)
283 << lastArgumentForMask(D, Args, Kinds & NeedsLTO) << "-flto";
284 }
285
Alexey Samsonov9b873092015-06-25 23:14:32 +0000286 // Report error if there are non-trapping sanitizers that require
287 // c++abi-specific parts of UBSan runtime, and they are not provided by the
288 // toolchain. We don't have a good way to check the latter, so we just
289 // check if the toolchan supports vptr.
290 if (~Supported & Vptr) {
291 if (SanitizerMask KindsToDiagnose =
292 Kinds & ~TrappingKinds & NeedsUbsanCxxRt) {
293 SanitizerSet S;
294 S.Mask = KindsToDiagnose;
295 D.Diag(diag::err_drv_unsupported_opt_for_target)
296 << ("-fno-sanitize-trap=" + toString(S)) << TC.getTriple().str();
297 Kinds &= ~KindsToDiagnose;
298 }
299 }
300
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000301 // Warn about incompatible groups of sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000302 std::pair<SanitizerMask, SanitizerMask> IncompatibleGroups[] = {
303 std::make_pair(Address, Thread), std::make_pair(Address, Memory),
304 std::make_pair(Thread, Memory), std::make_pair(Leak, Thread),
Alexander Potapenkob9b73ef2015-06-19 12:19:07 +0000305 std::make_pair(Leak, Memory), std::make_pair(KernelAddress, Address),
306 std::make_pair(KernelAddress, Leak),
307 std::make_pair(KernelAddress, Thread),
308 std::make_pair(KernelAddress, 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}