[compiler-rt] [compiler-rt][rtsan] fseek api interception. (PR #122163)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 8 11:55:18 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-compiler-rt-sanitizer
Author: David CARLIER (devnexen)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/122163.diff
2 Files Affected:
- (modified) compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp (+59)
- (modified) compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp (+62)
``````````diff
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index 6a5f4b91d11d7e..6b7a6d15f05b64 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -376,6 +376,58 @@ INTERCEPTOR(void, setbuffer, FILE *stream, char *buf, int size) {
#define RTSAN_MAYBE_INTERCEPT_SETBUFFER
#endif
+#if SANITIZER_INTERCEPT_FSEEK
+INTERCEPTOR(int, fgetpos, FILE *stream, fpos_t *pos) {
+ __rtsan_notify_intercepted_call("fgetpos");
+ return REAL(fgetpos)(stream, pos);
+}
+
+INTERCEPTOR(int, fseek, FILE *stream, long offset, int whence) {
+ __rtsan_notify_intercepted_call("fseek");
+ return REAL(fgetpos)(stream, offset, whence);
+}
+
+INTERCEPTOR(int, fseeko, FILE *stream, long offset, int whence) {
+ __rtsan_notify_intercepted_call("fseeko");
+ return REAL(fgetpos)(stream, offset, whence);
+}
+
+INTERCEPTOR(int, fsetpos, FILE *stream, const fpos_t pos) {
+ __rtsan_notify_intercepted_call("fsetpos");
+ return REAL(fsetpos)(stream, pos);
+}
+
+INTERCEPTOR(long, ftell, FILE *stream) {
+ __rtsan_notify_intercepted_call("ftell");
+ return REAL(ftell)(stream);
+}
+
+INTERCEPTOR(off_t, ftello, FILE *stream) {
+ __rtsan_notify_intercepted_call("ftello");
+ return REAL(ftello)(stream);
+}
+
+INTERCEPTOR(void, rewind, FILE *stream) {
+ __rtsan_notify_intercepted_call("rewind");
+ return REAL(rewind)(stream);
+}
+#define RTSAN_MAYBE_INTERCEPT_FGETPOS INTERCEPT_FUNCTION(fgetpos)
+#define RTSAN_MAYBE_INTERCEPT_FSEEK INTERCEPT_FUNCTION(fseek)
+#define RTSAN_MAYBE_INTERCEPT_FSEEKO INTERCEPT_FUNCTION(fseeko)
+#define RTSAN_MAYBE_INTERCEPT_FSETPOS INTERCEPT_FUNCTION(fsetpos)
+#define RTSAN_MAYBE_INTERCEPT_FTELL INTERCEPT_FUNCTION(ftell)
+#define RTSAN_MAYBE_INTERCEPT_FTELLO INTERCEPT_FUNCTION(ftello)
+#define RTSAN_MAYBE_INTERCEPT_REWIND INTERCEPT_FUNCTION(rewind)
+#else
+#define RTSAN_MAYBE_INTERCEPT_FGETPOS
+#define RTSAN_MAYBE_INTERCEPT_FSEEK
+#define RTSAN_MAYBE_INTERCEPT_FSEEKO
+#define RTSAN_MAYBE_INTERCEPT_FSETPOS
+#define RTSAN_MAYBE_INTERCEPT_FTELL
+#define RTSAN_MAYBE_INTERCEPT_FTELLO
+#define RTSAN_MAYBE_INTERCEPT_REWIND
+#endif
+
INTERCEPTOR(int, puts, const char *s) {
__rtsan_notify_intercepted_call("puts");
return REAL(puts)(s);
@@ -1042,6 +1094,13 @@ void __rtsan::InitializeInterceptors() {
RTSAN_MAYBE_INTERCEPT_SETVBUF;
RTSAN_MAYBE_INTERCEPT_SETLINEBUF;
RTSAN_MAYBE_INTERCEPT_SETBUFFER;
+ RTSAN_MAYBE_INTERCEPT_FGETPOS;
+ RTSAN_MAYBE_INTERCEPT_FSEEK;
+ RTSAN_MAYBE_INTERCEPT_FSEEKO;
+ RTSAN_MAYBE_INTERCEPT_FSETPOS;
+ RTSAN_MAYBE_INTERCEPT_FTELL;
+ RTSAN_MAYBE_INTERCEPT_FTELLO;
+ RTSAN_MAYBE_INTERCEPT_REWIND;
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 5488d3c7e2056c..f27ff1f7a350f0 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -453,6 +453,68 @@ TEST_F(RtsanFileTest, SetbufferDieWhenRealtime) {
}
#endif
+#if SANITIZER_INTERCEPT_FSEEK
+TEST_F(RtsanOpenedFileTest, FgetposDieWhenRealtime) {
+ auto Func = [this]() {
+ int pos = fgetpos(GetOpenFile());
+ ASSERT_THAT(pos, Eq(0));
+ };
+
+ ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fgetpos"));
+ ExpectNonRealtimeSurvival(Func);
+}
+
+TEST_F(RtsanOpenedFileTest, FseekDieWhenRealtime) {
+ auto Func = [this]() {
+ int ret = fseek(GetOpenFile(), 0, SEEK_CUR);
+ ASSERT_THAT(ret, Eq(0));
+ };
+
+ ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fseek"));
+ ExpectNonRealtimeSurvival(Func);
+}
+
+TEST_F(RtsanOpenedFileTest, FseekoDieWhenRealtime) {
+ auto Func = [this]() {
+ int ret = fseeko(GetOpenFile(), 0, SEEK_CUR);
+ ASSERT_THAT(ret, Eq(0));
+ };
+
+ ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fseeko"));
+ ExpectNonRealtimeSurvival(Func);
+}
+
+TEST_F(RtsanOpenedFileTest, FtellDieWhenRealtime) {
+ auto Func = [this]() {
+ long ret = ftell(GetOpenFile());
+ ASSERT_THAT(ret, Eq(0));
+ };
+
+ ExpectRealtimeDeath(Func, MAYBE_APPEND_64("ftell"));
+ ExpectNonRealtimeSurvival(Func);
+}
+
+TEST_F(RtsanOpenedFileTest, FtelloDieWhenRealtime) {
+ auto Func = [this]() {
+ off_t ret = ftello(GetOpenFile());
+ ASSERT_THAT(ret, Eq(0));
+ };
+
+ ExpectRealtimeDeath(Func, MAYBE_APPEND_64("ftello"));
+ ExpectNonRealtimeSurvival(Func);
+}
+
+TEST_F(RtsanOpenedFileTest, RewindDieWhenRealtime) {
+ int end = fseek(GetOpenFile(), 0, SEEK_END);
+ auto Func = [this]() {
+ rewind(GetOpenFile());
+ };
+
+ ExpectRealtimeDeath(Func, "rewind");
+ ExpectNonRealtimeSurvival(Func);
+}
+#endif
+
class RtsanOpenedFileTest : public RtsanFileTest {
protected:
void SetUp() override {
``````````
</details>
https://github.com/llvm/llvm-project/pull/122163
More information about the llvm-commits
mailing list