Skip to content

Commit

Permalink
Fixed a bug on accessible area not being preserved on memory with a b…
Browse files Browse the repository at this point in the history
…acking file
  • Loading branch information
john-sharratt committed Jun 1, 2024
1 parent 47741a2 commit 4937d1d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
10 changes: 5 additions & 5 deletions lib/vm/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, MemoryError> {
unsafe { Self::new_internal(memory, style, None, Some(backing_file), memory_type) }
Expand Down Expand Up @@ -261,7 +261,7 @@ impl VMOwnedMemory {
memory: &MemoryType,
style: &MemoryStyle,
vm_memory_location: NonNull<VMMemoryDefinition>,
backing_file: Option<std::fs::File>,
backing_file: Option<std::path::PathBuf>,
memory_type: MmapType,
) -> Result<Self, MemoryError> {
Self::new_internal(
Expand All @@ -278,7 +278,7 @@ impl VMOwnedMemory {
memory: &MemoryType,
style: &MemoryStyle,
vm_memory_location: Option<NonNull<VMMemoryDefinition>>,
backing_file: Option<std::fs::File>,
backing_file: Option<std::path::PathBuf>,
memory_type: MmapType,
) -> Result<Self, MemoryError> {
if memory.minimum > Pages::max_value() {
Expand Down Expand Up @@ -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<Self, MemoryError> {
Ok(VMOwnedMemory::new_with_file(memory, style, backing_file, memory_type)?.to_shared())
Expand Down Expand Up @@ -497,7 +497,7 @@ impl VMSharedMemory {
memory: &MemoryType,
style: &MemoryStyle,
vm_memory_location: NonNull<VMMemoryDefinition>,
backing_file: Option<std::fs::File>,
backing_file: Option<std::path::PathBuf>,
memory_type: MmapType,
) -> Result<Self, MemoryError> {
Ok(VMOwnedMemory::from_definition_with_file(
Expand Down
37 changes: 29 additions & 8 deletions lib/vm/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Mmap {
pub fn accessible_reserved(
mut accessible_size: usize,
mapping_size: usize,
mut backing_file: Option<std::fs::File>,
mut backing_file: Option<std::path::PathBuf>,
memory_type: MmapType,
) -> Result<Self, String> {
use std::os::fd::IntoRawFd;
Expand All @@ -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::<usize>()
.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 {
Expand Down
8 changes: 4 additions & 4 deletions lib/wasix/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4937d1d

Please sign in to comment.