Skip to content

Commit

Permalink
wasi: Allow specifying a FsMemoryLimiter in the Console options
Browse files Browse the repository at this point in the history
Allows users to set a memory limiter for limiting temp fs memory usage
when using the Console.

Part of #3865
  • Loading branch information
theduke committed May 24, 2023
1 parent 40f5abc commit b5c5aef
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/wasi/src/os/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub struct Console {
stdout: ArcBoxFile,
stderr: ArcBoxFile,
capabilities: Capabilities,
memfs_memory_limiter: Option<virtual_fs::limiter::DynFsMemoryLimiter>,
}

impl Console {
Expand Down Expand Up @@ -81,6 +82,7 @@ impl Console {
stdout: ArcBoxFile::new(Box::new(Pipe::channel().0)),
stderr: ArcBoxFile::new(Box::new(Pipe::channel().0)),
capabilities: Default::default(),
memfs_memory_limiter: None,
}
}

Expand Down Expand Up @@ -143,6 +145,14 @@ impl Console {
self
}

pub fn with_mem_fs_memory_limiter(
mut self,
limiter: virtual_fs::limiter::DynFsMemoryLimiter,
) -> Self {
self.memfs_memory_limiter = Some(limiter);
self
}

pub fn run(&mut self) -> Result<(TaskJoinHandle, WasiProcess), VirtualBusError> {
// Extract the program name from the arguments
let empty_args: Vec<&[u8]> = Vec::new();
Expand Down Expand Up @@ -193,6 +203,18 @@ impl Console {
// TODO: no unwrap!
let env = WasiEnv::from_init(env_init).unwrap();

if let Some(limiter) = &self.memfs_memory_limiter {
match &env.state.fs.root_fs {
crate::fs::WasiFsRoot::Sandbox(tmpfs) => {
tmpfs.set_memory_limiter(limiter.clone());
}
crate::fs::WasiFsRoot::Backing(_) => {
tracing::error!("tried to set a tmpfs memory limiter on a backing fs");
return Err(VirtualBusError::InvokeFailed);
}
}
}

// TODO: this should not happen here...
// Display the welcome message
let tasks = env.tasks().clone();
Expand Down

0 comments on commit b5c5aef

Please sign in to comment.