blob: 73f852624a69728b2645dc2200ff3f5ce12a8839 [file] [log] [blame]
Peter Collingbourne49062a92013-07-09 22:03:17 +00001//===-- SpecialCaseList.cpp - special case list for sanitizers ------------===//
Kostya Serebryany01401ce2012-03-14 23:22:10 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Serebryany01401ce2012-03-14 23:22:10 +00006//
7//===----------------------------------------------------------------------===//
8//
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +00009// This is a utility class for instrumentation passes (like AddressSanitizer
10// or ThreadSanitizer) to avoid instrumenting some functions or global
Peter Collingbourne49062a92013-07-09 22:03:17 +000011// variables, or to instrument some functions or global variables in a specific
12// way, based on a user-supplied list.
Kostya Serebryany01401ce2012-03-14 23:22:10 +000013//
14//===----------------------------------------------------------------------===//
15
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000016#include "llvm/Support/SpecialCaseList.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000017#include "llvm/ADT/SmallVector.h"
Kostya Serebryany01401ce2012-03-14 23:22:10 +000018#include "llvm/ADT/StringExtras.h"
Kostya Serebryany01401ce2012-03-14 23:22:10 +000019#include "llvm/Support/MemoryBuffer.h"
20#include "llvm/Support/Regex.h"
Ilya Biryukovaa981c12019-11-21 11:32:17 +010021#include "llvm/Support/VirtualFileSystem.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000023#include <system_error>
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include <utility>
Kostya Serebryany01401ce2012-03-14 23:22:10 +000025
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000026#include <stdio.h>
Kostya Serebryany01401ce2012-03-14 23:22:10 +000027namespace llvm {
28
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000029bool SpecialCaseList::Matcher::insert(std::string Regexp,
Mitch Phillips40d66632017-11-07 21:16:46 +000030 unsigned LineNumber,
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000031 std::string &REError) {
Mitch Phillipsfa2eda82017-10-24 23:56:12 +000032 if (Regexp.empty()) {
33 REError = "Supplied regexp was blank";
34 return false;
35 }
36
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000037 if (Regex::isLiteralERE(Regexp)) {
Mitch Phillips40d66632017-11-07 21:16:46 +000038 Strings[Regexp] = LineNumber;
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000039 return true;
Peter Collingbournebace6062013-08-05 17:48:04 +000040 }
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000041 Trigrams.insert(Regexp);
Peter Collingbournebace6062013-08-05 17:48:04 +000042
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000043 // 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 Phillips40d66632017-11-07 21:16:46 +000049 Regexp = (Twine("^(") + StringRef(Regexp) + ")$").str();
50
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000051 // Check that the regexp is valid.
52 Regex CheckRE(Regexp);
53 if (!CheckRE.isValid(REError))
54 return false;
55
Mitch Phillips40d66632017-11-07 21:16:46 +000056 RegExes.emplace_back(
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000057 std::make_pair(std::make_unique<Regex>(std::move(CheckRE)), LineNumber));
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000058 return true;
59}
60
Mitch Phillips40d66632017-11-07 21:16:46 +000061unsigned SpecialCaseList::Matcher::match(StringRef Query) const {
62 auto It = Strings.find(Query);
63 if (It != Strings.end())
64 return It->second;
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000065 if (Trigrams.isDefinitelyOut(Query))
66 return false;
Mitch Phillips40d66632017-11-07 21:16:46 +000067 for (auto& RegExKV : RegExes)
68 if (RegExKV.first->match(Query))
69 return RegExKV.second;
70 return 0;
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000071}
72
Alexey Samsonovb9b80272015-02-04 17:39:48 +000073std::unique_ptr<SpecialCaseList>
74SpecialCaseList::create(const std::vector<std::string> &Paths,
Ilya Biryukovaa981c12019-11-21 11:32:17 +010075 llvm::vfs::FileSystem &FS, std::string &Error) {
Alexey Samsonovb9b80272015-02-04 17:39:48 +000076 std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
Ilya Biryukovaa981c12019-11-21 11:32:17 +010077 if (SCL->createInternal(Paths, FS, Error))
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000078 return SCL;
79 return nullptr;
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000080}
81
David Blaikie15913f42014-09-02 18:13:54 +000082std::unique_ptr<SpecialCaseList> SpecialCaseList::create(const MemoryBuffer *MB,
83 std::string &Error) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000084 std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000085 if (SCL->createInternal(MB, Error))
86 return SCL;
87 return nullptr;
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000088}
89
Alexey Samsonovb9b80272015-02-04 17:39:48 +000090std::unique_ptr<SpecialCaseList>
Ilya Biryukovaa981c12019-11-21 11:32:17 +010091SpecialCaseList::createOrDie(const std::vector<std::string> &Paths,
92 llvm::vfs::FileSystem &FS) {
Alexey Samsonove4b5fb82013-08-12 11:46:09 +000093 std::string Error;
Ilya Biryukovaa981c12019-11-21 11:32:17 +010094 if (auto SCL = create(Paths, FS, Error))
Alexey Samsonove4b5fb82013-08-12 11:46:09 +000095 return SCL;
96 report_fatal_error(Error);
97}
98
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000099bool SpecialCaseList::createInternal(const std::vector<std::string> &Paths,
Ilya Biryukovaa981c12019-11-21 11:32:17 +0100100 vfs::FileSystem &VFS, std::string &Error) {
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000101 StringMap<size_t> Sections;
102 for (const auto &Path : Paths) {
103 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
Jan Korous590f2792019-11-08 10:40:27 -0800104 VFS.getBufferForFile(Path);
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000105 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 Tsyrklevich998b2202017-09-25 22:11:11 +0000115 return true;
116}
117
118bool SpecialCaseList::createInternal(const MemoryBuffer *MB,
119 std::string &Error) {
120 StringMap<size_t> Sections;
121 if (!parse(MB, Sections, Error))
122 return false;
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000123 return true;
124}
125
126bool SpecialCaseList::parse(const MemoryBuffer *MB,
127 StringMap<size_t> &SectionsMap,
128 std::string &Error) {
Eric Christopher858d3852020-06-20 00:24:57 -0700129 // Iterate through each line in the exclusion list file.
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000130 SmallVector<StringRef, 16> Lines;
Mitch Phillips40d66632017-11-07 21:16:46 +0000131 MB->getBuffer().split(Lines, '\n');
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000132
Mitch Phillips40d66632017-11-07 21:16:46 +0000133 unsigned LineNo = 1;
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000134 StringRef Section = "*";
Mitch Phillips40d66632017-11-07 21:16:46 +0000135
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000136 for (auto I = Lines.begin(), E = Lines.end(); I != E; ++I, ++LineNo) {
Mitch Phillips40d66632017-11-07 21:16:46 +0000137 *I = I->trim();
Alexey Samsonov84184422012-10-19 15:24:46 +0000138 // Ignore empty lines and lines starting with "#"
139 if (I->empty() || I->startswith("#"))
140 continue;
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000141
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 Serebryany36dfc5c2012-08-24 16:40:11 +0000164 // Get our prefix and unparsed regexp.
165 std::pair<StringRef, StringRef> SplitLine = I->split(":");
166 StringRef Prefix = SplitLine.first;
Peter Collingbourne49062a92013-07-09 22:03:17 +0000167 if (SplitLine.second.empty()) {
Nick Lewycky1af94eb2012-11-29 00:01:38 +0000168 // Missing ':' in the line.
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000169 Error = (Twine("malformed line ") + Twine(LineNo) + ": '" +
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +0000170 SplitLine.first + "'").str();
171 return false;
Nick Lewycky1af94eb2012-11-29 00:01:38 +0000172 }
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000173
Peter Collingbourne49062a92013-07-09 22:03:17 +0000174 std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
Benjamin Krameradcd0262020-01-28 20:23:46 +0100175 std::string Regexp = std::string(SplitRegexp.first);
Peter Collingbourne49062a92013-07-09 22:03:17 +0000176 StringRef Category = SplitRegexp.second;
177
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000178 // Create this section if it has not been seen before.
179 if (SectionsMap.find(Section) == SectionsMap.end()) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000180 std::unique_ptr<Matcher> M = std::make_unique<Matcher>();
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000181 std::string REError;
Benjamin Krameradcd0262020-01-28 20:23:46 +0100182 if (!M->insert(std::string(Section), LineNo, REError)) {
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000183 Error = (Twine("malformed section ") + Section + ": '" + REError).str();
184 return false;
185 }
Peter Collingbournebace6062013-08-05 17:48:04 +0000186
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000187 SectionsMap[Section] = Sections.size();
188 Sections.emplace_back(std::move(M));
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000189 }
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000190
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000191 auto &Entry = Sections[SectionsMap[Section]].Entries[Prefix][Category];
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +0000192 std::string REError;
Mitch Phillips40d66632017-11-07 21:16:46 +0000193 if (!Entry.insert(std::move(Regexp), LineNo, REError)) {
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000194 Error = (Twine("malformed regex in line ") + Twine(LineNo) + ": '" +
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +0000195 SplitLine.second + "': " + REError).str();
196 return false;
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000197 }
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000198 }
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000199 return true;
200}
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000201
Peter Collingbourned5feb7b2014-07-10 03:55:02 +0000202SpecialCaseList::~SpecialCaseList() {}
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000203
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000204bool SpecialCaseList::inSection(StringRef Section, StringRef Prefix,
205 StringRef Query, StringRef Category) const {
Mitch Phillips40d66632017-11-07 21:16:46 +0000206 return inSectionBlame(Section, Prefix, Query, Category);
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000207}
208
Mitch Phillips40d66632017-11-07 21:16:46 +0000209unsigned 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
222unsigned SpecialCaseList::inSectionBlame(const SectionEntries &Entries,
223 StringRef Prefix, StringRef Query,
224 StringRef Category) const {
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000225 SectionEntries::const_iterator I = Entries.find(Prefix);
Mitch Phillips40d66632017-11-07 21:16:46 +0000226 if (I == Entries.end()) return 0;
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000227 StringMap<Matcher>::const_iterator II = I->second.find(Category);
Mitch Phillips40d66632017-11-07 21:16:46 +0000228 if (II == I->second.end()) return 0;
Peter Collingbourne49062a92013-07-09 22:03:17 +0000229
Peter Collingbournebace6062013-08-05 17:48:04 +0000230 return II->getValue().match(Query);
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000231}
232
233} // namespace llvm