Skip to content

Fix two latent LLP64 bugs in roundUpToPowerOfTwoOrZero and DATE_LUT_ADD - #112185

Merged
alexey-milovidov merged 3 commits into
masterfrom
llp64-latent-portability-bugs
Jul 28, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
llp64-latent-portability-bugs

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jul 27, 2026 •

Copy link
Copy Markdown
Member

Two places where the code needs 64 bits but asks the compiler for long, which is only that wide on LP64 platforms. Every platform ClickHouse currently supports is LP64, so neither can be observed today — I found them while cross-compiling for Windows, which is LLP64 and where long is 32 bits. Fixing them separately from that work, since both are self-contained and the fixes are no-ops on LP64.

roundUpToPowerOfTwoOrZero passed a size_t to __builtin_clzl, whose parameter is unsigned long. The function's own body makes the intent explicit — it compares against 0x8000000000000000ULL and subtracts the leading-zero count from a hardcoded 64 — so __builtin_clzll, which is at least 64 bits everywhere, is what it meant. On an LLP64 platform the argument is instead truncated to 32 bits, and every input above 2^32 rounds to a small power of two, or to 1 when the low half happens to be zero. The callers are allocation sizing paths (PODArray, GraceHashJoin, the cache dictionaries), where such inputs are ordinary.

DATE_LUT_ADD is (1970 - 0) * 366L * 86400, about 6.2e10, which does not fit in the 32 bits L guarantees; it becomes 366LL. It is added to a time value before the relative hour and minute helpers divide, so a wrapped constant would put those results far from the epoch they are measured against.

Also adds a unit test for roundUpToPowerOfTwoOrZero, which had none. Beyond the documented edge cases (zero, and inputs with the most significant bit set, which are returned unchanged) it covers the range above 2^32 — the range that distinguishes a 64-bit leading-zero count from a 32-bit one, so the test fails on an LLP64 platform if the builtin is ever narrowed again. It passes on LP64 both before and after the fix, as it must, since the bug cannot be reproduced there; I checked that it does report 64 failures against a deliberately-truncating version of the function.

Note the L → LL change alters the type of the DATE_LUT_ADD expression on LP64 (long → long long, both 64 bits), so I verified clickhouse_common_io still builds clean under -Weverything -Werror.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.8.1.326 (included in 26.8 and later)

Both are places where the code needs 64 bits but asks the compiler for `long`, which is
only that wide on LP64 platforms. Every platform ClickHouse currently supports is LP64, so
neither can be observed today - these were found by cross-compiling for Windows, which is
LLP64 and where `long` is 32 bits.

`roundUpToPowerOfTwoOrZero` passed a `size_t` to `__builtin_clzl`, whose parameter is
`unsigned long`. The function's own body makes the intent explicit - it compares against
`0x8000000000000000ULL` and subtracts the leading-zero count from a hardcoded 64 - so
`__builtin_clzll`, which is at least 64 bits everywhere, is what it meant. On an LLP64
platform the argument would instead be truncated to 32 bits, and every input above 2^32
would round to a small power of two, or to `1` when the low half happened to be zero. The
callers are allocation sizing paths (`PODArray`, `GraceHashJoin`, the cache dictionaries),
where such inputs are ordinary.

`DATE_LUT_ADD` is `(1970 - 0) * 366L * 86400`, about 6.2e10, which does not fit in the 32
bits `L` guarantees; it becomes `366LL`. It is added to a time value before the relative
hour and minute helpers divide, so a wrapped constant would put those results far from the
epoch they are measured against.

Adds a unit test for `roundUpToPowerOfTwoOrZero`, which had none. Beyond the documented
edge cases (zero, and inputs with the most significant bit set, which are returned
unchanged) it covers the range above 2^32 - the range that distinguishes a 64-bit
leading-zero count from a 32-bit one, so the test fails on an LLP64 platform if the builtin
is ever narrowed again. It passes on LP64 both before and after the fix, as it must, since
the bug cannot be reproduced there.
@clickhouse-gh

clickhouse-gh Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [617d8ed]

Summary: ✅


AI Review

Summary

This PR correctly fixes the two LLP64-specific width assumptions it describes: roundUpToPowerOfTwoOrZero now uses a 64-bit leading-zero builtin in src/Common/BitHelpers.h, DATE_LUT_ADD is widened to a 64-bit constant in src/Common/DateLUTImpl.h, and the new gtest_bit_helpers coverage exercises the > 2^32 range that distinguishes the allocation-size bug. I did not find a remaining correctness, compatibility, or testing gap in the current code, and the current Praktika reports for PR #112185 are green.

