From d4b0ef09dfdae5e4bb95ce2c93d75299678dd254 Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Wed, 1 May 2024 00:56:39 -0700 Subject: [PATCH] Use libc::getrandom on DragonflyBSD (#411) This also moves the "only use libc::getrandom" implementaiton to its own file, allowing multiple OSes to use it, simplifying the implementation. Bumps libc to include https://github.com/rust-lang/libc/commit/e79e95f7909c9e04b620b4ec088baaec0377ebb6 Also moves Dragonfly to using `__errno_location`, as that binding was added here: https://github.com/DragonFlyBSD/DragonFlyBSD/commit/60d311380ff2bf02a87700a0f3e6eb53e6034920 which predates the introduction of `getrandom` support. Signed-off-by: Joe Richey --- Cargo.toml | 2 +- src/3ds.rs | 10 ---------- src/dragonfly.rs | 22 ---------------------- src/{hurd.rs => getrandom.rs} | 5 ++--- src/lib.rs | 24 ++++++++++-------------- src/util_libc.rs | 6 +----- 6 files changed, 14 insertions(+), 55 deletions(-) delete mode 100644 src/3ds.rs delete mode 100644 src/dragonfly.rs rename src/{hurd.rs => getrandom.rs} (72%) diff --git a/Cargo.toml b/Cargo.toml index 95e420ff..0d68e1ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ compiler_builtins = { version = "0.1", optional = true } core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" } [target.'cfg(unix)'.dependencies] -libc = { version = "0.2.149", default-features = false } +libc = { version = "0.2.154", default-features = false } [target.'cfg(target_os = "wasi")'.dependencies] wasi = { version = "0.11", default-features = false } diff --git a/src/3ds.rs b/src/3ds.rs deleted file mode 100644 index a5aae77d..00000000 --- a/src/3ds.rs +++ /dev/null @@ -1,10 +0,0 @@ -//! Implementation for Nintendo 3DS -use crate::util_libc::sys_fill_exact; -use crate::Error; -use core::mem::MaybeUninit; - -pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { - sys_fill_exact(dest, |buf| unsafe { - libc::getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0) - }) -} diff --git a/src/dragonfly.rs b/src/dragonfly.rs deleted file mode 100644 index ac4794cd..00000000 --- a/src/dragonfly.rs +++ /dev/null @@ -1,22 +0,0 @@ -//! Implementation for DragonFly BSD -use crate::{ - use_file, - util_libc::{sys_fill_exact, Weak}, - Error, -}; -use core::mem::MaybeUninit; - -pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { - static GETRANDOM: Weak = unsafe { Weak::new("getrandom\0") }; - type GetRandomFn = unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) -> libc::ssize_t; - - // getrandom(2) was introduced in DragonflyBSD 5.7 - if let Some(fptr) = GETRANDOM.ptr() { - let func: GetRandomFn = unsafe { core::mem::transmute(fptr) }; - return sys_fill_exact(dest, |buf| unsafe { - func(buf.as_mut_ptr() as *mut u8, buf.len(), 0) - }); - } else { - use_file::getrandom_inner(dest) - } -} diff --git a/src/hurd.rs b/src/getrandom.rs similarity index 72% rename from src/hurd.rs rename to src/getrandom.rs index 472a7d86..38100384 100644 --- a/src/hurd.rs +++ b/src/getrandom.rs @@ -1,6 +1,5 @@ -//! Implementation for GNU/Hurd -use crate::util_libc::sys_fill_exact; -use crate::Error; +//! Implementation using libc::getrandom +use crate::{util_libc::sys_fill_exact, Error}; use core::mem::MaybeUninit; pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { diff --git a/src/lib.rs b/src/lib.rs index f9ea6ada..31c18001 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ //! | FreeBSD | `*‑freebsd` | [`getrandom`][5] if available, otherwise [`kern.arandom`][6] //! | OpenBSD | `*‑openbsd` | [`getentropy`][7] //! | NetBSD | `*‑netbsd` | [`getrandom`][16] if available, otherwise [`kern.arandom`][8] -//! | Dragonfly BSD | `*‑dragonfly` | [`getrandom`][9] if available, otherwise [`/dev/urandom`][10] (identical to `/dev/random`) +//! | Dragonfly BSD | `*‑dragonfly` | [`getrandom`][9] //! | Solaris, illumos | `*‑solaris`, `*‑illumos` | [`getrandom`][11] if available, otherwise [`/dev/random`][12] //! | Fuchsia OS | `*‑fuchsia` | [`cprng_draw`] //! | Redox | `*‑redox` | `/dev/urandom` @@ -177,7 +177,6 @@ //! [7]: https://man.openbsd.org/getentropy.2 //! [8]: https://man.netbsd.org/sysctl.7 //! [9]: https://leaf.dragonflybsd.org/cgi/web-man?command=getrandom -//! [10]: https://leaf.dragonflybsd.org/cgi/web-man?command=random§ion=4 //! [11]: https://docs.oracle.com/cd/E88353_01/html/E37841/getrandom-2.html //! [12]: https://docs.oracle.com/cd/E86824_01/html/E54777/random-7d.html //! [13]: https://github.com/emscripten-core/emscripten/pull/12240 @@ -239,6 +238,15 @@ cfg_if! { if #[cfg(any(target_os = "haiku", target_os = "redox", target_os = "nto", target_os = "aix"))] { mod util_libc; #[path = "use_file.rs"] mod imp; + } else if #[cfg(any( + target_os = "dragonfly", + target_os = "hurd", + // Check for target_arch = "arm" to only include the 3DS. Does not + // include the Nintendo Switch (which is target_arch = "aarch64"). + all(target_os = "horizon", target_arch = "arm"), + ))] { + mod util_libc; + #[path = "getrandom.rs"] mod imp; } else if #[cfg(all( not(feature = "linux_disable_fallback"), any( @@ -293,10 +301,6 @@ cfg_if! { } else if #[cfg(any(target_os = "freebsd", target_os = "netbsd"))] { mod util_libc; #[path = "bsd_arandom.rs"] mod imp; - } else if #[cfg(target_os = "dragonfly")] { - mod util_libc; - mod use_file; - #[path = "dragonfly.rs"] mod imp; } else if #[cfg(target_os = "fuchsia")] { #[path = "fuchsia.rs"] mod imp; } else if #[cfg(any(target_os = "ios", target_os = "visionos", target_os = "watchos", target_os = "tvos"))] { @@ -320,11 +324,6 @@ cfg_if! { #[path = "espidf.rs"] mod imp; } else if #[cfg(windows)] { #[path = "windows.rs"] mod imp; - } else if #[cfg(all(target_os = "horizon", target_arch = "arm"))] { - // We check for target_arch = "arm" because the Nintendo Switch also - // uses Horizon OS (it is aarch64). - mod util_libc; - #[path = "3ds.rs"] mod imp; } else if #[cfg(target_os = "vita")] { mod util_libc; #[path = "vita.rs"] mod imp; @@ -342,9 +341,6 @@ cfg_if! { any(target_arch = "wasm32", target_arch = "wasm64"), target_os = "unknown"))] { #[path = "js.rs"] mod imp; - } else if #[cfg(target_os = "hurd")] { - mod util_libc; - #[path = "hurd.rs"] mod imp; } else if #[cfg(feature = "custom")] { use custom as imp; } else if #[cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"), diff --git a/src/util_libc.rs b/src/util_libc.rs index e86ef776..129362d5 100644 --- a/src/util_libc.rs +++ b/src/util_libc.rs @@ -11,7 +11,7 @@ use libc::c_void; cfg_if! { if #[cfg(any(target_os = "netbsd", target_os = "openbsd", target_os = "android"))] { use libc::__errno as errno_location; - } else if #[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "hurd", target_os = "redox"))] { + } else if #[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "hurd", target_os = "redox", target_os = "dragonfly"))] { use libc::__errno_location as errno_location; } else if #[cfg(any(target_os = "solaris", target_os = "illumos"))] { use libc::___errno as errno_location; @@ -35,10 +35,6 @@ cfg_if! { cfg_if! { if #[cfg(target_os = "vxworks")] { use libc::errnoGet as get_errno; - } else if #[cfg(target_os = "dragonfly")] { - // Until rust-lang/rust#29594 is stable, we cannot get the errno value - // on DragonFlyBSD. So we just return an out-of-range errno. - unsafe fn get_errno() -> libc::c_int { -1 } } else { unsafe fn get_errno() -> libc::c_int { *errno_location() } }