[compiler-rt] [compiler-rt][rtsan] intercept fflush. (PR #121643)

David CARLIER via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 4 07:37:01 PST 2025


https://github.com/devnexen updated https://github.com/llvm/llvm-project/pull/121643

>From 7802b20b55d644592c5c7e978f194b709fb13b2d Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Sat, 4 Jan 2025 12:00:59 +0000
Subject: [PATCH 1/2] [compiler-rt][rtsan] intercept fflush.

---
 compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp  |  6 ++++++
 .../rtsan/tests/rtsan_test_interceptors_posix.cpp   | 13 +++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index 9f89ab6bf1fc7d..73e87c4db84f96 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -292,6 +292,11 @@ 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);
+}
+
 INTERCEPTOR(FILE *, fdopen, int fd, const char *mode) {
   __rtsan_notify_intercepted_call("fdopen");
   return REAL(fdopen)(fd, mode);
@@ -981,6 +986,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 5adbf0fb63de80..569b77806a9254 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,19 @@ TEST_F(RtsanOpenedFileTest, FputsDiesWhenRealtime) {
   ExpectNonRealtimeSurvival(Func);
 }
 
+TEST_F(RtsanFileTest, FflushDiesWhenRealtime) {
+  FILE *f = fopen(GetTemporaryFilePath(), "w");
+  EXPECT_THAT(f, Ne(nullptr));
+  int r = fwrite("abc", 1, 3, f);
+  EXPECT_THAT(r, Eq(3));
+  auto Func = [&f]() {
+    int r = fflush(f);
+    EXPECT_THAT(r, Eq(0));
+  };
+  ExpectRealtimeDeath(Func, "fflush");
+  ExpectNonRealtimeSurvival(Func);
+}
+
 TEST_F(RtsanOpenedFileTest, ReadDiesWhenRealtime) {
   auto Func = [this]() {
     char c{};

>From ba155fe85ac70ed8a2498553a929d91ddf177e6e Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Sat, 4 Jan 2025 15:36:44 +0000
Subject: [PATCH 2/2] adding fpurge

---
 .../lib/rtsan/rtsan_interceptors_posix.cpp    |  7 ++++++
 .../tests/rtsan_test_interceptors_posix.cpp   | 23 +++++++++++++++----
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index 73e87c4db84f96..f1fe20b255d9c9 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -297,6 +297,13 @@ INTERCEPTOR(int, fflush, FILE *stream) {
   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);
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 569b77806a9254..15dfc1af016251 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -607,16 +607,31 @@ TEST_F(RtsanOpenedFileTest, FputsDiesWhenRealtime) {
 TEST_F(RtsanFileTest, FflushDiesWhenRealtime) {
   FILE *f = fopen(GetTemporaryFilePath(), "w");
   EXPECT_THAT(f, Ne(nullptr));
-  int r = fwrite("abc", 1, 3, f);
-  EXPECT_THAT(r, Eq(3));
+  int written = fwrite("abc", 1, 3, f);
+  EXPECT_THAT(written, Eq(3));
   auto Func = [&f]() {
-    int r = fflush(f);
-    EXPECT_THAT(r, Eq(0));
+    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{};



More information about the llvm-commits mailing list