Repository navigation
Conversation
With string_serialization_version='with_size_stream' the String sizes are read into a ColumnUInt64 kept in the per-column deserialize state. That same column is also stored in the substreams cache and can be handed out as the .size subcolumn output. When a single read spans several granules (continue_reading, i.e. max_block_size larger than index_granularity) the sizes were appended in place via assumeMutable(), which reallocates the shared buffer and leaves the cache and the emitted column pointing at freed memory. Under sanitizers this surfaces as a double-free / heap-use-after-free in a background merge thread (MergeTreeSequentialSource::generate). Append through IColumn::mutate so a shared sizes column is cloned first; a solely-owned column is still mutated in place, preserving the optimization. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-0:20260701-045700 |
|
cc @Avogar @CurtizJ — could you review? This fixes a use-after-free in the String size-stream deserialize: the |
|
Workflow [PR], commit [c2da58b] Summary: ❌
AI ReviewSummaryThis PR fixes the shared-buffer append bug in the Final Verdict
|
The first query used countIf(s.size != length(s)) with the default optimize_functions_to_subcolumns = 1, so FunctionToSubcolumnsPass rewrote length(s) to the s.size subcolumn. Confirmed via EXPLAIN actions=1 and system.query_log: the plan read only default.T.s.size and never the full String data stream, so the patched SerializationString::deserializeBinaryBulkWithSizeStream append path was not exercised (the test would pass with or without the fix). Pin optimize_functions_to_subcolumns = 0 on that query so length(s) reads the regular String data stream; the plan now reads both default.T.s and default.T.s.size, exercising the cross-granule sizes append the fix targets. The .size == length assertion and the .reference are unchanged. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Done in a1598b6. Added You were right: with the default With |
CI finish ledger — a1598b6Every failure below has an owner (a fixing PR of ours). Only
Session id: cron:our-pr-ci-monitor:20260701-133000 |
| size_t prev_size = string_state->size_column->size(); | ||
| /// `size_column` may be shared: besides the persistent deserialize state it is also put | ||
| /// into the substreams cache below, and that cached column can be handed out as the | ||
| /// `.size` subcolumn output. Appending through `assumeMutable()` would reallocate that |
There was a problem hiding this comment.
optional: project convention writes function names without parens, so assumeMutable rather than assumeMutable() here (and in the test's header comment). purely cosmetic.
CI finish ledger — a1598b6CI finished (Finish Workflow completed; its FAIL conclusion is a rollup of the infra env-setup + chronic-family reds below, none PR-caused). This PR fixes a real use-after-free reading the String
Session id: cron:our-pr-ci-monitor:20260703-053000 |
| /// `.size` subcolumn output. Appending through `assumeMutable()` would reallocate that | ||
| /// shared buffer in place, freeing memory the cache or the emitted column still points | ||
| /// at. Go through `IColumn::mutate` so a shared column is cloned before we append. | ||
| auto mutable_sizes = IColumn::mutate(std::move(string_state->size_column)); |
There was a problem hiding this comment.
the same violation exists in the mirror path: when .size is read before the full column, the cache-miss branch of SerializationStringSize::deserializeBinaryBulkWithSizeStream appends via assumeMutable to the .size result column that this size_column already aliases through the cache (instrumented locally: use_count is 2 at the append when one block spans several discontiguous mark ranges). worth the same IColumn::mutate treatment in a follow-up.
The full-string append path in SerializationString was already fixed to clone a shared size column via IColumn::mutate. The symmetric append in SerializationStringSize::deserializeBinaryBulkWithSizeStream (the path that reads the .size subcolumn directly) had the identical bug: when the full String column is also read, SerializationString picks the .size output up from the substreams cache into its persistent deserialize state, so at the next range's append the .size column is shared (use_count == 2 when one block spans several discontiguous mark ranges). Appending in place via assumeMutable would realloc that shared buffer, freeing memory the persistent state still points at, and crash as a double-free in a background merge thread under sanitizers. Route the mirror append through IColumn::mutate as well, so a shared column is cloned before we append. Confirmed by instrumentation: with disjoint key ranges plus reading both s.size and the full s, the append sees use_count==2 and mutate clones; on the common non-shared path use_count==1 and it is a no-op. Extended the 04409 regression test with a disjoint-mark-range query that exercises the mirror path. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Good catch. Folded the mirror-path fix into this PR (
Reproduced your The Extended 04409 with a disjoint-mark-range query hitting this path; 50/50 with randomization. Also applied your |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 17/18 (94.44%) · Uncovered code |
CI finish ledger — c2da58bNew head (mirror-path fold-in). Every failure below has an owner; none is new PR-caused work on this PR (the diff only touches
Session id: cron:our-pr-ci-monitor:20260703-200000 |
CI finish ledger — c2da58bCI fully finished on this head (Finish Workflow completed 2026-07-03T18:46:55Z, 0 checks queued/in_progress). Every failure has an owner.
The Session id: cron:our-pr-ci-monitor:20260704-170000 |
|
@groeneai please check ci failures. |
|
Checked the CI failures on c2da58b. Both stress reds are pre-existing trunk failures, not caused by this PR (it only touches
The Bugfix-validation |
tiandiwonder
left a comment
There was a problem hiding this comment.
The fix seems wrong. cc @Avogar
groeneai
left a comment
There was a problem hiding this comment.
@Avogar the change routes the .size append through IColumn::mutate instead of assumeMutable. The concrete hazard: string_state->size_column (the persistent deserialize state) is also inserted into the substreams cache and can be handed out as the .size subcolumn. On a continuation read, assumeMutable appends in place and reallocates that buffer, so any other holder of the same object is left pointing at freed memory. I reproduced use_count == 2 at the append when one block spans discontiguous mark ranges (instrumented locally, matching tiandiwonder's earlier note). IColumn::mutate clones only when shared, so it stays a no-op on the common unique-owner path.
Could you point at what specifically looks wrong, so I fix the right thing? The candidates I can see:
- premise: whether there is a real C++ UAF here at all, given #109950 traced the silent no-report MAPERR bulk of #106370 to the Rust-uninstrumented
asan_ubsanbuild (wasmtime sigaltstack) rather than a data-path bug. This path is a separately reproduced aliasing, not that family. - layer: whether the fix belongs at the source (do not alias the persistent-state
.sizecolumn into the cache, e.g. cache acut()copy) instead of cloning on append. - cost: repeated cloning if the same state accumulates across many continuation reads.
If the source-level approach is preferred I will switch to it.
|
It was fixed by pr #109950 (merged 2026-07-10), and neither signature has occurred in CI since. The actual root cause is unrelated to serializations: combined So the use-after-free / double-free claim in the description and changelog entry does not hold. Whether the in-place |
|
Understood, accepting the close. Given #109950 (merged 2026-07-10) as the actual root cause (the Rust address,undefined build losing -Zsanitizer=address, disarming wasmtime's guard, not a serialization data path), and Avogar's point that filling a shared substream column in place via assumeMutable is the intended cache behavior so all holders see the single fill, the use-after-free premise here does not hold and the IColumn::mutate change would break that intended sharing. Thanks for the detailed rationale. |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a heap-use-after-free / double-free that could occur during merges and reads of a
Stringcolumn's.sizesubcolumn whenstring_serialization_version = 'with_size_stream'(the default). The sizes column kept in the deserialize state was appended in place while still referenced by the substreams cache, corrupting memory in a background merge thread.Description
With
string_serialization_version = 'with_size_stream'the byte-sizes of aStringcolumn are stored in a separate stream. During deserialization the sizes are read into aColumnUInt64that is kept in the per-column deserialize state (DeserializeBinaryBulkStateStringWithSizeStream::size_column). That same column is also placed into the substreams cache, and the cached column can be handed out as the.sizesubcolumn output.When a single read spans more than one granule (
continue_reading, i.e.max_block_sizelarger thanindex_granularity), the next granule's sizes were appended in place viaassumeMutable()atSerializationString::deserializeBinaryBulkWithSizeStream.assumeMutable()is aconst_castthat does not clone a shared column, so once the sizes column grew past itsPODArraycapacity the append reallocated the buffer in place, freeing memory that the substreams cache (and any emitted.sizecolumn) still pointed at. Under sanitizers this surfaces as a double-free / heap-use-after-free in a background merge thread, with the free happening inMergeTreeSequentialSource::generateand the crash while the state or the emitted column is later released.The fix routes the append through
IColumn::mutate, which clones the sizes column only when it is shared; a solely-owned column is still mutated in place, so the sizes-only optimization is preserved.Reproduction (deterministic precondition): a
with_size_streamwide table with a smallindex_granularity, read withmax_block_sizefar larger than the granule so one read appends sizes across many granules while the.sizesubcolumn is also read. Before the fix the append reallocates the shared sizes buffer; after the fix it clones first. The added regression test04409_string_size_subcolumn_shared_buffer_mergeexercises this path and checks that.sizeequalslength()for every row.Found by CI in
Stateless tests (arm_asan_ubsan, azure, sequential)and sibling asan checks (recurring double-free / allocator-CHECK signature inSerializationString::deserializeBinaryBulkWithSizeStream, e.g. STID1367-3d4eand1288-3bd5). Example reports:Related: #106370