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