]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/lib/Transforms/Instrumentation/BlackList.cpp
MFC r244628:
[FreeBSD/stable/9.git] / contrib / llvm / 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/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 BlackList::BlackList(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     // Ignore empty lines and lines starting with "#"
49     if (I->empty() || I->startswith("#"))
50       continue;
51     // Get our prefix and unparsed regexp.
52     std::pair<StringRef, StringRef> SplitLine = I->split(":");
53     StringRef Prefix = SplitLine.first;
54     std::string Regexp = SplitLine.second;
55
56     // Replace * with .*
57     for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
58          pos += strlen(".*")) {
59       Regexp.replace(pos, strlen("*"), ".*");
60     }
61
62     // Check that the regexp is valid.
63     Regex CheckRE(Regexp);
64     std::string Error;
65     if (!CheckRE.isValid(Error)) {
66       report_fatal_error("malformed blacklist regex: " + SplitLine.second +
67           ": " + Error);
68     }
69
70     // Add this regexp into the proper group by its prefix.
71     if (Regexps[Prefix].size())
72       Regexps[Prefix] += "|";
73     Regexps[Prefix] += Regexp;
74   }
75
76   // Iterate through each of the prefixes, and create Regexs for them.
77   for (StringMap<std::string>::iterator I = Regexps.begin(), E = Regexps.end();
78        I != E; ++I) {
79     Entries[I->getKey()] = new Regex(I->getValue());
80   }
81 }
82
83 bool BlackList::isIn(const Function &F) {
84   return isIn(*F.getParent()) || inSection("fun", F.getName());
85 }
86
87 bool BlackList::isIn(const GlobalVariable &G) {
88   return isIn(*G.getParent()) || inSection("global", G.getName());
89 }
90
91 bool BlackList::isIn(const Module &M) {
92   return inSection("src", M.getModuleIdentifier());
93 }
94
95 bool BlackList::isInInit(const GlobalVariable &G) {
96   return isIn(*G.getParent()) || inSection("global-init", G.getName());
97 }
98
99 bool BlackList::inSection(const StringRef Section,
100                                   const StringRef Query) {
101   Regex *FunctionRegex = Entries[Section];
102   return FunctionRegex ? FunctionRegex->match(Query) : false;
103 }
104
105 }  // namespace llvm