Repository navigation
Description
What happens
UBSan fires inside the fixed-point polyphase on the l3-nonstandard-big-iscf conformance vector:
minimp3.h:2699:29: runtime error: signed integer overflow: -838085126 + -1518500249 cannot be represented in type 'int32_t'
minimp3.h:2700:29: runtime error: signed integer overflow: 1326454660 - -838085126 cannot be represented in type 'int32_t'
Cause
mp3d_synth_pair, the fixed-point one:
a = (int64_t)(z[14*64] - z[ 0]) * 29;
a += (int64_t)(z[ 1*64] + z[13*64]) * 213;The cast applies to the result of the subtraction, not to its operands. So z[14*64] - z[0] is evaluated in int32_t and only then widened. With large sample values the int32 arithmetic overflows, which is undefined behaviour -- the compiler is entitled to assume it cannot happen, so the generated code for a malformed stream is not merely wrong but unconstrained.
This is FastLED's code, not upstream's. The int32 variant is a + block in FASTLED.patch, added for the fixed-point port. Upstream's is float:
float a;
a = (z[14*64] - z[ 0]) * 29; /* no integer overflow possible */Fix
Widen the operands before the arithmetic, in the seven add/sub pairs of the first accumulator:
a = ((int64_t)z[14*64] - (int64_t)z[ 0]) * 29;Widening rather than saturating is the right call here. The result feeds an int64_t accumulator, so there is headroom for the true difference, and preserving it matches the float path's semantics -- mp3d_add_sat/mp3d_sub_sat exist for values that must stay in int32, which these do not. The second accumulator multiplies single z[] terms with no add or subtract and needs no change.
Why nothing caught it
Third instance of the same pattern in this codebase, so it is worth naming rather than just fixing.
The differential fuzzer runs under ASan and UBSan and is clean -- over the vendored corpus. Normal audio never drives the polyphase inputs large enough to overflow int32; l3-nonstandard-big-iscf does, because it is a deliberately malformed intensity-stereo scalefactor stream that pushes the pipeline to 230767 where every other vector stays under 4.
The other two instances: #4127 (dequant clamped to 1.0 on a headroom measurement taken over 2 of 83 vectors) and the conformance gate itself, which measured PSNR but not decoded length and so scored 115.61 dB on a vector that dropped 27904 samples. In all three the measurement was sound and the scope it was taken over was too narrow.
Severity
Low likelihood, non-trivial consequence. It needs a malformed or hostile stream, not ordinary audio. But an MP3 decoder is exactly the kind of component that gets pointed at untrusted input, and UB is not a "wrong output" bug -- it is a "no guarantees at all" bug, and it lives in the hottest kernel in the decoder.
Acceptance criteria
- UBSan clean on
l3-nonstandard-big-iscfand the rest of the conformance suite. - Decoded PCM byte-identical on every vector that does not overflow, since widening changes nothing where int32 was already sufficient.
- The conformance runner gains a sanitizer pass so this class of finding is caught by CI rather than by hand.
Found by running the conformance suite added in #4127 under ASan and UBSan.
Refs #4127
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
fix(mp3): widen operands before the polyphase add/sub (#4133)
fix(mp3): count widened Q-format multiplies, and fix the GEP exclusion
perf(mp3): remove the polyphase UB without the 32-bit cost
fix(mp3): clear VBR state between streams, and bound the length excep…
fix(mp3): stop clamping dequantised samples to 1.0 (FastLED#4127) (Fa…
feat(mp3): verify the fixed-point decoder on hardware via AutoResearch
feat(mp3): verify the fixed-point decoder on hardware via AutoResearch
perf(mp3): the DCT saturation defends exactly one malformed vector
perf(mp3): measure both decoders on the C6 -- the gap is 4x, not 1.59x
perf(mp3): drop the DCT/IMDCT butterfly clamp -- 28% faster on the C6
feat(mp3): verify the fixed-point decoder on hardware via AutoResearch (