Skip to content

Commit 31c178e

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 fed8951 commit 31c178e

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> {
@@ -598,7 +595,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
598595
}
599596

600597
async fn fd_pread<'a>(
601-
&mut self,
598+
&self,
602599
fd: types::Fd,
603600
iovs: &types::IovecArray<'a>,
604601
offset: types::Filesize,
@@ -675,7 +672,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
675672
}
676673

677674
async fn fd_write<'a>(
678-
&mut self,
675+
&self,
679676
fd: types::Fd,
680677
ciovs: &types::CiovecArray<'a>,
681678
) -> Result<types::Size, Error> {
@@ -701,7 +698,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
701698
}
702699

703700
async fn fd_pwrite<'a>(
704-
&mut self,
701+
&self,
705702
fd: types::Fd,
706703
ciovs: &types::CiovecArray<'a>,
707704
offset: types::Filesize,
@@ -727,12 +724,12 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
727724
Ok(types::Size::try_from(bytes_written)?)
728725
}
729726

730-
async fn fd_prestat_get(&mut self, fd: types::Fd) -> Result<types::Prestat, Error> {
727+
async fn fd_prestat_get(&self, fd: types::Fd) -> Result<types::Prestat, Error> {
731728
Ok(Snapshot1::fd_prestat_get(self, fd.into()).await?.into())
732729
}
733730

734731
async fn fd_prestat_dir_name<'a>(
735-
&mut self,
732+
&self,
736733
fd: types::Fd,
737734
path: &GuestPtr<'a, u8>,
738735
path_max_len: types::Size,
@@ -741,31 +738,31 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
741738
Ok(())
742739
}
743740

744-
async fn fd_renumber(&mut self, from: types::Fd, to: types::Fd) -> Result<(), Error> {
741+
async fn fd_renumber(&self, from: types::Fd, to: types::Fd) -> Result<(), Error> {
745742
Snapshot1::fd_renumber(self, from.into(), to.into()).await?;
746743
Ok(())
747744
}
748745

749746
async fn fd_seek(
750-
&mut self,
747+
&self,
751748
fd: types::Fd,
752749
offset: types::Filedelta,
753750
whence: types::Whence,
754751
) -> Result<types::Filesize, Error> {
755752
Ok(Snapshot1::fd_seek(self, fd.into(), offset, whence.into()).await?)
756753
}
757754

758-
async fn fd_sync(&mut self, fd: types::Fd) -> Result<(), Error> {
755+
async fn fd_sync(&self, fd: types::Fd) -> Result<(), Error> {
759756
Snapshot1::fd_sync(self, fd.into()).await?;
760757
Ok(())
761758
}
762759

763-
async fn fd_tell(&mut self, fd: types::Fd) -> Result<types::Filesize, Error> {
760+
async fn fd_tell(&self, fd: types::Fd) -> Result<types::Filesize, Error> {
764761
Ok(Snapshot1::fd_tell(self, fd.into()).await?)
765762
}
766763

767764
async fn fd_readdir<'a>(
768-
&mut self,
765+
&self,
769766
fd: types::Fd,
770767
buf: &GuestPtr<'a, u8>,
771768
buf_len: types::Size,
@@ -775,7 +772,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
775772
}
776773

777774
async fn path_create_directory<'a>(
778-
&mut self,
775+
&self,
779776
dirfd: types::Fd,
780777
path: &GuestPtr<'a, str>,
781778
) -> Result<(), Error> {
@@ -784,7 +781,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
784781
}
785782

786783
async fn path_filestat_get<'a>(
787-
&mut self,
784+
&self,
788785
dirfd: types::Fd,
789786
flags: types::Lookupflags,
790787
path: &GuestPtr<'a, str>,
@@ -797,7 +794,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
797794
}
798795

799796
async fn path_filestat_set_times<'a>(
800-
&mut self,
797+
&self,
801798
dirfd: types::Fd,
802799
flags: types::Lookupflags,
803800
path: &GuestPtr<'a, str>,
@@ -819,7 +816,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
819816
}
820817

821818
async fn path_link<'a>(
822-
&mut self,
819+
&self,
823820
src_fd: types::Fd,
824821
src_flags: types::Lookupflags,
825822
src_path: &GuestPtr<'a, str>,
@@ -839,7 +836,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
839836
}
840837

