Skip to content

Commit f3e84bf

Browse files
committed
wasi-common: use &self instead of &mut self
After #5236, it is now easier to modify the `wasi-common` implementations (both `preview0` and `preview1`) to use `&self` instead of `&mut self` in their function signatures. This makes it possible to access and use a `WasiCtx` from an `Arc<Host>` (the top-level Wasmtime CLI structure holding the WASI implementations). An `Arc<Host>` will only give out immutable access to the WASI implementations it contains. This relies on #5428 to configure Wiggle to emit `&self`-receiving traits. This change also wraps `wasi-common`'s source of randomness in `WasiCtx` with a `Mutex` to enable this `&mut self` to `&self` refactoring. One remaining issue to figure out with this change is the interaction with the `async` side of wiggle. E.g., `cargo build --all-features -p wasmtime-wasi` will fail due to the `tokyo` feature being enabled. The errors have to do with incorrect `Send` and `Sync` bounds on various parts of the Wiggle-generated API. We had previously discussed turning off these `async` parts when the `wasi-threads` feature is enabled, or at least prevent these features from being enabled at the same time, since as-is even `cargo test --all` is affected.
1 parent 4a92cc1 commit f3e84bf

File tree

4 files changed

+103
-111
lines changed

4 files changed

+103
-111
lines changed

crates/wasi-common/src/ctx.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ use crate::table::Table;
77
use crate::Error;
88
use cap_rand::RngCore;
99
use std::path::{Path, PathBuf};
10-
use std::sync::Arc;
10+
use std::sync::{Arc, Mutex};
1111

