[compiler-rt] [compiler-rt][rtsan] adding Linux's clone call interception. (PR #130423)
David CARLIER via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 28 14:34:46 PDT 2025
https://github.com/devnexen updated https://github.com/llvm/llvm-project/pull/130423
>From 385589d1b8c47d6f32efb15a4eae15dc0a72d4c4 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Sat, 8 Mar 2025 15:57:40 +0000
Subject: [PATCH 1/2] [compiler-rt][rtsan] adding Linux's clone call
interception.
---
.../lib/rtsan/rtsan_interceptors_posix.cpp | 26 +++++++++++++++++++
.../tests/rtsan_test_interceptors_posix.cpp | 14 ++++++++++
2 files changed, 40 insertions(+)
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index cd57107450a47..6848f2c346761 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -1361,6 +1361,31 @@ INTERCEPTOR(int, execve, const char *filename, char *const argv[],
return REAL(execve)(filename, argv, envp);
}
+#if SANITIZER_GLIBC
+INTERCEPTOR(int, clone, int (*f)(void *), void *stack, int flags, void *arg,
+ ...) {
+ __rtsan_notify_intercepted_call("clone");
+
+ if ((flags & CLONE_PIDFD) || (flags & CLONE_SETTLS) ||
+ (flags & CLONE_PARENT_SETTID) || (flags & CLONE_CHILD_SETTID) ||
+ (flags & CLONE_CHILD_CLEARTID)) {
+ va_list args;
+ va_start(args, arg);
+ pid_t *parent = va_arg(args, pid_t *);
+ void *tls = va_arg(args, void *);
+ pid_t *child = va_arg(args, pid_t *);
+ va_end(args);
+
+ return REAL(clone)(f, stack, flags, arg, parent, tls, child);
+ }
+
+ return REAL(clone)(f, stack, flags, arg);
+}
+#define RTSAN_MAYBE_INTERCEPT_CLONE INTERCEPT_FUNCTION(clone)
+#else
+#define RTSAN_MAYBE_INTERCEPT_CLONE
+#endif
+
#if SANITIZER_INTERCEPT_PROCESS_VM_READV
INTERCEPTOR(ssize_t, process_vm_readv, pid_t pid, const struct iovec *local_iov,
unsigned long liovcnt, const struct iovec *remote_iov,
@@ -1595,6 +1620,7 @@ void __rtsan::InitializeInterceptors() {
INTERCEPT_FUNCTION(fork);
INTERCEPT_FUNCTION(execve);
+ RTSAN_MAYBE_INTERCEPT_CLONE;
RTSAN_MAYBE_INTERCEPT_PROCESS_VM_READV;
RTSAN_MAYBE_INTERCEPT_PROCESS_VM_WRITEV;
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 47bcafff51a4d..0fc7b9ae19bb9 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -1245,6 +1245,20 @@ TEST_F(PthreadRwlockTest, PthreadRwlockWrlockSurvivesWhenNonRealtime) {
ExpectNonRealtimeSurvival(Func);
}
+#if SANITIZER_GLIBC
+TEST(TestRtsanInterceptors, CloneDiesWhenRealtime) {
+ std::vector<char> buf;
+ buf.resize(1024 * 1024);
+ auto Call = [](void *a) { return 0; };
+ auto Func = [&buf, &Call]() {
+ clone(Call, buf.data() + buf.size(), 0, nullptr);
+ };
+
+ ExpectRealtimeDeath(Func, "clone");
+ ExpectNonRealtimeSurvival(Func);
+}
+#endif
+
/*
Sockets
*/
>From eb28b2ba45899e6645b85f7ce754aef5b100b424 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Fri, 28 Mar 2025 21:34:16 +0000
Subject: [PATCH 2/2] add test
---
.../rtsan/tests/rtsan_test_interceptors_posix.cpp | 15 +++++++++++++++
1 file changed, 15 insertions(+)
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 0fc7b9ae19bb9..a8b8e149e88ee 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -1257,6 +1257,21 @@ TEST(TestRtsanInterceptors, CloneDiesWhenRealtime) {
ExpectRealtimeDeath(Func, "clone");
ExpectNonRealtimeSurvival(Func);
}
+
+TEST(TestRtsanInterceptors, CloneWithTldDiesWhenRealtime) {
+ std::vector<char> buf;
+ pid_t pidfd;
+ void *tls = ::operator new(4096, std::align_val_t(16));
+ buf.resize(1024 * 1024);
+ auto Call = [](void *a) { return 0; };
+ auto Func = [&buf, &Call, &pidfd, &tls]() {
+ clone(Call, buf.data() + buf.size(), CLONE_PIDFD | CLONE_SETTLS, &pidfd,
+ tls, nullptr);
+ };
+
+ ExpectRealtimeDeath(Func, "clone");
+ ExpectNonRealtimeSurvival(Func);
+}
#endif
/*
More information about the llvm-commits
mailing list