From f75127843bb0d841311f1b9c61ac42aa3d4861fa Mon Sep 17 00:00:00 2001 From: Jade Ellis Date: Sat, 21 Nov 2026 01:59:40 +0000 Subject: [PATCH] fix(core): Remove unsound assert_unchecked in compute::getcpu --- src/core/utils/sys/compute.rs | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/core/utils/sys/compute.rs b/src/core/utils/sys/compute.rs index 91abddc28..76795be04 100644 --- a/src/core/utils/sys/compute.rs +++ b/src/core/utils/sys/compute.rs @@ -120,21 +120,13 @@ pub fn cores_available() -> impl Iterator { from_mask(*CORES_AVAILABL pub fn getcpu() -> Result { use crate::{Error, utils::math}; - // SAFETY: This is part of an interface with many low-level calls taking - // many raw params, but it's unclear why this specific call is unsafe. - // Nevertheless the value obtained here is semantically unsafe because it - // can change on the instruction boundary trailing its own acquisition and - // also any other time. + // SAFETY: `sched_getcpu(2)` takes no pointers and has no preconditions. + // Note that it may still return -1 with errno set (e.g. under seccomp + // filters or kernels without getcpu support), so the error branch below + // must remain live. let ret: i32 = unsafe { libc::sched_getcpu() }; - #[cfg(target_os = "linux")] - // SAFETY: On modern linux systems with a vdso if we can optimize away the branch checking - // for error (see getcpu(2)) then this system call becomes a memory access. - unsafe { - std::hint::assert_unchecked(ret >= 0); - }; - - if ret == -1 { + if ret < 0 { return Err(Error::from_errno()); }