Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linux_android_with_fallback: do not use dlsym on MUSL targets #602

Merged
merged 3 commits into from
Feb 12, 2025

Conversation

newpavlov
Copy link
Member

Fixes #600

#[cfg(target_env = "musl")]
let raw_ptr = {
let fptr: GetRandomFn = libc::getrandom;
unsafe { transmute::<GetRandomFn, *mut c_void>(fptr) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast to a pointer and back smells like an indirect call.

Copy link
Member Author

@newpavlov newpavlov Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is effectively no overhead to this approach. "Direct" call to libc::getradom compiles down to:

xor     edx, edx
jmp     qword ptr [rip + getrandom@GOTPCREL]

While the "indirect" call compiles to:

mov     rax, qword ptr [rip + GETRANDOM_FN]
xor     edx, edx
jmp     rax

Yes, the former is a bit friendlier to CPUs, but compared to the syscall cost the difference is negligible. If you care about this difference, then you should use the linux_getrandom opt-in backend.

@tamird
Copy link
Contributor

tamird commented Feb 5, 2025

@newpavlov just force push to remove the two spurious commits - you're going to clutter the git history this way =/

@newpavlov
Copy link
Member Author

We always squash commits before merge, so local commit history in PR does not matter much.

@tamird
Copy link
Contributor

tamird commented Feb 5, 2025

OK. If this were my project I'd ask you to back out all the unnecessary import churn and random comment changes - but it's not!

Copy link
Contributor

@tamird tamird left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@newpavlov can we please merge this one way or the other to fix the breakage and cut 0.3.2? Thank you.

@@ -17,18 +17,28 @@ type GetRandomFn = unsafe extern "C" fn(*mut c_void, libc::size_t, libc::c_uint)
/// or not supported by kernel.
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());
static GETRANDOM_FN: AtomicPtr<c_void> = AtomicPtr::new(core::ptr::null_mut());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we avoid this noise in the diff? doesn't seem related to the fix being made.

let res_ptr = match NonNull::new(raw_ptr) {
Some(fptr) => {
let getrandom_fn = unsafe { mem::transmute::<NonNull<c_void>, GetRandomFn>(fptr) };
let dangling_ptr = ptr::NonNull::dangling().as_ptr();
let getrandom_fn = unsafe { transmute::<NonNull<c_void>, GetRandomFn>(fptr) };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the diff on these 2 lines is also not necessary.

@@ -54,7 +64,7 @@ fn init() -> NonNull<c_void> {
res_ptr
}

// prevent inlining of the fallback implementation
// Prevent inlining of the fallback implementation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not necessary

@@ -78,7 +88,7 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
use_file_fallback(dest)
} else {
// note: `transmute` is currently the only way to convert a pointer into a function reference
let getrandom_fn = unsafe { mem::transmute::<NonNull<c_void>, GetRandomFn>(fptr) };
let getrandom_fn = unsafe { transmute::<NonNull<c_void>, GetRandomFn>(fptr) };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not necessary

@newpavlov
Copy link
Member Author

I think it's fine to keep minor code tweaks in this PR.

Before releasing v0.3.2 I would like to also merge the linux_raw and efi_rng PRs.

@newpavlov newpavlov merged commit b75db5c into master Feb 12, 2025
58 checks passed
@newpavlov newpavlov deleted the fix_musl branch February 12, 2025 15:28
@newpavlov newpavlov mentioned this pull request Mar 7, 2025
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

linux_android_with_fallback never uses getrandom on musl
2 participants