SpecialCaseList.cpp revision cd81d94322a39503e4a3e87b6ee03d4fcb3465fb
1474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//===-- SpecialCaseList.cpp - special case list for sanitizers ------------===//
2474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//
3474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//                     The LLVM Compiler Infrastructure
4474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//
5474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org// This file is distributed under the University of Illinois Open Source
6474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org// License. See LICENSE.TXT for details.
7474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//
8474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//===----------------------------------------------------------------------===//
9474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//
10474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org// This is a utility class for instrumentation passes (like AddressSanitizer
11474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org// or ThreadSanitizer) to avoid instrumenting some functions or global
128b26fe55f3e4daa2311dbd2d95e8ac2b4e080685johannkoenig@chromium.org// variables, or to instrument some functions or global variables in a specific
138b26fe55f3e4daa2311dbd2d95e8ac2b4e080685johannkoenig@chromium.org// way, based on a user-supplied list.
14474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//
15474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//===----------------------------------------------------------------------===//
16474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
17474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#include "llvm/Support/SpecialCaseList.h"
18474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#include "llvm/ADT/STLExtras.h"
19474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#include "llvm/ADT/SmallVector.h"
20474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#include "llvm/ADT/StringExtras.h"
21474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#include "llvm/ADT/StringSet.h"
22474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#include "llvm/Support/MemoryBuffer.h"
23474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#include "llvm/Support/Regex.h"
24474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#include "llvm/Support/raw_ostream.h"
25ed759d81a39febed3a8a395386639d54307504aagrunell@chromium.org#include <string>
26474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#include <system_error>
27167514562bbce1eb0566271d6cb41d90d2b5ffa0hclam@chromium.org#include <utility>
28167514562bbce1eb0566271d6cb41d90d2b5ffa0hclam@chromium.org
29474eb7536515fb785e925cc9375d22817c416851hclam@chromium.orgnamespace llvm {
30474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
31474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org/// Represents a set of regular expressions.  Regular expressions which are
32474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org/// "literal" (i.e. no regex metacharacters) are stored in Strings, while all
33474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org/// others are represented as a single pipe-separated regex in RegEx.  The
34474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org/// reason for doing so is efficiency; StringSet is much faster at matching
35474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org/// literal strings than Regex.
36474eb7536515fb785e925cc9375d22817c416851hclam@chromium.orgstruct SpecialCaseList::Entry {
37474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org  Entry() {}
38474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org  Entry(Entry &&Other)
39474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org      : Strings(std::move(Other.Strings)), RegEx(std::move(Other.RegEx)) {}
40474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
41474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org  StringSet<> Strings;
42474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org  std::unique_ptr<Regex> RegEx;
43474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
44474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org  bool match(StringRef Query) const {
45474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org    return Strings.count(Query) || (RegEx && RegEx->match(Query));
46167514562bbce1eb0566271d6cb41d90d2b5ffa0hclam@chromium.org  }
47474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org};
48167514562bbce1eb0566271d6cb41d90d2b5ffa0hclam@chromium.org
49474eb7536515fb785e925cc9375d22817c416851hclam@chromium.orgSpecialCaseList::SpecialCaseList() : Entries() {}
50ed759d81a39febed3a8a395386639d54307504aagrunell@chromium.org
51ed759d81a39febed3a8a395386639d54307504aagrunell@chromium.orgSpecialCaseList *SpecialCaseList::create(
52ed759d81a39febed3a8a395386639d54307504aagrunell@chromium.org    const StringRef Path, std::string &Error) {
53167514562bbce1eb0566271d6cb41d90d2b5ffa0hclam@chromium.org  if (Path.empty())
54474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org    return new SpecialCaseList();
55ed759d81a39febed3a8a395386639d54307504aagrunell@chromium.org  ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
56ed759d81a39febed3a8a395386639d54307504aagrunell@chromium.org      MemoryBuffer::getFile(Path);
57474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org  if (std::error_code EC = FileOrErr.getError()) {
58474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org    Error = (Twine("Can't open file '") + Path + "': " + EC.message()).str();
59474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org    return nullptr;
60474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org  }
61474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org  return create(FileOrErr.get().get(), Error);
62474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org}
638b26fe55f3e4daa2311dbd2d95e8ac2b4e080685johannkoenig@chromium.org
64SpecialCaseList *SpecialCaseList::create(
65    const MemoryBuffer *MB, std::string &Error) {
66  std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
67  if (!SCL->parse(MB, Error))
68    return nullptr;
69  return SCL.release();
70}
71
72SpecialCaseList *SpecialCaseList::createOrDie(const StringRef Path) {
73  std::string Error;
74  if (SpecialCaseList *SCL = create(Path, Error))
75    return SCL;
76  report_fatal_error(Error);
77}
78
79bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
80  // Iterate through each line in the blacklist file.
81  SmallVector<StringRef, 16> Lines;
82  SplitString(MB->getBuffer(), Lines, "\n\r");
83  StringMap<StringMap<std::string> > Regexps;
84  assert(Entries.empty() &&
85         "parse() should be called on an empty SpecialCaseList");
86  int LineNo = 1;
87  for (SmallVectorImpl<StringRef>::iterator I = Lines.begin(), E = Lines.end();
88       I != E; ++I, ++LineNo) {
89    // Ignore empty lines and lines starting with "#"
90    if (I->empty() || I->startswith("#"))
91      continue;
92    // Get our prefix and unparsed regexp.
93    std::pair<StringRef, StringRef> SplitLine = I->split(":");
94    StringRef Prefix = SplitLine.first;
95    if (SplitLine.second.empty()) {
96      // Missing ':' in the line.
97      Error = (Twine("Malformed line ") + Twine(LineNo) + ": '" +
98               SplitLine.first + "'").str();
99      return false;
100    }
101
102    std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
103    std::string Regexp = SplitRegexp.first;
104    StringRef Category = SplitRegexp.second;
105
106    // Backwards compatibility.
107    if (Prefix == "global-init") {
108      Prefix = "global";
109      Category = "init";
110    } else if (Prefix == "global-init-type") {
111      Prefix = "type";
112      Category = "init";
113    } else if (Prefix == "global-init-src") {
114      Prefix = "src";
115      Category = "init";
116    }
117
118    // See if we can store Regexp in Strings.
119    if (Regex::isLiteralERE(Regexp)) {
120      Entries[Prefix][Category].Strings.insert(Regexp);
121      continue;
122    }
123
124    // Replace * with .*
125    for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
126         pos += strlen(".*")) {
127      Regexp.replace(pos, strlen("*"), ".*");
128    }
129
130    // Check that the regexp is valid.
131    Regex CheckRE(Regexp);
132    std::string REError;
133    if (!CheckRE.isValid(REError)) {
134      Error = (Twine("Malformed regex in line ") + Twine(LineNo) + ": '" +
135               SplitLine.second + "': " + REError).str();
136      return false;
137    }
138
139    // Add this regexp into the proper group by its prefix.
140    if (!Regexps[Prefix][Category].empty())
141      Regexps[Prefix][Category] += "|";
142    Regexps[Prefix][Category] += "^" + Regexp + "$";
143  }
144
145  // Iterate through each of the prefixes, and create Regexs for them.
146  for (StringMap<StringMap<std::string> >::const_iterator I = Regexps.begin(),
147                                                          E = Regexps.end();
148       I != E; ++I) {
149    for (StringMap<std::string>::const_iterator II = I->second.begin(),
150                                                IE = I->second.end();
151         II != IE; ++II) {
152      Entries[I->getKey()][II->getKey()].RegEx.reset(new Regex(II->getValue()));
153    }
154  }
155  return true;
156}
157
158SpecialCaseList::~SpecialCaseList() {}
159
160bool SpecialCaseList::inSection(const StringRef Section, const StringRef Query,
161                                const StringRef Category) const {
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;
166
167  return II->getValue().match(Query);
168}
169
170}  // namespace llvm
171