Skip to content

Commit

Permalink
Stop preventing WASI apps including unimplemented syscalls
Browse files Browse the repository at this point in the history
The recent wasi-libc includes some new syscalls not implemented in
@wasmer-js/wasi and we currently disable `--gc-sections` (see
swiftwasm/swift#5128), so binaries built with
SwiftWasm 5.9 toolchain contains references to those unimplemented
syscalls.
This commit allows running such binaries by inserting dummy functions
for unimplemented syscalls.
  • Loading branch information
kateinoigakukun committed Jan 16, 2024
1 parent 7fc43f0 commit 33eafb0
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions entrypoint/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export const WasmRunner = (rawOptions, SwiftRuntime) => {
},
});

const createWasmImportObject = (extraWasmImports) => {
const createWasmImportObject = (extraWasmImports, wasmModule) => {
const importObject = {
wasi_snapshot_preview1: wrapWASI(wasi),
wasi_snapshot_preview1: wrapWASI(wasi, wasmModule),
};

if (swift) {
Expand All @@ -72,11 +72,10 @@ export const WasmRunner = (rawOptions, SwiftRuntime) => {
throw new Error("Detected stack buffer overflow.");
},
};
const importObject = createWasmImportObject(extraWasmImports);
const module = await WebAssembly.instantiate(wasmBytes, importObject);

// Node support
const instance = "instance" in module ? module.instance : module;
const module = await WebAssembly.compile(wasmBytes);
const importObject = createWasmImportObject(extraWasmImports, module);
console.log(importObject)
const instance = await WebAssembly.instantiate(module, importObject);

if (swift && instance.exports.swjs_library_version) {
swift.setInstance(instance);
Expand Down Expand Up @@ -132,7 +131,7 @@ const createWasmFS = (onStdout, onStderr) => {
return wasmFs;
};

const wrapWASI = (wasiObject) => {
const wrapWASI = (wasiObject, wasmModule) => {
// 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.
Expand All @@ -148,5 +147,26 @@ const wrapWASI = (wasiObject) => {
wasiObject.refreshMemory();
return original_clock_res_get(clockId, resolution);
};

// @wasmer-js/wasi polyfill does not support all WASI syscalls like `sock_accept`.
// So we need to insert a dummy function for unimplemented syscalls.
const __WASI_ERRNO_NOTSUP = 58;
for (const importEntry of WebAssembly.Module.imports(wasmModule)) {
const { importModule, importName, importKind } = importEntry;
// Skip dummy import entries for non-WASI and already implemented syscalls.
if (
importModule !== "wasi_snapshot_preview1" ||
importKind !== "function" ||
wasiObject.wasiImport[importName]
) {
continue;
}

wasiObject.wasiImport[importName] = () => {
console.warn(`WASI syscall ${importModule}.${importName} is not supported, returning ENOTSUP.`);
return __WASI_ERRNO_NOTSUP;
}
}

return wasiObject.wasiImport;
};

0 comments on commit 33eafb0

Please sign in to comment.