LLVM 18.0.0git
SpecialCaseList.cpp
Go to the documentation of this file.
1//===-- SpecialCaseList.cpp - special case list for sanitizers ------------===//
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//
9// This is a utility class for instrumentation passes (like AddressSanitizer
10// or ThreadSanitizer) to avoid instrumenting some functions or global
11// variables, or to instrument some functions or global variables in a specific
12// way, based on a user-supplied list.
13//
14//===----------------------------------------------------------------------===//
15
20#include <stdio.h>
21#include <string>
22#include <system_error>
23#include <utility>
24
25namespace llvm {
26
28 bool UseGlobs) {
29 if (Pattern.empty())
31 Twine("Supplied ") +
32 (UseGlobs ? "glob" : "regex") + " was blank");
33
34 if (!UseGlobs) {
35 // Replace * with .*
36 auto Regexp = Pattern.str();
37 for (size_t pos = 0; (pos = Regexp.find('*', pos)) != std::string::npos;
38 pos += strlen(".*")) {
39 Regexp.replace(pos, strlen("*"), ".*");
40 }
41
42 Regexp = (Twine("^(") + StringRef(Regexp) + ")$").str();
43
44 // Check that the regexp is valid.
45 Regex CheckRE(Regexp);
46 std::string REError;
47 if (!CheckRE.isValid(REError))
49
50 RegExes.emplace_back(std::make_pair(
51 std::make_unique<Regex>(std::move(CheckRE)), LineNumber));
52
53 return Error::success();
54 }
55
56 auto [It, DidEmplace] = Globs.try_emplace(Pattern);
57 if (DidEmplace) {
58 // We must be sure to use the string in the map rather than the provided
59 // reference which could be destroyed before match() is called
60 Pattern = It->getKey();
61 auto &Pair = It->getValue();
62 if (auto Err = GlobPattern::create(Pattern, /*MaxSubPatterns=*/1024)
63 .moveInto(Pair.first))
64 return Err;
65 Pair.second = LineNumber;
66 }
67 return Error::success();
68}
69
71 for (const auto &[Pattern, Pair] : Globs)
72 if (Pair.first.match(Query))
73 return Pair.second;
74 for (const auto &[Regex, LineNumber] : RegExes)
75 if (Regex->match(Query))
76 return LineNumber;
77 return 0;
78}
79
80// TODO: Refactor this to return Expected<...>
81std::unique_ptr<SpecialCaseList>
82SpecialCaseList::create(const std::vector<std::string> &Paths,
83 llvm::vfs::FileSystem &FS, std::string &Error) {
84 std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
85 if (SCL->createInternal(Paths, FS, Error))
86 return SCL;
87 return nullptr;
88}
89
90std::unique_ptr<SpecialCaseList> SpecialCaseList::create(const MemoryBuffer *MB,
91 std::string &Error) {
92 std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
93 if (SCL->createInternal(MB, Error))
94 return SCL;
95 return nullptr;
96}
97
98std::unique_ptr<SpecialCaseList>
99SpecialCaseList::createOrDie(const std::vector<std::string> &Paths,
101 std::string Error;
102 if (auto SCL = create(Paths, FS, Error))
103 return SCL;
105}
106
107bool SpecialCaseList::createInternal(const std::vector<std::string> &Paths,
108 vfs::FileSystem &VFS, std::string &Error) {
109 for (const auto &Path : Paths) {
111 VFS.getBufferForFile(Path);
112 if (std::error_code EC = FileOrErr.getError()) {
113 Error = (Twine("can't open file '") + Path + "': " + EC.message()).str();
114 return false;
115 }
116 std::string ParseError;
117 if (!parse(FileOrErr.get().get(), ParseError)) {
118 Error = (Twine("error parsing file '") + Path + "': " + ParseError).str();
119 return false;
120 }
121 }
122 return true;
123}
124
126 std::string &Error) {
127 if (!parse(MB, Error))
128 return false;
129 return true;
130}
131
133SpecialCaseList::addSection(StringRef SectionStr, unsigned LineNo,
134 bool UseGlobs) {
135 auto [It, DidEmplace] = Sections.try_emplace(SectionStr);
136 auto &Section = It->getValue();
137 if (DidEmplace)
138 if (auto Err = Section.SectionMatcher->insert(SectionStr, LineNo, UseGlobs))
140 "malformed section at line " + Twine(LineNo) +
141 ": '" + SectionStr +
142 "': " + toString(std::move(Err)));
143 return &Section;
144}
145
146bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
147 Section *CurrentSection;
148 if (auto Err = addSection("*", 1).moveInto(CurrentSection)) {
149 Error = toString(std::move(Err));
150 return false;
151 }
152
153 // In https://reviews.llvm.org/D154014 we transitioned to using globs instead
154 // of regexes to match patterns in special case lists. Since this was a
155 // breaking change, we will temporarily support the original behavior using
156 // regexes. If "#!special-case-list-v2" is the first line of the file, then
157 // we will use the new behavior using globs. For more details, see
158 // https://discourse.llvm.org/t/use-glob-instead-of-regex-for-specialcaselists/71666
159 bool UseGlobs = MB->getBuffer().starts_with("#!special-case-list-v2\n");
160
161 for (line_iterator LineIt(*MB, /*SkipBlanks=*/true, /*CommentMarker=*/'#');
162 !LineIt.is_at_eof(); LineIt++) {
163 unsigned LineNo = LineIt.line_number();
164 StringRef Line = LineIt->trim();
165 if (Line.empty())
166 continue;
167
168 // Save section names
169 if (Line.starts_with("[")) {
170 if (!Line.ends_with("]")) {
171 Error =
172 ("malformed section header on line " + Twine(LineNo) + ": " + Line)
173 .str();
174 return false;
175 }
176
177 if (auto Err = addSection(Line.drop_front().drop_back(), LineNo, UseGlobs)
178 .moveInto(CurrentSection)) {
179 Error = toString(std::move(Err));
180 return false;
181 }
182 continue;
183 }
184
185 // Get our prefix and unparsed glob.
186 auto [Prefix, Postfix] = Line.split(":");
187 if (Postfix.empty()) {
188 // Missing ':' in the line.
189 Error = ("malformed line " + Twine(LineNo) + ": '" + Line + "'").str();
190 return false;
191 }
192
193 auto [Pattern, Category] = Postfix.split("=");
194 auto &Entry = CurrentSection->Entries[Prefix][Category];
195 if (auto Err = Entry.insert(Pattern, LineNo, UseGlobs)) {
196 Error =
197 (Twine("malformed ") + (UseGlobs ? "glob" : "regex") + " in line " +
198 Twine(LineNo) + ": '" + Pattern + "': " + toString(std::move(Err)))
199 .str();
200 return false;
201 }
202 }
203 return true;
204}
205
207
209 StringRef Query, StringRef Category) const {
210 return inSectionBlame(Section, Prefix, Query, Category);
211}
212
214 StringRef Query,
215 StringRef Category) const {
216 for (const auto &It : Sections) {
217 const auto &S = It.getValue();
218 if (S.SectionMatcher->match(Section)) {
219 unsigned Blame = inSectionBlame(S.Entries, Prefix, Query, Category);
220 if (Blame)
221 return Blame;
222 }
223 }
224 return 0;
225}
226
228 StringRef Prefix, StringRef Query,
229 StringRef Category) const {
230 SectionEntries::const_iterator I = Entries.find(Prefix);
231 if (I == Entries.end()) return 0;
232 StringMap<Matcher>::const_iterator II = I->second.find(Category);
233 if (II == I->second.end()) return 0;
234
235 return II->getValue().match(Query);
236}
237
238} // namespace llvm
#define I(x, y, z)
Definition: MD5.cpp:58
Defines the virtual file system interface vfs::FileSystem.
Represents either an error or a value T.
Definition: ErrorOr.h:56
reference get()
Definition: ErrorOr.h:149
std::error_code getError() const
Definition: ErrorOr.h:152
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
static Expected< GlobPattern > create(StringRef Pat, std::optional< size_t > MaxSubPatterns={})
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
StringRef getBuffer() const
Definition: MemoryBuffer.h:70
bool isValid(std::string &Error) const
isValid - returns the error encountered during regex compilation, if any.
Definition: Regex.cpp:69
bool match(StringRef String, SmallVectorImpl< StringRef > *Matches=nullptr, std::string *Error=nullptr) const
matches - Match the regex against a given String.
Definition: Regex.cpp:83
unsigned match(StringRef Query) const
Error insert(StringRef Pattern, unsigned LineNumber, bool UseRegex)
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.
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.
StringMap< Section > Sections
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...
Expected< Section * > addSection(StringRef SectionStr, unsigned LineNo, bool UseGlobs=true)
bool inSection(StringRef Section, StringRef Prefix, StringRef Query, StringRef Category=StringRef()) const
Returns true, if special case list contains a line.
bool parse(const MemoryBuffer *MB, std::string &Error)
Parses just-constructed SpecialCaseList entries from a memory buffer.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:257
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:33
bool is_at_eof() const
Return true if we've reached EOF or are an "end" iterator.
Definition: LineIterator.h:60
The virtual file system interface.
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(const Twine &Name, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
This is a convenience method that opens a file, gets its content and then closes the file.
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1244
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
std::unique_ptr< Matcher > SectionMatcher
Definition: regcomp.c:192