Repository navigation
arm: compute the NEON palette expansion offsets in size_t - #975
Open
itsgaldoron wants to merge 1 commit into
Open
itsgaldoron wants to merge 1 commit into
itsgaldoron wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
png_do_expand_palette_rgba8_neonandpng_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 aswith
iapng_uint_32. Each product is evaluated in 32 bits and wraps ati >= 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 iswidth <= PNG_UINT_31_MAX, and applications raise thePNG_USER_WIDTH_MAXdefault of 1,000,000 through the documentedpng_set_user_limits().ac0c35324)arm/palette_neon_intrinsics.c:85,99i * 4arm/palette_neon_intrinsics.c:85,99i * 4arm/palette_neon_intrinsics.c:85,99i * 4arm/palette_neon_intrinsics.c:125,143i * 3arm/palette_neon_intrinsics.c:125,143i * 3arm/palette_neon_intrinsics.c:125,143i * 3(The wrap boundary is sharp: at w = 0x40000000 the last chunk product
4*0x3FFFFFFCstill 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 fallbackpng_do_expand_palettein 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 tocontrib/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 insize_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 40instead ofcc 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 insize_tin 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'sarm/arm_init.c4*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 wheresize_tis 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)andpng_set_expand, library atac0c35324, aarch64 (qemu-user 6.2),-fsanitize=address: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.cgainstest_palette_expand_offsets, which evaluates the offset expressions as written against 64-bit ground truth at the trigger widths, shows the historicalpng_uint_32products wrapping there, and checks that the offset expressions in arm/palette_neon_intrinsics.c are spelled insize_t(located via the compile-time path of the test source), somake checkcan 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 checkcannot 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-bitsize_thosts, where the path is unreachable anyway.Verification (both trees at
ac0c35324, gcc-fsanitize=address, x86-64):pngtestover all 51contrib/pngsuiteimages: 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);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.cthere; on libpng16 the row-end offset is already computed insize_tin 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 libpng18arm/arm_init.crow-end wrap and fix/report only that file — neither touchesarm/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 * 3in #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