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.empty()) 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;
57 // Missing ':' in the line.
58 report_fatal_error("malformed blacklist line: " + SplitLine.first);
62 for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
63 pos += strlen(".*")) {
64 Regexp.replace(pos, strlen("*"), ".*");
67 // Check that the regexp is valid.
68 Regex CheckRE(Regexp);
70 if (!CheckRE.isValid(Error)) {
71 report_fatal_error("malformed blacklist regex: " + SplitLine.second +
75 // Add this regexp into the proper group by its prefix.
76 if (!Regexps[Prefix].empty())
77 Regexps[Prefix] += "|";
78 Regexps[Prefix] += Regexp;
81 // Iterate through each of the prefixes, and create Regexs for them.
82 for (StringMap<std::string>::iterator I = Regexps.begin(), E = Regexps.end();
84 Entries[I->getKey()] = new Regex(I->getValue());
88 bool BlackList::isIn(const Function &F) {
89 return isIn(*F.getParent()) || inSection("fun", F.getName());
92 bool BlackList::isIn(const GlobalVariable &G) {
93 return isIn(*G.getParent()) || inSection("global", G.getName());
96 bool BlackList::isIn(const Module &M) {
97 return inSection("src", M.getModuleIdentifier());
100 static StringRef GetGVTypeString(const GlobalVariable &G) {
101 // Types of GlobalVariables are always pointer types.
102 Type *GType = G.getType()->getElementType();
103 // For now we support blacklisting struct types only.
104 if (StructType *SGType = dyn_cast<StructType>(GType)) {
105 if (!SGType->isLiteral())
106 return SGType->getName();
108 return "<unknown type>";
111 bool BlackList::isInInit(const GlobalVariable &G) {
112 return (isIn(*G.getParent()) ||
113 inSection("global-init", G.getName()) ||
114 inSection("global-init-type", GetGVTypeString(G)));
117 bool BlackList::inSection(const StringRef Section, const StringRef Query) {
118 StringMap<Regex*>::iterator I = Entries.find(Section);
119 if (I == Entries.end()) return false;
121 Regex *FunctionRegex = I->getValue();
122 return FunctionRegex->match(Query);