Skip to content

Commit

Permalink
add wasm32-wasip2 support
Browse files Browse the repository at this point in the history
This adds support for the new `wasm32-wasip2` target platform, which includes
more extensive support for sockets than `wasm32-wasip1` (formerly known as
`wasm32-wasi`).

The bulk of the changes are in tokio-rs/mio#1836.  This
patch just tweaks a few `cfg` directives to indicate `wasm32-wasip2`'s
additional capabilities.

In the future, we could consider adding support for `ToSocketAddrs`.  WASIp2
natively supports asynchronous DNS lookups and is single threaded, whereas Tokio
currently assumes DNS lookups are blocking and require multithreading to emulate
async lookups.  A WASIp2-specific implementation could do the lookup directly
without multithreading.

I've tested this end-to-end using https://github.com/dicej/wasi-sockets-tests,
which includes smoke tests for `mio`, `tokio`, `tokio-postgres`, etc.  I'd also
be happy to add tests to this repo if appropriate; it would require adding a
dev-dependency on e.g. `wasmtime` to actually run the test cases.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
  • Loading branch information
dicej authored and pimeys committed Feb 14, 2025
1 parent 0a15768 commit 2b3e795
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 52 deletions.
109 changes: 60 additions & 49 deletions tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ default = []

# enable everything
full = [
"fs",
"io-util",
"io-std",
"macros",
"net",
"parking_lot",
"process",
"rt",
"rt-multi-thread",
"signal",
"sync",
"time",
"fs",
"io-util",
"io-std",
"macros",
"net",
"parking_lot",
"process",
"rt",
"rt-multi-thread",
"signal",
"sync",
"time",
]

fs = []
Expand All @@ -47,39 +47,39 @@ io-util = ["bytes"]
io-std = []
macros = ["tokio-macros"]
net = [
"libc",
"mio/os-poll",
"mio/os-ext",
"mio/net",
"socket2",
"windows-sys/Win32_Foundation",
"windows-sys/Win32_Security",
"windows-sys/Win32_Storage_FileSystem",
"windows-sys/Win32_System_Pipes",
"windows-sys/Win32_System_SystemServices",
"libc",
"mio/os-poll",
"mio/os-ext",
"mio/net",
"socket2",
"windows-sys/Win32_Foundation",
"windows-sys/Win32_Security",
"windows-sys/Win32_Storage_FileSystem",
"windows-sys/Win32_System_Pipes",
"windows-sys/Win32_System_SystemServices",
]
process = [
"bytes",
"libc",
"mio/os-poll",
"mio/os-ext",
"mio/net",
"signal-hook-registry",
"windows-sys/Win32_Foundation",
"windows-sys/Win32_System_Threading",
"windows-sys/Win32_System_WindowsProgramming",
"bytes",
"libc",
"mio/os-poll",
"mio/os-ext",
"mio/net",
"signal-hook-registry",
"windows-sys/Win32_Foundation",
"windows-sys/Win32_System_Threading",
"windows-sys/Win32_System_WindowsProgramming",
]
# Includes basic task execution capabilities
rt = []
rt-multi-thread = ["rt"]
signal = [
"libc",
"mio/os-poll",
"mio/net",
"mio/os-ext",
"signal-hook-registry",
"windows-sys/Win32_Foundation",
"windows-sys/Win32_System_Console",
"libc",
"mio/os-poll",
"mio/net",
"mio/os-ext",
"signal-hook-registry",
"windows-sys/Win32_Foundation",
"windows-sys/Win32_System_Console",
]
sync = []
test-util = ["rt", "sync", "time"]
Expand All @@ -92,7 +92,8 @@ pin-project-lite = "0.2.11"

# Everything else is optional...
bytes = { version = "1.2.1", optional = true }
mio = { version = "1.0.1", optional = true, default-features = false }
# TODO dicej: switch to upstream once WASIp2 support is merged:
mio = { git = "https://github.com/dicej/mio", branch = "wasip2", optional = true, default-features = false }
parking_lot = { version = "0.12.0", optional = true }

