Skip to content

Commit f707391

Browse files
committed
[compiler-rt][rtsan] inotify api for Linux interception.
1 parent 775d0f3 commit f707391

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>
@@ -1130,6 +1133,39 @@ INTERCEPTOR(int, kevent64, int kq, const struct kevent64_s *changelist,
11301133
#define RTSAN_MAYBE_INTERCEPT_KEVENT64
11311134
#endif // SANITIZER_INTERCEPT_KQUEUE
11321135

1136+
#if SANITIZER_LINUX
1137+
INTERCEPTOR(int, inotify_init) {
1138+
__rtsan_notify_intercepted_call("inotify_init");
1139+
return REAL(inotify_init)();
1140+
}
1141+
1142+
INTERCEPTOR(int, inotify_init1, int flags) {
1143+
__rtsan_notify_intercepted_call("inotify_init1");
1144+
return REAL(inotify_init1)(flags);
1145+
}
1146+
1147+
INTERCEPTOR(int, inotify_add_watch, int fd, const char *path, uint32_t mask) {
1148+
__rtsan_notify_intercepted_call("inotify_add_watch");
1149+
return REAL(inotify_add_watch)(fd, path, mask);
1150+
}
1151+
1152+
INTERCEPTOR(int, inotify_rm_watch, int fd, int wd) {
1153+
__rtsan_notify_intercepted_call("inotify_rm_watch");
1154+
return REAL(inotify_rm_watch)(fd, wd);
1155+
}
1156+
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT INTERCEPT_FUNCTION(inotify_init)
1157+
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT1 INTERCEPT_FUNCTION(inotify_init1)
1158+
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_ADD_WATCH \
1159+
INTERCEPT_FUNCTION(inotify_add_watch)
1160+
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_RM_WATCH \
1161+
INTERCEPT_FUNCTION(inotify_rm_watch)
1162+
#else
1163+
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT
1164+
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT1
1165+
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_ADD_WATCH
1166+
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_RM_WATCH
1167+
#endif
1168+
11331169
INTERCEPTOR(int, pipe, int pipefd[2]) {
11341170
__rtsan_notify_intercepted_call("pipe");
11351171
return REAL(pipe)(pipefd);
@@ -1367,6 +1403,11 @@ void __rtsan::InitializeInterceptors() {
13671403
RTSAN_MAYBE_INTERCEPT_KEVENT;
13681404
RTSAN_MAYBE_INTERCEPT_KEVENT64;
13691405

1406+
RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT;
1407+
RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT1;
1408+
RTSAN_MAYBE_INTERCEPT_INOTIFY_ADD_WATCH;
1409+
RTSAN_MAYBE_INTERCEPT_INOTIFY_RM_WATCH;
1410+
13701411
INTERCEPT_FUNCTION(pipe);
13711412
INTERCEPT_FUNCTION(mkfifo);
13721413

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>
@@ -1481,6 +1484,38 @@ TEST_F(KqueueTest, Kevent64DiesWhenRealtime) {
14811484
}
14821485
#endif // SANITIZER_INTERCEPT_KQUEUE
14831486

1487+
#if SANITIZER_LINUX
1488+
TEST(TestRtsanInterceptors, InotifyInitDiesWhenRealtime) {
1489+
auto Func = []() { inotify_init(); };
1490+
ExpectRealtimeDeath(Func, "inotify_init");
1491+
ExpectNonRealtimeSurvival(Func);
1492+
}
1493+
1494+
TEST(TestRtsanInterceptors, InotifyInit1DiesWhenRealtime) {
1495+
auto Func = []() { inotify_init1(0); };
1496+
ExpectRealtimeDeath(Func, "inotify_init1");
1497+
ExpectNonRealtimeSurvival(Func);
1498+
}
1499+
1500+
TEST(TestRtsanInterceptors, InotifyAddWatchDiesWhenRealtime) {
1501+
int fd = inotify_init();
1502+
EXPECT_THAT(fd, Ne(-1));
1503+
auto Func = [fd]() {
1504+
inotify_add_watch(fd, "/tmp/rtsan_inotify", IN_CREATE);
1505+
};
1506+
ExpectRealtimeDeath(Func, "inotify_add_watch");
1507+
ExpectNonRealtimeSurvival(Func);
1508+
}
1509+
1510+
TEST(TestRtsanInterceptors, InotifyRmWatchDiesWhenRealtime) {
1511+
int fd = inotify_init();
1512+
EXPECT_THAT(fd, Ne(-1));
1513+
auto Func = [fd]() { inotify_rm_watch(fd, -1); };
1514+
ExpectRealtimeDeath(Func, "inotify_rm_watch");
1515+
ExpectNonRealtimeSurvival(Func);
1516+
}
1517+
#endif
1518+
14841519
TEST(TestRtsanInterceptors, MkfifoDiesWhenRealtime) {
14851520
auto Func = []() { mkfifo("/tmp/rtsan_test_fifo", 0); };
14861521
ExpectRealtimeDeath(Func, "mkfifo");

0 commit comments

Comments
 (0)