SanitizerCoverage.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //===--------- Definition of the SanitizerCoverage class --------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  6. // See https://llvm.org/LICENSE.txt for license information.
  7. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  8. //
  9. //===----------------------------------------------------------------------===//
  10. //
  11. // This file declares the SanitizerCoverage class which is a port of the legacy
  12. // SanitizerCoverage pass to use the new PassManager infrastructure.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_SANITIZERCOVERAGE_H
  16. #define LLVM_TRANSFORMS_INSTRUMENTATION_SANITIZERCOVERAGE_H
  17. #include "llvm/IR/Module.h"
  18. #include "llvm/IR/PassManager.h"
  19. #include "llvm/Support/SpecialCaseList.h"
  20. #include "llvm/Support/VirtualFileSystem.h"
  21. #include "llvm/Transforms/Instrumentation.h"
  22. namespace llvm {
  23. /// This is the ModuleSanitizerCoverage pass used in the new pass manager. The
  24. /// pass instruments functions for coverage, adds initialization calls to the
  25. /// module for trace PC guards and 8bit counters if they are requested, and
  26. /// appends globals to llvm.compiler.used.
  27. class ModuleSanitizerCoveragePass
  28. : public PassInfoMixin<ModuleSanitizerCoveragePass> {
  29. public:
  30. explicit ModuleSanitizerCoveragePass(
  31. SanitizerCoverageOptions Options = SanitizerCoverageOptions(),
  32. const std::vector<std::string> &AllowlistFiles =
  33. std::vector<std::string>(),
  34. const std::vector<std::string> &BlocklistFiles =
  35. std::vector<std::string>())
  36. : Options(Options) {
  37. if (AllowlistFiles.size() > 0)
  38. Allowlist = SpecialCaseList::createOrDie(AllowlistFiles,
  39. *vfs::getRealFileSystem());
  40. if (BlocklistFiles.size() > 0)
  41. Blocklist = SpecialCaseList::createOrDie(BlocklistFiles,
  42. *vfs::getRealFileSystem());
  43. }
  44. PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
  45. static bool isRequired() { return true; }
  46. private:
  47. SanitizerCoverageOptions Options;
  48. std::unique_ptr<SpecialCaseList> Allowlist;
  49. std::unique_ptr<SpecialCaseList> Blocklist;
  50. };
  51. // Insert SanitizerCoverage instrumentation.
  52. ModulePass *createModuleSanitizerCoverageLegacyPassPass(
  53. const SanitizerCoverageOptions &Options = SanitizerCoverageOptions(),
  54. const std::vector<std::string> &AllowlistFiles = std::vector<std::string>(),
  55. const std::vector<std::string> &BlocklistFiles =
  56. std::vector<std::string>());
  57. } // namespace llvm
  58. #endif