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

TCP overrides #2

Merged
merged 33 commits into from
Jan 16, 2024
Merged

TCP overrides #2

merged 33 commits into from
Jan 16, 2024

Conversation

badeend
Copy link

@badeend badeend commented Jan 11, 2024

For TCP sockets and IP name lookup, I've moved all the WASI interop code into the crates/wasi/src/preview2/host/ folder. The files directly in crates/wasi/src/preview2/ should now be as "idiomatic" Rust as I could get it. Part of this change is that I updated Network::resolve_addresses to return a plain Box<dyn Future> and effectively demoted ResolveAddressStream to be an implementation detail of the WASI interop.

In the end I did get the regular AsyncRead and AsyncWrite to work, thanks to the existing AsyncReadStream and AsyncWriteStream implementations in pipe.rs and write_stream.rs

I've combined the two distinct permission mechanisms (AllowedNetworkUses & SocketAddrCheck) into one.

In UdpSocket, I've removed the Option<> wrapper around the SocketAddrCheck.

Because the System*** types are expected to be wrapped inside other implementations, I've added non-boxing variants of methods that would otherwise require a box because of the dyn Trait restrictions. This eliminates unnecessary intermediate boxes in places where the inner type is statically known. For example, the inner field of DefaultTcpSocket can be of type SystemTcpSocket instead of Box<dyn TcpSocket>. Places where this is relevant:

  • SystemNetwork::resolve_addresses
  • SystemNetwork::new_tcp_socket
  • SystemTcpSocket::connect
  • SystemTcpSocket::poll_accept

- BoxSyncFuture: for public facing signatures. This is just a regular `Pin<Box<dyn Future ...>>`
- Preview2Future: utility type for use within the WASI wrapper code
- Remove AllowedNetworkUses in favor of just socket_addr_checks
- Let all permission checks go through self.ctx(), instead of NetworkHandle
@badeend badeend marked this pull request as ready for review January 14, 2024 13:16
Copy link
Owner

@rylev rylev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! I only have a few nits that I don't think should block us from merging this and starting on UDP.

crates/wasi/src/preview2/ctx.rs Outdated Show resolved Hide resolved
crates/wasi/src/preview2/host/ip_name_lookup.rs Outdated Show resolved Hide resolved
fn resolve_addresses(&mut self, name: String) -> ResolveAddressStream;
///
/// Unicode domain names are automatically converted to ASCII using IDNA encoding.
/// If the input is an IP address string, the address is parsed and returned
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: these docs are written as if resolve_addresses was a concrete function. We should phrase them so that it's clear that these are expectations the implementor must uphold.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a large part I (mindlessly) copied these docs from the WIT files :)
That being said, don't think it's that weird to write the documentation for the consumer instead of the implementor. There is precedent

Comment on lines +86 to +97
/// Non-boxing variant of [Network::resolve_addresses]
pub fn resolve_addresses(
&mut self,
name: String,
) -> impl Future<Output = io::Result<Vec<IpAddr>>> + Send + Sync + 'static {
async move { resolve_addresses(&name).await }
}

/// Non-boxing variant of [Network::new_tcp_socket]
pub fn new_tcp_socket(&mut self, family: SocketAddrFamily) -> io::Result<SystemTcpSocket> {
SystemTcpSocket::new(family)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unclear to me why we need this functions instead of inlining their bodies everywhere they're called.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the functions they call are internal to the crate. We could open those up, but I think it makes sense (at least conceptually) that consumers can't fabricate a SystemTcpSocket without going through SystemNetwork.

crates/wasi/src/preview2/host/network.rs Outdated Show resolved Hide resolved
crates/wasi/src/preview2/host/network.rs Outdated Show resolved Hide resolved
crates/wasi/src/preview2/host/tcp.rs Outdated Show resolved Hide resolved
@badeend badeend requested a review from rylev January 15, 2024 19:50
@rylev
Copy link
Owner

rylev commented Jan 16, 2024

Amazing stuff! I'll get going on UDP!

@rylev rylev merged commit afcaae1 into rylev:lookup-override Jan 16, 2024
@rylev rylev mentioned this pull request Jan 22, 2024
rylev pushed a commit that referenced this pull request Apr 2, 2024
…dealliance#7029)

* Rename `Host*` things to avoid name conflicts with bindings.

* Update to the latest resource-enabled wit files.

* Adapting the code to the new bindings.

* Update wasi-http to the resource-enabled wit deps.

* Start adapting the wasi-http code to the new bindings.

* Make `get_directories` always return new owned handles.

* Simplify the `poll_one` implementation.

* Update the wasi-preview1-component-adapter.

FIXME: temporarily disable wasi-http tests.

Add logging to the cli world, since stderr is now a reseource that
can only be claimed once.

* Work around a bug hit by poll-list, fix a bug in poll-one.

* Comment out `test_fd_readwrite_invalid_fd`, which panics now.

* Fix a few FIXMEs.

* Use `.as_ref().trapping_unwrap()` instead of `TrappingUnwrapRef`.

* Use `drop_in_place`.

* Remove `State::with_mut`.

* Remove the `RefCell` around the `State`.

* Update to wit-bindgen 0.12.

* Update wasi-http to use resources for poll and I/O.

This required making incoming-body and outgoing-body resourrces too, to
work with `push_input_stream_child` and `push_output_stream_child`.

* Re-enable disabled tests, remove logging from the worlds.

* Remove the `poll_list` workarounds that are no longer needed.

* Remove logging from the adapter.

That said, there is no replacement yet, so add a FIXME comment.

* Reenable a test that now passes.

* Remove `.descriptors_mut` and use `with_descriptors_mut` instead.

Replace `.descriptors()` and `.descriptors_mut()` with functions
that take closures, which limits their scope, to prevent them from
invalid aliasing.

* Implement dynamic borrow checking for descriptors.

* Add a cargo-vet audit for wasmtime-wmemcheck.

* Update cargo vet for wit-bindgen 0.12.

* Cut down on duplicate sync/async resource types (#1)

* Allow calling `get-directories` more than once (#2)

For now `Clone` the directories into new descriptor slots as needed.

* Start to lift restriction of stdio only once  (#3)

* Start to lift restriction of stdio only once

This commit adds new `{Stdin,Stdout}Stream` traits which take over the
job of the stdio streams in `WasiCtxBuilder` and `WasiCtx`. These traits
bake in the ability to create a stream at any time to satisfy the API
of `wasi:cli`. The TTY functionality is folded into them as while I was
at it.

The implementation for stdin is relatively trivial since the stdin
implementation already handles multiple streams reading it. Built-in
impls of the `StdinStream` trait are also provided for helper types in
`preview2::pipe` which resulted in the implementation of
`MemoryInputPipe` being updated to support `Clone` where all clones read
the same original data.

* Get tests building

* Un-ignore now-passing test

* Remove unneeded argument from `WasiCtxBuilder::build`

* Fix tests

* Remove some workarounds

Stdio functions can now be called multiple times.

* If `poll_oneoff` fails part-way through, clean up properly.

Fix the `Drop` implementation for pollables to only drop the pollables
that have been successfully added to the list.

This fixes the poll_oneoff_files failure and removes a FIXME.

---------

Co-authored-by: Alex Crichton <alex@alexcrichton.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants