Skip to content

Commit 2da6796

Browse files
authored
Merge branch 'master' into linux_raw
2 parents 2edbfcf + 074b15b commit 2da6796

File tree

8 files changed

+51
-57
lines changed

8 files changed

+51
-57
lines changed

.github/workflows/nopanic.yaml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ jobs:
4242
- name: Check (linux_android_with_fallback.rs)
4343
run: (exit $( grep -c panic target/release/libgetrandom_wrapper.so ))
4444

45-
- name: Build (linux_android.rs)
45+
- name: Build (getrandom.rs)
4646
env:
4747
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom"
4848
run: cargo build --release
49-
- name: Check (linux_android.rs)
49+
- name: Check (getrandom.rs)
5050
run: (exit $( grep -c panic target/release/libgetrandom_wrapper.so ))
5151

5252
- name: Build (linux_raw.rs)
@@ -105,11 +105,6 @@ jobs:
105105
- name: Check (rndr.rs)
106106
run: (exit $( grep -c panic target/aarch64-unknown-linux-gnu/release/libgetrandom_wrapper.so ))
107107

108-
- name: Build (getrandom.rs)
109-
run: cross build --release --target=x86_64-unknown-freebsd
110-
- name: Check (getrandom.rs)
111-
run: (exit $( grep -c panic target/x86_64-unknown-freebsd/release/libgetrandom_wrapper.so ))
112-
113108
- name: Build (netbsd.rs)
114109
run: cross build --release --target=x86_64-unknown-netbsd
115110
- name: Check (netbsd.rs)

.github/workflows/tests.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ jobs:
6565
RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback
6666
RUSTDOCFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback
6767
run: cargo test --features=std
68+
- env:
69+
RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_without_fallback
70+
RUSTDOCFLAGS: -Dwarnings --cfg getrandom_test_linux_without_fallback
71+
run: cargo test --features=std
6872
- env:
6973
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand"
7074
RUSTDOCFLAGS: -Dwarnings --cfg getrandom_backend="rdrand"

.github/workflows/workspace.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ jobs:
4141
run: cargo clippy -Zbuild-std=core --target x86_64-unknown-fuchsia
4242
- name: OpenBSD (getentropy.rs)
4343
run: cargo clippy -Zbuild-std=core --target x86_64-unknown-openbsd
44-
- name: FreeBSD (getrandom.rs)
45-
run: cargo clippy -Zbuild-std=core --target x86_64-unknown-freebsd
4644
- name: Hermit (hermit.rs)
4745
run: cargo clippy -Zbuild-std=core --target x86_64-unknown-hermit
4846
- name: Web WASM (wasm_js.rs)
@@ -53,7 +51,7 @@ jobs:
5351
env:
5452
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory
5553
run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown --features wasm_js
56-
- name: Linux (linux_android.rs)
54+
- name: Linux (getrandom.rs)
5755
env:
5856
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom"
5957
run: cargo clippy --target x86_64-unknown-linux-gnu

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ cfg-if = "1"
3030
compiler_builtins = { version = "0.1", optional = true }
3131
core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" }
3232

33-
# linux_android / linux_android_with_fallback
33+
# getrandom / linux_android_with_fallback
3434
[target.'cfg(all(any(target_os = "linux", target_os = "android"), not(any(getrandom_backend = "custom", getrandom_backend = "linux_raw", getrandom_backend = "rdrand", getrandom_backend = "rndr"))))'.dependencies]
3535
libc = { version = "0.2.154", default-features = false }
3636

@@ -84,6 +84,7 @@ check-cfg = [
8484
'cfg(getrandom_backend, values("custom", "rdrand", "rndr", "linux_getrandom", "linux_raw", "wasm_js"))',
8585
'cfg(getrandom_msan)',
8686
'cfg(getrandom_test_linux_fallback)',
87+
'cfg(getrandom_test_linux_without_fallback)',
8788
'cfg(getrandom_test_netbsd_fallback)',
8889
]
8990

src/backends.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ cfg_if! {
1111
mod custom;
1212
pub use custom::*;
1313
} else if #[cfg(getrandom_backend = "linux_getrandom")] {
14-
mod linux_android;
15-
pub use linux_android::*;
14+
mod getrandom;
15+
pub use getrandom::*;
1616
} else if #[cfg(getrandom_backend = "linux_raw")] {
1717
mod linux_raw;
1818
pub use linux_raw::*;
@@ -54,17 +54,6 @@ cfg_if! {
5454
))] {
5555
mod getentropy;
5656
pub use getentropy::*;
57-
} else if #[cfg(any(
58-
target_os = "dragonfly",
59-
target_os = "freebsd",
60-
target_os = "hurd",
61-
target_os = "illumos",
62-
// Check for target_arch = "arm" to only include the 3DS. Does not
63-
// include the Nintendo Switch (which is target_arch = "aarch64").
64-
all(target_os = "horizon", target_arch = "arm"),
65-
))] {
66-
mod getrandom;
67-
pub use getrandom::*;
6857
} else if #[cfg(any(
6958
// Rust supports Android API level 19 (KitKat) [0] and the next upgrade targets
7059
// level 21 (Lollipop) [1], while `getrandom(2)` was added only in
@@ -105,9 +94,19 @@ cfg_if! {
10594
mod use_file;
10695
mod linux_android_with_fallback;
10796
pub use linux_android_with_fallback::*;
108-
} else if #[cfg(any(target_os = "android", target_os = "linux"))] {
109-
mod linux_android;
110-
pub use linux_android::*;
97+
} else if #[cfg(any(
98+
target_os = "android",
99+
target_os = "linux",
100+
target_os = "dragonfly",
101+
target_os = "freebsd",
102+
target_os = "hurd",
103+
target_os = "illumos",
104+
// Check for target_arch = "arm" to only include the 3DS. Does not
105+
// include the Nintendo Switch (which is target_arch = "aarch64").
106+
all(target_os = "horizon", target_arch = "arm"),
107+
))] {
108+
mod getrandom;
109+
pub use getrandom::*;
111110
} else if #[cfg(target_os = "solaris")] {
112111
mod solaris;
113112
pub use solaris::*;