[target.'cfg(not(target_family = "wasm"))'.dependencies]
Expand All @@ -101,7 +102,9 @@ socket2 = { version = "0.5.5", optional = true, features = ["all"] }
# Currently unstable. The API exposed by these features may be broken at any time.
# Requires `--cfg tokio_unstable` to enable.
[target.'cfg(tokio_unstable)'.dependencies]
tracing = { version = "0.1.29", default-features = false, features = ["std"], optional = true } # Not in full
tracing = { version = "0.1.29", default-features = false, features = [
"std",
], optional = true } # Not in full

# Currently unstable. The API exposed by these features may be broken at any time.
# Requires `--cfg tokio_unstable` to enable.
Expand All @@ -114,18 +117,19 @@ signal-hook-registry = { version = "1.1.1", optional = true }

[target.'cfg(unix)'.dev-dependencies]
libc = { version = "0.2.168" }
nix = { version = "0.29.0", default-features = false, features = ["aio", "fs", "socket"] }
nix = { version = "0.29.0", default-features = false, features = [
"aio",
"fs",
"socket",
] }

[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.52"
optional = true

[target.'cfg(windows)'.dev-dependencies.windows-sys]
version = "0.52"
features = [
"Win32_Foundation",
"Win32_Security_Authorization",
]
features = ["Win32_Foundation", "Win32_Security_Authorization"]

[dev-dependencies]
tokio-test = { version = "0.4.0", path = "../tokio-test" }
Expand Down Expand Up @@ -157,7 +161,14 @@ tracing-mock = "= 0.1.0-beta.1"
[package.metadata.docs.rs]
all-features = true
# enable unstable features in the documentation
rustdoc-args = ["--cfg", "docsrs", "--cfg", "tokio_unstable", "--cfg", "tokio_taskdump"]
rustdoc-args = [
"--cfg",
"docsrs",
"--cfg",
"tokio_unstable",
"--cfg",
"tokio_taskdump",
]
# it's necessary to _also_ pass `--cfg tokio_unstable` and `--cfg tokio_taskdump`
# to rustc, or else dependencies will not be enabled, and the docs build will fail.
rustc-args = ["--cfg", "tokio_unstable", "--cfg", "tokio_taskdump"]
Expand All @@ -169,9 +180,9 @@ features = ["full", "test-util"]
# The following are types that are allowed to be exposed in Tokio's public API.
# The standard library is allowed by default.
allowed_external_types = [
"bytes::buf::buf_impl::Buf",
"bytes::buf::buf_mut::BufMut",
"tokio_macros::*",
"bytes::buf::buf_impl::Buf",
"bytes::buf::buf_mut::BufMut",
"tokio_macros::*",
]

[lints]
Expand Down
9 changes: 9 additions & 0 deletions tokio/src/macros/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,15 @@ macro_rules! cfg_not_wasi {
}
}

macro_rules! cfg_not_wasip1 {
($($item:item)*) => {
$(
#[cfg(any(not(target_os = "wasi"), target_env = "p2"))]
$item
)*
}
}

macro_rules! cfg_is_wasm_not_wasi {
($($item:item)*) => {
$(
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//! [`AsyncFd`]: crate::io::unix::AsyncFd
mod addr;
cfg_not_wasi! {
cfg_not_wasip1! {
#[cfg(feature = "net")]
pub(crate) use addr::to_socket_addrs;
}
Expand Down
7 changes: 5 additions & 2 deletions tokio/src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
cfg_not_wasi! {
use std::time::Duration;
}

cfg_not_wasip1! {
use crate::net::{to_socket_addrs, ToSocketAddrs};
use std::future::poll_fn;
use std::time::Duration;
}

use crate::io::{AsyncRead, AsyncWrite, Interest, PollEvented, ReadBuf, Ready};
Expand Down Expand Up @@ -72,7 +75,7 @@ cfg_net! {
}

impl TcpStream {
cfg_not_wasi! {
cfg_not_wasip1! {
/// Opens a TCP connection to a remote host.
///
/// `addr` is an address of the remote host. Anything which implements the
Expand Down

0 comments on commit 2b3e795

Please sign in to comment.