Skip to content

Commit 2b3e795

Browse files
dicejpimeys
authored andcommitted
add wasm32-wasip2 support
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 <[email protected]>
1 parent 0a15768 commit 2b3e795

File tree

4 files changed

+75
-52
lines changed

4 files changed

+75
-52
lines changed

tokio/Cargo.toml

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ default = []
2727

2828
# enable everything
2929
full = [
30-
"fs",
31-
"io-util",
32-
"io-std",
33-
"macros",
34-
"net",
35-
"parking_lot",
36-
"process",
37-
"rt",
38-
"rt-multi-thread",
39-
"signal",
40-
"sync",
41-
"time",
30+
"fs",
31+
"io-util",
32+
"io-std",
33+
"macros",
34+
"net",
35+
"parking_lot",
36+
"process",
37+
"rt",
38+
"rt-multi-thread",
39+
"signal",
40+
"sync",
41+
"time",
4242
]
4343

4444
fs = []
@@ -47,39 +47,39 @@ io-util = ["bytes"]
4747
io-std = []
4848
macros = ["tokio-macros"]
4949
net = [
50-
"libc",
51-
"mio/os-poll",
52-
"mio/os-ext",
53-
"mio/net",
54-
"socket2",
55-
"windows-sys/Win32_Foundation",
56-
"windows-sys/Win32_Security",
57-
"windows-sys/Win32_Storage_FileSystem",
58-
"windows-sys/Win32_System_Pipes",
59-
"windows-sys/Win32_System_SystemServices",
50+
"libc",
51+
"mio/os-poll",
52+
"mio/os-ext",
53+
"mio/net",
54+
"socket2",
55+
"windows-sys/Win32_Foundation",
56+
"windows-sys/Win32_Security",
57+
"windows-sys/Win32_Storage_FileSystem",
58+
"windows-sys/Win32_System_Pipes",
59+
"windows-sys/Win32_System_SystemServices",
6060
]
6161
process = [
62-
"bytes",
63-
"libc",
64-
"mio/os-poll",
65-
"mio/os-ext",
66-
"mio/net",
67-
"signal-hook-registry",
68-
"windows-sys/Win32_Foundation",
69-
"windows-sys/Win32_System_Threading",
70-
"windows-sys/Win32_System_WindowsProgramming",
62+
"bytes",
63+
"libc",
64+
"mio/os-poll",
65+
"mio/os-ext",
66+
"mio/net",
67+
"signal-hook-registry",
68+
"windows-sys/Win32_Foundation",
69+
"windows-sys/Win32_System_Threading",
70+
"windows-sys/Win32_System_WindowsProgramming",
7171
]
7272
# Includes basic task execution capabilities
7373
rt = []
7474
rt-multi-thread = ["rt"]
7575
signal = [
76-
"libc",
77-
"mio/os-poll",
78-
"mio/net",
79-
"mio/os-ext",
80-
"signal-hook-registry",
81-
"windows-sys/Win32_Foundation",
82-
"windows-sys/Win32_System_Console",
76+
"libc",
77+
"mio/os-poll",
78+
"mio/net",
79+
"mio/os-ext",
80+
"signal-hook-registry",
81+
"windows-sys/Win32_Foundation",
82+
"windows-sys/Win32_System_Console",
8383
]
8484
sync = []
8585
test-util = ["rt", "sync", "time"]
@@ -92,7 +92,8 @@ pin-project-lite = "0.2.11"
9292

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

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

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

115118
[target.'cfg(unix)'.dev-dependencies]
116119
libc = { version = "0.2.168" }
117-
nix = { version = "0.29.0", default-features = false, features = ["aio", "fs", "socket"] }
120+
nix = { version = "0.29.0", default-features = false, features = [
121+
"aio",
122+
"fs",
123+
"socket",
124+
] }
118125

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

123130
[target.'cfg(windows)'.dev-dependencies.windows-sys]
124131
version = "0.52"
125-
features = [
126-
"Win32_Foundation",
127-
"Win32_Security_Authorization",
128-
]
132+
features = ["Win32_Foundation", "Win32_Security_Authorization"]
129133

130134
[dev-dependencies]
131135
tokio-test = { version = "0.4.0", path = "../tokio-test" }
@@ -157,7 +161,14 @@ tracing-mock = "= 0.1.0-beta.1"
157161
[package.metadata.docs.rs]
158162
all-features = true
159163
# enable unstable features in the documentation
160-
rustdoc-args = ["--cfg", "docsrs", "--cfg", "tokio_unstable", "--cfg", "tokio_taskdump"]
164+
rustdoc-args = [
165+
"--cfg",
166+
"docsrs",
167+
"--cfg",
168+
"tokio_unstable",
169+
"--cfg",
170+
"tokio_taskdump",
171+
]
161172
# it's necessary to _also_ pass `--cfg tokio_unstable` and `--cfg tokio_taskdump`
162173
# to rustc, or else dependencies will not be enabled, and the docs build will fail.
163174
rustc-args = ["--cfg", "tokio_unstable", "--cfg", "tokio_taskdump"]
@@ -169,9 +180,9 @@ features = ["full", "test-util"]
169180
# The following are types that are allowed to be exposed in Tokio's public API.
170181
# The standard library is allowed by default.
171182
allowed_external_types = [
172-
"bytes::buf::buf_impl::Buf",
173-
"bytes::buf::buf_mut::BufMut",
174-
"tokio_macros::*",
183+
"bytes::buf::buf_impl::Buf",
184+
"bytes::buf::buf_mut::BufMut",
185+
"tokio_macros::*",
175186
]
176187

177188
[lints]

tokio/src/macros/cfg.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,15 @@ macro_rules! cfg_not_wasi {
594594
}
595595
}
596596

597+
macro_rules! cfg_not_wasip1 {
598+
($($item:item)*) => {
599+
$(
600+
#[cfg(any(not(target_os = "wasi"), target_env = "p2"))]
601+
$item
602+
)*
603+
}
604+
}
605+
597606
macro_rules! cfg_is_wasm_not_wasi {
598607
($($item:item)*) => {
599608
$(

tokio/src/net/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
//! [`AsyncFd`]: crate::io::unix::AsyncFd
3030
3131
mod addr;
32-
cfg_not_wasi! {
32+
cfg_not_wasip1! {
3333
#[cfg(feature = "net")]
3434
pub(crate) use addr::to_socket_addrs;
3535
}

tokio/src/net/tcp/stream.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
cfg_not_wasi! {
2+
use std::time::Duration;
3+
}
4+
5+
cfg_not_wasip1! {
26
use crate::net::{to_socket_addrs, ToSocketAddrs};
37
use std::future::poll_fn;
4-
use std::time::Duration;
58
}
69

710
use crate::io::{AsyncRead, AsyncWrite, Interest, PollEvented, ReadBuf, Ready};
@@ -72,7 +75,7 @@ cfg_net! {
7275
}
7376

7477
impl TcpStream {
75-
cfg_not_wasi! {
78+
cfg_not_wasip1! {
7679
/// Opens a TCP connection to a remote host.
7780
///
7881
/// `addr` is an address of the remote host. Anything which implements the

0 commit comments

Comments
 (0)