Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasmtime c-api wasi questions #10162

Open
mihaly-sisak opened this issue Jan 30, 2025 · 1 comment
Open

Wasmtime c-api wasi questions #10162

mihaly-sisak opened this issue Jan 30, 2025 · 1 comment

Comments

@mihaly-sisak
Copy link

I started to look around the wasi parts of the project and come to the realization that there is little customization in io when using the rust wasi c-api.

So I started to look around, how can I implement a small part of the proposed wasi interfaces.
I found this proposal: https://github.com/WebAssembly/wasi-cli/blob/main/command.md
I have a ton of questions.

  • wasi:io/error: Is this a special object created with wasmtime_error_new()?
  • wasi:io/poll: All defined resources (like pollable) are host-managed wasm_val_t.of.ref host pointers?
  • How can I detect that a variable got dropped out of scope? Gc-drc collects inside the store? Docs says a store has no garbage collection. What does gc-drc collects?
  • wasi:io/poll: How to get a list<pollable> of objects? If I call the poll() func with a list of 3 pollable resources what do I get? Are they just a list of params? Will wasm_func_param_arity() return 3? Is it a table?
  • wasi:io/streams: How to return a result<>? Same question like the list<> argument but in reverse. How to return list<u8>, if the datatypes avaliable are i32, u32, i64, u64? Is it packed?
  • wasi:io/streams: With write() what is the return _? What to return here?
  • wasi:io/streams: At the end of blocking-write-and-flush() do I return the result of check-write()?
  • wasi:cli/environment: How to return a list<(string, string)>? is it a list<tuple<string, string>>? How strings are handled?
@pchickey
Copy link
Contributor

pchickey commented Jan 30, 2025

Hi, that's correct, the C API only exports a very limited set of ways to configure a WASI 0.1 (aka Preview 1) implementation.

However, WASI has moved on to version 0.2, and with it wit is no longer based on bare Wasm Modules but on the Wasm Component Model proposal, which introduces a type system and interface definition language called wit. The wasi-cli repo link you refer to is the wit rendered as markdown. The ABIs for WASI 0.1 and 0.2 are completely different, and programs that are built to run on WASI 0.2 will not run on an implementation that only provides WASI 0.1. (There is a way to transform modules written against WASI 0.1 into components that use WASI 0.2.)The wasmtime-wasi crate provides both a 0.1 and 0.2 implementation, but only the 0.1 implementation is exposed when using the C API.

Unfortunately, the reason its not obvious for how to use any of those wit based types with wasmtime's C API is that wasmtimes C API does not yet have support for the component model. This is a long-standing bug that remains open because it is a big undertaking, and none of the core contributors who build and maintain Wasmtime as part of their jobs have been able to prioritize it, since our production use cases all use wasmtime through its native Rust API.

So, with that context, my suggestion is that you can take a look at using Wasmtime's Rust interfaces directly, where if you need a custom WASI implementation you have far more options, for example you could fork and modify the wasmtime-wasi crate, you could write a completely different implementation in Rust on top of the bindings created by wasmtime::component::bindgen!, such as in the example here. Alternatively, you can take on the C API issue yourself, which as warned above is also a pretty big undertaking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants