diff options
| author | Jack O'Connor <[email protected]> | 2025-02-20 12:05:18 -0800 |
|---|---|---|
| committer | Jack O'Connor <[email protected]> | 2025-02-20 12:14:50 -0800 |
| commit | eccd0e5379bacc1121e3550714f5a4d5690ba776 (patch) | |
| tree | b67352944e7f7dafcc6bf288d7db43d1d7512be1 | |
| parent | f8c5f7d023fd77ef7fc6f4596407e510d3468a57 (diff) | |
add `unsafe` annotations needed for the 2024 edition
| -rw-r--r-- | src/ffi_avx2.rs | 36 | ||||
| -rw-r--r-- | src/ffi_avx512.rs | 90 | ||||
| -rw-r--r-- | src/ffi_sse2.rs | 68 | ||||
| -rw-r--r-- | src/ffi_sse41.rs | 68 |
4 files changed, 151 insertions, 111 deletions
diff --git a/src/ffi_avx2.rs b/src/ffi_avx2.rs index 33961e9..ef954bd 100644 --- a/src/ffi_avx2.rs +++ b/src/ffi_avx2.rs @@ -14,26 +14,28 @@ pub unsafe fn hash_many<const N: usize>( flags_end: u8, out: &mut [u8], ) { - // The Rust hash_many implementations do bounds checking on the `out` - // array, but the C implementations don't. Even though this is an unsafe - // function, assert the bounds here. - assert!(out.len() >= inputs.len() * OUT_LEN); - ffi::blake3_hash_many_avx2( - inputs.as_ptr() as *const *const u8, - inputs.len(), - N / BLOCK_LEN, - key.as_ptr(), - counter, - increment_counter.yes(), - flags, - flags_start, - flags_end, - out.as_mut_ptr(), - ) + unsafe { + // The Rust hash_many implementations do bounds checking on the `out` + // array, but the C implementations don't. Even though this is an unsafe + // function, assert the bounds here. + assert!(out.len() >= inputs.len() * OUT_LEN); + ffi::blake3_hash_many_avx2( + inputs.as_ptr() as *const *const u8, + inputs.len(), + N / BLOCK_LEN, + key.as_ptr(), + counter, + increment_counter.yes(), + flags, + flags_start, + flags_end, + out.as_mut_ptr(), + ) + } } pub mod ffi { - extern "C" { + unsafe extern "C" { pub fn blake3_hash_many_avx2( inputs: *const *const u8, num_inputs: usize, diff --git a/src/ffi_avx512.rs b/src/ffi_avx512.rs index afa0221..c83048e 100644 --- a/src/ffi_avx512.rs +++ b/src/ffi_avx512.rs @@ -8,7 +8,15 @@ pub unsafe fn compress_in_place( counter: u64, flags: u8, ) { - ffi::blake3_compress_in_place_avx512(cv.as_mut_ptr(), block.as_ptr(), block_len, counter, flags) + unsafe { + ffi::blake3_compress_in_place_avx512( + cv.as_mut_ptr(), + block.as_ptr(), + block_len, + counter, + flags, + ) + } } // Unsafe because this may only be called on platforms supporting AVX-512. @@ -19,16 +27,18 @@ pub unsafe fn compress_xof( counter: u64, flags: u8, ) -> [u8; 64] { - let mut out = [0u8; 64]; - ffi::blake3_compress_xof_avx512( - cv.as_ptr(), - block.as_ptr(), - block_len, - counter, - flags, - out.as_mut_ptr(), - ); - out + unsafe { + let mut out = [0u8; 64]; + ffi::blake3_compress_xof_avx512( + cv.as_ptr(), + block.as_ptr(), + block_len, + counter, + flags, + out.as_mut_ptr(), + ); + out + } } // Unsafe because this may only be called on platforms supporting AVX-512. @@ -42,22 +52,24 @@ pub unsafe fn hash_many<const N: usize>( flags_end: u8, out: &mut [u8], ) { - // The Rust hash_many implementations do bounds checking on the `out` - // array, but the C implementations don't. Even though this is an unsafe - // function, assert the bounds here. - assert!(out.len() >= inputs.len() * OUT_LEN); - ffi::blake3_hash_many_avx512( - inputs.as_ptr() as *const *const u8, - inputs.len(), - N / BLOCK_LEN, - key.as_ptr(), - counter, - increment_counter.yes(), - flags, - flags_start, - flags_end, - out.as_mut_ptr(), - ) + unsafe { + // The Rust hash_many implementations do bounds checking on the `out` + // array, but the C implementations don't. Even though this is an unsafe + // function, assert the bounds here. + assert!(out.len() >= inputs.len() * OUT_LEN); + ffi::blake3_hash_many_avx512( + inputs.as_ptr() as *const *const u8, + inputs.len(), + N / BLOCK_LEN, + key.as_ptr(), + counter, + increment_counter.yes(), + flags, + flags_start, + flags_end, + out.as_mut_ptr(), + ) + } } // Unsafe because this may only be called on platforms supporting AVX-512. @@ -70,20 +82,22 @@ pub unsafe fn xof_many( flags: u8, out: &mut [u8], ) { - debug_assert_eq!(0, out.len() % BLOCK_LEN, "whole blocks only"); - ffi::blake3_xof_many_avx512( - cv.as_ptr(), - block.as_ptr(), - block_len, - counter, - flags, - out.as_mut_ptr(), - out.len() / BLOCK_LEN, - ); + unsafe { + debug_assert_eq!(0, out.len() % BLOCK_LEN, "whole blocks only"); + ffi::blake3_xof_many_avx512( + cv.as_ptr(), + block.as_ptr(), + block_len, + counter, + flags, + out.as_mut_ptr(), + out.len() / BLOCK_LEN, + ); + } } pub mod ffi { - extern "C" { + unsafe extern "C" { pub fn blake3_compress_in_place_avx512( cv: *mut u32, block: *const u8, diff --git a/src/ffi_sse2.rs b/src/ffi_sse2.rs index 1c5da81..78f09b8 100644 --- a/src/ffi_sse2.rs +++ b/src/ffi_sse2.rs @@ -8,7 +8,15 @@ pub unsafe fn compress_in_place( counter: u64, flags: u8, ) { - ffi::blake3_compress_in_place_sse2(cv.as_mut_ptr(), block.as_ptr(), block_len, counter, flags) + unsafe { + ffi::blake3_compress_in_place_sse2( + cv.as_mut_ptr(), + block.as_ptr(), + block_len, + counter, + flags, + ) + } } // Unsafe because this may only be called on platforms supporting SSE2. @@ -19,16 +27,18 @@ pub unsafe fn compress_xof( counter: u64, flags: u8, ) -> [u8; 64] { - let mut out = [0u8; 64]; - ffi::blake3_compress_xof_sse2( - cv.as_ptr(), - block.as_ptr(), - block_len, - counter, - flags, - out.as_mut_ptr(), - ); - out + unsafe { + let mut out = [0u8; 64]; + ffi::blake3_compress_xof_sse2( + cv.as_ptr(), + block.as_ptr(), + block_len, + counter, + flags, + out.as_mut_ptr(), + ); + out + } } // Unsafe because this may only be called on platforms supporting SSE2. @@ -42,26 +52,28 @@ pub unsafe fn hash_many<const N: usize>( flags_end: u8, out: &mut [u8], ) { - // The Rust hash_many implementations do bounds checking on the `out` - // array, but the C implementations don't. Even though this is an unsafe - // function, assert the bounds here. - assert!(out.len() >= inputs.len() * OUT_LEN); - ffi::blake3_hash_many_sse2( - inputs.as_ptr() as *const *const u8, - inputs.len(), - N / BLOCK_LEN, - key.as_ptr(), - counter, - increment_counter.yes(), - flags, - flags_start, - flags_end, - out.as_mut_ptr(), - ) + unsafe { + // The Rust hash_many implementations do bounds checking on the `out` + // array, but the C implementations don't. Even though this is an unsafe + // function, assert the bounds here. + assert!(out.len() >= inputs.len() * OUT_LEN); + ffi::blake3_hash_many_sse2( + inputs.as_ptr() as *const *const u8, + inputs.len(), + N / BLOCK_LEN, + key.as_ptr(), + counter, + increment_counter.yes(), + flags, + flags_start, + flags_end, + out.as_mut_ptr(), + ) + } } pub mod ffi { - extern "C" { + unsafe extern "C" { pub fn blake3_compress_in_place_sse2( cv: *mut u32, block: *const u8, diff --git a/src/ffi_sse41.rs b/src/ffi_sse41.rs index 62989c5..77ab89a 100644 --- a/src/ffi_sse41.rs +++ b/src/ffi_sse41.rs @@ -8,7 +8,15 @@ pub unsafe fn compress_in_place( counter: u64, flags: u8, ) { - ffi::blake3_compress_in_place_sse41(cv.as_mut_ptr(), block.as_ptr(), block_len, counter, flags) + unsafe { + ffi::blake3_compress_in_place_sse41( + cv.as_mut_ptr(), + block.as_ptr(), + block_len, + counter, + flags, + ) + } } // Unsafe because this may only be called on platforms supporting SSE4.1. @@ -19,16 +27,18 @@ pub unsafe fn compress_xof( counter: u64, flags: u8, ) -> [u8; 64] { - let mut out = [0u8; 64]; - ffi::blake3_compress_xof_sse41( - cv.as_ptr(), - block.as_ptr(), - block_len, - counter, - flags, - out.as_mut_ptr(), - ); - out + unsafe { + let mut out = [0u8; 64]; + ffi::blake3_compress_xof_sse41( + cv.as_ptr(), + block.as_ptr(), + block_len, + counter, + flags, + out.as_mut_ptr(), + ); + out + } } // Unsafe because this may only be called on platforms supporting SSE4.1. @@ -42,26 +52,28 @@ pub unsafe fn hash_many<const N: usize>( flags_end: u8, out: &mut [u8], ) { - // The Rust hash_many implementations do bounds checking on the `out` - // array, but the C implementations don't. Even though this is an unsafe - // function, assert the bounds here. - assert!(out.len() >= inputs.len() * OUT_LEN); - ffi::blake3_hash_many_sse41( - inputs.as_ptr() as *const *const u8, - inputs.len(), - N / BLOCK_LEN, - key.as_ptr(), - counter, - increment_counter.yes(), - flags, - flags_start, - flags_end, - out.as_mut_ptr(), - ) + unsafe { + // The Rust hash_many implementations do bounds checking on the `out` + // array, but the C implementations don't. Even though this is an unsafe + // function, assert the bounds here. + assert!(out.len() >= inputs.len() * OUT_LEN); + ffi::blake3_hash_many_sse41( + inputs.as_ptr() as *const *const u8, + inputs.len(), + N / BLOCK_LEN, + key.as_ptr(), + counter, + increment_counter.yes(), + flags, + flags_start, + flags_end, + out.as_mut_ptr(), + ) + } } pub mod ffi { - extern "C" { + unsafe extern "C" { pub fn blake3_compress_in_place_sse41( cv: *mut u32, block: *const u8, |
