Skip to content

fix: reject corrupt JPEG Huffman data, bound coefficient math and fix two data races - #1357

Open
giuliowaitforitdavide wants to merge 3 commits into
antirez:mainfrom
giuliowaitforitdavide:fix/decoder-and-race-findings
Open

giuliowaitforitdavide wants to merge 3 commits into
antirez:mainfrom
giuliowaitforitdavide:fix/decoder-and-race-findings

Conversation

@giuliowaitforitdavide

Copy link
Copy Markdown
Contributor

Closes #1356

Fixes items 2-6 of #1356: undefined behavior in the JPEG decoder on corrupt
input, and two data races. Item 1, the out-of-bounds luma read, is left to
#1112.

Changes

third_party/iris/jpeg.h:

  • Every Huffman table starts with no codes, so a scan that names a table the
    file never defines fails to decode instead of indexing values[-1].
  • Oversubscribed Huffman tables are rejected when they are built, the same
    check libjpeg makes.
  • jpeg_get_bits rejects reads longer than 16 bits, the most JPEG ever codes.
  • Coefficients and the DC predictor are kept 16-bit, as libjpeg's JCOEF and
    this decoder's progressive path already do, and the IDCT uses 64-bit
    intermediates, like libjpeg's JLONG.

ds4_tp.c: ds4_tp_big_gate_exchange reads DS4_TP_BIG_GATE_DEBUG and
DS4_TP_BIG_GATE_ARRIVAL_MS on each call instead of caching them in function
statics. The arrival floor now comes from the calling tp's own gate timeout.

ds4_metal.m: the queue keepalive stop flag uses __atomic loads and stores
instead of volatile.

tests/test_image_decode.c: four JPEGs built in memory cover an oversubscribed
table, an undefined table, a DC category of 17 and extreme coefficients with
16-bit quantizers.

Testing

On an M3 Max (macOS, Metal) and in Ubuntu 26.04 containers (arm64, and x86-64
under Rosetta):

  • tests/test_image_decode: passes. On main, three of the new checks fail;
    the extreme-coefficient check fails only under UBSan.
  • Decoded pixel fingerprints are identical to main on 309 valid JPEG and PNG
    files: Pillow-generated baseline and progressive images at several
    qualities and subsamplings, plus the repository fixtures.
  • Release builds have no warnings: make on macOS, make cpu on Linux.
  • ThreadSanitizer: no reports in test_tp_commands and test_tp_tcp (macOS
    and Linux), or in test-qwen4-ssd-experts and
    test-qwen4-resident-arithmetic (macOS).
  • libFuzzer on ds4_image_decode_memory with these fixes and the jpeg.h
    change from Upsample a subsampled JPEG luma plane instead of reading past it #1112: eight minutes under ASan and under UBSan found nothing.

No model was used. Distributed inference over a real network, CUDA and ROCm
were not run; the ds4_tp.c change only affects how the arrival timeout is
computed.

Conflicts

tests/test_image_decode.c conflicts with #1348, #1112, #1113 and #1110, which
also add checks to that file. The conflict is only where the new functions and
the main() calls go. jpeg.h merges cleanly with #1112, #1113 and #1110.

🤖 Generated with Claude Code

Fuzzing the image decoder under UBSan found undefined behavior on corrupt
JPEGs, with identical output for valid files:

- Start every Huffman table with no codes, and reject oversubscribed
  tables when they are built. Both made the slow decode path compute a
  negative index into the symbol table.
- Reject bit reads longer than 16 bits. A malformed table could ask for
  up to 255 bits and shift past the width of an int.
- Keep coefficients and the DC predictor 16-bit, as libjpeg and the
  progressive path do, and run the IDCT on 64-bit intermediates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ds4_tp_big_gate_exchange initialized two function statics on first use
without synchronization, which ThreadSanitizer reports when two ranks
share a process, as in the transport tests. The arrival grace also took
its floor from whichever tp called first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The main thread writes the stop flag while the keepalive thread polls it.
volatile does not make that access race-free; use the __atomic builtins
the file already uses elsewhere.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@giuliowaitforitdavide
giuliowaitforitdavide force-pushed the fix/decoder-and-race-findings branch from 069aa5a to 4c7ff68 Compare November 17, 2026 12:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant