From 087c4870ac10254b44c160500bfedd4e82f5ade4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Wed, 26 Feb 2025 16:41:50 +0300 Subject: [PATCH 1/7] windows: always use `RtlGenRandom` on pre-1.78 Rust versions --- Cargo.toml | 1 + build.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- src/backends.rs | 2 +- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2441555b..7268ec25 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,6 +83,7 @@ level = "warn" check-cfg = [ 'cfg(getrandom_backend, values("custom", "rdrand", "rndr", "linux_getrandom", "wasm_js"))', 'cfg(getrandom_msan)', + 'cfg(getrandom_windows_legacy)', 'cfg(getrandom_test_linux_fallback)', 'cfg(getrandom_test_linux_without_fallback)', 'cfg(getrandom_test_netbsd_fallback)', diff --git a/build.rs b/build.rs index 15d41919..3710886d 100644 --- a/build.rs +++ b/build.rs @@ -1,9 +1,54 @@ -// Automatically detect cfg(sanitize = "memory") even if cfg(sanitize) isn't -// supported. Build scripts get cfg() info, even if the cfg is unstable. +use std::{env, ffi::OsString, process::Command}; + +/// Minor version of the Rust compiler in which win7 targets were inroduced +const WIN7_INTRODUCED_MINOR_VER: u64 = 78; + +/// Tries to get the minor version of use Rust compiler. +/// +/// If it fails for any reason, returns `None`. +/// +/// Based on the `rustc_version` crate. +fn rustc_minor_version() -> Option { + let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc")); + let mut cmd = if let Some(wrapper) = env::var_os("RUSTC_WRAPPER").filter(|w| !w.is_empty()) { + let mut cmd = Command::new(wrapper); + cmd.arg(rustc); + cmd + } else { + Command::new(rustc) + }; + + let out = cmd.arg("-vV").output().ok()?; + + if !out.status.success() { + return None; + } + + let stdout = str::from_utf8(&out.stdout).ok()?; + + // Assumes that the first line contains "rustc 1.xx.0-channel (abcdef 2025-01-01)" + // where "xx" is the minor version which we want to extract + let mut lines = stdout.lines(); + let first_line = lines.next()?; + let minor_ver_str = first_line.split(".").nth(1)?; + minor_ver_str.parse().ok() +} + fn main() { + // Automatically detect cfg(sanitize = "memory") even if cfg(sanitize) isn't + // supported. Build scripts get cfg() info, even if the cfg is unstable. println!("cargo:rerun-if-changed=build.rs"); let santizers = std::env::var("CARGO_CFG_SANITIZE").unwrap_or_default(); if santizers.contains("memory") { println!("cargo:rustc-cfg=getrandom_msan"); } + + // Use `RtlGenRandom` on older compiler versions since win7 targets + // were introduced only in Rust 1.78 + let win_legacy = rustc_minor_version() + .map(|ver| ver < WIN7_INTRODUCED_MINOR_VER) + .unwrap_or(false); + if win_legacy { + println!("cargo:rustc-cfg=getrandom_windows_legacy"); + } } diff --git a/src/backends.rs b/src/backends.rs index 3b98a61d..f97059c4 100644 --- a/src/backends.rs +++ b/src/backends.rs @@ -145,7 +145,7 @@ cfg_if! { } else if #[cfg(target_os = "solid_asp3")] { mod solid; pub use solid::*; - } else if #[cfg(all(windows, target_vendor = "win7"))] { + } else if #[cfg(all(windows, any(target_vendor = "win7", getrandom_windows_legacy)))] { mod windows7; pub use windows7::*; } else if #[cfg(windows)] { From a921ba4fabd4a0ebe21709d8ef14755e0e0a23ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Wed, 26 Feb 2025 16:46:18 +0300 Subject: [PATCH 2/7] Fix build script --- build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 3710886d..b93deed4 100644 --- a/build.rs +++ b/build.rs @@ -24,7 +24,7 @@ fn rustc_minor_version() -> Option { return None; } - let stdout = str::from_utf8(&out.stdout).ok()?; + let stdout = std::str::from_utf8(&out.stdout).ok()?; // Assumes that the first line contains "rustc 1.xx.0-channel (abcdef 2025-01-01)" // where "xx" is the minor version which we want to extract From d478e53ba136f58b61ad59ce4a79b20ce840ac6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Wed, 26 Feb 2025 16:49:54 +0300 Subject: [PATCH 3/7] Update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 79082409..6fb9b00e 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ fn get_random_u128() -> Result { | Target | Target Triple | Implementation | ------------------ | ------------------ | -------------- | Linux, Android | `*‑linux‑*` | [`getrandom`][1] system call if available, otherwise [`/dev/urandom`][2] after successfully polling `/dev/random` -| Windows 10+ | `*‑windows‑*` | [`ProcessPrng`] +| Windows 10+ | `*‑windows‑*` | [`ProcessPrng`] on Rust 1.78+, [`RtlGenRandom`] otherwise | Windows 7, 8 | `*-win7‑windows‑*` | [`RtlGenRandom`] | macOS | `*‑apple‑darwin` | [`getentropy`][3] | iOS, tvOS, watchOS | `*‑apple‑{ios,tvos,watchos}` | [`CCRandomGenerateBytes`] From fc4866050292139dbfb94f457a63d29c2e8c54f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Wed, 26 Feb 2025 17:03:09 +0300 Subject: [PATCH 4/7] Run version check only for windows targets --- build.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/build.rs b/build.rs index b93deed4..755c35c3 100644 --- a/build.rs +++ b/build.rs @@ -1,8 +1,5 @@ use std::{env, ffi::OsString, process::Command}; -/// Minor version of the Rust compiler in which win7 targets were inroduced -const WIN7_INTRODUCED_MINOR_VER: u64 = 78; - /// Tries to get the minor version of use Rust compiler. /// /// If it fails for any reason, returns `None`. @@ -45,10 +42,16 @@ fn main() { // Use `RtlGenRandom` on older compiler versions since win7 targets // were introduced only in Rust 1.78 - let win_legacy = rustc_minor_version() - .map(|ver| ver < WIN7_INTRODUCED_MINOR_VER) - .unwrap_or(false); - if win_legacy { - println!("cargo:rustc-cfg=getrandom_windows_legacy"); + let target_family = env::var_os("CARGO_CFG_TARGET_FAMILY").and_then(|f| f.into_string().ok()); + if target_family.as_deref() == Some("windows") { + /// Minor version of the Rust compiler in which win7 targets were inroduced + const WIN7_INTRODUCED_MINOR_VER: u64 = 78; + + let win_legacy = rustc_minor_version() + .map(|ver| ver < WIN7_INTRODUCED_MINOR_VER) + .unwrap_or(false); + if win_legacy { + println!("cargo:rustc-cfg=getrandom_windows_legacy"); + } } } From 4481d257842755c1bd4647c7f75a5998cedc3dcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Wed, 26 Feb 2025 17:04:40 +0300 Subject: [PATCH 5/7] Disable the windows-targets dep with enabled getrandom_windows_legacy --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 7268ec25..a5fb2dbe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,7 +67,7 @@ libc = { version = "0.2.154", default-features = false } wasi = { version = "0.14", default-features = false } # windows7 -[target.'cfg(all(windows, not(target_vendor = "win7")))'.dependencies] +[target.'cfg(all(windows, not(target_vendor = "win7"), not(getrandom_windows_legacy)))'.dependencies] windows-targets = "0.53" # wasm_js From fe727d825a736065cd317e58b168eecc88aaa561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Wed, 26 Feb 2025 17:09:28 +0300 Subject: [PATCH 6/7] Tweak build script --- build.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/build.rs b/build.rs index 755c35c3..ea9baaaa 100644 --- a/build.rs +++ b/build.rs @@ -1,7 +1,6 @@ use std::{env, ffi::OsString, process::Command}; -/// Tries to get the minor version of use Rust compiler. -/// +/// Tries to get the minor version of the Rust compiler in use. /// If it fails for any reason, returns `None`. /// /// Based on the `rustc_version` crate. @@ -47,11 +46,12 @@ fn main() { /// Minor version of the Rust compiler in which win7 targets were inroduced const WIN7_INTRODUCED_MINOR_VER: u64 = 78; - let win_legacy = rustc_minor_version() - .map(|ver| ver < WIN7_INTRODUCED_MINOR_VER) - .unwrap_or(false); - if win_legacy { - println!("cargo:rustc-cfg=getrandom_windows_legacy"); + match rustc_minor_version() { + Some(minor_ver) if minor_ver < WIN7_INTRODUCED_MINOR_VER => { + println!("cargo:rustc-cfg=getrandom_windows_legacy"); + } + None => println!("cargo:warning=Couldn't detect minor version of the Rust compiler"), + _ => {} } } } From 315ea84a783a98643e7e6eed93cf5aa7070071f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 3 Mar 2025 14:21:52 +0300 Subject: [PATCH 7/7] Update changelog --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96dba823..f27fdbef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.3.2] - UNRELEASED + +### Changed +- Always use `RtlGenRandom` on Windows targets when compiling with pre-1.78 Rust [#610] + +[#610]: https://github.com/rust-random/getrandom/pull/610 + ## [0.3.1] - 2025-01-28 ### Fixed @@ -526,6 +533,8 @@ Publish initial implementation. ## [0.0.0] - 2019-01-19 Publish an empty template library. +[0.3.2]: https://github.com/rust-random/getrandom/compare/v0.3.0...v0.3.2 +[0.3.1]: https://github.com/rust-random/getrandom/compare/v0.3.0...v0.3.1 [0.3.0]: https://github.com/rust-random/getrandom/compare/v0.2.15...v0.3.0 [0.2.15]: https://github.com/rust-random/getrandom/compare/v0.2.14...v0.2.15 [0.2.14]: https://github.com/rust-random/getrandom/compare/v0.2.13...v0.2.14