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

Override clock_res_get function from wasmer/wasi-js to fix memory issue #323

Merged
merged 1 commit into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
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
20 changes: 19 additions & 1 deletion entrypoint/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ const wasi = new WASI({
},
});

const wrapWASI = (wasiObject) => {
// PATCH: @wasmer-js/[email protected] forgets to call `refreshMemory` in `clock_res_get`,
// which writes its result to memory view. Without the refresh the memory view,
// it accesses a detached array buffer if the memory is grown by malloc.
// But they wasmer team discarded the 0.x codebase at all and replaced it with
// a new implementation written in Rust. The new version 1.x is really unstable
// and not production-ready as far as katei investigated in Apr 2022.
// So override the broken implementation of `clock_res_get` here instead of
// fixing the wasi polyfill.
// Reference: https://github.com/wasmerio/wasmer-js/blob/55fa8c17c56348c312a8bd23c69054b1aa633891/packages/wasi/src/index.ts#L557
const original_clock_res_get = wasiObject.wasiImport["clock_res_get"];
wasiObject.wasiImport["clock_res_get"] = (clockId, resolution) => {
wasiObject.refreshMemory();
return original_clock_res_get(clockId, resolution)
};
return wasiObject.wasiImport;
}

const startWasiTask = async () => {
// Fetch our Wasm File
const response = await fetch("REPLACE_THIS_WITH_THE_MAIN_WEBASSEMBLY_MODULE");
Expand All @@ -54,7 +72,7 @@ const startWasiTask = async () => {
// Instantiate the WebAssembly file
const wasmBytes = new Uint8Array(responseArrayBuffer).buffer;
const { instance } = await WebAssembly.instantiate(wasmBytes, {
wasi_snapshot_preview1: wasi.wasiImport,
wasi_snapshot_preview1: wrapWASI(wasi),
javascript_kit: swift.importObjects(),
});

Expand Down
20 changes: 19 additions & 1 deletion entrypoint/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ const wasi = new WASI({
},
});

const wrapWASI = (wasiObject) => {
// PATCH: @wasmer-js/[email protected] forgets to call `refreshMemory` in `clock_res_get`,
// which writes its result to memory view. Without the refresh the memory view,
// it accesses a detached array buffer if the memory is grown by malloc.
// But they wasmer team discarded the 0.x codebase at all and replaced it with
// a new implementation written in Rust. The new version 1.x is really unstable
// and not production-ready as far as katei investigated in Apr 2022.
// So override the broken implementation of `clock_res_get` here instead of
// fixing the wasi polyfill.
// Reference: https://github.com/wasmerio/wasmer-js/blob/55fa8c17c56348c312a8bd23c69054b1aa633891/packages/wasi/src/index.ts#L557
const original_clock_res_get = wasiObject.wasiImport["clock_res_get"];
wasiObject.wasiImport["clock_res_get"] = (clockId, resolution) => {
wasiObject.refreshMemory();
return original_clock_res_get(clockId, resolution)
};
return wasiObject.wasiImport;
}

const startWasiTask = async () => {
// Fetch our Wasm File
const response = await fetch("/main.wasm");
Expand All @@ -72,7 +90,7 @@ const startWasiTask = async () => {
// Instantiate the WebAssembly file
const wasmBytes = new Uint8Array(responseArrayBuffer).buffer;
const { instance } = await WebAssembly.instantiate(wasmBytes, {
wasi_snapshot_preview1: wasi.wasiImport,
wasi_snapshot_preview1: wrapWASI(wasi),
javascript_kit: swift.importObjects(),
__stack_sanitizer: {
report_stack_overflow: () => {
Expand Down
20 changes: 19 additions & 1 deletion entrypoint/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ const wasi = new WASI({
},
});

const wrapWASI = (wasiObject) => {
// PATCH: @wasmer-js/[email protected] forgets to call `refreshMemory` in `clock_res_get`,
// which writes its result to memory view. Without the refresh the memory view,
// it accesses a detached array buffer if the memory is grown by malloc.
// But they wasmer team discarded the 0.x codebase at all and replaced it with
// a new implementation written in Rust. The new version 1.x is really unstable
// and not production-ready as far as katei investigated in Apr 2022.
// So override the broken implementation of `clock_res_get` here instead of
// fixing the wasi polyfill.
// Reference: https://github.com/wasmerio/wasmer-js/blob/55fa8c17c56348c312a8bd23c69054b1aa633891/packages/wasi/src/index.ts#L557
const original_clock_res_get = wasiObject.wasiImport["clock_res_get"];
wasiObject.wasiImport["clock_res_get"] = (clockId, resolution) => {
wasiObject.refreshMemory();
return original_clock_res_get(clockId, resolution)
};
return wasiObject.wasiImport;
}

const startWasiTask = async () => {
// Fetch our Wasm File
const response = await fetch("/main.wasm");
Expand All @@ -72,7 +90,7 @@ const startWasiTask = async () => {
// Instantiate the WebAssembly file
const wasmBytes = new Uint8Array(responseArrayBuffer).buffer;
const { instance } = await WebAssembly.instantiate(wasmBytes, {
wasi_snapshot_preview1: wasi.wasiImport,
wasi_snapshot_preview1: wrapWASI(wasi),
javascript_kit: swift.importObjects(),
__stack_sanitizer: {
report_stack_overflow: () => {
Expand Down