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 | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 49 | SpecialCaseList::SpecialCaseList() : Entries() {} |
| 50 | |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 51 | std::unique_ptr<SpecialCaseList> SpecialCaseList::create(StringRef Path, |
| 52 | std::string &Error) { |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 53 | if (Path.empty()) |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 54 | return std::unique_ptr<SpecialCaseList>(new SpecialCaseList()); |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 55 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 56 | MemoryBuffer::getFile(Path); |
| 57 | if (std::error_code EC = FileOrErr.getError()) { |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 58 | Error = (Twine("Can't open file '") + Path + "': " + EC.message()).str(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 59 | return nullptr; |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 60 | } |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 61 | return create(FileOrErr.get().get(), Error); |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 62 | } |
| 63 | |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 64 | std::unique_ptr<SpecialCaseList> SpecialCaseList::create(const MemoryBuffer *MB, |
| 65 | std::string &Error) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 66 | std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList()); |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 67 | if (!SCL->parse(MB, Error)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 68 | return nullptr; |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 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::createOrDie(StringRef Path) { |
Alexey Samsonov | e4b5fb8 | 2013-08-12 11:46:09 +0000 | [diff] [blame] | 73 | std::string Error; |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 74 | if (auto SCL = create(Path, Error)) |
Alexey Samsonov | e4b5fb8 | 2013-08-12 11:46:09 +0000 | [diff] [blame] | 75 | return SCL; |
| 76 | report_fatal_error(Error); |
| 77 | } |
| 78 | |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 79 | bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) { |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 80 | // Iterate through each line in the blacklist file. |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 81 | SmallVector<StringRef, 16> Lines; |
Peter Collingbourne | 2eb048d | 2013-07-09 22:03:09 +0000 | [diff] [blame] | 82 | SplitString(MB->getBuffer(), Lines, "\n\r"); |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 83 | StringMap<StringMap<std::string> > Regexps; |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 84 | assert(Entries.empty() && |
| 85 | "parse() should be called on an empty SpecialCaseList"); |
| 86 | int LineNo = 1; |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 87 | for (SmallVectorImpl<StringRef>::iterator I = Lines.begin(), E = Lines.end(); |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 88 | I != E; ++I, ++LineNo) { |
Alexey Samsonov | 8418442 | 2012-10-19 15:24:46 +0000 | [diff] [blame] | 89 | // Ignore empty lines and lines starting with "#" |
| 90 | if (I->empty() || I->startswith("#")) |
| 91 | continue; |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 92 | // Get our prefix and unparsed regexp. |
| 93 | std::pair<StringRef, StringRef> SplitLine = I->split(":"); |
| 94 | StringRef Prefix = SplitLine.first; |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 95 | if (SplitLine.second.empty()) { |
Nick Lewycky | 1af94eb | 2012-11-29 00:01:38 +0000 | [diff] [blame] | 96 | // Missing ':' in the line. |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 97 | Error = (Twine("Malformed line ") + Twine(LineNo) + ": '" + |
| 98 | SplitLine.first + "'").str(); |
| 99 | return false; |
Nick Lewycky | 1af94eb | 2012-11-29 00:01:38 +0000 | [diff] [blame] | 100 | } |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 101 | |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 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 | |
Peter Collingbourne | bace606 | 2013-08-05 17:48:04 +0000 | [diff] [blame] | 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 | |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 124 | // Replace * with .* |
| 125 | for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos; |
| 126 | pos += strlen(".*")) { |
| 127 | Regexp.replace(pos, strlen("*"), ".*"); |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 128 | } |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 129 | |
| 130 | // Check that the regexp is valid. |
| 131 | Regex CheckRE(Regexp); |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 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; |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // Add this regexp into the proper group by its prefix. |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 140 | if (!Regexps[Prefix][Category].empty()) |
| 141 | Regexps[Prefix][Category] += "|"; |
Peter Collingbourne | 8b77f18 | 2013-07-16 17:56:07 +0000 | [diff] [blame] | 142 | Regexps[Prefix][Category] += "^" + Regexp + "$"; |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 143 | } |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 144 | |
| 145 | // Iterate through each of the prefixes, and create Regexs for them. |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 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) { |
Peter Collingbourne | d5feb7b | 2014-07-10 03:55:02 +0000 | [diff] [blame] | 152 | Entries[I->getKey()][II->getKey()].RegEx.reset(new Regex(II->getValue())); |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 153 | } |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 154 | } |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 155 | return true; |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Peter Collingbourne | d5feb7b | 2014-07-10 03:55:02 +0000 | [diff] [blame] | 158 | SpecialCaseList::~SpecialCaseList() {} |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 159 | |
Craig Topper | 6dc4a8bc | 2014-08-30 16:48:02 +0000 | [diff] [blame] | 160 | bool SpecialCaseList::inSection(StringRef Section, StringRef Query, |
| 161 | StringRef Category) const { |
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 |