getrandom/
backends.rs

1//! System-specific implementations.
2//!
3//! This module should provide `fill_inner` with the signature
4//! `fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>`.
5//! The function MUST fully initialize `dest` when `Ok(())` is returned.
6//! The function MUST NOT ever write uninitialized bytes into `dest`,
7//! regardless of what value it returns.
8
9cfg_if! {
10    if #[cfg(getrandom_backend = "custom")] {
11        mod custom;
12        pub use custom::*;
13    } else if #[cfg(getrandom_backend = "linux_getrandom")] {
14        mod getrandom;
15        pub use getrandom::*;
16    } else if #[cfg(getrandom_backend = "linux_raw")] {
17        mod linux_raw;
18        pub use linux_raw::*;
19    } else if #[cfg(getrandom_backend = "rdrand")] {
20        mod rdrand;
21        pub use rdrand::*;
22    } else if #[cfg(getrandom_backend = "rndr")] {
23        mod rndr;
24        pub use rndr::*;
25    } else if #[cfg(getrandom_backend = "efi_rng")] {
26        mod efi_rng;
27        pub use efi_rng::*;
28    } else if #[cfg(all(getrandom_backend = "wasm_js"))] {
29        cfg_if! {
30            if #[cfg(feature = "wasm_js")] {
31                mod wasm_js;
32                pub use wasm_js::*;
33            } else {
34                compile_error!(
35                    "The \"wasm_js\" backend requires the `wasm_js` feature \
36                    for `getrandom`. For more information see: \
37                    https://docs.rs/getrandom/#webassembly-support"
38                );
39            }
40        }
41    } else if #[cfg(all(target_os = "linux", target_env = ""))] {
42        mod linux_raw;
43        pub use linux_raw::*;
44    } else if #[cfg(target_os = "espidf")] {
45        mod esp_idf;
46        pub use esp_idf::*;
47    } else if #[cfg(any(
48        target_os = "haiku",
49        target_os = "redox",
50        target_os = "nto",
51        target_os = "aix",
52    ))] {
53        mod use_file;
54        pub use use_file::*;
55    } else if #[cfg(any(
56        target_os = "macos",
57        target_os = "openbsd",
58        target_os = "vita",
59        target_os = "emscripten",
60    ))] {
61        mod getentropy;
62        pub use getentropy::*;
63    } else if #[cfg(any(
64        // Rust supports Android API level 19 (KitKat) [0] and the next upgrade targets
65        // level 21 (Lollipop) [1], while `getrandom(2)` was added only in
66        // level 23 (Marshmallow). Note that it applies only to the "old" `target_arch`es,
67        // RISC-V Android targets sufficiently new API level, same will apply for potential
68        // new Android `target_arch`es.
69        // [0]: https://blog.rust-lang.org/2023/01/09/android-ndk-update-r25.html
70        // [1]: https://github.com/rust-lang/rust/pull/120593
71        all(
72            target_os = "android",
73            any(
74                target_arch = "aarch64",
75                target_arch = "arm",
76                target_arch = "x86",
77                target_arch = "x86_64",
78            ),
79        ),
80        // Only on these `target_arch`es Rust supports Linux kernel versions (3.2+)
81        // that precede the version (3.17) in which `getrandom(2)` was added:
82        // https://doc.rust-lang.org/stable/rustc/platform-support.html
83        all(
84            target_os = "linux",
85            any(
86                target_arch = "aarch64",
87                target_arch = "arm",
88                target_arch = "powerpc",
89                target_arch = "powerpc64",
90                target_arch = "s390x",
91                target_arch = "x86",
92                target_arch = "x86_64",
93                // Minimum supported Linux kernel version for MUSL targets
94                // is not specified explicitly (as of Rust 1.77) and they
95                // are used in practice to target pre-3.17 kernels.
96                target_env = "musl",
97            ),
98        )
99    ))] {
100        mod use_file;
101        mod linux_android_with_fallback;
102        pub use linux_android_with_fallback::*;
103    } else if #[cfg(any(
104        target_os = "android",
105        target_os = "linux",
106        target_os = "dragonfly",
107        target_os = "freebsd",
108        target_os = "hurd",
109        target_os = "illumos",
110        target_os = "cygwin",
111        // Check for target_arch = "arm" to only include the 3DS. Does not
112        // include the Nintendo Switch (which is target_arch = "aarch64").
113        all(target_os = "horizon", target_arch = "arm"),
114    ))] {
115        mod getrandom;
116        pub use getrandom::*;
117    } else if #[cfg(target_os = "solaris")] {
118        mod solaris;
119        pub use solaris::*;
120    } else if #[cfg(target_os = "netbsd")] {
121        mod netbsd;
122        pub use netbsd::*;
123    } else if #[cfg(target_os = "fuchsia")] {
124        mod fuchsia;
125        pub use fuchsia::*;
126    } else if #[cfg(any(
127        target_os = "ios",
128        target_os = "visionos",
129        target_os = "watchos",
130        target_os = "tvos",
131    ))] {
132        mod apple_other;
133        pub use apple_other::*;
134    } else if #[cfg(all(target_arch = "wasm32", target_os = "wasi"))] {
135        cfg_if! {
136            if #[cfg(target_env = "p1")] {
137                mod wasi_p1;
138                pub use wasi_p1::*;
139            } else if #[cfg(target_env = "p2")] {
140                mod wasi_p2;
141                pub use wasi_p2::*;
142            } else {
143                compile_error!(
144                    "Unknown version of WASI (only previews 1 and 2 are supported) \
145                    or Rust version older than 1.80 was used"
146                );
147            }
148        }
149    } else if #[cfg(target_os = "hermit")] {
150        mod hermit;
151        pub use hermit::*;
152    } else if #[cfg(target_os = "vxworks")] {
153        mod vxworks;
154        pub use vxworks::*;
155    } else if #[cfg(target_os = "solid_asp3")] {
156        mod solid;
157        pub use solid::*;
158    } else if #[cfg(all(windows, any(target_vendor = "win7", getrandom_windows_legacy)))] {
159        mod windows7;
160        pub use windows7::*;
161    } else if #[cfg(windows)] {
162        mod windows;
163        pub use windows::*;
164    } else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] {
165        mod rdrand;
166        pub use rdrand::*;
167    } else if #[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))] {
168        compile_error!(
169            "The wasm32-unknown-unknown targets are not supported by default; \
170            you may need to enable the \"wasm_js\" configuration flag. Note \
171            that enabling the `wasm_js` feature flag alone is insufficient. \
172            For more information see: \
173            https://docs.rs/getrandom/#webassembly-support"
174        );
175    } else {
176        compile_error!("target is not supported. You may need to define \
177                        a custom backend see: \
178                        https://docs.rs/getrandom/#custom-backends");
179    }
180}