Path: blob/main/crates/wasi-common/src/ctx.rs
1691 views
use crate::clocks::WasiClocks;1use crate::dir::{DirEntry, WasiDir};2use crate::file::{FileAccessMode, FileEntry, WasiFile};3use crate::sched::WasiSched;4use crate::string_array::StringArray;5use crate::table::Table;6use crate::{Error, StringArrayError};7use cap_rand::RngCore;8use std::ops::Deref;9use std::path::{Path, PathBuf};10use std::sync::{Arc, Mutex};1112/// An `Arc`-wrapper around the wasi-common context to allow mutable access to13/// the file descriptor table. This wrapper is only necessary due to the14/// signature of `fd_fdstat_set_flags`; if that changes, there are a variety of15/// improvements that can be made (TODO:16/// <https://github.com/bytecodealliance/wasmtime/issues/5643)>.17#[derive(Clone)]18pub struct WasiCtx(Arc<WasiCtxInner>);1920pub struct WasiCtxInner {21pub args: StringArray,22pub env: StringArray,23// TODO: this mutex should not be necessary, it forces threads to serialize24// their access to randomness unnecessarily25// (https://github.com/bytecodealliance/wasmtime/issues/5660).26pub random: Mutex<Box<dyn RngCore + Send + Sync>>,27pub clocks: WasiClocks,28pub sched: Box<dyn WasiSched>,29pub table: Table,30}3132impl WasiCtx {33pub fn new(34random: Box<dyn RngCore + Send + Sync>,35clocks: WasiClocks,36sched: Box<dyn WasiSched>,37table: Table,38) -> Self {39let s = WasiCtx(Arc::new(WasiCtxInner {40args: StringArray::new(),41env: StringArray::new(),42random: Mutex::new(random),43clocks,44sched,45table,46}));47s.set_stdin(Box::new(crate::pipe::ReadPipe::new(std::io::empty())));48s.set_stdout(Box::new(crate::pipe::WritePipe::new(std::io::sink())));49s.set_stderr(Box::new(crate::pipe::WritePipe::new(std::io::sink())));50s51}5253pub fn insert_file(&self, fd: u32, file: Box<dyn WasiFile>, access_mode: FileAccessMode) {54self.table()55.insert_at(fd, Arc::new(FileEntry::new(file, access_mode)));56}5758pub fn push_file(59&self,60file: Box<dyn WasiFile>,61access_mode: FileAccessMode,62) -> Result<u32, Error> {63self.table()64.push(Arc::new(FileEntry::new(file, access_mode)))65}6667pub fn insert_dir(&self, fd: u32, dir: Box<dyn WasiDir>, path: PathBuf) {68self.table()69.insert_at(fd, Arc::new(DirEntry::new(Some(path), dir)));70}7172pub fn push_dir(&self, dir: Box<dyn WasiDir>, path: PathBuf) -> Result<u32, Error> {73self.table().push(Arc::new(DirEntry::new(Some(path), dir)))74}7576pub fn table(&self) -> &Table {77&self.table78}7980pub fn table_mut(&mut self) -> Option<&mut Table> {81Arc::get_mut(&mut self.0).map(|c| &mut c.table)82}8384pub fn push_arg(&mut self, arg: &str) -> Result<(), StringArrayError> {85let s = Arc::get_mut(&mut self.0).expect(86"`push_arg` should only be used during initialization before the context is cloned",87);88s.args.push(arg.to_owned())89}9091pub fn push_env(&mut self, var: &str, value: &str) -> Result<(), StringArrayError> {92let s = Arc::get_mut(&mut self.0).expect(93"`push_env` should only be used during initialization before the context is cloned",94);95s.env.push(format!("{var}={value}"))?;96Ok(())97}9899pub fn set_stdin(&self, f: Box<dyn WasiFile>) {100self.insert_file(0, f, FileAccessMode::READ);101}102103pub fn set_stdout(&self, f: Box<dyn WasiFile>) {104self.insert_file(1, f, FileAccessMode::WRITE);105}106107pub fn set_stderr(&self, f: Box<dyn WasiFile>) {108self.insert_file(2, f, FileAccessMode::WRITE);109}110111pub fn push_preopened_dir(112&self,113dir: Box<dyn WasiDir>,114path: impl AsRef<Path>,115) -> Result<(), Error> {116self.table()117.push(Arc::new(DirEntry::new(Some(path.as_ref().to_owned()), dir)))?;118Ok(())119}120}121122impl Deref for WasiCtx {123type Target = WasiCtxInner;124fn deref(&self) -> &Self::Target {125&self.0126}127}128129130