Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/backends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")] {
Expand Down Expand Up @@ -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::*;
Expand Down
5 changes: 0 additions & 5 deletions src/backends/apple_other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,3 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
Err(Error::IOS_RANDOM_GEN)
}
}

impl Error {
/// Call to `CCRandomGenerateBytes` failed.
pub(crate) const IOS_RANDOM_GEN: Error = Self::new_internal(10);
}
7 changes: 0 additions & 7 deletions src/backends/rdrand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,3 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> 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);
}
7 changes: 0 additions & 7 deletions src/backends/rndr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,3 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> 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);
}
5 changes: 0 additions & 5 deletions src/backends/vxworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,3 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> 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);
}
5 changes: 0 additions & 5 deletions src/backends/wasm_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
5 changes: 0 additions & 5 deletions src/backends/windows_legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,3 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> 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);
}
58 changes: 55 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,23 +168,24 @@ 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",
target_os = "watchos",
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",
target_arch = "wasm32",
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",
Expand All @@ -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 {
Expand Down
Loading