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

Fix VSEncoding reading past the end of __vseblocks_possLens - #151

Open
aikhanjum wants to merge 1 commit into
fast-pack:masterfrom
aikhanjum:fix-vsencoding-optpartition-overread
Open

aikhanjum wants to merge 1 commit into
fast-pack:masterfrom
aikhanjum:fix-vsencoding-optpartition-overread

Conversation

@aikhanjum

Copy link
Copy Markdown
Contributor

For every input of 17 or more integers, VSEncoding::compute_OptPartition() reads __vseblocks_possLens[16], one word past the end of the 16-entry table. Under AddressSanitizer that is a global-buffer-overflow, so a sanitizer build aborts the first time it encodes more than 16 integers with VSEncoding. Without a sanitizer the read is silent, and on the builds I checked the word after the table is 0 or 1, so the output was not affected. That is luck of the memory layout. If that word held a number from 17 to 32, the encoder could choose a block of non-zero integers of that length, which the format has no code for, and the result would not decode.

The partition loop tries block lengths 1, 2, 3 and so on for each end position i, and advances l through possLens[] as they match. It cuts non-zero blocks at 16 only once i > maxBlk, and maxBlk is 32, the longest run of zeros, so for positions 17 to 32 it keeps going after l has reached poss_sz.

The fix checks l against poss_sz before reading possLens[l], in the non-zero branch where the read happens and in the zero-run branch, which has the same read. That branch only runs for a log of 0, which __vseblocks_remapLogs[1 + asmbits(v)] does not produce for values below 2^31, so there the check only matters if that ever changes. When l is in bounds the code does exactly what it did before.

The test from #150 stopped at 16 integers because of this bug. It now runs its two inputs, increasing values and all zeros, at every length from 0 to 300. That spans many blocks and several descriptor words, so it also covers the whole-word decode path from #150, which 16 integers never reached. Under AddressSanitizer the test fails without the fix at 17 integers with the global-buffer-overflow, and passes with it. Without a sanitizer it passes either way, since the stray read did not change the output on these builds, so the sanitizer CI job is what catches a regression.

Validation on macOS 26.3.1 arm64, with Apple clang 21 unless noted

  • FASTPFOR_SANITIZE=ON, FASTPFOR_SIMD_MODE=portable, as in the sanitizer CI job. Without the fix the test aborts with AddressSanitizer: global-buffer-overflow in VSEncoding::compute_OptPartition at vsencoding.h:210, 0 bytes after __vseblocks_possLens. With the fix the gtest binary passes 73 of 73. The VSEncoding test now takes about 1 s here, up from 50 ms, almost all of it in constructing a VSEncodingBlocks, with its 1 MB scratch buffer, for each input.
  • Release and Debug with FASTPFOR_SIMD_MODE=native, as in the macOS CI job. 73 of 73 pass, and vsencoding.h gets no new warnings under the library's warning flags.
  • Homebrew GCC 16 with ASan fails the same way without the fix and passes 73 of 73 with it. That run used native SIMD mode, since portable mode adds -msse4.2, which GCC rejects on arm64. GCC 16 Release passes too.
  • src/unit.cpp under ASan goes through CODECFactory. Without the fix it hits the same overflow, this time in the copy of the table in codecfactory.cpp, and with the fix it prints "Your code is good." I ran it with ASAN_OPTIONS=detect_container_overflow=0, since otherwise it stops earlier at an unrelated read in Simple9_RLE decoding.
  • A scratch program, not part of this PR, encodes 27,090 inputs with the old and the fixed encoder, 9 value patterns below 2^31 at every length from 0 to 3000 and at multi-block lengths up to 200,003. The encoded words are identical with clang and GCC at -O0 and -O3, and everything the old encoder wrote decodes correctly with the fixed code. When I pad the old table with a 17th entry of 17, 20 or 32 to stand in for the stray word, 17,328 to 18,999 of those encodings change, one I decoded gets 19 of 21 values wrong, and the run that decodes them all crashes.

Not verified. Linux, the x86 SSE builds, GCC's portable mode, Windows and MSVC. I only looked at what sits after the table on these builds, so another compiler or linker could have put a different word there.

This follows #150 and extends its test.

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

🤖 Generated with Claude Code

VSEncoding::compute_OptPartition() scans the candidate blocks that end
at position i, from length 1 upward, and advances l through possLens[]
as the lengths match. A block of non-zero integers is at most 16 long,
but the scan only stops at that length once i is past maxBlk (32, the
longest run of zeros). For positions 17 to 32 it keeps going, l reaches
poss_sz, and possLens[l] reads one word past the 16-entry table. Every
input of 17 or more integers does this, and AddressSanitizer reports a
global-buffer-overflow in the encoder.

Check l against poss_sz before reading possLens[l], in the non-zero
branch and in the zero-run branch, which has the same unchecked read.
When l is in bounds nothing changes. The old code could only pick a
different partition if the word after the table held a block length
from 17 to 32.

Extend the VSEncoding test from 16 to 300 integers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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