-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop preventing WASI apps including unimplemented syscalls
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
1 parent
7fc43f0
commit 33eafb0
Showing
1 changed file
with
28 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -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); | ||
|
@@ -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. | ||
|
@@ -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; | ||
}; |