Skip to content

Commit 99e9853

Browse files
authored
Rollup merge of rust-lang#129638 - nickrum:wasip2-net, r=alexcrichton
Hook up std::net to wasi-libc on wasm32-wasip2 target One of the improvements of the `wasm32-wasip2` target over `wasm32-wasip1` is better support for networking. Right now, p2 is just re-using the `std::net` implementation from p1. This PR adds a new net module for p2 that makes use of net from `sys_common` and calls wasi-libc functions directly. There are currently a few limitations: - Duplicating a socket is not supported by WASIp2 (directly returns an error) - Peeking is not yet implemented in wasi-libc (we could let wasi-libc handle this, but I opted to directly return an error instead) - Vectored reads/writes are not supported by WASIp2 (the necessary functions are available in wasi-libc, but they call WASIp1 functions which do not support sockets, so I opted to directly return an error instead) - Getting/setting `TCP_NODELAY` is faked in wasi-libc (uses the fake implementation instead of returning an error) - Getting/setting `SO_LINGER` is not supported by WASIp2 (directly returns an error) - Setting `SO_REUSEADDR` is faked in wasi-libc (since this is done from `sys_common`, the fake implementation is used instead of returning an error) - Getting/setting `IPV6_V6ONLY` is not supported by WASIp2 and will always be set for IPv6 sockets (since this is done from `sys_common`, wasi-libc will return an error) - UDP broadcast/multicast is not supported by WASIp2 (since this is configured from `sys_common`, wasi-libc will return appropriate errors) - The `MSG_NOSIGNAL` send flag is a no-op because there are no signals in WASIp2 (since explicitly setting this flag would require a change to `sys_common` and the result would be exactly the same, I opted to not set it) Do those decisions make sense? While working on this PR, I noticed that there is a `std::os::wasi::net::TcpListenerExt` trait that adds a `sock_accept()` method to `std::net::TcpListener`. Now that WASIp2 supports standard accept, would it make sense to remove this? cc `@alexcrichton`
2 parents b362019 + 9ae087c commit 99e9853

32 files changed

+506
-47
lines changed

std/src/collections/hash/map/tests.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,7 @@ mod test_extract_if {
10181018
}
10191019

