Skip to content

Fix SVE2 histogram counter overflow - #4990

Merged
terrelln merged 1 commit into
meta:devfrom
terrelln:2026-11-18-hist-sve2
Nov 19, 2026
Merged

terrelln merged 1 commit into
meta:devfrom
terrelln:2026-11-18-hist-sve2

Conversation

@terrelln

@terrelln terrelln commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

#4711 flags an issue in SVE2 histogramming that this PR fixes.

The SVE2 histogram accumulates counts in U16 lanes, so a symbol that appears 65,536 or more times in one call wraps, possibly to zero. That yields wrong probabilities, missed RLE detection, and corrupt Huffman output.

Split large inputs into chunks of at most 65,520 bytes, a multiple of 16 that also leaves room for the zero padding of the final vector load. Merge each chunk's counts into U32 totals, tracking the largest count and max symbol across the whole input.

Add a fuzzer test that checks exact counts, largest count and max symbol for every byte value and for sizes around the chunk and 16-bit boundaries, through both HIST_count_wksp and HIST_countFast_wksp. It also checks that entries past maxSymbolValue are not written and that an out-of-range symbol in a later chunk returns an error.

@Cyan4973 Cyan4973 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is now an ARM64 qemu job that runs the fuzzer unit tests on SVE2 build (#5000). Could you rebase onto dev, so that the CI runs your new test on the SVE2 path? Locally under qemu, it fails on dev and passes with this PR.

The SVE2 histogram accumulates counts in U16 lanes, so a symbol that
appears 65,536 or more times in one call wraps, possibly to zero. That
yields wrong probabilities, missed RLE detection, and corrupt Huffman
output.

Split large inputs into chunks of at most 65,520 bytes, a multiple of 16
that also leaves room for the zero padding of the final vector load.
Merge each chunk's counts into U32 totals, tracking the largest count and
max symbol across the whole input.

Add a fuzzer test that checks exact counts, largest count and max symbol
for every byte value and for sizes around the chunk and 16-bit
boundaries, through both HIST_count_wksp and HIST_countFast_wksp. It also
checks that entries past maxSymbolValue are not written and that an
out-of-range symbol in a later chunk returns an error.
terrelln pushed a commit to terrelln/zstd that referenced this pull request Nov 19, 2026
The ARM64 qemu job builds with SVE2, but only runs the CLI tests. Also run the fuzzer unit tests, which check the SVE2 histogram directly (see meta#4990).
@terrelln
terrelln force-pushed the 2026-11-18-hist-sve2 branch from 19cdcee to d98ace5 Compare November 19, 2026 21:04
@terrelln
terrelln merged commit 7141f24 into meta:dev Nov 19, 2026
112 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants