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)) {}
40 Entry &operator=(Entry &&Other) {
41 Strings = std::move(Other.Strings);
42 RegEx = std::move(Other.RegEx);
47 std::unique_ptr<Regex> RegEx;
49 bool match(StringRef Query) const {
50 return Strings.count(Query) || (RegEx && RegEx->match(Query));
54 SpecialCaseList::SpecialCaseList() : Entries() {}
56 SpecialCaseList *SpecialCaseList::create(
57 const StringRef Path, std::string &Error) {
59 return new SpecialCaseList();
60 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
61 MemoryBuffer::getFile(Path);
62 if (std::error_code EC = FileOrErr.getError()) {
63 Error = (Twine("Can't open file '") + Path + "': " + EC.message()).str();
66 return create(FileOrErr.get().get(), Error);
69 SpecialCaseList *SpecialCaseList::create(
70 const MemoryBuffer *MB, std::string &Error) {
71 std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
72 if (!SCL->parse(MB, Error))
77 SpecialCaseList *SpecialCaseList::createOrDie(const StringRef Path) {
79 if (SpecialCaseList *SCL = create(Path, Error))
81 report_fatal_error(Error);
84 bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
85 // Iterate through each line in the blacklist file.
86 SmallVector<StringRef, 16> Lines;
87 SplitString(MB->getBuffer(), Lines, "\n\r");
88 StringMap<StringMap<std::string> > Regexps;
89 assert(Entries.empty() &&
90 "parse() should be called on an empty SpecialCaseList");
92 for (SmallVectorImpl<StringRef>::iterator I = Lines.begin(), E = Lines.end();
93 I != E; ++I, ++LineNo) {
94 // Ignore empty lines and lines starting with "#"
95 if (I->empty() || I->startswith("#"))
97 // Get our prefix and unparsed regexp.
98 std::pair<StringRef, StringRef> SplitLine = I->split(":");
99 StringRef Prefix = SplitLine.first;
100 if (SplitLine.second.empty()) {
101 // Missing ':' in the line.
102 Error = (Twine("Malformed line ") + Twine(LineNo) + ": '" +
103 SplitLine.first + "'").str();
107 std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
108 std::string Regexp = SplitRegexp.first;
109 StringRef Category = SplitRegexp.second;
111 // Backwards compatibility.
112 if (Prefix == "global-init") {
115 } else if (Prefix == "global-init-type") {
118 } else if (Prefix == "global-init-src") {
123 // See if we can store Regexp in Strings.
124 if (Regex::isLiteralERE(Regexp)) {
125 Entries[Prefix][Category].Strings.insert(Regexp);
130 for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
131 pos += strlen(".*")) {
132 Regexp.replace(pos, strlen("*"), ".*");
135 // Check that the regexp is valid.
136 Regex CheckRE(Regexp);
138 if (!CheckRE.isValid(REError)) {
139 Error = (Twine("Malformed regex in line ") + Twine(LineNo) + ": '" +
140 SplitLine.second + "': " + REError).str();
144 // Add this regexp into the proper group by its prefix.
145 if (!Regexps[Prefix][Category].empty())
146 Regexps[Prefix][Category] += "|";
147 Regexps[Prefix][Category] += "^" + Regexp + "$";
150 // Iterate through each of the prefixes, and create Regexs for them.
151 for (StringMap<StringMap<std::string> >::const_iterator I = Regexps.begin(),
154 for (StringMap<std::string>::const_iterator II = I->second.begin(),
155 IE = I->second.end();
157 Entries[I->getKey()][II->getKey()].RegEx.reset(new Regex(II->getValue()));
163 SpecialCaseList::~SpecialCaseList() {}
165 bool SpecialCaseList::inSection(const StringRef Section, const StringRef Query,
166 const StringRef Category) const {
167 StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
168 if (I == Entries.end()) return false;
169 StringMap<Entry>::const_iterator II = I->second.find(Category);
170 if (II == I->second.end()) return false;
172 return II->getValue().match(Query);