From 131efb4043747cf26c331decc36ae71fdffb2753 Mon Sep 17 00:00:00 2001 From: David Trevelyan Date: Thu, 7 Mar 2024 22:28:02 +0000 Subject: [PATCH 1/8] Add interceptor for read [skip ci] --- compiler-rt/lib/radsan/radsan_interceptors.cpp | 6 ++++++ .../radsan/tests/radsan_test_interceptors.cpp | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/compiler-rt/lib/radsan/radsan_interceptors.cpp b/compiler-rt/lib/radsan/radsan_interceptors.cpp index 53f28f0e98269..225e2d7d98abb 100644 --- a/compiler-rt/lib/radsan/radsan_interceptors.cpp +++ b/compiler-rt/lib/radsan/radsan_interceptors.cpp @@ -102,6 +102,11 @@ INTERCEPTOR(size_t, fread, void *ptr, size_t size, size_t nitems, return REAL(fread)(ptr, size, nitems, stream); } +INTERCEPTOR(ssize_t, read, int fd, void *buf, size_t count) { + radsan::expectNotRealtime("read"); + return REAL(read)(fd, buf, count); +} + INTERCEPTOR(size_t, fwrite, const void *ptr, size_t size, size_t nitems, FILE *stream) { radsan::expectNotRealtime("fwrite"); @@ -368,6 +373,7 @@ void initialiseInterceptors() { INTERCEPT_FUNCTION(close); INTERCEPT_FUNCTION(fopen); INTERCEPT_FUNCTION(fread); + INTERCEPT_FUNCTION(read); INTERCEPT_FUNCTION(fwrite); INTERCEPT_FUNCTION(fclose); INTERCEPT_FUNCTION(fcntl); diff --git a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp index 183306a18726d..8438110b86193 100644 --- a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp +++ b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp @@ -201,6 +201,7 @@ TEST(TestRadsanInterceptors, closeDiesWhenRealtime) { TEST(TestRadsanInterceptors, fopenDiesWhenRealtime) { auto func = []() { auto fd = fopen(temporary_file_path(), "w"); + // Avoid fopen being dead-code eliminated EXPECT_THAT(fd, Ne(nullptr)); }; expectRealtimeDeath(func, "fopen"); @@ -221,6 +222,20 @@ TEST(TestRadsanInterceptors, freadDiesWhenRealtime) { std::remove(temporary_file_path()); } +TEST(TestRadsanInterceptors, readDiesWhenRealtime) { + auto fd = open(temporary_file_path(), O_RDONLY); + auto func = [fd]() { + char c{}; + read(fd, &c, 1); + // Avoid the read call be dead-code eliminated + EXPECT_THAT(c, Eq('\0')); + }; + expectRealtimeDeath(func, "read"); + expectNonrealtimeSurvival(func); + close(fd); + std::remove(temporary_file_path()); +} + TEST(TestRadsanInterceptors, fwriteDiesWhenRealtime) { auto fd = fopen(temporary_file_path(), "w"); ASSERT_NE(nullptr, fd); @@ -233,7 +248,7 @@ TEST(TestRadsanInterceptors, fwriteDiesWhenRealtime) { TEST(TestRadsanInterceptors, fcloseDiesWhenRealtime) { auto fd = fopen(temporary_file_path(), "w"); - EXPECT_THAT(fd, Ne(nullptr)); + EXPECT_THAT(fd, Ne(nullptr)); // Avoids DCE auto func = [fd]() { fclose(fd); }; expectRealtimeDeath(func, "fclose"); expectNonrealtimeSurvival(func); From 5ec8f9e4fb468adec1173b840470dea3376093d0 Mon Sep 17 00:00:00 2001 From: Chris Apple <14171107+cjappl@users.noreply.github.com> Date: Thu, 7 Mar 2024 16:06:52 -0800 Subject: [PATCH 2/8] Add apple read functions --- .../lib/radsan/radsan_interceptors.cpp | 16 ++++++++++ .../radsan/tests/radsan_test_interceptors.cpp | 30 ++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/compiler-rt/lib/radsan/radsan_interceptors.cpp b/compiler-rt/lib/radsan/radsan_interceptors.cpp index 225e2d7d98abb..63d359f9b1758 100644 --- a/compiler-rt/lib/radsan/radsan_interceptors.cpp +++ b/compiler-rt/lib/radsan/radsan_interceptors.cpp @@ -107,6 +107,20 @@ INTERCEPTOR(ssize_t, read, int fd, void *buf, size_t count) { return REAL(read)(fd, buf, count); } +#if SANITIZER_APPLE +INTERCEPTOR(ssize_t, pread, int fd, void *buf, size_t count, off_t offset) { + radsan::expectNotRealtime("pread"); + return REAL(pread)(fd, buf, count, offset); +} + +INTERCEPTOR(ssize_t, readv, int fd, const struct iovec *iov, int iovcnt) { + radsan::expectNotRealtime("readv"); + return REAL(readv)(fd, iov, iovcnt); +} +#endif + + + INTERCEPTOR(size_t, fwrite, const void *ptr, size_t size, size_t nitems, FILE *stream) { radsan::expectNotRealtime("fwrite"); @@ -384,6 +398,8 @@ void initialiseInterceptors() { #if SANITIZER_APPLE INTERCEPT_FUNCTION(OSSpinLockLock); INTERCEPT_FUNCTION(os_unfair_lock_lock); + INTERCEPT_FUNCTION(pread); + INTERCEPT_FUNCTION(readv); #elif SANITIZER_LINUX INTERCEPT_FUNCTION(pthread_spin_lock); #endif diff --git a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp index 8438110b86193..1489a2a23a6b8 100644 --- a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp +++ b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp @@ -16,6 +16,9 @@ #if SANITIZER_APPLE #include #include +#include +#include +#include #endif #if SANITIZER_INTERCEPT_MEMALIGN || SANITIZER_INTERCEPT_PVALLOC @@ -340,7 +343,32 @@ TEST(TestRadsanInterceptors, osUnfairLockLockDiesWhenRealtime) { expectRealtimeDeath(func, "os_unfair_lock_lock"); expectNonrealtimeSurvival(func); } -#endif + +TEST(TestRadsanInterceptors, preadDiesWhenRealtime) { + auto fd = open(temporary_file_path(), O_RDONLY); + auto func = [fd]() { + char c{}; + pread(fd, &c, 1, 0); + }; + expectRealtimeDeath(func, "pread"); + expectNonrealtimeSurvival(func); + close(fd); + std::remove(temporary_file_path()); +} + +TEST(TestRadsanInterceptors, readvDiesWhenRealtime) { + auto fd = open(temporary_file_path(), O_RDONLY); + auto func = [fd]() { + char c{}; + iovec iov = {&c, 1}; + readv(fd, &iov, 1); + }; + expectRealtimeDeath(func, "readv"); + expectNonrealtimeSurvival(func); + close(fd); + std::remove(temporary_file_path()); +} +#endif // SANITIZER_APPLE #if SANITIZER_LINUX TEST(TestRadsanInterceptors, spinLockLockDiesWhenRealtime) { From 9d50120de50af0cca74d17eefa38fdc9fb8a1028 Mon Sep 17 00:00:00 2001 From: Chris Apple <14171107+cjappl@users.noreply.github.com> Date: Thu, 7 Mar 2024 16:18:07 -0800 Subject: [PATCH 3/8] Add write variants --- .../lib/radsan/radsan_interceptors.cpp | 19 +++- .../radsan/tests/radsan_test_interceptors.cpp | 89 ++++++++++++++----- 2 files changed, 83 insertions(+), 25 deletions(-) diff --git a/compiler-rt/lib/radsan/radsan_interceptors.cpp b/compiler-rt/lib/radsan/radsan_interceptors.cpp index 63d359f9b1758..127bd41db99e9 100644 --- a/compiler-rt/lib/radsan/radsan_interceptors.cpp +++ b/compiler-rt/lib/radsan/radsan_interceptors.cpp @@ -107,6 +107,12 @@ INTERCEPTOR(ssize_t, read, int fd, void *buf, size_t count) { return REAL(read)(fd, buf, count); } +// intercept write +INTERCEPTOR(ssize_t, write, int fd, const void *buf, size_t count) { + radsan::expectNotRealtime("write"); + return REAL(write)(fd, buf, count); +} + #if SANITIZER_APPLE INTERCEPTOR(ssize_t, pread, int fd, void *buf, size_t count, off_t offset) { radsan::expectNotRealtime("pread"); @@ -117,8 +123,19 @@ INTERCEPTOR(ssize_t, readv, int fd, const struct iovec *iov, int iovcnt) { radsan::expectNotRealtime("readv"); return REAL(readv)(fd, iov, iovcnt); } -#endif +INTERCEPTOR(ssize_t, pwrite, int fd, const void *buf, size_t count, + off_t offset) { + radsan::expectNotRealtime("pwrite"); + return REAL(pwrite)(fd, buf, count, offset); +} + +INTERCEPTOR(ssize_t, writev, int fd, const struct iovec *iov, int iovcnt) { + radsan::expectNotRealtime("writev"); + return REAL(writev)(fd, iov, iovcnt); +} + +#endif INTERCEPTOR(size_t, fwrite, const void *ptr, size_t size, size_t nitems, diff --git a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp index 1489a2a23a6b8..52beb325157c9 100644 --- a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp +++ b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp @@ -239,6 +239,71 @@ TEST(TestRadsanInterceptors, readDiesWhenRealtime) { std::remove(temporary_file_path()); } +TEST(TestRadsanInterceptors, writeDiesWhenRealtime) { + auto fd = open(temporary_file_path(), O_WRONLY); + auto func = [fd]() { + char c{}; + write(fd, &c, 1); + }; + expectRealtimeDeath(func, "write"); + expectNonrealtimeSurvival(func); + close(fd); + std::remove(temporary_file_path()); +} + +#if SANITIZER_APPLE +TEST(TestRadsanInterceptors, preadDiesWhenRealtime) { + auto fd = open(temporary_file_path(), O_RDONLY); + auto func = [fd]() { + char c{}; + pread(fd, &c, 1, 0); + }; + expectRealtimeDeath(func, "pread"); + expectNonrealtimeSurvival(func); + close(fd); + std::remove(temporary_file_path()); +} + +TEST(TestRadsanInterceptors, readvDiesWhenRealtime) { + auto fd = open(temporary_file_path(), O_RDONLY); + auto func = [fd]() { + char c{}; + iovec iov = {&c, 1}; + readv(fd, &iov, 1); + }; + expectRealtimeDeath(func, "readv"); + expectNonrealtimeSurvival(func); + close(fd); + std::remove(temporary_file_path()); +} + +TEST(TestRadsanInterceptors, pwriteDiesWhenRealtime) { + auto fd = open(temporary_file_path(), O_WRONLY); + auto func = [fd]() { + char c{}; + pwrite(fd, &c, 1, 0); + }; + expectRealtimeDeath(func, "pwrite"); + expectNonrealtimeSurvival(func); + close(fd); + std::remove(temporary_file_path()); +} + +TEST(TestRadsanInterceptors, writevDiesWhenRealtime) { + auto fd = open(temporary_file_path(), O_WRONLY); + auto func = [fd]() { + char c{}; + iovec iov = {&c, 1}; + writev(fd, &iov, 1); + }; + expectRealtimeDeath(func, "writev"); + expectNonrealtimeSurvival(func); + close(fd); + std::remove(temporary_file_path()); +} + +#endif // SANITIZER_APPLE + TEST(TestRadsanInterceptors, fwriteDiesWhenRealtime) { auto fd = fopen(temporary_file_path(), "w"); ASSERT_NE(nullptr, fd); @@ -344,30 +409,6 @@ TEST(TestRadsanInterceptors, osUnfairLockLockDiesWhenRealtime) { expectNonrealtimeSurvival(func); } -TEST(TestRadsanInterceptors, preadDiesWhenRealtime) { - auto fd = open(temporary_file_path(), O_RDONLY); - auto func = [fd]() { - char c{}; - pread(fd, &c, 1, 0); - }; - expectRealtimeDeath(func, "pread"); - expectNonrealtimeSurvival(func); - close(fd); - std::remove(temporary_file_path()); -} - -TEST(TestRadsanInterceptors, readvDiesWhenRealtime) { - auto fd = open(temporary_file_path(), O_RDONLY); - auto func = [fd]() { - char c{}; - iovec iov = {&c, 1}; - readv(fd, &iov, 1); - }; - expectRealtimeDeath(func, "readv"); - expectNonrealtimeSurvival(func); - close(fd); - std::remove(temporary_file_path()); -} #endif // SANITIZER_APPLE #if SANITIZER_LINUX From 680b5795eb06bd621e5f90fe407aabd25bb01f17 Mon Sep 17 00:00:00 2001 From: Chris Apple <14171107+cjappl@users.noreply.github.com> Date: Thu, 7 Mar 2024 21:50:25 -0800 Subject: [PATCH 4/8] Add checks for valid fd --- compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp index 52beb325157c9..13dd3822199c9 100644 --- a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp +++ b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp @@ -227,6 +227,7 @@ TEST(TestRadsanInterceptors, freadDiesWhenRealtime) { TEST(TestRadsanInterceptors, readDiesWhenRealtime) { auto fd = open(temporary_file_path(), O_RDONLY); + EXPECT_THAT(fd, Ne(-1)); auto func = [fd]() { char c{}; read(fd, &c, 1); @@ -241,6 +242,7 @@ TEST(TestRadsanInterceptors, readDiesWhenRealtime) { TEST(TestRadsanInterceptors, writeDiesWhenRealtime) { auto fd = open(temporary_file_path(), O_WRONLY); + EXPECT_THAT(fd, Ne(-1)); auto func = [fd]() { char c{}; write(fd, &c, 1); From 64e5fed6f7ce31892339498ac4c8d81dd3bd594d Mon Sep 17 00:00:00 2001 From: Chris Apple <14171107+cjappl@users.noreply.github.com> Date: Thu, 7 Mar 2024 21:56:29 -0800 Subject: [PATCH 5/8] Add missing interceptors for linux --- compiler-rt/lib/radsan/radsan_interceptors.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/compiler-rt/lib/radsan/radsan_interceptors.cpp b/compiler-rt/lib/radsan/radsan_interceptors.cpp index 127bd41db99e9..83040e36bc653 100644 --- a/compiler-rt/lib/radsan/radsan_interceptors.cpp +++ b/compiler-rt/lib/radsan/radsan_interceptors.cpp @@ -135,8 +135,7 @@ INTERCEPTOR(ssize_t, writev, int fd, const struct iovec *iov, int iovcnt) { return REAL(writev)(fd, iov, iovcnt); } -#endif - +#endif // SANITIZER_APPLE INTERCEPTOR(size_t, fwrite, const void *ptr, size_t size, size_t nitems, FILE *stream) { @@ -405,6 +404,14 @@ void initialiseInterceptors() { INTERCEPT_FUNCTION(fopen); INTERCEPT_FUNCTION(fread); INTERCEPT_FUNCTION(read); + INTERCEPT_FUNCTION(write); +#if SANITIZER_APPLE + INTERCEPT_FUNCTION(pread); + INTERCEPT_FUNCTION(readv); + INTERCEPT_FUNCTION(pwrite); + INTERCEPT_FUNCTION(writev); +#endif + INTERCEPT_FUNCTION(fwrite); INTERCEPT_FUNCTION(fclose); INTERCEPT_FUNCTION(fcntl); @@ -415,8 +422,6 @@ void initialiseInterceptors() { #if SANITIZER_APPLE INTERCEPT_FUNCTION(OSSpinLockLock); INTERCEPT_FUNCTION(os_unfair_lock_lock); - INTERCEPT_FUNCTION(pread); - INTERCEPT_FUNCTION(readv); #elif SANITIZER_LINUX INTERCEPT_FUNCTION(pthread_spin_lock); #endif From 7c9b2c4a1ecd5ae4ab643fe99b667c1bbce82286 Mon Sep 17 00:00:00 2001 From: Chris Apple <14171107+cjappl@users.noreply.github.com> Date: Fri, 8 Mar 2024 07:20:41 -0800 Subject: [PATCH 6/8] Add extra invalid checks, make test fixture --- .../radsan/tests/radsan_test_interceptors.cpp | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp index 13dd3822199c9..ea48f0e0cd9e1 100644 --- a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp +++ b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp @@ -225,21 +225,36 @@ TEST(TestRadsanInterceptors, freadDiesWhenRealtime) { std::remove(temporary_file_path()); } -TEST(TestRadsanInterceptors, readDiesWhenRealtime) { - auto fd = open(temporary_file_path(), O_RDONLY); - EXPECT_THAT(fd, Ne(-1)); - auto func = [fd]() { + +class FileTestFixture : public ::testing::Test { +protected: + int fd = -1; // File descriptor + const char* filePath = "testfile.txt"; + + void SetUp() override { + // Create and open the file, RW mode, create if not exist, permissions 0666 + fd = open(filePath, O_RDWR | O_CREAT, 0666); + ASSERT_TRUE(fd != -1) << "Failed to open file"; + } + + void TearDown() override { + // Close the file + ASSERT_EQ(close(fd), 0) << "Failed to close file"; + // Delete the file + ASSERT_EQ(remove(filePath), 0) << "Failed to delete file"; + } +}; + +TEST_F(FileTestFixture, readDiesWhenRealtime) { + auto func = [this]() { char c{}; read(fd, &c, 1); - // Avoid the read call be dead-code eliminated - EXPECT_THAT(c, Eq('\0')); }; expectRealtimeDeath(func, "read"); expectNonrealtimeSurvival(func); - close(fd); - std::remove(temporary_file_path()); } + TEST(TestRadsanInterceptors, writeDiesWhenRealtime) { auto fd = open(temporary_file_path(), O_WRONLY); EXPECT_THAT(fd, Ne(-1)); @@ -256,6 +271,7 @@ TEST(TestRadsanInterceptors, writeDiesWhenRealtime) { #if SANITIZER_APPLE TEST(TestRadsanInterceptors, preadDiesWhenRealtime) { auto fd = open(temporary_file_path(), O_RDONLY); + EXPECT_THAT(fd, Ne(-1)); auto func = [fd]() { char c{}; pread(fd, &c, 1, 0); @@ -268,6 +284,7 @@ TEST(TestRadsanInterceptors, preadDiesWhenRealtime) { TEST(TestRadsanInterceptors, readvDiesWhenRealtime) { auto fd = open(temporary_file_path(), O_RDONLY); + EXPECT_THAT(fd, Ne(-1)); auto func = [fd]() { char c{}; iovec iov = {&c, 1}; @@ -281,6 +298,7 @@ TEST(TestRadsanInterceptors, readvDiesWhenRealtime) { TEST(TestRadsanInterceptors, pwriteDiesWhenRealtime) { auto fd = open(temporary_file_path(), O_WRONLY); + EXPECT_THAT(fd, Ne(-1)); auto func = [fd]() { char c{}; pwrite(fd, &c, 1, 0); @@ -293,6 +311,7 @@ TEST(TestRadsanInterceptors, pwriteDiesWhenRealtime) { TEST(TestRadsanInterceptors, writevDiesWhenRealtime) { auto fd = open(temporary_file_path(), O_WRONLY); + EXPECT_THAT(fd, Ne(-1)); auto func = [fd]() { char c{}; iovec iov = {&c, 1}; From 77f76c34befb93c16c3f7df2d495199cb6ced7d7 Mon Sep 17 00:00:00 2001 From: Chris Apple <14171107+cjappl@users.noreply.github.com> Date: Fri, 8 Mar 2024 07:32:02 -0800 Subject: [PATCH 7/8] Tests passing on all systems --- .../radsan/tests/radsan_test_interceptors.cpp | 107 +++++++----------- 1 file changed, 42 insertions(+), 65 deletions(-) diff --git a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp index ea48f0e0cd9e1..ad575bc8e96df 100644 --- a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp +++ b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp @@ -225,24 +225,22 @@ TEST(TestRadsanInterceptors, freadDiesWhenRealtime) { std::remove(temporary_file_path()); } - class FileTestFixture : public ::testing::Test { protected: - int fd = -1; // File descriptor - const char* filePath = "testfile.txt"; - - void SetUp() override { - // Create and open the file, RW mode, create if not exist, permissions 0666 - fd = open(filePath, O_RDWR | O_CREAT, 0666); - ASSERT_TRUE(fd != -1) << "Failed to open file"; - } - - void TearDown() override { - // Close the file - ASSERT_EQ(close(fd), 0) << "Failed to close file"; - // Delete the file - ASSERT_EQ(remove(filePath), 0) << "Failed to delete file"; + int fd = -1; + + void SetUp() override { + fd = open(temporary_file_path(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_TRUE(fd != -1) << "Failed to open file"; + } + + void TearDown() override { + if (fd != -1) + { + close(fd); + remove(temporary_file_path()); } + } }; TEST_F(FileTestFixture, readDiesWhenRealtime) { @@ -254,73 +252,52 @@ TEST_F(FileTestFixture, readDiesWhenRealtime) { expectNonrealtimeSurvival(func); } - -TEST(TestRadsanInterceptors, writeDiesWhenRealtime) { - auto fd = open(temporary_file_path(), O_WRONLY); - EXPECT_THAT(fd, Ne(-1)); - auto func = [fd]() { +TEST_F(FileTestFixture, writeDiesWhenRealtime) { + auto func = [this]() { char c{}; write(fd, &c, 1); }; expectRealtimeDeath(func, "write"); expectNonrealtimeSurvival(func); - close(fd); - std::remove(temporary_file_path()); } #if SANITIZER_APPLE -TEST(TestRadsanInterceptors, preadDiesWhenRealtime) { - auto fd = open(temporary_file_path(), O_RDONLY); - EXPECT_THAT(fd, Ne(-1)); - auto func = [fd]() { - char c{}; - pread(fd, &c, 1, 0); +TEST_F(FileTestFixture, preadDiesWhenRealtime) { + auto func = [this]() { + char c{}; + pread(fd, &c, 1, 0); }; expectRealtimeDeath(func, "pread"); expectNonrealtimeSurvival(func); - close(fd); - std::remove(temporary_file_path()); } -TEST(TestRadsanInterceptors, readvDiesWhenRealtime) { - auto fd = open(temporary_file_path(), O_RDONLY); - EXPECT_THAT(fd, Ne(-1)); - auto func = [fd]() { - char c{}; - iovec iov = {&c, 1}; - readv(fd, &iov, 1); - }; - expectRealtimeDeath(func, "readv"); - expectNonrealtimeSurvival(func); - close(fd); - std::remove(temporary_file_path()); +TEST_F(FileTestFixture, readvDiesWhenRealtime) { + auto func = [this]() { + char c{}; + iovec iov = {&c, 1}; + readv(fd, &iov, 1); + }; + expectRealtimeDeath(func, "readv"); + expectNonrealtimeSurvival(func); } -TEST(TestRadsanInterceptors, pwriteDiesWhenRealtime) { - auto fd = open(temporary_file_path(), O_WRONLY); - EXPECT_THAT(fd, Ne(-1)); - auto func = [fd]() { - char c{}; - pwrite(fd, &c, 1, 0); - }; - expectRealtimeDeath(func, "pwrite"); - expectNonrealtimeSurvival(func); - close(fd); - std::remove(temporary_file_path()); +TEST_F(FileTestFixture, pwriteDiesWhenRealtime) { + auto func = [this]() { + char c{}; + pwrite(fd, &c, 1, 0); + }; + expectRealtimeDeath(func, "pwrite"); + expectNonrealtimeSurvival(func); } -TEST(TestRadsanInterceptors, writevDiesWhenRealtime) { - auto fd = open(temporary_file_path(), O_WRONLY); - EXPECT_THAT(fd, Ne(-1)); - auto func = [fd]() { - char c{}; - iovec iov = {&c, 1}; - writev(fd, &iov, 1); - }; - expectRealtimeDeath(func, "writev"); - expectNonrealtimeSurvival(func); - close(fd); - std::remove(temporary_file_path()); +TEST_F(FileTestFixture, writevDiesWhenRealtime) { + auto func = [this]() { + char c{}; + iovec iov = {&c, 1}; + writev(fd, &iov, 1); + }; + expectRealtimeDeath(func, "writev"); + expectNonrealtimeSurvival(func); } #endif // SANITIZER_APPLE From ed319d98c0f0cb1380ef3dba529db640e3035818 Mon Sep 17 00:00:00 2001 From: Chris Apple <14171107+cjappl@users.noreply.github.com> Date: Fri, 8 Mar 2024 07:40:25 -0800 Subject: [PATCH 8/8] Rename test fixture --- .../lib/radsan/tests/radsan_test_interceptors.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp index ad575bc8e96df..7fd381c289505 100644 --- a/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp +++ b/compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp @@ -225,7 +225,7 @@ TEST(TestRadsanInterceptors, freadDiesWhenRealtime) { std::remove(temporary_file_path()); } -class FileTestFixture : public ::testing::Test { +class FdFixture : public ::testing::Test { protected: int fd = -1; @@ -243,7 +243,7 @@ class FileTestFixture : public ::testing::Test { } }; -TEST_F(FileTestFixture, readDiesWhenRealtime) { +TEST_F(FdFixture, readDiesWhenRealtime) { auto func = [this]() { char c{}; read(fd, &c, 1); @@ -252,7 +252,7 @@ TEST_F(FileTestFixture, readDiesWhenRealtime) { expectNonrealtimeSurvival(func); } -TEST_F(FileTestFixture, writeDiesWhenRealtime) { +TEST_F(FdFixture, writeDiesWhenRealtime) { auto func = [this]() { char c{}; write(fd, &c, 1); @@ -262,7 +262,7 @@ TEST_F(FileTestFixture, writeDiesWhenRealtime) { } #if SANITIZER_APPLE -TEST_F(FileTestFixture, preadDiesWhenRealtime) { +TEST_F(FdFixture, preadDiesWhenRealtime) { auto func = [this]() { char c{}; pread(fd, &c, 1, 0); @@ -271,7 +271,7 @@ TEST_F(FileTestFixture, preadDiesWhenRealtime) { expectNonrealtimeSurvival(func); } -TEST_F(FileTestFixture, readvDiesWhenRealtime) { +TEST_F(FdFixture, readvDiesWhenRealtime) { auto func = [this]() { char c{}; iovec iov = {&c, 1}; @@ -281,7 +281,7 @@ TEST_F(FileTestFixture, readvDiesWhenRealtime) { expectNonrealtimeSurvival(func); } -TEST_F(FileTestFixture, pwriteDiesWhenRealtime) { +TEST_F(FdFixture, pwriteDiesWhenRealtime) { auto func = [this]() { char c{}; pwrite(fd, &c, 1, 0); @@ -290,7 +290,7 @@ TEST_F(FileTestFixture, pwriteDiesWhenRealtime) { expectNonrealtimeSurvival(func); } -TEST_F(FileTestFixture, writevDiesWhenRealtime) { +TEST_F(FdFixture, writevDiesWhenRealtime) { auto func = [this]() { char c{}; iovec iov = {&c, 1};