Repository navigation
Fix two latent LLP64 bugs in roundUpToPowerOfTwoOrZero and DATE_LUT_ADD - #112185
Conversation
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.
|
Workflow [PR], commit [617d8ed] Summary: ✅
AI ReviewSummaryThis PR correctly fixes the two LLP64-specific width assumptions it describes: Final Verdict✅ Approve |
…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
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 24/24 (100.00%) · Uncovered code |
alexey-milovidov
left a comment
There was a problem hiding this comment.
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.
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]>
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 wherelongis 32 bits. Fixing them separately from that work, since both are self-contained and the fixes are no-ops on LP64.roundUpToPowerOfTwoOrZeropassed asize_tto__builtin_clzl, whose parameter isunsigned long. The function's own body makes the intent explicit — it compares against0x8000000000000000ULLand subtracts the leading-zero count from a hardcoded64— 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 to1when 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_ADDis(1970 - 0) * 366L * 86400, about 6.2e10, which does not fit in the 32 bitsLguarantees; it becomes366LL. 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→LLchange alters the type of theDATE_LUT_ADDexpression on LP64 (long→long long, both 64 bits), so I verifiedclickhouse_common_iostill builds clean under-Weverything -Werror.Changelog category (leave one):
Documentation entry for user-facing changes
Version info
26.8.1.326(included in26.8and later)