[C++] Use 'nullptr'. Transforms edition.
[oota-llvm.git] / lib / Transforms / Utils / SpecialCaseList.cpp
1 //===-- SpecialCaseList.cpp - special case list for sanitizers ------------===//
2 //
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 //
10 // This is a utility class for instrumentation passes (like AddressSanitizer
11 // or ThreadSanitizer) to avoid instrumenting some functions or global
12 // variables, or to instrument some functions or global variables in a specific
13 // way, based on a user-supplied list.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Transforms/Utils/SpecialCaseList.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/StringSet.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/GlobalVariable.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/Regex.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Support/system_error.h"
30 #include <string>
31 #include <utility>
32
33 namespace llvm {
34
35 /// Represents a set of regular expressions.  Regular expressions which are
36 /// "literal" (i.e. no regex metacharacters) are stored in Strings, while all
37 /// others are represented as a single pipe-separated regex in RegEx.  The
38 /// reason for doing so is efficiency; StringSet is much faster at matching
39 /// literal strings than Regex.
40 struct SpecialCaseList::Entry {
41   StringSet<> Strings;
42   Regex *RegEx;
43
44   Entry() : RegEx(nullptr) {}
45
46   bool match(StringRef Query) const {
47     return Strings.count(Query) || (RegEx && RegEx->match(Query));
48   }
49 };
50
51 SpecialCaseList::SpecialCaseList() : Entries() {}
52
53 SpecialCaseList *SpecialCaseList::create(
54     const StringRef Path, std::string &Error) {
55   if (Path.empty())
56     return new SpecialCaseList();
57   std::unique_ptr<MemoryBuffer> File;
58   if (error_code EC = MemoryBuffer::getFile(Path, File)) {
59     Error = (Twine("Can't open file '") + Path + "': " + EC.message()).str();
60     return nullptr;
61   }
62   return create(File.get(), Error);
63 }
64
65 SpecialCaseList *SpecialCaseList::create(
66     const MemoryBuffer *MB, std::string &Error) {
67   std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
68   if (!SCL->parse(MB, Error))
69     return nullptr;
70   return SCL.release();
71 }
72
73 SpecialCaseList *SpecialCaseList::createOrDie(const StringRef Path) {
74   std::string Error;
75   if (SpecialCaseList *SCL = create(Path, Error))
76     return SCL;
77   report_fatal_error(Error);
78 }
79
80 bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
81   // Iterate through each line in the blacklist file.
82   SmallVector<StringRef, 16> Lines;
83   SplitString(MB->getBuffer(), Lines, "\n\r");
84   StringMap<StringMap<std::string> > Regexps;
85   assert(Entries.empty() &&
86          "parse() should be called on an empty SpecialCaseList");
87   int LineNo = 1;
88   for (SmallVectorImpl<StringRef>::iterator I = Lines.begin(), E = Lines.end();
89        I != E; ++I, ++LineNo) {
90     // Ignore empty lines and lines starting with "#"
91     if (I->empty() || I->startswith("#"))
92       continue;
93     // Get our prefix and unparsed regexp.
94     std::pair<StringRef, StringRef> SplitLine = I->split(":");
95     StringRef Prefix = SplitLine.first;
96     if (SplitLine.second.empty()) {
97       // Missing ':' in the line.
98       Error = (Twine("Malformed line ") + Twine(LineNo) + ": '" +
99                SplitLine.first + "'").str();
100       return false;
101     }
102
103     std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
104     std::string Regexp = SplitRegexp.first;
105     StringRef Category = SplitRegexp.second;
106
107     // Backwards compatibility.
108     if (Prefix == "global-init") {
109       Prefix = "global";
110       Category = "init";
111     } else if (Prefix == "global-init-type") {
112       Prefix = "type";
113       Category = "init";
114     } else if (Prefix == "global-init-src") {
115       Prefix = "src";
116       Category = "init";
117     }
118
119     // See if we can store Regexp in Strings.
120     if (Regex::isLiteralERE(Regexp)) {
121       Entries[Prefix][Category].Strings.insert(Regexp);
122       continue;
123     }
124
125     // Replace * with .*
126     for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
127          pos += strlen(".*")) {
128       Regexp.replace(pos, strlen("*"), ".*");
129     }
130
131     // Check that the regexp is valid.
132     Regex CheckRE(Regexp);
133     std::string REError;
134     if (!CheckRE.isValid(REError)) {
135       Error = (Twine("Malformed regex in line ") + Twine(LineNo) + ": '" +
136                SplitLine.second + "': " + REError).str();
137       return false;
138     }
139
140     // Add this regexp into the proper group by its prefix.
141     if (!Regexps[Prefix][Category].empty())
142       Regexps[Prefix][Category] += "|";
143     Regexps[Prefix][Category] += "^" + Regexp + "$";
144   }
145
146   // Iterate through each of the prefixes, and create Regexs for them.
147   for (StringMap<StringMap<std::string> >::const_iterator I = Regexps.begin(),
148                                                           E = Regexps.end();
149        I != E; ++I) {
150     for (StringMap<std::string>::const_iterator II = I->second.begin(),
151                                                 IE = I->second.end();
152          II != IE; ++II) {
153       Entries[I->getKey()][II->getKey()].RegEx = new Regex(II->getValue());
154     }
155   }
156   return true;
157 }
158
159 SpecialCaseList::~SpecialCaseList() {
160   for (StringMap<StringMap<Entry> >::iterator I = Entries.begin(),
161                                               E = Entries.end();
162        I != E; ++I) {
163     for (StringMap<Entry>::const_iterator II = I->second.begin(),
164                                           IE = I->second.end();
165          II != IE; ++II) {
166       delete II->second.RegEx;
167     }
168   }
169 }
170
171 bool SpecialCaseList::isIn(const Function& F, const StringRef Category) const {
172   return isIn(*F.getParent(), Category) ||
173          inSectionCategory("fun", F.getName(), Category);
174 }
175
176 static StringRef GetGlobalTypeString(const GlobalValue &G) {
177   // Types of GlobalVariables are always pointer types.
178   Type *GType = G.getType()->getElementType();
179   // For now we support blacklisting struct types only.
180   if (StructType *SGType = dyn_cast<StructType>(GType)) {
181     if (!SGType->isLiteral())
182       return SGType->getName();
183   }
184   return "<unknown type>";
185 }
186
187 bool SpecialCaseList::isIn(const GlobalVariable &G,
188                            const StringRef Category) const {
189   return isIn(*G.getParent(), Category) ||
190          inSectionCategory("global", G.getName(), Category) ||
191          inSectionCategory("type", GetGlobalTypeString(G), Category);
192 }
193
194 bool SpecialCaseList::isIn(const GlobalAlias &GA,
195                            const StringRef Category) const {
196   if (isIn(*GA.getParent(), Category))
197     return true;
198
199   if (isa<FunctionType>(GA.getType()->getElementType()))
200     return inSectionCategory("fun", GA.getName(), Category);
201
202   return inSectionCategory("global", GA.getName(), Category) ||
203          inSectionCategory("type", GetGlobalTypeString(GA), Category);
204 }
205
206 bool SpecialCaseList::isIn(const Module &M, const StringRef Category) const {
207   return inSectionCategory("src", M.getModuleIdentifier(), Category);
208 }
209
210 bool SpecialCaseList::inSectionCategory(const StringRef Section,
211                                         const StringRef Query,
212                                         const StringRef Category) const {
213   StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
214   if (I == Entries.end()) return false;
215   StringMap<Entry>::const_iterator II = I->second.find(Category);
216   if (II == I->second.end()) return false;
217
218   return II->getValue().match(Query);
219 }
220
221 }  // namespace llvm