From 09015a218ebb311bc62703a6dddf1efa1d683da9 Mon Sep 17 00:00:00 2001 From: elle Date: Sun, 7 Sep 2025 20:04:28 +0000 Subject: [PATCH 01/11] ld_so: remove unsafe transmute in mmap_and_copy Refactors casting the `raw` address as a byte array to use idiomatic Rust APIs instead of the unsafe call to `transmute`. --- src/ld_so/dso.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ld_so/dso.rs b/src/ld_so/dso.rs index 575c255635..0cfa202b3d 100644 --- a/src/ld_so/dso.rs +++ b/src/ld_so/dso.rs @@ -618,7 +618,7 @@ impl DSO { let (ph, _) = dynamic.unwrap(); let vaddr = ph.p_vaddr(endian) as usize; let bytes: [u8; size_of::() / 2] = - unsafe { core::mem::transmute((&_r_debug) as *const RTLDDebug as usize) }; + ((&raw const _r_debug).cast::<*const RTLDDebug>() as usize).to_ne_bytes(); let start = if is_pie_enabled(elf) { vaddr + i * size_of::() + size_of::() / 2 } else { From 01c8942a3ccf3c80d1dd20fe0f69df4355367095 Mon Sep 17 00:00:00 2001 From: elle Date: Sun, 7 Sep 2025 20:20:42 +0000 Subject: [PATCH 02/11] build: rename unsused `crate_dir` variable --- build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 4f02830469..efdfda1143 100644 --- a/build.rs +++ b/build.rs @@ -9,7 +9,7 @@ fn get_target() -> String { } fn main() { - let crate_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"); + let _crate_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"); let target = get_target(); println!("cargo:rerun-if-changed=src/c"); From 6b9103a5ec25b5e7f2c337f5ba546fe420e25bdc Mon Sep 17 00:00:00 2001 From: elle Date: Sun, 7 Sep 2025 20:23:02 +0000 Subject: [PATCH 03/11] generic-rt: remove use of core instrinsics Fixes warning about not using compiler intrinsics. Calls into the `panic` macro instead of directly calling the `abort` intrinsic. Slightly changes the behavior of the function by printing the panic message, unwinding, and aborting. --- generic-rt/src/lib.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/generic-rt/src/lib.rs b/generic-rt/src/lib.rs index 394db7b9f8..9a69e042f0 100644 --- a/generic-rt/src/lib.rs +++ b/generic-rt/src/lib.rs @@ -1,5 +1,4 @@ #![no_std] -#![feature(core_intrinsics)] use core::{ arch::asm, @@ -92,10 +91,8 @@ impl GenericTcb { Some(&mut *Self::current_ptr()?) } } -pub fn panic_notls(_msg: impl core::fmt::Display) -> ! { - //eprintln!("panicked in ld.so: {}", msg); - - core::intrinsics::abort(); +pub fn panic_notls(msg: impl core::fmt::Display) -> ! { + panic!("panicked in ld.so: {msg}"); } pub trait ExpectTlsFree { From 7a78c5a71971067127e1c1eff6033da503d9e17d Mon Sep 17 00:00:00 2001 From: elle Date: Sun, 7 Sep 2025 20:26:38 +0000 Subject: [PATCH 04/11] generic-rt: move format args into format string --- generic-rt/src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/generic-rt/src/lib.rs b/generic-rt/src/lib.rs index 9a69e042f0..f15756a435 100644 --- a/generic-rt/src/lib.rs +++ b/generic-rt/src/lib.rs @@ -107,8 +107,7 @@ impl ExpectTlsFree for Result { match self { Ok(t) => t, Err(err) => panic_notls(format_args!( - "{}: expect failed for Result with err: {:?}", - msg, err + "{msg}: expect failed for Result with err: {err:?}", )), } } @@ -119,7 +118,7 @@ impl ExpectTlsFree for Option { fn expect_notls(self, msg: &str) -> T { match self { Some(t) => t, - None => panic_notls(format_args!("{}: expect failed for Option", msg)), + None => panic_notls(format_args!("{msg}: expect failed for Option")), } } } From a434c0ef12317ab1af3064735a6df8603a8352d9 Mon Sep 17 00:00:00 2001 From: elle Date: Sun, 7 Sep 2025 20:30:18 +0000 Subject: [PATCH 05/11] cargo: add explicit editions Adds explicit `edition` definitions to sub-crates. --- ld_so/Cargo.toml | 1 + src/crt0/Cargo.toml | 1 + src/crti/Cargo.toml | 1 + src/crtn/Cargo.toml | 1 + 4 files changed, 4 insertions(+) diff --git a/ld_so/Cargo.toml b/ld_so/Cargo.toml index d5e0298cd8..7af9864c18 100644 --- a/ld_so/Cargo.toml +++ b/ld_so/Cargo.toml @@ -2,6 +2,7 @@ name = "ld_so" version = "0.1.0" authors = ["Jeremy Soller "] +edition = "2021" [lib] name = "ld_so" diff --git a/src/crt0/Cargo.toml b/src/crt0/Cargo.toml index 8c4bfad227..301ef28b8f 100644 --- a/src/crt0/Cargo.toml +++ b/src/crt0/Cargo.toml @@ -2,6 +2,7 @@ name = "crt0" version = "0.1.0" authors = ["Jeremy Soller "] +edition = "2021" [lib] name = "crt0" diff --git a/src/crti/Cargo.toml b/src/crti/Cargo.toml index 05052d50a9..9c253e38b7 100644 --- a/src/crti/Cargo.toml +++ b/src/crti/Cargo.toml @@ -2,6 +2,7 @@ name = "crti" version = "0.1.0" authors = ["jD91mZM2 "] +edition = "2021" [lib] name = "crti" diff --git a/src/crtn/Cargo.toml b/src/crtn/Cargo.toml index cec7a20b70..a0a2c22be6 100644 --- a/src/crtn/Cargo.toml +++ b/src/crtn/Cargo.toml @@ -2,6 +2,7 @@ name = "crtn" version = "0.1.0" authors = ["jD91mZM2 "] +edition = "2021" [lib] name = "crtn" From 5d77f617f511d966f28e4723105534c95c14a7fd Mon Sep 17 00:00:00 2001 From: elle Date: Sun, 7 Sep 2025 20:47:32 +0000 Subject: [PATCH 06/11] ld_so: use mutex for `_r_debug` Uses a `spin::Mutex` for the global static `_r_debug` instance to remove warning about a global mutable static. Guarantees exclusive mutable access to the `_r_debug` variable. --- src/ld_so/debug.rs | 11 ++++++++++- src/ld_so/dso.rs | 8 ++++++-- src/ld_so/linker.rs | 4 ++-- src/ld_so/start.rs | 4 +--- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/ld_so/debug.rs b/src/ld_so/debug.rs index d48e827a07..0c569fac04 100644 --- a/src/ld_so/debug.rs +++ b/src/ld_so/debug.rs @@ -63,6 +63,15 @@ impl RTLDDebug { } } +/// SAFETY: safe as long as caller wraps the instance in a mutex, +/// or similar structure that guarantees exclusive mutable access. +/// Separate instances must not contain pointers to the same LinkMap instance. +unsafe impl Send for RTLDDebug {} +/// SAFETY: safe as long as caller wraps the instance in a mutex, +/// or similar structure that guarantees exclusive mutable access. +/// Separate instances must not contain pointers to the same LinkMap instance. +unsafe impl Sync for RTLDDebug {} + #[repr(C)] struct LinkMap { /* These members are part of the protocol with the debugger. @@ -126,4 +135,4 @@ impl LinkMap { pub extern "C" fn _dl_debug_state() {} #[no_mangle] -pub static mut _r_debug: RTLDDebug = RTLDDebug::NEW; +pub static _r_debug: spin::Mutex = spin::Mutex::new(RTLDDebug::NEW); diff --git a/src/ld_so/dso.rs b/src/ld_so/dso.rs index 0cfa202b3d..b1639de7ab 100644 --- a/src/ld_so/dso.rs +++ b/src/ld_so/dso.rs @@ -505,7 +505,9 @@ impl DSO { } else { bounds.1 - bounds.0 }; - _r_debug.insert_first(addr, path, addr + l_ld as usize); + _r_debug + .lock() + .insert_first(addr, path, addr + l_ld as usize); slice::from_raw_parts_mut(addr as *mut u8, size) } else { let (start, end) = bounds; @@ -535,7 +537,9 @@ impl DSO { } trace!(" = {:p}", ptr); ptr::write_bytes(ptr as *mut u8, 0, size); - _r_debug.insert(ptr as usize, path, ptr as usize + l_ld as usize); + _r_debug + .lock() + .insert(ptr as usize, path, ptr as usize + l_ld as usize); slice::from_raw_parts_mut(ptr as *mut u8, size) } }; diff --git a/src/ld_so/linker.rs b/src/ld_so/linker.rs index b60a188c52..6ea71c3dfd 100644 --- a/src/ld_so/linker.rs +++ b/src/ld_so/linker.rs @@ -593,7 +593,7 @@ impl Linker { Resolve::Now }; - unsafe { _r_debug.state = RTLDState::RT_ADD }; + _r_debug.lock().state = RTLDState::RT_ADD; _dl_debug_state(); let mut new_objects = Vec::new(); @@ -724,7 +724,7 @@ impl Linker { self.register_object(obj); } - unsafe { _r_debug.state = RTLDState::RT_CONSISTENT }; + _r_debug.lock().state = RTLDState::RT_CONSISTENT; _dl_debug_state(); Ok(loaded_dso) diff --git a/src/ld_so/start.rs b/src/ld_so/start.rs index 26e1a0f3a3..e5d3c41411 100644 --- a/src/ld_so/start.rs +++ b/src/ld_so/start.rs @@ -208,9 +208,7 @@ pub unsafe extern "C" fn relibc_ld_so_start(sp: &'static mut Stack, ld_entry: us }; // we might need global lock for this kind of stuff - unsafe { - _r_debug.r_ldbase = ld_entry; - } + _r_debug.lock().r_ldbase = ld_entry; // TODO: Fix memory leak, although minimal. unsafe { From a5c41f77d0a96a31590a430a8b47c28f10fcf6c8 Mon Sep 17 00:00:00 2001 From: elle Date: Mon, 8 Sep 2025 20:05:45 +0000 Subject: [PATCH 07/11] ld_so: remove access dead code --- src/ld_so/access.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/ld_so/access.rs b/src/ld_so/access.rs index 1d083fe893..32cd03ab3b 100644 --- a/src/ld_so/access.rs +++ b/src/ld_so/access.rs @@ -1,5 +1,3 @@ -#[cfg(target_os = "redox")] -use crate::header::unistd::{F_OK, R_OK, W_OK, X_OK}; use crate::{ c_str::{CStr, CString}, error::Errno, @@ -7,8 +5,6 @@ use crate::{ }; pub fn accessible(path: &str, mode: c_int) -> Result<(), Errno> { - let path_c = CString::new(path.as_bytes()).unwrap(); /*.map_err(|err| { - Error::Malformed(format!("invalid path '{}': {}", path, err)) - })?;*/ + let path_c = CString::new(path.as_bytes()).unwrap(); unsafe { Sys::access(CStr::from_ptr(path_c.as_ptr()), mode) } } From 9f446e2fd3d82575f1673741562ea4c18fc4c429 Mon Sep 17 00:00:00 2001 From: elle Date: Mon, 8 Sep 2025 20:22:36 +0000 Subject: [PATCH 08/11] redox-rt: remove `signal` dead code --- redox-rt/src/signal.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/redox-rt/src/signal.rs b/redox-rt/src/signal.rs index 31413dd598..6019ecc4fa 100644 --- a/redox-rt/src/signal.rs +++ b/redox-rt/src/signal.rs @@ -152,7 +152,8 @@ unsafe fn inner(stack: &mut SigStack) { flags: SigactionFlags::empty(), }), None, - ); + ) + .ok(); } action }; @@ -546,7 +547,7 @@ bitflags::bitflags! { const STORED_FLAGS: u32 = 0xfe00_0000; -fn default_handler(sig: c_int) { +fn default_handler(_sig: c_int) { unreachable!(); } @@ -569,10 +570,6 @@ pub(crate) static PROC_CONTROL_STRUCT: SigProcControl = SigProcControl { sender_infos: [const { AtomicU64::new(0) }; 32], }; -fn combine_allowset([lo, hi]: [u64; 2]) -> u64 { - (lo >> 32) | ((hi >> 32) << 32) -} - const fn sig_bit(sig: u32) -> u64 { //assert_ne!(sig, 32); //assert_ne!(sig, 0); @@ -631,7 +628,7 @@ pub fn setup_sighandler(tcb: &RtTcb, first_thread: bool) { .expect("failed to sync signal tctl"); // TODO: Inherited set of ignored signals - set_sigmask(Some(0), None); + set_sigmask(Some(0), None).ok(); } pub type RtSigarea = RtTcb; // TODO pub fn current_setsighandler_struct() -> SetSighandlerData { From 5032708f8864e304b1ff269e98f0f2c390f6cba7 Mon Sep 17 00:00:00 2001 From: elle Date: Mon, 8 Sep 2025 20:22:10 +0000 Subject: [PATCH 09/11] redox-rt: fix dead code in `sync` --- redox-rt/src/sync.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/redox-rt/src/sync.rs b/redox-rt/src/sync.rs index 5b56434d70..beae40aa05 100644 --- a/redox-rt/src/sync.rs +++ b/redox-rt/src/sync.rs @@ -11,14 +11,17 @@ pub struct Mutex { pub inner: UnsafeCell, } -const UNLOCKED: u32 = 0; -const LOCKED: u32 = 1; -const WAITING: u32 = 2; - unsafe impl Send for Mutex {} unsafe impl Sync for Mutex {} impl Mutex { + /// Represents an unlocked [Mutex]. + pub const UNLOCKED: u32 = 0; + /// Represents a locked [Mutex]. + pub const LOCKED: u32 = 1; + /// Represents a waiting [Mutex]. + pub const WAITING: u32 = 2; + pub const fn new(t: T) -> Self { Self { lockword: AtomicU32::new(0), @@ -28,7 +31,12 @@ impl Mutex { pub fn lock(&self) -> MutexGuard<'_, T> { while self .lockword - .compare_exchange(UNLOCKED, LOCKED, Ordering::Acquire, Ordering::Relaxed) + .compare_exchange( + Self::UNLOCKED, + Self::LOCKED, + Ordering::Acquire, + Ordering::Relaxed, + ) .is_err() { core::hint::spin_loop(); @@ -53,6 +61,8 @@ impl DerefMut for MutexGuard<'_, T> { } impl Drop for MutexGuard<'_, T> { fn drop(&mut self) { - self.lock.lockword.store(UNLOCKED, Ordering::Release); + self.lock + .lockword + .store(Mutex::::UNLOCKED, Ordering::Release); } } From 5a8c166e67d3dbedf3cde1239e5c502e8eac9e37 Mon Sep 17 00:00:00 2001 From: elle Date: Mon, 8 Sep 2025 21:02:20 +0000 Subject: [PATCH 10/11] redox-rt: clean up unused code Removes unused imports, and replaces calls to `core::intrinsics::abort` with `panic` macro call. --- redox-rt/src/lib.rs | 27 ++++++++++----------------- redox-rt/src/signal.rs | 2 +- redox-rt/src/sys.rs | 2 +- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/redox-rt/src/lib.rs b/redox-rt/src/lib.rs index 1d495bb3ce..b7e35035a7 100644 --- a/redox-rt/src/lib.rs +++ b/redox-rt/src/lib.rs @@ -1,16 +1,9 @@ #![no_std] -#![feature( - asm_const, - core_intrinsics, - int_roundings, - let_chains, - slice_ptr_get, - sync_unsafe_cell -)] +#![feature(int_roundings, let_chains, slice_ptr_get, sync_unsafe_cell)] #![forbid(unreachable_patterns)] use core::{ - cell::{SyncUnsafeCell, UnsafeCell}, + cell::UnsafeCell, mem::{size_of, MaybeUninit}, }; @@ -232,14 +225,14 @@ pub(crate) struct StaticProcInfo { proc_fd: MaybeUninit, has_proc_fd: bool, } -struct DynamicProcInfo { - pgid: u32, - euid: u32, - suid: u32, - ruid: u32, - egid: u32, - rgid: u32, - sgid: u32, +pub struct DynamicProcInfo { + pub pgid: u32, + pub euid: u32, + pub suid: u32, + pub ruid: u32, + pub egid: u32, + pub rgid: u32, + pub sgid: u32, } static DYNAMIC_PROC_INFO: Mutex = Mutex::new(DynamicProcInfo { diff --git a/redox-rt/src/signal.rs b/redox-rt/src/signal.rs index 6019ecc4fa..c61b8ec814 100644 --- a/redox-rt/src/signal.rs +++ b/redox-rt/src/signal.rs @@ -172,7 +172,7 @@ unsafe fn inner(stack: &mut SigStack) { CallFlags::empty(), &[ProcCall::Exit as u64, u64::from(sig) << 8], ); - core::intrinsics::abort() + panic!() } SigactionKind::Handled { handler } => handler, }; diff --git a/redox-rt/src/sys.rs b/redox-rt/src/sys.rs index 14662331d7..2af087742b 100644 --- a/redox-rt/src/sys.rs +++ b/redox-rt/src/sys.rs @@ -324,7 +324,7 @@ pub fn posix_exit(status: i32) -> ! { ) .expect("failed to call proc mgr with Exit"); let _ = syscall::write(1, b"redox-rt: ProcCall::Exit FAILED, abort()ing!\n"); - core::intrinsics::abort(); + panic!(); } pub fn setrens(rns: usize, ens: usize) -> Result<()> { this_proc_call( From e148be2296c0356351fae8c5a6edcd29f2aa3d94 Mon Sep 17 00:00:00 2001 From: elle Date: Mon, 8 Sep 2025 20:21:16 +0000 Subject: [PATCH 11/11] redox-rt: remove unused code from `proc` --- redox-rt/src/proc.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/redox-rt/src/proc.rs b/redox-rt/src/proc.rs index 058f8bccf1..1c7fef24f5 100644 --- a/redox-rt/src/proc.rs +++ b/redox-rt/src/proc.rs @@ -8,7 +8,7 @@ use crate::{ arch::*, auxv_defs::*, protocol::{ProcCall, ThreadCall}, - read_proc_meta, static_proc_info, + read_proc_meta, sys::{proc_call, thread_call}, RtTcb, StaticProcInfo, DYNAMIC_PROC_INFO, }; @@ -30,8 +30,8 @@ use goblin::elf64::{ use syscall::{ error::*, flag::{MapFlags, SEEK_SET}, - CallFlags, GrantDesc, GrantFlags, Map, ProcSchemeAttrs, SetSighandlerData, MAP_FIXED_NOREPLACE, - MAP_SHARED, O_CLOEXEC, PAGE_SIZE, PROT_EXEC, PROT_READ, PROT_WRITE, + CallFlags, GrantDesc, GrantFlags, Map, SetSighandlerData, MAP_FIXED_NOREPLACE, MAP_SHARED, + PAGE_SIZE, PROT_EXEC, PROT_READ, PROT_WRITE, }; pub enum FexecResult { @@ -902,7 +902,7 @@ pub fn fork_inner(initial_rsp: *mut usize, args: &ForkArgs) -> Result { Ok(new_pid) } -struct NewChildProc { +pub struct NewChildProc { proc_fd: Option, thr_fd: FdGuard,