LLVM  10.0.0
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 is a utility class used to parse user-provided text files with
9 // "special case lists" for code sanitizers. Such files are used to
10 // define an "ABI list" for DataFlowSanitizer and blacklists for sanitizers
11 // like AddressSanitizer or UndefinedBehaviorSanitizer.
12 //
13 // Empty lines and lines starting with "#" are ignored. Sections are defined
14 // using a '[section_name]' header and can be used to specify sanitizers the
15 // entries below it apply to. Section names are regular expressions, and
16 // entries without a section header match all sections (e.g. an '[*]' header
17 // is assumed.)
18 // The remaining lines should have the form:
19 // prefix:wildcard_expression[=category]
20 // If category is not specified, it is assumed to be empty string.
21 // Definitions of "prefix" and "category" are sanitizer-specific. For example,
22 // sanitizer blacklists support prefixes "src", "fun" and "global".
23 // Wildcard expressions define, respectively, source files, functions or
24 // globals which shouldn't be instrumented.
25 // Examples of categories:
26 // "functional": used in DFSan to list functions with pure functional
27 // semantics.
28 // "init": used in ASan blacklist to disable initialization-order bugs
29 // detection for certain globals or source files.
30 // Full special case list file example:
31 // ---
32 // [address]
33 // # Blacklisted items:
34 // fun:*_ZN4base6subtle*
35 // global:*global_with_bad_access_or_initialization*
36 // global:*global_with_initialization_issues*=init
37 // type:*Namespace::ClassName*=init
38 // src:file_with_tricky_code.cc
39 // src:ignore-global-initializers-issues.cc=init
40 //
41 // [dataflow]
42 // # Functions with pure functional semantics:
43 // fun:cos=functional
44 // fun:sin=functional
45 // ---
46 // Note that the wild card is in fact an llvm::Regex, but * is automatically
47 // replaced with .*
48 //
49 //===----------------------------------------------------------------------===//
50 
51 #ifndef LLVM_SUPPORT_SPECIALCASELIST_H
52 #define LLVM_SUPPORT_SPECIALCASELIST_H
53 
54 #include "llvm/ADT/StringMap.h"
55 #include "llvm/ADT/StringSet.h"
56 #include "llvm/Support/Regex.h"
59 #include <string>
60 #include <vector>
61 
62 namespace llvm {
63 class MemoryBuffer;
64 class Regex;
65 class StringRef;
66 
68 public:
69  /// Parses the special case list entries from files. On failure, returns
70  /// 0 and writes an error message to string.
71  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  static std::unique_ptr<SpecialCaseList> create(const MemoryBuffer *MB,
77  std::string &Error);
78  /// Parses the special case list entries from files. On failure, reports a
79  /// fatal error.
80  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 wildcard expression <E> in a given @Section.
91  StringRef Category = StringRef()) const;
92 
93  /// Returns the line number corresponding to the special case list entry if
94  /// the special case list contains a line
95  /// \code
96  /// @Prefix:<E>=@Category
97  /// \endcode
98  /// where @Query satisfies wildcard expression <E> in a given @Section.
99  /// Returns zero if there is no blacklist entry corresponding to this
100  /// expression.
101  unsigned inSectionBlame(StringRef Section, StringRef Prefix, StringRef Query,
102  StringRef Category = StringRef()) const;
103 
104 protected:
105  // Implementations of the create*() functions that can also be used by derived
106  // classes.
107  bool createInternal(const std::vector<std::string> &Paths,
108  vfs::FileSystem &VFS, std::string &Error);
109  bool createInternal(const MemoryBuffer *MB, std::string &Error);
110 
111  SpecialCaseList() = default;
112  SpecialCaseList(SpecialCaseList const &) = delete;
113  SpecialCaseList &operator=(SpecialCaseList const &) = delete;
114 
115  /// Represents a set of regular expressions. Regular expressions which are
116  /// "literal" (i.e. no regex metacharacters) are stored in Strings. The
117  /// reason for doing so is efficiency; StringMap is much faster at matching
118  /// literal strings than Regex.
119  class Matcher {
120  public:
121  bool insert(std::string Regexp, unsigned LineNumber, std::string &REError);
122  // Returns the line number in the source file that this query matches to.
123  // Returns zero if no match is found.
124  unsigned match(StringRef Query) const;
125 
126  private:
127  StringMap<unsigned> Strings;
128  TrigramIndex Trigrams;
129  std::vector<std::pair<std::unique_ptr<Regex>, unsigned>> RegExes;
130  };
131 
133 
134  struct Section {
135  Section(std::unique_ptr<Matcher> M) : SectionMatcher(std::move(M)){};
136 
137  std::unique_ptr<Matcher> SectionMatcher;
139  };
140 
141  std::vector<Section> Sections;
142 
143  /// Parses just-constructed SpecialCaseList entries from a memory buffer.
144  bool parse(const MemoryBuffer *MB, StringMap<size_t> &SectionsMap,
145  std::string &Error);
146 
147  // Helper method for derived classes to search by Prefix, Query, and Category
148  // once they have already resolved a section entry.
149  unsigned inSectionBlame(const SectionEntries &Entries, StringRef Prefix,
150  StringRef Query, StringRef Category) const;
151 };
152 
153 } // namespace llvm
154 
155 #endif // LLVM_SUPPORT_SPECIALCASELIST_H
156 
unsigned inSectionBlame(StringRef Section, StringRef Prefix, StringRef Query, StringRef Category=StringRef()) const
Returns the line number corresponding to the special case list entry if the special case list contain...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool inSection(StringRef Section, StringRef Prefix, StringRef Query, StringRef Category=StringRef()) const
Returns true, if special case list contains a line.
std::unique_ptr< Matcher > SectionMatcher
unsigned match(StringRef Query) const
Definition: BitVector.h:937
bool parse(const MemoryBuffer *MB, StringMap< size_t > &SectionsMap, std::string &Error)
Parses just-constructed SpecialCaseList entries from a memory buffer.
bool createInternal(const std::vector< std::string > &Paths, vfs::FileSystem &VFS, std::string &Error)
static std::unique_ptr< SpecialCaseList > createOrDie(const std::vector< std::string > &Paths, llvm::vfs::FileSystem &FS)
Parses the special case list entries from files.
SpecialCaseList & operator=(SpecialCaseList const &)=delete
std::vector< Section > Sections
The virtual file system interface.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:41
Represents a set of regular expressions.
Defines the virtual file system interface vfs::FileSystem.
static 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.
bool insert(std::string Regexp, unsigned LineNumber, std::string &REError)
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54