Truly independent web browser
LibWeb: Remove finalized WebAssembly memories from their cache
Andreas Kling3923628ab96b
diff --git a/Libraries/LibWeb/WebAssembly/Memory.cpp b/Libraries/LibWeb/WebAssembly/Memory.cppindex 7d8ad7df5aa..21a1f4cd2b2 100644--- a/Libraries/LibWeb/WebAssembly/Memory.cpp+++ b/Libraries/LibWeb/WebAssembly/Memory.cpp@@ -315,6 +315,20 @@ void Memory::visit_edges(Visitor& visitor)visitor.ignore(m_cache);}+void Memory::finalize()+{+ Base::finalize();++ // NB: A buffer can keep the cache alive after its realm is gone, so the cache can outlive this Memory. Remove our+ // entry and grow hook before this cell is reused.+ auto entry = m_cache->get_memory_instance(m_address);+ if (!entry.has_value() || entry.value().ptr() != this)+ return;+ m_cache->remove_memory_instance(m_address);+ if (auto* memory = m_cache->abstract_machine().store().get(m_address))+ memory->successful_grow_hook = nullptr;+}+// https://webassembly.github.io/spec/js-api/#dom-memory-growWebIDL::ExceptionOr<u32> Memory::grow(u32 delta){diff --git a/Libraries/LibWeb/WebAssembly/Memory.h b/Libraries/LibWeb/WebAssembly/Memory.hindex 3572139fc2e..34249c565e8 100644--- a/Libraries/LibWeb/WebAssembly/Memory.h+++ b/Libraries/LibWeb/WebAssembly/Memory.h@@ -54,6 +54,7 @@ private:Memory(NonnullRefPtr<Detail::WebAssemblyCache>, Wasm::MemoryAddress, Shared shared);virtual void visit_edges(Visitor&) override;+ virtual void finalize() override;NonnullRefPtr<Detail::WebAssemblyCache> m_cache;Wasm::MemoryAddress m_address;diff --git a/Libraries/LibWeb/WebAssembly/WebAssembly.cpp b/Libraries/LibWeb/WebAssembly/WebAssembly.cppindex a5ba01fb625..fa9721674c8 100644--- a/Libraries/LibWeb/WebAssembly/WebAssembly.cpp+++ b/Libraries/LibWeb/WebAssembly/WebAssembly.cpp@@ -563,44 +563,46 @@ JS::ThrowCompletionOr<JS::HandledByHost> host_resize_array_buffer(JS::VM& vm, JSauto detach_key = buffer.detach_key();if (detach_key.is_string() && detach_key.as_string() == JS::PrimitiveString::create(vm, "WebAssembly.Memory"_utf16_fly_string)) {// 3. For each memaddr → mem in all Memory object caches,- bool seen = false;+ GC::Ptr<Memory> memory;for (auto* cache : live_caches()) {auto const& map = cache->memory_instances();for (auto const& entry : map) {- auto memory = entry.value;// 1. If SameValue(mem.[[BufferObject]], buffer) is true,- if (memory_has_buffer_object(*memory, buffer)) {+ if (memory_has_buffer_object(*entry.value, buffer)) {// 2. Assert: buffer is the [[BufferObject]] of exactly one value in map.- VERIFY(!seen);- seen = true;+ VERIFY(!memory);+ memory = entry.value;+ }+ }+ }- // 1. Assert: buffer.[[ArrayBufferByteLength]] modulo 65536 is 0.- VERIFY(buffer.byte_length() % Wasm::Constants::page_size == 0);+ // 2. Assert: buffer is the [[BufferObject]] of exactly one value in map.+ VERIFY(memory);- // 2. Let lengthDelta be newLength - buffer.[[ArrayBufferByteLength]].- auto length_delta = new_length - buffer.byte_length();+ // NB: We perform the following steps for mem after the loop above, because growing mem can collect garbage, and+ // that removes finalized Memory objects from the caches we iterate.- // 3. If lengthDelta < 0 or lengthDelta modulo 65536 is not 0,- if (new_length < buffer.byte_length() || length_delta % Wasm::Constants::page_size != 0) {- // 1. Throw a RangeError exception.- return vm.throw_completion<JS::RangeError>("WebAssembly.Memory buffers must be resized by a multiple of the page size"sv);- }+ // 1. Assert: buffer.[[ArrayBufferByteLength]] modulo 65536 is 0.+ VERIFY(buffer.byte_length() % Wasm::Constants::page_size == 0);- // 4. Let delta be lengthDelta ÷ 65536.- auto delta = length_delta / Wasm::Constants::page_size;+ // 2. Let lengthDelta be newLength - buffer.[[ArrayBufferByteLength]].+ auto length_delta = new_length - buffer.byte_length();- // 5. Grow the memory buffer associated with memaddr by delta.- // FIXME: "Grow the memory buffer" is a separate algorithm from the Memory#grow() method.- TRY(WebIDL::throw_dom_exception_if_needed(vm, buffer.shape().realm(), [&] {- return memory->grow(delta);- }));- }- }+ // 3. If lengthDelta < 0 or lengthDelta modulo 65536 is not 0,+ if (new_length < buffer.byte_length() || length_delta % Wasm::Constants::page_size != 0) {+ // 1. Throw a RangeError exception.+ return vm.throw_completion<JS::RangeError>("WebAssembly.Memory buffers must be resized by a multiple of the page size"sv);}- // 2. Assert: buffer is the [[BufferObject]] of exactly one value in map.- VERIFY(seen);+ // 4. Let delta be lengthDelta ÷ 65536.+ auto delta = length_delta / Wasm::Constants::page_size;++ // 5. Grow the memory buffer associated with memaddr by delta.+ // FIXME: "Grow the memory buffer" is a separate algorithm from the Memory#grow() method.+ TRY(WebIDL::throw_dom_exception_if_needed(vm, buffer.shape().realm(), [&] {+ return memory->grow(delta);+ }));// 4. Return handled.return JS::HandledByHost::Handled;@@ -619,44 +621,46 @@ JS::ThrowCompletionOr<JS::HandledByHost> host_grow_shared_array_buffer(JS::VM& vauto current_byte_length = buffer.byte_length();// 3. For each memaddr → mem in all Memory object caches,- bool seen = false;+ GC::Ptr<Memory> memory;for (auto* cache : live_caches()) {auto const& map = cache->memory_instances();for (auto const& entry : map) {- auto memory = entry.value;// 1. If SameValue(mem.[[BufferObject]], buffer) is true,- if (memory_has_buffer_object(*memory, buffer)) {+ if (memory_has_buffer_object(*entry.value, buffer)) {// 2. Assert: buffer is the [[BufferObject]] of exactly one value in map.- VERIFY(!seen);- seen = true;+ VERIFY(!memory);+ memory = entry.value;+ }+ }+ }- // 1. Assert: buffer.[[ArrayBufferByteLength]] modulo 65536 is 0.- VERIFY(buffer.byte_length() % Wasm::Constants::page_size == 0);+ // 2. Assert: buffer is the [[BufferObject]] of exactly one value in map.+ VERIFY(memory);- // 2. Let lengthDelta be newLength - buffer.[[ArrayBufferByteLength]].- auto length_delta = new_length - buffer.byte_length();+ // NB: We perform the following steps for mem after the loop above, because growing mem can collect garbage, and+ // that removes finalized Memory objects from the caches we iterate.- // 3. If lengthDelta < 0 or lengthDelta modulo 65536 is not 0,- if (new_length < buffer.byte_length() || length_delta % Wasm::Constants::page_size != 0) {- // 1. Throw a RangeError exception.- return vm.throw_completion<JS::RangeError>("WebAssembly.Memory buffers must be resized by a multiple of the page size"sv);- }+ // 1. Assert: buffer.[[ArrayBufferByteLength]] modulo 65536 is 0.+ VERIFY(buffer.byte_length() % Wasm::Constants::page_size == 0);- // 4. Let delta be lengthDelta ÷ 65536.- auto delta = length_delta / Wasm::Constants::page_size;+ // 2. Let lengthDelta be newLength - buffer.[[ArrayBufferByteLength]].+ auto length_delta = new_length - buffer.byte_length();- // 5. Grow the memory buffer associated with memaddr by delta.- // FIXME: "Grow the memory buffer" is a separate algorithm from the Memory#grow() method.- TRY(WebIDL::throw_dom_exception_if_needed(vm, buffer.shape().realm(), [&] {- return memory->grow(delta);- }));- }- }+ // 3. If lengthDelta < 0 or lengthDelta modulo 65536 is not 0,+ if (new_length < buffer.byte_length() || length_delta % Wasm::Constants::page_size != 0) {+ // 1. Throw a RangeError exception.+ return vm.throw_completion<JS::RangeError>("WebAssembly.Memory buffers must be resized by a multiple of the page size"sv);}- // 2. Assert: buffer is the [[BufferObject]] of exactly one value in map.- VERIFY(seen);+ // 4. Let delta be lengthDelta ÷ 65536.+ auto delta = length_delta / Wasm::Constants::page_size;++ // 5. Grow the memory buffer associated with memaddr by delta.+ // FIXME: "Grow the memory buffer" is a separate algorithm from the Memory#grow() method.+ TRY(WebIDL::throw_dom_exception_if_needed(vm, buffer.shape().realm(), [&] {+ return memory->grow(delta);+ }));// 4. If newLength < buffer.[[ArrayBufferByteLength]] or newLength > buffer.[[ArrayBufferMaxByteLength]], throw a RangeError exception.if (new_length < current_byte_length)diff --git a/Libraries/LibWeb/WebAssembly/WebAssembly.h b/Libraries/LibWeb/WebAssembly/WebAssembly.hindex 80e3019782f..7112fe313d6 100644--- a/Libraries/LibWeb/WebAssembly/WebAssembly.h+++ b/Libraries/LibWeb/WebAssembly/WebAssembly.h@@ -68,6 +68,7 @@ public:}void add_global_instance(Wasm::GlobalAddress address, GC::Ptr<WebAssembly::Global> global) { m_global_instances.set(address, global); }void add_memory_instance(Wasm::MemoryAddress address, GC::Ptr<WebAssembly::Memory> memory) { m_memory_instances.set(address, memory); }+ void remove_memory_instance(Wasm::MemoryAddress address) { m_memory_instances.remove(address); }void add_table_instance(Wasm::TableAddress address, GC::Ptr<WebAssembly::Table> table) { m_table_instances.set(address, table); }Optional<GC::Ptr<JS::NativeFunction>> get_function_instance(Wasm::FunctionAddress address) { return m_function_instances.get(address); }diff --git a/Tests/LibWeb/Text/expected/Wasm/WebAssembly-memory-cache-finalization.txt b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-memory-cache-finalization.txtnew file mode 100644index 00000000000..2fb56587dd2--- /dev/null+++ b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-memory-cache-finalization.txt@@ -0,0 +1,2 @@+Retained buffer size: 65536+New memory buffers resized successfullydiff --git a/Tests/LibWeb/Text/input/Wasm/WebAssembly-memory-cache-finalization.html b/Tests/LibWeb/Text/input/Wasm/WebAssembly-memory-cache-finalization.htmlnew file mode 100644index 00000000000..840d08f18b7--- /dev/null+++ b/Tests/LibWeb/Text/input/Wasm/WebAssembly-memory-cache-finalization.html@@ -0,0 +1,31 @@+<!doctype html>+<script src="../include.js"></script>+<script>+ function createBufferFromRemovedFrame() {+ const frame = document.createElement("iframe");+ document.body.append(frame);+ const Memory = frame.contentWindow.WebAssembly.Memory;+ for (let index = 0; index < 8; ++index)+ new Memory({ initial: 0, maximum: 1 });++ // Keep the cache alive through a buffer whose realm survives the frame.+ const buffer = WebAssembly.Memory.prototype.toResizableBuffer.call(new Memory({ initial: 0, maximum: 1 }));+ frame.remove();+ return buffer;+ }++ asyncTest(async done => {+ const buffer = createBufferFromRemovedFrame();+ await animationFrame();+ await internals.gcAsync();++ for (let index = 0; index < 16; ++index) {+ const memory = new WebAssembly.Memory({ initial: 0, maximum: 1 });+ memory.toResizableBuffer().resize(65536);+ }+ buffer.resize(65536);+ println(`Retained buffer size: ${buffer.byteLength}`);+ println("New memory buffers resized successfully");+ done();+ });+</script>