blob: 35041b56971abafe7332c70410d7708c68bb702f [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
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000191void SanitizerArgs::clear() {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000192 Sanitizers.clear();
Alexey Samsonov88459522015-01-12 22:39:12 +0000193 RecoverableSanitizers.clear();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000194 TrapSanitizers.clear();
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000195 BlacklistFiles.clear();
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000196 CoverageFeatures = 0;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000197 MsanTrackOrigins = 0;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000198 AsanFieldPadding = 0;
Peter Collingbourne32701642013-11-01 18:16:25 +0000199 AsanZeroBaseShadow = false;
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000200 AsanSharedRuntime = false;
Alexey Samsonov90490af2014-08-08 22:47:17 +0000201 LinkCXXRuntimes = false;
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000202}
Alexey Samsonovcf055962013-08-08 10:11:02 +0000203
Peter Collingbourne32701642013-11-01 18:16:25 +0000204SanitizerArgs::SanitizerArgs(const ToolChain &TC,
205 const llvm::opt::ArgList &Args) {
Alexey Samsonovbb14f342013-08-08 11:32:17 +0000206 clear();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000207 SanitizerMask AllRemove = 0; // During the loop below, the accumulated set of
208 // sanitizers disabled by the current sanitizer
209 // argument or any argument after it.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000210 SanitizerMask AllAddedKinds = 0; // Mask of all sanitizers ever enabled by
211 // -fsanitize= flags (directly or via group
212 // expansion), some of which may be disabled
213 // later. Used to carefully prune
214 // unused-argument diagnostics.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000215 SanitizerMask DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
216 // Used to deduplicate diagnostics.
217 SanitizerMask Kinds = 0;
Alexey Samsonov9bc2ad52015-06-17 22:27:32 +0000218 SanitizerMask NotSupported = getToolchainUnsupportedKinds(TC);
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000219 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
220
Peter Collingbourne32701642013-11-01 18:16:25 +0000221 const Driver &D = TC.getDriver();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000222 SanitizerMask TrappingKinds = parseSanitizeTrapArgs(D, Args);
223 NotSupported |= TrappingKinds & NotAllowedWithTrap;
224
Peter Collingbourne32701642013-11-01 18:16:25 +0000225 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
226 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000227 const auto *Arg = *I;
228 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
229 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000230 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000231 AllAddedKinds |= expandSanitizerGroups(Add);
Peter Collingbourne32701642013-11-01 18:16:25 +0000232
Alexey Samsonov799f7932014-12-19 02:35:16 +0000233 // Avoid diagnosing any sanitizer which is disabled later.
234 Add &= ~AllRemove;
235 // At this point we have not expanded groups, so any unsupported
236 // sanitizers in Add are those which have been explicitly enabled.
237 // Diagnose them.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000238 if (SanitizerMask KindsToDiagnose =
Peter Collingbourne9881b782015-06-18 23:59:22 +0000239 Add & TrappingKinds & NotAllowedWithTrap & ~DiagnosedKinds) {
240 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
241 D.Diag(diag::err_drv_argument_not_allowed_with)
242 << Desc << "-fsanitize-trap=undefined";
243 DiagnosedKinds |= KindsToDiagnose;
244 Add &= ~KindsToDiagnose;
245 }
246 if (SanitizerMask KindsToDiagnose = Add & NotSupported & ~DiagnosedKinds) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000247 // Only diagnose the new kinds.
248 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
249 D.Diag(diag::err_drv_unsupported_opt_for_target)
250 << Desc << TC.getTriple().str();
251 DiagnosedKinds |= KindsToDiagnose;
252 }
Alexey Samsonov9bc2ad52015-06-17 22:27:32 +0000253 Add &= ~NotSupported;
Peter Collingbourne32701642013-11-01 18:16:25 +0000254
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000255 // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups
256 // so we don't error out if -fno-rtti and -fsanitize=undefined were
257 // passed.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000258 if (Add & Vptr &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000259 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
260 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
261 if (RTTIMode == ToolChain::RM_DisabledImplicitly)
262 // Warn about not having rtti enabled if the vptr sanitizer is
263 // explicitly enabled
264 D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default);
265 else {
266 const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg();
267 assert(NoRTTIArg &&
268 "RTTI disabled explicitly but we have no argument!");
269 D.Diag(diag::err_drv_argument_not_allowed_with)
270 << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args);
271 }
272
273 // Take out the Vptr sanitizer from the enabled sanitizers
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000274 AllRemove |= Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000275 }
276
Peter Collingbournebf59c342015-05-11 21:39:20 +0000277 Add = expandSanitizerGroups(Add);
Alexey Samsonov799f7932014-12-19 02:35:16 +0000278 // Group expansion may have enabled a sanitizer which is disabled later.
279 Add &= ~AllRemove;
280 // Silently discard any unsupported sanitizers implicitly enabled through
281 // group expansion.
Alexey Samsonov9bc2ad52015-06-17 22:27:32 +0000282 Add &= ~NotSupported;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000283
Alexey Samsonov799f7932014-12-19 02:35:16 +0000284 Kinds |= Add;
285 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
286 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000287 SanitizerMask Remove = parseArgValues(D, Arg, true);
Peter Collingbournebf59c342015-05-11 21:39:20 +0000288 AllRemove |= expandSanitizerGroups(Remove);
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000289 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000290 }
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000291
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000292 // We disable the vptr sanitizer if it was enabled by group expansion but RTTI
293 // is disabled.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000294 if ((Kinds & Vptr) &&
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000295 (RTTIMode == ToolChain::RM_DisabledImplicitly ||
296 RTTIMode == ToolChain::RM_DisabledExplicitly)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000297 Kinds &= ~Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000298 }
299
Alexey Samsonov907880e2015-06-19 19:57:46 +0000300 // Check that LTO is enabled if we need it.
301 if ((Kinds & NeedsLTO) && !D.IsUsingLTO(Args)) {
302 D.Diag(diag::err_drv_argument_only_allowed_with)
303 << lastArgumentForMask(D, Args, Kinds & NeedsLTO) << "-flto";
304 }
305
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000306 // Warn about incompatible groups of sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000307 std::pair<SanitizerMask, SanitizerMask> IncompatibleGroups[] = {
308 std::make_pair(Address, Thread), std::make_pair(Address, Memory),
309 std::make_pair(Thread, Memory), std::make_pair(Leak, Thread),
Alexander Potapenkob9b73ef2015-06-19 12:19:07 +0000310 std::make_pair(Leak, Memory), std::make_pair(KernelAddress, Address),
311 std::make_pair(KernelAddress, Leak),
312 std::make_pair(KernelAddress, Thread),
313 std::make_pair(KernelAddress, Memory)};
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000314 for (auto G : IncompatibleGroups) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000315 SanitizerMask Group = G.first;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000316 if (Kinds & Group) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000317 if (SanitizerMask Incompatible = Kinds & G.second) {
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000318 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
319 << lastArgumentForMask(D, Args, Group)
320 << lastArgumentForMask(D, Args, Incompatible);
321 Kinds &= ~Incompatible;
322 }
323 }
324 }
325 // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
326 // -fsanitize=address. Perhaps it should print an error, or perhaps
327 // -f(-no)sanitize=leak should change whether leak detection is enabled by
328 // default in ASan?
329
Alexey Samsonov88459522015-01-12 22:39:12 +0000330 // Parse -f(no-)?sanitize-recover flags.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000331 SanitizerMask RecoverableKinds = RecoverableByDefault;
332 SanitizerMask DiagnosedUnrecoverableKinds = 0;
Alexey Samsonov88459522015-01-12 22:39:12 +0000333 for (const auto *Arg : Args) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000334 const char *DeprecatedReplacement = nullptr;
Alexey Samsonov88459522015-01-12 22:39:12 +0000335 if (Arg->getOption().matches(options::OPT_fsanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000336 DeprecatedReplacement = "-fsanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000337 RecoverableKinds |= expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000338 Arg->claim();
339 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000340 DeprecatedReplacement = "-fno-sanitize-recover=undefined,integer";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000341 RecoverableKinds &= ~expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000342 Arg->claim();
343 } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000344 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonov88459522015-01-12 22:39:12 +0000345 // Report error if user explicitly tries to recover from unrecoverable
346 // sanitizer.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000347 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov88459522015-01-12 22:39:12 +0000348 Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
349 SanitizerSet SetToDiagnose;
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000350 SetToDiagnose.Mask |= KindsToDiagnose;
Alexey Samsonov88459522015-01-12 22:39:12 +0000351 D.Diag(diag::err_drv_unsupported_option_argument)
352 << Arg->getOption().getName() << toString(SetToDiagnose);
353 DiagnosedUnrecoverableKinds |= KindsToDiagnose;
354 }
Peter Collingbournebf59c342015-05-11 21:39:20 +0000355 RecoverableKinds |= expandSanitizerGroups(Add);
Alexey Samsonov88459522015-01-12 22:39:12 +0000356 Arg->claim();
357 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000358 RecoverableKinds &= ~expandSanitizerGroups(parseArgValues(D, Arg, true));
Alexey Samsonov88459522015-01-12 22:39:12 +0000359 Arg->claim();
360 }
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000361 if (DeprecatedReplacement) {
362 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
363 << DeprecatedReplacement;
364 }
Alexey Samsonov88459522015-01-12 22:39:12 +0000365 }
366 RecoverableKinds &= Kinds;
367 RecoverableKinds &= ~Unrecoverable;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000368
Peter Collingbourne9881b782015-06-18 23:59:22 +0000369 TrappingKinds &= Kinds;
370
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000371 // Setup blacklist files.
372 // Add default blacklist from resource directory.
373 {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000374 std::string BLPath;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000375 if (getDefaultBlacklist(D, Kinds, BLPath) && llvm::sys::fs::exists(BLPath))
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000376 BlacklistFiles.push_back(BLPath);
377 }
378 // Parse -f(no-)sanitize-blacklist options.
379 for (const auto *Arg : Args) {
380 if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) {
381 Arg->claim();
382 std::string BLPath = Arg->getValue();
383 if (llvm::sys::fs::exists(BLPath))
384 BlacklistFiles.push_back(BLPath);
385 else
386 D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
387 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) {
388 Arg->claim();
389 BlacklistFiles.clear();
390 }
391 }
392 // Validate blacklists format.
393 {
394 std::string BLError;
395 std::unique_ptr<llvm::SpecialCaseList> SCL(
396 llvm::SpecialCaseList::create(BlacklistFiles, BLError));
397 if (!SCL.get())
398 D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000399 }
400
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000401 // Parse -f[no-]sanitize-memory-track-origins[=level] options.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000402 if (AllAddedKinds & Memory) {
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000403 if (Arg *A =
404 Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
405 options::OPT_fsanitize_memory_track_origins,
406 options::OPT_fno_sanitize_memory_track_origins)) {
407 if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
Evgeniy Stepanov6e09bca2015-02-26 15:59:30 +0000408 MsanTrackOrigins = 2;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000409 } else if (A->getOption().matches(
410 options::OPT_fno_sanitize_memory_track_origins)) {
411 MsanTrackOrigins = 0;
412 } else {
413 StringRef S = A->getValue();
414 if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
415 MsanTrackOrigins > 2) {
Nico Weberf2a39a72015-04-13 20:03:03 +0000416 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000417 }
418 }
419 }
420 }
421
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000422 // Parse -f(no-)?sanitize-coverage flags if coverage is supported by the
423 // enabled sanitizers.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000424 if (AllAddedKinds & SupportsCoverage) {
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000425 for (const auto *Arg : Args) {
426 if (Arg->getOption().matches(options::OPT_fsanitize_coverage)) {
427 Arg->claim();
428 int LegacySanitizeCoverage;
429 if (Arg->getNumValues() == 1 &&
430 !StringRef(Arg->getValue(0))
431 .getAsInteger(0, LegacySanitizeCoverage) &&
432 LegacySanitizeCoverage >= 0 && LegacySanitizeCoverage <= 4) {
433 // TODO: Add deprecation notice for this form.
434 switch (LegacySanitizeCoverage) {
435 case 0:
436 CoverageFeatures = 0;
437 break;
438 case 1:
439 CoverageFeatures = CoverageFunc;
440 break;
441 case 2:
442 CoverageFeatures = CoverageBB;
443 break;
444 case 3:
445 CoverageFeatures = CoverageEdge;
446 break;
447 case 4:
448 CoverageFeatures = CoverageEdge | CoverageIndirCall;
449 break;
450 }
451 continue;
452 }
453 CoverageFeatures |= parseCoverageFeatures(D, Arg);
454 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_coverage)) {
455 Arg->claim();
456 CoverageFeatures &= ~parseCoverageFeatures(D, Arg);
457 }
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000458 }
459 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000460 // Choose at most one coverage type: function, bb, or edge.
461 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageBB))
462 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
463 << "-fsanitize-coverage=func"
464 << "-fsanitize-coverage=bb";
465 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageEdge))
466 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
467 << "-fsanitize-coverage=func"
468 << "-fsanitize-coverage=edge";
469 if ((CoverageFeatures & CoverageBB) && (CoverageFeatures & CoverageEdge))
470 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
471 << "-fsanitize-coverage=bb"
472 << "-fsanitize-coverage=edge";
473 // Basic block tracing and 8-bit counters require some type of coverage
474 // enabled.
475 int CoverageTypes = CoverageFunc | CoverageBB | CoverageEdge;
476 if ((CoverageFeatures & CoverageTraceBB) &&
477 !(CoverageFeatures & CoverageTypes))
478 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
479 << "-fsanitize-coverage=trace-bb"
480 << "-fsanitize-coverage=(func|bb|edge)";
481 if ((CoverageFeatures & Coverage8bitCounters) &&
482 !(CoverageFeatures & CoverageTypes))
483 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
484 << "-fsanitize-coverage=8bit-counters"
485 << "-fsanitize-coverage=(func|bb|edge)";
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000486
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000487 if (AllAddedKinds & Address) {
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000488 AsanSharedRuntime =
Evgeniy Stepanov6f0ae182014-06-05 11:14:00 +0000489 Args.hasArg(options::OPT_shared_libasan) ||
490 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Peter Collingbourne32701642013-11-01 18:16:25 +0000491 AsanZeroBaseShadow =
Evgeniy Stepanovd04b8612014-01-16 10:19:31 +0000492 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000493 if (Arg *A =
494 Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
495 StringRef S = A->getValue();
496 // Legal values are 0 and 1, 2, but in future we may add more levels.
497 if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
498 AsanFieldPadding > 2) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000499 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000500 }
501 }
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000502
503 if (Arg *WindowsDebugRTArg =
504 Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
505 options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
506 options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
507 switch (WindowsDebugRTArg->getOption().getID()) {
508 case options::OPT__SLASH_MTd:
509 case options::OPT__SLASH_MDd:
510 case options::OPT__SLASH_LDd:
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000511 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000512 << WindowsDebugRTArg->getAsString(Args)
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000513 << lastArgumentForMask(D, Args, Address);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000514 D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000515 }
516 }
Alexey Samsonovbdfa6c22014-04-01 13:31:10 +0000517 }
Alexey Samsonov90490af2014-08-08 22:47:17 +0000518
519 // Parse -link-cxx-sanitizer flag.
520 LinkCXXRuntimes =
521 Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX();
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000522
523 // Finally, initialize the set of available and recoverable sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000524 Sanitizers.Mask |= Kinds;
525 RecoverableSanitizers.Mask |= RecoverableKinds;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000526 TrapSanitizers.Mask |= TrappingKinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000527}
528
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000529static std::string toString(const clang::SanitizerSet &Sanitizers) {
530 std::string Res;
531#define SANITIZER(NAME, ID) \
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000532 if (Sanitizers.has(ID)) { \
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000533 if (!Res.empty()) \
534 Res += ","; \
535 Res += NAME; \
536 }
537#include "clang/Basic/Sanitizers.def"
538 return Res;
539}
540
Peter Collingbourne32701642013-11-01 18:16:25 +0000541void SanitizerArgs::addArgs(const llvm::opt::ArgList &Args,
Alexey Samsonovcf055962013-08-08 10:11:02 +0000542 llvm::opt::ArgStringList &CmdArgs) const {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000543 if (Sanitizers.empty())
Alexey Samsonovcf055962013-08-08 10:11:02 +0000544 return;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000545 CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
546
Alexey Samsonov88459522015-01-12 22:39:12 +0000547 if (!RecoverableSanitizers.empty())
548 CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
549 toString(RecoverableSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000550
Peter Collingbourne9881b782015-06-18 23:59:22 +0000551 if (!TrapSanitizers.empty())
552 CmdArgs.push_back(
553 Args.MakeArgString("-fsanitize-trap=" + toString(TrapSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000554
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000555 for (const auto &BLPath : BlacklistFiles) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000556 SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000557 BlacklistOpt += BLPath;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000558 CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
559 }
560
561 if (MsanTrackOrigins)
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000562 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
563 llvm::utostr(MsanTrackOrigins)));
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000564 if (AsanFieldPadding)
565 CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
566 llvm::utostr(AsanFieldPadding)));
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000567 // Translate available CoverageFeatures to corresponding clang-cc1 flags.
568 std::pair<int, const char *> CoverageFlags[] = {
569 std::make_pair(CoverageFunc, "-fsanitize-coverage-type=1"),
570 std::make_pair(CoverageBB, "-fsanitize-coverage-type=2"),
571 std::make_pair(CoverageEdge, "-fsanitize-coverage-type=3"),
572 std::make_pair(CoverageIndirCall, "-fsanitize-coverage-indirect-calls"),
573 std::make_pair(CoverageTraceBB, "-fsanitize-coverage-trace-bb"),
574 std::make_pair(CoverageTraceCmp, "-fsanitize-coverage-trace-cmp"),
575 std::make_pair(Coverage8bitCounters, "-fsanitize-coverage-8bit-counters")};
576 for (auto F : CoverageFlags) {
577 if (CoverageFeatures & F.first)
578 CmdArgs.push_back(Args.MakeArgString(F.second));
Alexey Samsonov3f3b3ab2015-05-07 18:31:29 +0000579 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000580
581
Sergey Matveev2ba87782015-02-17 15:09:33 +0000582 // MSan: Workaround for PR16386.
583 // ASan: This is mainly to help LSan with cases such as
584 // https://code.google.com/p/address-sanitizer/issues/detail?id=373
585 // We can't make this conditional on -fsanitize=leak, as that flag shouldn't
586 // affect compilation.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000587 if (Sanitizers.has(Memory) || Sanitizers.has(Address))
Alexey Samsonovcf055962013-08-08 10:11:02 +0000588 CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
589}
590
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000591SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
592 bool DiagnoseErrors) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000593 assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
Alexey Samsonov88459522015-01-12 22:39:12 +0000594 A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
595 A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
Peter Collingbourne9881b782015-06-18 23:59:22 +0000596 A->getOption().matches(options::OPT_fno_sanitize_recover_EQ) ||
597 A->getOption().matches(options::OPT_fsanitize_trap_EQ) ||
598 A->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) &&
Alexey Samsonov799f7932014-12-19 02:35:16 +0000599 "Invalid argument in parseArgValues!");
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000600 SanitizerMask Kinds = 0;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000601 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
602 const char *Value = A->getValue(i);
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000603 SanitizerMask Kind;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000604 // Special case: don't accept -fsanitize=all.
605 if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
606 0 == strcmp("all", Value))
607 Kind = 0;
608 else
Peter Collingbournebf59c342015-05-11 21:39:20 +0000609 Kind = parseSanitizerValue(Value, /*AllowGroups=*/true);
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000610
611 if (Kind)
612 Kinds |= Kind;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000613 else if (DiagnoseErrors)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000614 D.Diag(clang::diag::err_drv_unsupported_option_argument)
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000615 << A->getOption().getName() << Value;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000616 }
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000617 return Kinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000618}
619
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000620int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A) {
621 assert(A->getOption().matches(options::OPT_fsanitize_coverage) ||
622 A->getOption().matches(options::OPT_fno_sanitize_coverage));
623 int Features = 0;
624 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
625 const char *Value = A->getValue(i);
626 int F = llvm::StringSwitch<int>(Value)
627 .Case("func", CoverageFunc)
628 .Case("bb", CoverageBB)
629 .Case("edge", CoverageEdge)
630 .Case("indirect-calls", CoverageIndirCall)
631 .Case("trace-bb", CoverageTraceBB)
632 .Case("trace-cmp", CoverageTraceCmp)
633 .Case("8bit-counters", Coverage8bitCounters)
634 .Default(0);
635 if (F == 0)
636 D.Diag(clang::diag::err_drv_unsupported_option_argument)
637 << A->getOption().getName() << Value;
638 Features |= F;
639 }
640 return Features;
641}
642
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000643std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000644 SanitizerMask Mask) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000645 for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
646 E = Args.rend();
647 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000648 const auto *Arg = *I;
649 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000650 SanitizerMask AddKinds =
651 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000652 if (AddKinds & Mask)
653 return describeSanitizeArg(Arg, Mask);
654 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000655 SanitizerMask RemoveKinds =
656 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000657 Mask &= ~RemoveKinds;
658 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000659 }
660 llvm_unreachable("arg list didn't provide expected value");
661}
662
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000663std::string describeSanitizeArg(const llvm::opt::Arg *A, SanitizerMask Mask) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000664 assert(A->getOption().matches(options::OPT_fsanitize_EQ)
665 && "Invalid argument in describeSanitizerArg!");
Alexey Samsonovcf055962013-08-08 10:11:02 +0000666
Peter Collingbourne32701642013-11-01 18:16:25 +0000667 std::string Sanitizers;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000668 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000669 if (expandSanitizerGroups(
670 parseSanitizerValue(A->getValue(i), /*AllowGroups=*/true)) &
671 Mask) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000672 if (!Sanitizers.empty())
673 Sanitizers += ",";
Alexey Samsonov83791e22015-03-03 22:15:32 +0000674 Sanitizers += A->getValue(i);
Peter Collingbourne32701642013-11-01 18:16:25 +0000675 }
676 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000677
Peter Collingbourne32701642013-11-01 18:16:25 +0000678 assert(!Sanitizers.empty() && "arg didn't provide expected value");
679 return "-fsanitize=" + Sanitizers;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000680}