Skip to content

Instantly share code, notes, and snippets.

@kkebo
Last active November 13, 2022 10:36
Show Gist options
  • Save kkebo/b5522a97f8b3c188628dccb1087a0ab7 to your computer and use it in GitHub Desktop.
Save kkebo/b5522a97f8b3c188628dccb1087a0ab7 to your computer and use it in GitHub Desktop.
@wasmer/wasi@0.10.2 で swift-format.wasm のリリースビルドバイナリが実行できない再現コード
const fs = require('fs-extra')
const { WASI } = require('@wasmer/wasi')
const wasiBindings = require('@wasmer/wasi/lib/bindings/node')
const { lowerI64Imports } = require("@wasmer/wasm-transformer")
const wasi = new WASI({
args: ['swift-format', '--version'],
env: {},
bindings: wasiBindings.default,
preopens: {'.': '.'}
})
;(async () => {
const bytes = fs.readFileSync('./swift-format.wasm')
const loweredWasmBytes = lowerI64Imports(bytes)
const module = await WebAssembly.compile(loweredWasmBytes)
// ↓これなら OK
// const module = await WebAssembly.compile(bytes)
const instance = await WebAssembly.instantiate(module, wasi.getImports(module))
wasi.start(instance)
})()
@kkebo
Copy link
Author

kkebo commented Nov 11, 2022

$ node main.js
node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[CompileError: WebAssembly.compile(): signature index 56071 out of bounds (449 signatures) @+5789]

Node.js v19.0.1

@kkebo
Copy link
Author

kkebo commented Nov 13, 2022

const fs = require('fs-extra')
const { WASI } = require('@wasmer/wasi')
const wasiBindings = require('@wasmer/wasi/lib/bindings/node')
const { lowerI64Imports } = require("@wasmer/wasm-transformer")

const wasi = new WASI({
  args: ['swift-format', '--version'],
  env: {},
  bindings: wasiBindings.default,
  preopens: {'.': '.'}
})

;(async () => {
  const bytes = fs.readFileSync('./swift-format.wasm')
  const loweredWasmBytes = lowerI64Imports(bytes)
  fs.writeFileSync('loweredWasmBytes.wasm', loweredWasmBytes)
})()
$ wasmer loweredWasmBytes.wasm
error: failed to run `loweredWasmBytes.wasm`
│   1: module instantiation failed (engine: universal, compiler: cranelift)
╰─▶ 2: Validation error: unknown type 56071: type index out of bounds (at offset 5755)
$ wasmer inspect swift-format.wasm
Type: wasm
Size: 25.8 MB
Imports:
  Functions:
    "wasi_snapshot_preview1"."args_get": [I32, I32] -> [I32]
    "wasi_snapshot_preview1"."args_sizes_get": [I32, I32] -> [I32]
    "wasi_snapshot_preview1"."environ_get": [I32, I32] -> [I32]
    "wasi_snapshot_preview1"."environ_sizes_get": [I32, I32] -> [I32]
    "wasi_snapshot_preview1"."clock_res_get": [I32, I32] -> [I32]
    "wasi_snapshot_preview1"."clock_time_get": [I32, I64, I32] -> [I32]
    "wasi_snapshot_preview1"."fd_advise": [I32, I64, I64, I32] -> [I32]
    "wasi_snapshot_preview1"."fd_allocate": [I32, I64, I64] -> [I32]
    "wasi_snapshot_preview1"."fd_close": [I32] -> [I32]
    "wasi_snapshot_preview1"."fd_datasync": [I32] -> [I32]
    "wasi_snapshot_preview1"."fd_fdstat_get": [I32, I32] -> [I32]
    "wasi_snapshot_preview1"."fd_fdstat_set_flags": [I32, I32] -> [I32]
    "wasi_snapshot_preview1"."fd_fdstat_set_rights": [I32, I64, I64] -> [I32]
    "wasi_snapshot_preview1"."fd_filestat_get": [I32, I32] -> [I32]
    "wasi_snapshot_preview1"."fd_filestat_set_size": [I32, I64] -> [I32]
    "wasi_snapshot_preview1"."fd_filestat_set_times": [I32, I64, I64, I32] -> [I32]
    "wasi_snapshot_preview1"."fd_pread": [I32, I32, I32, I64, I32] -> [I32]
    "wasi_snapshot_preview1"."fd_prestat_get": [I32, I32] -> [I32]
    "wasi_snapshot_preview1"."fd_prestat_dir_name": [I32, I32, I32] -> [I32]
    "wasi_snapshot_preview1"."fd_pwrite": [I32, I32, I32, I64, I32] -> [I32]
    "wasi_snapshot_preview1"."fd_read": [I32, I32, I32, I32] -> [I32]
    "wasi_snapshot_preview1"."fd_readdir": [I32, I32, I32, I64, I32] -> [I32]
    "wasi_snapshot_preview1"."fd_renumber": [I32, I32] -> [I32]
    "wasi_snapshot_preview1"."fd_seek": [I32, I64, I32, I32] -> [I32]
    "wasi_snapshot_preview1"."fd_sync": [I32] -> [I32]
    "wasi_snapshot_preview1"."fd_tell": [I32, I32] -> [I32]
    "wasi_snapshot_preview1"."fd_write": [I32, I32, I32, I32] -> [I32]
    "wasi_snapshot_preview1"."path_create_directory": [I32, I32, I32] -> [I32]
    "wasi_snapshot_preview1"."path_filestat_get": [I32, I32, I32, I32, I32] -> [I32]
    "wasi_snapshot_preview1"."path_filestat_set_times": [I32, I32, I32, I32, I64, I64, I32] -> [I32]
    "wasi_snapshot_preview1"."path_link": [I32, I32, I32, I32, I32, I32, I32] -> [I32]
    "wasi_snapshot_preview1"."path_open": [I32, I32, I32, I32, I32, I64, I64, I32, I32] -> [I32]
    "wasi_snapshot_preview1"."path_readlink": [I32, I32, I32, I32, I32, I32] -> [I32]
    "wasi_snapshot_preview1"."path_remove_directory": [I32, I32, I32] -> [I32]
    "wasi_snapshot_preview1"."path_rename": [I32, I32, I32, I32, I32, I32] -> [I32]
    "wasi_snapshot_preview1"."path_symlink": [I32, I32, I32, I32, I32] -> [I32]
    "wasi_snapshot_preview1"."path_unlink_file": [I32, I32, I32] -> [I32]
    "wasi_snapshot_preview1"."poll_oneoff": [I32, I32, I32, I32] -> [I32]
    "wasi_snapshot_preview1"."proc_exit": [I32] -> []
    "wasi_snapshot_preview1"."proc_raise": [I32] -> [I32]
    "wasi_snapshot_preview1"."sched_yield": [] -> [I32]
    "wasi_snapshot_preview1"."random_get": [I32, I32] -> [I32]
    "wasi_snapshot_preview1"."sock_recv": [I32, I32, I32, I32, I32, I32] -> [I32]
    "wasi_snapshot_preview1"."sock_send": [I32, I32, I32, I32, I32] -> [I32]
    "wasi_snapshot_preview1"."sock_shutdown": [I32, I32] -> [I32]
  Memories:
  Tables:
  Globals:
Exports:
  Functions:
    "_start": [] -> []
  Memories:
    "memory": not shared (82 pages..)
  Tables:
  Globals:
$ wasmer inspect loweredWasmBytes.wasm
error: failed to inspect `loweredWasmBytes.wasm`
╰─▶ 1: Validation error: unknown type 56071: type index out of bounds (at offset 5755)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment