getrandom/backends/
linux_android_with_fallback.rs1use super::use_file;
3use crate::Error;
4use core::{
5 ffi::c_void,
6 mem::{transmute, MaybeUninit},
7 ptr::NonNull,
8 sync::atomic::{AtomicPtr, Ordering},
9};
10use use_file::util_libc;
11
12pub use crate::util::{inner_u32, inner_u64};
13
14type GetRandomFn = unsafe extern "C" fn(*mut c_void, libc::size_t, libc::c_uint) -> libc::ssize_t;
15
16const NOT_AVAILABLE: NonNull<c_void> = unsafe { NonNull::new_unchecked(usize::MAX as *mut c_void) };
19
20static GETRANDOM_FN: AtomicPtr<c_void> = AtomicPtr::new(core::ptr::null_mut());
21
22#[cold]
23#[inline(never)]
24fn init() -> NonNull<c_void> {
25 #[cfg(not(target_env = "musl"))]
27 let raw_ptr = {
28 static NAME: &[u8] = b"getrandom\0";
29 let name_ptr = NAME.as_ptr().cast::<libc::c_char>();
30 unsafe { libc::dlsym(libc::RTLD_DEFAULT, name_ptr) }
31 };
32 #[cfg(target_env = "musl")]
33 let raw_ptr = {
34 let fptr: GetRandomFn = libc::getrandom;
35 unsafe { transmute::<GetRandomFn, *mut c_void>(fptr) }
36 };
37
38 let res_ptr = match NonNull::new(raw_ptr) {
39 Some(fptr) => {
40 let getrandom_fn = unsafe { transmute::<NonNull<c_void>, GetRandomFn>(fptr) };
41 let dangling_ptr = NonNull::dangling().as_ptr();
42 let res = unsafe { getrandom_fn(dangling_ptr, 0, 0) };
44 if cfg!(getrandom_test_linux_fallback) {
45 NOT_AVAILABLE
46 } else if res.is_negative() {
47 match util_libc::last_os_error().raw_os_error() {
48 Some(libc::ENOSYS) => NOT_AVAILABLE, #[cfg(target_os = "linux")]
53 Some(libc::EPERM) => NOT_AVAILABLE, _ => fptr,
55 }
56 } else {
57 fptr
58 }
59 }
60 None => NOT_AVAILABLE,
61 };
62
63 #[cfg(getrandom_test_linux_without_fallback)]
64 if res_ptr == NOT_AVAILABLE {
65 panic!("Fallback is triggered with enabled `getrandom_test_linux_without_fallback`")
66 }
67
68 GETRANDOM_FN.store(res_ptr.as_ptr(), Ordering::Release);
69 res_ptr
70}
71
72#[inline(never)]
74fn use_file_fallback(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
75 use_file::fill_inner(dest)
76}
77
78#[inline]
79pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
80 let raw_ptr = GETRANDOM_FN.load(Ordering::Acquire);
87 let fptr = match NonNull::new(raw_ptr) {
88 Some(p) => p,
89 None => init(),
90 };
91
92 if fptr == NOT_AVAILABLE {
93 use_file_fallback(dest)
94 } else {
95 let getrandom_fn = unsafe { transmute::<NonNull<c_void>, GetRandomFn>(fptr) };
97 util_libc::sys_fill_exact(dest, |buf| unsafe {
98 getrandom_fn(buf.as_mut_ptr().cast(), buf.len(), 0)
99 })
100 }
101}