From b81f8f69dfcc7b7835e0f16ecb52864fd6972f9d Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Tue, 19 Jul 2022 11:16:47 +0200 Subject: [PATCH] Adapt clone_grant_using_fmap to fork and deprecate. --- Cargo.lock | 7 +++++++ Cargo.toml | 3 ++- src/clone_grant_using_fmap.rs | 26 ++++++++++++-------------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8ee9ef3..f288860 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,7 @@ version = 3 name = "acid" version = "0.1.0" dependencies = [ + "libc", "redox_syscall 0.2.12 (git+https://gitlab.redox-os.org/redox-os/syscall.git)", "strace", "x86", @@ -23,6 +24,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "libc" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" + [[package]] name = "raw-cpuid" version = "10.3.0" diff --git a/Cargo.toml b/Cargo.toml index 5ff4674..af1c79e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,8 @@ authors = ["Jeremy Soller "] edition = "2018" [dependencies] -x86 = "0.47.0" +libc = "0.2" redox_syscall = { git = "https://gitlab.redox-os.org/redox-os/syscall.git" } strace = { git = "https://gitlab.redox-os.org/redox-os/strace-redox", default-features = false } +x86 = "0.47.0" #strace = { path = "../../strace/source", default-features = false } diff --git a/src/clone_grant_using_fmap.rs b/src/clone_grant_using_fmap.rs index 11a411c..7d72e51 100644 --- a/src/clone_grant_using_fmap.rs +++ b/src/clone_grant_using_fmap.rs @@ -1,3 +1,5 @@ +// TODO: This is no longer implemented by the kernel. Should it be moved to resist? + use syscall::data::{Map, Packet}; use syscall::error::{Error, Result, EFAULT, EINVAL}; use syscall::flag::{CloneFlags, MapFlags, O_CREAT, O_RDONLY, O_RDWR, O_CLOEXEC, WaitFlags}; @@ -28,7 +30,9 @@ impl Daemon { let [read_pipe, write_pipe] = pipes; - if unsafe { clone(CloneFlags::empty())? } == 0 { + let result = unsafe { libc::fork() }; + + if result == 0 { let _ = close(read_pipe); f(Daemon { @@ -36,7 +40,7 @@ impl Daemon { }); // TODO: Replace Infallible with the never type once it is stabilized. unreachable!(); - } else { + } else if result > 0 { let _ = close(write_pipe); let mut data = [0]; @@ -50,6 +54,8 @@ impl Daemon { } else { Err(Error::new(EIO)) } + } else { + return Err(Error::new(std::io::Error::last_os_error().raw_os_error().unwrap_or(EINVAL))); } } @@ -132,24 +138,16 @@ fn inner(readonly: bool) -> Result<()> { let pid; unsafe { - pid = syscall::clone(CloneFlags::empty())?; + pid = libc::fork() as usize; - // Keep in mind these two constants may disappear in the near future. - const PML4_SIZE: usize = 0x0000_0080_0000_0000; - const USER_TMP_GRANT_OFFSET: usize = 7 * PML4_SIZE; - const USER_GRANT_OFFSET: usize = 2 * PML4_SIZE; + assert_ne!(pid, (-1_isize) as usize); - assert!((ptr as usize) >= USER_GRANT_OFFSET); - assert!((ptr as usize) <= USER_TMP_GRANT_OFFSET); - - // Make sure the temporary addresses are not left after the kernel has cloned. - assert_eq!(syscall::virttophys((ptr as usize) - USER_GRANT_OFFSET + USER_TMP_GRANT_OFFSET), Err(Error::new(EFAULT))); - println!("Old memory was correctly unmapped, for the {} process", if pid == 0 { "child" } else { "parent" }); + println!("Fork was successful, for the {} process", if pid == 0 { "child" } else { "parent" }); if pid == 0 { println!("Child process: checking..."); - // We are the child process. Hopefully the kernel copied the grant properly and without + // We are the child process. Hopefully relibc copied the grant properly and without // aliasing. if readonly { assert_eq!(ptr.read_volatile(), 42);