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

This commit is contained in:
David CARLIER
2025-03-24 11:47:43 +00:00
committed by GitHub
parent 9ab3b6a006
commit 8a8e4cfc0b
2 changed files with 53 additions and 0 deletions

View File

@@ -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]) {
@@ -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);

View File

@@ -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>
@@ -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) {