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