diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e301510b..38f3e799 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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 4beb2a0b..188b1ed3 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 d644ab62..8d8755f3 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 c7b51c0e..9a241774 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 6e615eb8..7c1ab6b9 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 50849d50..bb8351e3 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 aa07246b..c5d8b41a 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 ee22b63f..814738cf 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 0e917015..a50f8ed9 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 73a82743..cc40dc6c 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,9 @@ 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 +186,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 +208,57 @@ 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 {