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/DerivedTypes.h"
24 #include "llvm/Function.h"
25 #include "llvm/GlobalVariable.h"
26 #include "llvm/Module.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/Regex.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/Support/system_error.h"
34 BlackList::BlackList(const StringRef Path) {
35 // Validate and open blacklist file.
36 if (!Path.size()) return;
37 OwningPtr<MemoryBuffer> File;
38 if (error_code EC = MemoryBuffer::getFile(Path, File)) {
39 report_fatal_error("Can't open blacklist file: " + Path + ": " +
43 // Iterate through each line in the blacklist file.
44 SmallVector<StringRef, 16> Lines;
45 SplitString(File.take()->getBuffer(), Lines, "\n\r");
46 StringMap<std::string> Regexps;
47 for (SmallVector<StringRef, 16>::iterator I = Lines.begin(), E = Lines.end();
49 // Ignore empty lines and lines starting with "#"
50 if (I->empty() || I->startswith("#"))
52 // Get our prefix and unparsed regexp.
53 std::pair<StringRef, StringRef> SplitLine = I->split(":");
54 StringRef Prefix = SplitLine.first;
55 std::string Regexp = SplitLine.second;
58 for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
59 pos += strlen(".*")) {
60 Regexp.replace(pos, strlen("*"), ".*");
63 // Check that the regexp is valid.
64 Regex CheckRE(Regexp);
66 if (!CheckRE.isValid(Error)) {
67 report_fatal_error("malformed blacklist regex: " + SplitLine.second +
71 // Add this regexp into the proper group by its prefix.
72 if (Regexps[Prefix].size())
73 Regexps[Prefix] += "|";
74 Regexps[Prefix] += Regexp;
77 // Iterate through each of the prefixes, and create Regexs for them.
78 for (StringMap<std::string>::iterator I = Regexps.begin(), E = Regexps.end();
80 Entries[I->getKey()] = new Regex(I->getValue());
84 bool BlackList::isIn(const Function &F) {
85 return isIn(*F.getParent()) || inSection("fun", F.getName());
88 bool BlackList::isIn(const GlobalVariable &G) {
89 return isIn(*G.getParent()) || inSection("global", G.getName());
92 bool BlackList::isIn(const Module &M) {
93 return inSection("src", M.getModuleIdentifier());
96 static StringRef GetGVTypeString(const GlobalVariable &G) {
97 // Types of GlobalVariables are always pointer types.
98 Type *GType = G.getType()->getElementType();
99 // For now we support blacklisting struct types only.
100 if (StructType *SGType = dyn_cast<StructType>(GType)) {
101 if (!SGType->isLiteral())
102 return SGType->getName();
104 return "<unknown type>";
107 bool BlackList::isInInit(const GlobalVariable &G) {
108 return (isIn(*G.getParent()) ||
109 inSection("global-init", G.getName()) ||
110 inSection("global-init-type", GetGVTypeString(G)));
113 bool BlackList::inSection(const StringRef Section,
114 const StringRef Query) {
115 Regex *FunctionRegex = Entries[Section];
116 return FunctionRegex ? FunctionRegex->match(Query) : false;