SpecialCaseList.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //===-- SpecialCaseList.cpp - special case list for sanitizers ------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  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.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Support/SpecialCaseList.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/ADT/StringExtras.h"
  19. #include "llvm/ADT/StringSet.h"
  20. #include "llvm/Support/MemoryBuffer.h"
  21. #include "llvm/Support/Regex.h"
  22. #include <string>
  23. #include <system_error>
  24. #include <utility>
  25. namespace llvm {
  26. /// Represents a set of regular expressions. Regular expressions which are
  27. /// "literal" (i.e. no regex metacharacters) are stored in Strings, while all
  28. /// others are represented as a single pipe-separated regex in RegEx. The
  29. /// reason for doing so is efficiency; StringSet is much faster at matching
  30. /// literal strings than Regex.
  31. struct SpecialCaseList::Entry {
  32. Entry() {}
  33. Entry(Entry &&Other)
  34. : Strings(std::move(Other.Strings)), RegEx(std::move(Other.RegEx)) {}
  35. StringSet<> Strings;
  36. std::unique_ptr<Regex> RegEx;
  37. bool match(StringRef Query) const {
  38. return Strings.count(Query) || (RegEx && RegEx->match(Query));
  39. }
  40. };
  41. SpecialCaseList::SpecialCaseList() : Entries(), Regexps(), IsCompiled(false) {}
  42. std::unique_ptr<SpecialCaseList>
  43. SpecialCaseList::create(const std::vector<std::string> &Paths,
  44. std::string &Error) {
  45. std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
  46. for (auto Path : Paths) {
  47. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  48. MemoryBuffer::getFile(Path);
  49. if (std::error_code EC = FileOrErr.getError()) {
  50. Error = (Twine("can't open file '") + Path + "': " + EC.message()).str();
  51. return nullptr;
  52. }
  53. std::string ParseError;
  54. if (!SCL->parse(FileOrErr.get().get(), ParseError)) {
  55. Error = (Twine("error parsing file '") + Path + "': " + ParseError).str();
  56. return nullptr;
  57. }
  58. }
  59. SCL->compile();
  60. return SCL;
  61. }
  62. std::unique_ptr<SpecialCaseList> SpecialCaseList::create(const MemoryBuffer *MB,
  63. std::string &Error) {
  64. std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
  65. if (!SCL->parse(MB, Error))
  66. return nullptr;
  67. SCL->compile();
  68. return SCL;
  69. }
  70. std::unique_ptr<SpecialCaseList>
  71. SpecialCaseList::createOrDie(const std::vector<std::string> &Paths) {
  72. std::string Error;
  73. if (auto SCL = create(Paths, Error))
  74. return SCL;
  75. report_fatal_error(Error);
  76. }
  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. int LineNo = 1;
  82. for (auto I = Lines.begin(), E = Lines.end(); I != E; ++I, ++LineNo) {
  83. // Ignore empty lines and lines starting with "#"
  84. if (I->empty() || I->startswith("#"))
  85. continue;
  86. // Get our prefix and unparsed regexp.
  87. std::pair<StringRef, StringRef> SplitLine = I->split(":");
  88. StringRef Prefix = SplitLine.first;
  89. if (SplitLine.second.empty()) {
  90. // Missing ':' in the line.
  91. Error = (Twine("malformed line ") + Twine(LineNo) + ": '" +
  92. SplitLine.first + "'").str();
  93. return false;
  94. }
  95. std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
  96. std::string Regexp = SplitRegexp.first;
  97. StringRef Category = SplitRegexp.second;
  98. // See if we can store Regexp in Strings.
  99. if (Regex::isLiteralERE(Regexp)) {
  100. Entries[Prefix][Category].Strings.insert(Regexp);
  101. continue;
  102. }
  103. // Replace * with .*
  104. for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
  105. pos += strlen(".*")) {
  106. Regexp.replace(pos, strlen("*"), ".*");
  107. }
  108. // Check that the regexp is valid.
  109. Regex CheckRE(Regexp);
  110. std::string REError;
  111. if (!CheckRE.isValid(REError)) {
  112. Error = (Twine("malformed regex in line ") + Twine(LineNo) + ": '" +
  113. SplitLine.second + "': " + REError).str();
  114. return false;
  115. }
  116. // Add this regexp into the proper group by its prefix.
  117. if (!Regexps[Prefix][Category].empty())
  118. Regexps[Prefix][Category] += "|";
  119. Regexps[Prefix][Category] += "^" + Regexp + "$";
  120. }
  121. return true;
  122. }
  123. void SpecialCaseList::compile() {
  124. assert(!IsCompiled && "compile() should only be called once");
  125. // Iterate through each of the prefixes, and create Regexs for them.
  126. for (StringMap<StringMap<std::string>>::const_iterator I = Regexps.begin(),
  127. E = Regexps.end();
  128. I != E; ++I) {
  129. for (StringMap<std::string>::const_iterator II = I->second.begin(),
  130. IE = I->second.end();
  131. II != IE; ++II) {
  132. Entries[I->getKey()][II->getKey()].RegEx.reset(new Regex(II->getValue()));
  133. }
  134. }
  135. Regexps.clear();
  136. IsCompiled = true;
  137. }
  138. SpecialCaseList::~SpecialCaseList() {}
  139. bool SpecialCaseList::inSection(StringRef Section, StringRef Query,
  140. StringRef Category) const {
  141. assert(IsCompiled && "SpecialCaseList::compile() was not called!");
  142. StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
  143. if (I == Entries.end()) return false;
  144. StringMap<Entry>::const_iterator II = I->second.find(Category);
  145. if (II == I->second.end()) return false;
  146. return II->getValue().match(Query);
  147. }
  148. } // namespace llvm