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

Conversation

devnexen
Copy link
Member

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Mar 24, 2025

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: David CARLIER (devnexen)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/132709.diff

2 Files Affected:

  • (modified) compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp (+28)
  • (modified) compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp (+25)
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index b139f628703f9..9d1a689a5a070 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -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);
 
diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
index 321cc4f0c78f1..f12df9ea90855 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -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) {

@devnexen devnexen merged commit 8a8e4cf into llvm:main Mar 24, 2025
13 checks passed
@devnexen devnexen deleted the rtsan_timerfd branch March 24, 2025 11:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants