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