Skip to content

arm: compute the NEON palette expansion offsets in size_t - #975

Open
itsgaldoron wants to merge 1 commit into
pnggroup:libpng16from
itsgaldoron:libpng16-palette-neon-offsets-size-t
Open

itsgaldoron wants to merge 1 commit into
pnggroup:libpng16from
itsgaldoron:libpng16-palette-neon-offsets-size-t

Conversation

@itsgaldoron

@itsgaldoron itsgaldoron commented Nov 4, 2026 •

Copy link
Copy Markdown

png_do_expand_palette_rgba8_neon and png_do_expand_palette_rgb8_neon (arm/palette_neon_intrinsics.c) compute the destination offset of each expanded chunk and the hand-off back to the scalar fallback as

png_bytep dp = *ddp - i * 4;   /* RGBA, line 85 */
*ddp = *ddp - i * 4;           /* RGBA hand-off, line 99 */
png_bytep dp = *ddp - i * 3;   /* RGB, line 125 */
*ddp = *ddp - i * 3;           /* RGB hand-off, line 143 */

with i a png_uint_32. Each product is evaluated in 32 bits and wraps at i >= 0x40000000 (RGBA) / i >= 0x55555556 (RGB) — i.e. at row widths above 2^30 (RGBA) / ~1.43 * 2^30 (RGB). Such widths are legal inputs: the IHDR check is width <= PNG_UINT_31_MAX, and applications raise the PNG_USER_WIDTH_MAX default of 1,000,000 through the documented png_set_user_limits().

site (at ac0c35324) expression input wraps to true
arm/palette_neon_intrinsics.c:85,99 i * 4 w = 0x40000001 (hand-off, i = 0x40000000) 0 4,294,967,296
arm/palette_neon_intrinsics.c:85,99 i * 4 w = 0x40000004 (last chunk, i = 0x40000000) 0 4,294,967,296
arm/palette_neon_intrinsics.c:85,99 i * 4 w = 0x7FFFFFFF (last chunk, i = 0x7FFFFFF8) 4,294,967,264 8,589,934,560
arm/palette_neon_intrinsics.c:125,143 i * 3 w = 0x5555555E (hand-off, i = 0x55555558) 8 4,294,967,304
arm/palette_neon_intrinsics.c:125,143 i * 3 w = 0x55555566 (last chunk, i = 0x55555558) 8 4,294,967,304
arm/palette_neon_intrinsics.c:125,143 i * 3 w = 0x7FFFFFFF (last chunk, i = 0x7FFFFFF8) 2,147,483,600 6,442,450,896

(The wrap boundary is sharp: at w = 0x40000000 the last chunk product 4*0x3FFFFFFC still fits and the hand-off product wraps to 0, which the zero-iteration scalar tail never dereferences — that cell decodes clean, and so does the RGB boundary cell w = 0x55555550. Ground truth evaluated in __int128.)

Changes: compute the six palette-offset products in this file in size_t — the spelling the C fallback png_do_expand_palette in pngrtran.c already uses (dp = row + ((size_t)row_width << 2) - 1, which is why the scalar path is not affected) — and add a regression check to contrib/libtests/pnggetset.c. The two riffle-loop products (png_riffle_palette_neon, i < 256) cannot wrap and are widened only so that every palette offset product in the file is computed in size_t.

What is and is not claimed. On ARM NEON builds, a palette image with a width past the wrap silently corrupts the expanded row: measured at w = 0x40000001 (RGBA), the first pixel keeps its raw palette-index bytes (00 00 00 00) and the last pixel receives the first pixel's colour (11 22 33 40 instead of cc dd ee c0) — the wrapped hand-off lands exactly 2^32 bytes off. At w = 0x40000004 the wrapped in-loop store displaces the first four pixels to the last four, and the RGB path shows the same shape at w = 0x5555555E / 0x55555566 (displaced and byte-rotated tail pixels). On this branch the wrap does not leave the row buffer — the row-end offset is computed in size_t in pngrtran.c, so the displaced stores land 2^32 bytes off inside the ~4 GiB buffer, and every ASan cell below is free of out-of-bounds reports; the defect is output corruption, not a heap overflow. (The out-of-buffer limb of this wrap class exists where the row end itself wraps — libpng18's arm/arm_init.c 4*row_width - 1, PR #971.) Nothing is reachable at the default user limits. The C fallback path is unaffected — the same images decode cleanly on x86, where the NEON code is not compiled. On hosts where size_t is 32 bits the cast does not widen, but the required >4 GiB row buffer cannot be allocated there either, so the wrapped code is unreachable.

Reproducing. Palette PNG, 8-bit, height 1, width 0x40000001 (RGBA cell: tRNS present; RGB cell: no tRNS, width 0x5555555E), index pattern two runs, read with png_set_user_limits(png_ptr, PNG_UINT_31_MAX, PNG_UINT_31_MAX) and png_set_expand, library at ac0c35324, aarch64 (qemu-user 6.2), -fsanitize=address:

