Skip to content

Commit

Permalink
[rtsan] Add pipe, mkfifo interceptors (llvm#116915)
Browse files Browse the repository at this point in the history
## Why we think this are unsafe

Again, these correspond directly to system calls on linux and OSX. They
are two ways to do interprocess communication so it would make sense
that they take some synchronization by the OS.
  • Loading branch information
cjappl authored Nov 20, 2024
1 parent acc3266 commit fce917d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
24 changes: 24 additions & 0 deletions compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,26 @@ INTERCEPTOR(int, kevent64, int kq, const struct kevent64_s *changelist,
#define RTSAN_MAYBE_INTERCEPT_KEVENT64
#endif // SANITIZER_INTERCEPT_KQUEUE

INTERCEPTOR(int, pipe, int pipefd[2]) {
__rtsan_notify_intercepted_call("pipe");
return REAL(pipe)(pipefd);
}

INTERCEPTOR(int, mkfifo, const char *pathname, mode_t mode) {
__rtsan_notify_intercepted_call("mkfifo");
return REAL(mkfifo)(pathname, mode);
}

// see comment above about -Wunguarded-availability-new
// and why we disable it here
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
INTERCEPTOR(int, mkfifoat, int dirfd, const char *pathname, mode_t mode) {
__rtsan_notify_intercepted_call("mkfifoat");
return REAL(mkfifoat)(dirfd, pathname, mode);
}
#pragma clang diagnostic pop

// Preinit
void __rtsan::InitializeInterceptors() {
INTERCEPT_FUNCTION(calloc);
Expand Down Expand Up @@ -836,6 +856,10 @@ void __rtsan::InitializeInterceptors() {
RTSAN_MAYBE_INTERCEPT_KQUEUE;
RTSAN_MAYBE_INTERCEPT_KEVENT;
RTSAN_MAYBE_INTERCEPT_KEVENT64;

INTERCEPT_FUNCTION(pipe);
INTERCEPT_FUNCTION(mkfifo);
INTERCEPT_FUNCTION(mkfifoat);
}

#endif // SANITIZER_POSIX
28 changes: 28 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 @@ -965,4 +965,32 @@ TEST_F(KqueueTest, Kevent64DiesWhenRealtime) {
}
#endif // SANITIZER_INTERCEPT_KQUEUE

TEST(TestRtsanInterceptors, MkfifoDiesWhenRealtime) {
auto Func = []() { mkfifo("/tmp/rtsan_test_fifo", 0); };
ExpectRealtimeDeath(Func, "mkfifo");
ExpectNonRealtimeSurvival(Func);
}

#if __has_builtin(__builtin_available) && SANITIZER_APPLE
#define MKFIFOAT_AVAILABLE() (__builtin_available(macOS 10.13, *))
#else
// We are going to assume this is true until we hit systems where it isn't
#define MKFIFOAT_AVAILABLE() (true)
#endif

TEST(TestRtsanInterceptors, MkfifoatDiesWhenRealtime) {
if (MKFIFOAT_AVAILABLE()) {
auto Func = []() { mkfifoat(0, "/tmp/rtsan_test_fifo", 0); };
ExpectRealtimeDeath(Func, "mkfifoat");
ExpectNonRealtimeSurvival(Func);
}
}

TEST(TestRtsanInterceptors, PipeDiesWhenRealtime) {
int fds[2];
auto Func = [&fds]() { pipe(fds); };
ExpectRealtimeDeath(Func, "pipe");
ExpectNonRealtimeSurvival(Func);
}

#endif // SANITIZER_POSIX

0 comments on commit fce917d

Please sign in to comment.