Skip to content

Commit a4e7b59

Browse files
committed
Review fixes
1 parent d4c1444 commit a4e7b59

File tree

4 files changed

+37
-25
lines changed

4 files changed

+37
-25
lines changed

src/lib.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,17 +225,25 @@ cfg_if! {
225225
#[path = "use_file.rs"] mod imp;
226226
} else if #[cfg(all(
227227
not(feature = "disable_urandom_fallback"),
228-
target_os = "linux",
229228
any(
230-
target_arch = "i586",
231-
target_arch = "i686",
232-
target_arch = "x86",
233-
target_arch = "x86_64",
234-
target_arch = "arm",
235-
target_arch = "armv7",
236-
target_arch = "powerpc",
237-
target_arch = "powerpc64le",
238-
target_arch = "s390x",
229+
// Rust supports Android API level 19 (KitKat) [0], while `getrandom(2)`
230+
// was added only in level 23 (Marshmallow).
231+
// [0]: https://blog.rust-lang.org/2023/01/09/android-ndk-update-r25.html
232+
target_os = "android",
233+
all(
234+
target_os = "linux",
235+
// Only on these architectures Rust supports Linux kernel versions (3.2+)
236+
// that precede the version (3.17) in which `getrandom(2)` was added:
237+
// https://doc.rust-lang.org/stable/rustc/platform-support.html
238+
any(
239+
target_arch = "arm",
240+
target_arch = "powerpc",
241+
target_arch = "powerpc64",
242+
target_arch = "s390x",
243+
target_arch = "x86",
244+
target_arch = "x86_64",
245+
),
246+
)
239247
),
240248
))] {
241249
mod util_libc;

src/linux_android.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
//! Implementation for Linux / Android
2-
use crate::{util_libc::sys_fill_exact, Error};
1+
//! Implementation for Linux / Android without `/dev/urandom` fallback
2+
use crate::{util_libc, Error};
33
use core::mem::MaybeUninit;
44

55
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
6-
sys_fill_exact(dest, |buf| unsafe {
7-
libc::syscall(
8-
libc::SYS_getrandom,
9-
buf.as_mut_ptr() as *mut libc::c_void,
10-
buf.len(),
11-
0,
12-
) as libc::ssize_t
13-
})
6+
util_libc::sys_fill_exact(dest, util_libc::getrandom_syscall)
147
}

src/linux_with_fallback.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//! Implementation for Linux / Android
1+
//! Implementation for Linux with `/dev/urandom` fallback
22
use crate::{
33
lazy::LazyBool,
4-
util_libc::{last_os_error, sys_fill_exact},
4+
util_libc::{getrandom_syscall, last_os_error, sys_fill_exact},
55
{use_file, Error},
66
};
77
use core::mem::MaybeUninit;
@@ -10,9 +10,7 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1010
// getrandom(2) was introduced in Linux 3.17
1111
static HAS_GETRANDOM: LazyBool = LazyBool::new();
1212
if HAS_GETRANDOM.unsync_init(is_getrandom_available) {
13-
sys_fill_exact(dest, |buf| unsafe {
14-
getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0)
15-
})
13+
sys_fill_exact(dest, getrandom_syscall)
1614
} else {
1715
use_file::getrandom_inner(dest)
1816
}

src/util_libc.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,16 @@ pub unsafe fn open_readonly(path: &str) -> Result<libc::c_int, Error> {
151151
}
152152
}
153153
}
154+
155+
/// Thin wrapper around the `getrandom()` Linux system call
156+
#[cfg(any(target_os = "android", target_os = "linux"))]
157+
pub fn getrandom_syscall(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {
158+
unsafe {
159+
libc::syscall(
160+
libc::SYS_getrandom,
161+
buf.as_mut_ptr() as *mut libc::c_void,
162+
buf.len(),
163+
0,
164+
) as libc::ssize_t
165+
}
166+
}

0 commit comments

Comments
 (0)