diff --git a/lib/vm/src/memory.rs b/lib/vm/src/memory.rs index d3565608cf1..ca227404c86 100644 --- a/lib/vm/src/memory.rs +++ b/lib/vm/src/memory.rs @@ -221,7 +221,7 @@ impl VMOwnedMemory { pub fn new_with_file( memory: &MemoryType, style: &MemoryStyle, - backing_file: std::fs::File, + backing_file: std::path::PathBuf, memory_type: MmapType, ) -> Result { unsafe { Self::new_internal(memory, style, None, Some(backing_file), memory_type) } @@ -261,7 +261,7 @@ impl VMOwnedMemory { memory: &MemoryType, style: &MemoryStyle, vm_memory_location: NonNull, - backing_file: Option, + backing_file: Option, memory_type: MmapType, ) -> Result { Self::new_internal( @@ -278,7 +278,7 @@ impl VMOwnedMemory { memory: &MemoryType, style: &MemoryStyle, vm_memory_location: Option>, - backing_file: Option, + backing_file: Option, memory_type: MmapType, ) -> Result { if memory.minimum > Pages::max_value() { @@ -463,7 +463,7 @@ impl VMSharedMemory { pub fn new_with_file( memory: &MemoryType, style: &MemoryStyle, - backing_file: std::fs::File, + backing_file: std::path::PathBuf, memory_type: MmapType, ) -> Result { Ok(VMOwnedMemory::new_with_file(memory, style, backing_file, memory_type)?.to_shared()) @@ -497,7 +497,7 @@ impl VMSharedMemory { memory: &MemoryType, style: &MemoryStyle, vm_memory_location: NonNull, - backing_file: Option, + backing_file: Option, memory_type: MmapType, ) -> Result { Ok(VMOwnedMemory::from_definition_with_file( diff --git a/lib/vm/src/mmap.rs b/lib/vm/src/mmap.rs index c6d82f88308..9374f7319d1 100644 --- a/lib/vm/src/mmap.rs +++ b/lib/vm/src/mmap.rs @@ -67,7 +67,7 @@ impl Mmap { pub fn accessible_reserved( mut accessible_size: usize, mapping_size: usize, - mut backing_file: Option, + mut backing_file: Option, memory_type: MmapType, ) -> Result { use std::os::fd::IntoRawFd; @@ -85,17 +85,38 @@ impl Mmap { // If there is a backing file, resize the file so that its at least // `mapping_size` bytes. - if let Some(backing_file) = &mut backing_file { - let len = backing_file.metadata().map_err(|e| e.to_string())?.len() as usize; + let mut memory_fd = -1; + if let Some(backing_file_path) = &mut backing_file { + let file = std::fs::OpenOptions::new() + .read(true) + .write(true) + .open(&backing_file_path) + .map_err(|e| e.to_string())?; + + let mut backing_file_accessible = backing_file_path.clone(); + backing_file_accessible.set_extension("accessible"); + + let len = file.metadata().map_err(|e| e.to_string())?.len() as usize; if len < mapping_size { - backing_file - .set_len(mapping_size as u64) + std::fs::write(&backing_file_accessible, format!("{}", len).as_bytes()).ok(); + + file.set_len(mapping_size as u64) .map_err(|e| e.to_string())?; } - accessible_size = accessible_size.max(len).min(mapping_size); - } - let memory_fd = backing_file.map_or(-1, |fd| fd.into_raw_fd()); + if backing_file_accessible.exists() { + let accessible = std::fs::read_to_string(&backing_file_accessible) + .map_err(|e| e.to_string())? + .parse::() + .map_err(|e| e.to_string())?; + accessible_size = accessible_size.max(accessible); + } else { + accessible_size = accessible_size.max(len); + } + + accessible_size = accessible_size.min(mapping_size); + memory_fd = file.into_raw_fd(); + } // Compute the flags let mut flags = match memory_fd { diff --git a/lib/wasix/src/fs/mod.rs b/lib/wasix/src/fs/mod.rs index 18ab594bba6..384a929811e 100644 --- a/lib/wasix/src/fs/mod.rs +++ b/lib/wasix/src/fs/mod.rs @@ -60,10 +60,10 @@ pub const VIRTUAL_ROOT_FD: WasiFd = 3; /// The root inode and stdio inodes are the first inodes in the /// file system tree -pub const FS_ROOT_INO: Inode = Inode(0); -pub const FS_STDIN_INO: Inode = Inode(1); -pub const FS_STDOUT_INO: Inode = Inode(2); -pub const FS_STDERR_INO: Inode = Inode(3); +pub const FS_STDIN_INO: Inode = Inode(10); +pub const FS_STDOUT_INO: Inode = Inode(11); +pub const FS_STDERR_INO: Inode = Inode(12); +pub const FS_ROOT_INO: Inode = Inode(13); const STDIN_DEFAULT_RIGHTS: Rights = { // This might seem a bit overenineered, but it's the only way I