LLVM 22.0.0git
SpecialCaseList.h
Go to the documentation of this file.
1//===-- SpecialCaseList.h - special case list for sanitizers ----*- C++ -*-===//
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//
8// This file implements a Special Case List for code sanitizers.
9//
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_SUPPORT_SPECIALCASELIST_H
13#define LLVM_SUPPORT_SPECIALCASELIST_H
14
15#include "llvm/ADT/StringMap.h"
18#include "llvm/Support/Regex.h"
19#include <memory>
20#include <string>
21#include <utility>
22#include <vector>
23
24namespace llvm {
25class MemoryBuffer;
26class StringRef;
27
28namespace vfs {
29class FileSystem;
30}
31
32/// This is a utility class used to parse user-provided text files with
33/// "special case lists" for code sanitizers. Such files are used to
34/// define an "ABI list" for DataFlowSanitizer and allow/exclusion lists for
35/// sanitizers like AddressSanitizer or UndefinedBehaviorSanitizer.
36///
37/// Empty lines and lines starting with "#" are ignored. Sections are defined
38/// using a '[section_name]' header and can be used to specify sanitizers the
39/// entries below it apply to. Section names are globs, and
40/// entries without a section header match all sections (e.g. an '[*]' header
41/// is assumed.)
42/// The remaining lines should have the form:
43/// prefix:glob_pattern[=category]
44/// If category is not specified, it is assumed to be empty string.
45/// Definitions of "prefix" and "category" are sanitizer-specific. For example,
46/// sanitizer exclusion support prefixes "src", "mainfile", "fun" and "global".
47/// "glob_pattern" defines source files, main files, functions or globals which
48/// shouldn't be instrumented.
49/// Examples of categories:
50/// "functional": used in DFSan to list functions with pure functional
51/// semantics.
52/// "init": used in ASan exclusion list to disable initialization-order bugs
53/// detection for certain globals or source files.
54/// Full special case list file example:
55/// ---
56/// [address]
57/// # Excluded items:
58/// fun:*_ZN4base6subtle*
59/// global:*global_with_bad_access_or_initialization*
60/// global:*global_with_initialization_issues*=init
61/// type:*Namespace::ClassName*=init
62/// src:file_with_tricky_code.cc
63/// src:ignore-global-initializers-issues.cc=init
64/// mainfile:main_file.cc
65///
66/// [dataflow]
67/// # Functions with pure functional semantics:
68/// fun:cos=functional
69/// fun:sin=functional
70/// ---
72public:
73 static constexpr std::pair<unsigned, unsigned> NotFound = {0, 0};
74 /// Parses the special case list entries from files. On failure, returns
75 /// 0 and writes an error message to string.
76 LLVM_ABI static std::unique_ptr<SpecialCaseList>
77 create(const std::vector<std::string> &Paths, llvm::vfs::FileSystem &FS,
78 std::string &Error);
79 /// Parses the special case list from a memory buffer. On failure, returns
80 /// 0 and writes an error message to string.
81 LLVM_ABI static std::unique_ptr<SpecialCaseList>
82 create(const MemoryBuffer *MB, std::string &Error);
83 /// Parses the special case list entries from files. On failure, reports a
84 /// fatal error.
85 LLVM_ABI static std::unique_ptr<SpecialCaseList>
86 createOrDie(const std::vector<std::string> &Paths, llvm::vfs::FileSystem &FS);
87
89
90 /// Returns true, if special case list contains a line
91 /// \code
92 /// @Prefix:<E>=@Category
93 /// \endcode
94 /// where @Query satisfies the glob <E> in a given @Section.
96 StringRef Category = StringRef()) const;
97
98 /// Returns the file index and the line number <FileIdx, LineNo> corresponding
99 /// to the special case list entry if the special case list contains a line
100 /// \code
101 /// @Prefix:<E>=@Category
102 /// \endcode
103 /// where @Query satisfies the glob <E> in a given @Section.
104 /// Returns (zero, zero) if there is no exclusion entry corresponding to this
105 /// expression.
106 LLVM_ABI std::pair<unsigned, unsigned>
108 StringRef Category = StringRef()) const;
109
110protected:
111 // Implementations of the create*() functions that can also be used by derived
112 // classes.
113 LLVM_ABI bool createInternal(const std::vector<std::string> &Paths,
114 vfs::FileSystem &VFS, std::string &Error);
115 LLVM_ABI bool createInternal(const MemoryBuffer *MB, std::string &Error);
116
117 SpecialCaseList() = default;
120
121 /// Represents a set of globs and their line numbers
122 class Matcher {
123 public:
124 LLVM_ABI Error insert(StringRef Pattern, unsigned LineNumber,
125 bool UseRegex);
126 // Returns the line number in the source file that this query matches to.
127 // Returns zero if no match is found.
128 LLVM_ABI unsigned match(StringRef Query) const;
129
130 struct Glob {
131 std::string Name;
132 unsigned LineNo;
134 // neither copyable nor movable because GlobPattern contains
135 // Glob::StringRef that points to Glob::Name.
136 Glob(Glob &&) = delete;
137 Glob() = default;
138 };
139
140 std::vector<std::unique_ptr<Matcher::Glob>> Globs;
141 std::vector<std::pair<std::unique_ptr<Regex>, unsigned>> RegExes;
142 };
143
145
146 struct Section {
147 Section(StringRef Str, unsigned FileIdx)
148 : SectionStr(Str), FileIdx(FileIdx) {};
149
150 std::unique_ptr<Matcher> SectionMatcher = std::make_unique<Matcher>();
152 std::string SectionStr;
153 unsigned FileIdx;
154 };
155
156 std::vector<Section> Sections;
157
159 unsigned FileIdx, unsigned LineNo,
160 bool UseGlobs = true);
161
162 /// Parses just-constructed SpecialCaseList entries from a memory buffer.
163 LLVM_ABI bool parse(unsigned FileIdx, const MemoryBuffer *MB,
164 std::string &Error);
165
166 // Helper method for derived classes to search by Prefix, Query, and Category
167 // once they have already resolved a section entry.
168 LLVM_ABI unsigned inSectionBlame(const SectionEntries &Entries,
169 StringRef Prefix, StringRef Query,
170 StringRef Category) const;
171};
172
173} // namespace llvm
174
175#endif // LLVM_SUPPORT_SPECIALCASELIST_H
This file defines the StringMap class.
#define LLVM_ABI
Definition Compiler.h:213
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
This class implements a glob pattern matcher similar to the one found in bash, but with some key diff...
Definition GlobPattern.h:52
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Represents a set of globs and their line numbers.
std::vector< std::pair< std::unique_ptr< Regex >, unsigned > > RegExes
LLVM_ABI unsigned match(StringRef Query) const
std::vector< std::unique_ptr< Matcher::Glob > > Globs
LLVM_ABI Error insert(StringRef Pattern, unsigned LineNumber, bool UseRegex)
SpecialCaseList & operator=(SpecialCaseList const &)=delete
SpecialCaseList(SpecialCaseList const &)=delete
std::vector< Section > Sections
static constexpr std::pair< unsigned, unsigned > NotFound
LLVM_ABI std::pair< unsigned, unsigned > inSectionBlame(StringRef Section, StringRef Prefix, StringRef Query, StringRef Category=StringRef()) const
Returns the file index and the line number <FileIdx, LineNo> corresponding to the special case list e...
LLVM_ABI bool createInternal(const std::vector< std::string > &Paths, vfs::FileSystem &VFS, std::string &Error)
static LLVM_ABI std::unique_ptr< SpecialCaseList > createOrDie(const std::vector< std::string > &Paths, llvm::vfs::FileSystem &FS)
Parses the special case list entries from files.
static LLVM_ABI std::unique_ptr< SpecialCaseList > create(const std::vector< std::string > &Paths, llvm::vfs::FileSystem &FS, std::string &Error)
Parses the special case list entries from files.
LLVM_ABI ~SpecialCaseList()
LLVM_ABI Expected< Section * > addSection(StringRef SectionStr, unsigned FileIdx, unsigned LineNo, bool UseGlobs=true)
LLVM_ABI bool parse(unsigned FileIdx, const MemoryBuffer *MB, std::string &Error)
Parses just-constructed SpecialCaseList entries from a memory buffer.
StringMap< StringMap< Matcher > > SectionEntries
LLVM_ABI bool inSection(StringRef Section, StringRef Prefix, StringRef Query, StringRef Category=StringRef()) const
Returns true, if special case list contains a line.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
The virtual file system interface.
This is an optimization pass for GlobalISel generic memory operations.
Section(StringRef Str, unsigned FileIdx)
std::unique_ptr< Matcher > SectionMatcher