Index: clang/include/clang/Basic/SanitizerSpecialCaseList.h =================================================================== --- clang/include/clang/Basic/SanitizerSpecialCaseList.h +++ clang/include/clang/Basic/SanitizerSpecialCaseList.h @@ -15,6 +15,7 @@ #include "clang/Basic/LLVM.h" #include "clang/Basic/Sanitizers.h" +#include "clang/Basic/SourceManager.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/SpecialCaseList.h" #include @@ -24,10 +25,12 @@ class SanitizerSpecialCaseList : public llvm::SpecialCaseList { public: static std::unique_ptr - create(const std::vector &Paths, std::string &Error); + create(const std::vector &Paths, llvm::vfs::FileSystem &VFS, + std::string &Error); static std::unique_ptr - createOrDie(const std::vector &Paths); + createOrDie(const std::vector &Paths, + llvm::vfs::FileSystem &VFS); // Query blacklisted entries if any bit in Mask matches the entry's section. bool inSection(SanitizerMask Mask, StringRef Prefix, StringRef Query, Index: clang/lib/AST/ASTContext.cpp =================================================================== --- clang/lib/AST/ASTContext.cpp +++ clang/lib/AST/ASTContext.cpp @@ -72,6 +72,7 @@ #include "llvm/ADT/PointerUnion.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallPtrSet.h" +#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" @@ -81,6 +82,7 @@ #include "llvm/Support/Compiler.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MathExtras.h" +#include "llvm/Support/VirtualFileSystem.h" #include "llvm/Support/raw_ostream.h" #include #include Index: clang/lib/Basic/SanitizerBlacklist.cpp =================================================================== --- clang/lib/Basic/SanitizerBlacklist.cpp +++ clang/lib/Basic/SanitizerBlacklist.cpp @@ -16,7 +16,9 @@ SanitizerBlacklist::SanitizerBlacklist( const std::vector &BlacklistPaths, SourceManager &SM) - : SSCL(SanitizerSpecialCaseList::createOrDie(BlacklistPaths)), SM(SM) {} + : SSCL(SanitizerSpecialCaseList::createOrDie( + BlacklistPaths, SM.getFileManager().getVirtualFileSystem())), + SM(SM) {} bool SanitizerBlacklist::isBlacklistedGlobal(SanitizerMask Mask, StringRef GlobalName, Index: clang/lib/Basic/SanitizerSpecialCaseList.cpp =================================================================== --- clang/lib/Basic/SanitizerSpecialCaseList.cpp +++ clang/lib/Basic/SanitizerSpecialCaseList.cpp @@ -16,20 +16,32 @@ std::unique_ptr SanitizerSpecialCaseList::create(const std::vector &Paths, + llvm::vfs::FileSystem &VFS, std::string &Error) { std::unique_ptr SSCL( new SanitizerSpecialCaseList()); - if (SSCL->createInternal(Paths, Error)) { - SSCL->createSanitizerSections(); - return SSCL; + + for (const auto &Path : Paths) { + llvm::ErrorOr> MaybeBuffer = + VFS.getBufferForFile(Path); + if (std::error_code EC = MaybeBuffer.getError()) { + Error = (Twine("can't open file '") + Path + "': " + EC.message()).str(); + return nullptr; + } + + if (!SSCL->createInternal(MaybeBuffer.get().get(), Error)) + return nullptr; } - return nullptr; + + SSCL->createSanitizerSections(); + return SSCL; } std::unique_ptr -SanitizerSpecialCaseList::createOrDie(const std::vector &Paths) { +SanitizerSpecialCaseList::createOrDie(const std::vector &Paths, + llvm::vfs::FileSystem &VFS) { std::string Error; - if (auto SSCL = create(Paths, Error)) + if (auto SSCL = create(Paths, VFS, Error)) return SSCL; llvm::report_fatal_error(Error); } Index: clang/test/CodeGen/Inputs/sanitizer-blacklist-vfsoverlay.yaml =================================================================== --- /dev/null +++ clang/test/CodeGen/Inputs/sanitizer-blacklist-vfsoverlay.yaml @@ -0,0 +1,15 @@ +{ + 'version': 0, + 'roots': [ + { 'name': '@DIR@', 'type': 'directory', + 'contents': [ + { 'name': 'only-virtual-file.blacklist', 'type': 'file', + 'external-contents': '@REAL_FILE@' + }, + { 'name': 'invalid-virtual-file.blacklist', 'type': 'file', + 'external-contents': '@NONEXISTENT_FILE@' + } + ] + } + ] +} Index: clang/test/CodeGen/ubsan-blacklist-vfs.c =================================================================== --- /dev/null +++ clang/test/CodeGen/ubsan-blacklist-vfs.c @@ -0,0 +1,36 @@ +// Verify ubsan doesn't emit checks for blacklisted functions and files +// RUN: echo "fun:hash" > %t-func.blacklist +// RUN: echo "src:%s" | sed -e 's/\\/\\\\/g' > %t-file.blacklist + +// RUN: rm -f %t-vfsoverlay.yaml +// RUN: rm -f %t-nonexistent.blacklist +// RUN: sed -e "s|@DIR@|%/T|g" %S/Inputs/sanitizer-blacklist-vfsoverlay.yaml | sed -e "s|@REAL_FILE@|%/t-func.blacklist|g" | sed -e "s|@NONEXISTENT_FILE@|%/t-nonexistent.blacklist|g" > %t-vfsoverlay.yaml +// RUN: %clang_cc1 -fsanitize=unsigned-integer-overflow -ivfsoverlay %t-vfsoverlay.yaml -fsanitize-blacklist=%/T/only-virtual-file.blacklist -emit-llvm %s -o - | FileCheck %s --check-prefix=FUNC + +// RUN: not %clang_cc1 -fsanitize=unsigned-integer-overflow -ivfsoverlay %t-vfsoverlay.yaml -fsanitize-blacklist=%/T/invalid-virtual-file.blacklist -emit-llvm %s -o - 2>&1 | FileCheck %s --check-prefix=INVALID-MAPPED-FILE +// INVALID-MAPPED-FILE: invalid-virtual-file.blacklist': {{[Nn]}}o such file or directory + +// RUN: not %clang_cc1 -fsanitize=unsigned-integer-overflow -ivfsoverlay %t-vfsoverlay.yaml -fsanitize-blacklist=%t-nonexistent.blacklist -emit-llvm %s -o - 2>&1 | FileCheck %s --check-prefix=INVALID +// INVALID: nonexistent.blacklist': {{[Nn]}}o such file or directory + +unsigned i; + +// DEFAULT: @hash +// FUNC: @hash +// FILE: @hash +unsigned hash() { +// DEFAULT: call {{.*}}void @__ubsan +// FUNC-NOT: call {{.*}}void @__ubsan +// FILE-NOT: call {{.*}}void @__ubsan + return i * 37; +} + +// DEFAULT: @add +// FUNC: @add +// FILE: @add +unsigned add() { +// DEFAULT: call {{.*}}void @__ubsan +// FUNC: call {{.*}}void @__ubsan +// FILE-NOT: call {{.*}}void @__ubsan + return i + 1; +} Index: llvm/lib/Support/SpecialCaseList.cpp =================================================================== --- llvm/lib/Support/SpecialCaseList.cpp +++ llvm/lib/Support/SpecialCaseList.cpp @@ -116,8 +116,10 @@ bool SpecialCaseList::createInternal(const MemoryBuffer *MB, std::string &Error) { StringMap Sections; - if (!parse(MB, Sections, Error)) + if (!parse(MB, Sections, Error)) { + Error = (Twine("error parsing buffer: " + Error)).str(); return false; + } return true; }