Issue a fatal error if the line doesn't have a regular expression.
[oota-llvm.git] / lib / Transforms / Instrumentation / BlackList.cpp
1 //===-- BlackList.cpp - blacklist 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 based on a user-supplied blacklist.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include <utility>
17 #include <string>
18
19 #include "BlackList.h"
20 #include "llvm/ADT/OwningPtr.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Function.h"
25 #include "llvm/GlobalVariable.h"
26 #include "llvm/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"
31
32 namespace llvm {
33
34 BlackList::BlackList(const StringRef Path) {
35   // Validate and open blacklist file.
36   if (Path.empty()) return;
37   OwningPtr<MemoryBuffer> File;
38   if (error_code EC = MemoryBuffer::getFile(Path, File)) {
39     report_fatal_error("Can't open blacklist file: " + Path + ": " +
40                        EC.message());
41   }
42
43   // Iterate through each line in the blacklist file.
44   SmallVector<StringRef, 16> Lines;
45   SplitString(File.take()->getBuffer(), Lines, "\n\r");
46   StringMap<std::string> Regexps;
47   for (SmallVector<StringRef, 16>::iterator I = Lines.begin(), E = Lines.end();
48        I != E; ++I) {
49     // Ignore empty lines and lines starting with "#"
50     if (I->empty() || I->startswith("#"))
51       continue;
52     // Get our prefix and unparsed regexp.
53     std::pair<StringRef, StringRef> SplitLine = I->split(":");
54     StringRef Prefix = SplitLine.first;
55     std::string Regexp = SplitLine.second;
56     if (Regexp.empty()) {
57       // Missing ':' in the line.
58       report_fatal_error("malformed blacklist line: " + SplitLine.first);
59     }
60
61     // Replace * with .*
62     for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
63          pos += strlen(".*")) {
64       Regexp.replace(pos, strlen("*"), ".*");
65     }
66
67     // Check that the regexp is valid.
68     Regex CheckRE(Regexp);
69     std::string Error;
70     if (!CheckRE.isValid(Error)) {
71       report_fatal_error("malformed blacklist regex: " + SplitLine.second +
72           ": " + Error);
73     }
74
75     // Add this regexp into the proper group by its prefix.
76     if (!Regexps[Prefix].empty())
77       Regexps[Prefix] += "|";
78     Regexps[Prefix] += Regexp;
79   }
80
81   // Iterate through each of the prefixes, and create Regexs for them.
82   for (StringMap<std::string>::iterator I = Regexps.begin(), E = Regexps.end();
83        I != E; ++I) {
84     Entries[I->getKey()] = new Regex(I->getValue());
85   }
86 }
87
88 bool BlackList::isIn(const Function &F) {
89   return isIn(*F.getParent()) || inSection("fun", F.getName());
90 }
91
92 bool BlackList::isIn(const GlobalVariable &G) {
93   return isIn(*G.getParent()) || inSection("global", G.getName());
94 }
95
96 bool BlackList::isIn(const Module &M) {
97   return inSection("src", M.getModuleIdentifier());
98 }
99
100 static StringRef GetGVTypeString(const GlobalVariable &G) {
101   // Types of GlobalVariables are always pointer types.
102   Type *GType = G.getType()->getElementType();
103   // For now we support blacklisting struct types only.
104   if (StructType *SGType = dyn_cast<StructType>(GType)) {
105     if (!SGType->isLiteral())
106       return SGType->getName();
107   }
108   return "<unknown type>";
109 }
110
111 bool BlackList::isInInit(const GlobalVariable &G) {
112   return (isIn(*G.getParent()) ||
113           inSection("global-init", G.getName()) ||
114           inSection("global-init-type", GetGVTypeString(G)));
115 }
116
117 bool BlackList::inSection(const StringRef Section, const StringRef Query) {
118   StringMap<Regex*>::iterator I = Entries.find(Section);
119   if (I == Entries.end()) return false;
120
121   Regex *FunctionRegex = I->getValue();
122   return FunctionRegex->match(Query);
123 }
124
125 }  // namespace llvm