Repository navigation
SipHash::update: avoid forming an out-of-range pointer in the unrolled loop - #107103
Conversation
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-25:20260610-225600 |
|
cc @azat — could you review this? It's the follow-up to #106469 you asked about: the gh-bot review noted the unrolled loop still forms |
|
Workflow [PR], commit [6532f09] Summary: ✅
AI ReviewSummaryThis PR rewrites the unrolled Findings❌ Blockers
Final VerdictStatus: ❌ Block Minimum required action: keep the empty-input path out of all pointer arithmetic, then retain the new non-empty loop condition. |
|
CI finished on `982f514546f3`. Both test failures are the same infrastructure outage, not caused by this PR (a header-only `SipHash::update` loop-condition rewrite + 2 gtest cases):
Both are the amd/arm `asan_ubsan` mass outage of 2026-06-10/11 (ASan secondary-allocator OOM, no ClickHouse frames; STID 1288/2046/0883 family), which hit dozens of unrelated PRs and master. A header-only SipHash change cannot produce server segfaults in unrelated stateless tests. Ready for review. |
…d loop The unrolled 8-byte loop used `while (data + 8 <= end)`. When fewer than 8 bytes remain (a 1-7 byte input, or the tail after the last full chunk where data == end), `data + 8` points past the one-past-the-end pointer `end`. Forming such a pointer is undefined behavior under [expr.add]/4, independent of whether it is dereferenced. This is the general form of the same defect class as the empty-input case fixed in ClickHouse#106469: there `data` was nullptr and `data + 8` triggered UBSan's "applying non-zero offset to null pointer"; here `data` is a valid non-null pointer but the formed pointer still leaves the [base, base+size] range. Rewrite the condition as `end - data >= 8`, which only ever evaluates pointer subtraction and never forms a pointer outside the buffer. The loop body is unchanged, so the computed hash is identical for every input length. The `size == 0` early return added in ClickHouse#106469 is now redundant and is removed. After the rewrite the empty input path forms no out-of-range pointer: `end = data + size` is `nullptr + 0` (well-defined), and the only remaining pointer arithmetic is the subtractions `end - data` (all `nullptr - nullptr`, i.e. 0), so the loop body and the tail switch never run. This is the same as a zero-length valid buffer. Verified under clang-21 `-fsanitize=address,undefined -fno-sanitize-recover=all`: the existing empty-string-view tests from ClickHouse#106469 and the new boundary/chunk tests all pass and produce byte-identical hashes with the guard removed. Found by clickhouse-gh[bot] inline review on ClickHouse#106469. Co-Authored-By: Claude Opus 4.8 <[email protected]>
982f514 to
6532f09
Compare
Pre-PR validation gate (updated for 6532f09:
|
| # | Question | Answer |
|---|---|---|
| a | Deterministic repro? | Yes, at the standards level for the over-range pointer, and deterministically UBSan-observable for the nullptr + 8 form (#106469). The question this push answers (is the size == 0 guard still needed?) is settled deterministically: built gtest_sip_hash.cpp with the guard present and with it removed under clang-21 -fsanitize=address,undefined -fno-sanitize-recover=all; both runs are clean and byte-identical. |
| b | Root cause explained? | The unrolled loop evaluated data + 8 before checking 8 bytes remain; for a 1-7 byte buffer and the final data == end check, data + 8 leaves [base, base+size] (UB under [expr.add]/4). The original size == 0 guard existed only because the old code path reached data + 8 with data == nullptr. The rewrite to end - data >= 8 removes every out-of-range pointer formation, so the guard's reason no longer exists. |
| c | Fix matches root cause? | Yes. Condition rewritten to pure pointer subtraction end - data >= 8. The now-redundant size == 0 early return is removed (it was tied to the old data + 8 arithmetic), not kept as a defensive band-aid. |
| d | Test intent preserved / new tests added? | The two empty-input tests from #106469 are kept unchanged and now serve as the regression proof that the loop logic (not the guard) makes nullptr, 0 a no-op. New UpdateBoundaryLengths (1-7 byte tails at exact-size allocation end, sizes 0..64) and UpdateChunkedMatchesWhole (chunked across the 8-byte boundary) assert streamed == single-shot. |
| e | Both directions demonstrated? | Yes. Guard PRESENT vs guard REMOVED, real SipHash.h header, full ASAN+UBSan -fno-sanitize-recover=all: both UBSan-clean on the nullptr, 0 path and byte-identical output (empty-interspersed lo=ea8674e1bc37999f hi=04eb82c75932ce4f, all sizes 0..64 streamed == whole). All 4 gtest_sip_hash.cpp tests pass with the guard removed. |
| f | Fix is general, not a narrow patch? | Yes. Audited all of SipHash.h: line ~136 was the only out-of-range pointer formation; data += 8 only runs in range and end = data + size is now nullptr + 0 on the empty path (well-defined). Sibling hand-written hashers (WeakHash, FieldVisitorHash, getHashOfLoadedBinary) do not use this loop shape; CityHash/xxHash are vendored. Root-cause fix (eliminate the out-of-range arithmetic), not a guard. |
Session id: cron:clickhouse-worker-slot-13:20260611-124900
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 25/25 (100.00%) | Lost baseline coverage: none · Uncovered code |
Related: #106469
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
Follow-up to #106469, addressing the clickhouse-gh[bot] inline review on that PR.
The unrolled 8-byte loop in
SipHash::update(const char *, UInt64)usedwhile (data + 8 <= end). When fewer than 8 bytes remain (a 1-7 byte input, or the tail iteration after the last full chunk wheredata == end),data + 8points past the one-past-the-end pointerend. Forming such a pointer is undefined behavior under [expr.add]/4, regardless of whether it is dereferenced.This is the general form of the defect fixed narrowly in #106469: there
datawasnullptranddata + 8triggered UBSan's "applying non-zero offset to null pointer"; heredatais a valid non-null pointer but the formed pointer still leaves the[base, base + size]range.The fix rewrites the condition as
while (end - data >= 8), which only ever evaluates pointer subtraction and never forms a pointer outside the buffer. The loop body is unchanged, so the hash is byte-identical for every input length.The
size == 0early return added in #106469 is now redundant and is removed (per review feedback). After the rewrite the empty-input path forms no out-of-range pointer:end = data + 0is well-defined, and the only remaining pointer arithmetic isend - datasubtractions (allnullptr - nullptr, i.e. 0), so the loop body and the tail switch never run, exactly as for a zero-length valid buffer.Verification (clang-21,
-fsanitize=address,undefined -fno-sanitize-recover=all, matchingamd_asan_ubsan): with the guard removed, the existing empty-string-view tests from #106469 (UpdateWithEmptyStringViewDoesNotInvokeUB,UpdateWithEmptyStringViewIsNoOp, which drive the exactnullptr, 0path) and the newUpdateBoundaryLengths/UpdateChunkedMatchesWholecases all pass and produce byte-identical hashes.