You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

getrandom

Package Overview
Dependencies
Maintainers
0
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

getrandom - cargo Package Compare versions

Comparing version
0.4.0-rc.1
to
0.4.0
+19
src/backends/extern_impl.rs
//! An implementation which calls out to an externally defined function.
use crate::Error;
use core::mem::MaybeUninit;
/// Declares this function as an external implementation of [`fill_uninit`](crate::fill_uninit).
#[eii(fill_uninit)]
pub(crate) fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>;
/// Declares this function as an external implementation of [`u32`](crate::u32).
#[eii(u32)]
pub(crate) fn inner_u32() -> Result<u32, crate::Error> {
crate::util::inner_u32()
}
/// Declares this function as an external implementation of [`u64`](crate::u64).
#[eii(u64)]
pub(crate) fn inner_u64() -> Result<u64, crate::Error> {
crate::util::inner_u64()
}
+1
-1
{
"git": {
"sha1": "b6a28dcbcfc928bc45613f5f6807b93378f2f1be"
"sha1": "35fd5af2ed77b4a103b5b9a5248f0b8cd56c7aaa"
},
"path_in_vcs": ""
}

@@ -0,1 +1,2 @@

//! Basic benchmarks
#![feature(test, maybe_uninit_uninit_array_transpose)]

@@ -26,3 +27,3 @@ extern crate test;

