From adc71a523fba822670768a4387b91e6797174944 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Tue, 17 Sep 2024 22:55:14 +0100 Subject: [PATCH 1/5] Declare Windows imports jobserver depends on (#106) --- src/windows.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/windows.rs b/src/windows.rs index 7e6e6d4..0ddfa2e 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -36,6 +36,7 @@ const WAIT_FAILED: DWORD = 4294967295u32; const WAIT_OBJECT_0: DWORD = 0u32; const WAIT_TIMEOUT: DWORD = 258u32; +#[link(name = "kernel32")] extern "system" { fn CloseHandle(handle: HANDLE) -> BOOL; fn SetEvent(hEvent: HANDLE) -> BOOL; @@ -64,6 +65,10 @@ extern "system" { ) -> HANDLE; fn OpenSemaphoreA(dwDesiredAccess: DWORD, bInheritHandle: BOOL, lpName: *const i8) -> HANDLE; fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD; +} + +#[link(name = "advapi32")] +extern "system" { #[link_name = "SystemFunction036"] fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: u32) -> u8; } From ebd38416dbe9f604d6d4fce6b962419938eec144 Mon Sep 17 00:00:00 2001 From: Augie Fackler Date: Tue, 22 Oct 2024 14:20:42 -0400 Subject: [PATCH 2/5] unix: fix typo in safety comment (#107) --- src/unix.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unix.rs b/src/unix.rs index cd05edd..8467368 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -516,7 +516,7 @@ unsafe fn fd_check(fd: c_int, check_pipe: bool) -> Result<(), FromEnvErrorInner> } fn clone_fd_and_set_cloexec(fd: c_int) -> Result { - // Safety: fd is a valid fd dand it remains open until returns + // Safety: fd is a valid fd and it remains open until returns unsafe { BorrowedFd::borrow_raw(fd) } .try_clone_to_owned() .map(File::from) From 9358531d777a00bf463640f23815c36bd2242f0f Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Wed, 26 Mar 2025 21:39:25 +0000 Subject: [PATCH 3/5] Use getrandom for randomness (#110) --- 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( From 665135dbf050176df97b42828171f52452f533b3 Mon Sep 17 00:00:00 2001 From: Xing Xue Date: Tue, 1 Apr 2025 11:05:07 -0400 Subject: [PATCH 4/5] Use sa_sigaction instead of sa_union for AIX. (#109) --- Cargo.toml | 2 +- src/unix.rs | 9 +-------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a98c10d..15243b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ edition = "2021" rust-version = "1.63" [target.'cfg(unix)'.dependencies] -libc = "0.2.87" +libc = "0.2.171" [target.'cfg(unix)'.dev-dependencies] nix = { version = "0.28.0", features = ["fs"] } diff --git a/src/unix.rs b/src/unix.rs index 8467368..2b310c2 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -394,14 +394,7 @@ pub(crate) fn spawn_helper( let mut err = None; USR1_INIT.call_once(|| unsafe { let mut new: libc::sigaction = mem::zeroed(); - #[cfg(target_os = "aix")] - { - new.sa_union.__su_sigaction = sigusr1_handler; - } - #[cfg(not(target_os = "aix"))] - { - new.sa_sigaction = sigusr1_handler as usize; - } + new.sa_sigaction = sigusr1_handler as usize; new.sa_flags = libc::SA_SIGINFO as _; if libc::sigaction(libc::SIGUSR1, &new, ptr::null_mut()) != 0 { err = Some(io::Error::last_os_error()); From 65921f1d58ebb4a2511d45aadcc31d586042d73c Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Tue, 1 Apr 2025 13:02:39 -0400 Subject: [PATCH 5/5] chore: bump to 0.1.33 (#111) --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f6fffe3..dd7c4f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,7 +50,7 @@ dependencies = [ [[package]] name = "jobserver" -version = "0.1.32" +version = "0.1.33" dependencies = [ "getrandom", "libc", diff --git a/Cargo.toml b/Cargo.toml index 15243b9..fa4a761 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jobserver" -version = "0.1.32" +version = "0.1.33" authors = ["Alex Crichton "] license = "MIT OR Apache-2.0" repository = "https://github.com/rust-lang/jobserver-rs"