Truly independent web browser
LibWasm+Tests: Require the host values of references to be cells
Andreas Kling49499258cdf8
diff --git a/Libraries/LibWasm/Engine/src/runtime/gc.rs b/Libraries/LibWasm/Engine/src/runtime/gc.rsindex a6c2261f6df..f68ab33523e 100644--- a/Libraries/LibWasm/Engine/src/runtime/gc.rs+++ b/Libraries/LibWasm/Engine/src/runtime/gc.rs@@ -9,8 +9,8 @@//! References in the any hierarchy are encoded so that their kind shows in their low bits: zero is null, an i31 has//! bit 0 set (its value is in the bits above), a host value converted from an externref has bits 1:0 equal to 0b10,//! and GC objects and exceptions are the addresses of their cells. Converting between externref and anyref keeps the-//! bits. An embedder's host values should be the addresses of its cells with bit 1 set, so that the collector keeps-//! them alive; other host values are never followed.+//! bits. An embedder's host values must be the addresses of its cells with bit 1 set, which the collector follows as+//! it does any other cell.use super::heap::{CELL_HEADER_SIZE, CellType, Heap, TRACE_FUNCTION, TRACE_NONE, TRACE_REFERENCE, Visitor, forget_host_object,diff --git a/Libraries/LibWasm/Engine/src/runtime/heap.rs b/Libraries/LibWasm/Engine/src/runtime/heap.rsindex d33e45f8959..9261be6baa9 100644--- a/Libraries/LibWasm/Engine/src/runtime/heap.rs+++ b/Libraries/LibWasm/Engine/src/runtime/heap.rs@@ -483,17 +483,7 @@ impl Visitor {if reference == 0 || reference & 1 != 0 {return;}- if reference & 3 == 2 {- // An embedder's host value, which LibGC only follows if it points into a cell.- unsafe {- ((*self.interface).gc_visitor_visit_possible_values)(- self.raw,- (&raw const reference).cast(),- size_of::<u64>(),- );- }- return;- }- self.visit_cell(reference as *mut u8);+ // NB: An embedder's host value is the address of one of its cells, with bit 1 set.+ self.visit_cell((reference & !3) as *mut u8);}}diff --git a/Tests/LibWasm/test-wasm.cpp b/Tests/LibWasm/test-wasm.cppindex 17cb1987e35..ab6547c9398 100644--- a/Tests/LibWasm/test-wasm.cpp+++ b/Tests/LibWasm/test-wasm.cpp@@ -9,6 +9,8 @@#include <AK/NeverDestroyed.h>#include <LibCore/File.h>#include <LibGC/CellAllocator.h>+#include <LibGC/Heap.h>+#include <LibGC/Root.h>#include <LibJS/Heap/Cell.h>#include <LibJS/HostClassBuilder.h>#include <LibJS/Runtime/Array.h>@@ -68,18 +70,41 @@ namespace {static constexpr JSHostClass web_assembly_module_host_class = JS::make_host_class(JS_HOST_CLASS_OBJECT, "WebAssemblyModule"sv, nullptr, nullptr, nullptr, 0);-// Host references (`ref.extern n` in the spec tests) are encoded so that they are never null, and never look like a-// pointer or an i31 reference.+// The value of a host reference (`ref.extern n` in the spec tests). A host reference is the address of a cell with bit+// 1 set, which the garbage collector follows.+class HostValue final : public GC::Cell {+ GC_CELL(HostValue, GC::Cell);+ GC_DECLARE_ALLOCATOR(HostValue);++public:+ explicit HostValue(u64 value)+ : value(value)+ {+ }++ u64 value { 0 };+};++GC_DEFINE_ALLOCATOR(HostValue);++// NB: Each value has one cell, so that equal values are equal references. The cells live until the store is reset.+static HashMap<u64, GC::Root<HostValue>>& host_values()+{+ static NeverDestroyed<HashMap<u64, GC::Root<HostValue>>> host_values;+ return *host_values;+}+static u64 host_reference_bits(u64 value){- return (value << 2) | 2;+ auto& cell = host_values().ensure(value, [&] { return GC::make_root(GC::Heap::the().allocate<HostValue>(value)); });+ return bit_cast<FlatPtr>(cell.ptr()) | 2;}static Optional<u64> host_reference_value(u64 bits){if ((bits & 3) != 2)return {};- return bits >> 2;+ return bit_cast<HostValue*>(static_cast<FlatPtr>(bits & ~u64(3)))->value;}struct ImportName {@@ -123,6 +148,7 @@ public:{s_spec_test_namespace.clear();roots().clear();+ host_values().clear();delete s_store;s_store = nullptr;}