[asan/tsan] extend the functionality of FunctionBlackList to globals and modules...
[oota-llvm.git] / lib / Transforms / Instrumentation / FunctionBlackList.cpp
1 //===-- FunctionBlackList.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 "FunctionBlackList.h"
20 #include "llvm/ADT/OwningPtr.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/Function.h"
24 #include "llvm/GlobalVariable.h"
25 #include "llvm/Module.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/Regex.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Support/system_error.h"
30
31 namespace llvm {
32
33 FunctionBlackList::FunctionBlackList(const StringRef Path) {
34   // Validate and open blacklist file.
35   if (!Path.size()) 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   // Iterate through each line in the blacklist file.
43   SmallVector<StringRef, 16> Lines;
44   SplitString(File.take()->getBuffer(), Lines, "\n\r");
45   StringMap<std::string> Regexps;
46   for (SmallVector<StringRef, 16>::iterator I = Lines.begin(), E = Lines.end();
47        I != E; ++I) {
48     // Get our prefix and unparsed regexp.
49     std::pair<StringRef, StringRef> SplitLine = I->split(":");
50     StringRef Prefix = SplitLine.first;
51     std::string Regexp = SplitLine.second;
52
53     // Replace * with .*
54     for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
55          pos += strlen(".*")) {
56       Regexp.replace(pos, strlen("*"), ".*");
57     }
58
59     // Check that the regexp is valid.
60     Regex CheckRE(Regexp);
61     std::string Error;
62     if (!CheckRE.isValid(Error)) {
63       report_fatal_error("malformed blacklist regex: " + SplitLine.second +
64           ": " + Error);
65     }
66
67     // Add this regexp into the proper group by its prefix.
68     if (Regexps[Prefix].size())
69       Regexps[Prefix] += "|";
70     Regexps[Prefix] += Regexp;
71   }
72
73   // Iterate through each of the prefixes, and create Regexs for them.
74   for (StringMap<std::string>::iterator I = Regexps.begin(), E = Regexps.end();
75        I != E; ++I) {
76     Entries[I->getKey()] = new Regex(I->getValue());
77   }
78 }
79
80 bool FunctionBlackList::isIn(const Function &F) {
81   return isIn(*F.getParent()) || inSection("fun", F.getName());
82 }
83
84 bool FunctionBlackList::isIn(const GlobalVariable &G) {
85   return isIn(*G.getParent()) || inSection("global", G.getName());
86 }
87
88 bool FunctionBlackList::isIn(const Module &M) {
89   return inSection("src", M.getModuleIdentifier());
90 }
91
92 bool FunctionBlackList::inSection(const StringRef Section,
93                                   const StringRef Query) {
94   Regex *FunctionRegex = Entries[Section];
95   return FunctionRegex ? FunctionRegex->match(Query) : false;
96 }
97
98 }  // namespace llvm