Repository navigation
Conversation
`CompressionCodecGDeflate` gave libdeflate a page buffer of `libdeflate_gdeflate_compress_bound(65536)` = 65708 bytes and called `libdeflate_gdeflate_compress`, which hands the writer the library's own bound whatever the buffer size. That bound is too small for GDeflate: the bit stream is split over 32 interleaved streams of 32-bit packets, and each stream can reserve up to two packets beyond its bits (`deflate_init_output` reserves one per stream up front, `deflate_add_bits` reserves the next one as soon as the emulated decoder has fewer than 32 bits left), i.e. up to 256 bytes beyond the deflate bit count. The library accounts for this in an `#ifdef GDEFLATE` term of `libdeflate_deflate_compress_bound`, but that function is compiled only in the plain-deflate translation unit, where `GDEFLATE` is not defined (NVIDIA/libdeflate#1, fix NVIDIA/libdeflate#3 not merged). The GDeflate writer has no bounds checks, so: - a page whose output ends exactly at the writer's end (65700) is reported as an overflow: a valid INSERT fails with `Cannot compress a chunk of 65536 bytes with GDeflate (CANNOT_COMPRESS)`, seen on `05443_gdeflate_codec` with `index_granularity = 49963`; - a page whose output passes it is written past the buffer and returned (up to 65748 bytes measured, against a reserve of 65716). Each page now gets the library bound + 2 * 32 * 4 = 65964 bytes, through `libdeflate_gdeflate_compress_ex`, which hands `nbytes` to the writer. Why that holds: the output is 4 bytes per packet allocated; per stream, `input_bitcount + bitcount = 32 * live packets`, and a reservation happens only when `input_bitcount < 32`, so a stream holding B_s bits allocates at most B_s/32 + 2 packets and a page is at most B/8 + 256 bytes, where B/8 is bounded by the plain-deflate bound 65580. The capacity is 65580 + 128 + 256 = 65964, the writer's end 65956 >= 65836. Measured on 80048 page shapes x levels 1..12 with exact-size ASan allocations: 0 refusals, 0 overruns, largest page 65748. The bytes written do not change (the column files of the 05443 tables are byte-identical before and after), so existing parts stay readable and no format change is needed. The reserve grows by 256 bytes per 64 KiB chunk. Tests: a page in `05443_gdeflate_codec` that compressed to exactly the old end (failed 20/20 under random settings before, passes 50/50 after), and `GDeflateCodec.CompressedChunkFitsReserve`, which compresses into exactly `getCompressedReserveSize` bytes (ASan reported heap-buffer-overflow in `deflate_add_bits` before). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review (2 nits, both on the PR description).
Not taken: patching the library in a fork. Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20261027-022500 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261027-011300 |
|
cc @scanhex12 @rienath, could you review this and add the |
|
Workflow [PR], commit [544fcdb] Summary: ❌
AI ReviewSummaryThis PR fixes the Final Verdict
|
CI finish ledger - 544fcdbEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The first run of this head stopped at Session id: cron:our-pr-ci-monitor:20261027-203012 |
|
Closing: the codec was reverted in #128947. |
… and writes `contrib/gdeflate` now points at the branch `gdeflate` of `ClickHouse/libdeflate`, with ClickHouse/libdeflate#9 on top of `NVIDIA/libdeflate@8ba9502`: - The decoder refilled a stream with 4 bytes without comparing its position with the end of the page, so a page shorter than 128 bytes or a corrupt one was read past its end (`multiple_decompress_fuzzer`, heap-buffer-overflow in `deflate_decompress_default`). - `libdeflate_gdeflate_compress_bound` left out the two packets each of the 32 streams may hold at its end, and the encoder did not check its packet writes against the buffer: a valid `INSERT` failed with `CANNOT_COMPRESS` (`05443_gdeflate_codec` with a random `index_granularity`), and a page past the bound was written out of bounds. The bytes of every page that compressed before are unchanged. The library is no longer built with `deflate_compress.c` on its own: it was there only for `libdeflate_deflate_compress_bound`. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=b08434c5500c10180f0c43fedb0f5e860b20f46f&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_tsan%2C%20parallel%29 Related: #128625 Related: #127910 Related: #128947
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Description
Related: #127422 (the codec; master only, absent on 26.10, hence
Not for changelog).The
GDeflatecodec could reject a validINSERTwithCannot compress a chunk of 65536 bytes with GDeflate. (CANNOT_COMPRESS)(seen on05443_gdeflate_codecwithindex_granularity = 49963), and for other pages libdeflate wrote past the buffer the codec reserved.Root cause: each 64 KiB page was sized by
libdeflate_gdeflate_compress_bound(65708 bytes). That bound omits the two 32-bit packets each of the 32 GDeflate streams can reserve beyond its bits (the library's#ifdef GDEFLATEterm for it is compiled out; NVIDIA/libdeflate#1, fix NVIDIA/libdeflate#3 unmerged), and the GDeflate writer does not check its packet writes against the buffer. A page whose output ends exactly at the writer's end is refused; one that passes it is written out of bounds (the new gtest measured 65769 bytes against a reserve of 65725).Change: each page gets the library bound plus 256 bytes, through
libdeflate_gdeflate_compress_ex, which honours the buffer size (libdeflate_gdeflate_compressuses the library's own bound whatever the buffer). The bytes written are unchanged (the column files of the05443tables are byte-identical before and after), so existing parts stay readable; the reserve grows by 256 bytes per chunk.Validation: a new block in
05443_gdeflate_codecfails 20/20 on the unfixed server under random settings and passes 50/50 with the fix. The new gtestGDeflateCodec.CompressedChunkFitsReservecompresses into exactlygetCompressedReserveSizebytes: before the fix ASan reportsheap-buffer-overflowindeflate_add_bits(debug: the size exceeds the reserve); after it passes on debug and ASan.Workflow [PR]
Sync PR [sync-upstream/pr/127910]