[compiler-rt] [rtsan] Make sure rtsan gets initialized on mac (PR #100188)

Chris Apple via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 30 12:15:31 PDT 2024


https://github.com/cjappl updated https://github.com/llvm/llvm-project/pull/100188

>From 8b411264f70bfe9670f1a7954f293a1083a64cc4 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Tue, 23 Jul 2024 13:00:02 -0700
Subject: [PATCH 1/2] Make sure rtsan gets initialized on mac

---
 compiler-rt/lib/rtsan/rtsan_interceptors.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
index 4d5423ec629d22..833062c3de41e7 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
@@ -39,7 +39,6 @@
 
 using namespace __sanitizer;
 
-using __rtsan::rtsan_init_is_running;
 using __rtsan::rtsan_initialized;
 
 namespace {
@@ -49,6 +48,9 @@ struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
 } // namespace
 
 void ExpectNotRealtime(const char *intercepted_function_name) {
+  if (!rtsan_initialized)
+    __rtsan_init();
+
   __rtsan::GetContextForThisThread().ExpectNotRealtime(
       intercepted_function_name);
 }

>From 0182c5dd2cd2d3a29c1cbc6c2e4cb01a5cc24880 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Tue, 30 Jul 2024 12:15:13 -0700
Subject: [PATCH 2/2] [PR] move to atomic variables for initialization

---
 compiler-rt/lib/rtsan/rtsan.cpp              | 34 ++++++++++++++++----
 compiler-rt/lib/rtsan/rtsan.h                |  9 ++----
 compiler-rt/lib/rtsan/rtsan_interceptors.cpp |  6 ++--
 3 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/compiler-rt/lib/rtsan/rtsan.cpp b/compiler-rt/lib/rtsan/rtsan.cpp
index cf7fbddd9eb9c2..61884387d410cd 100644
--- a/compiler-rt/lib/rtsan/rtsan.cpp
+++ b/compiler-rt/lib/rtsan/rtsan.cpp
@@ -12,23 +12,43 @@
 #include <rtsan/rtsan_context.h>
 #include <rtsan/rtsan_interceptors.h>
 
+#include "sanitizer_common/sanitizer_atomic.h"
+
 using namespace __rtsan;
+using namespace __sanitizer;
+
+static atomic_uint8_t rtsan_initialized{0};
+static atomic_uint8_t rtsan_init_is_running{0};
+
+static void SetInitIsRunning(bool is_running) {
+  atomic_store(&rtsan_init_is_running, is_running, memory_order_release);
+}
+
+static bool IsInitRunning() {
+  return atomic_load(&rtsan_init_is_running, memory_order_acquire) == 1;
+}
 
-bool __rtsan::rtsan_initialized;
-bool __rtsan::rtsan_init_is_running;
+static void SetInitialized() {
+  atomic_store(&rtsan_initialized, 1, memory_order_release);
+}
 
 extern "C" {
 
 SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_init() {
-  CHECK(!rtsan_init_is_running);
-  if (rtsan_initialized)
+  CHECK(!IsInitRunning());
+  if (__rtsan_is_initialized())
     return;
-  rtsan_init_is_running = true;
+
+  SetInitIsRunning(true);
 
   InitializeInterceptors();
 
-  rtsan_init_is_running = false;
-  rtsan_initialized = true;
+  SetInitIsRunning(false);
+  SetInitialized();
+}
+
+SANITIZER_INTERFACE_ATTRIBUTE bool __rtsan_is_initialized() {
+  return atomic_load(&rtsan_initialized, memory_order_acquire) == 1;
 }
 
 SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_realtime_enter() {
diff --git a/compiler-rt/lib/rtsan/rtsan.h b/compiler-rt/lib/rtsan/rtsan.h
index ccddaf2c893efc..9ba30b5b111e06 100644
--- a/compiler-rt/lib/rtsan/rtsan.h
+++ b/compiler-rt/lib/rtsan/rtsan.h
@@ -14,17 +14,12 @@
 
 extern "C" {
 
-namespace __rtsan {
-
-extern bool rtsan_initialized;
-extern bool rtsan_init_is_running;
-
-} // namespace __rtsan
-
 // Initialise rtsan interceptors.
 // A call to this method is added to the preinit array on Linux systems.
 SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_init();
 
+SANITIZER_INTERFACE_ATTRIBUTE bool __rtsan_is_initialized();
+
 // Enter real-time context.
 // When in a real-time context, RTSan interceptors will error if realtime
 // violations are detected. Calls to this method are injected at the code
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
index 833062c3de41e7..f0b82758351c34 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
@@ -39,16 +39,14 @@
 
 using namespace __sanitizer;
 
-using __rtsan::rtsan_initialized;
-
 namespace {
 struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
-  static bool UseImpl() { return !rtsan_initialized; }
+  static bool UseImpl() { return !__rtsan_is_initialized(); }
 };
 } // namespace
 
 void ExpectNotRealtime(const char *intercepted_function_name) {
-  if (!rtsan_initialized)
+  if (!__rtsan_is_initialized())
     __rtsan_init();
 
   __rtsan::GetContextForThisThread().ExpectNotRealtime(



More information about the llvm-commits mailing list