Conversation
| #[cfg(target_env = "musl")] | ||
| let raw_ptr = { | ||
| let fptr: GetRandomFn = libc::getrandom; | ||
| unsafe { transmute::<GetRandomFn, *mut c_void>(fptr) } |
There was a problem hiding this comment.
This cast to a pointer and back smells like an indirect call.
There was a problem hiding this comment.
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 raxYes, 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.
|
@newpavlov just force push to remove the two spurious commits - you're going to clutter the git history this way =/ |
|
We always squash commits before merge, so local commit history in PR does not matter much. |
|
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! |
tamird
left a comment
There was a problem hiding this comment.
@newpavlov can we please merge this one way or the other to fix the breakage and cut 0.3.2? Thank you.
| 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()); |
There was a problem hiding this comment.
could we avoid this noise in the diff? doesn't seem related to the fix being made.
| 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) }; |
There was a problem hiding this comment.
the diff on these 2 lines is also not necessary.
| } | ||
|
|
||
| // prevent inlining of the fallback implementation | ||
| // Prevent inlining of the fallback implementation |
| } 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) }; |
|
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 |
### Added - `efi_rng` opt-in backend [#570] - `linux_raw` opt-in backend [#572] - `.cargo/config.toml` example in the crate-level docs [#591] - `getrandom_test_linux_without_fallback` configuration flag to test that file fallback is not triggered in the `linux_android_with_fallback` backend [#605] - Built-in support for `*-linux-none` targets [#618] - Cygwin support [#626] ### Changed - Update `wasi` dependency to v0.14 [#594] - Add `#[inline]` attribute to the inner functions [#596] - Update WASI and Emscripten links in the crate-level docs [#597] - Do not use `dlsym` on MUSL targets in the `linux_android_with_fallback` backend [#602] - Remove `linux_android.rs` and use `getrandom.rs` instead [#603] - Always use `RtlGenRandom` on Windows targets when compiling with pre-1.78 Rust [#610] - Internal representation of the `Error` type [#614] - Remove `windows-targets` dependency and use [`raw-dylib`] directly [#627] ### Removed - `Error::INTERNAL_START` and `Error::CUSTOM_START` associated constants [#614] [#570]: #570 [#572]: #572 [#591]: #591 [#594]: #594 [#596]: #596 [#597]: #597 [#602]: #602 [#603]: #603 [#605]: #605 [#610]: #610 [#614]: #614 [#618]: #618 [#626]: #626 [#627]: #627 [`raw-dylib`]: https://doc.rust-lang.org/reference/items/external-blocks.html?highlight=link#dylib-versus-raw-dylib
|
好尴尬 🙃,当我为 mips musl 编译它时,遇到了一些奇奇怪怪的错误: https://github.com/sbwml/shadowsocks-rust-mips/actions/runs/13956208010/job/39067818527#step:5:404 给它定义一个 getrandom 函数它才可以编译 https://github.com/sbwml/shadowsocks-rust-mips/actions/runs/13971234091/job/39113320608#step:5:419 |
|
@sbwml Rust officially uses musl v1.2.3 for the For now you could downgrade |
|
Indeed, Looks like that was somewhat intentional: rust-lang/libc@457c02d |
Either way, it's worth to clarify it with |
|
The discussion in that PR (rust-lang/libc#1399) suggests that it was failing CI at the time. The musl version was bumped in rust-lang/rust#107129, though. I'll send a patch and see what happens. |
Same issue, the problem was temporarily solved by specifying the getramdom version in Cargo.toml: getrandom = "=0.3.1"Experienced supply poisoning :( |
|
The fix has been merged in rust-lang/libc#4346, once it's released on |
### Added - `efi_rng` opt-in backend [rust-random#570] - `linux_raw` opt-in backend [rust-random#572] - `.cargo/config.toml` example in the crate-level docs [rust-random#591] - `getrandom_test_linux_without_fallback` configuration flag to test that file fallback is not triggered in the `linux_android_with_fallback` backend [rust-random#605] - Built-in support for `*-linux-none` targets [rust-random#618] - Cygwin support [rust-random#626] ### Changed - Update `wasi` dependency to v0.14 [rust-random#594] - Add `#[inline]` attribute to the inner functions [rust-random#596] - Update WASI and Emscripten links in the crate-level docs [rust-random#597] - Do not use `dlsym` on MUSL targets in the `linux_android_with_fallback` backend [rust-random#602] - Remove `linux_android.rs` and use `getrandom.rs` instead [rust-random#603] - Always use `RtlGenRandom` on Windows targets when compiling with pre-1.78 Rust [rust-random#610] - Internal representation of the `Error` type [rust-random#614] - Remove `windows-targets` dependency and use [`raw-dylib`] directly [rust-random#627] ### Removed - `Error::INTERNAL_START` and `Error::CUSTOM_START` associated constants [rust-random#614] [rust-random#570]: rust-random#570 [rust-random#572]: rust-random#572 [rust-random#591]: rust-random#591 [rust-random#594]: rust-random#594 [rust-random#596]: rust-random#596 [rust-random#597]: rust-random#597 [rust-random#602]: rust-random#602 [rust-random#603]: rust-random#603 [rust-random#605]: rust-random#605 [rust-random#610]: rust-random#610 [rust-random#614]: rust-random#614 [rust-random#618]: rust-random#618 [rust-random#626]: rust-random#626 [rust-random#627]: rust-random#627 [`raw-dylib`]: https://doc.rust-lang.org/reference/items/external-blocks.html?highlight=link#dylib-versus-raw-dylib
Fixes #600