[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:06:48 PDT 2024


================
@@ -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());
----------------
cjappl wrote:

Good catch. Alright, this now does the following:

Checks if initialized - using an atomic to prevent data races
If not, grabs the mutex
Once the mutex is grabbed, checks the atomic once more to ensure we don't double initialize.

I think this handles all the possible data races in this path.

The separation of rtsan_ensure_inited vs rtsan_init is to make sure the preinit array initialization of rtsan is as lean as possible. This is following some of the patterns I saw in other sanitizers.

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


More information about the llvm-commits mailing list