blob: d9921ac69205bb3bdbb7fbb94fc508c0ca5772bc [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"
Peter Collingbourne49062a92013-07-09 22:03:17 +000018#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000019#include "llvm/ADT/SmallVector.h"
Kostya Serebryany01401ce2012-03-14 23:22:10 +000020#include "llvm/ADT/StringExtras.h"
Peter Collingbournebace6062013-08-05 17:48:04 +000021#include "llvm/ADT/StringSet.h"
Kostya Serebryany01401ce2012-03-14 23:22:10 +000022#include "llvm/Support/MemoryBuffer.h"
23#include "llvm/Support/Regex.h"
24#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000026#include <system_error>
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include <utility>
Kostya Serebryany01401ce2012-03-14 23:22:10 +000028
29namespace llvm {
30
Peter Collingbournebace6062013-08-05 17:48:04 +000031/// Represents a set of regular expressions. Regular expressions which are
32/// "literal" (i.e. no regex metacharacters) are stored in Strings, while all
33/// others are represented as a single pipe-separated regex in RegEx. The
34/// reason for doing so is efficiency; StringSet is much faster at matching
35/// literal strings than Regex.
36struct SpecialCaseList::Entry {
37 StringSet<> Strings;
38 Regex *RegEx;
39
Craig Topperf40110f2014-04-25 05:29:35 +000040 Entry() : RegEx(nullptr) {}
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 Samsonov9e4fdd22013-08-12 07:49:36 +000047SpecialCaseList::SpecialCaseList() : Entries() {}
48
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000049SpecialCaseList *SpecialCaseList::create(
50 const StringRef Path, std::string &Error) {
51 if (Path.empty())
52 return new SpecialCaseList();
Rafael Espindolaadf21f22014-07-06 17:43:13 +000053 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
54 MemoryBuffer::getFile(Path);
55 if (std::error_code EC = FileOrErr.getError()) {
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000056 Error = (Twine("Can't open file '") + Path + "': " + EC.message()).str();
Craig Topperf40110f2014-04-25 05:29:35 +000057 return nullptr;
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000058 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +000059 return create(FileOrErr.get().get(), Error);
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000060}
61
62SpecialCaseList *SpecialCaseList::create(
63 const MemoryBuffer *MB, std::string &Error) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000064 std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000065 if (!SCL->parse(MB, Error))
Craig Topperf40110f2014-04-25 05:29:35 +000066 return nullptr;
Ahmed Charles96c9d952014-03-05 10:19:29 +000067 return SCL.release();
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000068}
69
Alexey Samsonove4b5fb82013-08-12 11:46:09 +000070SpecialCaseList *SpecialCaseList::createOrDie(const StringRef Path) {
71 std::string Error;
72 if (SpecialCaseList *SCL = create(Path, Error))
73 return SCL;
74 report_fatal_error(Error);
75}
76
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000077bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +000078 // Iterate through each line in the blacklist file.
Kostya Serebryany01401ce2012-03-14 23:22:10 +000079 SmallVector<StringRef, 16> Lines;
Peter Collingbourne2eb048d2013-07-09 22:03:09 +000080 SplitString(MB->getBuffer(), Lines, "\n\r");
Peter Collingbourne49062a92013-07-09 22:03:17 +000081 StringMap<StringMap<std::string> > Regexps;
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000082 assert(Entries.empty() &&
83 "parse() should be called on an empty SpecialCaseList");
84 int LineNo = 1;
Craig Topperaf0dea12013-07-04 01:31:24 +000085 for (SmallVectorImpl<StringRef>::iterator I = Lines.begin(), E = Lines.end();
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000086 I != E; ++I, ++LineNo) {
Alexey Samsonov84184422012-10-19 15:24:46 +000087 // Ignore empty lines and lines starting with "#"
88 if (I->empty() || I->startswith("#"))
89 continue;
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +000090 // Get our prefix and unparsed regexp.
91 std::pair<StringRef, StringRef> SplitLine = I->split(":");
92 StringRef Prefix = SplitLine.first;
Peter Collingbourne49062a92013-07-09 22:03:17 +000093 if (SplitLine.second.empty()) {
Nick Lewycky1af94eb2012-11-29 00:01:38 +000094 // Missing ':' in the line.
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000095 Error = (Twine("Malformed line ") + Twine(LineNo) + ": '" +
96 SplitLine.first + "'").str();
97 return false;
Nick Lewycky1af94eb2012-11-29 00:01:38 +000098 }
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +000099
Peter Collingbourne49062a92013-07-09 22:03:17 +0000100 std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
101 std::string Regexp = SplitRegexp.first;
102 StringRef Category = SplitRegexp.second;
103
104 // Backwards compatibility.
105 if (Prefix == "global-init") {
106 Prefix = "global";
107 Category = "init";
108 } else if (Prefix == "global-init-type") {
109 Prefix = "type";
110 Category = "init";
111 } else if (Prefix == "global-init-src") {
112 Prefix = "src";
113 Category = "init";
114 }
115
Peter Collingbournebace6062013-08-05 17:48:04 +0000116 // See if we can store Regexp in Strings.
117 if (Regex::isLiteralERE(Regexp)) {
118 Entries[Prefix][Category].Strings.insert(Regexp);
119 continue;
120 }
121
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000122 // Replace * with .*
123 for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
124 pos += strlen(".*")) {
125 Regexp.replace(pos, strlen("*"), ".*");
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000126 }
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000127
128 // Check that the regexp is valid.
129 Regex CheckRE(Regexp);
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +0000130 std::string REError;
131 if (!CheckRE.isValid(REError)) {
132 Error = (Twine("Malformed regex in line ") + Twine(LineNo) + ": '" +
133 SplitLine.second + "': " + REError).str();
134 return false;
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000135 }
136
137 // Add this regexp into the proper group by its prefix.
Peter Collingbourne49062a92013-07-09 22:03:17 +0000138 if (!Regexps[Prefix][Category].empty())
139 Regexps[Prefix][Category] += "|";
Peter Collingbourne8b77f182013-07-16 17:56:07 +0000140 Regexps[Prefix][Category] += "^" + Regexp + "$";
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000141 }
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000142
143 // Iterate through each of the prefixes, and create Regexs for them.
Peter Collingbourne49062a92013-07-09 22:03:17 +0000144 for (StringMap<StringMap<std::string> >::const_iterator I = Regexps.begin(),
145 E = Regexps.end();
146 I != E; ++I) {
147 for (StringMap<std::string>::const_iterator II = I->second.begin(),
148 IE = I->second.end();
149 II != IE; ++II) {
Peter Collingbournebace6062013-08-05 17:48:04 +0000150 Entries[I->getKey()][II->getKey()].RegEx = new Regex(II->getValue());
Peter Collingbourne49062a92013-07-09 22:03:17 +0000151 }
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000152 }
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +0000153 return true;
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000154}
155
Peter Collingbourne49062a92013-07-09 22:03:17 +0000156SpecialCaseList::~SpecialCaseList() {
Peter Collingbournebace6062013-08-05 17:48:04 +0000157 for (StringMap<StringMap<Entry> >::iterator I = Entries.begin(),
158 E = Entries.end();
Peter Collingbourne49062a92013-07-09 22:03:17 +0000159 I != E; ++I) {
Peter Collingbournebace6062013-08-05 17:48:04 +0000160 for (StringMap<Entry>::const_iterator II = I->second.begin(),
161 IE = I->second.end();
162 II != IE; ++II) {
163 delete II->second.RegEx;
164 }
Peter Collingbourne49062a92013-07-09 22:03:17 +0000165 }
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000166}
167
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000168bool SpecialCaseList::inSection(const StringRef Section, const StringRef Query,
169 const StringRef Category) const {
Peter Collingbournebace6062013-08-05 17:48:04 +0000170 StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
Peter Collingbourne49062a92013-07-09 22:03:17 +0000171 if (I == Entries.end()) return false;
Peter Collingbournebace6062013-08-05 17:48:04 +0000172 StringMap<Entry>::const_iterator II = I->second.find(Category);
Peter Collingbourne49062a92013-07-09 22:03:17 +0000173 if (II == I->second.end()) return false;
174
Peter Collingbournebace6062013-08-05 17:48:04 +0000175 return II->getValue().match(Query);
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000176}
177
178} // namespace llvm