diff --git a/api/ruxos_posix_api/src/imp/mmap/api.rs b/api/ruxos_posix_api/src/imp/mmap/api.rs index f26eee7c4..7c4edd271 100644 --- a/api/ruxos_posix_api/src/imp/mmap/api.rs +++ b/api/ruxos_posix_api/src/imp/mmap/api.rs @@ -75,7 +75,7 @@ pub fn sys_mmap( fid }; - let mut new = Vma::new(fid, offset, prot, flags); + let mut new = Vma::new(fid, offset, prot, flags)?; let binding_task = current(); let mut vma_map = binding_task.mm.vma_map.lock(); let addr_condition = if start == 0 { None } else { Some(start) }; @@ -460,10 +460,11 @@ pub fn sys_mremap( return Err(LinuxError::ENOMEM); } // find the right region to expand them in orignal addr. + let dummy_vma = Vma::new(-1, 0, 0, 0).unwrap(); let (upper, _) = vma_map .lower_bound(Bound::Included(&old_end)) .peek_next() - .unwrap_or((&VMA_END, &Vma::new(-1, 0, 0, 0))); + .unwrap_or((&VMA_END, &dummy_vma)); if upper - old_end >= new_size - old_size { let ret = old_vma.start_addr; let new_end = old_start + new_size; diff --git a/api/ruxos_posix_api/src/imp/mmap/utils.rs b/api/ruxos_posix_api/src/imp/mmap/utils.rs index 4fbac13b1..12b7d7588 100644 --- a/api/ruxos_posix_api/src/imp/mmap/utils.rs +++ b/api/ruxos_posix_api/src/imp/mmap/utils.rs @@ -108,10 +108,11 @@ pub(crate) fn find_free_region( } else { VMA_START }; + let dummy_vma = Vma::new(-1, 0, 0, 0).unwrap(); let (upper, _) = vma_map .lower_bound(Bound::Included(&start)) .peek_next() - .unwrap_or((&VMA_END, &Vma::new(-1, 0, 0, 0))); + .unwrap_or((&VMA_END, &dummy_vma)); if upper - start >= len && end_addr <= start { return Some(start); } diff --git a/modules/ruxtask/src/vma.rs b/modules/ruxtask/src/vma.rs index 6a2d5fc4c..0864ccb13 100644 --- a/modules/ruxtask/src/vma.rs +++ b/modules/ruxtask/src/vma.rs @@ -17,6 +17,7 @@ use crate::fs::get_file_like; use crate::TaskId; use alloc::{collections::BTreeMap, sync::Arc}; use axalloc::global_allocator; +use axerrno::LinuxError; use memory_addr::PhysAddr; use ruxfs::File; use ruxhal::mem::phys_to_virt; @@ -181,20 +182,20 @@ impl Default for MmapStruct { /// Impl for Vma. impl Vma { /// Create a new `Vma` instance. - pub fn new(_fid: i32, offset: usize, prot: u32, flags: u32) -> Self { + pub fn new(_fid: i32, offset: usize, prot: u32, flags: u32) -> Result { // #[cfg(feature = "fs")] let file = if _fid < 0 { None } else { - let f = get_file_like(_fid).expect("invaild fd for vma"); + let f = get_file_like(_fid)?; Some( f.clone() .into_any() .downcast::() - .expect("should be effective fid"), + .map_err(|_| LinuxError::EBADF)?, ) }; - Vma { + Ok(Vma { start_addr: 0, end_addr: 0, size: 0, @@ -204,7 +205,7 @@ impl Vma { flags, prot, from_process: current().id(), - } + }) } /// Clone a new `Vma` instance.