Skip to content

Commit d526299

Browse files
authored
Mirror WASIp{2,3} sockets implementations (#11379)
This is an implementation of #11362 but for the `wasi:sockets` implementation of WASIp2. This additionally internalizes much of the WASIp3 implementation with `pub(crate)` to avoid unnecessarily exposing implementation details of the crate.
1 parent db87fbe commit d526299

File tree

16 files changed

+165
-154
lines changed

16 files changed

+165
-154
lines changed

crates/wasi/src/ctx.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use std::mem;
1212
use std::net::SocketAddr;
1313
use std::path::Path;
1414
use std::pin::Pin;
15-
use std::sync::Arc;
1615
use tokio::io::{stderr, stdin, stdout};
1716

1817
/// Builder-style structure used to create a [`WasiCtx`].
@@ -390,7 +389,7 @@ impl WasiCtxBuilder {
390389
+ Sync
391390
+ 'static,
392391
{
393-
self.sockets.socket_addr_check = SocketAddrCheck(Arc::new(check));
392+
self.sockets.socket_addr_check = SocketAddrCheck::new(check);
394393
self
395394
}
396395

crates/wasi/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ pub use self::ctx::{WasiCtx, WasiCtxBuilder};
3434
pub use self::error::{I32Exit, TrappableError};
3535
pub use self::filesystem::{DirPerms, FilePerms, OpenMode};
3636
pub use self::random::{Deterministic, thread_rng};
37-
pub use self::sockets::{AllowedNetworkUses, SocketAddrUse};
3837
pub use self::view::{WasiCtxView, WasiView};
3938
#[doc(no_inline)]
4039
pub use async_trait::async_trait;

crates/wasi/src/p2/host/instance_network.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use crate::p2::WasiCtxView;
21
use crate::p2::bindings::sockets::instance_network;
32
use crate::p2::network::Network;
3+
use crate::sockets::WasiSocketsCtxView;
44
use wasmtime::component::Resource;
55

6-
impl instance_network::Host for WasiCtxView<'_> {
6+
impl instance_network::Host for WasiSocketsCtxView<'_> {
77
fn instance_network(&mut self) -> Result<Resource<Network>, anyhow::Error> {
88
let network = Network {
9-
socket_addr_check: self.ctx.sockets.socket_addr_check.clone(),
10-
allow_ip_name_lookup: self.ctx.sockets.allowed_network_uses.ip_name_lookup,
9+
socket_addr_check: self.ctx.socket_addr_check.clone(),
10+
allow_ip_name_lookup: self.ctx.allowed_network_uses.ip_name_lookup,
1111
};
1212
let network = self.table.push(network)?;
1313
Ok(network)

crates/wasi/src/p2/host/network.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
use crate::p2::SocketError;
12
use crate::p2::bindings::sockets::network::{
23
self, ErrorCode, IpAddress, IpAddressFamily, IpSocketAddress, Ipv4SocketAddress,
34
Ipv6SocketAddress,
45
};
5-
use crate::p2::{SocketError, WasiCtxView};
6+
use crate::sockets::WasiSocketsCtxView;
67
use crate::sockets::util::{from_ipv4_addr, from_ipv6_addr, to_ipv4_addr, to_ipv6_addr};
78
use anyhow::Error;
89
use rustix::io::Errno;
910
use std::io;
1011
use wasmtime::component::Resource;
1112

12-
impl network::Host for WasiCtxView<'_> {
13+
impl network::Host for WasiSocketsCtxView<'_> {
1314
fn convert_error_code(&mut self, error: SocketError) -> anyhow::Result<ErrorCode> {
1415
error.downcast()
1516
}
@@ -25,7 +26,7 @@ impl network::Host for WasiCtxView<'_> {
2526
}
2627
}
2728

28-
impl crate::p2::bindings::sockets::network::HostNetwork for WasiCtxView<'_> {
29+
impl crate::p2::bindings::sockets::network::HostNetwork for WasiSocketsCtxView<'_> {
2930
fn drop(&mut self, this: Resource<network::Network>) -> Result<(), anyhow::Error> {
3031
self.table.delete(this)?;
3132

crates/wasi/src/p2/host/tcp.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
use crate::p2::SocketResult;
12
use crate::p2::bindings::{
23
sockets::network::{IpAddressFamily, IpSocketAddress, Network},
34
sockets::tcp::{self, ShutdownType},
45
};
5-
use crate::p2::{SocketResult, WasiCtxView};
6-
use crate::sockets::{SocketAddrUse, SocketAddressFamily};
6+
use crate::sockets::{SocketAddrUse, SocketAddressFamily, WasiSocketsCtxView};
77
use std::net::SocketAddr;
88
use std::time::Duration;
99
use wasmtime::component::Resource;
@@ -12,16 +12,16 @@ use wasmtime_wasi_io::{
1212
streams::{DynInputStream, DynOutputStream},
1313
};
1414

15-
impl tcp::Host for WasiCtxView<'_> {}
15+
impl tcp::Host for WasiSocketsCtxView<'_> {}
1616

17-
impl crate::p2::host::tcp::tcp::HostTcpSocket for WasiCtxView<'_> {
17+
impl crate::p2::host::tcp::tcp::HostTcpSocket for WasiSocketsCtxView<'_> {
1818
async fn start_bind(
1919
&mut self,
2020
this: Resource<tcp::TcpSocket>,
2121
network: Resource<Network>,
2222
local_address: IpSocketAddress,
2323
) -> SocketResult<()> {
24-
self.ctx.sockets.allowed_network_uses.check_allowed_tcp()?;
24+
self.ctx.allowed_network_uses.check_allowed_tcp()?;
2525
let network = self.table.get(&network)?;
2626
let local_address: SocketAddr = local_address.into();
2727

@@ -48,7 +48,7 @@ impl crate::p2::host::tcp::tcp::HostTcpSocket for WasiCtxView<'_> {
4848
network: Resource<Network>,
4949
remote_address: IpSocketAddress,
5050
) -> SocketResult<()> {
51-
self.ctx.sockets.allowed_network_uses.check_allowed_tcp()?;
51+
self.ctx.allowed_network_uses.check_allowed_tcp()?;
5252
let network = self.table.get(&network)?;
5353
let remote_address: SocketAddr = remote_address.into();
5454

@@ -78,7 +78,7 @@ impl crate::p2::host::tcp::tcp::HostTcpSocket for WasiCtxView<'_> {
7878
}
7979

8080
fn start_listen(&mut self, this: Resource<tcp::TcpSocket>) -> SocketResult<()> {
81-
self.ctx.sockets.allowed_network_uses.check_allowed_tcp()?;
81+
self.ctx.allowed_network_uses.check_allowed_tcp()?;
8282
let socket = self.table.get_mut(&this)?;
8383

8484
socket.start_listen()
@@ -97,7 +97,7 @@ impl crate::p2::host::tcp::tcp::HostTcpSocket for WasiCtxView<'_> {
9797
Resource<DynInputStream>,
9898
Resource<DynOutputStream>,
9999
)> {
100-
self.ctx.sockets.allowed_network_uses.check_allowed_tcp()?;
100+
self.ctx.allowed_network_uses.check_allowed_tcp()?;
101101
let socket = self.table.get_mut(&this)?;
102102

103103
let (tcp_socket, input, output) = socket.accept()?;
@@ -281,10 +281,8 @@ impl crate::p2::host::tcp::tcp::HostTcpSocket for WasiCtxView<'_> {
281281
}
282282

283283
pub mod sync {
284-
use wasmtime::component::Resource;
285-
286284
use crate::p2::{
287-
SocketError, WasiCtxView,
285+
SocketError,
288286
bindings::{
289287
sockets::{
290288
network::Network,
@@ -297,10 +295,12 @@ pub mod sync {
297295
},
298296
};
299297
use crate::runtime::in_tokio;
298+
use crate::sockets::WasiSocketsCtxView;
299+
use wasmtime::component::Resource;
300300

301-
impl tcp::Host for WasiCtxView<'_> {}
301+
impl tcp::Host for WasiSocketsCtxView<'_> {}
302302

303-
impl HostTcpSocket for WasiCtxView<'_> {
303+
impl HostTcpSocket for WasiSocketsCtxView<'_> {
304304
fn start_bind(
305305
&mut self,
306306
self_: Resource<TcpSocket>,

crates/wasi/src/p2/host/tcp_create_socket.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use crate::p2::SocketResult;
12
use crate::p2::bindings::{sockets::network::IpAddressFamily, sockets::tcp_create_socket};
23
use crate::p2::tcp::TcpSocket;
3-
use crate::p2::{SocketResult, WasiCtxView};
4+
use crate::sockets::WasiSocketsCtxView;
45
use wasmtime::component::Resource;
56

6-
impl tcp_create_socket::Host for WasiCtxView<'_> {
7+
impl tcp_create_socket::Host for WasiSocketsCtxView<'_> {
78
fn create_tcp_socket(
89
&mut self,
910
address_family: IpAddressFamily,

crates/wasi/src/p2/host/udp.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use crate::p2::bindings::sockets::network::{ErrorCode, IpAddressFamily, IpSocketAddress, Network};
22
use crate::p2::bindings::sockets::udp;
33
use crate::p2::udp::{IncomingDatagramStream, OutgoingDatagramStream, SendState, UdpState};
4-
use crate::p2::{Pollable, SocketError, SocketResult, WasiCtxView};
4+
use crate::p2::{Pollable, SocketError, SocketResult};
55
use crate::sockets::util::{
66
get_ip_ttl, get_ipv6_unicast_hops, is_valid_address_family, is_valid_remote_address,
77
receive_buffer_size, send_buffer_size, set_receive_buffer_size, set_send_buffer_size,
88
set_unicast_hop_limit, udp_bind, udp_disconnect,
99
};
10-
use crate::sockets::{MAX_UDP_DATAGRAM_SIZE, SocketAddrUse, SocketAddressFamily};
10+
use crate::sockets::{
11+
MAX_UDP_DATAGRAM_SIZE, SocketAddrUse, SocketAddressFamily, WasiSocketsCtxView,
12+
};
1113
use anyhow::anyhow;
1214
use async_trait::async_trait;
1315
use io_lifetimes::AsSocketlike;
@@ -17,16 +19,16 @@ use tokio::io::Interest;
1719
use wasmtime::component::Resource;
1820
use wasmtime_wasi_io::poll::DynPollable;
1921

20-
impl udp::Host for WasiCtxView<'_> {}
22+
impl udp::Host for WasiSocketsCtxView<'_> {}
2123

22-
impl udp::HostUdpSocket for WasiCtxView<'_> {
24+
impl udp::HostUdpSocket for WasiSocketsCtxView<'_> {
2325
async fn start_bind(
2426
&mut self,
2527
this: Resource<udp::UdpSocket>,
2628
network: Resource<Network>,
2729
local_address: IpSocketAddress,
2830
) -> SocketResult<()> {
29-
self.ctx.sockets.allowed_network_uses.check_allowed_udp()?;
31+
self.ctx.allowed_network_uses.check_allowed_udp()?;
3032

3133
match self.table.get(&this)?.udp_state {
3234
UdpState::Default => {}
@@ -274,7 +276,7 @@ impl udp::HostUdpSocket for WasiCtxView<'_> {
274276
}
275277
}
276278

277-
impl udp::HostIncomingDatagramStream for WasiCtxView<'_> {
279+
impl udp::HostIncomingDatagramStream for WasiSocketsCtxView<'_> {
278280
fn receive(
279281
&mut self,
280282
this: Resource<udp::IncomingDatagramStream>,
@@ -362,7 +364,7 @@ impl Pollable for IncomingDatagramStream {
362364
}
363365
}
364366

365-
impl udp::HostOutgoingDatagramStream for WasiCtxView<'_> {
367+
impl udp::HostOutgoingDatagramStream for WasiSocketsCtxView<'_> {
366368
fn check_send(&mut self, this: Resource<udp::OutgoingDatagramStream>) -> SocketResult<u64> {
367369
let stream = self.table.get_mut(&this)?;
368370

@@ -506,7 +508,7 @@ pub mod sync {
506508
use wasmtime::component::Resource;
507509

508510
use crate::p2::{
509-
SocketError, WasiCtxView,
511+
SocketError,
510512
bindings::{
511513
sockets::{
512514
network::Network,
@@ -526,10 +528,11 @@ pub mod sync {
526528
},
527529
};
528530
use crate::runtime::in_tokio;
531+
use crate::sockets::WasiSocketsCtxView;
529532

530-
impl udp::Host for WasiCtxView<'_> {}
533+
impl udp::Host for WasiSocketsCtxView<'_> {}
531534

532-
impl HostUdpSocket for WasiCtxView<'_> {
535+
impl HostUdpSocket for WasiSocketsCtxView<'_> {
533536
fn start_bind(
534537
&mut self,
535538
self_: Resource<UdpSocket>,
@@ -628,7 +631,7 @@ pub mod sync {
628631
}
629632
}
630633

631-
impl HostIncomingDatagramStream for WasiCtxView<'_> {
634+
impl HostIncomingDatagramStream for WasiSocketsCtxView<'_> {
632635
fn receive(
633636
&mut self,
634637
self_: Resource<IncomingDatagramStream>,
@@ -667,7 +670,7 @@ pub mod sync {
667670
}
668671
}
669672

670-
impl HostOutgoingDatagramStream for WasiCtxView<'_> {
673+
impl HostOutgoingDatagramStream for WasiSocketsCtxView<'_> {
671674
fn check_send(
672675
&mut self,
673676
self_: Resource<OutgoingDatagramStream>,

crates/wasi/src/p2/host/udp_create_socket.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use crate::p2::SocketResult;
12
use crate::p2::bindings::{sockets::network::IpAddressFamily, sockets::udp_create_socket};
23
use crate::p2::udp::UdpSocket;
3-
use crate::p2::{SocketResult, WasiCtxView};
4+
use crate::sockets::WasiSocketsCtxView;
45
use wasmtime::component::Resource;
56

6-
impl udp_create_socket::Host for WasiCtxView<'_> {
7+
impl udp_create_socket::Host for WasiSocketsCtxView<'_> {
78
fn create_udp_socket(
89
&mut self,
910
address_family: IpAddressFamily,

crates/wasi/src/p2/ip_name_lookup.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use crate::p2::SocketError;
12
use crate::p2::bindings::sockets::ip_name_lookup::{Host, HostResolveAddressStream};
23
use crate::p2::bindings::sockets::network::{ErrorCode, IpAddress, Network};
3-
use crate::p2::{SocketError, WasiCtxView};
44
use crate::runtime::{AbortOnDropJoinHandle, spawn_blocking};
5+
use crate::sockets::WasiSocketsCtxView;
56
use anyhow::Result;
67
use std::mem;
78
use std::net::ToSocketAddrs;
@@ -17,7 +18,7 @@ pub enum ResolveAddressStream {
1718
Done(Result<vec::IntoIter<IpAddress>, SocketError>),
1819
}
1920

20-
impl Host for WasiCtxView<'_> {
21+
impl Host for WasiSocketsCtxView<'_> {
2122
fn resolve_addresses(
2223
&mut self,
2324
network: Resource<Network>,
@@ -37,7 +38,7 @@ impl Host for WasiCtxView<'_> {
3738
}
3839
}
3940

40-
impl HostResolveAddressStream for WasiCtxView<'_> {
41+
impl HostResolveAddressStream for WasiSocketsCtxView<'_> {
4142
fn resolve_next_address(
4243
&mut self,
4344
resource: Resource<ResolveAddressStream>,

crates/wasi/src/p2/mod.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224
use crate::cli::{WasiCli, WasiCliView};
225225
use crate::clocks::{WasiClocks, WasiClocksView};
226226
use crate::random::WasiRandom;
227+
use crate::sockets::{WasiSockets, WasiSocketsView};
227228
use crate::{WasiCtxView, WasiView};
228229
use wasmtime::component::{HasData, Linker, ResourceTable};
229230

@@ -323,8 +324,8 @@ pub fn add_to_linker_with_options_async<T: WasiView>(
323324

324325
let l = linker;
325326
bindings::filesystem::types::add_to_linker::<T, HasWasi>(l, T::ctx)?;
326-
bindings::sockets::tcp::add_to_linker::<T, HasWasi>(l, T::ctx)?;
327-
bindings::sockets::udp::add_to_linker::<T, HasWasi>(l, T::ctx)?;
327+
bindings::sockets::tcp::add_to_linker::<T, WasiSockets>(l, T::sockets)?;
328+
bindings::sockets::udp::add_to_linker::<T, WasiSockets>(l, T::sockets)?;
328329
Ok(())
329330
}
330331

@@ -356,11 +357,11 @@ where
356357
cli::terminal_stdin::add_to_linker::<T, WasiCli>(l, T::cli)?;
357358
cli::terminal_stdout::add_to_linker::<T, WasiCli>(l, T::cli)?;
358359
cli::terminal_stderr::add_to_linker::<T, WasiCli>(l, T::cli)?;
359-
sockets::tcp_create_socket::add_to_linker::<T, HasWasi>(l, T::ctx)?;
360-
sockets::udp_create_socket::add_to_linker::<T, HasWasi>(l, T::ctx)?;
361-
sockets::instance_network::add_to_linker::<T, HasWasi>(l, T::ctx)?;
362-
sockets::network::add_to_linker::<T, HasWasi>(l, &options.into(), T::ctx)?;
363-
sockets::ip_name_lookup::add_to_linker::<T, HasWasi>(l, T::ctx)?;
360+
sockets::tcp_create_socket::add_to_linker::<T, WasiSockets>(l, T::sockets)?;
361+
sockets::udp_create_socket::add_to_linker::<T, WasiSockets>(l, T::sockets)?;
362+
sockets::instance_network::add_to_linker::<T, WasiSockets>(l, T::sockets)?;
363+
sockets::network::add_to_linker::<T, WasiSockets>(l, &options.into(), T::sockets)?;
364+
sockets::ip_name_lookup::add_to_linker::<T, WasiSockets>(l, T::sockets)?;
364365
Ok(())
365366
}
366367

@@ -467,8 +468,8 @@ pub fn add_to_linker_with_options_sync<T: WasiView>(
467468

468469
let l = linker;
469470
bindings::sync::filesystem::types::add_to_linker::<T, HasWasi>(l, T::ctx)?;
470-
bindings::sync::sockets::tcp::add_to_linker::<T, HasWasi>(l, T::ctx)?;
471-
bindings::sync::sockets::udp::add_to_linker::<T, HasWasi>(l, T::ctx)?;
471+
bindings::sync::sockets::tcp::add_to_linker::<T, WasiSockets>(l, T::sockets)?;
472+
bindings::sync::sockets::udp::add_to_linker::<T, WasiSockets>(l, T::sockets)?;
472473
Ok(())
473474
}
474475

0 commit comments

Comments
 (0)