Skip to content
Merged
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
28 changes: 22 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
3 changes: 1 addition & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ jobs:
matrix:
toolchain: [
stable-x86_64-gnu,
stable-i686-gnu,
stable-i686-msvc,
stable-x86_64-msvc,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This drops our 32-bit Windows support, and we have special handling for 32-bit Windows in

#[cfg_attr(
target_arch = "x86",
link(
name = "bcryptprimitives",
kind = "raw-dylib",
import_name_type = "undecorated"
)
)]

Do we want to find some way to keep these working somehow? We can also figure out how to add them back in a follow-up PR if that's easier.

@newpavlov newpavlov Nov 16, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I don't care about 32 bit Windows targets, so I would be fine with not testing them in CI.

Ideally, the CI failures should be fixed in a separate PR (either by removing the targets like I did here, or by fixing them properly), but I just wanted for CI to pass.

]
steps:
- uses: actions/checkout@v7
Expand Down
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
5 changes: 1 addition & 4 deletions src/backends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")] {
Expand All @@ -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")] {
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);
}
5 changes: 0 additions & 5 deletions src/backends/efi_rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,3 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> 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);
}
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);
}
82 changes: 75 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) })
Expand All @@ -168,45 +168,113 @@ 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",
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",

#[cfg(getrandom_backend = "rndr")]
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 {
Expand Down
Loading