wasi-common: snapshots 0 and 1 traits are all async now

This commit is contained in:
Pat Hickey
2021-04-14 15:05:38 -07:00
parent 228096c840
commit 025a1ecff4
2 changed files with 211 additions and 171 deletions

View File

@@ -22,7 +22,7 @@ use wiggle::GuestPtr;
wiggle::from_witx!({
witx: ["$WASI_ROOT/phases/snapshot/witx/wasi_snapshot_preview1.witx"],
errors: { errno => Error },
async: { wasi_snapshot_preview1::{poll_oneoff, sched_yield} }
async: *
});
impl wiggle::GuestErrorType for types::Errno {
@@ -191,7 +191,7 @@ impl TryFrom<std::io::Error> for types::Errno {
#[wiggle::async_trait]
impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
fn args_get<'b>(
async fn args_get<'b>(
&self,
argv: &GuestPtr<'b, GuestPtr<'b, u8>>,
argv_buf: &GuestPtr<'b, u8>,
@@ -199,11 +199,11 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
self.args.write_to_guest(argv_buf, argv)
}
fn args_sizes_get(&self) -> Result<(types::Size, types::Size), Error> {
async fn args_sizes_get(&self) -> Result<(types::Size, types::Size), Error> {
Ok((self.args.number_elements(), self.args.cumulative_size()))
}
fn environ_get<'b>(
async fn environ_get<'b>(
&self,
environ: &GuestPtr<'b, GuestPtr<'b, u8>>,
environ_buf: &GuestPtr<'b, u8>,
@@ -211,11 +211,11 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
self.env.write_to_guest(environ_buf, environ)
}
fn environ_sizes_get(&self) -> Result<(types::Size, types::Size), Error> {
async fn environ_sizes_get(&self) -> Result<(types::Size, types::Size), Error> {
Ok((self.env.number_elements(), self.env.cumulative_size()))
}
fn clock_res_get(&self, id: types::Clockid) -> Result<types::Timestamp, Error> {
async fn clock_res_get(&self, id: types::Clockid) -> Result<types::Timestamp, Error> {
let resolution = match id {
types::Clockid::Realtime => Ok(self.clocks.system.resolution()),
types::Clockid::Monotonic => Ok(self.clocks.monotonic.resolution()),
@@ -226,7 +226,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(resolution.as_nanos().try_into()?)
}
fn clock_time_get(
async fn clock_time_get(
&self,
id: types::Clockid,
precision: types::Timestamp,
@@ -251,7 +251,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
}
}
fn fd_advise(
async fn fd_advise(
&self,
fd: types::Fd,
offset: types::Filesize,
@@ -265,7 +265,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(())
}
fn fd_allocate(
async fn fd_allocate(
&self,
fd: types::Fd,
offset: types::Filesize,
@@ -278,7 +278,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(())
}
fn fd_close(&self, fd: types::Fd) -> Result<(), Error> {
async fn fd_close(&self, fd: types::Fd) -> Result<(), Error> {
let mut table = self.table();
let fd = u32::from(fd);
@@ -304,7 +304,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(())
}
fn fd_datasync(&self, fd: types::Fd) -> Result<(), Error> {
async fn fd_datasync(&self, fd: types::Fd) -> Result<(), Error> {
self.table()
.get_file(u32::from(fd))?
.get_cap(FileCaps::DATASYNC)?
@@ -312,7 +312,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(())
}
fn fd_fdstat_get(&self, fd: types::Fd) -> Result<types::Fdstat, Error> {
async fn fd_fdstat_get(&self, fd: types::Fd) -> Result<types::Fdstat, Error> {
let table = self.table();
let fd = u32::from(fd);
if table.is::<FileEntry>(fd) {
@@ -328,14 +328,14 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
}
}
fn fd_fdstat_set_flags(&self, fd: types::Fd, flags: types::Fdflags) -> Result<(), Error> {
async fn fd_fdstat_set_flags(&self, fd: types::Fd, flags: types::Fdflags) -> Result<(), Error> {
self.table()
.get_file_mut(u32::from(fd))?
.get_cap(FileCaps::FDSTAT_SET_FLAGS)?
.set_fdflags(FdFlags::from(flags))
}
fn fd_fdstat_set_rights(
async fn fd_fdstat_set_rights(
&self,
fd: types::Fd,
fs_rights_base: types::Rights,
@@ -357,7 +357,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
}
}
fn fd_filestat_get(&self, fd: types::Fd) -> Result<types::Filestat, Error> {
async fn fd_filestat_get(&self, fd: types::Fd) -> Result<types::Filestat, Error> {
let table = self.table();
let fd = u32::from(fd);
if table.is::<FileEntry>(fd) {
@@ -377,7 +377,11 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
}
}
fn fd_filestat_set_size(&self, fd: types::Fd, size: types::Filesize) -> Result<(), Error> {
async fn fd_filestat_set_size(
&self,
fd: types::Fd,
size: types::Filesize,
) -> Result<(), Error> {
self.table()
.get_file(u32::from(fd))?
.get_cap(FileCaps::FILESTAT_SET_SIZE)?
@@ -385,7 +389,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(())
}
fn fd_filestat_set_times(
async fn fd_filestat_set_times(
&self,
fd: types::Fd,
atim: types::Timestamp,
@@ -420,7 +424,11 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
}
}
fn fd_read(&self, fd: types::Fd, iovs: &types::IovecArray<'_>) -> Result<types::Size, Error> {
async fn fd_read<'a>(
&self,
fd: types::Fd,
iovs: &types::IovecArray<'a>,
) -> Result<types::Size, Error> {
let table = self.table();
let f = table.get_file(u32::from(fd))?.get_cap(FileCaps::READ)?;
@@ -442,10 +450,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(types::Size::try_from(bytes_read)?)
}
fn fd_pread(
async fn fd_pread<'a>(
&self,
fd: types::Fd,
iovs: &types::IovecArray<'_>,
iovs: &types::IovecArray<'a>,
offset: types::Filesize,
) -> Result<types::Size, Error> {
let table = self.table();
@@ -471,10 +479,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(types::Size::try_from(bytes_read)?)
}
fn fd_write(
async fn fd_write<'a>(
&self,
fd: types::Fd,
ciovs: &types::CiovecArray<'_>,
ciovs: &types::CiovecArray<'a>,
) -> Result<types::Size, Error> {
let table = self.table();
let f = table.get_file(u32::from(fd))?.get_cap(FileCaps::WRITE)?;
@@ -497,10 +505,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(types::Size::try_from(bytes_written)?)
}
fn fd_pwrite(
async fn fd_pwrite<'a>(
&self,
fd: types::Fd,
ciovs: &types::CiovecArray<'_>,
ciovs: &types::CiovecArray<'a>,
offset: types::Filesize,
) -> Result<types::Size, Error> {
let table = self.table();
@@ -526,7 +534,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(types::Size::try_from(bytes_written)?)
}
fn fd_prestat_get(&self, fd: types::Fd) -> Result<types::Prestat, Error> {
async fn fd_prestat_get(&self, fd: types::Fd) -> Result<types::Prestat, Error> {
let table = self.table();
let dir_entry: Ref<DirEntry> = table.get(u32::from(fd)).map_err(|_| Error::badf())?;
if let Some(ref preopen) = dir_entry.preopen_path() {
@@ -538,10 +546,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
}
}
fn fd_prestat_dir_name(
async fn fd_prestat_dir_name<'a>(
&self,
fd: types::Fd,
path: &GuestPtr<u8>,
path: &GuestPtr<'a, u8>,
path_max_len: types::Size,
) -> Result<(), Error> {
let table = self.table();
@@ -562,7 +570,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Err(Error::not_supported())
}
}
fn fd_renumber(&self, from: types::Fd, to: types::Fd) -> Result<(), Error> {
async fn fd_renumber(&self, from: types::Fd, to: types::Fd) -> Result<(), Error> {
let mut table = self.table();
let from = u32::from(from);
let to = u32::from(to);
@@ -579,7 +587,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(())
}
fn fd_seek(
async fn fd_seek(
&self,
fd: types::Fd,
offset: types::Filedelta,
@@ -606,7 +614,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(newoffset)
}
fn fd_sync(&self, fd: types::Fd) -> Result<(), Error> {
async fn fd_sync(&self, fd: types::Fd) -> Result<(), Error> {
self.table()
.get_file(u32::from(fd))?
.get_cap(FileCaps::SYNC)?
@@ -614,7 +622,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(())
}
fn fd_tell(&self, fd: types::Fd) -> Result<types::Filesize, Error> {
async fn fd_tell(&self, fd: types::Fd) -> Result<types::Filesize, Error> {
// XXX should this be stream_position?
let offset = self
.table()
@@ -624,10 +632,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(offset)
}
fn fd_readdir(
async fn fd_readdir<'a>(
&self,
fd: types::Fd,
buf: &GuestPtr<u8>,
buf: &GuestPtr<'a, u8>,
buf_len: types::Size,
cookie: types::Dircookie,
) -> Result<types::Size, Error> {
@@ -677,10 +685,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(bufused)
}
fn path_create_directory(
async fn path_create_directory<'a>(
&self,
dirfd: types::Fd,
path: &GuestPtr<'_, str>,
path: &GuestPtr<'a, str>,
) -> Result<(), Error> {
self.table()
.get_dir(u32::from(dirfd))?
@@ -688,11 +696,11 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
.create_dir(path.as_str()?.deref())
}
fn path_filestat_get(
async fn path_filestat_get<'a>(
&self,
dirfd: types::Fd,
flags: types::Lookupflags,
path: &GuestPtr<'_, str>,
path: &GuestPtr<'a, str>,
) -> Result<types::Filestat, Error> {
let filestat = self
.table()
@@ -705,11 +713,11 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(types::Filestat::from(filestat))
}
fn path_filestat_set_times(
async fn path_filestat_set_times<'a>(
&self,
dirfd: types::Fd,
flags: types::Lookupflags,
path: &GuestPtr<'_, str>,
path: &GuestPtr<'a, str>,
atim: types::Timestamp,
mtim: types::Timestamp,
fst_flags: types::Fstflags,
@@ -732,13 +740,13 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
)
}
fn path_link(
async fn path_link<'a>(
&self,
src_fd: types::Fd,
src_flags: types::Lookupflags,
src_path: &GuestPtr<'_, str>,
src_path: &GuestPtr<'a, str>,
target_fd: types::Fd,
target_path: &GuestPtr<'_, str>,
target_path: &GuestPtr<'a, str>,
) -> Result<(), Error> {
let table = self.table();
let src_dir = table
@@ -760,11 +768,11 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
)
}
fn path_open(
async fn path_open<'a>(
&self,
dirfd: types::Fd,
dirflags: types::Lookupflags,
path: &GuestPtr<'_, str>,
path: &GuestPtr<'a, str>,
oflags: types::Oflags,
fs_rights_base: types::Rights,
fs_rights_inheriting: types::Rights,
@@ -817,11 +825,11 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
}
}
fn path_readlink(
async fn path_readlink<'a>(
&self,
dirfd: types::Fd,
path: &GuestPtr<'_, str>,
buf: &GuestPtr<u8>,
path: &GuestPtr<'a, str>,
buf: &GuestPtr<'a, u8>,
buf_len: types::Size,
) -> Result<types::Size, Error> {
let link = self
@@ -842,10 +850,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(link_len as types::Size)
}
fn path_remove_directory(
async fn path_remove_directory<'a>(
&self,
dirfd: types::Fd,
path: &GuestPtr<'_, str>,
path: &GuestPtr<'a, str>,
) -> Result<(), Error> {
self.table()
.get_dir(u32::from(dirfd))?
@@ -853,12 +861,12 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
.remove_dir(path.as_str()?.deref())
}
fn path_rename(
async fn path_rename<'a>(
&self,
src_fd: types::Fd,
src_path: &GuestPtr<'_, str>,
src_path: &GuestPtr<'a, str>,
dest_fd: types::Fd,
dest_path: &GuestPtr<'_, str>,
dest_path: &GuestPtr<'a, str>,
) -> Result<(), Error> {
let table = self.table();
let src_dir = table
@@ -874,11 +882,11 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
)
}
fn path_symlink(
async fn path_symlink<'a>(
&self,
src_path: &GuestPtr<'_, str>,
src_path: &GuestPtr<'a, str>,
dirfd: types::Fd,
dest_path: &GuestPtr<'_, str>,
dest_path: &GuestPtr<'a, str>,
) -> Result<(), Error> {
self.table()
.get_dir(u32::from(dirfd))?
@@ -886,17 +894,21 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
.symlink(src_path.as_str()?.deref(), dest_path.as_str()?.deref())
}
fn path_unlink_file(&self, dirfd: types::Fd, path: &GuestPtr<'_, str>) -> Result<(), Error> {
async fn path_unlink_file<'a>(
&self,
dirfd: types::Fd,
path: &GuestPtr<'a, str>,
) -> Result<(), Error> {
self.table()
.get_dir(u32::from(dirfd))?
.get_cap(DirCaps::UNLINK_FILE)?
.unlink_file(path.as_str()?.deref())
}
async fn poll_oneoff<'b>(
async fn poll_oneoff<'a>(
&self,
subs: &GuestPtr<'b, types::Subscription>,
events: &GuestPtr<'b, types::Event>,
subs: &GuestPtr<'a, types::Subscription>,
events: &GuestPtr<'a, types::Event>,
nsubscriptions: types::Size,
) -> Result<types::Size, Error> {
if nsubscriptions == 0 {
@@ -1057,7 +1069,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
Ok(num_results.try_into().expect("results fit into memory"))
}
fn proc_exit(&self, status: types::Exitcode) -> wiggle::Trap {
async fn proc_exit(&self, status: types::Exitcode) -> wiggle::Trap {
// Check that the status is within WASI's range.
if status < 126 {
wiggle::Trap::I32Exit(status as i32)
@@ -1066,7 +1078,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
}
}
fn proc_raise(&self, _sig: types::Signal) -> Result<(), Error> {
async fn proc_raise(&self, _sig: types::Signal) -> Result<(), Error> {
Err(Error::trap("proc_raise unsupported"))
}
@@ -1074,31 +1086,35 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
self.sched.sched_yield().await
}
fn random_get(&self, buf: &GuestPtr<u8>, buf_len: types::Size) -> Result<(), Error> {
async fn random_get<'a>(
&self,
buf: &GuestPtr<'a, u8>,
buf_len: types::Size,
) -> Result<(), Error> {
let mut buf = buf.as_array(buf_len).as_slice_mut()?;
self.random.borrow_mut().try_fill_bytes(buf.deref_mut())?;
Ok(())
}
fn sock_recv(
async fn sock_recv<'a>(
&self,
_fd: types::Fd,
_ri_data: &types::IovecArray<'_>,
_ri_data: &types::IovecArray<'a>,
_ri_flags: types::Riflags,
) -> Result<(types::Size, types::Roflags), Error> {
Err(Error::trap("sock_recv unsupported"))
}
fn sock_send(
async fn sock_send<'a>(
&self,
_fd: types::Fd,
_si_data: &types::CiovecArray<'_>,
_si_data: &types::CiovecArray<'a>,
_si_flags: types::Siflags,
) -> Result<types::Size, Error> {
Err(Error::trap("sock_send unsupported"))
}
fn sock_shutdown(&self, _fd: types::Fd, _how: types::Sdflags) -> Result<(), Error> {
async fn sock_shutdown(&self, _fd: types::Fd, _how: types::Sdflags) -> Result<(), Error> {
Err(Error::trap("sock_shutdown unsupported"))
}
}