1 //===-- SpecialCaseList.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 "llvm/Transforms/Utils/SpecialCaseList.h"
17 #include "llvm/ADT/OwningPtr.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/GlobalVariable.h"
23 #include "llvm/IR/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 SpecialCaseList::SpecialCaseList(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 + ": " +
45 SpecialCaseList::SpecialCaseList(const MemoryBuffer *MB) {
49 void SpecialCaseList::init(const MemoryBuffer *MB) {
50 // Iterate through each line in the blacklist file.
51 SmallVector<StringRef, 16> Lines;
52 SplitString(MB->getBuffer(), Lines, "\n\r");
53 StringMap<std::string> Regexps;
54 for (SmallVectorImpl<StringRef>::iterator I = Lines.begin(), E = Lines.end();
56 // Ignore empty lines and lines starting with "#"
57 if (I->empty() || I->startswith("#"))
59 // Get our prefix and unparsed regexp.
60 std::pair<StringRef, StringRef> SplitLine = I->split(":");
61 StringRef Prefix = SplitLine.first;
62 std::string Regexp = SplitLine.second;
64 // Missing ':' in the line.
65 report_fatal_error("malformed blacklist line: " + SplitLine.first);
69 for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
70 pos += strlen(".*")) {
71 Regexp.replace(pos, strlen("*"), ".*");
74 // Check that the regexp is valid.
75 Regex CheckRE(Regexp);
77 if (!CheckRE.isValid(Error)) {
78 report_fatal_error("malformed blacklist regex: " + SplitLine.second +
82 // Add this regexp into the proper group by its prefix.
83 if (!Regexps[Prefix].empty())
84 Regexps[Prefix] += "|";
85 Regexps[Prefix] += Regexp;
88 // Iterate through each of the prefixes, and create Regexs for them.
89 for (StringMap<std::string>::const_iterator I = Regexps.begin(),
90 E = Regexps.end(); I != E; ++I) {
91 Entries[I->getKey()] = new Regex(I->getValue());
95 bool SpecialCaseList::isIn(const Function &F) const {
96 return isIn(*F.getParent()) || inSection("fun", F.getName());
99 bool SpecialCaseList::isIn(const GlobalVariable &G) const {
100 return isIn(*G.getParent()) || inSection("global", G.getName());
103 bool SpecialCaseList::isIn(const Module &M) const {
104 return inSection("src", M.getModuleIdentifier());
107 static StringRef GetGVTypeString(const GlobalVariable &G) {
108 // Types of GlobalVariables are always pointer types.
109 Type *GType = G.getType()->getElementType();
110 // For now we support blacklisting struct types only.
111 if (StructType *SGType = dyn_cast<StructType>(GType)) {
112 if (!SGType->isLiteral())
113 return SGType->getName();
115 return "<unknown type>";
118 bool SpecialCaseList::isInInit(const GlobalVariable &G) const {
119 return (isIn(*G.getParent()) ||
120 inSection("global-init", G.getName()) ||
121 inSection("global-init-type", GetGVTypeString(G)) ||
122 inSection("global-init-src", G.getParent()->getModuleIdentifier()));
125 bool SpecialCaseList::inSection(const StringRef Section,
126 const StringRef Query) const {
127 StringMap<Regex*>::const_iterator I = Entries.find(Section);
128 if (I == Entries.end()) return false;
130 Regex *FunctionRegex = I->getValue();
131 return FunctionRegex->match(Query);