blob: 05886eaa8aee722b58748f7a0c95b9df84650780 [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"
Peter Collingbournebace6062013-08-05 17:48:04 +000020#include "llvm/ADT/StringSet.h"
Kostya Serebryany01401ce2012-03-14 23:22:10 +000021#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/Regex.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000023#include "llvm/Support/TrigramIndex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000025#include <system_error>
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include <utility>
Kostya Serebryany01401ce2012-03-14 23:22:10 +000027
28namespace llvm {
29
Peter Collingbournebace6062013-08-05 17:48:04 +000030/// Represents a set of regular expressions. Regular expressions which are
31/// "literal" (i.e. no regex metacharacters) are stored in Strings, while all
32/// others are represented as a single pipe-separated regex in RegEx. The
33/// reason for doing so is efficiency; StringSet is much faster at matching
34/// literal strings than Regex.
35struct SpecialCaseList::Entry {
36 StringSet<> Strings;
Ivan Krasin3dade412016-12-01 02:54:54 +000037 TrigramIndex Trigrams;
Peter Collingbourned5feb7b2014-07-10 03:55:02 +000038 std::unique_ptr<Regex> RegEx;
Peter Collingbournebace6062013-08-05 17:48:04 +000039
40 bool match(StringRef Query) const {
Ivan Krasin3dade412016-12-01 02:54:54 +000041 if (Strings.count(Query))
42 return true;
43 if (Trigrams.isDefinitelyOut(Query))
44 return false;
45 return RegEx && RegEx->match(Query);
Peter Collingbournebace6062013-08-05 17:48:04 +000046 }
47};
48
Alexey Samsonovb9b80272015-02-04 17:39:48 +000049SpecialCaseList::SpecialCaseList() : Entries(), Regexps(), IsCompiled(false) {}
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000050
Alexey Samsonovb9b80272015-02-04 17:39:48 +000051std::unique_ptr<SpecialCaseList>
52SpecialCaseList::create(const std::vector<std::string> &Paths,
53 std::string &Error) {
54 std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
Benjamin Kramer4fed9282016-05-27 12:30:51 +000055 for (const auto &Path : Paths) {
Alexey Samsonovb9b80272015-02-04 17:39:48 +000056 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
57 MemoryBuffer::getFile(Path);
58 if (std::error_code EC = FileOrErr.getError()) {
59 Error = (Twine("can't open file '") + Path + "': " + EC.message()).str();
60 return nullptr;
61 }
62 std::string ParseError;
63 if (!SCL->parse(FileOrErr.get().get(), ParseError)) {
64 Error = (Twine("error parsing file '") + Path + "': " + ParseError).str();
65 return nullptr;
66 }
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000067 }
Alexey Samsonovb9b80272015-02-04 17:39:48 +000068 SCL->compile();
69 return SCL;
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000070}
71
David Blaikie15913f42014-09-02 18:13:54 +000072std::unique_ptr<SpecialCaseList> SpecialCaseList::create(const MemoryBuffer *MB,
73 std::string &Error) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000074 std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000075 if (!SCL->parse(MB, Error))
Craig Topperf40110f2014-04-25 05:29:35 +000076 return nullptr;
Alexey Samsonovb9b80272015-02-04 17:39:48 +000077 SCL->compile();
David Blaikie15913f42014-09-02 18:13:54 +000078 return SCL;
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000079}
80
Alexey Samsonovb9b80272015-02-04 17:39:48 +000081std::unique_ptr<SpecialCaseList>
82SpecialCaseList::createOrDie(const std::vector<std::string> &Paths) {
Alexey Samsonove4b5fb82013-08-12 11:46:09 +000083 std::string Error;
Alexey Samsonovb9b80272015-02-04 17:39:48 +000084 if (auto SCL = create(Paths, Error))
Alexey Samsonove4b5fb82013-08-12 11:46:09 +000085 return SCL;
86 report_fatal_error(Error);
87}
88
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000089bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +000090 // Iterate through each line in the blacklist file.
Kostya Serebryany01401ce2012-03-14 23:22:10 +000091 SmallVector<StringRef, 16> Lines;
Peter Collingbourne2eb048d2013-07-09 22:03:09 +000092 SplitString(MB->getBuffer(), Lines, "\n\r");
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000093 int LineNo = 1;
Alexey Samsonovb9b80272015-02-04 17:39:48 +000094 for (auto I = Lines.begin(), E = Lines.end(); I != E; ++I, ++LineNo) {
Alexey Samsonov84184422012-10-19 15:24:46 +000095 // Ignore empty lines and lines starting with "#"
96 if (I->empty() || I->startswith("#"))
97 continue;
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +000098 // Get our prefix and unparsed regexp.
99 std::pair<StringRef, StringRef> SplitLine = I->split(":");
100 StringRef Prefix = SplitLine.first;
Peter Collingbourne49062a92013-07-09 22:03:17 +0000101 if (SplitLine.second.empty()) {
Nick Lewycky1af94eb2012-11-29 00:01:38 +0000102 // Missing ':' in the line.
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000103 Error = (Twine("malformed line ") + Twine(LineNo) + ": '" +
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +0000104 SplitLine.first + "'").str();
105 return false;
Nick Lewycky1af94eb2012-11-29 00:01:38 +0000106 }
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000107
Peter Collingbourne49062a92013-07-09 22:03:17 +0000108 std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
109 std::string Regexp = SplitRegexp.first;
110 StringRef Category = SplitRegexp.second;
111
Peter Collingbournebace6062013-08-05 17:48:04 +0000112 // See if we can store Regexp in Strings.
Ivan Krasin3dade412016-12-01 02:54:54 +0000113 auto &Entry = Entries[Prefix][Category];
Peter Collingbournebace6062013-08-05 17:48:04 +0000114 if (Regex::isLiteralERE(Regexp)) {
Ivan Krasin3dade412016-12-01 02:54:54 +0000115 Entry.Strings.insert(Regexp);
Peter Collingbournebace6062013-08-05 17:48:04 +0000116 continue;
117 }
Ivan Krasin3dade412016-12-01 02:54:54 +0000118 Entry.Trigrams.insert(Regexp);
Peter Collingbournebace6062013-08-05 17:48:04 +0000119
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000120 // Replace * with .*
Benjamin Kramere6ba5ef2016-11-30 10:01:11 +0000121 for (size_t pos = 0; (pos = Regexp.find('*', pos)) != std::string::npos;
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000122 pos += strlen(".*")) {
123 Regexp.replace(pos, strlen("*"), ".*");
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000124 }
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000125
126 // Check that the regexp is valid.
127 Regex CheckRE(Regexp);
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +0000128 std::string REError;
129 if (!CheckRE.isValid(REError)) {
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000130 Error = (Twine("malformed regex in line ") + Twine(LineNo) + ": '" +
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +0000131 SplitLine.second + "': " + REError).str();
132 return false;
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000133 }
134
135 // Add this regexp into the proper group by its prefix.
Peter Collingbourne49062a92013-07-09 22:03:17 +0000136 if (!Regexps[Prefix][Category].empty())
137 Regexps[Prefix][Category] += "|";
Peter Collingbourne8b77f182013-07-16 17:56:07 +0000138 Regexps[Prefix][Category] += "^" + Regexp + "$";
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000139 }
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000140 return true;
141}
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000142
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000143void SpecialCaseList::compile() {
144 assert(!IsCompiled && "compile() should only be called once");
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000145 // Iterate through each of the prefixes, and create Regexs for them.
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000146 for (StringMap<StringMap<std::string>>::const_iterator I = Regexps.begin(),
147 E = Regexps.end();
Peter Collingbourne49062a92013-07-09 22:03:17 +0000148 I != E; ++I) {
149 for (StringMap<std::string>::const_iterator II = I->second.begin(),
150 IE = I->second.end();
151 II != IE; ++II) {
Peter Collingbourned5feb7b2014-07-10 03:55:02 +0000152 Entries[I->getKey()][II->getKey()].RegEx.reset(new Regex(II->getValue()));
Peter Collingbourne49062a92013-07-09 22:03:17 +0000153 }
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000154 }
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000155 Regexps.clear();
156 IsCompiled = true;
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000157}
158
Peter Collingbourned5feb7b2014-07-10 03:55:02 +0000159SpecialCaseList::~SpecialCaseList() {}
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000160
Craig Topper6dc4a8bc2014-08-30 16:48:02 +0000161bool SpecialCaseList::inSection(StringRef Section, StringRef Query,
162 StringRef Category) const {
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000163 assert(IsCompiled && "SpecialCaseList::compile() was not called!");
Peter Collingbournebace6062013-08-05 17:48:04 +0000164 StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
Peter Collingbourne49062a92013-07-09 22:03:17 +0000165 if (I == Entries.end()) return false;
Peter Collingbournebace6062013-08-05 17:48:04 +0000166 StringMap<Entry>::const_iterator II = I->second.find(Category);
Peter Collingbourne49062a92013-07-09 22:03:17 +0000167 if (II == I->second.end()) return false;
168
Peter Collingbournebace6062013-08-05 17:48:04 +0000169 return II->getValue().match(Query);
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000170}
171
172} // namespace llvm