From 583d1fef1da76707a5cebd149121530170da340b Mon Sep 17 00:00:00 2001 From: Markus Mayer Date: Mon, 23 Nov 2026 22:39:20 +0100 Subject: [PATCH] Guard AVX2 entry points and the input length check fdct_avx2 and RgbImageAVX2 (and its siblings) are reachable as safe public items with the `benchmark` feature, but ran AVX2 instructions without checking the CPU, which is undefined behavior on machines without AVX2. fdct_avx2 is now itself #[target_feature(enable = "avx2")], so calls from a non-AVX2 context require `unsafe` and the encoder's dispatch states why it is sound. The AVX2 image buffers assert AVX2 support in fill_buffers, once per row. Encoder::encode computed width * height * bytes_per_pixel in usize. On 32-bit targets (e.g. wasm32) large dimensions wrap in release builds, so too-short data could pass the check and panic later on a slice index. The product is now checked and saturates, which returns BadImageData instead. Also fix operator precedence in the custom quantization table fuzz target: `a + b << 8` built `(a + b) << 8` and lost the low byte. --- fuzz/fuzz_targets/encode_rgb_custom_q_table.rs | 2 +- src/avx2.rs | 3 ++- src/avx2/fdct.rs | 16 +++++++--------- src/avx2/ycbcr.rs | 6 ++++++ src/encoder.rs | 6 +++++- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/fuzz/fuzz_targets/encode_rgb_custom_q_table.rs b/fuzz/fuzz_targets/encode_rgb_custom_q_table.rs index 02ce621..d77793e 100644 --- a/fuzz/fuzz_targets/encode_rgb_custom_q_table.rs +++ b/fuzz/fuzz_targets/encode_rgb_custom_q_table.rs @@ -17,7 +17,7 @@ fuzz_target!(|data: &[u8]| { if width >0 && width < u16::MAX && height >0 && height < u16::MAX { let mut table = [0;64]; for i in 0..64 { - let v = q_table[i*2] as u16 + (q_table[i*2+1] as u16) << 8; + let v = u16::from_le_bytes([q_table[i * 2], q_table[i * 2 + 1]]); table[i] = v.max(1); } diff --git a/src/avx2.rs b/src/avx2.rs index f8eb103..8f1633b 100644 --- a/src/avx2.rs +++ b/src/avx2.rs @@ -10,6 +10,7 @@ pub(crate) struct AVX2Operations; impl Operations for AVX2Operations { #[inline(always)] fn fdct(data: &mut AlignedBlock) { - fdct_avx2(data); + // SAFETY: the encoder only selects AVX2Operations after is_x86_feature_detected!("avx2"). + unsafe { fdct_avx2(data) }; } } diff --git a/src/avx2/fdct.rs b/src/avx2/fdct.rs index c14f9be..c0d7667 100644 --- a/src/avx2/fdct.rs +++ b/src/avx2/fdct.rs @@ -58,15 +58,12 @@ const F_3_072: i16 = 25172; const DESCALE_P1: i32 = CONST_BITS - PASS1_BITS; const DESCALE_P2: i32 = CONST_BITS + PASS1_BITS; -#[inline(always)] -pub fn fdct_avx2(data: &mut AlignedBlock) { - unsafe { - fdct_avx2_internal(data); - } -} - +/// Forward DCT using AVX2. +/// +/// Callers outside an AVX2 context must check `is_x86_feature_detected!("avx2")` first, +/// which `#[target_feature]` enforces by making the call `unsafe` there. #[target_feature(enable = "avx2")] -fn fdct_avx2_internal(data: &mut AlignedBlock) { +pub fn fdct_avx2(data: &mut AlignedBlock) { #[target_feature(enable = "avx2")] #[allow(non_snake_case)] #[inline] @@ -500,7 +497,8 @@ mod tests { crate::fdct::fdct_scalar(&mut expected); let mut actual = AlignedBlock::new(input); - fdct_avx2(&mut actual); + // SAFETY: AVX2 support was checked above. + unsafe { fdct_avx2(&mut actual) }; assert_eq!(actual.data, expected.data); } diff --git a/src/avx2/ycbcr.rs b/src/avx2/ycbcr.rs index 9df37f3..717ade0 100644 --- a/src/avx2/ycbcr.rs +++ b/src/avx2/ycbcr.rs @@ -140,6 +140,12 @@ macro_rules! ycbcr_image_avx2 { #[inline(always)] fn fill_buffers(&self, y: u16, buffers: &mut [Vec; 4]) { + // The type is public (benchmark feature), so a safe call must not run AVX2 blindly. + assert!( + std::is_x86_feature_detected!("avx2"), + "AVX2 is not available" + ); + // SAFETY: AVX2 support was checked above. unsafe { self.fill_buffers_avx2(y, buffers); } diff --git a/src/encoder.rs b/src/encoder.rs index 5f41217..7cf1642 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -475,7 +475,11 @@ impl Encoder { height: u16, color_type: ColorType, ) -> Result<(), EncodingError> { - let required_data_len = width as usize * height as usize * color_type.get_bytes_per_pixel(); + // Saturate so 32-bit targets report an error instead of wrapping past the check. + let required_data_len = (width as usize) + .checked_mul(height as usize) + .and_then(|n| n.checked_mul(color_type.get_bytes_per_pixel())) + .unwrap_or(usize::MAX); if data.len() < required_data_len { return Err(EncodingError::BadImageData {