[compiler-rt] 7004d68 - [compiler-rt][rtsan] adding setlinebuf/setbuffer interception. (#122018)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 8 05:22:13 PST 2025
Author: David CARLIER
Date: 2025-01-08T13:22:09Z
New Revision: 7004d6815b3a0c6d9c15a19b6927746a97564ba7
URL: https://github.com/llvm/llvm-project/commit/7004d6815b3a0c6d9c15a19b6927746a97564ba7
DIFF: https://github.com/llvm/llvm-project/commit/7004d6815b3a0c6d9c15a19b6927746a97564ba7.diff
LOG: [compiler-rt][rtsan] adding setlinebuf/setbuffer interception. (#122018)
catering to platform differences as those calls are not posix.
Added:
Modified:
compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
Removed:
################################################################################
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index 7ec0382b585660..6a5f4b91d11d7e 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -347,11 +347,33 @@ INTERCEPTOR(int, setvbuf, FILE *stream, char *buf, int mode, size_t size) {
__rtsan_notify_intercepted_call("setvbuf");
return REAL(setvbuf)(stream, buf, mode, size);
}
+
+#if SANITIZER_LINUX
+INTERCEPTOR(void, setlinebuf, FILE *stream) {
+#else
+INTERCEPTOR(int, setlinebuf, FILE *stream) {
+#endif
+ __rtsan_notify_intercepted_call("setlinebuf");
+ return REAL(setlinebuf)(stream);
+}
+
+#if SANITIZER_LINUX
+INTERCEPTOR(void, setbuffer, FILE *stream, char *buf, size_t size) {
+#else
+INTERCEPTOR(void, setbuffer, FILE *stream, char *buf, int size) {
+#endif
+ __rtsan_notify_intercepted_call("setbuffer");
+ return REAL(setbuffer)(stream, buf, size);
+}
#define RTSAN_MAYBE_INTERCEPT_SETBUF INTERCEPT_FUNCTION(setbuf)
#define RTSAN_MAYBE_INTERCEPT_SETVBUF INTERCEPT_FUNCTION(setvbuf)
+#define RTSAN_MAYBE_INTERCEPT_SETLINEBUF INTERCEPT_FUNCTION(setlinebuf)
+#define RTSAN_MAYBE_INTERCEPT_SETBUFFER INTERCEPT_FUNCTION(setbuffer)
#else
#define RTSAN_MAYBE_INTERCEPT_SETBUF
#define RTSAN_MAYBE_INTERCEPT_SETVBUF
+#define RTSAN_MAYBE_INTERCEPT_SETLINEBUF
+#define RTSAN_MAYBE_INTERCEPT_SETBUFFER
#endif
INTERCEPTOR(int, puts, const char *s) {
@@ -1018,6 +1040,8 @@ void __rtsan::InitializeInterceptors() {
RTSAN_MAYBE_INTERCEPT_FMEMOPEN;
RTSAN_MAYBE_INTERCEPT_SETBUF;
RTSAN_MAYBE_INTERCEPT_SETVBUF;
+ RTSAN_MAYBE_INTERCEPT_SETLINEBUF;
+ RTSAN_MAYBE_INTERCEPT_SETBUFFER;
INTERCEPT_FUNCTION(lseek);
RTSAN_MAYBE_INTERCEPT_LSEEK64;
INTERCEPT_FUNCTION(dup);
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 db0ec951ad10c7..5488d3c7e2056c 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -409,7 +409,7 @@ TEST_F(RtsanFileTest, SetbufDieWhenRealtime) {
FILE *f = fopen(GetTemporaryFilePath(), "w");
EXPECT_THAT(f, Ne(nullptr));
- auto Func = [&f, &buffer]() { setbuf(f, buffer); };
+ auto Func = [f, &buffer]() { setbuf(f, buffer); };
ExpectRealtimeDeath(Func, "setbuf");
ExpectNonRealtimeSurvival(Func);
@@ -421,7 +421,7 @@ TEST_F(RtsanFileTest, SetvbufDieWhenRealtime) {
FILE *f = fopen(GetTemporaryFilePath(), "w");
EXPECT_THAT(f, Ne(nullptr));
- auto Func = [&f, &buffer, &size]() {
+ auto Func = [f, &buffer, size]() {
int r = setvbuf(f, buffer, _IOFBF, size);
EXPECT_THAT(r, Eq(0));
};
@@ -429,6 +429,28 @@ TEST_F(RtsanFileTest, SetvbufDieWhenRealtime) {
ExpectRealtimeDeath(Func, "setvbuf");
ExpectNonRealtimeSurvival(Func);
}
+
+TEST_F(RtsanFileTest, SetlinebufDieWhenRealtime) {
+ FILE *f = fopen(GetTemporaryFilePath(), "w");
+ EXPECT_THAT(f, Ne(nullptr));
+
+ auto Func = [f]() { setlinebuf(f); };
+
+ ExpectRealtimeDeath(Func, "setlinebuf");
+ ExpectNonRealtimeSurvival(Func);
+}
+
+TEST_F(RtsanFileTest, SetbufferDieWhenRealtime) {
+ char buffer[1024];
+ size_t size = sizeof(buffer);
+ FILE *f = fopen(GetTemporaryFilePath(), "w");
+ EXPECT_THAT(f, Ne(nullptr));
+
+ auto Func = [f, &buffer, size]() { setbuffer(f, buffer, size); };
+
+ ExpectRealtimeDeath(Func, "setbuffer");
+ ExpectNonRealtimeSurvival(Func);
+}
#endif
class RtsanOpenedFileTest : public RtsanFileTest {
More information about the llvm-commits
mailing list