Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 3 additions & 21 deletions compiler-rt/lib/radsan/tests/radsan_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <shared_mutex>
#include <thread>

#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101200
#define SI_MAC_DEPLOYMENT_AT_LEAST_10_12 1
#else
Expand Down Expand Up @@ -62,35 +62,18 @@ TEST(TestRadsan, sleepingAThreadDiesWhenRealtime) {
expectNonrealtimeSurvival(func);
}

TEST(TestRadsan, fopenDiesWhenRealtime) {
auto func = []() {
auto fd = fopen("./file.txt", "w");
EXPECT_THAT(fd, Ne(nullptr));
if (fd != nullptr)
fclose(fd);
};
expectRealtimeDeath(func);
expectNonrealtimeSurvival(func);
}

TEST(TestRadsan, fcloseDiesWhenRealtime) {
auto fd = fopen("./file.txt", "r");
ASSERT_THAT(fd, Ne(nullptr));
auto func = [fd]() { fclose(fd); };
expectRealtimeDeath(func);
expectNonrealtimeSurvival(func);
}

TEST(TestRadsan, ifstreamCreationDiesWhenRealtime) {
auto func = []() { auto ifs = std::ifstream("./file.txt"); };
expectRealtimeDeath(func);
expectNonrealtimeSurvival(func);
std::remove("./file.txt");
}

TEST(TestRadsan, ofstreamCreationDiesWhenRealtime) {
auto func = []() { auto ofs = std::ofstream("./file.txt"); };
expectRealtimeDeath(func);
expectNonrealtimeSurvival(func);
std::remove("./file.txt");
}

TEST(TestRadsan, lockingAMutexDiesWhenRealtime) {
Expand All @@ -108,7 +91,6 @@ TEST(TestRadsan, unlockingAMutexDiesWhenRealtime) {
expectNonrealtimeSurvival(func);
}


#if RADSAN_TEST_SHARED_MUTEX

TEST(TestRadsan, lockingASharedMutexDiesWhenRealtime) {
Expand Down
43 changes: 33 additions & 10 deletions compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ using namespace std::chrono_literals;

namespace {
void *fake_thread_entry_point(void *) { return nullptr; }

/*
The creat function doesn't seem to work on an ubuntu Docker image when the
path is in a shared volume of the host. For now, to keep testing convenient
with a local Docker container, we just put it somewhere that's not in the
shared volume (/tmp). This is volatile and will be cleaned up as soon as the
container is stopped.
*/
constexpr const char *temporary_file_path() {
#if SANITIZER_LINUX
return "/tmp/radsan_temporary_test_file.txt";
#elif SANITIZER_APPLE
return "./radsan_temporary_test_file.txt";
#endif
}
} // namespace

/*
Expand Down Expand Up @@ -131,21 +146,24 @@ TEST(TestRadsanInterceptors, nanosleepDiesWhenRealtime) {
*/

TEST(TestRadsanInterceptors, openDiesWhenRealtime) {
auto func = []() { open("./file.txt", O_RDONLY); };
auto func = []() { open(temporary_file_path(), O_RDONLY); };
expectRealtimeDeath(func, "open");
expectNonrealtimeSurvival(func);
std::remove(temporary_file_path());
}

TEST(TestRadsanInterceptors, openatDiesWhenRealtime) {
auto func = []() { openat(0, "./file.txt", O_RDONLY); };
auto func = []() { openat(0, temporary_file_path(), O_RDONLY); };
expectRealtimeDeath(func, "openat");
expectNonrealtimeSurvival(func);
std::remove(temporary_file_path());
}

TEST(TestRadsanInterceptors, creatDiesWhenRealtime) {
auto func = []() { creat("./file.txt", O_TRUNC); };
auto func = []() { creat(temporary_file_path(), S_IWOTH | S_IROTH); };
expectRealtimeDeath(func, "creat");
expectNonrealtimeSurvival(func);
std::remove(temporary_file_path());
}

TEST(TestRadsanInterceptors, fcntlDiesWhenRealtime) {
Expand All @@ -162,16 +180,16 @@ TEST(TestRadsanInterceptors, closeDiesWhenRealtime) {

TEST(TestRadsanInterceptors, fopenDiesWhenRealtime) {
auto func = []() {
auto fd = fopen("./file.txt", "r");
if (fd != nullptr)
fclose(fd);
auto fd = fopen(temporary_file_path(), "w");
EXPECT_THAT(fd, Ne(nullptr));
};
expectRealtimeDeath(func, "fopen");
expectNonrealtimeSurvival(func);
std::remove(temporary_file_path());
}

TEST(TestRadsanInterceptors, freadDiesWhenRealtime) {
auto fd = fopen("./file.txt", "r");
auto fd = fopen(temporary_file_path(), "w");
auto func = [fd]() {
char c{};
fread(&c, 1, 1, fd);
Expand All @@ -180,22 +198,26 @@ TEST(TestRadsanInterceptors, freadDiesWhenRealtime) {
expectNonrealtimeSurvival(func);
if (fd != nullptr)
fclose(fd);
std::remove(temporary_file_path());
}

TEST(TestRadsanInterceptors, fwriteDiesWhenRealtime) {
auto fd = fopen("./file.txt", "w");
auto fd = fopen(temporary_file_path(), "w");
ASSERT_NE(nullptr, fd);
auto message = "Hello, world!";
auto func = [&]() { fwrite(&message, 1, 4, fd); };
expectRealtimeDeath(func, "fwrite");
expectNonrealtimeSurvival(func);
std::remove(temporary_file_path());
}

TEST(TestRadsanInterceptors, fcloseDiesWhenRealtime) {
auto fd = fopen("./file.txt", "r");
auto fd = fopen(temporary_file_path(), "w");
EXPECT_THAT(fd, Ne(nullptr));
auto func = [fd]() { fclose(fd); };
expectRealtimeDeath(func, "fclose");
expectNonrealtimeSurvival(func);
std::remove(temporary_file_path());
}

TEST(TestRadsanInterceptors, putsDiesWhenRealtime) {
Expand All @@ -205,13 +227,14 @@ TEST(TestRadsanInterceptors, putsDiesWhenRealtime) {
}

TEST(TestRadsanInterceptors, fputsDiesWhenRealtime) {
auto fd = fopen("./file.txt", "w");
auto fd = fopen(temporary_file_path(), "w");
ASSERT_THAT(fd, Ne(nullptr)) << errno;
auto func = [fd]() { fputs("Hello, world!\n", fd); };
expectRealtimeDeath(func);
expectNonrealtimeSurvival(func);
if (fd != nullptr)
fclose(fd);
std::remove(temporary_file_path());
}

/*
Expand Down