[compiler-rt] [scudo] [MTE] resize stack depot for allocation ring buffer (PR #74515)

via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 2 11:43:28 PST 2024


================
@@ -1504,6 +1529,28 @@ class Allocator {
       return;
     u32 AllocationRingBufferSize =
         static_cast<u32>(getFlags()->allocation_ring_buffer_size);
+    // We store alloc and free stacks for each entry.
+    constexpr auto kStacksPerRingBufferEntry = 2;
+    u32 TabSize = static_cast<u32>(roundUpPowerOfTwo(kStacksPerRingBufferEntry *
+                                                     AllocationRingBufferSize));
+    constexpr auto kFramesPerStack = 8;
+    static_assert(isPowerOfTwo(kFramesPerStack));
+    u32 RingSize = static_cast<u32>(TabSize * kFramesPerStack);
+    DCHECK(isPowerOfTwo(RingSize));
+    static_assert(sizeof(StackDepot) % alignof(atomic_u64) == 0);
+
+    StackDepotSize = sizeof(StackDepot) + sizeof(atomic_u64) * RingSize +
+                     sizeof(atomic_u32) * TabSize;
+    MemMapT DepotMap;
+    DepotMap.map(
+        /*Addr=*/0U, roundUp(StackDepotSize, getPageSizeCached()),
+        "scudo:stack_depot");
+    RawStackDepot = reinterpret_cast<char *>(DepotMap.getBase());
+    auto *Depot = reinterpret_cast<StackDepot *>(DepotMap.getBase());
+    Depot->init(RingSize, TabSize);
+    DCHECK(Depot->isValid(StackDepotSize));
----------------
ChiaHungDuan wrote:

As I mentioned, if the `DCHECK` fails, how do we know why it's failed? We still need to check all the logic in `isValid` and review the two arguments of `init`. Why don't we just stop the initialization when we see something wrong? If you already know the arguments are going to generate an invalid object, why do you still do that?

The reason for the vector example is, after construction, we don't need to verify the size and content like
```
std::vector<int> foo(4, 0);
DCHECK_EQ(foo.size(), 4);
for (int v : foo)
  DCHECK_EQ(v, 0);
```
Of course you may want to add some debug checks in the implementation but not asking the user to verify it. In this case, the user has to do the verification and I think it is kind of unreasonable. 

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


More information about the llvm-commits mailing list