[compiler-rt] [compiler-rt][rtsan] intercept accept4 syscall. (PR #117278)

David CARLIER via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 4 20:52:35 PST 2024


https://github.com/devnexen updated https://github.com/llvm/llvm-project/pull/117278

>From 011e9cef0f485cea3f7e979640d0619929383768 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Fri, 22 Nov 2024 02:48:11 +0000
Subject: [PATCH 1/2] [compiler-rt][rtsan] intercept accept4 syscall.

---
 compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index 4f7b534ee17a86..20021e414794a7 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -707,6 +707,14 @@ INTERCEPTOR(int, shutdown, int socket, int how) {
   return REAL(shutdown)(socket, how);
 }
 
+#if SANITIZER_INTERCEPT_ACCEPT4
+INTERCEPTOR(int, accept4, int socket, struct sockaddr *address,
+            socklen_t *address_len, int flags) {
+  __rtsan_notify_intercepted_call("accept4");
+  return REAL(accept4)(socket, address, address_len, flags);
+}
+#endif
+
 // I/O Multiplexing
 
 INTERCEPTOR(int, poll, struct pollfd *fds, nfds_t nfds, int timeout) {
@@ -956,6 +964,9 @@ void __rtsan::InitializeInterceptors() {
   INTERCEPT_FUNCTION(sendto);
   INTERCEPT_FUNCTION(shutdown);
   INTERCEPT_FUNCTION(socket);
+#if SANITIZER_INTERCEPT_ACCEPT4
+  INTERCEPT_FUNCTION(accept4);
+#endif
 
   RTSAN_MAYBE_INTERCEPT_SELECT;
   INTERCEPT_FUNCTION(pselect);

>From d947f9f14125ce7cece6c6916c4f1beeae2adba6 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Thu, 5 Dec 2024 04:52:19 +0000
Subject: [PATCH 2/2] changes from review

---
 compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp        | 7 ++++---
 .../lib/rtsan/tests/rtsan_test_interceptors_posix.cpp     | 8 ++++++++
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index 20021e414794a7..f99d68defa613b 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -713,6 +713,9 @@ INTERCEPTOR(int, accept4, int socket, struct sockaddr *address,
   __rtsan_notify_intercepted_call("accept4");
   return REAL(accept4)(socket, address, address_len, flags);
 }
+#define RTSAN_MAYBE_INTERCEPT_ACCEPT4 INTERCEPT_FUNCTION(accept4)
+#else
+#define RTSAN_MAYBE_INTERCEPT_ACCEPT4
 #endif
 
 // I/O Multiplexing
@@ -964,9 +967,7 @@ void __rtsan::InitializeInterceptors() {
   INTERCEPT_FUNCTION(sendto);
   INTERCEPT_FUNCTION(shutdown);
   INTERCEPT_FUNCTION(socket);
-#if SANITIZER_INTERCEPT_ACCEPT4
-  INTERCEPT_FUNCTION(accept4);
-#endif
+  RTSAN_MAYBE_INTERCEPT_ACCEPT4;
 
   RTSAN_MAYBE_INTERCEPT_SELECT;
   INTERCEPT_FUNCTION(pselect);
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 88df8ec46d0a33..d03831235625e5 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -860,6 +860,14 @@ TEST(TestRtsanInterceptors, AcceptingASocketDiesWhenRealtime) {
   ExpectNonRealtimeSurvival(Func);
 }
 
+#if SANITIZER_INTERCEPT_ACCEPT4
+TEST(TestRtsanInterceptors, Accepting4ASocketDiesWhenRealtime) {
+  auto Func = []() { EXPECT_LT(accept4(kNotASocketFd, nullptr, nullptr, 0), 0); };
+  ExpectRealtimeDeath(Func, "accept4");
+  ExpectNonRealtimeSurvival(Func);
+}
+#endif
+
 TEST(TestRtsanInterceptors, ConnectingASocketDiesWhenRealtime) {
   auto Func = []() { EXPECT_NE(connect(kNotASocketFd, nullptr, 0), 0); };
   ExpectRealtimeDeath(Func, "connect");



More information about the llvm-commits mailing list