Skip to content

Commit a9a0a31

Browse files
tlivelyMs2ger
authored andcommitted
[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.
1 parent f2d26ea commit a9a0a31

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

test/js-api/js-string/basic.any.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// META: script=/wasm/jsapi/wasm-module-builder.js
44
// META: script=/wasm/jsapi/js-string/polyfill.js
55

6+
// The list of builtins and their signatures.
7+
let builtins;
8+
69
// Generate two sets of exports, one from a polyfill implementation and another
710
// from the builtins provided by the host.
811
let polyfillExports;
@@ -14,7 +17,7 @@ setup(() => {
1417
// a known builtin function from wasm.
1518
const builder = new WasmModuleBuilder();
1619
const arrayIndex = builder.addArray(kWasmI16, true, kNoSuperType, true);
17-
const builtins = [
20+
builtins = [
1821
{
1922
name: "test",
2023
params: [kWasmExternRef],
@@ -381,3 +384,35 @@ test(() => {
381384
}
382385
}
383386
});
387+
388+
// Test that incorrect import types are rejected, even if they have correct
389+
// signatures.
390+
test(() => {
391+
for (let builtin of builtins) {
392+
const builder = new WasmModuleBuilder();
393+
// The type is wrong because it is in a nontrivial rec group.
394+
const typeIndex = builder.nextTypeIndex();
395+
builder.startRecGroup();
396+
builder.addType({
397+
params: builtin.params,
398+
results: builtin.results
399+
});
400+
builder.addStruct([]);
401+
builder.endRecGroup();
402+
403+
builder.addImport(
404+
"wasm:js-string",
405+
builtin.name,
406+
typeIndex);
407+
408+
const buffer = builder.toBuffer();
409+
410+
// Validation should fail.
411+
assert_false(WebAssembly.validate(buffer, { builtins: ["js-string"] }));
412+
413+
// Compilation should fail.
414+
assert_throws_js(WebAssembly.CompileError, () => {
415+
new WebAssembly.Module(buffer, { builtins: ["js-string"] });
416+
});
417+
}
418+
}, "Incorrect types");

test/js-api/wasm-module-builder.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,8 @@ class WasmModuleBuilder {
906906
return this.types.length - 1;
907907
}
908908

909+
nextTypeIndex() { return this.types.length; }
910+
909911
static defaultFor(type) {
910912
switch (type) {
911913
case kWasmI32:

0 commit comments

Comments
 (0)