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 //===----------------------------------------------------------------------===//
16 #include "BlackList.h"
17 #include "llvm/ADT/OwningPtr.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Function.h"
22 #include "llvm/GlobalVariable.h"
23 #include "llvm/Module.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/Regex.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Support/system_error.h"
33 BlackList::BlackList(const StringRef Path) {
34 // Validate and open blacklist file.
35 if (Path.empty()) 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 // Ignore empty lines and lines starting with "#"
49 if (I->empty() || I->startswith("#"))
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;
56 // Missing ':' in the line.
57 report_fatal_error("malformed blacklist line: " + SplitLine.first);
61 for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
62 pos += strlen(".*")) {
63 Regexp.replace(pos, strlen("*"), ".*");
66 // Check that the regexp is valid.
67 Regex CheckRE(Regexp);
69 if (!CheckRE.isValid(Error)) {
70 report_fatal_error("malformed blacklist regex: " + SplitLine.second +
74 // Add this regexp into the proper group by its prefix.
75 if (!Regexps[Prefix].empty())
76 Regexps[Prefix] += "|";
77 Regexps[Prefix] += Regexp;
80 // Iterate through each of the prefixes, and create Regexs for them.
81 for (StringMap<std::string>::iterator I = Regexps.begin(), E = Regexps.end();
83 Entries[I->getKey()] = new Regex(I->getValue());
87 bool BlackList::isIn(const Function &F) {
88 return isIn(*F.getParent()) || inSection("fun", F.getName());
91 bool BlackList::isIn(const GlobalVariable &G) {
92 return isIn(*G.getParent()) || inSection("global", G.getName());
95 bool BlackList::isIn(const Module &M) {
96 return inSection("src", M.getModuleIdentifier());
99 static StringRef GetGVTypeString(const GlobalVariable &G) {
100 // Types of GlobalVariables are always pointer types.
101 Type *GType = G.getType()->getElementType();
102 // For now we support blacklisting struct types only.
103 if (StructType *SGType = dyn_cast<StructType>(GType)) {
104 if (!SGType->isLiteral())
105 return SGType->getName();
107 return "<unknown type>";
110 bool BlackList::isInInit(const GlobalVariable &G) {
111 return (isIn(*G.getParent()) ||
112 inSection("global-init", G.getName()) ||
113 inSection("global-init-type", GetGVTypeString(G)));
116 bool BlackList::inSection(const StringRef Section, const StringRef Query) {
117 StringMap<Regex*>::iterator I = Entries.find(Section);
118 if (I == Entries.end()) return false;
120 Regex *FunctionRegex = I->getValue();
121 return FunctionRegex->match(Query);