Skip to content

Commit 11da19b

Browse files
committed
Replace util_libc and sanitizer modules with impl_utils macro
1 parent 8861a62 commit 11da19b

File tree

13 files changed

+185
-178
lines changed

13 files changed

+185
-178
lines changed

src/backends.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! This module should provide `fill_inner` with the signature
44
//! `fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>`.
55
//! The function MUST fully initialize `dest` when `Ok(())` is returned;
6-
//! the function may need to use `sanitizer::unpoison` as well.
6+
//! the function may need to use `unpoison` as well.
77
//! The function MUST NOT ever write uninitialized bytes into `dest`,
88
//! regardless of what value it returns.
99
@@ -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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ 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+
crate::impl_utils!(get_errno, last_os_error);
1716

1817
#[inline]
1918
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2019
for chunk in dest.chunks_mut(256) {
2120
let ret = unsafe { libc::getentropy(chunk.as_mut_ptr().cast::<c_void>(), chunk.len()) };
2221
if ret != 0 {
23-
return Err(util_libc::last_os_error());
22+
return Err(last_os_error());
2423
}
2524
}
2625
Ok(())

src/backends/getrandom.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@ 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+
crate::impl_utils!(get_errno, last_os_error, sys_fill_exact);
2524

2625
#[inline]
2726
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
28-
util_libc::sys_fill_exact(dest, |buf| {
27+
sys_fill_exact(dest, |buf| {
2928
let ret = unsafe { libc::getrandom(buf.as_mut_ptr().cast(), buf.len(), 0) };
3029

3130
#[cfg(any(target_os = "android", target_os = "linux"))]
3231
unsafe {
33-
super::sanitizer::unpoison_linux_getrandom_result(buf, ret);
32+
crate::impl_utils!(unpoison_linux_getrandom_result);
33+
34+
unpoison_linux_getrandom_result(buf, ret);
3435
}
3536

3637
ret

src/backends/linux_android_with_fallback.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
//! Implementation for Linux / Android with `/dev/urandom` fallback
2-
use super::{sanitizer, use_file};
2+
use super::use_file::{self, last_os_error, sys_fill_exact};
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;
1110

1211
pub use crate::util::{inner_u32, inner_u64};
1312

@@ -19,6 +18,8 @@ const NOT_AVAILABLE: NonNull<c_void> = unsafe { NonNull::new_unchecked(usize::MA
1918

2019
static GETRANDOM_FN: AtomicPtr<c_void> = AtomicPtr::new(core::ptr::null_mut());
2120

21+
crate::impl_utils!(unpoison_linux_getrandom_result);
22+
2223
#[cold]
2324
#[inline(never)]
2425
fn init() -> NonNull<c_void> {
@@ -44,7 +45,7 @@ fn init() -> NonNull<c_void> {
4445
if cfg!(getrandom_test_linux_fallback) {
4546
NOT_AVAILABLE
4647
} else if res.is_negative() {
47-
match util_libc::last_os_error().raw_os_error() {
48+
match last_os_error().raw_os_error() {
4849
Some(libc::ENOSYS) => NOT_AVAILABLE, // No kernel support
4950
// The fallback on EPERM is intentionally not done on Android since this workaround
5051
// seems to be needed only for specific Linux-based products that aren't based
@@ -94,9 +95,9 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
9495
} else {
9596
// note: `transmute` is currently the only way to convert a pointer into a function reference
9697
let getrandom_fn = unsafe { transmute::<NonNull<c_void>, GetRandomFn>(fptr) };
97-
util_libc::sys_fill_exact(dest, |buf| unsafe {
98+
sys_fill_exact(dest, |buf| unsafe {
9899
let ret = getrandom_fn(buf.as_mut_ptr().cast(), buf.len(), 0);
99-
sanitizer::unpoison_linux_getrandom_result(buf, ret);
100+
unpoison_linux_getrandom_result(buf, ret);
100101
ret
101102
})
102103
}

src/backends/linux_raw.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! Implementation for Linux / Android using `asm!`-based syscalls.
2-
use super::sanitizer;
32
pub use crate::util::{inner_u32, inner_u64};
43
use crate::{Error, MaybeUninit};
54

@@ -140,14 +139,16 @@ unsafe fn getrandom_syscall(buf: *mut u8, buflen: usize, flags: u32) -> isize {
140139
r0
141140
}
142141

142+
crate::impl_utils!(unpoison_linux_getrandom_result);
143+
143144
#[inline]
144145
pub fn fill_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
145146
// Value of this error code is stable across all target arches.
146147
const EINTR: isize = -4;
147148

148149
loop {
149150
let ret = unsafe { getrandom_syscall(dest.as_mut_ptr().cast(), dest.len(), 0) };
150-
unsafe { sanitizer::unpoison_linux_getrandom_result(dest, ret) };
151+
unsafe { unpoison_linux_getrandom_result(dest, ret) };
151152
match usize::try_from(ret) {
152153
Ok(0) => return Err(Error::UNEXPECTED),
153154
Ok(len) => {

src/backends/netbsd.rs

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

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

17-
#[path = "../util_libc.rs"]
18-
mod util_libc;
17+
crate::impl_utils!(get_errno, last_os_error, sys_fill_exact);
1918

2019
unsafe extern "C" fn polyfill_using_kern_arand(
2120
buf: *mut c_void,
@@ -72,7 +71,7 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
7271
fptr = init();
7372
}
7473
let fptr = unsafe { mem::transmute::<*mut c_void, GetRandomFn>(fptr) };
75-
util_libc::sys_fill_exact(dest, |buf| unsafe {
74+
sys_fill_exact(dest, |buf| unsafe {
7675
fptr(buf.as_mut_ptr().cast::<c_void>(), buf.len(), 0)
7776
})
7877
}

src/backends/sanitizer.rs

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

src/backends/solaris.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ use core::{ffi::c_void, mem::MaybeUninit};
1717

1818
pub use crate::util::{inner_u32, inner_u64};
1919

20-
#[path = "../util_libc.rs"]
21-
mod util_libc;
20+
crate::impl_utils!(get_errno, last_os_error);
2221

2322
const MAX_BYTES: usize = 1024;
2423

@@ -33,7 +32,7 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
3332
// Good. Keep going.
3433
Ok(ret) if ret == chunk.len() => {}
3534
// The syscall failed.
36-
Ok(0) => return Err(util_libc::last_os_error()),
35+
Ok(0) => return Err(last_os_error()),
3736
// All other cases should be impossible.
3837
_ => return Err(Error::UNEXPECTED),
3938
}

src/backends/use_file.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ 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;
14-
1512
/// For all platforms, we use `/dev/urandom` rather than `/dev/random`.
1613
/// For more information see the linked man pages in lib.rs.
1714
/// - On Linux, "/dev/urandom is preferred and sufficient in all use cases".
@@ -40,13 +37,15 @@ const FD_ONGOING_INIT: libc::c_int = -2;
4037
// `Ordering::Acquire` to synchronize with it.
4138
static FD: AtomicI32 = AtomicI32::new(FD_UNINIT);
4239

40+
crate::impl_utils!(get_errno, last_os_error, sys_fill_exact);
41+
4342
#[inline]
4443
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
4544
let mut fd = FD.load(Ordering::Acquire);
4645
if fd == FD_UNINIT || fd == FD_ONGOING_INIT {
4746
fd = open_or_wait()?;
4847
}
49-
util_libc::sys_fill_exact(dest, |buf| unsafe {
48+
sys_fill_exact(dest, |buf| unsafe {
5049
libc::read(fd, buf.as_mut_ptr().cast::<c_void>(), buf.len())
5150
})
5251
}
@@ -58,7 +57,7 @@ fn open_readonly(path: &CStr) -> Result<libc::c_int, Error> {
5857
if fd >= 0 {
5958
return Ok(fd);
6059
}
61-
let err = util_libc::last_os_error();
60+
let err = last_os_error();
6261
// We should try again if open() was interrupted.
6362
if err.raw_os_error() != Some(libc::EINTR) {
6463
return Err(err);
@@ -136,7 +135,7 @@ mod sync {
136135

137136
#[cfg(any(target_os = "android", target_os = "linux"))]
138137
mod sync {
139-
use super::{Error, FD, FD_ONGOING_INIT, open_readonly, util_libc::last_os_error};
138+
use super::{Error, FD, FD_ONGOING_INIT, last_os_error, open_readonly};
140139

141140
/// Wait for atomic `FD` to change value from `FD_ONGOING_INIT` to something else.
142141
///

src/backends/vxworks.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use core::{
66
sync::atomic::{AtomicBool, Ordering::Relaxed},
77
};
88

9-
#[path = "../util_libc.rs"]
10-
mod util_libc;
11-
129
pub use crate::util::{inner_u32, inner_u64};
1310

1411
static RNG_INIT: AtomicBool = AtomicBool::new(false);
1512

13+
use libc::errnoGet as get_errno;
14+
crate::impl_utils!(last_os_error);
15+
1616
#[cold]
1717
fn init() -> Result<(), Error> {
1818
let ret = unsafe { libc::randSecure() };
@@ -42,7 +42,7 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
4242
let p: *mut libc::c_uchar = chunk.as_mut_ptr().cast();
4343
let ret = unsafe { libc::randABytes(p, chunk_len) };
4444
if ret != 0 {
45-
return Err(util_libc::last_os_error());
45+
return Err(last_os_error());
4646
}
4747
}
4848
Ok(())

0 commit comments

Comments
 (0)