Skip to content

[compiler-rt][rtsan] Linux timerfd api interception. #132709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1343,17 +1343,41 @@ INTERCEPTOR(int, inotify_rm_watch, int fd, int wd) {
__rtsan_notify_intercepted_call("inotify_rm_watch");
return REAL(inotify_rm_watch)(fd, wd);
}

INTERCEPTOR(int, timerfd_create, int clockid, int flags) {
__rtsan_notify_intercepted_call("timerfd_create");
return REAL(timerfd_create)(clockid, flags);
}

INTERCEPTOR(int, timerfd_settime, int fd, int flags, const itimerspec *newval,
struct itimerspec *oldval) {
__rtsan_notify_intercepted_call("timerfd_settime");
return REAL(timerfd_settime)(fd, flags, newval, oldval);
}

INTERCEPTOR(int, timerfd_gettime, int fd, struct itimerspec *val) {
__rtsan_notify_intercepted_call("timerfd_gettime");
return REAL(timerfd_gettime)(fd, val);
}
#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)
#define RTSAN_MAYBE_INTERCEPT_TIMERFD_CREATE INTERCEPT_FUNCTION(timerfd_create)
#define RTSAN_MAYBE_INTERCEPT_TIMERFD_SETTIME \
INTERCEPT_FUNCTION(timerfd_settime)
#define RTSAN_MAYBE_INTERCEPT_TIMERFD_GETTIME \
INTERCEPT_FUNCTION(timerfd_gettime)
#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
#define RTSAN_MAYBE_INTERCEPT_TIMERFD_CREATE
#define RTSAN_MAYBE_INTERCEPT_TIMERFD_SETTIME
#define RTSAN_MAYBE_INTERCEPT_TIMERFD_GETTIME
#endif

INTERCEPTOR(int, pipe, int pipefd[2]) {
Expand Down Expand Up @@ -1617,6 +1641,10 @@ void __rtsan::InitializeInterceptors() {
RTSAN_MAYBE_INTERCEPT_INOTIFY_ADD_WATCH;
RTSAN_MAYBE_INTERCEPT_INOTIFY_RM_WATCH;

RTSAN_MAYBE_INTERCEPT_TIMERFD_CREATE;
RTSAN_MAYBE_INTERCEPT_TIMERFD_SETTIME;
RTSAN_MAYBE_INTERCEPT_TIMERFD_GETTIME;

INTERCEPT_FUNCTION(pipe);
INTERCEPT_FUNCTION(mkfifo);

Expand Down
25 changes: 25 additions & 0 deletions compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <stdio.h>
#if SANITIZER_LINUX
#include <sys/inotify.h>
#include <sys/timerfd.h>
#endif
#include <sys/ioctl.h>
#include <sys/mman.h>
Expand Down Expand Up @@ -1652,6 +1653,30 @@ TEST(TestRtsanInterceptors, InotifyRmWatchDiesWhenRealtime) {
ExpectRealtimeDeath(Func, "inotify_rm_watch");
ExpectNonRealtimeSurvival(Func);
}

TEST(TestRtsanInterceptors, TimerfdCreateDiesWhenRealtime) {
auto Func = []() { timerfd_create(CLOCK_MONOTONIC, 0); };
ExpectRealtimeDeath(Func, "timerfd_create");
ExpectNonRealtimeSurvival(Func);
}

TEST(TestRtsanInterceptors, TimerfdSettimeDiesWhenRealtime) {
int fd = timerfd_create(CLOCK_MONOTONIC, 0);
EXPECT_THAT(fd, Ne(-1));
auto ts = itimerspec{{0, 0}, {0, 0}};
auto Func = [fd, ts]() { timerfd_settime(fd, 0, &ts, NULL); };
ExpectRealtimeDeath(Func, "timerfd_settime");
ExpectNonRealtimeSurvival(Func);
}

TEST(TestRtsanInterceptors, TimerfdGettimeDiesWhenRealtime) {
int fd = timerfd_create(CLOCK_MONOTONIC, 0);
EXPECT_THAT(fd, Ne(-1));
itimerspec ts{};
auto Func = [fd, &ts]() { timerfd_gettime(fd, &ts); };
ExpectRealtimeDeath(Func, "timerfd_gettime");
ExpectNonRealtimeSurvival(Func);
}
#endif

TEST(TestRtsanInterceptors, MkfifoDiesWhenRealtime) {
Expand Down