Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Fix VSEncoding writing past the end of the output buffer - #150

Merged
lemire merged 1 commit into
fast-pack:masterfrom
aikhanjum:fix-vsencoding-decode-overrun
Nov 27, 2026
Merged

lemire merged 1 commit into
fast-pack:masterfrom
aikhanjum:fix-vsencoding-decode-overrun

Conversation

@aikhanjum

Copy link
Copy Markdown
Contributor

Fixes #128.

VSEncodingBlocks::decodeVS() reads four block descriptors per 32-bit word. It copies 16 integers for every block of non-zero integers (__vseblocks_copy16) and clears 32 for every run of zeros (__vseblocks_zero32), whatever the block length, and it decodes all four descriptors of the last word, including the padding ones. So decoding writes up to 34 integers past out + nvalue, or 35 for an empty input. codecs.h only asks callers for nvalue integers of output, so a caller that follows that, like the Rust wrapper in the issue with 10 integers for 5 values, corrupts the heap. The existing tests never hit it because src/unit.cpp gives every codec 1024 integers of headroom.

The fix leaves the fast loop as it is but only runs it while a whole descriptor word, at most 4 * 32 integers, fits before the end. The blocks after that, fewer than 128 integers per call, are decoded one at a time with their exact lengths. For an empty input the old do/while also read a descriptor word past the encoded data and returned a pointer one past the end. That goes away too.

The new test unittest/test_vsencoding.cpp decodes into a buffer with guard words after the nvalue integers and checks that they are untouched and that the returned pointer is the end of the encoded data. It runs {1, 2, 3, 4, 5} plus increasing and all-zero inputs of length 0 to 16, and it fails without the fix in every build type, not only under ASan. The inputs stop at 16 on purpose. From 17 integers on, VSEncoding::compute_OptPartition reads __vseblocks_possLens[16], one past the end of the table, and ASan aborts in the encoder. That is a separate bug and this PR does not touch it.

Validation on macOS 26.3.1 arm64 with Apple clang 21

  • FASTPFOR_SANITIZE=ON, FASTPFOR_SIMD_MODE=portable, as in the sanitizer CI job. Without the fix the new test fails, every length from 0 to 16 writes 33 to 35 integers past the buffer. A standalone program that decodes {1, 2, 3, 4, 5} into a std::vector<uint32_t>(5) gets heap-buffer-overflow ... WRITE of size 64 in __vseblocks_copy16 called from decodeVS, the same corruption as in the issue. With the fix ctest passes, the gtest binary passes 73 of 73, and the standalone program decodes cleanly.
  • Release and Debug with FASTPFOR_SIMD_MODE=native, as in the macOS CI job. 73 of 73 pass with the fix, and the Release build without ASan fails the new test without the fix.
  • src/unit.cpp, every factory codec, prints "Your code is good." Master does not build it at the moment (see About CI below), so I ran it on a copy with ztimer.h patched locally.
  • A scratch round-trip check, not part of this PR, over every length from 0 to 3000, multi-block lengths up to 300000 and four value patterns, finds no mismatch and nothing written past the output. Before the fix 10578 of those 12040 cases wrote past it.
  • Decode speed in a scratch benchmark (8-bit values, -O3) is unchanged from 65536 integers up. For 100 integers a call goes from about 21 ns to 27 ns, because a short array now goes entirely through the exact path.

Not verified. GCC, Linux, Windows and MSVC, and the x86 inline asm versions of copy16 and zero32. Also not fixed here, the decoder still reads past the end of a tightly sized input buffer (the unpackers work in groups of 32 integers), and integers of 2^31 and above still do not round trip because __vseblocks_remapLogs[1 + asmbits(v)] reads past its 33 entries.

About CI. Since #148, building all targets fails on Linux and macOS with ztimer.h:122: extraneous closing brace, so Ubuntu-CI and Macos-CI are red on master and will be red here too until #149 is merged. For the runs above I built the FastPFOR_unittest target directly.

AI assistance (Claude Code) was used to investigate the issue, write the fix and tests, and run the validation.

🤖 Generated with Claude Code

VSEncodingBlocks::decodeVS() reads four block descriptors per 32-bit
word. It always copies 16 integers for a block of non-zero integers and
clears 32 for a run of zeros, and it decodes every descriptor of the
last word, including the padding ones. Decoding can therefore write up
to 34 integers past out + len, or 35 for an empty input, so decoding
into a buffer of exactly nvalue integers corrupts the heap.

Keep the fast path while a whole descriptor word fits before the end,
then decode the remaining blocks one at a time with their exact
lengths. For an empty input this also stops reading a descriptor word
that does not exist.

Fixes fast-pack#128.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Suspected memory corruption in vsencoding

2 participants