Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve WASI start() coverage #30972

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 69 additions & 9 deletions test/wasi/test-wasi-start-validation.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

require('../common');
const common = require('../common');
const assert = require('assert');
const { WASI } = require('wasi');

const fixtures = require('../common/fixtures');
const bufferSource = fixtures.readSync('simple.wasm');

{
const wasi = new WASI();
Expand All @@ -16,16 +17,75 @@ const fixtures = require('../common/fixtures');
);
}

{
(async () => {
const wasi = new WASI({});
(async () => {
const bufferSource = fixtures.readSync('simple.wasm');
const wasm = await WebAssembly.compile(bufferSource);
const instance = await WebAssembly.instantiate(wasm);
const wasm = await WebAssembly.compile(bufferSource);
const instance = await WebAssembly.instantiate(wasm);

assert.throws(
() => { wasi.start(instance); },
{ code: 'ERR_INVALID_ARG_TYPE', message: /\bWebAssembly\.Memory\b/ }
);
})();

(async () => {
const wasi = new WASI();
const wasm = await WebAssembly.compile(bufferSource);
const instance = await WebAssembly.instantiate(wasm);
const values = [undefined, null, 'foo', 42, true, false, () => {}];
let cnt = 0;

// Mock instance.exports to trigger start() validation.
Object.defineProperty(instance, 'exports', {
get() { return values[cnt++]; }
});

values.forEach((val) => {
assert.throws(
() => { wasi.start(instance); },
{ code: 'ERR_INVALID_ARG_TYPE', message: /\bWebAssembly\.Memory\b/ }
{ code: 'ERR_INVALID_ARG_TYPE', message: /\binstance\.exports\b/ }
);
})();
}
});
})();

(async () => {
const wasi = new WASI();
const wasm = await WebAssembly.compile(bufferSource);
const instance = await WebAssembly.instantiate(wasm);

// Mock instance.exports.memory to bypass start() validation.
Object.defineProperty(instance, 'exports', {
get() {
return {
memory: new WebAssembly.Memory({ initial: 1 })
};
}
});

wasi.start(instance);
assert.throws(
() => { wasi.start(instance); },
{
code: 'ERR_WASI_ALREADY_STARTED',
message: /^WASI instance has already started$/
}
);
})();

(async () => {
const wasi = new WASI();
const wasm = await WebAssembly.compile(bufferSource);
const instance = await WebAssembly.instantiate(wasm);

// Mock instance.exports to bypass start() validation.
Object.defineProperty(instance, 'exports', {
get() {
return {
memory: new WebAssembly.Memory({ initial: 1 }),
__wasi_unstable_reactor_start: common.mustCall()
};
}
});

wasi.start(instance);
})();