841838
async fn path_open<'a>(
842-
&mut self,
839+
&self,
843840
dirfd: types::Fd,
844841
dirflags: types::Lookupflags,
845842
path: &GuestPtr<'a, str>,
@@ -863,7 +860,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
863860
}
864861

865862
async fn path_readlink<'a>(
866-
&mut self,
863+
&self,
867864
dirfd: types::Fd,
868865
path: &GuestPtr<'a, str>,
869866
buf: &GuestPtr<'a, u8>,
@@ -873,7 +870,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
873870
}
874871

875872
async fn path_remove_directory<'a>(
876-
&mut self,
873+
&self,
877874
dirfd: types::Fd,
878875
path: &GuestPtr<'a, str>,
879876
) -> Result<(), Error> {
@@ -882,7 +879,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
882879
}
883880

884881
async fn path_rename<'a>(
885-
&mut self,
882+
&self,
886883
src_fd: types::Fd,
887884
src_path: &GuestPtr<'a, str>,
888885
dest_fd: types::Fd,
@@ -893,7 +890,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
893890
}
894891

895892
async fn path_symlink<'a>(
896-
&mut self,
893+
&self,
897894
src_path: &GuestPtr<'a, str>,
898895
dirfd: types::Fd,
899896
dest_path: &GuestPtr<'a, str>,
@@ -903,7 +900,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
903900
}
904901

905902
async fn path_unlink_file<'a>(
906-
&mut self,
903+
&self,
907904
dirfd: types::Fd,
908905
path: &GuestPtr<'a, str>,
909906
) -> Result<(), Error> {
@@ -919,7 +916,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
919916
// The bodies of these functions is mostly about converting the GuestPtr and types::-based
920917
// representation to use the Poll abstraction.
921918
async fn poll_oneoff<'a>(
922-
&mut self,
919+
&self,
923920
subs: &GuestPtr<'a, types::Subscription>,
924921
events: &GuestPtr<'a, types::Event>,
925922
nsubscriptions: types::Size,
@@ -1098,21 +1095,21 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
10981095
Ok(num_results.try_into().expect("results fit into memory"))
10991096
}
11001097

1101-
async fn proc_exit(&mut self, status: types::Exitcode) -> anyhow::Error {
1098+
async fn proc_exit(&self, status: types::Exitcode) -> anyhow::Error {
11021099
Snapshot1::proc_exit(self, status).await
11031100
}
11041101

1105-
async fn proc_raise(&mut self, _sig: types::Signal) -> Result<(), Error> {
1102+
async fn proc_raise(&self, _sig: types::Signal) -> Result<(), Error> {
11061103
Err(Error::trap(anyhow::Error::msg("proc_raise unsupported")))
11071104
}
11081105

1109-
async fn sched_yield(&mut self) -> Result<(), Error> {
1106+
async fn sched_yield(&self) -> Result<(), Error> {
11101107
Snapshot1::sched_yield(self).await?;
11111108
Ok(())
11121109
}
11131110

11141111
async fn random_get<'a>(
1115-
&mut self,
1112+
&self,
11161113
buf: &GuestPtr<'a, u8>,
11171114
buf_len: types::Size,
11181115
) -> Result<(), Error> {
@@ -1121,7 +1118,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
11211118
}
11221119

11231120
async fn sock_recv<'a>(
1124-
&mut self,
1121+
&self,
11251122
_fd: types::Fd,
11261123
_ri_data: &types::IovecArray<'a>,
11271124
_ri_flags: types::Riflags,
@@ -1130,15 +1127,15 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
11301127
}
11311128

11321129
async fn sock_send<'a>(
1133-
&mut self,
1130+
&self,
11341131
_fd: types::Fd,
11351132
_si_data: &types::CiovecArray<'a>,
11361133
_si_flags: types::Siflags,
11371134
) -> Result<types::Size, Error> {
11381135
Err(Error::trap(anyhow::Error::msg("sock_send unsupported")))
11391136
}
11401137

1141-
async fn sock_shutdown(&mut self, _fd: types::Fd, _how: types::Sdflags) -> Result<(), Error> {
1138+
async fn sock_shutdown(&self, _fd: types::Fd, _how: types::Sdflags) -> Result<(), Error> {
11421139
Err(Error::trap(anyhow::Error::msg("sock_shutdown unsupported")))
11431140
}
11441141
}

0 commit comments

Comments
 (0)