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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 9 | // This is a utility class for instrumentation passes (like AddressSanitizer |
| 10 | // or ThreadSanitizer) to avoid instrumenting some functions or global |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 11 | // variables, or to instrument some functions or global variables in a specific |
| 12 | // way, based on a user-supplied list. |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 16 | #include "llvm/Support/SpecialCaseList.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 19 | #include "llvm/Support/MemoryBuffer.h" |
| 20 | #include "llvm/Support/Regex.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include <string> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 22 | #include <system_error> |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include <utility> |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 24 | |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 26 | namespace llvm { |
| 27 | |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 28 | bool SpecialCaseList::Matcher::insert(std::string Regexp, |
Mitch Phillips | 40d6663 | 2017-11-07 21:16:46 +0000 | [diff] [blame] | 29 | unsigned LineNumber, |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 30 | std::string &REError) { |
Mitch Phillips | fa2eda8 | 2017-10-24 23:56:12 +0000 | [diff] [blame] | 31 | if (Regexp.empty()) { |
| 32 | REError = "Supplied regexp was blank"; |
| 33 | return false; |
| 34 | } |
| 35 | |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 36 | if (Regex::isLiteralERE(Regexp)) { |
Mitch Phillips | 40d6663 | 2017-11-07 21:16:46 +0000 | [diff] [blame] | 37 | Strings[Regexp] = LineNumber; |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 38 | return true; |
Peter Collingbourne | bace606 | 2013-08-05 17:48:04 +0000 | [diff] [blame] | 39 | } |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 40 | Trigrams.insert(Regexp); |
Peter Collingbourne | bace606 | 2013-08-05 17:48:04 +0000 | [diff] [blame] | 41 | |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 42 | // Replace * with .* |
| 43 | for (size_t pos = 0; (pos = Regexp.find('*', pos)) != std::string::npos; |
| 44 | pos += strlen(".*")) { |
| 45 | Regexp.replace(pos, strlen("*"), ".*"); |
| 46 | } |
| 47 | |
Mitch Phillips | 40d6663 | 2017-11-07 21:16:46 +0000 | [diff] [blame] | 48 | Regexp = (Twine("^(") + StringRef(Regexp) + ")$").str(); |
| 49 | |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 50 | // Check that the regexp is valid. |
| 51 | Regex CheckRE(Regexp); |
| 52 | if (!CheckRE.isValid(REError)) |
| 53 | return false; |
| 54 | |
Mitch Phillips | 40d6663 | 2017-11-07 21:16:46 +0000 | [diff] [blame] | 55 | RegExes.emplace_back( |
| 56 | std::make_pair(make_unique<Regex>(std::move(CheckRE)), LineNumber)); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 57 | return true; |
| 58 | } |
| 59 | |
Mitch Phillips | 40d6663 | 2017-11-07 21:16:46 +0000 | [diff] [blame] | 60 | unsigned SpecialCaseList::Matcher::match(StringRef Query) const { |
| 61 | auto It = Strings.find(Query); |
| 62 | if (It != Strings.end()) |
| 63 | return It->second; |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 64 | if (Trigrams.isDefinitelyOut(Query)) |
| 65 | return false; |
Mitch Phillips | 40d6663 | 2017-11-07 21:16:46 +0000 | [diff] [blame] | 66 | for (auto& RegExKV : RegExes) |
| 67 | if (RegExKV.first->match(Query)) |
| 68 | return RegExKV.second; |
| 69 | return 0; |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 72 | std::unique_ptr<SpecialCaseList> |
| 73 | SpecialCaseList::create(const std::vector<std::string> &Paths, |
| 74 | std::string &Error) { |
| 75 | std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList()); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 76 | if (SCL->createInternal(Paths, Error)) |
| 77 | return SCL; |
| 78 | return nullptr; |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 79 | } |
| 80 | |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 81 | std::unique_ptr<SpecialCaseList> SpecialCaseList::create(const MemoryBuffer *MB, |
| 82 | std::string &Error) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 83 | std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList()); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 84 | if (SCL->createInternal(MB, Error)) |
| 85 | return SCL; |
| 86 | return nullptr; |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 89 | std::unique_ptr<SpecialCaseList> |
| 90 | SpecialCaseList::createOrDie(const std::vector<std::string> &Paths) { |
Alexey Samsonov | e4b5fb8 | 2013-08-12 11:46:09 +0000 | [diff] [blame] | 91 | std::string Error; |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 92 | if (auto SCL = create(Paths, Error)) |
Alexey Samsonov | e4b5fb8 | 2013-08-12 11:46:09 +0000 | [diff] [blame] | 93 | return SCL; |
| 94 | report_fatal_error(Error); |
| 95 | } |
| 96 | |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 97 | bool SpecialCaseList::createInternal(const std::vector<std::string> &Paths, |
| 98 | std::string &Error) { |
| 99 | StringMap<size_t> Sections; |
| 100 | for (const auto &Path : Paths) { |
| 101 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 102 | MemoryBuffer::getFile(Path); |
| 103 | if (std::error_code EC = FileOrErr.getError()) { |
| 104 | Error = (Twine("can't open file '") + Path + "': " + EC.message()).str(); |
| 105 | return false; |
| 106 | } |
| 107 | std::string ParseError; |
| 108 | if (!parse(FileOrErr.get().get(), Sections, ParseError)) { |
| 109 | Error = (Twine("error parsing file '") + Path + "': " + ParseError).str(); |
| 110 | return false; |
| 111 | } |
| 112 | } |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 113 | return true; |
| 114 | } |
| 115 | |
| 116 | bool SpecialCaseList::createInternal(const MemoryBuffer *MB, |
| 117 | std::string &Error) { |
| 118 | StringMap<size_t> Sections; |
| 119 | if (!parse(MB, Sections, Error)) |
| 120 | return false; |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 121 | return true; |
| 122 | } |
| 123 | |
| 124 | bool SpecialCaseList::parse(const MemoryBuffer *MB, |
| 125 | StringMap<size_t> &SectionsMap, |
| 126 | std::string &Error) { |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 127 | // Iterate through each line in the blacklist file. |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 128 | SmallVector<StringRef, 16> Lines; |
Mitch Phillips | 40d6663 | 2017-11-07 21:16:46 +0000 | [diff] [blame] | 129 | MB->getBuffer().split(Lines, '\n'); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 130 | |
Mitch Phillips | 40d6663 | 2017-11-07 21:16:46 +0000 | [diff] [blame] | 131 | unsigned LineNo = 1; |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 132 | StringRef Section = "*"; |
Mitch Phillips | 40d6663 | 2017-11-07 21:16:46 +0000 | [diff] [blame] | 133 | |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 134 | for (auto I = Lines.begin(), E = Lines.end(); I != E; ++I, ++LineNo) { |
Mitch Phillips | 40d6663 | 2017-11-07 21:16:46 +0000 | [diff] [blame] | 135 | *I = I->trim(); |
Alexey Samsonov | 8418442 | 2012-10-19 15:24:46 +0000 | [diff] [blame] | 136 | // Ignore empty lines and lines starting with "#" |
| 137 | if (I->empty() || I->startswith("#")) |
| 138 | continue; |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 139 | |
| 140 | // Save section names |
| 141 | if (I->startswith("[")) { |
| 142 | if (!I->endswith("]")) { |
| 143 | Error = (Twine("malformed section header on line ") + Twine(LineNo) + |
| 144 | ": " + *I).str(); |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | Section = I->slice(1, I->size() - 1); |
| 149 | |
| 150 | std::string REError; |
| 151 | Regex CheckRE(Section); |
| 152 | if (!CheckRE.isValid(REError)) { |
| 153 | Error = |
| 154 | (Twine("malformed regex for section ") + Section + ": '" + REError) |
| 155 | .str(); |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | continue; |
| 160 | } |
| 161 | |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 162 | // Get our prefix and unparsed regexp. |
| 163 | std::pair<StringRef, StringRef> SplitLine = I->split(":"); |
| 164 | StringRef Prefix = SplitLine.first; |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 165 | if (SplitLine.second.empty()) { |
Nick Lewycky | 1af94eb | 2012-11-29 00:01:38 +0000 | [diff] [blame] | 166 | // Missing ':' in the line. |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 167 | Error = (Twine("malformed line ") + Twine(LineNo) + ": '" + |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 168 | SplitLine.first + "'").str(); |
| 169 | return false; |
Nick Lewycky | 1af94eb | 2012-11-29 00:01:38 +0000 | [diff] [blame] | 170 | } |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 171 | |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 172 | std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("="); |
| 173 | std::string Regexp = SplitRegexp.first; |
| 174 | StringRef Category = SplitRegexp.second; |
| 175 | |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 176 | // Create this section if it has not been seen before. |
| 177 | if (SectionsMap.find(Section) == SectionsMap.end()) { |
| 178 | std::unique_ptr<Matcher> M = make_unique<Matcher>(); |
| 179 | std::string REError; |
Mitch Phillips | 40d6663 | 2017-11-07 21:16:46 +0000 | [diff] [blame] | 180 | if (!M->insert(Section, LineNo, REError)) { |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 181 | Error = (Twine("malformed section ") + Section + ": '" + REError).str(); |
| 182 | return false; |
| 183 | } |
Peter Collingbourne | bace606 | 2013-08-05 17:48:04 +0000 | [diff] [blame] | 184 | |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 185 | SectionsMap[Section] = Sections.size(); |
| 186 | Sections.emplace_back(std::move(M)); |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 187 | } |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 188 | |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 189 | auto &Entry = Sections[SectionsMap[Section]].Entries[Prefix][Category]; |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 190 | std::string REError; |
Mitch Phillips | 40d6663 | 2017-11-07 21:16:46 +0000 | [diff] [blame] | 191 | if (!Entry.insert(std::move(Regexp), LineNo, REError)) { |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 192 | Error = (Twine("malformed regex in line ") + Twine(LineNo) + ": '" + |
Alexey Samsonov | 9e4fdd2 | 2013-08-12 07:49:36 +0000 | [diff] [blame] | 193 | SplitLine.second + "': " + REError).str(); |
| 194 | return false; |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 195 | } |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 196 | } |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 197 | return true; |
| 198 | } |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 199 | |
Peter Collingbourne | d5feb7b | 2014-07-10 03:55:02 +0000 | [diff] [blame] | 200 | SpecialCaseList::~SpecialCaseList() {} |
Kostya Serebryany | 36dfc5c | 2012-08-24 16:40:11 +0000 | [diff] [blame] | 201 | |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 202 | bool SpecialCaseList::inSection(StringRef Section, StringRef Prefix, |
| 203 | StringRef Query, StringRef Category) const { |
Mitch Phillips | 40d6663 | 2017-11-07 21:16:46 +0000 | [diff] [blame] | 204 | return inSectionBlame(Section, Prefix, Query, Category); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Mitch Phillips | 40d6663 | 2017-11-07 21:16:46 +0000 | [diff] [blame] | 207 | unsigned SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix, |
| 208 | StringRef Query, |
| 209 | StringRef Category) const { |
| 210 | for (auto &SectionIter : Sections) |
| 211 | if (SectionIter.SectionMatcher->match(Section)) { |
| 212 | unsigned Blame = |
| 213 | inSectionBlame(SectionIter.Entries, Prefix, Query, Category); |
| 214 | if (Blame) |
| 215 | return Blame; |
| 216 | } |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | unsigned SpecialCaseList::inSectionBlame(const SectionEntries &Entries, |
| 221 | StringRef Prefix, StringRef Query, |
| 222 | StringRef Category) const { |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 223 | SectionEntries::const_iterator I = Entries.find(Prefix); |
Mitch Phillips | 40d6663 | 2017-11-07 21:16:46 +0000 | [diff] [blame] | 224 | if (I == Entries.end()) return 0; |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame] | 225 | StringMap<Matcher>::const_iterator II = I->second.find(Category); |
Mitch Phillips | 40d6663 | 2017-11-07 21:16:46 +0000 | [diff] [blame] | 226 | if (II == I->second.end()) return 0; |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 227 | |
Peter Collingbourne | bace606 | 2013-08-05 17:48:04 +0000 | [diff] [blame] | 228 | return II->getValue().match(Query); |
Kostya Serebryany | 01401ce | 2012-03-14 23:22:10 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | } // namespace llvm |