Skip to content

Commit 7c92025

Browse files
committed
wasi: Allow specifying a FsMemoryLimiter in the Console options
Allows users to set a memory limiter for limiting temp fs memory usage when using the Console. Part of #3865
1 parent 01c20f6 commit 7c92025

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

lib/wasi/src/os/console/mod.rs

+22
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub struct Console {
5151
stdout: ArcBoxFile,
5252
stderr: ArcBoxFile,
5353
capabilities: Capabilities,
54+
memfs_memory_limiter: Option<virtual_fs::limiter::DynFsMemoryLimiter>,
5455
}
5556

5657
impl Console {
@@ -81,6 +82,7 @@ impl Console {
8182
stdout: ArcBoxFile::new(Box::new(Pipe::channel().0)),
8283
stderr: ArcBoxFile::new(Box::new(Pipe::channel().0)),
8384
capabilities: Default::default(),
85+
memfs_memory_limiter: None,
8486
}
8587
}
8688

@@ -143,6 +145,14 @@ impl Console {
143145
self
144146
}
145147

148+
pub fn with_mem_fs_memory_limiter(
149+
mut self,
150+
limiter: virtual_fs::limiter::DynFsMemoryLimiter,
151+
) -> Self {
152+
self.memfs_memory_limiter = Some(limiter);
153+
self
154+
}
155+
146156
pub fn run(&mut self) -> Result<(TaskJoinHandle, WasiProcess), VirtualBusError> {
147157
// Extract the program name from the arguments
148158
let empty_args: Vec<&[u8]> = Vec::new();
@@ -193,6 +203,18 @@ impl Console {
193203
// TODO: no unwrap!
194204
let env = WasiEnv::from_init(env_init).unwrap();
195205

206+
if let Some(limiter) = &self.memfs_memory_limiter {
207+
match &env.state.fs.root_fs {
208+
crate::fs::WasiFsRoot::Sandbox(tmpfs) => {
209+
tmpfs.set_memory_limiter(limiter.clone());
210+
}
211+
crate::fs::WasiFsRoot::Backing(_) => {
212+
tracing::error!("tried to set a tmpfs memory limiter on a backing fs");
213+
return Err(VirtualBusError::InvokeFailed);
214+
}
215+
}
216+
}
217+
196218
// TODO: this should not happen here...
197219
// Display the welcome message
198220
let tasks = env.tasks().clone();

0 commit comments

Comments
 (0)