0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-02 04:38:21 -05:00

fix(std/wasi): disallow multiple starts (#8712)

This commit is contained in:
Casper Beyer 2020-12-10 20:37:09 +08:00 committed by GitHub
parent c5ccbf3699
commit 77b2bc3bc1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 0 deletions

View file

@ -294,6 +294,7 @@ export default class Context {
fds: FileDescriptor[];
exports: Record<string, WebAssembly.ImportValue>;
#started: boolean;
constructor(options: ContextOptions) {
this.args = options.args ?? [];
@ -1571,6 +1572,8 @@ export default class Context {
return ERRNO_NOSYS;
}),
};
this.#started = false;
}
/**
@ -1585,6 +1588,12 @@ export default class Context {
* thrown.
*/
start(instance: WebAssembly.Instance) {
if (this.#started) {
throw new Error("WebAssembly.Instance has already started");
}
this.#started = true;
const { _start, _initialize, memory } = instance.exports;
if (!(memory instanceof WebAssembly.Memory)) {
@ -1618,6 +1627,12 @@ export default class Context {
* thrown.
*/
initialize(instance: WebAssembly.Instance) {
if (this.#started) {
throw new Error("WebAssembly.Instance has already started");
}
this.#started = true;
const { _start, _initialize, memory } = instance.exports;
if (!(memory instanceof WebAssembly.Memory)) {

View file

@ -198,6 +198,23 @@ Deno.test("context_start", function () {
assert(err instanceof ExitStatus);
assertEquals(err.code, 0);
}
assertThrows(
() => {
const context = new Context({});
context.start({
exports: {
memory: new WebAssembly.Memory({ initial: 1 }),
_start() {},
},
});
context.start({
exports: {},
});
},
Error,
"WebAssembly.Instance has already started",
);
});
Deno.test("context_initialize", function () {
@ -240,4 +257,20 @@ Deno.test("context_initialize", function () {
TypeError,
"export _initialize must be a function",
);
assertThrows(
() => {
const context = new Context({});
context.initialize({
exports: {
memory: new WebAssembly.Memory({ initial: 1 }),
_initialize() {},
},
});
context.initialize({
exports: {},
});
},
Error,
"WebAssembly.Instance has already started",
);
});