]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/lib/Transforms/Instrumentation/FunctionBlackList.cpp
MFC r234353:
[FreeBSD/stable/9.git] / contrib / llvm / lib / Transforms / Instrumentation / FunctionBlackList.cpp
1 //===-- FunctionBlackList.cpp - blacklist of functions --------------------===//
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 based on
12 // user-supplied blacklist.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "FunctionBlackList.h"
17 #include "llvm/ADT/OwningPtr.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Function.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/Regex.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Support/system_error.h"
25
26 namespace llvm {
27
28 FunctionBlackList::FunctionBlackList(const std::string &Path) {
29   Functions = NULL;
30   const char *kFunPrefix = "fun:";
31   if (!Path.size()) return;
32   std::string Fun;
33
34   OwningPtr<MemoryBuffer> File;
35   if (error_code EC = MemoryBuffer::getFile(Path.c_str(), File)) {
36     report_fatal_error("Can't open blacklist file " + Path + ": " +
37                        EC.message());
38   }
39   MemoryBuffer *Buff = File.take();
40   const char *Data = Buff->getBufferStart();
41   size_t DataLen = Buff->getBufferSize();
42   SmallVector<StringRef, 16> Lines;
43   SplitString(StringRef(Data, DataLen), Lines, "\n\r");
44   for (size_t i = 0, numLines = Lines.size(); i < numLines; i++) {
45     if (Lines[i].startswith(kFunPrefix)) {
46       std::string ThisFunc = Lines[i].substr(strlen(kFunPrefix));
47       std::string ThisFuncRE;
48       // add ThisFunc replacing * with .*
49       for (size_t j = 0, n = ThisFunc.size(); j < n; j++) {
50         if (ThisFunc[j] == '*')
51           ThisFuncRE += '.';
52         ThisFuncRE += ThisFunc[j];
53       }
54       // Check that the regexp is valid.
55       Regex CheckRE(ThisFuncRE);
56       std::string Error;
57       if (!CheckRE.isValid(Error))
58         report_fatal_error("malformed blacklist regex: " + ThisFunc +
59                            ": " + Error);
60       // Append to the final regexp.
61       if (Fun.size())
62         Fun += "|";
63       Fun += ThisFuncRE;
64     }
65   }
66   if (Fun.size()) {
67     Functions = new Regex(Fun);
68   }
69 }
70
71 bool FunctionBlackList::isIn(const Function &F) {
72   if (Functions) {
73     bool Res = Functions->match(F.getName());
74     return Res;
75   }
76   return false;
77 }
78
79 }  // namespace llvm