blob: b4eeebdd192f5c183003a0f2b4f71ffaf91231b8 [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 {
Peter Collingbourne05b9ebf2014-07-10 04:29:06 +000037 Entry() {}
38 Entry(Entry &&Other)
39 : Strings(std::move(Other.Strings)), RegEx(std::move(Other.RegEx)) {}
Peter Collingbourne05b9ebf2014-07-10 04:29:06 +000040
Peter Collingbournebace6062013-08-05 17:48:04 +000041 StringSet<> Strings;
Peter Collingbourned5feb7b2014-07-10 03:55:02 +000042 std::unique_ptr<Regex> RegEx;
Peter Collingbournebace6062013-08-05 17:48:04 +000043
44 bool match(StringRef Query) const {
45 return Strings.count(Query) || (RegEx && RegEx->match(Query));
46 }
47};
48
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000049SpecialCaseList::SpecialCaseList() : Entries() {}
50
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000051SpecialCaseList *SpecialCaseList::create(StringRef Path, std::string &Error) {
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000052 if (Path.empty())
53 return new SpecialCaseList();
Rafael Espindolaadf21f22014-07-06 17:43:13 +000054 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
55 MemoryBuffer::getFile(Path);
56 if (std::error_code EC = FileOrErr.getError()) {
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000057 Error = (Twine("Can't open file '") + Path + "': " + EC.message()).str();
Craig Topperf40110f2014-04-25 05:29:35 +000058 return nullptr;
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000059 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +000060 return create(FileOrErr.get().get(), Error);
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000061}
62
63SpecialCaseList *SpecialCaseList::create(
64 const MemoryBuffer *MB, std::string &Error) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000065 std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000066 if (!SCL->parse(MB, Error))
Craig Topperf40110f2014-04-25 05:29:35 +000067 return nullptr;
Ahmed Charles96c9d952014-03-05 10:19:29 +000068 return SCL.release();
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000069}
70
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000071SpecialCaseList *SpecialCaseList::createOrDie(StringRef Path) {
Alexey Samsonove4b5fb82013-08-12 11:46:09 +000072 std::string Error;
73 if (SpecialCaseList *SCL = create(Path, Error))
74 return SCL;
75 report_fatal_error(Error);
76}
77
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000078bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +000079 // Iterate through each line in the blacklist file.
Kostya Serebryany01401ce2012-03-14 23:22:10 +000080 SmallVector<StringRef, 16> Lines;
Peter Collingbourne2eb048d2013-07-09 22:03:09 +000081 SplitString(MB->getBuffer(), Lines, "\n\r");
Peter Collingbourne49062a92013-07-09 22:03:17 +000082 StringMap<StringMap<std::string> > Regexps;
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000083 assert(Entries.empty() &&
84 "parse() should be called on an empty SpecialCaseList");
85 int LineNo = 1;
Craig Topperaf0dea12013-07-04 01:31:24 +000086 for (SmallVectorImpl<StringRef>::iterator I = Lines.begin(), E = Lines.end();
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000087 I != E; ++I, ++LineNo) {
Alexey Samsonov84184422012-10-19 15:24:46 +000088 // Ignore empty lines and lines starting with "#"
89 if (I->empty() || I->startswith("#"))
90 continue;
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +000091 // Get our prefix and unparsed regexp.
92 std::pair<StringRef, StringRef> SplitLine = I->split(":");
93 StringRef Prefix = SplitLine.first;
Peter Collingbourne49062a92013-07-09 22:03:17 +000094 if (SplitLine.second.empty()) {
Nick Lewycky1af94eb2012-11-29 00:01:38 +000095 // Missing ':' in the line.
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +000096 Error = (Twine("Malformed line ") + Twine(LineNo) + ": '" +
97 SplitLine.first + "'").str();
98 return false;
Nick Lewycky1af94eb2012-11-29 00:01:38 +000099 }
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000100
Peter Collingbourne49062a92013-07-09 22:03:17 +0000101 std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
102 std::string Regexp = SplitRegexp.first;
103 StringRef Category = SplitRegexp.second;
104
105 // Backwards compatibility.
106 if (Prefix == "global-init") {
107 Prefix = "global";
108 Category = "init";
109 } else if (Prefix == "global-init-type") {
110 Prefix = "type";
111 Category = "init";
112 } else if (Prefix == "global-init-src") {
113 Prefix = "src";
114 Category = "init";
115 }
116
Peter Collingbournebace6062013-08-05 17:48:04 +0000117 // See if we can store Regexp in Strings.
118 if (Regex::isLiteralERE(Regexp)) {
119 Entries[Prefix][Category].Strings.insert(Regexp);
120 continue;
121 }
122
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000123 // Replace * with .*
124 for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
125 pos += strlen(".*")) {
126 Regexp.replace(pos, strlen("*"), ".*");
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000127 }
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000128
129 // Check that the regexp is valid.
130 Regex CheckRE(Regexp);
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +0000131 std::string REError;
132 if (!CheckRE.isValid(REError)) {
133 Error = (Twine("Malformed regex in line ") + Twine(LineNo) + ": '" +
134 SplitLine.second + "': " + REError).str();
135 return false;
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000136 }
137
138 // Add this regexp into the proper group by its prefix.
Peter Collingbourne49062a92013-07-09 22:03:17 +0000139 if (!Regexps[Prefix][Category].empty())
140 Regexps[Prefix][Category] += "|";
Peter Collingbourne8b77f182013-07-16 17:56:07 +0000141 Regexps[Prefix][Category] += "^" + Regexp + "$";
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000142 }
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000143
144 // Iterate through each of the prefixes, and create Regexs for them.
Peter Collingbourne49062a92013-07-09 22:03:17 +0000145 for (StringMap<StringMap<std::string> >::const_iterator I = Regexps.begin(),
146 E = Regexps.end();
147 I != E; ++I) {
148 for (StringMap<std::string>::const_iterator II = I->second.begin(),
149 IE = I->second.end();
150 II != IE; ++II) {
Peter Collingbourned5feb7b2014-07-10 03:55:02 +0000151 Entries[I->getKey()][II->getKey()].RegEx.reset(new Regex(II->getValue()));
Peter Collingbourne49062a92013-07-09 22:03:17 +0000152 }
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000153 }
Alexey Samsonov9e4fdd22013-08-12 07:49:36 +0000154 return true;
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000155}
156
Peter Collingbourned5feb7b2014-07-10 03:55:02 +0000157SpecialCaseList::~SpecialCaseList() {}
Kostya Serebryany36dfc5c2012-08-24 16:40:11 +0000158
Craig Topper6dc4a8bc2014-08-30 16:48:02 +0000159bool SpecialCaseList::inSection(StringRef Section, StringRef Query,
160 StringRef Category) const {
Peter Collingbournebace6062013-08-05 17:48:04 +0000161 StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
Peter Collingbourne49062a92013-07-09 22:03:17 +0000162 if (I == Entries.end()) return false;
Peter Collingbournebace6062013-08-05 17:48:04 +0000163 StringMap<Entry>::const_iterator II = I->second.find(Category);
Peter Collingbourne49062a92013-07-09 22:03:17 +0000164 if (II == I->second.end()) return false;
165
Peter Collingbournebace6062013-08-05 17:48:04 +0000166 return II->getValue().match(Query);
Kostya Serebryany01401ce2012-03-14 23:22:10 +0000167}
168
169} // namespace llvm