Truly independent web browser
LibGC+LibJS: Free weak blocks when a heap goes away
Andreas Klingff03b9e5b1ad
diff --git a/Libraries/LibGC/Heap.cpp b/Libraries/LibGC/Heap.cppindex bfea6f07aa9..b7133d71ae5 100644--- a/Libraries/LibGC/Heap.cpp+++ b/Libraries/LibGC/Heap.cpp@@ -334,6 +334,7 @@ Heap::Heap(AK::Function<void(HashMap<Cell*, GC::HeapRoot>&)> gather_embedder_rooHeap::~Heap(){collect_garbage(CollectionType::CollectEverything);+ free_weak_blocks();while (auto* allocator = m_all_cell_allocators.first())m_all_cell_allocators.remove(*allocator);@@ -348,6 +349,20 @@ Heap::~Heap()m_incoming_cross_heap_members.clear();}+void Heap::free_weak_blocks()+{+ // NB: A weak pointer outside the heap may outlive it, so a block that one still refers to has to stay. Its pointers+ // are all null by now, and it is leaked once this heap forgets about it.+ auto free_blocks = [](WeakBlock::List& blocks) {+ while (auto* block = blocks.take_first()) {+ if (!block->has_referenced_impls())+ WeakBlock::destroy(block);+ }+ };+ free_blocks(m_usable_weak_blocks);+ free_blocks(m_full_weak_blocks);+}+void Heap::enable_stats(){VERIFY(!m_stats);@@ -2061,7 +2076,6 @@ WeakImpl* Heap::create_weak_impl(void* ptr)return &WeakImpl::the_null_weak_impl;if (m_usable_weak_blocks.is_empty()) {- // NOTE: These are leaked on Heap destruction, but that's fine since Heap is tied to process lifetime.auto* weak_block = WeakBlock::create();m_usable_weak_blocks.append(*weak_block);}diff --git a/Libraries/LibGC/Heap.h b/Libraries/LibGC/Heap.hindex 6f62b6f9122..5bd3cafc732 100644--- a/Libraries/LibGC/Heap.h+++ b/Libraries/LibGC/Heap.h@@ -290,6 +290,7 @@ private:void finalize_unmarked_cells();void sweep_dead_cells(bool print_report, Core::ElapsedTimer const&);void sweep_weak_blocks();+ void free_weak_blocks();void run_post_gc_tasks();bool sweep_next_block();diff --git a/Libraries/LibGC/WeakBlock.cpp b/Libraries/LibGC/WeakBlock.cppindex a4cc81dd773..53b8651ca9e 100644--- a/Libraries/LibGC/WeakBlock.cpp+++ b/Libraries/LibGC/WeakBlock.cpp@@ -18,6 +18,12 @@ WeakBlock* WeakBlock::create()return new (block) WeakBlock;}+void WeakBlock::destroy(WeakBlock* block)+{+ block->~WeakBlock();+ MUST(Core::System::release_address_space(block, WeakBlock::BLOCK_SIZE));+}+WeakBlock::WeakBlock(){for (size_t i = 0; i < IMPL_COUNT; ++i) {@@ -29,6 +35,15 @@ WeakBlock::WeakBlock()WeakBlock::~WeakBlock() = default;+bool WeakBlock::has_referenced_impls() const+{+ for (auto const& impl : m_impls) {+ if (impl.ref_count() != 0)+ return true;+ }+ return false;+}+WeakImpl* WeakBlock::allocate(Cell* cell){auto* impl = m_freelist;diff --git a/Libraries/LibGC/WeakBlock.h b/Libraries/LibGC/WeakBlock.hindex c475ddd0e60..384ab576ad0 100644--- a/Libraries/LibGC/WeakBlock.h+++ b/Libraries/LibGC/WeakBlock.h@@ -17,11 +17,13 @@ public:static constexpr size_t BLOCK_SIZE = 16 * KiB;static WeakBlock* create();+ static void destroy(WeakBlock*);WeakImpl* allocate(Cell*);void deallocate(WeakImpl*);bool can_allocate() const { return m_freelist != nullptr; }+ bool has_referenced_impls() const;void sweep();diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rsindex e26b918c40b..27030b24fbd 100644--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs@@ -2082,6 +2082,9 @@ impl Drop for Vm {fn drop(&mut self) {// The debugger holds weak references to executables, which go away with the heap.drop(self.debugger.take());+ // So do these caches of shapes. A weak reference that outlives the heap also keeps its weak block allocated.+ self.json_parse_cache = JsonParseCache::default();+ self.json_stringify_cache = JsonStringifyCache::new();// Compile jobs that are still running are abandoned; their results are dropped when they finish.self.jit.abandon_compile_jobs();// The heap's final collection destroys every cell, so it has to happen while the rest of the VM exists.