Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two small soundness and robustness fixes, plus a fuzz harness typo.
AVX2 entry points
With
simd+benchmark,fdct_avx2andRgbImageAVX2(and the BGR/RGBA variants) are safe public items, but they ran AVX2 instructions without checking the CPU. Calling them on a machine without AVX2 is undefined behavior reached from safe code. The criterion benches call them this way.fdct_avx2is now itself#[target_feature(enable = "avx2")], so calling it from a non-AVX2 context requiresunsafe.AVX2Operations::fdctcalls it under aSAFETYnote: the encoder only selectsAVX2Operationsafteris_x86_feature_detected!("avx2"). The internal dispatch is unchanged and costs nothing extra.assert!AVX2 support infill_buffers, once per row.ImageBuffer::fill_buffersis a safe trait method, so it can't be markedunsafe.Input length on 32-bit targets
Encoder::encodecheckeddata.len() < width * height * bppwith plainusizemath. On 32-bit targets such as wasm32, 65535 × 65535 × 4 overflows. Debug builds panic on the multiply. Release builds wrap to a small number, so too-short data passes the check and the encoder panics later on a slice index. The product is now computed withchecked_muland saturates, so the call returnsBadImageData.Fuzz target
encode_rgb_custom_q_tablebuilt each table value witha as u16 + (b as u16) << 8, which parses as(a + b) << 8and drops the low byte. It now usesu16::from_le_bytes.Tested with
cargo test --features simd, asimd,benchmarkbuild, awasm32-unknown-unknowncheck, and acargo checkof the fuzz crate. The 32-bit overflow path itself was not run.