Skip to content

Commit df1fd39

Browse files
authored
Revert "[scudo] Only init RingBuffer when needed. (#85994)"
This reverts commit 0dbd804.
1 parent e68d505 commit df1fd39

File tree

2 files changed

+22
-87
lines changed

2 files changed

+22
-87
lines changed

compiler-rt/lib/scudo/standalone/combined.h

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "local_cache.h"
1919
#include "mem_map.h"
2020
#include "memtag.h"
21-
#include "mutex.h"
2221
#include "options.h"
2322
#include "quarantine.h"
2423
#include "report.h"
@@ -182,17 +181,17 @@ class Allocator {
182181
Quarantine.init(
183182
static_cast<uptr>(getFlags()->quarantine_size_kb << 10),
184183
static_cast<uptr>(getFlags()->thread_local_quarantine_size_kb << 10));
184+
185+
mapAndInitializeRingBuffer();
185186
}
186187

187-
void enableRingBuffer() NO_THREAD_SAFETY_ANALYSIS {
188+
void enableRingBuffer() {
188189
AllocationRingBuffer *RB = getRingBuffer();
189190
if (RB)
190191
RB->Depot->enable();
191-
RingBufferInitLock.unlock();
192192
}
193193

194-
void disableRingBuffer() NO_THREAD_SAFETY_ANALYSIS {
195-
RingBufferInitLock.lock();
194+
void disableRingBuffer() {
196195
AllocationRingBuffer *RB = getRingBuffer();
197196
if (RB)
198197
RB->Depot->disable();
@@ -919,11 +918,9 @@ class Allocator {
919918
DCHECK(!Primary.Options.load().get(OptionBit::TrackAllocationStacks));
920919
return;
921920
}
922-
923-
if (Track) {
924-
initRingBufferMaybe();
921+
if (Track)
925922
Primary.Options.set(OptionBit::TrackAllocationStacks);
926-
} else
923+
else
927924
Primary.Options.clear(OptionBit::TrackAllocationStacks);
928925
}
929926

@@ -1098,9 +1095,6 @@ class Allocator {
10981095
0,
10991096
"invalid alignment");
11001097

1101-
// Lock to initialize the RingBuffer
1102-
HybridMutex RingBufferInitLock;
1103-
11041098
// Pointer to memory mapped area starting with AllocationRingBuffer struct,
11051099
// and immediately followed by Size elements of type Entry.
11061100
atomic_uptr RingBufferAddress = {};
@@ -1555,16 +1549,11 @@ class Allocator {
15551549
RBEntryStart)[N];
15561550
}
15571551

1558-
void initRingBufferMaybe() {
1559-
ScopedLock L(RingBufferInitLock);
1560-
if (getRingBuffer() != nullptr)
1552+
void mapAndInitializeRingBuffer() {
1553+
if (getFlags()->allocation_ring_buffer_size <= 0)
15611554
return;
1562-
1563-
int ring_buffer_size = getFlags()->allocation_ring_buffer_size;
1564-
if (ring_buffer_size <= 0)
1565-
return;
1566-
1567-
u32 AllocationRingBufferSize = static_cast<u32>(ring_buffer_size);
1555+
u32 AllocationRingBufferSize =
1556+
static_cast<u32>(getFlags()->allocation_ring_buffer_size);
15681557

15691558
// We store alloc and free stacks for each entry.
15701559
constexpr u32 kStacksPerRingBufferEntry = 2;

compiler-rt/lib/scudo/standalone/tests/combined_test.cpp

Lines changed: 12 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -867,86 +867,32 @@ SCUDO_TYPED_TEST(ScudoCombinedTest, ReallocateInPlaceStress) {
867867
}
868868
}
869869

870-
SCUDO_TYPED_TEST(ScudoCombinedTest, RingBufferDefaultDisabled) {
871-
// The RingBuffer is not initialized until tracking is enabled for the
872-
// first time.
873-
auto *Allocator = this->Allocator.get();
874-
EXPECT_EQ(0u, Allocator->getRingBufferSize());
875-
EXPECT_EQ(nullptr, Allocator->getRingBufferAddress());
876-
}
877-
878-
SCUDO_TYPED_TEST(ScudoCombinedTest, RingBufferInitOnce) {
879-
auto *Allocator = this->Allocator.get();
880-
Allocator->setTrackAllocationStacks(true);
881-
882-
auto RingBufferSize = Allocator->getRingBufferSize();
883-
ASSERT_GT(RingBufferSize, 0u);
884-
auto *RingBufferAddress = Allocator->getRingBufferAddress();
885-
EXPECT_NE(nullptr, RingBufferAddress);
886-
887-
// Enable tracking again to verify that the initialization only happens once.
888-
Allocator->setTrackAllocationStacks(true);
889-
ASSERT_EQ(RingBufferSize, Allocator->getRingBufferSize());
890-
EXPECT_EQ(RingBufferAddress, Allocator->getRingBufferAddress());
891-
}
892-
893870
SCUDO_TYPED_TEST(ScudoCombinedTest, RingBufferSize) {
894871
auto *Allocator = this->Allocator.get();
895-
Allocator->setTrackAllocationStacks(true);
896-
897-
auto RingBufferSize = Allocator->getRingBufferSize();
898-
ASSERT_GT(RingBufferSize, 0u);
899-
EXPECT_EQ(Allocator->getRingBufferAddress()[RingBufferSize - 1], '\0');
872+
auto Size = Allocator->getRingBufferSize();
873+
ASSERT_GT(Size, 0u);
874+
EXPECT_EQ(Allocator->getRingBufferAddress()[Size - 1], '\0');
900875
}
901876

902877
SCUDO_TYPED_TEST(ScudoCombinedTest, RingBufferAddress) {
903878
auto *Allocator = this->Allocator.get();
904-
Allocator->setTrackAllocationStacks(true);
905-
906-
auto *RingBufferAddress = Allocator->getRingBufferAddress();
907-
EXPECT_NE(RingBufferAddress, nullptr);
908-
EXPECT_EQ(RingBufferAddress, Allocator->getRingBufferAddress());
909-
}
910-
911-
SCUDO_TYPED_TEST(ScudoCombinedTest, StackDepotDefaultDisabled) {
912-
// The StackDepot is not initialized until tracking is enabled for the
913-
// first time.
914-
auto *Allocator = this->Allocator.get();
915-
EXPECT_EQ(0u, Allocator->getStackDepotSize());
916-
EXPECT_EQ(nullptr, Allocator->getStackDepotAddress());
917-
}
918-
919-
SCUDO_TYPED_TEST(ScudoCombinedTest, StackDepotInitOnce) {
920-
auto *Allocator = this->Allocator.get();
921-
Allocator->setTrackAllocationStacks(true);
922-
923-
auto StackDepotSize = Allocator->getStackDepotSize();
924-
EXPECT_GT(StackDepotSize, 0u);
925-
auto *StackDepotAddress = Allocator->getStackDepotAddress();
926-
EXPECT_NE(nullptr, StackDepotAddress);
927-
928-
// Enable tracking again to verify that the initialization only happens once.
929-
Allocator->setTrackAllocationStacks(true);
930-
EXPECT_EQ(StackDepotSize, Allocator->getStackDepotSize());
931-
EXPECT_EQ(StackDepotAddress, Allocator->getStackDepotAddress());
879+
auto *Addr = Allocator->getRingBufferAddress();
880+
EXPECT_NE(Addr, nullptr);
881+
EXPECT_EQ(Addr, Allocator->getRingBufferAddress());
932882
}
933883

934884
SCUDO_TYPED_TEST(ScudoCombinedTest, StackDepotSize) {
935885
auto *Allocator = this->Allocator.get();
936-
Allocator->setTrackAllocationStacks(true);
937-
938-
auto StackDepotSize = Allocator->getStackDepotSize();
939-
EXPECT_GT(StackDepotSize, 0u);
940-
EXPECT_EQ(Allocator->getStackDepotAddress()[StackDepotSize - 1], '\0');
886+
auto Size = Allocator->getStackDepotSize();
887+
ASSERT_GT(Size, 0u);
888+
EXPECT_EQ(Allocator->getStackDepotAddress()[Size - 1], '\0');
941889
}
942890

943891
SCUDO_TYPED_TEST(ScudoCombinedTest, StackDepotAddress) {
944892
auto *Allocator = this->Allocator.get();
945-
Allocator->setTrackAllocationStacks(true);
946-
947-
auto *StackDepotAddress = Allocator->getStackDepotAddress();
948-
EXPECT_NE(StackDepotAddress, nullptr);
949-
EXPECT_EQ(StackDepotAddress, Allocator->getStackDepotAddress());
893+
auto *Addr = Allocator->getStackDepotAddress();
894+
EXPECT_NE(Addr, nullptr);
895+
EXPECT_EQ(Addr, Allocator->getStackDepotAddress());
950896
}
951897

952898
SCUDO_TYPED_TEST(ScudoCombinedTest, StackDepot) {

0 commit comments

Comments
 (0)