[compiler-rt] [compiler-rt][rtsan] inotify api for Linux interception. (PR #124177)
David CARLIER via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 23 11:43:35 PST 2025
https://github.com/devnexen created https://github.com/llvm/llvm-project/pull/124177
None
>From f707391a932167e465aaf0f8bfed4307cd0b3f8e Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Thu, 23 Jan 2025 19:42:47 +0000
Subject: [PATCH] [compiler-rt][rtsan] inotify api for Linux interception.
---
.../lib/rtsan/rtsan_interceptors_posix.cpp | 41 +++++++++++++++++++
.../tests/rtsan_test_interceptors_posix.cpp | 35 ++++++++++++++++
2 files changed, 76 insertions(+)
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index a01354781272d5..9908a9e3d3dc64 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -46,6 +46,9 @@ void OSSpinLockLock(volatile OSSpinLock *__lock);
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
+#if SANITIZER_LINUX
+#include <sys/inotify.h>
+#endif
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
@@ -1130,6 +1133,39 @@ INTERCEPTOR(int, kevent64, int kq, const struct kevent64_s *changelist,
#define RTSAN_MAYBE_INTERCEPT_KEVENT64
#endif // SANITIZER_INTERCEPT_KQUEUE
+#if SANITIZER_LINUX
+INTERCEPTOR(int, inotify_init) {
+ __rtsan_notify_intercepted_call("inotify_init");
+ return REAL(inotify_init)();
+}
+
+INTERCEPTOR(int, inotify_init1, int flags) {
+ __rtsan_notify_intercepted_call("inotify_init1");
+ return REAL(inotify_init1)(flags);
+}
+
+INTERCEPTOR(int, inotify_add_watch, int fd, const char *path, uint32_t mask) {
+ __rtsan_notify_intercepted_call("inotify_add_watch");
+ return REAL(inotify_add_watch)(fd, path, mask);
+}
+
+INTERCEPTOR(int, inotify_rm_watch, int fd, int wd) {
+ __rtsan_notify_intercepted_call("inotify_rm_watch");
+ return REAL(inotify_rm_watch)(fd, wd);
+}
+#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT INTERCEPT_FUNCTION(inotify_init)
+#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT1 INTERCEPT_FUNCTION(inotify_init1)
+#define RTSAN_MAYBE_INTERCEPT_INOTIFY_ADD_WATCH \
+ INTERCEPT_FUNCTION(inotify_add_watch)
+#define RTSAN_MAYBE_INTERCEPT_INOTIFY_RM_WATCH \
+ INTERCEPT_FUNCTION(inotify_rm_watch)
+#else
+#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT
+#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT1
+#define RTSAN_MAYBE_INTERCEPT_INOTIFY_ADD_WATCH
+#define RTSAN_MAYBE_INTERCEPT_INOTIFY_RM_WATCH
+#endif
+
INTERCEPTOR(int, pipe, int pipefd[2]) {
__rtsan_notify_intercepted_call("pipe");
return REAL(pipe)(pipefd);
@@ -1367,6 +1403,11 @@ void __rtsan::InitializeInterceptors() {
RTSAN_MAYBE_INTERCEPT_KEVENT;
RTSAN_MAYBE_INTERCEPT_KEVENT64;
+ RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT;
+ RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT1;
+ RTSAN_MAYBE_INTERCEPT_INOTIFY_ADD_WATCH;
+ RTSAN_MAYBE_INTERCEPT_INOTIFY_RM_WATCH;
+
INTERCEPT_FUNCTION(pipe);
INTERCEPT_FUNCTION(mkfifo);
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 981766c85f965e..a5cf0f31a4ea48 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -43,6 +43,9 @@
#include <poll.h>
#include <pthread.h>
#include <stdio.h>
+#if SANITIZER_LINUX
+#include <sys/inotify.h>
+#endif
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
@@ -1481,6 +1484,38 @@ TEST_F(KqueueTest, Kevent64DiesWhenRealtime) {
}
#endif // SANITIZER_INTERCEPT_KQUEUE
+#if SANITIZER_LINUX
+TEST(TestRtsanInterceptors, InotifyInitDiesWhenRealtime) {
+ auto Func = []() { inotify_init(); };
+ ExpectRealtimeDeath(Func, "inotify_init");
+ ExpectNonRealtimeSurvival(Func);
+}
+
+TEST(TestRtsanInterceptors, InotifyInit1DiesWhenRealtime) {
+ auto Func = []() { inotify_init1(0); };
+ ExpectRealtimeDeath(Func, "inotify_init1");
+ ExpectNonRealtimeSurvival(Func);
+}
+
+TEST(TestRtsanInterceptors, InotifyAddWatchDiesWhenRealtime) {
+ int fd = inotify_init();
+ EXPECT_THAT(fd, Ne(-1));
+ auto Func = [fd]() {
+ inotify_add_watch(fd, "/tmp/rtsan_inotify", IN_CREATE);
+ };
+ ExpectRealtimeDeath(Func, "inotify_add_watch");
+ ExpectNonRealtimeSurvival(Func);
+}
+
+TEST(TestRtsanInterceptors, InotifyRmWatchDiesWhenRealtime) {
+ int fd = inotify_init();
+ EXPECT_THAT(fd, Ne(-1));
+ auto Func = [fd]() { inotify_rm_watch(fd, -1); };
+ ExpectRealtimeDeath(Func, "inotify_rm_watch");
+ ExpectNonRealtimeSurvival(Func);
+}
+#endif
+
TEST(TestRtsanInterceptors, MkfifoDiesWhenRealtime) {
auto Func = []() { mkfifo("/tmp/rtsan_test_fifo", 0); };
ExpectRealtimeDeath(Func, "mkfifo");
More information about the llvm-commits
mailing list