1 //===-- SpecialCaseList.cpp - special case list for sanitizers ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/Utils/SpecialCaseList.h"
18 #include "llvm/ADT/OwningPtr.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/ADT/StringSet.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/GlobalVariable.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/Regex.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/Support/system_error.h"
36 /// Represents a set of regular expressions. Regular expressions which are
37 /// "literal" (i.e. no regex metacharacters) are stored in Strings, while all
38 /// others are represented as a single pipe-separated regex in RegEx. The
39 /// reason for doing so is efficiency; StringSet is much faster at matching
40 /// literal strings than Regex.
41 struct SpecialCaseList::Entry {
47 bool match(StringRef Query) const {
48 return Strings.count(Query) || (RegEx && RegEx->match(Query));
52 SpecialCaseList::SpecialCaseList() : Entries() {}
54 SpecialCaseList::SpecialCaseList(const StringRef Path) {
55 // Validate and open blacklist file.
56 if (Path.empty()) return;
57 OwningPtr<MemoryBuffer> File;
58 if (error_code EC = MemoryBuffer::getFile(Path, File)) {
59 report_fatal_error("Can't open file '" + Path + "': " +
64 if (!parse(File.get(), Error))
65 report_fatal_error(Error);
68 SpecialCaseList::SpecialCaseList(const MemoryBuffer *MB) {
70 if (!parse(MB, Error))
71 report_fatal_error(Error);
74 SpecialCaseList *SpecialCaseList::create(
75 const StringRef Path, std::string &Error) {
77 return new SpecialCaseList();
78 OwningPtr<MemoryBuffer> File;
79 if (error_code EC = MemoryBuffer::getFile(Path, File)) {
80 Error = (Twine("Can't open file '") + Path + "': " + EC.message()).str();
83 return create(File.get(), Error);
86 SpecialCaseList *SpecialCaseList::create(
87 const MemoryBuffer *MB, std::string &Error) {
88 OwningPtr<SpecialCaseList> SCL(new SpecialCaseList());
89 if (!SCL->parse(MB, Error))
94 bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
95 // Iterate through each line in the blacklist file.
96 SmallVector<StringRef, 16> Lines;
97 SplitString(MB->getBuffer(), Lines, "\n\r");
98 StringMap<StringMap<std::string> > Regexps;
99 assert(Entries.empty() &&
100 "parse() should be called on an empty SpecialCaseList");
102 for (SmallVectorImpl<StringRef>::iterator I = Lines.begin(), E = Lines.end();
103 I != E; ++I, ++LineNo) {
104 // Ignore empty lines and lines starting with "#"
105 if (I->empty() || I->startswith("#"))
107 // Get our prefix and unparsed regexp.
108 std::pair<StringRef, StringRef> SplitLine = I->split(":");
109 StringRef Prefix = SplitLine.first;
110 if (SplitLine.second.empty()) {
111 // Missing ':' in the line.
112 Error = (Twine("Malformed line ") + Twine(LineNo) + ": '" +
113 SplitLine.first + "'").str();
117 std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
118 std::string Regexp = SplitRegexp.first;
119 StringRef Category = SplitRegexp.second;
121 // Backwards compatibility.
122 if (Prefix == "global-init") {
125 } else if (Prefix == "global-init-type") {
128 } else if (Prefix == "global-init-src") {
133 // See if we can store Regexp in Strings.
134 if (Regex::isLiteralERE(Regexp)) {
135 Entries[Prefix][Category].Strings.insert(Regexp);
140 for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
141 pos += strlen(".*")) {
142 Regexp.replace(pos, strlen("*"), ".*");
145 // Check that the regexp is valid.
146 Regex CheckRE(Regexp);
148 if (!CheckRE.isValid(REError)) {
149 Error = (Twine("Malformed regex in line ") + Twine(LineNo) + ": '" +
150 SplitLine.second + "': " + REError).str();
154 // Add this regexp into the proper group by its prefix.
155 if (!Regexps[Prefix][Category].empty())
156 Regexps[Prefix][Category] += "|";
157 Regexps[Prefix][Category] += "^" + Regexp + "$";
160 // Iterate through each of the prefixes, and create Regexs for them.
161 for (StringMap<StringMap<std::string> >::const_iterator I = Regexps.begin(),
164 for (StringMap<std::string>::const_iterator II = I->second.begin(),
165 IE = I->second.end();
167 Entries[I->getKey()][II->getKey()].RegEx = new Regex(II->getValue());
173 SpecialCaseList::~SpecialCaseList() {
174 for (StringMap<StringMap<Entry> >::iterator I = Entries.begin(),
177 for (StringMap<Entry>::const_iterator II = I->second.begin(),
178 IE = I->second.end();
180 delete II->second.RegEx;
185 bool SpecialCaseList::findCategory(const Function &F,
186 StringRef &Category) const {
187 return findCategory(*F.getParent(), Category) ||
188 findCategory("fun", F.getName(), Category);
191 bool SpecialCaseList::isIn(const Function& F, const StringRef Category) const {
192 return isIn(*F.getParent(), Category) ||
193 inSectionCategory("fun", F.getName(), Category);
196 static StringRef GetGVTypeString(const GlobalVariable &G) {
197 // Types of GlobalVariables are always pointer types.
198 Type *GType = G.getType()->getElementType();
199 // For now we support blacklisting struct types only.
200 if (StructType *SGType = dyn_cast<StructType>(GType)) {
201 if (!SGType->isLiteral())
202 return SGType->getName();
204 return "<unknown type>";
207 bool SpecialCaseList::findCategory(const GlobalVariable &G,
208 StringRef &Category) const {
209 return findCategory(*G.getParent(), Category) ||
210 findCategory("global", G.getName(), Category) ||
211 findCategory("type", GetGVTypeString(G), Category);
214 bool SpecialCaseList::isIn(const GlobalVariable &G,
215 const StringRef Category) const {
216 return isIn(*G.getParent(), Category) ||
217 inSectionCategory("global", G.getName(), Category) ||
218 inSectionCategory("type", GetGVTypeString(G), Category);
221 bool SpecialCaseList::findCategory(const Module &M, StringRef &Category) const {
222 return findCategory("src", M.getModuleIdentifier(), Category);
225 bool SpecialCaseList::isIn(const Module &M, const StringRef Category) const {
226 return inSectionCategory("src", M.getModuleIdentifier(), Category);
229 bool SpecialCaseList::findCategory(const StringRef Section,
230 const StringRef Query,
231 StringRef &Category) const {
232 StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
233 if (I == Entries.end()) return false;
235 for (StringMap<Entry>::const_iterator II = I->second.begin(),
236 IE = I->second.end();
238 if (II->getValue().match(Query)) {
239 Category = II->first();
247 bool SpecialCaseList::inSectionCategory(const StringRef Section,
248 const StringRef Query,
249 const StringRef Category) const {
250 StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
251 if (I == Entries.end()) return false;
252 StringMap<Entry>::const_iterator II = I->second.find(Category);
253 if (II == I->second.end()) return false;
255 return II->getValue().match(Query);