diff --git a/public_autocargo/experimental/Cargo.toml b/public_autocargo/experimental/Cargo.toml index 376f823..265ede5 100644 --- a/public_autocargo/experimental/Cargo.toml +++ b/public_autocargo/experimental/Cargo.toml @@ -17,5 +17,5 @@ edition = "2018" clap = { version = "3.2.25", features = ["derive", "env", "regex", "unicode", "wrap_help"] } goblin = "0.5.2" libc = "0.2.139" -nix = "0.26.4" +nix = { version = "0.29.0", features = ["dir", "event", "hostname", "inotify", "ioctl", "mman", "mount", "net", "poll", "ptrace", "reboot", "resource", "sched", "term", "time", "user", "zerocopy"] } scroll = { version = "0.10", features = ["derive"] } diff --git a/reverie-process/Cargo.toml b/reverie-process/Cargo.toml index c2666a9..75359e5 100644 --- a/reverie-process/Cargo.toml +++ b/reverie-process/Cargo.toml @@ -14,7 +14,7 @@ bitflags = { version = "2.6", features = ["serde"] } colored = "2.1.0" futures = { version = "0.3.30", features = ["async-await", "compat"] } libc = "0.2.139" -nix = "0.26.4" +nix = { version = "0.29.0", features = ["dir", "event", "hostname", "inotify", "ioctl", "mman", "mount", "net", "poll", "ptrace", "reboot", "resource", "sched", "term", "time", "user", "zerocopy"] } serde = { version = "1.0.185", features = ["derive", "rc"] } syscalls = { version = "0.6.7", features = ["serde"] } thiserror = "2" diff --git a/reverie-ptrace/Cargo.toml b/reverie-ptrace/Cargo.toml index 0cccfee..a6ab723 100644 --- a/reverie-ptrace/Cargo.toml +++ b/reverie-ptrace/Cargo.toml @@ -18,7 +18,7 @@ goblin = "0.5.2" iced-x86 = "1.17.0" lazy_static = "1.4" libc = "0.2.139" -nix = "0.26.4" +nix = { version = "0.29.0", features = ["dir", "event", "hostname", "inotify", "ioctl", "mman", "mount", "net", "poll", "ptrace", "reboot", "resource", "sched", "term", "time", "user", "zerocopy"] } num-traits = { version = "0.2.18", default-features = false } paste = "1.0.14" perf-event-open-sys = "4.0" diff --git a/reverie-ptrace/src/gdbstub/session.rs b/reverie-ptrace/src/gdbstub/session.rs index 8864bce..64f76cc 100644 --- a/reverie-ptrace/src/gdbstub/session.rs +++ b/reverie-ptrace/src/gdbstub/session.rs @@ -7,6 +7,7 @@ */ use std::collections::BTreeMap; +use std::os::fd::BorrowedFd; use std::sync::Arc; use bytes::Bytes; @@ -327,7 +328,11 @@ impl Session { Mode::from_bits_truncate(0o644), ) .and_then(|fd| { - let nb = uio::pread(fd, &mut auxv, offset as libc::off_t)?; + let nb = uio::pread( + unsafe { BorrowedFd::borrow_raw(fd) }, + &mut auxv, + offset as libc::off_t, + )?; let _ = unistd::close(fd); Ok(nb) }) { @@ -535,6 +540,7 @@ impl Session { writer.put_str(unistd::close(fd).map_or("F-1", |_| "F0")); } vFile::Pread(fd, count, offset) => { + let fd = unsafe { BorrowedFd::borrow_raw(fd) }; let count = std::cmp::min(count as usize, self.bufsiz); let mut buf: Vec = vec![0; count]; match uio::pread(fd, &mut buf, offset as i64) { @@ -549,15 +555,18 @@ impl Session { } } } - vFile::Pwrite(fd, offset, data) => match uio::pwrite(fd, &data, offset as i64) { - Ok(nb) => { - writer.put_str("F"); - writer.put_num(nb); - } - Err(_) => { - writer.put_str("F-1"); + vFile::Pwrite(fd, offset, data) => { + let fd = unsafe { BorrowedFd::borrow_raw(fd) }; + match uio::pwrite(fd, &data, offset as i64) { + Ok(nb) => { + writer.put_str("F"); + writer.put_num(nb); + } + Err(_) => { + writer.put_str("F-1"); + } } - }, + } vFile::Unlink(fname) => { writer.put_str(unistd::unlink(&fname).map_or("F-1", |_| "F0")); } diff --git a/reverie-ptrace/src/tracer.rs b/reverie-ptrace/src/tracer.rs index c3b4c76..b02987c 100644 --- a/reverie-ptrace/src/tracer.rs +++ b/reverie-ptrace/src/tracer.rs @@ -11,7 +11,7 @@ use std::io::Write; use std::net::SocketAddr; use std::os::fd::AsRawFd; -use std::os::fd::OwnedFd; +use std::os::fd::BorrowedFd; use std::path::PathBuf; use std::sync::Arc; @@ -571,8 +571,6 @@ where L: Tool + 'static, F: FnOnce(), { - use std::os::unix::io::FromRawFd; - // Because this ptrace backend is CENTRALIZED, it can keep all the // tool's state here in a single address space. let global_state = ::init_global_state(&config).await; @@ -584,11 +582,6 @@ where let (read1, write1) = unistd::pipe().map_err(from_nix_error)?; let (read2, write2) = unistd::pipe().map_err(from_nix_error)?; - let read1 = unsafe { OwnedFd::from_raw_fd(read1) }; - let write1 = unsafe { OwnedFd::from_raw_fd(write1) }; - let read2 = unsafe { OwnedFd::from_raw_fd(read2) }; - let write2 = unsafe { OwnedFd::from_raw_fd(write2) }; - // Disable io redirection just before forking. We want the child process to // be able to call `println!()` and have that output go to stdout. // @@ -620,7 +613,7 @@ where Err(e) => { std::io::stdout().flush()?; let _ = nix::unistd::write( - 2, + unsafe { BorrowedFd::borrow_raw(2) }, format!("Forked Rust process panicked, cause: {:?}", e).as_ref(), ); std::process::exit(1); diff --git a/reverie-syscalls/Cargo.toml b/reverie-syscalls/Cargo.toml index 453eae0..47eef99 100644 --- a/reverie-syscalls/Cargo.toml +++ b/reverie-syscalls/Cargo.toml @@ -12,7 +12,7 @@ license = "BSD-2-Clause" bitflags = { version = "2.6", features = ["serde"] } derive_more = { version = "1.0.0", features = ["full"] } libc = "0.2.139" -nix = "0.26.4" +nix = { version = "0.29.0", features = ["dir", "event", "hostname", "inotify", "ioctl", "mman", "mount", "net", "poll", "ptrace", "reboot", "resource", "sched", "term", "time", "user", "zerocopy"] } paste = "1.0.14" reverie-memory = { version = "0.1.0", path = "../reverie-memory" } serde = { version = "1.0.185", features = ["derive", "rc"] } diff --git a/reverie-syscalls/src/raw.rs b/reverie-syscalls/src/raw.rs index a445798..16686f6 100644 --- a/reverie-syscalls/src/raw.rs +++ b/reverie-syscalls/src/raw.rs @@ -116,7 +116,7 @@ macro_rules! impl_raw_bits { ($t:ty : $inner:ty) => { impl $crate::FromToRaw for $t { fn from_raw(raw: usize) -> Self { - unsafe { Self::from_bits_unchecked(raw as $inner) } + Self::from_bits_retain(raw as $inner) } fn into_raw(self) -> usize { diff --git a/reverie-syscalls/src/syscalls/mod.rs b/reverie-syscalls/src/syscalls/mod.rs index 4bc5b73..89af93f 100644 --- a/reverie-syscalls/src/syscalls/mod.rs +++ b/reverie-syscalls/src/syscalls/mod.rs @@ -3563,7 +3563,7 @@ mod test { assert_eq!( format!("{}", syscall.display(&memory)), format!( - "openat(-100, {:p} -> \"/some/file/path\", O_APPEND)", + "openat(-100, {:p} -> \"/some/file/path\", OFlag(O_APPEND))", name.as_ptr() ) ); @@ -3585,7 +3585,7 @@ mod test { .with_flags(OFlag::O_APPEND) .display(&memory) ), - "openat(-100, NULL, O_APPEND)" + "openat(-100, NULL, OFlag(O_APPEND))" ); assert_eq!( @@ -3598,7 +3598,7 @@ mod test { .with_mode(Some(Mode::from_bits_truncate(0o644))) .display(&memory) ), - "openat(-100, NULL, O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)" + "openat(-100, NULL, OFlag(O_CREAT), Mode(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))" ); assert_eq!( @@ -3611,7 +3611,7 @@ mod test { .with_mode(Some(Mode::from_bits_truncate(0o600))) .display(&memory) ), - "openat(-100, NULL, O_DIRECTORY | O_TMPFILE, S_IRUSR | S_IWUSR)" + "openat(-100, NULL, OFlag(O_DIRECTORY | O_TMPFILE), Mode(S_IRUSR | S_IWUSR))" ); #[cfg(target_arch = "x86_64")] @@ -3644,7 +3644,7 @@ mod test { assert_eq!( format!("{}", syscall.display_with_outputs(&memory)), format!( - "stat({:p} -> \"/dev/null\", {:p} -> {{st_mode=S_IFCHR | 0666, st_size=0, ...}})", + "stat({:p} -> \"/dev/null\", {:p} -> {{st_mode=SFlag(S_IFCHR) | 0666, st_size=0, ...}})", name.as_ptr(), &stat as *const _ ) diff --git a/reverie/Cargo.toml b/reverie/Cargo.toml index fe16a46..bcedeb6 100644 --- a/reverie/Cargo.toml +++ b/reverie/Cargo.toml @@ -19,7 +19,7 @@ libc = "0.2.139" linked-hash-map = { version = "0.5", features = ["serde_impl"] } memmap2 = "0.5.10" never-say-never = "6" -nix = "0.26.4" +nix = { version = "0.29.0", features = ["dir", "event", "hostname", "inotify", "ioctl", "mman", "mount", "net", "poll", "ptrace", "reboot", "resource", "sched", "term", "time", "user", "zerocopy"] } object = { version = "0.29", features = ["write"] } procfs = "0.15.1" raw-cpuid = "10.6.0" diff --git a/safeptrace/Cargo.toml b/safeptrace/Cargo.toml index fe60065..2953401 100644 --- a/safeptrace/Cargo.toml +++ b/safeptrace/Cargo.toml @@ -13,7 +13,7 @@ bitflags = { version = "2.6", features = ["serde"] } futures = { version = "0.3.30", features = ["async-await", "compat"] } lazy_static = "1.4" libc = "0.2.139" -nix = "0.26.4" +nix = { version = "0.29.0", features = ["dir", "event", "hostname", "inotify", "ioctl", "mman", "mount", "net", "poll", "ptrace", "reboot", "resource", "sched", "term", "time", "user", "zerocopy"] } parking_lot = { version = "0.12.1", features = ["send_guard"] } reverie-memory = { version = "0.1.0", path = "../reverie-memory", optional = true } reverie-process = { version = "0.1.0", path = "../reverie-process" } diff --git a/safeptrace/src/lib.rs b/safeptrace/src/lib.rs index 04f8e9a..2698d2b 100644 --- a/safeptrace/src/lib.rs +++ b/safeptrace/src/lib.rs @@ -980,6 +980,7 @@ pub fn traceme_and_stop() -> Result<(), Errno> { #[cfg(test)] mod test { use std::io; + use std::os::fd::BorrowedFd; use std::thread; use nix::sys::signal; @@ -1305,7 +1306,7 @@ mod test { _siginfo: *mut libc::siginfo_t, _ucontext: *const libc::c_void, ) { - nix::unistd::write(2, b"caught SIGALRM!").unwrap(); + nix::unistd::write(unsafe { BorrowedFd::borrow_raw(2) }, b"caught SIGALRM!").unwrap(); } #[allow(dead_code)] diff --git a/safeptrace/src/memory.rs b/safeptrace/src/memory.rs index 8fe5f70..5d015a7 100644 --- a/safeptrace/src/memory.rs +++ b/safeptrace/src/memory.rs @@ -7,6 +7,7 @@ */ use core::mem; +use std::ffi::c_long; use std::io; use nix::sys::ptrace; @@ -51,7 +52,7 @@ impl Stopped { ptrace::write( self.0.into(), addr.as_mut_ptr() as *mut ::core::ffi::c_void, - value as *mut ::core::ffi::c_void, + value as c_long, ) } .map_err(|err| Errno::new(err as i32)) diff --git a/tests/basics.rs b/tests/basics.rs index 8e7fd3a..5ab221f 100644 --- a/tests/basics.rs +++ b/tests/basics.rs @@ -12,6 +12,8 @@ use std::ffi::CString; #[allow(unused_imports)] use std::io::Write; +#[allow(unused_imports)] +use std::os::fd::AsRawFd; use std::sync::atomic::AtomicU64; use std::sync::atomic::Ordering; @@ -265,17 +267,17 @@ fn child_should_inherit_fds() { let msg: [u8; 8] = [0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]; match unsafe { unistd::fork() } { Ok(ForkResult::Parent { child, .. }) => { - assert!(unistd::close(fdwrite).is_ok()); + drop(fdwrite); let mut buf: [u8; 8] = [0; 8]; - assert_eq!(unistd::read(fdread, &mut buf), Ok(8)); + assert_eq!(unistd::read(fdread.as_raw_fd(), &mut buf), Ok(8)); assert_eq!(buf, msg); assert_eq!(wait::waitpid(child, None), Ok(WaitStatus::Exited(child, 0))); unsafe { libc::syscall(libc::SYS_exit_group, 0) }; unreachable!(); } Ok(ForkResult::Child) => { - assert!(unistd::close(fdread).is_ok()); - assert_eq!(unistd::write(fdwrite, &msg), Ok(8)); + drop(fdread); + assert_eq!(unistd::write(&fdwrite, &msg), Ok(8)); unsafe { libc::syscall(libc::SYS_exit_group, 0) }; unreachable!(); } diff --git a/tests/delay_signal.rs b/tests/delay_signal.rs index 6cfec0d..b3e45e2 100644 --- a/tests/delay_signal.rs +++ b/tests/delay_signal.rs @@ -142,6 +142,7 @@ impl Tool for LocalState { mod tests { use std::io; use std::mem::MaybeUninit; + use std::os::fd::BorrowedFd; use std::sync::mpsc; use std::thread; use std::time; @@ -217,7 +218,7 @@ mod tests { _siginfo: *mut libc::siginfo_t, _ucontext: *const libc::c_void, ) { - nix::unistd::write(2, b"caught SIGPROF!").unwrap(); + nix::unistd::write(unsafe { BorrowedFd::borrow_raw(2) }, b"caught SIGPROF!").unwrap(); unsafe { libc::syscall(libc::SYS_exit_group, 0); } diff --git a/tests/parallelism.rs b/tests/parallelism.rs index fb1a99f..b8b3224 100644 --- a/tests/parallelism.rs +++ b/tests/parallelism.rs @@ -7,7 +7,6 @@ */ //! Tests for parallelism and concurrency - use reverie::syscalls::Syscall; use reverie::Error; use reverie::GlobalTool; @@ -75,21 +74,25 @@ impl Tool for TestTool { #[test] #[cfg(not(sanitized))] pub fn delay_childprint_test() { + use std::os::fd::BorrowedFd; + use reverie::ExitStatus; use reverie_ptrace::testing::print_tracee_output; use reverie_ptrace::testing::test_fn; + let fd1 = unsafe { BorrowedFd::borrow_raw(1) }; + let (output, _state) = test_fn::(|| { let child = std::thread::spawn(move || { for _ in 0..2 { - nix::unistd::write(1, b"a").unwrap(); + nix::unistd::write(fd1, b"a").unwrap(); } }); for _ in 0..100 { - nix::unistd::write(1, b"b").unwrap(); + nix::unistd::write(fd1, b"b").unwrap(); } child.join().unwrap(); - nix::unistd::write(1, b"\n").unwrap(); + nix::unistd::write(fd1, b"\n").unwrap(); }) .unwrap();