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

Chris Apple via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 7 18:04:03 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/3] 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 4d5423ec629d2..833062c3de41e 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/3] [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 cf7fbddd9eb9c..61884387d410c 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 ccddaf2c893ef..9ba30b5b111e0 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 833062c3de41e..f0b82758351c3 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(

>From 99baf4a409d0940936dbdec5b7869a996f6b45b5 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Wed, 7 Aug 2024 16:18:44 -0700
Subject: [PATCH 3/3] [PR]: Introduce mutex for initalization, introduce
 _ensure_initialized

---
 compiler-rt/lib/rtsan/rtsan.cpp              | 28 +++++++++-----------
 compiler-rt/lib/rtsan/rtsan.h                |  5 ++++
 compiler-rt/lib/rtsan/rtsan_interceptors.cpp |  3 +--
 3 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/compiler-rt/lib/rtsan/rtsan.cpp b/compiler-rt/lib/rtsan/rtsan.cpp
index 61884387d410c..d0bbaa9472c42 100644
--- a/compiler-rt/lib/rtsan/rtsan.cpp
+++ b/compiler-rt/lib/rtsan/rtsan.cpp
@@ -13,20 +13,13 @@
 #include <rtsan/rtsan_interceptors.h>
 
 #include "sanitizer_common/sanitizer_atomic.h"
+#include "sanitizer_common/sanitizer_mutex.h"
 
 using namespace __rtsan;
 using namespace __sanitizer;
 
+static StaticSpinMutex rtsan_inited_mutex;
 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;
-}
 
 static void SetInitialized() {
   atomic_store(&rtsan_initialized, 1, memory_order_release);
@@ -35,16 +28,21 @@ static void SetInitialized() {
 extern "C" {
 
 SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_init() {
-  CHECK(!IsInitRunning());
-  if (__rtsan_is_initialized())
+  InitializeInterceptors();
+  SetInitialized();
+}
+
+SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_ensure_initialized() {
+  if (LIKELY(__rtsan_is_initialized()))
     return;
 
-  SetInitIsRunning(true);
+  SpinMutexLock lock(&rtsan_inited_mutex);
 
-  InitializeInterceptors();
+  // Someone may have initialized us while we were waiting for the lock
+  if (__rtsan_is_initialized())
+    return;
 
-  SetInitIsRunning(false);
-  SetInitialized();
+  __rtsan_init();
 }
 
 SANITIZER_INTERFACE_ATTRIBUTE bool __rtsan_is_initialized() {
diff --git a/compiler-rt/lib/rtsan/rtsan.h b/compiler-rt/lib/rtsan/rtsan.h
index 9ba30b5b111e0..094c989895d22 100644
--- a/compiler-rt/lib/rtsan/rtsan.h
+++ b/compiler-rt/lib/rtsan/rtsan.h
@@ -18,6 +18,11 @@ extern "C" {
 // A call to this method is added to the preinit array on Linux systems.
 SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_init();
 
+// Initializes rtsan if it has not been initialized yet.
+// Used by the RTSan runtime to ensure that rtsan is initialized before any
+// other rtsan functions are called.
+SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_ensure_initialized();
+
 SANITIZER_INTERFACE_ATTRIBUTE bool __rtsan_is_initialized();
 
 // Enter real-time context.
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
index f0b82758351c3..fe368928a9b10 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
@@ -46,8 +46,7 @@ struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
 } // namespace
 
 void ExpectNotRealtime(const char *intercepted_function_name) {
-  if (!__rtsan_is_initialized())
-    __rtsan_init();
+  __rtsan_ensure_initialized();
 
   __rtsan::GetContextForThisThread().ExpectNotRealtime(
       intercepted_function_name);



More information about the llvm-commits mailing list