src/backends/getrandom.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//! nothing. On illumos, the default pool is used to implement getentropy(2),
1717
//! so we assume it is acceptable here.
1818
use crate::Error;
19-
use core::{ffi::c_void, mem::MaybeUninit};
19+
use core::mem::MaybeUninit;
2020

2121
pub use crate::util::{inner_u32, inner_u64};
2222

@@ -26,6 +26,6 @@ mod util_libc;
2626
#[inline]
2727
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2828
util_libc::sys_fill_exact(dest, |buf| unsafe {
29-
libc::getrandom(buf.as_mut_ptr().cast::<c_void>(), buf.len(), 0)
29+
libc::getrandom(buf.as_mut_ptr().cast(), buf.len(), 0)
3030
})
3131
}

src/backends/linux_android.rs

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/backends/linux_android_with_fallback.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use super::use_file;
33
use crate::Error;
44
use core::{
55
ffi::c_void,
6-
mem::{self, MaybeUninit},
7-
ptr::{self, NonNull},
6+
mem::{transmute, MaybeUninit},
7+
ptr::NonNull,
88
sync::atomic::{AtomicPtr, Ordering},
99
};
1010
use use_file::util_libc;
@@ -17,18 +17,28 @@ type GetRandomFn = unsafe extern "C" fn(*mut c_void, libc::size_t, libc::c_uint)
1717
/// or not supported by kernel.
1818
const NOT_AVAILABLE: NonNull<c_void> = unsafe { NonNull::new_unchecked(usize::MAX as *mut c_void) };
1919

20-
static GETRANDOM_FN: AtomicPtr<c_void> = AtomicPtr::new(ptr::null_mut());
20+
static GETRANDOM_FN: AtomicPtr<c_void> = AtomicPtr::new(core::ptr::null_mut());
2121

2222
#[cold]
2323
#[inline(never)]
2424
fn init() -> NonNull<c_void> {
25-
static NAME: &[u8] = b"getrandom\0";
26-
let name_ptr = NAME.as_ptr().cast::<libc::c_char>();
27-
let raw_ptr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name_ptr) };
25+
// Use static linking to `libc::getrandom` on MUSL targets and `dlsym` everywhere else
26+
#[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+
2838
let res_ptr = match NonNull::new(raw_ptr) {
2939
Some(fptr) => {
30-
let getrandom_fn = unsafe { mem::transmute::<NonNull<c_void>, GetRandomFn>(fptr) };
31-
let dangling_ptr = ptr::NonNull::dangling().as_ptr();
40+
let getrandom_fn = unsafe { transmute::<NonNull<c_void>, GetRandomFn>(fptr) };
41+
let dangling_ptr = NonNull::dangling().as_ptr();
3242
// Check that `getrandom` syscall is supported by kernel
3343
let res = unsafe { getrandom_fn(dangling_ptr, 0, 0) };
3444
if cfg!(getrandom_test_linux_fallback) {
@@ -50,11 +60,16 @@ fn init() -> NonNull<c_void> {
5060
None => NOT_AVAILABLE,
5161
};
5262

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+
5368
GETRANDOM_FN.store(res_ptr.as_ptr(), Ordering::Release);
5469
res_ptr
5570
}
5671

57-
// prevent inlining of the fallback implementation
72+
// Prevent inlining of the fallback implementation
5873
#[inline(never)]
5974
fn use_file_fallback(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
6075
use_file::fill_inner(dest)
@@ -78,7 +93,7 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
7893
use_file_fallback(dest)
7994
} else {
8095
// note: `transmute` is currently the only way to convert a pointer into a function reference
81-
let getrandom_fn = unsafe { mem::transmute::<NonNull<c_void>, GetRandomFn>(fptr) };
96+
let getrandom_fn = unsafe { transmute::<NonNull<c_void>, GetRandomFn>(fptr) };
8297
util_libc::sys_fill_exact(dest, |buf| unsafe {
8398
getrandom_fn(buf.as_mut_ptr().cast(), buf.len(), 0)
8499
})

0 commit comments

Comments
 (0)