diff --git a/src/header/pthread/attr.rs b/src/header/pthread/attr.rs index e82eac089f..aa2329859e 100644 --- a/src/header/pthread/attr.rs +++ b/src/header/pthread/attr.rs @@ -1,3 +1,6 @@ +// TODO: set this for entire crate when possible +#![deny(unsafe_op_in_unsafe_fn)] + use core::{ptr, sync::atomic::Ordering}; use super::*; @@ -36,7 +39,7 @@ impl Default for RlctAttr { #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_attr_destroy(attr: *mut pthread_attr_t) -> c_int { - core::ptr::drop_in_place(attr); + unsafe { core::ptr::drop_in_place(attr) }; 0 } @@ -45,7 +48,7 @@ pub unsafe extern "C" fn pthread_attr_getdetachstate( attr: *const pthread_attr_t, detachstate: *mut c_int, ) -> c_int { - core::ptr::write(detachstate, (*attr.cast::()).detachstate as _); + unsafe { core::ptr::write(detachstate, (*attr.cast::()).detachstate as _) }; 0 } @@ -54,7 +57,7 @@ pub unsafe extern "C" fn pthread_attr_getguardsize( attr: *const pthread_attr_t, size: *mut size_t, ) -> c_int { - core::ptr::write(size, (*attr.cast::()).guardsize); + unsafe { core::ptr::write(size, (*attr.cast::()).guardsize) }; 0 } @@ -63,7 +66,7 @@ pub unsafe extern "C" fn pthread_attr_getinheritsched( attr: *const pthread_attr_t, inheritsched: *mut c_int, ) -> c_int { - core::ptr::write(inheritsched, (*attr.cast::()).inheritsched as _); + unsafe { core::ptr::write(inheritsched, (*attr.cast::()).inheritsched as _) }; 0 } @@ -72,7 +75,7 @@ pub unsafe extern "C" fn pthread_attr_getschedparam( attr: *const pthread_attr_t, param: *mut sched_param, ) -> c_int { - param.write((*attr.cast::()).param); + unsafe { param.write((*attr.cast::()).param) }; 0 } @@ -81,7 +84,7 @@ pub unsafe extern "C" fn pthread_attr_getschedpolicy( attr: *const pthread_attr_t, policy: *mut c_int, ) -> c_int { - core::ptr::write(policy, (*attr.cast::()).schedpolicy as _); + unsafe { core::ptr::write(policy, (*attr.cast::()).schedpolicy as _) }; 0 } @@ -90,7 +93,7 @@ pub unsafe extern "C" fn pthread_attr_getscope( attr: *const pthread_attr_t, scope: *mut c_int, ) -> c_int { - core::ptr::write(scope, (*attr.cast::()).scope as _); + unsafe { core::ptr::write(scope, (*attr.cast::()).scope as _) }; 0 } @@ -100,8 +103,8 @@ pub unsafe extern "C" fn pthread_attr_getstack( stackaddr: *mut *mut c_void, stacksize: *mut size_t, ) -> c_int { - core::ptr::write(stackaddr, (*attr.cast::()).stack as _); - core::ptr::write(stacksize, (*attr.cast::()).stacksize as _); + unsafe { core::ptr::write(stackaddr, (*attr.cast::()).stack as _) }; + unsafe { core::ptr::write(stacksize, (*attr.cast::()).stacksize as _) }; 0 } @@ -110,13 +113,13 @@ pub unsafe extern "C" fn pthread_attr_getstacksize( attr: *const pthread_attr_t, stacksize: *mut size_t, ) -> c_int { - core::ptr::write(stacksize, (*attr.cast::()).stacksize as _); + unsafe { core::ptr::write(stacksize, (*attr.cast::()).stacksize as _) }; 0 } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_attr_init(attr: *mut pthread_attr_t) -> c_int { - core::ptr::write(attr.cast::(), RlctAttr::default()); + unsafe { core::ptr::write(attr.cast::(), RlctAttr::default()) }; 0 } @@ -125,7 +128,7 @@ pub unsafe extern "C" fn pthread_attr_setdetachstate( attr: *mut pthread_attr_t, detachstate: c_int, ) -> c_int { - (*attr.cast::()).detachstate = detachstate as _; + (unsafe { *attr.cast::() }).detachstate = detachstate as _; 0 } @@ -134,7 +137,7 @@ pub unsafe extern "C" fn pthread_attr_setguardsize( attr: *mut pthread_attr_t, guardsize: c_int, ) -> c_int { - (*attr.cast::()).guardsize = guardsize as _; + (unsafe { *attr.cast::() }).guardsize = guardsize as _; 0 } @@ -143,7 +146,7 @@ pub unsafe extern "C" fn pthread_attr_setinheritsched( attr: *mut pthread_attr_t, inheritsched: c_int, ) -> c_int { - (*attr.cast::()).inheritsched = inheritsched as _; + (unsafe { *attr.cast::() }).inheritsched = inheritsched as _; 0 } @@ -152,7 +155,7 @@ pub unsafe extern "C" fn pthread_attr_setschedparam( attr: *mut pthread_attr_t, param: *const sched_param, ) -> c_int { - (*attr.cast::()).param = param.read(); + (unsafe { *attr.cast::() }).param = unsafe { param.read() }; 0 } @@ -161,13 +164,13 @@ pub unsafe extern "C" fn pthread_attr_setschedpolicy( attr: *mut pthread_attr_t, policy: c_int, ) -> c_int { - (*attr.cast::()).schedpolicy = policy as u8; + (unsafe { *attr.cast::() }).schedpolicy = policy as u8; 0 } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_attr_setscope(attr: *mut pthread_attr_t, scope: c_int) -> c_int { - (*attr.cast::()).scope = scope as u8; + (unsafe { *attr.cast::() }).scope = scope as u8; 0 } @@ -177,8 +180,8 @@ pub unsafe extern "C" fn pthread_attr_setstack( stackaddr: *mut c_void, stacksize: size_t, ) -> c_int { - (*attr.cast::()).stack = stackaddr as usize; - (*attr.cast::()).stacksize = stacksize; + (unsafe { *attr.cast::() }).stack = stackaddr as usize; + (unsafe { *attr.cast::() }).stacksize = stacksize; 0 } @@ -187,7 +190,7 @@ pub unsafe extern "C" fn pthread_attr_setstacksize( attr: *mut pthread_attr_t, stacksize: size_t, ) -> c_int { - (*attr.cast::()).stacksize = stacksize; + (unsafe { *attr.cast::() }).stacksize = stacksize; 0 } @@ -196,10 +199,10 @@ pub unsafe extern "C" fn pthread_getattr_np( thread_ptr: pthread_t, attr_ptr: *mut pthread_attr_t, ) -> c_int { - let thread = &*thread_ptr.cast::(); + let thread = unsafe { &*thread_ptr.cast::() }; let attr_ptr = attr_ptr.cast::(); - ptr::write(attr_ptr, RlctAttr::default()); - let attr = &mut *attr_ptr; + unsafe { ptr::write(attr_ptr, RlctAttr::default()) }; + let attr = unsafe { &mut *attr_ptr }; if thread.flags.load(Ordering::Acquire) & PthreadFlags::DETACHED.bits() != 0 { attr.detachstate = PTHREAD_CREATE_DETACHED as _; } diff --git a/src/header/pthread/barrier.rs b/src/header/pthread/barrier.rs index 06b74e3059..f3618bf685 100644 --- a/src/header/pthread/barrier.rs +++ b/src/header/pthread/barrier.rs @@ -1,3 +1,6 @@ +// TODO: set this for entire crate when possible +#![deny(unsafe_op_in_unsafe_fn)] + use crate::header::errno::*; use core::num::NonZeroU32; @@ -27,7 +30,7 @@ pub unsafe extern "C" fn pthread_barrier_destroy(barrier: *mut pthread_barrier_t // Behavior is undefined if any thread is currently waiting when this is called. // No-op, currently. - core::ptr::drop_in_place(barrier.cast::()); + unsafe { core::ptr::drop_in_place(barrier.cast::()) }; 0 } @@ -39,9 +42,7 @@ pub unsafe extern "C" fn pthread_barrier_init( attr: *const pthread_barrierattr_t, count: c_uint, ) -> c_int { - let attr = attr - .cast::() - .as_ref() + let attr = unsafe { attr.cast::().as_ref() } .copied() .unwrap_or_default(); @@ -49,7 +50,7 @@ pub unsafe extern "C" fn pthread_barrier_init( return EINVAL; }; - barrier.cast::().write(RlctBarrier::new(count)); + unsafe { barrier.cast::().write(RlctBarrier::new(count)) }; 0 } @@ -60,7 +61,7 @@ fn unlikely(condition: bool) -> bool { // Not async-signal-safe. #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_barrier_wait(barrier: *mut pthread_barrier_t) -> c_int { - let barrier = &*barrier.cast::(); + let barrier = unsafe { &*barrier.cast::() }; match barrier.wait() { WaitResult::NotifiedAll => PTHREAD_BARRIER_SERIAL_THREAD, @@ -71,7 +72,7 @@ pub unsafe extern "C" fn pthread_barrier_wait(barrier: *mut pthread_barrier_t) - // Not async-signal-safe. #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_barrierattr_init(attr: *mut pthread_barrierattr_t) -> c_int { - core::ptr::write(attr.cast::(), RlctBarrierAttr::default()); + unsafe { core::ptr::write(attr.cast::(), RlctBarrierAttr::default()) }; 0 } @@ -82,7 +83,7 @@ pub unsafe extern "C" fn pthread_barrierattr_setpshared( attr: *mut pthread_barrierattr_t, pshared: c_int, ) -> c_int { - (*attr.cast::()).pshared = pshared; + (unsafe { *attr.cast::() }).pshared = pshared; 0 } @@ -92,13 +93,13 @@ pub unsafe extern "C" fn pthread_barrierattr_getpshared( attr: *const pthread_barrierattr_t, pshared: *mut c_int, ) -> c_int { - core::ptr::write(pshared, (*attr.cast::()).pshared); + unsafe { core::ptr::write(pshared, (*attr.cast::()).pshared) }; 0 } // Not async-signal-safe. #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_barrierattr_destroy(attr: *mut pthread_barrierattr_t) -> c_int { - core::ptr::drop_in_place(attr); + unsafe { core::ptr::drop_in_place(attr) }; 0 } diff --git a/src/header/pthread/cond.rs b/src/header/pthread/cond.rs index 91b9763775..7fda576d39 100644 --- a/src/header/pthread/cond.rs +++ b/src/header/pthread/cond.rs @@ -1,5 +1,8 @@ // Used design from https://www.remlab.net/op/futex-condvar.shtml +// TODO: set this for entire crate when possible +#![deny(unsafe_op_in_unsafe_fn)] + use crate::header::time::CLOCK_REALTIME; use super::*; @@ -8,13 +11,13 @@ use super::*; #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_cond_broadcast(cond: *mut pthread_cond_t) -> c_int { - e((&*cond.cast::()).broadcast()) + e((unsafe { &*cond.cast::() }).broadcast()) } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_cond_destroy(cond: *mut pthread_cond_t) -> c_int { // No-op - core::ptr::drop_in_place(cond.cast::()); + unsafe { core::ptr::drop_in_place(cond.cast::()) }; 0 } @@ -23,9 +26,7 @@ pub unsafe extern "C" fn pthread_cond_init( cond: *mut pthread_cond_t, attr: *const pthread_condattr_t, ) -> c_int { - let attr = attr - .cast::() - .as_ref() + let attr = unsafe { attr.cast::().as_ref() } .copied() .unwrap_or_default(); @@ -34,14 +35,14 @@ pub unsafe extern "C" fn pthread_cond_init( println!("TODO: pthread_cond_init with monotonic clock"); } - cond.cast::().write(RlctCond::new()); + unsafe { cond.cast::().write(RlctCond::new()) }; 0 } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_cond_signal(cond: *mut pthread_cond_t) -> c_int { - e((&*cond.cast::()).signal()) + e((unsafe { &*cond.cast::() }).signal()) } #[unsafe(no_mangle)] @@ -50,7 +51,8 @@ pub unsafe extern "C" fn pthread_cond_timedwait( mutex: *mut pthread_mutex_t, timeout: *const timespec, ) -> c_int { - e((&*cond.cast::()).timedwait(&*mutex.cast::(), &*timeout)) + e((unsafe { &*cond.cast::() }) + .timedwait(unsafe { &*mutex.cast::() }, unsafe { &*timeout })) } #[unsafe(no_mangle)] @@ -60,7 +62,11 @@ pub unsafe extern "C" fn pthread_cond_clockwait( clock_id: clockid_t, timeout: *const timespec, ) -> c_int { - e((&*cond.cast::()).clockwait(&*mutex.cast::(), &*timeout, clock_id)) + e((unsafe { &*cond.cast::() }).clockwait( + unsafe { &*mutex.cast::() }, + unsafe { &*timeout }, + clock_id, + )) } #[unsafe(no_mangle)] @@ -68,12 +74,12 @@ pub unsafe extern "C" fn pthread_cond_wait( cond: *mut pthread_cond_t, mutex: *mut pthread_mutex_t, ) -> c_int { - e((&*cond.cast::()).wait(&*mutex.cast::())) + e((unsafe { &*cond.cast::() }).wait(unsafe { &*mutex.cast::() })) } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_condattr_destroy(condattr: *mut pthread_condattr_t) -> c_int { - core::ptr::drop_in_place(condattr.cast::()); + unsafe { core::ptr::drop_in_place(condattr.cast::()) }; // No-op 0 } @@ -83,7 +89,7 @@ pub unsafe extern "C" fn pthread_condattr_getclock( condattr: *const pthread_condattr_t, clock: *mut clockid_t, ) -> c_int { - core::ptr::write(clock, (*condattr.cast::()).clock); + unsafe { core::ptr::write(clock, (*condattr.cast::()).clock) }; 0 } @@ -92,16 +98,17 @@ pub unsafe extern "C" fn pthread_condattr_getpshared( condattr: *const pthread_condattr_t, pshared: *mut c_int, ) -> c_int { - core::ptr::write(pshared, (*condattr.cast::()).pshared); + unsafe { core::ptr::write(pshared, (*condattr.cast::()).pshared) }; 0 } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_condattr_init(condattr: *mut pthread_condattr_t) -> c_int { - condattr - .cast::() - .write(RlctCondAttr::default()); - + unsafe { + condattr + .cast::() + .write(RlctCondAttr::default()) + }; 0 } @@ -110,7 +117,7 @@ pub unsafe extern "C" fn pthread_condattr_setclock( condattr: *mut pthread_condattr_t, clock: clockid_t, ) -> c_int { - (*condattr.cast::()).clock = clock; + (unsafe { *condattr.cast::() }).clock = clock; 0 } @@ -119,7 +126,7 @@ pub unsafe extern "C" fn pthread_condattr_setpshared( condattr: *mut pthread_condattr_t, pshared: c_int, ) -> c_int { - (*condattr.cast::()).pshared = pshared; + (unsafe { *condattr.cast::() }).pshared = pshared; 0 } diff --git a/src/header/pthread/mod.rs b/src/header/pthread/mod.rs index f01cfa1f75..eba998a6b1 100644 --- a/src/header/pthread/mod.rs +++ b/src/header/pthread/mod.rs @@ -2,6 +2,9 @@ //! //! See . +// TODO: set this for entire crate when possible +#![deny(unsafe_op_in_unsafe_fn)] + use alloc::collections::LinkedList; use core::{cell::Cell, ptr::NonNull}; @@ -92,7 +95,7 @@ pub static mut fork_hooks: [LinkedList; 3] = [const { LinkedLis /// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_cancel(thread: pthread_t) -> c_int { - match pthread::cancel(&*thread.cast()) { + match unsafe { pthread::cancel(&*thread.cast()) } { Ok(()) => 0, Err(Errno(error)) => error, } @@ -106,11 +109,11 @@ pub unsafe extern "C" fn pthread_create( start_routine: extern "C" fn(arg: *mut c_void) -> *mut c_void, arg: *mut c_void, ) -> c_int { - let attr = attr.cast::().as_ref(); + let attr = unsafe { attr.cast::().as_ref() }; - match pthread::create(attr, start_routine, arg) { + match unsafe { pthread::create(attr, start_routine, arg) } { Ok(ptr) => { - core::ptr::write(pthread, ptr); + unsafe { core::ptr::write(pthread, ptr) }; 0 } Err(Errno(code)) => code, @@ -120,7 +123,7 @@ pub unsafe extern "C" fn pthread_create( /// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_detach(pthread: pthread_t) -> c_int { - match pthread::detach(&*pthread.cast()) { + match unsafe { pthread::detach(&*pthread.cast()) } { Ok(()) => 0, Err(Errno(errno)) => errno, } @@ -160,7 +163,7 @@ pub extern "C" fn pthread_atfork( /// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_exit(retval: *mut c_void) -> ! { - pthread::exit_current_thread(pthread::Retval(retval)) + unsafe { pthread::exit_current_thread(pthread::Retval(retval)) } } // Not in latest POSIX, mark as depreciated? @@ -177,9 +180,9 @@ pub unsafe extern "C" fn pthread_getcpuclockid( thread: pthread_t, clock_out: *mut clockid_t, ) -> c_int { - match pthread::get_cpu_clkid(&*thread.cast()) { + match pthread::get_cpu_clkid(unsafe { &*thread.cast() }) { Ok(clock) => { - clock_out.write(clock); + unsafe { clock_out.write(clock) }; 0 } Err(Errno(error)) => error, @@ -193,11 +196,10 @@ pub unsafe extern "C" fn pthread_getschedparam( policy_out: *mut c_int, param_out: *mut sched_param, ) -> c_int { - match pthread::get_sched_param(&*thread.cast()) { + match pthread::get_sched_param(unsafe { &*thread.cast() }) { Ok((policy, param)) => { - policy_out.write(policy); - param_out.write(param); - + unsafe { policy_out.write(policy) }; + unsafe { param_out.write(param) }; 0 } Err(Errno(error)) => error, @@ -210,10 +212,10 @@ pub use tls::*; /// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_join(thread: pthread_t, retval: *mut *mut c_void) -> c_int { - match pthread::join(&*thread.cast()) { + match unsafe { pthread::join(&*thread.cast()) } { Ok(pthread::Retval(ret)) => { if !retval.is_null() { - core::ptr::write(retval, ret); + unsafe { core::ptr::write(retval, ret) }; } 0 } @@ -233,7 +235,7 @@ pub use self::rwlock::*; /// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_self() -> pthread_t { - pthread::current_thread().unwrap_unchecked() as *const _ as *mut _ + (unsafe { pthread::current_thread().unwrap_unchecked() }) as *const _ as *mut _ } /// See . @@ -244,7 +246,7 @@ pub unsafe extern "C" fn pthread_setcancelstate(state: c_int, oldstate: *mut c_i // POSIX doesn't imply oldstate can be NULL anywhere, but a lot of C code probably // relies on it... if let Some(oldstate) = NonNull::new(oldstate) { - oldstate.write(old); + unsafe { oldstate.write(old) }; } 0 } @@ -260,7 +262,7 @@ pub unsafe extern "C" fn pthread_setcanceltype(ty: c_int, oldty: *mut c_int) -> // POSIX doesn't imply oldty can be NULL anywhere, but a lot of C code probably relies // on it... if let Some(oldty) = NonNull::new(oldty) { - oldty.write(old); + unsafe { oldty.write(old) }; } 0 } @@ -283,13 +285,20 @@ pub unsafe extern "C" fn pthread_setschedparam( policy: c_int, param: *const sched_param, ) -> c_int { - e(pthread::set_sched_param(&*thread.cast(), policy, &*param)) + e(pthread::set_sched_param( + unsafe { &*thread.cast() }, + policy, + unsafe { &*param }, + )) } /// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_setschedprio(thread: pthread_t, prio: c_int) -> c_int { - e(pthread::set_sched_priority(&*thread.cast(), prio)) + e(pthread::set_sched_priority( + unsafe { &*thread.cast() }, + prio, + )) } pub mod spin; @@ -298,7 +307,7 @@ pub use self::spin::*; /// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_testcancel() { - pthread::testcancel(); + unsafe { pthread::testcancel() }; } // Must be the same struct as defined in the pthread_cleanup_push macro. @@ -317,14 +326,14 @@ pub(crate) static CLEANUP_LL_HEAD: Cell<*const CleanupLinkedListEntry> = #[unsafe(no_mangle)] pub unsafe extern "C" fn __relibc_internal_pthread_cleanup_push(new_entry: *mut c_void) { - let new_entry = &mut *new_entry.cast::(); + let new_entry = unsafe { &mut *new_entry.cast::() }; new_entry.prev = CLEANUP_LL_HEAD.get().cast(); CLEANUP_LL_HEAD.set(new_entry); } #[unsafe(no_mangle)] pub unsafe extern "C" fn __relibc_internal_pthread_cleanup_pop(execute: c_int) { - let prev_head = CLEANUP_LL_HEAD.get().read(); + let prev_head = unsafe { CLEANUP_LL_HEAD.get().read() }; CLEANUP_LL_HEAD.set(prev_head.prev.cast()); if execute != 0 { @@ -338,7 +347,7 @@ pub(crate) unsafe fn run_destructor_stack() { let mut ptr = CLEANUP_LL_HEAD.get(); while !ptr.is_null() { - let entry = ptr.read(); + let entry = unsafe { ptr.read() }; ptr = entry.prev.cast(); (entry.routine)(entry.arg); diff --git a/src/header/pthread/mutex.rs b/src/header/pthread/mutex.rs index 0ea0233a17..c2d7becb7d 100644 --- a/src/header/pthread/mutex.rs +++ b/src/header/pthread/mutex.rs @@ -1,3 +1,6 @@ +// TODO: set this for entire crate when possible +#![deny(unsafe_op_in_unsafe_fn)] + use super::*; use crate::{ @@ -12,12 +15,12 @@ use crate::{ #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_mutex_consistent(mutex: *mut pthread_mutex_t) -> c_int { - e((&*mutex.cast::()).make_consistent()) + e((unsafe { &*mutex.cast::() }).make_consistent()) } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_mutex_destroy(mutex: *mut pthread_mutex_t) -> c_int { // No-op - core::ptr::drop_in_place(mutex.cast::()); + unsafe { core::ptr::drop_in_place(mutex.cast::()) }; 0 } @@ -26,9 +29,9 @@ pub unsafe extern "C" fn pthread_mutex_getprioceiling( mutex: *const pthread_mutex_t, prioceiling: *mut c_int, ) -> c_int { - match (&*mutex.cast::()).prioceiling() { + match (unsafe { &*mutex.cast::() }).prioceiling() { Ok(value) => { - prioceiling.write(value); + unsafe { prioceiling.write(value) }; 0 } Err(Errno(errno)) => errno, @@ -40,15 +43,13 @@ pub unsafe extern "C" fn pthread_mutex_init( mutex: *mut pthread_mutex_t, attr: *const pthread_mutexattr_t, ) -> c_int { - let attr = attr - .cast::() - .as_ref() + let attr = unsafe { attr.cast::().as_ref() } .copied() .unwrap_or_default(); match RlctMutex::new(&attr) { Ok(new) => { - mutex.cast::().write(new); + unsafe { mutex.cast::().write(new) }; 0 } @@ -57,7 +58,7 @@ pub unsafe extern "C" fn pthread_mutex_init( } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_mutex_lock(mutex: *mut pthread_mutex_t) -> c_int { - e((&*mutex.cast::()).lock()) + e((unsafe { &*mutex.cast::() }).lock()) } #[unsafe(no_mangle)] @@ -66,9 +67,9 @@ pub unsafe extern "C" fn pthread_mutex_setprioceiling( prioceiling: c_int, old_prioceiling: *mut c_int, ) -> c_int { - match (&*mutex.cast::()).replace_prioceiling(prioceiling) { + match (unsafe { &*mutex.cast::() }).replace_prioceiling(prioceiling) { Ok(old) => { - old_prioceiling.write(old); + unsafe { old_prioceiling.write(old) }; 0 } Err(Errno(errno)) => errno, @@ -80,27 +81,27 @@ pub unsafe extern "C" fn pthread_mutex_timedlock( mutex: *mut pthread_mutex_t, abstime: *const timespec, ) -> c_int { - let relative = match timespec_realtime_to_monotonic(*abstime) { + let relative = match timespec_realtime_to_monotonic(unsafe { *abstime }) { Ok(relative) => relative, Err(err) => return e(Err(err)), }; - e((&*mutex.cast::()).lock_with_timeout(&relative)) + e((unsafe { &*mutex.cast::() }).lock_with_timeout(&relative)) } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_mutex_trylock(mutex: *mut pthread_mutex_t) -> c_int { - e((&*mutex.cast::()).try_lock()) + e((unsafe { &*mutex.cast::() }).try_lock()) } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_mutex_unlock(mutex: *mut pthread_mutex_t) -> c_int { - e((&*mutex.cast::()).unlock()) + e((unsafe { &*mutex.cast::() }).unlock()) } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_mutexattr_destroy(attr: *mut pthread_mutexattr_t) -> c_int { // No-op - core::ptr::drop_in_place(attr.cast::()); + unsafe { core::ptr::drop_in_place(attr.cast::()) }; 0 } @@ -109,7 +110,7 @@ pub unsafe extern "C" fn pthread_mutexattr_getprioceiling( attr: *const pthread_mutexattr_t, prioceiling: *mut c_int, ) -> c_int { - prioceiling.write((*attr.cast::()).prioceiling); + unsafe { prioceiling.write((*attr.cast::()).prioceiling) }; 0 } @@ -118,7 +119,7 @@ pub unsafe extern "C" fn pthread_mutexattr_getprotocol( attr: *const pthread_mutexattr_t, protocol: *mut c_int, ) -> c_int { - protocol.write((*attr.cast::()).protocol); + unsafe { protocol.write((*attr.cast::()).protocol) }; 0 } @@ -127,7 +128,7 @@ pub unsafe extern "C" fn pthread_mutexattr_getpshared( attr: *const pthread_mutexattr_t, pshared: *mut c_int, ) -> c_int { - pshared.write((*attr.cast::()).pshared); + unsafe { pshared.write((*attr.cast::()).pshared) }; 0 } @@ -136,7 +137,7 @@ pub unsafe extern "C" fn pthread_mutexattr_getrobust( attr: *const pthread_mutexattr_t, robust: *mut c_int, ) -> c_int { - robust.write((*attr.cast::()).robust); + unsafe { robust.write((*attr.cast::()).robust) }; 0 } #[unsafe(no_mangle)] @@ -144,12 +145,12 @@ pub unsafe extern "C" fn pthread_mutexattr_gettype( attr: *const pthread_mutexattr_t, ty: *mut c_int, ) -> c_int { - ty.write((*attr.cast::()).ty); + unsafe { ty.write((*attr.cast::()).ty) }; 0 } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_mutexattr_init(attr: *mut pthread_mutexattr_t) -> c_int { - attr.cast::().write(RlctMutexAttr::default()); + unsafe { attr.cast::().write(RlctMutexAttr::default()) }; 0 } @@ -158,7 +159,7 @@ pub unsafe extern "C" fn pthread_mutexattr_setprioceiling( attr: *mut pthread_mutexattr_t, prioceiling: c_int, ) -> c_int { - (*attr.cast::()).prioceiling = prioceiling; + (unsafe { *attr.cast::() }).prioceiling = prioceiling; 0 } @@ -167,7 +168,7 @@ pub unsafe extern "C" fn pthread_mutexattr_setprotocol( attr: *mut pthread_mutexattr_t, protocol: c_int, ) -> c_int { - (*attr.cast::()).protocol = protocol; + (unsafe { *attr.cast::() }).protocol = protocol; 0 } @@ -176,7 +177,7 @@ pub unsafe extern "C" fn pthread_mutexattr_setpshared( attr: *mut pthread_mutexattr_t, pshared: c_int, ) -> c_int { - (*attr.cast::()).pshared = pshared; + (unsafe { *attr.cast::() }).pshared = pshared; 0 } @@ -185,7 +186,7 @@ pub unsafe extern "C" fn pthread_mutexattr_setrobust( attr: *mut pthread_mutexattr_t, robust: c_int, ) -> c_int { - (*attr.cast::()).robust = robust; + (unsafe { *attr.cast::() }).robust = robust; 0 } #[unsafe(no_mangle)] @@ -193,7 +194,7 @@ pub unsafe extern "C" fn pthread_mutexattr_settype( attr: *mut pthread_mutexattr_t, ty: c_int, ) -> c_int { - (*attr.cast::()).ty = ty; + (unsafe { *attr.cast::() }).ty = ty; 0 } diff --git a/src/header/pthread/once.rs b/src/header/pthread/once.rs index 15100ee976..f13da472c4 100644 --- a/src/header/pthread/once.rs +++ b/src/header/pthread/once.rs @@ -1,3 +1,6 @@ +// TODO: set this for entire crate when possible +#![deny(unsafe_op_in_unsafe_fn)] + use super::*; // PTHREAD_ONCE_INIT @@ -7,7 +10,7 @@ pub unsafe extern "C" fn pthread_once( once: *mut pthread_once_t, constructor: extern "C" fn(), ) -> c_int { - let once = &*once.cast::(); + let once = unsafe { &*once.cast::() }; // TODO: Cancellation points diff --git a/src/header/pthread/rwlock.rs b/src/header/pthread/rwlock.rs index d2259ca3a6..d29cd31515 100644 --- a/src/header/pthread/rwlock.rs +++ b/src/header/pthread/rwlock.rs @@ -1,3 +1,6 @@ +// TODO: set this for entire crate when possible +#![deny(unsafe_op_in_unsafe_fn)] + use super::*; use crate::header::errno::EBUSY; @@ -9,21 +12,21 @@ pub unsafe extern "C" fn pthread_rwlock_init( rwlock: *mut pthread_rwlock_t, attr: *const pthread_rwlockattr_t, ) -> c_int { - let attr = attr - .cast::() - .as_ref() + let attr = unsafe { attr.cast::().as_ref() } .copied() .unwrap_or_default(); - rwlock - .cast::() - .write(RlctRwlock::new(attr.pshared)); + unsafe { + rwlock + .cast::() + .write(RlctRwlock::new(attr.pshared)) + }; 0 } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_rwlock_rdlock(rwlock: *mut pthread_rwlock_t) -> c_int { - get(rwlock).acquire_read_lock(None); + unsafe { get(rwlock) }.acquire_read_lock(None); 0 } @@ -32,7 +35,7 @@ pub unsafe extern "C" fn pthread_rwlock_timedrdlock( rwlock: *mut pthread_rwlock_t, timeout: *const timespec, ) -> c_int { - get(rwlock).acquire_read_lock(Some(&*timeout)); + unsafe { get(rwlock) }.acquire_read_lock(Some(unsafe { &*timeout })); 0 } @@ -41,41 +44,43 @@ pub unsafe extern "C" fn pthread_rwlock_timedwrlock( rwlock: *mut pthread_rwlock_t, timeout: *const timespec, ) -> c_int { - get(rwlock).acquire_write_lock(Some(&*timeout)); + unsafe { get(rwlock) }.acquire_write_lock(Some(unsafe { &*timeout })); 0 } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_rwlock_tryrdlock(rwlock: *mut pthread_rwlock_t) -> c_int { - match get(rwlock).try_acquire_read_lock() { + match unsafe { get(rwlock) }.try_acquire_read_lock() { Ok(()) => 0, Err(_) => EBUSY, } } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_rwlock_trywrlock(rwlock: *mut pthread_rwlock_t) -> c_int { - match get(rwlock).try_acquire_write_lock() { + match unsafe { get(rwlock) }.try_acquire_write_lock() { Ok(()) => 0, Err(_) => EBUSY, } } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_rwlock_unlock(rwlock: *mut pthread_rwlock_t) -> c_int { - get(rwlock).unlock(); + unsafe { get(rwlock) }.unlock(); 0 } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_rwlock_wrlock(rwlock: *mut pthread_rwlock_t) -> c_int { - get(rwlock).acquire_write_lock(None); + unsafe { get(rwlock) }.acquire_write_lock(None); 0 } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_rwlockattr_init(attr: *mut pthread_rwlockattr_t) -> c_int { - attr.cast::() - .write(RlctRwlockAttr::default()); + unsafe { + attr.cast::() + .write(RlctRwlockAttr::default()) + }; 0 } @@ -85,7 +90,7 @@ pub unsafe extern "C" fn pthread_rwlockattr_getpshared( attr: *const pthread_rwlockattr_t, pshared_out: *mut c_int, ) -> c_int { - core::ptr::write(pshared_out, (*attr.cast::()).pshared.raw()); + unsafe { core::ptr::write(pshared_out, (*attr.cast::()).pshared.raw()) }; 0 } @@ -95,7 +100,7 @@ pub unsafe extern "C" fn pthread_rwlockattr_setpshared( attr: *mut pthread_rwlockattr_t, pshared: c_int, ) -> c_int { - (*attr.cast::()).pshared = + (unsafe { *attr.cast::() }).pshared = Pshared::from_raw(pshared).expect("invalid pshared in pthread_rwlockattr_setpshared"); 0 @@ -103,13 +108,13 @@ pub unsafe extern "C" fn pthread_rwlockattr_setpshared( #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_rwlockattr_destroy(attr: *mut pthread_rwlockattr_t) -> c_int { - core::ptr::drop_in_place(attr); + unsafe { core::ptr::drop_in_place(attr) }; 0 } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_rwlock_destroy(rwlock: *mut pthread_rwlock_t) -> c_int { - core::ptr::drop_in_place(rwlock); + unsafe { core::ptr::drop_in_place(rwlock) }; 0 } @@ -122,5 +127,5 @@ pub(crate) struct RlctRwlockAttr { } #[inline] unsafe fn get<'a>(ptr: *mut pthread_rwlock_t) -> &'a RlctRwlock { - &*ptr.cast() + unsafe { &*ptr.cast() } } diff --git a/src/header/pthread/spin.rs b/src/header/pthread/spin.rs index f2b4f55e82..cef8da6035 100644 --- a/src/header/pthread/spin.rs +++ b/src/header/pthread/spin.rs @@ -1,3 +1,6 @@ +// TODO: set this for entire crate when possible +#![deny(unsafe_op_in_unsafe_fn)] + use core::sync::atomic::{AtomicI32 as AtomicInt, Ordering}; use crate::header::errno::EBUSY; @@ -9,7 +12,7 @@ const LOCKED: c_int = 1; #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_spin_destroy(spinlock: *mut pthread_spinlock_t) -> c_int { - let _spinlock = &mut *spinlock.cast::(); + let _spinlock = unsafe { &mut *spinlock.cast::() }; // No-op 0 @@ -22,15 +25,17 @@ pub unsafe extern "C" fn pthread_spin_init( // TODO: pshared doesn't matter in most situations, as memory is just memory, but this may be // different on some architectures... - spinlock.cast::().write(RlctSpinlock { - inner: AtomicInt::new(UNLOCKED), - }); + unsafe { + spinlock.cast::().write(RlctSpinlock { + inner: AtomicInt::new(UNLOCKED), + }) + }; 0 } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_spin_lock(spinlock: *mut pthread_spinlock_t) -> c_int { - let spinlock = &*spinlock.cast::(); + let spinlock = unsafe { &*spinlock.cast::() }; loop { match spinlock.inner.compare_exchange_weak( @@ -48,7 +53,7 @@ pub unsafe extern "C" fn pthread_spin_lock(spinlock: *mut pthread_spinlock_t) -> } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_spin_trylock(spinlock: *mut pthread_spinlock_t) -> c_int { - let spinlock = &*spinlock.cast::(); + let spinlock = unsafe { &*spinlock.cast::() }; match spinlock .inner @@ -62,7 +67,7 @@ pub unsafe extern "C" fn pthread_spin_trylock(spinlock: *mut pthread_spinlock_t) } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_spin_unlock(spinlock: *mut pthread_spinlock_t) -> c_int { - let spinlock = &*spinlock.cast::(); + let spinlock = unsafe { &*spinlock.cast::() }; spinlock.inner.store(UNLOCKED, Ordering::Release); diff --git a/src/header/pthread/tls.rs b/src/header/pthread/tls.rs index 82d214a00e..e0a0276c92 100644 --- a/src/header/pthread/tls.rs +++ b/src/header/pthread/tls.rs @@ -1,3 +1,6 @@ +// TODO: set this for entire crate when possible +#![deny(unsafe_op_in_unsafe_fn)] + use super::*; // TODO: Hashmap? @@ -52,7 +55,7 @@ pub unsafe extern "C" fn pthread_key_create( }, ); - key_ptr.write(key); + unsafe { key_ptr.write(key) }; 0 }