Skip to content

Truly independent web browser

LibGC: Finish the pending sweep before giving back local free lists

Tim Ledbetter74d169b110e4
diff --git a/Libraries/LibGC/Heap.cpp b/Libraries/LibGC/Heap.cpp
index a43a91295eb..9c517899bcd 100644
--- a/Libraries/LibGC/Heap.cpp
+++ b/Libraries/LibGC/Heap.cpp
@@ -581,17 +581,17 @@ AK::JsonObject Heap::build_graph(ReadonlySpan<FlatPtr> callee_saved_registers)
.callee_saved_registers = callee_saved_registers,
};
- // NB: While an allocator has a block's never-allocated cells out, the block counts them as cells, and the
- // conservative scan in gather_roots() would take a possible pointer into one for a live cell. Give them back,
- // as a collection does, so that they are the block's lazy region again.
- give_back_local_free_lists();
-
// An in-progress incremental sweep would leave parts of the heap as freelist
// entries while the conservative scan in gather_roots() can still pick up
// not-yet-swept (but unreachable) cells whose internal pointers lead to
// those freelist entries. Drain the sweep so we operate on a stable heap.
finish_pending_incremental_sweep();
+ // NB: While an allocator has a block's never-allocated cells out, the block counts them as cells, and the
+ // conservative scan in gather_roots() would take a possible pointer into one for a live cell. Give them back,
+ // as a collection does, so that they are the block's lazy region again.
+ give_back_local_free_lists();
+
HashMap<Cell*, HeapRoot> roots;
Vector<StackFrameInfo> stack_frames;
gather_roots(origin, roots, &stack_frames);
@@ -677,8 +677,8 @@ void Heap::run_collection(ReadonlySpan<FlatPtr> callee_saved_registers, Collecti
VERIFY(!m_collecting_garbage);
- give_back_local_free_lists();
finish_pending_incremental_sweep();
+ give_back_local_free_lists();
g_next_incremental_sweep_should_report = false;
{
@@ -859,6 +859,9 @@ void Heap::register_sweep_callback(AK::Function<void()> callback)
void Heap::gather_roots(ConservativeScanOrigin const& origin, HashMap<Cell*, HeapRoot>& roots, Vector<StackFrameInfo>* out_stack_frames, IncludeIncomingCrossHeapMembers include_incoming_cross_heap_members)
{
+ for (auto& allocator : m_all_cell_allocators)
+ VERIFY(!allocator.m_local_block);
+
// Cross-heap members targeting this heap act as roots for local collections (as the foreign holder is invisible to a local mark).
if (include_incoming_cross_heap_members == IncludeIncomingCrossHeapMembers::Yes) {
for (auto* member : m_incoming_cross_heap_members) {
@@ -1437,6 +1440,7 @@ void Heap::start_incremental_sweep()
// and don't need sweeping.
size_t total_blocks = 0;
for (auto& allocator : m_all_cell_allocators) {
+ VERIFY(!allocator.m_local_block);
allocator.for_each_block([&](HeapBlock& block) {
allocator.m_blocks_pending_sweep.append(block);
++total_blocks;
diff --git a/Libraries/LibGC/Heap.h b/Libraries/LibGC/Heap.h
index 1a13ba7e9f0..3092def6ac6 100644
--- a/Libraries/LibGC/Heap.h
+++ b/Libraries/LibGC/Heap.h
@@ -212,7 +212,8 @@ private:
}
// Marking makes every block pending sweep again, which local free lists
- // must never be in, so every collection starts with this.
+ // must never be in, so every collection does this once it has finished the
+ // pending sweep, whose destructors may take new local free lists.
void give_back_local_free_lists();
void will_allocate(size_t, TriggersCollection = TriggersCollection::Yes);
diff --git a/Libraries/LibGC/HeapGroup.cpp b/Libraries/LibGC/HeapGroup.cpp
index 9a1b84cc216..a287eb515c6 100644
--- a/Libraries/LibGC/HeapGroup.cpp
+++ b/Libraries/LibGC/HeapGroup.cpp
@@ -56,9 +56,11 @@ void HeapGroup::run_collection(ReadonlySpan<FlatPtr> callee_saved_registers, boo
}
}
+ for (auto* heap : m_heaps)
+ heap->finish_pending_incremental_sweep();
for (auto* heap : m_heaps) {
+ VERIFY(!heap->is_incremental_sweep_active());
heap->give_back_local_free_lists();
- heap->finish_pending_incremental_sweep();
heap->m_collecting_garbage = true;
}
ScopeGuard unset_collecting = [&] {
diff --git a/Tests/LibGC/TestGCIncrementalSweep.cpp b/Tests/LibGC/TestGCIncrementalSweep.cpp
index e479bfd65fb..ea5cefb8bf9 100644
--- a/Tests/LibGC/TestGCIncrementalSweep.cpp
+++ b/Tests/LibGC/TestGCIncrementalSweep.cpp
@@ -7,6 +7,7 @@
#include <LibGC/Cell.h>
#include <LibGC/CellAllocator.h>
#include <LibGC/Heap.h>
+#include <LibGC/HeapBlock.h>
#include <LibGC/Root.h>
#include <LibTest/TestCase.h>
@@ -15,6 +16,20 @@ namespace {
bool s_cell_that_allocates_when_destroyed_was_destroyed = false;
bool s_reachable_cell_was_destroyed = false;
+class CellOfAnotherType final : public GC::Cell {
+ GC_CELL(CellOfAnotherType, GC::Cell);
+ GC_DECLARE_ALLOCATOR(CellOfAnotherType);
+
+private:
+ CellOfAnotherType() = default;
+
+ [[maybe_unused]] FlatPtr m_room_for_a_freelist_entry[2] {};
+};
+
+GC_DEFINE_ALLOCATOR(CellOfAnotherType);
+
+CellOfAnotherType* s_cell_of_another_type_allocated_by_a_destructor = nullptr;
+
class SweptCell final : public GC::Cell {
GC_CELL(SweptCell, GC::Cell);
GC_DECLARE_ALLOCATOR(SweptCell);
@@ -22,6 +37,7 @@ class SweptCell final : public GC::Cell {
public:
enum class Role : u8 {
AllocatesWhenDestroyed,
+ AllocatesCellOfAnotherTypeWhenDestroyed,
Reachable,
AllocatedByADestructor,
};
@@ -33,6 +49,9 @@ public:
s_cell_that_allocates_when_destroyed_was_destroyed = true;
(void)heap().allocate<SweptCell>(Role::AllocatedByADestructor);
break;
+ case Role::AllocatesCellOfAnotherTypeWhenDestroyed:
+ s_cell_of_another_type_allocated_by_a_destructor = heap().allocate<CellOfAnotherType>().ptr();
+ break;
case Role::Reachable:
s_reachable_cell_was_destroyed = true;
break;
@@ -68,6 +87,13 @@ NEVER_INLINE GC::Root<SweptCell> allocate_unreachable_then_reachable_cell(GC::He
return GC::make_root(heap.allocate<SweptCell>(SweptCell::Role::Reachable));
}
+NEVER_INLINE GC::Root<CellOfAnotherType> allocate_reachable_cell_of_another_type_then_unreachable_cell(GC::Heap& heap)
+{
+ auto reachable_cell = GC::make_root(heap.allocate<CellOfAnotherType>());
+ (void)heap.allocate<SweptCell>(SweptCell::Role::AllocatesCellOfAnotherTypeWhenDestroyed);
+ return reachable_cell;
+}
+
}
TEST_CASE(allocating_from_a_destructor_during_a_sweep_does_not_free_reachable_cells_of_the_block)
@@ -90,3 +116,30 @@ TEST_CASE(allocating_from_a_destructor_during_a_sweep_does_not_free_reachable_ce
EXPECT(s_cell_that_allocates_when_destroyed_was_destroyed);
EXPECT(!s_reachable_cell_was_destroyed);
}
+
+TEST_CASE(free_cells_that_a_destructor_takes_while_a_collection_finishes_the_sweep_go_back_to_their_block)
+{
+ GC::Heap heap([](auto&) { }, GC::Heap::BecomeProcessDefault::No);
+
+ auto reachable_cell = allocate_reachable_cell_of_another_type_then_unreachable_cell(heap);
+ scrub_stack();
+
+ // This collection leaves the destruction of the unreachable cell to the incremental sweep.
+ heap.collect_garbage();
+ EXPECT(heap.is_incremental_sweep_active());
+
+ // The next collection finishes that sweep. The destructor takes the free cells of the block that holds the
+ // reachable cell, and the cell it allocates there is unreachable.
+ heap.collect_garbage();
+ auto* cell_allocated_by_the_destructor = s_cell_of_another_type_allocated_by_a_destructor;
+ EXPECT(cell_allocated_by_the_destructor);
+ auto& block = *GC::HeapBlock::from_cell(cell_allocated_by_the_destructor);
+ EXPECT_EQ(&block, GC::HeapBlock::from_cell(reachable_cell.ptr()));
+ EXPECT(block.is_pending_sweep());
+
+ // Sweeping the block, as the sweep timer would, frees that cell. Once no allocator holds free cells of the block,
+ // the next cell comes from the free list of the block.
+ heap.sweep_block(block);
+ auto next_cell = GC::make_root(heap.allocate<CellOfAnotherType>());
+ EXPECT_EQ(next_cell.ptr(), cell_allocated_by_the_destructor);
+}