1212
pub struct WasiCtx {
1313
pub args: StringArray,
1414
pub env: StringArray,
15-
pub random: Box<dyn RngCore + Send + Sync>,
15+
pub random: Mutex<Box<dyn RngCore + Send + Sync>>,
1616
pub clocks: WasiClocks,
1717
pub sched: Box<dyn WasiSched>,
1818
pub table: Table,
@@ -28,7 +28,7 @@ impl WasiCtx {
2828
let s = WasiCtx {
2929
args: StringArray::new(),
3030
env: StringArray::new(),
31-
random,
31+
random: Mutex::new(random),
3232
clocks,
3333
sched,
3434
table,

crates/wasi-common/src/snapshots/preview_0.rs

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ wiggle::from_witx!({
1919
errors: { errno => trappable Error },
2020
async: *,
2121
wasmtime: false,
22+
mutable: false,
2223
});
2324

2425
use types::Error;
@@ -390,40 +391,40 @@ convert_flags_bidirectional!(
390391
#[wiggle::async_trait]
391392
impl wasi_unstable::WasiUnstable for WasiCtx {
392393
async fn args_get<'a>(
393-
&mut self,
394+
&self,
394395
argv: &GuestPtr<'a, GuestPtr<'a, u8>>,
395396
argv_buf: &GuestPtr<'a, u8>,
396397
) -> Result<(), Error> {
397398
Snapshot1::args_get(self, argv, argv_buf).await?;
398399
Ok(())
399400
}
400401

401-
async fn args_sizes_get(&mut self) -> Result<(types::Size, types::Size), Error> {
402+
async fn args_sizes_get(&self) -> Result<(types::Size, types::Size), Error> {
402403
let s = Snapshot1::args_sizes_get(self).await?;
403404
Ok(s)
404405
}
405406

406407
async fn environ_get<'a>(
407-
&mut self,
408+
&self,
408409
environ: &GuestPtr<'a, GuestPtr<'a, u8>>,
409410
environ_buf: &GuestPtr<'a, u8>,
410411
) -> Result<(), Error> {
411412
Snapshot1::environ_get(self, environ, environ_buf).await?;
412413
Ok(())
413414
}
414415

415-
async fn environ_sizes_get(&mut self) -> Result<(types::Size, types::Size), Error> {
416+
async fn environ_sizes_get(&self) -> Result<(types::Size, types::Size), Error> {
416417
let s = Snapshot1::environ_sizes_get(self).await?;
417418
Ok(s)
418419
}
419420

420-
async fn clock_res_get(&mut self, id: types::Clockid) -> Result<types::Timestamp, Error> {
421+
async fn clock_res_get(&self, id: types::Clockid) -> Result<types::Timestamp, Error> {
421422
let t = Snapshot1::clock_res_get(self, id.into()).await?;
422423
Ok(t)
423424
}
424425

425426
async fn clock_time_get(
426-
&mut self,
427+
&self,
427428
id: types::Clockid,
428429
precision: types::Timestamp,
429430
) -> Result<types::Timestamp, Error> {
@@ -432,7 +433,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
432433
}
433434

434435
async fn fd_advise(
435-
&mut self,
436+
&self,
436437
fd: types::Fd,
437438
offset: types::Filesize,
438439
len: types::Filesize,
@@ -443,7 +444,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
443444
}
444445

445446
async fn fd_allocate(
446-
&mut self,
447+
&self,
447448
fd: types::Fd,
448449
offset: types::Filesize,
449450
len: types::Filesize,
@@ -452,31 +453,27 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
452453
Ok(())
453454
}
454455

455-
async fn fd_close(&mut self, fd: types::Fd) -> Result<(), Error> {
456+
async fn fd_close(&self, fd: types::Fd) -> Result<(), Error> {
456457
Snapshot1::fd_close(self, fd.into()).await?;
457458
Ok(())
458459
}
459460

460-
async fn fd_datasync(&mut self, fd: types::Fd) -> Result<(), Error> {
461+
async fn fd_datasync(&self, fd: types::Fd) -> Result<(), Error> {
461462
Snapshot1::fd_datasync(self, fd.into()).await?;
462463
Ok(())
463464
}
464465

465-
async fn fd_fdstat_get(&mut self, fd: types::Fd) -> Result<types::Fdstat, Error> {
466+
async fn fd_fdstat_get(&self, fd: types::Fd) -> Result<types::Fdstat, Error> {
466467
Ok(Snapshot1::fd_fdstat_get(self, fd.into()).await?.into())
467468
}
468469

469-
async fn fd_fdstat_set_flags(
470-
&mut self,
471-
fd: types::Fd,
472-
flags: types::Fdflags,
473-
) -> Result<(), Error> {
470+
async fn fd_fdstat_set_flags(&self, fd: types::Fd, flags: types::Fdflags) -> Result<(), Error> {
474471
Snapshot1::fd_fdstat_set_flags(self, fd.into(), flags.into()).await?;
475472
Ok(())
476473
}
477474

478475
async fn fd_fdstat_set_rights(
479-
&mut self,
476+
&self,
480477
fd: types::Fd,
481478
fs_rights_base: types::Rights,
482479
fs_rights_inheriting: types::Rights,
@@ -491,12 +488,12 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
491488
Ok(())
492489
}
493490

494-
async fn fd_filestat_get(&mut self, fd: types::Fd) -> Result<types::Filestat, Error> {
491+
async fn fd_filestat_get(&self, fd: types::Fd) -> Result<types::Filestat, Error> {
495492
Ok(Snapshot1::fd_filestat_get(self, fd.into()).await?.into())
496493
}
497494

498495
async fn fd_filestat_set_size(
499-
&mut self,
496+
&self,
500497
fd: types::Fd,
501498
size: types::Filesize,
502499
) -> Result<(), Error> {
@@ -505,7 +502,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
505502
}
506503

507504
async fn fd_filestat_set_times(
508-
&mut self,
505+
&self,
509506
fd: types::Fd,
510507
atim: types::Timestamp,
511508
mtim: types::Timestamp,
@@ -524,7 +521,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
524521
// representation to a std::io::IoSlice(Mut) representation.
525522

526523
async fn fd_read<'a>(
527-
&mut self,
524+
&self,
528525
fd: types::Fd,
529526
iovs: &types::IovecArray<'a>,
530527
) -> Result<types::Size, Error> {
@@ -594,7 +591,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
594591
}
595592

596593
async fn fd_pread<'a>(
597-
&mut self,
594+
&self,
598595
fd: types::Fd,
599596
iovs: &types::IovecArray<'a>,
600597
offset: types::Filesize,
@@ -667,7 +664,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
667664
}
668665

669666
async fn fd_write<'a>(
670-
&mut self,
667+
&self,
671668
fd: types::Fd,
672669
ciovs: &types::CiovecArray<'a>,
673670
) -> Result<types::Size, Error> {
@@ -693,7 +690,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
693690
}
694691

695692
async fn fd_pwrite<'a>(
696-
&mut self,
693+
&self,
697694
fd: types::Fd,
698695
ciovs: &types::CiovecArray<'a>,
699696
offset: types::Filesize,
@@ -719,12 +716,12 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
719716
Ok(types::Size::try_from(bytes_written)?)
720717
}
721718

722-
async fn fd_prestat_get(&mut self, fd: types::Fd) -> Result<types::Prestat, Error> {
719+
async fn fd_prestat_get(&self, fd: types::Fd) -> Result<types::Prestat, Error> {
723720
Ok(Snapshot1::fd_prestat_get(self, fd.into()).await?.into())
724721
}
725722

726723
async fn fd_prestat_dir_name<'a>(
727-
&mut self,
724+
&self,
728725
fd: types::Fd,
729726
path: &GuestPtr<'a, u8>,
730727
path_max_len: types::Size,
@@ -733,31 +730,31 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
733730
Ok(())
734731
}
735732

736-
async fn fd_renumber(&mut self, from: types::Fd, to: types::Fd) -> Result<(), Error> {
733+
async fn fd_renumber(&self, from: types::Fd, to: types::Fd) -> Result<(), Error> {
737734
Snapshot1::fd_renumber(self, from.into(), to.into()).await?;
738735
Ok(())
739736
}
740737

741738
async fn fd_seek(
742-
&mut self,
739+
&self,
743740
fd: types::Fd,
744741
offset: types::Filedelta,
745742
whence: types::Whence,
746743
) -> Result<types::Filesize, Error> {
747744
Ok(Snapshot1::fd_seek(self, fd.into(), offset, whence.into()).await?)
748745
}
749746

750-
async fn fd_sync(&mut self, fd: types::Fd) -> Result<(), Error> {
747+
async fn fd_sync(&self, fd: types::Fd) -> Result<(), Error> {
751748
Snapshot1::fd_sync(self, fd.into()).await?;
752749
Ok(())
753750
}
754751

755-
async fn fd_tell(&mut self, fd: types::Fd) -> Result<types::Filesize, Error> {
752+
async fn fd_tell(&self, fd: types::Fd) -> Result<types::Filesize, Error> {
756753
Ok(Snapshot1::fd_tell(self, fd.into()).await?)
757754
}
758755

759756
async fn fd_readdir<'a>(
760-
&mut self,
757+
&self,
761758
fd: types::Fd,
762759
buf: &GuestPtr<'a, u8>,
763760
buf_len: types::Size,
@@ -767,7 +764,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
767764
}
768765

769766
async fn path_create_directory<'a>(
770-
&mut self,
767+
&self,
771768
dirfd: types::Fd,
772769
path: &GuestPtr<'a, str>,
773770
) -> Result<(), Error> {
@@ -776,7 +773,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
776773
}
777774

778775
async fn path_filestat_get<'a>(
779-
&mut self,
776+
&self,
780777
dirfd: types::Fd,
781778
flags: types::Lookupflags,
782779
path: &GuestPtr<'a, str>,
@@ -789,7 +786,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
789786
}
790787

791788
async fn path_filestat_set_times<'a>(
792-
&mut self,
789+
&self,
793790
dirfd: types::Fd,
794791
flags: types::Lookupflags,
795792
path: &GuestPtr<'a, str>,
@@ -811,7 +808,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
811808
}
812809

813810
async fn path_link<'a>(
814-
&mut self,
811+
&self,
815812
src_fd: types::Fd,
816813
src_flags: types::Lookupflags,
817814
src_path: &GuestPtr<'a, str>,
@@ -831,7 +828,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
831828
}
832829

833830
async fn path_open<'a>(
834-
&mut self,
831+
&self,
835832
dirfd: types::Fd,
836833
dirflags: types::Lookupflags,
837834
path: &GuestPtr<'a, str>,
@@ -855,7 +852,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
855852
}
856853

857854
async fn path_readlink<'a>(
858-
&mut self,
855+
&self,
859856
dirfd: types::Fd,
860857
path: &GuestPtr<'a, str>,
861858
buf: &GuestPtr<'a, u8>,
@@ -865,7 +862,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
865862
}
866863

867864
async fn path_remove_directory<'a>(
868-
&mut self,
865+
&self,
869866
dirfd: types::Fd,
870867
path: &GuestPtr<'a, str>,
871868
) -> Result<(), Error> {
@@ -874,7 +871,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
874871
}
875872

876873
async fn path_rename<'a>(
877-
&mut self,
874+
&self,
878875
src_fd: types::Fd,
879876
src_path: &GuestPtr<'a, str>,
880877
dest_fd: types::Fd,
@@ -885,7 +882,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
885882
}
886883

887884
async fn path_symlink<'a>(
888-
&mut self,
885+
&self,
889886
src_path: &GuestPtr<'a, str>,
890887
dirfd: types::Fd,
891888
dest_path: &GuestPtr<'a, str>,
@@ -895,7 +892,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
895892
}
896893

897894
async fn path_unlink_file<'a>(
898-
&mut self,
895+
&self,
899896
dirfd: types::Fd,
900897
path: &GuestPtr<'a, str>,
901898
) -> Result<(), Error> {
@@ -911,7 +908,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
911908
// The bodies of these functions is mostly about converting the GuestPtr and types::-based
912909
// representation to use the Poll abstraction.
913910
async fn poll_oneoff<'a>(
914-
&mut self,
911+
&self,
915912
subs: &GuestPtr<'a, types::Subscription>,
916913
events: &GuestPtr<'a, types::Event>,
917914
nsubscriptions: types::Size,
@@ -1090,21 +1087,21 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
10901087
Ok(num_results.try_into().expect("results fit into memory"))
10911088
}
10921089