Final Verdict

✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jul 27, 2026
@yariks5s yariks5s self-assigned this Jul 27, 2026
…agnostic

The `arm_tidy` build failed on nine translation units with

    contrib/arrow/cpp/src/arrow/util/bit_util.h:207:10: error: redundant explicit
    casting to the same type 'int' as the sub-expression [readability-redundant-casting]
    src/Common/BitHelpers.h:27:31: note: source type originates from the invocation
    of this function

The cast is arrow's, not ours, and it is unchanged - but a compiler builtin is
implicitly declared at its first use in a translation unit, so moving
`roundUpToPowerOfTwoOrZero` from `__builtin_clzl` to `__builtin_clzll` made line 27
the declaration point of `__builtin_clzll` for every unit that includes this header
before arrow's. `clang-tidy` then anchors the note there, and a note pointing into
`src/` makes the whole diagnostic count as user code instead of `contrib`, which
`ExcludeHeaderFilterRegex` would otherwise drop.

Previously the first use in this header was the `__builtin_clzll` call in
`getLeadingZeroBitsUnsafe`, which already carries this exact `NOLINT` for the same
reason, so the diagnostic was suppressed there. Add the same suppression to the new
first use.

Verified with `clang-tidy-21 --checks='-*,readability-redundant-casting'` on two of
the failing units, `Processors/Formats/Impl/ArrowIPC/ArrowIPCSchemaReader.cpp` and
`Processors/Formats/Impl/Parquet/Write.cpp`: both reproduce the error before this
commit and are clean after it.

Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112185&sha=ac441419a3945640355331d37903b7d4ba996b10&name_0=PR&name_1=Build%20%28arm_tidy%29
@clickhouse-gh

clickhouse-gh Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.30% 86.30% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 78.40% 78.40% +0.00%

Changed lines: Changed C/C++ lines covered: 24/24 (100.00%) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unit test is pointless, and the comments are more verbose than needed, but the changes are trivial, correct, and no-op on Linux/Mac/FreeBSD.

@alexey-milovidov alexey-milovidov self-assigned this Jul 28, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 28, 2026
Merged via the queue into master with commit ec17f36 Jul 28, 2026
178 checks passed
@alexey-milovidov
alexey-milovidov deleted the llp64-latent-portability-bugs branch July 28, 2026 23:43
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 29, 2026
pull Bot pushed a commit to peijunlin2008/ClickHouse that referenced this pull request Aug 3, 2026
None of these are Windows-specific; they are things the port happened to walk into.

`FailPoint.cpp`'s `USE_LIBFIU=0` branch had drifted from the class it implements. It
defined `enablePauseFailPoint`, `wait` and `enableFromGlobalConfig`, none of which
`FailPointInjection` declares any more, and did not define
`notifyPauseAndWaitForResume`, which it does. So a build configured with
`-DENABLE_LIBFIU=OFF` failed to compile the file at all - four errors - and would
have failed to link even without them. Verified by compiling the file with
`USE_LIBFIU 0`: four errors before, clean after.

`ReadBufferFromPocoSocketBase::setReceiveTimeout` took a count of microseconds and
passed it to `Poco::Timespan(long seconds, long microseconds)`, so it set a timeout a
million times longer than asked for. It has no callers, which is why nobody noticed.

`KeeperNodeStats` packs three flags into the top bits of a `uint64_t` with
`1ul << 63`, `1ul << 62` and `1ul << 61`, and checks the mask against
`~(~0ul >> NUM_FLAGS)`. `unsigned long` is 32 bits on any LLP64 target, where all four
of those are undefined behaviour and the constants come out wrong. Likewise
`BinaryRow`'s `0x80L << 56` and `0x7FL << 56` for `Int64` constants. Same class as
the two fixed in ClickHouse#112185; harmless on
the platforms we ship, which is exactly why they survive.

`ConfigProcessor.cpp` included `<sys/utsname.h>` without using anything from it.

No tests: the failpoint fix is a build configuration CI does not exercise, the timeout
has no callers to observe it through, and the shift constants have the right values on
every platform we build for - they are only wrong where `long` is 32 bits.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants