Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 1 | //===-- SpecialCaseList.cpp - special case list for sanitizers ------------===// |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 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 | // |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 10 | // This is a utility class for instrumentation passes (like AddressSanitizer |
| 11 | // or ThreadSanitizer) to avoid instrumenting some functions or global |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 12 | // variables, or to instrument some functions or global variables in a specific |
| 13 | // way, based on a user-supplied list. |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 17 | #include "llvm/Support/SpecialCaseList.h" |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.h" |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Peter Collingbourne | bace606 | 2013-08-05 17:48:04 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringSet.h" |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MemoryBuffer.h" |
| 23 | #include "llvm/Support/Regex.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include <string> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 26 | #include <system_error> |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include <utility> |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 28 | |
| 29 | namespace llvm { |
| 30 | |
Peter Collingbourne | bace606 | 2013-08-05 17:48:04 +0000 | [diff] [blame] | 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 { |
Peter Collingbourne | 05b9ebf | 2014-07-10 04:29:06 +0000 | [diff] [blame] | 37 | Entry() {} |
| 38 | Entry(Entry &&Other) |
| 39 | : Strings(std::move(Other.Strings)), RegEx(std::move(Other.RegEx)) {} |
Peter Collingbourne | 05b9ebf | 2014-07-10 04:29:06 +0000 | [diff] [blame] | 40 | |
Peter Collingbourne | bace606 | 2013-08-05 17:48:04 +0000 | [diff] [blame] | 41 | StringSet<> Strings; |
Peter Collingbourne | d5feb7b | 2014-07-10 03:55:02 +0000 | [diff] [blame] | 42 | std::unique_ptr<Regex> RegEx; |
Peter Collingbourne | bace606 | 2013-08-05 17:48:04 +0000 | [diff] [blame] | 43 | |
| 44 | bool match(StringRef Query) const { |
| 45 | return Strings.count(Query) || (RegEx && RegEx->match(Query)); |
| 46 | } |
| 47 | }; |
| 48 | |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame^] | 49 | SpecialCaseList::SpecialCaseList() : Entries(), Regexps(), IsCompiled(false) {} |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 50 | |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame^] | 51 | std::unique_ptr<SpecialCaseList> |
| 52 | SpecialCaseList::create(const std::vector<std::string> &Paths, |
| 53 | std::string &Error) { |
| 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(); |
| 60 | return nullptr; |
| 61 | } |
| 62 | std::string ParseError; |
| 63 | if (!SCL->parse(FileOrErr.get().get(), ParseError)) { |
| 64 | Error = (Twine("error parsing file '") + Path + "': " + ParseError).str(); |
| 65 | return nullptr; |
| 66 | } |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 67 | } |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame^] | 68 | SCL->compile(); |
| 69 | return SCL; |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 70 | } |
| 71 | |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 72 | std::unique_ptr<SpecialCaseList> SpecialCaseList::create(const MemoryBuffer *MB, |
| 73 | std::string &Error) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 74 | std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList()); |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 75 | if (!SCL->parse(MB, Error)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 76 | return nullptr; |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame^] | 77 | SCL->compile(); |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 78 | return SCL; |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame^] | 81 | std::unique_ptr<SpecialCaseList> |
| 82 | SpecialCaseList::createOrDie(const std::vector<std::string> &Paths) { |
Alexey Samsonov | e4b5fb8 | 2013-08-12 11:46:09 +0000 | [diff] [blame] | 83 | std::string Error; |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame^] | 84 | if (auto SCL = create(Paths, Error)) |
Alexey Samsonov | e4b5fb8 | 2013-08-12 11:46:09 +0000 | [diff] [blame] | 85 | return SCL; |
| 86 | report_fatal_error(Error); |
| 87 | } |
| 88 | |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 89 | bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) { |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 90 | // Iterate through each line in the blacklist file. |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 91 | SmallVector<StringRef, 16> Lines; |
Peter Collingbourne | 2eb048d | 2013-07-09 22:03:09 +0000 | [diff] [blame] | 92 | SplitString(MB->getBuffer(), Lines, "\n\r"); |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 93 | int LineNo = 1; |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame^] | 94 | for (auto I = Lines.begin(), E = Lines.end(); I != E; ++I, ++LineNo) { |
Alexey Samsonov | 8418442 | 2012-10-19 15:24:46 +0000 | [diff] [blame] | 95 | // Ignore empty lines and lines starting with "#" |
| 96 | if (I->empty() || I->startswith("#")) |
| 97 | continue; |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 98 | // Get our prefix and unparsed regexp. |
| 99 | std::pair<StringRef, StringRef> SplitLine = I->split(":"); |
| 100 | StringRef Prefix = SplitLine.first; |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 101 | if (SplitLine.second.empty()) { |
Nick Lewycky | 1af94eb | 2012-11-29 00:01:38 +0000 | [diff] [blame] | 102 | // Missing ':' in the line. |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame^] | 103 | Error = (Twine("malformed line ") + Twine(LineNo) + ": '" + |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 104 | SplitLine.first + "'").str(); |
| 105 | return false; |
Nick Lewycky | 1af94eb | 2012-11-29 00:01:38 +0000 | [diff] [blame] | 106 | } |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 107 | |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 108 | std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("="); |
| 109 | std::string Regexp = SplitRegexp.first; |
| 110 | StringRef Category = SplitRegexp.second; |
| 111 | |
Peter Collingbourne | bace606 | 2013-08-05 17:48:04 +0000 | [diff] [blame] | 112 | // See if we can store Regexp in Strings. |
| 113 | if (Regex::isLiteralERE(Regexp)) { |
| 114 | Entries[Prefix][Category].Strings.insert(Regexp); |
| 115 | continue; |
| 116 | } |
| 117 | |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 118 | // Replace * with .* |
| 119 | for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos; |
| 120 | pos += strlen(".*")) { |
| 121 | Regexp.replace(pos, strlen("*"), ".*"); |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 122 | } |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 123 | |
| 124 | // Check that the regexp is valid. |
| 125 | Regex CheckRE(Regexp); |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 126 | std::string REError; |
| 127 | if (!CheckRE.isValid(REError)) { |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame^] | 128 | Error = (Twine("malformed regex in line ") + Twine(LineNo) + ": '" + |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 129 | SplitLine.second + "': " + REError).str(); |
| 130 | return false; |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // Add this regexp into the proper group by its prefix. |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 134 | if (!Regexps[Prefix][Category].empty()) |
| 135 | Regexps[Prefix][Category] += "|"; |
Peter Collingbourne | 8b77f18 | 2013-07-16 17:56:07 +0000 | [diff] [blame] | 136 | Regexps[Prefix][Category] += "^" + Regexp + "$"; |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 137 | } |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame^] | 138 | return true; |
| 139 | } |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 140 | |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame^] | 141 | void SpecialCaseList::compile() { |
| 142 | assert(!IsCompiled && "compile() should only be called once"); |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 143 | // Iterate through each of the prefixes, and create Regexs for them. |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame^] | 144 | for (StringMap<StringMap<std::string>>::const_iterator I = Regexps.begin(), |
| 145 | E = Regexps.end(); |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 146 | I != E; ++I) { |
| 147 | for (StringMap<std::string>::const_iterator II = I->second.begin(), |
| 148 | IE = I->second.end(); |
| 149 | II != IE; ++II) { |
Peter Collingbourne | d5feb7b | 2014-07-10 03:55:02 +0000 | [diff] [blame] | 150 | Entries[I->getKey()][II->getKey()].RegEx.reset(new Regex(II->getValue())); |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 151 | } |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 152 | } |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame^] | 153 | Regexps.clear(); |
| 154 | IsCompiled = true; |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Peter Collingbourne | d5feb7b | 2014-07-10 03:55:02 +0000 | [diff] [blame] | 157 | SpecialCaseList::~SpecialCaseList() {} |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 158 | |
Craig Topper | 6dc4a8bc | 2014-08-30 16:48:02 +0000 | [diff] [blame] | 159 | bool SpecialCaseList::inSection(StringRef Section, StringRef Query, |
| 160 | StringRef Category) const { |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame^] | 161 | assert(IsCompiled && "SpecialCaseList::compile() was not called!"); |
Peter Collingbourne | bace606 | 2013-08-05 17:48:04 +0000 | [diff] [blame] | 162 | StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section); |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 163 | if (I == Entries.end()) return false; |
Peter Collingbourne | bace606 | 2013-08-05 17:48:04 +0000 | [diff] [blame] | 164 | StringMap<Entry>::const_iterator II = I->second.find(Category); |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 165 | if (II == I->second.end()) return false; |
| 166 | |
Peter Collingbourne | bace606 | 2013-08-05 17:48:04 +0000 | [diff] [blame] | 167 | return II->getValue().match(Query); |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | } // namespace llvm |