witness_decode: width=1073741825 mode=rgba checked=15 bad=3 -> FAIL
pixel 0: got 00 00 00 00
pixel 1073741824: got 11 22 33 40

Same FAIL at width 0x40000004 (11/17 sampled pixels wrong) and at the RGB widths 0x5555555E / 0x55555566 (11/19). Controls on the same unpatched binary: width 1000 -> PASS; width 0x40000000 -> PASS (the boundary cell where the wrapped product is coincidentally exact or unused); width 0x55555550 -> PASS (RGB boundary). x86 build of the same unpatched tree (no NEON; the C fallback runs) at all trigger widths -> PASS. After this patch: every width PASS on aarch64, including a full 1,073,741,825-pixel verify of the 0x40000001 output row (0 wrong pixels), and the RGB cells pass likewise.

Test. contrib/libtests/pnggetset.c gains test_palette_expand_offsets, which evaluates the offset expressions as written against 64-bit ground truth at the trigger widths, shows the historical png_uint_32 products wrapping there, and checks that the offset expressions in arm/palette_neon_intrinsics.c are spelled in size_t (located via the compile-time path of the test source), so make check can catch a regression of an ARM-only fix on any host. Against the unpatched source that check fails; against this patch it passes. Honest limitation: make check cannot execute the end-to-end decode — the NEON path is not compiled on non-ARM hosts and the trigger needs >4 GiB row buffers — so the aarch64 runs above carry that part. The large-width cells are skipped on 32-bit size_t hosts, where the path is unreachable anyway.

Verification (both trees at ac0c35324, gcc -fsanitize=address, x86-64):

  • pngtest over all 51 contrib/pngsuite images: 51/51 PASS unpatched, 51/51 PASS patched, zero behavioural mismatches;
  • pnggetset: 7/7 PASS unpatched, 8/8 PASS patched (the new check added);
  • make check: identical results both trees (0 failures either tree; the patched tree adds the new check);
  • aarch64 ASan matrix (qemu-user): unpatched FAIL at all four trigger widths / PASS controls; patched PASS at all cells including the full-row verify; x86 PASS both trees.

Category: Cleanups of integer arithmetics; also a correctness fix for raised user limits.

Related. This is the libpng16 twin of the libpng18 fix in #971 (arm/arm_init.c + arm/palette_neon_intrinsics.c there; on libpng16 the row-end offset is already computed in size_t in pngrtran.c — dp = row + ((size_t)row_width << 2) - 1 — so only the palette_neon_intrinsics.c sites remain). #971's commit message offered this exact change for libpng16. Competition, all verified by diff: #899 (soul-sol) and issue #862 (Yanhaoxi) report the libpng18 arm/arm_init.c row-end wrap and fix/report only that file — neither touches arm/palette_neon_intrinsics.c, so the libpng16 sites here are unfixed by both. #874 and #827 touch this file but carry the wrapping arithmetic unchanged (*ddp - i * 4 / *ddp - i * 3 in #874's re-added copy; *ddp - (i << 2) / *ddp - ((i << 1) + i) in #827's refactor — same 32-bit wraps), so neither fixes this root cause.

🤖 Generated with Claude Code

png_do_expand_palette_rgba8_neon and png_do_expand_palette_rgb8_neon
compute the destination offset of each expanded chunk and the hand-off
back to the scalar fallback as "i * 4" / "i * 3" in png_uint_32
arithmetic.  The products wrap at i >= 0x40000000 (RGBA) /
i >= 0x55555556 (RGB), i.e. at row widths above 2^30, which are legal
inputs: the IHDR check is width <= PNG_UINT_31_MAX and applications
raise the PNG_USER_WIDTH_MAX default of 1,000,000 through the
documented png_set_user_limits().  The wrap displaces the chunk stores
and the hand-off by 2^32 within the row buffer, so the expanded row is
silently corrupted: the head pixels keep raw palette index bytes and
the tail receives displaced colours.  (On this branch the row-end
offset is already computed in size_t in pngrtran.c, so the displaced
stores stay inside the row buffer; the C fallback is unaffected.)

Compute the offsets in size_t, matching the spelling the C fallback
uses.  The riffle-loop palette offsets in png_riffle_palette_neon are
bounded by the 256-entry palette and cannot wrap; they are widened too,
so that every palette offset product in this file is computed in
size_t.

Add a regression check to contrib/libtests/pnggetset.c that pins the
offset arithmetic at the trigger widths against 64-bit ground truth and
verifies the source spellings, so make check can catch a regression of
an ARM-only fix on any host.  The end-to-end trigger is not exercised
by make check: the NEON path is not compiled on non-ARM hosts and needs
>4 GiB row buffers.
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