Skip to content

Commit 6b2c9e0

Browse files
committed
Refactor util_libc
1 parent 11783b3 commit 6b2c9e0

17 files changed

+124
-165
lines changed

src/backends.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ cfg_if! {
1313
pub use custom::*;
1414
} else if #[cfg(getrandom_backend = "linux_getrandom")] {
1515
mod getrandom;
16-
mod sanitizer;
1716
pub use getrandom::*;
1817
} else if #[cfg(getrandom_backend = "linux_raw")] {
1918
mod linux_raw;
20-
mod sanitizer;
2119
pub use linux_raw::*;
2220
} else if #[cfg(getrandom_backend = "rdrand")] {
2321
mod rdrand;
@@ -49,7 +47,6 @@ cfg_if! {
4947
pub use unsupported::*;
5048
} else if #[cfg(all(target_os = "linux", target_env = ""))] {
5149
mod linux_raw;
52-
mod sanitizer;
5350
pub use linux_raw::*;
5451
} else if #[cfg(target_os = "espidf")] {
5552
mod esp_idf;
@@ -117,7 +114,6 @@ cfg_if! {
117114
))] {
118115
mod use_file;
119116
mod linux_android_with_fallback;
120-
mod sanitizer;
121117
pub use linux_android_with_fallback::*;
122118
} else if #[cfg(any(
123119
target_os = "android",
@@ -132,8 +128,6 @@ cfg_if! {
132128
all(target_os = "horizon", target_arch = "arm"),
133129
))] {
134130
mod getrandom;
135-
#[cfg(any(target_os = "android", target_os = "linux"))]
136-
mod sanitizer;
137131
pub use getrandom::*;
138132
} else if #[cfg(target_os = "solaris")] {
139133
mod solaris;

src/backends/getentropy.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ use core::{ffi::c_void, mem::MaybeUninit};
1212

1313
pub use crate::util::{inner_u32, inner_u64};
1414

15-
#[path = "../util_libc.rs"]
16-
mod util_libc;
15+
#[path = "../utils/get_errno.rs"]
16+
mod utils;
1717

1818
#[inline]
1919
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2020
for chunk in dest.chunks_mut(256) {
2121
let ret = unsafe { libc::getentropy(chunk.as_mut_ptr().cast::<c_void>(), chunk.len()) };
2222
if ret != 0 {
23-
return Err(util_libc::last_os_error());
23+
let errno = utils::get_errno();
24+
return Err(Error::from_errno(errno));
2425
}
2526
}
2627
Ok(())

src/backends/getrandom.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,12 @@ use core::mem::MaybeUninit;
2020

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

23-
#[path = "../util_libc.rs"]
24-
mod util_libc;
23+
#[path = "../utils/sys_fill_exact.rs"]
24+
mod utils;
2525

