Impl cygwin rand with getrandom

This commit is contained in:
王宇逸
2025-03-07 15:53:00 +08:00
parent 7d80aaaca8
commit 268e734996
3 changed files with 13 additions and 11 deletions
+8
View File
@@ -0,0 +1,8 @@
pub fn fill_bytes(mut bytes: &mut [u8]) {
while !bytes.is_empty() {
let ret =
unsafe { libc::getrandom(bytes.as_mut_ptr().cast(), bytes.len(), libc::GRND_NONBLOCK) };
assert!(ret != -1, "failed to generate random data");
bytes = &mut bytes[ret as usize..];
}
}
+1 -9
View File
@@ -94,14 +94,7 @@ fn getrandom(
let flags = if insecure {
if GRND_INSECURE_AVAILABLE.load(Relaxed) {
#[cfg(target_os = "cygwin")]
{
libc::GRND_NONBLOCK
}
#[cfg(not(target_os = "cygwin"))]
{
libc::GRND_INSECURE
}
libc::GRND_INSECURE
} else {
libc::GRND_NONBLOCK
}
@@ -117,7 +110,6 @@ fn getrandom(
libc::EINTR => continue,
// `GRND_INSECURE` is not available, try
// `GRND_NONBLOCK`.
#[cfg(not(target_os = "cygwin"))]
libc::EINVAL if flags == libc::GRND_INSECURE => {
GRND_INSECURE_AVAILABLE.store(false, Relaxed);
continue;
+4 -2
View File
@@ -1,11 +1,14 @@
cfg_if::cfg_if! {
// Tier 1
if #[cfg(any(target_os = "linux", target_os = "android", target_os = "cygwin"))] {
if #[cfg(any(target_os = "linux", target_os = "android"))] {
mod linux;
pub use linux::{fill_bytes, hashmap_random_keys};
} else if #[cfg(target_os = "windows")] {
mod windows;
pub use windows::fill_bytes;
} else if #[cfg(target_os = "cygwin")] {
mod cygwin;
pub use cygwin::fill_bytes;
} else if #[cfg(target_vendor = "apple")] {
mod apple;
pub use apple::fill_bytes;
@@ -88,7 +91,6 @@
target_os = "android",
all(target_family = "wasm", target_os = "unknown"),
target_os = "xous",
target_os = "cygwin",
)))]
pub fn hashmap_random_keys() -> (u64, u64) {
let mut buf = [0; 16];