SanitizerArgs.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- SanitizerArgs.h - Arguments for sanitizer tools -------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_DRIVER_SANITIZERARGS_H
  14. #define LLVM_CLANG_DRIVER_SANITIZERARGS_H
  15. #include "clang/Basic/Sanitizers.h"
  16. #include "clang/Driver/Types.h"
  17. #include "llvm/Option/Arg.h"
  18. #include "llvm/Option/ArgList.h"
  19. #include "llvm/Transforms/Instrumentation/AddressSanitizerOptions.h"
  20. #include <string>
  21. #include <vector>
  22. namespace clang {
  23. namespace driver {
  24. class ToolChain;
  25. class SanitizerArgs {
  26. SanitizerSet Sanitizers;
  27. SanitizerSet RecoverableSanitizers;
  28. SanitizerSet TrapSanitizers;
  29. std::vector<std::string> UserIgnorelistFiles;
  30. std::vector<std::string> SystemIgnorelistFiles;
  31. std::vector<std::string> CoverageAllowlistFiles;
  32. std::vector<std::string> CoverageIgnorelistFiles;
  33. int CoverageFeatures = 0;
  34. int BinaryMetadataFeatures = 0;
  35. int MsanTrackOrigins = 0;
  36. bool MsanUseAfterDtor = true;
  37. bool MsanParamRetval = true;
  38. bool CfiCrossDso = false;
  39. bool CfiICallGeneralizePointers = false;
  40. bool CfiCanonicalJumpTables = false;
  41. int AsanFieldPadding = 0;
  42. bool SharedRuntime = false;
  43. bool AsanUseAfterScope = true;
  44. bool AsanPoisonCustomArrayCookie = false;
  45. bool AsanGlobalsDeadStripping = false;
  46. bool AsanUseOdrIndicator = false;
  47. bool AsanInvalidPointerCmp = false;
  48. bool AsanInvalidPointerSub = false;
  49. bool AsanOutlineInstrumentation = false;
  50. llvm::AsanDtorKind AsanDtorKind = llvm::AsanDtorKind::Invalid;
  51. std::string HwasanAbi;
  52. bool LinkRuntimes = true;
  53. bool LinkCXXRuntimes = false;
  54. bool NeedPIE = false;
  55. bool SafeStackRuntime = false;
  56. bool Stats = false;
  57. bool TsanMemoryAccess = true;
  58. bool TsanFuncEntryExit = true;
  59. bool TsanAtomics = true;
  60. bool MinimalRuntime = false;
  61. // True if cross-dso CFI support if provided by the system (i.e. Android).
  62. bool ImplicitCfiRuntime = false;
  63. bool NeedsMemProfRt = false;
  64. bool HwasanUseAliases = false;
  65. llvm::AsanDetectStackUseAfterReturnMode AsanUseAfterReturn =
  66. llvm::AsanDetectStackUseAfterReturnMode::Invalid;
  67. std::string MemtagMode;
  68. public:
  69. /// Parses the sanitizer arguments from an argument list.
  70. SanitizerArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
  71. bool DiagnoseErrors = true);
  72. bool needsSharedRt() const { return SharedRuntime; }
  73. bool needsMemProfRt() const { return NeedsMemProfRt; }
  74. bool needsAsanRt() const { return Sanitizers.has(SanitizerKind::Address); }
  75. bool needsHwasanRt() const {
  76. return Sanitizers.has(SanitizerKind::HWAddress);
  77. }
  78. bool needsHwasanAliasesRt() const {
  79. return needsHwasanRt() && HwasanUseAliases;
  80. }
  81. bool needsTsanRt() const { return Sanitizers.has(SanitizerKind::Thread); }
  82. bool needsMsanRt() const { return Sanitizers.has(SanitizerKind::Memory); }
  83. bool needsFuzzer() const { return Sanitizers.has(SanitizerKind::Fuzzer); }
  84. bool needsLsanRt() const {
  85. return Sanitizers.has(SanitizerKind::Leak) &&
  86. !Sanitizers.has(SanitizerKind::Address) &&
  87. !Sanitizers.has(SanitizerKind::HWAddress);
  88. }
  89. bool needsFuzzerInterceptors() const;
  90. bool needsUbsanRt() const;
  91. bool requiresMinimalRuntime() const { return MinimalRuntime; }
  92. bool needsDfsanRt() const { return Sanitizers.has(SanitizerKind::DataFlow); }
  93. bool needsSafeStackRt() const { return SafeStackRuntime; }
  94. bool needsCfiRt() const;
  95. bool needsCfiDiagRt() const;
  96. bool needsStatsRt() const { return Stats; }
  97. bool needsScudoRt() const { return Sanitizers.has(SanitizerKind::Scudo); }
  98. bool hasMemTag() const {
  99. return hasMemtagHeap() || hasMemtagStack() || hasMemtagGlobals();
  100. }
  101. bool hasMemtagHeap() const {
  102. return Sanitizers.has(SanitizerKind::MemtagHeap);
  103. }
  104. bool hasMemtagStack() const {
  105. return Sanitizers.has(SanitizerKind::MemtagStack);
  106. }
  107. bool hasMemtagGlobals() const {
  108. return Sanitizers.has(SanitizerKind::MemtagGlobals);
  109. }
  110. const std::string &getMemtagMode() const {
  111. assert(!MemtagMode.empty());
  112. return MemtagMode;
  113. }
  114. bool requiresPIE() const;
  115. bool needsUnwindTables() const;
  116. bool needsLTO() const;
  117. bool linkRuntimes() const { return LinkRuntimes; }
  118. bool linkCXXRuntimes() const { return LinkCXXRuntimes; }
  119. bool hasCrossDsoCfi() const { return CfiCrossDso; }
  120. bool hasAnySanitizer() const { return !Sanitizers.empty(); }
  121. void addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
  122. llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const;
  123. };
  124. } // namespace driver
  125. } // namespace clang
  126. #endif
  127. #ifdef __GNUC__
  128. #pragma GCC diagnostic pop
  129. #endif