Skip to content

Truly independent web browser

LibGC: Verify that no root outlives its heap

Andreas Klingb288e419ac65
diff --git a/Libraries/LibGC/Heap.cpp b/Libraries/LibGC/Heap.cpp
index 4cb3714173f..09f5ae3fa5d 100644
--- a/Libraries/LibGC/Heap.cpp
+++ b/Libraries/LibGC/Heap.cpp
@@ -336,6 +336,10 @@ Heap::~Heap()
{
VERIFY(is_on_owner_thread());
collect_garbage(CollectionType::CollectEverything);
+
+ // A root that outlives its heap would reach into freed memory when it goes.
+ VERIFY(m_roots.is_empty());
+
free_weak_blocks();
while (auto* allocator = m_all_cell_allocators.first())
diff --git a/Libraries/LibTest/JavaScriptTestHost.h b/Libraries/LibTest/JavaScriptTestHost.h
index df69d1f0f6e..5ba936e0e66 100644
--- a/Libraries/LibTest/JavaScriptTestHost.h
+++ b/Libraries/LibTest/JavaScriptTestHost.h
@@ -106,6 +106,8 @@ struct FunctionWithLength {
extern HashMap<Utf16String, FunctionWithLength> s_exposed_global_functions;
extern ByteString g_test_root_fragment;
extern void (*g_main_hook)();
+// Runs before the VM goes away, which nothing may keep roots into.
+extern void (*g_main_exit_hook)();
extern HashMap<bool*, Tuple<ByteString, ByteString, char>> g_extra_args;
struct ParserError {
diff --git a/Libraries/LibTest/JavaScriptTestRunnerMain.cpp b/Libraries/LibTest/JavaScriptTestRunnerMain.cpp
index 3a0986f20d5..f8a05579765 100644
--- a/Libraries/LibTest/JavaScriptTestRunnerMain.cpp
+++ b/Libraries/LibTest/JavaScriptTestRunnerMain.cpp
@@ -26,6 +26,7 @@ bool g_collect_on_every_allocation = false;
ByteString g_currently_running_test;
HashMap<Utf16String, FunctionWithLength> s_exposed_global_functions;
void (*g_main_hook)() = nullptr;
+void (*g_main_exit_hook)() = nullptr;
HashMap<bool*, Tuple<ByteString, ByteString, char>> g_extra_args;
IntermediateRunFileResult (*g_run_file)(ByteString const&, JS::Realm&, JS::ExecutionContext&) = nullptr;
ByteString g_test_root;
@@ -200,6 +201,9 @@ int main(int argc, char** argv)
Test::JS::TestRunner test_runner(test_root, common_path, print_times, print_progress, print_json, per_file, print_each_test);
test_runner.run(test_globs);
+ if (g_main_exit_hook)
+ g_main_exit_hook();
+
g_vm = nullptr;
return test_runner.counts().tests_failed > 0 ? 1 : 0;
diff --git a/Tests/LibWasm/test-wasm.cpp b/Tests/LibWasm/test-wasm.cpp
index bb090525076..17cb1987e35 100644
--- a/Tests/LibWasm/test-wasm.cpp
+++ b/Tests/LibWasm/test-wasm.cpp
@@ -136,7 +136,6 @@ public:
return entity;
}
- // NB: Like the last store, the last roots are left for the process to reclaim, since the heap is gone at exit.
static Vector<GC::Root<GC::ForeignCell>>& roots()
{
static NeverDestroyed<Vector<GC::Root<GC::ForeignCell>>> roots;
@@ -242,6 +241,11 @@ HashMap<ImportName, Wasm::Runtime::Extern> WebAssemblyModule::s_spec_test_namesp
}
+TESTJS_MAIN_HOOK()
+{
+ ::Test::JS::g_main_exit_hook = WebAssemblyModule::reset_store;
+}
+
TESTJS_RUN_FILE_FUNCTION(ByteString const&, JS::Realm&, JS::ExecutionContext&)
{
WebAssemblyModule::reset_store();