Truly independent web browser
LibWeb: Serialize a WebAssembly.Module with its original bytes
Andreas Klinge01020596541
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/module.rs b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/module.rsindex a864199a9ad..585179f9994 100644--- a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/module.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/module.rs@@ -89,9 +89,13 @@ fn kind_of_import(description: &ImportDescription) -> ExternalKind {#[derive(Trace)]#[gc(crate = libjs_rust)]pub(crate) struct Module {- // [[Module]], which keeps the bytes it was compiled from: [[Bytes]].+ // [[Module]]#[gc(untraced)]module: CompiledModule,+ // [[Bytes]]+ // NB: The compiled module can't provide these, as the validator quickens the code in its own copy of the bytes.+ #[gc(untraced)]+ bytes: Vec<u8>,// [[BuiltinSets]]#[gc(untraced)]builtin_sets: Vec<UsvString>,@@ -106,18 +110,19 @@ impl Module {/// https://webassembly.github.io/spec/js-api/#construct-a-webassembly-module-objectpub(crate) fn new(module: CompiledModule,+ bytes: Vec<u8>,builtin_sets: Vec<UsvString>,imported_string_module: Option<UsvString>,) -> Self {// 1. Let moduleObject be a new Module.// 2. Set moduleObject.[[Module]] to module.// 3. Set moduleObject.[[Bytes]] to bytes.- // NB: The compiled module keeps the bytes it was compiled from.// 4. Set moduleObject.[[BuiltinSets]] to builtinSetNames.// 5. Set moduleObject.[[ImportedStringModule]] to importedStringModule.// 6. Return moduleObject.Self {module,+ bytes,builtin_sets,imported_string_module,}@@ -163,12 +168,12 @@ impl Module {// 8. Set this.[[Bytes]] to stableBytes.// 9. Set this.[[BuiltinSets]] to builtinSetNames.// 10. Set this.[[ImportedStringModule]] to importedStringModule.- Ok(Self::new(module, builtin_set_names, imported_string_module))- }-- /// [[Bytes]]: the bytes the module was compiled from.- fn bytes(&self) -> &[u8] {- &self.module.bytes+ Ok(Self::new(+ module,+ stable_bytes,+ builtin_set_names,+ imported_string_module,+ ))}pub(crate) fn from_value(value: Value) -> Option<Ref<Self>> {@@ -307,7 +312,7 @@ impl Serializable for Module {}// 2. Set serialized.[[Bytes]] to the sub-serialization of value.[[Bytes]].- serialized.write_bytes(self.bytes());+ serialized.write_bytes(&self.bytes);// 3. Set serialized.[[AgentCluster]] to the current Realm’s corresponding agent cluster.serialized.write_optional_u64(current_agent_cluster());@@ -332,6 +337,6 @@ impl Serializable for Module {// 4. Compile a WebAssembly module from bytes and set value.[[Module]] to the result.let module = compile_a_webassembly_module(cx.vm(), cx.realm(), &bytes)?;- Ok(Self::new(module, Vec::new(), None))+ Ok(Self::new(module, bytes, Vec::new(), None))}}diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/module_record.rs b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/module_record.rsindex 470c9a0ec12..6a4a22e45e1 100644--- a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/module_record.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/module_record.rs@@ -174,7 +174,7 @@ fn parse_a_webassembly_module(.collect();// 4. Construct a WebAssembly module object from module and bytes, and let module be the result.- let module_source = allocate(vm, |_| Module::new(module, Vec::new(), None));+ let module_source = allocate(vm, |_| Module::new(module, stable_bytes, Vec::new(), None));// 8. Let moduleRecord be { [[Instance]]: ~empty~, [[Realm]]: realm, [[Environment]]: ~empty~,// [[Namespace]]: ~empty~, [[ModuleSource]]: module, [[HostDefined]]: hostDefined,diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/web_assembly.rs b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/web_assembly.rsindex 2454264765f..128df518040 100644--- a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/web_assembly.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/web_assembly.rs@@ -121,8 +121,6 @@ fn asynchronously_compile_a_webassembly_module(let realm = vm.current_realm().expect("the steps run in the realm of the promise");let result = compile_a_webassembly_module(vm, realm, &job.bytes.borrow()).map_err(|throw| throw.value());*job.result.borrow_mut() = Some(result);- // NB: The compiled module keeps the bytes it was compiled from.- job.bytes.take();// 2. Queue a task on taskSource, if provided, to perform the following steps:queue_a_task(vm, realm, task_source, (promise, job), |vm, (promise, job)| {@@ -165,8 +163,10 @@ fn asynchronously_compile_a_webassembly_module(else {// 1. Construct a WebAssembly module object from module, bytes, builtinSetNames, importedStringModule, and// let moduleObject be the result.- let module_object =- allocate(vm, |_| Module::new(module, builtin_set_names, imported_string_module));+ let bytes = job.bytes.take();+ let module_object = allocate(vm, |_| {+ Module::new(module, bytes, builtin_set_names, imported_string_module)+ });// 2. Resolve promise with moduleObject.let cx = CallContext::new(vm, realm);diff --git a/Tests/LibWeb/Text/expected/Wasm/WebAssembly-Module-serialization-of-quickened-code.txt b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-Module-serialization-of-quickened-code.txtnew file mode 100644index 00000000000..24dc997a67b--- /dev/null+++ b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-Module-serialization-of-quickened-code.txt@@ -0,0 +1,2 @@+structuredClone: 1000+worker: 1000diff --git a/Tests/LibWeb/Text/input/Wasm/WebAssembly-Module-serialization-of-quickened-code.html b/Tests/LibWeb/Text/input/Wasm/WebAssembly-Module-serialization-of-quickened-code.htmlnew file mode 100644index 00000000000..158b80300e8--- /dev/null+++ b/Tests/LibWeb/Text/input/Wasm/WebAssembly-Module-serialization-of-quickened-code.html@@ -0,0 +1,32 @@+<!doctype html>+<script type="worker">+ onmessage = ({ data }) => postMessage(new WebAssembly.Instance(data).exports.f());+ onmessageerror = () => postMessage("messageerror");+</script>+<script src="../include.js"></script>+<script>+ asyncTest(done => {+ // (module (func (export "f") (result i32) (i32.const 1000)))+ // NB: The validator rewrites a constant whose immediate takes more than one byte.+ const bytes = new Uint8Array([+ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x05, 0x01, 0x60, 0x00, 0x01, 0x7f, 0x03,+ 0x02, 0x01, 0x00, 0x07, 0x05, 0x01, 0x01, 0x66, 0x00, 0x00, 0x0a, 0x07, 0x01, 0x05, 0x00, 0x41,+ 0xe8, 0x07, 0x0b,+ ]);+ const module = new WebAssembly.Module(bytes);++ try {+ println(`structuredClone: ${new WebAssembly.Instance(structuredClone(module)).exports.f()}`);+ } catch (error) {+ println(`structuredClone: ${error}`);+ }++ const source = document.querySelector("[type=worker]").innerText;+ const worker = new Worker(URL.createObjectURL(new Blob([source], { type: "text/javascript" })));+ worker.onmessage = ({ data }) => {+ println(`worker: ${data}`);+ done();+ };+ worker.postMessage(module);+ });+</script>