Truly independent web browser
LibGC: Share the stack scrub that GC tests use before a collection
Luke Wilde98a2f1465ac0
diff --git a/Libraries/LibGC/Testing.h b/Libraries/LibGC/Testing.hnew file mode 100644index 00000000000..0ea88aea1a6--- /dev/null+++ b/Libraries/LibGC/Testing.h@@ -0,0 +1,22 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Platform.h>+#include <AK/Types.h>++namespace GC {++// Zeroes the 8 KiB of stack below its caller, on the real stack even under ASan.+inline NO_SANITIZE_ADDRESS NEVER_INLINE void scrub_stack_for_testing()+{+ u8 volatile filler[8 * KiB];+ for (size_t i = 0; i < sizeof(filler); ++i)+ filler[i] = 0;+}++}diff --git a/Tests/LibGC/TestGCContainers.cpp b/Tests/LibGC/TestGCContainers.cppindex 94ac7e65267..8a784959165 100644--- a/Tests/LibGC/TestGCContainers.cpp+++ b/Tests/LibGC/TestGCContainers.cpp@@ -18,6 +18,7 @@#include <LibGC/RootHashMap.h>#include <LibGC/RootHashTable.h>#include <LibGC/RootVector.h>+#include <LibGC/Testing.h>#include <LibGC/Weak.h>#include <LibGC/WeakHashMap.h>#include <LibTest/TestCase.h>@@ -92,13 +93,6 @@ static GC::Heap& test_heap()return *heap;}-static NEVER_INLINE void scrub_stack()-{- u8 volatile filler[8 * KiB];- for (size_t i = 0; i < sizeof(filler); ++i)- filler[i] = 0;-}-TEST_SETUP{GC::Heap::set_default_heap_for_testing(test_heap());@@ -172,12 +166,12 @@ TEST_CASE(activity_root_controls_cell_liveness)root.take(*cell);}- scrub_stack();+ GC::scrub_stack_for_testing();heap.collect_garbage();EXPECT(weak_cell);root.release();- scrub_stack();+ GC::scrub_stack_for_testing();heap.collect_garbage(GC::Heap::CollectionType::CollectEverything);EXPECT(!weak_cell);}@@ -419,7 +413,7 @@ static void expect_transferred_container_keeps_cell_alive(AddCell add_cell, Trantransfer(destination, source);}- scrub_stack();+ GC::scrub_stack_for_testing();heap.collect_garbage();EXPECT(weak_cell);}@@ -744,7 +738,7 @@ TEST_CASE(weak_pointers_to_dead_cells_are_null_while_finalizing)observer->weak_to_self_was_null_while_finalizing = &weak_to_self_was_null_while_finalizing;}- scrub_stack();+ GC::scrub_stack_for_testing();heap.collect_garbage(GC::Heap::CollectionType::CollectEverything);EXPECT(weak_cell_was_null_while_finalizing);EXPECT(weak_to_self_was_null_while_finalizing);diff --git a/Tests/LibGC/TestGCFreeList.cpp b/Tests/LibGC/TestGCFreeList.cppindex 2e2dac1b7a0..32d695ed5a9 100644--- a/Tests/LibGC/TestGCFreeList.cpp+++ b/Tests/LibGC/TestGCFreeList.cpp@@ -9,6 +9,7 @@#include <LibGC/Heap.h>#include <LibGC/HeapBlock.h>#include <LibGC/Root.h>+#include <LibGC/Testing.h>#include <LibTest/TestCase.h>namespace {@@ -25,13 +26,6 @@ private:GC_DEFINE_ALLOCATOR(FreeListTestCell);-NEVER_INLINE void scrub_stack()-{- u8 volatile filler[8 * KiB];- for (size_t i = 0; i < sizeof(filler); ++i)- filler[i] = 0;-}-NEVER_INLINE void allocate_unreachable_cells(GC::Heap& heap, size_t count){for (size_t i = 0; i < count; ++i)@@ -50,7 +44,7 @@ TEST_CASE(a_corrupted_free_cell_link_leads_to_a_cell_of_the_same_block)auto cell_size = GC::HeapBlock::from_cell(first.ptr())->cell_size();auto address_of = [&](size_t index) { return bit_cast<FlatPtr>(first.ptr()) + index * cell_size; };allocate_unreachable_cells(heap, 4);- scrub_stack();+ GC::scrub_stack_for_testing();// The sweep frees the four unreachable cells in address order, so the free list of the block leads from the last// of them to the first.diff --git a/Tests/LibGC/TestGCHeapGroup.cpp b/Tests/LibGC/TestGCHeapGroup.cppindex a760ac5a16e..3282d99835a 100644--- a/Tests/LibGC/TestGCHeapGroup.cpp+++ b/Tests/LibGC/TestGCHeapGroup.cpp@@ -11,6 +11,7 @@#include <LibGC/HeapGroup.h>#include <LibGC/Ptr.h>#include <LibGC/Root.h>+#include <LibGC/Testing.h>#include <LibGC/WeakInlines.h>#include <LibTest/TestCase.h>@@ -66,15 +67,6 @@ private:GC_DEFINE_ALLOCATOR(WeakCreatingCell);-// Under ASan, an instrumented function's locals live on a fake stack, so filling them would leave the real stack as it-// was — and the conservative scan reads the real stack. So this function isn't instrumented.-NO_SANITIZE_ADDRESS NEVER_INLINE void scrub_stack()-{- u8 volatile filler[8 * KiB];- for (size_t i = 0; i < sizeof(filler); ++i)- filler[i] = 0;-}-NEVER_INLINE GC::Root<LinkedCell> allocate_holder_and_foreign_target(GC::Heap& holder_heap, GC::Heap& target_heap){auto holder = GC::make_root(holder_heap.allocate<LinkedCell>());@@ -122,7 +114,7 @@ static NEVER_INLINE void sanity_single_heap_frees_garbage()allocate_garbage(heap);EXPECT_EQ(s_live_linked_cells, 1u);- scrub_stack();+ GC::scrub_stack_for_testing();heap.collect_garbage();EXPECT_EQ(s_live_linked_cells, 0u);}@@ -142,18 +134,18 @@ static NEVER_INLINE void incoming_cross_heap_member_roots_local_collection()EXPECT_EQ(s_live_linked_cells, 2u);// The only thing keeping the target alive is the incoming cross-heap member; B's local collection cannot see the holder on A, so the member registration must root it.- scrub_stack();+ GC::scrub_stack_for_testing();heap_b.collect_garbage();EXPECT_EQ(s_live_linked_cells, 2u);// Once the edge is dropped, B's next local collection should free the target.holder->foreign() = nullptr;- scrub_stack();+ GC::scrub_stack_for_testing();heap_b.collect_garbage();EXPECT_EQ(s_live_linked_cells, 1u);holder = {};- scrub_stack();+ GC::scrub_stack_for_testing();heap_a.collect_garbage();EXPECT_EQ(s_live_linked_cells, 0u);@@ -176,13 +168,13 @@ static NEVER_INLINE void group_collection_breaks_cross_heap_cycles()EXPECT_EQ(s_live_linked_cells, 2u);// Local collections see the incoming members as roots, so the boundary cycle should survive collection.- scrub_stack();+ GC::scrub_stack_for_testing();heap_a.collect_garbage();heap_b.collect_garbage();EXPECT_EQ(s_live_linked_cells, 2u);// The unified mark should not reach the cycle, so both cells should be freed by the group collection.- scrub_stack();+ GC::scrub_stack_for_testing();group.collect_garbage();EXPECT_EQ(s_live_linked_cells, 0u);@@ -205,12 +197,12 @@ static NEVER_INLINE void group_collection_traces_live_cross_heap_chains()EXPECT_EQ(s_live_linked_cells, 3u);// The whole chain must survive a group collection, including the second A cell that is only reachable through the B cell.- scrub_stack();+ GC::scrub_stack_for_testing();group.collect_garbage();EXPECT_EQ(s_live_linked_cells, 3u);holder = {};- scrub_stack();+ GC::scrub_stack_for_testing();group.collect_garbage();EXPECT_EQ(s_live_linked_cells, 0u);@@ -233,7 +225,7 @@ static NEVER_INLINE void group_collection_finalizer_creates_null_weak_to_dead_fo// finalizers yet, so a weak pointer that it creates to B's dead cell must be null.s_weak_to_dead_foreign_cell_was_null_while_finalizing = {};allocate_weak_creating_cell_and_foreign_garbage(heap_a, heap_b);- scrub_stack();+ GC::scrub_stack_for_testing();group.collect_garbage();EXPECT_EQ(s_weak_to_dead_foreign_cell_was_null_while_finalizing, true);EXPECT_EQ(s_live_linked_cells, 0u);@@ -245,33 +237,33 @@ static NEVER_INLINE void group_collection_finalizer_creates_null_weak_to_dead_fo// Under ASan with detect_stack_use_after_return, a function's locals live on a fake stack, but its frame on the real// stack still reserves room for them — room it never writes, so it keeps whatever earlier calls left there, and the// conservative scan reads it. A stale pointer there can keep a dropped cell alive. So each case runs in a frame of its-// own, on stack that scrub_stack() has just cleared.+// own, on stack that GC::scrub_stack_for_testing() has just cleared.TEST_CASE(sanity_single_heap_frees_garbage){- scrub_stack();+ GC::scrub_stack_for_testing();sanity_single_heap_frees_garbage();}TEST_CASE(incoming_cross_heap_member_roots_local_collection){- scrub_stack();+ GC::scrub_stack_for_testing();incoming_cross_heap_member_roots_local_collection();}TEST_CASE(group_collection_breaks_cross_heap_cycles){- scrub_stack();+ GC::scrub_stack_for_testing();group_collection_breaks_cross_heap_cycles();}TEST_CASE(group_collection_traces_live_cross_heap_chains){- scrub_stack();+ GC::scrub_stack_for_testing();group_collection_traces_live_cross_heap_chains();}TEST_CASE(group_collection_finalizer_creates_null_weak_to_dead_foreign_cell){- scrub_stack();+ GC::scrub_stack_for_testing();group_collection_finalizer_creates_null_weak_to_dead_foreign_cell();}diff --git a/Tests/LibGC/TestGCIncrementalSweep.cpp b/Tests/LibGC/TestGCIncrementalSweep.cppindex ea5cefb8bf9..484dab46fb4 100644--- a/Tests/LibGC/TestGCIncrementalSweep.cpp+++ b/Tests/LibGC/TestGCIncrementalSweep.cpp@@ -9,6 +9,7 @@#include <LibGC/Heap.h>#include <LibGC/HeapBlock.h>#include <LibGC/Root.h>+#include <LibGC/Testing.h>#include <LibTest/TestCase.h>namespace {@@ -72,13 +73,6 @@ private:GC_DEFINE_ALLOCATOR(SweptCell);-NEVER_INLINE void scrub_stack()-{- u8 volatile filler[8 * KiB];- for (size_t i = 0; i < sizeof(filler); ++i)- filler[i] = 0;-}-// NB: Both cells come from the same block of a fresh heap, the unreachable one first, so the sweep of that block// destroys it before it reaches the reachable one.NEVER_INLINE GC::Root<SweptCell> allocate_unreachable_then_reachable_cell(GC::Heap& heap)@@ -101,7 +95,7 @@ TEST_CASE(allocating_from_a_destructor_during_a_sweep_does_not_free_reachable_ceGC::Heap heap([](auto&) { }, GC::Heap::BecomeProcessDefault::No);auto reachable_cell = allocate_unreachable_then_reachable_cell(heap);- scrub_stack();+ GC::scrub_stack_for_testing();// This collection marks the reachable cell and leaves the destruction of the other one to the incremental sweep.heap.collect_garbage();@@ -122,7 +116,7 @@ TEST_CASE(free_cells_that_a_destructor_takes_while_a_collection_finishes_the_sweGC::Heap heap([](auto&) { }, GC::Heap::BecomeProcessDefault::No);auto reachable_cell = allocate_reachable_cell_of_another_type_then_unreachable_cell(heap);- scrub_stack();+ GC::scrub_stack_for_testing();// This collection leaves the destruction of the unreachable cell to the incremental sweep.heap.collect_garbage();diff --git a/Tests/LibGC/TestNanBoxedValue.cpp b/Tests/LibGC/TestNanBoxedValue.cppindex 28cf6d3f5fd..c9eacaa3499 100644--- a/Tests/LibGC/TestNanBoxedValue.cpp+++ b/Tests/LibGC/TestNanBoxedValue.cpp@@ -10,6 +10,7 @@#include <LibGC/Heap.h>#include <LibGC/HeapRegion.h>#include <LibGC/NanBoxedValue.h>+#include <LibGC/Testing.h>#include <LibGC/Weak.h>#include <LibGC/WeakInlines.h>#include <LibTest/TestCase.h>@@ -29,13 +30,6 @@ static GC::Heap& test_heap()return *heap;}-static NEVER_INLINE void scrub_stack()-{- u8 volatile filler[8 * KiB];- for (size_t i = 0; i < sizeof(filler); ++i)- filler[i] = 0;-}-TEST_SETUP{GC::Heap::set_default_heap_for_testing(test_heap());@@ -93,12 +87,12 @@ TEST_CASE(conservative_scanner_decodes_caged_value)EXPECT(boxed_value != 0);}- scrub_stack();+ GC::scrub_stack_for_testing();heap.collect_garbage();EXPECT(weak_cell);boxed_value = 0;- scrub_stack();+ GC::scrub_stack_for_testing();heap.collect_garbage(GC::Heap::CollectionType::CollectEverything);EXPECT(!weak_cell);}