Skip to content

Commit ce3b017

Browse files
authored
Add #[inline] attribute to the inner functions (#596)
1 parent e4dcbbc commit ce3b017

22 files changed

+51
-13
lines changed

src/backends/apple_other.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use core::{ffi::c_void, mem::MaybeUninit};
44

55
pub use crate::util::{inner_u32, inner_u64};
66

7+
#[inline]
78
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
89
let dst_ptr = dest.as_mut_ptr().cast::<c_void>();
910
let ret = unsafe { libc::CCRandomGenerateBytes(dst_ptr, dest.len()) };

src/backends/custom.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use core::mem::MaybeUninit;
44

55
pub use crate::util::{inner_u32, inner_u64};
66

7+
#[inline]
78
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
89
extern "Rust" {
910
fn __getrandom_v03_custom(dest: *mut u8, len: usize) -> Result<(), Error>;

src/backends/esp_idf.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88
fn esp_fill_random(buf: *mut c_void, len: usize) -> u32;
99
}
1010

11+
#[inline]
1112
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1213
// Not that NOT enabling WiFi, BT, or the voltage noise entropy source (via `bootloader_random_enable`)
1314
// will cause ESP-IDF to return pseudo-random numbers based on the voltage noise entropy, after the initial boot process:

src/backends/fuchsia.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern "C" {
99
fn zx_cprng_draw(buffer: *mut u8, length: usize);
1010
}
1111

12+
#[inline]
1213
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1314
unsafe { zx_cprng_draw(dest.as_mut_ptr().cast::<u8>(), dest.len()) }
1415
Ok(())

src/backends/getentropy.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub use crate::util::{inner_u32, inner_u64};
1515
#[path = "../util_libc.rs"]
1616
mod util_libc;
1717

