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
16#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_SANITIZERCOVERAGE_H
17#define LLVM_TRANSFORMS_INSTRUMENTATION_SANITIZERCOVERAGE_H
18
19#include "llvm/IR/Module.h"
20#include "llvm/IR/PassManager.h"
21#include "llvm/Support/SpecialCaseList.h"
22#include "llvm/Support/VirtualFileSystem.h"
23#include "llvm/Transforms/Instrumentation.h"
24
25namespace llvm {
26
27/// This is the ModuleSanitizerCoverage pass used in the new pass manager. The
28/// pass instruments functions for coverage, adds initialization calls to the
29/// module for trace PC guards and 8bit counters if they are requested, and
30/// appends globals to llvm.compiler.used.
31class ModuleSanitizerCoveragePass
32    : public PassInfoMixin<ModuleSanitizerCoveragePass> {
33public:
34  explicit ModuleSanitizerCoveragePass(
35      SanitizerCoverageOptions Options = SanitizerCoverageOptions(),
36      const std::vector<std::string> &AllowlistFiles =
37          std::vector<std::string>(),
38      const std::vector<std::string> &BlocklistFiles =
39          std::vector<std::string>())
40      : Options(Options) {
41    if (AllowlistFiles.size() > 0)
42      Allowlist = SpecialCaseList::createOrDie(AllowlistFiles,
43                                               *vfs::getRealFileSystem());
44    if (BlocklistFiles.size() > 0)
45      Blocklist = SpecialCaseList::createOrDie(BlocklistFiles,
46                                               *vfs::getRealFileSystem());
47  }
48  PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
49
50private:
51  SanitizerCoverageOptions Options;
52
53  std::unique_ptr<SpecialCaseList> Allowlist;
54  std::unique_ptr<SpecialCaseList> Blocklist;
55};
56
57// Insert SanitizerCoverage instrumentation.
58ModulePass *createModuleSanitizerCoverageLegacyPassPass(
59    const SanitizerCoverageOptions &Options = SanitizerCoverageOptions(),
60    const std::vector<std::string> &AllowlistFiles = std::vector<std::string>(),
61    const std::vector<std::string> &BlocklistFiles =
62        std::vector<std::string>());
63
64} // namespace llvm
65
66#endif
67