[compiler-rt] [compiler-rt][rtsan] stat api interception. (PR #128430)
David CARLIER via llvm-commits
llvm-commits at lists.llvm.org
Sun Feb 23 10:41:58 PST 2025
https://github.com/devnexen updated https://github.com/llvm/llvm-project/pull/128430
>From 4031334c7f934ef7973939c8b76bd7ee49f41d37 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Sun, 23 Feb 2025 18:02:37 +0000
Subject: [PATCH 1/2] [compiler-rt][rtsan] stat api interception.
---
.../lib/rtsan/rtsan_interceptors_posix.cpp | 36 +++++++++++++++++++
.../tests/rtsan_test_interceptors_posix.cpp | 31 ++++++++++++++--
2 files changed, 65 insertions(+), 2 deletions(-)
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index 5b9e992639f55..ee602bcad68f9 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -285,6 +285,36 @@ INTERCEPTOR(int, unlinkat, int fd, const char *pathname, int flag) {
return REAL(unlinkat)(fd, pathname, flag);
}
+INTERCEPTOR(int, stat, const char *pathname, struct stat *s) {
+ __rtsan_notify_intercepted_call("stat");
+ return REAL(stat)(pathname, s);
+}
+
+INTERCEPTOR(int, lstat, const char *pathname, struct stat *s) {
+ __rtsan_notify_intercepted_call("lstat");
+ return REAL(lstat)(pathname, s);
+}
+
+INTERCEPTOR(int, fstat, int fd, struct stat *s) {
+ __rtsan_notify_intercepted_call("fstat");
+ return REAL(fstat)(fd, s);
+}
+
+INTERCEPTOR(int, stat64, const char *pathname, struct stat64 *s) {
+ __rtsan_notify_intercepted_call("stat64");
+ return REAL(stat64)(pathname, s);
+}
+
+INTERCEPTOR(int, lstat64, const char *pathname, struct stat64 *s) {
+ __rtsan_notify_intercepted_call("lstat64");
+ return REAL(lstat64)(pathname, s);
+}
+
+INTERCEPTOR(int, fstat64, int fd, struct stat64 *s) {
+ __rtsan_notify_intercepted_call("fstat64");
+ return REAL(fstat64)(fd, s);
+}
+
// Streams
INTERCEPTOR(FILE *, fopen, const char *path, const char *mode) {
@@ -1437,6 +1467,12 @@ void __rtsan::InitializeInterceptors() {
RTSAN_MAYBE_INTERCEPT_READLINKAT;
INTERCEPT_FUNCTION(unlink);
INTERCEPT_FUNCTION(unlinkat);
+ INTERCEPT_FUNCTION(stat);
+ INTERCEPT_FUNCTION(lstat);
+ INTERCEPT_FUNCTION(fstat);
+ INTERCEPT_FUNCTION(stat64);
+ INTERCEPT_FUNCTION(lstat64);
+ INTERCEPT_FUNCTION(fstat64);
INTERCEPT_FUNCTION(fopen);
RTSAN_MAYBE_INTERCEPT_FOPEN64;
RTSAN_MAYBE_INTERCEPT_FREOPEN64;
diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
index d1c5a94c12213..301f4e2694962 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -401,7 +401,7 @@ TEST_F(RtsanFileTest, FcntlFlockDiesWhenRealtime) {
ASSERT_THAT(fd, Ne(-1));
auto Func = [fd]() {
- struct flock lock {};
+ struct flock lock{};
lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
@@ -735,7 +735,7 @@ TEST(TestRtsanInterceptors, IoctlBehavesWithOutputPointer) {
GTEST_SKIP();
}
- struct ifreq ifr {};
+ struct ifreq ifr{};
strncpy(ifr.ifr_name, ifaddr->ifa_name, IFNAMSIZ - 1);
int retval = ioctl(sock, SIOCGIFADDR, &ifr);
@@ -875,6 +875,33 @@ TEST_F(RtsanOpenedFileTest, UnlinkatDiesWhenRealtime) {
ExpectNonRealtimeSurvival(Func);
}
+TEST_F(RtsanOpenedFileTest, StatDiesWhenRealtime) {
+ auto Func = [&]() {
+ struct stat s{};
+ stat(GetTemporaryFilePath(), &s);
+ };
+ ExpectRealtimeDeath(Func, MAYBE_APPEND_64("stat"));
+ ExpectNonRealtimeSurvival(Func);
+}
+
+TEST_F(RtsanOpenedFileTest, LtatDiesWhenRealtime) {
+ auto Func = [&]() {
+ struct stat s{};
+ lstat(GetTemporaryFilePath(), &s);
+ };
+ ExpectRealtimeDeath(Func, MAYBE_APPEND_64("lstat"));
+ ExpectNonRealtimeSurvival(Func);
+}
+
+TEST_F(RtsanOpenedFileTest, FstatDiesWhenRealtime) {
+ auto Func = [&]() {
+ struct stat s{};
+ fstat(GetOpenFd(), &s);
+ };
+ ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fstat"));
+ ExpectNonRealtimeSurvival(Func);
+}
+
TEST_F(RtsanFileTest, FcloseDiesWhenRealtime) {
FILE *f = fopen(GetTemporaryFilePath(), "w");
EXPECT_THAT(f, Ne(nullptr));
>From 42fedd18a989da89ee31a6970c6a35a1b89e58d7 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Sun, 23 Feb 2025 18:41:47 +0000
Subject: [PATCH 2/2] do not bother with obsolete 64 interface with darwin
---
.../lib/rtsan/rtsan_interceptors_posix.cpp | 15 ++++++--
.../tests/rtsan_test_interceptors_posix.cpp | 37 +++++++++++++++++--
2 files changed, 45 insertions(+), 7 deletions(-)
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index ee602bcad68f9..43a88fafef5b7 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -300,6 +300,7 @@ INTERCEPTOR(int, fstat, int fd, struct stat *s) {
return REAL(fstat)(fd, s);
}
+#if !SANITIZER_APPLE // deprecated for darwin
INTERCEPTOR(int, stat64, const char *pathname, struct stat64 *s) {
__rtsan_notify_intercepted_call("stat64");
return REAL(stat64)(pathname, s);
@@ -314,6 +315,14 @@ INTERCEPTOR(int, fstat64, int fd, struct stat64 *s) {
__rtsan_notify_intercepted_call("fstat64");
return REAL(fstat64)(fd, s);
}
+#define RTSAN_MAYBE_INTERCEPT_STAT64 INTERCEPTOR_FUNCTION(stat64)
+#define RTSAN_MAYBE_INTERCEPT_LSTAT64 INTERCEPTOR_FUNCTION(lstat64)
+#define RTSAN_MAYBE_INTERCEPT_FSTAT64 INTERCEPTOR_FUNCTION(fstat64)
+#else
+#define RTSAN_MAYBE_INTERCEPT_STAT64
+#define RTSAN_MAYBE_INTERCEPT_LSTAT64
+#define RTSAN_MAYBE_INTERCEPT_FSTAT64
+#endif
// Streams
@@ -1470,9 +1479,9 @@ void __rtsan::InitializeInterceptors() {
INTERCEPT_FUNCTION(stat);
INTERCEPT_FUNCTION(lstat);
INTERCEPT_FUNCTION(fstat);
- INTERCEPT_FUNCTION(stat64);
- INTERCEPT_FUNCTION(lstat64);
- INTERCEPT_FUNCTION(fstat64);
+ RTSAN_MAYBE_INTERCEPT_STAT64;
+ RTSAN_MAYBE_INTERCEPT_LSTAT64;
+ RTSAN_MAYBE_INTERCEPT_FSTAT64;
INTERCEPT_FUNCTION(fopen);
RTSAN_MAYBE_INTERCEPT_FOPEN64;
RTSAN_MAYBE_INTERCEPT_FREOPEN64;
diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
index 301f4e2694962..2db7bf12bb8e4 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -880,16 +880,16 @@ TEST_F(RtsanOpenedFileTest, StatDiesWhenRealtime) {
struct stat s{};
stat(GetTemporaryFilePath(), &s);
};
- ExpectRealtimeDeath(Func, MAYBE_APPEND_64("stat"));
+ ExpectRealtimeDeath(Func, "stat");
ExpectNonRealtimeSurvival(Func);
}
-TEST_F(RtsanOpenedFileTest, LtatDiesWhenRealtime) {
+TEST_F(RtsanOpenedFileTest, LstatDiesWhenRealtime) {
auto Func = [&]() {
struct stat s{};
lstat(GetTemporaryFilePath(), &s);
};
- ExpectRealtimeDeath(Func, MAYBE_APPEND_64("lstat"));
+ ExpectRealtimeDeath(Func, "lstat");
ExpectNonRealtimeSurvival(Func);
}
@@ -898,10 +898,39 @@ TEST_F(RtsanOpenedFileTest, FstatDiesWhenRealtime) {
struct stat s{};
fstat(GetOpenFd(), &s);
};
- ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fstat"));
+ ExpectRealtimeDeath(Func, "fstat");
ExpectNonRealtimeSurvival(Func);
}
+#if !SANITIZER_APPLE // deprecated for darwin
+TEST_F(RtsanOpenedFileTest, Stat64DiesWhenRealtime) {
+ auto Func = [&]() {
+ struct stat64 s{};
+ stat64(GetTemporaryFilePath(), &s);
+ };
+ ExpectRealtimeDeath(Func, "stat");
+ ExpectNonRealtimeSurvival(Func);
+}
+
+TEST_F(RtsanOpenedFileTest, Lstat64DiesWhenRealtime) {
+ auto Func = [&]() {
+ struct stat64 s{};
+ lstat64(GetTemporaryFilePath(), &s);
+ };
+ ExpectRealtimeDeath(Func, "lstat64");
+ ExpectNonRealtimeSurvival(Func);
+}
+
+TEST_F(RtsanOpenedFileTest, Fstat64DiesWhenRealtime) {
+ auto Func = [&]() {
+ struct stat64 s{};
+ fstat64(GetOpenFd(), &s);
+ };
+ ExpectRealtimeDeath(Func, "fstat64");
+ ExpectNonRealtimeSurvival(Func);
+}
+#endif
+
TEST_F(RtsanFileTest, FcloseDiesWhenRealtime) {
FILE *f = fopen(GetTemporaryFilePath(), "w");
EXPECT_THAT(f, Ne(nullptr));
More information about the llvm-commits
mailing list