Skip to content

Commit 388f6af

Browse files
committed
add wasm32-wasip2 support
This implementation currently uses a mix of POSIX-style APIs (provided by `wasi-libc` via the `libc` crate) and WASIp2-native APIs (provided by the `wasi` crate). Alternatively, we could implement `Selector` using only POSIX APIs, e.g. `poll(2)`. However, that would add an extra layer of abstraction to support and debug, as well as make it impossible to support polling `wasi:io/poll/pollable` objects which cannot be represented as POSIX file descriptors (e.g. timer events, DNS queries, HTTP requests, etc.). Another approach would be to use _only_ the WASIp2 APIs and bypass `wasi-libc` entirely. However, that would break interoperability with both Rust `std` and e.g. C libraries which expect to work with file descriptors. Since `wasi-libc` does not yet provide a public API for converting between file descriptors and WASIp2 resource handles, we currently use a non-public API (see the `netc` module below) to do so. Once WebAssembly/wasi-libc#542 is addressed, we'll be able to switch to a public API. 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. add WASIp2 TODO comments // TODO #1: Add a public, WASIp2-only API for registering // `wasi::io::poll::Pollable`s directly (i.e. those which do not correspond to // any `wasi-libc` file descriptor, such as `wasi:http` requests). // // TODO #2: Add support for binding, listening, and accepting. This would // involve adding cases for `TCP_SOCKET_STATE_UNBOUND`, // `TCP_SOCKET_STATE_BOUND`, and `TCP_SOCKET_STATE_LISTENING` to the `match` // statements in `Selector::select`. // // TODO #3: Add support for UDP sockets. This would involve adding cases for // the `UDP_SOCKET_STATE_*` tags to the `match` statements in // `Selector::select`. Signed-off-by: Joel Dice <[email protected]>
1 parent f352b81 commit 388f6af

File tree

7 files changed

+1066
-373
lines changed

7 files changed

+1066
-373
lines changed

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,13 @@ features = [
6565
"Win32_System_WindowsProgramming", # General future used for various types/funcs
6666
]
6767

68-
[target.'cfg(target_os = "wasi")'.dependencies]
68+
[target.'cfg(all(target_os = "wasi", not(target_env = "p2")))'.dependencies]
6969
wasi = "0.11.0"
70+
71+
[target.'cfg(all(target_os = "wasi", target_env = "p2"))'.dependencies]
72+
wasi = "0.13.3"
73+
74+
[target.'cfg(target_os = "wasi")'.dependencies]
7075
libc = "0.2.159"
7176

7277
[dev-dependencies]
@@ -80,6 +85,7 @@ targets = [
8085
"aarch64-apple-ios",
8186
"aarch64-linux-android",
8287
"wasm32-wasip1",
88+
"wasm32-wasip2",
8389
"x86_64-apple-darwin",
8490
"x86_64-pc-windows-gnu",
8591
"x86_64-pc-windows-msvc",

src/io_source.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl<T> DerefMut for IoSource<T> {
104104
}
105105
}
106106

107-
#[cfg(any(unix, target_os = "hermit"))]
107+
#[cfg(any(unix, target_os = "hermit", all(target_os = "wasi", target_env = "p2")))]
108108
impl<T> event::Source for IoSource<T>
109109
where
110110
T: AsRawFd,
@@ -175,7 +175,7 @@ where
175175
}
176176
}
177177

178-
#[cfg(target_os = "wasi")]
178+
#[cfg(all(target_os = "wasi", not(target_env = "p2")))]
179179
impl<T> event::Source for IoSource<T>
180180
where
181181
T: AsRawFd,

src/net/tcp/stream.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::os::windows::io::{
1313
};
1414

1515
use crate::io_source::IoSource;
16-
#[cfg(not(target_os = "wasi"))]
16+
#[cfg(any(not(target_os = "wasi"), target_env = "p2"))]
1717
use crate::sys::tcp::{connect, new_for_addr};
1818
use crate::{event, Interest, Registry, Token};
1919

@@ -87,10 +87,10 @@ impl TcpStream {
8787
/// entries in the routing cache.
8888
///
8989
/// [write interest]: Interest::WRITABLE
90-
#[cfg(not(target_os = "wasi"))]
90+
#[cfg(any(not(target_os = "wasi"), target_env = "p2"))]
9191
pub fn connect(addr: SocketAddr) -> io::Result<TcpStream> {
9292
let socket = new_for_addr(addr)?;
93-
#[cfg(any(unix, target_os = "hermit"))]
93+
#[cfg(any(unix, target_os = "hermit", all(target_os = "wasi", target_env = "p2")))]
9494
let stream = unsafe { TcpStream::from_raw_fd(socket) };
9595
#[cfg(windows)]
9696
let stream = unsafe { TcpStream::from_raw_socket(socket as _) };

src/sys/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//! * `Waker`: see [`crate::Waker`].
1515
1616
cfg_os_poll! {
17+
#[cfg(not(all(target_os = "wasi", target_env = "p2")))]
1718
macro_rules! debug_detail {
1819
(
1920
$type: ident ($event_type: ty), $test: path,

0 commit comments

Comments
 (0)