Stream: wasi

Topic: compiling wasi-libc p2 hello world


view this post on Zulip ovf (Apr 08 2024 at 12:26):

i've finally started looking at p2.
built current main (d0382948) of https://github.com/WebAssembly/wasi-libc with make WASI_SNAPSHOT=p2

i'm compiling this one-line c program:
$ cat hello.c int puts(const char*);int main(){puts("Hello, world!");} $ clang-17 -target wasm32-wasip2 --sysroot /s/wasi-libc/sysroot -O2 hello.c
this seems to produce a file with rather unexpected imports:

$ wasm-objdump -x -j Import a.out

a.out:  file format wasm 0x1

Section Details:

Import[12]:
 - func[0] sig=2 <wasi_snapshot_preview1.fd_close> <- wasi_snapshot_preview1.fd_close
 - func[1] sig=5 <wasi_snapshot_preview1.fd_fdstat_get> <- wasi_snapshot_preview1.fd_fdstat_get
 - func[2] sig=6 <wasi_snapshot_preview1.fd_seek> <- wasi_snapshot_preview1.fd_seek
 - func[3] sig=4 <wasi_snapshot_preview1.fd_write> <- wasi_snapshot_preview1.fd_write
 - func[4] sig=1 <wasi:io/poll@0.2.0.[resource-drop]pollable> <- wasi:io/poll@0.2.0.[resource-drop]pollable
 - func[5] sig=1 <wasi:io/streams@0.2.0.[resource-drop]input-stream> <- wasi:io/streams@0.2.0.[resource-drop]input-stream
 - func[6] sig=1 <wasi:io/streams@0.2.0.[resource-drop]output-stream> <- wasi:io/streams@0.2.0.[resource-drop]output-stream
 - func[7] sig=1 <wasi:sockets/udp@0.2.0.[resource-drop]udp-socket> <- wasi:sockets/udp@0.2.0.[resource-drop]udp-socket
 - func[8] sig=1 <wasi:sockets/udp@0.2.0.[resource-drop]incoming-datagram-stream> <- wasi:sockets/udp@0.2.0.[resource-drop]incoming-datagram-stream
 - func[9] sig=1 <wasi:sockets/udp@0.2.0.[resource-drop]outgoing-datagram-stream> <- wasi:sockets/udp@0.2.0.[resource-drop]outgoing-datagram-stream
 - func[10] sig=1 <wasi:sockets/tcp@0.2.0.[resource-drop]tcp-socket> <- wasi:sockets/tcp@0.2.0.[resource-drop]tcp-socket
 - func[11] sig=2 <wasi_snapshot_preview1.adapter_close_badfd> <- wasi_snapshot_preview1.adapter_close_badfd

and sure enough, i can't seem to run it:

$ wasmtime run -S preview2 a.out
Error: failed to run main module `a.out`

Caused by:
    0: failed to instantiate "a.out"
    1: unknown import: `wasi:io/poll@0.2.0::[resource-drop]pollable` has not been defined

what am i missing?

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

view this post on Zulip Joel Dice (Apr 08 2024 at 13:34):

If you install wasm-tools, then grab the Preview 1 adapter (e.g. curl -LO https://github.com/bytecodealliance/wasmtime/releases/download/v19.0.1/wasi_snapshot_preview1.command.wasm) and run wasm-tools component new --adapt wasi_snapshot_preview1.command.wasm a.out -o component.wasm, Wasmtime should be able to run component.wasm.

The reasons you need to do the above are (1) wasm-ld generates a module, but Wasmtime only knows how to run Preview 1 modules and Preview 2 components, and (2) the wasm32-wasip2 target in wasi-sdk currently imports a mix of Preview 1 and Preview 2 functions while we incrementally add support for Preview 2, and while that is the case we need to use an adapter to map the Preview 1 imports to Preview 2 imports. See https://github.com/WebAssembly/wasi-libc/issues/447 for details.

CLI and Rust libraries for low-level manipulation of WebAssembly modules - bytecodealliance/wasm-tools
Summary This is a proposal to add wasi-sockets support to wasi-libc as a first step towards full WASI Preview 2 support. This includes adding a new wasm32-wasi-preview2 build target to differentiat...

view this post on Zulip ovf (Apr 08 2024 at 14:54):

thanks! so if my aim was to implement enough of wasip2 in my runtime to run my (wasi-libc-based) code, it might be a bit too soon?

the second takeaway for me, which isn't really a question, is that i should once again try and read through the component model documentation in the hopes of understanding (in order of importance) the why and the how.

is wasmtime expected to eventually support wasip2 modules through the cm canonical abi, or will it be component model all the way?

view this post on Zulip Joel Dice (Apr 08 2024 at 15:17):

I'm not aware of any plans to support wasip2 modules. One easy way to do it would be to convert such modules to components "just in time" using wit-component prior to instantiating them in wasmtime run. Not sure if there's interest in adding such a feature, though. If you're adding support for wasip2 to your own runtime, though, you're free to do whatever you like. This might be relevant: https://github.com/WebAssembly/wasi-libc/issues/447#issuecomment-1971367144

Summary This is a proposal to add wasi-sockets support to wasi-libc as a first step towards full WASI Preview 2 support. This includes adding a new wasm32-wasi-preview2 build target to differentiat...

Last updated: Oct 23 2024 at 20:03 UTC