Skip to content

Commit 59354a8

Browse files
authored
[compiler-rt][rtsan] intercept fflush. (#121643)
1 parent 1fa0036 commit 59354a8

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,18 @@ INTERCEPTOR(int, fputs, const char *s, FILE *stream) {
292292
return REAL(fputs)(s, stream);
293293
}
294294

295+
INTERCEPTOR(int, fflush, FILE *stream) {
296+
__rtsan_notify_intercepted_call("fflush");
297+
return REAL(fflush)(stream);
298+
}
299+
300+
#if SANITIZER_APPLE
301+
INTERCEPTOR(int, fpurge, FILE *stream) {
302+
__rtsan_notify_intercepted_call("fpurge");
303+
return REAL(fpurge)(stream);
304+
}
305+
#endif
306+
295307
INTERCEPTOR(FILE *, fdopen, int fd, const char *mode) {
296308
__rtsan_notify_intercepted_call("fdopen");
297309
return REAL(fdopen)(fd, mode);
@@ -981,6 +993,7 @@ void __rtsan::InitializeInterceptors() {
981993
RTSAN_MAYBE_INTERCEPT_CREAT64;
982994
INTERCEPT_FUNCTION(puts);
983995
INTERCEPT_FUNCTION(fputs);
996+
INTERCEPT_FUNCTION(fflush);
984997
INTERCEPT_FUNCTION(fdopen);
985998
INTERCEPT_FUNCTION(freopen);
986999
RTSAN_MAYBE_INTERCEPT_FOPENCOOKIE;

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,34 @@ TEST_F(RtsanOpenedFileTest, FputsDiesWhenRealtime) {
604604
ExpectNonRealtimeSurvival(Func);
605605
}
606606

607+
TEST_F(RtsanFileTest, FflushDiesWhenRealtime) {
608+
FILE *f = fopen(GetTemporaryFilePath(), "w");
609+
EXPECT_THAT(f, Ne(nullptr));
610+
int written = fwrite("abc", 1, 3, f);
611+
EXPECT_THAT(written, Eq(3));
612+
auto Func = [&f]() {
613+
int res = fflush(f);
614+
EXPECT_THAT(res, Eq(0));
615+
};
616+
ExpectRealtimeDeath(Func, "fflush");
617+
ExpectNonRealtimeSurvival(Func);
618+
}
619+
620+
#if SANITIZER_APPLE
621+
TEST_F(RtsanFileTest, FpurgeDiesWhenRealtime) {
622+
FILE *f = fopen(GetTemporaryFilePath(), "w");
623+
EXPECT_THAT(f, Ne(nullptr));
624+
int written = fwrite("abc", 1, 3, f);
625+
EXPECT_THAT(written, Eq(3));
626+
auto Func = [&f]() {
627+
int res = fpurge(f);
628+
EXPECT_THAT(res, Eq(0));
629+
};
630+
ExpectRealtimeDeath(Func, "fpurge");
631+
ExpectNonRealtimeSurvival(Func);
632+
}
633+
#endif
634+
607635
TEST_F(RtsanOpenedFileTest, ReadDiesWhenRealtime) {
608636
auto Func = [this]() {
609637
char c{};

0 commit comments

Comments
 (0)