[compiler-rt] 265953c - [ctx_profile] Arena should zero-initialize its allocation area.

Mircea Trofin via llvm-commits llvm-commits at lists.llvm.org
Fri May 10 10:43:31 PDT 2024


Author: Mircea Trofin
Date: 2024-05-10T10:43:22-07:00
New Revision: 265953cc26b40c4f9a3300baa18c2b7a45074b74

URL: https://github.com/llvm/llvm-project/commit/265953cc26b40c4f9a3300baa18c2b7a45074b74
DIFF: https://github.com/llvm/llvm-project/commit/265953cc26b40c4f9a3300baa18c2b7a45074b74.diff

LOG: [ctx_profile] Arena should zero-initialize its allocation area.

Added: 
    

Modified: 
    compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
    compiler-rt/lib/ctx_profile/CtxInstrProfiling.h
    compiler-rt/lib/ctx_profile/tests/CtxInstrProfilingTest.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
index c5d167bf996ab..cff39eeafba6e 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
+++ b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
@@ -132,6 +132,10 @@ __thread ContextNode **volatile __llvm_ctx_profile_callsite[2] = {0, 0};
 __thread ContextRoot *volatile __llvm_ctx_profile_current_context_root =
     nullptr;
 
+Arena::Arena(uint32_t Size) : Size(Size) {
+  __sanitizer::internal_memset(start(), 0, Size);
+}
+
 // FIXME(mtrofin): use malloc / mmap instead of sanitizer common APIs to reduce
 // the dependency on the latter.
 Arena *Arena::allocateNewArena(size_t Size, Arena *Prev) {

diff  --git a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.h b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.h
index 69ce796b71e31..f55068e98dd43 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.h
+++ b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.h
@@ -15,6 +15,9 @@
 
 using namespace llvm::ctx_profile;
 
+// Forward-declare for the one unittest checking Arena construction zeroes out
+// its allocatable space.
+class ArenaTest_ZeroInit_Test;
 namespace __ctx_profile {
 
 static constexpr size_t ExpectedAlignment = 8;
@@ -51,7 +54,8 @@ class Arena final {
   const char *pos() const { return start() + Pos; }
 
 private:
-  explicit Arena(uint32_t Size) : Size(Size) {}
+  friend class ::ArenaTest_ZeroInit_Test;
+  explicit Arena(uint32_t Size);
   ~Arena() = delete;
 
   char *start() { return reinterpret_cast<char *>(&this[1]); }

diff  --git a/compiler-rt/lib/ctx_profile/tests/CtxInstrProfilingTest.cpp b/compiler-rt/lib/ctx_profile/tests/CtxInstrProfilingTest.cpp
index 1e96aea19ce47..b8d4342c41128 100644
--- a/compiler-rt/lib/ctx_profile/tests/CtxInstrProfilingTest.cpp
+++ b/compiler-rt/lib/ctx_profile/tests/CtxInstrProfilingTest.cpp
@@ -12,6 +12,15 @@ class ContextTest : public ::testing::Test {
   ContextRoot Root;
 };
 
+TEST(ArenaTest, ZeroInit) {
+  char Buffer[1024];
+  memset(Buffer, 1, 1024);
+  Arena *A = new (Buffer) Arena(10);
+  for (auto I = 0U; I < A->size(); ++I)
+    EXPECT_EQ(A->pos()[I], 0);
+  EXPECT_EQ(A->size(), 10);
+}
+
 TEST(ArenaTest, Basic) {
   Arena *A = Arena::allocateNewArena(1024);
   EXPECT_EQ(A->size(), 1024U);


        


More information about the llvm-commits mailing list