Skip to content

Commit f6689ef

Browse files
committed
rdrand: remove unused unsafe
In edition 2024 functions annotated with `target_feature(enable = "..")` are unsafe to call from contexts not so annotated, and otherwise safe. This is why the unsafe block around `rdrand_step` was considered unused by rustc. Thus carry this through and make `rdrand` itself safe (when called from an annotated context) and remove all the newly unused unsafe blocks. Link: https://doc.rust-lang.org/reference/attributes/codegen.html#r-attributes.codegen.target_feature.safety-restrictions.
1 parent 658bb1a commit f6689ef

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/backends/rdrand.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ static RDRAND_GOOD: lazy::LazyBool = lazy::LazyBool::new();
2828
const RETRY_LIMIT: usize = 10;
2929

3030
#[target_feature(enable = "rdrand")]
31-
#[cfg_attr(target_os = "uefi", allow(unused_unsafe))] // HACK: Rust lint gives false positive on uefi
32-
unsafe fn rdrand() -> Option<Word> {
31+
fn rdrand() -> Option<Word> {
3332
for _ in 0..RETRY_LIMIT {
3433
let mut val = 0;
35-
if unsafe { rdrand_step(&mut val) } == 1 {
34+
if rdrand_step(&mut val) == 1 {
3635
return Some(val);
3736
}
3837
}
@@ -54,7 +53,7 @@ unsafe fn self_test() -> bool {
5453
let mut prev = Word::MAX;
5554
let mut fails = 0;
5655
for _ in 0..8 {
57-
match unsafe { rdrand() } {
56+
match rdrand() {
5857
Some(val) if val == prev => fails += 1,
5958
Some(val) => prev = val,
6059
None => return false,
@@ -109,14 +108,14 @@ unsafe fn rdrand_exact(dest: &mut [MaybeUninit<u8>]) -> Option<()> {
109108
// calls to memcpy to be elided by the compiler.
110109
let mut chunks = dest.chunks_exact_mut(size_of::<Word>());
111110
for chunk in chunks.by_ref() {
112-
let src = unsafe { rdrand() }?.to_ne_bytes();
111+
let src = rdrand()?.to_ne_bytes();
113112
chunk.copy_from_slice(slice_as_uninit(&src));
114113
}
115114

116115
let tail = chunks.into_remainder();
117116
let n = tail.len();
118117
if n > 0 {
119-
let src = unsafe { rdrand() }?.to_ne_bytes();
118+
let src = rdrand()?.to_ne_bytes();
120119
tail.copy_from_slice(slice_as_uninit(&src[..n]));
121120
}
122121
Some(())
@@ -125,26 +124,26 @@ unsafe fn rdrand_exact(dest: &mut [MaybeUninit<u8>]) -> Option<()> {
125124
#[cfg(target_arch = "x86_64")]
126125
#[target_feature(enable = "rdrand")]
127126
unsafe fn rdrand_u32() -> Option<u32> {
128-
unsafe { rdrand() }.map(crate::util::truncate)
127+
rdrand().map(crate::util::truncate)
129128
}
130129

131130
#[cfg(target_arch = "x86_64")]
132131
#[target_feature(enable = "rdrand")]
133132
unsafe fn rdrand_u64() -> Option<u64> {
134-
unsafe { rdrand() }
133+
rdrand()
135134
}
136135

137136
#[cfg(target_arch = "x86")]
138137
#[target_feature(enable = "rdrand")]
139138
unsafe fn rdrand_u32() -> Option<u32> {
140-
unsafe { rdrand() }
139+
rdrand()
141140
}
142141

143142
#[cfg(target_arch = "x86")]
144143
#[target_feature(enable = "rdrand")]
145144
unsafe fn rdrand_u64() -> Option<u64> {
146-
let a = unsafe { rdrand() }?;
147-
let b = unsafe { rdrand() }?;
145+
let a = rdrand()?;
146+
let b = rdrand()?;
148147
Some((u64::from(a) << 32) | u64::from(b))
149148
}
150149

0 commit comments

Comments
 (0)