Skip to content

Commit

Permalink
modify syscall mmap and fstat, to support nested dynamic library depe…
Browse files Browse the repository at this point in the history
…ndencies.
  • Loading branch information
thesayol committed Jan 15, 2024
1 parent 14942ff commit 994be05
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
12 changes: 10 additions & 2 deletions api/ruxos_posix_api/src/imp/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ pub unsafe fn sys_fstat(fd: c_int, kst: *mut core::ffi::c_void) -> c_int {
let kst = kst as *mut ctypes::kstat;
unsafe {
(*kst).st_dev = st.st_dev;
(*kst).st_ino = st.st_dev;
// temporarily set different inode for each file, for musl libc interpreter
(*kst).st_ino = st.st_dev + st.st_size as u64 + (st.st_mtime.tv_nsec % 1000) as u64 ;
(*kst).st_mode = st.st_mode;
(*kst).st_nlink = st.st_nlink;
(*kst).st_uid = st.st_uid;
Expand Down Expand Up @@ -250,7 +251,14 @@ pub unsafe fn sys_newfstatat(
"sys_newfstatat <= fd: {}, path: {:?}, flag: {:x}",
_fd, path, flag
);
assert_eq!(_fd, ctypes::AT_FDCWD as c_int);

// temporary use for glic
if _fd != ctypes::AT_FDCWD as c_int {
sys_fstat(_fd, kst as *mut _);
return 0;
}

// assert_eq!(_fd, ctypes::AT_FDCWD as c_int);
syscall_body!(sys_newfstatat, {
if kst.is_null() {
return Err(LinuxError::EFAULT);
Expand Down
42 changes: 35 additions & 7 deletions api/ruxos_posix_api/src/imp/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
* See the Mulan PSL v2 for more details.
*/

use crate::ctypes;
use crate::{ctypes, sys_lseek};
use alloc::alloc::{alloc, dealloc};
use core::{
alloc::Layout,
ffi::{c_int, c_void},
ffi::{c_int, c_void}, borrow::Borrow,
};

use axerrno::LinuxError;
Expand All @@ -20,6 +20,7 @@ use axerrno::LinuxError;
/// ing process.
///
/// TODO: Only support `start` equals to NULL, ignore fd, prot, flags
/// add something, temporary use for musl interpreter, waiting for improvement.
pub fn sys_mmap(
start: *mut c_void,
len: ctypes::size_t,
Expand All @@ -30,17 +31,44 @@ pub fn sys_mmap(
) -> *mut c_void {
debug!("sys_mmap <= start: {:p}, len: {}, fd: {}", start, len, _fd);
syscall_body!(sys_mmap, {
if !start.is_null() {
debug!("Do not support explicitly specifying start addr");
return Ok(core::ptr::null_mut());
// if !start.is_null() {
// debug!("Do not support explicitly specifying start addr");
// return Ok(core::ptr::null_mut());
// }


#[cfg(feature = "fd")]
if !start.is_null() && _fd > 0 {
let ptr = start;
let fd = _fd;
use crate::imp::fd_ops::get_file_like;
let dst = unsafe { core::slice::from_raw_parts_mut(ptr as *mut u8, len) };
sys_lseek(fd, _off, 0);
get_file_like(fd)?.read(dst)?;
info!("read fd {:x} into , ptr {:#p}, {:x?}", fd, ptr, &dst[..20]);
return Ok(ptr);
}

let layout = Layout::from_size_align(len, 8).unwrap();
unsafe {

let ptr = unsafe {
let ptr = alloc(layout).cast::<c_void>();
(ptr as *mut u8).write_bytes(0, len);
assert!(!ptr.is_null(), "sys_mmap failed");
Ok(ptr)
ptr
};

#[cfg(feature = "fd")]
if _fd > 0 {
let fd = _fd;
use crate::imp::fd_ops::get_file_like;
let dst = unsafe { core::slice::from_raw_parts_mut(ptr as *mut u8, len) };
sys_lseek(fd, _off, 0);
get_file_like(fd)?.read(dst)?;
info!("read fd {:x} into , ptr {:#p}, {:x?}", fd, ptr, &dst[..20]);
}

Ok(ptr)
})
}

Expand Down

0 comments on commit 994be05

Please sign in to comment.