From 395379410f0fd08bf5ba0a2b922b7aa31f7bf07d Mon Sep 17 00:00:00 2001 From: qianxichen233 Date: Mon, 16 Sep 2024 06:01:29 +0000 Subject: [PATCH 01/37] integrated into lind-wasm --- src/safeposix/dispatcher.rs | 384 ++++++++++++++++++++++++++++++++++++ 1 file changed, 384 insertions(+) diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index 488422c3..c6653c8f 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -131,6 +131,8 @@ use std::io; // use super::net::NET_METADATA; // use super::shm::SHM_METADATA; // use super::syscalls::{fs_constants::IPC_STAT, sys_constants::*}; +use crate::interface::types::SockaddrDummy; +use crate::interface::SigactionStruct; use crate::{example_grates, interface}; use crate::interface::errnos::*; // use crate::lib_fs_utils::{lind_deltree, visit_children}; @@ -207,6 +209,388 @@ pub extern "C" fn rustposix_thread_init(cageid: u64, signalflag: u64) { interface::signalflag_set(signalflag); } +use std::ffi::CStr; +use std::str::Utf8Error; + +fn u64_to_str(ptr: u64) -> Result<&'static str, Utf8Error> { + // Convert the u64 to a pointer to a C string (null-terminated) + let c_str = ptr as *const i8; + + // Unsafe block to handle raw pointer and C string conversion + unsafe { + // Create a CStr from the raw pointer + let c_str = CStr::from_ptr(c_str); + + // Convert the CStr to a Rust &str + c_str.to_str() + } +} + +impl Arg { + pub fn from_u64_as_cbuf(value: u64) -> Self { + Arg { + dispatch_cbuf: value as *const u8, + } + } + + pub fn from_u64_as_socklen_ptr(value: u64) -> Self { + Arg { + dispatch_socklen_t_ptr: value as *mut u32, + } + } + + pub fn from_u64_as_sockaddrstruct(value: u64) -> Self { + Arg { + dispatch_constsockaddrstruct: value as *const SockaddrDummy, + } + } + pub fn from_u64_as_constsigactionstruct(value: u64) -> Self { + Arg { + dispatch_constsigactionstruct: value as *const SigactionStruct, + } + } + // pub fn from_u64_as_sigactionstruct(value: u64) -> Self { + // Arg { + // dispatch_sigactionstruct: value as *mut SigactionStruct, + // } + // } +} + +#[no_mangle] +pub extern "C" fn lind_syscall_api( + cageid: u64, + call_number: u32, + call_name: u64, + start_address: u64, + arg1: u64, + arg2: u64, + arg3: u64, + arg4: u64, + arg5: u64, + arg6: u64, +) -> i32 { + let call_number = call_number as i32; + + // Print all the arguments + // println!("rawposix call_number: {}", call_number); + // println!("call_name: {}", call_name); + // println!("start_address: {}", start_address); + // println!("arg1: {}", arg1); + // println!("arg2: {}", arg2); + // println!("arg3: {}", arg3); + // println!("arg4: {}", arg4); + // println!("arg5: {}", arg5); + // println!("arg6: {}", arg6); + + let ret = match call_number { + WRITE_SYSCALL => { + let fd = arg1 as i32; + let buf = (start_address + arg2) as *const u8; + let count = arg3 as usize; + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .write_syscall(fd, buf, count) + } + } + + WRITEV_SYSCALL => { + let fd = arg1 as i32; + let iovec = (start_address + arg2) as *const interface::IovecStruct; + let iovcnt = arg3 as i32; + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .writev_syscall(fd, iovec, iovcnt) + } + } + + MUNMAP_SYSCALL => { + let addr = (start_address + arg1) as *mut u8; + let len = arg2 as usize; + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .munmap_syscall(addr, len) + } + } + + MMAP_SYSCALL => { + let addr = (start_address + arg1) as *mut u8; + let len = arg2 as usize; + let prot = arg3 as i32; + let flags = arg4 as i32; + let fildes = arg5 as i32; + let off = arg6 as i64; + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .mmap_syscall(addr, len, prot, flags, fildes, off) + } + } + + PREAD_SYSCALL => { + let fd = arg1 as i32; + let buf = (start_address + arg2) as *mut u8; + let count = arg3 as usize; + let offset = arg4 as i64; + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .pread_syscall(fd, buf, count, offset) + } + } + + READ_SYSCALL => { + let fd = arg1 as i32; + let buf = (start_address + arg2) as *mut u8; + let count = arg3 as usize; + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .read_syscall(fd, buf, count) + } + } + + // SIGACTION_SYSCALL => { + // let sig = arg1 as i32; + // let act = match interface::get_constsigactionstruct(Arg::from_u64_as_constsigactionstruct(arg2)) { + // Ok(res_act) => res_act, + // Err(_) => return -1, // Handle error appropriately, return an error code + // }; + + // let oact = match interface::get_sigactionstruct(Arg::from_u64_as_sigactionstruct(arg3)) { + // Ok(res_oact) => res_oact, + // Err(_) => return -1, // Handle error appropriately, return an error code + // }; + + // interface::check_cageid(cageid); + // unsafe { + // CAGE_TABLE[cageid as usize] + // .as_ref() + // .unwrap() + // .sigaction_syscall(sig, act, oact) + // } + // } + + CLOSE_SYSCALL => { + let fd = arg1 as i32; + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .close_syscall(fd) + } + } + + ACCESS_SYSCALL => { + let path = match u64_to_str(arg1) { + Ok(path_str) => path_str, + Err(_) => return -1, // Handle error appropriately, return an error code + }; + let amode = arg2 as i32; + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .access_syscall(path, amode) + } + } + + OPEN_SYSCALL => { + let path = match u64_to_str(arg1) { + Ok(path_str) => path_str, + Err(_) => return -1, // Handle error appropriately, return an error code + }; + let flags = arg2 as i32; + let mode = arg3 as u32; + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .open_syscall(path, flags, mode) + } + } + + SOCKET_SYSCALL => { + let domain = arg1 as i32; + let socktype = arg2 as i32; + let protocol = arg3 as i32; + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .socket_syscall(domain, socktype, protocol) + } + } + + CONNECT_SYSCALL => { + let fd = arg1 as i32; + let addrlen = arg3 as u32; + let addr = get_onearg!(interface::get_sockaddr(Arg::from_u64_as_sockaddrstruct(arg2), addrlen)); + interface::check_cageid(cageid); + unsafe { + let remoteaddr = match Ok::<&interface::GenSockaddr, i32>(&addr) { + Ok(addr) => addr, + Err(_) => panic!("Failed to get sockaddr"), // Handle error appropriately + }; + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .connect_syscall(fd, remoteaddr) + } + } + + BIND_SYSCALL => { + let fd = arg1 as i32; + let addrlen = arg3 as u32; + let addr = get_onearg!(interface::get_sockaddr(Arg::from_u64_as_sockaddrstruct(arg2), addrlen)); + interface::check_cageid(cageid); + unsafe { + let localaddr = match Ok::<&interface::GenSockaddr, i32>(&addr) { + Ok(addr) => addr, + Err(_) => panic!("Failed to get sockaddr"), // Handle error appropriately + }; + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .bind_syscall(fd, localaddr) + } + } + + ACCEPT_SYSCALL => { + let mut addr = interface::GenSockaddr::V4(interface::SockaddrV4::default()); //value doesn't matter + let nullity1 = interface::arg_nullity(&Arg::from_u64_as_cbuf(arg2)); + let nullity2 = interface::arg_nullity(&Arg::from_u64_as_cbuf(arg3)); + + if nullity1 && nullity2 { + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .accept_syscall(arg1 as i32, &mut Some(&mut addr)) + } + } else if !(nullity1 || nullity2) { + interface::check_cageid(cageid); + let rv = unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .accept_syscall(arg1 as i32, &mut Some(&mut addr)) + }; + if rv >= 0 { + interface::copy_out_sockaddr(Arg::from_u64_as_sockaddrstruct(arg2), Arg::from_u64_as_socklen_ptr(arg3), addr); + } + rv + } else { + syscall_error( + Errno::EINVAL, + "accept", + "exactly one of the last two arguments was zero", + ) + } + } + + GETPID_SYSCALL => { + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .getpid_syscall() + } + } + + FORK_SYSCALL => { + let id = arg1 as u64; + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .fork_syscall(id) + } + } + + // WAIT_SYSCALL => { + // let mut status = 0; + // interface::check_cageid(cageid); + // let ret = unsafe { + // CAGE_TABLE[cageid as usize] + // .as_ref() + // .unwrap() + // .wait_syscall(&mut status) + // }; + + // let status_addr = (start_address + arg1) as *mut i32; + // unsafe { *status_addr = status; } + + // ret + // } + + EXEC_SYSCALL => { + interface::check_cageid(cageid); + let child_cageid = arg1 as u64; + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .exec_syscall(child_cageid) + } + } + + EXIT_SYSCALL => { + interface::check_cageid(cageid); + let status = arg1 as i32; + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .exit_syscall(status) + } + } + + // FUTEX_SYSCALL => { + // let uaddr = (start_address + arg1) as u64; + // let futex_op = arg2 as u32; + // let val = arg3 as u32; + // let timeout = arg4 as u32; + // let uaddr2 = arg5 as u32; + // let val3 = arg6 as u32; + + // interface::check_cageid(cageid); + // unsafe { + // CAGE_TABLE[cageid as usize] + // .as_ref() + // .unwrap() + // .futex_syscall(uaddr, futex_op, val, timeout, uaddr2, val3) + // } + // } + + _ => -1, // Return -1 for unknown syscalls + }; + + // println!("Lind returns: {}", ret); + ret +} + #[no_mangle] pub extern "C" fn dispatcher( cageid: u64, From 519e82809064c1fb7e20738aa273cfaf2acb60df Mon Sep 17 00:00:00 2001 From: qianxichen233 Date: Mon, 16 Sep 2024 16:47:03 +0000 Subject: [PATCH 02/37] futex implementation --- src/interface/misc.rs | 6 +++++- src/safeposix/dispatcher.rs | 30 +++++++++++++++--------------- src/safeposix/syscalls/fs_calls.rs | 10 ++++++++++ 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/interface/misc.rs b/src/interface/misc.rs index 5da54e0c..9ad234c6 100644 --- a/src/interface/misc.rs +++ b/src/interface/misc.rs @@ -25,7 +25,7 @@ pub use std::sync::atomic::{ pub use std::sync::Arc as RustRfc; pub use std::thread::spawn as helper_thread; -use libc::{mmap, pthread_exit, pthread_kill, pthread_self, sched_yield}; +use libc::{mmap, pthread_exit, pthread_kill, pthread_self, sched_yield, syscall, SYS_futex}; use std::ffi::c_void; pub use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; @@ -306,6 +306,10 @@ pub fn lind_kill_from_id(cage_id: u64, sig: i32) { } } +pub fn libc_futex(uaddr: u64, futex_op: u32, val: u32, val2: u32, uaddr2: u32, val3: u32) -> i32 { + unsafe { syscall(SYS_futex, uaddr, futex_op, val, val2, uaddr2, val3) as i32 } +} + #[derive(Debug)] pub struct AdvisoryLock { //0 signifies unlocked, -1 signifies locked exclusively, positive number signifies that many shared lock holders diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index c6653c8f..ab74fbfe 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -567,22 +567,22 @@ pub extern "C" fn lind_syscall_api( } } - // FUTEX_SYSCALL => { - // let uaddr = (start_address + arg1) as u64; - // let futex_op = arg2 as u32; - // let val = arg3 as u32; - // let timeout = arg4 as u32; - // let uaddr2 = arg5 as u32; - // let val3 = arg6 as u32; + FUTEX_SYSCALL => { + let uaddr = (start_address + arg1) as u64; + let futex_op = arg2 as u32; + let val = arg3 as u32; + let timeout = arg4 as u32; + let uaddr2 = arg5 as u32; + let val3 = arg6 as u32; - // interface::check_cageid(cageid); - // unsafe { - // CAGE_TABLE[cageid as usize] - // .as_ref() - // .unwrap() - // .futex_syscall(uaddr, futex_op, val, timeout, uaddr2, val3) - // } - // } + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .futex_syscall(uaddr, futex_op, val, timeout, uaddr2, val3) + } + } _ => -1, // Return -1 for unknown syscalls }; diff --git a/src/safeposix/syscalls/fs_calls.rs b/src/safeposix/syscalls/fs_calls.rs index 7defb615..58ce6452 100644 --- a/src/safeposix/syscalls/fs_calls.rs +++ b/src/safeposix/syscalls/fs_calls.rs @@ -2244,6 +2244,16 @@ impl Cage { } return 0; } + + // We're directly patching in the libc futex call for experimentation with lind-wasm + // this should allow us to use the nptl data structures such as mutexes and condvars directly + // as opposed to lind-nacl's individual implementations + // + // to perform this we just directly pass futex's var args as unsigned 32 bit integers to syscall() with SYS_futex + pub fn futex_syscall(&self, uaddr: u64, futex_op: u32, val: u32, val2: u32, uaddr2: u32, val3: u32) -> i32 { + let retval = interface::libc_futex(uaddr, futex_op, val, val2, uaddr2, val3); + return retval; + } } pub fn kernel_close(kernelfd: u64) { From 190cd403b688ce2a6a906ca2713fea75bb13cc6e Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Thu, 19 Sep 2024 21:27:42 -0400 Subject: [PATCH 03/37] update dispatcher.rs --- src/safeposix/dispatcher.rs | 196 +++++++++++++++++++++++++++++++++++- 1 file changed, 192 insertions(+), 4 deletions(-) diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index c6653c8f..0b60b68e 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -258,7 +258,6 @@ impl Arg { #[no_mangle] pub extern "C" fn lind_syscall_api( - cageid: u64, call_number: u32, call_name: u64, start_address: u64, @@ -269,10 +268,11 @@ pub extern "C" fn lind_syscall_api( arg5: u64, arg6: u64, ) -> i32 { + let cageid = 1; let call_number = call_number as i32; // Print all the arguments - // println!("rawposix call_number: {}", call_number); + println!("rawposix call_number: {}", call_number); // println!("call_name: {}", call_name); // println!("start_address: {}", start_address); // println!("arg1: {}", arg1); @@ -412,7 +412,7 @@ pub extern "C" fn lind_syscall_api( } OPEN_SYSCALL => { - let path = match u64_to_str(arg1) { + let path = match u64_to_str(start_address + arg1) { Ok(path_str) => path_str, Err(_) => return -1, // Handle error appropriately, return an error code }; @@ -567,6 +567,194 @@ pub extern "C" fn lind_syscall_api( } } + RENAME_SYSCALL => { + let old_ptr = (start_address + arg1) as *const u8; + let new_ptr = (start_address + arg2) as *const u8; + + // Convert the raw pointers to `&str` + let old = unsafe { + CStr::from_ptr(old_ptr as *const i8).to_str().unwrap() + }; + let new = unsafe { + CStr::from_ptr(new_ptr as *const i8).to_str().unwrap() + }; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .rename_syscall(old, new) + } + } + + XSTAT_SYSCALL => { + let fd_ptr = (start_address + arg1) as *const u8; + let buf = match interface::get_statdatastruct(Arg::from_u64_as_cbuf(start_address + arg2)) { + Ok(val) => val, + Err(errno) => { + return errno; + } + }; + + let fd = unsafe { + CStr::from_ptr(fd_ptr as *const i8).to_str().unwrap() + }; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .stat_syscall(fd, buf) + } + } + + MKDIR_SYSCALL => { + let fd_ptr = (start_address + arg1) as *const u8; + let mode = arg2 as u32; + + let fd= unsafe { + CStr::from_ptr(fd_ptr as *const i8).to_str().unwrap() + }; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .mkdir_syscall(fd, mode) + } + } + + RMDIR_SYSCALL => { + let fd_ptr = (start_address + arg1) as *const u8; + + let fd= unsafe { + CStr::from_ptr(fd_ptr as *const i8).to_str().unwrap() + }; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .rmdir_syscall(fd) + } + } + + FCHDIR_SYSCALL => { + let fd = arg1 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .fchdir_syscall(fd) + } + } + + GETCWD_SYSCALL => { + let buf = (start_address + arg1) as *mut u8; + let bufsize = arg2 as u32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .getcwd_syscall(buf, bufsize) + } + } + + FSTATFS_SYSCALL => { + let fd = arg1 as i32; + let buf = interface::get_fsdatastruct(Arg::from_u64_as_cbuf(start_address + arg2)).unwrap(); + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .fstatfs_syscall(fd, buf) + } + } + + CHMOD_SYSCALL => { + let fd_ptr = (start_address + arg1) as *const u8; + + let fd= unsafe { + CStr::from_ptr(fd_ptr as *const i8).to_str().unwrap() + }; + + let mode = arg2 as u32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .chmod_syscall(fd, mode) + } + } + + //testing + DUP_SYSCALL => { + let fd = arg1 as i32; + let fd2: Option = if arg1 <= i32::MAX as u64 { + Some(arg1 as i32) + } else { + None + }; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .dup_syscall(fd, fd2) + } + } + + DUP2_SYSCALL => { + let fd = arg1 as i32; + let fd2 = arg2 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .dup2_syscall(fd, fd2) + } + } + + FCHMOD_SYSCALL => { + let fd = arg1 as i32; + let mode = arg2 as u32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .fchmod_syscall(fd, mode) + } + } + + FXSTAT_SYSCALL => { + let fd = arg1 as i32; + let buf = interface::get_statdatastruct(Arg::from_u64_as_cbuf(start_address + arg2)).unwrap(); + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .fstat_syscall(fd, buf) + } + } + // FUTEX_SYSCALL => { // let uaddr = (start_address + arg1) as u64; // let futex_op = arg2 as u32; @@ -587,7 +775,7 @@ pub extern "C" fn lind_syscall_api( _ => -1, // Return -1 for unknown syscalls }; - // println!("Lind returns: {}", ret); + println!("Lind returns: {}", ret); ret } From fb549dfd266ae3bf311375204ab4e77b26a9bcdd Mon Sep 17 00:00:00 2001 From: qianxichen233 Date: Sun, 22 Sep 2024 20:12:25 +0000 Subject: [PATCH 04/37] clone impl inside rawposix --- Cargo.toml | 5 +- mod.rs | 1 + src/interface/comm.rs | 2 +- src/interface/types.rs | 28 ++ src/lib.rs | 348 ++++++++++++++++++++++++ src/safeposix/dispatcher.rs | 41 ++- src/safeposix/syscalls/sys_calls.rs | 74 ++++- src/safeposix/syscalls/sys_constants.rs | 30 ++ 8 files changed, 507 insertions(+), 22 deletions(-) create mode 100644 mod.rs diff --git a/Cargo.toml b/Cargo.toml index b6ce22a6..e39dc56d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "rustposix" +name = "rawposix" version = "0.1.0" authors = ["Nicholas Smith Renner ", "Jonathan Eli Singer ", "Tristan J. Brigham "] edition = "2018" @@ -24,6 +24,9 @@ ringbuf = "0.2.6" dashmap = { version = "5.1", features=["serde"] } parking_lot = "0.12" bit-set = "0.5" +wasmtime = { workspace = true, features = ['threads'] } +wasmtime-environ = { workspace = true } +wasmtime-lind = { path = "../lind" } [dependencies.lazy_static] version = "1.0" diff --git a/mod.rs b/mod.rs new file mode 100644 index 00000000..c60946ee --- /dev/null +++ b/mod.rs @@ -0,0 +1 @@ +pub mod rawposix; \ No newline at end of file diff --git a/src/interface/comm.rs b/src/interface/comm.rs index 75395936..f908e556 100644 --- a/src/interface/comm.rs +++ b/src/interface/comm.rs @@ -641,4 +641,4 @@ pub fn kernel_select( }; return result; -} +} \ No newline at end of file diff --git a/src/interface/types.rs b/src/interface/types.rs index 0cb7ef55..e89b20a3 100644 --- a/src/interface/types.rs +++ b/src/interface/types.rs @@ -229,6 +229,7 @@ pub union Arg { // pub dispatch_structsem: *mut sem_t, // pub dispatch_ifaddrs: *mut ifaddrs, pub dispatch_constiovecstruct: *const interface::IovecStruct, + pub dispatch_cloneargs: *mut interface::CloneArgStruct } use std::mem::size_of; @@ -241,6 +242,22 @@ pub struct ClippedDirent { pub d_reclen: u16, } +#[derive(Copy, Clone, Default, Debug)] +#[repr(C)] +pub struct CloneArgStruct { + pub flags: u64, // Flags that control the behavior of the child process + pub pidfd: u64, // File descriptor to receive the child's PID + pub child_tid: u64, // Pointer to a memory location where the child TID will be stored + pub parent_tid: u64, // Pointer to a memory location where the parent's TID will be stored + pub exit_signal: u64, // Signal to be sent when the child process exits + pub stack: u64, // Address of the stack for the child process + pub stack_size: u64, // Size of the stack for the child process + pub tls: u64, // Thread-Local Storage (TLS) descriptor for the child thread + pub set_tid: u64, // Pointer to an array of TIDs to be set in the child + pub set_tid_size: u64, // Number of TIDs in the `set_tid` array + pub cgroup: u64, // File descriptor for the cgroup to which the child process should be attached +} + pub const CLIPPED_DIRENT_SIZE: u32 = size_of::() as u32; pub fn get_int(union_argument: Arg) -> Result { @@ -478,6 +495,17 @@ pub fn get_ioctlptrunion<'a>(union_argument: Arg) -> Result<&'a mut u8, i32> { )); } +pub fn get_cloneargs<'a>(clone_args: Arg) -> Result<&'a mut CloneArgStruct, i32> { + let pointer = unsafe { clone_args.dispatch_cloneargs }; + if !pointer.is_null() { + return Ok(unsafe { &mut *pointer }); + } + return Err(syscall_error( + Errno::EFAULT, + "dispatcher", + "input data not valid", + )); +} // pub fn get_ioctlptrunion(union_argument: Arg) -> Result { // return Ok(unsafe { union_argument.dispatch_ioctlptrunion }); diff --git a/src/lib.rs b/src/lib.rs index f2688510..ba499204 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ #![feature(thread_local)] #![allow(unused_imports)] #![feature(hash_extract_if)] +#![allow(dead_code)] // interface and safeposix are public because otherwise there isn't a great // way to 'use' them for benchmarking. @@ -12,3 +13,350 @@ pub mod interface; pub mod safeposix; pub mod tests; pub mod example_grates; + +extern crate libc; +pub mod librawposix { + use libc::{ + c_char, c_void, fd_set, itimerval, off_t, rlimit, sockaddr, socklen_t, ssize_t, statfs, + timespec, timeval, + }; + + pub const LIND_SAFE_FS_ACCESS: i32 = 2; + pub const LIND_SAFE_FS_UNLINK: i32 = 4; + pub const LIND_SAFE_FS_LINK: i32 = 5; + pub const LIND_SAFE_FS_RENAME: i32 = 6; + + pub const LIND_SAFE_FS_XSTAT: i32 = 9; + pub const LIND_SAFE_FS_OPEN: i32 = 10; + pub const LIND_SAFE_FS_CLOSE: i32 = 11; + pub const LIND_SAFE_FS_READ: i32 = 12; + pub const LIND_SAFE_FS_WRITE: i32 = 13; + pub const LIND_SAFE_FS_LSEEK: i32 = 14; + pub const LIND_SAFE_FS_IOCTL: i32 = 15; + pub const LIND_SAFE_FS_TRUNCATE: i32 = 16; + pub const LIND_SAFE_FS_FXSTAT: i32 = 17; + pub const LIND_SAFE_FS_FTRUNCATE: i32 = 18; + pub const LIND_SAFE_FS_FSTATFS: i32 = 19; + pub const LIND_SAFE_FS_MMAP: i32 = 21; + pub const LIND_SAFE_FS_MUNMAP: i32 = 22; + pub const LIND_SAFE_FS_GETDENTS: i32 = 23; + pub const LIND_SAFE_FS_DUP: i32 = 24; + pub const LIND_SAFE_FS_DUP2: i32 = 25; + pub const LIND_SAFE_FS_STATFS: i32 = 26; + pub const LIND_SAFE_FS_FCNTL: i32 = 28; + + pub const LIND_SAFE_SYS_GETPPID: i32 = 29; + pub const LIND_SAFE_SYS_EXIT: i32 = 30; + pub const LIND_SAFE_SYS_GETPID: i32 = 31; + + pub const LIND_SAFE_NET_BIND: i32 = 33; + pub const LIND_SAFE_NET_SEND: i32 = 34; + pub const LIND_SAFE_NET_SENDTO: i32 = 35; + pub const LIND_SAFE_NET_RECV: i32 = 36; + pub const LIND_SAFE_NET_RECVFROM: i32 = 37; + pub const LIND_SAFE_NET_CONNECT: i32 = 38; + pub const LIND_SAFE_NET_LISTEN: i32 = 39; + pub const LIND_SAFE_NET_ACCEPT: i32 = 40; + + pub const LIND_SAFE_NET_GETSOCKOPT: i32 = 43; + pub const LIND_SAFE_NET_SETSOCKOPT: i32 = 44; + pub const LIND_SAFE_NET_SHUTDOWN: i32 = 45; + pub const LIND_SAFE_NET_SELECT: i32 = 46; + pub const LIND_SAFE_FS_GETCWD: i32 = 47; + pub const LIND_SAFE_NET_POLL: i32 = 48; + pub const LIND_SAFE_NET_SOCKETPAIR: i32 = 49; + pub const LIND_SAFE_SYS_GETUID: i32 = 50; + pub const LIND_SAFE_SYS_GETEUID: i32 = 51; + pub const LIND_SAFE_SYS_GETGID: i32 = 52; + pub const LIND_SAFE_SYS_GETEGID: i32 = 53; + pub const LIND_SAFE_FS_FLOCK: i32 = 54; + + pub const LIND_SAFE_NET_EPOLL_CREATE: i32 = 56; + pub const LIND_SAFE_NET_EPOLL_CTL: i32 = 57; + pub const LIND_SAFE_NET_EPOLL_WAIT: i32 = 58; + + pub const LIND_SAFE_FS_SHMGET: i32 = 62; + pub const LIND_SAFE_FS_SHMAT: i32 = 63; + pub const LIND_SAFE_FS_SHMDT: i32 = 64; + pub const LIND_SAFE_FS_SHMCTL: i32 = 65; + + pub const LIND_SAFE_FS_PIPE: i32 = 66; + pub const LIND_SAFE_FS_PIPE2: i32 = 67; + pub const LIND_SAFE_FS_FORK: i32 = 68; + pub const LIND_SAFE_FS_EXEC: i32 = 69; + + pub const LIND_SAFE_MUTEX_CREATE: i32 = 70; + pub const LIND_SAFE_MUTEX_DESTROY: i32 = 71; + pub const LIND_SAFE_MUTEX_LOCK: i32 = 72; + pub const LIND_SAFE_MUTEX_TRYLOCK: i32 = 73; + pub const LIND_SAFE_MUTEX_UNLOCK: i32 = 74; + pub const LIND_SAFE_COND_CREATE: i32 = 75; + pub const LIND_SAFE_COND_DESTROY: i32 = 76; + pub const LIND_SAFE_COND_WAIT: i32 = 77; + pub const LIND_SAFE_COND_BROADCAST: i32 = 78; + pub const LIND_SAFE_COND_SIGNAL: i32 = 79; + pub const LIND_SAFE_COND_TIMEDWAIT: i32 = 80; + + pub const LIND_SAFE_SEM_INIT: i32 = 91; + pub const LIND_SAFE_SEM_WAIT: i32 = 92; + pub const LIND_SAFE_SEM_TRYWAIT: i32 = 93; + pub const LIND_SAFE_SEM_TIMEDWAIT: i32 = 94; + pub const LIND_SAFE_SEM_POST: i32 = 95; + pub const LIND_SAFE_SEM_DESTROY: i32 = 96; + pub const LIND_SAFE_SEM_GETVALUE: i32 = 97; + + pub const LIND_SAFE_NET_GETHOSTNAME: i32 = 125; + + pub const LIND_SAFE_FS_PREAD: i32 = 126; + pub const LIND_SAFE_FS_PWRITE: i32 = 127; + pub const LIND_SAFE_FS_CHDIR: i32 = 130; + pub const LIND_SAFE_FS_MKDIR: i32 = 131; + pub const LIND_SAFE_FS_RMDIR: i32 = 132; + pub const LIND_SAFE_FS_CHMOD: i32 = 133; + pub const LIND_SAFE_FS_FCHMOD: i32 = 134; + + pub const LIND_SAFE_NET_SOCKET: i32 = 136; + + pub const LIND_SAFE_NET_GETSOCKNAME: i32 = 144; + pub const LIND_SAFE_NET_GETPEERNAME: i32 = 145; + pub const LIND_SAFE_NET_GETIFADDRS: i32 = 146; + pub const LIND_SAFE_SYS_SIGACTION: i32 = 147; + pub const LIND_SAFE_SYS_KILL: i32 = 148; + pub const LIND_SAFE_SYS_SIGPROCMASK: i32 = 149; + pub const LIND_SAFE_SYS_LINDSETITIMER: i32 = 150; + + pub const LIND_SAFE_FS_FCHDIR: i32 = 161; + pub const LIND_SAFE_FS_FSYNC: i32 = 162; + pub const LIND_SAFE_FS_FDATASYNC: i32 = 163; + pub const LIND_SAFE_FS_SYNC_FILE_RANGE: i32 = 164; + + #[repr(C)] + pub union RustArg { + pub dispatch_int: i32, + pub dispatch_uint: u32, + pub dispatch_intptr: *mut i32, + pub dispatch_ulong: u64, + pub dispatch_ulong_long: u64, + pub dispatch_long: i64, + pub dispatch_size_t: usize, + pub dispatch_ssize_t: ssize_t, + pub dispatch_off_t: off_t, + pub dispatch_socklen_t: socklen_t, + pub dispatch_socklen_t_ptr: *mut socklen_t, + pub dispatch_cbuf: *const c_void, + pub dispatch_mutcbuf: *mut c_void, + pub dispatch_cstr: *const c_char, + pub dispatch_cstrarr: *const *const c_char, + pub strdispatch_rlimitstruct: *mut rlimit, // Standard + // pub dispatch_statstruct: *mut lind_stat, // Needs manual definition + pub dispatch_statfsstruct: *mut statfs, // Standard + pub dispatch_timevalstruct: *mut timeval, // Standard + pub dispatch_timespecstruct: *mut timespec, // Standard + pub dispatch_sockaddrstruct: *mut sockaddr, // Standard + // pub dispatch_epolleventstruct: *mut epoll_event, // Standard + pub dispatch_constsockaddrstruct: *mut sockaddr, // Standard + // pub dispatch_shmidstruct: *mut lind_shmid_ds, // Needs manual definition + pub dispatch_pipearray: *mut i32, + // pub dispatch_naclabisigactionstruct: *mut nacl_abi_sigaction, // Needs manual definition + // pub dispatch_constnaclabisigactionstruct: *const nacl_abi_sigaction, // Needs manual definition + // pub dispatch_naclsigset: *mut u64, // Use u64 in Rust + // pub dispatch_constnaclsigset: *const u64, // Use u64 in Rust + pub dispatch_structitimerval: *mut itimerval, // Standard + pub dispatch_conststructitimerval: *const itimerval, // Standard + pub fdset: *mut fd_set, + } + + pub const BLANKARG: RustArg = RustArg { dispatch_ulong: 0 }; + + #[link(name = "rawposix")] + extern "C" { + pub(crate) fn dispatcher( + cageid: u64, + callnum: i32, + arg1: RustArg, + arg2: RustArg, + arg3: RustArg, + arg4: RustArg, + arg5: RustArg, + arg6: RustArg, + ); + + pub(crate) fn lindrustinit(verbosity: i32); + + pub(crate) fn lindrustfinalize(); + + pub(crate) fn quick_write(fd: i32, buf: *const u8, count: usize, cageid: u64); + + pub(crate) fn rustposix_thread_init(cageid: u64, signalflag: u64); + + pub(crate) fn lind_syscall_api( + cageid: u64, + call_number: u32, + call_name: u64, + start_address: u64, + arg1: u64, + arg2: u64, + arg3: u64, + arg4: u64, + arg5: u64, + arg6: u64, + ) -> u32; + } +} + +use std::sync::{Condvar, Mutex}; + +use wasmtime::Caller; +use wasmtime_lind::LindHost; + +// use crate::librawposix::*; +use crate::safeposix::dispatcher::*; + +#[macro_export] +macro_rules! dispatch { + ($cageid:expr, $callnum:expr) => { + dispatcher( + $cageid, $callnum, BLANKARG, BLANKARG, BLANKARG, BLANKARG, BLANKARG, BLANKARG, + ) + }; + ($cageid:expr, $callnum:expr, $arg1:expr) => { + dispatcher( + $cageid, $callnum, $arg1, BLANKARG, BLANKARG, BLANKARG, BLANKARG, BLANKARG, + ) + }; + ($cageid:expr, $callnum:expr, $arg1:expr, $arg2:expr) => { + dispatcher( + $cageid, $callnum, $arg1, $arg2, BLANKARG, BLANKARG, BLANKARG, BLANKARG, + ) + }; + ($cageid:expr, $callnum:expr, $arg1:expr, $arg2:expr, $arg3:expr) => { + dispatcher( + $cageid, $callnum, $arg1, $arg2, $arg3, BLANKARG, BLANKARG, BLANKARG, + ) + }; + ($cageid:expr, $callnum:expr, $arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr) => { + dispatcher( + $cageid, $callnum, $arg1, $arg2, $arg3, $arg4, BLANKARG, BLANKARG, + ) + }; + ($cageid:expr, $callnum:expr, $arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr, $arg5:expr) => { + dispatcher( + $cageid, $callnum, $arg1, $arg2, $arg3, $arg4, $arg5, BLANKARG, + ) + }; + ($cageid:expr, $callnum:expr, $arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr, $arg5:expr, $arg6:expr) => { + dispatcher($cageid, $callnum, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6) + }; +} + +pub fn lind_lindrustinit(verbosity: i32) { + unsafe { + lindrustinit(verbosity as isize); + } +} + +pub fn lind_lindrustfinalize() { + unsafe { + lindrustfinalize(); + } +} + +pub fn lind_rustposix_thread_init(cageid: u64, signalflag: u64) { + unsafe { + rustposix_thread_init(cageid, signalflag); + } +} + +pub fn lind_write_inner(fd: i32, buf: *const u8, count: usize, cageid: u64) { + unsafe { + quick_write(fd, buf, count, cageid); + // dispatch!( + // cageid, + // crate::librawposix::LIND_SAFE_FS_WRITE, + // RustArg { dispatch_int: fd }, + // RustArg { dispatch_cbuf: buf }, + // RustArg { + // dispatch_size_t: count + // } + // ) + } +} + +// pub fn lind_fork(parent_cageid: u64, child_cageid: u64) -> i32 { +// unsafe { +// lind_syscall_api( +// parent_cageid, +// 68 as u32, +// 0, +// 0, +// child_cageid, +// 0, +// 0, +// 0, +// 0, +// 0, +// ) +// } +// } + +// pub fn lind_exit(cageid: u64, status: i32) -> i32 { +// unsafe { +// lind_syscall_api( +// cageid, +// 30 as u32, +// 0, +// 0, +// status as u64, +// 0, +// 0, +// 0, +// 0, +// 0, +// ) +// } +// } + +// pub fn lind_exec(parent_cageid: u64, child_cageid: u64) -> i32 { +// unsafe { +// lind_syscall_api( +// parent_cageid, +// 69 as u32, +// 0, +// 0, +// child_cageid, +// 0, +// 0, +// 0, +// 0, +// 0, +// ) +// } +// } + +pub fn lind_syscall_inner + Clone + Send + 'static + std::marker::Sync, U: Clone + Send + 'static + std::marker::Sync>( + cageid: u64, + call_number: u32, + call_name: u64, + caller: &mut Caller<'_, T>, + arg1: u64, + arg2: u64, + arg3: u64, + arg4: u64, + arg5: u64, + arg6: u64, +) -> i32 { + unsafe { + lind_syscall_api( + cageid, + call_number, + call_name, + caller, + arg1, + arg2, + arg3, + arg4, + arg5, + arg6, + ) + } +} diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index ab74fbfe..9275477d 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -84,6 +84,7 @@ const SEM_TIMEDWAIT_SYSCALL: i32 = 94; const SEM_POST_SYSCALL: i32 = 95; const SEM_DESTROY_SYSCALL: i32 = 96; const SEM_GETVALUE_SYSCALL: i32 = 97; +const FUTEX_SYSCALL: i32 = 98; const GETHOSTNAME_SYSCALL: i32 = 125; const PREAD_SYSCALL: i32 = 126; @@ -112,8 +113,13 @@ const SYNC_FILE_RANGE: i32 = 164; const WRITEV_SYSCALL: i32 = 170; +const CLONE_SYSCALL: i32 = 171; + use std::collections::HashMap; +use wasmtime::Caller; +use wasmtime_lind::{get_memory_base, LindHost}; + use super::cage::*; use super::syscalls::kernel_close; // use crate::example_grates::vanillaglobal::*; @@ -132,7 +138,7 @@ use std::io; // use super::shm::SHM_METADATA; // use super::syscalls::{fs_constants::IPC_STAT, sys_constants::*}; use crate::interface::types::SockaddrDummy; -use crate::interface::SigactionStruct; +use crate::interface::{CloneArgStruct, SigactionStruct}; use crate::{example_grates, interface}; use crate::interface::errnos::*; // use crate::lib_fs_utils::{lind_deltree, visit_children}; @@ -254,14 +260,19 @@ impl Arg { // dispatch_sigactionstruct: value as *mut SigactionStruct, // } // } + pub fn from_u64_as_clone_args(value: u64) -> Self { + Arg { + dispatch_cloneargs: value as *mut CloneArgStruct + } + } } #[no_mangle] -pub extern "C" fn lind_syscall_api( +pub extern "C" fn lind_syscall_api + Clone + Send + 'static + std::marker::Sync, U: Clone + Send + 'static + std::marker::Sync>( cageid: u64, call_number: u32, call_name: u64, - start_address: u64, + caller: &mut Caller<'_, T>, arg1: u64, arg2: u64, arg3: u64, @@ -269,10 +280,11 @@ pub extern "C" fn lind_syscall_api( arg5: u64, arg6: u64, ) -> i32 { - let call_number = call_number as i32; + let start_address = get_memory_base(&caller); + let call_number = call_number as i32; // Print all the arguments - // println!("rawposix call_number: {}", call_number); + // println!("cage {} calls: {}", cageid, call_number); // println!("call_name: {}", call_name); // println!("start_address: {}", start_address); // println!("arg1: {}", arg1); @@ -584,6 +596,25 @@ pub extern "C" fn lind_syscall_api( } } + CLONE_SYSCALL => { + let clone_args = match interface::get_cloneargs(Arg::from_u64_as_clone_args(start_address + arg1)) { + Ok(val) => val, + Err(e) => { + return -1; + } + }; + + clone_args.child_tid = clone_args.child_tid + start_address; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .clone3_syscall(caller, clone_args) + } + } + _ => -1, // Return -1 for unknown syscalls }; diff --git a/src/safeposix/syscalls/sys_calls.rs b/src/safeposix/syscalls/sys_calls.rs index bea5f03c..98e52477 100644 --- a/src/safeposix/syscalls/sys_calls.rs +++ b/src/safeposix/syscalls/sys_calls.rs @@ -6,6 +6,7 @@ use super::net_constants::*; use super::sys_constants; use super::sys_constants::*; use crate::interface; +use crate::interface::CloneArgStruct; use crate::safeposix::cage; use crate::safeposix::cage::*; use crate::safeposix::shm::*; @@ -146,18 +147,18 @@ impl Cage { let newsigset = interface::RustHashMap::new(); if !interface::RUSTPOSIX_TESTSUITE.load(interface::RustAtomicOrdering::Relaxed) { // we don't add these for the test suite - let mainsigsetatomic = self - .sigset - .get( - &self - .main_threadid - .load(interface::RustAtomicOrdering::Relaxed), - ) - .unwrap(); - let mainsigset = interface::RustAtomicU64::new( - mainsigsetatomic.load(interface::RustAtomicOrdering::Relaxed), - ); - newsigset.insert(0, mainsigset); + // let mainsigsetatomic = self + // .sigset + // .get( + // &self + // .main_threadid + // .load(interface::RustAtomicOrdering::Relaxed), + // ) + // .unwrap(); + // let mainsigset = interface::RustAtomicU64::new( + // mainsigsetatomic.load(interface::RustAtomicOrdering::Relaxed), + // ); + // newsigset.insert(0, mainsigset); } /* @@ -297,15 +298,58 @@ impl Cage { // Trigger SIGCHLD if !interface::RUSTPOSIX_TESTSUITE.load(interface::RustAtomicOrdering::Relaxed) { // dont trigger SIGCHLD for test suite - if self.cageid != self.parent { - interface::lind_kill_from_id(self.parent, libc::SIGCHLD); - } + // if self.cageid != self.parent { + // interface::lind_kill_from_id(self.parent, libc::SIGCHLD); + // } } //fdtable will be dropped at end of dispatcher scope because of Arc status } + pub fn clone3_syscall + Clone + Send + 'static + std::marker::Sync, U: Clone + Send + 'static + std::marker::Sync> + (&self, caller: &mut wasmtime::Caller<'_, T>, args: &mut CloneArgStruct) -> i32 { + let rewind_res = match wasmtime_lind::catch_rewind(caller) { + Ok(val) => val, + Err(_) => -1 + }; + + // println!("rewind_res: {}", rewind_res); + if rewind_res >= 0 { return rewind_res; } + // get the flags + let flags = args.flags; + // if CLONE_VM is set, we are creating a new thread (i.e. pthread_create) + // otherwise, we are creating a process (i.e. fork) + let isthread = flags & (CLONE_VM as u64); + + if isthread == 0 { + // fork + let child_cageid = self.cageid + 1; + + self.fork_syscall(child_cageid); + + match wasmtime_lind::lind_fork(caller, move |status| { + let child_cage = interface::cagetable_getref(child_cageid); + child_cage.exit_syscall(status) + }) { + Ok(res) => res, + Err(e) => -1 + } + } + else { + // pthread_create + match wasmtime_lind::lind_fork_shared_memory(caller, move |status| { + // on thread exit, we do not need to call cage.exit_syscall + // but may need to call some signal stuff + // reserve the place for future support of signal + 0 + }, args.stack as i32, args.stack_size as i32, args.child_tid) { + Ok(res) => res, + Err(e) => -1 + } + } + } + pub fn getpid_syscall(&self) -> i32 { self.cageid as i32 //not sure if this is quite what we want but it's easy enough to change later } diff --git a/src/safeposix/syscalls/sys_constants.rs b/src/safeposix/syscalls/sys_constants.rs index 89feb715..4db67f62 100644 --- a/src/safeposix/syscalls/sys_constants.rs +++ b/src/safeposix/syscalls/sys_constants.rs @@ -75,3 +75,33 @@ pub const SIG_BLOCK: i32 = 0; pub const SIG_UNBLOCK: i32 = 1; pub const SIG_SETMASK: i32 = 2; pub const ITIMER_REAL: i32 = 0; + +// /* Cloning flags. */ +// pub const CSIGNAL: u64 = 0x000000ff; /* Signal mask to be sent at exit. */ +// pub const CLONE_VM: u64 = 0x00000100; /* Set if VM shared between processes. */ +// pub const CLONE_FS: u64 = 0x00000200; /* Set if fs info shared between processes. */ +// pub const CLONE_FILES: u64 = 0x00000400; /* Set if open files shared between processes. */ +// pub const CLONE_SIGHAND: u64 = 0x00000800; /* Set if signal handlers shared. */ +// pub const CLONE_PIDFD: u64 = 0x00001000; /* Set if a pidfd should be placed in parent. */ +// pub const CLONE_PTRACE: u64 = 0x00002000; /* Set if tracing continues on the child. */ +// pub const CLONE_VFORK: u64 = 0x00004000; /* Set if the parent wants the child to wake it up on mm_release. */ +// pub const CLONE_PARENT: u64 = 0x00008000; /* Set if we want to have the same parent as the cloner. */ +// pub const CLONE_THREAD: u64 = 0x00010000; /* Set to add to same thread group. */ +// pub const CLONE_NEWNS: u64 = 0x00020000; /* Set to create new namespace. */ +// pub const CLONE_SYSVSEM: u64 = 0x00040000; /* Set to shared SVID SEM_UNDO semantics. */ +// pub const CLONE_SETTLS: u64 = 0x00080000; /* Set TLS info. */ +// pub const CLONE_PARENT_SETTID: u64 = 0x00100000; /* Store TID in userlevel buffer before MM copy. */ +// pub const CLONE_CHILD_CLEARTID: u64 = 0x00200000; /* Register exit futex and memory location to clear. */ +// pub const CLONE_DETACHED: u64 = 0x00400000; /* Create clone detached. */ +// pub const CLONE_UNTRACED: u64 = 0x00800000; /* Set if the tracing process can't force CLONE_PTRACE on this clone. */ +// pub const CLONE_CHILD_SETTID: u64 = 0x01000000; /* Store TID in userlevel buffer in the child. */ +// pub const CLONE_NEWCGROUP: u64 = 0x02000000; /* New cgroup namespace. */ +// pub const CLONE_NEWUTS: u64 = 0x04000000; /* New utsname group. */ +// pub const CLONE_NEWIPC: u64 = 0x08000000; /* New ipcs. */ +// pub const CLONE_NEWUSER: u64 = 0x10000000; /* New user namespace. */ +// pub const CLONE_NEWPID: u64 = 0x20000000; /* New pid namespace. */ +// pub const CLONE_NEWNET: u64 = 0x40000000; /* New network namespace. */ +// pub const CLONE_IO: u64 = 0x80000000; /* Clone I/O context. */ +// /* cloning flags intersect with CSIGNAL so can be used only with unshare and +// clone3 syscalls. */ +// pub const CLONE_NEWTIME: u64 = 0x00000080; /* New time namespace */ \ No newline at end of file From 48f34e7033dd3148c2d7a2a3d42f320472bf9f7d Mon Sep 17 00:00:00 2001 From: qianxichen233 Date: Mon, 23 Sep 2024 19:26:34 +0000 Subject: [PATCH 05/37] moved clone impl into wasmtime --- Cargo.toml | 3 - src/lib.rs | 113 ++++++++++++++-------------- src/safeposix/dispatcher.rs | 42 +++++------ src/safeposix/syscalls/sys_calls.rs | 80 ++++++++++---------- 4 files changed, 116 insertions(+), 122 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e39dc56d..2ddc5ae8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,9 +24,6 @@ ringbuf = "0.2.6" dashmap = { version = "5.1", features=["serde"] } parking_lot = "0.12" bit-set = "0.5" -wasmtime = { workspace = true, features = ['threads'] } -wasmtime-environ = { workspace = true } -wasmtime-lind = { path = "../lind" } [dependencies.lazy_static] version = "1.0" diff --git a/src/lib.rs b/src/lib.rs index ba499204..4c62d8c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -130,6 +130,8 @@ pub mod librawposix { pub const LIND_SAFE_FS_FDATASYNC: i32 = 163; pub const LIND_SAFE_FS_SYNC_FILE_RANGE: i32 = 164; + pub const LIND_SAFE_SYS_CLONE: i32 = 171; + #[repr(C)] pub union RustArg { pub dispatch_int: i32, @@ -206,9 +208,6 @@ pub mod librawposix { use std::sync::{Condvar, Mutex}; -use wasmtime::Caller; -use wasmtime_lind::LindHost; - // use crate::librawposix::*; use crate::safeposix::dispatcher::*; @@ -282,62 +281,62 @@ pub fn lind_write_inner(fd: i32, buf: *const u8, count: usize, cageid: u64) { } } -// pub fn lind_fork(parent_cageid: u64, child_cageid: u64) -> i32 { -// unsafe { -// lind_syscall_api( -// parent_cageid, -// 68 as u32, -// 0, -// 0, -// child_cageid, -// 0, -// 0, -// 0, -// 0, -// 0, -// ) -// } -// } - -// pub fn lind_exit(cageid: u64, status: i32) -> i32 { -// unsafe { -// lind_syscall_api( -// cageid, -// 30 as u32, -// 0, -// 0, -// status as u64, -// 0, -// 0, -// 0, -// 0, -// 0, -// ) -// } -// } - -// pub fn lind_exec(parent_cageid: u64, child_cageid: u64) -> i32 { -// unsafe { -// lind_syscall_api( -// parent_cageid, -// 69 as u32, -// 0, -// 0, -// child_cageid, -// 0, -// 0, -// 0, -// 0, -// 0, -// ) -// } -// } - -pub fn lind_syscall_inner + Clone + Send + 'static + std::marker::Sync, U: Clone + Send + 'static + std::marker::Sync>( +pub fn lind_fork(parent_cageid: u64, child_cageid: u64) -> i32 { + unsafe { + lind_syscall_api( + parent_cageid, + 68 as u32, + 0, + 0, + child_cageid, + 0, + 0, + 0, + 0, + 0, + ) + } +} + +pub fn lind_exit(cageid: u64, status: i32) -> i32 { + unsafe { + lind_syscall_api( + cageid, + 30 as u32, + 0, + 0, + status as u64, + 0, + 0, + 0, + 0, + 0, + ) + } +} + +pub fn lind_exec(parent_cageid: u64, child_cageid: u64) -> i32 { + unsafe { + lind_syscall_api( + parent_cageid, + 69 as u32, + 0, + 0, + child_cageid, + 0, + 0, + 0, + 0, + 0, + ) + } +} + +pub fn lind_syscall_inner( cageid: u64, call_number: u32, call_name: u64, - caller: &mut Caller<'_, T>, + start_address: u64, arg1: u64, arg2: u64, arg3: u64, @@ -350,7 +349,7 @@ pub fn lind_syscall_inner + Clone + Send + 'static + std::mark cageid, call_number, call_name, - caller, + start_address, arg1, arg2, arg3, diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index 9275477d..b6bfee3a 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -117,8 +117,8 @@ const CLONE_SYSCALL: i32 = 171; use std::collections::HashMap; -use wasmtime::Caller; -use wasmtime_lind::{get_memory_base, LindHost}; +// use wasmtime::Caller; +// use wasmtime_lind::{get_memory_base, LindHost}; use super::cage::*; use super::syscalls::kernel_close; @@ -268,11 +268,11 @@ impl Arg { } #[no_mangle] -pub extern "C" fn lind_syscall_api + Clone + Send + 'static + std::marker::Sync, U: Clone + Send + 'static + std::marker::Sync>( +pub extern "C" fn lind_syscall_api( cageid: u64, call_number: u32, call_name: u64, - caller: &mut Caller<'_, T>, + start_address: u64, arg1: u64, arg2: u64, arg3: u64, @@ -280,8 +280,6 @@ pub extern "C" fn lind_syscall_api + Clone + Send + 'static + arg5: u64, arg6: u64, ) -> i32 { - let start_address = get_memory_base(&caller); - let call_number = call_number as i32; // Print all the arguments // println!("cage {} calls: {}", cageid, call_number); @@ -596,24 +594,24 @@ pub extern "C" fn lind_syscall_api + Clone + Send + 'static + } } - CLONE_SYSCALL => { - let clone_args = match interface::get_cloneargs(Arg::from_u64_as_clone_args(start_address + arg1)) { - Ok(val) => val, - Err(e) => { - return -1; - } - }; + // CLONE_SYSCALL => { + // let clone_args = match interface::get_cloneargs(Arg::from_u64_as_clone_args(start_address + arg1)) { + // Ok(val) => val, + // Err(e) => { + // return -1; + // } + // }; - clone_args.child_tid = clone_args.child_tid + start_address; + // clone_args.child_tid = clone_args.child_tid + start_address; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .clone3_syscall(caller, clone_args) - } - } + // interface::check_cageid(cageid); + // unsafe { + // CAGE_TABLE[cageid as usize] + // .as_ref() + // .unwrap() + // .clone3_syscall(caller, clone_args) + // } + // } _ => -1, // Return -1 for unknown syscalls }; diff --git a/src/safeposix/syscalls/sys_calls.rs b/src/safeposix/syscalls/sys_calls.rs index 98e52477..6c9fd36a 100644 --- a/src/safeposix/syscalls/sys_calls.rs +++ b/src/safeposix/syscalls/sys_calls.rs @@ -307,48 +307,48 @@ impl Cage { status } - pub fn clone3_syscall + Clone + Send + 'static + std::marker::Sync, U: Clone + Send + 'static + std::marker::Sync> - (&self, caller: &mut wasmtime::Caller<'_, T>, args: &mut CloneArgStruct) -> i32 { - let rewind_res = match wasmtime_lind::catch_rewind(caller) { - Ok(val) => val, - Err(_) => -1 - }; - - // println!("rewind_res: {}", rewind_res); - if rewind_res >= 0 { return rewind_res; } - // get the flags - let flags = args.flags; - // if CLONE_VM is set, we are creating a new thread (i.e. pthread_create) - // otherwise, we are creating a process (i.e. fork) - let isthread = flags & (CLONE_VM as u64); - - if isthread == 0 { - // fork - let child_cageid = self.cageid + 1; + // pub fn clone3_syscall + Clone + Send + 'static + std::marker::Sync, U: Clone + Send + 'static + std::marker::Sync> + // (&self, caller: &mut wasmtime::Caller<'_, T>, args: &mut CloneArgStruct) -> i32 { + // let rewind_res = match wasmtime_lind::catch_rewind(caller) { + // Ok(val) => val, + // Err(_) => -1 + // }; + + // // println!("rewind_res: {}", rewind_res); + // if rewind_res >= 0 { return rewind_res; } + // // get the flags + // let flags = args.flags; + // // if CLONE_VM is set, we are creating a new thread (i.e. pthread_create) + // // otherwise, we are creating a process (i.e. fork) + // let isthread = flags & (CLONE_VM as u64); + + // if isthread == 0 { + // // fork + // let child_cageid = self.cageid + 1; - self.fork_syscall(child_cageid); + // self.fork_syscall(child_cageid); - match wasmtime_lind::lind_fork(caller, move |status| { - let child_cage = interface::cagetable_getref(child_cageid); - child_cage.exit_syscall(status) - }) { - Ok(res) => res, - Err(e) => -1 - } - } - else { - // pthread_create - match wasmtime_lind::lind_fork_shared_memory(caller, move |status| { - // on thread exit, we do not need to call cage.exit_syscall - // but may need to call some signal stuff - // reserve the place for future support of signal - 0 - }, args.stack as i32, args.stack_size as i32, args.child_tid) { - Ok(res) => res, - Err(e) => -1 - } - } - } + // match wasmtime_lind::lind_fork(caller, move |status| { + // let child_cage = interface::cagetable_getref(child_cageid); + // child_cage.exit_syscall(status) + // }) { + // Ok(res) => res, + // Err(e) => -1 + // } + // } + // else { + // // pthread_create + // match wasmtime_lind::lind_fork_shared_memory(caller, move |status| { + // // on thread exit, we do not need to call cage.exit_syscall + // // but may need to call some signal stuff + // // reserve the place for future support of signal + // 0 + // }, args.stack as i32, args.stack_size as i32, args.child_tid) { + // Ok(res) => res, + // Err(e) => -1 + // } + // } + // } pub fn getpid_syscall(&self) -> i32 { self.cageid as i32 //not sure if this is quite what we want but it's easy enough to change later From 9e3e03d2222231bb13a58392e22b66faf2483b0a Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Mon, 23 Sep 2024 15:26:34 -0400 Subject: [PATCH 06/37] update --- src/safeposix/dispatcher.rs | 701 ++++++++++++++++++++++++++++++++++-- 1 file changed, 676 insertions(+), 25 deletions(-) diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index 0b60b68e..4f48435b 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -27,41 +27,41 @@ const STATFS_SYSCALL: i32 = 26; const FCNTL_SYSCALL: i32 = 28; const GETPPID_SYSCALL: i32 = 29; -const EXIT_SYSCALL: i32 = 30; +const EXIT_SYSCALL: i32 = 30; const GETPID_SYSCALL: i32 = 31; -const BIND_SYSCALL: i32 = 33; -const SEND_SYSCALL: i32 = 34; -const SENDTO_SYSCALL: i32 = 35; +const BIND_SYSCALL: i32 = 33; +const SEND_SYSCALL: i32 = 34; +const SENDTO_SYSCALL: i32 = 35; //can not find in fs_calls const RECV_SYSCALL: i32 = 36; -const RECVFROM_SYSCALL: i32 = 37; +const RECVFROM_SYSCALL: i32 = 37; //can not find in fs_calls const CONNECT_SYSCALL: i32 = 38; const LISTEN_SYSCALL: i32 = 39; const ACCEPT_SYSCALL: i32 = 40; -const GETSOCKOPT_SYSCALL: i32 = 43; +const GETSOCKOPT_SYSCALL: i32 = 43; // const SETSOCKOPT_SYSCALL: i32 = 44; const SHUTDOWN_SYSCALL: i32 = 45; -const SELECT_SYSCALL: i32 = 46; -const GETCWD_SYSCALL: i32 = 47; -const POLL_SYSCALL: i32 = 48; -const SOCKETPAIR_SYSCALL: i32 = 49; -const GETUID_SYSCALL: i32 = 50; +const SELECT_SYSCALL: i32 = 46; // +const GETCWD_SYSCALL: i32 = 47; +const POLL_SYSCALL: i32 = 48; +const SOCKETPAIR_SYSCALL: i32 = 49; // +const GETUID_SYSCALL: i32 = 50; const GETEUID_SYSCALL: i32 = 51; -const GETGID_SYSCALL: i32 = 52; +const GETGID_SYSCALL: i32 = 52; const GETEGID_SYSCALL: i32 = 53; const FLOCK_SYSCALL: i32 = 54; const EPOLL_CREATE_SYSCALL: i32 = 56; -const EPOLL_CTL_SYSCALL: i32 = 57; -const EPOLL_WAIT_SYSCALL: i32 = 58; +const EPOLL_CTL_SYSCALL: i32 = 57; // +const EPOLL_WAIT_SYSCALL: i32 = 58; // -const SHMGET_SYSCALL: i32 = 62; +const SHMGET_SYSCALL: i32 = 62; const SHMAT_SYSCALL: i32 = 63; const SHMDT_SYSCALL: i32 = 64; -const SHMCTL_SYSCALL: i32 = 65; +const SHMCTL_SYSCALL: i32 = 65; // -const PIPE_SYSCALL: i32 = 66; -const PIPE2_SYSCALL: i32 = 67; +const PIPE_SYSCALL: i32 = 66; // +const PIPE2_SYSCALL: i32 = 67; // const FORK_SYSCALL: i32 = 68; const EXEC_SYSCALL: i32 = 69; @@ -71,16 +71,16 @@ const MUTEX_LOCK_SYSCALL: i32 = 72; const MUTEX_TRYLOCK_SYSCALL: i32 = 73; const MUTEX_UNLOCK_SYSCALL: i32 = 74; const COND_CREATE_SYSCALL: i32 = 75; -const COND_DESTROY_SYSCALL: i32 = 76; +const COND_DESTROY_SYSCALL: i32 = 76; const COND_WAIT_SYSCALL: i32 = 77; const COND_BROADCAST_SYSCALL: i32 = 78; const COND_SIGNAL_SYSCALL: i32 = 79; -const COND_TIMEDWAIT_SYSCALL: i32 = 80; +const COND_TIMEDWAIT_SYSCALL: i32 = 80; //other type const SEM_INIT_SYSCALL: i32 = 91; const SEM_WAIT_SYSCALL: i32 = 92; const SEM_TRYWAIT_SYSCALL: i32 = 93; -const SEM_TIMEDWAIT_SYSCALL: i32 = 94; +const SEM_TIMEDWAIT_SYSCALL: i32 = 94; // type const SEM_POST_SYSCALL: i32 = 95; const SEM_DESTROY_SYSCALL: i32 = 96; const SEM_GETVALUE_SYSCALL: i32 = 97; @@ -96,14 +96,14 @@ const FCHMOD_SYSCALL: i32 = 134; const SOCKET_SYSCALL: i32 = 136; -const GETSOCKNAME_SYSCALL: i32 = 144; -const GETPEERNAME_SYSCALL: i32 = 145; +const GETSOCKNAME_SYSCALL: i32 = 144; // +const GETPEERNAME_SYSCALL: i32 = 145; // const GETIFADDRS_SYSCALL: i32 = 146; const SIGACTION_SYSCALL: i32 = 147; const KILL_SYSCALL: i32 = 148; -const SIGPROCMASK_SYSCALL: i32 = 149; -const SETITIMER_SYSCALL: i32 = 150; +const SIGPROCMASK_SYSCALL: i32 = 149; // +const SETITIMER_SYSCALL: i32 = 150; // const FCHDIR_SYSCALL: i32 = 161; const FSYNC_SYSCALL: i32 = 162; @@ -113,6 +113,9 @@ const SYNC_FILE_RANGE: i32 = 164; const WRITEV_SYSCALL: i32 = 170; use std::collections::HashMap; +use std::hash::BuildHasherDefault; + +use libc::IPOPT_OPTVAL; use super::cage::*; use super::syscalls::kernel_close; @@ -755,6 +758,654 @@ pub extern "C" fn lind_syscall_api( } } + //adding without testing + + UNLINK_SYSCALL => { + let fd_ptr = (start_address + arg1) as *const u8; + + let fd = unsafe { + CStr::from_ptr(fd_ptr as *const i8).to_str().unwrap() + }; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .unlink_syscall(fd) + } + } + + LINK_SYSCALL => { + let old_ptr = (start_address + arg1) as *const u8; + let new_ptr = (start_address + arg1) as *const u8; + + let old_fd= unsafe { + CStr::from_ptr(old_ptr as *const i8).to_str().unwrap() + }; + let new_fd = unsafe { + CStr::from_ptr(new_ptr as *const i8).to_str().unwrap() + }; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .link_syscall(old_fd, new_fd) + } + } + + LSEEK_SYSCALL => { + let virtual_fd = arg1 as i32; + let offset = arg2 as isize; + let whence = arg3 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .lseek_syscall(virtual_fd, offset, whence) + } + } + + IOCTL_SYSCALL => { + let virtual_fd = arg1 as i32; + let request = arg2 as u64; + let ptrunion = (start_address + arg3) as *mut u8; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .ioctl_syscall(virtual_fd, request, ptrunion) + } + } + + TRUNCATE_SYSCALL => { + let fd_ptr = (start_address + arg1) as *const u8; + let length = arg2 as isize; + + let fd = unsafe { + CStr::from_ptr(fd_ptr as *const i8).to_str().unwrap() + }; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .truncate_syscall(fd, length) + } + } + + FTRUNCATE_SYSCALL => { + let virtual_fd = arg1 as i32; + let length = arg2 as isize; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .ftruncate_syscall(virtual_fd, length) + } + } + + GETDENTS_SYSCALL => { + let virtual_fd = arg1 as i32; + let buf = (start_address + arg2) as *mut u8; + let nbytes = arg3 as u32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .getdents_syscall(virtual_fd, buf, nbytes) + } + } + + STATFS_SYSCALL => { + let fd_ptr = (start_address + arg1) as *const u8; + let rposix_databuf = interface::get_fsdatastruct(Arg::from_u64_as_cbuf(start_address + arg2)).unwrap(); + + let fd = unsafe { + CStr::from_ptr(fd_ptr as *const i8).to_str().unwrap() + }; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .statfs_syscall(fd, rposix_databuf) + } + } + + FCNTL_SYSCALL => { + let virtual_fd = arg1 as i32; + let cmd = arg2 as i32; + let arg = arg3 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .fcntl_syscall(virtual_fd, cmd, arg) + } + } + + // SEND_SYSCALL => { + + // interface::check_cageid(cageid); + // unsafe { + // CAGE_TABLE[cageid as usize] + // .as_ref() + // .unwrap() + // .send_syscall(virtual_fd, cmd, arg) + // } + // } + + FLOCK_SYSCALL => { + let virtual_fd = arg1 as i32; + let operation = arg2 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .flock_syscall(virtual_fd, operation) + } + } + + SHMGET_SYSCALL => { + let key = arg1 as i32; + let size = arg2 as usize; + let shmfig = arg3 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .shmget_syscall(key, size, shmfig) + } + } + + SHMAT_SYSCALL => { + let shmid = arg1 as i32; + let shmaddr = (start_address + arg2) as *mut u8; + let shmflg = arg3 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .shmat_syscall(shmid, shmaddr, shmflg) + } + } + + SHMDT_SYSCALL => { + let shmaddr = (start_address + arg1) as *mut u8; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .shmdt_syscall(shmaddr) + } + } + + MUTEX_DESTROY_SYSCALL => { + let mutex_handle = arg1 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .mutex_destroy_syscall(mutex_handle) + } + } + + MUTEX_LOCK_SYSCALL => { + let mutex_handle = arg1 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .mutex_lock_syscall(mutex_handle) + } + } + + MUTEX_TRYLOCK_SYSCALL => { + let mutex_handle = arg1 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .mutex_trylock_syscall(mutex_handle) + } + } + + MUTEX_UNLOCK_SYSCALL => { + let mutex_handle = arg1 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .mutex_unlock_syscall(mutex_handle) + } + } + + COND_DESTROY_SYSCALL => { + let cv_handle = arg1 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .cond_destroy_syscall(cv_handle) + } + } + + COND_WAIT_SYSCALL => { + let cv_handle = arg1 as i32; + let mutex_handle = arg2 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .cond_wait_syscall(cv_handle, mutex_handle) + } + } + + COND_BROADCAST_SYSCALL => { + let cv_handle = arg1 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .cond_broadcast_syscall(cv_handle) + } + } + + COND_SIGNAL_SYSCALL => { + let cv_handle = arg1 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .cond_signal_syscall(cv_handle) + } + } + + SEM_INIT_SYSCALL => { + let sem_handle = arg1 as u32; + let pshared = arg2 as i32; + let value = arg3 as u32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .sem_init_syscall(sem_handle, pshared, value) + } + } + + SEM_WAIT_SYSCALL => { + let sem_handle = arg1 as u32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .sem_wait_syscall(sem_handle) + } + } + + SEM_TRYWAIT_SYSCALL => { + let sem_handle = arg1 as u32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .sem_trywait_syscall(sem_handle) + } + } + + SEM_POST_SYSCALL => { + let sem_handle = arg1 as u32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .sem_post_syscall(sem_handle) + } + } + + SEM_DESTROY_SYSCALL => { + let sem_handle = arg1 as u32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .sem_destroy_syscall(sem_handle) + } + } + + SEM_GETVALUE_SYSCALL => { + let sem_handle = arg1 as u32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .sem_getvalue_syscall(sem_handle) + } + } + + PWRITE_SYSCALL => { + let virtual_fd = arg1 as i32; + let buf = (start_address + arg2) as *const u8; + let count = arg3 as usize; + let offset = arg4 as i64; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .pwrite_syscall(virtual_fd, buf, count, offset) + } + } + + GETUID_SYSCALL => { + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .getuid_syscall() + } + } + + GETEUID_SYSCALL => { + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .geteuid_syscall() + } + } + + GETGID_SYSCALL => { + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .getgid_syscall() + } + } + + GETEGID_SYSCALL => { + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .getegid_syscall() + } + } + + EPOLL_CREATE_SYSCALL => { + let size = arg1 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .epoll_create_syscall(size) + } + } + + SETSOCKOPT_SYSCALL => { + let virtual_fd = arg1 as i32; + let level = arg2 as i32; + let optname = arg3 as i32; + let optval = (start_address + arg4) as *mut u8; + let optlen = arg5 as u32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .setsockopt_syscall( virtual_fd, level, optname, optval, optlen) + } + } + + SHUTDOWN_SYSCALL => { + let virtual_fd = arg1 as i32; + let how = arg2 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .shutdown_syscall( virtual_fd, how) + } + } + + GETPPID_SYSCALL => { + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .getppid_syscall() + } + } + + SEND_SYSCALL => { + let virtual_fd = arg1 as i32; + let buf = (start_address + arg4) as *const u8; + let buflen = arg3 as usize; + let flags = arg4 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .send_syscall( virtual_fd, buf, buflen, flags) + } + } + + RECV_SYSCALL => { + let virtual_fd = arg1 as i32; + let buf = (start_address + arg4) as *mut u8; + let len = arg3 as usize; + let flags = arg4 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .recv_syscall( virtual_fd, buf, len, flags) + } + } + + LISTEN_SYSCALL => { + let virtual_fd = arg1 as i32; + let backlog = arg2 as i32; + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .listen_syscall(virtual_fd, backlog) + } + } + + MUTEX_CREATE_SYSCALL => { + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .mutex_create_syscall() + } + } + + COND_CREATE_SYSCALL => { + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .cond_create_syscall() + } + } + + GETHOSTNAME_SYSCALL => { + let name = (start_address + arg1) as *mut u8; + let len = arg2 as isize; + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .gethostname_syscall(name, len) + } + } + + GETIFADDRS_SYSCALL => { + let buf = (start_address + arg1) as *mut u8; + let count = arg2 as usize; + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .getifaddrs_syscall(buf, count) + } + } + + KILL_SYSCALL => { + let cage_id = arg1 as i32; + let sig = arg2 as i32; + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .kill_syscall(cage_id, sig) + } + } + + FSYNC_SYSCALL => { + let virtual_fd = arg1 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .fsync_syscall(virtual_fd) + } + } + + FDATASYNC_SYSCALL => { + let virtual_fd = arg1 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .fdatasync_syscall(virtual_fd) + } + } + + SYNC_FILE_RANGE => { + let virtual_fd = arg1 as i32; + let offset = arg2 as isize; + let nbytes = arg3 as isize; + let flags = arg4 as u32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .sync_file_range_syscall(virtual_fd, offset, nbytes, flags) + } + } + // GETSOCKNAME_SYSCALL => { + // let name = (start_address + arg1) as *mut u8; + // let len = arg2 as isize; + // interface::check_cageid(cageid); + // unsafe { + // CAGE_TABLE[cageid as usize] + // .as_ref() + // .unwrap() + // .getsockname_syscall(name, len) + // } + // } + + // GETSOCKOPT_SYSCALL => { + // let virtual_fd = arg1 as i32; + // let level = arg2 as i32; + // let optname = arg3 as i32; + // let optval_ptr = (start_address + arg4) as *mut u8; + + // let optval = unsafe { + // CStr::from_ptr(optval_ptr as *mut i8).to_str().unwrap() + // }; + // interface::check_cageid(cageid); + // unsafe { + // CAGE_TABLE[cageid as usize] + // .as_ref() + // .unwrap() + // .getsockopt_syscall(virtual_fd, level, optname, optval) + // } + // } + // FUTEX_SYSCALL => { // let uaddr = (start_address + arg1) as u64; // let futex_op = arg2 as u32; From b80df72c529e9f38c899e5f347d66e34aef7df13 Mon Sep 17 00:00:00 2001 From: qianxichen233 Date: Tue, 24 Sep 2024 03:43:47 +0000 Subject: [PATCH 07/37] resolve conflict --- src/safeposix/dispatcher.rs | 42 ++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index b6bfee3a..4556cf22 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -518,27 +518,6 @@ pub extern "C" fn lind_syscall_api( } } - GETPID_SYSCALL => { - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .getpid_syscall() - } - } - - FORK_SYSCALL => { - let id = arg1 as u64; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .fork_syscall(id) - } - } - // WAIT_SYSCALL => { // let mut status = 0; // interface::check_cageid(cageid); @@ -613,6 +592,27 @@ pub extern "C" fn lind_syscall_api( // } // } + GETPID_SYSCALL => { + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .getpid_syscall() + } + } + + FORK_SYSCALL => { + let id = arg1 as u64; + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .fork_syscall(id) + } + } + _ => -1, // Return -1 for unknown syscalls }; From c7e46ee29ff3fa1868265b5e5567862e7cdcde1b Mon Sep 17 00:00:00 2001 From: qianxichen233 Date: Tue, 24 Sep 2024 03:48:03 +0000 Subject: [PATCH 08/37] fixed merged version --- src/safeposix/dispatcher.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index cea8a383..c5131235 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -272,6 +272,7 @@ impl Arg { #[no_mangle] pub extern "C" fn lind_syscall_api( + cageid: u64, call_number: u32, call_name: u64, start_address: u64, @@ -282,7 +283,6 @@ pub extern "C" fn lind_syscall_api( arg5: u64, arg6: u64, ) -> i32 { - let cageid = 1; let call_number = call_number as i32; // Print all the arguments // println!("cage {} calls: {}", cageid, call_number); From 9fe1d739dbdf8dc7a92242250c8d2ed084653b72 Mon Sep 17 00:00:00 2001 From: qianxichen233 Date: Tue, 24 Sep 2024 08:03:08 +0000 Subject: [PATCH 09/37] add some syscalls --- src/interface/types.rs | 2 +- src/safeposix/dispatcher.rs | 90 ++++++++++++++++++++++++++++-- src/safeposix/syscalls/fs_calls.rs | 12 ++-- 3 files changed, 92 insertions(+), 12 deletions(-) diff --git a/src/interface/types.rs b/src/interface/types.rs index e89b20a3..85e22ea0 100644 --- a/src/interface/types.rs +++ b/src/interface/types.rs @@ -76,7 +76,7 @@ pub struct Rlimit { pub rlim_max: u64, } -#[derive(Eq, PartialEq, Default, Copy, Clone)] +#[derive(Eq, PartialEq, Default, Copy, Clone, Debug)] #[repr(C)] pub struct PipeArray { pub readfd: i32, diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index c5131235..520f69e1 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -141,7 +141,7 @@ use std::io; // use super::shm::SHM_METADATA; // use super::syscalls::{fs_constants::IPC_STAT, sys_constants::*}; use crate::interface::types::SockaddrDummy; -use crate::interface::{CloneArgStruct, SigactionStruct}; +use crate::interface::{CloneArgStruct, SigactionStruct, StatData}; use crate::{example_grates, interface}; use crate::interface::errnos::*; // use crate::lib_fs_utils::{lind_deltree, visit_children}; @@ -242,6 +242,18 @@ impl Arg { } } + pub fn from_u64_as_statstruct(value: u64) -> Self { + Arg { + dispatch_statdatastruct: value as *mut interface::StatData, + } + } + + pub fn from_u64_as_pipearray(value: u64) -> Self { + Arg { + dispatch_pipearray: value as *mut interface::PipeArray, + } + } + pub fn from_u64_as_socklen_ptr(value: u64) -> Self { Arg { dispatch_socklen_t_ptr: value as *mut u32, @@ -270,6 +282,17 @@ impl Arg { } } +fn parse_null_terminated_string(ptr: *const std::os::raw::c_char) -> Result { + // Convert the pointer to a CStr, which is a reference to a null-terminated string + let c_str = unsafe { + assert!(!ptr.is_null(), "Received a null pointer"); + std::ffi::CStr::from_ptr(ptr) + }; + + // Convert the CStr to a Rust String + c_str.to_str().map(|s| s.to_owned()) +} + #[no_mangle] pub extern "C" fn lind_syscall_api( cageid: u64, @@ -284,6 +307,16 @@ pub extern "C" fn lind_syscall_api( arg6: u64, ) -> i32 { let call_number = call_number as i32; + let call_name = match call_name { + 0 => { + "unnamed" + }, + _ => { + &parse_null_terminated_string((call_name + start_address) as (*const std::os::raw::c_char)).unwrap() + } + }; + // let call_name = ; + println!("------cage {} calls {} ({})", cageid, call_name, call_number); // Print all the arguments // println!("cage {} calls: {}", cageid, call_number); // println!("call_name: {}", call_name); @@ -410,7 +443,7 @@ pub extern "C" fn lind_syscall_api( } ACCESS_SYSCALL => { - let path = match u64_to_str(arg1) { + let path = match u64_to_str(start_address + arg1) { Ok(path_str) => path_str, Err(_) => return -1, // Handle error appropriately, return an error code }; @@ -582,12 +615,17 @@ pub extern "C" fn lind_syscall_api( XSTAT_SYSCALL => { let fd_ptr = (start_address + arg1) as *const u8; - let buf = match interface::get_statdatastruct(Arg::from_u64_as_cbuf(start_address + arg2)) { + println!("arg2: {:?}", arg2); + let buf = match interface::get_statdatastruct(Arg::from_u64_as_statstruct(start_address + arg2)) { Ok(val) => val, Err(errno) => { return errno; } }; + // println!("buf: {:?}", buf); + // let buf = unsafe { &mut *((start_address + arg2) as *mut interface::StatData) }; + // buf.st_size = 114514; + // return 0; let fd = unsafe { CStr::from_ptr(fd_ptr as *const i8).to_str().unwrap() @@ -647,17 +685,32 @@ pub extern "C" fn lind_syscall_api( } } + CHDIR_SYSCALL => { + let path = u64_to_str(start_address + arg1).unwrap(); + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .chdir_syscall(path) + } + } + GETCWD_SYSCALL => { let buf = (start_address + arg1) as *mut u8; let bufsize = arg2 as u32; interface::check_cageid(cageid); - unsafe { + + let ret = unsafe { CAGE_TABLE[cageid as usize] .as_ref() .unwrap() .getcwd_syscall(buf, bufsize) - } + }; + if ret == 0 { return arg1 as i32; } + ret } FSTATFS_SYSCALL => { @@ -1365,6 +1418,31 @@ pub extern "C" fn lind_syscall_api( .sync_file_range_syscall(virtual_fd, offset, nbytes, flags) } } + + PIPE_SYSCALL => { + let pipe = interface::get_pipearray(Arg::from_u64_as_pipearray(start_address + arg1)).unwrap(); + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .pipe_syscall(pipe) + } + } + PIPE2_SYSCALL => { + let pipe = interface::get_pipearray(Arg::from_u64_as_pipearray(start_address + arg1)).unwrap(); + let flag = arg2 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .pipe2_syscall(pipe, flag) + } + } + // GETSOCKNAME_SYSCALL => { // let name = (start_address + arg1) as *mut u8; // let len = arg2 as isize; @@ -1510,7 +1588,7 @@ pub extern "C" fn lind_syscall_api( _ => -1, // Return -1 for unknown syscalls }; - println!("Lind returns: {}", ret); + println!("------cage {} calling {} returns {}", cageid, call_name, ret); ret } diff --git a/src/safeposix/syscalls/fs_calls.rs b/src/safeposix/syscalls/fs_calls.rs index 58ce6452..0fb23ebc 100644 --- a/src/safeposix/syscalls/fs_calls.rs +++ b/src/safeposix/syscalls/fs_calls.rs @@ -33,7 +33,8 @@ use crate::example_grates::dashmapvecglobal::*; // use crate::example_grates::muthashmaxglobal::*; // use crate::example_grates::dashmaparrayglobal::*; -static LIND_ROOT: &str = "/home/lind/lind_project/src/safeposix-rust/tmp"; +// static LIND_ROOT: &str = "/home/lind/lind_project/src/safeposix-rust/tmp"; +static LIND_ROOT: &str = "/home/lind-wasm/lind_fs_root"; /* * We will receive parameters with type u64 by default, then we will do type conversion inside @@ -344,6 +345,8 @@ impl Cage { rposix_statbuf.st_size = libc_statbuf.st_size as usize; rposix_statbuf.st_uid = libc_statbuf.st_uid; + println!("stat: {:?}", rposix_statbuf); + libcret } @@ -1367,7 +1370,7 @@ impl Cage { * pipe() will return 0 when sucess, -1 when fail */ pub fn pipe_syscall(&self, pipefd: &mut PipeArray) -> i32 { - let mut kernel_fds = [0; 2]; + let mut kernel_fds: [i32; 2] = [0; 2]; let ret = unsafe { libc::pipe(kernel_fds.as_mut_ptr() as *mut i32) }; if ret < 0 { @@ -1375,9 +1378,8 @@ impl Cage { return handle_errno(errno, "pipe"); } - pipefd.readfd = get_unused_virtual_fd(self.cageid, kernel_fds[0], false, 0).unwrap() as i32; - pipefd.writefd = get_unused_virtual_fd(self.cageid, kernel_fds[1], false, 0).unwrap() as i32; - + pipefd.readfd = get_unused_virtual_fd(self.cageid, kernel_fds[0] as u64, false, 0).unwrap() as i32; + pipefd.writefd = get_unused_virtual_fd(self.cageid, kernel_fds[1] as u64, false, 0).unwrap() as i32; return ret; } From adce7bdd4cb78581fc0d23969e9e8a8d451b3d14 Mon Sep 17 00:00:00 2001 From: qianxichen233 Date: Tue, 24 Sep 2024 20:57:24 +0000 Subject: [PATCH 10/37] support more syscalls for lind-wasm --- src/interface/types.rs | 5 +- src/safeposix/dispatcher.rs | 220 ++++++++++++++++++++++------ src/safeposix/syscalls/net_calls.rs | 10 +- 3 files changed, 182 insertions(+), 53 deletions(-) diff --git a/src/interface/types.rs b/src/interface/types.rs index 85e22ea0..7bd41c6b 100644 --- a/src/interface/types.rs +++ b/src/interface/types.rs @@ -83,7 +83,7 @@ pub struct PipeArray { pub writefd: i32, } -#[derive(Eq, PartialEq, Default, Copy, Clone)] +#[derive(Eq, PartialEq, Default, Copy, Clone, Debug)] #[repr(C)] pub struct SockPair { pub sock1: i32, @@ -659,7 +659,8 @@ pub fn get_sockaddr(union_argument: Arg, addrlen: u32) -> Result { + val => { + println!("val: {}", val); return Err(syscall_error( Errno::EOPNOTSUPP, "dispatcher", diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index 520f69e1..40e2a2b6 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -248,12 +248,24 @@ impl Arg { } } + pub fn from_u64_as_pollstructarray(value: u64) -> Self { + Arg { + dispatch_pollstructarray: value as *mut interface::PollStruct, + } + } + pub fn from_u64_as_pipearray(value: u64) -> Self { Arg { dispatch_pipearray: value as *mut interface::PipeArray, } } + pub fn from_u64_as_sockpair(value: u64) -> Self { + Arg { + dispatch_sockpair: value as *mut interface::SockPair, + } + } + pub fn from_u64_as_socklen_ptr(value: u64) -> Self { Arg { dispatch_socklen_t_ptr: value as *mut u32, @@ -506,7 +518,7 @@ pub extern "C" fn lind_syscall_api( BIND_SYSCALL => { let fd = arg1 as i32; let addrlen = arg3 as u32; - let addr = get_onearg!(interface::get_sockaddr(Arg::from_u64_as_sockaddrstruct(arg2), addrlen)); + let addr = interface::get_sockaddr(Arg::from_u64_as_sockaddrstruct(start_address + arg2), addrlen).unwrap(); interface::check_cageid(cageid); unsafe { let localaddr = match Ok::<&interface::GenSockaddr, i32>(&addr) { @@ -542,7 +554,7 @@ pub extern "C" fn lind_syscall_api( .accept_syscall(arg1 as i32, &mut Some(&mut addr)) }; if rv >= 0 { - interface::copy_out_sockaddr(Arg::from_u64_as_sockaddrstruct(arg2), Arg::from_u64_as_socklen_ptr(arg3), addr); + interface::copy_out_sockaddr(Arg::from_u64_as_sockaddrstruct(start_address + arg2), Arg::from_u64_as_socklen_ptr(start_address + arg3), addr); } rv } else { @@ -941,6 +953,92 @@ pub extern "C" fn lind_syscall_api( } } + RECV_SYSCALL => { + let fd = arg1 as i32; + let buf = (start_address + arg2) as *mut u8; + let buflen = arg3 as usize; + let flag = arg4 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .recv_syscall(fd, buf, buflen, flag) + } + } + + SENDTO_SYSCALL => { + let fd = arg1 as i32; + let buf = (start_address + arg2) as *const u8; + let buflen = arg3 as usize; + let flag = arg4 as i32; + + let addrlen = arg6 as u32; + let addr = interface::get_sockaddr(Arg::from_u64_as_sockaddrstruct(start_address + arg5), addrlen).unwrap(); + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .sendto_syscall(fd, buf, buflen, flag, &addr) + } + } + + RECVFROM_SYSCALL => { + let fd = arg1 as i32; + let buf = (start_address + arg2) as *mut u8; + let buflen = arg3 as usize; + let flag = arg4 as i32; + + // let addrlen = arg6 as u32; + // let mut addr = interface::get_sockaddr(Arg::from_u64_as_sockaddrstruct(start_address + arg5), addrlen).unwrap(); + + let nullity1 = interface::arg_nullity(&Arg::from_u64_as_sockaddrstruct(arg5)); + let nullity2 = interface::arg_nullity(&Arg::from_u64_as_socklen_ptr(arg6)); + + interface::check_cageid(cageid); + // unsafe { + // CAGE_TABLE[cageid as usize] + // .as_ref() + // .unwrap() + // .recvfrom_syscall(fd, buf, buflen, flag, &addr) + // }; + + if nullity1 && nullity2 { + // let addrlen = arg6 as u32; + // let mut addr = interface::get_sockaddr(Arg::from_u64_as_sockaddrstruct(start_address + arg5), addrlen).unwrap(); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .recvfrom_syscall(fd, buf, buflen, flag, &mut None) + } + } else if !(nullity1 || nullity2) { + // let addrlen = interface::get_socklen_t_ptr(Arg::from_u64_as_socklen_ptr(arg6)).unwrap(); + let mut newsockaddr = interface::GenSockaddr::V4(interface::SockaddrV4::default()); //dummy value, rust would complain if we used an uninitialized value here + + let rv = unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .recvfrom_syscall(fd, buf, buflen, flag, &mut Some(&mut newsockaddr)) + }; + + if rv >= 0 { + interface::copy_out_sockaddr(Arg::from_u64_as_sockaddrstruct(start_address + arg5), Arg::from_u64_as_socklen_ptr(start_address + arg6), newsockaddr); + } + rv + } else { + syscall_error( + Errno::EINVAL, + "recvfrom", + "exactly one of the last two arguments was zero", + ) + } + } + // SEND_SYSCALL => { // interface::check_cageid(cageid); @@ -1348,12 +1446,13 @@ pub extern "C" fn lind_syscall_api( let name = (start_address + arg1) as *mut u8; let len = arg2 as isize; interface::check_cageid(cageid); - unsafe { + let ret = unsafe { CAGE_TABLE[cageid as usize] .as_ref() .unwrap() .gethostname_syscall(name, len) - } + }; + ret } GETIFADDRS_SYSCALL => { @@ -1443,52 +1542,81 @@ pub extern "C" fn lind_syscall_api( } } - // GETSOCKNAME_SYSCALL => { - // let name = (start_address + arg1) as *mut u8; - // let len = arg2 as isize; - // interface::check_cageid(cageid); - // unsafe { - // CAGE_TABLE[cageid as usize] - // .as_ref() - // .unwrap() - // .getsockname_syscall(name, len) - // } - // } + GETSOCKNAME_SYSCALL => { + let fd = arg1 as i32; - // GETSOCKOPT_SYSCALL => { - // let virtual_fd = arg1 as i32; - // let level = arg2 as i32; - // let optname = arg3 as i32; - // let optval_ptr = (start_address + arg4) as *mut u8; + // let addrlen = arg3 as u32; + // let mut addr = interface::get_sockaddr(Arg::from_u64_as_sockaddrstruct(start_address + arg2), addrlen).unwrap(); - // let optval = unsafe { - // CStr::from_ptr(optval_ptr as *mut i8).to_str().unwrap() - // }; - // interface::check_cageid(cageid); - // unsafe { - // CAGE_TABLE[cageid as usize] - // .as_ref() - // .unwrap() - // .getsockopt_syscall(virtual_fd, level, optname, optval) - // } - // } + let mut addr = interface::GenSockaddr::V4(interface::SockaddrV4::default()); //value doesn't matter + + if interface::arg_nullity(&Arg::from_u64_as_sockaddrstruct(arg2)) || interface::arg_nullity(&Arg::from_u64_as_socklen_ptr(arg3)) { + return syscall_error( + Errno::EINVAL, + "getsockname", + "Either the address or the length were null", + ); + } - // FUTEX_SYSCALL => { - // let uaddr = (start_address + arg1) as u64; - // let futex_op = arg2 as u32; - // let val = arg3 as u32; - // let timeout = arg4 as u32; - // let uaddr2 = arg5 as u32; - // let val3 = arg6 as u32; + interface::check_cageid(cageid); + let rv = unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .getsockname_syscall(fd, &mut Some(&mut addr)) + }; - // interface::check_cageid(cageid); - // unsafe { - // CAGE_TABLE[cageid as usize] - // .as_ref() - // .unwrap() - // .clone3_syscall(caller, clone_args) - // } - // } + if rv >= 0 { + interface::copy_out_sockaddr(Arg::from_u64_as_sockaddrstruct(start_address + arg2), Arg::from_u64_as_socklen_ptr(start_address + arg3), addr); + } + rv + } + + GETSOCKOPT_SYSCALL => { + let virtual_fd = arg1 as i32; + let level = arg2 as i32; + let optname = arg3 as i32; + + let optval_ptr = (start_address + arg4) as *mut i32; + let optval = unsafe { &mut *optval_ptr }; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .getsockopt_syscall(virtual_fd, level, optname, optval) + } + } + + SOCKETPAIR_SYSCALL => { + let domain = arg1 as i32; + let _type = arg2 as i32; + let protocol = arg3 as i32; + let virtual_socket_vector = interface::get_sockpair(Arg::from_u64_as_sockpair(start_address + arg4)).unwrap(); + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .socketpair_syscall(domain, _type, protocol, virtual_socket_vector) + } + } + + POLL_SYSCALL => { + let nfds = arg2 as u64; + let pollfds = interface::get_pollstruct_slice(Arg::from_u64_as_pollstructarray(start_address + arg1), nfds as usize).unwrap(); + let timeout = arg3 as i32; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .poll_syscall(pollfds, nfds, timeout) + } + } GETPID_SYSCALL => { interface::check_cageid(cageid); diff --git a/src/safeposix/syscalls/net_calls.rs b/src/safeposix/syscalls/net_calls.rs index 1989130d..64733d0a 100644 --- a/src/safeposix/syscalls/net_calls.rs +++ b/src/safeposix/syscalls/net_calls.rs @@ -27,7 +27,7 @@ use libc::*; use std::{os::fd::RawFd, ptr}; use bit_set::BitSet; -static LIND_ROOT: &str = "/home/lind/lind_project/src/safeposix-rust/tmp/"; +static LIND_ROOT: &str = "/home/lind-wasm/lind_fs_root/"; lazy_static! { // A hashmap used to store epoll mapping relationships @@ -188,11 +188,11 @@ impl Cage { let c_str = CStr::from_ptr(sun_path_ptr); let str_slice = c_str.to_str().expect("Failed to convert CStr to str"); - println!("[bind] addr: {:?}", addr); - println!("[bind] sun_path: {}", str_slice); + // println!("[bind] addr: {:?}", addr); + // println!("[bind] sun_path: {}", str_slice); io::stdout().flush().unwrap(); } - println!("[Bind] Error message: {:?}", err_msg); + // println!("[Bind] Error message: {:?}", err_msg); io::stdout().flush().unwrap(); return handle_errno(errno, "bind"); } @@ -475,7 +475,6 @@ impl Cage { }; let ret = unsafe { libc::recvfrom(kernel_fd as i32, buf as *mut c_void, buflen, flags, finalsockaddr, &mut addrlen as *mut u32) as i32 }; - if ret < 0 { // let err = unsafe { // libc::__errno_location() @@ -1371,6 +1370,7 @@ impl Cage { let vsv_2 = get_unused_virtual_fd(self.cageid, ksv_2 as u64, false, 0).unwrap(); virtual_socket_vector.sock1 = vsv_1 as i32; virtual_socket_vector.sock2 = vsv_2 as i32; + return 0; } From 0d51b64a31b5dda97b178e5114933dd1fe94f283 Mon Sep 17 00:00:00 2001 From: qianxichen233 Date: Wed, 25 Sep 2024 17:21:14 +0000 Subject: [PATCH 11/37] fixed librawposix --- src/lib.rs | 382 ++++++++++++++++++++++++++--------------------------- 1 file changed, 191 insertions(+), 191 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4c62d8c6..a4874c25 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,197 +14,197 @@ pub mod safeposix; pub mod tests; pub mod example_grates; -extern crate libc; -pub mod librawposix { - use libc::{ - c_char, c_void, fd_set, itimerval, off_t, rlimit, sockaddr, socklen_t, ssize_t, statfs, - timespec, timeval, - }; - - pub const LIND_SAFE_FS_ACCESS: i32 = 2; - pub const LIND_SAFE_FS_UNLINK: i32 = 4; - pub const LIND_SAFE_FS_LINK: i32 = 5; - pub const LIND_SAFE_FS_RENAME: i32 = 6; - - pub const LIND_SAFE_FS_XSTAT: i32 = 9; - pub const LIND_SAFE_FS_OPEN: i32 = 10; - pub const LIND_SAFE_FS_CLOSE: i32 = 11; - pub const LIND_SAFE_FS_READ: i32 = 12; - pub const LIND_SAFE_FS_WRITE: i32 = 13; - pub const LIND_SAFE_FS_LSEEK: i32 = 14; - pub const LIND_SAFE_FS_IOCTL: i32 = 15; - pub const LIND_SAFE_FS_TRUNCATE: i32 = 16; - pub const LIND_SAFE_FS_FXSTAT: i32 = 17; - pub const LIND_SAFE_FS_FTRUNCATE: i32 = 18; - pub const LIND_SAFE_FS_FSTATFS: i32 = 19; - pub const LIND_SAFE_FS_MMAP: i32 = 21; - pub const LIND_SAFE_FS_MUNMAP: i32 = 22; - pub const LIND_SAFE_FS_GETDENTS: i32 = 23; - pub const LIND_SAFE_FS_DUP: i32 = 24; - pub const LIND_SAFE_FS_DUP2: i32 = 25; - pub const LIND_SAFE_FS_STATFS: i32 = 26; - pub const LIND_SAFE_FS_FCNTL: i32 = 28; - - pub const LIND_SAFE_SYS_GETPPID: i32 = 29; - pub const LIND_SAFE_SYS_EXIT: i32 = 30; - pub const LIND_SAFE_SYS_GETPID: i32 = 31; - - pub const LIND_SAFE_NET_BIND: i32 = 33; - pub const LIND_SAFE_NET_SEND: i32 = 34; - pub const LIND_SAFE_NET_SENDTO: i32 = 35; - pub const LIND_SAFE_NET_RECV: i32 = 36; - pub const LIND_SAFE_NET_RECVFROM: i32 = 37; - pub const LIND_SAFE_NET_CONNECT: i32 = 38; - pub const LIND_SAFE_NET_LISTEN: i32 = 39; - pub const LIND_SAFE_NET_ACCEPT: i32 = 40; - - pub const LIND_SAFE_NET_GETSOCKOPT: i32 = 43; - pub const LIND_SAFE_NET_SETSOCKOPT: i32 = 44; - pub const LIND_SAFE_NET_SHUTDOWN: i32 = 45; - pub const LIND_SAFE_NET_SELECT: i32 = 46; - pub const LIND_SAFE_FS_GETCWD: i32 = 47; - pub const LIND_SAFE_NET_POLL: i32 = 48; - pub const LIND_SAFE_NET_SOCKETPAIR: i32 = 49; - pub const LIND_SAFE_SYS_GETUID: i32 = 50; - pub const LIND_SAFE_SYS_GETEUID: i32 = 51; - pub const LIND_SAFE_SYS_GETGID: i32 = 52; - pub const LIND_SAFE_SYS_GETEGID: i32 = 53; - pub const LIND_SAFE_FS_FLOCK: i32 = 54; - - pub const LIND_SAFE_NET_EPOLL_CREATE: i32 = 56; - pub const LIND_SAFE_NET_EPOLL_CTL: i32 = 57; - pub const LIND_SAFE_NET_EPOLL_WAIT: i32 = 58; - - pub const LIND_SAFE_FS_SHMGET: i32 = 62; - pub const LIND_SAFE_FS_SHMAT: i32 = 63; - pub const LIND_SAFE_FS_SHMDT: i32 = 64; - pub const LIND_SAFE_FS_SHMCTL: i32 = 65; - - pub const LIND_SAFE_FS_PIPE: i32 = 66; - pub const LIND_SAFE_FS_PIPE2: i32 = 67; - pub const LIND_SAFE_FS_FORK: i32 = 68; - pub const LIND_SAFE_FS_EXEC: i32 = 69; - - pub const LIND_SAFE_MUTEX_CREATE: i32 = 70; - pub const LIND_SAFE_MUTEX_DESTROY: i32 = 71; - pub const LIND_SAFE_MUTEX_LOCK: i32 = 72; - pub const LIND_SAFE_MUTEX_TRYLOCK: i32 = 73; - pub const LIND_SAFE_MUTEX_UNLOCK: i32 = 74; - pub const LIND_SAFE_COND_CREATE: i32 = 75; - pub const LIND_SAFE_COND_DESTROY: i32 = 76; - pub const LIND_SAFE_COND_WAIT: i32 = 77; - pub const LIND_SAFE_COND_BROADCAST: i32 = 78; - pub const LIND_SAFE_COND_SIGNAL: i32 = 79; - pub const LIND_SAFE_COND_TIMEDWAIT: i32 = 80; - - pub const LIND_SAFE_SEM_INIT: i32 = 91; - pub const LIND_SAFE_SEM_WAIT: i32 = 92; - pub const LIND_SAFE_SEM_TRYWAIT: i32 = 93; - pub const LIND_SAFE_SEM_TIMEDWAIT: i32 = 94; - pub const LIND_SAFE_SEM_POST: i32 = 95; - pub const LIND_SAFE_SEM_DESTROY: i32 = 96; - pub const LIND_SAFE_SEM_GETVALUE: i32 = 97; - - pub const LIND_SAFE_NET_GETHOSTNAME: i32 = 125; - - pub const LIND_SAFE_FS_PREAD: i32 = 126; - pub const LIND_SAFE_FS_PWRITE: i32 = 127; - pub const LIND_SAFE_FS_CHDIR: i32 = 130; - pub const LIND_SAFE_FS_MKDIR: i32 = 131; - pub const LIND_SAFE_FS_RMDIR: i32 = 132; - pub const LIND_SAFE_FS_CHMOD: i32 = 133; - pub const LIND_SAFE_FS_FCHMOD: i32 = 134; - - pub const LIND_SAFE_NET_SOCKET: i32 = 136; - - pub const LIND_SAFE_NET_GETSOCKNAME: i32 = 144; - pub const LIND_SAFE_NET_GETPEERNAME: i32 = 145; - pub const LIND_SAFE_NET_GETIFADDRS: i32 = 146; - pub const LIND_SAFE_SYS_SIGACTION: i32 = 147; - pub const LIND_SAFE_SYS_KILL: i32 = 148; - pub const LIND_SAFE_SYS_SIGPROCMASK: i32 = 149; - pub const LIND_SAFE_SYS_LINDSETITIMER: i32 = 150; - - pub const LIND_SAFE_FS_FCHDIR: i32 = 161; - pub const LIND_SAFE_FS_FSYNC: i32 = 162; - pub const LIND_SAFE_FS_FDATASYNC: i32 = 163; - pub const LIND_SAFE_FS_SYNC_FILE_RANGE: i32 = 164; - - pub const LIND_SAFE_SYS_CLONE: i32 = 171; - - #[repr(C)] - pub union RustArg { - pub dispatch_int: i32, - pub dispatch_uint: u32, - pub dispatch_intptr: *mut i32, - pub dispatch_ulong: u64, - pub dispatch_ulong_long: u64, - pub dispatch_long: i64, - pub dispatch_size_t: usize, - pub dispatch_ssize_t: ssize_t, - pub dispatch_off_t: off_t, - pub dispatch_socklen_t: socklen_t, - pub dispatch_socklen_t_ptr: *mut socklen_t, - pub dispatch_cbuf: *const c_void, - pub dispatch_mutcbuf: *mut c_void, - pub dispatch_cstr: *const c_char, - pub dispatch_cstrarr: *const *const c_char, - pub strdispatch_rlimitstruct: *mut rlimit, // Standard - // pub dispatch_statstruct: *mut lind_stat, // Needs manual definition - pub dispatch_statfsstruct: *mut statfs, // Standard - pub dispatch_timevalstruct: *mut timeval, // Standard - pub dispatch_timespecstruct: *mut timespec, // Standard - pub dispatch_sockaddrstruct: *mut sockaddr, // Standard - // pub dispatch_epolleventstruct: *mut epoll_event, // Standard - pub dispatch_constsockaddrstruct: *mut sockaddr, // Standard - // pub dispatch_shmidstruct: *mut lind_shmid_ds, // Needs manual definition - pub dispatch_pipearray: *mut i32, - // pub dispatch_naclabisigactionstruct: *mut nacl_abi_sigaction, // Needs manual definition - // pub dispatch_constnaclabisigactionstruct: *const nacl_abi_sigaction, // Needs manual definition - // pub dispatch_naclsigset: *mut u64, // Use u64 in Rust - // pub dispatch_constnaclsigset: *const u64, // Use u64 in Rust - pub dispatch_structitimerval: *mut itimerval, // Standard - pub dispatch_conststructitimerval: *const itimerval, // Standard - pub fdset: *mut fd_set, - } - - pub const BLANKARG: RustArg = RustArg { dispatch_ulong: 0 }; - - #[link(name = "rawposix")] - extern "C" { - pub(crate) fn dispatcher( - cageid: u64, - callnum: i32, - arg1: RustArg, - arg2: RustArg, - arg3: RustArg, - arg4: RustArg, - arg5: RustArg, - arg6: RustArg, - ); - - pub(crate) fn lindrustinit(verbosity: i32); - - pub(crate) fn lindrustfinalize(); - - pub(crate) fn quick_write(fd: i32, buf: *const u8, count: usize, cageid: u64); - - pub(crate) fn rustposix_thread_init(cageid: u64, signalflag: u64); - - pub(crate) fn lind_syscall_api( - cageid: u64, - call_number: u32, - call_name: u64, - start_address: u64, - arg1: u64, - arg2: u64, - arg3: u64, - arg4: u64, - arg5: u64, - arg6: u64, - ) -> u32; - } -} +// extern crate libc; +// pub mod librawposix { +// use libc::{ +// c_char, c_void, fd_set, itimerval, off_t, rlimit, sockaddr, socklen_t, ssize_t, statfs, +// timespec, timeval, +// }; + +// pub const LIND_SAFE_FS_ACCESS: i32 = 2; +// pub const LIND_SAFE_FS_UNLINK: i32 = 4; +// pub const LIND_SAFE_FS_LINK: i32 = 5; +// pub const LIND_SAFE_FS_RENAME: i32 = 6; + +// pub const LIND_SAFE_FS_XSTAT: i32 = 9; +// pub const LIND_SAFE_FS_OPEN: i32 = 10; +// pub const LIND_SAFE_FS_CLOSE: i32 = 11; +// pub const LIND_SAFE_FS_READ: i32 = 12; +// pub const LIND_SAFE_FS_WRITE: i32 = 13; +// pub const LIND_SAFE_FS_LSEEK: i32 = 14; +// pub const LIND_SAFE_FS_IOCTL: i32 = 15; +// pub const LIND_SAFE_FS_TRUNCATE: i32 = 16; +// pub const LIND_SAFE_FS_FXSTAT: i32 = 17; +// pub const LIND_SAFE_FS_FTRUNCATE: i32 = 18; +// pub const LIND_SAFE_FS_FSTATFS: i32 = 19; +// pub const LIND_SAFE_FS_MMAP: i32 = 21; +// pub const LIND_SAFE_FS_MUNMAP: i32 = 22; +// pub const LIND_SAFE_FS_GETDENTS: i32 = 23; +// pub const LIND_SAFE_FS_DUP: i32 = 24; +// pub const LIND_SAFE_FS_DUP2: i32 = 25; +// pub const LIND_SAFE_FS_STATFS: i32 = 26; +// pub const LIND_SAFE_FS_FCNTL: i32 = 28; + +// pub const LIND_SAFE_SYS_GETPPID: i32 = 29; +// pub const LIND_SAFE_SYS_EXIT: i32 = 30; +// pub const LIND_SAFE_SYS_GETPID: i32 = 31; + +// pub const LIND_SAFE_NET_BIND: i32 = 33; +// pub const LIND_SAFE_NET_SEND: i32 = 34; +// pub const LIND_SAFE_NET_SENDTO: i32 = 35; +// pub const LIND_SAFE_NET_RECV: i32 = 36; +// pub const LIND_SAFE_NET_RECVFROM: i32 = 37; +// pub const LIND_SAFE_NET_CONNECT: i32 = 38; +// pub const LIND_SAFE_NET_LISTEN: i32 = 39; +// pub const LIND_SAFE_NET_ACCEPT: i32 = 40; + +// pub const LIND_SAFE_NET_GETSOCKOPT: i32 = 43; +// pub const LIND_SAFE_NET_SETSOCKOPT: i32 = 44; +// pub const LIND_SAFE_NET_SHUTDOWN: i32 = 45; +// pub const LIND_SAFE_NET_SELECT: i32 = 46; +// pub const LIND_SAFE_FS_GETCWD: i32 = 47; +// pub const LIND_SAFE_NET_POLL: i32 = 48; +// pub const LIND_SAFE_NET_SOCKETPAIR: i32 = 49; +// pub const LIND_SAFE_SYS_GETUID: i32 = 50; +// pub const LIND_SAFE_SYS_GETEUID: i32 = 51; +// pub const LIND_SAFE_SYS_GETGID: i32 = 52; +// pub const LIND_SAFE_SYS_GETEGID: i32 = 53; +// pub const LIND_SAFE_FS_FLOCK: i32 = 54; + +// pub const LIND_SAFE_NET_EPOLL_CREATE: i32 = 56; +// pub const LIND_SAFE_NET_EPOLL_CTL: i32 = 57; +// pub const LIND_SAFE_NET_EPOLL_WAIT: i32 = 58; + +// pub const LIND_SAFE_FS_SHMGET: i32 = 62; +// pub const LIND_SAFE_FS_SHMAT: i32 = 63; +// pub const LIND_SAFE_FS_SHMDT: i32 = 64; +// pub const LIND_SAFE_FS_SHMCTL: i32 = 65; + +// pub const LIND_SAFE_FS_PIPE: i32 = 66; +// pub const LIND_SAFE_FS_PIPE2: i32 = 67; +// pub const LIND_SAFE_FS_FORK: i32 = 68; +// pub const LIND_SAFE_FS_EXEC: i32 = 69; + +// pub const LIND_SAFE_MUTEX_CREATE: i32 = 70; +// pub const LIND_SAFE_MUTEX_DESTROY: i32 = 71; +// pub const LIND_SAFE_MUTEX_LOCK: i32 = 72; +// pub const LIND_SAFE_MUTEX_TRYLOCK: i32 = 73; +// pub const LIND_SAFE_MUTEX_UNLOCK: i32 = 74; +// pub const LIND_SAFE_COND_CREATE: i32 = 75; +// pub const LIND_SAFE_COND_DESTROY: i32 = 76; +// pub const LIND_SAFE_COND_WAIT: i32 = 77; +// pub const LIND_SAFE_COND_BROADCAST: i32 = 78; +// pub const LIND_SAFE_COND_SIGNAL: i32 = 79; +// pub const LIND_SAFE_COND_TIMEDWAIT: i32 = 80; + +// pub const LIND_SAFE_SEM_INIT: i32 = 91; +// pub const LIND_SAFE_SEM_WAIT: i32 = 92; +// pub const LIND_SAFE_SEM_TRYWAIT: i32 = 93; +// pub const LIND_SAFE_SEM_TIMEDWAIT: i32 = 94; +// pub const LIND_SAFE_SEM_POST: i32 = 95; +// pub const LIND_SAFE_SEM_DESTROY: i32 = 96; +// pub const LIND_SAFE_SEM_GETVALUE: i32 = 97; + +// pub const LIND_SAFE_NET_GETHOSTNAME: i32 = 125; + +// pub const LIND_SAFE_FS_PREAD: i32 = 126; +// pub const LIND_SAFE_FS_PWRITE: i32 = 127; +// pub const LIND_SAFE_FS_CHDIR: i32 = 130; +// pub const LIND_SAFE_FS_MKDIR: i32 = 131; +// pub const LIND_SAFE_FS_RMDIR: i32 = 132; +// pub const LIND_SAFE_FS_CHMOD: i32 = 133; +// pub const LIND_SAFE_FS_FCHMOD: i32 = 134; + +// pub const LIND_SAFE_NET_SOCKET: i32 = 136; + +// pub const LIND_SAFE_NET_GETSOCKNAME: i32 = 144; +// pub const LIND_SAFE_NET_GETPEERNAME: i32 = 145; +// pub const LIND_SAFE_NET_GETIFADDRS: i32 = 146; +// pub const LIND_SAFE_SYS_SIGACTION: i32 = 147; +// pub const LIND_SAFE_SYS_KILL: i32 = 148; +// pub const LIND_SAFE_SYS_SIGPROCMASK: i32 = 149; +// pub const LIND_SAFE_SYS_LINDSETITIMER: i32 = 150; + +// pub const LIND_SAFE_FS_FCHDIR: i32 = 161; +// pub const LIND_SAFE_FS_FSYNC: i32 = 162; +// pub const LIND_SAFE_FS_FDATASYNC: i32 = 163; +// pub const LIND_SAFE_FS_SYNC_FILE_RANGE: i32 = 164; + +// pub const LIND_SAFE_SYS_CLONE: i32 = 171; + +// #[repr(C)] +// pub union RustArg { +// pub dispatch_int: i32, +// pub dispatch_uint: u32, +// pub dispatch_intptr: *mut i32, +// pub dispatch_ulong: u64, +// pub dispatch_ulong_long: u64, +// pub dispatch_long: i64, +// pub dispatch_size_t: usize, +// pub dispatch_ssize_t: ssize_t, +// pub dispatch_off_t: off_t, +// pub dispatch_socklen_t: socklen_t, +// pub dispatch_socklen_t_ptr: *mut socklen_t, +// pub dispatch_cbuf: *const c_void, +// pub dispatch_mutcbuf: *mut c_void, +// pub dispatch_cstr: *const c_char, +// pub dispatch_cstrarr: *const *const c_char, +// pub strdispatch_rlimitstruct: *mut rlimit, // Standard +// // pub dispatch_statstruct: *mut lind_stat, // Needs manual definition +// pub dispatch_statfsstruct: *mut statfs, // Standard +// pub dispatch_timevalstruct: *mut timeval, // Standard +// pub dispatch_timespecstruct: *mut timespec, // Standard +// pub dispatch_sockaddrstruct: *mut sockaddr, // Standard +// // pub dispatch_epolleventstruct: *mut epoll_event, // Standard +// pub dispatch_constsockaddrstruct: *mut sockaddr, // Standard +// // pub dispatch_shmidstruct: *mut lind_shmid_ds, // Needs manual definition +// pub dispatch_pipearray: *mut i32, +// // pub dispatch_naclabisigactionstruct: *mut nacl_abi_sigaction, // Needs manual definition +// // pub dispatch_constnaclabisigactionstruct: *const nacl_abi_sigaction, // Needs manual definition +// // pub dispatch_naclsigset: *mut u64, // Use u64 in Rust +// // pub dispatch_constnaclsigset: *const u64, // Use u64 in Rust +// pub dispatch_structitimerval: *mut itimerval, // Standard +// pub dispatch_conststructitimerval: *const itimerval, // Standard +// pub fdset: *mut fd_set, +// } + +// pub const BLANKARG: RustArg = RustArg { dispatch_ulong: 0 }; + +// #[link(name = "rawposix")] +// extern "C" { +// pub(crate) fn dispatcher( +// cageid: u64, +// callnum: i32, +// arg1: RustArg, +// arg2: RustArg, +// arg3: RustArg, +// arg4: RustArg, +// arg5: RustArg, +// arg6: RustArg, +// ); + +// pub(crate) fn lindrustinit(verbosity: i32); + +// pub(crate) fn lindrustfinalize(); + +// pub(crate) fn quick_write(fd: i32, buf: *const u8, count: usize, cageid: u64); + +// pub(crate) fn rustposix_thread_init(cageid: u64, signalflag: u64); + +// pub(crate) fn lind_syscall_api( +// cageid: u64, +// call_number: u32, +// call_name: u64, +// start_address: u64, +// arg1: u64, +// arg2: u64, +// arg3: u64, +// arg4: u64, +// arg5: u64, +// arg6: u64, +// ) -> u32; +// } +// } use std::sync::{Condvar, Mutex}; From 55be5f8980ee9033f4a96b04efe1c316c530f5a9 Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Mon, 30 Sep 2024 15:38:53 -0400 Subject: [PATCH 12/37] fixed sleep --- src/interface/misc.rs | 6 +++++- src/safeposix/dispatcher.rs | 17 +++++++++++++++++ src/safeposix/syscalls/sys_calls.rs | 5 +++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/interface/misc.rs b/src/interface/misc.rs index 9ad234c6..981df2c5 100644 --- a/src/interface/misc.rs +++ b/src/interface/misc.rs @@ -25,7 +25,7 @@ pub use std::sync::atomic::{ pub use std::sync::Arc as RustRfc; pub use std::thread::spawn as helper_thread; -use libc::{mmap, pthread_exit, pthread_kill, pthread_self, sched_yield, syscall, SYS_futex}; +use libc::{mmap, pthread_exit, pthread_kill, pthread_self, sched_yield, syscall, SYS_clock_nanosleep, SYS_futex}; use std::ffi::c_void; pub use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; @@ -310,6 +310,10 @@ pub fn libc_futex(uaddr: u64, futex_op: u32, val: u32, val2: u32, uaddr2: u32, v unsafe { syscall(SYS_futex, uaddr, futex_op, val, val2, uaddr2, val3) as i32 } } +pub fn libc_nanosleep_time64(clockid: u32, flags: i32, req: usize, rem: usize) -> i32 { + unsafe { syscall(SYS_clock_nanosleep, clockid, flags, req, rem) as i32 } +} + #[derive(Debug)] pub struct AdvisoryLock { //0 signifies unlocked, -1 signifies locked exclusively, positive number signifies that many shared lock holders diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index 40e2a2b6..bcce3914 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -115,6 +115,8 @@ const WRITEV_SYSCALL: i32 = 170; const CLONE_SYSCALL: i32 = 171; +const NANOSLEEP_TIME64_SYSCALL : i32 = 181; + use std::collections::HashMap; use std::hash::BuildHasherDefault; @@ -1694,6 +1696,21 @@ pub extern "C" fn lind_syscall_api( } } + NANOSLEEP_TIME64_SYSCALL => { + let clockid = arg1 as u32; + let flags = arg2 as i32; + let req = (start_address + arg3) as usize; + let rem = (start_address + arg4) as usize; + + interface::check_cageid(cageid); + unsafe { + CAGE_TABLE[cageid as usize] + .as_ref() + .unwrap() + .nanosleep_time64_syscall(clockid, flags, req, rem) + } + } + // CLONE_SYSCALL => { // let clone_args = match interface::get_cloneargs(Arg::from_u64_as_clone_args(start_address + arg1)) { // Ok(val) => val, diff --git a/src/safeposix/syscalls/sys_calls.rs b/src/safeposix/syscalls/sys_calls.rs index 6c9fd36a..bb187950 100644 --- a/src/safeposix/syscalls/sys_calls.rs +++ b/src/safeposix/syscalls/sys_calls.rs @@ -49,6 +49,11 @@ impl Cage { } } + pub fn nanosleep_time64_syscall(&self, clockid: u32, flags: i32, req: usize, rem: usize) -> i32 { + interface::libc_nanosleep_time64(clockid, flags, req, rem) + } + + pub fn fork_syscall(&self, child_cageid: u64) -> i32 { // Modify the fdtable manually copy_fdtable_for_cage(self.cageid, child_cageid).unwrap(); From 48d2477a3d0fefaef254c051cc66c8daa0881f16 Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Tue, 1 Oct 2024 11:27:25 -0400 Subject: [PATCH 13/37] delete some reference --- src/safeposix/dispatcher.rs | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index bcce3914..15e6cd51 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -32,36 +32,36 @@ const GETPID_SYSCALL: i32 = 31; const BIND_SYSCALL: i32 = 33; const SEND_SYSCALL: i32 = 34; -const SENDTO_SYSCALL: i32 = 35; //can not find in fs_calls +const SENDTO_SYSCALL: i32 = 35; const RECV_SYSCALL: i32 = 36; -const RECVFROM_SYSCALL: i32 = 37; //can not find in fs_calls +const RECVFROM_SYSCALL: i32 = 37; const CONNECT_SYSCALL: i32 = 38; const LISTEN_SYSCALL: i32 = 39; const ACCEPT_SYSCALL: i32 = 40; -const GETSOCKOPT_SYSCALL: i32 = 43; // +const GETSOCKOPT_SYSCALL: i32 = 43; const SETSOCKOPT_SYSCALL: i32 = 44; const SHUTDOWN_SYSCALL: i32 = 45; -const SELECT_SYSCALL: i32 = 46; // +const SELECT_SYSCALL: i32 = 46; const GETCWD_SYSCALL: i32 = 47; const POLL_SYSCALL: i32 = 48; -const SOCKETPAIR_SYSCALL: i32 = 49; // +const SOCKETPAIR_SYSCALL: i32 = 49; const GETUID_SYSCALL: i32 = 50; const GETEUID_SYSCALL: i32 = 51; const GETGID_SYSCALL: i32 = 52; const GETEGID_SYSCALL: i32 = 53; const FLOCK_SYSCALL: i32 = 54; const EPOLL_CREATE_SYSCALL: i32 = 56; -const EPOLL_CTL_SYSCALL: i32 = 57; // -const EPOLL_WAIT_SYSCALL: i32 = 58; // +const EPOLL_CTL_SYSCALL: i32 = 57; +const EPOLL_WAIT_SYSCALL: i32 = 58; const SHMGET_SYSCALL: i32 = 62; const SHMAT_SYSCALL: i32 = 63; const SHMDT_SYSCALL: i32 = 64; -const SHMCTL_SYSCALL: i32 = 65; // +const SHMCTL_SYSCALL: i32 = 65; -const PIPE_SYSCALL: i32 = 66; // -const PIPE2_SYSCALL: i32 = 67; // +const PIPE_SYSCALL: i32 = 66; +const PIPE2_SYSCALL: i32 = 67; const FORK_SYSCALL: i32 = 68; const EXEC_SYSCALL: i32 = 69; @@ -75,12 +75,12 @@ const COND_DESTROY_SYSCALL: i32 = 76; const COND_WAIT_SYSCALL: i32 = 77; const COND_BROADCAST_SYSCALL: i32 = 78; const COND_SIGNAL_SYSCALL: i32 = 79; -const COND_TIMEDWAIT_SYSCALL: i32 = 80; //other type +const COND_TIMEDWAIT_SYSCALL: i32 = 80; const SEM_INIT_SYSCALL: i32 = 91; const SEM_WAIT_SYSCALL: i32 = 92; const SEM_TRYWAIT_SYSCALL: i32 = 93; -const SEM_TIMEDWAIT_SYSCALL: i32 = 94; // type +const SEM_TIMEDWAIT_SYSCALL: i32 = 94; const SEM_POST_SYSCALL: i32 = 95; const SEM_DESTROY_SYSCALL: i32 = 96; const SEM_GETVALUE_SYSCALL: i32 = 97; @@ -97,14 +97,14 @@ const FCHMOD_SYSCALL: i32 = 134; const SOCKET_SYSCALL: i32 = 136; -const GETSOCKNAME_SYSCALL: i32 = 144; // -const GETPEERNAME_SYSCALL: i32 = 145; // +const GETSOCKNAME_SYSCALL: i32 = 144; +const GETPEERNAME_SYSCALL: i32 = 145; const GETIFADDRS_SYSCALL: i32 = 146; const SIGACTION_SYSCALL: i32 = 147; const KILL_SYSCALL: i32 = 148; -const SIGPROCMASK_SYSCALL: i32 = 149; // -const SETITIMER_SYSCALL: i32 = 150; // +const SIGPROCMASK_SYSCALL: i32 = 149; +const SETITIMER_SYSCALL: i32 = 150; const FCHDIR_SYSCALL: i32 = 161; const FSYNC_SYSCALL: i32 = 162; @@ -757,7 +757,6 @@ pub extern "C" fn lind_syscall_api( } } - //testing DUP_SYSCALL => { let fd = arg1 as i32; let fd2: Option = if arg1 <= i32::MAX as u64 { @@ -813,8 +812,6 @@ pub extern "C" fn lind_syscall_api( .fstat_syscall(fd, buf) } } - - //adding without testing UNLINK_SYSCALL => { let fd_ptr = (start_address + arg1) as *const u8; From db9fbc41515cc6b1df1dc90ff86576f8ccedc6f3 Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Tue, 1 Oct 2024 11:41:12 -0400 Subject: [PATCH 14/37] delete repeat recv --- src/safeposix/dispatcher.rs | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index 15e6cd51..fcb210cf 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -1394,21 +1394,6 @@ pub extern "C" fn lind_syscall_api( } } - RECV_SYSCALL => { - let virtual_fd = arg1 as i32; - let buf = (start_address + arg4) as *mut u8; - let len = arg3 as usize; - let flags = arg4 as i32; - - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .recv_syscall( virtual_fd, buf, len, flags) - } - } - LISTEN_SYSCALL => { let virtual_fd = arg1 as i32; let backlog = arg2 as i32; From 8d36a353bd5545f22573796e61edc94ce38708cf Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Tue, 1 Oct 2024 11:47:39 -0400 Subject: [PATCH 15/37] delete repeat exit exec --- src/safeposix/dispatcher.rs | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index fcb210cf..d2fbc7ef 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -1639,28 +1639,6 @@ pub extern "C" fn lind_syscall_api( // ret // } - EXEC_SYSCALL => { - interface::check_cageid(cageid); - let child_cageid = arg1 as u64; - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .exec_syscall(child_cageid) - } - } - - EXIT_SYSCALL => { - interface::check_cageid(cageid); - let status = arg1 as i32; - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .exit_syscall(status) - } - } - FUTEX_SYSCALL => { let uaddr = (start_address + arg1) as u64; let futex_op = arg2 as u32; From 0300e71de6562c4ada3d8e3bac548498c9523f67 Mon Sep 17 00:00:00 2001 From: qianxichen233 Date: Tue, 1 Oct 2024 17:11:48 +0000 Subject: [PATCH 16/37] some cleanup --- src/interface/types.rs | 30 ---------------- src/safeposix/dispatcher.rs | 47 ++++++------------------- src/safeposix/syscalls/fs_calls.rs | 5 +-- src/safeposix/syscalls/net_calls.rs | 4 +-- src/safeposix/syscalls/sys_calls.rs | 44 ----------------------- src/safeposix/syscalls/sys_constants.rs | 32 +---------------- 6 files changed, 14 insertions(+), 148 deletions(-) diff --git a/src/interface/types.rs b/src/interface/types.rs index 7bd41c6b..7baa28f8 100644 --- a/src/interface/types.rs +++ b/src/interface/types.rs @@ -229,7 +229,6 @@ pub union Arg { // pub dispatch_structsem: *mut sem_t, // pub dispatch_ifaddrs: *mut ifaddrs, pub dispatch_constiovecstruct: *const interface::IovecStruct, - pub dispatch_cloneargs: *mut interface::CloneArgStruct } use std::mem::size_of; @@ -242,22 +241,6 @@ pub struct ClippedDirent { pub d_reclen: u16, } -#[derive(Copy, Clone, Default, Debug)] -#[repr(C)] -pub struct CloneArgStruct { - pub flags: u64, // Flags that control the behavior of the child process - pub pidfd: u64, // File descriptor to receive the child's PID - pub child_tid: u64, // Pointer to a memory location where the child TID will be stored - pub parent_tid: u64, // Pointer to a memory location where the parent's TID will be stored - pub exit_signal: u64, // Signal to be sent when the child process exits - pub stack: u64, // Address of the stack for the child process - pub stack_size: u64, // Size of the stack for the child process - pub tls: u64, // Thread-Local Storage (TLS) descriptor for the child thread - pub set_tid: u64, // Pointer to an array of TIDs to be set in the child - pub set_tid_size: u64, // Number of TIDs in the `set_tid` array - pub cgroup: u64, // File descriptor for the cgroup to which the child process should be attached -} - pub const CLIPPED_DIRENT_SIZE: u32 = size_of::() as u32; pub fn get_int(union_argument: Arg) -> Result { @@ -495,18 +478,6 @@ pub fn get_ioctlptrunion<'a>(union_argument: Arg) -> Result<&'a mut u8, i32> { )); } -pub fn get_cloneargs<'a>(clone_args: Arg) -> Result<&'a mut CloneArgStruct, i32> { - let pointer = unsafe { clone_args.dispatch_cloneargs }; - if !pointer.is_null() { - return Ok(unsafe { &mut *pointer }); - } - return Err(syscall_error( - Errno::EFAULT, - "dispatcher", - "input data not valid", - )); -} - // pub fn get_ioctlptrunion(union_argument: Arg) -> Result { // return Ok(unsafe { union_argument.dispatch_ioctlptrunion }); // } @@ -660,7 +631,6 @@ pub fn get_sockaddr(union_argument: Arg, addrlen: u32) -> Result { - println!("val: {}", val); return Err(syscall_error( Errno::EOPNOTSUPP, "dispatcher", diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index d2fbc7ef..86d2195b 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -143,7 +143,7 @@ use std::io; // use super::shm::SHM_METADATA; // use super::syscalls::{fs_constants::IPC_STAT, sys_constants::*}; use crate::interface::types::SockaddrDummy; -use crate::interface::{CloneArgStruct, SigactionStruct, StatData}; +use crate::interface::{SigactionStruct, StatData}; use crate::{example_grates, interface}; use crate::interface::errnos::*; // use crate::lib_fs_utils::{lind_deltree, visit_children}; @@ -289,11 +289,6 @@ impl Arg { // dispatch_sigactionstruct: value as *mut SigactionStruct, // } // } - pub fn from_u64_as_clone_args(value: u64) -> Self { - Arg { - dispatch_cloneargs: value as *mut CloneArgStruct - } - } } fn parse_null_terminated_string(ptr: *const std::os::raw::c_char) -> Result { @@ -321,16 +316,16 @@ pub extern "C" fn lind_syscall_api( arg6: u64, ) -> i32 { let call_number = call_number as i32; - let call_name = match call_name { - 0 => { - "unnamed" - }, - _ => { - &parse_null_terminated_string((call_name + start_address) as (*const std::os::raw::c_char)).unwrap() - } - }; + // let call_name = match call_name { + // 0 => { + // "unnamed" + // }, + // _ => { + // &parse_null_terminated_string((call_name + start_address) as (*const std::os::raw::c_char)).unwrap() + // } + // }; // let call_name = ; - println!("------cage {} calls {} ({})", cageid, call_name, call_number); + // println!("------cage {} calls {} ({})", cageid, call_name, call_number); // Print all the arguments // println!("cage {} calls: {}", cageid, call_number); // println!("call_name: {}", call_name); @@ -629,7 +624,6 @@ pub extern "C" fn lind_syscall_api( XSTAT_SYSCALL => { let fd_ptr = (start_address + arg1) as *const u8; - println!("arg2: {:?}", arg2); let buf = match interface::get_statdatastruct(Arg::from_u64_as_statstruct(start_address + arg2)) { Ok(val) => val, Err(errno) => { @@ -1671,29 +1665,10 @@ pub extern "C" fn lind_syscall_api( } } - // CLONE_SYSCALL => { - // let clone_args = match interface::get_cloneargs(Arg::from_u64_as_clone_args(start_address + arg1)) { - // Ok(val) => val, - // Err(e) => { - // return -1; - // } - // }; - - // clone_args.child_tid = clone_args.child_tid + start_address; - - // interface::check_cageid(cageid); - // unsafe { - // CAGE_TABLE[cageid as usize] - // .as_ref() - // .unwrap() - // .clone3_syscall(caller, clone_args) - // } - // } - _ => -1, // Return -1 for unknown syscalls }; - println!("------cage {} calling {} returns {}", cageid, call_name, ret); + // println!("------cage {} calling {} returns {}", cageid, call_name, ret); ret } diff --git a/src/safeposix/syscalls/fs_calls.rs b/src/safeposix/syscalls/fs_calls.rs index 0fb23ebc..3aade273 100644 --- a/src/safeposix/syscalls/fs_calls.rs +++ b/src/safeposix/syscalls/fs_calls.rs @@ -33,8 +33,7 @@ use crate::example_grates::dashmapvecglobal::*; // use crate::example_grates::muthashmaxglobal::*; // use crate::example_grates::dashmaparrayglobal::*; -// static LIND_ROOT: &str = "/home/lind/lind_project/src/safeposix-rust/tmp"; -static LIND_ROOT: &str = "/home/lind-wasm/lind_fs_root"; +static LIND_ROOT: &str = "/home/lind/lind_project/src/safeposix-rust/tmp"; /* * We will receive parameters with type u64 by default, then we will do type conversion inside @@ -345,8 +344,6 @@ impl Cage { rposix_statbuf.st_size = libc_statbuf.st_size as usize; rposix_statbuf.st_uid = libc_statbuf.st_uid; - println!("stat: {:?}", rposix_statbuf); - libcret } diff --git a/src/safeposix/syscalls/net_calls.rs b/src/safeposix/syscalls/net_calls.rs index 64733d0a..52532daf 100644 --- a/src/safeposix/syscalls/net_calls.rs +++ b/src/safeposix/syscalls/net_calls.rs @@ -27,7 +27,7 @@ use libc::*; use std::{os::fd::RawFd, ptr}; use bit_set::BitSet; -static LIND_ROOT: &str = "/home/lind-wasm/lind_fs_root/"; +static LIND_ROOT: &str = "/home/lind/lind_project/src/safeposix-rust/tmp"; lazy_static! { // A hashmap used to store epoll mapping relationships @@ -188,8 +188,6 @@ impl Cage { let c_str = CStr::from_ptr(sun_path_ptr); let str_slice = c_str.to_str().expect("Failed to convert CStr to str"); - // println!("[bind] addr: {:?}", addr); - // println!("[bind] sun_path: {}", str_slice); io::stdout().flush().unwrap(); } // println!("[Bind] Error message: {:?}", err_msg); diff --git a/src/safeposix/syscalls/sys_calls.rs b/src/safeposix/syscalls/sys_calls.rs index bb187950..d276079e 100644 --- a/src/safeposix/syscalls/sys_calls.rs +++ b/src/safeposix/syscalls/sys_calls.rs @@ -6,7 +6,6 @@ use super::net_constants::*; use super::sys_constants; use super::sys_constants::*; use crate::interface; -use crate::interface::CloneArgStruct; use crate::safeposix::cage; use crate::safeposix::cage::*; use crate::safeposix::shm::*; @@ -312,49 +311,6 @@ impl Cage { status } - // pub fn clone3_syscall + Clone + Send + 'static + std::marker::Sync, U: Clone + Send + 'static + std::marker::Sync> - // (&self, caller: &mut wasmtime::Caller<'_, T>, args: &mut CloneArgStruct) -> i32 { - // let rewind_res = match wasmtime_lind::catch_rewind(caller) { - // Ok(val) => val, - // Err(_) => -1 - // }; - - // // println!("rewind_res: {}", rewind_res); - // if rewind_res >= 0 { return rewind_res; } - // // get the flags - // let flags = args.flags; - // // if CLONE_VM is set, we are creating a new thread (i.e. pthread_create) - // // otherwise, we are creating a process (i.e. fork) - // let isthread = flags & (CLONE_VM as u64); - - // if isthread == 0 { - // // fork - // let child_cageid = self.cageid + 1; - - // self.fork_syscall(child_cageid); - - // match wasmtime_lind::lind_fork(caller, move |status| { - // let child_cage = interface::cagetable_getref(child_cageid); - // child_cage.exit_syscall(status) - // }) { - // Ok(res) => res, - // Err(e) => -1 - // } - // } - // else { - // // pthread_create - // match wasmtime_lind::lind_fork_shared_memory(caller, move |status| { - // // on thread exit, we do not need to call cage.exit_syscall - // // but may need to call some signal stuff - // // reserve the place for future support of signal - // 0 - // }, args.stack as i32, args.stack_size as i32, args.child_tid) { - // Ok(res) => res, - // Err(e) => -1 - // } - // } - // } - pub fn getpid_syscall(&self) -> i32 { self.cageid as i32 //not sure if this is quite what we want but it's easy enough to change later } diff --git a/src/safeposix/syscalls/sys_constants.rs b/src/safeposix/syscalls/sys_constants.rs index 4db67f62..fcd219a9 100644 --- a/src/safeposix/syscalls/sys_constants.rs +++ b/src/safeposix/syscalls/sys_constants.rs @@ -74,34 +74,4 @@ pub const SIGUNUSED: i32 = 31; pub const SIG_BLOCK: i32 = 0; pub const SIG_UNBLOCK: i32 = 1; pub const SIG_SETMASK: i32 = 2; -pub const ITIMER_REAL: i32 = 0; - -// /* Cloning flags. */ -// pub const CSIGNAL: u64 = 0x000000ff; /* Signal mask to be sent at exit. */ -// pub const CLONE_VM: u64 = 0x00000100; /* Set if VM shared between processes. */ -// pub const CLONE_FS: u64 = 0x00000200; /* Set if fs info shared between processes. */ -// pub const CLONE_FILES: u64 = 0x00000400; /* Set if open files shared between processes. */ -// pub const CLONE_SIGHAND: u64 = 0x00000800; /* Set if signal handlers shared. */ -// pub const CLONE_PIDFD: u64 = 0x00001000; /* Set if a pidfd should be placed in parent. */ -// pub const CLONE_PTRACE: u64 = 0x00002000; /* Set if tracing continues on the child. */ -// pub const CLONE_VFORK: u64 = 0x00004000; /* Set if the parent wants the child to wake it up on mm_release. */ -// pub const CLONE_PARENT: u64 = 0x00008000; /* Set if we want to have the same parent as the cloner. */ -// pub const CLONE_THREAD: u64 = 0x00010000; /* Set to add to same thread group. */ -// pub const CLONE_NEWNS: u64 = 0x00020000; /* Set to create new namespace. */ -// pub const CLONE_SYSVSEM: u64 = 0x00040000; /* Set to shared SVID SEM_UNDO semantics. */ -// pub const CLONE_SETTLS: u64 = 0x00080000; /* Set TLS info. */ -// pub const CLONE_PARENT_SETTID: u64 = 0x00100000; /* Store TID in userlevel buffer before MM copy. */ -// pub const CLONE_CHILD_CLEARTID: u64 = 0x00200000; /* Register exit futex and memory location to clear. */ -// pub const CLONE_DETACHED: u64 = 0x00400000; /* Create clone detached. */ -// pub const CLONE_UNTRACED: u64 = 0x00800000; /* Set if the tracing process can't force CLONE_PTRACE on this clone. */ -// pub const CLONE_CHILD_SETTID: u64 = 0x01000000; /* Store TID in userlevel buffer in the child. */ -// pub const CLONE_NEWCGROUP: u64 = 0x02000000; /* New cgroup namespace. */ -// pub const CLONE_NEWUTS: u64 = 0x04000000; /* New utsname group. */ -// pub const CLONE_NEWIPC: u64 = 0x08000000; /* New ipcs. */ -// pub const CLONE_NEWUSER: u64 = 0x10000000; /* New user namespace. */ -// pub const CLONE_NEWPID: u64 = 0x20000000; /* New pid namespace. */ -// pub const CLONE_NEWNET: u64 = 0x40000000; /* New network namespace. */ -// pub const CLONE_IO: u64 = 0x80000000; /* Clone I/O context. */ -// /* cloning flags intersect with CSIGNAL so can be used only with unshare and -// clone3 syscalls. */ -// pub const CLONE_NEWTIME: u64 = 0x00000080; /* New time namespace */ \ No newline at end of file +pub const ITIMER_REAL: i32 = 0; \ No newline at end of file From e778e3b2d3b1fd99202fb02725a2fb005407ef97 Mon Sep 17 00:00:00 2001 From: qianxichen233 Date: Tue, 1 Oct 2024 17:14:04 +0000 Subject: [PATCH 17/37] restore signal --- src/safeposix/syscalls/sys_calls.rs | 30 ++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/safeposix/syscalls/sys_calls.rs b/src/safeposix/syscalls/sys_calls.rs index d276079e..10d247b0 100644 --- a/src/safeposix/syscalls/sys_calls.rs +++ b/src/safeposix/syscalls/sys_calls.rs @@ -151,18 +151,18 @@ impl Cage { let newsigset = interface::RustHashMap::new(); if !interface::RUSTPOSIX_TESTSUITE.load(interface::RustAtomicOrdering::Relaxed) { // we don't add these for the test suite - // let mainsigsetatomic = self - // .sigset - // .get( - // &self - // .main_threadid - // .load(interface::RustAtomicOrdering::Relaxed), - // ) - // .unwrap(); - // let mainsigset = interface::RustAtomicU64::new( - // mainsigsetatomic.load(interface::RustAtomicOrdering::Relaxed), - // ); - // newsigset.insert(0, mainsigset); + let mainsigsetatomic = self + .sigset + .get( + &self + .main_threadid + .load(interface::RustAtomicOrdering::Relaxed), + ) + .unwrap(); + let mainsigset = interface::RustAtomicU64::new( + mainsigsetatomic.load(interface::RustAtomicOrdering::Relaxed), + ); + newsigset.insert(0, mainsigset); } /* @@ -302,9 +302,9 @@ impl Cage { // Trigger SIGCHLD if !interface::RUSTPOSIX_TESTSUITE.load(interface::RustAtomicOrdering::Relaxed) { // dont trigger SIGCHLD for test suite - // if self.cageid != self.parent { - // interface::lind_kill_from_id(self.parent, libc::SIGCHLD); - // } + if self.cageid != self.parent { + interface::lind_kill_from_id(self.parent, libc::SIGCHLD); + } } //fdtable will be dropped at end of dispatcher scope because of Arc From e51cbef35fb49567067b5daf4df3731457c0a67e Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Tue, 1 Oct 2024 13:46:19 -0400 Subject: [PATCH 18/37] move futex and nanosleep from misc.rs to fs_call.rs --- src/interface/misc.rs | 8 -------- src/safeposix/syscalls/fs_calls.rs | 11 ++++++++--- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/interface/misc.rs b/src/interface/misc.rs index 981df2c5..ab308372 100644 --- a/src/interface/misc.rs +++ b/src/interface/misc.rs @@ -306,14 +306,6 @@ pub fn lind_kill_from_id(cage_id: u64, sig: i32) { } } -pub fn libc_futex(uaddr: u64, futex_op: u32, val: u32, val2: u32, uaddr2: u32, val3: u32) -> i32 { - unsafe { syscall(SYS_futex, uaddr, futex_op, val, val2, uaddr2, val3) as i32 } -} - -pub fn libc_nanosleep_time64(clockid: u32, flags: i32, req: usize, rem: usize) -> i32 { - unsafe { syscall(SYS_clock_nanosleep, clockid, flags, req, rem) as i32 } -} - #[derive(Debug)] pub struct AdvisoryLock { //0 signifies unlocked, -1 signifies locked exclusively, positive number signifies that many shared lock holders diff --git a/src/safeposix/syscalls/fs_calls.rs b/src/safeposix/syscalls/fs_calls.rs index 3aade273..14cb7b18 100644 --- a/src/safeposix/syscalls/fs_calls.rs +++ b/src/safeposix/syscalls/fs_calls.rs @@ -2249,9 +2249,14 @@ impl Cage { // as opposed to lind-nacl's individual implementations // // to perform this we just directly pass futex's var args as unsigned 32 bit integers to syscall() with SYS_futex - pub fn futex_syscall(&self, uaddr: u64, futex_op: u32, val: u32, val2: u32, uaddr2: u32, val3: u32) -> i32 { - let retval = interface::libc_futex(uaddr, futex_op, val, val2, uaddr2, val3); - return retval; + + pub fn futex_syscall(&self, uaddr: u64, futex_op: u32, val: u32, val2: u32, uaddr2: u32, val3: u32) -> i32 { + unsafe { syscall(SYS_futex, uaddr, futex_op, val, val2, uaddr2, val3) as i32 } + } + + + pub fn nanosleep_time64_syscall(&self, clockid: u32, flags: i32, req: usize, rem: usize) -> i32 { + unsafe { syscall(SYS_clock_nanosleep, clockid, flags, req, rem) as i32 } } } From 504f7cca910e3f96df3877c1d5a231b92c64a9ca Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Tue, 1 Oct 2024 16:52:27 -0400 Subject: [PATCH 19/37] delete misc.rs line 28 --- src/interface/misc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interface/misc.rs b/src/interface/misc.rs index ab308372..5da54e0c 100644 --- a/src/interface/misc.rs +++ b/src/interface/misc.rs @@ -25,7 +25,7 @@ pub use std::sync::atomic::{ pub use std::sync::Arc as RustRfc; pub use std::thread::spawn as helper_thread; -use libc::{mmap, pthread_exit, pthread_kill, pthread_self, sched_yield, syscall, SYS_clock_nanosleep, SYS_futex}; +use libc::{mmap, pthread_exit, pthread_kill, pthread_self, sched_yield}; use std::ffi::c_void; pub use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; From 112476bae26b6ddf6cdf8cdf72475d2766d8fdf2 Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Wed, 2 Oct 2024 13:16:14 -0400 Subject: [PATCH 20/37] delete comment for disrpatcher.rs --- src/safeposix/dispatcher.rs | 126 ------------------------------------ 1 file changed, 126 deletions(-) diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index a6e7633e..d57fe479 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -122,9 +122,6 @@ use std::hash::BuildHasherDefault; use libc::IPOPT_OPTVAL; -// use wasmtime::Caller; -// use wasmtime_lind::{get_memory_base, LindHost}; - use std::ffi::CString; use super::cage::*; @@ -137,13 +134,6 @@ const FDKIND_IMSOCK: u32 = 2; use std::io::{Read, Write}; use std::io; -// use super::filesystem::{ -// incref_root, load_fs, persist_metadata, remove_domain_sock, FilesystemMetadata, FS_METADATA, -// LOGFILENAME, LOGMAP, -// }; -// use super::net::NET_METADATA; -// use super::shm::SHM_METADATA; -// use super::syscalls::{fs_constants::IPC_STAT, sys_constants::*}; use crate::interface::types::SockaddrDummy; use crate::interface::{SigactionStruct, StatData}; use crate::{fdtables, interface}; @@ -170,14 +160,6 @@ macro_rules! check_and_dispatch { }; } -// macro_rules! check_and_dispatch_socketpair { -// ( $func:expr, $cage:ident, $($arg:expr),* ) => { -// match (|| Ok($func( $cage, $($arg?),* )))() { -// Ok(i) => i, Err(i) => i -// } -// }; -// } - // the following "quick" functions are implemented for research purposes // to increase I/O performance by bypassing the dispatcher and type checker #[no_mangle] @@ -285,11 +267,6 @@ impl Arg { dispatch_constsigactionstruct: value as *const SigactionStruct, } } - // pub fn from_u64_as_sigactionstruct(value: u64) -> Self { - // Arg { - // dispatch_sigactionstruct: value as *mut SigactionStruct, - // } - // } } fn parse_null_terminated_string(ptr: *const std::os::raw::c_char) -> Result { @@ -317,26 +294,6 @@ pub extern "C" fn lind_syscall_api( arg6: u64, ) -> i32 { let call_number = call_number as i32; - // let call_name = match call_name { - // 0 => { - // "unnamed" - // }, - // _ => { - // &parse_null_terminated_string((call_name + start_address) as (*const std::os::raw::c_char)).unwrap() - // } - // }; - // let call_name = ; - // println!("------cage {} calls {} ({})", cageid, call_name, call_number); - // Print all the arguments - // println!("cage {} calls: {}", cageid, call_number); - // println!("call_name: {}", call_name); - // println!("start_address: {}", start_address); - // println!("arg1: {}", arg1); - // println!("arg2: {}", arg2); - // println!("arg3: {}", arg3); - // println!("arg4: {}", arg4); - // println!("arg5: {}", arg5); - // println!("arg6: {}", arg6); let ret = match call_number { WRITE_SYSCALL => { @@ -420,27 +377,6 @@ pub extern "C" fn lind_syscall_api( } } - // SIGACTION_SYSCALL => { - // let sig = arg1 as i32; - // let act = match interface::get_constsigactionstruct(Arg::from_u64_as_constsigactionstruct(arg2)) { - // Ok(res_act) => res_act, - // Err(_) => return -1, // Handle error appropriately, return an error code - // }; - - // let oact = match interface::get_sigactionstruct(Arg::from_u64_as_sigactionstruct(arg3)) { - // Ok(res_oact) => res_oact, - // Err(_) => return -1, // Handle error appropriately, return an error code - // }; - - // interface::check_cageid(cageid); - // unsafe { - // CAGE_TABLE[cageid as usize] - // .as_ref() - // .unwrap() - // .sigaction_syscall(sig, act, oact) - // } - // } - CLOSE_SYSCALL => { let fd = arg1 as i32; interface::check_cageid(cageid); @@ -564,22 +500,6 @@ pub extern "C" fn lind_syscall_api( } } - // WAIT_SYSCALL => { - // let mut status = 0; - // interface::check_cageid(cageid); - // let ret = unsafe { - // CAGE_TABLE[cageid as usize] - // .as_ref() - // .unwrap() - // .wait_syscall(&mut status) - // }; - - // let status_addr = (start_address + arg1) as *mut i32; - // unsafe { *status_addr = status; } - - // ret - // } - EXEC_SYSCALL => { interface::check_cageid(cageid); let child_cageid = arg1 as u64; @@ -631,10 +551,6 @@ pub extern "C" fn lind_syscall_api( return errno; } }; - // println!("buf: {:?}", buf); - // let buf = unsafe { &mut *((start_address + arg2) as *mut interface::StatData) }; - // buf.st_size = 114514; - // return 0; let fd = unsafe { CStr::from_ptr(fd_ptr as *const i8).to_str().unwrap() @@ -985,24 +901,12 @@ pub extern "C" fn lind_syscall_api( let buf = (start_address + arg2) as *mut u8; let buflen = arg3 as usize; let flag = arg4 as i32; - - // let addrlen = arg6 as u32; - // let mut addr = interface::get_sockaddr(Arg::from_u64_as_sockaddrstruct(start_address + arg5), addrlen).unwrap(); - let nullity1 = interface::arg_nullity(&Arg::from_u64_as_sockaddrstruct(arg5)); let nullity2 = interface::arg_nullity(&Arg::from_u64_as_socklen_ptr(arg6)); interface::check_cageid(cageid); - // unsafe { - // CAGE_TABLE[cageid as usize] - // .as_ref() - // .unwrap() - // .recvfrom_syscall(fd, buf, buflen, flag, &addr) - // }; if nullity1 && nullity2 { - // let addrlen = arg6 as u32; - // let mut addr = interface::get_sockaddr(Arg::from_u64_as_sockaddrstruct(start_address + arg5), addrlen).unwrap(); unsafe { CAGE_TABLE[cageid as usize] .as_ref() @@ -1010,7 +914,6 @@ pub extern "C" fn lind_syscall_api( .recvfrom_syscall(fd, buf, buflen, flag, &mut None) } } else if !(nullity1 || nullity2) { - // let addrlen = interface::get_socklen_t_ptr(Arg::from_u64_as_socklen_ptr(arg6)).unwrap(); let mut newsockaddr = interface::GenSockaddr::V4(interface::SockaddrV4::default()); //dummy value, rust would complain if we used an uninitialized value here let rv = unsafe { @@ -1033,17 +936,6 @@ pub extern "C" fn lind_syscall_api( } } - // SEND_SYSCALL => { - - // interface::check_cageid(cageid); - // unsafe { - // CAGE_TABLE[cageid as usize] - // .as_ref() - // .unwrap() - // .send_syscall(virtual_fd, cmd, arg) - // } - // } - FLOCK_SYSCALL => { let virtual_fd = arg1 as i32; let operation = arg2 as i32; @@ -1618,22 +1510,6 @@ pub extern "C" fn lind_syscall_api( } } - // WAIT_SYSCALL => { - // let mut status = 0; - // interface::check_cageid(cageid); - // let ret = unsafe { - // CAGE_TABLE[cageid as usize] - // .as_ref() - // .unwrap() - // .wait_syscall(&mut status) - // }; - - // let status_addr = (start_address + arg1) as *mut i32; - // unsafe { *status_addr = status; } - - // ret - // } - FUTEX_SYSCALL => { let uaddr = (start_address + arg1) as u64; let futex_op = arg2 as u32; @@ -1668,8 +1544,6 @@ pub extern "C" fn lind_syscall_api( _ => -1, // Return -1 for unknown syscalls }; - - // println!("------cage {} calling {} returns {}", cageid, call_name, ret); ret } From 78f3b5fe022db5659112fb48f7d1faaa6b359231 Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Wed, 2 Oct 2024 13:20:16 -0400 Subject: [PATCH 21/37] delete comment for src/lib.rs --- src/lib.rs | 203 ----------------------------------------------------- 1 file changed, 203 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8a616e7b..e7254501 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,206 +9,12 @@ // interface and safeposix are public because otherwise there isn't a great // way to 'use' them for benchmarking. pub mod interface; -// mod lib_fs_utils; pub mod safeposix; pub mod tests; pub mod fdtables; -// extern crate libc; -// pub mod librawposix { -// use libc::{ -// c_char, c_void, fd_set, itimerval, off_t, rlimit, sockaddr, socklen_t, ssize_t, statfs, -// timespec, timeval, -// }; - -// pub const LIND_SAFE_FS_ACCESS: i32 = 2; -// pub const LIND_SAFE_FS_UNLINK: i32 = 4; -// pub const LIND_SAFE_FS_LINK: i32 = 5; -// pub const LIND_SAFE_FS_RENAME: i32 = 6; - -// pub const LIND_SAFE_FS_XSTAT: i32 = 9; -// pub const LIND_SAFE_FS_OPEN: i32 = 10; -// pub const LIND_SAFE_FS_CLOSE: i32 = 11; -// pub const LIND_SAFE_FS_READ: i32 = 12; -// pub const LIND_SAFE_FS_WRITE: i32 = 13; -// pub const LIND_SAFE_FS_LSEEK: i32 = 14; -// pub const LIND_SAFE_FS_IOCTL: i32 = 15; -// pub const LIND_SAFE_FS_TRUNCATE: i32 = 16; -// pub const LIND_SAFE_FS_FXSTAT: i32 = 17; -// pub const LIND_SAFE_FS_FTRUNCATE: i32 = 18; -// pub const LIND_SAFE_FS_FSTATFS: i32 = 19; -// pub const LIND_SAFE_FS_MMAP: i32 = 21; -// pub const LIND_SAFE_FS_MUNMAP: i32 = 22; -// pub const LIND_SAFE_FS_GETDENTS: i32 = 23; -// pub const LIND_SAFE_FS_DUP: i32 = 24; -// pub const LIND_SAFE_FS_DUP2: i32 = 25; -// pub const LIND_SAFE_FS_STATFS: i32 = 26; -// pub const LIND_SAFE_FS_FCNTL: i32 = 28; - -// pub const LIND_SAFE_SYS_GETPPID: i32 = 29; -// pub const LIND_SAFE_SYS_EXIT: i32 = 30; -// pub const LIND_SAFE_SYS_GETPID: i32 = 31; - -// pub const LIND_SAFE_NET_BIND: i32 = 33; -// pub const LIND_SAFE_NET_SEND: i32 = 34; -// pub const LIND_SAFE_NET_SENDTO: i32 = 35; -// pub const LIND_SAFE_NET_RECV: i32 = 36; -// pub const LIND_SAFE_NET_RECVFROM: i32 = 37; -// pub const LIND_SAFE_NET_CONNECT: i32 = 38; -// pub const LIND_SAFE_NET_LISTEN: i32 = 39; -// pub const LIND_SAFE_NET_ACCEPT: i32 = 40; - -// pub const LIND_SAFE_NET_GETSOCKOPT: i32 = 43; -// pub const LIND_SAFE_NET_SETSOCKOPT: i32 = 44; -// pub const LIND_SAFE_NET_SHUTDOWN: i32 = 45; -// pub const LIND_SAFE_NET_SELECT: i32 = 46; -// pub const LIND_SAFE_FS_GETCWD: i32 = 47; -// pub const LIND_SAFE_NET_POLL: i32 = 48; -// pub const LIND_SAFE_NET_SOCKETPAIR: i32 = 49; -// pub const LIND_SAFE_SYS_GETUID: i32 = 50; -// pub const LIND_SAFE_SYS_GETEUID: i32 = 51; -// pub const LIND_SAFE_SYS_GETGID: i32 = 52; -// pub const LIND_SAFE_SYS_GETEGID: i32 = 53; -// pub const LIND_SAFE_FS_FLOCK: i32 = 54; - -// pub const LIND_SAFE_NET_EPOLL_CREATE: i32 = 56; -// pub const LIND_SAFE_NET_EPOLL_CTL: i32 = 57; -// pub const LIND_SAFE_NET_EPOLL_WAIT: i32 = 58; - -// pub const LIND_SAFE_FS_SHMGET: i32 = 62; -// pub const LIND_SAFE_FS_SHMAT: i32 = 63; -// pub const LIND_SAFE_FS_SHMDT: i32 = 64; -// pub const LIND_SAFE_FS_SHMCTL: i32 = 65; - -// pub const LIND_SAFE_FS_PIPE: i32 = 66; -// pub const LIND_SAFE_FS_PIPE2: i32 = 67; -// pub const LIND_SAFE_FS_FORK: i32 = 68; -// pub const LIND_SAFE_FS_EXEC: i32 = 69; - -// pub const LIND_SAFE_MUTEX_CREATE: i32 = 70; -// pub const LIND_SAFE_MUTEX_DESTROY: i32 = 71; -// pub const LIND_SAFE_MUTEX_LOCK: i32 = 72; -// pub const LIND_SAFE_MUTEX_TRYLOCK: i32 = 73; -// pub const LIND_SAFE_MUTEX_UNLOCK: i32 = 74; -// pub const LIND_SAFE_COND_CREATE: i32 = 75; -// pub const LIND_SAFE_COND_DESTROY: i32 = 76; -// pub const LIND_SAFE_COND_WAIT: i32 = 77; -// pub const LIND_SAFE_COND_BROADCAST: i32 = 78; -// pub const LIND_SAFE_COND_SIGNAL: i32 = 79; -// pub const LIND_SAFE_COND_TIMEDWAIT: i32 = 80; - -// pub const LIND_SAFE_SEM_INIT: i32 = 91; -// pub const LIND_SAFE_SEM_WAIT: i32 = 92; -// pub const LIND_SAFE_SEM_TRYWAIT: i32 = 93; -// pub const LIND_SAFE_SEM_TIMEDWAIT: i32 = 94; -// pub const LIND_SAFE_SEM_POST: i32 = 95; -// pub const LIND_SAFE_SEM_DESTROY: i32 = 96; -// pub const LIND_SAFE_SEM_GETVALUE: i32 = 97; - -// pub const LIND_SAFE_NET_GETHOSTNAME: i32 = 125; - -// pub const LIND_SAFE_FS_PREAD: i32 = 126; -// pub const LIND_SAFE_FS_PWRITE: i32 = 127; -// pub const LIND_SAFE_FS_CHDIR: i32 = 130; -// pub const LIND_SAFE_FS_MKDIR: i32 = 131; -// pub const LIND_SAFE_FS_RMDIR: i32 = 132; -// pub const LIND_SAFE_FS_CHMOD: i32 = 133; -// pub const LIND_SAFE_FS_FCHMOD: i32 = 134; - -// pub const LIND_SAFE_NET_SOCKET: i32 = 136; - -// pub const LIND_SAFE_NET_GETSOCKNAME: i32 = 144; -// pub const LIND_SAFE_NET_GETPEERNAME: i32 = 145; -// pub const LIND_SAFE_NET_GETIFADDRS: i32 = 146; -// pub const LIND_SAFE_SYS_SIGACTION: i32 = 147; -// pub const LIND_SAFE_SYS_KILL: i32 = 148; -// pub const LIND_SAFE_SYS_SIGPROCMASK: i32 = 149; -// pub const LIND_SAFE_SYS_LINDSETITIMER: i32 = 150; - -// pub const LIND_SAFE_FS_FCHDIR: i32 = 161; -// pub const LIND_SAFE_FS_FSYNC: i32 = 162; -// pub const LIND_SAFE_FS_FDATASYNC: i32 = 163; -// pub const LIND_SAFE_FS_SYNC_FILE_RANGE: i32 = 164; - -// pub const LIND_SAFE_SYS_CLONE: i32 = 171; - -// #[repr(C)] -// pub union RustArg { -// pub dispatch_int: i32, -// pub dispatch_uint: u32, -// pub dispatch_intptr: *mut i32, -// pub dispatch_ulong: u64, -// pub dispatch_ulong_long: u64, -// pub dispatch_long: i64, -// pub dispatch_size_t: usize, -// pub dispatch_ssize_t: ssize_t, -// pub dispatch_off_t: off_t, -// pub dispatch_socklen_t: socklen_t, -// pub dispatch_socklen_t_ptr: *mut socklen_t, -// pub dispatch_cbuf: *const c_void, -// pub dispatch_mutcbuf: *mut c_void, -// pub dispatch_cstr: *const c_char, -// pub dispatch_cstrarr: *const *const c_char, -// pub strdispatch_rlimitstruct: *mut rlimit, // Standard -// // pub dispatch_statstruct: *mut lind_stat, // Needs manual definition -// pub dispatch_statfsstruct: *mut statfs, // Standard -// pub dispatch_timevalstruct: *mut timeval, // Standard -// pub dispatch_timespecstruct: *mut timespec, // Standard -// pub dispatch_sockaddrstruct: *mut sockaddr, // Standard -// // pub dispatch_epolleventstruct: *mut epoll_event, // Standard -// pub dispatch_constsockaddrstruct: *mut sockaddr, // Standard -// // pub dispatch_shmidstruct: *mut lind_shmid_ds, // Needs manual definition -// pub dispatch_pipearray: *mut i32, -// // pub dispatch_naclabisigactionstruct: *mut nacl_abi_sigaction, // Needs manual definition -// // pub dispatch_constnaclabisigactionstruct: *const nacl_abi_sigaction, // Needs manual definition -// // pub dispatch_naclsigset: *mut u64, // Use u64 in Rust -// // pub dispatch_constnaclsigset: *const u64, // Use u64 in Rust -// pub dispatch_structitimerval: *mut itimerval, // Standard -// pub dispatch_conststructitimerval: *const itimerval, // Standard -// pub fdset: *mut fd_set, -// } - -// pub const BLANKARG: RustArg = RustArg { dispatch_ulong: 0 }; - -// #[link(name = "rawposix")] -// extern "C" { -// pub(crate) fn dispatcher( -// cageid: u64, -// callnum: i32, -// arg1: RustArg, -// arg2: RustArg, -// arg3: RustArg, -// arg4: RustArg, -// arg5: RustArg, -// arg6: RustArg, -// ); - -// pub(crate) fn lindrustinit(verbosity: i32); - -// pub(crate) fn lindrustfinalize(); - -// pub(crate) fn quick_write(fd: i32, buf: *const u8, count: usize, cageid: u64); - -// pub(crate) fn rustposix_thread_init(cageid: u64, signalflag: u64); - -// pub(crate) fn lind_syscall_api( -// cageid: u64, -// call_number: u32, -// call_name: u64, -// start_address: u64, -// arg1: u64, -// arg2: u64, -// arg3: u64, -// arg4: u64, -// arg5: u64, -// arg6: u64, -// ) -> u32; -// } -// } - use std::sync::{Condvar, Mutex}; -// use crate::librawposix::*; use crate::safeposix::dispatcher::*; #[macro_export] @@ -269,15 +75,6 @@ pub fn lind_rustposix_thread_init(cageid: u64, signalflag: u64) { pub fn lind_write_inner(fd: i32, buf: *const u8, count: usize, cageid: u64) { unsafe { quick_write(fd, buf, count, cageid); - // dispatch!( - // cageid, - // crate::librawposix::LIND_SAFE_FS_WRITE, - // RustArg { dispatch_int: fd }, - // RustArg { dispatch_cbuf: buf }, - // RustArg { - // dispatch_size_t: count - // } - // ) } } From 1288f4968799a8c88b750eecea757e9fc3eefd31 Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Wed, 2 Oct 2024 13:35:12 -0400 Subject: [PATCH 22/37] add newline to mod.rs --- mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mod.rs b/mod.rs index c60946ee..e9bd7b85 100644 --- a/mod.rs +++ b/mod.rs @@ -1 +1 @@ -pub mod rawposix; \ No newline at end of file +pub mod rawposix; From 6f9401364f4a2163cb8442df224ddc89455807fb Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Wed, 2 Oct 2024 13:41:55 -0400 Subject: [PATCH 23/37] add comment for nanosleep_time64 syscall --- src/safeposix/syscalls/fs_calls.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/safeposix/syscalls/fs_calls.rs b/src/safeposix/syscalls/fs_calls.rs index 36fe10a2..12fd70e1 100644 --- a/src/safeposix/syscalls/fs_calls.rs +++ b/src/safeposix/syscalls/fs_calls.rs @@ -1889,6 +1889,8 @@ impl Cage { unsafe { syscall(SYS_futex, uaddr, futex_op, val, val2, uaddr2, val3) as i32 } } + //We directly call nanosleep syscall(SYS_clock_nanosleep) from the libc + //return an `i32` value representing the result of the system call. pub fn nanosleep_time64_syscall(&self, clockid: u32, flags: i32, req: usize, rem: usize) -> i32 { unsafe { syscall(SYS_clock_nanosleep, clockid, flags, req, rem) as i32 } } From e50487e3fd60e2a74e2e13d9c91a4511b643f818 Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Mon, 21 Oct 2024 13:00:20 -0400 Subject: [PATCH 24/37] change type function from union to u64 --- src/interface/types.rs | 246 ++++------- src/safeposix/cage.rs | 2 +- src/safeposix/dispatcher.rs | 846 +----------------------------------- 3 files changed, 116 insertions(+), 978 deletions(-) diff --git a/src/interface/types.rs b/src/interface/types.rs index 7baa28f8..03039833 100644 --- a/src/interface/types.rs +++ b/src/interface/types.rs @@ -179,58 +179,6 @@ pub struct SigactionStruct { pub sa_flags: i32, } -//redefining the Arg union to maintain the flow of the program -#[derive(Copy, Clone)] -#[repr(C)] -pub union Arg { - pub dispatch_int: i32, - pub dispatch_uint: u32, - pub dispatch_ulong: u64, - pub dispatch_long: i64, - pub dispatch_usize: usize, //For types not specified to be a given length, but often set to word size (i.e. size_t) - pub dispatch_isize: isize, //For types not specified to be a given length, but often set to word size (i.e. off_t) - pub dispatch_cbuf: *const u8, //Typically corresponds to an immutable void* pointer as in write - pub dispatch_mutcbuf: *mut u8, //Typically corresponds to a mutable void* pointer as in read - pub dispatch_cstr: *const i8, //Typically corresponds to a passed in string of type char*, as in open - pub dispatch_cstrarr: *const *const i8, //Typically corresponds to a passed in string array of type char* const[] as in execve - // pub dispatch_rlimitstruct: *mut Rlimit, - // pub dispatch_statdatastruct: *mut stat, - pub dispatch_statdatastruct: *mut StatData, - // pub dispatch_fsdatastruct: *mut statfs, - pub dispatch_fsdatastruct: *mut FSData, - pub dispatch_shmidstruct: *mut ShmidsStruct, - pub dispatch_constsockaddrstruct: *const SockaddrDummy, - pub dispatch_sockaddrstruct: *mut SockaddrDummy, - // pub dispatch_constsockaddrstruct: *const sockaddr, - // pub dispatch_sockaddrstruct: *mut sockaddr, - pub dispatch_socklen_t_ptr: *mut u32, - pub dispatch_intptr: *mut i32, - pub dispatch_pollstructarray: *mut PollStruct, - pub dispatch_epollevent: *mut EpollEvent, - // pub dispatch_epollevent: *mut epoll_event, - // pub dispatch_structtimeval: *mut TimeVal, - pub dispatch_structtimeval: *mut timeval, - pub dispatch_structtimespec_lind: *mut TimeSpec, - pub dispatch_structtimespec: *mut timespec, - pub dispatch_pipearray: *mut PipeArray, - pub dispatch_sockpair: *mut SockPair, - // pub dispatch_ioctlptrunion: IoctlPtrUnion, - pub dispatch_ioctlptrunion: *mut u8, - pub dispatch_sigactionstruct: *mut SigactionStruct, - pub dispatch_constsigactionstruct: *const SigactionStruct, - pub dispatch_sigsett: *mut SigsetType, - pub dispatch_constsigsett: *const SigsetType, - pub dispatch_structitimerval: *mut ITimerVal, - // pub dispatch_structitimerval: *mut itimerval, - pub dispatch_conststructitimerval: *const ITimerVal, - // pub dispatch_conststructitimerval: *const itimerval, - // pub dispatch_fdset: *mut BitSet, - pub dispatch_fdset: *mut libc::fd_set, - // pub dispatch_structsem: *mut sem_t, - // pub dispatch_ifaddrs: *mut ifaddrs, - pub dispatch_constiovecstruct: *const interface::IovecStruct, -} - use std::mem::size_of; // Represents a Dirent struct without the string, as rust has no flexible array member support @@ -243,14 +191,11 @@ pub struct ClippedDirent { pub const CLIPPED_DIRENT_SIZE: u32 = size_of::() as u32; -pub fn get_int(union_argument: Arg) -> Result { - let data = unsafe { union_argument.dispatch_int }; - let mut type_checker = Arg { dispatch_long: 0 }; - //turn part of the union into 0xffffffff, but, Rust - //does not like just using the hex value so we are forced to use - //a value of -1 - type_checker.dispatch_int = -1; - if (unsafe { union_argument.dispatch_long } & !unsafe { type_checker.dispatch_long }) == 0 { +pub fn get_int(argument: u64) -> Result { + let data = argument as i32; + let type_checker = (!0xffffffff) as u64; + + if (argument & (!type_checker)) == 0 { return Ok(data); } return Err(syscall_error( @@ -260,11 +205,11 @@ pub fn get_int(union_argument: Arg) -> Result { )); } -pub fn get_uint(union_argument: Arg) -> Result { - let data = unsafe { union_argument.dispatch_uint }; - let mut type_checker = Arg { dispatch_ulong: 0 }; - type_checker.dispatch_uint = 0xffffffff; - if (unsafe { union_argument.dispatch_ulong } & !unsafe { type_checker.dispatch_ulong }) == 0 { +pub fn get_uint(argument: u64) -> Result { + let data = argument as u32; + let type_checker = (!0xffffffff) as u64; + + if (argument & (!type_checker)) == 0 { return Ok(data); } return Err(syscall_error( @@ -274,26 +219,26 @@ pub fn get_uint(union_argument: Arg) -> Result { )); } -pub fn get_long(union_argument: Arg) -> Result { - return Ok(unsafe { union_argument.dispatch_long }); //this should not return error +pub fn get_long(argument: u64) -> Result { + return Ok(argument as i64); //this should not return error } -pub fn get_ulong(union_argument: Arg) -> Result { - return Ok(unsafe { union_argument.dispatch_ulong }); //this should not return error +pub fn get_ulong(argument: u64) -> Result { + return Ok(argument); //this should not return error } -pub fn get_isize(union_argument: Arg) -> Result { +pub fn get_isize(argument: u64) -> Result { // also should not return error - return Ok(unsafe { union_argument.dispatch_isize }); + return Ok(argument as isize); } -pub fn get_usize(union_argument: Arg) -> Result { +pub fn get_usize(argument: u64) -> Result { //should not return an error - return Ok(unsafe { union_argument.dispatch_usize }); + return Ok(argument as usize); } -pub fn get_cbuf(union_argument: Arg) -> Result<*const u8, i32> { - let data = unsafe { union_argument.dispatch_cbuf }; +pub fn get_cbuf(argument: u64) -> Result<*const u8, i32> { + let data = argument as *const u8; if !data.is_null() { return Ok(data); } @@ -304,8 +249,8 @@ pub fn get_cbuf(union_argument: Arg) -> Result<*const u8, i32> { )); } -pub fn get_mutcbuf(union_argument: Arg) -> Result<*mut u8, i32> { - let data = unsafe { union_argument.dispatch_mutcbuf }; +pub fn get_mutcbuf(argument: u64) -> Result<*mut u8, i32> { + let data = argument as *mut u8; if !data.is_null() { return Ok(data); } @@ -317,16 +262,17 @@ pub fn get_mutcbuf(union_argument: Arg) -> Result<*mut u8, i32> { } // for the case where the buffer pointer being Null is normal -pub fn get_mutcbuf_null(union_argument: Arg) -> Result, i32> { - let data = unsafe { union_argument.dispatch_mutcbuf }; +pub fn get_mutcbuf_null(argument: u64) -> Result, i32> { + let data = argument as *mut u8; if !data.is_null() { return Ok(Some(data)); } return Ok(None); } -pub fn get_fdset(union_argument: Arg) -> Result, i32> { - let data: *mut libc::fd_set = unsafe { union_argument.dispatch_fdset }; + +pub fn get_fdset(argument: u64) -> Result, i32> { + let data = argument as *mut libc::fd_set; if !data.is_null() { // let internal_fds: &mut interface::FdSet = interface::FdSet::new_from_ptr(data); let internal_fds = unsafe { &mut *(data as *mut fd_set) }; @@ -352,11 +298,11 @@ pub fn get_fdset(union_argument: Arg) -> Result, i32 // return Ok(pointer); // } -pub fn get_cstr<'a>(union_argument: Arg) -> Result<&'a str, i32> { +pub fn get_cstr<'a>(argument: u64) -> Result<&'a str, i32> { //first we check that the pointer is not null //and then we check so that we can get data from the memory - let pointer = unsafe { union_argument.dispatch_cstr }; + let pointer = argument as *const i8; if !pointer.is_null() { if let Ok(ret_data) = unsafe { interface::charstar_to_ruststr(pointer) } { return Ok(ret_data); @@ -375,13 +321,13 @@ pub fn get_cstr<'a>(union_argument: Arg) -> Result<&'a str, i32> { )); } -pub fn get_cstrarr<'a>(union_argument: Arg) -> Result, i32> { +pub fn get_cstrarr<'a>(argument: u64) -> Result, i32> { //iterate though the pointers in a function and: // 1: check that the pointer is not null // 2: push the data from that pointer onto the vector being returned //once we encounter a null pointer, we know that we have either hit the end of the array or another null pointer in the memory - let mut pointer = unsafe { union_argument.dispatch_cstrarr }; + let mut pointer = argument as *const *const i8; let mut data_vector: Vec<&str> = Vec::new(); if !pointer.is_null() { @@ -417,8 +363,8 @@ pub fn get_cstrarr<'a>(union_argument: Arg) -> Result, i32> { // "input data not valid", // )); // } -pub fn get_statdatastruct<'a>(union_argument: Arg) -> Result<&'a mut StatData, i32> { - let pointer = unsafe { union_argument.dispatch_statdatastruct }; +pub fn get_statdatastruct<'a>(argument: u64) -> Result<&'a mut StatData, i32> { + let pointer = argument as *mut StatData; if !pointer.is_null() { return Ok(unsafe { &mut *pointer }); } @@ -440,8 +386,8 @@ pub fn get_statdatastruct<'a>(union_argument: Arg) -> Result<&'a mut StatData, i // "input data not valid", // )); // } -pub fn get_fsdatastruct<'a>(union_argument: Arg) -> Result<&'a mut FSData, i32> { - let pointer = unsafe { union_argument.dispatch_fsdatastruct }; +pub fn get_fsdatastruct<'a>(argument: u64) -> Result<&'a mut FSData, i32> { + let pointer = argument as *mut FSData; if !pointer.is_null() { return Ok(unsafe { &mut *pointer }); } @@ -452,8 +398,8 @@ pub fn get_fsdatastruct<'a>(union_argument: Arg) -> Result<&'a mut FSData, i32> )); } -pub fn get_shmidstruct<'a>(union_argument: Arg) -> Result<&'a mut ShmidsStruct, i32> { - let pointer = unsafe { union_argument.dispatch_shmidstruct }; +pub fn get_shmidstruct<'a>(argument: u64) -> Result<&'a mut ShmidsStruct, i32> { + let pointer = argument as *mut ShmidsStruct; if !pointer.is_null() { return Ok(unsafe { &mut *pointer }); } @@ -464,8 +410,8 @@ pub fn get_shmidstruct<'a>(union_argument: Arg) -> Result<&'a mut ShmidsStruct, )); } -pub fn get_ioctlptrunion<'a>(union_argument: Arg) -> Result<&'a mut u8, i32> { - let pointer = unsafe { union_argument.dispatch_ioctlptrunion }; +pub fn get_ioctlptrunion<'a>(argument: u64) -> Result<&'a mut u8, i32> { + let pointer = argument as *mut u8; if !pointer.is_null() { return Ok(unsafe { &mut *pointer @@ -539,8 +485,8 @@ pub fn get_ioctlptrunion<'a>(union_argument: Arg) -> Result<&'a mut u8, i32> { // } // } -pub fn get_pipearray<'a>(union_argument: Arg) -> Result<&'a mut PipeArray, i32> { - let pointer = unsafe { union_argument.dispatch_pipearray }; +pub fn get_pipearray<'a>(argument: u64) -> Result<&'a mut PipeArray, i32> { + let pointer = argument as *mut PipeArray; if !pointer.is_null() { return Ok(unsafe { &mut *pointer }); } @@ -551,8 +497,8 @@ pub fn get_pipearray<'a>(union_argument: Arg) -> Result<&'a mut PipeArray, i32> )); } -pub fn get_sockpair<'a>(union_argument: Arg) -> Result<&'a mut SockPair, i32> { - let pointer = unsafe { union_argument.dispatch_sockpair }; +pub fn get_sockpair<'a>(argument: u64) -> Result<&'a mut SockPair, i32> { + let pointer = argument as *mut SockPair; if !pointer.is_null() { return Ok(unsafe { &mut *pointer }); } @@ -575,8 +521,8 @@ pub fn get_sockpair<'a>(union_argument: Arg) -> Result<&'a mut SockPair, i32> { // )); // } -pub fn get_constsockaddr<'a>(union_argument: Arg) -> Result<&'a SockaddrDummy, i32> { - let pointer = unsafe { union_argument.dispatch_constsockaddrstruct }; +pub fn get_constsockaddr<'a>(argument: u64) -> Result<&'a SockaddrDummy, i32> { + let pointer = argument as *const SockaddrDummy; if !pointer.is_null() { return Ok(unsafe { & *pointer }); } @@ -587,8 +533,8 @@ pub fn get_constsockaddr<'a>(union_argument: Arg) -> Result<&'a SockaddrDummy, i )); } -pub fn get_sockaddr(union_argument: Arg, addrlen: u32) -> Result { - let pointer = unsafe { union_argument.dispatch_constsockaddrstruct }; +pub fn get_sockaddr(argument: u64, addrlen: u32) -> Result { + let pointer = argument as *const SockaddrDummy; if !pointer.is_null() { let tmpsock = unsafe { &*pointer }; match tmpsock.sa_family { @@ -646,9 +592,9 @@ pub fn get_sockaddr(union_argument: Arg, addrlen: u32) -> Result Result { - let received = unsafe { union_argument.dispatch_sockaddrstruct }; - let received_addrlen = unsafe { len_argument.dispatch_socklen_t_ptr } as u32; +pub fn set_gensockaddr(argument: u64, argument1: u64) -> Result { + let received = argument as *mut SockaddrDummy; + let received_addrlen = (argument1 as *mut u32) as u32; let tmpsock = unsafe { &*received }; // println!("[Dispatcher set_gen] family: {:?}", tmpsock.sa_family); // println!("[Dispathcer set_gen] len: {:?}", received_addrlen); @@ -710,9 +656,9 @@ pub fn set_gensockaddr(union_argument: Arg, len_argument: Arg) -> Result( - union_argument: Arg, + argument: u64, nfds: usize, ) -> Result<&'a mut [PollStruct], i32> { - let pollstructptr = unsafe { union_argument.dispatch_pollstructarray }; + let pollstructptr = argument as *mut PollStruct; if !pollstructptr.is_null() { return Ok(unsafe { std::slice::from_raw_parts_mut(pollstructptr, nfds) }); } @@ -795,10 +741,10 @@ pub fn get_pollstruct_slice<'a>( } pub fn get_epollevent_slice<'a>( - union_argument: Arg, + argument: u64, nfds: i32, ) -> Result<&'a mut [EpollEvent], i32> { - let epolleventptr = unsafe { union_argument.dispatch_epollevent }; + let epolleventptr = argument as *mut EpollEvent; if !epolleventptr.is_null() { return Ok(unsafe { std::slice::from_raw_parts_mut(epolleventptr, nfds as usize) }); } @@ -809,8 +755,8 @@ pub fn get_epollevent_slice<'a>( )); } -pub fn get_slice_from_string<'a>(union_argument: Arg, len: usize) -> Result<&'a mut [u8], i32> { - let bufptr = unsafe { union_argument.dispatch_mutcbuf }; +pub fn get_slice_from_string<'a>(argument: u64, len: usize) -> Result<&'a mut [u8], i32> { + let bufptr = argument as *mut u8; if bufptr.is_null() { return Ok(unsafe { std::slice::from_raw_parts_mut(bufptr, len as usize) }); } @@ -821,8 +767,8 @@ pub fn get_slice_from_string<'a>(union_argument: Arg, len: usize) -> Result<&'a )); } -pub fn get_epollevent<'a>(union_argument: Arg) -> Result<&'a mut EpollEvent, i32> { - let epolleventptr = unsafe { union_argument.dispatch_epollevent }; +pub fn get_epollevent<'a>(argument: u64) -> Result<&'a mut EpollEvent, i32> { + let epolleventptr = argument as *mut EpollEvent; if !epolleventptr.is_null() { return Ok(unsafe { &mut *epolleventptr }); } @@ -844,8 +790,8 @@ pub fn get_epollevent<'a>(union_argument: Arg) -> Result<&'a mut EpollEvent, i32 // )); // } -pub fn get_socklen_t_ptr(union_argument: Arg) -> Result { - let socklenptr = unsafe { union_argument.dispatch_socklen_t_ptr }; +pub fn get_socklen_t_ptr(argument: u64) -> Result { + let socklenptr = argument as *mut u32; if !socklenptr.is_null() { return Ok(unsafe { *socklenptr }); } @@ -857,18 +803,18 @@ pub fn get_socklen_t_ptr(union_argument: Arg) -> Result { } //arg checked for nullity beforehand -pub fn get_int_from_intptr(union_argument: Arg) -> i32 { - return unsafe { *union_argument.dispatch_intptr }; +pub fn get_int_from_intptr(argument: u64) -> i32 { + return unsafe { *(argument as *mut i32)}; } -pub fn copy_out_intptr(union_argument: Arg, intval: i32) { +pub fn copy_out_intptr(argument: u64, intval: i32) { unsafe { - *union_argument.dispatch_intptr = intval; + *(argument as *mut i32) = intval; } } -pub fn duration_fromtimeval(union_argument: Arg) -> Result, i32> { - let pointer = unsafe { union_argument.dispatch_structtimeval }; +pub fn duration_fromtimeval(argument: u64) -> Result, i32> { + let pointer = argument as *mut timeval; if !pointer.is_null() { let times = unsafe { &mut *pointer }; return Ok(Some(interface::RustDuration::new( @@ -880,8 +826,8 @@ pub fn duration_fromtimeval(union_argument: Arg) -> Result(union_argument: Arg) -> Result<&'a mut timeval, i32> { - let pointer = unsafe { union_argument.dispatch_structtimeval }; +pub fn get_timerval<'a>(argument: u64) -> Result<&'a mut timeval, i32> { + let pointer = argument as *mut timeval; if !pointer.is_null() { return Ok(unsafe { &mut *pointer }); } @@ -892,8 +838,8 @@ pub fn get_timerval<'a>(union_argument: Arg) -> Result<&'a mut timeval, i32> { )); } -pub fn get_itimerval<'a>(union_argument: Arg) -> Result, i32> { - let pointer = unsafe { union_argument.dispatch_structitimerval }; +pub fn get_itimerval<'a>(argument: u64) -> Result, i32> { + let pointer = argument as *mut ITimerVal; if !pointer.is_null() { Ok(Some(unsafe { &mut *pointer })) } else { @@ -912,8 +858,8 @@ pub fn get_itimerval<'a>(union_argument: Arg) -> Result(union_argument: Arg) -> Result, i32> { - let pointer = unsafe { union_argument.dispatch_conststructitimerval }; +pub fn get_constitimerval<'a>(argument: u64) -> Result, i32> { + let pointer = argument as *const ITimerVal; if !pointer.is_null() { Ok(Some(unsafe { &*pointer })) } else { @@ -933,8 +879,8 @@ pub fn get_constitimerval<'a>(union_argument: Arg) -> Result Result { - let pointer = unsafe { union_argument.dispatch_structtimespec_lind }; +pub fn duration_fromtimespec(argument: u64) -> Result { + let pointer = argument as *mut TimeSpec; if !pointer.is_null() { let times = unsafe { &mut *pointer }; if times.tv_nsec < 0 || times.tv_nsec >= 1000000000 { @@ -957,8 +903,8 @@ pub fn duration_fromtimespec(union_argument: Arg) -> Result(union_argument: Arg) -> Result<&'a timespec, i32> { - let pointer = unsafe { union_argument.dispatch_structtimespec }; +pub fn get_timespec<'a>(argument: u64) -> Result<&'a timespec, i32> { + let pointer = argument as *mut timespec; if !pointer.is_null() { return Ok( unsafe { &*pointer @@ -971,10 +917,12 @@ pub fn get_timespec<'a>(union_argument: Arg) -> Result<&'a timespec, i32> { )); } + + pub fn get_duration_from_millis( - union_argument: Arg, + argument: u64, ) -> Result, i32> { - let posstimemillis = get_int(union_argument); + let posstimemillis = get_int(argument); match posstimemillis { Ok(timemillis) => { if timemillis >= 0 { @@ -989,14 +937,12 @@ pub fn get_duration_from_millis( } } -pub fn arg_nullity(union_argument: &Arg) -> bool { - unsafe { union_argument.dispatch_cbuf }.is_null() +pub fn arg_nullity(argument: u64) -> bool { + (argument as *const u8).is_null() } -pub fn get_sigactionstruct<'a>( - union_argument: Arg, -) -> Result, i32> { - let pointer = unsafe { union_argument.dispatch_sigactionstruct }; +pub fn get_sigactionstruct<'a>(argument: u64) -> Result, i32> { + let pointer = argument as *mut SigactionStruct; if !pointer.is_null() { Ok(Some(unsafe { &mut *pointer })) @@ -1005,10 +951,8 @@ pub fn get_sigactionstruct<'a>( } } -pub fn get_constsigactionstruct<'a>( - union_argument: Arg, -) -> Result, i32> { - let pointer = unsafe { union_argument.dispatch_constsigactionstruct }; +pub fn get_constsigactionstruct<'a>(argument: u64) -> Result, i32> { + let pointer = argument as *const SigactionStruct; if !pointer.is_null() { Ok(Some(unsafe { &*pointer })) @@ -1017,8 +961,8 @@ pub fn get_constsigactionstruct<'a>( } } -pub fn get_sigsett<'a>(union_argument: Arg) -> Result, i32> { - let pointer = unsafe { union_argument.dispatch_sigsett }; +pub fn get_sigsett<'a>(argument: u64) -> Result, i32> { + let pointer = argument as *mut u64; if !pointer.is_null() { Ok(Some(unsafe { &mut *pointer })) @@ -1027,8 +971,8 @@ pub fn get_sigsett<'a>(union_argument: Arg) -> Result } } -pub fn get_constsigsett<'a>(union_argument: Arg) -> Result, i32> { - let pointer = unsafe { union_argument.dispatch_constsigsett }; +pub fn get_constsigsett<'a>(argument: u64) -> Result, i32> { + let pointer = argument as *const SigsetType; if !pointer.is_null() { Ok(Some(unsafe { &*pointer })) @@ -1037,8 +981,8 @@ pub fn get_constsigsett<'a>(union_argument: Arg) -> Result Result<*const interface::IovecStruct, i32> { - let data = unsafe { union_argument.dispatch_constiovecstruct }; +pub fn get_iovecstruct(argument: u64) -> Result<*const interface::IovecStruct, i32> { + let data = argument as *const interface::IovecStruct; if !data.is_null() { return Ok(data); } diff --git a/src/safeposix/cage.rs b/src/safeposix/cage.rs index 60127ea6..946b8e35 100644 --- a/src/safeposix/cage.rs +++ b/src/safeposix/cage.rs @@ -7,7 +7,7 @@ pub use crate::interface::errnos::{syscall_error, Errno}; // }; pub use crate::interface::types::{ - Arg, EpollEvent, IoctlPtrUnion, PipeArray, PollStruct, + EpollEvent, IoctlPtrUnion, PipeArray, PollStruct, }; use super::filesystem::normpath; diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index d57fe479..ff5a529b 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -220,66 +220,6 @@ fn u64_to_str(ptr: u64) -> Result<&'static str, Utf8Error> { } } -impl Arg { - pub fn from_u64_as_cbuf(value: u64) -> Self { - Arg { - dispatch_cbuf: value as *const u8, - } - } - - pub fn from_u64_as_statstruct(value: u64) -> Self { - Arg { - dispatch_statdatastruct: value as *mut interface::StatData, - } - } - - pub fn from_u64_as_pollstructarray(value: u64) -> Self { - Arg { - dispatch_pollstructarray: value as *mut interface::PollStruct, - } - } - - pub fn from_u64_as_pipearray(value: u64) -> Self { - Arg { - dispatch_pipearray: value as *mut interface::PipeArray, - } - } - - pub fn from_u64_as_sockpair(value: u64) -> Self { - Arg { - dispatch_sockpair: value as *mut interface::SockPair, - } - } - - pub fn from_u64_as_socklen_ptr(value: u64) -> Self { - Arg { - dispatch_socklen_t_ptr: value as *mut u32, - } - } - - pub fn from_u64_as_sockaddrstruct(value: u64) -> Self { - Arg { - dispatch_constsockaddrstruct: value as *const SockaddrDummy, - } - } - pub fn from_u64_as_constsigactionstruct(value: u64) -> Self { - Arg { - dispatch_constsigactionstruct: value as *const SigactionStruct, - } - } -} - -fn parse_null_terminated_string(ptr: *const std::os::raw::c_char) -> Result { - // Convert the pointer to a CStr, which is a reference to a null-terminated string - let c_str = unsafe { - assert!(!ptr.is_null(), "Received a null pointer"); - std::ffi::CStr::from_ptr(ptr) - }; - - // Convert the CStr to a Rust String - c_str.to_str().map(|s| s.to_owned()) -} - #[no_mangle] pub extern "C" fn lind_syscall_api( cageid: u64, @@ -435,7 +375,7 @@ pub extern "C" fn lind_syscall_api( CONNECT_SYSCALL => { let fd = arg1 as i32; let addrlen = arg3 as u32; - let addr = get_onearg!(interface::get_sockaddr(Arg::from_u64_as_sockaddrstruct(arg2), addrlen)); + let addr = get_onearg!(interface::get_sockaddr(arg2, addrlen)); interface::check_cageid(cageid); unsafe { let remoteaddr = match Ok::<&interface::GenSockaddr, i32>(&addr) { @@ -452,7 +392,7 @@ pub extern "C" fn lind_syscall_api( BIND_SYSCALL => { let fd = arg1 as i32; let addrlen = arg3 as u32; - let addr = interface::get_sockaddr(Arg::from_u64_as_sockaddrstruct(start_address + arg2), addrlen).unwrap(); + let addr = interface::get_sockaddr(start_address + arg2, addrlen).unwrap(); interface::check_cageid(cageid); unsafe { let localaddr = match Ok::<&interface::GenSockaddr, i32>(&addr) { @@ -468,8 +408,8 @@ pub extern "C" fn lind_syscall_api( ACCEPT_SYSCALL => { let mut addr = interface::GenSockaddr::V4(interface::SockaddrV4::default()); //value doesn't matter - let nullity1 = interface::arg_nullity(&Arg::from_u64_as_cbuf(arg2)); - let nullity2 = interface::arg_nullity(&Arg::from_u64_as_cbuf(arg3)); + let nullity1 = interface::arg_nullity(arg2); + let nullity2 = interface::arg_nullity(arg3); if nullity1 && nullity2 { interface::check_cageid(cageid); @@ -488,7 +428,7 @@ pub extern "C" fn lind_syscall_api( .accept_syscall(arg1 as i32, &mut Some(&mut addr)) }; if rv >= 0 { - interface::copy_out_sockaddr(Arg::from_u64_as_sockaddrstruct(start_address + arg2), Arg::from_u64_as_socklen_ptr(start_address + arg3), addr); + interface::copy_out_sockaddr((start_address + arg2), (start_address + arg3), addr); } rv } else { @@ -545,7 +485,7 @@ pub extern "C" fn lind_syscall_api( XSTAT_SYSCALL => { let fd_ptr = (start_address + arg1) as *const u8; - let buf = match interface::get_statdatastruct(Arg::from_u64_as_statstruct(start_address + arg2)) { + let buf = match interface::get_statdatastruct(start_address + arg2) { Ok(val) => val, Err(errno) => { return errno; @@ -640,7 +580,7 @@ pub extern "C" fn lind_syscall_api( FSTATFS_SYSCALL => { let fd = arg1 as i32; - let buf = interface::get_fsdatastruct(Arg::from_u64_as_cbuf(start_address + arg2)).unwrap(); + let buf = interface::get_fsdatastruct(start_address + arg2).unwrap(); interface::check_cageid(cageid); unsafe { CAGE_TABLE[cageid as usize] @@ -713,7 +653,7 @@ pub extern "C" fn lind_syscall_api( FXSTAT_SYSCALL => { let fd = arg1 as i32; - let buf = interface::get_statdatastruct(Arg::from_u64_as_cbuf(start_address + arg2)).unwrap(); + let buf = interface::get_statdatastruct(start_address + arg2).unwrap(); interface::check_cageid(cageid); unsafe { @@ -834,7 +774,7 @@ pub extern "C" fn lind_syscall_api( STATFS_SYSCALL => { let fd_ptr = (start_address + arg1) as *const u8; - let rposix_databuf = interface::get_fsdatastruct(Arg::from_u64_as_cbuf(start_address + arg2)).unwrap(); + let rposix_databuf = interface::get_fsdatastruct(start_address + arg2).unwrap(); let fd = unsafe { CStr::from_ptr(fd_ptr as *const i8).to_str().unwrap() @@ -885,7 +825,7 @@ pub extern "C" fn lind_syscall_api( let flag = arg4 as i32; let addrlen = arg6 as u32; - let addr = interface::get_sockaddr(Arg::from_u64_as_sockaddrstruct(start_address + arg5), addrlen).unwrap(); + let addr = interface::get_sockaddr(start_address + arg5, addrlen).unwrap(); interface::check_cageid(cageid); unsafe { @@ -901,8 +841,8 @@ pub extern "C" fn lind_syscall_api( let buf = (start_address + arg2) as *mut u8; let buflen = arg3 as usize; let flag = arg4 as i32; - let nullity1 = interface::arg_nullity(&Arg::from_u64_as_sockaddrstruct(arg5)); - let nullity2 = interface::arg_nullity(&Arg::from_u64_as_socklen_ptr(arg6)); + let nullity1 = interface::arg_nullity(arg5); + let nullity2 = interface::arg_nullity(arg6); interface::check_cageid(cageid); @@ -924,7 +864,7 @@ pub extern "C" fn lind_syscall_api( }; if rv >= 0 { - interface::copy_out_sockaddr(Arg::from_u64_as_sockaddrstruct(start_address + arg5), Arg::from_u64_as_socklen_ptr(start_address + arg6), newsockaddr); + interface::copy_out_sockaddr(start_address + arg5, start_address + arg6, newsockaddr); } rv } else { @@ -1390,7 +1330,7 @@ pub extern "C" fn lind_syscall_api( } PIPE_SYSCALL => { - let pipe = interface::get_pipearray(Arg::from_u64_as_pipearray(start_address + arg1)).unwrap(); + let pipe = interface::get_pipearray(start_address + arg1).unwrap(); interface::check_cageid(cageid); unsafe { @@ -1401,7 +1341,7 @@ pub extern "C" fn lind_syscall_api( } } PIPE2_SYSCALL => { - let pipe = interface::get_pipearray(Arg::from_u64_as_pipearray(start_address + arg1)).unwrap(); + let pipe = interface::get_pipearray(start_address + arg1).unwrap(); let flag = arg2 as i32; interface::check_cageid(cageid); @@ -1417,11 +1357,11 @@ pub extern "C" fn lind_syscall_api( let fd = arg1 as i32; // let addrlen = arg3 as u32; - // let mut addr = interface::get_sockaddr(Arg::from_u64_as_sockaddrstruct(start_address + arg2), addrlen).unwrap(); + // let mut addr = interface::get_sockaddr(start_address + arg2), addrlen).unwrap(); let mut addr = interface::GenSockaddr::V4(interface::SockaddrV4::default()); //value doesn't matter - if interface::arg_nullity(&Arg::from_u64_as_sockaddrstruct(arg2)) || interface::arg_nullity(&Arg::from_u64_as_socklen_ptr(arg3)) { + if interface::arg_nullity(arg2) || interface::arg_nullity(arg3) { return syscall_error( Errno::EINVAL, "getsockname", @@ -1438,7 +1378,7 @@ pub extern "C" fn lind_syscall_api( }; if rv >= 0 { - interface::copy_out_sockaddr(Arg::from_u64_as_sockaddrstruct(start_address + arg2), Arg::from_u64_as_socklen_ptr(start_address + arg3), addr); + interface::copy_out_sockaddr(start_address + arg2, start_address + arg3, addr); } rv } @@ -1464,7 +1404,7 @@ pub extern "C" fn lind_syscall_api( let domain = arg1 as i32; let _type = arg2 as i32; let protocol = arg3 as i32; - let virtual_socket_vector = interface::get_sockpair(Arg::from_u64_as_sockpair(start_address + arg4)).unwrap(); + let virtual_socket_vector = interface::get_sockpair(start_address + arg4).unwrap(); interface::check_cageid(cageid); unsafe { @@ -1477,7 +1417,7 @@ pub extern "C" fn lind_syscall_api( POLL_SYSCALL => { let nfds = arg2 as u64; - let pollfds = interface::get_pollstruct_slice(Arg::from_u64_as_pollstructarray(start_address + arg1), nfds as usize).unwrap(); + let pollfds = interface::get_pollstruct_slice(start_address + arg1, nfds as usize).unwrap(); let timeout = arg3 as i32; interface::check_cageid(cageid); @@ -1547,752 +1487,6 @@ pub extern "C" fn lind_syscall_api( ret } -#[no_mangle] -pub extern "C" fn dispatcher( - cageid: u64, - callnum: i32, - arg1: Arg, - arg2: Arg, - arg3: Arg, - arg4: Arg, - arg5: Arg, - arg6: Arg, -) -> i32 { - // need to match based on if cage exists - let cage = interface::cagetable_getref(cageid); - - match callnum { - ACCESS_SYSCALL => { - check_and_dispatch!( - cage.access_syscall, - interface::get_cstr(arg1), - interface::get_int(arg2) - ) - } - UNLINK_SYSCALL => { - check_and_dispatch!(cage.unlink_syscall, interface::get_cstr(arg1)) - } - LINK_SYSCALL => { - check_and_dispatch!( - cage.link_syscall, - interface::get_cstr(arg1), - interface::get_cstr(arg2) - ) - } - CHDIR_SYSCALL => { - check_and_dispatch!(cage.chdir_syscall, interface::get_cstr(arg1)) - } - FSYNC_SYSCALL => { - check_and_dispatch!(cage.fsync_syscall, interface::get_int(arg1)) - } - FDATASYNC_SYSCALL => { - check_and_dispatch!(cage.fdatasync_syscall, interface::get_int(arg1)) - } - SYNC_FILE_RANGE => { - check_and_dispatch!( - cage.sync_file_range_syscall, - interface::get_int(arg1), - interface::get_isize(arg2), - interface::get_isize(arg3), - interface::get_uint(arg4) - ) - } - FCHDIR_SYSCALL => { - check_and_dispatch!(cage.fchdir_syscall, interface::get_int(arg1)) - } - XSTAT_SYSCALL => { - check_and_dispatch!( - cage.stat_syscall, - interface::get_cstr(arg1), - interface::get_statdatastruct(arg2) - ) - } - OPEN_SYSCALL => { - check_and_dispatch!( - cage.open_syscall, - interface::get_cstr(arg1), - interface::get_int(arg2), - interface::get_uint(arg3) - ) - } - READ_SYSCALL => { - check_and_dispatch!( - cage.read_syscall, - interface::get_int(arg1), - interface::get_mutcbuf(arg2), - interface::get_usize(arg3) - ) - } - WRITE_SYSCALL => { - check_and_dispatch!( - cage.write_syscall, - interface::get_int(arg1), - interface::get_mutcbuf(arg2), - interface::get_usize(arg3) - ) - } - CLOSE_SYSCALL => { - check_and_dispatch!(cage.close_syscall, interface::get_int(arg1)) - } - LSEEK_SYSCALL => { - check_and_dispatch!( - cage.lseek_syscall, - interface::get_int(arg1), - interface::get_isize(arg2), - interface::get_int(arg3) - ) - } - FXSTAT_SYSCALL => { - check_and_dispatch!( - cage.fstat_syscall, - interface::get_int(arg1), - interface::get_statdatastruct(arg2) - ) - } - FSTATFS_SYSCALL => { - check_and_dispatch!( - cage.fstatfs_syscall, - interface::get_int(arg1), - interface::get_fsdatastruct(arg2) - ) - } - MMAP_SYSCALL => { - check_and_dispatch!( - cage.mmap_syscall, - interface::get_mutcbuf(arg1), - interface::get_usize(arg2), - interface::get_int(arg3), - interface::get_int(arg4), - interface::get_int(arg5), - interface::get_long(arg6) - ) - } - MUNMAP_SYSCALL => { - check_and_dispatch!( - cage.munmap_syscall, - interface::get_mutcbuf(arg1), - interface::get_usize(arg2) - ) - } - DUP_SYSCALL => { - check_and_dispatch!( - cage.dup_syscall, - interface::get_int(arg1), - Ok::, i32>(None) - ) - } - DUP2_SYSCALL => { - check_and_dispatch!( - cage.dup2_syscall, - interface::get_int(arg1), - interface::get_int(arg2) - ) - } - STATFS_SYSCALL => { - check_and_dispatch!( - cage.statfs_syscall, - interface::get_cstr(arg1), - interface::get_fsdatastruct(arg2) - ) - } - FCNTL_SYSCALL => { - check_and_dispatch!( - cage.fcntl_syscall, - interface::get_int(arg1), - interface::get_int(arg2), - interface::get_int(arg3) - ) - } - IOCTL_SYSCALL => { - check_and_dispatch!( - cage.ioctl_syscall, - interface::get_int(arg1), - interface::get_ulong(arg2), - interface::get_ioctlptrunion(arg3) - ) - } - GETPPID_SYSCALL => { - check_and_dispatch!(cage.getppid_syscall,) - } - GETPID_SYSCALL => { - check_and_dispatch!(cage.getpid_syscall,) - } - SOCKET_SYSCALL => { - check_and_dispatch!( - cage.socket_syscall, - interface::get_int(arg1), - interface::get_int(arg2), - interface::get_int(arg3) - ) - } - BIND_SYSCALL => { - let addrlen = get_onearg!(interface::get_uint(arg3)); - let addr = get_onearg!(interface::get_sockaddr(arg2, addrlen)); - check_and_dispatch!( - cage.bind_syscall, - interface::get_int(arg1), - Ok::<&interface::GenSockaddr, i32>(&addr) - ) - } - SEND_SYSCALL => { - check_and_dispatch!( - cage.send_syscall, - interface::get_int(arg1), - interface::get_cbuf(arg2), - interface::get_usize(arg3), - interface::get_int(arg4) - ) - } - SENDTO_SYSCALL => { - let addrlen = get_onearg!(interface::get_uint(arg6)); - let addr = get_onearg!(interface::get_sockaddr(arg5, addrlen)); - check_and_dispatch!( - cage.sendto_syscall, - interface::get_int(arg1), - interface::get_cbuf(arg2), - interface::get_usize(arg3), - interface::get_int(arg4), - Ok::<&interface::GenSockaddr, i32>(&addr) - ) - } - RECV_SYSCALL => { - check_and_dispatch!( - cage.recv_syscall, - interface::get_int(arg1), - interface::get_mutcbuf(arg2), - interface::get_usize(arg3), - interface::get_int(arg4) - ) - } - RECVFROM_SYSCALL => { - let nullity1 = interface::arg_nullity(&arg5); - let nullity2 = interface::arg_nullity(&arg6); - - if nullity1 && nullity2 { - check_and_dispatch!( - cage.recvfrom_syscall, - interface::get_int(arg1), - interface::get_mutcbuf(arg2), - interface::get_usize(arg3), - interface::get_int(arg4), - Ok::<&mut Option<&mut interface::GenSockaddr>, i32>(&mut None) - ) - } else if !(nullity1 || nullity2) { - let addrlen = get_onearg!(interface::get_socklen_t_ptr(arg6)); - let mut newsockaddr = interface::GenSockaddr::V4(interface::SockaddrV4::default()); //dummy value, rust would complain if we used an uninitialized value here - let rv = check_and_dispatch!( - cage.recvfrom_syscall, - interface::get_int(arg1), - interface::get_mutcbuf(arg2), - interface::get_usize(arg3), - interface::get_int(arg4), - Ok::<&mut Option<&mut interface::GenSockaddr>, i32>(&mut Some( - &mut newsockaddr - )) - ); - - if rv >= 0 { - interface::copy_out_sockaddr(arg5, arg6, newsockaddr); - } - rv - } else { - syscall_error( - Errno::EINVAL, - "recvfrom", - "exactly one of the last two arguments was zero", - ) - } - } - CONNECT_SYSCALL => { - let addrlen = get_onearg!(interface::get_uint(arg3)); - let addr = get_onearg!(interface::get_sockaddr(arg2, addrlen)); - check_and_dispatch!( - cage.connect_syscall, - interface::get_int(arg1), - Ok::<&interface::GenSockaddr, i32>(&addr) - ) - } - LISTEN_SYSCALL => { - check_and_dispatch!( - cage.listen_syscall, - interface::get_int(arg1), - interface::get_int(arg2) - ) - } - ACCEPT_SYSCALL => { - - let nullity1 = interface::arg_nullity(&arg2); - let nullity2 = interface::arg_nullity(&arg3); - - if nullity1 && nullity2 { - check_and_dispatch!( - cage.accept_syscall, - interface::get_int(arg1), - Ok::<&mut Option<&mut interface::GenSockaddr>, i32>(&mut None) - ) - } else if !(nullity1 || nullity2) { - let mut addr = interface::set_gensockaddr(arg2, arg3).unwrap(); - let rv = check_and_dispatch!( - cage.accept_syscall, - interface::get_int(arg1), - Ok::<&mut Option<&mut interface::GenSockaddr>, i32>(&mut Some( - &mut addr - )) - ); - if rv >= 0 { - interface::copy_out_sockaddr(arg2, arg3, addr); - } - rv - } else { - syscall_error( - Errno::EINVAL, - "accept", - "exactly one of the last two arguments was zero", - ) - } - } - GETPEERNAME_SYSCALL => { - if interface::arg_nullity(&arg2) || interface::arg_nullity(&arg3) { - return syscall_error( - Errno::EINVAL, - "getpeername", - "Either the address or the length were null", - ); - } - let mut addr = interface::set_gensockaddr(arg2, arg3).unwrap(); - let rv = check_and_dispatch!( - cage.getpeername_syscall, - interface::get_int(arg1), - Ok::<&mut Option<&mut interface::GenSockaddr>, i32>(&mut Some( - &mut addr - )) - ); - - if rv >= 0 { - interface::copy_out_sockaddr(arg2, arg3, addr); - } - rv - } - GETSOCKNAME_SYSCALL => { - let mut addr = interface::set_gensockaddr(arg2, arg3).unwrap(); - - let len = interface::get_socklen_t_ptr(arg3).unwrap(); - - if interface::arg_nullity(&arg2) || interface::arg_nullity(&arg3) { - return syscall_error( - Errno::EINVAL, - "getsockname", - "Either the address or the length were null", - ); - } - let rv = check_and_dispatch!( - cage.getsockname_syscall, - interface::get_int(arg1), - Ok::<&mut Option<&mut interface::GenSockaddr>, i32>(&mut Some( - &mut addr - )) - ); - - if rv >= 0 { - interface::copy_out_sockaddr(arg2, arg3, addr); - } - rv - } - GETIFADDRS_SYSCALL => { - check_and_dispatch!( - cage.getifaddrs_syscall, - interface::get_mutcbuf(arg1), - interface::get_usize(arg2) - ) - } - GETSOCKOPT_SYSCALL => { - let mut sockval = 0; - if interface::arg_nullity(&arg4) || interface::arg_nullity(&arg5) { - return syscall_error( - Errno::EFAULT, - "getsockopt", - "Optval or optlen passed as null", - ); - } - if get_onearg!(interface::get_socklen_t_ptr(arg5)) != 4 { - return syscall_error(Errno::EINVAL, "getsockopt", "Invalid optlen passed"); - } - let rv = check_and_dispatch!( - cage.getsockopt_syscall, - interface::get_int(arg1), - interface::get_int(arg2), - interface::get_int(arg3), - Ok::<&mut i32, i32>(&mut sockval) - ); - - if rv >= 0 { - interface::copy_out_intptr(arg4, sockval); - } - //we take it as a given that the length is 4 both in and out - rv - - } - SETSOCKOPT_SYSCALL => { - check_and_dispatch!( - cage.setsockopt_syscall, - interface::get_int(arg1), - interface::get_int(arg2), - interface::get_int(arg3), - interface::get_mutcbuf(arg4), - interface::get_uint(arg5) - ) - } - SHUTDOWN_SYSCALL => { - check_and_dispatch!( - cage.shutdown_syscall, - interface::get_int(arg1), - interface::get_int(arg2) - ) - } - SELECT_SYSCALL => { - let nfds = get_onearg!(interface::get_int(arg1)); - if nfds < 0 { - //RLIMIT_NOFILE check as well? - return syscall_error( - Errno::EINVAL, - "select", - "The number of fds passed was invalid", - ); - } - check_and_dispatch!( - cage.select_syscall, - interface::get_int(arg1), - interface::get_fdset(arg2), - interface::get_fdset(arg3), - interface::get_fdset(arg4), - // interface::get_timerval(arg5) - interface::duration_fromtimeval(arg5) - ) - } - POLL_SYSCALL => { - let nfds = get_onearg!(interface::get_usize(arg2)); - check_and_dispatch!( - cage.poll_syscall, - interface::get_pollstruct_slice(arg1, nfds), - interface::get_ulong(arg2), - interface::get_int(arg3) - ) - } - SOCKETPAIR_SYSCALL => { - check_and_dispatch!( - cage.socketpair_syscall, - interface::get_int(arg1), - interface::get_int(arg2), - interface::get_int(arg3), - interface::get_sockpair(arg4) - ) - } - EXIT_SYSCALL => { - check_and_dispatch!(cage.exit_syscall, interface::get_int(arg1)) - } - FLOCK_SYSCALL => { - check_and_dispatch!( - cage.flock_syscall, - interface::get_int(arg1), - interface::get_int(arg2) - ) - } - FORK_SYSCALL => { - check_and_dispatch!(cage.fork_syscall, interface::get_ulong(arg1)) - } - EXEC_SYSCALL => { - check_and_dispatch!(cage.exec_syscall, interface::get_ulong(arg1)) - } - GETUID_SYSCALL => { - check_and_dispatch!(cage.getuid_syscall,) - } - GETEUID_SYSCALL => { - check_and_dispatch!(cage.geteuid_syscall,) - } - GETGID_SYSCALL => { - check_and_dispatch!(cage.getgid_syscall,) - } - GETEGID_SYSCALL => { - check_and_dispatch!(cage.getegid_syscall,) - } - PREAD_SYSCALL => { - check_and_dispatch!( - cage.pread_syscall, - interface::get_int(arg1), - interface::get_mutcbuf(arg2), - interface::get_usize(arg3), - interface::get_long(arg4) - ) - } - PWRITE_SYSCALL => { - check_and_dispatch!( - cage.pwrite_syscall, - interface::get_int(arg1), - interface::get_mutcbuf(arg2), - interface::get_usize(arg3), - interface::get_long(arg4) - ) - } - CHMOD_SYSCALL => { - check_and_dispatch!( - cage.chmod_syscall, - interface::get_cstr(arg1), - interface::get_uint(arg2) - ) - } - FCHMOD_SYSCALL => { - check_and_dispatch!( - cage.fchmod_syscall, - interface::get_int(arg1), - interface::get_uint(arg2) - ) - } - RMDIR_SYSCALL => { - check_and_dispatch!(cage.rmdir_syscall, interface::get_cstr(arg1)) - } - RENAME_SYSCALL => { - check_and_dispatch!( - cage.rename_syscall, - interface::get_cstr(arg1), - interface::get_cstr(arg2) - ) - } - EPOLL_CREATE_SYSCALL => { - check_and_dispatch!(cage.epoll_create_syscall, interface::get_int(arg1)) - } - EPOLL_CTL_SYSCALL => { - check_and_dispatch!( - cage.epoll_ctl_syscall, - interface::get_int(arg1), - interface::get_int(arg2), - interface::get_int(arg3), - interface::get_epollevent(arg4) - ) - } - EPOLL_WAIT_SYSCALL => { - let nfds = get_onearg!(interface::get_int(arg3)); - - if nfds < 0 { - //RLIMIT_NOFILE check as well? - return syscall_error( - Errno::EINVAL, - "select", - "The number of fds passed was invalid", - ); - } - check_and_dispatch!( - cage.epoll_wait_syscall, - interface::get_int(arg1), - interface::get_epollevent_slice(arg2, nfds), - interface::get_int(arg3), - interface::get_int(arg4) - ) - } - GETDENTS_SYSCALL => { - check_and_dispatch!( - cage.getdents_syscall, - interface::get_int(arg1), - interface::get_mutcbuf(arg2), - interface::get_uint(arg3) - ) - } - PIPE_SYSCALL => { - check_and_dispatch!(cage.pipe_syscall, interface::get_pipearray(arg1)) - } - PIPE2_SYSCALL => { - check_and_dispatch!( - cage.pipe2_syscall, - interface::get_pipearray(arg1), - interface::get_int(arg2) - ) - } - GETCWD_SYSCALL => { - check_and_dispatch!( - cage.getcwd_syscall, - interface::get_mutcbuf(arg1), - interface::get_uint(arg2) - ) - } - GETHOSTNAME_SYSCALL => { - check_and_dispatch!( - cage.gethostname_syscall, - interface::get_mutcbuf(arg1), - interface::get_isize(arg2) - ) - } - MKDIR_SYSCALL => { - check_and_dispatch!( - cage.mkdir_syscall, - interface::get_cstr(arg1), - interface::get_uint(arg2) - ) - } - SHMGET_SYSCALL => { - check_and_dispatch!( - cage.shmget_syscall, - interface::get_int(arg1), - interface::get_usize(arg2), - interface::get_int(arg3) - ) - } - SHMAT_SYSCALL => { - check_and_dispatch!( - cage.shmat_syscall, - interface::get_int(arg1), - interface::get_mutcbuf(arg2), - interface::get_int(arg3) - ) - } - SHMDT_SYSCALL => { - check_and_dispatch!(cage.shmdt_syscall, interface::get_mutcbuf(arg1)) - } - SHMCTL_SYSCALL => { - let cmd = get_onearg!(interface::get_int(arg2)); - let buf = if cmd == libc::IPC_STAT { - Some(get_onearg!(interface::get_shmidstruct(arg3))) - } else { - None - }; - check_and_dispatch!( - cage.shmctl_syscall, - interface::get_int(arg1), - Ok::(cmd), - Ok::, i32>(buf) - ) - } - MUTEX_CREATE_SYSCALL => { - check_and_dispatch!(cage.mutex_create_syscall,) - } - MUTEX_DESTROY_SYSCALL => { - check_and_dispatch!(cage.mutex_destroy_syscall, interface::get_int(arg1)) - } - MUTEX_LOCK_SYSCALL => { - check_and_dispatch!(cage.mutex_lock_syscall, interface::get_int(arg1)) - } - MUTEX_TRYLOCK_SYSCALL => { - check_and_dispatch!(cage.mutex_trylock_syscall, interface::get_int(arg1)) - } - MUTEX_UNLOCK_SYSCALL => { - check_and_dispatch!(cage.mutex_unlock_syscall, interface::get_int(arg1)) - } - COND_CREATE_SYSCALL => { - check_and_dispatch!(cage.cond_create_syscall,) - } - COND_DESTROY_SYSCALL => { - check_and_dispatch!(cage.cond_destroy_syscall, interface::get_int(arg1)) - } - COND_WAIT_SYSCALL => { - check_and_dispatch!( - cage.cond_wait_syscall, - interface::get_int(arg1), - interface::get_int(arg2) - ) - } - COND_BROADCAST_SYSCALL => { - check_and_dispatch!(cage.cond_broadcast_syscall, interface::get_int(arg1)) - } - COND_SIGNAL_SYSCALL => { - check_and_dispatch!(cage.cond_signal_syscall, interface::get_int(arg1)) - } - COND_TIMEDWAIT_SYSCALL => { - check_and_dispatch!( - cage.cond_timedwait_syscall, - interface::get_int(arg1), - interface::get_int(arg2), - interface::duration_fromtimespec(arg3) - ) - } - TRUNCATE_SYSCALL => { - check_and_dispatch!( - cage.truncate_syscall, - interface::get_cstr(arg1), - interface::get_isize(arg2) - ) - } - FTRUNCATE_SYSCALL => { - check_and_dispatch!( - cage.ftruncate_syscall, - interface::get_int(arg1), - interface::get_isize(arg2) - ) - } - SIGACTION_SYSCALL => { - check_and_dispatch!( - cage.sigaction_syscall, - interface::get_int(arg1), - interface::get_constsigactionstruct(arg2), - interface::get_sigactionstruct(arg3) - ) - } - KILL_SYSCALL => { - check_and_dispatch!( - cage.kill_syscall, - interface::get_int(arg1), - interface::get_int(arg2) - ) - } - SIGPROCMASK_SYSCALL => { - check_and_dispatch!( - cage.sigprocmask_syscall, - interface::get_int(arg1), - interface::get_constsigsett(arg2), - interface::get_sigsett(arg3) - ) - } - SETITIMER_SYSCALL => { - check_and_dispatch!( - cage.setitimer_syscall, - interface::get_int(arg1), - interface::get_constitimerval(arg2), - interface::get_itimerval(arg3) - ) - } - SEM_INIT_SYSCALL => { - check_and_dispatch!( - cage.sem_init_syscall, - interface::get_uint(arg1), - interface::get_int(arg2), - interface::get_uint(arg3) - ) - } - SEM_WAIT_SYSCALL => { - check_and_dispatch!(cage.sem_wait_syscall, interface::get_uint(arg1)) - } - SEM_POST_SYSCALL => { - check_and_dispatch!(cage.sem_post_syscall, interface::get_uint(arg1)) - } - SEM_DESTROY_SYSCALL => { - check_and_dispatch!(cage.sem_destroy_syscall, interface::get_uint(arg1)) - } - SEM_GETVALUE_SYSCALL => { - check_and_dispatch!(cage.sem_getvalue_syscall, interface::get_uint(arg1)) - } - SEM_TRYWAIT_SYSCALL => { - check_and_dispatch!(cage.sem_trywait_syscall, interface::get_uint(arg1)) - } - SEM_TIMEDWAIT_SYSCALL => { - check_and_dispatch!( - cage.sem_timedwait_syscall, - interface::get_uint(arg1), - interface::duration_fromtimespec(arg2) - ) - } - WRITEV_SYSCALL => { - check_and_dispatch!( - cage.writev_syscall, - interface::get_int(arg1), - interface::get_iovecstruct(arg2), - interface::get_int(arg3) - ) - } - - _ => { - //unknown syscall - -1 - } - } -} - #[no_mangle] pub extern "C" fn lindcancelinit(cageid: u64) { let cage = interface::cagetable_getref(cageid); From eb7121c449590f7499c7bcdcdd0fd4ad309f890f Mon Sep 17 00:00:00 2001 From: qianxichen233 Date: Tue, 22 Oct 2024 07:39:53 +0000 Subject: [PATCH 25/37] clean up & resolve comments --- src/lib.rs | 39 ------------------------- src/safeposix/dispatcher.rs | 5 ---- src/safeposix/syscalls/fs_calls.rs | 14 +++++++-- src/safeposix/syscalls/net_calls.rs | 1 - src/safeposix/syscalls/sys_constants.rs | 2 +- 5 files changed, 13 insertions(+), 48 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e7254501..e34ad94e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,47 +13,8 @@ pub mod safeposix; pub mod tests; pub mod fdtables; -use std::sync::{Condvar, Mutex}; - use crate::safeposix::dispatcher::*; -#[macro_export] -macro_rules! dispatch { - ($cageid:expr, $callnum:expr) => { - dispatcher( - $cageid, $callnum, BLANKARG, BLANKARG, BLANKARG, BLANKARG, BLANKARG, BLANKARG, - ) - }; - ($cageid:expr, $callnum:expr, $arg1:expr) => { - dispatcher( - $cageid, $callnum, $arg1, BLANKARG, BLANKARG, BLANKARG, BLANKARG, BLANKARG, - ) - }; - ($cageid:expr, $callnum:expr, $arg1:expr, $arg2:expr) => { - dispatcher( - $cageid, $callnum, $arg1, $arg2, BLANKARG, BLANKARG, BLANKARG, BLANKARG, - ) - }; - ($cageid:expr, $callnum:expr, $arg1:expr, $arg2:expr, $arg3:expr) => { - dispatcher( - $cageid, $callnum, $arg1, $arg2, $arg3, BLANKARG, BLANKARG, BLANKARG, - ) - }; - ($cageid:expr, $callnum:expr, $arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr) => { - dispatcher( - $cageid, $callnum, $arg1, $arg2, $arg3, $arg4, BLANKARG, BLANKARG, - ) - }; - ($cageid:expr, $callnum:expr, $arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr, $arg5:expr) => { - dispatcher( - $cageid, $callnum, $arg1, $arg2, $arg3, $arg4, $arg5, BLANKARG, - ) - }; - ($cageid:expr, $callnum:expr, $arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr, $arg5:expr, $arg6:expr) => { - dispatcher($cageid, $callnum, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6) - }; -} - pub fn lind_lindrustinit(verbosity: i32) { unsafe { lindrustinit(verbosity as isize); diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index ff5a529b..5b146635 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -117,11 +117,6 @@ const CLONE_SYSCALL: i32 = 171; const NANOSLEEP_TIME64_SYSCALL : i32 = 181; -use std::collections::HashMap; -use std::hash::BuildHasherDefault; - -use libc::IPOPT_OPTVAL; - use std::ffi::CString; use super::cage::*; diff --git a/src/safeposix/syscalls/fs_calls.rs b/src/safeposix/syscalls/fs_calls.rs index 12fd70e1..e958e911 100644 --- a/src/safeposix/syscalls/fs_calls.rs +++ b/src/safeposix/syscalls/fs_calls.rs @@ -1886,13 +1886,23 @@ impl Cage { // // to perform this we just directly pass futex's var args as unsigned 32 bit integers to syscall() with SYS_futex pub fn futex_syscall(&self, uaddr: u64, futex_op: u32, val: u32, val2: u32, uaddr2: u32, val3: u32) -> i32 { - unsafe { syscall(SYS_futex, uaddr, futex_op, val, val2, uaddr2, val3) as i32 } + let ret = unsafe { syscall(SYS_futex, uaddr, futex_op, val, val2, uaddr2, val3) as i32 }; + if ret < 0 { + let errno = get_errno(); + return handle_errno(errno, "fcntl"); + } + ret } //We directly call nanosleep syscall(SYS_clock_nanosleep) from the libc //return an `i32` value representing the result of the system call. pub fn nanosleep_time64_syscall(&self, clockid: u32, flags: i32, req: usize, rem: usize) -> i32 { - unsafe { syscall(SYS_clock_nanosleep, clockid, flags, req, rem) as i32 } + let ret = unsafe { syscall(SYS_clock_nanosleep, clockid, flags, req, rem) as i32 }; + if ret < 0 { + let errno = get_errno(); + return handle_errno(errno, "fcntl"); + } + ret } } diff --git a/src/safeposix/syscalls/net_calls.rs b/src/safeposix/syscalls/net_calls.rs index 22ac2994..e2270ac4 100644 --- a/src/safeposix/syscalls/net_calls.rs +++ b/src/safeposix/syscalls/net_calls.rs @@ -1224,7 +1224,6 @@ impl Cage { let vsv_2 = fdtables::get_unused_virtual_fd(self.cageid, FDKIND_KERNEL, ksv_2 as u64, false, 0).unwrap(); virtual_socket_vector.sock1 = vsv_1 as i32; virtual_socket_vector.sock2 = vsv_2 as i32; - return 0; } diff --git a/src/safeposix/syscalls/sys_constants.rs b/src/safeposix/syscalls/sys_constants.rs index fcd219a9..89feb715 100644 --- a/src/safeposix/syscalls/sys_constants.rs +++ b/src/safeposix/syscalls/sys_constants.rs @@ -74,4 +74,4 @@ pub const SIGUNUSED: i32 = 31; pub const SIG_BLOCK: i32 = 0; pub const SIG_UNBLOCK: i32 = 1; pub const SIG_SETMASK: i32 = 2; -pub const ITIMER_REAL: i32 = 0; \ No newline at end of file +pub const ITIMER_REAL: i32 = 0; From ef50f3b4189c8d84536d5a89cc55028eda8efe23 Mon Sep 17 00:00:00 2001 From: qianxichen233 Date: Sun, 27 Oct 2024 06:53:38 +0000 Subject: [PATCH 26/37] clean up lib.rs --- src/lib.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e34ad94e..91aec910 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,12 +33,6 @@ pub fn lind_rustposix_thread_init(cageid: u64, signalflag: u64) { } } -pub fn lind_write_inner(fd: i32, buf: *const u8, count: usize, cageid: u64) { - unsafe { - quick_write(fd, buf, count, cageid); - } -} - pub fn lind_fork(parent_cageid: u64, child_cageid: u64) -> i32 { unsafe { lind_syscall_api( From e9a2bb2a2655e3ee5331d9704b18178d88f636b4 Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Wed, 30 Oct 2024 13:35:25 -0400 Subject: [PATCH 27/37] comment and new name for type.rs --- src/interface/types.rs | 176 ++++++++++++++++++++--------------------- 1 file changed, 88 insertions(+), 88 deletions(-) diff --git a/src/interface/types.rs b/src/interface/types.rs index 03039833..9731accd 100644 --- a/src/interface/types.rs +++ b/src/interface/types.rs @@ -191,11 +191,11 @@ pub struct ClippedDirent { pub const CLIPPED_DIRENT_SIZE: u32 = size_of::() as u32; -pub fn get_int(argument: u64) -> Result { - let data = argument as i32; +pub fn get_int(generic_argument: u64) -> Result { + let data = generic_argument as i32; let type_checker = (!0xffffffff) as u64; - if (argument & (!type_checker)) == 0 { + if (generic_argument & (!type_checker)) == 0 { return Ok(data); } return Err(syscall_error( @@ -205,11 +205,11 @@ pub fn get_int(argument: u64) -> Result { )); } -pub fn get_uint(argument: u64) -> Result { - let data = argument as u32; +pub fn get_uint(generic_argument: u64) -> Result { + let data = generic_argument as u32; let type_checker = (!0xffffffff) as u64; - if (argument & (!type_checker)) == 0 { + if (generic_argument & (!type_checker)) == 0 { return Ok(data); } return Err(syscall_error( @@ -219,26 +219,26 @@ pub fn get_uint(argument: u64) -> Result { )); } -pub fn get_long(argument: u64) -> Result { - return Ok(argument as i64); //this should not return error +pub fn get_long(generic_argument: u64) -> Result { + return Ok(generic_argument as i64); //this should not return error } -pub fn get_ulong(argument: u64) -> Result { - return Ok(argument); //this should not return error +pub fn get_ulong(generic_argument: u64) -> Result { + return Ok(generic_argument); //this should not return error } -pub fn get_isize(argument: u64) -> Result { +pub fn get_isize(generic_argument: u64) -> Result { // also should not return error - return Ok(argument as isize); + return Ok(generic_argument as isize); } -pub fn get_usize(argument: u64) -> Result { +pub fn get_usize(generic_argument: u64) -> Result { //should not return an error - return Ok(argument as usize); + return Ok(generic_argument as usize); } -pub fn get_cbuf(argument: u64) -> Result<*const u8, i32> { - let data = argument as *const u8; +pub fn get_cbuf(generic_argument: u64) -> Result<*const u8, i32> { + let data = generic_argument as *const u8; if !data.is_null() { return Ok(data); } @@ -249,8 +249,8 @@ pub fn get_cbuf(argument: u64) -> Result<*const u8, i32> { )); } -pub fn get_mutcbuf(argument: u64) -> Result<*mut u8, i32> { - let data = argument as *mut u8; +pub fn get_mutcbuf(generic_argument: u64) -> Result<*mut u8, i32> { + let data = generic_argument as *mut u8; if !data.is_null() { return Ok(data); } @@ -262,8 +262,8 @@ pub fn get_mutcbuf(argument: u64) -> Result<*mut u8, i32> { } // for the case where the buffer pointer being Null is normal -pub fn get_mutcbuf_null(argument: u64) -> Result, i32> { - let data = argument as *mut u8; +pub fn get_mutcbuf_null(generic_argument: u64) -> Result, i32> { + let data = generic_argument as *mut u8; if !data.is_null() { return Ok(Some(data)); } @@ -271,8 +271,8 @@ pub fn get_mutcbuf_null(argument: u64) -> Result, i32> { } -pub fn get_fdset(argument: u64) -> Result, i32> { - let data = argument as *mut libc::fd_set; +pub fn get_fdset(generic_argument: u64) -> Result, i32> { + let data = generic_argument as *mut libc::fd_set; if !data.is_null() { // let internal_fds: &mut interface::FdSet = interface::FdSet::new_from_ptr(data); let internal_fds = unsafe { &mut *(data as *mut fd_set) }; @@ -298,11 +298,11 @@ pub fn get_fdset(argument: u64) -> Result, i32> { // return Ok(pointer); // } -pub fn get_cstr<'a>(argument: u64) -> Result<&'a str, i32> { +pub fn get_cstr<'a>(generic_argument: u64) -> Result<&'a str, i32> { //first we check that the pointer is not null //and then we check so that we can get data from the memory - let pointer = argument as *const i8; + let pointer = generic_argument as *const i8; if !pointer.is_null() { if let Ok(ret_data) = unsafe { interface::charstar_to_ruststr(pointer) } { return Ok(ret_data); @@ -321,13 +321,13 @@ pub fn get_cstr<'a>(argument: u64) -> Result<&'a str, i32> { )); } -pub fn get_cstrarr<'a>(argument: u64) -> Result, i32> { +pub fn get_cstrarr<'a>(generic_argument: u64) -> Result, i32> { //iterate though the pointers in a function and: // 1: check that the pointer is not null // 2: push the data from that pointer onto the vector being returned //once we encounter a null pointer, we know that we have either hit the end of the array or another null pointer in the memory - let mut pointer = argument as *const *const i8; + let mut pointer = generic_argument as *const *const i8; let mut data_vector: Vec<&str> = Vec::new(); if !pointer.is_null() { @@ -363,8 +363,8 @@ pub fn get_cstrarr<'a>(argument: u64) -> Result, i32> { // "input data not valid", // )); // } -pub fn get_statdatastruct<'a>(argument: u64) -> Result<&'a mut StatData, i32> { - let pointer = argument as *mut StatData; +pub fn get_statdatastruct<'a>(generic_argument: u64) -> Result<&'a mut StatData, i32> { + let pointer = generic_argument as *mut StatData; if !pointer.is_null() { return Ok(unsafe { &mut *pointer }); } @@ -386,8 +386,8 @@ pub fn get_statdatastruct<'a>(argument: u64) -> Result<&'a mut StatData, i32> { // "input data not valid", // )); // } -pub fn get_fsdatastruct<'a>(argument: u64) -> Result<&'a mut FSData, i32> { - let pointer = argument as *mut FSData; +pub fn get_fsdatastruct<'a>(generic_argument: u64) -> Result<&'a mut FSData, i32> { + let pointer = generic_argument as *mut FSData; if !pointer.is_null() { return Ok(unsafe { &mut *pointer }); } @@ -398,8 +398,8 @@ pub fn get_fsdatastruct<'a>(argument: u64) -> Result<&'a mut FSData, i32> { )); } -pub fn get_shmidstruct<'a>(argument: u64) -> Result<&'a mut ShmidsStruct, i32> { - let pointer = argument as *mut ShmidsStruct; +pub fn get_shmidstruct<'a>(generic_argument: u64) -> Result<&'a mut ShmidsStruct, i32> { + let pointer = generic_argument as *mut ShmidsStruct; if !pointer.is_null() { return Ok(unsafe { &mut *pointer }); } @@ -410,8 +410,8 @@ pub fn get_shmidstruct<'a>(argument: u64) -> Result<&'a mut ShmidsStruct, i32> { )); } -pub fn get_ioctlptrunion<'a>(argument: u64) -> Result<&'a mut u8, i32> { - let pointer = argument as *mut u8; +pub fn get_ioctlptrunion<'a>(generic_argument: u64) -> Result<&'a mut u8, i32> { + let pointer = generic_argument as *mut u8; if !pointer.is_null() { return Ok(unsafe { &mut *pointer @@ -485,8 +485,8 @@ pub fn get_ioctlptrunion<'a>(argument: u64) -> Result<&'a mut u8, i32> { // } // } -pub fn get_pipearray<'a>(argument: u64) -> Result<&'a mut PipeArray, i32> { - let pointer = argument as *mut PipeArray; +pub fn get_pipearray<'a>(generic_argument: u64) -> Result<&'a mut PipeArray, i32> { + let pointer = generic_argument as *mut PipeArray; if !pointer.is_null() { return Ok(unsafe { &mut *pointer }); } @@ -497,8 +497,8 @@ pub fn get_pipearray<'a>(argument: u64) -> Result<&'a mut PipeArray, i32> { )); } -pub fn get_sockpair<'a>(argument: u64) -> Result<&'a mut SockPair, i32> { - let pointer = argument as *mut SockPair; +pub fn get_sockpair<'a>(generic_argument: u64) -> Result<&'a mut SockPair, i32> { + let pointer = generic_argument as *mut SockPair; if !pointer.is_null() { return Ok(unsafe { &mut *pointer }); } @@ -521,8 +521,8 @@ pub fn get_sockpair<'a>(argument: u64) -> Result<&'a mut SockPair, i32> { // )); // } -pub fn get_constsockaddr<'a>(argument: u64) -> Result<&'a SockaddrDummy, i32> { - let pointer = argument as *const SockaddrDummy; +pub fn get_constsockaddr<'a>(generic_argument: u64) -> Result<&'a SockaddrDummy, i32> { + let pointer = generic_argument as *const SockaddrDummy; if !pointer.is_null() { return Ok(unsafe { & *pointer }); } @@ -533,8 +533,8 @@ pub fn get_constsockaddr<'a>(argument: u64) -> Result<&'a SockaddrDummy, i32> { )); } -pub fn get_sockaddr(argument: u64, addrlen: u32) -> Result { - let pointer = argument as *const SockaddrDummy; +pub fn get_sockaddr(generic_argument: u64, addrlen: u32) -> Result { + let pointer = generic_argument as *const SockaddrDummy; if !pointer.is_null() { let tmpsock = unsafe { &*pointer }; match tmpsock.sa_family { @@ -592,9 +592,9 @@ pub fn get_sockaddr(argument: u64, addrlen: u32) -> Result Result { - let received = argument as *mut SockaddrDummy; - let received_addrlen = (argument1 as *mut u32) as u32; +pub fn set_gensockaddr(generic_argument: u64, generic_argument1: u64) -> Result { + let received = generic_argument as *mut SockaddrDummy; + let received_addrlen = (generic_argument1 as *mut u32) as u32; let tmpsock = unsafe { &*received }; // println!("[Dispatcher set_gen] family: {:?}", tmpsock.sa_family); // println!("[Dispathcer set_gen] len: {:?}", received_addrlen); @@ -656,9 +656,9 @@ pub fn set_gensockaddr(argument: u64, argument1: u64) -> Result( - argument: u64, + generic_argument: u64, nfds: usize, ) -> Result<&'a mut [PollStruct], i32> { - let pollstructptr = argument as *mut PollStruct; + let pollstructptr = generic_argument as *mut PollStruct; if !pollstructptr.is_null() { return Ok(unsafe { std::slice::from_raw_parts_mut(pollstructptr, nfds) }); } @@ -741,10 +741,10 @@ pub fn get_pollstruct_slice<'a>( } pub fn get_epollevent_slice<'a>( - argument: u64, + generic_argument: u64, nfds: i32, ) -> Result<&'a mut [EpollEvent], i32> { - let epolleventptr = argument as *mut EpollEvent; + let epolleventptr = generic_argument as *mut EpollEvent; if !epolleventptr.is_null() { return Ok(unsafe { std::slice::from_raw_parts_mut(epolleventptr, nfds as usize) }); } @@ -755,8 +755,8 @@ pub fn get_epollevent_slice<'a>( )); } -pub fn get_slice_from_string<'a>(argument: u64, len: usize) -> Result<&'a mut [u8], i32> { - let bufptr = argument as *mut u8; +pub fn get_slice_from_string<'a>(generic_argument: u64, len: usize) -> Result<&'a mut [u8], i32> { + let bufptr = generic_argument as *mut u8; if bufptr.is_null() { return Ok(unsafe { std::slice::from_raw_parts_mut(bufptr, len as usize) }); } @@ -767,8 +767,8 @@ pub fn get_slice_from_string<'a>(argument: u64, len: usize) -> Result<&'a mut [u )); } -pub fn get_epollevent<'a>(argument: u64) -> Result<&'a mut EpollEvent, i32> { - let epolleventptr = argument as *mut EpollEvent; +pub fn get_epollevent<'a>(generic_argument: u64) -> Result<&'a mut EpollEvent, i32> { + let epolleventptr = generic_argument as *mut EpollEvent; if !epolleventptr.is_null() { return Ok(unsafe { &mut *epolleventptr }); } @@ -790,8 +790,8 @@ pub fn get_epollevent<'a>(argument: u64) -> Result<&'a mut EpollEvent, i32> { // )); // } -pub fn get_socklen_t_ptr(argument: u64) -> Result { - let socklenptr = argument as *mut u32; +pub fn get_socklen_t_ptr(generic_argument: u64) -> Result { + let socklenptr = generic_argument as *mut u32; if !socklenptr.is_null() { return Ok(unsafe { *socklenptr }); } @@ -803,18 +803,18 @@ pub fn get_socklen_t_ptr(argument: u64) -> Result { } //arg checked for nullity beforehand -pub fn get_int_from_intptr(argument: u64) -> i32 { - return unsafe { *(argument as *mut i32)}; +pub fn get_int_from_intptr(generic_argument: u64) -> i32 { + return unsafe { *(generic_argument as *mut i32)}; } -pub fn copy_out_intptr(argument: u64, intval: i32) { +pub fn copy_out_intptr(generic_argument: u64, intval: i32) { unsafe { - *(argument as *mut i32) = intval; + *(generic_argument as *mut i32) = intval; } } -pub fn duration_fromtimeval(argument: u64) -> Result, i32> { - let pointer = argument as *mut timeval; +pub fn duration_fromtimeval(generic_argument: u64) -> Result, i32> { + let pointer = generic_argument as *mut timeval; if !pointer.is_null() { let times = unsafe { &mut *pointer }; return Ok(Some(interface::RustDuration::new( @@ -826,8 +826,8 @@ pub fn duration_fromtimeval(argument: u64) -> Result(argument: u64) -> Result<&'a mut timeval, i32> { - let pointer = argument as *mut timeval; +pub fn get_timerval<'a>(generic_argument: u64) -> Result<&'a mut timeval, i32> { + let pointer = generic_argument as *mut timeval; if !pointer.is_null() { return Ok(unsafe { &mut *pointer }); } @@ -838,8 +838,8 @@ pub fn get_timerval<'a>(argument: u64) -> Result<&'a mut timeval, i32> { )); } -pub fn get_itimerval<'a>(argument: u64) -> Result, i32> { - let pointer = argument as *mut ITimerVal; +pub fn get_itimerval<'a>(generic_argument: u64) -> Result, i32> { + let pointer = generic_argument as *mut ITimerVal; if !pointer.is_null() { Ok(Some(unsafe { &mut *pointer })) } else { @@ -858,8 +858,8 @@ pub fn get_itimerval<'a>(argument: u64) -> Result, i32 // )); // } -pub fn get_constitimerval<'a>(argument: u64) -> Result, i32> { - let pointer = argument as *const ITimerVal; +pub fn get_constitimerval<'a>(generic_argument: u64) -> Result, i32> { + let pointer = generic_argument as *const ITimerVal; if !pointer.is_null() { Ok(Some(unsafe { &*pointer })) } else { @@ -879,8 +879,8 @@ pub fn get_constitimerval<'a>(argument: u64) -> Result, i3 // )); // } -pub fn duration_fromtimespec(argument: u64) -> Result { - let pointer = argument as *mut TimeSpec; +pub fn duration_fromtimespec(generic_argument: u64) -> Result { + let pointer = generic_argument as *mut TimeSpec; if !pointer.is_null() { let times = unsafe { &mut *pointer }; if times.tv_nsec < 0 || times.tv_nsec >= 1000000000 { @@ -903,8 +903,8 @@ pub fn duration_fromtimespec(argument: u64) -> Result(argument: u64) -> Result<&'a timespec, i32> { - let pointer = argument as *mut timespec; +pub fn get_timespec<'a>(generic_argument: u64) -> Result<&'a timespec, i32> { + let pointer = generic_argument as *mut timespec; if !pointer.is_null() { return Ok( unsafe { &*pointer @@ -920,9 +920,9 @@ pub fn get_timespec<'a>(argument: u64) -> Result<&'a timespec, i32> { pub fn get_duration_from_millis( - argument: u64, + generic_argument: u64, ) -> Result, i32> { - let posstimemillis = get_int(argument); + let posstimemillis = get_int(generic_argument); match posstimemillis { Ok(timemillis) => { if timemillis >= 0 { @@ -937,12 +937,12 @@ pub fn get_duration_from_millis( } } -pub fn arg_nullity(argument: u64) -> bool { - (argument as *const u8).is_null() +pub fn arg_nullity(generic_argument: u64) -> bool { + (generic_argument as *const u8).is_null() } -pub fn get_sigactionstruct<'a>(argument: u64) -> Result, i32> { - let pointer = argument as *mut SigactionStruct; +pub fn get_sigactionstruct<'a>(generic_argument: u64) -> Result, i32> { + let pointer = generic_argument as *mut SigactionStruct; if !pointer.is_null() { Ok(Some(unsafe { &mut *pointer })) @@ -951,8 +951,8 @@ pub fn get_sigactionstruct<'a>(argument: u64) -> Result(argument: u64) -> Result, i32> { - let pointer = argument as *const SigactionStruct; +pub fn get_constsigactionstruct<'a>(generic_argument: u64) -> Result, i32> { + let pointer = generic_argument as *const SigactionStruct; if !pointer.is_null() { Ok(Some(unsafe { &*pointer })) @@ -961,8 +961,8 @@ pub fn get_constsigactionstruct<'a>(argument: u64) -> Result(argument: u64) -> Result, i32> { - let pointer = argument as *mut u64; +pub fn get_sigsett<'a>(generic_argument: u64) -> Result, i32> { + let pointer = generic_argument as *mut u64; if !pointer.is_null() { Ok(Some(unsafe { &mut *pointer })) @@ -971,8 +971,8 @@ pub fn get_sigsett<'a>(argument: u64) -> Result, i32> } } -pub fn get_constsigsett<'a>(argument: u64) -> Result, i32> { - let pointer = argument as *const SigsetType; +pub fn get_constsigsett<'a>(generic_argument: u64) -> Result, i32> { + let pointer = generic_argument as *const SigsetType; if !pointer.is_null() { Ok(Some(unsafe { &*pointer })) @@ -981,8 +981,8 @@ pub fn get_constsigsett<'a>(argument: u64) -> Result, i32 } } -pub fn get_iovecstruct(argument: u64) -> Result<*const interface::IovecStruct, i32> { - let data = argument as *const interface::IovecStruct; +pub fn get_iovecstruct(generic_argument: u64) -> Result<*const interface::IovecStruct, i32> { + let data = generic_argument as *const interface::IovecStruct; if !data.is_null() { return Ok(data); } From 9714a5b7d63fd2fce9edb700741109d8be500046 Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Wed, 30 Oct 2024 13:39:31 -0400 Subject: [PATCH 28/37] long comment for type.rs --- src/interface/types.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/interface/types.rs b/src/interface/types.rs index 9731accd..d5e2cd0c 100644 --- a/src/interface/types.rs +++ b/src/interface/types.rs @@ -191,6 +191,16 @@ pub struct ClippedDirent { pub const CLIPPED_DIRENT_SIZE: u32 = size_of::() as u32; +/* +This file provides essential functions for handling and validating `u64` inputs, +converting them to various system-specific data types needed in system calls. +It includes utilities for transforming raw pointers to typed structures, such as integer, +buffer, and string pointers, as well as complex structures like polling, signal handling, +timing, and socket-related types. Each function ensures safe and correct usage by performing +null checks, boundary validations, and type casting, returning either a valid reference +or an error if data is invalid. This design promotes secure, reliable access to memory and + resources in a low-level systems environment. +*/ pub fn get_int(generic_argument: u64) -> Result { let data = generic_argument as i32; let type_checker = (!0xffffffff) as u64; From 77f8f07725467b84d7a92a5c4b8c17ba9ab5b4fb Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Thu, 31 Oct 2024 19:11:02 -0400 Subject: [PATCH 29/37] delete u64 to string function and use another function from type.rs --- src/interface/types.rs | 2 ++ src/safeposix/dispatcher.rs | 29 ++++++----------------------- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/src/interface/types.rs b/src/interface/types.rs index d5e2cd0c..75265e7c 100644 --- a/src/interface/types.rs +++ b/src/interface/types.rs @@ -2,6 +2,8 @@ use crate::interface; use crate::interface::errnos::{syscall_error, Errno}; +use std::ffi::CStr; +use std::str::Utf8Error; use std::io::{Read, Write}; use std::io; use std::ptr::null; diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index 5b146635..5395537d 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -118,7 +118,7 @@ const CLONE_SYSCALL: i32 = 171; const NANOSLEEP_TIME64_SYSCALL : i32 = 181; use std::ffi::CString; - +use std::ffi::CStr; use super::cage::*; use super::syscalls::kernel_close; @@ -129,7 +129,8 @@ const FDKIND_IMSOCK: u32 = 2; use std::io::{Read, Write}; use std::io; -use crate::interface::types::SockaddrDummy; +use crate::interface::types; +// use crate::interface::types::SockaddrDummy; use crate::interface::{SigactionStruct, StatData}; use crate::{fdtables, interface}; use crate::interface::errnos::*; @@ -142,7 +143,6 @@ macro_rules! get_onearg { } }; } - //this macro takes in a syscall invocation name (i.e. cage.fork_syscall), and all of the arguments //to the syscall. Then it unwraps the arguments, returning the error if any one of them is an error //value, and returning the value of the function if not. It does this by using the ? operator in @@ -198,23 +198,6 @@ pub extern "C" fn rustposix_thread_init(cageid: u64, signalflag: u64) { interface::signalflag_set(signalflag); } -use std::ffi::CStr; -use std::str::Utf8Error; - -fn u64_to_str(ptr: u64) -> Result<&'static str, Utf8Error> { - // Convert the u64 to a pointer to a C string (null-terminated) - let c_str = ptr as *const i8; - - // Unsafe block to handle raw pointer and C string conversion - unsafe { - // Create a CStr from the raw pointer - let c_str = CStr::from_ptr(c_str); - - // Convert the CStr to a Rust &str - c_str.to_str() - } -} - #[no_mangle] pub extern "C" fn lind_syscall_api( cageid: u64, @@ -324,7 +307,7 @@ pub extern "C" fn lind_syscall_api( } ACCESS_SYSCALL => { - let path = match u64_to_str(start_address + arg1) { + let path = match interface::types::get_cstr(start_address + arg1) { Ok(path_str) => path_str, Err(_) => return -1, // Handle error appropriately, return an error code }; @@ -339,7 +322,7 @@ pub extern "C" fn lind_syscall_api( } OPEN_SYSCALL => { - let path = match u64_to_str(start_address + arg1) { + let path = match interface::types::get_cstr(start_address + arg1) { Ok(path_str) => path_str, Err(_) => return -1, // Handle error appropriately, return an error code }; @@ -546,7 +529,7 @@ pub extern "C" fn lind_syscall_api( } CHDIR_SYSCALL => { - let path = u64_to_str(start_address + arg1).unwrap(); + let path = interface::types::get_cstr(start_address + arg1).unwrap(); interface::check_cageid(cageid); unsafe { From ee2801d627fc143d4396e3d5b9daa40c98fcea9e Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Mon, 4 Nov 2024 13:46:34 -0500 Subject: [PATCH 30/37] add newline --- src/interface/comm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interface/comm.rs b/src/interface/comm.rs index f908e556..75395936 100644 --- a/src/interface/comm.rs +++ b/src/interface/comm.rs @@ -641,4 +641,4 @@ pub fn kernel_select( }; return result; -} \ No newline at end of file +} From 79cac844ff83d4e7cd15c5241c991ddd17999e3d Mon Sep 17 00:00:00 2001 From: qianxichen233 Date: Mon, 4 Nov 2024 19:57:06 +0000 Subject: [PATCH 31/37] clean up lib.rs --- src/lib.rs | 100 ------------------------------------ src/safeposix/dispatcher.rs | 16 +++--- 2 files changed, 8 insertions(+), 108 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 91aec910..d7f3d819 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,6 @@ #![feature(thread_local)] #![allow(unused_imports)] #![feature(hash_extract_if)] -#![allow(dead_code)] // interface and safeposix are public because otherwise there isn't a great // way to 'use' them for benchmarking. @@ -12,102 +11,3 @@ pub mod interface; pub mod safeposix; pub mod tests; pub mod fdtables; - -use crate::safeposix::dispatcher::*; - -pub fn lind_lindrustinit(verbosity: i32) { - unsafe { - lindrustinit(verbosity as isize); - } -} - -pub fn lind_lindrustfinalize() { - unsafe { - lindrustfinalize(); - } -} - -pub fn lind_rustposix_thread_init(cageid: u64, signalflag: u64) { - unsafe { - rustposix_thread_init(cageid, signalflag); - } -} - -pub fn lind_fork(parent_cageid: u64, child_cageid: u64) -> i32 { - unsafe { - lind_syscall_api( - parent_cageid, - 68 as u32, - 0, - 0, - child_cageid, - 0, - 0, - 0, - 0, - 0, - ) - } -} - -pub fn lind_exit(cageid: u64, status: i32) -> i32 { - unsafe { - lind_syscall_api( - cageid, - 30 as u32, - 0, - 0, - status as u64, - 0, - 0, - 0, - 0, - 0, - ) - } -} - -pub fn lind_exec(parent_cageid: u64, child_cageid: u64) -> i32 { - unsafe { - lind_syscall_api( - parent_cageid, - 69 as u32, - 0, - 0, - child_cageid, - 0, - 0, - 0, - 0, - 0, - ) - } -} - -pub fn lind_syscall_inner( - cageid: u64, - call_number: u32, - call_name: u64, - start_address: u64, - arg1: u64, - arg2: u64, - arg3: u64, - arg4: u64, - arg5: u64, - arg6: u64, -) -> i32 { - unsafe { - lind_syscall_api( - cageid, - call_number, - call_name, - start_address, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - ) - } -} diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index 5395537d..6d1e8cbd 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -199,7 +199,7 @@ pub extern "C" fn rustposix_thread_init(cageid: u64, signalflag: u64) { } #[no_mangle] -pub extern "C" fn lind_syscall_api( +pub fn lind_syscall_api( cageid: u64, call_number: u32, call_name: u64, @@ -1466,7 +1466,7 @@ pub extern "C" fn lind_syscall_api( } #[no_mangle] -pub extern "C" fn lindcancelinit(cageid: u64) { +pub fn lindcancelinit(cageid: u64) { let cage = interface::cagetable_getref(cageid); cage.cancelstatus .store(true, interface::RustAtomicOrdering::Relaxed); @@ -1474,7 +1474,7 @@ pub extern "C" fn lindcancelinit(cageid: u64) { } #[no_mangle] -pub extern "C" fn lindsetthreadkill(cageid: u64, pthreadid: u64, kill: bool) { +pub fn lindsetthreadkill(cageid: u64, pthreadid: u64, kill: bool) { let cage = interface::cagetable_getref(cageid); cage.thread_table.insert(pthreadid, kill); if cage @@ -1490,18 +1490,18 @@ pub extern "C" fn lindsetthreadkill(cageid: u64, pthreadid: u64, kill: bool) { } #[no_mangle] -pub extern "C" fn lindcheckthread(cageid: u64, pthreadid: u64) -> bool { +pub fn lindcheckthread(cageid: u64, pthreadid: u64) -> bool { interface::check_thread(cageid, pthreadid) } #[no_mangle] -pub extern "C" fn lindthreadremove(cageid: u64, pthreadid: u64) { +pub fn lindthreadremove(cageid: u64, pthreadid: u64) { let cage = interface::cagetable_getref(cageid); cage.thread_table.remove(&pthreadid); } #[no_mangle] -pub extern "C" fn lindgetsighandler(cageid: u64, signo: i32) -> u32 { +pub fn lindgetsighandler(cageid: u64, signo: i32) -> u32 { let cage = interface::cagetable_getref(cageid); let pthreadid = interface::get_pthreadid(); let sigset = cage.sigset.get(&pthreadid).unwrap(); // these lock sigset dashmaps for concurrency @@ -1526,7 +1526,7 @@ pub extern "C" fn lindgetsighandler(cageid: u64, signo: i32) -> u32 { } #[no_mangle] -pub extern "C" fn lindrustinit(verbosity: isize) { +pub fn lindrustinit(verbosity: isize) { let _ = interface::VERBOSE.set(verbosity); //assigned to suppress unused result warning interface::cagetable_init(); @@ -1606,6 +1606,6 @@ pub extern "C" fn lindrustinit(verbosity: isize) { } #[no_mangle] -pub extern "C" fn lindrustfinalize() { +pub fn lindrustfinalize() { interface::cagetable_clear(); } From 39d14a3bd7e2069bdd242133c21a7eae521a1e30 Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Wed, 6 Nov 2024 13:24:22 -0500 Subject: [PATCH 32/37] add author name and clean up --- Cargo.toml | 2 +- src/safeposix/dispatcher.rs | 22 ---------------------- 2 files changed, 1 insertion(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2ddc5ae8..6c1596c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rawposix" version = "0.1.0" -authors = ["Nicholas Smith Renner ", "Jonathan Eli Singer ", "Tristan J. Brigham "] +authors = ["Nicholas Smith Renner ", "Jonathan Eli Singer ", "Tristan J. Brigham ", "Yaxuan Wen ", "Runbin Yuan ", "Qianxi Chen "] edition = "2018" [lib] diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index 6d1e8cbd..a2305341 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -130,7 +130,6 @@ use std::io::{Read, Write}; use std::io; use crate::interface::types; -// use crate::interface::types::SockaddrDummy; use crate::interface::{SigactionStruct, StatData}; use crate::{fdtables, interface}; use crate::interface::errnos::*; @@ -157,27 +156,6 @@ macro_rules! check_and_dispatch { // the following "quick" functions are implemented for research purposes // to increase I/O performance by bypassing the dispatcher and type checker -#[no_mangle] -pub extern "C" fn quick_write(fd: i32, buf: *const u8, count: usize, cageid: u64) -> i32 { - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .write_syscall(fd, buf, count) - } -} - -#[no_mangle] -pub extern "C" fn quick_read(fd: i32, buf: *mut u8, size: usize, cageid: u64) -> i32 { - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .read_syscall(fd, buf, size) - } -} #[no_mangle] pub extern "C" fn rustposix_thread_init(cageid: u64, signalflag: u64) { From 40416fe2cd7235576157f2320640972144d1e0c5 Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Wed, 6 Nov 2024 13:34:48 -0500 Subject: [PATCH 33/37] comment for lind_syscall_api function --- src/safeposix/dispatcher.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index a2305341..e50b6502 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -176,6 +176,10 @@ pub extern "C" fn rustposix_thread_init(cageid: u64, signalflag: u64) { interface::signalflag_set(signalflag); } +/// The `lind_syscall_api` function acts as the central handler for executing various system calls +/// within the Lind virtualized environment, where isolated processes (called "cages") operate +/// independently. This function enables a wide range of system calls (e.g., file I/O, memory +/// management) by interpreting the `call_number` parameter, which specifies the system call type. #[no_mangle] pub fn lind_syscall_api( cageid: u64, From 2751d625f523eaf27334aefe15e7ad6d248dd0c0 Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Wed, 6 Nov 2024 13:40:32 -0500 Subject: [PATCH 34/37] delete comment for types.rs --- src/interface/types.rs | 217 +---------------------------------------- 1 file changed, 3 insertions(+), 214 deletions(-) diff --git a/src/interface/types.rs b/src/interface/types.rs index 75265e7c..3aba6c48 100644 --- a/src/interface/types.rs +++ b/src/interface/types.rs @@ -15,23 +15,6 @@ const SIZEOF_SOCKADDR: u32 = 16; //derive eq attributes for testing whether the structs equal other fsdata structs from stat/fstat #[derive(Eq, PartialEq)] #[repr(C)] -// pub struct FSData { -// pub f_type: u64, -// pub f_bsize: i64, -// pub f_blocks: u64, -// pub f_bfree: u64, -// pub f_bavail: u64, -// //total files in the file system -- should be infinite -// pub f_files: u64, -// //free files in the file system -- should be infinite -// pub f_ffiles: u64, -// pub f_fsid: libc::fsid_t, -// //not really a limit for naming, but 254 works -// // pub f_namelen: u64, -// //arbitrary val for blocksize as well -// // pub f_frsize: u64, -// // pub f_spare: [u8; 32], -// } pub struct FSData { pub f_type: u64, pub f_bsize: u64, @@ -293,23 +276,6 @@ pub fn get_fdset(generic_argument: u64) -> Result, i return Ok(None); } -// pub fn get_fdset(union_argument: Arg) -> Result, i32> { -// let data: *mut libc::fd_set = unsafe { union_argument.dispatch_fdset }; -// if !data.is_null() { -// let internal_fds: &mut interface::FdSet = interface::FdSet::new_from_ptr(data); -// return Ok(Some(internal_fds)); -// } -// return Ok(None); -// } - -// pub fn get_fdset<'a>(union_argument: Arg) -> Result<*mut BitSet, i32> { -// let pointer = unsafe { union_argument.dispatch_fdset }; -// // if !pointer.is_null() { -// // return Ok(pointer); -// // } -// return Ok(pointer); -// } - pub fn get_cstr<'a>(generic_argument: u64) -> Result<&'a str, i32> { //first we check that the pointer is not null //and then we check so that we can get data from the memory @@ -364,17 +330,6 @@ pub fn get_cstrarr<'a>(generic_argument: u64) -> Result, i32> { )); } -// pub fn get_statdatastruct<'a>(union_argument: Arg) -> Result<&'a mut stat, i32> { -// let pointer = unsafe { union_argument.dispatch_statdatastruct }; -// if !pointer.is_null() { -// return Ok(unsafe { &mut *pointer }); -// } -// return Err(syscall_error( -// Errno::EFAULT, -// "dispatcher", -// "input data not valid", -// )); -// } pub fn get_statdatastruct<'a>(generic_argument: u64) -> Result<&'a mut StatData, i32> { let pointer = generic_argument as *mut StatData; if !pointer.is_null() { @@ -387,17 +342,6 @@ pub fn get_statdatastruct<'a>(generic_argument: u64) -> Result<&'a mut StatData, )); } -// pub fn get_fsdatastruct<'a>(union_argument: Arg) -> Result<&'a mut statfs, i32> { -// let pointer = unsafe { union_argument.dispatch_fsdatastruct }; -// if !pointer.is_null() { -// return Ok(unsafe { &mut *pointer }); -// } -// return Err(syscall_error( -// Errno::EFAULT, -// "dispatcher", -// "input data not valid", -// )); -// } pub fn get_fsdatastruct<'a>(generic_argument: u64) -> Result<&'a mut FSData, i32> { let pointer = generic_argument as *mut FSData; if !pointer.is_null() { @@ -436,67 +380,6 @@ pub fn get_ioctlptrunion<'a>(generic_argument: u64) -> Result<&'a mut u8, i32> { )); } -// pub fn get_ioctlptrunion(union_argument: Arg) -> Result { -// return Ok(unsafe { union_argument.dispatch_ioctlptrunion }); -// } - -// pub fn get_ioctl_int<'a>(ptrunion: IoctlPtrUnion) -> Result { -// let pointer = unsafe { ptrunion.int_ptr }; -// if !pointer.is_null() { -// return Ok(unsafe { *pointer }); -// } -// return Err(syscall_error(Errno::EFAULT, "ioctl", "argp is not valid")); -// } - -// pub fn get_ioctl_char<'a>(ptrunion: IoctlPtrUnion) -> Result { -// let pointer = unsafe { ptrunion.c_char_ptr }; -// if !pointer.is_null() { -// return Ok(unsafe { *pointer }); -// } -// return Err(syscall_error(Errno::EFAULT, "ioctl", "argp is not valid")); -// } - -/// Given the vector of tuples produced from getdents_syscall, each of which consists of -/// a ClippedDirent struct and a u8 vector representing the name, and also given the -/// pointer to the base of the buffer to which the getdents structs should be copied, -/// populate said buffer with these getdents structs and the names at the requisite locations -/// -/// We assume a number of things about the tuples that are input: -/// -/// 1. The name in the u8 vec is null terminated -/// 2. After being null terminated it is then padded to the next highest 8 byte boundary -/// 3. After being padded, the last byte of padding is populated with DT_UNKNOWN (0) for now, -/// as the d_type field does not have to be fully implemented for getdents to be POSIX compliant -/// 4. All fields in the clipped dirent, are correctly filled--i.e. d_off has the correct offset -/// of the next struct in the buffer and d_reclen has the length of the struct with the padded name -/// 5. The number of tuples in the vector is such that they all fit in the buffer -/// -/// There is enough information to produce a tuple vector that can satisfy these assumptions well -/// in getdents syscall, and thus all the work to satisfy these assumptions should be done there -// pub fn pack_dirents(dirtuplevec: Vec<(ClippedDirent, Vec)>, baseptr: *mut u8) { -// let mut curptr = baseptr; - -// //for each tuple we write in the ClippedDirent struct, and then the padded name vec -// for dirtuple in dirtuplevec { -// //get pointer to start of next dirent in the buffer as a ClippedDirent pointer -// let curclippedptr = curptr as *mut ClippedDirent; -// //turn that pointer into a rust reference -// let curwrappedptr = unsafe { &mut *curclippedptr }; -// //assign to the data that reference points to with the value of the ClippedDirent from the tuple -// *curwrappedptr = dirtuple.0; - -// //advance pointer by the size of one ClippedDirent, std::mem::size_of should be added into the interface -// curptr = curptr.wrapping_offset(size_of::() as isize); - -// //write, starting from this advanced location, the u8 vec representation of the name -// unsafe { curptr.copy_from(dirtuple.1.as_slice().as_ptr(), dirtuple.1.len()) }; - -// //advance pointer by the size of name, which we assume to be null terminated and padded correctly -// //and thus we are finished with this struct -// curptr = curptr.wrapping_offset(dirtuple.1.len() as isize); -// } -// } - pub fn get_pipearray<'a>(generic_argument: u64) -> Result<&'a mut PipeArray, i32> { let pointer = generic_argument as *mut PipeArray; if !pointer.is_null() { @@ -521,18 +404,6 @@ pub fn get_sockpair<'a>(generic_argument: u64) -> Result<&'a mut SockPair, i32> )); } -// pub fn get_sockaddr<'a>(union_argument: Arg) -> Result<&'a mut SockaddrDummy, i32> { -// let pointer = unsafe { union_argument.dispatch_sockaddrstruct }; -// if !pointer.is_null() { -// return Ok(unsafe { &mut *pointer }); -// } -// return Err(syscall_error( -// Errno::EFAULT, -// "dispatcher", -// "input data not valid", -// )); -// } - pub fn get_constsockaddr<'a>(generic_argument: u64) -> Result<&'a SockaddrDummy, i32> { let pointer = generic_argument as *const SockaddrDummy; if !pointer.is_null() { @@ -608,9 +479,6 @@ pub fn set_gensockaddr(generic_argument: u64, generic_argument1: u64) -> Result< let received = generic_argument as *mut SockaddrDummy; let received_addrlen = (generic_argument1 as *mut u32) as u32; let tmpsock = unsafe { &*received }; - // println!("[Dispatcher set_gen] family: {:?}", tmpsock.sa_family); - // println!("[Dispathcer set_gen] len: {:?}", received_addrlen); - // io::stdout().flush().unwrap(); match tmpsock.sa_family { /*AF_UNIX*/ 1 => { @@ -648,22 +516,13 @@ pub fn set_gensockaddr(generic_argument: u64, generic_argument1: u64) -> Result< "input length too small for family of sockaddr", )); } - // let v6_ptr = pointer as *const interface::SockaddrV6; + let v6_addr = interface::GenSockaddr::V6(interface::SockaddrV6::default()); return Ok(v6_addr); } _ => { - // println!("[Dispatcher] tmpsock.sa_family: {:?}", tmpsock.sa_family); - // io::stdout().flush().unwrap(); let null_addr = interface::GenSockaddr::Unix(interface::SockaddrUnix::default()); return Ok(null_addr); - // let v4_addr = interface::GenSockaddr::V4(interface::SockaddrV4::default()); - // return Ok(v4_addr); - // return Err(syscall_error( - // Errno::EOPNOTSUPP, - // "dispatcher", - // "sockaddr family not supported", - // )) } } } @@ -680,10 +539,7 @@ pub fn copy_out_sockaddr(generic_argument: u64, generic_argument1: u64, gensock: let unixlen = size_of::() as u32; let fullcopylen = interface::rust_min(initaddrlen, unixlen); - // println!("[Dispatcher copy] unixlen: {:?}", unixlen); - // println!("[Dispatcher copy] initaddrlen: {:?}", initaddrlen); - // println!("[Dispatcher copy] fullcopylen: {:?}", fullcopylen); - // io::stdout().flush().unwrap(); + unsafe { std::ptr::copy( (unixa) as *mut interface::SockaddrUnix as *mut u8, @@ -701,10 +557,7 @@ pub fn copy_out_sockaddr(generic_argument: u64, generic_argument1: u64, gensock: let v4len = size_of::() as u32; let fullcopylen = interface::rust_min(initaddrlen, v4len); - // println!("[Dispatcher copy] v4len: {:?}", v4len); - // println!("[Dispatcher copy] initaddrlen: {:?}", initaddrlen); - // println!("[Dispatcher copy] fullcopylen: {:?}", fullcopylen); - // io::stdout().flush().unwrap(); + unsafe { std::ptr::copy( (v4a) as *mut interface::SockaddrV4 as *mut u8, @@ -790,17 +643,6 @@ pub fn get_epollevent<'a>(generic_argument: u64) -> Result<&'a mut EpollEvent, i "input data not valid", )); } -// pub fn get_epollevent<'a>(union_argument: Arg) -> Result<&'a mut epoll_event, i32> { -// let epolleventptr = unsafe { union_argument.dispatch_epollevent }; -// if !epolleventptr.is_null() { -// return Ok(unsafe { &mut *epolleventptr }); -// } -// return Err(syscall_error( -// Errno::EFAULT, -// "dispatcher", -// "input data not valid", -// )); -// } pub fn get_socklen_t_ptr(generic_argument: u64) -> Result { let socklenptr = generic_argument as *mut u32; @@ -858,17 +700,6 @@ pub fn get_itimerval<'a>(generic_argument: u64) -> Result(union_argument: Arg) -> Result<&'a mut itimerval, i32> { -// let pointer = unsafe { union_argument.dispatch_structitimerval }; -// if !pointer.is_null() { -// return Ok(unsafe { &mut *pointer }); -// } -// return Err(syscall_error( -// Errno::EFAULT, -// "dispatcher", -// "input data not valid", -// )); -// } pub fn get_constitimerval<'a>(generic_argument: u64) -> Result, i32> { let pointer = generic_argument as *const ITimerVal; @@ -879,18 +710,6 @@ pub fn get_constitimerval<'a>(generic_argument: u64) -> Result(union_argument: Arg) -> Result<&'a itimerval, i32> { -// let pointer = unsafe { union_argument.dispatch_conststructitimerval }; -// if !pointer.is_null() { -// return Ok(unsafe { &*pointer }); -// } -// return Err(syscall_error( -// Errno::EFAULT, -// "dispatcher", -// "input data not valid", -// )); -// } - pub fn duration_fromtimespec(generic_argument: u64) -> Result { let pointer = generic_argument as *mut TimeSpec; if !pointer.is_null() { @@ -929,8 +748,6 @@ pub fn get_timespec<'a>(generic_argument: u64) -> Result<&'a timespec, i32> { )); } - - pub fn get_duration_from_millis( generic_argument: u64, ) -> Result, i32> { @@ -1004,31 +821,3 @@ pub fn get_iovecstruct(generic_argument: u64) -> Result<*const interface::IovecS "input data not valid", )); } - -// pub fn get_sem<'a>(union_argument: Arg) -> Result<&'a mut sem_t, i32> { -// let pointer = unsafe { union_argument.dispatch_structsem }; -// if !pointer.is_null() { -// return Ok(unsafe { -// &mut *pointer -// }); -// } -// return Err(syscall_error( -// Errno::EFAULT, -// "dispatcher", -// "input data not valid", -// )); -// } - -// pub fn get_ifaddrs<'a>(union_argument: Arg) -> Result<&'a mut ifaddrs, i32> { -// let pointer = unsafe { *union_argument.dispatch_ifaddrs }; -// if !pointer.is_null() { -// return Ok(unsafe { -// &mut *pointer -// }); -// } -// return Err(syscall_error( -// Errno::EFAULT, -// "dispatcher", -// "input data not valid", -// )); -// } \ No newline at end of file From 7346874e874a0bc3686288f3e4262037f1742636 Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Wed, 6 Nov 2024 13:46:25 -0500 Subject: [PATCH 35/37] add name --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 6c1596c4..5682de9c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rawposix" version = "0.1.0" -authors = ["Nicholas Smith Renner ", "Jonathan Eli Singer ", "Tristan J. Brigham ", "Yaxuan Wen ", "Runbin Yuan ", "Qianxi Chen "] +authors = ["Nicholas Smith Renner ", "Jonathan Eli Singer ", "Tristan J. Brigham ", "Yaxuan Wen ", "Runbin Yuan ", "Qianxi Chen ", "Yuchen Zhang "] edition = "2018" [lib] From de3b35afd5183829970d552f4ef01d26dbbd1345 Mon Sep 17 00:00:00 2001 From: qianxichen233 Date: Thu, 7 Nov 2024 18:27:17 +0000 Subject: [PATCH 36/37] update comment for lind_syscall_api --- src/safeposix/dispatcher.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index e50b6502..b111a246 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -176,10 +176,26 @@ pub extern "C" fn rustposix_thread_init(cageid: u64, signalflag: u64) { interface::signalflag_set(signalflag); } -/// The `lind_syscall_api` function acts as the central handler for executing various system calls -/// within the Lind virtualized environment, where isolated processes (called "cages") operate -/// independently. This function enables a wide range of system calls (e.g., file I/O, memory -/// management) by interpreting the `call_number` parameter, which specifies the system call type. +/// The `lind_syscall_api` function acts as the main dispatcher for handling system calls +/// within the Lind virtualized environment. It identifies the syscall to execute based on +/// `call_number`, and then invokes the appropriate syscall with the given arguments within +/// the specified cage (`cageid`). +/// +/// ### Arguments: +/// `lind_syscall_api()` accepts 10 arguments: +/// * `cageid` - Identifier for the cage in which the syscall will be executed. +/// * `call_number` - Unique number for each system call, used to identify which syscall to invoke. +/// * `call_name` - A legacy argument from the initial 3i proposal, currently unused and subject to +/// change with future 3i integration. +/// * `start_address` - Base address of WebAssembly linear memory, used for address translation +/// between virtual and system memory address. +/// * `arg1 - arg6` - Syscall-specific arguments. Any unused argument is set to `0xdeadbeefdeadbeef`. +/// +/// ### Returns: +/// On success, returns the syscall's return value. On failure, returns the negative errno code. +/// +/// ### Panics: +/// * If the specified `cageid` does not exist, the function will panic. #[no_mangle] pub fn lind_syscall_api( cageid: u64, From b9376f1965b6fd475cf45f5354e0739be787421f Mon Sep 17 00:00:00 2001 From: robinyuan1002 Date: Mon, 11 Nov 2024 15:20:54 -0500 Subject: [PATCH 37/37] delete macro and unsafe --- src/safeposix/dispatcher.rs | 849 +++++++++--------------------------- 1 file changed, 201 insertions(+), 648 deletions(-) diff --git a/src/safeposix/dispatcher.rs b/src/safeposix/dispatcher.rs index b111a246..e581e03e 100644 --- a/src/safeposix/dispatcher.rs +++ b/src/safeposix/dispatcher.rs @@ -142,17 +142,6 @@ macro_rules! get_onearg { } }; } -//this macro takes in a syscall invocation name (i.e. cage.fork_syscall), and all of the arguments -//to the syscall. Then it unwraps the arguments, returning the error if any one of them is an error -//value, and returning the value of the function if not. It does this by using the ? operator in -//the body of a closure within the variadic macro -macro_rules! check_and_dispatch { - ( $cage:ident . $func:ident, $($arg:expr),* ) => { - match (|| Ok($cage.$func( $($arg?),* )))() { - Ok(i) => i, Err(i) => i - } - }; -} // the following "quick" functions are implemented for research purposes // to increase I/O performance by bypassing the dispatcher and type checker @@ -216,38 +205,25 @@ pub fn lind_syscall_api( let fd = arg1 as i32; let buf = (start_address + arg2) as *const u8; let count = arg3 as usize; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .write_syscall(fd, buf, count) - } + interface::cagetable_getref(cageid) + .write_syscall(fd, buf, count) } WRITEV_SYSCALL => { let fd = arg1 as i32; let iovec = (start_address + arg2) as *const interface::IovecStruct; let iovcnt = arg3 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .writev_syscall(fd, iovec, iovcnt) - } + + interface::cagetable_getref(cageid) + .writev_syscall(fd, iovec, iovcnt) } MUNMAP_SYSCALL => { let addr = (start_address + arg1) as *mut u8; let len = arg2 as usize; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .munmap_syscall(addr, len) - } + + interface::cagetable_getref(cageid) + .munmap_syscall(addr, len) } MMAP_SYSCALL => { @@ -257,13 +233,9 @@ pub fn lind_syscall_api( let flags = arg4 as i32; let fildes = arg5 as i32; let off = arg6 as i64; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .mmap_syscall(addr, len, prot, flags, fildes, off) - } + + interface::cagetable_getref(cageid) + .mmap_syscall(addr, len, prot, flags, fildes, off) } PREAD_SYSCALL => { @@ -271,37 +243,25 @@ pub fn lind_syscall_api( let buf = (start_address + arg2) as *mut u8; let count = arg3 as usize; let offset = arg4 as i64; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .pread_syscall(fd, buf, count, offset) - } + + interface::cagetable_getref(cageid) + .pread_syscall(fd, buf, count, offset) } READ_SYSCALL => { let fd = arg1 as i32; let buf = (start_address + arg2) as *mut u8; let count = arg3 as usize; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .read_syscall(fd, buf, count) - } + + interface::cagetable_getref(cageid) + .read_syscall(fd, buf, count) } CLOSE_SYSCALL => { let fd = arg1 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .close_syscall(fd) - } + + interface::cagetable_getref(cageid) + .close_syscall(fd) } ACCESS_SYSCALL => { @@ -310,13 +270,9 @@ pub fn lind_syscall_api( Err(_) => return -1, // Handle error appropriately, return an error code }; let amode = arg2 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .access_syscall(path, amode) - } + + interface::cagetable_getref(cageid) + .access_syscall(path, amode) } OPEN_SYSCALL => { @@ -326,60 +282,43 @@ pub fn lind_syscall_api( }; let flags = arg2 as i32; let mode = arg3 as u32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .open_syscall(path, flags, mode) - } + + interface::cagetable_getref(cageid) + .open_syscall(path, flags, mode) } SOCKET_SYSCALL => { let domain = arg1 as i32; let socktype = arg2 as i32; let protocol = arg3 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .socket_syscall(domain, socktype, protocol) - } + + interface::cagetable_getref(cageid) + .socket_syscall(domain, socktype, protocol) } CONNECT_SYSCALL => { let fd = arg1 as i32; let addrlen = arg3 as u32; let addr = get_onearg!(interface::get_sockaddr(arg2, addrlen)); - interface::check_cageid(cageid); - unsafe { - let remoteaddr = match Ok::<&interface::GenSockaddr, i32>(&addr) { - Ok(addr) => addr, - Err(_) => panic!("Failed to get sockaddr"), // Handle error appropriately - }; - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .connect_syscall(fd, remoteaddr) - } + + let remoteaddr = match Ok::<&interface::GenSockaddr, i32>(&addr) { + Ok(addr) => addr, + Err(_) => panic!("Failed to get sockaddr"), // Handle error appropriately + }; + interface::cagetable_getref(cageid) + .connect_syscall(fd, remoteaddr) } BIND_SYSCALL => { let fd = arg1 as i32; let addrlen = arg3 as u32; let addr = interface::get_sockaddr(start_address + arg2, addrlen).unwrap(); - interface::check_cageid(cageid); - unsafe { - let localaddr = match Ok::<&interface::GenSockaddr, i32>(&addr) { - Ok(addr) => addr, - Err(_) => panic!("Failed to get sockaddr"), // Handle error appropriately - }; - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .bind_syscall(fd, localaddr) - } + let localaddr = match Ok::<&interface::GenSockaddr, i32>(&addr) { + Ok(addr) => addr, + Err(_) => panic!("Failed to get sockaddr"), // Handle error appropriately + }; + interface::cagetable_getref(cageid) + .bind_syscall(fd, localaddr) } ACCEPT_SYSCALL => { @@ -388,21 +327,11 @@ pub fn lind_syscall_api( let nullity2 = interface::arg_nullity(arg3); if nullity1 && nullity2 { - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .accept_syscall(arg1 as i32, &mut Some(&mut addr)) - } + interface::cagetable_getref(cageid) + .accept_syscall(arg1 as i32, &mut Some(&mut addr)) } else if !(nullity1 || nullity2) { - interface::check_cageid(cageid); - let rv = unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .accept_syscall(arg1 as i32, &mut Some(&mut addr)) - }; + let rv = interface::cagetable_getref(cageid) + .accept_syscall(arg1 as i32, &mut Some(&mut addr)); if rv >= 0 { interface::copy_out_sockaddr((start_address + arg2), (start_address + arg3), addr); } @@ -417,25 +346,15 @@ pub fn lind_syscall_api( } EXEC_SYSCALL => { - interface::check_cageid(cageid); let child_cageid = arg1 as u64; - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .exec_syscall(child_cageid) - } + interface::cagetable_getref(cageid) + .exec_syscall(child_cageid) } EXIT_SYSCALL => { - interface::check_cageid(cageid); let status = arg1 as i32; - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .exit_syscall(status) - } + interface::cagetable_getref(cageid) + .exit_syscall(status) } RENAME_SYSCALL => { @@ -450,13 +369,8 @@ pub fn lind_syscall_api( CStr::from_ptr(new_ptr as *const i8).to_str().unwrap() }; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .rename_syscall(old, new) - } + interface::cagetable_getref(cageid) + .rename_syscall(old, new) } XSTAT_SYSCALL => { @@ -472,13 +386,8 @@ pub fn lind_syscall_api( CStr::from_ptr(fd_ptr as *const i8).to_str().unwrap() }; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .stat_syscall(fd, buf) - } + interface::cagetable_getref(cageid) + .stat_syscall(fd, buf) } MKDIR_SYSCALL => { @@ -489,13 +398,8 @@ pub fn lind_syscall_api( CStr::from_ptr(fd_ptr as *const i8).to_str().unwrap() }; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .mkdir_syscall(fd, mode) - } + interface::cagetable_getref(cageid) + .mkdir_syscall(fd, mode) } RMDIR_SYSCALL => { @@ -505,51 +409,30 @@ pub fn lind_syscall_api( CStr::from_ptr(fd_ptr as *const i8).to_str().unwrap() }; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .rmdir_syscall(fd) - } + interface::cagetable_getref(cageid) + .rmdir_syscall(fd) } FCHDIR_SYSCALL => { let fd = arg1 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .fchdir_syscall(fd) - } + interface::cagetable_getref(cageid) + .fchdir_syscall(fd) } CHDIR_SYSCALL => { let path = interface::types::get_cstr(start_address + arg1).unwrap(); - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .chdir_syscall(path) - } + interface::cagetable_getref(cageid) + .chdir_syscall(path) } GETCWD_SYSCALL => { let buf = (start_address + arg1) as *mut u8; let bufsize = arg2 as u32; - interface::check_cageid(cageid); - - let ret = unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .getcwd_syscall(buf, bufsize) - }; + let ret = interface::cagetable_getref(cageid) + .getcwd_syscall(buf, bufsize); if ret == 0 { return arg1 as i32; } ret } @@ -557,31 +440,20 @@ pub fn lind_syscall_api( FSTATFS_SYSCALL => { let fd = arg1 as i32; let buf = interface::get_fsdatastruct(start_address + arg2).unwrap(); - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .fstatfs_syscall(fd, buf) - } + + interface::cagetable_getref(cageid) + .fstatfs_syscall(fd, buf) } CHMOD_SYSCALL => { let fd_ptr = (start_address + arg1) as *const u8; - let fd= unsafe { CStr::from_ptr(fd_ptr as *const i8).to_str().unwrap() - }; - + }; let mode = arg2 as u32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .chmod_syscall(fd, mode) - } + interface::cagetable_getref(cageid) + .chmod_syscall(fd, mode) } DUP_SYSCALL => { @@ -592,52 +464,32 @@ pub fn lind_syscall_api( None }; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .dup_syscall(fd, fd2) - } + interface::cagetable_getref(cageid) + .dup_syscall(fd, fd2) } DUP2_SYSCALL => { let fd = arg1 as i32; let fd2 = arg2 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .dup2_syscall(fd, fd2) - } + interface::cagetable_getref(cageid) + .dup2_syscall(fd, fd2) } FCHMOD_SYSCALL => { let fd = arg1 as i32; let mode = arg2 as u32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .fchmod_syscall(fd, mode) - } + interface::cagetable_getref(cageid) + .fchmod_syscall(fd, mode) } FXSTAT_SYSCALL => { let fd = arg1 as i32; let buf = interface::get_statdatastruct(start_address + arg2).unwrap(); - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .fstat_syscall(fd, buf) - } + interface::cagetable_getref(cageid) + .fstat_syscall(fd, buf) } UNLINK_SYSCALL => { @@ -647,13 +499,8 @@ pub fn lind_syscall_api( CStr::from_ptr(fd_ptr as *const i8).to_str().unwrap() }; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .unlink_syscall(fd) - } + interface::cagetable_getref(cageid) + .unlink_syscall(fd) } LINK_SYSCALL => { @@ -667,13 +514,8 @@ pub fn lind_syscall_api( CStr::from_ptr(new_ptr as *const i8).to_str().unwrap() }; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .link_syscall(old_fd, new_fd) - } + interface::cagetable_getref(cageid) + .link_syscall(old_fd, new_fd) } LSEEK_SYSCALL => { @@ -681,13 +523,8 @@ pub fn lind_syscall_api( let offset = arg2 as isize; let whence = arg3 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .lseek_syscall(virtual_fd, offset, whence) - } + interface::cagetable_getref(cageid) + .lseek_syscall(virtual_fd, offset, whence) } IOCTL_SYSCALL => { @@ -695,13 +532,8 @@ pub fn lind_syscall_api( let request = arg2 as u64; let ptrunion = (start_address + arg3) as *mut u8; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .ioctl_syscall(virtual_fd, request, ptrunion) - } + interface::cagetable_getref(cageid) + .ioctl_syscall(virtual_fd, request, ptrunion) } TRUNCATE_SYSCALL => { @@ -712,26 +544,16 @@ pub fn lind_syscall_api( CStr::from_ptr(fd_ptr as *const i8).to_str().unwrap() }; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .truncate_syscall(fd, length) - } + interface::cagetable_getref(cageid) + .truncate_syscall(fd, length) } FTRUNCATE_SYSCALL => { let virtual_fd = arg1 as i32; let length = arg2 as isize; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .ftruncate_syscall(virtual_fd, length) - } + interface::cagetable_getref(cageid) + .ftruncate_syscall(virtual_fd, length) } GETDENTS_SYSCALL => { @@ -739,13 +561,8 @@ pub fn lind_syscall_api( let buf = (start_address + arg2) as *mut u8; let nbytes = arg3 as u32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .getdents_syscall(virtual_fd, buf, nbytes) - } + interface::cagetable_getref(cageid) + .getdents_syscall(virtual_fd, buf, nbytes) } STATFS_SYSCALL => { @@ -756,13 +573,8 @@ pub fn lind_syscall_api( CStr::from_ptr(fd_ptr as *const i8).to_str().unwrap() }; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .statfs_syscall(fd, rposix_databuf) - } + interface::cagetable_getref(cageid) + .statfs_syscall(fd, rposix_databuf) } FCNTL_SYSCALL => { @@ -770,13 +582,8 @@ pub fn lind_syscall_api( let cmd = arg2 as i32; let arg = arg3 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .fcntl_syscall(virtual_fd, cmd, arg) - } + interface::cagetable_getref(cageid) + .fcntl_syscall(virtual_fd, cmd, arg) } RECV_SYSCALL => { @@ -785,13 +592,8 @@ pub fn lind_syscall_api( let buflen = arg3 as usize; let flag = arg4 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .recv_syscall(fd, buf, buflen, flag) - } + interface::cagetable_getref(cageid) + .recv_syscall(fd, buf, buflen, flag) } SENDTO_SYSCALL => { @@ -803,13 +605,8 @@ pub fn lind_syscall_api( let addrlen = arg6 as u32; let addr = interface::get_sockaddr(start_address + arg5, addrlen).unwrap(); - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .sendto_syscall(fd, buf, buflen, flag, &addr) - } + interface::cagetable_getref(cageid) + .sendto_syscall(fd, buf, buflen, flag, &addr) } RECVFROM_SYSCALL => { @@ -820,25 +617,15 @@ pub fn lind_syscall_api( let nullity1 = interface::arg_nullity(arg5); let nullity2 = interface::arg_nullity(arg6); - interface::check_cageid(cageid); - if nullity1 && nullity2 { - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .recvfrom_syscall(fd, buf, buflen, flag, &mut None) + interface::cagetable_getref(cageid) + .recvfrom_syscall(fd, buf, buflen, flag, &mut None) } - } else if !(nullity1 || nullity2) { + else if !(nullity1 || nullity2) { let mut newsockaddr = interface::GenSockaddr::V4(interface::SockaddrV4::default()); //dummy value, rust would complain if we used an uninitialized value here - let rv = unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .recvfrom_syscall(fd, buf, buflen, flag, &mut Some(&mut newsockaddr)) - }; - + let rv = interface::cagetable_getref(cageid) + .recvfrom_syscall(fd, buf, buflen, flag, &mut Some(&mut newsockaddr)); if rv >= 0 { interface::copy_out_sockaddr(start_address + arg5, start_address + arg6, newsockaddr); } @@ -856,13 +643,8 @@ pub fn lind_syscall_api( let virtual_fd = arg1 as i32; let operation = arg2 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .flock_syscall(virtual_fd, operation) - } + interface::cagetable_getref(cageid) + .flock_syscall(virtual_fd, operation) } SHMGET_SYSCALL => { @@ -870,13 +652,8 @@ pub fn lind_syscall_api( let size = arg2 as usize; let shmfig = arg3 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .shmget_syscall(key, size, shmfig) - } + interface::cagetable_getref(cageid) + .shmget_syscall(key, size, shmfig) } SHMAT_SYSCALL => { @@ -884,122 +661,72 @@ pub fn lind_syscall_api( let shmaddr = (start_address + arg2) as *mut u8; let shmflg = arg3 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .shmat_syscall(shmid, shmaddr, shmflg) - } + interface::cagetable_getref(cageid) + .shmat_syscall(shmid, shmaddr, shmflg) } SHMDT_SYSCALL => { let shmaddr = (start_address + arg1) as *mut u8; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .shmdt_syscall(shmaddr) - } + interface::cagetable_getref(cageid) + .shmdt_syscall(shmaddr) } MUTEX_DESTROY_SYSCALL => { let mutex_handle = arg1 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .mutex_destroy_syscall(mutex_handle) - } + interface::cagetable_getref(cageid) + .mutex_destroy_syscall(mutex_handle) } MUTEX_LOCK_SYSCALL => { let mutex_handle = arg1 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .mutex_lock_syscall(mutex_handle) - } + interface::cagetable_getref(cageid) + .mutex_lock_syscall(mutex_handle) } MUTEX_TRYLOCK_SYSCALL => { let mutex_handle = arg1 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .mutex_trylock_syscall(mutex_handle) - } + interface::cagetable_getref(cageid) + .mutex_trylock_syscall(mutex_handle) } MUTEX_UNLOCK_SYSCALL => { let mutex_handle = arg1 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .mutex_unlock_syscall(mutex_handle) - } + interface::cagetable_getref(cageid) + .mutex_unlock_syscall(mutex_handle) } COND_DESTROY_SYSCALL => { let cv_handle = arg1 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .cond_destroy_syscall(cv_handle) - } + interface::cagetable_getref(cageid) + .cond_destroy_syscall(cv_handle) } COND_WAIT_SYSCALL => { let cv_handle = arg1 as i32; let mutex_handle = arg2 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .cond_wait_syscall(cv_handle, mutex_handle) - } + interface::cagetable_getref(cageid) + .cond_wait_syscall(cv_handle, mutex_handle) } COND_BROADCAST_SYSCALL => { let cv_handle = arg1 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .cond_broadcast_syscall(cv_handle) - } + interface::cagetable_getref(cageid) + .cond_broadcast_syscall(cv_handle) } COND_SIGNAL_SYSCALL => { let cv_handle = arg1 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .cond_signal_syscall(cv_handle) - } + interface::cagetable_getref(cageid) + .cond_signal_syscall(cv_handle) } SEM_INIT_SYSCALL => { @@ -1007,73 +734,43 @@ pub fn lind_syscall_api( let pshared = arg2 as i32; let value = arg3 as u32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .sem_init_syscall(sem_handle, pshared, value) - } + interface::cagetable_getref(cageid) + .sem_init_syscall(sem_handle, pshared, value) } SEM_WAIT_SYSCALL => { let sem_handle = arg1 as u32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .sem_wait_syscall(sem_handle) - } + interface::cagetable_getref(cageid) + .sem_wait_syscall(sem_handle) } SEM_TRYWAIT_SYSCALL => { let sem_handle = arg1 as u32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .sem_trywait_syscall(sem_handle) - } + interface::cagetable_getref(cageid) + .sem_trywait_syscall(sem_handle) } SEM_POST_SYSCALL => { let sem_handle = arg1 as u32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .sem_post_syscall(sem_handle) - } + interface::cagetable_getref(cageid) + .sem_post_syscall(sem_handle) } SEM_DESTROY_SYSCALL => { let sem_handle = arg1 as u32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .sem_destroy_syscall(sem_handle) - } + interface::cagetable_getref(cageid) + .sem_destroy_syscall(sem_handle) } SEM_GETVALUE_SYSCALL => { let sem_handle = arg1 as u32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .sem_getvalue_syscall(sem_handle) - } + interface::cagetable_getref(cageid) + .sem_getvalue_syscall(sem_handle) } PWRITE_SYSCALL => { @@ -1082,65 +779,35 @@ pub fn lind_syscall_api( let count = arg3 as usize; let offset = arg4 as i64; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .pwrite_syscall(virtual_fd, buf, count, offset) - } + interface::cagetable_getref(cageid) + .pwrite_syscall(virtual_fd, buf, count, offset) } GETUID_SYSCALL => { - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .getuid_syscall() - } + interface::cagetable_getref(cageid) + .getuid_syscall() } GETEUID_SYSCALL => { - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .geteuid_syscall() - } + interface::cagetable_getref(cageid) + .geteuid_syscall() } GETGID_SYSCALL => { - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .getgid_syscall() - } + interface::cagetable_getref(cageid) + .getgid_syscall() } GETEGID_SYSCALL => { - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .getegid_syscall() - } + interface::cagetable_getref(cageid) + .getegid_syscall() } EPOLL_CREATE_SYSCALL => { let size = arg1 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .epoll_create_syscall(size) - } + interface::cagetable_getref(cageid) + .epoll_create_syscall(size) } SETSOCKOPT_SYSCALL => { @@ -1150,36 +817,21 @@ pub fn lind_syscall_api( let optval = (start_address + arg4) as *mut u8; let optlen = arg5 as u32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .setsockopt_syscall( virtual_fd, level, optname, optval, optlen) - } + interface::cagetable_getref(cageid) + .setsockopt_syscall( virtual_fd, level, optname, optval, optlen) } SHUTDOWN_SYSCALL => { let virtual_fd = arg1 as i32; let how = arg2 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .shutdown_syscall( virtual_fd, how) - } + interface::cagetable_getref(cageid) + .shutdown_syscall( virtual_fd, how) } GETPPID_SYSCALL => { - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .getppid_syscall() - } + interface::cagetable_getref(cageid) + .getppid_syscall() } SEND_SYSCALL => { @@ -1188,106 +840,61 @@ pub fn lind_syscall_api( let buflen = arg3 as usize; let flags = arg4 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .send_syscall( virtual_fd, buf, buflen, flags) - } + interface::cagetable_getref(cageid) + .send_syscall( virtual_fd, buf, buflen, flags) } LISTEN_SYSCALL => { let virtual_fd = arg1 as i32; let backlog = arg2 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .listen_syscall(virtual_fd, backlog) - } + interface::cagetable_getref(cageid) + .listen_syscall(virtual_fd, backlog) } MUTEX_CREATE_SYSCALL => { - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .mutex_create_syscall() - } + interface::cagetable_getref(cageid) + .mutex_create_syscall() } COND_CREATE_SYSCALL => { - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .cond_create_syscall() - } + interface::cagetable_getref(cageid) + .cond_create_syscall() } GETHOSTNAME_SYSCALL => { let name = (start_address + arg1) as *mut u8; let len = arg2 as isize; - interface::check_cageid(cageid); - let ret = unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .gethostname_syscall(name, len) - }; + let ret = interface::cagetable_getref(cageid) + .gethostname_syscall(name, len); ret } GETIFADDRS_SYSCALL => { let buf = (start_address + arg1) as *mut u8; let count = arg2 as usize; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .getifaddrs_syscall(buf, count) - } - } + interface::cagetable_getref(cageid) + .getifaddrs_syscall(buf, count) + } KILL_SYSCALL => { let cage_id = arg1 as i32; let sig = arg2 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .kill_syscall(cage_id, sig) - } + interface::cagetable_getref(cageid) + .kill_syscall(cage_id, sig) } FSYNC_SYSCALL => { let virtual_fd = arg1 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .fsync_syscall(virtual_fd) - } + interface::cagetable_getref(cageid) + .fsync_syscall(virtual_fd) } FDATASYNC_SYSCALL => { let virtual_fd = arg1 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .fdatasync_syscall(virtual_fd) - } + interface::cagetable_getref(cageid) + .fdatasync_syscall(virtual_fd) } SYNC_FILE_RANGE => { @@ -1296,37 +903,23 @@ pub fn lind_syscall_api( let nbytes = arg3 as isize; let flags = arg4 as u32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .sync_file_range_syscall(virtual_fd, offset, nbytes, flags) - } + interface::cagetable_getref(cageid) + .sync_file_range_syscall(virtual_fd, offset, nbytes, flags) } PIPE_SYSCALL => { let pipe = interface::get_pipearray(start_address + arg1).unwrap(); - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .pipe_syscall(pipe) - } + interface::cagetable_getref(cageid) + .pipe_syscall(pipe) } + PIPE2_SYSCALL => { let pipe = interface::get_pipearray(start_address + arg1).unwrap(); let flag = arg2 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .pipe2_syscall(pipe, flag) - } + interface::cagetable_getref(cageid) + .pipe2_syscall(pipe, flag) } GETSOCKNAME_SYSCALL => { @@ -1345,13 +938,8 @@ pub fn lind_syscall_api( ); } - interface::check_cageid(cageid); - let rv = unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .getsockname_syscall(fd, &mut Some(&mut addr)) - }; + let rv = interface::cagetable_getref(cageid) + .getsockname_syscall(fd, &mut Some(&mut addr)); if rv >= 0 { interface::copy_out_sockaddr(start_address + arg2, start_address + arg3, addr); @@ -1367,13 +955,8 @@ pub fn lind_syscall_api( let optval_ptr = (start_address + arg4) as *mut i32; let optval = unsafe { &mut *optval_ptr }; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .getsockopt_syscall(virtual_fd, level, optname, optval) - } + interface::cagetable_getref(cageid) + .getsockopt_syscall(virtual_fd, level, optname, optval) } SOCKETPAIR_SYSCALL => { @@ -1382,13 +965,8 @@ pub fn lind_syscall_api( let protocol = arg3 as i32; let virtual_socket_vector = interface::get_sockpair(start_address + arg4).unwrap(); - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .socketpair_syscall(domain, _type, protocol, virtual_socket_vector) - } + interface::cagetable_getref(cageid) + .socketpair_syscall(domain, _type, protocol, virtual_socket_vector) } POLL_SYSCALL => { @@ -1396,34 +974,19 @@ pub fn lind_syscall_api( let pollfds = interface::get_pollstruct_slice(start_address + arg1, nfds as usize).unwrap(); let timeout = arg3 as i32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .poll_syscall(pollfds, nfds, timeout) - } + interface::cagetable_getref(cageid) + .poll_syscall(pollfds, nfds, timeout) } GETPID_SYSCALL => { - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .getpid_syscall() - } + interface::cagetable_getref(cageid) + .getpid_syscall() } FORK_SYSCALL => { let id = arg1 as u64; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .fork_syscall(id) - } + interface::cagetable_getref(cageid) + .fork_syscall(id) } FUTEX_SYSCALL => { @@ -1434,13 +997,8 @@ pub fn lind_syscall_api( let uaddr2 = arg5 as u32; let val3 = arg6 as u32; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .futex_syscall(uaddr, futex_op, val, timeout, uaddr2, val3) - } + interface::cagetable_getref(cageid) + .futex_syscall(uaddr, futex_op, val, timeout, uaddr2, val3) } NANOSLEEP_TIME64_SYSCALL => { @@ -1449,13 +1007,8 @@ pub fn lind_syscall_api( let req = (start_address + arg3) as usize; let rem = (start_address + arg4) as usize; - interface::check_cageid(cageid); - unsafe { - CAGE_TABLE[cageid as usize] - .as_ref() - .unwrap() - .nanosleep_time64_syscall(clockid, flags, req, rem) - } + interface::cagetable_getref(cageid) + .nanosleep_time64_syscall(clockid, flags, req, rem) } _ => -1, // Return -1 for unknown syscalls