[clang] [compiler-rt] Rtsan fbsd (PR #125389)

David CARLIER via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 2 01:39:37 PST 2025


https://github.com/devnexen created https://github.com/llvm/llvm-project/pull/125389

None

>From 19d4d1b3501d8524a6d88d62317dd0ea0022ebfb Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Sun, 2 Feb 2025 09:36:50 +0000
Subject: [PATCH 1/2] [compiler-rt][rtsan] porting the sanitizer to FreeBSD.

Most of the apple api exceptions also apply to freebsd, however
to create a per-thread realtime context pthread api cannot be used
since freebsd calls calloc in _thr_alloc().
---
 compiler-rt/lib/rtsan/rtsan_context.cpp          | 16 ++++++++++++++++
 .../lib/rtsan/rtsan_interceptors_posix.cpp       |  4 ++--
 .../tests/rtsan_test_interceptors_posix.cpp      |  4 +++-
 3 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/compiler-rt/lib/rtsan/rtsan_context.cpp b/compiler-rt/lib/rtsan/rtsan_context.cpp
index 536d62e81e2fb6..dacc9b6dedf402 100644
--- a/compiler-rt/lib/rtsan/rtsan_context.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_context.cpp
@@ -19,6 +19,7 @@
 using namespace __sanitizer;
 using namespace __rtsan;
 
+#if !SANITIZER_FREEBSD
 static pthread_key_t context_key;
 static pthread_once_t key_once = PTHREAD_ONCE_INIT;
 
@@ -43,6 +44,21 @@ static __rtsan::Context &GetContextForThisThreadImpl() {
 
   return *current_thread_context;
 }
+#else
+
+// On FreeBSD, pthread api cannot be used as calloc is called under the hood
+// at library initialization time.
+static __thread Context *ctx = nullptr;
+
+static __rtsan::Context &GetContextForThisThreadImpl() {
+  if (ctx == nullptr) {
+    ctx = static_cast<Context *>(MmapOrDie(sizeof(Context), "RtsanContext"));
+    new (ctx) Context();
+  }
+
+  return *ctx;
+}
+#endif
 
 __rtsan::Context::Context() = default;
 
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index 3ea9e54a046cf8..2bd2f4c7ea8dd1 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -929,7 +929,7 @@ INTERCEPTOR(int, msync, void *addr, size_t length, int flag) {
   return REAL(msync)(addr, length, flag);
 }
 
-#if SANITIZER_APPLE
+#if SANITIZER_APPLE || SANITIZER_FREEBSD
 INTERCEPTOR(int, mincore, const void *addr, size_t length, char *vec) {
 #else
 INTERCEPTOR(int, mincore, void *addr, size_t length, unsigned char *vec) {
@@ -1324,7 +1324,7 @@ INTERCEPTOR(ssize_t, process_vm_writev, pid_t pid,
 // the test. Revisit this in the future, but hopefully intercepting fork/exec is
 // enough to dissuade usage of wait by proxy.
 
-#if SANITIZER_APPLE
+#if SANITIZER_APPLE || SANITIZER_FREEBSD
 #define INT_TYPE_SYSCALL int
 #else
 #define INT_TYPE_SYSCALL long
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 e3688157a842c7..ea81e510fd7173 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -280,7 +280,7 @@ TEST_F(RtsanOpenedMmapTest, MsyncDiesWhenRealtime) {
 }
 
 TEST_F(RtsanOpenedMmapTest, MincoreDiesWhenRealtime) {
-#if SANITIZER_APPLE
+#if SANITIZER_APPLE || SANITIZER_FREEBSD
   std::vector<char> vec(GetSize() / 1024);
 #else
   std::vector<unsigned char> vec(GetSize() / 1024);
@@ -1527,6 +1527,7 @@ TEST_F(KqueueTest, KeventDiesWhenRealtime) {
   ExpectNonRealtimeSurvival(Func);
 }
 
+#if SANITIZER_APPLE
 TEST_F(KqueueTest, Kevent64DiesWhenRealtime) {
   struct kevent64_s event;
   EV_SET64(&event, 0, EVFILT_READ, EV_ADD, 0, 0, 0, 0, 0);
@@ -1539,6 +1540,7 @@ TEST_F(KqueueTest, Kevent64DiesWhenRealtime) {
   ExpectRealtimeDeath(Func, "kevent64");
   ExpectNonRealtimeSurvival(Func);
 }
+#endif // SANITIZER_APPLE
 #endif // SANITIZER_INTERCEPT_KQUEUE
 
 #if SANITIZER_LINUX

>From c962385b9eac1abc002c0aa80b50f07efaa7897e Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Sun, 2 Feb 2025 09:38:52 +0000
Subject: [PATCH 2/2] freebsd clang frontend update.

---
 clang/lib/Driver/ToolChains/FreeBSD.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/lib/Driver/ToolChains/FreeBSD.cpp b/clang/lib/Driver/ToolChains/FreeBSD.cpp
index a6d859f0ebfec2..baabfabf26267f 100644
--- a/clang/lib/Driver/ToolChains/FreeBSD.cpp
+++ b/clang/lib/Driver/ToolChains/FreeBSD.cpp
@@ -497,6 +497,7 @@ SanitizerMask FreeBSD::getSupportedSanitizers() const {
   Res |= SanitizerKind::PointerCompare;
   Res |= SanitizerKind::PointerSubtract;
   Res |= SanitizerKind::Vptr;
+  Res |= SanitizerKind::Realtime;
   if (IsAArch64 || IsX86_64 || IsMIPS64) {
     Res |= SanitizerKind::Leak;
     Res |= SanitizerKind::Thread;



More information about the llvm-commits mailing list