[ctx_profile] Arena should zero-initialize its allocation area.
GitOrigin-RevId: 265953cc26b40c4f9a3300baa18c2b7a45074b74
diff --git a/lib/ctx_profile/CtxInstrProfiling.cpp b/lib/ctx_profile/CtxInstrProfiling.cpp
index c5d167b..cff39ee 100644
--- a/lib/ctx_profile/CtxInstrProfiling.cpp
+++ b/lib/ctx_profile/CtxInstrProfiling.cpp
@@ -132,6 +132,10 @@
__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/lib/ctx_profile/CtxInstrProfiling.h b/lib/ctx_profile/CtxInstrProfiling.h
index 69ce796..f55068e 100644
--- a/lib/ctx_profile/CtxInstrProfiling.h
+++ b/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 @@
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/lib/ctx_profile/tests/CtxInstrProfilingTest.cpp b/lib/ctx_profile/tests/CtxInstrProfilingTest.cpp
index 1e96aea..b8d4342 100644
--- a/lib/ctx_profile/tests/CtxInstrProfilingTest.cpp
+++ b/lib/ctx_profile/tests/CtxInstrProfilingTest.cpp
@@ -12,6 +12,15 @@
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);