Files
wasmtime/crates/wasi-common
Jakub Konka 7228a248c1 [wasi-common] add custom FdPool container for managing fd allocs/deallocs (#1329)
* Rename FdEntry to Entry

* Add custom FdSet container for managing fd allocs/deallocs

This commit adds a custom `FdSet` container which is intended
for use in `wasi-common` to track WASI fd allocs/deallocs. The
main aim for this container is to abstract away the current
approach of spawning new handles

```rust
fd = fd.checked_add(1).ok_or(...)?;
```

and to make it possible to reuse unused/reclaimed handles
which currently is not done.

The struct offers 3 methods to manage its functionality:
* `FdSet::new` initialises the internal data structures,
  and most notably, it preallocates an `FdSet::BATCH_SIZE`
  worth of handles in such a way that we always start popping
  from the "smallest" handle (think of it as of reversed stack,
  I guess; it's not a binary heap since we don't really care
  whether internally the handles are sorted in some way, just that
  the "largets" handle is at the bottom. Why will become clear
  when describing `allocate` method.)
* `FdSet::allocate` pops the next available handle if one is available.
  The tricky bit here is that, if we run out of handles, we preallocate
  the next `FdSet::BATCH_SIZE` worth of handles starting from the
  latest popped handle (i.e., the "largest" handle). This
  works only because we make sure to only ever pop and push already
  existing handles from the back, and push _new_ handles (from the
  preallocation step) from the front. When we ultimately run out
  of _all_ available handles, we then return `None` for the client
  to handle in some way (e.g., throwing an error such as `WasiError::EMFILE`
  or whatnot).
* `FdSet::deallocate` returns the already allocated handle back to
  the pool for further reuse.

When figuring out the internals, I've tried to optimise for both
alloc and dealloc performance, and I believe we've got an amortised
`O(1)~*` performance for both (if my maths is right, and it may very
well not be, so please verify!).

In order to keep `FdSet` fairly generic, I've made sure not to hard-code
it for the current type system generated by `wig` (i.e., `wasi::__wasi_fd_t`
representing WASI handle), but rather, any type which wants to be managed
by `FdSet` needs to conform to `Fd` trait. This trait is quite simple as
it only requires a couple of rudimentary traits (although `std:#️⃣:Hash`
is quite a powerful assumption here!), and a custom method

```rust
Fd::next(&self) -> Option<Self>;
```

which is there to encapsulate creating another handle from the given one.
In the current state of the code, that'd be simply `u32::checked_add(1)`.
When `wiggle` makes it way into the `wasi-common`, I'd imagine it being
similar to

```rust
fn next(&self) -> Option<Self> {
    self.0.checked_add(1).map(Self::from)
}
```

Anyhow, I'd be happy to learn your thoughts about this design!

* Fix compilation on other targets

* Rename FdSet to FdPool

* Fix FdPool unit tests

* Skip preallocation step in FdPool

* Replace 'replace' calls with direct assignment

* Reuse FdPool from snapshot1 in snapshot0

* Refactor FdPool::allocate

* Remove entry before deallocating the fd

* Refactor the design to accommodate `u32` as underlying type

This commit refactors the design by ensuring that the underlying
type in `FdPool` which we use to track and represent raw file
descriptors is `u32`. As a result, the structure of `FdPool` is
simplified massively as we no longer need to track the claimed
descriptors; in a way, we trust the caller to return the handle
after it's done with it. In case the caller decides to be clever
and return a handle which was not yet legally allocated, we panic.
This should never be a problem in `wasi-common` unless we hit a
bug.

To make all of this work, `Fd` trait is modified to require two
methods: `as_raw(&self) -> u32` and `from_raw(raw_fd: u32) -> Self`
both of which are used to convert to and from the `FdPool`'s underlying
type `u32`.
2020-03-17 22:58:49 +01:00
..
2019-11-08 06:35:40 -08:00
2019-11-08 06:35:40 -08:00

wasi-common

A Bytecode Alliance project

A library providing a common implementation of WASI hostcalls for re-use in any WASI-enabled runtime.

Crates.io version Download docs.rs docs

The wasi-common crate will ultimately serve as a library providing a common implementation of WASI hostcalls for re-use in any WASI (and potentially non-WASI) runtimes such as Wasmtime and Lucet.

The library is an adaption of lucet-wasi crate from the Lucet project, and it is currently based on 40ae1df git revision.

Please note that the library requires Rust compiler version at least 1.37.0.

Supported syscalls

*nix

In our *nix implementation, we currently support the entire WASI API with the exception of socket hostcalls:

  • sock_recv
  • sock_send
  • sock_shutdown

We expect these to be implemented when network access is standardised.

We also currently do not support the proc_raise hostcall, as it is expected to be dropped entirely from WASI.

Windows

In our Windows implementation, we currently support the minimal subset of WASI API which allows for running the very basic "Hello world!" style WASM apps. More coming shortly, so stay tuned!

Development hints

When testing the crate, you may want to enable and run full wasm32 integration testsuite. This requires wasm32-wasi target installed which can be done as follows using rustup

rustup target add wasm32-wasi

Now, you should be able to run the integration testsuite by running cargo test on the test-programs package with test_programs feature enabled:

cargo test --features test_programs --package test-programs

Third-Party Code

Significant parts of our hostcall implementations are derived from the C implementations in cloudabi-utils. See LICENSE.cloudabi-utils for license information.