Skip to content

Conversation

@devnexen
Copy link
Member

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Feb 11, 2025

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

Author: David CARLIER (devnexen)

Changes

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

2 Files Affected:

  • (modified) compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp (+12)
  • (modified) compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp (+22)
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index 410da0748b433..af074d119e86a 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -749,6 +749,16 @@ INTERCEPTOR(int, pthread_rwlock_wrlock, pthread_rwlock_t *lock) {
   return REAL(pthread_rwlock_wrlock)(lock);
 }
 
+INTERCEPTOR(int, pthread_detach, pthread_t thread) {
+  __rtsan_notify_intercepted_call("pthread_detach");
+  return REAL(pthread_detach)(thread);
+}
+
+INTERCEPTOR(int, pthread_kill, pthread_t thread, int signal) {
+  __rtsan_notify_intercepted_call("pthread_kill");
+  return REAL(pthread_kill)(thread, signal);
+}
+
 // Sleeping
 
 INTERCEPTOR(unsigned int, sleep, unsigned int s) {
@@ -1498,6 +1508,8 @@ void __rtsan::InitializeInterceptors() {
   INTERCEPT_FUNCTION(pthread_rwlock_rdlock);
   INTERCEPT_FUNCTION(pthread_rwlock_unlock);
   INTERCEPT_FUNCTION(pthread_rwlock_wrlock);
+  INTERCEPT_FUNCTION(pthread_detach);
+  INTERCEPT_FUNCTION(pthread_kill);
 
   INTERCEPT_FUNCTION(sleep);
   INTERCEPT_FUNCTION(usleep);
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 98d27caae94b8..994aeff6a1c8c 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -1148,6 +1148,28 @@ TEST(TestRtsanInterceptors, PthreadCondWaitDiesWhenRealtime) {
   pthread_mutex_destroy(&mutex);
 }
 
+TEST(TestRtsanInterceptors, PthreadDetachDiesWhenRealtime) {
+  pthread_t thread{};
+  ASSERT_EQ(0,
+            pthread_create(&thread, nullptr, &FakeThreadEntryPoint, nullptr));
+
+  auto Func = [&thread]() { pthread_detach(thread); };
+
+  ExpectRealtimeDeath(Func, "pthread_detach");
+  ExpectNonRealtimeSurvival(Func);
+}
+
+TEST(TestRtsanInterceptors, PthreadKillDiesWhenRealtime) {
+  pthread_t thread{};
+  ASSERT_EQ(0,
+            pthread_create(&thread, nullptr, &FakeThreadEntryPoint, nullptr));
+
+  auto Func = [&thread]() { pthread_kill(thread, -1); };
+
+  ExpectRealtimeDeath(Func, "pthread_kill");
+  ExpectNonRealtimeSurvival(Func);
+}
+
 class PthreadRwlockTest : public ::testing::Test {
 protected:
   void SetUp() override {

@davidtrevelyan
Copy link
Contributor

@devnexen thanks for the suggestion - how confident are you that both pthread_detach and pthread_kill are blocking? Have you taken a look at any implementations of these already? If they tend to just flick small atomic switches and return instantly, I'd be wary of including these interceptors.

@devnexen
Copy link
Member Author

Ah I did not look at linux/glibc, maybe we should add it only for freebsd later down the line.

@davidtrevelyan
Copy link
Contributor

Ah I did not look at linux/glibc, maybe we should add it only for freebsd later down the line.

Sorry - please could you clarify what you're proposing here? Do you mean introduce it for FreeBSD now and Linux later, or Linux now and FreeBSD later?

@devnexen
Copy link
Member Author

I would say not Linux but FreeBSD later.

@davidtrevelyan
Copy link
Contributor

@devnexen OK - are you happy if we close this PR for now?

@devnexen
Copy link
Member Author

Sure I mean either that or rebasing later whatever you all prefer.

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