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/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"
30 #include <system_error>
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 {
44 Entry() : RegEx(nullptr) {}
46 bool match(StringRef Query) const {
47 return Strings.count(Query) || (RegEx && RegEx->match(Query));
51 SpecialCaseList::SpecialCaseList() : Entries() {}
53 SpecialCaseList *SpecialCaseList::create(
54 const StringRef Path, std::string &Error) {
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();
62 return create(File.get(), Error);
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))
73 SpecialCaseList *SpecialCaseList::createOrDie(const StringRef Path) {
75 if (SpecialCaseList *SCL = create(Path, Error))
77 report_fatal_error(Error);
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");
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("#"))
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();
103 std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
104 std::string Regexp = SplitRegexp.first;
105 StringRef Category = SplitRegexp.second;
107 // Backwards compatibility.
108 if (Prefix == "global-init") {
111 } else if (Prefix == "global-init-type") {
114 } else if (Prefix == "global-init-src") {
119 // See if we can store Regexp in Strings.
120 if (Regex::isLiteralERE(Regexp)) {
121 Entries[Prefix][Category].Strings.insert(Regexp);
126 for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
127 pos += strlen(".*")) {
128 Regexp.replace(pos, strlen("*"), ".*");
131 // Check that the regexp is valid.
132 Regex CheckRE(Regexp);
134 if (!CheckRE.isValid(REError)) {
135 Error = (Twine("Malformed regex in line ") + Twine(LineNo) + ": '" +
136 SplitLine.second + "': " + REError).str();
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 + "$";
146 // Iterate through each of the prefixes, and create Regexs for them.
147 for (StringMap<StringMap<std::string> >::const_iterator I = Regexps.begin(),
150 for (StringMap<std::string>::const_iterator II = I->second.begin(),
151 IE = I->second.end();
153 Entries[I->getKey()][II->getKey()].RegEx = new Regex(II->getValue());
159 SpecialCaseList::~SpecialCaseList() {
160 for (StringMap<StringMap<Entry> >::iterator I = Entries.begin(),
163 for (StringMap<Entry>::const_iterator II = I->second.begin(),
164 IE = I->second.end();
166 delete II->second.RegEx;
171 bool SpecialCaseList::isIn(const Function& F, const StringRef Category) const {
172 return isIn(*F.getParent(), Category) ||
173 inSectionCategory("fun", F.getName(), Category);
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();
184 return "<unknown type>";
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);
194 bool SpecialCaseList::isIn(const GlobalAlias &GA,
195 const StringRef Category) const {
196 if (isIn(*GA.getParent(), Category))
199 if (isa<FunctionType>(GA.getType()->getElementType()))
200 return inSectionCategory("fun", GA.getName(), Category);
202 return inSectionCategory("global", GA.getName(), Category) ||
203 inSectionCategory("type", GetGlobalTypeString(GA), Category);
206 bool SpecialCaseList::isIn(const Module &M, const StringRef Category) const {
207 return inSectionCategory("src", M.getModuleIdentifier(), Category);
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;
218 return II->getValue().match(Query);