[ASan] fixup for r167725: Don't fetch name of StructType if it is literal
[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.size()) 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
57     // Replace * with .*
58     for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
59          pos += strlen(".*")) {
60       Regexp.replace(pos, strlen("*"), ".*");
61     }
62
63     // Check that the regexp is valid.
64     Regex CheckRE(Regexp);
65     std::string Error;
66     if (!CheckRE.isValid(Error)) {
67       report_fatal_error("malformed blacklist regex: " + SplitLine.second +
68           ": " + Error);
69     }
70
71     // Add this regexp into the proper group by its prefix.
72     if (Regexps[Prefix].size())
73       Regexps[Prefix] += "|";
74     Regexps[Prefix] += Regexp;
75   }
76
77   // Iterate through each of the prefixes, and create Regexs for them.
78   for (StringMap<std::string>::iterator I = Regexps.begin(), E = Regexps.end();
79        I != E; ++I) {
80     Entries[I->getKey()] = new Regex(I->getValue());
81   }
82 }
83
84 bool BlackList::isIn(const Function &F) {
85   return isIn(*F.getParent()) || inSection("fun", F.getName());
86 }
87
88 bool BlackList::isIn(const GlobalVariable &G) {
89   return isIn(*G.getParent()) || inSection("global", G.getName());
90 }
91
92 bool BlackList::isIn(const Module &M) {
93   return inSection("src", M.getModuleIdentifier());
94 }
95
96 static StringRef GetGVTypeString(const GlobalVariable &G) {
97   // Types of GlobalVariables are always pointer types.
98   Type *GType = G.getType()->getElementType();
99   // For now we support blacklisting struct types only.
100   if (StructType *SGType = dyn_cast<StructType>(GType)) {
101     if (!SGType->isLiteral())
102       return SGType->getName();
103   }
104   return "<unknown type>";
105 }
106
107 bool BlackList::isInInit(const GlobalVariable &G) {
108   return (isIn(*G.getParent()) ||
109           inSection("global-init", G.getName()) ||
110           inSection("global-init-type", GetGVTypeString(G)));
111 }
112
113 bool BlackList::inSection(const StringRef Section,
114                           const StringRef Query) {
115   Regex *FunctionRegex = Entries[Section];
116   return FunctionRegex ? FunctionRegex->match(Query) : false;
117 }
118
119 }  // namespace llvm