Skip to content

Commit 8a8e4cf

Browse files
authored
[compiler-rt][rtsan] Linux timerfd api interception. (#132709)
1 parent 9ab3b6a commit 8a8e4cf

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,17 +1343,41 @@ INTERCEPTOR(int, inotify_rm_watch, int fd, int wd) {
13431343
__rtsan_notify_intercepted_call("inotify_rm_watch");
13441344
return REAL(inotify_rm_watch)(fd, wd);
13451345
}
1346+
1347+
INTERCEPTOR(int, timerfd_create, int clockid, int flags) {
1348+
__rtsan_notify_intercepted_call("timerfd_create");
1349+
return REAL(timerfd_create)(clockid, flags);
1350+
}
1351+
1352+
INTERCEPTOR(int, timerfd_settime, int fd, int flags, const itimerspec *newval,
1353+
struct itimerspec *oldval) {
1354+
__rtsan_notify_intercepted_call("timerfd_settime");
1355+
return REAL(timerfd_settime)(fd, flags, newval, oldval);
1356+
}
1357+
1358+
INTERCEPTOR(int, timerfd_gettime, int fd, struct itimerspec *val) {
1359+
__rtsan_notify_intercepted_call("timerfd_gettime");
1360+
return REAL(timerfd_gettime)(fd, val);
1361+
}
13461362
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT INTERCEPT_FUNCTION(inotify_init)
13471363
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT1 INTERCEPT_FUNCTION(inotify_init1)
13481364
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_ADD_WATCH \
13491365
INTERCEPT_FUNCTION(inotify_add_watch)
13501366
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_RM_WATCH \
13511367
INTERCEPT_FUNCTION(inotify_rm_watch)
1368+
#define RTSAN_MAYBE_INTERCEPT_TIMERFD_CREATE INTERCEPT_FUNCTION(timerfd_create)
1369+
#define RTSAN_MAYBE_INTERCEPT_TIMERFD_SETTIME \
1370+
INTERCEPT_FUNCTION(timerfd_settime)
1371+
#define RTSAN_MAYBE_INTERCEPT_TIMERFD_GETTIME \
1372+
INTERCEPT_FUNCTION(timerfd_gettime)
13521373
#else
13531374
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT
13541375
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT1
13551376
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_ADD_WATCH
13561377
#define RTSAN_MAYBE_INTERCEPT_INOTIFY_RM_WATCH
1378+
#define RTSAN_MAYBE_INTERCEPT_TIMERFD_CREATE
1379+
#define RTSAN_MAYBE_INTERCEPT_TIMERFD_SETTIME
1380+
#define RTSAN_MAYBE_INTERCEPT_TIMERFD_GETTIME
13571381
#endif
13581382

13591383
INTERCEPTOR(int, pipe, int pipefd[2]) {
@@ -1617,6 +1641,10 @@ void __rtsan::InitializeInterceptors() {
16171641
RTSAN_MAYBE_INTERCEPT_INOTIFY_ADD_WATCH;
16181642
RTSAN_MAYBE_INTERCEPT_INOTIFY_RM_WATCH;
16191643

1644+
RTSAN_MAYBE_INTERCEPT_TIMERFD_CREATE;
1645+
RTSAN_MAYBE_INTERCEPT_TIMERFD_SETTIME;
1646+
RTSAN_MAYBE_INTERCEPT_TIMERFD_GETTIME;
1647+
16201648
INTERCEPT_FUNCTION(pipe);
16211649
INTERCEPT_FUNCTION(mkfifo);
16221650

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <stdio.h>
4646
#if SANITIZER_LINUX
4747
#include <sys/inotify.h>
48+
#include <sys/timerfd.h>
4849
#endif
4950
#include <sys/ioctl.h>
5051
#include <sys/mman.h>
@@ -1652,6 +1653,30 @@ TEST(TestRtsanInterceptors, InotifyRmWatchDiesWhenRealtime) {
16521653
ExpectRealtimeDeath(Func, "inotify_rm_watch");
16531654
ExpectNonRealtimeSurvival(Func);
16541655
}
1656+
1657+
TEST(TestRtsanInterceptors, TimerfdCreateDiesWhenRealtime) {
1658+
auto Func = []() { timerfd_create(CLOCK_MONOTONIC, 0); };
1659+
ExpectRealtimeDeath(Func, "timerfd_create");
1660+
ExpectNonRealtimeSurvival(Func);
1661+
}
1662+
1663+
TEST(TestRtsanInterceptors, TimerfdSettimeDiesWhenRealtime) {
1664+
int fd = timerfd_create(CLOCK_MONOTONIC, 0);
1665+
EXPECT_THAT(fd, Ne(-1));
1666+
auto ts = itimerspec{{0, 0}, {0, 0}};
1667+
auto Func = [fd, ts]() { timerfd_settime(fd, 0, &ts, NULL); };
1668+
ExpectRealtimeDeath(Func, "timerfd_settime");
1669+
ExpectNonRealtimeSurvival(Func);
1670+
}
1671+
1672+
TEST(TestRtsanInterceptors, TimerfdGettimeDiesWhenRealtime) {
1673+
int fd = timerfd_create(CLOCK_MONOTONIC, 0);
1674+
EXPECT_THAT(fd, Ne(-1));
1675+
itimerspec ts{};
1676+
auto Func = [fd, &ts]() { timerfd_gettime(fd, &ts); };
1677+
ExpectRealtimeDeath(Func, "timerfd_gettime");
1678+
ExpectNonRealtimeSurvival(Func);
1679+
}
16551680
#endif
16561681

16571682
TEST(TestRtsanInterceptors, MkfifoDiesWhenRealtime) {

0 commit comments

Comments
 (0)