Skip to content

Commit f3d2e75

Browse files
authored
[compiler-rt][rtsan] inotify api for Linux interception. (#124177)
1 parent 02a3004 commit f3d2e75

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ void OSSpinLockLock(volatile OSSpinLock *__lock);
4646
#include <pthread.h>
4747
#include <stdarg.h>
4848
#include <stdio.h>
49+
#if SANITIZER_LINUX
50+
#include <sys/inotify.h>
51+
#endif
4952
#include <sys/select.h>
5053
#include <sys/socket.h>
5154
#include <sys/stat.h>
@@ -1174,6 +1177,39 @@ INTERCEPTOR(int, kevent64, int kq, const struct kevent64_s *changelist,
11741177
#define RTSAN_MAYBE_INTERCEPT_KEVENT64
11751178
#endif // SANITIZER_INTERCEPT_KQUEUE
11761179

1180+
#if SANITIZER_LINUX
1181+
INTERCEPTOR(int, inotify_init) {
1182+
__rtsan_notify_intercepted_call("inotify_init");
1183+
return REAL(inotify_init)();
1184+
}
1185+
1186+
INTERCEPTOR(int, inotify_init1, int flags) {
1187+
__rtsan_notify_intercepted_call("inotify_init1");
1188+
return REAL(inotify_init1)(flags);
1189+
}
1190+
1191+
INTERCEPTOR(int, inotify_add_watch, int fd, const char *path, uint32_t mask) {
1192+
__rtsan_notify_intercepted_call("inotify_add_watch");
1193+
return REAL(inotify_add_watch)(fd, path, mask);
1194+
}
1195+
1196+
INTERCEPTOR(int, inotify_rm_watch, int fd, int wd) {
1197+
__rtsan_notify_intercepted_call("inotify_rm_watch");
1198+
return REAL(inotify_rm_watch)(fd, wd);
1199+
}
1200+
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT INTERCEPT_FUNCTION(inotify_init)
1201+
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT1 INTERCEPT_FUNCTION(inotify_init1)
1202+
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_ADD_WATCH \
1203+
INTERCEPT_FUNCTION(inotify_add_watch)
1204+
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_RM_WATCH \
1205+
INTERCEPT_FUNCTION(inotify_rm_watch)
1206+
#else
1207+
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT
1208+
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT1
1209+
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_ADD_WATCH
1210+
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_RM_WATCH
1211+
#endif
1212+
11771213
INTERCEPTOR(int, pipe, int pipefd[2]) {
11781214
__rtsan_notify_intercepted_call("pipe");
11791215
return REAL(pipe)(pipefd);
@@ -1415,6 +1451,11 @@ void __rtsan::InitializeInterceptors() {
14151451
RTSAN_MAYBE_INTERCEPT_KEVENT;
14161452
RTSAN_MAYBE_INTERCEPT_KEVENT64;
14171453

1454+
RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT;
1455+
RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT1;
1456+
RTSAN_MAYBE_INTERCEPT_INOTIFY_ADD_WATCH;
1457+
RTSAN_MAYBE_INTERCEPT_INOTIFY_RM_WATCH;
1458+
14181459
INTERCEPT_FUNCTION(pipe);
14191460
INTERCEPT_FUNCTION(mkfifo);
14201461

compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
#include <poll.h>
4444
#include <pthread.h>
4545
#include <stdio.h>
46+
#if SANITIZER_LINUX
47+
#include <sys/inotify.h>
48+
#endif
4649
#include <sys/ioctl.h>
4750
#include <sys/mman.h>
4851
#include <sys/socket.h>
@@ -1505,6 +1508,38 @@ TEST_F(KqueueTest, Kevent64DiesWhenRealtime) {
15051508
}
15061509
#endif // SANITIZER_INTERCEPT_KQUEUE
15071510

1511+
#if SANITIZER_LINUX
1512+
TEST(TestRtsanInterceptors, InotifyInitDiesWhenRealtime) {
1513+
auto Func = []() { inotify_init(); };
1514+
ExpectRealtimeDeath(Func, "inotify_init");
1515+
ExpectNonRealtimeSurvival(Func);
1516+
}
1517+
1518+
TEST(TestRtsanInterceptors, InotifyInit1DiesWhenRealtime) {
1519+
auto Func = []() { inotify_init1(0); };
1520+
ExpectRealtimeDeath(Func, "inotify_init1");
1521+
ExpectNonRealtimeSurvival(Func);
1522+
}
1523+
1524+
TEST(TestRtsanInterceptors, InotifyAddWatchDiesWhenRealtime) {
1525+
int fd = inotify_init();
1526+
EXPECT_THAT(fd, Ne(-1));
1527+
auto Func = [fd]() {
1528+
inotify_add_watch(fd, "/tmp/rtsan_inotify", IN_CREATE);
1529+
};
1530+
ExpectRealtimeDeath(Func, "inotify_add_watch");
1531+
ExpectNonRealtimeSurvival(Func);
1532+
}
1533+
1534+
TEST(TestRtsanInterceptors, InotifyRmWatchDiesWhenRealtime) {
1535+
int fd = inotify_init();
1536+
EXPECT_THAT(fd, Ne(-1));
1537+
auto Func = [fd]() { inotify_rm_watch(fd, -1); };
1538+
ExpectRealtimeDeath(Func, "inotify_rm_watch");
1539+
ExpectNonRealtimeSurvival(Func);
1540+
}
1541+
#endif
1542+
15081543
TEST(TestRtsanInterceptors, MkfifoDiesWhenRealtime) {
15091544
auto Func = []() { mkfifo("/tmp/rtsan_test_fifo", 0); };
15101545
ExpectRealtimeDeath(Func, "mkfifo");

0 commit comments

Comments
 (0)