1 //===-- BlackList.cpp - blacklist for sanitizers --------------------------===//
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 or global
12 // variables based on a user-supplied blacklist.
14 //===----------------------------------------------------------------------===//
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"
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 + ": " +
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();
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;
54 for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
55 pos += strlen(".*")) {
56 Regexp.replace(pos, strlen("*"), ".*");
59 // Check that the regexp is valid.
60 Regex CheckRE(Regexp);
62 if (!CheckRE.isValid(Error)) {
63 report_fatal_error("malformed blacklist regex: " + SplitLine.second +
67 // Add this regexp into the proper group by its prefix.
68 if (Regexps[Prefix].size())
69 Regexps[Prefix] += "|";
70 Regexps[Prefix] += Regexp;
73 // Iterate through each of the prefixes, and create Regexs for them.
74 for (StringMap<std::string>::iterator I = Regexps.begin(), E = Regexps.end();
76 Entries[I->getKey()] = new Regex(I->getValue());
80 bool BlackList::isIn(const Function &F) {
81 return isIn(*F.getParent()) || inSection("fun", F.getName());
84 bool BlackList::isIn(const GlobalVariable &G) {
85 return isIn(*G.getParent()) || inSection("global", G.getName());
88 bool BlackList::isIn(const Module &M) {
89 return inSection("src", M.getModuleIdentifier());
92 bool BlackList::inSection(const StringRef Section,
93 const StringRef Query) {
94 Regex *FunctionRegex = Entries[Section];
95 return FunctionRegex ? FunctionRegex->match(Query) : false;