18+
#[inline]
1819
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1920
for chunk in dest.chunks_mut(256) {
2021
let ret = unsafe { libc::getentropy(chunk.as_mut_ptr().cast::<c_void>(), chunk.len()) };

src/backends/getrandom.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub use crate::util::{inner_u32, inner_u64};
2323
#[path = "../util_libc.rs"]
2424
mod util_libc;
2525

26+
#[inline]
2627
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2728
util_libc::sys_fill_exact(dest, |buf| unsafe {
2829
libc::getrandom(buf.as_mut_ptr().cast::<c_void>(), buf.len(), 0)

src/backends/hermit.rs

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extern "C" {
1212
fn sys_secure_rand64(value: *mut u64) -> i32;
1313
}
1414

15+
#[inline]
1516
pub fn inner_u32() -> Result<u32, Error> {
1617
let mut res = MaybeUninit::uninit();
1718
let ret = unsafe { sys_secure_rand32(res.as_mut_ptr()) };
@@ -22,6 +23,7 @@ pub fn inner_u32() -> Result<u32, Error> {
2223
}
2324
}
2425

26+
#[inline]
2527
pub fn inner_u64() -> Result<u64, Error> {
2628
let mut res = MaybeUninit::uninit();
2729
let ret = unsafe { sys_secure_rand64(res.as_mut_ptr()) };
@@ -32,6 +34,7 @@ pub fn inner_u64() -> Result<u64, Error> {
3234
}
3335
}
3436

37+
#[inline]
3538
pub fn fill_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
3639
while !dest.is_empty() {
3740
let res = unsafe { sys_read_entropy(dest.as_mut_ptr().cast::<u8>(), dest.len(), 0) };

src/backends/linux_android.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod util_libc;
1010
#[cfg(not(any(target_os = "android", target_os = "linux")))]
1111
compile_error!("`linux_getrandom` backend can be enabled only for Linux/Android targets!");
1212

13+
#[inline]
1314
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1415
util_libc::sys_fill_exact(dest, |buf| unsafe {
1516
libc::getrandom(buf.as_mut_ptr().cast(), buf.len(), 0)

src/backends/linux_android_with_fallback.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const NOT_AVAILABLE: NonNull<c_void> = unsafe { NonNull::new_unchecked(usize::MA
2020
static GETRANDOM_FN: AtomicPtr<c_void> = AtomicPtr::new(ptr::null_mut());
2121

2222
#[cold]
23+
#[inline(never)]
2324
fn init() -> NonNull<c_void> {
2425
static NAME: &[u8] = b"getrandom\0";
2526
let name_ptr = NAME.as_ptr().cast::<libc::c_char>();
@@ -59,6 +60,7 @@ fn use_file_fallback(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
5960
use_file::fill_inner(dest)
6061
}
6162

63+
#[inline]
6264
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
6365
// Despite being only a single atomic variable, we still cannot always use
6466
// Ordering::Relaxed, as we need to make sure a successful call to `init`
@@ -75,7 +77,7 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
7577
if fptr == NOT_AVAILABLE {
7678
use_file_fallback(dest)
7779
} else {
78-
// note: `transume` is currently the only way to convert pointer into function reference
80+
// note: `transmute` is currently the only way to convert a pointer into a function reference
7981
let getrandom_fn = unsafe { mem::transmute::<NonNull<c_void>, GetRandomFn>(fptr) };
8082
util_libc::sys_fill_exact(dest, |buf| unsafe {
8183
getrandom_fn(buf.as_mut_ptr().cast(), buf.len(), 0)

src/backends/netbsd.rs

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type GetRandomFn = unsafe extern "C" fn(*mut c_void, libc::size_t, libc::c_uint)
4545
static GETRANDOM: AtomicPtr<c_void> = AtomicPtr::new(ptr::null_mut());
4646

4747
#[cold]
48+
#[inline(never)]
4849
fn init() -> *mut c_void {
4950
static NAME: &[u8] = b"getrandom\0";
5051
let name_ptr = NAME.as_ptr().cast::<libc::c_char>();
@@ -58,6 +59,7 @@ fn init() -> *mut c_void {
5859
ptr
5960
}
6061

62+
#[inline]
6163
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
6264
// Despite being only a single atomic variable, we still cannot always use
6365
// Ordering::Relaxed, as we need to make sure a successful call to `init`

src/backends/rdrand.rs

+3
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ unsafe fn rdrand_u64() -> Option<u64> {
147147
Some((u64::from(a) << 32) | u64::from(b))
148148
}
149149

150+
#[inline]
150151
pub fn inner_u32() -> Result<u32, Error> {
151152
if !RDRAND_GOOD.unsync_init(is_rdrand_good) {
152153
return Err(Error::NO_RDRAND);
@@ -155,6 +156,7 @@ pub fn inner_u32() -> Result<u32, Error> {
155156
unsafe { rdrand_u32() }.ok_or(Error::FAILED_RDRAND)
156157
}
157158

159+
#[inline]
158160
pub fn inner_u64() -> Result<u64, Error> {
159161
if !RDRAND_GOOD.unsync_init(is_rdrand_good) {
160162
return Err(Error::NO_RDRAND);
@@ -163,6 +165,7 @@ pub fn inner_u64() -> Result<u64, Error> {
163165
unsafe { rdrand_u64() }.ok_or(Error::FAILED_RDRAND)
164166
}
165167

168+
#[inline]
166169
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
167170
if !RDRAND_GOOD.unsync_init(is_rdrand_good) {
168171
return Err(Error::NO_RDRAND);

src/backends/rndr.rs

+3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ fn is_rndr_available() -> bool {
108108
}
109109
}
110110

111+
#[inline]
111112
pub fn inner_u32() -> Result<u32, Error> {
112113
if !is_rndr_available() {
113114
return Err(Error::RNDR_NOT_AVAILABLE);
@@ -117,6 +118,7 @@ pub fn inner_u32() -> Result<u32, Error> {
117118
res.map(truncate).ok_or(Error::RNDR_FAILURE)
118119
}
119120

121+
#[inline]
120122
pub fn inner_u64() -> Result<u64, Error> {
121123
if !is_rndr_available() {
122124
return Err(Error::RNDR_NOT_AVAILABLE);
@@ -126,6 +128,7 @@ pub fn inner_u64() -> Result<u64, Error> {
126128
res.ok_or(Error::RNDR_FAILURE)
127129
}
128130

131+
#[inline]
129132
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
130133
if !is_rndr_available() {
131134
return Err(Error::RNDR_NOT_AVAILABLE);

src/backends/solaris.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mod util_libc;
2222

2323
const MAX_BYTES: usize = 1024;
2424

25+
#[inline]
2526
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2627
for chunk in dest.chunks_mut(MAX_BYTES) {
2728
let ptr = chunk.as_mut_ptr().cast::<c_void>();

src/backends/solid.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88
pub fn SOLID_RNG_SampleRandomBytes(buffer: *mut u8, length: usize) -> i32;
99
}
1010

11+
#[inline]
1112
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1213
let ret = unsafe { SOLID_RNG_SampleRandomBytes(dest.as_mut_ptr().cast::<u8>(), dest.len()) };
1314
if ret >= 0 {

src/backends/use_file.rs

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const FD_ONGOING_INIT: libc::c_int = -2;
4040
// `Ordering::Acquire` to synchronize with it.
4141
static FD: AtomicI32 = AtomicI32::new(FD_UNINIT);
4242

43+
#[inline]
4344
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
4445
let mut fd = FD.load(Ordering::Acquire);
4546
if fd == FD_UNINIT || fd == FD_ONGOING_INIT {
@@ -77,6 +78,7 @@ fn open_readonly(path: &[u8]) -> Result<libc::c_int, Error> {
7778
}
7879

7980
#[cold]
81+
#[inline(never)]
8082
fn open_or_wait() -> Result<libc::c_int, Error> {
8183
loop {
8284
match FD.load(Ordering::Acquire) {

src/backends/vxworks.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,25 @@ mod util_libc;
1111

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

14+
static RNG_INIT: AtomicBool = AtomicBool::new(false);
15+
16+
#[cold]
17+
fn init() -> Result<(), Error> {
18+
let ret = unsafe { libc::randSecure() };
19+
match ret.cmp(&0) {
20+
Greater => RNG_INIT.store(true, Relaxed),
21+
Equal => unsafe {
22+
libc::usleep(10);
23+
},
24+
Less => return Err(Error::VXWORKS_RAND_SECURE),
25+
}
26+
Ok(())
27+
}
28+
29+
#[inline]
1430
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
15-
static RNG_INIT: AtomicBool = AtomicBool::new(false);
1631
while !RNG_INIT.load(Relaxed) {
17-
let ret = unsafe { libc::randSecure() };
18-
match ret.cmp(&0) {
19-
Greater => {
20-
RNG_INIT.store(true, Relaxed);
21-
break;
22-
}
23-
Equal => unsafe {
24-
libc::usleep(10);
25-
},
26-
Less => return Err(Error::VXWORKS_RAND_SECURE),
27-
}
32+
init()?;
2833
}
2934

3035
// Prevent overflow of i32

src/backends/wasi_p1.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extern "C" {
1111
fn random_get(arg0: i32, arg1: i32) -> i32;
1212
}
1313

14+
#[inline]
1415
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1516
// Based on the wasi code:
1617
// https://docs.rs/wasi/0.11.0+wasi-snapshot-preview1/src/wasi/lib_generated.rs.html#2046-2062

src/backends/wasi_p2.rs

+3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ use crate::Error;
33
use core::mem::MaybeUninit;
44
use wasi::random::random::get_random_u64;
55

6+
#[inline]
67
pub fn inner_u32() -> Result<u32, Error> {
78
let val = get_random_u64();
89
Ok(crate::util::truncate(val))
910
}
1011

12+
#[inline]
1113
pub fn inner_u64() -> Result<u64, Error> {
1214
Ok(get_random_u64())
1315
}
1416

17+
#[inline]
1518
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1619
use core::ptr::copy_nonoverlapping;
1720
use wasi::random::random::get_random_u64;

src/backends/wasm_js.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
1414
const MAX_BUFFER_SIZE: usize = 65536;
1515

1616
#[cfg(not(target_feature = "atomics"))]
17+
#[inline]
1718
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1819
for chunk in dest.chunks_mut(MAX_BUFFER_SIZE) {
1920
if get_random_values(chunk).is_err() {

src/backends/windows.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub use crate::util::{inner_u32, inner_u64};
3434
// https://github.com/microsoft/windows-rs/blob/0.60.0/crates/libs/targets/src/lib.rs
3535
windows_targets::link!("bcryptprimitives.dll" "system" fn ProcessPrng(pbdata: *mut u8, cbdata: usize) -> i32);
3636

37+
#[inline]
3738
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
3839
let result = unsafe { ProcessPrng(dest.as_mut_ptr().cast::<u8>(), dest.len()) };
3940
// Since Windows 10, calls to the user-mode RNG are guaranteed to never

src/backends/windows7.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extern "system" {
2525
type BOOLEAN = u8;
2626
const TRUE: BOOLEAN = 1u8;
2727

28+
#[inline]
2829
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2930
// Prevent overflow of u32
3031
let chunk_size = usize::try_from(i32::MAX).expect("Windows does not support 16-bit targets");

src/util.rs

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ fn ptr_from_ref<T: ?Sized>(r: &T) -> *const T {
4949
}
5050

5151
/// Default implementation of `inner_u32` on top of `fill_uninit`
52+
#[inline]
5253
pub fn inner_u32() -> Result<u32, Error> {
5354
let mut res = MaybeUninit::<u32>::uninit();
5455
// SAFETY: the created slice has the same size as `res`
@@ -63,6 +64,7 @@ pub fn inner_u32() -> Result<u32, Error> {
6364
}
6465

6566
/// Default implementation of `inner_u64` on top of `fill_uninit`
67+
#[inline]
6668
pub fn inner_u64() -> Result<u64, Error> {
6769
let mut res = MaybeUninit::<u64>::uninit();
6870
// SAFETY: the created slice has the same size as `res`

0 commit comments

Comments
 (0)