From ab404a2f8861a30ccc8e200ca06399b23add453c Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 16 Mar 2020 19:08:59 +0100 Subject: [PATCH] LibJS: Implement basic conservative garbage collection We now scan the stack and CPU registers for potential pointers into the GC heap, and include any valid Cell pointers in the set of roots. This works pretty well but we'll also need to solve marking of things passed to native functions, since those are currently in Vector and the Vector storage is on the heap (not scanned.) --- Libraries/LibJS/Heap/Heap.cpp | 72 +++++++++++++++++++++++++++++++- Libraries/LibJS/Heap/Heap.h | 3 ++ Libraries/LibJS/Heap/HeapBlock.h | 8 ++++ 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/Libraries/LibJS/Heap/Heap.cpp b/Libraries/LibJS/Heap/Heap.cpp index 9838242a1b3..31707a11508 100644 --- a/Libraries/LibJS/Heap/Heap.cpp +++ b/Libraries/LibJS/Heap/Heap.cpp @@ -30,6 +30,9 @@ #include #include #include +#include +#include +#include #define HEAP_DEBUG @@ -71,14 +74,76 @@ void Heap::gather_roots(HashTable& roots) { m_interpreter.gather_roots({}, roots); + gather_conservative_roots(roots); + #ifdef HEAP_DEBUG - dbg() << "collect_roots:"; + dbg() << "gather_roots:"; for (auto* root : roots) { dbg() << " + " << root; } #endif } +void Heap::gather_conservative_roots(HashTable& roots) +{ + FlatPtr dummy; + +#ifdef HEAP_DEBUG + dbg() << "gather_conservative_roots:"; +#endif + + jmp_buf buf; + setjmp(buf); + + HashTable possible_pointers; + + for (size_t i = 0; i < (sizeof(buf->regs) / sizeof(FlatPtr)); ++i) + possible_pointers.set(buf->regs[i]); + + FlatPtr stack_base; + size_t stack_size; + if (get_stack_bounds(&stack_base, &stack_size) < 0) { + perror("get_stack_bounds"); + ASSERT_NOT_REACHED(); + } + + FlatPtr stack_reference = reinterpret_cast(&dummy); + FlatPtr stack_top = stack_base + stack_size; + + for (FlatPtr stack_address = stack_reference; stack_address < stack_top; stack_address += sizeof(FlatPtr)) { + auto data = *reinterpret_cast(stack_address); + possible_pointers.set(data); + } + + for (auto possible_pointer : possible_pointers) { + if (!possible_pointer) + continue; +#ifdef HEAP_DEBUG + dbg() << " ? " << (const void*)possible_pointer; +#endif + if (auto* cell = cell_from_possible_pointer(possible_pointer)) { + if (cell->is_live()) { +#ifdef HEAP_DEBUG + dbg() << " ?-> " << (const void*)cell; +#endif + roots.set(cell); + } else { +#ifdef HEAP_DEBUG + dbg() << " #-> " << (const void*)cell; +#endif + } + } + } +} + +Cell* Heap::cell_from_possible_pointer(FlatPtr pointer) +{ + auto* possible_heap_block = HeapBlock::from_cell(reinterpret_cast(pointer)); + if (m_blocks.find([possible_heap_block](auto& block) { return block.ptr() == possible_heap_block; }) == m_blocks.end()) + return nullptr; + return possible_heap_block->cell_from_possible_pointer(pointer); +} + class MarkingVisitor final : public Cell::Visitor { public: MarkingVisitor() {} @@ -101,8 +166,11 @@ void Heap::mark_live_cells(const HashTable& roots) dbg() << "mark_live_cells:"; #endif MarkingVisitor visitor; - for (auto* root : roots) + for (auto* root : roots) { + if (!root) + continue; visitor.visit(root); + } } void Heap::sweep_dead_cells() diff --git a/Libraries/LibJS/Heap/Heap.h b/Libraries/LibJS/Heap/Heap.h index 8366c808988..691c362f357 100644 --- a/Libraries/LibJS/Heap/Heap.h +++ b/Libraries/LibJS/Heap/Heap.h @@ -59,9 +59,12 @@ private: Cell* allocate_cell(size_t); void gather_roots(HashTable&); + void gather_conservative_roots(HashTable&); void mark_live_cells(const HashTable& live_cells); void sweep_dead_cells(); + Cell* cell_from_possible_pointer(FlatPtr); + Interpreter& m_interpreter; Vector> m_blocks; }; diff --git a/Libraries/LibJS/Heap/HeapBlock.h b/Libraries/LibJS/Heap/HeapBlock.h index f1cfd2674c7..ecb1266eb64 100644 --- a/Libraries/LibJS/Heap/HeapBlock.h +++ b/Libraries/LibJS/Heap/HeapBlock.h @@ -61,6 +61,14 @@ public: return reinterpret_cast((FlatPtr)cell & ~(block_size - 1)); } + Cell* cell_from_possible_pointer(FlatPtr pointer) + { + if (pointer < reinterpret_cast(m_storage)) + return nullptr; + size_t cell_index = (pointer - reinterpret_cast(m_storage)) / m_cell_size; + return cell(cell_index); + } + private: HeapBlock(Heap&, size_t cell_size);