blob: 14c37025eb390744090fad843c547e7a8161b3b2 [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
Peter Collingbourne3eea6772015-05-11 21:39:14 +000080static bool getDefaultBlacklist(const Driver &D, SanitizerMask Kinds,
Alexey Samsonovecf380e2015-03-20 18:45:06 +000081 std::string &BLPath) {
82 const char *BlacklistFile = nullptr;
Peter Collingbourne3eea6772015-05-11 21:39:14 +000083 if (Kinds & Address)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000084 BlacklistFile = "asan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000085 else if (Kinds & Memory)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000086 BlacklistFile = "msan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000087 else if (Kinds & Thread)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000088 BlacklistFile = "tsan_blacklist.txt";
Peter Collingbourne3eea6772015-05-11 21:39:14 +000089 else if (Kinds & DataFlow)
Alexey Samsonovecf380e2015-03-20 18:45:06 +000090 BlacklistFile = "dfsan_abilist.txt";
91
92 if (BlacklistFile) {
93 clang::SmallString<64> Path(D.ResourceDir);
94 llvm::sys::path::append(Path, BlacklistFile);
95 BLPath = Path.str();
96 return true;
97 }
98 return false;
99}
100
Peter Collingbourne9881b782015-06-18 23:59:22 +0000101/// Sets group bits for every group that has at least one representative already
102/// enabled in \p Kinds.
103static SanitizerMask setGroupBits(SanitizerMask Kinds) {
104#define SANITIZER(NAME, ID)
105#define SANITIZER_GROUP(NAME, ID, ALIAS) \
106 if (Kinds & SanitizerKind::ID) \
107 Kinds |= SanitizerKind::ID##Group;
108#include "clang/Basic/Sanitizers.def"
109 return Kinds;
110}
111
112static SanitizerMask parseSanitizeTrapArgs(const Driver &D,
113 const llvm::opt::ArgList &Args) {
114 SanitizerMask TrapRemove = 0; // During the loop below, the accumulated set of
115 // sanitizers disabled by the current sanitizer
116 // argument or any argument after it.
117 SanitizerMask TrappingKinds = 0;
118 SanitizerMask TrappingSupportedWithGroups = setGroupBits(TrappingSupported);
119
120 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
121 I != E; ++I) {
122 const auto *Arg = *I;
123 if (Arg->getOption().matches(options::OPT_fsanitize_trap_EQ)) {
124 Arg->claim();
125 SanitizerMask Add = parseArgValues(D, Arg, true);
126 Add &= ~TrapRemove;
127 if (SanitizerMask InvalidValues = Add & ~TrappingSupportedWithGroups) {
128 SanitizerSet S;
129 S.Mask = InvalidValues;
130 D.Diag(diag::err_drv_unsupported_option_argument) << "-fsanitize-trap"
131 << toString(S);
132 }
133 TrappingKinds |= expandSanitizerGroups(Add) & ~TrapRemove;
134 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) {
135 Arg->claim();
136 TrapRemove |= expandSanitizerGroups(parseArgValues(D, Arg, true));
137 } else if (Arg->getOption().matches(
138 options::OPT_fsanitize_undefined_trap_on_error)) {
139 Arg->claim();
140 TrappingKinds |=
141 expandSanitizerGroups(UndefinedGroup & ~TrapRemove) & ~TrapRemove;
142 } else if (Arg->getOption().matches(
143 options::OPT_fno_sanitize_undefined_trap_on_error)) {
144 Arg->claim();
145 TrapRemove |= expandSanitizerGroups(UndefinedGroup);
146 }
147 }
148
Peter Collingbourne6708c4a2015-06-19 01:51:54 +0000149 // Apply default trapping behavior.
150 TrappingKinds |= TrappingDefault & ~TrapRemove;
151
Peter Collingbourne9881b782015-06-18 23:59:22 +0000152 return TrappingKinds;
153}
154
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000155bool SanitizerArgs::needsUbsanRt() const {
Peter Collingbourne9881b782015-06-18 23:59:22 +0000156 return (Sanitizers.Mask & NeedsUbsanRt & ~TrapSanitizers.Mask) &&
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000157 !Sanitizers.has(Address) &&
158 !Sanitizers.has(Memory) &&
159 !Sanitizers.has(Thread);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000160}
161
Dmitry Vyukov43419a72014-11-21 12:19:01 +0000162bool SanitizerArgs::requiresPIE() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000163 return AsanZeroBaseShadow || (Sanitizers.Mask & RequiresPIE);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000164}
165
166bool SanitizerArgs::needsUnwindTables() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000167 return Sanitizers.Mask & NeedsUnwindTables;
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000168}
169
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000170void SanitizerArgs::clear() {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000171 Sanitizers.clear();
Alexey Samsonov88459522015-01-12 22:39:12 +0000172 RecoverableSanitizers.clear();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000173 TrapSanitizers.clear();
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000174 BlacklistFiles.clear();
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000175 CoverageFeatures = 0;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000176 MsanTrackOrigins = 0;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000177 AsanFieldPadding = 0;
Peter Collingbourne32701642013-11-01 18:16:25 +0000178 AsanZeroBaseShadow = false;
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000179 AsanSharedRuntime = false;
Alexey Samsonov90490af2014-08-08 22:47:17 +0000180 LinkCXXRuntimes = false;
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000181}
Alexey Samsonovcf055962013-08-08 10:11:02 +0000182
Peter Collingbourne32701642013-11-01 18:16:25 +0000183SanitizerArgs::SanitizerArgs(const ToolChain &TC,
184 const llvm::opt::ArgList &Args) {
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000185 clear();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000186 SanitizerMask AllRemove = 0; // During the loop below, the accumulated set of
187 // sanitizers disabled by the current sanitizer
188 // argument or any argument after it.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000189 SanitizerMask AllAddedKinds = 0; // Mask of all sanitizers ever enabled by
190 // -fsanitize= flags (directly or via group
191 // expansion), some of which may be disabled
192 // later. Used to carefully prune
193 // unused-argument diagnostics.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000194 SanitizerMask DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
195 // Used to deduplicate diagnostics.
196 SanitizerMask Kinds = 0;
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000197 SanitizerMask Supported = setGroupBits(TC.getSupportedSanitizers());
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000198 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
199
Peter Collingbourne32701642013-11-01 18:16:25 +0000200 const Driver &D = TC.getDriver();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000201 SanitizerMask TrappingKinds = parseSanitizeTrapArgs(D, Args);
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000202 SanitizerMask InvalidTrappingKinds = TrappingKinds & NotAllowedWithTrap;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000203
Peter Collingbourne32701642013-11-01 18:16:25 +0000204 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
205 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000206 const auto *Arg = *I;
207 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
208 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000209 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000210 AllAddedKinds |= expandSanitizerGroups(Add);
Peter Collingbourne32701642013-11-01 18:16:25 +0000211
Alexey Samsonov799f7932014-12-19 02:35:16 +0000212 // Avoid diagnosing any sanitizer which is disabled later.
213 Add &= ~AllRemove;
214 // At this point we have not expanded groups, so any unsupported
215 // sanitizers in Add are those which have been explicitly enabled.
216 // Diagnose them.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000217 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000218 Add & InvalidTrappingKinds & ~DiagnosedKinds) {
Peter Collingbourne9881b782015-06-18 23:59:22 +0000219 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
220 D.Diag(diag::err_drv_argument_not_allowed_with)
221 << Desc << "-fsanitize-trap=undefined";
222 DiagnosedKinds |= KindsToDiagnose;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000223 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000224 Add &= ~InvalidTrappingKinds;
225 if (SanitizerMask KindsToDiagnose = Add & ~Supported & ~DiagnosedKinds) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000226 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
227 D.Diag(diag::err_drv_unsupported_opt_for_target)
228 << Desc << TC.getTriple().str();
229 DiagnosedKinds |= KindsToDiagnose;
230 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000231 Add &= Supported;
Peter Collingbourne32701642013-11-01 18:16:25 +0000232
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000233 // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups
234 // so we don't error out if -fno-rtti and -fsanitize=undefined were
235 // passed.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000236 if (Add & Vptr &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000237 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
238 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
239 if (RTTIMode == ToolChain::RM_DisabledImplicitly)
240 // Warn about not having rtti enabled if the vptr sanitizer is
241 // explicitly enabled
242 D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default);
243 else {
244 const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg();
245 assert(NoRTTIArg &&
246 "RTTI disabled explicitly but we have no argument!");
247 D.Diag(diag::err_drv_argument_not_allowed_with)
248 << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args);
249 }
250
251 // Take out the Vptr sanitizer from the enabled sanitizers
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000252 AllRemove |= Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000253 }
254
Peter Collingbournebf59c342015-05-11 21:39:20 +0000255 Add = expandSanitizerGroups(Add);
Alexey Samsonov799f7932014-12-19 02:35:16 +0000256 // Group expansion may have enabled a sanitizer which is disabled later.
257 Add &= ~AllRemove;
258 // Silently discard any unsupported sanitizers implicitly enabled through
259 // group expansion.
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000260 Add &= ~InvalidTrappingKinds;
261 Add &= Supported;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000262
Alexey Samsonov799f7932014-12-19 02:35:16 +0000263 Kinds |= Add;
264 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
265 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000266 SanitizerMask Remove = parseArgValues(D, Arg, true);
Peter Collingbournebf59c342015-05-11 21:39:20 +0000267 AllRemove |= expandSanitizerGroups(Remove);
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000268 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000269 }
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000270
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000271 // We disable the vptr sanitizer if it was enabled by group expansion but RTTI
272 // is disabled.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000273 if ((Kinds & Vptr) &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000274 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
275 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000276 Kinds &= ~Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000277 }
278
Alexey Samsonov907880e2015-06-19 19:57:46 +0000279 // Check that LTO is enabled if we need it.
280 if ((Kinds & NeedsLTO) && !D.IsUsingLTO(Args)) {
281 D.Diag(diag::err_drv_argument_only_allowed_with)
282 << lastArgumentForMask(D, Args, Kinds & NeedsLTO) << "-flto";
283 }
284
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000285 // Warn about incompatible groups of sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000286 std::pair<SanitizerMask, SanitizerMask> IncompatibleGroups[] = {
287 std::make_pair(Address, Thread), std::make_pair(Address, Memory),
288 std::make_pair(Thread, Memory), std::make_pair(Leak, Thread),
Alexander Potapenkob9b73ef2015-06-19 12:19:07 +0000289 std::make_pair(Leak, Memory), std::make_pair(KernelAddress, Address),
290 std::make_pair(KernelAddress, Leak),
291 std::make_pair(KernelAddress, Thread),
292 std::make_pair(KernelAddress, Memory)};
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000293 for (auto G : IncompatibleGroups) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000294 SanitizerMask Group = G.first;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000295 if (Kinds & Group) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000296 if (SanitizerMask Incompatible = Kinds & G.second) {
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000297 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
298 << lastArgumentForMask(D, Args, Group)
299 << lastArgumentForMask(D, Args, Incompatible);
300 Kinds &= ~Incompatible;
301 }
302 }
303 }
304 // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
305 // -fsanitize=address. Perhaps it should print an error, or perhaps
306 // -f(-no)sanitize=leak should change whether leak detection is enabled by
307 // default in ASan?
308
Alexey Samsonov88459522015-01-12 22:39:12 +0000309 // Parse -f(no-)?sanitize-recover flags.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000310 SanitizerMask RecoverableKinds = RecoverableByDefault;
311 SanitizerMask DiagnosedUnrecoverableKinds = 0;
Alexey Samsonov88459522015-01-12 22:39:12 +0000312 for (const auto *Arg : Args) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000313 const char *DeprecatedReplacement = nullptr;
Alexey Samsonov88459522015-01-12 22:39:12 +0000314 if (Arg->getOption().matches(options::OPT_fsanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000315 DeprecatedReplacement = "-fsanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000316 RecoverableKinds |= expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000317 Arg->claim();
318 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000319 DeprecatedReplacement = "-fno-sanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000320 RecoverableKinds &= ~expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000321 Arg->claim();
322 } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000323 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonov88459522015-01-12 22:39:12 +0000324 // Report error if user explicitly tries to recover from unrecoverable
325 // sanitizer.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000326 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov88459522015-01-12 22:39:12 +0000327 Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
328 SanitizerSet SetToDiagnose;
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000329 SetToDiagnose.Mask |= KindsToDiagnose;
Alexey Samsonov88459522015-01-12 22:39:12 +0000330 D.Diag(diag::err_drv_unsupported_option_argument)
331 << Arg->getOption().getName() << toString(SetToDiagnose);
332 DiagnosedUnrecoverableKinds |= KindsToDiagnose;
333 }
Peter Collingbournebf59c342015-05-11 21:39:20 +0000334 RecoverableKinds |= expandSanitizerGroups(Add);
Alexey Samsonov88459522015-01-12 22:39:12 +0000335 Arg->claim();
336 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000337 RecoverableKinds &= ~expandSanitizerGroups(parseArgValues(D, Arg, true));
Alexey Samsonov88459522015-01-12 22:39:12 +0000338 Arg->claim();
339 }
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000340 if (DeprecatedReplacement) {
341 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
342 << DeprecatedReplacement;
343 }
Alexey Samsonov88459522015-01-12 22:39:12 +0000344 }
345 RecoverableKinds &= Kinds;
346 RecoverableKinds &= ~Unrecoverable;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000347
Peter Collingbourne9881b782015-06-18 23:59:22 +0000348 TrappingKinds &= Kinds;
349
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000350 // Setup blacklist files.
351 // Add default blacklist from resource directory.
352 {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000353 std::string BLPath;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000354 if (getDefaultBlacklist(D, Kinds, BLPath) && llvm::sys::fs::exists(BLPath))
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000355 BlacklistFiles.push_back(BLPath);
356 }
357 // Parse -f(no-)sanitize-blacklist options.
358 for (const auto *Arg : Args) {
359 if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) {
360 Arg->claim();
361 std::string BLPath = Arg->getValue();
362 if (llvm::sys::fs::exists(BLPath))
363 BlacklistFiles.push_back(BLPath);
364 else
365 D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
366 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) {
367 Arg->claim();
368 BlacklistFiles.clear();
369 }
370 }
371 // Validate blacklists format.
372 {
373 std::string BLError;
374 std::unique_ptr<llvm::SpecialCaseList> SCL(
375 llvm::SpecialCaseList::create(BlacklistFiles, BLError));
376 if (!SCL.get())
377 D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000378 }
379
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000380 // Parse -f[no-]sanitize-memory-track-origins[=level] options.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000381 if (AllAddedKinds & Memory) {
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000382 if (Arg *A =
383 Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
384 options::OPT_fsanitize_memory_track_origins,
385 options::OPT_fno_sanitize_memory_track_origins)) {
386 if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
Evgeniy Stepanov6e09bca2015-02-26 15:59:30 +0000387 MsanTrackOrigins = 2;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000388 } else if (A->getOption().matches(
389 options::OPT_fno_sanitize_memory_track_origins)) {
390 MsanTrackOrigins = 0;
391 } else {
392 StringRef S = A->getValue();
393 if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
394 MsanTrackOrigins > 2) {
Nico Weberf2a39a72015-04-13 20:03:03 +0000395 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000396 }
397 }
398 }
399 }
400
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000401 // Parse -f(no-)?sanitize-coverage flags if coverage is supported by the
402 // enabled sanitizers.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000403 if (AllAddedKinds & SupportsCoverage) {
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000404 for (const auto *Arg : Args) {
405 if (Arg->getOption().matches(options::OPT_fsanitize_coverage)) {
406 Arg->claim();
407 int LegacySanitizeCoverage;
408 if (Arg->getNumValues() == 1 &&
409 !StringRef(Arg->getValue(0))
410 .getAsInteger(0, LegacySanitizeCoverage) &&
411 LegacySanitizeCoverage >= 0 && LegacySanitizeCoverage <= 4) {
412 // TODO: Add deprecation notice for this form.
413 switch (LegacySanitizeCoverage) {
414 case 0:
415 CoverageFeatures = 0;
416 break;
417 case 1:
418 CoverageFeatures = CoverageFunc;
419 break;
420 case 2:
421 CoverageFeatures = CoverageBB;
422 break;
423 case 3:
424 CoverageFeatures = CoverageEdge;
425 break;
426 case 4:
427 CoverageFeatures = CoverageEdge | CoverageIndirCall;
428 break;
429 }
430 continue;
431 }
432 CoverageFeatures |= parseCoverageFeatures(D, Arg);
433 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_coverage)) {
434 Arg->claim();
435 CoverageFeatures &= ~parseCoverageFeatures(D, Arg);
436 }
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000437 }
438 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000439 // Choose at most one coverage type: function, bb, or edge.
440 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageBB))
441 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
442 << "-fsanitize-coverage=func"
443 << "-fsanitize-coverage=bb";
444 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageEdge))
445 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
446 << "-fsanitize-coverage=func"
447 << "-fsanitize-coverage=edge";
448 if ((CoverageFeatures & CoverageBB) && (CoverageFeatures & CoverageEdge))
449 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
450 << "-fsanitize-coverage=bb"
451 << "-fsanitize-coverage=edge";
452 // Basic block tracing and 8-bit counters require some type of coverage
453 // enabled.
454 int CoverageTypes = CoverageFunc | CoverageBB | CoverageEdge;
455 if ((CoverageFeatures & CoverageTraceBB) &&
456 !(CoverageFeatures & CoverageTypes))
457 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
458 << "-fsanitize-coverage=trace-bb"
459 << "-fsanitize-coverage=(func|bb|edge)";
460 if ((CoverageFeatures & Coverage8bitCounters) &&
461 !(CoverageFeatures & CoverageTypes))
462 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
463 << "-fsanitize-coverage=8bit-counters"
464 << "-fsanitize-coverage=(func|bb|edge)";
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000465
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000466 if (AllAddedKinds & Address) {
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000467 AsanSharedRuntime =
Evgeniy Stepanov6f0ae182014-06-05 11:14:00 +0000468 Args.hasArg(options::OPT_shared_libasan) ||
469 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Peter Collingbourne32701642013-11-01 18:16:25 +0000470 AsanZeroBaseShadow =
Evgeniy Stepanovd04b8612014-01-16 10:19:31 +0000471 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000472 if (Arg *A =
473 Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
474 StringRef S = A->getValue();
475 // Legal values are 0 and 1, 2, but in future we may add more levels.
476 if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
477 AsanFieldPadding > 2) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000478 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000479 }
480 }
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000481
482 if (Arg *WindowsDebugRTArg =
483 Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
484 options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
485 options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
486 switch (WindowsDebugRTArg->getOption().getID()) {
487 case options::OPT__SLASH_MTd:
488 case options::OPT__SLASH_MDd:
489 case options::OPT__SLASH_LDd:
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000490 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000491 << WindowsDebugRTArg->getAsString(Args)
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000492 << lastArgumentForMask(D, Args, Address);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000493 D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000494 }
495 }
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000496 }
Alexey Samsonov90490af2014-08-08 22:47:17 +0000497
498 // Parse -link-cxx-sanitizer flag.
499 LinkCXXRuntimes =
500 Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX();
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000501
502 // Finally, initialize the set of available and recoverable sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000503 Sanitizers.Mask |= Kinds;
504 RecoverableSanitizers.Mask |= RecoverableKinds;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000505 TrapSanitizers.Mask |= TrappingKinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000506}
507
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000508static std::string toString(const clang::SanitizerSet &Sanitizers) {
509 std::string Res;
510#define SANITIZER(NAME, ID) \
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000511 if (Sanitizers.has(ID)) { \
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000512 if (!Res.empty()) \
513 Res += ","; \
514 Res += NAME; \
515 }
516#include "clang/Basic/Sanitizers.def"
517 return Res;
518}
519
Peter Collingbourne32701642013-11-01 18:16:25 +0000520void SanitizerArgs::addArgs(const llvm::opt::ArgList &Args,
Alexey Samsonovcf055962013-08-08 10:11:02 +0000521 llvm::opt::ArgStringList &CmdArgs) const {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000522 if (Sanitizers.empty())
Alexey Samsonovcf055962013-08-08 10:11:02 +0000523 return;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000524 CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
525
Alexey Samsonov88459522015-01-12 22:39:12 +0000526 if (!RecoverableSanitizers.empty())
527 CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
528 toString(RecoverableSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000529
Peter Collingbourne9881b782015-06-18 23:59:22 +0000530 if (!TrapSanitizers.empty())
531 CmdArgs.push_back(
532 Args.MakeArgString("-fsanitize-trap=" + toString(TrapSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000533
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000534 for (const auto &BLPath : BlacklistFiles) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000535 SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000536 BlacklistOpt += BLPath;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000537 CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
538 }
539
540 if (MsanTrackOrigins)
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000541 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
542 llvm::utostr(MsanTrackOrigins)));
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000543 if (AsanFieldPadding)
544 CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
545 llvm::utostr(AsanFieldPadding)));
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000546 // Translate available CoverageFeatures to corresponding clang-cc1 flags.
547 std::pair<int, const char *> CoverageFlags[] = {
548 std::make_pair(CoverageFunc, "-fsanitize-coverage-type=1"),
549 std::make_pair(CoverageBB, "-fsanitize-coverage-type=2"),
550 std::make_pair(CoverageEdge, "-fsanitize-coverage-type=3"),
551 std::make_pair(CoverageIndirCall, "-fsanitize-coverage-indirect-calls"),
552 std::make_pair(CoverageTraceBB, "-fsanitize-coverage-trace-bb"),
553 std::make_pair(CoverageTraceCmp, "-fsanitize-coverage-trace-cmp"),
554 std::make_pair(Coverage8bitCounters, "-fsanitize-coverage-8bit-counters")};
555 for (auto F : CoverageFlags) {
556 if (CoverageFeatures & F.first)
557 CmdArgs.push_back(Args.MakeArgString(F.second));
Alexey Samsonov3f3b3ab2015-05-07 18:31:29 +0000558 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000559
560
Sergey Matveev2ba87782015-02-17 15:09:33 +0000561 // MSan: Workaround for PR16386.
562 // ASan: This is mainly to help LSan with cases such as
563 // https://code.google.com/p/address-sanitizer/issues/detail?id=373
564 // We can't make this conditional on -fsanitize=leak, as that flag shouldn't
565 // affect compilation.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000566 if (Sanitizers.has(Memory) || Sanitizers.has(Address))
Alexey Samsonovcf055962013-08-08 10:11:02 +0000567 CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
568}
569
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000570SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
571 bool DiagnoseErrors) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000572 assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
Alexey Samsonov88459522015-01-12 22:39:12 +0000573 A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
574 A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
Peter Collingbourne9881b782015-06-18 23:59:22 +0000575 A->getOption().matches(options::OPT_fno_sanitize_recover_EQ) ||
576 A->getOption().matches(options::OPT_fsanitize_trap_EQ) ||
577 A->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) &&
Alexey Samsonov799f7932014-12-19 02:35:16 +0000578 "Invalid argument in parseArgValues!");
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000579 SanitizerMask Kinds = 0;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000580 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
581 const char *Value = A->getValue(i);
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000582 SanitizerMask Kind;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000583 // Special case: don't accept -fsanitize=all.
584 if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
585 0 == strcmp("all", Value))
586 Kind = 0;
587 else
Peter Collingbournebf59c342015-05-11 21:39:20 +0000588 Kind = parseSanitizerValue(Value, /*AllowGroups=*/true);
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000589
590 if (Kind)
591 Kinds |= Kind;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000592 else if (DiagnoseErrors)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000593 D.Diag(clang::diag::err_drv_unsupported_option_argument)
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000594 << A->getOption().getName() << Value;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000595 }
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000596 return Kinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000597}
598
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000599int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A) {
600 assert(A->getOption().matches(options::OPT_fsanitize_coverage) ||
601 A->getOption().matches(options::OPT_fno_sanitize_coverage));
602 int Features = 0;
603 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
604 const char *Value = A->getValue(i);
605 int F = llvm::StringSwitch<int>(Value)
606 .Case("func", CoverageFunc)
607 .Case("bb", CoverageBB)
608 .Case("edge", CoverageEdge)
609 .Case("indirect-calls", CoverageIndirCall)
610 .Case("trace-bb", CoverageTraceBB)
611 .Case("trace-cmp", CoverageTraceCmp)
612 .Case("8bit-counters", Coverage8bitCounters)
613 .Default(0);
614 if (F == 0)
615 D.Diag(clang::diag::err_drv_unsupported_option_argument)
616 << A->getOption().getName() << Value;
617 Features |= F;
618 }
619 return Features;
620}
621
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000622std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000623 SanitizerMask Mask) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000624 for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
625 E = Args.rend();
626 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000627 const auto *Arg = *I;
628 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000629 SanitizerMask AddKinds =
630 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000631 if (AddKinds & Mask)
632 return describeSanitizeArg(Arg, Mask);
633 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000634 SanitizerMask RemoveKinds =
635 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000636 Mask &= ~RemoveKinds;
637 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000638 }
639 llvm_unreachable("arg list didn't provide expected value");
640}
641
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000642std::string describeSanitizeArg(const llvm::opt::Arg *A, SanitizerMask Mask) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000643 assert(A->getOption().matches(options::OPT_fsanitize_EQ)
644 && "Invalid argument in describeSanitizerArg!");
Alexey Samsonovcf055962013-08-08 10:11:02 +0000645
Peter Collingbourne32701642013-11-01 18:16:25 +0000646 std::string Sanitizers;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000647 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000648 if (expandSanitizerGroups(
649 parseSanitizerValue(A->getValue(i), /*AllowGroups=*/true)) &
650 Mask) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000651 if (!Sanitizers.empty())
652 Sanitizers += ",";
Alexey Samsonov83791e22015-03-03 22:15:32 +0000653 Sanitizers += A->getValue(i);
Peter Collingbourne32701642013-11-01 18:16:25 +0000654 }
655 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000656
Peter Collingbourne32701642013-11-01 18:16:25 +0000657 assert(!Sanitizers.empty() && "arg didn't provide expected value");
658 return "-fsanitize=" + Sanitizers;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000659}