2012-08-24 16:44:47 +00:00
|
|
|
//===-- BlackList.cpp - blacklist for sanitizers --------------------------===//
|
2012-03-14 23:22:10 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-08-24 16:40:11 +00:00
|
|
|
// This is a utility class for instrumentation passes (like AddressSanitizer
|
|
|
|
// or ThreadSanitizer) to avoid instrumenting some functions or global
|
|
|
|
// variables based on a user-supplied blacklist.
|
2012-03-14 23:22:10 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-08-24 16:44:47 +00:00
|
|
|
#include "BlackList.h"
|
2012-03-14 23:22:10 +00:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2012-11-12 14:00:01 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2012-03-14 23:22:10 +00:00
|
|
|
#include "llvm/Function.h"
|
2012-08-24 16:40:11 +00:00
|
|
|
#include "llvm/GlobalVariable.h"
|
|
|
|
#include "llvm/Module.h"
|
2012-03-14 23:22:10 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Regex.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Support/system_error.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2012-03-14 23:22:10 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2012-08-24 16:44:47 +00:00
|
|
|
BlackList::BlackList(const StringRef Path) {
|
2012-08-24 16:40:11 +00:00
|
|
|
// Validate and open blacklist file.
|
2012-11-29 00:01:38 +00:00
|
|
|
if (Path.empty()) return;
|
2012-03-14 23:22:10 +00:00
|
|
|
OwningPtr<MemoryBuffer> File;
|
2012-08-24 16:40:11 +00:00
|
|
|
if (error_code EC = MemoryBuffer::getFile(Path, File)) {
|
|
|
|
report_fatal_error("Can't open blacklist file: " + Path + ": " +
|
2012-03-14 23:22:10 +00:00
|
|
|
EC.message());
|
|
|
|
}
|
2012-08-24 16:40:11 +00:00
|
|
|
|
|
|
|
// Iterate through each line in the blacklist file.
|
2012-03-14 23:22:10 +00:00
|
|
|
SmallVector<StringRef, 16> Lines;
|
2012-08-24 16:40:11 +00:00
|
|
|
SplitString(File.take()->getBuffer(), Lines, "\n\r");
|
|
|
|
StringMap<std::string> Regexps;
|
|
|
|
for (SmallVector<StringRef, 16>::iterator I = Lines.begin(), E = Lines.end();
|
|
|
|
I != E; ++I) {
|
2012-10-19 15:24:46 +00:00
|
|
|
// Ignore empty lines and lines starting with "#"
|
|
|
|
if (I->empty() || I->startswith("#"))
|
|
|
|
continue;
|
2012-08-24 16:40:11 +00:00
|
|
|
// Get our prefix and unparsed regexp.
|
|
|
|
std::pair<StringRef, StringRef> SplitLine = I->split(":");
|
|
|
|
StringRef Prefix = SplitLine.first;
|
|
|
|
std::string Regexp = SplitLine.second;
|
2012-11-29 00:01:38 +00:00
|
|
|
if (Regexp.empty()) {
|
|
|
|
// Missing ':' in the line.
|
|
|
|
report_fatal_error("malformed blacklist line: " + SplitLine.first);
|
|
|
|
}
|
2012-08-24 16:40:11 +00:00
|
|
|
|
|
|
|
// Replace * with .*
|
|
|
|
for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
|
|
|
|
pos += strlen(".*")) {
|
|
|
|
Regexp.replace(pos, strlen("*"), ".*");
|
2012-03-14 23:22:10 +00:00
|
|
|
}
|
2012-08-24 16:40:11 +00:00
|
|
|
|
|
|
|
// Check that the regexp is valid.
|
|
|
|
Regex CheckRE(Regexp);
|
|
|
|
std::string Error;
|
|
|
|
if (!CheckRE.isValid(Error)) {
|
|
|
|
report_fatal_error("malformed blacklist regex: " + SplitLine.second +
|
|
|
|
": " + Error);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add this regexp into the proper group by its prefix.
|
2012-11-29 00:01:38 +00:00
|
|
|
if (!Regexps[Prefix].empty())
|
2012-08-24 16:40:11 +00:00
|
|
|
Regexps[Prefix] += "|";
|
|
|
|
Regexps[Prefix] += Regexp;
|
2012-03-14 23:22:10 +00:00
|
|
|
}
|
2012-08-24 16:40:11 +00:00
|
|
|
|
|
|
|
// Iterate through each of the prefixes, and create Regexs for them.
|
|
|
|
for (StringMap<std::string>::iterator I = Regexps.begin(), E = Regexps.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
Entries[I->getKey()] = new Regex(I->getValue());
|
2012-03-14 23:22:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-24 16:44:47 +00:00
|
|
|
bool BlackList::isIn(const Function &F) {
|
2012-08-24 16:40:11 +00:00
|
|
|
return isIn(*F.getParent()) || inSection("fun", F.getName());
|
|
|
|
}
|
|
|
|
|
2012-08-24 16:44:47 +00:00
|
|
|
bool BlackList::isIn(const GlobalVariable &G) {
|
2012-08-24 16:40:11 +00:00
|
|
|
return isIn(*G.getParent()) || inSection("global", G.getName());
|
|
|
|
}
|
|
|
|
|
2012-08-24 16:44:47 +00:00
|
|
|
bool BlackList::isIn(const Module &M) {
|
2012-08-24 16:40:11 +00:00
|
|
|
return inSection("src", M.getModuleIdentifier());
|
|
|
|
}
|
|
|
|
|
2012-11-12 14:00:01 +00:00
|
|
|
static StringRef GetGVTypeString(const GlobalVariable &G) {
|
|
|
|
// Types of GlobalVariables are always pointer types.
|
|
|
|
Type *GType = G.getType()->getElementType();
|
|
|
|
// For now we support blacklisting struct types only.
|
2012-11-12 14:47:00 +00:00
|
|
|
if (StructType *SGType = dyn_cast<StructType>(GType)) {
|
|
|
|
if (!SGType->isLiteral())
|
|
|
|
return SGType->getName();
|
2012-11-12 14:00:01 +00:00
|
|
|
}
|
|
|
|
return "<unknown type>";
|
|
|
|
}
|
|
|
|
|
2012-09-05 07:29:56 +00:00
|
|
|
bool BlackList::isInInit(const GlobalVariable &G) {
|
2012-11-12 14:00:01 +00:00
|
|
|
return (isIn(*G.getParent()) ||
|
|
|
|
inSection("global-init", G.getName()) ||
|
|
|
|
inSection("global-init-type", GetGVTypeString(G)));
|
2012-09-05 07:29:56 +00:00
|
|
|
}
|
|
|
|
|
2012-11-29 00:01:38 +00:00
|
|
|
bool BlackList::inSection(const StringRef Section, const StringRef Query) {
|
|
|
|
StringMap<Regex*>::iterator I = Entries.find(Section);
|
|
|
|
if (I == Entries.end()) return false;
|
|
|
|
|
|
|
|
Regex *FunctionRegex = I->getValue();
|
|
|
|
return FunctionRegex->match(Query);
|
2012-03-14 23:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llvm
|