clang  16.0.0git
SanitizerArgs.cpp
Go to the documentation of this file.
1 //===--- SanitizerArgs.cpp - Arguments for sanitizer tools ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
10 #include "clang/Basic/Sanitizers.h"
11 #include "clang/Driver/Driver.h"
13 #include "clang/Driver/Options.h"
14 #include "clang/Driver/ToolChain.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/Support/AArch64TargetParser.h"
18 #include "llvm/Support/Path.h"
19 #include "llvm/Support/SpecialCaseList.h"
20 #include "llvm/Support/TargetParser.h"
21 #include "llvm/Support/VirtualFileSystem.h"
22 #include "llvm/Transforms/Instrumentation/AddressSanitizerOptions.h"
23 #include <memory>
24 
25 using namespace clang;
26 using namespace clang::driver;
27 using namespace llvm::opt;
28 
30  SanitizerKind::Undefined | SanitizerKind::Integer |
31  SanitizerKind::ImplicitConversion | SanitizerKind::Nullability |
32  SanitizerKind::CFI | SanitizerKind::FloatDivideByZero |
33  SanitizerKind::ObjCCast;
35  SanitizerKind::Vptr | SanitizerKind::CFI;
36 static const SanitizerMask NotAllowedWithTrap = SanitizerKind::Vptr;
38  SanitizerKind::Function | SanitizerKind::Vptr;
39 static const SanitizerMask RequiresPIE =
40  SanitizerKind::DataFlow | SanitizerKind::HWAddress | SanitizerKind::Scudo |
41  SanitizerKind::KCFI;
43  SanitizerKind::Address | SanitizerKind::HWAddress | SanitizerKind::Thread |
44  SanitizerKind::Memory | SanitizerKind::DataFlow;
46  SanitizerKind::Address | SanitizerKind::HWAddress |
47  SanitizerKind::KernelAddress | SanitizerKind::KernelHWAddress |
48  SanitizerKind::MemtagStack | SanitizerKind::MemtagHeap |
49  SanitizerKind::MemtagGlobals | SanitizerKind::Memory |
50  SanitizerKind::KernelMemory | SanitizerKind::Leak |
51  SanitizerKind::Undefined | SanitizerKind::Integer | SanitizerKind::Bounds |
52  SanitizerKind::ImplicitConversion | SanitizerKind::Nullability |
53  SanitizerKind::DataFlow | SanitizerKind::Fuzzer |
54  SanitizerKind::FuzzerNoLink | SanitizerKind::FloatDivideByZero |
55  SanitizerKind::SafeStack | SanitizerKind::ShadowCallStack |
56  SanitizerKind::Thread | SanitizerKind::ObjCCast | SanitizerKind::KCFI;
58  SanitizerKind::Undefined | SanitizerKind::Integer |
59  SanitizerKind::ImplicitConversion | SanitizerKind::Nullability |
60  SanitizerKind::FloatDivideByZero | SanitizerKind::ObjCCast;
62  SanitizerKind::Unreachable | SanitizerKind::Return;
63 static const SanitizerMask AlwaysRecoverable = SanitizerKind::KernelAddress |
64  SanitizerKind::KernelHWAddress |
65  SanitizerKind::KCFI;
66 static const SanitizerMask NeedsLTO = SanitizerKind::CFI;
68  (SanitizerKind::Undefined & ~SanitizerKind::Vptr) | SanitizerKind::Integer |
69  SanitizerKind::Nullability | SanitizerKind::LocalBounds |
70  SanitizerKind::CFI | SanitizerKind::FloatDivideByZero |
71  SanitizerKind::ObjCCast;
72 static const SanitizerMask TrappingDefault = SanitizerKind::CFI;
73 static const SanitizerMask CFIClasses =
74  SanitizerKind::CFIVCall | SanitizerKind::CFINVCall |
75  SanitizerKind::CFIMFCall | SanitizerKind::CFIDerivedCast |
76  SanitizerKind::CFIUnrelatedCast;
78  TrappingSupported | SanitizerKind::Scudo | SanitizerKind::ShadowCallStack |
79  SanitizerKind::MemtagStack | SanitizerKind::MemtagHeap |
80  SanitizerKind::MemtagGlobals;
81 
83  CoverageFunc = 1 << 0,
84  CoverageBB = 1 << 1,
85  CoverageEdge = 1 << 2,
87  CoverageTraceBB = 1 << 4, // Deprecated.
88  CoverageTraceCmp = 1 << 5,
89  CoverageTraceDiv = 1 << 6,
90  CoverageTraceGep = 1 << 7,
91  Coverage8bitCounters = 1 << 8, // Deprecated.
92  CoverageTracePC = 1 << 9,
94  CoverageNoPrune = 1 << 11,
96  CoveragePCTable = 1 << 13,
97  CoverageStackDepth = 1 << 14,
99  CoverageTraceLoads = 1 << 16,
102 };
103 
107 };
108 
109 /// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any
110 /// invalid components. Returns a SanitizerMask.
111 static SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
112  bool DiagnoseErrors);
113 
114 /// Parse -f(no-)?sanitize-coverage= flag values, diagnosing any invalid
115 /// components. Returns OR of members of \c CoverageFeature enumeration.
116 static int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A,
117  bool DiagnoseErrors);
118 
119 /// Parse -f(no-)?sanitize-metadata= flag values, diagnosing any invalid
120 /// components. Returns OR of members of \c BinaryMetadataFeature enumeration.
121 static int parseBinaryMetadataFeatures(const Driver &D, const llvm::opt::Arg *A,
122  bool DiagnoseErrors);
123 
124 /// Produce an argument string from ArgList \p Args, which shows how it
125 /// provides some sanitizer kind from \p Mask. For example, the argument list
126 /// "-fsanitize=thread,vptr -fsanitize=address" with mask \c NeedsUbsanRt
127 /// would produce "-fsanitize=vptr".
128 static std::string lastArgumentForMask(const Driver &D,
129  const llvm::opt::ArgList &Args,
130  SanitizerMask Mask);
131 
132 /// Produce an argument string from argument \p A, which shows how it provides
133 /// a value in \p Mask. For instance, the argument
134 /// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce
135 /// "-fsanitize=alignment".
136 static std::string describeSanitizeArg(const llvm::opt::Arg *A,
137  SanitizerMask Mask);
138 
139 /// Produce a string containing comma-separated names of sanitizers in \p
140 /// Sanitizers set.
141 static std::string toString(const clang::SanitizerSet &Sanitizers);
142 
144  std::vector<std::string> &SCLFiles,
145  unsigned MalformedSCLErrorDiagID,
146  bool DiagnoseErrors) {
147  if (SCLFiles.empty())
148  return;
149 
150  std::string BLError;
151  std::unique_ptr<llvm::SpecialCaseList> SCL(
152  llvm::SpecialCaseList::create(SCLFiles, D.getVFS(), BLError));
153  if (!SCL.get() && DiagnoseErrors)
154  D.Diag(MalformedSCLErrorDiagID) << BLError;
155 }
156 
157 static void addDefaultIgnorelists(const Driver &D, SanitizerMask Kinds,
158  std::vector<std::string> &IgnorelistFiles,
159  bool DiagnoseErrors) {
160  struct Ignorelist {
161  const char *File;
162  SanitizerMask Mask;
163  } Ignorelists[] = {{"asan_ignorelist.txt", SanitizerKind::Address},
164  {"hwasan_ignorelist.txt", SanitizerKind::HWAddress},
165  {"memtag_ignorelist.txt", SanitizerKind::MemTag},
166  {"msan_ignorelist.txt", SanitizerKind::Memory},
167  {"tsan_ignorelist.txt", SanitizerKind::Thread},
168  {"dfsan_abilist.txt", SanitizerKind::DataFlow},
169  {"cfi_ignorelist.txt", SanitizerKind::CFI},
170  {"ubsan_ignorelist.txt",
171  SanitizerKind::Undefined | SanitizerKind::Integer |
173  SanitizerKind::FloatDivideByZero}};
174 
175  for (auto BL : Ignorelists) {
176  if (!(Kinds & BL.Mask))
177  continue;
178 
180  llvm::sys::path::append(Path, "share", BL.File);
181  if (D.getVFS().exists(Path))
182  IgnorelistFiles.push_back(std::string(Path.str()));
183  else if (BL.Mask == SanitizerKind::CFI && DiagnoseErrors)
184  // If cfi_ignorelist.txt cannot be found in the resource dir, driver
185  // should fail.
186  D.Diag(clang::diag::err_drv_missing_sanitizer_ignorelist) << Path;
187  }
189  D, IgnorelistFiles, clang::diag::err_drv_malformed_sanitizer_ignorelist,
190  DiagnoseErrors);
191 }
192 
193 /// Parse -f(no-)?sanitize-(coverage-)?(allow|ignore)list argument's values,
194 /// diagnosing any invalid file paths and validating special case list format.
195 static void parseSpecialCaseListArg(const Driver &D,
196  const llvm::opt::ArgList &Args,
197  std::vector<std::string> &SCLFiles,
198  llvm::opt::OptSpecifier SCLOptionID,
199  llvm::opt::OptSpecifier NoSCLOptionID,
200  unsigned MalformedSCLErrorDiagID,
201  bool DiagnoseErrors) {
202  for (const auto *Arg : Args) {
203  // Match -fsanitize-(coverage-)?(allow|ignore)list.
204  if (Arg->getOption().matches(SCLOptionID)) {
205  Arg->claim();
206  std::string SCLPath = Arg->getValue();
207  if (D.getVFS().exists(SCLPath)) {
208  SCLFiles.push_back(SCLPath);
209  } else if (DiagnoseErrors) {
210  D.Diag(clang::diag::err_drv_no_such_file) << SCLPath;
211  }
212  // Match -fno-sanitize-ignorelist.
213  } else if (Arg->getOption().matches(NoSCLOptionID)) {
214  Arg->claim();
215  SCLFiles.clear();
216  }
217  }
218  validateSpecialCaseListFormat(D, SCLFiles, MalformedSCLErrorDiagID,
219  DiagnoseErrors);
220 }
221 
222 /// Sets group bits for every group that has at least one representative already
223 /// enabled in \p Kinds.
225 #define SANITIZER(NAME, ID)
226 #define SANITIZER_GROUP(NAME, ID, ALIAS) \
227  if (Kinds & SanitizerKind::ID) \
228  Kinds |= SanitizerKind::ID##Group;
229 #include "clang/Basic/Sanitizers.def"
230  return Kinds;
231 }
232 
234  const llvm::opt::ArgList &Args,
235  bool DiagnoseErrors) {
236  SanitizerMask TrapRemove; // During the loop below, the accumulated set of
237  // sanitizers disabled by the current sanitizer
238  // argument or any argument after it.
239  SanitizerMask TrappingKinds;
240  SanitizerMask TrappingSupportedWithGroups = setGroupBits(TrappingSupported);
241 
242  for (const llvm::opt::Arg *Arg : llvm::reverse(Args)) {
243  if (Arg->getOption().matches(options::OPT_fsanitize_trap_EQ)) {
244  Arg->claim();
245  SanitizerMask Add = parseArgValues(D, Arg, true);
246  Add &= ~TrapRemove;
247  SanitizerMask InvalidValues = Add & ~TrappingSupportedWithGroups;
248  if (InvalidValues && DiagnoseErrors) {
249  SanitizerSet S;
250  S.Mask = InvalidValues;
251  D.Diag(diag::err_drv_unsupported_option_argument)
252  << Arg->getSpelling() << toString(S);
253  }
254  TrappingKinds |= expandSanitizerGroups(Add) & ~TrapRemove;
255  } else if (Arg->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) {
256  Arg->claim();
257  TrapRemove |=
258  expandSanitizerGroups(parseArgValues(D, Arg, DiagnoseErrors));
259  }
260  }
261 
262  // Apply default trapping behavior.
263  TrappingKinds |= TrappingDefault & ~TrapRemove;
264 
265  return TrappingKinds;
266 }
267 
269  return needsFuzzer() && !needsAsanRt() && !needsTsanRt() && !needsMsanRt();
270 }
271 
273  // All of these include ubsan.
274  if (needsAsanRt() || needsMsanRt() || needsHwasanRt() || needsTsanRt() ||
275  needsDfsanRt() || needsLsanRt() || needsCfiDiagRt() ||
276  (needsScudoRt() && !requiresMinimalRuntime()))
277  return false;
278 
279  return (Sanitizers.Mask & NeedsUbsanRt & ~TrapSanitizers.Mask) ||
280  CoverageFeatures;
281 }
282 
284  return !(Sanitizers.Mask & SanitizerKind::CFI & ~TrapSanitizers.Mask) &&
285  CfiCrossDso && !ImplicitCfiRuntime;
286 }
287 
289  return (Sanitizers.Mask & SanitizerKind::CFI & ~TrapSanitizers.Mask) &&
290  CfiCrossDso && !ImplicitCfiRuntime;
291 }
292 
294  return NeedPIE || (Sanitizers.Mask & RequiresPIE);
295 }
296 
298  return static_cast<bool>(Sanitizers.Mask & NeedsUnwindTables);
299 }
300 
302  return static_cast<bool>(Sanitizers.Mask & NeedsLTO);
303 }
304 
306  const llvm::opt::ArgList &Args,
307  bool DiagnoseErrors) {
308  SanitizerMask AllRemove; // During the loop below, the accumulated set of
309  // sanitizers disabled by the current sanitizer
310  // argument or any argument after it.
311  SanitizerMask AllAddedKinds; // Mask of all sanitizers ever enabled by
312  // -fsanitize= flags (directly or via group
313  // expansion), some of which may be disabled
314  // later. Used to carefully prune
315  // unused-argument diagnostics.
316  SanitizerMask DiagnosedKinds; // All Kinds we have diagnosed up to now.
317  // Used to deduplicate diagnostics.
318  SanitizerMask Kinds;
319  const SanitizerMask Supported = setGroupBits(TC.getSupportedSanitizers());
320 
321  CfiCrossDso = Args.hasFlag(options::OPT_fsanitize_cfi_cross_dso,
322  options::OPT_fno_sanitize_cfi_cross_dso, false);
323 
324  ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
325 
326  const Driver &D = TC.getDriver();
327  SanitizerMask TrappingKinds = parseSanitizeTrapArgs(D, Args, DiagnoseErrors);
328  SanitizerMask InvalidTrappingKinds = TrappingKinds & NotAllowedWithTrap;
329 
330  MinimalRuntime =
331  Args.hasFlag(options::OPT_fsanitize_minimal_runtime,
332  options::OPT_fno_sanitize_minimal_runtime, MinimalRuntime);
333 
334  // The object size sanitizer should not be enabled at -O0.
335  Arg *OptLevel = Args.getLastArg(options::OPT_O_Group);
336  bool RemoveObjectSizeAtO0 =
337  !OptLevel || OptLevel->getOption().matches(options::OPT_O0);
338 
339  for (const llvm::opt::Arg *Arg : llvm::reverse(Args)) {
340  if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
341  Arg->claim();
342  SanitizerMask Add = parseArgValues(D, Arg, DiagnoseErrors);
343 
344  if (RemoveObjectSizeAtO0) {
345  AllRemove |= SanitizerKind::ObjectSize;
346 
347  // The user explicitly enabled the object size sanitizer. Warn
348  // that this does nothing at -O0.
349  if ((Add & SanitizerKind::ObjectSize) && DiagnoseErrors)
350  D.Diag(diag::warn_drv_object_size_disabled_O0)
351  << Arg->getAsString(Args);
352  }
353 
354  AllAddedKinds |= expandSanitizerGroups(Add);
355 
356  // Avoid diagnosing any sanitizer which is disabled later.
357  Add &= ~AllRemove;
358  // At this point we have not expanded groups, so any unsupported
359  // sanitizers in Add are those which have been explicitly enabled.
360  // Diagnose them.
361  if (SanitizerMask KindsToDiagnose =
362  Add & InvalidTrappingKinds & ~DiagnosedKinds) {
363  if (DiagnoseErrors) {
364  std::string Desc = describeSanitizeArg(Arg, KindsToDiagnose);
365  D.Diag(diag::err_drv_argument_not_allowed_with)
366  << Desc << "-fsanitize-trap=undefined";
367  }
368  DiagnosedKinds |= KindsToDiagnose;
369  }
370  Add &= ~InvalidTrappingKinds;
371 
372  if (MinimalRuntime) {
373  if (SanitizerMask KindsToDiagnose =
374  Add & NotAllowedWithMinimalRuntime & ~DiagnosedKinds) {
375  if (DiagnoseErrors) {
376  std::string Desc = describeSanitizeArg(Arg, KindsToDiagnose);
377  D.Diag(diag::err_drv_argument_not_allowed_with)
378  << Desc << "-fsanitize-minimal-runtime";
379  }
380  DiagnosedKinds |= KindsToDiagnose;
381  }
383  }
384 
385  if (llvm::opt::Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
386  StringRef CM = A->getValue();
387  if (CM != "small" &&
388  (Add & SanitizerKind::Function & ~DiagnosedKinds)) {
389  if (DiagnoseErrors)
390  D.Diag(diag::err_drv_argument_only_allowed_with)
391  << "-fsanitize=function"
392  << "-mcmodel=small";
393  Add &= ~SanitizerKind::Function;
394  DiagnosedKinds |= SanitizerKind::Function;
395  }
396  }
397 
398  // FIXME: Make CFI on member function calls compatible with cross-DSO CFI.
399  // There are currently two problems:
400  // - Virtual function call checks need to pass a pointer to the function
401  // address to llvm.type.test and a pointer to the address point to the
402  // diagnostic function. Currently we pass the same pointer to both
403  // places.
404  // - Non-virtual function call checks may need to check multiple type
405  // identifiers.
406  // Fixing both of those may require changes to the cross-DSO CFI
407  // interface.
408  if (CfiCrossDso && (Add & SanitizerKind::CFIMFCall & ~DiagnosedKinds)) {
409  if (DiagnoseErrors)
410  D.Diag(diag::err_drv_argument_not_allowed_with)
411  << "-fsanitize=cfi-mfcall"
412  << "-fsanitize-cfi-cross-dso";
413  Add &= ~SanitizerKind::CFIMFCall;
414  DiagnosedKinds |= SanitizerKind::CFIMFCall;
415  }
416 
417  if (SanitizerMask KindsToDiagnose = Add & ~Supported & ~DiagnosedKinds) {
418  if (DiagnoseErrors) {
419  std::string Desc = describeSanitizeArg(Arg, KindsToDiagnose);
420  D.Diag(diag::err_drv_unsupported_opt_for_target)
421  << Desc << TC.getTriple().str();
422  }
423  DiagnosedKinds |= KindsToDiagnose;
424  }
425  Add &= Supported;
426 
427  // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups
428  // so we don't error out if -fno-rtti and -fsanitize=undefined were
429  // passed.
430  if ((Add & SanitizerKind::Vptr) && (RTTIMode == ToolChain::RM_Disabled)) {
431  if (const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg()) {
432  assert(NoRTTIArg->getOption().matches(options::OPT_fno_rtti) &&
433  "RTTI disabled without -fno-rtti option?");
434  // The user explicitly passed -fno-rtti with -fsanitize=vptr, but
435  // the vptr sanitizer requires RTTI, so this is a user error.
436  if (DiagnoseErrors)
437  D.Diag(diag::err_drv_argument_not_allowed_with)
438  << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args);
439  } else {
440  // The vptr sanitizer requires RTTI, but RTTI is disabled (by
441  // default). Warn that the vptr sanitizer is being disabled.
442  if (DiagnoseErrors)
443  D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default);
444  }
445 
446  // Take out the Vptr sanitizer from the enabled sanitizers
447  AllRemove |= SanitizerKind::Vptr;
448  }
449 
451  // Group expansion may have enabled a sanitizer which is disabled later.
452  Add &= ~AllRemove;
453  // Silently discard any unsupported sanitizers implicitly enabled through
454  // group expansion.
455  Add &= ~InvalidTrappingKinds;
456  if (MinimalRuntime) {
458  }
459  if (CfiCrossDso)
460  Add &= ~SanitizerKind::CFIMFCall;
461  Add &= Supported;
462 
463  if (Add & SanitizerKind::Fuzzer)
464  Add |= SanitizerKind::FuzzerNoLink;
465 
466  // Enable coverage if the fuzzing flag is set.
467  if (Add & SanitizerKind::FuzzerNoLink) {
468  CoverageFeatures |= CoverageInline8bitCounters | CoverageIndirCall |
470  // Due to TLS differences, stack depth tracking is only enabled on Linux
471  if (TC.getTriple().isOSLinux())
472  CoverageFeatures |= CoverageStackDepth;
473  }
474 
475  Kinds |= Add;
476  } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
477  Arg->claim();
478  SanitizerMask Remove = parseArgValues(D, Arg, DiagnoseErrors);
479  AllRemove |= expandSanitizerGroups(Remove);
480  }
481  }
482 
483  std::pair<SanitizerMask, SanitizerMask> IncompatibleGroups[] = {
484  std::make_pair(SanitizerKind::Address,
485  SanitizerKind::Thread | SanitizerKind::Memory),
486  std::make_pair(SanitizerKind::Thread, SanitizerKind::Memory),
487  std::make_pair(SanitizerKind::Leak,
488  SanitizerKind::Thread | SanitizerKind::Memory),
489  std::make_pair(SanitizerKind::KernelAddress,
490  SanitizerKind::Address | SanitizerKind::Leak |
491  SanitizerKind::Thread | SanitizerKind::Memory),
492  std::make_pair(SanitizerKind::HWAddress,
493  SanitizerKind::Address | SanitizerKind::Thread |
494  SanitizerKind::Memory | SanitizerKind::KernelAddress),
495  std::make_pair(SanitizerKind::Scudo,
496  SanitizerKind::Address | SanitizerKind::HWAddress |
497  SanitizerKind::Leak | SanitizerKind::Thread |
498  SanitizerKind::Memory | SanitizerKind::KernelAddress),
499  std::make_pair(SanitizerKind::SafeStack,
500  (TC.getTriple().isOSFuchsia() ? SanitizerMask()
501  : SanitizerKind::Leak) |
502  SanitizerKind::Address | SanitizerKind::HWAddress |
503  SanitizerKind::Thread | SanitizerKind::Memory |
504  SanitizerKind::KernelAddress),
505  std::make_pair(SanitizerKind::KernelHWAddress,
506  SanitizerKind::Address | SanitizerKind::HWAddress |
507  SanitizerKind::Leak | SanitizerKind::Thread |
508  SanitizerKind::Memory | SanitizerKind::KernelAddress |
509  SanitizerKind::SafeStack),
510  std::make_pair(SanitizerKind::KernelMemory,
511  SanitizerKind::Address | SanitizerKind::HWAddress |
512  SanitizerKind::Leak | SanitizerKind::Thread |
513  SanitizerKind::Memory | SanitizerKind::KernelAddress |
514  SanitizerKind::Scudo | SanitizerKind::SafeStack),
515  std::make_pair(SanitizerKind::MemTag,
516  SanitizerKind::Address | SanitizerKind::KernelAddress |
517  SanitizerKind::HWAddress |
518  SanitizerKind::KernelHWAddress)};
519  // Enable toolchain specific default sanitizers if not explicitly disabled.
520  SanitizerMask Default = TC.getDefaultSanitizers() & ~AllRemove;
521 
522  // Disable default sanitizers that are incompatible with explicitly requested
523  // ones.
524  for (auto G : IncompatibleGroups) {
525  SanitizerMask Group = G.first;
526  if ((Default & Group) && (Kinds & G.second))
527  Default &= ~Group;
528  }
529 
530  Kinds |= Default;
531 
532  // We disable the vptr sanitizer if it was enabled by group expansion but RTTI
533  // is disabled.
534  if ((Kinds & SanitizerKind::Vptr) && (RTTIMode == ToolChain::RM_Disabled)) {
535  Kinds &= ~SanitizerKind::Vptr;
536  }
537 
538  // Check that LTO is enabled if we need it.
539  if ((Kinds & NeedsLTO) && !D.isUsingLTO() && DiagnoseErrors) {
540  D.Diag(diag::err_drv_argument_only_allowed_with)
541  << lastArgumentForMask(D, Args, Kinds & NeedsLTO) << "-flto";
542  }
543 
544  if ((Kinds & SanitizerKind::ShadowCallStack) &&
545  ((TC.getTriple().isAArch64() &&
546  !llvm::AArch64::isX18ReservedByDefault(TC.getTriple())) ||
547  TC.getTriple().isRISCV()) &&
548  !Args.hasArg(options::OPT_ffixed_x18) && DiagnoseErrors) {
549  D.Diag(diag::err_drv_argument_only_allowed_with)
550  << lastArgumentForMask(D, Args, Kinds & SanitizerKind::ShadowCallStack)
551  << "-ffixed-x18";
552  }
553 
554  // Report error if there are non-trapping sanitizers that require
555  // c++abi-specific parts of UBSan runtime, and they are not provided by the
556  // toolchain. We don't have a good way to check the latter, so we just
557  // check if the toolchan supports vptr.
558  if (~Supported & SanitizerKind::Vptr) {
559  SanitizerMask KindsToDiagnose = Kinds & ~TrappingKinds & NeedsUbsanCxxRt;
560  // The runtime library supports the Microsoft C++ ABI, but only well enough
561  // for CFI. FIXME: Remove this once we support vptr on Windows.
562  if (TC.getTriple().isOSWindows())
563  KindsToDiagnose &= ~SanitizerKind::CFI;
564  if (KindsToDiagnose) {
565  SanitizerSet S;
566  S.Mask = KindsToDiagnose;
567  if (DiagnoseErrors)
568  D.Diag(diag::err_drv_unsupported_opt_for_target)
569  << ("-fno-sanitize-trap=" + toString(S)) << TC.getTriple().str();
570  Kinds &= ~KindsToDiagnose;
571  }
572  }
573 
574  // Warn about incompatible groups of sanitizers.
575  for (auto G : IncompatibleGroups) {
576  SanitizerMask Group = G.first;
577  if (Kinds & Group) {
578  if (SanitizerMask Incompatible = Kinds & G.second) {
579  if (DiagnoseErrors)
580  D.Diag(clang::diag::err_drv_argument_not_allowed_with)
581  << lastArgumentForMask(D, Args, Group)
582  << lastArgumentForMask(D, Args, Incompatible);
583  Kinds &= ~Incompatible;
584  }
585  }
586  }
587  // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
588  // -fsanitize=address. Perhaps it should print an error, or perhaps
589  // -f(-no)sanitize=leak should change whether leak detection is enabled by
590  // default in ASan?
591 
592  // Parse -f(no-)?sanitize-recover flags.
594  SanitizerMask DiagnosedUnrecoverableKinds;
595  SanitizerMask DiagnosedAlwaysRecoverableKinds;
596  for (const auto *Arg : Args) {
597  if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
598  SanitizerMask Add = parseArgValues(D, Arg, DiagnoseErrors);
599  // Report error if user explicitly tries to recover from unrecoverable
600  // sanitizer.
601  if (SanitizerMask KindsToDiagnose =
602  Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
603  SanitizerSet SetToDiagnose;
604  SetToDiagnose.Mask |= KindsToDiagnose;
605  if (DiagnoseErrors)
606  D.Diag(diag::err_drv_unsupported_option_argument)
607  << Arg->getSpelling() << toString(SetToDiagnose);
608  DiagnosedUnrecoverableKinds |= KindsToDiagnose;
609  }
610  RecoverableKinds |= expandSanitizerGroups(Add);
611  Arg->claim();
612  } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
613  SanitizerMask Remove = parseArgValues(D, Arg, DiagnoseErrors);
614  // Report error if user explicitly tries to disable recovery from
615  // always recoverable sanitizer.
616  if (SanitizerMask KindsToDiagnose =
617  Remove & AlwaysRecoverable & ~DiagnosedAlwaysRecoverableKinds) {
618  SanitizerSet SetToDiagnose;
619  SetToDiagnose.Mask |= KindsToDiagnose;
620  if (DiagnoseErrors)
621  D.Diag(diag::err_drv_unsupported_option_argument)
622  << Arg->getSpelling() << toString(SetToDiagnose);
623  DiagnosedAlwaysRecoverableKinds |= KindsToDiagnose;
624  }
625  RecoverableKinds &= ~expandSanitizerGroups(Remove);
626  Arg->claim();
627  }
628  }
629  RecoverableKinds &= Kinds;
630  RecoverableKinds &= ~Unrecoverable;
631 
632  TrappingKinds &= Kinds;
633  RecoverableKinds &= ~TrappingKinds;
634 
635  // Setup ignorelist files.
636  // Add default ignorelist from resource directory for activated sanitizers,
637  // and validate special case lists format.
638  if (!Args.hasArgNoClaim(options::OPT_fno_sanitize_ignorelist))
639  addDefaultIgnorelists(D, Kinds, SystemIgnorelistFiles, DiagnoseErrors);
640 
641  // Parse -f(no-)?sanitize-ignorelist options.
642  // This also validates special case lists format.
644  D, Args, UserIgnorelistFiles, options::OPT_fsanitize_ignorelist_EQ,
645  options::OPT_fno_sanitize_ignorelist,
646  clang::diag::err_drv_malformed_sanitizer_ignorelist, DiagnoseErrors);
647 
648  // Parse -f[no-]sanitize-memory-track-origins[=level] options.
649  if (AllAddedKinds & SanitizerKind::Memory) {
650  if (Arg *A =
651  Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
652  options::OPT_fsanitize_memory_track_origins,
653  options::OPT_fno_sanitize_memory_track_origins)) {
654  if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
655  MsanTrackOrigins = 2;
656  } else if (A->getOption().matches(
657  options::OPT_fno_sanitize_memory_track_origins)) {
658  MsanTrackOrigins = 0;
659  } else {
660  StringRef S = A->getValue();
661  if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
662  MsanTrackOrigins > 2) {
663  if (DiagnoseErrors)
664  D.Diag(clang::diag::err_drv_invalid_value)
665  << A->getAsString(Args) << S;
666  }
667  }
668  }
669  MsanUseAfterDtor = Args.hasFlag(
670  options::OPT_fsanitize_memory_use_after_dtor,
671  options::OPT_fno_sanitize_memory_use_after_dtor, MsanUseAfterDtor);
672  MsanParamRetval = Args.hasFlag(
673  options::OPT_fsanitize_memory_param_retval,
674  options::OPT_fno_sanitize_memory_param_retval, MsanParamRetval);
675  NeedPIE |= !(TC.getTriple().isOSLinux() &&
676  TC.getTriple().getArch() == llvm::Triple::x86_64);
677  } else if (AllAddedKinds & SanitizerKind::KernelMemory) {
678  MsanUseAfterDtor = false;
679  MsanParamRetval = Args.hasFlag(
680  options::OPT_fsanitize_memory_param_retval,
681  options::OPT_fno_sanitize_memory_param_retval, MsanParamRetval);
682  } else {
683  MsanUseAfterDtor = false;
684  MsanParamRetval = false;
685  }
686 
687  if (AllAddedKinds & SanitizerKind::MemTag) {
688  StringRef S =
689  Args.getLastArgValue(options::OPT_fsanitize_memtag_mode_EQ, "sync");
690  if (S == "async" || S == "sync") {
691  MemtagMode = S.str();
692  } else {
693  D.Diag(clang::diag::err_drv_invalid_value_with_suggestion)
694  << "-fsanitize-memtag-mode=" << S << "{async, sync}";
695  MemtagMode = "sync";
696  }
697  }
698 
699  if (AllAddedKinds & SanitizerKind::Thread) {
700  TsanMemoryAccess = Args.hasFlag(
701  options::OPT_fsanitize_thread_memory_access,
702  options::OPT_fno_sanitize_thread_memory_access, TsanMemoryAccess);
703  TsanFuncEntryExit = Args.hasFlag(
704  options::OPT_fsanitize_thread_func_entry_exit,
705  options::OPT_fno_sanitize_thread_func_entry_exit, TsanFuncEntryExit);
706  TsanAtomics =
707  Args.hasFlag(options::OPT_fsanitize_thread_atomics,
708  options::OPT_fno_sanitize_thread_atomics, TsanAtomics);
709  }
710 
711  if (AllAddedKinds & SanitizerKind::CFI) {
712  // Without PIE, external function address may resolve to a PLT record, which
713  // can not be verified by the target module.
714  NeedPIE |= CfiCrossDso;
715  CfiICallGeneralizePointers =
716  Args.hasArg(options::OPT_fsanitize_cfi_icall_generalize_pointers);
717 
718  if (CfiCrossDso && CfiICallGeneralizePointers && DiagnoseErrors)
719  D.Diag(diag::err_drv_argument_not_allowed_with)
720  << "-fsanitize-cfi-cross-dso"
721  << "-fsanitize-cfi-icall-generalize-pointers";
722 
723  CfiCanonicalJumpTables =
724  Args.hasFlag(options::OPT_fsanitize_cfi_canonical_jump_tables,
725  options::OPT_fno_sanitize_cfi_canonical_jump_tables, true);
726  }
727 
728  if (AllAddedKinds & SanitizerKind::KCFI && DiagnoseErrors) {
729  if (AllAddedKinds & SanitizerKind::CFI)
730  D.Diag(diag::err_drv_argument_not_allowed_with)
731  << "-fsanitize=kcfi"
732  << lastArgumentForMask(D, Args, SanitizerKind::CFI);
733  }
734 
735  Stats = Args.hasFlag(options::OPT_fsanitize_stats,
736  options::OPT_fno_sanitize_stats, false);
737 
738  if (MinimalRuntime) {
739  SanitizerMask IncompatibleMask =
741  if (IncompatibleMask && DiagnoseErrors)
742  D.Diag(clang::diag::err_drv_argument_not_allowed_with)
743  << "-fsanitize-minimal-runtime"
744  << lastArgumentForMask(D, Args, IncompatibleMask);
745 
746  SanitizerMask NonTrappingCfi = Kinds & SanitizerKind::CFI & ~TrappingKinds;
747  if (NonTrappingCfi && DiagnoseErrors)
748  D.Diag(clang::diag::err_drv_argument_only_allowed_with)
749  << "fsanitize-minimal-runtime"
750  << "fsanitize-trap=cfi";
751  }
752 
753  // Parse -f(no-)?sanitize-coverage flags if coverage is supported by the
754  // enabled sanitizers.
755  for (const auto *Arg : Args) {
756  if (Arg->getOption().matches(options::OPT_fsanitize_coverage)) {
757  int LegacySanitizeCoverage;
758  if (Arg->getNumValues() == 1 &&
759  !StringRef(Arg->getValue(0))
760  .getAsInteger(0, LegacySanitizeCoverage)) {
761  CoverageFeatures = 0;
762  Arg->claim();
763  if (LegacySanitizeCoverage != 0 && DiagnoseErrors) {
764  D.Diag(diag::warn_drv_deprecated_arg)
765  << Arg->getAsString(Args) << "-fsanitize-coverage=trace-pc-guard";
766  }
767  continue;
768  }
769  CoverageFeatures |= parseCoverageFeatures(D, Arg, DiagnoseErrors);
770 
771  // Disable coverage and not claim the flags if there is at least one
772  // non-supporting sanitizer.
773  if (!(AllAddedKinds & ~AllRemove & ~setGroupBits(SupportsCoverage))) {
774  Arg->claim();
775  } else {
776  CoverageFeatures = 0;
777  }
778  } else if (Arg->getOption().matches(options::OPT_fno_sanitize_coverage)) {
779  Arg->claim();
780  CoverageFeatures &= ~parseCoverageFeatures(D, Arg, DiagnoseErrors);
781  }
782  }
783  // Choose at most one coverage type: function, bb, or edge.
784  if (DiagnoseErrors) {
785  if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageBB))
786  D.Diag(clang::diag::err_drv_argument_not_allowed_with)
787  << "-fsanitize-coverage=func"
788  << "-fsanitize-coverage=bb";
789  if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageEdge))
790  D.Diag(clang::diag::err_drv_argument_not_allowed_with)
791  << "-fsanitize-coverage=func"
792  << "-fsanitize-coverage=edge";
793  if ((CoverageFeatures & CoverageBB) && (CoverageFeatures & CoverageEdge))
794  D.Diag(clang::diag::err_drv_argument_not_allowed_with)
795  << "-fsanitize-coverage=bb"
796  << "-fsanitize-coverage=edge";
797  // Basic block tracing and 8-bit counters require some type of coverage
798  // enabled.
799  if (CoverageFeatures & CoverageTraceBB)
800  D.Diag(clang::diag::warn_drv_deprecated_arg)
801  << "-fsanitize-coverage=trace-bb"
802  << "-fsanitize-coverage=trace-pc-guard";
803  if (CoverageFeatures & Coverage8bitCounters)
804  D.Diag(clang::diag::warn_drv_deprecated_arg)
805  << "-fsanitize-coverage=8bit-counters"
806  << "-fsanitize-coverage=trace-pc-guard";
807  }
808 
809  int InsertionPointTypes = CoverageFunc | CoverageBB | CoverageEdge;
810  int InstrumentationTypes = CoverageTracePC | CoverageTracePCGuard |
814  if ((CoverageFeatures & InsertionPointTypes) &&
815  !(CoverageFeatures & InstrumentationTypes) && DiagnoseErrors) {
816  D.Diag(clang::diag::warn_drv_deprecated_arg)
817  << "-fsanitize-coverage=[func|bb|edge]"
818  << "-fsanitize-coverage=[func|bb|edge],[trace-pc-guard|trace-pc],["
819  "control-flow]";
820  }
821 
822  // trace-pc w/o func/bb/edge implies edge.
823  if (!(CoverageFeatures & InsertionPointTypes)) {
824  if (CoverageFeatures &
827  CoverageFeatures |= CoverageEdge;
828 
829  if (CoverageFeatures & CoverageStackDepth)
830  CoverageFeatures |= CoverageFunc;
831  }
832 
833  // Parse -fsanitize-coverage-(allow|ignore)list options if coverage enabled.
834  // This also validates special case lists format.
835  // Here, OptSpecifier() acts as a never-matching command-line argument.
836  // So, there is no way to clear coverage lists but you can append to them.
837  if (CoverageFeatures) {
839  D, Args, CoverageAllowlistFiles,
840  options::OPT_fsanitize_coverage_allowlist, OptSpecifier(),
841  clang::diag::err_drv_malformed_sanitizer_coverage_allowlist,
842  DiagnoseErrors);
844  D, Args, CoverageIgnorelistFiles,
845  options::OPT_fsanitize_coverage_ignorelist, OptSpecifier(),
846  clang::diag::err_drv_malformed_sanitizer_coverage_ignorelist,
847  DiagnoseErrors);
848  }
849 
850  // Parse -f(no-)?sanitize-metadata.
851  for (const auto *Arg :
852  Args.filtered(options::OPT_fexperimental_sanitize_metadata_EQ,
853  options::OPT_fno_experimental_sanitize_metadata_EQ)) {
854  if (Arg->getOption().matches(
855  options::OPT_fexperimental_sanitize_metadata_EQ)) {
856  Arg->claim();
857  BinaryMetadataFeatures |=
858  parseBinaryMetadataFeatures(D, Arg, DiagnoseErrors);
859  } else {
860  Arg->claim();
861  BinaryMetadataFeatures &=
862  ~parseBinaryMetadataFeatures(D, Arg, DiagnoseErrors);
863  }
864  }
865 
866  SharedRuntime =
867  Args.hasFlag(options::OPT_shared_libsan, options::OPT_static_libsan,
868  TC.getTriple().isAndroid() || TC.getTriple().isOSFuchsia() ||
869  TC.getTriple().isOSDarwin());
870 
871  ImplicitCfiRuntime = TC.getTriple().isAndroid();
872 
873  if (AllAddedKinds & SanitizerKind::Address) {
874  NeedPIE |= TC.getTriple().isOSFuchsia();
875  if (Arg *A =
876  Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
877  StringRef S = A->getValue();
878  // Legal values are 0 and 1, 2, but in future we may add more levels.
879  if ((S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
880  AsanFieldPadding > 2) &&
881  DiagnoseErrors) {
882  D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
883  }
884  }
885 
886  if (Arg *WindowsDebugRTArg =
887  Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
888  options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
889  options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
890  switch (WindowsDebugRTArg->getOption().getID()) {
891  case options::OPT__SLASH_MTd:
892  case options::OPT__SLASH_MDd:
893  case options::OPT__SLASH_LDd:
894  if (DiagnoseErrors) {
895  D.Diag(clang::diag::err_drv_argument_not_allowed_with)
896  << WindowsDebugRTArg->getAsString(Args)
897  << lastArgumentForMask(D, Args, SanitizerKind::Address);
898  D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
899  }
900  }
901  }
902 
903  AsanUseAfterScope = Args.hasFlag(
904  options::OPT_fsanitize_address_use_after_scope,
905  options::OPT_fno_sanitize_address_use_after_scope, AsanUseAfterScope);
906 
907  AsanPoisonCustomArrayCookie = Args.hasFlag(
908  options::OPT_fsanitize_address_poison_custom_array_cookie,
909  options::OPT_fno_sanitize_address_poison_custom_array_cookie,
910  AsanPoisonCustomArrayCookie);
911 
912  AsanOutlineInstrumentation =
913  Args.hasFlag(options::OPT_fsanitize_address_outline_instrumentation,
914  options::OPT_fno_sanitize_address_outline_instrumentation,
915  AsanOutlineInstrumentation);
916 
917  // As a workaround for a bug in gold 2.26 and earlier, dead stripping of
918  // globals in ASan is disabled by default on most ELF targets.
919  // See https://sourceware.org/bugzilla/show_bug.cgi?id=19002
920  AsanGlobalsDeadStripping = Args.hasFlag(
921  options::OPT_fsanitize_address_globals_dead_stripping,
922  options::OPT_fno_sanitize_address_globals_dead_stripping,
923  !TC.getTriple().isOSBinFormatELF() || TC.getTriple().isOSFuchsia() ||
924  TC.getTriple().isPS());
925 
926  // Enable ODR indicators which allow better handling of mixed instrumented
927  // and uninstrumented globals. Disable them for Windows where weak odr
928  // indicators (.weak.__odr_asan_gen*) may cause multiple definition linker
929  // errors in the absence of -lldmingw.
930  AsanUseOdrIndicator =
931  Args.hasFlag(options::OPT_fsanitize_address_use_odr_indicator,
932  options::OPT_fno_sanitize_address_use_odr_indicator,
933  !TC.getTriple().isOSWindows());
934 
935  if (AllAddedKinds & SanitizerKind::PointerCompare & ~AllRemove) {
936  AsanInvalidPointerCmp = true;
937  }
938 
939  if (AllAddedKinds & SanitizerKind::PointerSubtract & ~AllRemove) {
940  AsanInvalidPointerSub = true;
941  }
942 
943  if (TC.getTriple().isOSDarwin() &&
944  (Args.hasArg(options::OPT_mkernel) ||
945  Args.hasArg(options::OPT_fapple_kext))) {
946  AsanDtorKind = llvm::AsanDtorKind::None;
947  }
948 
949  if (const auto *Arg =
950  Args.getLastArg(options::OPT_sanitize_address_destructor_EQ)) {
951  auto parsedAsanDtorKind = AsanDtorKindFromString(Arg->getValue());
952  if (parsedAsanDtorKind == llvm::AsanDtorKind::Invalid && DiagnoseErrors) {
953  TC.getDriver().Diag(clang::diag::err_drv_unsupported_option_argument)
954  << Arg->getSpelling() << Arg->getValue();
955  }
956  AsanDtorKind = parsedAsanDtorKind;
957  }
958 
959  if (const auto *Arg = Args.getLastArg(
960  options::OPT_sanitize_address_use_after_return_EQ)) {
961  auto parsedAsanUseAfterReturn =
963  if (parsedAsanUseAfterReturn ==
965  DiagnoseErrors) {
966  TC.getDriver().Diag(clang::diag::err_drv_unsupported_option_argument)
967  << Arg->getSpelling() << Arg->getValue();
968  }
969  AsanUseAfterReturn = parsedAsanUseAfterReturn;
970  }
971 
972  } else {
973  AsanUseAfterScope = false;
974  // -fsanitize=pointer-compare/pointer-subtract requires -fsanitize=address.
975  SanitizerMask DetectInvalidPointerPairs =
976  SanitizerKind::PointerCompare | SanitizerKind::PointerSubtract;
977  if ((AllAddedKinds & DetectInvalidPointerPairs & ~AllRemove) &&
978  DiagnoseErrors) {
979  TC.getDriver().Diag(clang::diag::err_drv_argument_only_allowed_with)
980  << lastArgumentForMask(D, Args,
981  SanitizerKind::PointerCompare |
982  SanitizerKind::PointerSubtract)
983  << "-fsanitize=address";
984  }
985  }
986 
987  if (AllAddedKinds & SanitizerKind::HWAddress) {
988  if (Arg *HwasanAbiArg =
989  Args.getLastArg(options::OPT_fsanitize_hwaddress_abi_EQ)) {
990  HwasanAbi = HwasanAbiArg->getValue();
991  if (HwasanAbi != "platform" && HwasanAbi != "interceptor" &&
992  DiagnoseErrors)
993  D.Diag(clang::diag::err_drv_invalid_value)
994  << HwasanAbiArg->getAsString(Args) << HwasanAbi;
995  } else {
996  HwasanAbi = "interceptor";
997  }
998  if (TC.getTriple().getArch() == llvm::Triple::x86_64)
999  HwasanUseAliases = Args.hasFlag(
1000  options::OPT_fsanitize_hwaddress_experimental_aliasing,
1001  options::OPT_fno_sanitize_hwaddress_experimental_aliasing,
1002  HwasanUseAliases);
1003  }
1004 
1005  if (AllAddedKinds & SanitizerKind::SafeStack) {
1006  // SafeStack runtime is built into the system on Android and Fuchsia.
1007  SafeStackRuntime =
1008  !TC.getTriple().isAndroid() && !TC.getTriple().isOSFuchsia();
1009  }
1010 
1011  LinkRuntimes =
1012  Args.hasFlag(options::OPT_fsanitize_link_runtime,
1013  options::OPT_fno_sanitize_link_runtime, LinkRuntimes);
1014 
1015  // Parse -link-cxx-sanitizer flag.
1016  LinkCXXRuntimes = Args.hasArg(options::OPT_fsanitize_link_cxx_runtime,
1017  options::OPT_fno_sanitize_link_cxx_runtime,
1018  LinkCXXRuntimes) ||
1019  D.CCCIsCXX();
1020 
1021  NeedsMemProfRt = Args.hasFlag(options::OPT_fmemory_profile,
1022  options::OPT_fmemory_profile_EQ,
1023  options::OPT_fno_memory_profile, false);
1024 
1025  // Finally, initialize the set of available and recoverable sanitizers.
1026  Sanitizers.Mask |= Kinds;
1027  RecoverableSanitizers.Mask |= RecoverableKinds;
1028  TrapSanitizers.Mask |= TrappingKinds;
1029  assert(!(RecoverableKinds & TrappingKinds) &&
1030  "Overlap between recoverable and trapping sanitizers");
1031 }
1032 
1033 static std::string toString(const clang::SanitizerSet &Sanitizers) {
1034  std::string Res;
1035 #define SANITIZER(NAME, ID) \
1036  if (Sanitizers.has(SanitizerKind::ID)) { \
1037  if (!Res.empty()) \
1038  Res += ","; \
1039  Res += NAME; \
1040  }
1041 #include "clang/Basic/Sanitizers.def"
1042  return Res;
1043 }
1044 
1045 static void addSpecialCaseListOpt(const llvm::opt::ArgList &Args,
1046  llvm::opt::ArgStringList &CmdArgs,
1047  const char *SCLOptFlag,
1048  const std::vector<std::string> &SCLFiles) {
1049  for (const auto &SCLPath : SCLFiles) {
1050  SmallString<64> SCLOpt(SCLOptFlag);
1051  SCLOpt += SCLPath;
1052  CmdArgs.push_back(Args.MakeArgString(SCLOpt));
1053  }
1054 }
1055 
1056 static void addIncludeLinkerOption(const ToolChain &TC,
1057  const llvm::opt::ArgList &Args,
1058  llvm::opt::ArgStringList &CmdArgs,
1059  StringRef SymbolName) {
1060  SmallString<64> LinkerOptionFlag;
1061  LinkerOptionFlag = "--linker-option=/include:";
1062  if (TC.getTriple().getArch() == llvm::Triple::x86) {
1063  // Win32 mangles C function names with a '_' prefix.
1064  LinkerOptionFlag += '_';
1065  }
1066  LinkerOptionFlag += SymbolName;
1067  CmdArgs.push_back(Args.MakeArgString(LinkerOptionFlag));
1068 }
1069 
1070 static bool hasTargetFeatureMTE(const llvm::opt::ArgStringList &CmdArgs) {
1071  for (auto Start = CmdArgs.begin(), End = CmdArgs.end(); Start != End;
1072  ++Start) {
1073  auto It = std::find(Start, End, StringRef("+mte"));
1074  if (It == End)
1075  break;
1076  if (It > Start && *std::prev(It) == StringRef("-target-feature"))
1077  return true;
1078  Start = It;
1079  }
1080  return false;
1081 }
1082 
1083 void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
1084  llvm::opt::ArgStringList &CmdArgs,
1085  types::ID InputType) const {
1086  // NVPTX doesn't currently support sanitizers. Bailing out here means
1087  // that e.g. -fsanitize=address applies only to host code, which is what we
1088  // want for now.
1089  //
1090  // AMDGPU sanitizer support is experimental and controlled by -fgpu-sanitize.
1091  if (TC.getTriple().isNVPTX() ||
1092  (TC.getTriple().isAMDGPU() &&
1093  !Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize,
1094  true)))
1095  return;
1096 
1097  // Translate available CoverageFeatures to corresponding clang-cc1 flags.
1098  // Do it even if Sanitizers.empty() since some forms of coverage don't require
1099  // sanitizers.
1100  std::pair<int, const char *> CoverageFlags[] = {
1101  std::make_pair(CoverageFunc, "-fsanitize-coverage-type=1"),
1102  std::make_pair(CoverageBB, "-fsanitize-coverage-type=2"),
1103  std::make_pair(CoverageEdge, "-fsanitize-coverage-type=3"),
1104  std::make_pair(CoverageIndirCall, "-fsanitize-coverage-indirect-calls"),
1105  std::make_pair(CoverageTraceBB, "-fsanitize-coverage-trace-bb"),
1106  std::make_pair(CoverageTraceCmp, "-fsanitize-coverage-trace-cmp"),
1107  std::make_pair(CoverageTraceDiv, "-fsanitize-coverage-trace-div"),
1108  std::make_pair(CoverageTraceGep, "-fsanitize-coverage-trace-gep"),
1109  std::make_pair(Coverage8bitCounters, "-fsanitize-coverage-8bit-counters"),
1110  std::make_pair(CoverageTracePC, "-fsanitize-coverage-trace-pc"),
1111  std::make_pair(CoverageTracePCGuard,
1112  "-fsanitize-coverage-trace-pc-guard"),
1113  std::make_pair(CoverageInline8bitCounters,
1114  "-fsanitize-coverage-inline-8bit-counters"),
1115  std::make_pair(CoverageInlineBoolFlag,
1116  "-fsanitize-coverage-inline-bool-flag"),
1117  std::make_pair(CoveragePCTable, "-fsanitize-coverage-pc-table"),
1118  std::make_pair(CoverageNoPrune, "-fsanitize-coverage-no-prune"),
1119  std::make_pair(CoverageStackDepth, "-fsanitize-coverage-stack-depth"),
1120  std::make_pair(CoverageTraceLoads, "-fsanitize-coverage-trace-loads"),
1121  std::make_pair(CoverageTraceStores, "-fsanitize-coverage-trace-stores"),
1122  std::make_pair(CoverageControlFlow, "-fsanitize-coverage-control-flow")};
1123  for (auto F : CoverageFlags) {
1124  if (CoverageFeatures & F.first)
1125  CmdArgs.push_back(F.second);
1126  }
1128  Args, CmdArgs, "-fsanitize-coverage-allowlist=", CoverageAllowlistFiles);
1129  addSpecialCaseListOpt(Args, CmdArgs, "-fsanitize-coverage-ignorelist=",
1130  CoverageIgnorelistFiles);
1131 
1132  // Translate available BinaryMetadataFeatures to corresponding clang-cc1
1133  // flags. Does not depend on any other sanitizers.
1134  const std::pair<int, std::string> BinaryMetadataFlags[] = {
1135  std::make_pair(BinaryMetadataCovered, "covered"),
1136  std::make_pair(BinaryMetadataAtomics, "atomics")};
1137  for (const auto &F : BinaryMetadataFlags) {
1138  if (BinaryMetadataFeatures & F.first)
1139  CmdArgs.push_back(
1140  Args.MakeArgString("-fexperimental-sanitize-metadata=" + F.second));
1141  }
1142 
1143  if (TC.getTriple().isOSWindows() && needsUbsanRt()) {
1144  // Instruct the code generator to embed linker directives in the object file
1145  // that cause the required runtime libraries to be linked.
1146  CmdArgs.push_back(
1147  Args.MakeArgString("--dependent-lib=" +
1148  TC.getCompilerRTBasename(Args, "ubsan_standalone")));
1149  if (types::isCXX(InputType))
1150  CmdArgs.push_back(Args.MakeArgString(
1151  "--dependent-lib=" +
1152  TC.getCompilerRTBasename(Args, "ubsan_standalone_cxx")));
1153  }
1154  if (TC.getTriple().isOSWindows() && needsStatsRt()) {
1155  CmdArgs.push_back(Args.MakeArgString(
1156  "--dependent-lib=" + TC.getCompilerRTBasename(Args, "stats_client")));
1157 
1158  // The main executable must export the stats runtime.
1159  // FIXME: Only exporting from the main executable (e.g. based on whether the
1160  // translation unit defines main()) would save a little space, but having
1161  // multiple copies of the runtime shouldn't hurt.
1162  CmdArgs.push_back(Args.MakeArgString(
1163  "--dependent-lib=" + TC.getCompilerRTBasename(Args, "stats")));
1164  addIncludeLinkerOption(TC, Args, CmdArgs, "__sanitizer_stats_register");
1165  }
1166 
1167  if (Sanitizers.empty())
1168  return;
1169  CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
1170 
1171  if (!RecoverableSanitizers.empty())
1172  CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
1173  toString(RecoverableSanitizers)));
1174 
1175  if (!TrapSanitizers.empty())
1176  CmdArgs.push_back(
1177  Args.MakeArgString("-fsanitize-trap=" + toString(TrapSanitizers)));
1178 
1179  addSpecialCaseListOpt(Args, CmdArgs,
1180  "-fsanitize-ignorelist=", UserIgnorelistFiles);
1181  addSpecialCaseListOpt(Args, CmdArgs,
1182  "-fsanitize-system-ignorelist=", SystemIgnorelistFiles);
1183 
1184  if (MsanTrackOrigins)
1185  CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
1186  Twine(MsanTrackOrigins)));
1187 
1188  if (MsanUseAfterDtor)
1189  CmdArgs.push_back("-fsanitize-memory-use-after-dtor");
1190 
1191  if (!MsanParamRetval)
1192  CmdArgs.push_back("-fno-sanitize-memory-param-retval");
1193 
1194  // FIXME: Pass these parameters as function attributes, not as -llvm flags.
1195  if (!TsanMemoryAccess) {
1196  CmdArgs.push_back("-mllvm");
1197  CmdArgs.push_back("-tsan-instrument-memory-accesses=0");
1198  CmdArgs.push_back("-mllvm");
1199  CmdArgs.push_back("-tsan-instrument-memintrinsics=0");
1200  }
1201  if (!TsanFuncEntryExit) {
1202  CmdArgs.push_back("-mllvm");
1203  CmdArgs.push_back("-tsan-instrument-func-entry-exit=0");
1204  }
1205  if (!TsanAtomics) {
1206  CmdArgs.push_back("-mllvm");
1207  CmdArgs.push_back("-tsan-instrument-atomics=0");
1208  }
1209 
1210  if (HwasanUseAliases) {
1211  CmdArgs.push_back("-mllvm");
1212  CmdArgs.push_back("-hwasan-experimental-use-page-aliases=1");
1213  }
1214 
1215  if (CfiCrossDso)
1216  CmdArgs.push_back("-fsanitize-cfi-cross-dso");
1217 
1218  if (CfiICallGeneralizePointers)
1219  CmdArgs.push_back("-fsanitize-cfi-icall-generalize-pointers");
1220 
1221  if (CfiCanonicalJumpTables)
1222  CmdArgs.push_back("-fsanitize-cfi-canonical-jump-tables");
1223 
1224  if (Stats)
1225  CmdArgs.push_back("-fsanitize-stats");
1226 
1227  if (MinimalRuntime)
1228  CmdArgs.push_back("-fsanitize-minimal-runtime");
1229 
1230  if (AsanFieldPadding)
1231  CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
1232  Twine(AsanFieldPadding)));
1233 
1234  if (AsanUseAfterScope)
1235  CmdArgs.push_back("-fsanitize-address-use-after-scope");
1236 
1237  if (AsanPoisonCustomArrayCookie)
1238  CmdArgs.push_back("-fsanitize-address-poison-custom-array-cookie");
1239 
1240  if (AsanGlobalsDeadStripping)
1241  CmdArgs.push_back("-fsanitize-address-globals-dead-stripping");
1242 
1243  if (!AsanUseOdrIndicator)
1244  CmdArgs.push_back("-fno-sanitize-address-use-odr-indicator");
1245 
1246  if (AsanInvalidPointerCmp) {
1247  CmdArgs.push_back("-mllvm");
1248  CmdArgs.push_back("-asan-detect-invalid-pointer-cmp");
1249  }
1250 
1251  if (AsanInvalidPointerSub) {
1252  CmdArgs.push_back("-mllvm");
1253  CmdArgs.push_back("-asan-detect-invalid-pointer-sub");
1254  }
1255 
1256  if (AsanOutlineInstrumentation) {
1257  CmdArgs.push_back("-mllvm");
1258  CmdArgs.push_back("-asan-instrumentation-with-call-threshold=0");
1259  }
1260 
1261  // Only pass the option to the frontend if the user requested,
1262  // otherwise the frontend will just use the codegen default.
1263  if (AsanDtorKind != llvm::AsanDtorKind::Invalid) {
1264  CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-destructor=" +
1265  AsanDtorKindToString(AsanDtorKind)));
1266  }
1267 
1268  if (AsanUseAfterReturn != llvm::AsanDetectStackUseAfterReturnMode::Invalid) {
1269  CmdArgs.push_back(Args.MakeArgString(
1270  "-fsanitize-address-use-after-return=" +
1271  AsanDetectStackUseAfterReturnModeToString(AsanUseAfterReturn)));
1272  }
1273 
1274  if (!HwasanAbi.empty()) {
1275  CmdArgs.push_back("-default-function-attr");
1276  CmdArgs.push_back(Args.MakeArgString("hwasan-abi=" + HwasanAbi));
1277  }
1278 
1279  if (Sanitizers.has(SanitizerKind::HWAddress) && !HwasanUseAliases) {
1280  CmdArgs.push_back("-target-feature");
1281  CmdArgs.push_back("+tagged-globals");
1282  }
1283 
1284  // MSan: Workaround for PR16386.
1285  // ASan: This is mainly to help LSan with cases such as
1286  // https://github.com/google/sanitizers/issues/373
1287  // We can't make this conditional on -fsanitize=leak, as that flag shouldn't
1288  // affect compilation.
1289  if (Sanitizers.has(SanitizerKind::Memory) ||
1290  Sanitizers.has(SanitizerKind::Address))
1291  CmdArgs.push_back("-fno-assume-sane-operator-new");
1292 
1293  // libFuzzer wants to intercept calls to certain library functions, so the
1294  // following -fno-builtin-* flags force the compiler to emit interposable
1295  // libcalls to these functions. Other sanitizers effectively do the same thing
1296  // by marking all library call sites with NoBuiltin attribute in their LLVM
1297  // pass. (see llvm::maybeMarkSanitizerLibraryCallNoBuiltin)
1298  if (Sanitizers.has(SanitizerKind::FuzzerNoLink)) {
1299  CmdArgs.push_back("-fno-builtin-bcmp");
1300  CmdArgs.push_back("-fno-builtin-memcmp");
1301  CmdArgs.push_back("-fno-builtin-strncmp");
1302  CmdArgs.push_back("-fno-builtin-strcmp");
1303  CmdArgs.push_back("-fno-builtin-strncasecmp");
1304  CmdArgs.push_back("-fno-builtin-strcasecmp");
1305  CmdArgs.push_back("-fno-builtin-strstr");
1306  CmdArgs.push_back("-fno-builtin-strcasestr");
1307  CmdArgs.push_back("-fno-builtin-memmem");
1308  }
1309 
1310  // Require -fvisibility= flag on non-Windows when compiling if vptr CFI is
1311  // enabled.
1312  if (Sanitizers.hasOneOf(CFIClasses) && !TC.getTriple().isOSWindows() &&
1313  !Args.hasArg(options::OPT_fvisibility_EQ)) {
1314  TC.getDriver().Diag(clang::diag::err_drv_argument_only_allowed_with)
1315  << lastArgumentForMask(TC.getDriver(), Args,
1316  Sanitizers.Mask & CFIClasses)
1317  << "-fvisibility=";
1318  }
1319 
1320  if (Sanitizers.has(SanitizerKind::MemtagStack) &&
1321  !hasTargetFeatureMTE(CmdArgs))
1322  TC.getDriver().Diag(diag::err_stack_tagging_requires_hardware_feature);
1323 }
1324 
1325 SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
1326  bool DiagnoseErrors) {
1327  assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
1328  A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
1329  A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
1330  A->getOption().matches(options::OPT_fno_sanitize_recover_EQ) ||
1331  A->getOption().matches(options::OPT_fsanitize_trap_EQ) ||
1332  A->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) &&
1333  "Invalid argument in parseArgValues!");
1334  SanitizerMask Kinds;
1335  for (int i = 0, n = A->getNumValues(); i != n; ++i) {
1336  const char *Value = A->getValue(i);
1338  // Special case: don't accept -fsanitize=all.
1339  if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
1340  0 == strcmp("all", Value))
1341  Kind = SanitizerMask();
1342  else
1343  Kind = parseSanitizerValue(Value, /*AllowGroups=*/true);
1344 
1345  if (Kind)
1346  Kinds |= Kind;
1347  else if (DiagnoseErrors)
1348  D.Diag(clang::diag::err_drv_unsupported_option_argument)
1349  << A->getSpelling() << Value;
1350  }
1351  return Kinds;
1352 }
1353 
1354 int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A,
1355  bool DiagnoseErrors) {
1356  assert(A->getOption().matches(options::OPT_fsanitize_coverage) ||
1357  A->getOption().matches(options::OPT_fno_sanitize_coverage));
1358  int Features = 0;
1359  for (int i = 0, n = A->getNumValues(); i != n; ++i) {
1360  const char *Value = A->getValue(i);
1361  int F = llvm::StringSwitch<int>(Value)
1362  .Case("func", CoverageFunc)
1363  .Case("bb", CoverageBB)
1364  .Case("edge", CoverageEdge)
1365  .Case("indirect-calls", CoverageIndirCall)
1366  .Case("trace-bb", CoverageTraceBB)
1367  .Case("trace-cmp", CoverageTraceCmp)
1368  .Case("trace-div", CoverageTraceDiv)
1369  .Case("trace-gep", CoverageTraceGep)
1370  .Case("8bit-counters", Coverage8bitCounters)
1371  .Case("trace-pc", CoverageTracePC)
1372  .Case("trace-pc-guard", CoverageTracePCGuard)
1373  .Case("no-prune", CoverageNoPrune)
1374  .Case("inline-8bit-counters", CoverageInline8bitCounters)
1375  .Case("inline-bool-flag", CoverageInlineBoolFlag)
1376  .Case("pc-table", CoveragePCTable)
1377  .Case("stack-depth", CoverageStackDepth)
1378  .Case("trace-loads", CoverageTraceLoads)
1379  .Case("trace-stores", CoverageTraceStores)
1380  .Case("control-flow", CoverageControlFlow)
1381  .Default(0);
1382  if (F == 0 && DiagnoseErrors)
1383  D.Diag(clang::diag::err_drv_unsupported_option_argument)
1384  << A->getSpelling() << Value;
1385  Features |= F;
1386  }
1387  return Features;
1388 }
1389 
1390 int parseBinaryMetadataFeatures(const Driver &D, const llvm::opt::Arg *A,
1391  bool DiagnoseErrors) {
1392  assert(
1393  A->getOption().matches(options::OPT_fexperimental_sanitize_metadata_EQ) ||
1394  A->getOption().matches(
1395  options::OPT_fno_experimental_sanitize_metadata_EQ));
1396  int Features = 0;
1397  for (int i = 0, n = A->getNumValues(); i != n; ++i) {
1398  const char *Value = A->getValue(i);
1399  int F = llvm::StringSwitch<int>(Value)
1400  .Case("covered", BinaryMetadataCovered)
1401  .Case("atomics", BinaryMetadataAtomics)
1402  .Case("all", ~0)
1403  .Default(0);
1404  if (F == 0 && DiagnoseErrors)
1405  D.Diag(clang::diag::err_drv_unsupported_option_argument)
1406  << A->getSpelling() << Value;
1407  Features |= F;
1408  }
1409  return Features;
1410 }
1411 
1412 std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
1413  SanitizerMask Mask) {
1414  for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
1415  E = Args.rend();
1416  I != E; ++I) {
1417  const auto *Arg = *I;
1418  if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
1419  SanitizerMask AddKinds =
1420  expandSanitizerGroups(parseArgValues(D, Arg, false));
1421  if (AddKinds & Mask)
1422  return describeSanitizeArg(Arg, Mask);
1423  } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
1424  SanitizerMask RemoveKinds =
1425  expandSanitizerGroups(parseArgValues(D, Arg, false));
1426  Mask &= ~RemoveKinds;
1427  }
1428  }
1429  llvm_unreachable("arg list didn't provide expected value");
1430 }
1431 
1432 std::string describeSanitizeArg(const llvm::opt::Arg *A, SanitizerMask Mask) {
1433  assert(A->getOption().matches(options::OPT_fsanitize_EQ) &&
1434  "Invalid argument in describeSanitizerArg!");
1435 
1436  std::string Sanitizers;
1437  for (int i = 0, n = A->getNumValues(); i != n; ++i) {
1439  parseSanitizerValue(A->getValue(i), /*AllowGroups=*/true)) &
1440  Mask) {
1441  if (!Sanitizers.empty())
1442  Sanitizers += ",";
1443  Sanitizers += A->getValue(i);
1444  }
1445  }
1446 
1447  assert(!Sanitizers.empty() && "arg didn't provide expected value");
1448  return "-fsanitize=" + Sanitizers;
1449 }
clang::driver::SanitizerArgs::needsUnwindTables
bool needsUnwindTables() const
Definition: SanitizerArgs.cpp:297
clang::driver::SanitizerArgs::SanitizerArgs
SanitizerArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, bool DiagnoseErrors=true)
Parses the sanitizer arguments from an argument list.
Definition: SanitizerArgs.cpp:305
CoverageStackDepth
@ CoverageStackDepth
Definition: SanitizerArgs.cpp:97
parseSanitizeTrapArgs
static SanitizerMask parseSanitizeTrapArgs(const Driver &D, const llvm::opt::ArgList &Args, bool DiagnoseErrors)
Definition: SanitizerArgs.cpp:233
clang::interp::Add
bool Add(InterpState &S, CodePtr OpPC)
Definition: Interp.h:155
RequiresPIE
static const SanitizerMask RequiresPIE
Definition: SanitizerArgs.cpp:39
hasTargetFeatureMTE
static bool hasTargetFeatureMTE(const llvm::opt::ArgStringList &CmdArgs)
Definition: SanitizerArgs.cpp:1070
clang::DeclaratorContext::File
@ File
CoverageTraceStores
@ CoverageTraceStores
Definition: SanitizerArgs.cpp:100
clang::ento::Nullability
Nullability
Definition: CheckerHelpers.h:55
Driver.h
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
setGroupBits
static SanitizerMask setGroupBits(SanitizerMask Kinds)
Sets group bits for every group that has at least one representative already enabled in Kinds.
Definition: SanitizerArgs.cpp:224
clang::driver::ToolChain::getRTTIArg
const llvm::opt::Arg * getRTTIArg() const
Definition: ToolChain.h:289
NeedsUbsanRt
static const SanitizerMask NeedsUbsanRt
Definition: SanitizerArgs.cpp:29
clang::driver::ToolChain::getDriver
const Driver & getDriver() const
Definition: ToolChain.h:232
CoverageInline8bitCounters
@ CoverageInline8bitCounters
Definition: SanitizerArgs.cpp:95
NotAllowedWithTrap
static const SanitizerMask NotAllowedWithTrap
Definition: SanitizerArgs.cpp:36
clang::diag::Group
Group
Definition: DiagnosticCategories.h:23
BinaryMetadataAtomics
@ BinaryMetadataAtomics
Definition: SanitizerArgs.cpp:106
TrappingSupported
static const SanitizerMask TrappingSupported
Definition: SanitizerArgs.cpp:67
CompatibleWithMinimalRuntime
static const SanitizerMask CompatibleWithMinimalRuntime
Definition: SanitizerArgs.cpp:77
End
SourceLocation End
Definition: USRLocFinder.cpp:167
clang::driver::types::isCXX
bool isCXX(ID Id)
isCXX - Is this a "C++" input (C++ and Obj-C++ sources and headers).
Definition: Types.cpp:230
clang::SanitizerSet::Mask
SanitizerMask Mask
Bitmask of enabled sanitizers.
Definition: Sanitizers.h:176
clang::StructuralEquivalenceKind::Default
@ Default
clang::AsanDtorKindFromString
llvm::AsanDtorKind AsanDtorKindFromString(StringRef kind)
Definition: Sanitizers.cpp:84
parseBinaryMetadataFeatures
static int parseBinaryMetadataFeatures(const Driver &D, const llvm::opt::Arg *A, bool DiagnoseErrors)
Parse -f(no-)?sanitize-metadata= flag values, diagnosing any invalid components.
Definition: SanitizerArgs.cpp:1390
clang::driver::Driver::Diag
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:142
parseArgValues
static SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A, bool DiagnoseErrors)
Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any invalid components.
Definition: SanitizerArgs.cpp:1325
clang::driver::SanitizerArgs::requiresPIE
bool requiresPIE() const
Definition: SanitizerArgs.cpp:293
clang::SanitizerSet
Definition: Sanitizers.h:153
Options.h
clang::driver::SanitizerArgs::addArgs
void addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const
Definition: SanitizerArgs.cpp:1083
clang::XRayInstrKind::None
constexpr XRayInstrMask None
Definition: XRayInstr.h:38
toString
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
Definition: SanitizerArgs.cpp:1033
clang::driver::ToolChain::getDefaultSanitizers
virtual SanitizerMask getDefaultSanitizers() const
Return sanitizers which are enabled by default.
Definition: ToolChain.h:741
CoverageTraceGep
@ CoverageTraceGep
Definition: SanitizerArgs.cpp:90
llvm::opt
Definition: DiagnosticOptions.h:19
DriverDiagnostic.h
CoverageTracePC
@ CoverageTracePC
Definition: SanitizerArgs.cpp:92
describeSanitizeArg
static std::string describeSanitizeArg(const llvm::opt::Arg *A, SanitizerMask Mask)
Produce an argument string from argument A, which shows how it provides a value in Mask.
Definition: SanitizerArgs.cpp:1432
clang::AsanDetectStackUseAfterReturnModeFromString
llvm::AsanDetectStackUseAfterReturnMode AsanDetectStackUseAfterReturnModeFromString(StringRef modeStr)
Definition: Sanitizers.cpp:107
CoverageFeature
CoverageFeature
Definition: SanitizerArgs.cpp:82
CoverageFunc
@ CoverageFunc
Definition: SanitizerArgs.cpp:83
NotAllowedWithMinimalRuntime
static const SanitizerMask NotAllowedWithMinimalRuntime
Definition: SanitizerArgs.cpp:37
llvm::SmallString
Definition: LLVM.h:37
clang::driver::ToolChain::getTriple
const llvm::Triple & getTriple() const
Definition: ToolChain.h:234
clang::driver::ToolChain::getRTTIMode
RTTIMode getRTTIMode() const
Definition: ToolChain.h:292
CoverageBB
@ CoverageBB
Definition: SanitizerArgs.cpp:84
clang::driver::Driver::CCCIsCXX
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:211
clang::driver::SanitizerArgs::needsFuzzerInterceptors
bool needsFuzzerInterceptors() const
Definition: SanitizerArgs.cpp:268
TrappingDefault
static const SanitizerMask TrappingDefault
Definition: SanitizerArgs.cpp:72
SanitizerArgs.h
clang::driver::SanitizerArgs::needsLTO
bool needsLTO() const
Definition: SanitizerArgs.cpp:301
CoverageTracePCGuard
@ CoverageTracePCGuard
Definition: SanitizerArgs.cpp:93
CoverageIndirCall
@ CoverageIndirCall
Definition: SanitizerArgs.cpp:86
clang::driver::Driver::ResourceDir
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:165
Unrecoverable
static const SanitizerMask Unrecoverable
Definition: SanitizerArgs.cpp:61
parseCoverageFeatures
static int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A, bool DiagnoseErrors)
Parse -f(no-)?sanitize-coverage= flag values, diagnosing any invalid components.
Definition: SanitizerArgs.cpp:1354
clang::expandSanitizerGroups
SanitizerMask expandSanitizerGroups(SanitizerMask Kinds)
For each sanitizer group bit set in Kinds, set the bits for sanitizers this group enables.
Definition: Sanitizers.cpp:47
clang::driver::ToolChain::getCompilerRTBasename
std::string getCompilerRTBasename(const llvm::opt::ArgList &Args, StringRef Component, FileType Type=ToolChain::FT_Static) const
Definition: ToolChain.cpp:475
ToolChain.h
BinaryMetadataCovered
@ BinaryMetadataCovered
Definition: SanitizerArgs.cpp:105
clang::serialized_diags::create
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
Definition: SerializedDiagnosticPrinter.cpp:301
clang::driver::ToolChain::getSupportedSanitizers
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:1089
clang::driver::Driver::isUsingLTO
bool isUsingLTO(bool IsOffload=false) const
Returns true if we are performing any kind of LTO.
Definition: Driver.h:663
addDefaultIgnorelists
static void addDefaultIgnorelists(const Driver &D, SanitizerMask Kinds, std::vector< std::string > &IgnorelistFiles, bool DiagnoseErrors)
Definition: SanitizerArgs.cpp:157
SupportsCoverage
static const SanitizerMask SupportsCoverage
Definition: SanitizerArgs.cpp:45
CoverageTraceDiv
@ CoverageTraceDiv
Definition: SanitizerArgs.cpp:89
lastArgumentForMask
static std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args, SanitizerMask Mask)
Produce an argument string from ArgList Args, which shows how it provides some sanitizer kind from Ma...
Definition: SanitizerArgs.cpp:1412
clang::driver::ToolChain
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:91
Coverage8bitCounters
@ Coverage8bitCounters
Definition: SanitizerArgs.cpp:91
Value
Value
Definition: UninitializedValues.cpp:103
clang::driver::SanitizerArgs::needsCfiRt
bool needsCfiRt() const
Definition: SanitizerArgs.cpp:283
CoverageTraceLoads
@ CoverageTraceLoads
Definition: SanitizerArgs.cpp:99
clang::SanitizerKind
Definition: Sanitizers.h:131
clang::driver::SanitizerArgs::needsCfiDiagRt
bool needsCfiDiagRt() const
Definition: SanitizerArgs.cpp:288
clang::driver::types::ID
ID
Definition: Types.h:23
RecoverableByDefault
static const SanitizerMask RecoverableByDefault
Definition: SanitizerArgs.cpp:57
CoverageEdge
@ CoverageEdge
Definition: SanitizerArgs.cpp:85
clang::AsanDtorKindToString
StringRef AsanDtorKindToString(llvm::AsanDtorKind kind)
Definition: Sanitizers.cpp:72
CoverageInlineBoolFlag
@ CoverageInlineBoolFlag
Definition: SanitizerArgs.cpp:98
clang::ObjCPropertyAttribute::Kind
Kind
Definition: DeclObjCCommon.h:22
CoveragePCTable
@ CoveragePCTable
Definition: SanitizerArgs.cpp:96
BinaryMetadataFeature
BinaryMetadataFeature
Definition: SanitizerArgs.cpp:104
Sanitizers.h
CommonArgs.h
clang
Definition: CalledOnceCheck.h:17
CoverageControlFlow
@ CoverageControlFlow
Definition: SanitizerArgs.cpp:101
CoverageTraceBB
@ CoverageTraceBB
Definition: SanitizerArgs.cpp:87
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:75
CoverageNoPrune
@ CoverageNoPrune
Definition: SanitizerArgs.cpp:94
CoverageTraceCmp
@ CoverageTraceCmp
Definition: SanitizerArgs.cpp:88
clang::AsanDetectStackUseAfterReturnModeToString
StringRef AsanDetectStackUseAfterReturnModeToString(llvm::AsanDetectStackUseAfterReturnMode mode)
Definition: Sanitizers.cpp:91
clang::driver::SanitizerArgs::needsUbsanRt
bool needsUbsanRt() const
Definition: SanitizerArgs.cpp:272
clang::driver::Driver::getVFS
llvm::vfs::FileSystem & getVFS() const
Definition: Driver.h:369
NeedsUbsanCxxRt
static const SanitizerMask NeedsUbsanCxxRt
Definition: SanitizerArgs.cpp:34
NeedsUnwindTables
static const SanitizerMask NeedsUnwindTables
Definition: SanitizerArgs.cpp:42
clang::driver::ToolChain::RTTIMode
RTTIMode
Definition: ToolChain.h:117
addIncludeLinkerOption
static void addIncludeLinkerOption(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, StringRef SymbolName)
Definition: SanitizerArgs.cpp:1056
clang::driver
Definition: Action.h:31
addSpecialCaseListOpt
static void addSpecialCaseListOpt(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const char *SCLOptFlag, const std::vector< std::string > &SCLFiles)
Definition: SanitizerArgs.cpp:1045
clang::parseSanitizerValue
SanitizerMask parseSanitizerValue(StringRef Value, bool AllowGroups)
Parse a single value from a -fsanitize= or -fno-sanitize= value list.
Definition: Sanitizers.cpp:29
CFIClasses
static const SanitizerMask CFIClasses
Definition: SanitizerArgs.cpp:73
NeedsLTO
static const SanitizerMask NeedsLTO
Definition: SanitizerArgs.cpp:66
clang::RISCV::Invalid
@ Invalid
Definition: RISCVVIntrinsicUtils.h:171
AlwaysRecoverable
static const SanitizerMask AlwaysRecoverable
Definition: SanitizerArgs.cpp:63
clang::SanitizerMask
Definition: Sanitizers.h:30
validateSpecialCaseListFormat
static void validateSpecialCaseListFormat(const Driver &D, std::vector< std::string > &SCLFiles, unsigned MalformedSCLErrorDiagID, bool DiagnoseErrors)
Definition: SanitizerArgs.cpp:143
parseSpecialCaseListArg
static void parseSpecialCaseListArg(const Driver &D, const llvm::opt::ArgList &Args, std::vector< std::string > &SCLFiles, llvm::opt::OptSpecifier SCLOptionID, llvm::opt::OptSpecifier NoSCLOptionID, unsigned MalformedSCLErrorDiagID, bool DiagnoseErrors)
Parse -f(no-)?sanitize-(coverage-)?(allow|ignore)list argument's values, diagnosing any invalid file ...
Definition: SanitizerArgs.cpp:195
clang::driver::ToolChain::RM_Disabled
@ RM_Disabled
Definition: ToolChain.h:119