Truly independent web browser
LibGC: Destroy the captures of a GC::Function when it is swept
Andreas Klingf161a153353d
diff --git a/Libraries/LibGC/Function.h b/Libraries/LibGC/Function.hindex 9349fcfc03e..71676f3014b 100644--- a/Libraries/LibGC/Function.h+++ b/Libraries/LibGC/Function.h@@ -319,9 +319,10 @@ public:Function const& function() const { return *this; }- virtual void finalize() override+ // NB: The captured values die when the cell is swept, not when it is finalized, so that the finalizer of another dead+ // cell can still call this function.+ GC_ALLOW_CELL_DESTRUCTOR virtual ~Function() override{- Base::finalize();if (!m_handle.is_valid())return;auto& table = heap().function_table();diff --git a/Tests/LibGC/TestGCFunction.cpp b/Tests/LibGC/TestGCFunction.cppindex 9f10788f742..b24c02e80cf 100644--- a/Tests/LibGC/TestGCFunction.cpp+++ b/Tests/LibGC/TestGCFunction.cpp@@ -51,6 +51,31 @@ private:GC_DEFINE_ALLOCATOR(CellWithFunction);+class CellCallingFunctionWhenFinalized final : public GC::Cell {+ GC_CELL(CellCallingFunctionWhenFinalized, GC::Cell);+ GC_DECLARE_ALLOCATOR(CellCallingFunctionWhenFinalized);++public:+ GC::Ptr<GC::Function<void()>> function;++private:+ CellCallingFunctionWhenFinalized() = default;++ virtual void visit_edges(Visitor& visitor) override+ {+ Base::visit_edges(visitor);+ visitor.visit(function);+ }++ virtual void finalize() override+ {+ Base::finalize();+ function->function()();+ }+};++GC_DEFINE_ALLOCATOR(CellCallingFunctionWhenFinalized);+struct DestructionFlag {GC_HOLDS_NO_CELL_EDGES;@@ -72,6 +97,17 @@ struct RunState : RefCounted<RunState> {bool survived_collection { false };};+struct CalledFlag : RefCounted<CalledFlag> {+ GC_HOLDS_NO_CELL_EDGES;++ explicit CalledFlag(bool& called)+ : called(called)+ {+ }++ bool& called;+};+struct CountedWithCell : RefCounted<CountedWithCell> {GC::Ptr<TestCell> cell;};@@ -145,6 +181,15 @@ NEVER_INLINE GC::Weak<GC::Function<void()>> make_unrooted_function_owning(GC::Hereturn function.ptr();}+NEVER_INLINE void make_unrooted_cell_calling_function_when_finalized(GC::Heap& heap, bool& called)+{+ // NB: Finalizers run in no particular order. The function is made first, which here finalizes it first.+ auto function = GC::create_function(heap, GC::captures(make_ref_counted<CalledFlag>(called)), [](NonnullRefPtr<CalledFlag>& flag) {+ flag->called = true;+ });+ heap.allocate<CellCallingFunctionWhenFinalized>()->function = function;+}+}TEST_CASE(captured_cell_is_traced)@@ -208,6 +253,19 @@ TEST_CASE(captures_are_destroyed_with_the_function)EXPECT(destroyed);}+TEST_CASE(dead_function_can_be_called_by_the_finalizer_of_another_dead_cell)+{+ GC::Heap heap([](auto&) { }, GC::Heap::BecomeThreadDefault::No);+ heap.set_incremental_sweep_enabled(false);++ bool called = false;+ make_unrooted_cell_calling_function_when_finalized(heap, called);+ GC::scrub_stack_for_testing();+ heap.collect_garbage();++ EXPECT(called);+}+TEST_CASE(captures_taken_as_auto_with_a_named_function_type){GC::Heap heap([](auto&) { }, GC::Heap::BecomeThreadDefault::No);