2013-07-09 22:03:17 +00:00
|
|
|
//===-- SpecialCaseList.cpp - special case list 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
|
2013-07-09 22:03:17 +00:00
|
|
|
// variables, or to instrument some functions or global variables in a specific
|
|
|
|
// way, based on a user-supplied list.
|
2012-03-14 23:22:10 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-07-09 22:02:49 +00:00
|
|
|
#include "llvm/Transforms/Utils/SpecialCaseList.h"
|
2013-07-09 22:03:17 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2012-03-14 23:22:10 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2013-08-05 17:48:04 +00:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
|
|
|
#include "llvm/IR/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"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include <string>
|
2014-06-12 17:38:55 +00:00
|
|
|
#include <system_error>
|
2012-12-03 16:50:05 +00:00
|
|
|
#include <utility>
|
2012-03-14 23:22:10 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2013-08-05 17:48:04 +00:00
|
|
|
/// Represents a set of regular expressions. Regular expressions which are
|
|
|
|
/// "literal" (i.e. no regex metacharacters) are stored in Strings, while all
|
|
|
|
/// others are represented as a single pipe-separated regex in RegEx. The
|
|
|
|
/// reason for doing so is efficiency; StringSet is much faster at matching
|
|
|
|
/// literal strings than Regex.
|
|
|
|
struct SpecialCaseList::Entry {
|
|
|
|
StringSet<> Strings;
|
|
|
|
Regex *RegEx;
|
|
|
|
|
2014-04-25 05:29:35 +00:00
|
|
|
Entry() : RegEx(nullptr) {}
|
2013-08-05 17:48:04 +00:00
|
|
|
|
|
|
|
bool match(StringRef Query) const {
|
|
|
|
return Strings.count(Query) || (RegEx && RegEx->match(Query));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-12 07:49:36 +00:00
|
|
|
SpecialCaseList::SpecialCaseList() : Entries() {}
|
|
|
|
|
|
|
|
SpecialCaseList *SpecialCaseList::create(
|
|
|
|
const StringRef Path, std::string &Error) {
|
|
|
|
if (Path.empty())
|
|
|
|
return new SpecialCaseList();
|
2014-07-06 17:43:13 +00:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
|
|
|
|
MemoryBuffer::getFile(Path);
|
|
|
|
if (std::error_code EC = FileOrErr.getError()) {
|
2013-08-12 07:49:36 +00:00
|
|
|
Error = (Twine("Can't open file '") + Path + "': " + EC.message()).str();
|
2014-04-25 05:29:35 +00:00
|
|
|
return nullptr;
|
2013-08-12 07:49:36 +00:00
|
|
|
}
|
2014-07-06 17:43:13 +00:00
|
|
|
return create(FileOrErr.get().get(), Error);
|
2013-08-12 07:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SpecialCaseList *SpecialCaseList::create(
|
|
|
|
const MemoryBuffer *MB, std::string &Error) {
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
|
2013-08-12 07:49:36 +00:00
|
|
|
if (!SCL->parse(MB, Error))
|
2014-04-25 05:29:35 +00:00
|
|
|
return nullptr;
|
2014-03-05 10:19:29 +00:00
|
|
|
return SCL.release();
|
2013-08-12 07:49:36 +00:00
|
|
|
}
|
|
|
|
|
2013-08-12 11:46:09 +00:00
|
|
|
SpecialCaseList *SpecialCaseList::createOrDie(const StringRef Path) {
|
|
|
|
std::string Error;
|
|
|
|
if (SpecialCaseList *SCL = create(Path, Error))
|
|
|
|
return SCL;
|
|
|
|
report_fatal_error(Error);
|
|
|
|
}
|
|
|
|
|
2013-08-12 07:49:36 +00:00
|
|
|
bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
|
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;
|
2013-07-09 22:03:09 +00:00
|
|
|
SplitString(MB->getBuffer(), Lines, "\n\r");
|
2013-07-09 22:03:17 +00:00
|
|
|
StringMap<StringMap<std::string> > Regexps;
|
2013-08-12 07:49:36 +00:00
|
|
|
assert(Entries.empty() &&
|
|
|
|
"parse() should be called on an empty SpecialCaseList");
|
|
|
|
int LineNo = 1;
|
2013-07-04 01:31:24 +00:00
|
|
|
for (SmallVectorImpl<StringRef>::iterator I = Lines.begin(), E = Lines.end();
|
2013-08-12 07:49:36 +00:00
|
|
|
I != E; ++I, ++LineNo) {
|
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;
|
2013-07-09 22:03:17 +00:00
|
|
|
if (SplitLine.second.empty()) {
|
2012-11-29 00:01:38 +00:00
|
|
|
// Missing ':' in the line.
|
2013-08-12 07:49:36 +00:00
|
|
|
Error = (Twine("Malformed line ") + Twine(LineNo) + ": '" +
|
|
|
|
SplitLine.first + "'").str();
|
|
|
|
return false;
|
2012-11-29 00:01:38 +00:00
|
|
|
}
|
2012-08-24 16:40:11 +00:00
|
|
|
|
2013-07-09 22:03:17 +00:00
|
|
|
std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
|
|
|
|
std::string Regexp = SplitRegexp.first;
|
|
|
|
StringRef Category = SplitRegexp.second;
|
|
|
|
|
|
|
|
// Backwards compatibility.
|
|
|
|
if (Prefix == "global-init") {
|
|
|
|
Prefix = "global";
|
|
|
|
Category = "init";
|
|
|
|
} else if (Prefix == "global-init-type") {
|
|
|
|
Prefix = "type";
|
|
|
|
Category = "init";
|
|
|
|
} else if (Prefix == "global-init-src") {
|
|
|
|
Prefix = "src";
|
|
|
|
Category = "init";
|
|
|
|
}
|
|
|
|
|
2013-08-05 17:48:04 +00:00
|
|
|
// See if we can store Regexp in Strings.
|
|
|
|
if (Regex::isLiteralERE(Regexp)) {
|
|
|
|
Entries[Prefix][Category].Strings.insert(Regexp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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);
|
2013-08-12 07:49:36 +00:00
|
|
|
std::string REError;
|
|
|
|
if (!CheckRE.isValid(REError)) {
|
|
|
|
Error = (Twine("Malformed regex in line ") + Twine(LineNo) + ": '" +
|
|
|
|
SplitLine.second + "': " + REError).str();
|
|
|
|
return false;
|
2012-08-24 16:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add this regexp into the proper group by its prefix.
|
2013-07-09 22:03:17 +00:00
|
|
|
if (!Regexps[Prefix][Category].empty())
|
|
|
|
Regexps[Prefix][Category] += "|";
|
2013-07-16 17:56:07 +00:00
|
|
|
Regexps[Prefix][Category] += "^" + 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.
|
2013-07-09 22:03:17 +00:00
|
|
|
for (StringMap<StringMap<std::string> >::const_iterator I = Regexps.begin(),
|
|
|
|
E = Regexps.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
for (StringMap<std::string>::const_iterator II = I->second.begin(),
|
|
|
|
IE = I->second.end();
|
|
|
|
II != IE; ++II) {
|
2013-08-05 17:48:04 +00:00
|
|
|
Entries[I->getKey()][II->getKey()].RegEx = new Regex(II->getValue());
|
2013-07-09 22:03:17 +00:00
|
|
|
}
|
2012-03-14 23:22:10 +00:00
|
|
|
}
|
2013-08-12 07:49:36 +00:00
|
|
|
return true;
|
2012-03-14 23:22:10 +00:00
|
|
|
}
|
|
|
|
|
2013-07-09 22:03:17 +00:00
|
|
|
SpecialCaseList::~SpecialCaseList() {
|
2013-08-05 17:48:04 +00:00
|
|
|
for (StringMap<StringMap<Entry> >::iterator I = Entries.begin(),
|
|
|
|
E = Entries.end();
|
2013-07-09 22:03:17 +00:00
|
|
|
I != E; ++I) {
|
2013-08-05 17:48:04 +00:00
|
|
|
for (StringMap<Entry>::const_iterator II = I->second.begin(),
|
|
|
|
IE = I->second.end();
|
|
|
|
II != IE; ++II) {
|
|
|
|
delete II->second.RegEx;
|
|
|
|
}
|
2013-07-09 22:03:17 +00:00
|
|
|
}
|
2012-08-24 16:40:11 +00:00
|
|
|
}
|
|
|
|
|
2013-07-09 22:03:17 +00:00
|
|
|
bool SpecialCaseList::isIn(const Function& F, const StringRef Category) const {
|
|
|
|
return isIn(*F.getParent(), Category) ||
|
|
|
|
inSectionCategory("fun", F.getName(), Category);
|
2012-08-24 16:40:11 +00:00
|
|
|
}
|
|
|
|
|
2013-08-19 19:00:35 +00:00
|
|
|
static StringRef GetGlobalTypeString(const GlobalValue &G) {
|
2012-11-12 14:00:01 +00:00
|
|
|
// 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>";
|
|
|
|
}
|
|
|
|
|
2013-07-09 22:03:17 +00:00
|
|
|
bool SpecialCaseList::isIn(const GlobalVariable &G,
|
|
|
|
const StringRef Category) const {
|
|
|
|
return isIn(*G.getParent(), Category) ||
|
|
|
|
inSectionCategory("global", G.getName(), Category) ||
|
2013-08-19 19:00:35 +00:00
|
|
|
inSectionCategory("type", GetGlobalTypeString(G), Category);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpecialCaseList::isIn(const GlobalAlias &GA,
|
|
|
|
const StringRef Category) const {
|
|
|
|
if (isIn(*GA.getParent(), Category))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (isa<FunctionType>(GA.getType()->getElementType()))
|
|
|
|
return inSectionCategory("fun", GA.getName(), Category);
|
|
|
|
|
|
|
|
return inSectionCategory("global", GA.getName(), Category) ||
|
|
|
|
inSectionCategory("type", GetGlobalTypeString(GA), Category);
|
2013-07-09 22:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SpecialCaseList::isIn(const Module &M, const StringRef Category) const {
|
|
|
|
return inSectionCategory("src", M.getModuleIdentifier(), Category);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpecialCaseList::inSectionCategory(const StringRef Section,
|
|
|
|
const StringRef Query,
|
|
|
|
const StringRef Category) const {
|
2013-08-05 17:48:04 +00:00
|
|
|
StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
|
2012-11-29 00:01:38 +00:00
|
|
|
if (I == Entries.end()) return false;
|
2013-08-05 17:48:04 +00:00
|
|
|
StringMap<Entry>::const_iterator II = I->second.find(Category);
|
2013-07-09 22:03:17 +00:00
|
|
|
if (II == I->second.end()) return false;
|
2012-11-29 00:01:38 +00:00
|
|
|
|
2013-08-05 17:48:04 +00:00
|
|
|
return II->getValue().match(Query);
|
2012-03-14 23:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llvm
|