From 8ebe174b1f68f00ff102611f0a35af0fb3fcd73d Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Wed, 26 Mar 2025 14:27:11 +0000 Subject: [PATCH] Use getrandom for randomness --- Cargo.lock | 41 +++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 3 +++ src/windows.rs | 30 ++---------------------------- 3 files changed, 44 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bbb8569..f6fffe3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -36,10 +36,23 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" +[[package]] +name = "getrandom" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73fea8450eea4bac3940448fb7ae50d91f034f941199fcd9d909a5a07aa455f0" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasi", +] + [[package]] name = "jobserver" version = "0.1.32" dependencies = [ + "getrandom", "libc", "nix", "tempfile", @@ -47,9 +60,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.153" +version = "0.2.171" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" [[package]] name = "linux-raw-sys" @@ -69,6 +82,12 @@ dependencies = [ "libc", ] +[[package]] +name = "r-efi" +version = "5.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" + [[package]] name = "rustix" version = "0.38.31" @@ -94,6 +113,15 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "wasi" +version = "0.14.2+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" +dependencies = [ + "wit-bindgen-rt", +] + [[package]] name = "windows-sys" version = "0.52.0" @@ -159,3 +187,12 @@ name = "windows_x86_64_msvc" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" + +[[package]] +name = "wit-bindgen-rt" +version = "0.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" +dependencies = [ + "bitflags", +] diff --git a/Cargo.toml b/Cargo.toml index 78df256..a98c10d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,9 @@ libc = "0.2.87" [target.'cfg(unix)'.dev-dependencies] nix = { version = "0.28.0", features = ["fs"] } +[target.'cfg(windows)'.dependencies] +getrandom = { version = "0.3.2", features = ["std"] } + [dev-dependencies] tempfile = "3.10.1" diff --git a/src/windows.rs b/src/windows.rs index 0ddfa2e..550f658 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -67,31 +67,6 @@ extern "system" { fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD; } -#[link(name = "advapi32")] -extern "system" { - #[link_name = "SystemFunction036"] - fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: u32) -> u8; -} - -// Note that we ideally would use the `getrandom` crate, but unfortunately -// that causes build issues when this crate is used in rust-lang/rust (see -// rust-lang/rust#65014 for more information). As a result we just inline -// the pretty simple Windows-specific implementation of generating -// randomness. -fn getrandom(dest: &mut [u8]) -> io::Result<()> { - // Prevent overflow of u32 - for chunk in dest.chunks_mut(u32::MAX as usize) { - let ret = unsafe { RtlGenRandom(chunk.as_mut_ptr(), chunk.len() as u32) }; - if ret == 0 { - return Err(io::Error::new( - io::ErrorKind::Other, - "failed to generate random bytes", - )); - } - } - Ok(()) -} - impl Client { pub fn new(limit: usize) -> io::Result { // Try a bunch of random semaphore names until we get a unique one, @@ -103,9 +78,8 @@ impl Client { // slot and then immediately acquire it (without ever releaseing it // back). for _ in 0..100 { - let mut bytes = [0; 4]; - getrandom(&mut bytes)?; - let mut name = format!("__rust_jobserver_semaphore_{}\0", u32::from_ne_bytes(bytes)); + let bytes = getrandom::u32()?; + let mut name = format!("__rust_jobserver_semaphore_{}\0", bytes); unsafe { let create_limit = if limit == 0 { 1 } else { limit }; let r = CreateSemaphoreA(