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