blob: 0ffe4444a179fc2347452b8e3f4511f8b783b1af [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 Carruthed0881b2012-12-03 16:50:05 +000023#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000024#include <system_error>
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include <utility>
Kostya Serebryany01401ce2012-03-14 23:22:10 +000026
27namespace llvm {
28
Peter Collingbournebace6062013-08-05 17:48:04 +000029/// Represents a set of regular expressions. Regular expressions which are
30/// "literal" (i.e. no regex metacharacters) are stored in Strings, while all
31/// others are represented as a single pipe-separated regex in RegEx. The
32/// reason for doing so is efficiency; StringSet is much faster at matching
33/// literal strings than Regex.
34struct SpecialCaseList::Entry {
Peter Collingbourne05b9ebf2014-07-10 04:29:06 +000035 Entry() {}
36 Entry(Entry &&Other)
37 : Strings(std::move(Other.Strings)), RegEx(std::move(Other.RegEx)) {}
Peter Collingbourne05b9ebf2014-07-10 04:29:06 +000038
Peter Collingbournebace6062013-08-05 17:48:04 +000039 StringSet<> Strings;
Peter Collingbourned5feb7b2014-07-10 03:55:02 +000040 std::unique_ptr<Regex> RegEx;
Peter Collingbournebace6062013-08-05 17:48:04 +000041
42 bool match(StringRef Query) const {
43 return Strings.count(Query) || (RegEx && RegEx->match(Query));
44 }
45};
46
Alexey Samsonovb9b80272015-02-04 17:39:48 +000047SpecialCaseList::SpecialCaseList() : Entries(), Regexps(), IsCompiled(false) {}
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000048
Alexey Samsonovb9b80272015-02-04 17:39:48 +000049std::unique_ptr<SpecialCaseList>
50SpecialCaseList::create(const std::vector<std::string> &Paths,
51 std::string &Error) {
52 std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
Benjamin Kramer4fed9282016-05-27 12:30:51 +000053 for (const auto &Path : Paths) {
Alexey Samsonovb9b80272015-02-04 17:39:48 +000054 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
55 MemoryBuffer::getFile(Path);
56 if (std::error_code EC = FileOrErr.getError()) {
57 Error = (Twine("can't open file '") + Path + "': " + EC.message()).str();
58 return nullptr;
59 }
60 std::string ParseError;
61 if (!SCL->parse(FileOrErr.get().get(), ParseError)) {
62 Error = (Twine("error parsing file '") + Path + "': " + ParseError).str();
63 return nullptr;
64 }
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000065 }
Alexey Samsonovb9b80272015-02-04 17:39:48 +000066 SCL->compile();
67 return SCL;
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000068}
69
David Blaikie15913f42014-09-02 18:13:54 +000070std::unique_ptr<SpecialCaseList> SpecialCaseList::create(const MemoryBuffer *MB,
71 std::string &Error) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000072 std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000073 if (!SCL->parse(MB, Error))
Craig Topperf40110f2014-04-25 05:29:35 +000074 return nullptr;
Alexey Samsonovb9b80272015-02-04 17:39:48 +000075 SCL->compile();
David Blaikie15913f42014-09-02 18:13:54 +000076 return SCL;
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000077}
78
Alexey Samsonovb9b80272015-02-04 17:39:48 +000079std::unique_ptr<SpecialCaseList>
80SpecialCaseList::createOrDie(const std::vector<std::string> &Paths) {
Alexey Samsonove4b5fb82013-08-12 11:46:09 +000081 std::string Error;
Alexey Samsonovb9b80272015-02-04 17:39:48 +000082 if (auto SCL = create(Paths, Error))
Alexey Samsonove4b5fb82013-08-12 11:46:09 +000083 return SCL;
84 report_fatal_error(Error);
85}
86
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000087bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +000088 // Iterate through each line in the blacklist file.
Kostya Serebryany01401ce2012-03-14 23:22:10 +000089 SmallVector<StringRef, 16> Lines;
Peter Collingbourne2eb048d2013-07-09 22:03:09 +000090 SplitString(MB->getBuffer(), Lines, "\n\r");
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000091 int LineNo = 1;
Alexey Samsonovb9b80272015-02-04 17:39:48 +000092 for (auto I = Lines.begin(), E = Lines.end(); I != E; ++I, ++LineNo) {
Alexey Samsonov84184422012-10-19 15:24:46 +000093 // Ignore empty lines and lines starting with "#"
94 if (I->empty() || I->startswith("#"))
95 continue;
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +000096 // Get our prefix and unparsed regexp.
97 std::pair<StringRef, StringRef> SplitLine = I->split(":");
98 StringRef Prefix = SplitLine.first;
Peter Collingbourne49062a92013-07-09 22:03:17 +000099 if (SplitLine.second.empty()) {
Nick Lewycky1af94eb2012-11-29 00:01:38 +0000100 // Missing ':' in the line.
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000101 Error = (Twine("malformed line ") + Twine(LineNo) + ": '" +
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +0000102 SplitLine.first + "'").str();
103 return false;
Nick Lewycky1af94eb2012-11-29 00:01:38 +0000104 }
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000105
Peter Collingbourne49062a92013-07-09 22:03:17 +0000106 std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
107 std::string Regexp = SplitRegexp.first;
108 StringRef Category = SplitRegexp.second;
109
Peter Collingbournebace6062013-08-05 17:48:04 +0000110 // See if we can store Regexp in Strings.
111 if (Regex::isLiteralERE(Regexp)) {
112 Entries[Prefix][Category].Strings.insert(Regexp);
113 continue;
114 }
115
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000116 // Replace * with .*
117 for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
118 pos += strlen(".*")) {
119 Regexp.replace(pos, strlen("*"), ".*");
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000120 }
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000121
122 // Check that the regexp is valid.
123 Regex CheckRE(Regexp);
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +0000124 std::string REError;
125 if (!CheckRE.isValid(REError)) {
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000126 Error = (Twine("malformed regex in line ") + Twine(LineNo) + ": '" +
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +0000127 SplitLine.second + "': " + REError).str();
128 return false;
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000129 }
130
131 // Add this regexp into the proper group by its prefix.
Peter Collingbourne49062a92013-07-09 22:03:17 +0000132 if (!Regexps[Prefix][Category].empty())
133 Regexps[Prefix][Category] += "|";
Peter Collingbourne8b77f182013-07-16 17:56:07 +0000134 Regexps[Prefix][Category] += "^" + Regexp + "$";
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000135 }
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000136 return true;
137}
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000138
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000139void SpecialCaseList::compile() {
140 assert(!IsCompiled && "compile() should only be called once");
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000141 // Iterate through each of the prefixes, and create Regexs for them.
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000142 for (StringMap<StringMap<std::string>>::const_iterator I = Regexps.begin(),
143 E = Regexps.end();
Peter Collingbourne49062a92013-07-09 22:03:17 +0000144 I != E; ++I) {
145 for (StringMap<std::string>::const_iterator II = I->second.begin(),
146 IE = I->second.end();
147 II != IE; ++II) {
Peter Collingbourned5feb7b2014-07-10 03:55:02 +0000148 Entries[I->getKey()][II->getKey()].RegEx.reset(new Regex(II->getValue()));
Peter Collingbourne49062a92013-07-09 22:03:17 +0000149 }
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000150 }
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000151 Regexps.clear();
152 IsCompiled = true;
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000153}
154
Peter Collingbourned5feb7b2014-07-10 03:55:02 +0000155SpecialCaseList::~SpecialCaseList() {}
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000156
Craig Topper6dc4a8bc2014-08-30 16:48:02 +0000157bool SpecialCaseList::inSection(StringRef Section, StringRef Query,
158 StringRef Category) const {
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000159 assert(IsCompiled && "SpecialCaseList::compile() was not called!");
Peter Collingbournebace6062013-08-05 17:48:04 +0000160 StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
Peter Collingbourne49062a92013-07-09 22:03:17 +0000161 if (I == Entries.end()) return false;
Peter Collingbournebace6062013-08-05 17:48:04 +0000162 StringMap<Entry>::const_iterator II = I->second.find(Category);
Peter Collingbourne49062a92013-07-09 22:03:17 +0000163 if (II == I->second.end()) return false;
164
Peter Collingbournebace6062013-08-05 17:48:04 +0000165 return II->getValue().match(Query);
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000166}
167
168} // namespace llvm