[compiler-rt] [scudo] Only init RingBuffer when needed. (PR #85994)

via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 26 11:49:40 PDT 2024


================
@@ -1594,14 +1598,19 @@ class Allocator {
     RB->StackDepotSize = StackDepotSize;
     RB->RawStackDepotMap = DepotMap;
 
-    atomic_store(&RingBufferAddress, reinterpret_cast<uptr>(RB),
-                 memory_order_release);
+    // If multiple threads try to initialize at the same time, let one thread
+    // win and throw away the work done in the other threads. Since this
+    // path is only meant for debugging, a race that results in work being
+    // discarded should not matter.
+    uptr EmptyPtr = 0;
+    if (!atomic_compare_exchange_strong(&RingBufferAddress, &EmptyPtr,
+                                        reinterpret_cast<uptr>(RB),
+                                        memory_order_acquire)) {
+      unmapRingBuffer(RB);
+    }
----------------
ChiaHungDuan wrote:

I would suggest doing the initialization under a lock so that if there's any issue during buffer allocation, we won't see several failures with same error (I know it's unlikely according to the use case but still want to avoid any confusion if possible)

We may also want to ensure the enable()/disable() take/release the init-lock respectively

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


More information about the llvm-commits mailing list