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(const StringRef Path) {
53 // Validate and open blacklist file.
54 if (Path.empty()) return;
55 OwningPtr<MemoryBuffer> File;
56 if (error_code EC = MemoryBuffer::getFile(Path, File)) {
57 report_fatal_error("Can't open blacklist file: " + Path + ": " +
64 SpecialCaseList::SpecialCaseList(const MemoryBuffer *MB) {
68 void SpecialCaseList::init(const MemoryBuffer *MB) {
69 // Iterate through each line in the blacklist file.
70 SmallVector<StringRef, 16> Lines;
71 SplitString(MB->getBuffer(), Lines, "\n\r");
72 StringMap<StringMap<std::string> > Regexps;
73 for (SmallVectorImpl<StringRef>::iterator I = Lines.begin(), E = Lines.end();
75 // Ignore empty lines and lines starting with "#"
76 if (I->empty() || I->startswith("#"))
78 // Get our prefix and unparsed regexp.
79 std::pair<StringRef, StringRef> SplitLine = I->split(":");
80 StringRef Prefix = SplitLine.first;
81 if (SplitLine.second.empty()) {
82 // Missing ':' in the line.
83 report_fatal_error("malformed blacklist line: " + SplitLine.first);
86 std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
87 std::string Regexp = SplitRegexp.first;
88 StringRef Category = SplitRegexp.second;
90 // Backwards compatibility.
91 if (Prefix == "global-init") {
94 } else if (Prefix == "global-init-type") {
97 } else if (Prefix == "global-init-src") {
102 // See if we can store Regexp in Strings.
103 if (Regex::isLiteralERE(Regexp)) {
104 Entries[Prefix][Category].Strings.insert(Regexp);
109 for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
110 pos += strlen(".*")) {
111 Regexp.replace(pos, strlen("*"), ".*");
114 // Check that the regexp is valid.
115 Regex CheckRE(Regexp);
117 if (!CheckRE.isValid(Error)) {
118 report_fatal_error("malformed blacklist regex: " + SplitLine.second +
122 // Add this regexp into the proper group by its prefix.
123 if (!Regexps[Prefix][Category].empty())
124 Regexps[Prefix][Category] += "|";
125 Regexps[Prefix][Category] += "^" + Regexp + "$";
128 // Iterate through each of the prefixes, and create Regexs for them.
129 for (StringMap<StringMap<std::string> >::const_iterator I = Regexps.begin(),
132 for (StringMap<std::string>::const_iterator II = I->second.begin(),
133 IE = I->second.end();
135 Entries[I->getKey()][II->getKey()].RegEx = new Regex(II->getValue());
140 SpecialCaseList::~SpecialCaseList() {
141 for (StringMap<StringMap<Entry> >::iterator I = Entries.begin(),
144 for (StringMap<Entry>::const_iterator II = I->second.begin(),
145 IE = I->second.end();
147 delete II->second.RegEx;
152 bool SpecialCaseList::findCategory(const Function &F,
153 StringRef &Category) const {
154 return findCategory(*F.getParent(), Category) ||
155 findCategory("fun", F.getName(), Category);
158 bool SpecialCaseList::isIn(const Function& F, const StringRef Category) const {
159 return isIn(*F.getParent(), Category) ||
160 inSectionCategory("fun", F.getName(), Category);
163 static StringRef GetGVTypeString(const GlobalVariable &G) {
164 // Types of GlobalVariables are always pointer types.
165 Type *GType = G.getType()->getElementType();
166 // For now we support blacklisting struct types only.
167 if (StructType *SGType = dyn_cast<StructType>(GType)) {
168 if (!SGType->isLiteral())
169 return SGType->getName();
171 return "<unknown type>";
174 bool SpecialCaseList::findCategory(const GlobalVariable &G,
175 StringRef &Category) const {
176 return findCategory(*G.getParent(), Category) ||
177 findCategory("global", G.getName(), Category) ||
178 findCategory("type", GetGVTypeString(G), Category);
181 bool SpecialCaseList::isIn(const GlobalVariable &G,
182 const StringRef Category) const {
183 return isIn(*G.getParent(), Category) ||
184 inSectionCategory("global", G.getName(), Category) ||
185 inSectionCategory("type", GetGVTypeString(G), Category);
188 bool SpecialCaseList::findCategory(const Module &M, StringRef &Category) const {
189 return findCategory("src", M.getModuleIdentifier(), Category);
192 bool SpecialCaseList::isIn(const Module &M, const StringRef Category) const {
193 return inSectionCategory("src", M.getModuleIdentifier(), Category);
196 bool SpecialCaseList::findCategory(const StringRef Section,
197 const StringRef Query,
198 StringRef &Category) const {
199 StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
200 if (I == Entries.end()) return false;
202 for (StringMap<Entry>::const_iterator II = I->second.begin(),
203 IE = I->second.end();
205 if (II->getValue().match(Query)) {
206 Category = II->first();
214 bool SpecialCaseList::inSectionCategory(const StringRef Section,
215 const StringRef Query,
216 const StringRef Category) const {
217 StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
218 if (I == Entries.end()) return false;
219 StringMap<Entry>::const_iterator II = I->second.find(Category);
220 if (II == I->second.end()) return false;
222 return II->getValue().match(Query);