Skip to content

Fuzz the output transformations in the decode target - #734

Open
rowanz wants to merge 1 commit into
image-rs:masterfrom
rowanz:fuzz/decode-with-transformations
Open

rowanz wants to merge 1 commit into
image-rs:masterfrom
rowanz:fuzz/decode-with-transformations

Conversation

@rowanz

@rowanz rowanz commented Nov 22, 2026 •

Copy link
Copy Markdown

Problem

The three fuzz targets (decode, buf_independent and roundtrip) decode with Transformations::IDENTITY, the default of Decoder::new and Decoder::new_with_limits. Under IDENTITY, create_transform_fn returns copy_row for every image. The other row transformations in src/decoder/transform (palette, tRNS and bit-depth expansion, and STRIP_16) are selected only with EXPAND, ALPHA or STRIP_16, so none of the three targets reaches them. They are the targets that the OSS-Fuzz project builds (projects/image-png/build.sh), which CIFuzz uses too. image decodes with EXPAND.

The out-of-bounds panic that #682 fixed was in the palette expansion (transform/palette.rs), which these targets cannot reach.

Repro

Line coverage of the decode target over the committed corpus (fuzz/corpus/decode, 1,210 files), on master (b2f10c6):

cd fuzz
host=$(rustc +nightly -vV | sed -n 's/^host: //p')
cargo +nightly fuzz coverage --fuzz-dir . --target-dir target-cov decode corpus/decode
"$(rustc +nightly --print sysroot)/lib/rustlib/$host/bin/llvm-cov" report \
  "target-cov/$host/release/decode" -instr-profile=coverage/decode/coverage.profdata
File Lines covered on master With this change
src/decoder/transform/palette.rs 0 of 69 54 of 69
src/decoder/transform.rs 19 of 122 112 of 122
src/decoder/mod.rs 244 of 354 264 of 354

I also ran the target in one campaign for each checkout: 132 libFuzzer invocations (88 without a sanitizer, 44 with AddressSanitizer), each with a budget of 25 minutes, seeded with fuzz/corpus/decode and the PNG files under tests/ of at most 16,384 bytes:

Checkout Invocations that saved a crash Saved inputs
0.18.1 (2260b15) 90 of 132 449; each one is the panic that #682 fixed (transform/palette.rs:74)
master (b2f10c6) 0 of 132 none

A separate campaign with the first version of this PR, which decoded each input six times, found that panic on 0.18.1 in 25 of 132 invocations.

Fix

For each nonempty input, the last byte selects one of the six distinct combinations of EXPAND, ALPHA and STRIP_16 (ALPHA implies EXPAND), and the target passes the whole input to the decoder one time. The corpus needs no conversion. The target takes the buffer size from output_buffer_size, because the expanded output can be larger than raw_bytes; the 5 MB cap on that buffer stays.

A complete PNG file ends with the byte 0x82 of the IEND CRC, which selects ALPHA. The corpora hold all six combinations. In the order of the array in the target, OSS-Fuzz's public corpus of the target (7,993 files) has 2,855, 801, 983, 1,013, 958 and 1,383 files for them, and fuzz/corpus/decode has 467, 96, 100, 253, 217 and 77. To replay a saved input, use its bytes unchanged.

On an aarch64 machine with AddressSanitizer, one pass over that public corpus (decode -runs=0) took about as long as on master. The times are the median of three passes, and cov and ft are libFuzzer's numbers at the end of the pass:

Target Time cov ft
master 35.7 s 2,689 15,906
this change 35.4 s 2,958 16,946
six decodes for each input 98.9 s 3,029 14,159

The last row is that first version of this PR, which decoded each input under all six combinations. Over that corpus it covers the same lines of src/ as this version (2,401 of 4,732 in llvm-cov report). Over fuzz/corpus/decode it covers 66 of the 69 lines of palette.rs, where this version covers 54. In one comparison on master (24 invocations of each target with a five-minute budget, no sanitizer, the seeds of the campaigns above), the pooled corpora covered 67 of those 69 lines with this version and 66 with six decodes.

I measured the first version because of its CIFuzz run (https://github.com/image-rs/image-png/actions/runs/44976672754), which got through the corpus at 68 executions per second. The CIFuzz run of this version (https://github.com/image-rs/image-png/actions/runs/44981025503) had the same corpus (7,993 files, 75,663,893 bytes) and got through it at 194 executions per second. With a 120-second budget, the six-decode run recorded 8,937 total executions and this version recorded 27,121. The nine CIFuzz runs before these two that ran decode got 194 to 486 executions per second, on other commits and on the corpus of other days.

The three fuzz targets decode with `Transformations::IDENTITY`, the
default of `Decoder::new` and `Decoder::new_with_limits`. Under
`IDENTITY`, `create_transform_fn` returns `copy_row` for every image.
The other row transformations in `src/decoder/transform` are selected
only with `EXPAND`, `ALPHA` or `STRIP_16`, so none of the targets
reaches them: over `fuzz/corpus/decode`, the `decode` target covers 0 of
69 lines of `transform/palette.rs` and 19 of 122 lines of
`transform.rs`. The panic that image-rs#682 fixed was in the palette expansion.

For each nonempty input, the last byte now selects one of the six
distinct combinations of the three flags (`ALPHA` implies `EXPAND`), and
`decode` passes the whole input to the decoder one time, so the corpus
needs no conversion. The target takes the buffer size from
`output_buffer_size`, because the expanded output can be larger than
`raw_bytes`. Over the same corpus it covers 54 of 69 and 112 of 122
lines.
@rowanz
rowanz force-pushed the fuzz/decode-with-transformations branch from c1e5109 to 3fcf211 Compare November 22, 2026 03:31
@rowanz rowanz changed the title Fuzz decode under every output transformation Fuzz the output transformations in the decode target Nov 22, 2026
@rowanz
rowanz marked this pull request as ready for review November 22, 2026 03:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant