2013-07-10 06:02:49 +08:00
|
|
|
//===-- SpecialCaseList.cpp - blacklist for sanitizers --------------------===//
|
2012-03-15 07:22:10 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-08-25 00:40:11 +08: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-15 07:22:10 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-07-10 06:02:49 +08:00
|
|
|
#include "llvm/Transforms/Utils/SpecialCaseList.h"
|
2012-03-15 07:22:10 +08:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2012-03-15 07:22:10 +08: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-04 00:50:05 +08:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2012-03-15 07:22:10 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2013-07-10 06:02:49 +08:00
|
|
|
SpecialCaseList::SpecialCaseList(const StringRef Path) {
|
2012-08-25 00:40:11 +08:00
|
|
|
// Validate and open blacklist file.
|
2012-11-29 08:01:38 +08:00
|
|
|
if (Path.empty()) return;
|
2012-03-15 07:22:10 +08:00
|
|
|
OwningPtr<MemoryBuffer> File;
|
2012-08-25 00:40:11 +08:00
|
|
|
if (error_code EC = MemoryBuffer::getFile(Path, File)) {
|
|
|
|
report_fatal_error("Can't open blacklist file: " + Path + ": " +
|
2012-03-15 07:22:10 +08:00
|
|
|
EC.message());
|
|
|
|
}
|
2012-08-25 00:40:11 +08:00
|
|
|
|
2013-07-10 06:03:09 +08:00
|
|
|
init(File.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
SpecialCaseList::SpecialCaseList(const MemoryBuffer *MB) {
|
|
|
|
init(MB);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpecialCaseList::init(const MemoryBuffer *MB) {
|
2012-08-25 00:40:11 +08:00
|
|
|
// Iterate through each line in the blacklist file.
|
2012-03-15 07:22:10 +08:00
|
|
|
SmallVector<StringRef, 16> Lines;
|
2013-07-10 06:03:09 +08:00
|
|
|
SplitString(MB->getBuffer(), Lines, "\n\r");
|
2012-08-25 00:40:11 +08:00
|
|
|
StringMap<std::string> Regexps;
|
2013-07-04 09:31:24 +08:00
|
|
|
for (SmallVectorImpl<StringRef>::iterator I = Lines.begin(), E = Lines.end();
|
2012-08-25 00:40:11 +08:00
|
|
|
I != E; ++I) {
|
2012-10-19 23:24:46 +08:00
|
|
|
// Ignore empty lines and lines starting with "#"
|
|
|
|
if (I->empty() || I->startswith("#"))
|
|
|
|
continue;
|
2012-08-25 00:40:11 +08: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 08:01:38 +08:00
|
|
|
if (Regexp.empty()) {
|
|
|
|
// Missing ':' in the line.
|
|
|
|
report_fatal_error("malformed blacklist line: " + SplitLine.first);
|
|
|
|
}
|
2012-08-25 00:40:11 +08:00
|
|
|
|
|
|
|
// Replace * with .*
|
|
|
|
for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
|
|
|
|
pos += strlen(".*")) {
|
|
|
|
Regexp.replace(pos, strlen("*"), ".*");
|
2012-03-15 07:22:10 +08:00
|
|
|
}
|
2012-08-25 00:40:11 +08: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 08:01:38 +08:00
|
|
|
if (!Regexps[Prefix].empty())
|
2012-08-25 00:40:11 +08:00
|
|
|
Regexps[Prefix] += "|";
|
|
|
|
Regexps[Prefix] += Regexp;
|
2012-03-15 07:22:10 +08:00
|
|
|
}
|
2012-08-25 00:40:11 +08:00
|
|
|
|
|
|
|
// Iterate through each of the prefixes, and create Regexs for them.
|
2013-01-18 19:29:21 +08:00
|
|
|
for (StringMap<std::string>::const_iterator I = Regexps.begin(),
|
|
|
|
E = Regexps.end(); I != E; ++I) {
|
2012-08-25 00:40:11 +08:00
|
|
|
Entries[I->getKey()] = new Regex(I->getValue());
|
2012-03-15 07:22:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 06:02:49 +08:00
|
|
|
bool SpecialCaseList::isIn(const Function &F) const {
|
2012-08-25 00:40:11 +08:00
|
|
|
return isIn(*F.getParent()) || inSection("fun", F.getName());
|
|
|
|
}
|
|
|
|
|
2013-07-10 06:02:49 +08:00
|
|
|
bool SpecialCaseList::isIn(const GlobalVariable &G) const {
|
2012-08-25 00:40:11 +08:00
|
|
|
return isIn(*G.getParent()) || inSection("global", G.getName());
|
|
|
|
}
|
|
|
|
|
2013-07-10 06:02:49 +08:00
|
|
|
bool SpecialCaseList::isIn(const Module &M) const {
|
2012-08-25 00:40:11 +08:00
|
|
|
return inSection("src", M.getModuleIdentifier());
|
|
|
|
}
|
|
|
|
|
2012-11-12 22:00:01 +08: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 22:47:00 +08:00
|
|
|
if (StructType *SGType = dyn_cast<StructType>(GType)) {
|
|
|
|
if (!SGType->isLiteral())
|
|
|
|
return SGType->getName();
|
2012-11-12 22:00:01 +08:00
|
|
|
}
|
|
|
|
return "<unknown type>";
|
|
|
|
}
|
|
|
|
|
2013-07-10 06:02:49 +08:00
|
|
|
bool SpecialCaseList::isInInit(const GlobalVariable &G) const {
|
2012-11-12 22:00:01 +08:00
|
|
|
return (isIn(*G.getParent()) ||
|
|
|
|
inSection("global-init", G.getName()) ||
|
2013-04-11 21:20:00 +08:00
|
|
|
inSection("global-init-type", GetGVTypeString(G)) ||
|
|
|
|
inSection("global-init-src", G.getParent()->getModuleIdentifier()));
|
2012-09-05 15:29:56 +08:00
|
|
|
}
|
|
|
|
|
2013-07-10 06:02:49 +08:00
|
|
|
bool SpecialCaseList::inSection(const StringRef Section,
|
|
|
|
const StringRef Query) const {
|
2013-01-18 19:29:21 +08:00
|
|
|
StringMap<Regex*>::const_iterator I = Entries.find(Section);
|
2012-11-29 08:01:38 +08:00
|
|
|
if (I == Entries.end()) return false;
|
|
|
|
|
|
|
|
Regex *FunctionRegex = I->getValue();
|
|
|
|
return FunctionRegex->match(Query);
|
2012-03-15 07:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llvm
|