From e1f416975db1c1cbb402b4c742f55d6b300e4e40 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Fri, 16 Jan 2026 17:44:08 -0800 Subject: [PATCH] [js-api] Test validation of string builtin types Validation should fail if string builtin imports do not have the expected types. Add a test for this validation. --- test/js-api/js-string/basic.any.js | 37 +++++++++++++++++++++++++++++- test/js-api/wasm-module-builder.js | 2 ++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/test/js-api/js-string/basic.any.js b/test/js-api/js-string/basic.any.js index de4a21c976..b6f528725d 100644 --- a/test/js-api/js-string/basic.any.js +++ b/test/js-api/js-string/basic.any.js @@ -3,6 +3,9 @@ // META: script=/wasm/jsapi/wasm-module-builder.js // META: script=/wasm/jsapi/js-string/polyfill.js +// The list of builtins and their signatures. +let builtins; + // Generate two sets of exports, one from a polyfill implementation and another // from the builtins provided by the host. let polyfillExports; @@ -14,7 +17,7 @@ setup(() => { // a known builtin function from wasm. const builder = new WasmModuleBuilder(); const arrayIndex = builder.addArray(kWasmI16, true, kNoSuperType, true); - const builtins = [ + builtins = [ { name: "test", params: [kWasmExternRef], @@ -381,3 +384,35 @@ test(() => { } } }); + +// Test that incorrect import types are rejected, even if they have correct +// signatures. +test(() => { + for (let builtin of builtins) { + const builder = new WasmModuleBuilder(); + // The type is wrong because it is in a nontrivial rec group. + const typeIndex = builder.nextTypeIndex(); + builder.startRecGroup(); + builder.addType({ + params: builtin.params, + results: builtin.results + }); + builder.addStruct([]); + builder.endRecGroup(); + + builder.addImport( + "wasm:js-string", + builtin.name, + typeIndex); + + const buffer = builder.toBuffer(); + + // Validation should fail. + assert_false(WebAssembly.validate(buffer, { builtins: ["js-string"] })); + + // Compilation should fail. + assert_throws_js(WebAssembly.CompileError, () => { + new WebAssembly.Module(buffer, { builtins: ["js-string"] }); + }); + } +}, "Incorrect types"); diff --git a/test/js-api/wasm-module-builder.js b/test/js-api/wasm-module-builder.js index babec0fbe4..82687c89c6 100644 --- a/test/js-api/wasm-module-builder.js +++ b/test/js-api/wasm-module-builder.js @@ -906,6 +906,8 @@ class WasmModuleBuilder { return this.types.length - 1; } + nextTypeIndex() { return this.types.length; } + static defaultFor(type) { switch (type) { case kWasmI32: