Repository navigation
Fix memory leak in SZ3 compression scratch buffers - #113916
Conversation
Bump contrib/sz3 to make the compress-side scratch buffers exception-safe. `Lossless_zstd::compress` throws `std::length_error` when the destination capacity is too small for poorly-compressible data, and every compress-side scratch buffer in SZ3 was a raw `malloc` with a bare `free` at the end of the function, so each failed compression leaked the buffer. Observed by LeakSanitizer in a stress test as a leak in `SZ3::SZGenericCompressor::compress` reached from `CompressionCodecSZ3::doCompressData`: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105249&sha=e803b6fd63e0a94ea3529b56af8a31fc8ada9562&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%29 Related PR: #105249 The buffers now use the same `unique_ptr`-with-`free` RAII pattern already used in `SZGenericCompressor::decompress`; the Exaalt decompression path is hardened the same way.
|
Workflow [PR], commit [4dd1e8c] Summary: ✅
AI ReviewSummaryThis PR bumps Findings
Tests
Final Verdict
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units7 translation units recompiled, 18 s compile time in total, 7 of them have a recent master baseline. |
…ratch-buffer-leak
…tion-safe Addresses the review finding that `CompressionCodecSZ3::doCompressData` calls the heap-owning `SZ_compress(config, data, compressed_size)` overload, whose top-level output buffer (a bare `new char[]` in `include/SZ3/api/sz.hpp`) was still leaked when the inner compression threw before the pointer reached `compressed.reset`. The buffer is now held in `std::unique_ptr` and released only on success (ClickHouse/SZ3@89c5e97). Co-Authored-By: Claude Fable 5 <[email protected]>
…ratch-buffer-leak
Addresses the review finding that the OpenMP compressor still kept its per-thread scratch chunks in raw `malloc` buffers freed only on the success path, and that their capacity did not account for the size header written by `Lossless_zstd::compress`.
|
🕵 @groeneai, investigate the failure: It is unrelated to this PR (which only bumps The other red, |
| @@ -1 +1 @@ | |||
| Subproject commit ff760cc4c011cbdef5dc0025dc1bec45b1931cc8 | |||
| Subproject commit f36e8b064fdde4bac260988ea459786964bbb492 | |||
There was a problem hiding this comment.
The code changes look consistent with the earlier leak reports, but the PR still does not provide proof for the Bug Fix claim. The linked local codec tests are functional tests; they do not force the throwing compression path that originally leaked, so they would still pass if one of these scratch buffers were still lost on exception.
Please attach either:
- a rerun of the linked LeakSanitizer stress repro against
f36e8b0, or - the smallest focused sanitizer test that makes
SZ3throw during compression and shows a clean leak report.
Without that, this remains a reasoned fix rather than a verified one.
There was a problem hiding this comment.
I built the focused sanitizer probe you asked for. It reddens on the base pin and is clean on this PR's pin, so the Bug Fix claim now has a measurement behind it. One correction to the reply above, though: the "not compiled in the ClickHouse build" argument covers only 1 of the 7 changed files.
The reason the linked functional tests cannot see this is worse than them being round-trip tests. The exception never reaches the caller: SZ_compress_dispatcher catches the length_error and downgrades to lossless, so the public SZ_compress call returns successfully while the scratch buffers abandoned by the unwind are already lost. The leak is silent, which is also why my first three probe designs measured 0 throws across 2700 swept configurations and would have been vacuous greens.
Trigger: uniform [0,1) Float64 with EB_ABS bound 1e-6. Worst ZSTD_compressBound(srcLen) / (dstCap - 8) is 1.1560, so the bound is genuinely missed. Random-bit and mixed-scale data never exceed 1.0, which is why "incompressible data" does not reach it.
A/B, n=8192, 40 rounds x 3 algorithms, counting the inner length_error separately from the leak report:
| arm | inner throws reached | LSan |
|---|---|---|
ff760cc4 (base) |
120 | 22471492 bytes in 160 allocations |
f36e8b06 (this PR) |
120 | clean |
f36e8b06 with the SZGenericCompressor guard removed |
120 | 19850052 bytes in 120 allocations |
Reached is identical in all three arms, so the clean arm is not an unreached path, and I asserted LeakSanitizer: checking for leaks is present in it so it is not a skipped check. Four distinct leak sites on the base pin: three at SZGenericCompressor.hpp:51 (via SZ_compress_LorenzoReg and SZ_compress_Interp) and one at SZAlgoInterp.hpp:181 (via SZ_compress_Interp_lorenzo). The mutation arm loses exactly the three SZGenericCompressor ones, so per-hunk attribution holds.
This matches a live CI report rather than a synthetic one. CIDB STID 3671-3a9b on Stress test (arm_asan_ubsan) and (amd_asan_ubsan) has the same file, line and callers: most recently #113963 at 2026-08-10 00:34 (1352832 bytes / 6 objects), #113796 at 2026-08-09 20:55 (209040 bytes), #109453 at 2026-08-09 07:57 (1890620 bytes).
It is user-reachable without any unusual configuration: EB_ABS with a caller-supplied bound is parsed straight from SQL at CompressionCodecSZ3.cpp:371, so CODEC(SZ3('ALGO_LORENZO_REG', 'ABS', 0.000001)) on such a Float64 column reaches it from an ordinary INSERT.
Where the "not compiled" argument does apply, and only there: SZImplOMP.hpp. I confirmed -fopenmp and -D_OPENMP are absent from the SZ3 codec translation unit in compile_commands.json and from cmake/, contrib/sz3-cmake/ and src/CMakeLists.txt, and SZImpl.hpp:11-13 forces conf.openmp = false under #ifndef _OPENMP. So no ClickHouse test can reach that hunk and the standalone -fopenmp compile is the right proof for it. The SZTruncateCompressor and SZExaaltCompressor hunks are also unreachable, since the codec only permits ALGO_LORENZO_REG, ALGO_INTERP_LORENZO and ALGO_INTERP. The other four files are compiled and reachable, and two of them are where the bytes above leak.
Probe source and both arms are reproducible with the pins unpacked side by side:
clang++-21 -std=c++23 -g -O1 -fsanitize=address -fno-omit-frame-pointer \
-DSZ3_USE_SKA_HASH=0 -I <pin>/include -I contrib/sz3-cmake -I contrib/zstd/lib \
leak_probe.cpp build/contrib/zstd-cmake/lib_zstd.a -o probe
ASAN_OPTIONS=detect_leaks=1 ./probe 40 8192
I have not pushed anything here. If a committed regression test is wanted, the honest shape is a gtest over the codec rather than a stateless query, because the leak produces no user-visible symptom to assert on and only LSan can observe it.
|
The fix is already in progress: #113908 (yours, open) pins That matches this failure and the carriers you listed. CIDB over the last 30 days, excluding the flaky-check reruns: 14 failures across 12 PRs and 8 check names, 0 on master. On #113563, #113412, #113382 and #112688 the harness minimization named One caveat so the link is not read as covering everything: 2 of those 14 failures are |
|
It shows that SZ3 is written by someone who doesn't know C++. |
|
Submitted upstream: szcompressor/SZ3#139 |
Pulls in the fix for the SZ3 compress-side scratch buffer leak (#113916), which was the only remaining CI red on this pull request (LeakSanitizer report in `SZ3::SZGenericCompressor::compress` via `CompressionCodecSZ3::doCompressData`). # Conflicts: # src/Storages/MergeTree/MergeTreeData.cpp
Bump
contrib/sz3(ClickHouse/SZ3@f36e8b0) to make the compress-side scratch buffers exception-safe.Lossless_zstd::compressthrowsstd::length_errorwhen the destination capacity is too small for poorly-compressible data, and the Huffman encoder can also throw on encode. Every compress-side scratch buffer in SZ3 was a rawmallocwith a barefreeat the end of the function, so each failed compression leaked the buffer.CompressionCodecSZ3::doCompressDatacatches the exception and rethrows it asCORRUPTED_DATA, so the server keeps running and accumulates the leaked buffers.This was observed by LeakSanitizer in a stress test (383 KB leaked in 2 allocations from
SZ3::SZGenericCompressor::compress): https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105249&sha=e803b6fd63e0a94ea3529b56af8a31fc8ada9562&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%29The fix applies the same
unique_ptr-with-freeRAII pattern already used inSZGenericCompressor::decompressto all compress-side scratch buffers (SZGenericCompressor,SZTruncateCompressor,SZExaaltCompressor,SZDispatcher,SZAlgoInterp), and hardens the Exaalt decompression path the same way (it parses untrusted data and could leak on corrupted input). It also makes the heap-owningSZ_compress(const Config &, const T *, size_t &)overload ininclude/SZ3/api/sz.hpp(the entry point used byCompressionCodecSZ3::doCompressData) exception-safe: its top-level output buffer was a barenew char[]and leaked when the inner compression threw before the pointer reached the caller.It also makes
SZ_compress_OMP(include/SZ3/api/impl/SZImplOMP.hpp) exception-safe: its per-thread scratch chunks were rawmallocbuffers freed only afterSZ_compress_dispatcherreturned, and their capacity did not reserve room for the size header thatLossless_zstd::compresswrites in front of the zstd stream. That path is_OPENMP-guarded and is not compiled in the ClickHouse build, so it was verified by compiling the header standalone with-fopenmp.No behavior change; the existing SZ3 tests (
03202_sz3_codec,04603_sz3_codec_non_finite,04604_sz3_codec_lorenzo_reg) pass locally with the patched submodule.Related: #105249
Changelog category (leave one):
Changelog entry:
Fix a memory leak in the experimental
SZ3compression codec: when compression of poorly-compressible or corrupted data failed with an exception, temporary scratch buffers were left allocated.Version info
26.8.1.1101(included in26.8and later)