From 2a13f540b62be4a494276883bc75782d3642bd13 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: Fri, 6 Nov 2026 05:38:59 +0300 Subject: [PATCH 1/3] Move definition of custom backend errors to the `errors` module --- CHANGELOG.md | 3 ++ src/backends.rs | 8 ++--- src/backends/apple_other.rs | 5 --- src/backends/rdrand.rs | 7 ---- src/backends/rndr.rs | 7 ---- src/backends/vxworks.rs | 5 --- src/backends/wasm_js.rs | 5 --- src/backends/windows_legacy.rs | 5 --- src/error.rs | 64 ++++++++++++++++++++++++++++++++-- 9 files changed, 68 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4beb2a0be..188b1ed39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Deprecation warning for `libc::INT_MAX` [#868] +- Compilation error caused by custom backend errors when backend is + overridden using `getrandom_backend` configuration flag ([#876]) ### Changed - Bumped `r-efi` dependency to v7 ([#869]) @@ -22,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#869]: https://github.com/rust-random/getrandom/pull/869 [#870]: https://github.com/rust-random/getrandom/pull/870 [#871]: https://github.com/rust-random/getrandom/pull/871 +[#876]: https://github.com/rust-random/getrandom/pull/876 ## [0.4.3] - 2026-06-17 ### Added diff --git a/src/backends.rs b/src/backends.rs index d644ab62c..8d8755f3f 100644 --- a/src/backends.rs +++ b/src/backends.rs @@ -26,7 +26,10 @@ cfg_if! { } else if #[cfg(getrandom_backend = "efi_rng")] { mod efi_rng; pub use efi_rng::*; - } else if #[cfg(getrandom_backend = "windows_legacy")] { + } else if #[cfg(any( + getrandom_backend = "windows_legacy", + target_vendor = "win7", + ))] { mod windows_legacy; pub use windows_legacy::*; } else if #[cfg(getrandom_backend = "unsupported")] { @@ -159,9 +162,6 @@ cfg_if! { } else if #[cfg(target_os = "solid_asp3")] { mod solid; pub use solid::*; - } else if #[cfg(all(windows, target_vendor = "win7"))] { - mod windows_legacy; - pub use windows_legacy::*; } else if #[cfg(windows)] { mod windows; pub use windows::*; diff --git a/src/backends/apple_other.rs b/src/backends/apple_other.rs index c7b51c0e0..9a2417747 100644 --- a/src/backends/apple_other.rs +++ b/src/backends/apple_other.rs @@ -14,8 +14,3 @@ pub fn fill_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { Err(Error::IOS_RANDOM_GEN) } } - -impl Error { - /// Call to `CCRandomGenerateBytes` failed. - pub(crate) const IOS_RANDOM_GEN: Error = Self::new_internal(10); -} diff --git a/src/backends/rdrand.rs b/src/backends/rdrand.rs index 6e615eb89..7c1ab6b94 100644 --- a/src/backends/rdrand.rs +++ b/src/backends/rdrand.rs @@ -192,10 +192,3 @@ pub fn fill_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { // SAFETY: After this point, we know rdrand is supported. unsafe { rdrand_exact(dest) }.ok_or(Error::FAILED_RDRAND) } - -impl Error { - /// RDRAND instruction failed due to a hardware issue. - pub(crate) const FAILED_RDRAND: Error = Self::new_internal(10); - /// RDRAND instruction unsupported on this target. - pub(crate) const NO_RDRAND: Error = Self::new_internal(11); -} diff --git a/src/backends/rndr.rs b/src/backends/rndr.rs index 50849d506..bb8351e30 100644 --- a/src/backends/rndr.rs +++ b/src/backends/rndr.rs @@ -139,10 +139,3 @@ pub fn fill_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { // SAFETY: after this point, we know the `rand` target feature is enabled unsafe { rndr_fill(dest).ok_or(Error::RNDR_FAILURE) } } - -impl Error { - /// RNDR register read failed due to a hardware issue. - pub(crate) const RNDR_FAILURE: Error = Self::new_internal(10); - /// RNDR register is not supported on this target. - pub(crate) const RNDR_NOT_AVAILABLE: Error = Self::new_internal(11); -} diff --git a/src/backends/vxworks.rs b/src/backends/vxworks.rs index aa07246ba..c5d8b41a2 100644 --- a/src/backends/vxworks.rs +++ b/src/backends/vxworks.rs @@ -49,8 +49,3 @@ pub fn fill_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { } Ok(()) } - -impl Error { - /// On VxWorks, call to `randSecure` failed (random number generator is not yet initialized). - pub(crate) const VXWORKS_RAND_SECURE: Error = Self::new_internal(10); -} diff --git a/src/backends/wasm_js.rs b/src/backends/wasm_js.rs index ee22b63f9..814738cf5 100644 --- a/src/backends/wasm_js.rs +++ b/src/backends/wasm_js.rs @@ -65,8 +65,3 @@ unsafe extern "C" { #[wasm_bindgen(js_namespace = ["globalThis", "crypto"], js_name = getRandomValues, catch)] fn get_random_values(buf: &js_sys::Uint8Array) -> Result<(), JsValue>; } - -impl Error { - /// The environment does not support the Web Crypto API. - pub(crate) const WEB_CRYPTO: Error = Self::new_internal(10); -} diff --git a/src/backends/windows_legacy.rs b/src/backends/windows_legacy.rs index 0e917015d..a50f8ed96 100644 --- a/src/backends/windows_legacy.rs +++ b/src/backends/windows_legacy.rs @@ -41,8 +41,3 @@ pub fn fill_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { } Ok(()) } - -impl Error { - /// Call to Windows [`RtlGenRandom`](https://docs.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom) failed. - pub(crate) const WINDOWS_RTL_GEN_RANDOM: Error = Self::new_internal(10); -} diff --git a/src/error.rs b/src/error.rs index 73a827431..cd64b6608 100644 --- a/src/error.rs +++ b/src/error.rs @@ -168,6 +168,7 @@ impl Error { Error::UNSUPPORTED => "getrandom: this target is not supported", Error::ERRNO_NOT_POSITIVE => "errno: did not return a positive value", Error::UNEXPECTED => "unexpected situation", + #[cfg(any( target_os = "ios", target_os = "visionos", @@ -175,7 +176,12 @@ impl Error { target_os = "tvos", ))] Error::IOS_RANDOM_GEN => "SecRandomCopyBytes: iOS Security framework failure", - #[cfg(all(windows, target_vendor = "win7"))] + #[cfg(target_os = "vxworks")] + Error::VXWORKS_RAND_SECURE => "randSecure: VxWorks RNG module is not initialized", + #[cfg(any( + getrandom_backend = "windows_legacy", + target_vendor = "win7", + ))] Error::WINDOWS_RTL_GEN_RANDOM => "RtlGenRandom: Windows system function failure", #[cfg(all( feature = "wasm_js", @@ -183,8 +189,6 @@ impl Error { any(target_os = "unknown", target_os = "none") ))] Error::WEB_CRYPTO => "Web Crypto API is unavailable", - #[cfg(target_os = "vxworks")] - Error::VXWORKS_RAND_SECURE => "randSecure: VxWorks RNG module is not initialized", #[cfg(any( getrandom_backend = "rdrand", @@ -207,6 +211,60 @@ impl Error { } } +// Backend-specific custom errors. +// The errors must mirror code in `Error::internal_desc`. +impl Error { + /// Call to `CCRandomGenerateBytes` failed. + #[cfg(any( + target_os = "ios", + target_os = "visionos", + target_os = "watchos", + target_os = "tvos", + ))] + pub(crate) const IOS_RANDOM_GEN: Error = Self::new_internal(10); + + /// Call to VxWorks `randSecure` failed (random number generator is not yet initialized). + #[cfg(target_os = "vxworks")] + pub(crate) const VXWORKS_RAND_SECURE: Error = Self::new_internal(11); + + /// Call to Windows `RtlGenRandom` failed. + #[cfg(any( + getrandom_backend = "windows_legacy", + target_vendor = "win7", + ))] + pub(crate) const WINDOWS_RTL_GEN_RANDOM: Error = Self::new_internal(12); + + /// The environment does not support the Web Crypto API. + #[cfg(all( + feature = "wasm_js", + target_arch = "wasm32", + any(target_os = "unknown", target_os = "none") + ))] + pub(crate) const WEB_CRYPTO: Error = Self::new_internal(13); + + /// RDRAND instruction failed due to a hardware issue. + #[cfg(any( + getrandom_backend = "rdrand", + all(target_arch = "x86_64", target_env = "sgx") + ))] + pub(crate) const FAILED_RDRAND: Error = Self::new_internal(14); + + /// RDRAND instruction unsupported on this target. + #[cfg(any( + getrandom_backend = "rdrand", + all(target_arch = "x86_64", target_env = "sgx") + ))] + pub(crate) const NO_RDRAND: Error = Self::new_internal(15); + + /// RNDR register read failed due to a hardware issue. + #[cfg(getrandom_backend = "rndr")] + pub(crate) const RNDR_FAILURE: Error = Self::new_internal(16); + + /// RNDR register is not supported on this target. + #[cfg(getrandom_backend = "rndr")] + pub(crate) const RNDR_NOT_AVAILABLE: Error = Self::new_internal(17); +} + impl core::error::Error for Error {} impl fmt::Debug for Error { From a7c4e2e77702cc98fc4dc1f49d50b72113e20888 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: Fri, 6 Nov 2026 05:41:28 +0300 Subject: [PATCH 2/3] use CI config from #875 --- .github/workflows/build.yml | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e301510b8..38f3e799d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -271,15 +271,19 @@ jobs: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="unsupported" run: cargo build --target riscv32i-unknown-none-elf - unsupported: - name: Runtime error + wasm_override: + name: Web WASM override (${{ matrix.backend }}) runs-on: ubuntu-24.04 + strategy: + matrix: + backend: [unsupported, custom] + env: + RUSTFLAGS: -Dwarnings --cfg getrandom_backend="${{ matrix.backend }}" steps: - uses: actions/checkout@v7 - uses: dtolnay/rust-toolchain@stable with: targets: wasm32-unknown-unknown - uses: Swatinem/rust-cache@v2 - - env: - RUSTFLAGS: -Dwarnings --cfg getrandom_backend="unsupported" - run: cargo build --target wasm32-unknown-unknown + - run: cargo build --target wasm32-unknown-unknown + - run: cargo build --target wasm32-unknown-unknown --features wasm_js From 218da2a47c0be784e5c843e69ceb3fda453d2751 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: Fri, 6 Nov 2026 05:42:00 +0300 Subject: [PATCH 3/3] fmt --- src/error.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/error.rs b/src/error.rs index cd64b6608..cc40dc6ca 100644 --- a/src/error.rs +++ b/src/error.rs @@ -178,10 +178,7 @@ impl Error { Error::IOS_RANDOM_GEN => "SecRandomCopyBytes: iOS Security framework failure", #[cfg(target_os = "vxworks")] Error::VXWORKS_RAND_SECURE => "randSecure: VxWorks RNG module is not initialized", - #[cfg(any( - getrandom_backend = "windows_legacy", - target_vendor = "win7", - ))] + #[cfg(any(getrandom_backend = "windows_legacy", target_vendor = "win7",))] Error::WINDOWS_RTL_GEN_RANDOM => "RtlGenRandom: Windows system function failure", #[cfg(all( feature = "wasm_js", @@ -228,10 +225,7 @@ impl Error { pub(crate) const VXWORKS_RAND_SECURE: Error = Self::new_internal(11); /// Call to Windows `RtlGenRandom` failed. - #[cfg(any( - getrandom_backend = "windows_legacy", - target_vendor = "win7", - ))] + #[cfg(any(getrandom_backend = "windows_legacy", target_vendor = "win7",))] pub(crate) const WINDOWS_RTL_GEN_RANDOM: Error = Self::new_internal(12); /// The environment does not support the Web Crypto API.