diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e301510b..b89c65d7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -91,6 +91,7 @@ jobs: armv7-sony-vita-newlibeabihf, i686-unknown-hurd-gnu, riscv32imc-esp-espidf, + x86_64-unknown-motor, x86_64-unknown-hermit, x86_64-wrs-vxworks, x86_64-unknown-dragonfly, @@ -187,7 +188,7 @@ jobs: - uses: dtolnay/rust-toolchain@master with: targets: ${{ matrix.target.target }} - toolchain: nightly-2025-09-28 + toolchain: nightly-2026-11-15 components: rust-src - uses: Swatinem/rust-cache@v2 - name: Build @@ -271,15 +272,30 @@ 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 + + wasm64: + name: 64-bit WASM + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v7 + - uses: dtolnay/rust-toolchain@nightly + with: + components: rust-src + - uses: Swatinem/rust-cache@v2 + - run: cargo build -Zbuild-std=core,alloc --target wasm64-unknown-unknown --features wasm_js diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a3f26c40..a41d4f25 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -139,8 +139,7 @@ jobs: matrix: toolchain: [ stable-x86_64-gnu, - stable-i686-gnu, - stable-i686-msvc, + stable-x86_64-msvc, ] steps: - uses: actions/checkout@v7 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..36c06f87 100644 --- a/src/backends.rs +++ b/src/backends.rs @@ -150,7 +150,7 @@ cfg_if! { } else if #[cfg(target_os = "hermit")] { mod hermit; pub use hermit::*; - } else if #[cfg(all(target_arch = "x86_64", target_os = "motor"))] { + } else if #[cfg(all(target_arch = "x86_64", any(target_os = "motor", target_env = "sgx")))] { mod rdrand; pub use rdrand::*; } else if #[cfg(target_os = "vxworks")] { @@ -165,9 +165,6 @@ cfg_if! { } else if #[cfg(windows)] { mod windows; pub use windows::*; - } else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] { - mod rdrand; - pub use rdrand::*; } else if #[cfg(all(target_family = "wasm", any(target_os = "unknown", target_os = "none")))] { cfg_if! { if #[cfg(feature = "wasm_js")] { 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/efi_rng.rs b/src/backends/efi_rng.rs index f667c7fa..d9f20516 100644 --- a/src/backends/efi_rng.rs +++ b/src/backends/efi_rng.rs @@ -115,8 +115,3 @@ pub fn fill_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { Ok(()) } } - -impl Error { - pub(crate) const BOOT_SERVICES_UNAVAILABLE: Error = Self::new_internal(10); - pub(crate) const NO_RNG_HANDLE: Error = Self::new_internal(11); -} 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..dab99fc4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -157,7 +157,7 @@ impl Error { } /// Creates a new instance of an `Error` from a particular internal error code. - pub(crate) const fn new_internal(n: u16) -> Error { + const fn new_internal(n: u16) -> Error { // SAFETY: code > 0 as INTERNAL_START > 0 and adding `n` won't overflow `RawOsError`. let code = Error::INTERNAL_START + (n as RawOsError); Error(unsafe { NonZeroRawOsError::new_unchecked(code) }) @@ -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,25 +176,28 @@ 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", + all(windows, target_vendor = "win7"), + ))] Error::WINDOWS_RTL_GEN_RANDOM => "RtlGenRandom: Windows system function failure", #[cfg(all( feature = "wasm_js", - target_arch = "wasm32", + target_family = "wasm", 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", - all(target_arch = "x86_64", target_env = "sgx") + all(target_arch = "x86_64", any(target_os = "motor", target_env = "sgx")), ))] Error::FAILED_RDRAND => "RDRAND: failed multiple times: CPU issue likely", #[cfg(any( getrandom_backend = "rdrand", - all(target_arch = "x86_64", target_env = "sgx") + all(target_arch = "x86_64", any(target_os = "motor", target_env = "sgx")), ))] Error::NO_RDRAND => "RDRAND: instruction not supported", @@ -201,12 +205,76 @@ impl Error { Error::RNDR_FAILURE => "RNDR: Could not generate a random number", #[cfg(getrandom_backend = "rndr")] Error::RNDR_NOT_AVAILABLE => "RNDR: Register not supported", + + #[cfg(getrandom_backend = "efi_rng")] + Error::BOOT_SERVICES_UNAVAILABLE => "UEFI boot services are not available", + #[cfg(getrandom_backend = "efi_rng")] + Error::NO_RNG_HANDLE => "Failed to retrieve handle for UEFI RNG protocol", _ => return None, }; Some(desc) } } +// 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", + all(windows, 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_family = "wasm", + 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", any(target_os = "motor", 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", any(target_os = "motor", 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); + + #[cfg(getrandom_backend = "efi_rng")] + pub(crate) const BOOT_SERVICES_UNAVAILABLE: Error = Self::new_internal(18); + #[cfg(getrandom_backend = "efi_rng")] + pub(crate) const NO_RNG_HANDLE: Error = Self::new_internal(19); +} + impl core::error::Error for Error {} impl fmt::Debug for Error {