Skip to content

Commit 4d928d5

Browse files
authored
[compiler-rt][rtsan] stat api interception. (#128430)
1 parent 1794dfb commit 4d928d5

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,45 @@ INTERCEPTOR(int, unlinkat, int fd, const char *pathname, int flag) {
285285
return REAL(unlinkat)(fd, pathname, flag);
286286
}
287287

288+
INTERCEPTOR(int, stat, const char *pathname, struct stat *s) {
289+
__rtsan_notify_intercepted_call("stat");
290+
return REAL(stat)(pathname, s);
291+
}
292+
293+
INTERCEPTOR(int, lstat, const char *pathname, struct stat *s) {
294+
__rtsan_notify_intercepted_call("lstat");
295+
return REAL(lstat)(pathname, s);
296+
}
297+
298+
INTERCEPTOR(int, fstat, int fd, struct stat *s) {
299+
__rtsan_notify_intercepted_call("fstat");
300+
return REAL(fstat)(fd, s);
301+
}
302+
303+
#if !SANITIZER_APPLE // deprecated for darwin
304+
INTERCEPTOR(int, stat64, const char *pathname, struct stat64 *s) {
305+
__rtsan_notify_intercepted_call("stat64");
306+
return REAL(stat64)(pathname, s);
307+
}
308+
309+
INTERCEPTOR(int, lstat64, const char *pathname, struct stat64 *s) {
310+
__rtsan_notify_intercepted_call("lstat64");
311+
return REAL(lstat64)(pathname, s);
312+
}
313+
314+
INTERCEPTOR(int, fstat64, int fd, struct stat64 *s) {
315+
__rtsan_notify_intercepted_call("fstat64");
316+
return REAL(fstat64)(fd, s);
317+
}
318+
#define RTSAN_MAYBE_INTERCEPT_STAT64 INTERCEPT_FUNCTION(stat64)
319+
#define RTSAN_MAYBE_INTERCEPT_LSTAT64 INTERCEPT_FUNCTION(lstat64)
320+
#define RTSAN_MAYBE_INTERCEPT_FSTAT64 INTERCEPT_FUNCTION(fstat64)
321+
#else
322+
#define RTSAN_MAYBE_INTERCEPT_STAT64
323+
#define RTSAN_MAYBE_INTERCEPT_LSTAT64
324+
#define RTSAN_MAYBE_INTERCEPT_FSTAT64
325+
#endif
326+
288327
// Streams
289328

290329
INTERCEPTOR(FILE *, fopen, const char *path, const char *mode) {
@@ -1437,6 +1476,12 @@ void __rtsan::InitializeInterceptors() {
14371476
RTSAN_MAYBE_INTERCEPT_READLINKAT;
14381477
INTERCEPT_FUNCTION(unlink);
14391478
INTERCEPT_FUNCTION(unlinkat);
1479+
INTERCEPT_FUNCTION(stat);
1480+
INTERCEPT_FUNCTION(lstat);
1481+
INTERCEPT_FUNCTION(fstat);
1482+
RTSAN_MAYBE_INTERCEPT_STAT64;
1483+
RTSAN_MAYBE_INTERCEPT_LSTAT64;
1484+
RTSAN_MAYBE_INTERCEPT_FSTAT64;
14401485
INTERCEPT_FUNCTION(fopen);
14411486
RTSAN_MAYBE_INTERCEPT_FOPEN64;
14421487
RTSAN_MAYBE_INTERCEPT_FREOPEN64;

compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ TEST_F(RtsanFileTest, FcntlFlockDiesWhenRealtime) {
401401
ASSERT_THAT(fd, Ne(-1));
402402

403403
auto Func = [fd]() {
404-
struct flock lock {};
404+
struct flock lock{};
405405
lock.l_type = F_RDLCK;
406406
lock.l_whence = SEEK_SET;
407407
lock.l_start = 0;
@@ -735,7 +735,7 @@ TEST(TestRtsanInterceptors, IoctlBehavesWithOutputPointer) {
735735
GTEST_SKIP();
736736
}
737737

738-
struct ifreq ifr {};
738+
struct ifreq ifr{};
739739
strncpy(ifr.ifr_name, ifaddr->ifa_name, IFNAMSIZ - 1);
740740

741741
int retval = ioctl(sock, SIOCGIFADDR, &ifr);
@@ -875,6 +875,33 @@ TEST_F(RtsanOpenedFileTest, UnlinkatDiesWhenRealtime) {
875875
ExpectNonRealtimeSurvival(Func);
876876
}
877877

878+
TEST_F(RtsanOpenedFileTest, StatDiesWhenRealtime) {
879+
auto Func = [&]() {
880+
struct stat s{};
881+
stat(GetTemporaryFilePath(), &s);
882+
};
883+
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("stat"));
884+
ExpectNonRealtimeSurvival(Func);
885+
}
886+
887+
TEST_F(RtsanOpenedFileTest, LstatDiesWhenRealtime) {
888+
auto Func = [&]() {
889+
struct stat s{};
890+
lstat(GetTemporaryFilePath(), &s);
891+
};
892+
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("lstat"));
893+
ExpectNonRealtimeSurvival(Func);
894+
}
895+
896+
TEST_F(RtsanOpenedFileTest, FstatDiesWhenRealtime) {
897+
auto Func = [&]() {
898+
struct stat s{};
899+
fstat(GetOpenFd(), &s);
900+
};
901+
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fstat"));
902+
ExpectNonRealtimeSurvival(Func);
903+
}
904+
878905
TEST_F(RtsanFileTest, FcloseDiesWhenRealtime) {
879906
FILE *f = fopen(GetTemporaryFilePath(), "w");
880907
EXPECT_THAT(f, Ne(nullptr));

0 commit comments

Comments
 (0)