Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the remaining socket-related WASI functions. #4776

Merged
merged 4 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions crates/wasi-common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,7 @@ Please note that the library requires Rust compiler version at least 1.37.0.

### *nix
In our *nix implementation, we currently support the entire [WASI API]
with the exception of socket hostcalls:
- `sock_recv`
- `sock_send`
- `sock_shutdown`

We expect these to be implemented when network access is standardised.

We also currently do not support the `proc_raise` hostcall, as it is expected to
with the exception of the `proc_raise` hostcall, as it is expected to
be dropped entirely from WASI.

[WASI API]: https://github.com/WebAssembly/WASI/blob/master/phases/snapshot/docs.md
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi-common/cap-std-sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ cap-fs-ext = "0.25.0"
cap-time-ext = "0.25.0"
cap-rand = "0.25.0"
fs-set-times = "0.17.0"
system-interface = { version = "0.21.0", features = ["cap_std_impls"] }
system-interface = { version = "0.22.0", features = ["cap_std_impls"] }
tracing = "0.1.19"
io-lifetimes = { version = "0.7.0", default-features = false }
is-terminal = "0.3.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi-common/cap-std-sync/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::convert::TryInto;
use std::io;
use system_interface::{
fs::{FileIoExt, GetSetFdFlags},
io::ReadReady,
io::{IoExt, ReadReady},
};
use wasi_common::{
file::{Advice, FdFlags, FileType, Filestat, WasiFile},
Expand Down
60 changes: 57 additions & 3 deletions crates/wasi-common/cap-std-sync/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ use std::any::Any;
use std::convert::TryInto;
use std::io;
#[cfg(unix)]
use system_interface::fs::FileIoExt;
#[cfg(unix)]
use system_interface::fs::GetSetFdFlags;
use system_interface::io::IoExt;
use system_interface::io::IsReadWrite;
use system_interface::io::ReadReady;
use wasi_common::{
file::{FdFlags, FileType, WasiFile},
file::{FdFlags, FileType, RiFlags, RoFlags, SdFlags, SiFlags, WasiFile},
Error, ErrorExt,
};

Expand Down Expand Up @@ -245,6 +244,61 @@ macro_rules! wasi_stream_write_impl {
Err(Error::io())
}
}

async fn sock_recv<'a>(
&mut self,
ri_data: &mut [std::io::IoSliceMut<'a>],
ri_flags: RiFlags,
) -> Result<(u64, RoFlags), Error> {
if (ri_flags & !(RiFlags::RECV_PEEK | RiFlags::RECV_WAITALL)) != RiFlags::empty() {
return Err(Error::not_supported());
}

if ri_flags.contains(RiFlags::RECV_PEEK) {
if let Some(first) = ri_data.iter_mut().next() {
let n = self.0.peek(first)?;
return Ok((n as u64, RoFlags::empty()));
} else {
return Ok((0, RoFlags::empty()));
}
}

if ri_flags.contains(RiFlags::RECV_WAITALL) {
let n: usize = ri_data.iter().map(|buf| buf.len()).sum();
self.0.read_exact_vectored(ri_data)?;
return Ok((n as u64, RoFlags::empty()));
}

let n = self.0.read_vectored(ri_data)?;
Ok((n as u64, RoFlags::empty()))
}

async fn sock_send<'a>(
&mut self,
si_data: &[std::io::IoSlice<'a>],
si_flags: SiFlags,
) -> Result<u64, Error> {
if si_flags != SiFlags::empty() {
return Err(Error::not_supported());
}

let n = self.0.write_vectored(si_data)?;
Ok(n as u64)
}

async fn sock_shutdown(&mut self, how: SdFlags) -> Result<(), Error> {
let how = if how == SdFlags::RD | SdFlags::WR {
cap_std::net::Shutdown::Both
} else if how == SdFlags::RD {
cap_std::net::Shutdown::Read
} else if how == SdFlags::WR {
cap_std::net::Shutdown::Write
} else {
return Err(Error::invalid_argument());
};
self.0.shutdown(how)?;
Ok(())
}
}
#[cfg(unix)]
impl AsFd for $ty {
Expand Down
5 changes: 5 additions & 0 deletions supply-chain/audits.toml
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,8 @@ criteria = "safe-to-deploy"
version = "1.0.48"
notes = "The Bytecode Alliance is the author of this crate."

[[audits.system-interface]]
who = "Dan Gohman <dev@sunfishcode.online>"
criteria = "safe-to-deploy"
version = "0.22.0"
notes = "The Bytecode Alliance is the author of this crate."
4 changes: 0 additions & 4 deletions supply-chain/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1011,10 +1011,6 @@ criteria = "safe-to-deploy"
version = "0.12.6"
criteria = "safe-to-deploy"

[[exemptions.system-interface]]
version = "0.21.0"
criteria = "safe-to-deploy"

[[exemptions.target-lexicon]]
version = "0.12.3"
criteria = "safe-to-deploy"
Expand Down