@@ -105,24 +105,25 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
105105 if !RDRAND_GOOD . unsync_init ( is_rdrand_good) {
106106 return Err ( Error :: NO_RDRAND ) ;
107107 }
108- rdrand_exact ( dest) . ok_or ( Error :: FAILED_RDRAND )
108+ // SAFETY: After this point, we know rdrand is supported.
109+ unsafe { rdrand_exact ( dest) } . ok_or ( Error :: FAILED_RDRAND )
109110}
110111
111- fn rdrand_exact ( dest : & mut [ MaybeUninit < u8 > ] ) -> Option < ( ) > {
112+ // TODO: make this function safe when we have feature(target_feature_11)
113+ #[ target_feature( enable = "rdrand" ) ]
114+ unsafe fn rdrand_exact ( dest : & mut [ MaybeUninit < u8 > ] ) -> Option < ( ) > {
112115 // We use chunks_exact_mut instead of chunks_mut as it allows almost all
113116 // calls to memcpy to be elided by the compiler.
114117 let mut chunks = dest. chunks_exact_mut ( size_of :: < usize > ( ) ) ;
115118 for chunk in chunks. by_ref ( ) {
116- // SAFETY: After this point, we know rdrand is supported, so calling
117- // rdrand is not undefined behavior.
118- let src = unsafe { rdrand ( ) } ?. to_ne_bytes ( ) ;
119+ let src = rdrand ( ) ?. to_ne_bytes ( ) ;
119120 chunk. copy_from_slice ( slice_as_uninit ( & src) ) ;
120121 }
121122
122123 let tail = chunks. into_remainder ( ) ;
123124 let n = tail. len ( ) ;
124125 if n > 0 {
125- let src = unsafe { rdrand ( ) } ?. to_ne_bytes ( ) ;
126+ let src = rdrand ( ) ?. to_ne_bytes ( ) ;
126127 tail. copy_from_slice ( slice_as_uninit ( & src[ ..n] ) ) ;
127128 }
128129 Some ( ( ) )
0 commit comments