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