1 //===-- SpecialCaseList.cpp - special case list for sanitizers ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is a utility class for instrumentation passes (like AddressSanitizer
11 // or ThreadSanitizer) to avoid instrumenting some functions or global
12 // variables, or to instrument some functions or global variables in a specific
13 // way, based on a user-supplied list.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Support/SpecialCaseList.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/StringSet.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/Regex.h"
24 #include "llvm/Support/raw_ostream.h"
26 #include <system_error>
31 /// Represents a set of regular expressions. Regular expressions which are
32 /// "literal" (i.e. no regex metacharacters) are stored in Strings, while all
33 /// others are represented as a single pipe-separated regex in RegEx. The
34 /// reason for doing so is efficiency; StringSet is much faster at matching
35 /// literal strings than Regex.
36 struct SpecialCaseList::Entry {
39 : Strings(std::move(Other.Strings)), RegEx(std::move(Other.RegEx)) {}
42 std::unique_ptr<Regex> RegEx;
44 bool match(StringRef Query) const {
45 return Strings.count(Query) || (RegEx && RegEx->match(Query));
49 SpecialCaseList::SpecialCaseList() : Entries(), Regexps(), IsCompiled(false) {}
51 std::unique_ptr<SpecialCaseList>
52 SpecialCaseList::create(const std::vector<std::string> &Paths,
54 std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
55 for (auto Path : Paths) {
56 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
57 MemoryBuffer::getFile(Path);
58 if (std::error_code EC = FileOrErr.getError()) {
59 Error = (Twine("can't open file '") + Path + "': " + EC.message()).str();
62 std::string ParseError;
63 if (!SCL->parse(FileOrErr.get().get(), ParseError)) {
64 Error = (Twine("error parsing file '") + Path + "': " + ParseError).str();
72 std::unique_ptr<SpecialCaseList> SpecialCaseList::create(const MemoryBuffer *MB,
74 std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
75 if (!SCL->parse(MB, Error))
81 std::unique_ptr<SpecialCaseList>
82 SpecialCaseList::createOrDie(const std::vector<std::string> &Paths) {
84 if (auto SCL = create(Paths, Error))
86 report_fatal_error(Error);
89 bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
90 // Iterate through each line in the blacklist file.
91 SmallVector<StringRef, 16> Lines;
92 SplitString(MB->getBuffer(), Lines, "\n\r");
94 for (auto I = Lines.begin(), E = Lines.end(); I != E; ++I, ++LineNo) {
95 // Ignore empty lines and lines starting with "#"
96 if (I->empty() || I->startswith("#"))
98 // Get our prefix and unparsed regexp.
99 std::pair<StringRef, StringRef> SplitLine = I->split(":");
100 StringRef Prefix = SplitLine.first;
101 if (SplitLine.second.empty()) {
102 // Missing ':' in the line.
103 Error = (Twine("malformed line ") + Twine(LineNo) + ": '" +
104 SplitLine.first + "'").str();
108 std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
109 std::string Regexp = SplitRegexp.first;
110 StringRef Category = SplitRegexp.second;
112 // See if we can store Regexp in Strings.
113 if (Regex::isLiteralERE(Regexp)) {
114 Entries[Prefix][Category].Strings.insert(Regexp);
119 for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
120 pos += strlen(".*")) {
121 Regexp.replace(pos, strlen("*"), ".*");
124 // Check that the regexp is valid.
125 Regex CheckRE(Regexp);
127 if (!CheckRE.isValid(REError)) {
128 Error = (Twine("malformed regex in line ") + Twine(LineNo) + ": '" +
129 SplitLine.second + "': " + REError).str();
133 // Add this regexp into the proper group by its prefix.
134 if (!Regexps[Prefix][Category].empty())
135 Regexps[Prefix][Category] += "|";
136 Regexps[Prefix][Category] += "^" + Regexp + "$";
141 void SpecialCaseList::compile() {
142 assert(!IsCompiled && "compile() should only be called once");
143 // Iterate through each of the prefixes, and create Regexs for them.
144 for (StringMap<StringMap<std::string>>::const_iterator I = Regexps.begin(),
147 for (StringMap<std::string>::const_iterator II = I->second.begin(),
148 IE = I->second.end();
150 Entries[I->getKey()][II->getKey()].RegEx.reset(new Regex(II->getValue()));
157 SpecialCaseList::~SpecialCaseList() {}
159 bool SpecialCaseList::inSection(StringRef Section, StringRef Query,
160 StringRef Category) const {
161 assert(IsCompiled && "SpecialCaseList::compile() was not called!");
162 StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
163 if (I == Entries.end()) return false;
164 StringMap<Entry>::const_iterator II = I->second.find(Category);
165 if (II == I->second.end()) return false;
167 return II->getValue().match(Query);