Repository navigation
Make a dangling DataPartsAnyLock token a compile-time error - #121376
Conversation
DataPartsAnyLock is the proof token that 14 MergeTreeData members accept as the
caller's evidence that it holds data_parts_mutex. Both converting constructors
take the lock by const & and store nothing, which gives clang no reason to
connect the constructed token's lifetime to the lock's, so -Wdangling analysis
stops at the constructor boundary and this compiles with no diagnostic:
const DB::DataPartsAnyLock & token = data.readLockParts();
Guaranteed elision materialises the token directly (the deleted copy and move
constructors do not prevent it) and lifetime-extends it to the end of the
enclosing block, while the DataPartsSharedLock temporary it was built from is
destroyed at the semicolon. The token then certifies a lock that no longer
exists.
[[clang::lifetimebound]] on each constructor parameter declares the missing
connection, which turns that spelling into:
error: temporary bound to local reference 'token' will be destroyed at the
end of the full-expression [-Werror,-Wdangling]
No existing site has the dangling shape, so nothing else needed changing: every
use of the type outside its own definition is a const DataPartsAnyLock &
parameter. I measured both directions under clang-21 and the CI-pinned clang-22,
and the 24 src/ translation units that can construct a token stay clean under
both.
The two annotated constructors are the complete construction surface: the class
is final, deletes copy and move, and has no default constructor, factory or
friend. The attribute is diagnostic-only, with no effect on overload resolution,
mangling, codegen, ABI or runtime behaviour.
Leading attribute position is required because the parameters are unnamed. After
the reference the attribute appertains to the type rather than to the parameter
declaration, which clang rejects outright, and naming the parameters would need
[[maybe_unused]] under -Weverything -Werror.
Requested by @ azat in
ClickHouse#120093 (comment)
Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings), plus my own cold review
Severity: ❌ blocker / One addition to the description's reasoning: I re-ran the "no existing site has the dangling shape" Session id: cron:clickhouse-review-slot-11:20260921-142100 |
|
Workflow [PR], commit [b3d1cd9] Summary: ❌
AI ReviewSummaryThis PR turns Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit b3d1cd9.
Changed lines: Changed C/C++ lines covered: 2/2 (100.00%) · Uncovered code |
Build profile diff (arm_release)Commit See the job log for details. |
Build (wasm64) compiles with Emscripten 6.0.5, whose clang is 24.0.0git, and its
lifetime-safety analysis checks [[clang::lifetimebound]] instead of trusting it:
it looks for something in the constructed object that can refer to the annotated
parameter. The token stored nothing, so there was nothing to find, and every
translation unit including the header failed under -Werror:
src/Storages/MergeTree/MergeTreeData.h:153:24: error: could not verify that
the return value can be lifetime bound to an unnamed parameter
[-Werror,-Wlifetime-safety-lifetimebound-violation]
Keeping the address of the lock the token was built from makes the annotation a
claim the compiler can check, and the diagnostic the annotation exists for is
unchanged: binding a token to a reference that outlives its lock is still
rejected, now by -Wdangling and also by -Wlifetime-safety-use-after-scope. The
pointer is never dereferenced, so the token still carries no runtime meaning,
and [[maybe_unused]] keeps -Wunused-private-field quiet (I checked that the
warning does fire without it).
Measured in the container that job uses (clickhouse/binary-builder, em++ 6.0.5,
clang 24): the three objects it reported build clean and all 24 src/ translation
units that construct a token are clean, while the previous shape fails the same
three objects with the two errors above. Under the CI-pinned clang-22 the full
build is clean, clang-tidy is clean, and the dangling spelling is still an error.
Moving the constructor bodies out of the header does not help: the diagnostic
fires on the definition instead.
Suppressing -Wlifetime-safety-lifetimebound-violation in cmake/warnings.cmake,
next to the four lifetime-safety diagnostics already disabled there for clang 23
and newer, also goes green. I did not do that: the annotation genuinely was
unverifiable, and the suppression would hide the same mistake in every future
annotation.
Co-Authored-By: Claude Opus 5 <[email protected]>
Head branch was pushed to by a user without write access
|
b3d1cd9 keeps the address of the lock the token was built from, which makes the annotation checkable. The pointer is never dereferenced, so the token still has no runtime meaning, and the check you approved is unchanged: the dangling spelling is still rejected, now by The alternative was to suppress |
CI finish ledger - b3d1cd9Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
This PR changes one header, Session id: cron:our-pr-ci-monitor:20260922-030126 |
|
Fixing PR for both The previous release's own |
c911d10
Related: #120093
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Binding a
DataPartsAnyLocktoken to a reference that outlives its parts lock is now a compile-time error. No user-visible change.Description
DataPartsAnyLock(src/Storages/MergeTree/MergeTreeData.h) is the token that 14MergeTreeDatamembers accept as the caller's proof that it holdsdata_parts_mutex. Its two converting constructors take the lock byconst &, so the token's lifetime was not tied to the lock's, and this compiled silently:Guaranteed elision materialises the token directly and extends it to the end of the enclosing block, while the
DataPartsSharedLockit was built from is destroyed at the semicolon. The token then certifies a lock that no longer exists.[[clang::lifetimebound]]on each constructor parameter turns that spelling into:The token now also keeps the address of the lock it was built from. Emscripten's clang 24, which the
wasm64build uses, verifies such an annotation instead of trusting it, and rejects it while the token stores nothing. The pointer is never dereferenced, so the token still carries no runtime meaning.I measured both directions under clang-21, under the CI-pinned clang-22, and under clang 24 in the container the
wasm64job uses: the spelling above is clean without the attribute and an error with it, a full build is clean, and the 24src/translation units that can construct a token are clean.Nothing else needed changing: every mention of the type outside its own definition is a
const DataPartsAnyLock ¶meter, so a conversion can only happen in call-argument position, where the token and the lock are destroyed together.@ azat asked for this in #120093 (comment):
No test is added: the only effect is a compiler diagnostic, and the tree has no negative-compile harness to run one in.
Workflow [PR]
Sync PR [sync-upstream/pr/121376]
Version info
26.10.1.432-master(included in26.10and later)