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