diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp index 9f89ab6bf1fc7..f1fe20b255d9c 100644 --- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp +++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp @@ -292,6 +292,18 @@ INTERCEPTOR(int, fputs, const char *s, FILE *stream) { return REAL(fputs)(s, stream); } +INTERCEPTOR(int, fflush, FILE *stream) { + __rtsan_notify_intercepted_call("fflush"); + return REAL(fflush)(stream); +} + +#if SANITIZER_APPLE +INTERCEPTOR(int, fpurge, FILE *stream) { + __rtsan_notify_intercepted_call("fpurge"); + return REAL(fpurge)(stream); +} +#endif + INTERCEPTOR(FILE *, fdopen, int fd, const char *mode) { __rtsan_notify_intercepted_call("fdopen"); return REAL(fdopen)(fd, mode); @@ -981,6 +993,7 @@ void __rtsan::InitializeInterceptors() { RTSAN_MAYBE_INTERCEPT_CREAT64; INTERCEPT_FUNCTION(puts); INTERCEPT_FUNCTION(fputs); + INTERCEPT_FUNCTION(fflush); INTERCEPT_FUNCTION(fdopen); INTERCEPT_FUNCTION(freopen); RTSAN_MAYBE_INTERCEPT_FOPENCOOKIE; 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 5adbf0fb63de8..15dfc1af01625 100644 --- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp +++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp @@ -604,6 +604,34 @@ TEST_F(RtsanOpenedFileTest, FputsDiesWhenRealtime) { ExpectNonRealtimeSurvival(Func); } +TEST_F(RtsanFileTest, FflushDiesWhenRealtime) { + FILE *f = fopen(GetTemporaryFilePath(), "w"); + EXPECT_THAT(f, Ne(nullptr)); + int written = fwrite("abc", 1, 3, f); + EXPECT_THAT(written, Eq(3)); + auto Func = [&f]() { + int res = fflush(f); + EXPECT_THAT(res, Eq(0)); + }; + ExpectRealtimeDeath(Func, "fflush"); + ExpectNonRealtimeSurvival(Func); +} + +#if SANITIZER_APPLE +TEST_F(RtsanFileTest, FpurgeDiesWhenRealtime) { + FILE *f = fopen(GetTemporaryFilePath(), "w"); + EXPECT_THAT(f, Ne(nullptr)); + int written = fwrite("abc", 1, 3, f); + EXPECT_THAT(written, Eq(3)); + auto Func = [&f]() { + int res = fpurge(f); + EXPECT_THAT(res, Eq(0)); + }; + ExpectRealtimeDeath(Func, "fpurge"); + ExpectNonRealtimeSurvival(Func); +} +#endif + TEST_F(RtsanOpenedFileTest, ReadDiesWhenRealtime) { auto Func = [this]() { char c{};