10201020
#[test]
1021+
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
10211022
fn drop_panic_leak() {
10221023
static PREDS: AtomicUsize = AtomicUsize::new(0);
10231024
static DROPS: AtomicUsize = AtomicUsize::new(0);
@@ -1047,6 +1048,7 @@ mod test_extract_if {
10471048
}
10481049

10491050
#[test]
1051+
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
10501052
fn pred_panic_leak() {
10511053
static PREDS: AtomicUsize = AtomicUsize::new(0);
10521054
static DROPS: AtomicUsize = AtomicUsize::new(0);
@@ -1076,6 +1078,7 @@ mod test_extract_if {
10761078

10771079
// Same as above, but attempt to use the iterator again after the panic in the predicate
10781080
#[test]
1081+
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
10791082
fn pred_panic_reuse() {
10801083
static PREDS: AtomicUsize = AtomicUsize::new(0);
10811084
static DROPS: AtomicUsize = AtomicUsize::new(0);

std/src/collections/hash/set/tests.rs

+2
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ fn test_extract_if() {
429429
}
430430

431431
#[test]
432+
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
432433
fn test_extract_if_drop_panic_leak() {
433434
static PREDS: AtomicU32 = AtomicU32::new(0);
434435
static DROPS: AtomicU32 = AtomicU32::new(0);
@@ -459,6 +460,7 @@ fn test_extract_if_drop_panic_leak() {
459460
}
460461

461462
#[test]
463+
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
462464
fn test_extract_if_pred_panic_leak() {
463465
static PREDS: AtomicU32 = AtomicU32::new(0);
464466
static DROPS: AtomicU32 = AtomicU32::new(0);

std/src/env/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::*;
22

33
#[test]
4-
#[cfg_attr(any(target_os = "emscripten", target_env = "sgx"), ignore)]
4+
#[cfg_attr(any(target_os = "emscripten", target_os = "wasi", target_env = "sgx"), ignore)]
55
fn test_self_exe_path() {
66
let path = current_exe();
77
assert!(path.is_ok());

std/src/fs.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@
88
#![stable(feature = "rust1", since = "1.0.0")]
99
#![deny(unsafe_op_in_unsafe_fn)]
1010

11-
#[cfg(all(test, not(any(target_os = "emscripten", target_env = "sgx", target_os = "xous"))))]
11+
#[cfg(all(
12+
test,
13+
not(any(
14+
target_os = "emscripten",
15+
target_os = "wasi",
16+
target_env = "sgx",
17+
target_os = "xous"
18+
))
19+
))]
1220
mod tests;
1321

1422
use crate::ffi::OsString;

std/src/io/buffered/tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ fn test_buffered_reader_stream_position() {
164164
}
165165

166166
#[test]
167+
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
167168
fn test_buffered_reader_stream_position_panic() {
168169
let inner: &[u8] = &[5, 6, 7, 0, 1, 2, 3, 4];
169170
let mut reader = BufReader::with_capacity(4, io::Cursor::new(inner));
@@ -487,7 +488,7 @@ fn dont_panic_in_drop_on_panicked_flush() {
487488
}
488489

489490
#[test]
490-
#[cfg_attr(target_os = "emscripten", ignore)]
491+
#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads
491492
fn panic_in_write_doesnt_flush_in_drop() {
492493
static WRITES: AtomicUsize = AtomicUsize::new(0);
493494

std/src/io/stdio/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn stderrlock_unwind_safe() {
2525
fn assert_unwind_safe<T: UnwindSafe + RefUnwindSafe>() {}
2626

2727
#[test]
28-
#[cfg_attr(target_os = "emscripten", ignore)]
28+
#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads
2929
fn panic_doesnt_poison() {
3030
thread::spawn(|| {
3131
let _a = stdin();
@@ -48,17 +48,17 @@ fn panic_doesnt_poison() {
4848
}
4949

5050
#[test]
51-
#[cfg_attr(target_os = "emscripten", ignore)]
51+
#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads
5252
fn test_lock_stderr() {
5353
test_lock(stderr, || stderr().lock());
5454
}
5555
#[test]
56-
#[cfg_attr(target_os = "emscripten", ignore)]
56+
#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads
5757
fn test_lock_stdin() {
5858
test_lock(stdin, || stdin().lock());
5959
}
6060
#[test]
61-
#[cfg_attr(target_os = "emscripten", ignore)]
61+
#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads
6262
fn test_lock_stdout() {
6363
test_lock(stdout, || stdout().lock());
6464
}

std/src/net/ip_addr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Tests for this module
2-
#[cfg(all(test, not(target_os = "emscripten")))]
2+
#[cfg(all(test, not(any(target_os = "emscripten", all(target_os = "wasi", target_env = "p1")))))]
33
mod tests;
44

55
#[stable(feature = "ip_addr", since = "1.7.0")]

std/src/net/socket_addr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Tests for this module
2-
#[cfg(all(test, not(target_os = "emscripten")))]
2+
#[cfg(all(test, not(any(target_os = "emscripten", all(target_os = "wasi", target_env = "p1")))))]
33
mod tests;
44

55
#[stable(feature = "rust1", since = "1.0.0")]

std/src/net/tcp.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
#![deny(unsafe_op_in_unsafe_fn)]
22

3-
#[cfg(all(test, not(any(target_os = "emscripten", target_os = "xous"))))]
3+
#[cfg(all(
4+
test,
5+
not(any(
6+
target_os = "emscripten",
7+
all(target_os = "wasi", target_env = "p1"),
8+
target_os = "xous"
9+
))
10+
))]
411
mod tests;
512

613
use crate::fmt;

std/src/net/tcp/tests.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ fn connect_timeout_error() {
5757
}
5858

5959
#[test]
60+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
6061
fn listen_localhost() {
6162
let socket_addr = next_test_ip4();
6263
let listener = t!(TcpListener::bind(&socket_addr));
@@ -73,6 +74,7 @@ fn listen_localhost() {
7374
}
7475

7576
#[test]
77+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
7678
fn connect_loopback() {
7779
each_ip(&mut |addr| {
7880
let acceptor = t!(TcpListener::bind(&addr));
@@ -94,6 +96,7 @@ fn connect_loopback() {
9496
}
9597

9698
#[test]
99+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
97100
fn smoke_test() {
98101
each_ip(&mut |addr| {
99102
let acceptor = t!(TcpListener::bind(&addr));
@@ -114,6 +117,7 @@ fn smoke_test() {
114117
}
115118

116119
#[test]
120+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
117121
fn read_eof() {
118122
each_ip(&mut |addr| {
119123
let acceptor = t!(TcpListener::bind(&addr));
@@ -133,6 +137,7 @@ fn read_eof() {
133137
}
134138

135139
#[test]
140+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
136141
fn write_close() {
137142
each_ip(&mut |addr| {
138143
let acceptor = t!(TcpListener::bind(&addr));
@@ -161,6 +166,7 @@ fn write_close() {
161166
}
162167

163168
#[test]
169+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
164170
fn multiple_connect_serial() {
165171
each_ip(&mut |addr| {
166172
let max = 10;
@@ -183,6 +189,7 @@ fn multiple_connect_serial() {
183189
}
184190

185191
#[test]
192+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
186193
fn multiple_connect_interleaved_greedy_schedule() {
187194
const MAX: usize = 10;
188195
each_ip(&mut |addr| {
@@ -220,6 +227,7 @@ fn multiple_connect_interleaved_greedy_schedule() {
220227
}
221228

222229
#[test]
230+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
223231
fn multiple_connect_interleaved_lazy_schedule() {
224232
const MAX: usize = 10;
225233
each_ip(&mut |addr| {
@@ -255,6 +263,7 @@ fn multiple_connect_interleaved_lazy_schedule() {
255263
}
256264

257265
#[test]
266+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
258267
fn socket_and_peer_name() {
259268
each_ip(&mut |addr| {
260269
let listener = t!(TcpListener::bind(&addr));
@@ -270,6 +279,7 @@ fn socket_and_peer_name() {
270279
}
271280

272281
#[test]
282+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
273283
fn partial_read() {
274284
each_ip(&mut |addr| {
275285
let (tx, rx) = channel();
@@ -291,6 +301,7 @@ fn partial_read() {
291301
}
292302

293303
#[test]
304+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
294305
fn read_buf() {
295306
each_ip(&mut |addr| {
296307
let srv = t!(TcpListener::bind(&addr));
@@ -389,6 +400,7 @@ fn double_bind() {
389400
}
390401

391402
#[test]
403+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
392404
fn tcp_clone_smoke() {
393405
each_ip(&mut |addr| {
394406
let acceptor = t!(TcpListener::bind(&addr));
@@ -420,6 +432,7 @@ fn tcp_clone_smoke() {
420432
}
421433

422434
#[test]
435+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
423436
fn tcp_clone_two_read() {
424437
each_ip(&mut |addr| {
425438
let acceptor = t!(TcpListener::bind(&addr));
@@ -454,6 +467,7 @@ fn tcp_clone_two_read() {
454467
}
455468

456469
#[test]
470+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
457471
fn tcp_clone_two_write() {
458472
each_ip(&mut |addr| {
459473
let acceptor = t!(TcpListener::bind(&addr));
@@ -483,6 +497,7 @@ fn tcp_clone_two_write() {
483497
#[test]
484498
// FIXME: https://github.com/fortanix/rust-sgx/issues/110
485499
#[cfg_attr(target_env = "sgx", ignore)]
500+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
486501
fn shutdown_smoke() {
487502
each_ip(&mut |addr| {
488503
let a = t!(TcpListener::bind(&addr));
@@ -505,6 +520,7 @@ fn shutdown_smoke() {
505520
#[test]
506521
// FIXME: https://github.com/fortanix/rust-sgx/issues/110
507522
#[cfg_attr(target_env = "sgx", ignore)]
523+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
508524
fn close_readwrite_smoke() {
509525
each_ip(&mut |addr| {
510526
let a = t!(TcpListener::bind(&addr));
@@ -547,6 +563,7 @@ fn close_readwrite_smoke() {
547563
#[cfg_attr(target_env = "sgx", ignore)]
548564
// On windows, shutdown will not wake up blocking I/O operations.
549565
#[cfg_attr(windows, ignore)]
566+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
550567
fn close_read_wakes_up() {
551568
each_ip(&mut |addr| {
552569
let listener = t!(TcpListener::bind(&addr));
@@ -574,6 +591,7 @@ fn close_read_wakes_up() {
574591
}
575592

576593
#[test]
594+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
577595
fn clone_while_reading() {
578596
each_ip(&mut |addr| {
579597
let accept = t!(TcpListener::bind(&addr));
@@ -614,6 +632,7 @@ fn clone_while_reading() {
614632
}
615633

616634
#[test]
635+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
617636
fn clone_accept_smoke() {
618637
each_ip(&mut |addr| {
619638
let a = t!(TcpListener::bind(&addr));
@@ -632,6 +651,7 @@ fn clone_accept_smoke() {
632651
}
633652

634653
#[test]
654+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
635655
fn clone_accept_concurrent() {
636656
each_ip(&mut |addr| {
637657
let a = t!(TcpListener::bind(&addr));
@@ -670,10 +690,10 @@ fn debug() {
670690
addr.to_string()
671691
}
672692

693+
#[cfg(any(unix, target_os = "wasi"))]
694+
use crate::os::fd::AsRawFd;
673695
#[cfg(target_env = "sgx")]
674696
use crate::os::fortanix_sgx::io::AsRawFd;
675-
#[cfg(unix)]
676-
use crate::os::unix::io::AsRawFd;
677697
#[cfg(not(windows))]
678698
fn render_inner(addr: &dyn AsRawFd) -> impl fmt::Debug {
679699
addr.as_raw_fd()
@@ -714,6 +734,7 @@ fn debug() {
714734
ignore
715735
)]
716736
#[cfg_attr(target_env = "sgx", ignore)] // FIXME: https://github.com/fortanix/rust-sgx/issues/31
737+
#[cfg_attr(target_os = "wasi", ignore)] // timeout not supported
717738
#[test]
718739
fn timeouts() {
719740
let addr = next_test_ip4();
@@ -742,6 +763,7 @@ fn timeouts() {
742763

743764
#[test]
744765
#[cfg_attr(target_env = "sgx", ignore)] // FIXME: https://github.com/fortanix/rust-sgx/issues/31
766+
#[cfg_attr(target_os = "wasi", ignore)] // timeout not supported
745767
fn test_read_timeout() {
746768
let addr = next_test_ip4();
747769
let listener = t!(TcpListener::bind(&addr));
@@ -763,6 +785,7 @@ fn test_read_timeout() {
763785

764786
#[test]
765787
#[cfg_attr(target_env = "sgx", ignore)] // FIXME: https://github.com/fortanix/rust-sgx/issues/31
788+
#[cfg_attr(target_os = "wasi", ignore)] // timeout not supported
766789
fn test_read_with_timeout() {
767790
let addr = next_test_ip4();
768791
let listener = t!(TcpListener::bind(&addr));
@@ -810,6 +833,7 @@ fn test_timeout_zero_duration() {
810833

811834
#[test]
812835
#[cfg_attr(target_env = "sgx", ignore)]
836+
#[cfg_attr(target_os = "wasi", ignore)] // linger not supported
813837
fn linger() {
814838
let addr = next_test_ip4();
815839
let _listener = t!(TcpListener::bind(&addr));
@@ -879,6 +903,7 @@ fn set_nonblocking() {
879903

880904
#[test]
881905
#[cfg_attr(target_env = "sgx", ignore)] // FIXME: https://github.com/fortanix/rust-sgx/issues/31
906+
#[cfg_attr(target_os = "wasi", ignore)] // no threads
882907
fn peek() {
883908
each_ip(&mut |addr| {
884909
let (txdone, rxdone) = channel();

std/src/net/udp.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
#[cfg(all(test, not(any(target_os = "emscripten", target_env = "sgx", target_os = "xous"))))]
1+
#[cfg(all(
2+
test,
3+
not(any(
4+
target_os = "emscripten",
5+
all(target_os = "wasi", target_env = "p1"),
6+
target_env = "sgx",
7+
target_os = "xous"
8+
))
9+
))]
210
mod tests;
311

412
use crate::fmt;

0 commit comments

Comments
 (0)