-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the
tcp
interface of wasi-sockets. (#6837)
* Implement the `tcp` interface of wasi-sockets. Implement the `tcp`, `tcp-create-socket`, and `network` interfaces of wasi-sockets. * Minor cleanups. * Update to the latest upstream wasi-sockets. * Address review feedback. * Handle zero-length reads and writes, and other cleanups. * Fix compilation on macOS. * Fix compilation on Windows. * Update all the copies of wasi-socket wit files. * Sync up more wit files. * Fix the errno code for non-blocking `connect` on Windows. prtest:full * Tolerate `NOTCONN` errors when cleaning up with `shutdown`. * Simplify the polling mechanism. This requires an updated tokio for `Interest::ERROR`. * Downgrade to tokio 1.29.1 for now. * Move `tcp_state` out of the `Arc`. * `accept` doesn't need a write lock. * Remove `tcp_state`'s `RwLock`.
- Loading branch information
1 parent
819fad0
commit 2f6e977
Showing
39 changed files
with
1,827 additions
and
252 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#![cfg(all(feature = "test_programs", not(skip_wasi_sockets_tests)))] | ||
use cap_std::ambient_authority; | ||
use wasmtime::component::Linker; | ||
use wasmtime::{Config, Engine, Store}; | ||
use wasmtime_wasi::preview2::{self, command::Command, Table, WasiCtx, WasiCtxBuilder, WasiView}; | ||
|
||
lazy_static::lazy_static! { | ||
static ref ENGINE: Engine = { | ||
let mut config = Config::new(); | ||
config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); | ||
config.wasm_component_model(true); | ||
config.async_support(true); | ||
|
||
let engine = Engine::new(&config).unwrap(); | ||
engine | ||
}; | ||
} | ||
// uses ENGINE, creates a fn get_component(&str) -> Component | ||
include!(concat!( | ||
env!("OUT_DIR"), | ||
"/wasi_sockets_tests_components.rs" | ||
)); | ||
|
||
struct SocketsCtx { | ||
table: Table, | ||
wasi: WasiCtx, | ||
} | ||
|
||
impl WasiView for SocketsCtx { | ||
fn table(&self) -> &Table { | ||
&self.table | ||
} | ||
fn table_mut(&mut self) -> &mut Table { | ||
&mut self.table | ||
} | ||
fn ctx(&self) -> &WasiCtx { | ||
&self.wasi | ||
} | ||
fn ctx_mut(&mut self) -> &mut WasiCtx { | ||
&mut self.wasi | ||
} | ||
} | ||
|
||
async fn run(name: &str) -> anyhow::Result<()> { | ||
let component = get_component(name); | ||
let mut linker = Linker::new(&ENGINE); | ||
|
||
preview2::bindings::io::streams::add_to_linker(&mut linker, |x| x)?; | ||
preview2::bindings::poll::poll::add_to_linker(&mut linker, |x| x)?; | ||
preview2::bindings::cli::exit::add_to_linker(&mut linker, |x| x)?; | ||
preview2::bindings::cli::stdin::add_to_linker(&mut linker, |x| x)?; | ||
preview2::bindings::cli::stdout::add_to_linker(&mut linker, |x| x)?; | ||
preview2::bindings::cli::stderr::add_to_linker(&mut linker, |x| x)?; | ||
preview2::bindings::cli::terminal_input::add_to_linker(&mut linker, |x| x)?; | ||
preview2::bindings::cli::terminal_output::add_to_linker(&mut linker, |x| x)?; | ||
preview2::bindings::cli::terminal_stdin::add_to_linker(&mut linker, |x| x)?; | ||
preview2::bindings::cli::terminal_stdout::add_to_linker(&mut linker, |x| x)?; | ||
preview2::bindings::cli::terminal_stderr::add_to_linker(&mut linker, |x| x)?; | ||
preview2::bindings::cli::environment::add_to_linker(&mut linker, |x| x)?; | ||
preview2::bindings::filesystem::types::add_to_linker(&mut linker, |x| x)?; | ||
preview2::bindings::filesystem::preopens::add_to_linker(&mut linker, |x| x)?; | ||
preview2::bindings::sockets::tcp::add_to_linker(&mut linker, |x| x)?; | ||
preview2::bindings::sockets::tcp_create_socket::add_to_linker(&mut linker, |x| x)?; | ||
preview2::bindings::sockets::network::add_to_linker(&mut linker, |x| x)?; | ||
preview2::bindings::sockets::instance_network::add_to_linker(&mut linker, |x| x)?; | ||
|
||
// Create our wasi context. | ||
let mut table = Table::new(); | ||
let wasi = WasiCtxBuilder::new() | ||
.inherit_stdio() | ||
.inherit_network(ambient_authority()) | ||
.arg(name) | ||
.build(&mut table)?; | ||
|
||
let mut store = Store::new(&ENGINE, SocketsCtx { table, wasi }); | ||
|
||
let (command, _instance) = Command::instantiate_async(&mut store, &component, &linker).await?; | ||
command | ||
.wasi_cli_run() | ||
.call_run(&mut store) | ||
.await? | ||
.map_err(|()| anyhow::anyhow!("command returned with failing exit status")) | ||
} | ||
|
||
#[test_log::test(tokio::test(flavor = "multi_thread"))] | ||
async fn tcp_v4() { | ||
run("tcp_v4").await.unwrap(); | ||
} | ||
|
||
#[test_log::test(tokio::test(flavor = "multi_thread"))] | ||
async fn tcp_v6() { | ||
run("tcp_v6").await.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "wasi-sockets-tests" | ||
version = "0.0.0" | ||
readme = "README.md" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
anyhow = { workspace = true } | ||
wit-bindgen = { workspace = true, default-features = false, features = ["macros"] } |
Oops, something went wrong.