[compiler-rt] [scudo] Add static vector functionality. (PR #98986)

Joshua Baehring via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 16 16:40:36 PDT 2024


https://github.com/JoshuaMBa updated https://github.com/llvm/llvm-project/pull/98986

>From 94531fd182ca6fa4621b2dc5a55c8c3da70250a6 Mon Sep 17 00:00:00 2001
From: Joshua Baehring <jmbaehring at google.com>
Date: Fri, 14 Jun 2024 19:34:25 +0000
Subject: [PATCH 1/5] [scudo] Update error handling for cache entry count in
 secondary cache option handler.

Initially, the scudo allocator would return an error if the user attempted to set the cache
capacity (i.e. the number of possible entries in the cache) above the maximum cache capacity.
Now the allocator will resort to using the maximum cache capacity in this event. An
error will still be returned if the user attempts to set the number of entries to a
negative value.
---
 compiler-rt/lib/scudo/standalone/secondary.h              | 8 ++++----
 compiler-rt/lib/scudo/standalone/tests/secondary_test.cpp | 6 +++---
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/compiler-rt/lib/scudo/standalone/secondary.h b/compiler-rt/lib/scudo/standalone/secondary.h
index d8c9f5bcfcaf6..0e0788f830431 100644
--- a/compiler-rt/lib/scudo/standalone/secondary.h
+++ b/compiler-rt/lib/scudo/standalone/secondary.h
@@ -391,10 +391,10 @@ template <typename Config> class MapAllocatorCache {
       return true;
     }
     if (O == Option::MaxCacheEntriesCount) {
-      const u32 MaxCount = static_cast<u32>(Value);
-      if (MaxCount > Config::getEntriesArraySize())
-        return false;
-      atomic_store_relaxed(&MaxEntriesCount, MaxCount);
+      if (Value < 0) return false;
+      atomic_store_relaxed(
+          &MaxEntriesCount,
+          Min<u32>(static_cast<u32>(Value), Config::getEntriesArraySize()));
       return true;
     }
     if (O == Option::MaxCacheEntrySize) {
diff --git a/compiler-rt/lib/scudo/standalone/tests/secondary_test.cpp b/compiler-rt/lib/scudo/standalone/tests/secondary_test.cpp
index 8f0250e88ebf3..af69313214ea6 100644
--- a/compiler-rt/lib/scudo/standalone/tests/secondary_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/secondary_test.cpp
@@ -192,9 +192,9 @@ TEST_F(MapAllocatorTest, SecondaryIterate) {
 
 TEST_F(MapAllocatorTest, SecondaryOptions) {
   // Attempt to set a maximum number of entries higher than the array size.
-  EXPECT_FALSE(
-      Allocator->setOption(scudo::Option::MaxCacheEntriesCount, 4096U));
-  // A negative number will be cast to a scudo::u32, and fail.
+  EXPECT_TRUE(Allocator->setOption(scudo::Option::MaxCacheEntriesCount, 4096U));
+
+  // Attempt to set an invalid (negative) number of entries
   EXPECT_FALSE(Allocator->setOption(scudo::Option::MaxCacheEntriesCount, -1));
   if (Allocator->canCache(0U)) {
     // Various valid combinations.

>From b4d5564d0e63d2a9e991dc07bae973dab4a222b6 Mon Sep 17 00:00:00 2001
From: Joshua Baehring <jmbaehring at google.com>
Date: Fri, 14 Jun 2024 21:50:30 +0000
Subject: [PATCH 2/5] Ran clang-format

---
 compiler-rt/lib/scudo/standalone/secondary.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/compiler-rt/lib/scudo/standalone/secondary.h b/compiler-rt/lib/scudo/standalone/secondary.h
index 0e0788f830431..9a8e53be388b7 100644
--- a/compiler-rt/lib/scudo/standalone/secondary.h
+++ b/compiler-rt/lib/scudo/standalone/secondary.h
@@ -391,7 +391,8 @@ template <typename Config> class MapAllocatorCache {
       return true;
     }
     if (O == Option::MaxCacheEntriesCount) {
-      if (Value < 0) return false;
+      if (Value < 0)
+        return false;
       atomic_store_relaxed(
           &MaxEntriesCount,
           Min<u32>(static_cast<u32>(Value), Config::getEntriesArraySize()));

>From b4cd8cfcc337f540d4b047bcf064f3e32051223e Mon Sep 17 00:00:00 2001
From: Joshua Baehring <jmbaehring at google.com>
Date: Tue, 16 Jul 2024 02:37:32 +0000
Subject: [PATCH 3/5] [scudo] Add static vector specification.

The scudo vector implementation maintains static local data before
switching to dynamically allocated data as the array size grows.
Users of the vector can now specify the size of the
static local data through the vector template (the default size is
currently set to 10 * sizeof(T) where T is the vector element
type).
---
 compiler-rt/lib/scudo/standalone/vector.h | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/compiler-rt/lib/scudo/standalone/vector.h b/compiler-rt/lib/scudo/standalone/vector.h
index ca10cc281d770..d972ef068bee5 100644
--- a/compiler-rt/lib/scudo/standalone/vector.h
+++ b/compiler-rt/lib/scudo/standalone/vector.h
@@ -9,6 +9,7 @@
 #ifndef SCUDO_VECTOR_H_
 #define SCUDO_VECTOR_H_
 
+#include "common.h"
 #include "mem_map.h"
 
 #include <string.h>
@@ -21,7 +22,7 @@ namespace scudo {
 // implementation supports only POD types.
 //
 // NOTE: This class is not meant to be used directly, use Vector<T> instead.
-template <typename T> class VectorNoCtor {
+template <typename T, size_t StaticCapacity> class VectorNoCtor {
 public:
   T &operator[](uptr I) {
     DCHECK_LT(I, Size);
@@ -116,18 +117,19 @@ template <typename T> class VectorNoCtor {
   uptr CapacityBytes = 0;
   uptr Size = 0;
 
-  T LocalData[256 / sizeof(T)] = {};
+  T LocalData[StaticCapacity] = {};
   MemMapT ExternalBuffer;
 };
 
-template <typename T> class Vector : public VectorNoCtor<T> {
+template <typename T, size_t StaticCapacity = 10>
+class Vector : public VectorNoCtor<T, Max<size_t>(10, StaticCapacity)> {
 public:
-  constexpr Vector() { VectorNoCtor<T>::init(); }
+  constexpr Vector() { VectorNoCtor<T, StaticCapacity>::init(); }
   explicit Vector(uptr Count) {
-    VectorNoCtor<T>::init(Count);
+    VectorNoCtor<T, StaticCapacity>::init(Count);
     this->resize(Count);
   }
-  ~Vector() { VectorNoCtor<T>::destroy(); }
+  ~Vector() { VectorNoCtor<T, StaticCapacity>::destroy(); }
   // Disallow copies and moves.
   Vector(const Vector &) = delete;
   Vector &operator=(const Vector &) = delete;
@@ -138,3 +140,4 @@ template <typename T> class Vector : public VectorNoCtor<T> {
 } // namespace scudo
 
 #endif // SCUDO_VECTOR_H_
+

>From 5d3f1c150faa4a9a3b548f70db8a9b1ef7121633 Mon Sep 17 00:00:00 2001
From: Joshua Baehring <jmbaehring at google.com>
Date: Tue, 16 Jul 2024 03:44:11 +0000
Subject: [PATCH 4/5] Ran clang-format

---
 compiler-rt/lib/scudo/standalone/vector.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/compiler-rt/lib/scudo/standalone/vector.h b/compiler-rt/lib/scudo/standalone/vector.h
index d972ef068bee5..1c4a7e2d9d31f 100644
--- a/compiler-rt/lib/scudo/standalone/vector.h
+++ b/compiler-rt/lib/scudo/standalone/vector.h
@@ -140,4 +140,3 @@ class Vector : public VectorNoCtor<T, Max<size_t>(10, StaticCapacity)> {
 } // namespace scudo
 
 #endif // SCUDO_VECTOR_H_
-

>From 9cf8cef23d66d111269082d68ae21a6e86453d30 Mon Sep 17 00:00:00 2001
From: Joshua Baehring <jmbaehring at google.com>
Date: Tue, 16 Jul 2024 03:44:11 +0000
Subject: [PATCH 5/5] Ran clang-format

---
 compiler-rt/lib/scudo/standalone/vector.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/compiler-rt/lib/scudo/standalone/vector.h b/compiler-rt/lib/scudo/standalone/vector.h
index 1c4a7e2d9d31f..bd56555db691a 100644
--- a/compiler-rt/lib/scudo/standalone/vector.h
+++ b/compiler-rt/lib/scudo/standalone/vector.h
@@ -9,7 +9,6 @@
 #ifndef SCUDO_VECTOR_H_
 #define SCUDO_VECTOR_H_
 
-#include "common.h"
 #include "mem_map.h"
 
 #include <string.h>
@@ -121,8 +120,8 @@ template <typename T, size_t StaticCapacity> class VectorNoCtor {
   MemMapT ExternalBuffer;
 };
 
-template <typename T, size_t StaticCapacity = 10>
-class Vector : public VectorNoCtor<T, Max<size_t>(10, StaticCapacity)> {
+template <typename T, size_t StaticCapacity = 8>
+class Vector : public VectorNoCtor<T, StaticCapacity> {
 public:
   constexpr Vector() { VectorNoCtor<T, StaticCapacity>::init(); }
   explicit Vector(uptr Count) {



More information about the llvm-commits mailing list