Fuzz every mode with garbage payloads
Browse files at this commitacb0d046f725688efb8c98ca2952ead09d5d15efParent: 8bd3b2b- test
- fuzz every mode with garbage payloads
- document the PLC gap
3 changed files +59 -7
- crates/ec-opus/src/lib.rs +13 -6
- crates/ec-opus/src/packet.rs +1 -1
- crates/ec-opus/tests/conformance.rs +45 -0
crates/ec-opus/src/lib.rsmodified+13 -6
| @@ -8,17 +8,24 @@ | |||
| 8 | //! | 8 | //! |
| 9 | //! Contracts worth knowing before implementing against this crate: | 9 | //! Contracts worth knowing before implementing against this crate: |
| 10 | //! | 10 | //! |
| 11 | -//! - **The MDCT is the shared one.** CELT synthesis runs through | 11 | +//! - **The FFT is never optional.** CELT synthesis runs an `N/4`-point FFT |
| 12 | -//! [`ec_dsp::Mdct`], never a direct-form DFT. An incumbent Opus decoder in | 12 | +//! through [`ec_dsp`], never a direct-form DFT. An incumbent Opus decoder in |
| 13 | //! this product shipped its FFT behind a default-off feature and ran at 0.95x | 13 | //! this product shipped its FFT behind a default-off feature and ran at 0.95x |
| 14 | //! realtime on 5.1; there is no such switch here, and no code path that could | 14 | //! realtime on 5.1; there is no such switch here, and no code path that could |
| 15 | -//! answer to one. | 15 | +//! answer to one. (CELT's transform sizes are `15*2^k`, so [`ec_dsp::Mdct`] |
| 16 | -//! - **Malformed input is an error, never a panic.** Framing rules [R1]..[R7] | 16 | +//! itself — power-of-two only — cannot serve them; [`celt`] wraps |
| 17 | +//! [`ec_dsp::Fft`] with one radix-3/radix-5 stage instead.) | ||
| 18 | +//! - **Malformed input is an error, never a panic.** Framing rules `[R1]`..`[R7]` | ||
| 17 | //! are checked in [`packet`], and the range decoder feeds zeros past the end | 19 | //! are checked in [`packet`], and the range decoder feeds zeros past the end |
| 18 | //! of a truncated frame exactly as Section 4.1.2.1 requires. | 20 | //! of a truncated frame exactly as Section 4.1.2.1 requires. |
| 19 | //! - **Output is `f32`, interleaved, at the rate you asked for.** 48, 24, 16, | 21 | //! - **Output is `f32`, interleaved, at the rate you asked for.** 48, 24, 16, |
| 20 | -//! 12 and 8 kHz are supported; SILK's internal rate is resampled up and the | 22 | +//! 12 and 8 kHz are supported; SILK's internal rate is resampled up (by the |
| 21 | -//! sum of the two layers is resampled down once, at the end. | 23 | +//! normative resampler in [`silk`]) and CELT decimates on the way out. |
| 24 | +//! - **There is no packet loss concealment.** A frame of one byte or none — | ||
| 25 | +//! a dropped or DTX frame — decodes to silence, where the reference | ||
| 26 | +//! extrapolates from the previous frame. This decoder is fed whole packets by | ||
| 27 | +//! a container, never a gap, so the concealment would be dead weight; a | ||
| 28 | +//! caller that needs it (an RTP receiver) has to add it. | ||
| 22 | //! | 29 | //! |
| 23 | //! No unsafe, no allocation on the per-frame path beyond the decoder's own | 30 | //! No unsafe, no allocation on the per-frame path beyond the decoder's own |
| 24 | //! buffers, no dependencies outside the family. | 31 | //! buffers, no dependencies outside the family. |
crates/ec-opus/src/packet.rsmodified+1 -1
| @@ -1,7 +1,7 @@ | |||
| 1 | //! Opus packet framing: the TOC byte, codes 0-3, and the self-delimiting | 1 | //! Opus packet framing: the TOC byte, codes 0-3, and the self-delimiting |
| 2 | //! variant (RFC 6716, Section 3 and Appendix B). | 2 | //! variant (RFC 6716, Section 3 and Appendix B). |
| 3 | //! | 3 | //! |
| 4 | -//! Every rule marked [R1]..[R7] in Section 3.4 is enforced here and reported as | 4 | +//! Every rule marked `[R1]`..`[R7]` in Section 3.4 is enforced here and reported as |
| 5 | //! [`Error::Corrupt`] — a malformed packet is rejected at the framing layer, so | 5 | //! [`Error::Corrupt`] — a malformed packet is rejected at the framing layer, so |
| 6 | //! nothing downstream ever sees a frame it cannot account for. | 6 | //! nothing downstream ever sees a frame it cannot account for. |
| 7 | //! | 7 | //! |
crates/ec-opus/tests/conformance.rsmodified+45 -0
| @@ -723,3 +723,48 @@ fn real_library_sweep() { | |||
| 723 | ); | 723 | ); |
| 724 | } | 724 | } |
| 725 | } | 725 | } |
| 726 | + | ||
| 727 | +/// Malformed input must come back as an error, never a panic: the decoders | ||
| 728 | +/// index tables with values the bitstream chooses, so this throws well-formed | ||
| 729 | +/// TOC bytes at random payloads to make sure every mode is actually entered. | ||
| 730 | +#[test] | ||
| 731 | +fn garbage_payloads_never_panic() { | ||
| 732 | + let mut state = 0x12345678u32; | ||
| 733 | + let mut rand = move || { | ||
| 734 | + state ^= state << 13; | ||
| 735 | + state ^= state >> 17; | ||
| 736 | + state ^= state << 5; | ||
| 737 | + state | ||
| 738 | + }; | ||
| 739 | + let mut dec = Decoder::new(48000, 2).unwrap(); | ||
| 740 | + let mut ms = MultistreamDecoder::with_rate(48000, 4, 2, &[0, 4, 1, 2, 3, 5]); | ||
| 741 | + let mut out = vec![0.0f32; 5760 * 6]; | ||
| 742 | + for config in 0..32u8 { | ||
| 743 | + for stereo in 0..2u8 { | ||
| 744 | + for code in 0..4u8 { | ||
| 745 | + let toc = (config << 3) | (stereo << 2) | code; | ||
| 746 | + for trial in 0..40 { | ||
| 747 | + let len = 2 + (rand() as usize % 400); | ||
| 748 | + let mut packet = vec![toc]; | ||
| 749 | + for _ in 0..len { | ||
| 750 | + packet.push(rand() as u8); | ||
| 751 | + } | ||
| 752 | + if trial % 2 == 0 { | ||
| 753 | + dec.reset(); | ||
| 754 | + } | ||
| 755 | + let _ = dec.decode_float(&packet, &mut out); | ||
| 756 | + let _ = ms.decode_float(&packet, &mut out); | ||
| 757 | + } | ||
| 758 | + } | ||
| 759 | + } | ||
| 760 | + } | ||
| 761 | + // Truncations of a real packet, which exercise the mid-frame paths. | ||
| 762 | + let path = vectors_dir().join("testvector10.bit"); | ||
| 763 | + if path.exists() { | ||
| 764 | + for p in read_vector(&path).iter().take(200) { | ||
| 765 | + for cut in 1..p.payload.len().min(64) { | ||
| 766 | + let _ = dec.decode_float(&p.payload[..cut], &mut out); | ||
| 767 | + } | ||
| 768 | + } | ||
| 769 | + } | ||
| 770 | +} | ||