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
16#include "llvm/Support/Error.h"
17#include <memory>
18#include <string>
19#include <utility>
20#include <vector>
21
22namespace llvm {
23class MemoryBuffer;
24class StringRef;
25
26namespace vfs {
27class FileSystem;
28}
29
30/// This is a utility class used to parse user-provided text files with
31/// "special case lists" for code sanitizers. Such files are used to
32/// define an "ABI list" for DataFlowSanitizer and allow/exclusion lists for
33/// sanitizers like AddressSanitizer or UndefinedBehaviorSanitizer.
34///
35/// Empty lines and lines starting with "#" are ignored. Sections are defined
36/// using a '[section_name]' header and can be used to specify sanitizers the
37/// entries below it apply to. Section names are globs, and
38/// entries without a section header match all sections (e.g. an '[*]' header
39/// is assumed.)
40/// The remaining lines should have the form:
41/// prefix:glob_pattern[=category]
42/// If category is not specified, it is assumed to be empty string.
43/// Definitions of "prefix" and "category" are sanitizer-specific. For example,
44/// sanitizer exclusion support prefixes "src", "mainfile", "fun" and "global".
45/// "glob_pattern" defines source files, main files, functions or globals which
46/// shouldn't be instrumented.
47/// Examples of categories:
48/// "functional": used in DFSan to list functions with pure functional
49/// semantics.
50/// "init": used in ASan exclusion list to disable initialization-order bugs
51/// detection for certain globals or source files.
52/// Full special case list file example:
53/// ---
54/// [address]
55/// # Excluded items:
56/// fun:*_ZN4base6subtle*
57/// global:*global_with_bad_access_or_initialization*
58/// global:*global_with_initialization_issues*=init
59/// type:*Namespace::ClassName*=init
60/// src:file_with_tricky_code.cc
61/// src:ignore-global-initializers-issues.cc=init
62/// mainfile:main_file.cc
63///
64/// [dataflow]
65/// # Functions with pure functional semantics:
66/// fun:cos=functional
67/// fun:sin=functional
68/// ---
70public:
71 static constexpr std::pair<unsigned, unsigned> NotFound = {0, 0};
72 /// Parses the special case list entries from files. On failure, returns
73 /// 0 and writes an error message to string.
74 LLVM_ABI static std::unique_ptr<SpecialCaseList>
75 create(const std::vector<std::string> &Paths, llvm::vfs::FileSystem &FS,
76 std::string &Error);
77 /// Parses the special case list from a memory buffer. On failure, returns
78 /// 0 and writes an error message to string.
79 LLVM_ABI static std::unique_ptr<SpecialCaseList>
80 create(const MemoryBuffer *MB, std::string &Error);
81 /// Parses the special case list entries from files. On failure, reports a
82 /// fatal error.
83 LLVM_ABI static std::unique_ptr<SpecialCaseList>
84 createOrDie(const std::vector<std::string> &Paths, llvm::vfs::FileSystem &FS);
85
87
88 /// Returns true, if special case list contains a line
89 /// \code
90 /// @Prefix:<E>=@Category
91 /// \endcode
92 /// where @Query satisfies the glob <E> in a given @Section.
94 StringRef Category = StringRef()) const;
95
96 /// Returns the file index and the line number <FileIdx, LineNo> corresponding
97 /// to the special case list entry if the special case list contains a line
98 /// \code
99 /// @Prefix:<E>=@Category
100 /// \endcode
101 /// where @Query satisfies the glob <E> in a given @Section.
102 /// Returns (zero, zero) if there is no exclusion entry corresponding to this
103 /// expression.
104 LLVM_ABI std::pair<unsigned, unsigned>
106 StringRef Category = StringRef()) const;
107
108protected:
109 // Implementations of the create*() functions that can also be used by derived
110 // classes.
111 LLVM_ABI bool createInternal(const std::vector<std::string> &Paths,
112 vfs::FileSystem &VFS, std::string &Error);
113 LLVM_ABI bool createInternal(const MemoryBuffer *MB, std::string &Error,
114 bool OrderBySize = false);
115
116 SpecialCaseList() = default;
119
120 class Section {
121 public:
122 LLVM_ABI Section(StringRef Name, unsigned FileIdx, bool UseGlobs);
125
126 // Returns name of the section, its entire string in [].
127 StringRef name() const { return Name; }
128
129 // Returns true if string 'Name' matches section name interpreted as a glob.
130 LLVM_ABI bool matchName(StringRef Name) const;
131
132 // Returns sequence number of the file where this section is defined.
133 unsigned fileIndex() const { return FileIdx; }
134
135 // Helper method to search by Prefix, Query, and Category. Returns
136 // 1-based line number on which rule is defined, or 0 if there is no match.
137 LLVM_ABI unsigned getLastMatch(StringRef Prefix, StringRef Query,
138 StringRef Category) const;
139
140 // Helper method to search by Prefix, Query, and Category. Returns
141 // matching rule, or empty string if there is no match.
143 StringRef Category) const;
144
145 /// Returns true if the section has any entries for the given prefix.
146 LLVM_ABI bool hasPrefix(StringRef Prefix) const;
147
148 private:
149 friend class SpecialCaseList;
150 class SectionImpl;
151
152 StringRef Name;
153 unsigned FileIdx;
154 std::unique_ptr<SectionImpl> Impl;
155 };
156
157 ArrayRef<const Section> sections() const { return Sections; }
158
159private:
160 BumpPtrAllocator StrAlloc;
161 std::vector<Section> Sections;
162
164 unsigned FileIdx, unsigned LineNo,
165 bool UseGlobs);
166
167 /// Parses just-constructed SpecialCaseList entries from a memory buffer.
168 LLVM_ABI bool parse(unsigned FileIdx, const MemoryBuffer *MB,
169 std::string &Error, bool OrderBySize);
170};
171
172} // namespace llvm
173
174#endif // LLVM_SUPPORT_SPECIALCASELIST_H
This file defines the BumpPtrAllocator interface.
#define LLVM_ABI
Definition Compiler.h:213
static llvm::Error parse(DataExtractor &Data, uint64_t BaseAddr, LineEntryCallback const &Callback)
Definition LineTable.cpp:54
static Error addSection(const NewSectionInfo &NewSection, Object &Obj)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
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 interface provides simple read-only access to a block of memory, and provides simple methods for...
LLVM_ABI Section(StringRef Name, unsigned FileIdx, bool UseGlobs)
LLVM_ABI StringRef getLongestMatch(StringRef Prefix, StringRef Query, StringRef Category) const
LLVM_ABI bool hasPrefix(StringRef Prefix) const
Returns true if the section has any entries for the given prefix.
LLVM_ABI Section(Section &&)
LLVM_ABI unsigned getLastMatch(StringRef Prefix, StringRef Query, StringRef Category) const
LLVM_ABI bool matchName(StringRef Name) const
ArrayRef< const Section > sections() const
SpecialCaseList & operator=(SpecialCaseList const &)=delete
SpecialCaseList(SpecialCaseList const &)=delete
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 bool inSection(StringRef Section, StringRef Prefix, StringRef Query, StringRef Category=StringRef()) const
Returns true, if special case list contains a line.
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.
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383