#[bench]
pub fn bench_u32(b: &mut test::Bencher) {
fn bench_u32(b: &mut test::Bencher) {
#[inline(never)]

@@ -35,4 +36,5 @@ fn inner() -> u32 {

}
#[bench]
pub fn bench_u32_via_fill(b: &mut test::Bencher) {
fn bench_u32_via_fill(b: &mut test::Bencher) {
#[inline(never)]

@@ -51,3 +53,3 @@ fn inner() -> u32 {

#[bench]
pub fn bench_u64(b: &mut test::Bencher) {
fn bench_u64(b: &mut test::Bencher) {
#[inline(never)]

@@ -62,3 +64,3 @@ fn inner() -> u64 {

#[bench]
pub fn bench_u64_via_fill(b: &mut test::Bencher) {
fn bench_u64_via_fill(b: &mut test::Bencher) {
#[inline(never)]

@@ -85,5 +87,5 @@ fn inner() -> u64 {

( $name:ident, $size:expr ) => {
pub mod $name {
mod $name {
#[bench]
pub fn bench_fill(b: &mut test::Bencher) {
fn bench_fill(b: &mut test::Bencher) {
#[inline(never)]

@@ -98,3 +100,3 @@ fn inner() {

#[bench]
pub fn bench_fill_uninit(b: &mut test::Bencher) {
fn bench_fill_uninit(b: &mut test::Bencher) {
#[inline(never)]

@@ -101,0 +103,0 @@ fn inner() {

@@ -0,1 +1,3 @@

//! Build script for memory sanitization support
fn main() {

@@ -2,0 +4,0 @@ // Automatically detect cfg(sanitize = "memory") even if cfg(sanitize) isn't

+106
-61

@@ -48,5 +48,5 @@ # This file is automatically @generated by Cargo.

name = "cc"
version = "1.2.53"
version = "1.2.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "755d2fce177175ffca841e9a06afdb2c4ab0f593d53b4dee48147dfaade85932"
checksum = "6354c81bbfd62d9cfa9cb3c773c2b7b2a3a482d569de977fd0e961f6e7c00583"
dependencies = [

@@ -82,4 +82,29 @@ "find-msvc-tools",

[[package]]
name = "futures-core"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
[[package]]
name = "futures-task"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988"
[[package]]
name = "futures-util"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
dependencies = [
"futures-core",
"futures-task",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]]
name = "getrandom"
version = "0.4.0-rc.1"
version = "0.4.0"
dependencies = [

@@ -144,5 +169,5 @@ "cfg-if",

name = "js-sys"
version = "0.3.83"
version = "0.3.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "464a3709c7f55f1f721e5389aa6ea4e3bc6aba669353300af094b29ffbdde1d8"
checksum = "8c942ebf8e95485ca0d52d97da7c5a2c387d0e7f0ba4c35e93bfcaee045955b3"
dependencies = [

@@ -167,5 +192,5 @@ "once_cell",

name = "libm"
version = "0.2.15"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de"
checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"

@@ -226,2 +251,14 @@ [[package]]

[[package]]
name = "pin-project-lite"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b"
[[package]]
name = "pin-utils"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "prettyplease"

@@ -238,5 +275,5 @@ version = "0.2.37"

name = "proc-macro2"
version = "1.0.105"
version = "1.0.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "535d180e0ecab6268a3e718bb9fd44db66bbbc256257165fc699dadf70d16fe7"
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
dependencies = [

@@ -248,5 +285,5 @@ "unicode-ident",

name = "quote"
version = "1.0.43"
version = "1.0.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc74d9a594b72ae6656596548f56f667211f8a97b3d4c3d467150794690dc40a"
checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4"
dependencies = [

@@ -264,5 +301,5 @@ "proc-macro2",

name = "rand_core"
version = "0.10.0-rc-6"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70765ff7112b0fb2d272d24d9a2f907fc206211304328fe58b2db15a5649ef28"
checksum = "0c8d0fd677905edcbeedbf2edb6494d676f0e98d54d5cf9bda0b061cb8fb8aba"

@@ -340,2 +377,8 @@ [[package]]

[[package]]
name = "slab"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a2ae44ef20feb57a68b23d846850f861394c2e02dc425a50098ae8c90267589"
[[package]]
name = "syn"

@@ -375,7 +418,7 @@ version = "2.0.114"

name = "wasip2"
version = "1.0.1+wasi-0.2.4"
version = "1.0.2+wasi-0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0562428422c63773dad2c345a1882263bbf4d65cf3f42e90921f787ef5ad58e7"
checksum = "9517f9239f02c069db75e65f174b3da828fe5f5b945c4dd26bd25d89c03ebcf5"
dependencies = [
"wit-bindgen 0.46.0",
"wit-bindgen",
]

@@ -385,7 +428,7 @@

name = "wasip3"
version = "0.3.1+wasi-0.3.0-rc-2025-09-16"
version = "0.4.0+wasi-0.3.0-rc-2026-01-06"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87ba4be47b1d11244670d11857eee0758a8f2c39aea64d80b78c1ce29b4642cd"
checksum = "5428f8bf88ea5ddc08faddef2ac4a67e390b88186c703ce6dbd955e1c145aca5"
dependencies = [
"wit-bindgen 0.48.1",
"wit-bindgen",
]

@@ -395,5 +438,5 @@

name = "wasm-bindgen"
version = "0.2.106"
version = "0.2.108"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d759f433fa64a2d763d1340820e46e111a7a5ab75f993d1852d70b03dbb80fd"
checksum = "64024a30ec1e37399cf85a7ffefebdb72205ca1c972291c51512360d90bd8566"
dependencies = [

@@ -409,7 +452,8 @@ "cfg-if",

name = "wasm-bindgen-futures"
version = "0.4.56"
version = "0.4.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "836d9622d604feee9e5de25ac10e3ea5f2d65b41eac0d9ce72eb5deae707ce7c"
checksum = "70a6e77fd0ae8029c9ea0063f87c46fde723e7d887703d74ad2616d792e51e6f"
dependencies = [
"cfg-if",
"futures-util",
"js-sys",

@@ -423,5 +467,5 @@ "once_cell",

name = "wasm-bindgen-macro"
version = "0.2.106"
version = "0.2.108"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48cb0d2638f8baedbc542ed444afc0644a29166f1595371af4fecf8ce1e7eeb3"
checksum = "008b239d9c740232e71bd39e8ef6429d27097518b6b30bdf9086833bd5b6d608"
dependencies = [

@@ -434,5 +478,5 @@ "quote",

name = "wasm-bindgen-macro-support"
version = "0.2.106"
version = "0.2.108"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cefb59d5cd5f92d9dcf80e4683949f15ca4b511f4ac0a6e14d4e1ac60c6ecd40"
checksum = "5256bae2d58f54820e6490f9839c49780dff84c65aeab9e772f15d5f0e913a55"
dependencies = [

@@ -448,5 +492,5 @@ "bumpalo",

name = "wasm-bindgen-shared"
version = "0.2.106"
version = "0.2.108"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbc538057e648b67f72a982e708d485b2efa771e1ac05fec311f9f63e5800db4"
checksum = "1f01b580c9ac74c8d8f0c0e4afb04eeef2acf145458e52c03845ee9cd23e3d12"
dependencies = [

@@ -458,5 +502,5 @@ "unicode-ident",

name = "wasm-bindgen-test"
version = "0.3.56"
version = "0.3.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25e90e66d265d3a1efc0e72a54809ab90b9c0c515915c67cdf658689d2c22c6c"
checksum = "45649196a53b0b7a15101d845d44d2dda7374fc1b5b5e2bbf58b7577ff4b346d"
dependencies = [

@@ -476,2 +520,3 @@ "async-trait",

"wasm-bindgen-test-macro",
"wasm-bindgen-test-shared",
]

@@ -481,5 +526,5 @@

name = "wasm-bindgen-test-macro"
version = "0.3.56"
version = "0.3.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7150335716dce6028bead2b848e72f47b45e7b9422f64cccdc23bedca89affc1"
checksum = "f579cdd0123ac74b94e1a4a72bd963cf30ebac343f2df347da0b8df24cdebed2"
dependencies = [

@@ -492,6 +537,12 @@ "proc-macro2",

[[package]]
name = "wasm-bindgen-test-shared"
version = "0.2.108"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8145dd1593bf0fb137dbfa85b8be79ec560a447298955877804640e40c2d6ea"
[[package]]
name = "wasm-encoder"
version = "0.241.2"
version = "0.244.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e01164c9dda68301e34fdae536c23ed6fe90ce6d97213ccc171eebbd3d02d6b8"
checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319"
dependencies = [

@@ -504,5 +555,5 @@ "leb128fmt",

name = "wasm-metadata"
version = "0.241.2"
version = "0.244.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "876fe286f2fa416386deedebe8407e6f19e0b5aeaef3d03161e77a15fa80f167"
checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909"
dependencies = [

@@ -517,5 +568,5 @@ "anyhow",

name = "wasmparser"
version = "0.241.2"
version = "0.244.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46d90019b1afd4b808c263e428de644f3003691f243387d30d673211ee0cb8e8"
checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe"
dependencies = [

@@ -530,5 +581,5 @@ "bitflags",

name = "web-sys"
version = "0.3.83"
version = "0.3.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b32828d774c412041098d182a8b38b16ea816958e07cf40eec2bc080ae137ac"
checksum = "312e32e551d92129218ea9a2452120f4aabc03529ef03e4d0d82fb2780608598"
dependencies = [

@@ -565,11 +616,5 @@ "js-sys",

name = "wit-bindgen"
version = "0.46.0"
version = "0.51.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59"
[[package]]
name = "wit-bindgen"
version = "0.48.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f8c2adb5f74ac9395bc3121c99a1254bf9310482c27b13f97167aedb5887138"
checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5"
dependencies = [

@@ -581,5 +626,5 @@ "wit-bindgen-rust-macro",

name = "wit-bindgen-core"
version = "0.48.1"
version = "0.51.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b881a098cae03686d7a0587f8f306f8a58102ad8da8b5599100fbe0e7f5800b"
checksum = "ea61de684c3ea68cb082b7a88508a8b27fcc8b797d738bfc99a82facf1d752dc"
dependencies = [

@@ -593,5 +638,5 @@ "anyhow",

name = "wit-bindgen-rust"
version = "0.48.1"
version = "0.51.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69667efa439a453e1d50dac939c6cab6d2c3ac724a9d232b6631dad2472a5b70"
checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21"
dependencies = [

@@ -610,5 +655,5 @@ "anyhow",

name = "wit-bindgen-rust-macro"
version = "0.48.1"
version = "0.51.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eae2e22cceb5d105d52326c07e3e67603a861cc7add70fc467f7cc7ec5265017"
checksum = "0c0f9bfd77e6a48eccf51359e3ae77140a7f50b1e2ebfe62422d8afdaffab17a"
dependencies = [

@@ -626,5 +671,5 @@ "anyhow",

name = "wit-component"
version = "0.241.2"
version = "0.244.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0c57df25e7ee612d946d3b7646c1ddb2310f8280aa2c17e543b66e0812241"
checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2"
dependencies = [

@@ -646,5 +691,5 @@ "anyhow",

name = "wit-parser"
version = "0.241.2"
version = "0.244.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ef1c6ad67f35c831abd4039c02894de97034100899614d1c44e2268ad01c91"
checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736"
dependencies = [

@@ -665,4 +710,4 @@ "anyhow",

name = "zmij"
version = "1.0.15"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94f63c051f4fe3c1509da62131a678643c5b6fbdc9273b2b79d4378ebda003d2"
checksum = "02aae0f83f69aafc94776e879363e9771d7ecbffe2c7fbb6c14c5e00dfe88439"

@@ -16,3 +16,3 @@ # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO

name = "getrandom"
version = "0.4.0-rc.1"
version = "0.4.0"
authors = ["The Rand Project Developers"]

@@ -41,2 +41,3 @@ build = "build.rs"

]
rustdoc-args = ['--cfg getrandom_backend="extern_impl"']

@@ -81,3 +82,3 @@ [package.metadata.cross.target.x86_64-unknown-netbsd]

[dependencies.rand_core]
version = "0.10.0-rc-6"
version = "0.10.0"
optional = true

@@ -107,4 +108,3 @@

[target.'cfg(all(target_arch = "wasm32", target_os = "wasi", target_env = "p3"))'.dependencies.wasip3]
version = "0.3"
default-features = false
version = "0.4"

@@ -143,2 +143,21 @@ [target.'cfg(all(target_os = "uefi", getrandom_backend = "efi_rng"))'.dependencies.r-efi]

[lints.clippy]
cast_lossless = "warn"
cast_possible_truncation = "warn"
cast_possible_wrap = "warn"
cast_precision_loss = "warn"
cast_ptr_alignment = "warn"
cast_sign_loss = "warn"
char_lit_as_u8 = "warn"
checked_conversions = "warn"
fn_to_numeric_cast = "warn"
fn_to_numeric_cast_with_truncation = "warn"
ptr_as_ptr = "warn"
unnecessary_cast = "warn"
useless_conversion = "warn"
[lints.rust]
missing_docs = "warn"
unused_lifetimes = "warn"
[lints.rust.unexpected_cfgs]

@@ -148,3 +167,3 @@ level = "warn"

check-cfg = [
'cfg(getrandom_backend, values("custom", "efi_rng", "rdrand", "rndr", "linux_getrandom", "linux_raw", "windows_legacy", "unsupported"))',
'cfg(getrandom_backend, values("custom", "efi_rng", "rdrand", "rndr", "linux_getrandom", "linux_raw", "windows_legacy", "unsupported", "extern_impl"))',
"cfg(getrandom_msan)",

@@ -155,2 +174,3 @@ "cfg(getrandom_test_linux_fallback)",

'cfg(target_os, values("cygwin"))',
'cfg(target_os, values("motor"))',
]

@@ -7,3 +7,3 @@ # Changelog

## [Unreleased]
## [0.4.0] - 2026-02-02

@@ -14,2 +14,4 @@ ### Added

- WASIp3 support [#779]
- `extern_impl` opt-in backend [#786] [#794]
- Motor OS support [#797]

@@ -23,2 +25,4 @@ ### Changed

[#779]: https://github.com/rust-random/getrandom/pull/779
[#786]: https://github.com/rust-random/getrandom/pull/786
[#794]: https://github.com/rust-random/getrandom/pull/794

@@ -25,0 +29,0 @@ ## [0.3.4] - 2025-10-14

+46
-11

@@ -46,3 +46,3 @@ # getrandom: system's random number generator

| Linux, Android | `*‑linux‑*` | [`getrandom`][1] system call if available, otherwise [`/dev/urandom`][2] after successfully polling `/dev/random`
| Windows 10+ | `*‑windows‑*` | [`ProcessPrng`] on Rust 1.78+, [`RtlGenRandom`] otherwise
| Windows 10+ | `*‑windows‑*` | [`ProcessPrng`]
| Windows 7, 8 | `*-win7‑windows‑*` | [`RtlGenRandom`]

@@ -74,2 +74,3 @@ | macOS | `*‑apple‑darwin` | [`getentropy`][3]

| Cygwin | `*-cygwin` | [`getrandom`][20] (based on [`RtlGenRandom`])
| Motor OS | `x86_64-unknown-motor` | [`RDRAND`]

@@ -104,12 +105,13 @@ Pull Requests that add support for new targets to `getrandom` are always welcome.

| Backend name | Target | Target Triple | Implementation
| ----------------- | -------------------- | ------------------------ | --------------
| `linux_getrandom` | Linux, Android | `*‑linux‑*` | [`getrandom`][1] system call (without `/dev/urandom` fallback). Bumps minimum supported Linux kernel version to 3.17 and Android API level to 23 (Marshmallow).
| `linux_raw` | Linux, Android | `*‑linux‑*` | Same as `linux_getrandom`, but uses raw `asm!`-based syscalls instead of `libc`.
| `rdrand` | x86, x86-64 | `x86_64-*`, `i686-*` | [`RDRAND`] instruction
| `rndr` | AArch64 | `aarch64-*` | [`RNDR`] register
| `efi_rng` | UEFI | `*-unknown‑uefi` | [`EFI_RNG_PROTOCOL`] with `EFI_RNG_ALGORITHM_RAW` (requires `std` and Nightly compiler)
| `windows_legacy` | Windows | `*-windows-*` | [`RtlGenRandom`]
| `custom` | All targets | `*` | User-provided custom implementation (see [custom backend])
| `unsupported` | All targets | `*` | Always returns `Err(Error::UNSUPPORTED)` (see [unsupported backend])
| Backend name | Target | Target Triple | Implementation
| ------------------- | -------------------- | ------------------------ | --------------
| `linux_getrandom` | Linux, Android | `*‑linux‑*` | [`getrandom`][1] system call (without `/dev/urandom` fallback). Bumps minimum supported Linux kernel version to 3.17 and Android API level to 23 (Marshmallow).
| `linux_raw` | Linux, Android | `*‑linux‑*` | Same as `linux_getrandom`, but uses raw `asm!`-based syscalls instead of `libc`.
| `rdrand` | x86, x86-64 | `x86_64-*`, `i686-*` | [`RDRAND`] instruction
| `rndr` | AArch64 | `aarch64-*` | [`RNDR`] register
| `efi_rng` | UEFI | `*-unknown‑uefi` | [`EFI_RNG_PROTOCOL`] with `EFI_RNG_ALGORITHM_RAW` (requires `std` and Nightly compiler)
| `windows_legacy` | Windows | `*-windows-*` | [`RtlGenRandom`]
| `custom` | All targets | `*` | User-provided custom implementation (see [custom backend])
| `unsupported` | All targets | `*` | Always returns `Err(Error::UNSUPPORTED)` (see [unsupported backend])
| `extern_impl` | All targets | `*` | Externally-provided custom implementation (see [externally implemented interface])

@@ -207,2 +209,34 @@ Opt-in backends can be enabled using the `getrandom_backend` configuration flag.

### Externally Implemented Interface
Using the nightly-only feature [`extern_item_impls`] it is possible to provide
a custom backend for `getrandom`, even to override an existing first-party implementation.
First, enable the `extern_impl` opt-in backend to allow usage of this nightly feature.
Then, you may provide implementations for `fill_uninit`, `u32`, and/or `u64`
with an attribute macro from the `implementation` module.
[`extern_item_impls`]: https://github.com/rust-lang/rust/issues/125418
```rust
use core::mem::MaybeUninit;
#[cfg(getrandom_backend = "extern_impl")]
#[getrandom::implementation::fill_uninit]
fn my_fill_uninit_implementation(
dest: &mut [MaybeUninit<u8>]
) -> Result<(), getrandom::Error> {
// ...
Ok(())
}
```
For further details on what a suitable implementation for `fill_uninit` may look
like, see [custom backend].
`getrandom` will provide a default implementation for `u32` and `u64`, but does
not currently provide a default for `fill_uninit`, even if one is normally
available for the current target. If no implementation is available,
a compilation error will be raised with instructions for how to provide
an implementation.
### Unsupported backend

@@ -380,2 +414,3 @@

[opt-in]: #opt-in-backends
[externally implemented interface]: #externally-implemented-interface

@@ -382,0 +417,0 @@ [//]: # (licenses)

@@ -35,2 +35,5 @@ //! System-specific implementations.

pub use unsupported::*;
} else if #[cfg(getrandom_backend = "extern_impl")] {
pub(crate) mod extern_impl;
pub use extern_impl::*;
} else if #[cfg(all(target_os = "linux", target_env = ""))] {

@@ -150,2 +153,5 @@ mod linux_raw;

pub use hermit::*;
} else if #[cfg(all(target_arch = "x86_64", target_os = "motor"))] {
mod rdrand;
pub use rdrand::*;
} else if #[cfg(target_os = "vxworks")] {

@@ -152,0 +158,0 @@ mod vxworks;

@@ -11,20 +11,5 @@ // Overwrite links to crate items with intra-crate links

#![doc = include_str!("../README.md")]
#![warn(rust_2018_idioms, unused_lifetimes, missing_docs)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(getrandom_backend = "efi_rng", feature(uefi_std))]
#![deny(
clippy::cast_lossless,
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_precision_loss,
clippy::cast_ptr_alignment,
clippy::cast_sign_loss,
clippy::char_lit_as_u8,
clippy::checked_conversions,
clippy::fn_to_numeric_cast,
clippy::fn_to_numeric_cast_with_truncation,
clippy::ptr_as_ptr,
clippy::unnecessary_cast,
clippy::useless_conversion
)]
#![cfg_attr(getrandom_backend = "extern_impl", feature(extern_item_impls))]

@@ -54,2 +39,29 @@ #[macro_use]

/// Attribute macros for overwriting the core functionality of this crate.
///
/// This allows `getrandom` to provide a default implementation and a common interface
/// for all crates to use, while giving users a safe way to override that default where required.
///
/// Must be enabled via the `extern_impl` opt-in backend, as this functionality
/// is currently limited to nightly.
///
/// # Examples
///
/// ```rust
/// # use core::mem::MaybeUninit;
/// # #[cfg(getrandom_backend = "extern_impl")]
/// #[getrandom::implementation::fill_uninit]
/// fn my_fill_uninit_implementation(
/// dest: &mut [MaybeUninit<u8>]
/// ) -> Result<(), getrandom::Error> {
/// // ...
/// # let _ = dest;
/// # Err(Error::UNSUPPORTED)
/// }
/// ```
#[cfg(getrandom_backend = "extern_impl")]
pub mod implementation {
pub use crate::backends::extern_impl::{fill_uninit, u32, u64};
}
/// Fill `dest` with random bytes from the system's preferred random number source.

@@ -56,0 +68,0 @@ ///

@@ -32,2 +32,3 @@ use crate::Error;

/// [`Rng`]: rand_core::Rng
/// [`UnwrapErr`]: rand_core::UnwrapErr
#[derive(Clone, Copy, Debug, Default)]

@@ -34,0 +35,0 @@ pub struct SysRng;

@@ -0,1 +1,2 @@

//! Main `getrandom` tests
use core::mem::MaybeUninit;

@@ -209,85 +210,1 @@ use getrandom::{fill, fill_uninit};

}
#[cfg(getrandom_backend = "custom")]
mod custom {
use getrandom::Error;
struct Xoshiro128PlusPlus {
s: [u32; 4],
}
impl Xoshiro128PlusPlus {
fn new(mut seed: u64) -> Self {
const PHI: u64 = 0x9e3779b97f4a7c15;
let mut s = [0u32; 4];
for val in s.iter_mut() {
seed = seed.wrapping_add(PHI);
let mut z = seed;
z = (z ^ (z >> 30)).wrapping_mul(0xbf58476d1ce4e5b9);
z = (z ^ (z >> 27)).wrapping_mul(0x94d049bb133111eb);
z = z ^ (z >> 31);
*val = z as u32;
}
Self { s }
}
fn next_u32(&mut self) -> u32 {
let res = self.s[0]
.wrapping_add(self.s[3])
.rotate_left(7)
.wrapping_add(self.s[0]);
let t = self.s[1] << 9;
self.s[2] ^= self.s[0];
self.s[3] ^= self.s[1];
self.s[1] ^= self.s[2];
self.s[0] ^= self.s[3];
self.s[2] ^= t;
self.s[3] = self.s[3].rotate_left(11);
res
}
}
// This implementation uses current timestamp as a PRNG seed.
//
// WARNING: this custom implementation is for testing purposes ONLY!
#[unsafe(no_mangle)]
unsafe extern "Rust" fn __getrandom_v03_custom(dest: *mut u8, len: usize) -> Result<(), Error> {
use std::time::{SystemTime, UNIX_EPOCH};
assert_ne!(len, 0);
if len == 142 {
return Err(Error::new_custom(142));
}
let dest_u32 = dest.cast::<u32>();
let ts = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
let mut rng = Xoshiro128PlusPlus::new(ts.as_nanos() as u64);
for i in 0..len / 4 {
let val = rng.next_u32();
unsafe { core::ptr::write_unaligned(dest_u32.add(i), val) };
}
if len % 4 != 0 {
let start = 4 * (len / 4);
for i in start..len {
let val = rng.next_u32();
unsafe { core::ptr::write_unaligned(dest.add(i), val as u8) };
}
}
Ok(())
}
// Test that enabling the custom feature indeed uses the custom implementation
#[test]
fn test_custom() {
let mut buf = [0u8; 142];
let res = getrandom::fill(&mut buf);
assert!(res.is_err());
}
}

@@ -0,1 +1,2 @@

//! Tests for `SysRng`
#![cfg(feature = "sys_rng")]

@@ -2,0 +3,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet