Skip to content

Commit 590f279

Browse files
committed
[clang] Add VFS support for sanitizers' blacklists
Differential Revision: https://reviews.llvm.org/D69648
1 parent 3ffbf97 commit 590f279

File tree

7 files changed

+69
-10
lines changed

7 files changed

+69
-10
lines changed

clang/include/clang/Basic/SanitizerSpecialCaseList.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,20 @@
1717
#include "clang/Basic/Sanitizers.h"
1818
#include "llvm/ADT/StringRef.h"
1919
#include "llvm/Support/SpecialCaseList.h"
20+
#include "llvm/Support/VirtualFileSystem.h"
2021
#include <memory>
2122

2223
namespace clang {
2324

2425
class SanitizerSpecialCaseList : public llvm::SpecialCaseList {
2526
public:
2627
static std::unique_ptr<SanitizerSpecialCaseList>
27-
create(const std::vector<std::string> &Paths, std::string &Error);
28+
create(const std::vector<std::string> &Paths, llvm::vfs::FileSystem &VFS,
29+
std::string &Error);
2830

2931
static std::unique_ptr<SanitizerSpecialCaseList>
30-
createOrDie(const std::vector<std::string> &Paths);
32+
createOrDie(const std::vector<std::string> &Paths,
33+
llvm::vfs::FileSystem &VFS);
3134

3235
// Query blacklisted entries if any bit in Mask matches the entry's section.
3336
bool inSection(SanitizerMask Mask, StringRef Prefix, StringRef Query,

clang/lib/Basic/SanitizerBlacklist.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ using namespace clang;
1616

1717
SanitizerBlacklist::SanitizerBlacklist(
1818
const std::vector<std::string> &BlacklistPaths, SourceManager &SM)
19-
: SSCL(SanitizerSpecialCaseList::createOrDie(BlacklistPaths)), SM(SM) {}
19+
: SSCL(SanitizerSpecialCaseList::createOrDie(
20+
BlacklistPaths, SM.getFileManager().getVirtualFileSystem())),
21+
SM(SM) {}
2022

2123
bool SanitizerBlacklist::isBlacklistedGlobal(SanitizerMask Mask,
2224
StringRef GlobalName,

clang/lib/Basic/SanitizerSpecialCaseList.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@ using namespace clang;
1616

1717
std::unique_ptr<SanitizerSpecialCaseList>
1818
SanitizerSpecialCaseList::create(const std::vector<std::string> &Paths,
19+
llvm::vfs::FileSystem &VFS,
1920
std::string &Error) {
2021
std::unique_ptr<clang::SanitizerSpecialCaseList> SSCL(
2122
new SanitizerSpecialCaseList());
22-
if (SSCL->createInternal(Paths, Error)) {
23+
if (SSCL->createInternal(Paths, Error, VFS)) {
2324
SSCL->createSanitizerSections();
2425
return SSCL;
2526
}
2627
return nullptr;
2728
}
2829

2930
std::unique_ptr<SanitizerSpecialCaseList>
30-
SanitizerSpecialCaseList::createOrDie(const std::vector<std::string> &Paths) {
31+
SanitizerSpecialCaseList::createOrDie(const std::vector<std::string> &Paths,
32+
llvm::vfs::FileSystem &VFS) {
3133
std::string Error;
32-
if (auto SSCL = create(Paths, Error))
34+
if (auto SSCL = create(Paths, VFS, Error))
3335
return SSCL;
3436
llvm::report_fatal_error(Error);
3537
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
'version': 0,
3+
'roots': [
4+
{ 'name': '@DIR@', 'type': 'directory',
5+
'contents': [
6+
{ 'name': 'only-virtual-file.blacklist', 'type': 'file',
7+
'external-contents': '@REAL_FILE@'
8+
},
9+
{ 'name': 'invalid-virtual-file.blacklist', 'type': 'file',
10+
'external-contents': '@NONEXISTENT_FILE@'
11+
}
12+
]
13+
}
14+
]
15+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Verify ubsan doesn't emit checks for blacklisted functions and files
2+
// RUN: echo "fun:hash" > %t-func.blacklist
3+
// RUN: echo "src:%s" | sed -e 's/\\/\\\\/g' > %t-file.blacklist
4+
5+
// RUN: rm -f %t-vfsoverlay.yaml
6+
// RUN: rm -f %t-nonexistent.blacklist
7+
// 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
8+
// 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
9+
10+
// 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
11+
// INVALID-MAPPED-FILE: invalid-virtual-file.blacklist': {{[Nn]}}o such file or directory
12+
13+
// 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
14+
// INVALID: nonexistent.blacklist': {{[Nn]}}o such file or directory
15+
16+
unsigned i;
17+
18+
// DEFAULT: @hash
19+
// FUNC: @hash
20+
// FILE: @hash
21+
unsigned hash() {
22+
// DEFAULT: call {{.*}}void @__ubsan
23+
// FUNC-NOT: call {{.*}}void @__ubsan
24+
// FILE-NOT: call {{.*}}void @__ubsan
25+
return i * 37;
26+
}
27+
28+
// DEFAULT: @add
29+
// FUNC: @add
30+
// FILE: @add
31+
unsigned add() {
32+
// DEFAULT: call {{.*}}void @__ubsan
33+
// FUNC: call {{.*}}void @__ubsan
34+
// FILE-NOT: call {{.*}}void @__ubsan
35+
return i + 1;
36+
}

llvm/include/llvm/Support/SpecialCaseList.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "llvm/ADT/StringSet.h"
5656
#include "llvm/Support/Regex.h"
5757
#include "llvm/Support/TrigramIndex.h"
58+
#include "llvm/Support/VirtualFileSystem.h"
5859
#include <string>
5960
#include <vector>
6061

@@ -102,8 +103,8 @@ class SpecialCaseList {
102103
protected:
103104
// Implementations of the create*() functions that can also be used by derived
104105
// classes.
105-
bool createInternal(const std::vector<std::string> &Paths,
106-
std::string &Error);
106+
bool createInternal(const std::vector<std::string> &Paths, std::string &Error,
107+
vfs::FileSystem &VFS = *vfs::getRealFileSystem());
107108
bool createInternal(const MemoryBuffer *MB, std::string &Error);
108109

109110
SpecialCaseList() = default;

llvm/lib/Support/SpecialCaseList.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ SpecialCaseList::createOrDie(const std::vector<std::string> &Paths) {
9595
}
9696

9797
bool SpecialCaseList::createInternal(const std::vector<std::string> &Paths,
98-
std::string &Error) {
98+
std::string &Error, vfs::FileSystem &VFS) {
9999
StringMap<size_t> Sections;
100100
for (const auto &Path : Paths) {
101101
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
102-
MemoryBuffer::getFile(Path);
102+
VFS.getBufferForFile(Path);
103103
if (std::error_code EC = FileOrErr.getError()) {
104104
Error = (Twine("can't open file '") + Path + "': " + EC.message()).str();
105105
return false;

0 commit comments

Comments
 (0)