Skip to content

Commit

Permalink
Use NonNull
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Oct 9, 2024
1 parent ec6c1c5 commit 87b49b8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ cfg_if! {
),
)
))] {
mod lazy;
mod util_libc;
mod use_file;
#[path = "linux_android_with_fallback.rs"] mod imp;
Expand Down
65 changes: 33 additions & 32 deletions src/linux_android_with_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,49 @@
use crate::{use_file, util_libc, Error};
use core::{
ffi::c_void,
mem,
mem::MaybeUninit,
ptr,
mem::{self, MaybeUninit},
ptr::{self, NonNull},
sync::atomic::{AtomicPtr, Ordering},
};

type GetRandomFn = unsafe extern "C" fn(*mut c_void, libc::size_t, libc::c_uint) -> libc::ssize_t;

/// Sentinel value which indicates that `libc::getrandom` either not available,
/// or not supported by kernel.
const NOT_AVAILABLE: *mut c_void = usize::MAX as *mut c_void;
const NOT_AVAILABLE: NonNull<c_void> = unsafe { NonNull::new_unchecked(usize::MAX as *mut c_void) };

static GETRANDOM_FN: AtomicPtr<c_void> = AtomicPtr::new(ptr::null_mut());

#[cold]
fn init() -> *mut c_void {
fn init() -> NonNull<c_void> {
static NAME: &[u8] = b"getrandom\0";
let name_ptr = NAME.as_ptr().cast::<libc::c_char>();
let raw_ptr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name_ptr) };
let res_ptr = if raw_ptr.is_null() {
NOT_AVAILABLE
} else {
let fptr = unsafe { mem::transmute::<*mut c_void, GetRandomFn>(raw_ptr) };
// Check that `getrandom` syscall is supported by kernel
let res = unsafe { fptr(ptr::NonNull::dangling().as_ptr(), 0, 0) };
if cfg!(getrandom_test_linux_fallback) {
NOT_AVAILABLE
} else if res < 0 {
match util_libc::last_os_error().raw_os_error() {
Some(libc::ENOSYS) => NOT_AVAILABLE, // No kernel support
// The fallback on EPERM is intentionally not done on Android since this workaround
// seems to be needed only for specific Linux-based products that aren't based
// on Android. See https://github.com/rust-random/getrandom/issues/229.
#[cfg(target_os = "linux")]
Some(libc::EPERM) => NOT_AVAILABLE, // Blocked by seccomp
_ => raw_ptr,
let res_ptr = match NonNull::new(raw_ptr) {
Some(fptr) => {
let getrandom_fn = unsafe { mem::transmute::<NonNull<c_void>, GetRandomFn>(fptr) };
// Check that `getrandom` syscall is supported by kernel
let res = unsafe { getrandom_fn(ptr::NonNull::dangling().as_ptr(), 0, 0) };
if cfg!(getrandom_test_linux_fallback) {
NOT_AVAILABLE
} else if res < 0 {
match util_libc::last_os_error().raw_os_error() {
Some(libc::ENOSYS) => NOT_AVAILABLE, // No kernel support
// The fallback on EPERM is intentionally not done on Android since this workaround
// seems to be needed only for specific Linux-based products that aren't based
// on Android. See https://github.com/rust-random/getrandom/issues/229.
#[cfg(target_os = "linux")]
Some(libc::EPERM) => NOT_AVAILABLE, // Blocked by seccomp
_ => fptr,
}
} else {
fptr
}
} else {
raw_ptr
}
None => NOT_AVAILABLE,
};

GETRANDOM_FN.store(res_ptr, Ordering::Release);
GETRANDOM_FN.store(res_ptr.as_ptr(), Ordering::Release);
res_ptr
}

Expand All @@ -55,12 +55,13 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
// occurs when the function is called). Our implementation mirrors that of
// the one in libstd, meaning that the use of non-Relaxed operations is
// probably unnecessary.
let mut raw_ptr = GETRANDOM_FN.load(Ordering::Acquire);
if raw_ptr.is_null() {
raw_ptr = init();
}
let raw_ptr = GETRANDOM_FN.load(Ordering::Acquire);
let fptr = match NonNull::new(raw_ptr) {
Some(p) => p,
None => init(),
};

if raw_ptr == NOT_AVAILABLE {
if fptr == NOT_AVAILABLE {
// prevent inlining of the fallback implementation
#[inline(never)]
fn inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
Expand All @@ -70,9 +71,9 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
inner(dest)
} else {
// note: `transume` is currently the only way to get function pointer
let fptr = unsafe { mem::transmute::<*mut c_void, GetRandomFn>(raw_ptr) };
let getrandom_fn = unsafe { mem::transmute::<NonNull<c_void>, GetRandomFn>(fptr) };
util_libc::sys_fill_exact(dest, |buf| unsafe {
fptr(buf.as_mut_ptr().cast(), buf.len(), 0)
getrandom_fn(buf.as_mut_ptr().cast(), buf.len(), 0)
})
}
}

0 comments on commit 87b49b8

Please sign in to comment.