blob: 55036daa44470ecbf9e7660d80f7c72f5f00df29 [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"
David L. Jonesf561aba2017-03-08 01:02:16 +000010#include "ToolChains/CommonArgs.h"
Peter Collingbournebf59c342015-05-11 21:39:20 +000011#include "clang/Basic/Sanitizers.h"
Alexey Samsonovcf055962013-08-08 10:11:02 +000012#include "clang/Driver/Driver.h"
13#include "clang/Driver/DriverDiagnostic.h"
14#include "clang/Driver/Options.h"
15#include "clang/Driver/ToolChain.h"
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +000016#include "llvm/ADT/StringExtras.h"
Alexey Samsonovcf055962013-08-08 10:11:02 +000017#include "llvm/ADT/StringSwitch.h"
18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/Path.h"
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000020#include "llvm/Support/SpecialCaseList.h"
Peter Collingbournef11eb3e2018-04-04 21:55:44 +000021#include "llvm/Support/TargetParser.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000022#include <memory>
Alexey Samsonovcf055962013-08-08 10:11:02 +000023
Peter Collingbourne3eea6772015-05-11 21:39:14 +000024using namespace clang;
25using namespace clang::SanitizerKind;
Alexey Samsonovcf055962013-08-08 10:11:02 +000026using namespace clang::driver;
27using namespace llvm::opt;
28
Peter Collingbourne3eea6772015-05-11 21:39:14 +000029enum : SanitizerMask {
Roman Lebedevb69ba222018-07-30 18:58:30 +000030 NeedsUbsanRt = Undefined | Integer | ImplicitConversion | Nullability | CFI,
Alexey Samsonov9b873092015-06-25 23:14:32 +000031 NeedsUbsanCxxRt = Vptr | CFI,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000032 NotAllowedWithTrap = Vptr,
Evgeniy Stepanov6d2b6f02017-08-29 20:03:51 +000033 NotAllowedWithMinimalRuntime = Vptr,
Alex Shlyapnikov0a20cef2018-03-23 19:47:45 +000034 RequiresPIE = DataFlow | HWAddress | Scudo,
Evgeniy Stepanov12817e52017-12-09 01:32:07 +000035 NeedsUnwindTables = Address | HWAddress | Thread | Memory | DataFlow,
Andrey Konovalov1ba9d9c2018-04-13 18:05:21 +000036 SupportsCoverage = Address | HWAddress | KernelAddress | KernelHWAddress |
Roman Lebedevb69ba222018-07-30 18:58:30 +000037 Memory | Leak | Undefined | Integer | ImplicitConversion |
38 Nullability | DataFlow | Fuzzer | FuzzerNoLink,
39 RecoverableByDefault = Undefined | Integer | ImplicitConversion | Nullability,
Yury Gribov5bfeca12015-11-11 10:45:48 +000040 Unrecoverable = Unreachable | Return,
Andrey Konovalov1ba9d9c2018-04-13 18:05:21 +000041 AlwaysRecoverable = KernelAddress | KernelHWAddress,
Peter Collingbournea4ccff32015-02-20 20:30:56 +000042 LegacyFsanitizeRecoverMask = Undefined | Integer,
Peter Collingbourne1a7488a2015-04-02 00:23:30 +000043 NeedsLTO = CFI,
Vedant Kumar42c17ec2017-03-14 01:56:34 +000044 TrappingSupported = (Undefined & ~Vptr) | UnsignedIntegerOverflow |
Roman Lebedevb69ba222018-07-30 18:58:30 +000045 ImplicitConversion | Nullability | LocalBounds | CFI,
Peter Collingbourne6708c4a2015-06-19 01:51:54 +000046 TrappingDefault = CFI,
Peter Collingbournee44acad2018-06-26 02:15:47 +000047 CFIClasses =
48 CFIVCall | CFINVCall | CFIMFCall | CFIDerivedCast | CFIUnrelatedCast,
Kostya Kortchinsky64d80932018-06-22 14:31:30 +000049 CompatibleWithMinimalRuntime = TrappingSupported | Scudo,
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000050};
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000051
52enum CoverageFeature {
53 CoverageFunc = 1 << 0,
54 CoverageBB = 1 << 1,
55 CoverageEdge = 1 << 2,
56 CoverageIndirCall = 1 << 3,
Kostya Serebryany2c2fb882017-06-08 22:58:19 +000057 CoverageTraceBB = 1 << 4, // Deprecated.
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000058 CoverageTraceCmp = 1 << 5,
Kostya Serebryany3b419712016-08-30 01:27:03 +000059 CoverageTraceDiv = 1 << 6,
60 CoverageTraceGep = 1 << 7,
Kostya Serebryany2c2fb882017-06-08 22:58:19 +000061 Coverage8bitCounters = 1 << 8, // Deprecated.
Kostya Serebryany3b419712016-08-30 01:27:03 +000062 CoverageTracePC = 1 << 9,
Kostya Serebryany60cdd612016-09-14 01:39:49 +000063 CoverageTracePCGuard = 1 << 10,
Kostya Serebryany50fb6182017-05-05 23:28:18 +000064 CoverageNoPrune = 1 << 11,
Kostya Serebryany61457762017-07-28 00:10:10 +000065 CoverageInline8bitCounters = 1 << 12,
66 CoveragePCTable = 1 << 13,
Matt Morehouse5c7fc762017-08-18 18:43:30 +000067 CoverageStackDepth = 1 << 14,
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000068};
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000069
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000070/// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any
Peter Collingbourne3eea6772015-05-11 21:39:14 +000071/// invalid components. Returns a SanitizerMask.
72static SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
73 bool DiagnoseErrors);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000074
Alexey Samsonovdfa908c2015-05-07 22:34:06 +000075/// Parse -f(no-)?sanitize-coverage= flag values, diagnosing any invalid
76/// components. Returns OR of members of \c CoverageFeature enumeration.
77static int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A);
78
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000079/// Produce an argument string from ArgList \p Args, which shows how it
80/// provides some sanitizer kind from \p Mask. For example, the argument list
81/// "-fsanitize=thread,vptr -fsanitize=address" with mask \c NeedsUbsanRt
82/// would produce "-fsanitize=vptr".
83static std::string lastArgumentForMask(const Driver &D,
84 const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +000085 SanitizerMask Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000086
87/// Produce an argument string from argument \p A, which shows how it provides
88/// a value in \p Mask. For instance, the argument
89/// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce
90/// "-fsanitize=alignment".
Peter Collingbourne3eea6772015-05-11 21:39:14 +000091static std::string describeSanitizeArg(const llvm::opt::Arg *A,
92 SanitizerMask Mask);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +000093
Alexey Samsonov1e715a62014-11-16 20:53:53 +000094/// Produce a string containing comma-separated names of sanitizers in \p
95/// Sanitizers set.
96static std::string toString(const clang::SanitizerSet &Sanitizers);
97
Richard Smith6cd48612018-02-20 23:17:41 +000098static void addDefaultBlacklists(const Driver &D, SanitizerMask Kinds,
99 std::vector<std::string> &BlacklistFiles) {
100 struct Blacklist {
101 const char *File;
102 SanitizerMask Mask;
103 } Blacklists[] = {{"asan_blacklist.txt", Address},
104 {"hwasan_blacklist.txt", HWAddress},
105 {"msan_blacklist.txt", Memory},
106 {"tsan_blacklist.txt", Thread},
107 {"dfsan_abilist.txt", DataFlow},
108 {"cfi_blacklist.txt", CFI},
109 {"ubsan_blacklist.txt", Undefined | Integer | Nullability}};
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000110
Richard Smith6cd48612018-02-20 23:17:41 +0000111 for (auto BL : Blacklists) {
112 if (!(Kinds & BL.Mask))
113 continue;
114
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000115 clang::SmallString<64> Path(D.ResourceDir);
Richard Smith6cd48612018-02-20 23:17:41 +0000116 llvm::sys::path::append(Path, "share", BL.File);
117 if (llvm::sys::fs::exists(Path))
118 BlacklistFiles.push_back(Path.str());
Peter Collingbourne374599cf22018-05-07 20:54:05 +0000119 else if (BL.Mask == CFI)
120 // If cfi_blacklist.txt cannot be found in the resource dir, driver
121 // should fail.
122 D.Diag(clang::diag::err_drv_no_such_file) << Path;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000123 }
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000124}
125
Peter Collingbourne9881b782015-06-18 23:59:22 +0000126/// Sets group bits for every group that has at least one representative already
127/// enabled in \p Kinds.
128static SanitizerMask setGroupBits(SanitizerMask Kinds) {
129#define SANITIZER(NAME, ID)
130#define SANITIZER_GROUP(NAME, ID, ALIAS) \
131 if (Kinds & SanitizerKind::ID) \
132 Kinds |= SanitizerKind::ID##Group;
133#include "clang/Basic/Sanitizers.def"
134 return Kinds;
135}
136
137static SanitizerMask parseSanitizeTrapArgs(const Driver &D,
138 const llvm::opt::ArgList &Args) {
139 SanitizerMask TrapRemove = 0; // During the loop below, the accumulated set of
140 // sanitizers disabled by the current sanitizer
141 // argument or any argument after it.
142 SanitizerMask TrappingKinds = 0;
143 SanitizerMask TrappingSupportedWithGroups = setGroupBits(TrappingSupported);
144
145 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
146 I != E; ++I) {
147 const auto *Arg = *I;
148 if (Arg->getOption().matches(options::OPT_fsanitize_trap_EQ)) {
149 Arg->claim();
150 SanitizerMask Add = parseArgValues(D, Arg, true);
151 Add &= ~TrapRemove;
152 if (SanitizerMask InvalidValues = Add & ~TrappingSupportedWithGroups) {
153 SanitizerSet S;
154 S.Mask = InvalidValues;
155 D.Diag(diag::err_drv_unsupported_option_argument) << "-fsanitize-trap"
156 << toString(S);
157 }
158 TrappingKinds |= expandSanitizerGroups(Add) & ~TrapRemove;
159 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) {
160 Arg->claim();
161 TrapRemove |= expandSanitizerGroups(parseArgValues(D, Arg, true));
162 } else if (Arg->getOption().matches(
163 options::OPT_fsanitize_undefined_trap_on_error)) {
164 Arg->claim();
165 TrappingKinds |=
166 expandSanitizerGroups(UndefinedGroup & ~TrapRemove) & ~TrapRemove;
167 } else if (Arg->getOption().matches(
168 options::OPT_fno_sanitize_undefined_trap_on_error)) {
169 Arg->claim();
170 TrapRemove |= expandSanitizerGroups(UndefinedGroup);
171 }
172 }
173
Peter Collingbourne6708c4a2015-06-19 01:51:54 +0000174 // Apply default trapping behavior.
175 TrappingKinds |= TrappingDefault & ~TrapRemove;
176
Peter Collingbourne9881b782015-06-18 23:59:22 +0000177 return TrappingKinds;
178}
179
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000180bool SanitizerArgs::needsUbsanRt() const {
Evgeniy Stepanovd5d36a12017-10-16 18:02:57 +0000181 // All of these include ubsan.
Evgeniy Stepanov12817e52017-12-09 01:32:07 +0000182 if (needsAsanRt() || needsMsanRt() || needsHwasanRt() || needsTsanRt() ||
Kostya Kortchinsky64d80932018-06-22 14:31:30 +0000183 needsDfsanRt() || needsLsanRt() || needsCfiDiagRt() ||
184 (needsScudoRt() && !requiresMinimalRuntime()))
Evgeniy Stepanovd5d36a12017-10-16 18:02:57 +0000185 return false;
186
187 return (Sanitizers.Mask & NeedsUbsanRt & ~TrapSanitizers.Mask) ||
188 CoverageFeatures;
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000189}
190
191bool SanitizerArgs::needsCfiRt() const {
Evgeniy Stepanovd5d36a12017-10-16 18:02:57 +0000192 return !(Sanitizers.Mask & CFI & ~TrapSanitizers.Mask) && CfiCrossDso &&
193 !ImplicitCfiRuntime;
Evgeniy Stepanove3fb51c2015-12-16 00:38:42 +0000194}
195
196bool SanitizerArgs::needsCfiDiagRt() const {
Evgeniy Stepanovd5d36a12017-10-16 18:02:57 +0000197 return (Sanitizers.Mask & CFI & ~TrapSanitizers.Mask) && CfiCrossDso &&
198 !ImplicitCfiRuntime;
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000199}
200
Dmitry Vyukov43419a72014-11-21 12:19:01 +0000201bool SanitizerArgs::requiresPIE() const {
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +0000202 return NeedPIE || (Sanitizers.Mask & RequiresPIE);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000203}
204
205bool SanitizerArgs::needsUnwindTables() const {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000206 return Sanitizers.Mask & NeedsUnwindTables;
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000207}
208
Peter Collingbourne32701642013-11-01 18:16:25 +0000209SanitizerArgs::SanitizerArgs(const ToolChain &TC,
210 const llvm::opt::ArgList &Args) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000211 SanitizerMask AllRemove = 0; // During the loop below, the accumulated set of
212 // sanitizers disabled by the current sanitizer
213 // argument or any argument after it.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000214 SanitizerMask AllAddedKinds = 0; // Mask of all sanitizers ever enabled by
215 // -fsanitize= flags (directly or via group
216 // expansion), some of which may be disabled
217 // later. Used to carefully prune
218 // unused-argument diagnostics.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000219 SanitizerMask DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
220 // Used to deduplicate diagnostics.
221 SanitizerMask Kinds = 0;
Alexey Samsonov9b873092015-06-25 23:14:32 +0000222 const SanitizerMask Supported = setGroupBits(TC.getSupportedSanitizers());
Peter Collingbournee44acad2018-06-26 02:15:47 +0000223
224 CfiCrossDso = Args.hasFlag(options::OPT_fsanitize_cfi_cross_dso,
225 options::OPT_fno_sanitize_cfi_cross_dso, false);
226
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000227 ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
228
Peter Collingbourne32701642013-11-01 18:16:25 +0000229 const Driver &D = TC.getDriver();
Peter Collingbourne9881b782015-06-18 23:59:22 +0000230 SanitizerMask TrappingKinds = parseSanitizeTrapArgs(D, Args);
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000231 SanitizerMask InvalidTrappingKinds = TrappingKinds & NotAllowedWithTrap;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000232
Evgeniy Stepanov6d2b6f02017-08-29 20:03:51 +0000233 MinimalRuntime =
234 Args.hasFlag(options::OPT_fsanitize_minimal_runtime,
235 options::OPT_fno_sanitize_minimal_runtime, MinimalRuntime);
236
Vedant Kumar7aacb652017-06-23 23:15:24 +0000237 // The object size sanitizer should not be enabled at -O0.
238 Arg *OptLevel = Args.getLastArg(options::OPT_O_Group);
239 bool RemoveObjectSizeAtO0 =
240 !OptLevel || OptLevel->getOption().matches(options::OPT_O0);
241
Peter Collingbourne32701642013-11-01 18:16:25 +0000242 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
243 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000244 const auto *Arg = *I;
245 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
246 Arg->claim();
Vedant Kumar7aacb652017-06-23 23:15:24 +0000247 SanitizerMask Add = parseArgValues(D, Arg, /*AllowGroups=*/true);
248
249 if (RemoveObjectSizeAtO0) {
250 AllRemove |= SanitizerKind::ObjectSize;
251
252 // The user explicitly enabled the object size sanitizer. Warn that
253 // that this does nothing at -O0.
254 if (Add & SanitizerKind::ObjectSize)
255 D.Diag(diag::warn_drv_object_size_disabled_O0)
256 << Arg->getAsString(Args);
257 }
258
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000259 AllAddedKinds |= expandSanitizerGroups(Add);
Peter Collingbourne32701642013-11-01 18:16:25 +0000260
Alexey Samsonov799f7932014-12-19 02:35:16 +0000261 // Avoid diagnosing any sanitizer which is disabled later.
262 Add &= ~AllRemove;
263 // At this point we have not expanded groups, so any unsupported
264 // sanitizers in Add are those which have been explicitly enabled.
265 // Diagnose them.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000266 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000267 Add & InvalidTrappingKinds & ~DiagnosedKinds) {
Peter Collingbourne9881b782015-06-18 23:59:22 +0000268 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
269 D.Diag(diag::err_drv_argument_not_allowed_with)
270 << Desc << "-fsanitize-trap=undefined";
271 DiagnosedKinds |= KindsToDiagnose;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000272 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000273 Add &= ~InvalidTrappingKinds;
Evgeniy Stepanov6d2b6f02017-08-29 20:03:51 +0000274
275 if (MinimalRuntime) {
276 if (SanitizerMask KindsToDiagnose =
277 Add & NotAllowedWithMinimalRuntime & ~DiagnosedKinds) {
278 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
279 D.Diag(diag::err_drv_argument_not_allowed_with)
280 << Desc << "-fsanitize-minimal-runtime";
281 DiagnosedKinds |= KindsToDiagnose;
282 }
283 Add &= ~NotAllowedWithMinimalRuntime;
284 }
285
Peter Collingbournee44acad2018-06-26 02:15:47 +0000286 // FIXME: Make CFI on member function calls compatible with cross-DSO CFI.
287 // There are currently two problems:
288 // - Virtual function call checks need to pass a pointer to the function
289 // address to llvm.type.test and a pointer to the address point to the
290 // diagnostic function. Currently we pass the same pointer to both
291 // places.
292 // - Non-virtual function call checks may need to check multiple type
293 // identifiers.
294 // Fixing both of those may require changes to the cross-DSO CFI
295 // interface.
296 if (CfiCrossDso && (Add & CFIMFCall & ~DiagnosedKinds)) {
297 D.Diag(diag::err_drv_argument_not_allowed_with)
298 << "-fsanitize=cfi-mfcall"
299 << "-fsanitize-cfi-cross-dso";
300 Add &= ~CFIMFCall;
301 DiagnosedKinds |= CFIMFCall;
302 }
303
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000304 if (SanitizerMask KindsToDiagnose = Add & ~Supported & ~DiagnosedKinds) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000305 std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
306 D.Diag(diag::err_drv_unsupported_opt_for_target)
307 << Desc << TC.getTriple().str();
308 DiagnosedKinds |= KindsToDiagnose;
309 }
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000310 Add &= Supported;
Peter Collingbourne32701642013-11-01 18:16:25 +0000311
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000312 // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups
313 // so we don't error out if -fno-rtti and -fsanitize=undefined were
314 // passed.
Sunil Srivastava2ada2492018-05-18 23:32:01 +0000315 if ((Add & Vptr) && (RTTIMode == ToolChain::RM_Disabled)) {
316 if (const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg()) {
317 assert(NoRTTIArg->getOption().matches(options::OPT_fno_rtti) &&
318 "RTTI disabled without -fno-rtti option?");
319 // The user explicitly passed -fno-rtti with -fsanitize=vptr, but
320 // the vptr sanitizer requires RTTI, so this is a user error.
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000321 D.Diag(diag::err_drv_argument_not_allowed_with)
322 << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args);
Sunil Srivastava2ada2492018-05-18 23:32:01 +0000323 } else {
324 // The vptr sanitizer requires RTTI, but RTTI is disabled (by
325 // default). Warn that the vptr sanitizer is being disabled.
326 D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default);
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000327 }
328
329 // Take out the Vptr sanitizer from the enabled sanitizers
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000330 AllRemove |= Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000331 }
332
Peter Collingbournebf59c342015-05-11 21:39:20 +0000333 Add = expandSanitizerGroups(Add);
Alexey Samsonov799f7932014-12-19 02:35:16 +0000334 // Group expansion may have enabled a sanitizer which is disabled later.
335 Add &= ~AllRemove;
336 // Silently discard any unsupported sanitizers implicitly enabled through
337 // group expansion.
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000338 Add &= ~InvalidTrappingKinds;
Evgeniy Stepanov6d2b6f02017-08-29 20:03:51 +0000339 if (MinimalRuntime) {
340 Add &= ~NotAllowedWithMinimalRuntime;
341 }
Peter Collingbournee44acad2018-06-26 02:15:47 +0000342 if (CfiCrossDso)
343 Add &= ~CFIMFCall;
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000344 Add &= Supported;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000345
George Karpenkovf2fc5b02017-04-24 18:23:24 +0000346 if (Add & Fuzzer)
George Karpenkov33613f62017-08-11 17:22:58 +0000347 Add |= FuzzerNoLink;
348
Matt Morehouse6ec75952017-08-25 22:01:21 +0000349 // Enable coverage if the fuzzing flag is set.
Matt Morehouse034126e2017-08-30 22:49:31 +0000350 if (Add & FuzzerNoLink) {
Kostya Serebryanya5231352017-09-01 18:34:36 +0000351 CoverageFeatures |= CoverageInline8bitCounters | CoverageIndirCall |
Matt Morehouse6ec75952017-08-25 22:01:21 +0000352 CoverageTraceCmp | CoveragePCTable;
Matt Morehouse034126e2017-08-30 22:49:31 +0000353 // Due to TLS differences, stack depth tracking is only enabled on Linux
354 if (TC.getTriple().isOSLinux())
355 CoverageFeatures |= CoverageStackDepth;
356 }
George Karpenkovf2fc5b02017-04-24 18:23:24 +0000357
Alexey Samsonov799f7932014-12-19 02:35:16 +0000358 Kinds |= Add;
359 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
360 Arg->claim();
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000361 SanitizerMask Remove = parseArgValues(D, Arg, true);
Peter Collingbournebf59c342015-05-11 21:39:20 +0000362 AllRemove |= expandSanitizerGroups(Remove);
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000363 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000364 }
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000365
Petr Hoseka14b4602018-03-07 01:27:03 +0000366 std::pair<SanitizerMask, SanitizerMask> IncompatibleGroups[] = {
367 std::make_pair(Address, Thread | Memory),
368 std::make_pair(Thread, Memory),
369 std::make_pair(Leak, Thread | Memory),
370 std::make_pair(KernelAddress, Address | Leak | Thread | Memory),
371 std::make_pair(HWAddress, Address | Thread | Memory | KernelAddress),
372 std::make_pair(Efficiency, Address | HWAddress | Leak | Thread | Memory |
373 KernelAddress),
374 std::make_pair(Scudo, Address | HWAddress | Leak | Thread | Memory |
375 KernelAddress | Efficiency),
376 std::make_pair(SafeStack, Address | HWAddress | Leak | Thread | Memory |
Vlad Tsyrkleviche55aa032018-04-03 22:33:53 +0000377 KernelAddress | Efficiency),
378 std::make_pair(ShadowCallStack, Address | HWAddress | Leak | Thread |
379 Memory | KernelAddress | Efficiency |
Andrey Konovalov1ba9d9c2018-04-13 18:05:21 +0000380 SafeStack),
381 std::make_pair(KernelHWAddress, Address | HWAddress | Leak | Thread |
382 Memory | KernelAddress | Efficiency |
383 SafeStack | ShadowCallStack)};
Petr Hoseka14b4602018-03-07 01:27:03 +0000384
Ed Schoutenfc79d2c2016-03-29 21:13:53 +0000385 // Enable toolchain specific default sanitizers if not explicitly disabled.
Petr Hoseka14b4602018-03-07 01:27:03 +0000386 SanitizerMask Default = TC.getDefaultSanitizers() & ~AllRemove;
387
Petr Hosek8b8d6bf2018-03-12 00:23:37 +0000388 // Disable default sanitizers that are incompatible with explicitly requested
389 // ones.
Petr Hoseka14b4602018-03-07 01:27:03 +0000390 for (auto G : IncompatibleGroups) {
391 SanitizerMask Group = G.first;
392 if ((Default & Group) && (Kinds & G.second))
393 Default &= ~Group;
394 }
395
396 Kinds |= Default;
Ed Schoutenfc79d2c2016-03-29 21:13:53 +0000397
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000398 // We disable the vptr sanitizer if it was enabled by group expansion but RTTI
399 // is disabled.
Sunil Srivastava2ada2492018-05-18 23:32:01 +0000400 if ((Kinds & Vptr) && (RTTIMode == ToolChain::RM_Disabled)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000401 Kinds &= ~Vptr;
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +0000402 }
403
Alexey Samsonov907880e2015-06-19 19:57:46 +0000404 // Check that LTO is enabled if we need it.
Teresa Johnson945bc502015-10-15 20:35:53 +0000405 if ((Kinds & NeedsLTO) && !D.isUsingLTO()) {
Alexey Samsonov907880e2015-06-19 19:57:46 +0000406 D.Diag(diag::err_drv_argument_only_allowed_with)
407 << lastArgumentForMask(D, Args, Kinds & NeedsLTO) << "-flto";
408 }
409
Peter Collingbournef11eb3e2018-04-04 21:55:44 +0000410 if ((Kinds & ShadowCallStack) &&
411 TC.getTriple().getArch() == llvm::Triple::aarch64 &&
412 !llvm::AArch64::isX18ReservedByDefault(TC.getTriple()) &&
413 !Args.hasArg(options::OPT_ffixed_x18)) {
414 D.Diag(diag::err_drv_argument_only_allowed_with)
415 << lastArgumentForMask(D, Args, Kinds & ShadowCallStack)
416 << "-ffixed-x18";
417 }
418
Alexey Samsonov9b873092015-06-25 23:14:32 +0000419 // Report error if there are non-trapping sanitizers that require
420 // c++abi-specific parts of UBSan runtime, and they are not provided by the
421 // toolchain. We don't have a good way to check the latter, so we just
422 // check if the toolchan supports vptr.
423 if (~Supported & Vptr) {
Peter Collingbourne4bdceaa2015-07-08 21:08:05 +0000424 SanitizerMask KindsToDiagnose = Kinds & ~TrappingKinds & NeedsUbsanCxxRt;
425 // The runtime library supports the Microsoft C++ ABI, but only well enough
426 // for CFI. FIXME: Remove this once we support vptr on Windows.
427 if (TC.getTriple().isOSWindows())
428 KindsToDiagnose &= ~CFI;
429 if (KindsToDiagnose) {
Alexey Samsonov9b873092015-06-25 23:14:32 +0000430 SanitizerSet S;
431 S.Mask = KindsToDiagnose;
432 D.Diag(diag::err_drv_unsupported_opt_for_target)
433 << ("-fno-sanitize-trap=" + toString(S)) << TC.getTriple().str();
434 Kinds &= ~KindsToDiagnose;
435 }
436 }
437
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000438 // Warn about incompatible groups of sanitizers.
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000439 for (auto G : IncompatibleGroups) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000440 SanitizerMask Group = G.first;
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000441 if (Kinds & Group) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000442 if (SanitizerMask Incompatible = Kinds & G.second) {
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000443 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
444 << lastArgumentForMask(D, Args, Group)
445 << lastArgumentForMask(D, Args, Incompatible);
446 Kinds &= ~Incompatible;
447 }
448 }
449 }
450 // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
451 // -fsanitize=address. Perhaps it should print an error, or perhaps
452 // -f(-no)sanitize=leak should change whether leak detection is enabled by
453 // default in ASan?
454
Alexey Samsonov88459522015-01-12 22:39:12 +0000455 // Parse -f(no-)?sanitize-recover flags.
Andrey Konovalov1ba9d9c2018-04-13 18:05:21 +0000456 SanitizerMask RecoverableKinds = RecoverableByDefault | AlwaysRecoverable;
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000457 SanitizerMask DiagnosedUnrecoverableKinds = 0;
Andrey Konovalov1ba9d9c2018-04-13 18:05:21 +0000458 SanitizerMask DiagnosedAlwaysRecoverableKinds = 0;
Alexey Samsonov88459522015-01-12 22:39:12 +0000459 for (const auto *Arg : Args) {
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000460 const char *DeprecatedReplacement = nullptr;
Alexey Samsonov88459522015-01-12 22:39:12 +0000461 if (Arg->getOption().matches(options::OPT_fsanitize_recover)) {
Kostya Serebryanyceb1add2016-05-04 20:21:47 +0000462 DeprecatedReplacement =
463 "-fsanitize-recover=undefined,integer' or '-fsanitize-recover=all";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000464 RecoverableKinds |= expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000465 Arg->claim();
466 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) {
Kostya Serebryanyceb1add2016-05-04 20:21:47 +0000467 DeprecatedReplacement = "-fno-sanitize-recover=undefined,integer' or "
468 "'-fno-sanitize-recover=all";
Peter Collingbournebf59c342015-05-11 21:39:20 +0000469 RecoverableKinds &= ~expandSanitizerGroups(LegacyFsanitizeRecoverMask);
Alexey Samsonov88459522015-01-12 22:39:12 +0000470 Arg->claim();
471 } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000472 SanitizerMask Add = parseArgValues(D, Arg, true);
Alexey Samsonov88459522015-01-12 22:39:12 +0000473 // Report error if user explicitly tries to recover from unrecoverable
474 // sanitizer.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000475 if (SanitizerMask KindsToDiagnose =
Alexey Samsonov88459522015-01-12 22:39:12 +0000476 Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
477 SanitizerSet SetToDiagnose;
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000478 SetToDiagnose.Mask |= KindsToDiagnose;
Alexey Samsonov88459522015-01-12 22:39:12 +0000479 D.Diag(diag::err_drv_unsupported_option_argument)
480 << Arg->getOption().getName() << toString(SetToDiagnose);
481 DiagnosedUnrecoverableKinds |= KindsToDiagnose;
482 }
Peter Collingbournebf59c342015-05-11 21:39:20 +0000483 RecoverableKinds |= expandSanitizerGroups(Add);
Alexey Samsonov88459522015-01-12 22:39:12 +0000484 Arg->claim();
485 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
Andrey Konovalov1ba9d9c2018-04-13 18:05:21 +0000486 SanitizerMask Remove = parseArgValues(D, Arg, true);
487 // Report error if user explicitly tries to disable recovery from
488 // always recoverable sanitizer.
489 if (SanitizerMask KindsToDiagnose =
490 Remove & AlwaysRecoverable & ~DiagnosedAlwaysRecoverableKinds) {
491 SanitizerSet SetToDiagnose;
492 SetToDiagnose.Mask |= KindsToDiagnose;
493 D.Diag(diag::err_drv_unsupported_option_argument)
494 << Arg->getOption().getName() << toString(SetToDiagnose);
495 DiagnosedAlwaysRecoverableKinds |= KindsToDiagnose;
496 }
497 RecoverableKinds &= ~expandSanitizerGroups(Remove);
Alexey Samsonov88459522015-01-12 22:39:12 +0000498 Arg->claim();
499 }
Alexey Samsonovce2e77c2015-03-11 23:34:25 +0000500 if (DeprecatedReplacement) {
501 D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
502 << DeprecatedReplacement;
503 }
Alexey Samsonov88459522015-01-12 22:39:12 +0000504 }
505 RecoverableKinds &= Kinds;
506 RecoverableKinds &= ~Unrecoverable;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000507
Peter Collingbourne9881b782015-06-18 23:59:22 +0000508 TrappingKinds &= Kinds;
Vedant Kumarfae4f7c2017-12-21 00:10:24 +0000509 RecoverableKinds &= ~TrappingKinds;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000510
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000511 // Setup blacklist files.
512 // Add default blacklist from resource directory.
Richard Smith6cd48612018-02-20 23:17:41 +0000513 addDefaultBlacklists(D, Kinds, BlacklistFiles);
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000514 // Parse -f(no-)sanitize-blacklist options.
515 for (const auto *Arg : Args) {
516 if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) {
517 Arg->claim();
518 std::string BLPath = Arg->getValue();
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000519 if (llvm::sys::fs::exists(BLPath)) {
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000520 BlacklistFiles.push_back(BLPath);
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000521 ExtraDeps.push_back(BLPath);
Richard Smith6cd48612018-02-20 23:17:41 +0000522 } else {
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000523 D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
Richard Smith6cd48612018-02-20 23:17:41 +0000524 }
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000525 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) {
526 Arg->claim();
527 BlacklistFiles.clear();
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000528 ExtraDeps.clear();
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000529 }
530 }
531 // Validate blacklists format.
532 {
533 std::string BLError;
534 std::unique_ptr<llvm::SpecialCaseList> SCL(
535 llvm::SpecialCaseList::create(BlacklistFiles, BLError));
536 if (!SCL.get())
537 D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000538 }
539
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000540 // Parse -f[no-]sanitize-memory-track-origins[=level] options.
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000541 if (AllAddedKinds & Memory) {
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000542 if (Arg *A =
543 Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
544 options::OPT_fsanitize_memory_track_origins,
545 options::OPT_fno_sanitize_memory_track_origins)) {
546 if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
Evgeniy Stepanov6e09bca2015-02-26 15:59:30 +0000547 MsanTrackOrigins = 2;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000548 } else if (A->getOption().matches(
549 options::OPT_fno_sanitize_memory_track_origins)) {
550 MsanTrackOrigins = 0;
551 } else {
552 StringRef S = A->getValue();
553 if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
554 MsanTrackOrigins > 2) {
Nico Weberf2a39a72015-04-13 20:03:03 +0000555 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000556 }
557 }
558 }
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +0000559 MsanUseAfterDtor =
Matt Morehousebb26f862017-09-14 23:14:37 +0000560 Args.hasFlag(options::OPT_fsanitize_memory_use_after_dtor,
561 options::OPT_fno_sanitize_memory_use_after_dtor,
Matt Morehouse175625c2017-09-14 23:53:56 +0000562 MsanUseAfterDtor);
Evgeniy Stepanov71554fe2015-10-21 21:28:49 +0000563 NeedPIE |= !(TC.getTriple().isOSLinux() &&
564 TC.getTriple().getArch() == llvm::Triple::x86_64);
Matt Morehouse175625c2017-09-14 23:53:56 +0000565 } else {
566 MsanUseAfterDtor = false;
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000567 }
568
Evgeniy Stepanov8a2bedb2016-11-11 23:17:36 +0000569 if (AllAddedKinds & Thread) {
570 TsanMemoryAccess = Args.hasFlag(options::OPT_fsanitize_thread_memory_access,
571 options::OPT_fno_sanitize_thread_memory_access,
572 TsanMemoryAccess);
573 TsanFuncEntryExit = Args.hasFlag(options::OPT_fsanitize_thread_func_entry_exit,
574 options::OPT_fno_sanitize_thread_func_entry_exit,
575 TsanFuncEntryExit);
576 TsanAtomics = Args.hasFlag(options::OPT_fsanitize_thread_atomics,
577 options::OPT_fno_sanitize_thread_atomics,
578 TsanAtomics);
579 }
580
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000581 if (AllAddedKinds & CFI) {
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000582 // Without PIE, external function address may resolve to a PLT record, which
583 // can not be verified by the target module.
584 NeedPIE |= CfiCrossDso;
Vlad Tsyrklevich634c6012017-10-31 22:39:44 +0000585 CfiICallGeneralizePointers =
586 Args.hasArg(options::OPT_fsanitize_cfi_icall_generalize_pointers);
587
588 if (CfiCrossDso && CfiICallGeneralizePointers)
589 D.Diag(diag::err_drv_argument_not_allowed_with)
590 << "-fsanitize-cfi-cross-dso"
591 << "-fsanitize-cfi-icall-generalize-pointers";
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000592 }
593
Peter Collingbournedc134532016-01-16 00:31:22 +0000594 Stats = Args.hasFlag(options::OPT_fsanitize_stats,
595 options::OPT_fno_sanitize_stats, false);
596
Evgeniy Stepanov6d2b6f02017-08-29 20:03:51 +0000597 if (MinimalRuntime) {
598 SanitizerMask IncompatibleMask =
599 Kinds & ~setGroupBits(CompatibleWithMinimalRuntime);
600 if (IncompatibleMask)
601 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
602 << "-fsanitize-minimal-runtime"
603 << lastArgumentForMask(D, Args, IncompatibleMask);
604
605 SanitizerMask NonTrappingCfi = Kinds & CFI & ~TrappingKinds;
606 if (NonTrappingCfi)
607 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
608 << "fsanitize-minimal-runtime"
609 << "fsanitize-trap=cfi";
610 }
611
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000612 // Parse -f(no-)?sanitize-coverage flags if coverage is supported by the
613 // enabled sanitizers.
Kostya Serebryany52e86492016-02-18 00:49:23 +0000614 for (const auto *Arg : Args) {
615 if (Arg->getOption().matches(options::OPT_fsanitize_coverage)) {
616 int LegacySanitizeCoverage;
617 if (Arg->getNumValues() == 1 &&
618 !StringRef(Arg->getValue(0))
Kostya Serebryany9d1ed132017-04-19 19:57:16 +0000619 .getAsInteger(0, LegacySanitizeCoverage)) {
620 CoverageFeatures = 0;
621 Arg->claim();
622 if (LegacySanitizeCoverage != 0) {
Nico Weber4152f522016-02-18 19:32:54 +0000623 D.Diag(diag::warn_drv_deprecated_arg)
Kostya Serebryany9d1ed132017-04-19 19:57:16 +0000624 << Arg->getAsString(Args) << "-fsanitize-coverage=trace-pc-guard";
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000625 }
Kostya Serebryany52e86492016-02-18 00:49:23 +0000626 continue;
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000627 }
Kostya Serebryany52e86492016-02-18 00:49:23 +0000628 CoverageFeatures |= parseCoverageFeatures(D, Arg);
Evgeniy Stepanov5b49eb42016-06-14 21:33:40 +0000629
630 // Disable coverage and not claim the flags if there is at least one
631 // non-supporting sanitizer.
Petr Hosekeb4127f2017-07-21 01:17:49 +0000632 if (!(AllAddedKinds & ~AllRemove & ~setGroupBits(SupportsCoverage))) {
Kostya Serebryany52e86492016-02-18 00:49:23 +0000633 Arg->claim();
Kostya Serebryanyf5b25f82016-04-18 21:30:17 +0000634 } else {
635 CoverageFeatures = 0;
Kostya Serebryany52e86492016-02-18 00:49:23 +0000636 }
637 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_coverage)) {
638 Arg->claim();
639 CoverageFeatures &= ~parseCoverageFeatures(D, Arg);
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000640 }
641 }
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000642 // Choose at most one coverage type: function, bb, or edge.
643 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageBB))
644 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
645 << "-fsanitize-coverage=func"
646 << "-fsanitize-coverage=bb";
647 if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageEdge))
648 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
649 << "-fsanitize-coverage=func"
650 << "-fsanitize-coverage=edge";
651 if ((CoverageFeatures & CoverageBB) && (CoverageFeatures & CoverageEdge))
652 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
653 << "-fsanitize-coverage=bb"
654 << "-fsanitize-coverage=edge";
655 // Basic block tracing and 8-bit counters require some type of coverage
656 // enabled.
Kostya Serebryany1c0e9e92017-04-19 21:31:11 +0000657 if (CoverageFeatures & CoverageTraceBB)
658 D.Diag(clang::diag::warn_drv_deprecated_arg)
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000659 << "-fsanitize-coverage=trace-bb"
Kostya Serebryany1c0e9e92017-04-19 21:31:11 +0000660 << "-fsanitize-coverage=trace-pc-guard";
661 if (CoverageFeatures & Coverage8bitCounters)
Kostya Serebryany1a02d8b2017-04-19 20:15:58 +0000662 D.Diag(clang::diag::warn_drv_deprecated_arg)
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000663 << "-fsanitize-coverage=8bit-counters"
Kostya Serebryany1a02d8b2017-04-19 20:15:58 +0000664 << "-fsanitize-coverage=trace-pc-guard";
Kostya Serebryany8955efc2017-05-03 01:27:28 +0000665
666 int InsertionPointTypes = CoverageFunc | CoverageBB | CoverageEdge;
Kostya Serebryany9f338dc2017-08-08 20:20:40 +0000667 int InstrumentationTypes =
668 CoverageTracePC | CoverageTracePCGuard | CoverageInline8bitCounters;
Kostya Serebryany8955efc2017-05-03 01:27:28 +0000669 if ((CoverageFeatures & InsertionPointTypes) &&
Kostya Serebryany9f338dc2017-08-08 20:20:40 +0000670 !(CoverageFeatures & InstrumentationTypes)) {
Kostya Serebryany8955efc2017-05-03 01:27:28 +0000671 D.Diag(clang::diag::warn_drv_deprecated_arg)
672 << "-fsanitize-coverage=[func|bb|edge]"
673 << "-fsanitize-coverage=[func|bb|edge],[trace-pc-guard|trace-pc]";
674 }
675
Kostya Serebryany52e86492016-02-18 00:49:23 +0000676 // trace-pc w/o func/bb/edge implies edge.
Matt Morehouse5c7fc762017-08-18 18:43:30 +0000677 if (!(CoverageFeatures & InsertionPointTypes)) {
678 if (CoverageFeatures &
679 (CoverageTracePC | CoverageTracePCGuard | CoverageInline8bitCounters))
680 CoverageFeatures |= CoverageEdge;
681
682 if (CoverageFeatures & CoverageStackDepth)
683 CoverageFeatures |= CoverageFunc;
684 }
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000685
Evgeniy Stepanov0876cfb2017-10-05 20:14:00 +0000686 SharedRuntime =
687 Args.hasFlag(options::OPT_shared_libsan, options::OPT_static_libsan,
Vedant Kumar358d6422017-10-07 01:42:09 +0000688 TC.getTriple().isAndroid() || TC.getTriple().isOSFuchsia() ||
689 TC.getTriple().isOSDarwin());
Evgeniy Stepanov0876cfb2017-10-05 20:14:00 +0000690
Evgeniy Stepanovd5d36a12017-10-16 18:02:57 +0000691 ImplicitCfiRuntime = TC.getTriple().isAndroid();
692
Alexey Samsonovde0aff32015-05-21 01:07:52 +0000693 if (AllAddedKinds & Address) {
Evgeniy Stepanov117627c2017-10-25 20:39:22 +0000694 NeedPIE |= TC.getTriple().isOSFuchsia();
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000695 if (Arg *A =
696 Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
697 StringRef S = A->getValue();
698 // Legal values are 0 and 1, 2, but in future we may add more levels.
699 if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
700 AsanFieldPadding > 2) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000701 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000702 }
703 }
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000704
705 if (Arg *WindowsDebugRTArg =
706 Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
707 options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
708 options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
709 switch (WindowsDebugRTArg->getOption().getID()) {
710 case options::OPT__SLASH_MTd:
711 case options::OPT__SLASH_MDd:
712 case options::OPT__SLASH_LDd:
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000713 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000714 << WindowsDebugRTArg->getAsString(Args)
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000715 << lastArgumentForMask(D, Args, Address);
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000716 D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
Ehsan Akhgarie0db1962014-10-14 23:15:44 +0000717 }
718 }
Alexey Samsonov90490af2014-08-08 22:47:17 +0000719
Vedant Kumara9d8be62017-05-08 21:11:55 +0000720 AsanUseAfterScope = Args.hasFlag(
721 options::OPT_fsanitize_address_use_after_scope,
722 options::OPT_fno_sanitize_address_use_after_scope, AsanUseAfterScope);
Evgeniy Stepanovd991cdd2017-05-09 21:57:43 +0000723
724 // As a workaround for a bug in gold 2.26 and earlier, dead stripping of
725 // globals in ASan is disabled by default on ELF targets.
726 // See https://sourceware.org/bugzilla/show_bug.cgi?id=19002
727 AsanGlobalsDeadStripping =
Petr Hosekb31582b2017-08-03 23:02:22 +0000728 !TC.getTriple().isOSBinFormatELF() || TC.getTriple().isOSFuchsia() ||
Evgeniy Stepanovd991cdd2017-05-09 21:57:43 +0000729 Args.hasArg(options::OPT_fsanitize_address_globals_dead_stripping);
Vedant Kumara9d8be62017-05-08 21:11:55 +0000730 } else {
731 AsanUseAfterScope = false;
Vitaly Buka9d4eb6f2016-06-02 00:24:20 +0000732 }
733
Petr Hosek766288b2017-08-16 19:06:05 +0000734 if (AllAddedKinds & SafeStack) {
735 // SafeStack runtime is built into the system on Fuchsia.
736 SafeStackRuntime = !TC.getTriple().isOSFuchsia();
737 }
738
Alexey Samsonov90490af2014-08-08 22:47:17 +0000739 // Parse -link-cxx-sanitizer flag.
740 LinkCXXRuntimes =
741 Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX();
Alexey Samsonovecf380e2015-03-20 18:45:06 +0000742
743 // Finally, initialize the set of available and recoverable sanitizers.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000744 Sanitizers.Mask |= Kinds;
745 RecoverableSanitizers.Mask |= RecoverableKinds;
Peter Collingbourne9881b782015-06-18 23:59:22 +0000746 TrapSanitizers.Mask |= TrappingKinds;
Vedant Kumarfae4f7c2017-12-21 00:10:24 +0000747 assert(!(RecoverableKinds & TrappingKinds) &&
748 "Overlap between recoverable and trapping sanitizers");
Alexey Samsonovcf055962013-08-08 10:11:02 +0000749}
750
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000751static std::string toString(const clang::SanitizerSet &Sanitizers) {
752 std::string Res;
753#define SANITIZER(NAME, ID) \
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000754 if (Sanitizers.has(ID)) { \
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000755 if (!Res.empty()) \
756 Res += ","; \
757 Res += NAME; \
758 }
759#include "clang/Basic/Sanitizers.def"
760 return Res;
761}
762
Peter Collingbournedc134532016-01-16 00:31:22 +0000763static void addIncludeLinkerOption(const ToolChain &TC,
764 const llvm::opt::ArgList &Args,
765 llvm::opt::ArgStringList &CmdArgs,
766 StringRef SymbolName) {
767 SmallString<64> LinkerOptionFlag;
768 LinkerOptionFlag = "--linker-option=/include:";
769 if (TC.getTriple().getArch() == llvm::Triple::x86) {
770 // Win32 mangles C function names with a '_' prefix.
771 LinkerOptionFlag += '_';
772 }
773 LinkerOptionFlag += SymbolName;
774 CmdArgs.push_back(Args.MakeArgString(LinkerOptionFlag));
775}
776
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000777void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
Peter Collingbourne581f4382015-07-02 01:48:12 +0000778 llvm::opt::ArgStringList &CmdArgs,
779 types::ID InputType) const {
Justin Lebar6efbc732016-09-15 23:44:13 +0000780 // NVPTX doesn't currently support sanitizers. Bailing out here means that
781 // e.g. -fsanitize=address applies only to host code, which is what we want
782 // for now.
783 if (TC.getTriple().isNVPTX())
784 return;
785
Kostya Serebryany52e86492016-02-18 00:49:23 +0000786 // Translate available CoverageFeatures to corresponding clang-cc1 flags.
787 // Do it even if Sanitizers.empty() since some forms of coverage don't require
788 // sanitizers.
789 std::pair<int, const char *> CoverageFlags[] = {
790 std::make_pair(CoverageFunc, "-fsanitize-coverage-type=1"),
791 std::make_pair(CoverageBB, "-fsanitize-coverage-type=2"),
792 std::make_pair(CoverageEdge, "-fsanitize-coverage-type=3"),
793 std::make_pair(CoverageIndirCall, "-fsanitize-coverage-indirect-calls"),
794 std::make_pair(CoverageTraceBB, "-fsanitize-coverage-trace-bb"),
795 std::make_pair(CoverageTraceCmp, "-fsanitize-coverage-trace-cmp"),
Kostya Serebryany3b419712016-08-30 01:27:03 +0000796 std::make_pair(CoverageTraceDiv, "-fsanitize-coverage-trace-div"),
797 std::make_pair(CoverageTraceGep, "-fsanitize-coverage-trace-gep"),
Kostya Serebryany52e86492016-02-18 00:49:23 +0000798 std::make_pair(Coverage8bitCounters, "-fsanitize-coverage-8bit-counters"),
Kostya Serebryany60cdd612016-09-14 01:39:49 +0000799 std::make_pair(CoverageTracePC, "-fsanitize-coverage-trace-pc"),
Kostya Serebryany50fb6182017-05-05 23:28:18 +0000800 std::make_pair(CoverageTracePCGuard, "-fsanitize-coverage-trace-pc-guard"),
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000801 std::make_pair(CoverageInline8bitCounters, "-fsanitize-coverage-inline-8bit-counters"),
Kostya Serebryany61457762017-07-28 00:10:10 +0000802 std::make_pair(CoveragePCTable, "-fsanitize-coverage-pc-table"),
Matt Morehouse5c7fc762017-08-18 18:43:30 +0000803 std::make_pair(CoverageNoPrune, "-fsanitize-coverage-no-prune"),
804 std::make_pair(CoverageStackDepth, "-fsanitize-coverage-stack-depth")};
Kostya Serebryany52e86492016-02-18 00:49:23 +0000805 for (auto F : CoverageFlags) {
806 if (CoverageFeatures & F.first)
Evgeniy Stepanovb86ca112017-05-09 21:57:39 +0000807 CmdArgs.push_back(F.second);
Kostya Serebryany52e86492016-02-18 00:49:23 +0000808 }
809
Evgeniy Stepanov5c063c12016-06-14 23:21:19 +0000810 if (TC.getTriple().isOSWindows() && needsUbsanRt()) {
811 // Instruct the code generator to embed linker directives in the object file
812 // that cause the required runtime libraries to be linked.
813 CmdArgs.push_back(Args.MakeArgString(
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000814 "--dependent-lib=" + TC.getCompilerRT(Args, "ubsan_standalone")));
Evgeniy Stepanov5c063c12016-06-14 23:21:19 +0000815 if (types::isCXX(InputType))
816 CmdArgs.push_back(Args.MakeArgString(
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000817 "--dependent-lib=" + TC.getCompilerRT(Args, "ubsan_standalone_cxx")));
Evgeniy Stepanov5c063c12016-06-14 23:21:19 +0000818 }
819 if (TC.getTriple().isOSWindows() && needsStatsRt()) {
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000820 CmdArgs.push_back(Args.MakeArgString("--dependent-lib=" +
821 TC.getCompilerRT(Args, "stats_client")));
Evgeniy Stepanov5c063c12016-06-14 23:21:19 +0000822
823 // The main executable must export the stats runtime.
824 // FIXME: Only exporting from the main executable (e.g. based on whether the
825 // translation unit defines main()) would save a little space, but having
826 // multiple copies of the runtime shouldn't hurt.
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000827 CmdArgs.push_back(Args.MakeArgString("--dependent-lib=" +
828 TC.getCompilerRT(Args, "stats")));
Evgeniy Stepanov5c063c12016-06-14 23:21:19 +0000829 addIncludeLinkerOption(TC, Args, CmdArgs, "__sanitizer_stats_register");
830 }
831
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000832 if (Sanitizers.empty())
Alexey Samsonovcf055962013-08-08 10:11:02 +0000833 return;
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000834 CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
835
Alexey Samsonov88459522015-01-12 22:39:12 +0000836 if (!RecoverableSanitizers.empty())
837 CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
838 toString(RecoverableSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000839
Peter Collingbourne9881b782015-06-18 23:59:22 +0000840 if (!TrapSanitizers.empty())
841 CmdArgs.push_back(
842 Args.MakeArgString("-fsanitize-trap=" + toString(TrapSanitizers)));
Alexey Samsonov1e715a62014-11-16 20:53:53 +0000843
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000844 for (const auto &BLPath : BlacklistFiles) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000845 SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000846 BlacklistOpt += BLPath;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000847 CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
848 }
Ivan Krasin4c3f2372015-09-02 20:02:38 +0000849 for (const auto &Dep : ExtraDeps) {
850 SmallString<64> ExtraDepOpt("-fdepfile-entry=");
851 ExtraDepOpt += Dep;
852 CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
853 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000854
855 if (MsanTrackOrigins)
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000856 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
Benjamin Kramer3a13ed62017-12-28 16:58:54 +0000857 Twine(MsanTrackOrigins)));
Evgeniy Stepanov45be9e02015-07-10 20:07:16 +0000858
859 if (MsanUseAfterDtor)
Evgeniy Stepanovb86ca112017-05-09 21:57:39 +0000860 CmdArgs.push_back("-fsanitize-memory-use-after-dtor");
Evgeniy Stepanov45be9e02015-07-10 20:07:16 +0000861
Evgeniy Stepanov8a2bedb2016-11-11 23:17:36 +0000862 // FIXME: Pass these parameters as function attributes, not as -llvm flags.
863 if (!TsanMemoryAccess) {
864 CmdArgs.push_back("-mllvm");
865 CmdArgs.push_back("-tsan-instrument-memory-accesses=0");
866 CmdArgs.push_back("-mllvm");
867 CmdArgs.push_back("-tsan-instrument-memintrinsics=0");
868 }
869 if (!TsanFuncEntryExit) {
870 CmdArgs.push_back("-mllvm");
871 CmdArgs.push_back("-tsan-instrument-func-entry-exit=0");
872 }
873 if (!TsanAtomics) {
874 CmdArgs.push_back("-mllvm");
875 CmdArgs.push_back("-tsan-instrument-atomics=0");
876 }
877
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000878 if (CfiCrossDso)
Evgeniy Stepanovb86ca112017-05-09 21:57:39 +0000879 CmdArgs.push_back("-fsanitize-cfi-cross-dso");
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000880
Vlad Tsyrklevich634c6012017-10-31 22:39:44 +0000881 if (CfiICallGeneralizePointers)
882 CmdArgs.push_back("-fsanitize-cfi-icall-generalize-pointers");
883
Peter Collingbournedc134532016-01-16 00:31:22 +0000884 if (Stats)
Evgeniy Stepanovb86ca112017-05-09 21:57:39 +0000885 CmdArgs.push_back("-fsanitize-stats");
Peter Collingbournedc134532016-01-16 00:31:22 +0000886
Evgeniy Stepanov6d2b6f02017-08-29 20:03:51 +0000887 if (MinimalRuntime)
888 CmdArgs.push_back("-fsanitize-minimal-runtime");
889
Kostya Serebryanyaed71a82014-10-09 17:53:04 +0000890 if (AsanFieldPadding)
891 CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
Benjamin Kramer3a13ed62017-12-28 16:58:54 +0000892 Twine(AsanFieldPadding)));
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000893
Vitaly Buka9d4eb6f2016-06-02 00:24:20 +0000894 if (AsanUseAfterScope)
Evgeniy Stepanovb86ca112017-05-09 21:57:39 +0000895 CmdArgs.push_back("-fsanitize-address-use-after-scope");
Vitaly Buka9d4eb6f2016-06-02 00:24:20 +0000896
Evgeniy Stepanovd991cdd2017-05-09 21:57:43 +0000897 if (AsanGlobalsDeadStripping)
898 CmdArgs.push_back("-fsanitize-address-globals-dead-stripping");
899
Sergey Matveev2ba87782015-02-17 15:09:33 +0000900 // MSan: Workaround for PR16386.
901 // ASan: This is mainly to help LSan with cases such as
Hans Wennborg5fd1f172017-11-13 23:27:54 +0000902 // https://github.com/google/sanitizers/issues/373
Sergey Matveev2ba87782015-02-17 15:09:33 +0000903 // We can't make this conditional on -fsanitize=leak, as that flag shouldn't
904 // affect compilation.
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000905 if (Sanitizers.has(Memory) || Sanitizers.has(Address))
Evgeniy Stepanovb86ca112017-05-09 21:57:39 +0000906 CmdArgs.push_back("-fno-assume-sane-operator-new");
Peter Collingbourne581f4382015-07-02 01:48:12 +0000907
Peter Collingbourne3afb2662016-04-28 17:09:37 +0000908 // Require -fvisibility= flag on non-Windows when compiling if vptr CFI is
909 // enabled.
910 if (Sanitizers.hasOneOf(CFIClasses) && !TC.getTriple().isOSWindows() &&
911 !Args.hasArg(options::OPT_fvisibility_EQ)) {
912 TC.getDriver().Diag(clang::diag::err_drv_argument_only_allowed_with)
913 << lastArgumentForMask(TC.getDriver(), Args,
914 Sanitizers.Mask & CFIClasses)
915 << "-fvisibility=";
916 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000917}
918
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000919SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
920 bool DiagnoseErrors) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000921 assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
Alexey Samsonov88459522015-01-12 22:39:12 +0000922 A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
923 A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
Peter Collingbourne9881b782015-06-18 23:59:22 +0000924 A->getOption().matches(options::OPT_fno_sanitize_recover_EQ) ||
925 A->getOption().matches(options::OPT_fsanitize_trap_EQ) ||
926 A->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) &&
Alexey Samsonov799f7932014-12-19 02:35:16 +0000927 "Invalid argument in parseArgValues!");
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000928 SanitizerMask Kinds = 0;
Alexey Samsonov83791e22015-03-03 22:15:32 +0000929 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
930 const char *Value = A->getValue(i);
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000931 SanitizerMask Kind;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000932 // Special case: don't accept -fsanitize=all.
933 if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
934 0 == strcmp("all", Value))
935 Kind = 0;
Derek Bruening256c2e12016-04-21 21:32:04 +0000936 // Similarly, don't accept -fsanitize=efficiency-all.
937 else if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
938 0 == strcmp("efficiency-all", Value))
939 Kind = 0;
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000940 else
Peter Collingbournebf59c342015-05-11 21:39:20 +0000941 Kind = parseSanitizerValue(Value, /*AllowGroups=*/true);
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000942
943 if (Kind)
944 Kinds |= Kind;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000945 else if (DiagnoseErrors)
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000946 D.Diag(clang::diag::err_drv_unsupported_option_argument)
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000947 << A->getOption().getName() << Value;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000948 }
Alexey Samsonovabd5bea2014-12-19 18:41:43 +0000949 return Kinds;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000950}
951
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000952int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A) {
953 assert(A->getOption().matches(options::OPT_fsanitize_coverage) ||
954 A->getOption().matches(options::OPT_fno_sanitize_coverage));
955 int Features = 0;
956 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
957 const char *Value = A->getValue(i);
958 int F = llvm::StringSwitch<int>(Value)
959 .Case("func", CoverageFunc)
960 .Case("bb", CoverageBB)
961 .Case("edge", CoverageEdge)
962 .Case("indirect-calls", CoverageIndirCall)
963 .Case("trace-bb", CoverageTraceBB)
964 .Case("trace-cmp", CoverageTraceCmp)
Kostya Serebryany3b419712016-08-30 01:27:03 +0000965 .Case("trace-div", CoverageTraceDiv)
966 .Case("trace-gep", CoverageTraceGep)
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000967 .Case("8bit-counters", Coverage8bitCounters)
Kostya Serebryanyd4590c72016-02-17 21:34:43 +0000968 .Case("trace-pc", CoverageTracePC)
Kostya Serebryany60cdd612016-09-14 01:39:49 +0000969 .Case("trace-pc-guard", CoverageTracePCGuard)
Kostya Serebryany50fb6182017-05-05 23:28:18 +0000970 .Case("no-prune", CoverageNoPrune)
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000971 .Case("inline-8bit-counters", CoverageInline8bitCounters)
Kostya Serebryany61457762017-07-28 00:10:10 +0000972 .Case("pc-table", CoveragePCTable)
Matt Morehouse5c7fc762017-08-18 18:43:30 +0000973 .Case("stack-depth", CoverageStackDepth)
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000974 .Default(0);
975 if (F == 0)
976 D.Diag(clang::diag::err_drv_unsupported_option_argument)
977 << A->getOption().getName() << Value;
978 Features |= F;
979 }
980 return Features;
981}
982
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +0000983std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
Peter Collingbourne3eea6772015-05-11 21:39:14 +0000984 SanitizerMask Mask) {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000985 for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
986 E = Args.rend();
987 I != E; ++I) {
Alexey Samsonov799f7932014-12-19 02:35:16 +0000988 const auto *Arg = *I;
989 if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000990 SanitizerMask AddKinds =
991 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000992 if (AddKinds & Mask)
993 return describeSanitizeArg(Arg, Mask);
994 } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
Peter Collingbournebf59c342015-05-11 21:39:20 +0000995 SanitizerMask RemoveKinds =
996 expandSanitizerGroups(parseArgValues(D, Arg, false));
Alexey Samsonov799f7932014-12-19 02:35:16 +0000997 Mask &= ~RemoveKinds;
998 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000999 }
1000 llvm_unreachable("arg list didn't provide expected value");
1001}
1002
Peter Collingbourne3eea6772015-05-11 21:39:14 +00001003std::string describeSanitizeArg(const llvm::opt::Arg *A, SanitizerMask Mask) {
Alexey Samsonov4c12c6c2014-11-14 02:59:20 +00001004 assert(A->getOption().matches(options::OPT_fsanitize_EQ)
1005 && "Invalid argument in describeSanitizerArg!");
Alexey Samsonovcf055962013-08-08 10:11:02 +00001006
Peter Collingbourne32701642013-11-01 18:16:25 +00001007 std::string Sanitizers;
Alexey Samsonov83791e22015-03-03 22:15:32 +00001008 for (int i = 0, n = A->getNumValues(); i != n; ++i) {
Peter Collingbournebf59c342015-05-11 21:39:20 +00001009 if (expandSanitizerGroups(
1010 parseSanitizerValue(A->getValue(i), /*AllowGroups=*/true)) &
1011 Mask) {
Peter Collingbourne32701642013-11-01 18:16:25 +00001012 if (!Sanitizers.empty())
1013 Sanitizers += ",";
Alexey Samsonov83791e22015-03-03 22:15:32 +00001014 Sanitizers += A->getValue(i);
Peter Collingbourne32701642013-11-01 18:16:25 +00001015 }
1016 }
Alexey Samsonovcf055962013-08-08 10:11:02 +00001017
Peter Collingbourne32701642013-11-01 18:16:25 +00001018 assert(!Sanitizers.empty() && "arg didn't provide expected value");
1019 return "-fsanitize=" + Sanitizers;
Alexey Samsonovcf055962013-08-08 10:11:02 +00001020}