Index: bindings/go/llvm/InstrumentationBindings.h =================================================================== --- bindings/go/llvm/InstrumentationBindings.h +++ bindings/go/llvm/InstrumentationBindings.h @@ -28,8 +28,8 @@ void LLVMAddAddressSanitizerModulePass(LLVMPassManagerRef PM); void LLVMAddThreadSanitizerPass(LLVMPassManagerRef PM); void LLVMAddMemorySanitizerPass(LLVMPassManagerRef PM); -void LLVMAddDataFlowSanitizerPass(LLVMPassManagerRef PM, - const char *ABIListFile); +void LLVMAddDataFlowSanitizerPass(LLVMPassManagerRef PM, int ABIListFilesNum, + const char **ABIListFiles); #ifdef __cplusplus } Index: bindings/go/llvm/InstrumentationBindings.cpp =================================================================== --- bindings/go/llvm/InstrumentationBindings.cpp +++ bindings/go/llvm/InstrumentationBindings.cpp @@ -37,6 +37,11 @@ } void LLVMAddDataFlowSanitizerPass(LLVMPassManagerRef PM, - const char *ABIListFile) { - unwrap(PM)->add(createDataFlowSanitizerPass(ABIListFile)); + int ABIListFilesNum, + const char **ABIListFiles) { + std::vector ABIListFilesVec; + for (int i = 0, n = ABIListFilesNum; i < n; ++i) { + ABIListFilesVec.push_back(ABIListFiles[i]); + } + unwrap(PM)->add(createDataFlowSanitizerPass(ABIListFilesVec)); } Index: bindings/go/llvm/transforms_instrumentation.go =================================================================== --- bindings/go/llvm/transforms_instrumentation.go +++ bindings/go/llvm/transforms_instrumentation.go @@ -36,8 +36,12 @@ C.LLVMAddMemorySanitizerPass(pm.C) } -func (pm PassManager) AddDataFlowSanitizerPass(abilist string) { - cabilist := C.CString(abilist) - defer C.free(unsafe.Pointer(cabilist)) - C.LLVMAddDataFlowSanitizerPass(pm.C, cabilist) +func (pm PassManager) AddDataFlowSanitizerPass(abilist []string) { + abiliststrs := make([]*C.char, len(abilist)) + for i, arg := range abilist { + abiliststrs[i] = C.CString(arg) + defer C.free(unsafe.Pointer(abiliststrs[i])) + } + C.LLVMAddDataFlowSanitizerPass(pm.C, C.int(len(abilist)), + &abiliststrs[0]) } Index: include/llvm/Support/SpecialCaseList.h =================================================================== --- include/llvm/Support/SpecialCaseList.h +++ include/llvm/Support/SpecialCaseList.h @@ -49,6 +49,8 @@ #define LLVM_SUPPORT_SPECIALCASELIST_H #include "llvm/ADT/StringMap.h" +#include +#include namespace llvm { class MemoryBuffer; @@ -57,18 +59,18 @@ class SpecialCaseList { public: - /// Parses the special case list from a file. If Path is empty, returns - /// an empty special case list. On failure, returns 0 and writes an error - /// message to string. - static std::unique_ptr create(StringRef Path, - std::string &Error); + /// Parses the special case list entries from files. On failure, returns + /// 0 and writes an error message to string. + static std::unique_ptr + create(const std::vector &Paths, std::string &Error); /// Parses the special case list from a memory buffer. On failure, returns /// 0 and writes an error message to string. static std::unique_ptr create(const MemoryBuffer *MB, - std::string &Error); - /// Parses the special case list from a file. On failure, reports a fatal - /// error. - static std::unique_ptr createOrDie(StringRef Path); + std::string &Error); + /// Parses the special case list entries from files. On failure, reports a + /// fatal error. + static std::unique_ptr + createOrDie(const std::vector &Paths); ~SpecialCaseList(); @@ -85,11 +87,15 @@ SpecialCaseList &operator=(SpecialCaseList const &) LLVM_DELETED_FUNCTION; struct Entry; - StringMap > Entries; + StringMap> Entries; + StringMap> Regexps; + bool IsCompiled; SpecialCaseList(); /// Parses just-constructed SpecialCaseList entries from a memory buffer. bool parse(const MemoryBuffer *MB, std::string &Error); + /// compile() should be called once, after parsing all the memory buffers. + void compile(); }; } // namespace llvm Index: include/llvm/Transforms/Instrumentation.h =================================================================== --- include/llvm/Transforms/Instrumentation.h +++ include/llvm/Transforms/Instrumentation.h @@ -86,17 +86,17 @@ FunctionPass *createThreadSanitizerPass(); // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation -ModulePass *createDataFlowSanitizerPass(StringRef ABIListFile = StringRef(), - void *(*getArgTLS)() = nullptr, - void *(*getRetValTLS)() = nullptr); +ModulePass *createDataFlowSanitizerPass( + const std::vector &ABIListFiles = std::vector(), + void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr); // Insert SanitizerCoverage instrumentation. ModulePass *createSanitizerCoverageModulePass(int CoverageLevel); #if defined(__GNUC__) && defined(__linux__) && !defined(ANDROID) -inline ModulePass *createDataFlowSanitizerPassForJIT(StringRef ABIListFile = - StringRef()) { - return createDataFlowSanitizerPass(ABIListFile, getDFSanArgTLSPtrForJIT, +inline ModulePass *createDataFlowSanitizerPassForJIT( + const std::vector &ABIListFiles = std::vector()) { + return createDataFlowSanitizerPass(ABIListFiles, getDFSanArgTLSPtrForJIT, getDFSanRetValTLSPtrForJIT); } #endif Index: lib/Support/SpecialCaseList.cpp =================================================================== --- lib/Support/SpecialCaseList.cpp +++ lib/Support/SpecialCaseList.cpp @@ -46,19 +46,26 @@ } }; -SpecialCaseList::SpecialCaseList() : Entries() {} +SpecialCaseList::SpecialCaseList() : Entries(), Regexps(), IsCompiled(false) {} -std::unique_ptr SpecialCaseList::create(StringRef Path, - std::string &Error) { - if (Path.empty()) - return std::unique_ptr(new SpecialCaseList()); - ErrorOr> FileOrErr = - MemoryBuffer::getFile(Path); - if (std::error_code EC = FileOrErr.getError()) { - Error = (Twine("Can't open file '") + Path + "': " + EC.message()).str(); - return nullptr; +std::unique_ptr +SpecialCaseList::create(const std::vector &Paths, + std::string &Error) { + std::unique_ptr SCL(new SpecialCaseList()); + for (auto Path : Paths) { + if (Path.empty()) + continue; + ErrorOr> FileOrErr = + MemoryBuffer::getFile(Path); + if (std::error_code EC = FileOrErr.getError()) { + Error = (Twine("Can't open file '") + Path + "': " + EC.message()).str(); + return nullptr; + } + if (!SCL->parse(FileOrErr.get().get(), Error)) + return nullptr; } - return create(FileOrErr.get().get(), Error); + SCL->compile(); + return SCL; } std::unique_ptr SpecialCaseList::create(const MemoryBuffer *MB, @@ -66,12 +73,14 @@ std::unique_ptr SCL(new SpecialCaseList()); if (!SCL->parse(MB, Error)) return nullptr; + SCL->compile(); return SCL; } -std::unique_ptr SpecialCaseList::createOrDie(StringRef Path) { +std::unique_ptr +SpecialCaseList::createOrDie(const std::vector &Paths) { std::string Error; - if (auto SCL = create(Path, Error)) + if (auto SCL = create(Paths, Error)) return SCL; report_fatal_error(Error); } @@ -80,12 +89,8 @@ // Iterate through each line in the blacklist file. SmallVector Lines; SplitString(MB->getBuffer(), Lines, "\n\r"); - StringMap > Regexps; - assert(Entries.empty() && - "parse() should be called on an empty SpecialCaseList"); int LineNo = 1; - for (SmallVectorImpl::iterator I = Lines.begin(), E = Lines.end(); - I != E; ++I, ++LineNo) { + for (auto I = Lines.begin(), E = Lines.end(); I != E; ++I, ++LineNo) { // Ignore empty lines and lines starting with "#" if (I->empty() || I->startswith("#")) continue; @@ -129,10 +134,14 @@ Regexps[Prefix][Category] += "|"; Regexps[Prefix][Category] += "^" + Regexp + "$"; } + return true; +} +void SpecialCaseList::compile() { + assert(!IsCompiled && "compile() should only be called once"); // Iterate through each of the prefixes, and create Regexs for them. - for (StringMap >::const_iterator I = Regexps.begin(), - E = Regexps.end(); + for (StringMap>::const_iterator I = Regexps.begin(), + E = Regexps.end(); I != E; ++I) { for (StringMap::const_iterator II = I->second.begin(), IE = I->second.end(); @@ -140,13 +149,15 @@ Entries[I->getKey()][II->getKey()].RegEx.reset(new Regex(II->getValue())); } } - return true; + Regexps.clear(); + IsCompiled = true; } SpecialCaseList::~SpecialCaseList() {} bool SpecialCaseList::inSection(StringRef Section, StringRef Query, StringRef Category) const { + assert(IsCompiled && "SpecialCaseList::compile() was not called!"); StringMap >::const_iterator I = Entries.find(Section); if (I == Entries.end()) return false; StringMap::const_iterator II = I->second.find(Category); Index: lib/Transforms/Instrumentation/DataFlowSanitizer.cpp =================================================================== --- lib/Transforms/Instrumentation/DataFlowSanitizer.cpp +++ lib/Transforms/Instrumentation/DataFlowSanitizer.cpp @@ -83,14 +83,14 @@ cl::desc("respect alignment requirements provided by input IR"), cl::Hidden, cl::init(false)); -// The ABI list file controls how shadow parameters are passed. The pass treats +// The ABI list files controls how shadow parameters are passed. The pass treats // every function labelled "uninstrumented" in the ABI list file as conforming // to the "native" (i.e. unsanitized) ABI. Unless the ABI list contains // additional annotations for those functions, a call to one of those functions // will produce a warning message, as the labelling behaviour of the function is // unknown. The other supported annotations are "functional" and "discard", // which are described below under DataFlowSanitizer::WrapperKind. -static cl::opt ClABIListFile( +static cl::list ClABIListFiles( "dfsan-abilist", cl::desc("File listing native ABI functions and how the pass treats them"), cl::Hidden); @@ -141,7 +141,9 @@ std::unique_ptr SCL; public: - DFSanABIList(std::unique_ptr SCL) : SCL(std::move(SCL)) {} + DFSanABIList() {} + + void set(std::unique_ptr List) { SCL = std::move(List); } /// Returns whether either this function or its source file are listed in the /// given category. @@ -264,9 +266,9 @@ Constant *getOrBuildTrampolineFunction(FunctionType *FT, StringRef FName); public: - DataFlowSanitizer(StringRef ABIListFile = StringRef(), - void *(*getArgTLS)() = nullptr, - void *(*getRetValTLS)() = nullptr); + DataFlowSanitizer( + const std::vector &ABIListFiles = std::vector(), + void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr); static char ID; bool doInitialization(Module &M) override; bool runOnModule(Module &M) override; @@ -351,18 +353,21 @@ INITIALIZE_PASS(DataFlowSanitizer, "dfsan", "DataFlowSanitizer: dynamic data flow analysis.", false, false) -ModulePass *llvm::createDataFlowSanitizerPass(StringRef ABIListFile, - void *(*getArgTLS)(), - void *(*getRetValTLS)()) { - return new DataFlowSanitizer(ABIListFile, getArgTLS, getRetValTLS); +ModulePass * +llvm::createDataFlowSanitizerPass(const std::vector &ABIListFiles, + void *(*getArgTLS)(), + void *(*getRetValTLS)()) { + return new DataFlowSanitizer(ABIListFiles, getArgTLS, getRetValTLS); } -DataFlowSanitizer::DataFlowSanitizer(StringRef ABIListFile, - void *(*getArgTLS)(), - void *(*getRetValTLS)()) - : ModulePass(ID), GetArgTLSPtr(getArgTLS), GetRetvalTLSPtr(getRetValTLS), - ABIList(SpecialCaseList::createOrDie(ABIListFile.empty() ? ClABIListFile - : ABIListFile)) { +DataFlowSanitizer::DataFlowSanitizer( + const std::vector &ABIListFiles, void *(*getArgTLS)(), + void *(*getRetValTLS)()) + : ModulePass(ID), GetArgTLSPtr(getArgTLS), GetRetvalTLSPtr(getRetValTLS) { + std::vector AllABIListFiles(std::move(ABIListFiles)); + AllABIListFiles.insert(AllABIListFiles.end(), ClABIListFiles.begin(), + ClABIListFiles.end()); + ABIList.set(SpecialCaseList::createOrDie(AllABIListFiles)); } FunctionType *DataFlowSanitizer::getArgsFunctionType(FunctionType *T) { Index: unittests/Support/SpecialCaseListTest.cpp =================================================================== --- unittests/Support/SpecialCaseListTest.cpp +++ unittests/Support/SpecialCaseListTest.cpp @@ -95,7 +95,8 @@ Error)); EXPECT_EQ("Malformed regex in line 2: 'fun(a': parentheses not balanced", Error); - EXPECT_EQ(nullptr, SpecialCaseList::create("unexisting", Error)); + std::vector Files(1, "unexisting"); + EXPECT_EQ(nullptr, SpecialCaseList::create(Files, Error)); EXPECT_EQ(0U, Error.find("Can't open file 'unexisting':")); }