blob: bf807e66e02c19886d47f91d832bedd055544fc7 [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//
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 Serebryany36dfc5c2012-08-24 16:40:11 +000010// This is a utility class for instrumentation passes (like AddressSanitizer
11// or ThreadSanitizer) to avoid instrumenting some functions or global
Peter Collingbourne49062a92013-07-09 22:03:17 +000012// variables, or to instrument some functions or global variables in a specific
13// way, based on a user-supplied list.
Kostya Serebryany01401ce2012-03-14 23:22:10 +000014//
15//===----------------------------------------------------------------------===//
16
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000017#include "llvm/Support/SpecialCaseList.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000018#include "llvm/ADT/SmallVector.h"
Kostya Serebryany01401ce2012-03-14 23:22:10 +000019#include "llvm/ADT/StringExtras.h"
Kostya Serebryany01401ce2012-03-14 23:22:10 +000020#include "llvm/Support/MemoryBuffer.h"
21#include "llvm/Support/Regex.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(
57 std::make_pair(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,
75 std::string &Error) {
76 std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000077 if (SCL->createInternal(Paths, Error))
78 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>
91SpecialCaseList::createOrDie(const std::vector<std::string> &Paths) {
Alexey Samsonove4b5fb82013-08-12 11:46:09 +000092 std::string Error;
Alexey Samsonovb9b80272015-02-04 17:39:48 +000093 if (auto SCL = create(Paths, Error))
Alexey Samsonove4b5fb82013-08-12 11:46:09 +000094 return SCL;
95 report_fatal_error(Error);
96}
97
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000098bool SpecialCaseList::createInternal(const std::vector<std::string> &Paths,
99 std::string &Error) {
100 StringMap<size_t> Sections;
101 for (const auto &Path : Paths) {
102 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
103 MemoryBuffer::getFile(Path);
104 if (std::error_code EC = FileOrErr.getError()) {
105 Error = (Twine("can't open file '") + Path + "': " + EC.message()).str();
106 return false;
107 }
108 std::string ParseError;
109 if (!parse(FileOrErr.get().get(), Sections, ParseError)) {
110 Error = (Twine("error parsing file '") + Path + "': " + ParseError).str();
111 return false;
112 }
113 }
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000114 return true;
115}
116
117bool SpecialCaseList::createInternal(const MemoryBuffer *MB,
118 std::string &Error) {
119 StringMap<size_t> Sections;
120 if (!parse(MB, Sections, Error))
121 return false;
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000122 return true;
123}
124
125bool SpecialCaseList::parse(const MemoryBuffer *MB,
126 StringMap<size_t> &SectionsMap,
127 std::string &Error) {
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000128 // Iterate through each line in the blacklist file.
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000129 SmallVector<StringRef, 16> Lines;
Mitch Phillips40d66632017-11-07 21:16:46 +0000130 MB->getBuffer().split(Lines, '\n');
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000131
Mitch Phillips40d66632017-11-07 21:16:46 +0000132 unsigned LineNo = 1;
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000133 StringRef Section = "*";
Mitch Phillips40d66632017-11-07 21:16:46 +0000134
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000135 for (auto I = Lines.begin(), E = Lines.end(); I != E; ++I, ++LineNo) {
Mitch Phillips40d66632017-11-07 21:16:46 +0000136 *I = I->trim();
Alexey Samsonov84184422012-10-19 15:24:46 +0000137 // Ignore empty lines and lines starting with "#"
138 if (I->empty() || I->startswith("#"))
139 continue;
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000140
141 // Save section names
142 if (I->startswith("[")) {
143 if (!I->endswith("]")) {
144 Error = (Twine("malformed section header on line ") + Twine(LineNo) +
145 ": " + *I).str();
146 return false;
147 }
148
149 Section = I->slice(1, I->size() - 1);
150
151 std::string REError;
152 Regex CheckRE(Section);
153 if (!CheckRE.isValid(REError)) {
154 Error =
155 (Twine("malformed regex for section ") + Section + ": '" + REError)
156 .str();
157 return false;
158 }
159
160 continue;
161 }
162
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000163 // Get our prefix and unparsed regexp.
164 std::pair<StringRef, StringRef> SplitLine = I->split(":");
165 StringRef Prefix = SplitLine.first;
Peter Collingbourne49062a92013-07-09 22:03:17 +0000166 if (SplitLine.second.empty()) {
Nick Lewycky1af94eb2012-11-29 00:01:38 +0000167 // Missing ':' in the line.
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000168 Error = (Twine("malformed line ") + Twine(LineNo) + ": '" +
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +0000169 SplitLine.first + "'").str();
170 return false;
Nick Lewycky1af94eb2012-11-29 00:01:38 +0000171 }
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000172
Peter Collingbourne49062a92013-07-09 22:03:17 +0000173 std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
174 std::string Regexp = SplitRegexp.first;
175 StringRef Category = SplitRegexp.second;
176
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000177 // Create this section if it has not been seen before.
178 if (SectionsMap.find(Section) == SectionsMap.end()) {
179 std::unique_ptr<Matcher> M = make_unique<Matcher>();
180 std::string REError;
Mitch Phillips40d66632017-11-07 21:16:46 +0000181 if (!M->insert(Section, LineNo, REError)) {
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000182 Error = (Twine("malformed section ") + Section + ": '" + REError).str();
183 return false;
184 }
Peter Collingbournebace6062013-08-05 17:48:04 +0000185
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000186 SectionsMap[Section] = Sections.size();
187 Sections.emplace_back(std::move(M));
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000188 }
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000189
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000190 auto &Entry = Sections[SectionsMap[Section]].Entries[Prefix][Category];
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +0000191 std::string REError;
Mitch Phillips40d66632017-11-07 21:16:46 +0000192 if (!Entry.insert(std::move(Regexp), LineNo, REError)) {
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000193 Error = (Twine("malformed regex in line ") + Twine(LineNo) + ": '" +
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +0000194 SplitLine.second + "': " + REError).str();
195 return false;
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000196 }
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000197 }
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000198 return true;
199}
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000200
Peter Collingbourned5feb7b2014-07-10 03:55:02 +0000201SpecialCaseList::~SpecialCaseList() {}
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000202
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000203bool SpecialCaseList::inSection(StringRef Section, StringRef Prefix,
204 StringRef Query, StringRef Category) const {
Mitch Phillips40d66632017-11-07 21:16:46 +0000205 return inSectionBlame(Section, Prefix, Query, Category);
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000206}
207
Mitch Phillips40d66632017-11-07 21:16:46 +0000208unsigned SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix,
209 StringRef Query,
210 StringRef Category) const {
211 for (auto &SectionIter : Sections)
212 if (SectionIter.SectionMatcher->match(Section)) {
213 unsigned Blame =
214 inSectionBlame(SectionIter.Entries, Prefix, Query, Category);
215 if (Blame)
216 return Blame;
217 }
218 return 0;
219}
220
221unsigned SpecialCaseList::inSectionBlame(const SectionEntries &Entries,
222 StringRef Prefix, StringRef Query,
223 StringRef Category) const {
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000224 SectionEntries::const_iterator I = Entries.find(Prefix);
Mitch Phillips40d66632017-11-07 21:16:46 +0000225 if (I == Entries.end()) return 0;
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000226 StringMap<Matcher>::const_iterator II = I->second.find(Category);
Mitch Phillips40d66632017-11-07 21:16:46 +0000227 if (II == I->second.end()) return 0;
Peter Collingbourne49062a92013-07-09 22:03:17 +0000228
Peter Collingbournebace6062013-08-05 17:48:04 +0000229 return II->getValue().match(Query);
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000230}
231
232} // namespace llvm