Stream: wasi

Topic: wasi_snapshot_preview1::fd_seek inconsistent signature


view this post on Zulip wsta (Mar 30 2023 at 07:49):

Hi,

I tried invoking a Wasm module I compiled using emscripten 3.1.24 and wasmtime-cli 8.0.0. It has a bunch of imports I don't care about, so I used the new --default-values-unknown-imports flag to ignore them. I then got this error:

Error: failed to run main module module.wasm

Caused by:
0: failed to instantiate "module.wasm"
1: incompatible import type for wasi_snapshot_preview1::fd_seek
2: function types incompatible: expected func of type (i32, i32, i32, i32, i32) -> (i32), found func of type (i32, i64, i32, i32) -> (i32)

I'm not sure why there are more than 3 arguments in both the signature that wasmtime expects and the signature that the module wants, becaue according to the spec fd_seek should only accept the fd, offset, and whence arguments. Does anyone have an idea of what is happening? Could there be inconsistent calling conventions at play? Are there any good mitigations?

view this post on Zulip wsta (Mar 30 2023 at 08:56):

OK, I found the reason for the fourth argument, it is a pointer that contains the filesize retrun value
https://github.com/WebAssembly/wasi-libc/blob/0b3b1bb9f1776d7e64ef65b899ed6b3cfa9576eb/libc-bottom-half/headers/public/wasi/api.h#L1721

Still, why does wasmtime expect five arguments then? Did I accidentally compile a 32-bit wasmtime that does not support i64, and it split up the offset argument into two i32s?

WASI libc implementation for WebAssembly. Contribute to WebAssembly/wasi-libc development by creating an account on GitHub.

view this post on Zulip bjorn3 (Mar 30 2023 at 09:02):

If you want to run a wasm module using wasmtime, you have to compile it with wasi-sdk, not emscripten. Emscripten uses a different syscall interface. It just so happens to overlap a lot with wasi.

view this post on Zulip wsta (Mar 30 2023 at 09:27):

Thanks! Just out of curiosity, is there any documentation regarding the Wasi functions signatures that explain why fd_seek uses five arguments?

view this post on Zulip bjorn3 (Mar 30 2023 at 11:37):

No idea why that is the case.

view this post on Zulip bjorn3 (Mar 30 2023 at 11:38):

No idea why that is the case.

view this post on Zulip Eduardo Rodrigues (Mar 30 2023 at 13:28):

@wsta , you are probably looking for the WASI preview1 definition here: https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#fd_seek

WebAssembly System Interface. Contribute to WebAssembly/WASI development by creating an account on GitHub.

view this post on Zulip wsta (Mar 30 2023 at 13:34):

Thanks, but I already found that. It shows that fd_seek has three arguments viewed from a high level, but not how many arguments are actually used in Wasm. Based on the wasi-libc api, it would make sense that four arguments are used
https://github.com/WebAssembly/wasi-libc/blob/0b3b1bb9f1776d7e64ef65b899ed6b3cfa9576eb/libc-bottom-half/headers/public/wasi/api.h#L1721

I also checked with another Wasm module compiled using wasi-sdk, and interestingly it uses four arguments, just like the module compiled with emscripten. But in this case wasmtime has no problems with it.

WASI libc implementation for WebAssembly. Contribute to WebAssembly/wasi-libc development by creating an account on GitHub.

Last updated: Oct 23 2024 at 20:03 UTC