Skip to content

Commit 265953c

Browse files
committed
[ctx_profile] Arena should zero-initialize its allocation area.
1 parent 3bde798 commit 265953c

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ __thread ContextNode **volatile __llvm_ctx_profile_callsite[2] = {0, 0};
132132
__thread ContextRoot *volatile __llvm_ctx_profile_current_context_root =
133133
nullptr;
134134

135+
Arena::Arena(uint32_t Size) : Size(Size) {
136+
__sanitizer::internal_memset(start(), 0, Size);
137+
}
138+
135139
// FIXME(mtrofin): use malloc / mmap instead of sanitizer common APIs to reduce
136140
// the dependency on the latter.
137141
Arena *Arena::allocateNewArena(size_t Size, Arena *Prev) {

compiler-rt/lib/ctx_profile/CtxInstrProfiling.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
using namespace llvm::ctx_profile;
1717

18+
// Forward-declare for the one unittest checking Arena construction zeroes out
19+
// its allocatable space.
20+
class ArenaTest_ZeroInit_Test;
1821
namespace __ctx_profile {
1922

2023
static constexpr size_t ExpectedAlignment = 8;
@@ -51,7 +54,8 @@ class Arena final {
5154
const char *pos() const { return start() + Pos; }
5255

5356
private:
54-
explicit Arena(uint32_t Size) : Size(Size) {}
57+
friend class ::ArenaTest_ZeroInit_Test;
58+
explicit Arena(uint32_t Size);
5559
~Arena() = delete;
5660

5761
char *start() { return reinterpret_cast<char *>(&this[1]); }

compiler-rt/lib/ctx_profile/tests/CtxInstrProfilingTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ class ContextTest : public ::testing::Test {
1212
ContextRoot Root;
1313
};
1414

15+
TEST(ArenaTest, ZeroInit) {
16+
char Buffer[1024];
17+
memset(Buffer, 1, 1024);
18+
Arena *A = new (Buffer) Arena(10);
19+
for (auto I = 0U; I < A->size(); ++I)
20+
EXPECT_EQ(A->pos()[I], 0);
21+
EXPECT_EQ(A->size(), 10);
22+
}
23+
1524
TEST(ArenaTest, Basic) {
1625
Arena *A = Arena::allocateNewArena(1024);
1726
EXPECT_EQ(A->size(), 1024U);

0 commit comments

Comments
 (0)