1093-
async fn proc_exit(&mut self, status: types::Exitcode) -> anyhow::Error {
1090+
async fn proc_exit(&self, status: types::Exitcode) -> anyhow::Error {
10941091
Snapshot1::proc_exit(self, status).await
10951092
}
10961093

1097-
async fn proc_raise(&mut self, _sig: types::Signal) -> Result<(), Error> {
1094+
async fn proc_raise(&self, _sig: types::Signal) -> Result<(), Error> {
10981095
Err(Error::trap(anyhow::Error::msg("proc_raise unsupported")))
10991096
}
11001097

1101-
async fn sched_yield(&mut self) -> Result<(), Error> {
1098+
async fn sched_yield(&self) -> Result<(), Error> {
11021099
Snapshot1::sched_yield(self).await?;
11031100
Ok(())
11041101
}
11051102

11061103
async fn random_get<'a>(
1107-
&mut self,
1104+
&self,
11081105
buf: &GuestPtr<'a, u8>,
11091106
buf_len: types::Size,
11101107
) -> Result<(), Error> {
@@ -1113,7 +1110,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
11131110
}
11141111

11151112
async fn sock_recv<'a>(
1116-
&mut self,
1113+
&self,
11171114
_fd: types::Fd,
11181115
_ri_data: &types::IovecArray<'a>,
11191116
_ri_flags: types::Riflags,
@@ -1122,15 +1119,15 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
11221119
}
11231120

11241121
async fn sock_send<'a>(
1125-
&mut self,
1122+
&self,
11261123
_fd: types::Fd,
11271124
_si_data: &types::CiovecArray<'a>,
11281125
_si_flags: types::Siflags,
11291126
) -> Result<types::Size, Error> {
11301127
Err(Error::trap(anyhow::Error::msg("sock_send unsupported")))
11311128
}
11321129

1133-
async fn sock_shutdown(&mut self, _fd: types::Fd, _how: types::Sdflags) -> Result<(), Error> {
1130+
async fn sock_shutdown(&self, _fd: types::Fd, _how: types::Sdflags) -> Result<(), Error> {
11341131
Err(Error::trap(anyhow::Error::msg("sock_shutdown unsupported")))
11351132
}
11361133
}

0 commit comments

Comments
 (0)