2626
#[inline]
2727
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
28-
util_libc::sys_fill_exact(dest, |buf| {
29-
let ret = unsafe { libc::getrandom(buf.as_mut_ptr().cast(), buf.len(), 0) };
30-
31-
#[cfg(any(target_os = "android", target_os = "linux"))]
32-
unsafe {
33-
super::sanitizer::unpoison_linux_getrandom_result(buf, ret);
34-
}
35-
36-
ret
28+
utils::sys_fill_exact(dest, |buf| unsafe {
29+
libc::getrandom(buf.as_mut_ptr().cast(), buf.len(), 0)
3730
})
3831
}

src/backends/linux_android_with_fallback.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//! Implementation for Linux / Android with `/dev/urandom` fallback
2-
use super::{sanitizer, use_file};
2+
use super::use_file;
33
use crate::Error;
44
use core::{
55
ffi::c_void,
66
mem::{MaybeUninit, transmute},
77
ptr::NonNull,
88
sync::atomic::{AtomicPtr, Ordering},
99
};
10-
use use_file::util_libc;
10+
use use_file::utils;
1111

1212
pub use crate::util::{inner_u32, inner_u64};
1313

@@ -44,13 +44,13 @@ fn init() -> NonNull<c_void> {
4444
if cfg!(getrandom_test_linux_fallback) {
4545
NOT_AVAILABLE
4646
} else if res.is_negative() {
47-
match util_libc::last_os_error().raw_os_error() {
48-
Some(libc::ENOSYS) => NOT_AVAILABLE, // No kernel support
47+
match utils::get_errno() {
48+
libc::ENOSYS => NOT_AVAILABLE, // No kernel support
4949
// The fallback on EPERM is intentionally not done on Android since this workaround
5050
// seems to be needed only for specific Linux-based products that aren't based
5151
// on Android. See https://github.com/rust-random/getrandom/issues/229.
5252
#[cfg(target_os = "linux")]
53-
Some(libc::EPERM) => NOT_AVAILABLE, // Blocked by seccomp
53+
libc::EPERM => NOT_AVAILABLE, // Blocked by seccomp
5454
_ => fptr,
5555
}
5656
} else {
@@ -94,10 +94,8 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
9494
} else {
9595
// note: `transmute` is currently the only way to convert a pointer into a function reference
9696
let getrandom_fn = unsafe { transmute::<NonNull<c_void>, GetRandomFn>(fptr) };
97-
util_libc::sys_fill_exact(dest, |buf| unsafe {
98-
let ret = getrandom_fn(buf.as_mut_ptr().cast(), buf.len(), 0);
99-
sanitizer::unpoison_linux_getrandom_result(buf, ret);
100-
ret
97+
utils::sys_fill_exact(dest, |buf| unsafe {
98+
getrandom_fn(buf.as_mut_ptr().cast(), buf.len(), 0)
10199
})
102100
}
103101
}

src/backends/linux_raw.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ use crate::{Error, MaybeUninit};
66
#[cfg(not(any(target_os = "android", target_os = "linux")))]
77
compile_error!("`linux_raw` backend can be enabled only for Linux/Android targets!");
88

9+
#[path = "../utils/sanitizer.rs"]
10+
mod utils;
11+
912
#[allow(non_upper_case_globals)]
1013
unsafe fn getrandom_syscall(buf: *mut u8, buflen: usize, flags: u32) -> isize {
1114
let r0;
@@ -147,11 +150,12 @@ pub fn fill_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
147150

148151
loop {
149152
let ret = unsafe { getrandom_syscall(dest.as_mut_ptr().cast(), dest.len(), 0) };
150-
unsafe { sanitizer::unpoison_linux_getrandom_result(dest, ret) };
151153
match usize::try_from(ret) {
152154
Ok(0) => return Err(Error::UNEXPECTED),
153155
Ok(len) => {
154-
dest = dest.get_mut(len..).ok_or(Error::UNEXPECTED)?;
156+
let (l, r) = dest.split_at_mut_checked(len).ok_or(Error::UNEXPECTED)?;
157+
unsafe { utils::unpoison(l) };
158+
dest = r;
155159
if dest.is_empty() {
156160
return Ok(());
157161
}

src/backends/netbsd.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use core::{
1414

1515
pub use crate::util::{inner_u32, inner_u64};
1616

17-
#[path = "../util_libc.rs"]
18-
mod util_libc;
17+
#[path = "../utils/sys_fill_exact.rs"]
18+
mod utils;
1919

2020
unsafe extern "C" fn polyfill_using_kern_arand(
2121
buf: *mut c_void,
@@ -72,7 +72,7 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
7272
fptr = init();
7373
}
7474
let fptr = unsafe { mem::transmute::<*mut c_void, GetRandomFn>(fptr) };
75-
util_libc::sys_fill_exact(dest, |buf| unsafe {
75+
utils::sys_fill_exact(dest, |buf| unsafe {
7676
fptr(buf.as_mut_ptr().cast::<c_void>(), buf.len(), 0)
7777
})
7878
}

src/backends/rdrand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use crate::{Error, util::slice_as_uninit};
33
use core::mem::{MaybeUninit, size_of};
44

5-
#[path = "../lazy.rs"]
5+
#[path = "../utils/lazy.rs"]
66
mod lazy;
77

88
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))]

src/backends/rndr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn is_rndr_available() -> bool {
6969

7070
#[cfg(not(target_feature = "rand"))]
7171
fn is_rndr_available() -> bool {
72-
#[path = "../lazy.rs"]
72+
#[path = "../utils/lazy.rs"]
7373
mod lazy;
7474
static RNDR_GOOD: lazy::LazyBool = lazy::LazyBool::new();
7575

src/backends/solaris.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414
//! which also explains why this crate should not use getentropy(2).
1515
use crate::Error;
1616
use core::{ffi::c_void, mem::MaybeUninit};
17+
use libc::___errno as errno_location;
1718

1819
pub use crate::util::{inner_u32, inner_u64};
1920

20-
#[path = "../util_libc.rs"]
21-
mod util_libc;
22-
2321
const MAX_BYTES: usize = 1024;
2422

2523
#[inline]
@@ -33,7 +31,10 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
3331
// Good. Keep going.
3432
Ok(ret) if ret == chunk.len() => {}
3533
// The syscall failed.
36-
Ok(0) => return Err(util_libc::last_os_error()),
34+
Ok(0) => {
35+
let errno = unsafe { core::ptr::read(errno_location()) };
36+
return Err(Error::from_errno(errno));
37+
}
3738
// All other cases should be impossible.
3839
_ => return Err(Error::UNEXPECTED),
3940
}

src/backends/use_file.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use core::{
99
#[cfg(not(any(target_os = "android", target_os = "linux")))]
1010
pub use crate::util::{inner_u32, inner_u64};
1111

12-
#[path = "../util_libc.rs"]
13-
pub(super) mod util_libc;
12+
#[path = "../utils/sys_fill_exact.rs"]
13+
pub(super) mod utils;
1414

1515
/// For all platforms, we use `/dev/urandom` rather than `/dev/random`.
1616
/// For more information see the linked man pages in lib.rs.
@@ -46,7 +46,7 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
4646
if fd == FD_UNINIT || fd == FD_ONGOING_INIT {
4747
fd = open_or_wait()?;
4848
}
49-
util_libc::sys_fill_exact(dest, |buf| unsafe {
49+
utils::sys_fill_exact(dest, |buf| unsafe {
5050
libc::read(fd, buf.as_mut_ptr().cast::<c_void>(), buf.len())
5151
})
5252
}
@@ -58,10 +58,10 @@ fn open_readonly(path: &CStr) -> Result<libc::c_int, Error> {
5858
if fd >= 0 {
5959
return Ok(fd);
6060
}
61-
let err = util_libc::last_os_error();
61+
let errno = utils::get_errno();
6262
// We should try again if open() was interrupted.
63-
if err.raw_os_error() != Some(libc::EINTR) {
64-
return Err(err);
63+
if errno != libc::EINTR {
64+
return Err(Error::from_errno(errno));
6565
}
6666
}
6767
}
@@ -136,7 +136,7 @@ mod sync {
136136

137137
#[cfg(any(target_os = "android", target_os = "linux"))]
138138
mod sync {
139-
use super::{Error, FD, FD_ONGOING_INIT, open_readonly, util_libc::last_os_error};
139+
use super::{Error, FD, FD_ONGOING_INIT, open_readonly, utils};
140140

141141
/// Wait for atomic `FD` to change value from `FD_ONGOING_INIT` to something else.
142142
///
@@ -152,7 +152,7 @@ mod sync {
152152
debug_assert!({
153153
match ret {
154154
0 => true,
155-
-1 => last_os_error().raw_os_error() == Some(libc::EAGAIN),
155+
-1 => utils::get_errno() == libc::EAGAIN,
156156
_ => false,
157157
}
158158
});
@@ -209,12 +209,12 @@ mod sync {
209209
debug_assert_eq!(res, 1);
210210
break Ok(());
211211
}
212-
let err = last_os_error();
212+
let errno = utils::get_errno();
213213
// Assuming that `poll` is called correctly,
214214
// on Linux it can return only EINTR and ENOMEM errors.
215-
match err.raw_os_error() {
216-
Some(libc::EINTR) => continue,
217-
_ => break Err(err),
215+
match errno {
216+
libc::EINTR => continue,
217+
_ => break Err(Error::from_errno(errno)),
218218
}
219219
};
220220
unsafe { libc::close(fd) };

0 commit comments

Comments
 (0)