1 //===-- FunctionBlackList.cpp - blacklist of functions --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
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"
28 FunctionBlackList::FunctionBlackList(const std::string &Path) {
30 const char *kFunPrefix = "fun:";
31 if (!Path.size()) return;
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 + ": " +
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] == '*')
52 ThisFuncRE += ThisFunc[j];
54 // Check that the regexp is valid.
55 Regex CheckRE(ThisFuncRE);
57 if (!CheckRE.isValid(Error))
58 report_fatal_error("malformed blacklist regex: " + ThisFunc +
60 // Append to the final regexp.
67 Functions = new Regex(Fun);
71 bool FunctionBlackList::isIn(const Function &F) {
73 bool Res = Functions->match(F.getName());