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 {
40 Entry() : RegEx(nullptr) {}
42 bool match(StringRef Query) const {
43 return Strings.count(Query) || (RegEx && RegEx->match(Query));
47 SpecialCaseList::SpecialCaseList() : Entries() {}
49 SpecialCaseList *SpecialCaseList::create(
50 const StringRef Path, std::string &Error) {
52 return new SpecialCaseList();
53 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
54 MemoryBuffer::getFile(Path);
55 if (std::error_code EC = FileOrErr.getError()) {
56 Error = (Twine("Can't open file '") + Path + "': " + EC.message()).str();
59 return create(FileOrErr.get().get(), Error);
62 SpecialCaseList *SpecialCaseList::create(
63 const MemoryBuffer *MB, std::string &Error) {
64 std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
65 if (!SCL->parse(MB, Error))
70 SpecialCaseList *SpecialCaseList::createOrDie(const StringRef Path) {
72 if (SpecialCaseList *SCL = create(Path, Error))
74 report_fatal_error(Error);
77 bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
78 // Iterate through each line in the blacklist file.
79 SmallVector<StringRef, 16> Lines;
80 SplitString(MB->getBuffer(), Lines, "\n\r");
81 StringMap<StringMap<std::string> > Regexps;
82 assert(Entries.empty() &&
83 "parse() should be called on an empty SpecialCaseList");
85 for (SmallVectorImpl<StringRef>::iterator I = Lines.begin(), E = Lines.end();
86 I != E; ++I, ++LineNo) {
87 // Ignore empty lines and lines starting with "#"
88 if (I->empty() || I->startswith("#"))
90 // Get our prefix and unparsed regexp.
91 std::pair<StringRef, StringRef> SplitLine = I->split(":");
92 StringRef Prefix = SplitLine.first;
93 if (SplitLine.second.empty()) {
94 // Missing ':' in the line.
95 Error = (Twine("Malformed line ") + Twine(LineNo) + ": '" +
96 SplitLine.first + "'").str();
100 std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
101 std::string Regexp = SplitRegexp.first;
102 StringRef Category = SplitRegexp.second;
104 // Backwards compatibility.
105 if (Prefix == "global-init") {
108 } else if (Prefix == "global-init-type") {
111 } else if (Prefix == "global-init-src") {
116 // See if we can store Regexp in Strings.
117 if (Regex::isLiteralERE(Regexp)) {
118 Entries[Prefix][Category].Strings.insert(Regexp);
123 for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
124 pos += strlen(".*")) {
125 Regexp.replace(pos, strlen("*"), ".*");
128 // Check that the regexp is valid.
129 Regex CheckRE(Regexp);
131 if (!CheckRE.isValid(REError)) {
132 Error = (Twine("Malformed regex in line ") + Twine(LineNo) + ": '" +
133 SplitLine.second + "': " + REError).str();
137 // Add this regexp into the proper group by its prefix.
138 if (!Regexps[Prefix][Category].empty())
139 Regexps[Prefix][Category] += "|";
140 Regexps[Prefix][Category] += "^" + Regexp + "$";
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 = new Regex(II->getValue());
156 SpecialCaseList::~SpecialCaseList() {
157 for (StringMap<StringMap<Entry> >::iterator I = Entries.begin(),
160 for (StringMap<Entry>::const_iterator II = I->second.begin(),
161 IE = I->second.end();
163 delete II->second.RegEx;
168 bool SpecialCaseList::inSection(const StringRef Section, const StringRef Query,
169 const StringRef Category) const {
170 StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
171 if (I == Entries.end()) return false;
172 StringMap<Entry>::const_iterator II = I->second.find(Category);
173 if (II == I->second.end()) return false;
175 return II->getValue().match(Query);